wip: test signal
This commit is contained in:
58
daemon/process_unix_test.go
Normal file
58
daemon/process_unix_test.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package daemon
|
||||
|
||||
import (
|
||||
"os"
|
||||
"os/exec"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"golang.org/x/sys/unix"
|
||||
)
|
||||
|
||||
func TestWait(t *testing.T) {
|
||||
var (
|
||||
err error
|
||||
pid int
|
||||
testpid int
|
||||
ws unix.WaitStatus
|
||||
)
|
||||
|
||||
cmd := exec.Command("sleep", "10")
|
||||
if err = cmd.Start(); err != nil {
|
||||
t.Fatal("failed to start command:", err)
|
||||
}
|
||||
|
||||
pid = cmd.Process.Pid
|
||||
t.Log("started pid:", pid)
|
||||
go func() {
|
||||
time.Sleep(1200 * time.Millisecond)
|
||||
p, terr := os.FindProcess(pid)
|
||||
if terr == nil {
|
||||
if terr = p.Signal(unix.SIGTERM); terr != nil {
|
||||
t.Log("sending signal failed:", terr)
|
||||
}
|
||||
} else {
|
||||
t.Logf("find process %d: %v", pid, terr)
|
||||
}
|
||||
}()
|
||||
|
||||
testpid, err = unix.Wait4(-1, &ws, 0, nil)
|
||||
if err != nil {
|
||||
t.Fatal("wait4 error:", err)
|
||||
}
|
||||
|
||||
if ws.Signaled() {
|
||||
t.Log("the child process got signal:", ws.Signal())
|
||||
}
|
||||
|
||||
if ws.Exited() {
|
||||
t.Log("the child process exit with status:", ws.ExitStatus())
|
||||
}
|
||||
|
||||
// if err = cmd.Wait(); err != nil {
|
||||
// t.Fatal("is this expected?", err)
|
||||
// }
|
||||
|
||||
assert.Equal(t, pid, testpid)
|
||||
}
|
||||
Reference in New Issue
Block a user