github.com/rawahars/moby@v24.0.4+incompatible/pkg/process/process_test.go (about)

     1  package process
     2  
     3  import (
     4  	"fmt"
     5  	"os"
     6  	"os/exec"
     7  	"runtime"
     8  	"testing"
     9  )
    10  
    11  func TestAlive(t *testing.T) {
    12  	for _, pid := range []int{0, -1, -123} {
    13  		t.Run(fmt.Sprintf("invalid process (%d)", pid), func(t *testing.T) {
    14  			if Alive(pid) {
    15  				t.Errorf("PID %d should not be alive", pid)
    16  			}
    17  		})
    18  	}
    19  	t.Run("current process", func(t *testing.T) {
    20  		if pid := os.Getpid(); !Alive(pid) {
    21  			t.Errorf("current PID (%d) should be alive", pid)
    22  		}
    23  	})
    24  	t.Run("exited process", func(t *testing.T) {
    25  		if runtime.GOOS == "windows" {
    26  			t.Skip("TODO: make this work on Windows")
    27  		}
    28  
    29  		// Get a PID of an exited process.
    30  		cmd := exec.Command("echo", "hello world")
    31  		err := cmd.Run()
    32  		if err != nil {
    33  			t.Fatal(err)
    34  		}
    35  		exitedPID := cmd.ProcessState.Pid()
    36  		if Alive(exitedPID) {
    37  			t.Errorf("PID %d should not be alive", exitedPID)
    38  		}
    39  	})
    40  }