Skip to content

Commit

Permalink
add TestSupervisor_crashRecovery
Browse files Browse the repository at this point in the history
  • Loading branch information
Songmu committed Feb 12, 2017
1 parent ae62675 commit 288de1c
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
40 changes: 40 additions & 0 deletions supervisor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,43 @@ func TestSupervisor_launchFailed(t *testing.T) {
t.Errorf("child process isn't terminated")
}
}

func TestSupervisor_crashRecovery(t *testing.T) {
origSpawnInterval := spawnInterval
spawnInterval = 300 * time.Millisecond
defer func() { spawnInterval = origSpawnInterval }()

sv := &supervisor{
prog: stubAgent,
argv: []string{"blah blah blah"},
}
sv.start()
ch := make(chan os.Signal)
go sv.handleSignal(ch)
done := make(chan error)
go func() {
done <- sv.wait()
}()
oldPid := sv.cmd.Process.Pid
if !existsPid(oldPid) {
t.Errorf("process doesn't exist")
}
time.Sleep(spawnInterval)

// let it crash
sv.cmd.Process.Signal(syscall.SIGUSR1)

time.Sleep(spawnInterval)
newPid := sv.cmd.Process.Pid
if oldPid == newPid {
t.Errorf("crash recovery failed")
}
if existsPid(oldPid) {
t.Errorf("old process isn't terminated")
}
if !existsPid(newPid) {
t.Errorf("new process doesn't exist")
}
ch <- syscall.SIGTERM
<-done
}
9 changes: 6 additions & 3 deletions testdata/stub-agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,15 @@ func main() {
}

ch := make(chan os.Signal)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
signal.Notify(ch, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP, syscall.SIGUSR1)
go func() {
for sig := range ch {
if sig == syscall.SIGHUP {
switch sig {
case syscall.SIGHUP:
// nop
} else {
case syscall.SIGUSR1:
panic("[stub] panicked by SIGUSR1")
default:
os.Exit(0)
}
}
Expand Down

0 comments on commit 288de1c

Please sign in to comment.