summaryrefslogtreecommitdiff
path: root/pkg/process/process_test.go
blob: 496f5475d969af962ca6f81701013c2e1b802130 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package process

import (
	"fmt"
	"os"
	"os/exec"
	"runtime"
	"testing"
)

func TestAlive(t *testing.T) {
	for _, pid := range []int{0, -1, -123} {
		t.Run(fmt.Sprintf("invalid process (%d)", pid), func(t *testing.T) {
			if Alive(pid) {
				t.Errorf("PID %d should not be alive", pid)
			}
		})
	}
	t.Run("current process", func(t *testing.T) {
		if pid := os.Getpid(); !Alive(pid) {
			t.Errorf("current PID (%d) should be alive", pid)
		}
	})
	t.Run("exited process", func(t *testing.T) {
		if runtime.GOOS == "windows" {
			t.Skip("TODO: make this work on Windows")
		}

		// Get a PID of an exited process.
		cmd := exec.Command("echo", "hello world")
		err := cmd.Run()
		if err != nil {
			t.Fatal(err)
		}
		exitedPID := cmd.ProcessState.Pid()
		if Alive(exitedPID) {
			t.Errorf("PID %d should not be alive", exitedPID)
		}
	})
}