wip: test signal

This commit is contained in:
2023-08-31 08:55:48 +10:00
parent 92f7873f98
commit eac78ec322
4 changed files with 80 additions and 6 deletions

View 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)
}