2017-11-01 19:37:53 -04:00
|
|
|
// +build darwin linux
|
2017-05-31 12:40:41 -04:00
|
|
|
|
2018-02-05 16:05:59 -05:00
|
|
|
package signal // import "github.com/docker/docker/pkg/signal"
|
2017-05-31 12:40:41 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestCatchAll(t *testing.T) {
|
|
|
|
sigs := make(chan os.Signal, 1)
|
|
|
|
CatchAll(sigs)
|
|
|
|
defer StopCatch(sigs)
|
|
|
|
|
|
|
|
listOfSignals := map[string]string{
|
|
|
|
"CONT": syscall.SIGCONT.String(),
|
|
|
|
"HUP": syscall.SIGHUP.String(),
|
|
|
|
"CHLD": syscall.SIGCHLD.String(),
|
|
|
|
"ILL": syscall.SIGILL.String(),
|
|
|
|
"FPE": syscall.SIGFPE.String(),
|
|
|
|
"CLD": syscall.SIGCLD.String(),
|
|
|
|
}
|
|
|
|
|
|
|
|
for sigStr := range listOfSignals {
|
|
|
|
signal, ok := SignalMap[sigStr]
|
|
|
|
if ok {
|
|
|
|
go func() {
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
syscall.Kill(syscall.Getpid(), signal)
|
|
|
|
}()
|
|
|
|
|
|
|
|
s := <-sigs
|
|
|
|
assert.EqualValues(t, s.String(), signal.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStopCatch(t *testing.T) {
|
2017-09-11 14:55:05 -04:00
|
|
|
signal := SignalMap["HUP"]
|
2017-05-31 12:40:41 -04:00
|
|
|
channel := make(chan os.Signal, 1)
|
|
|
|
CatchAll(channel)
|
|
|
|
go func() {
|
|
|
|
|
|
|
|
time.Sleep(1 * time.Millisecond)
|
|
|
|
syscall.Kill(syscall.Getpid(), signal)
|
|
|
|
}()
|
|
|
|
signalString := <-channel
|
|
|
|
assert.EqualValues(t, signalString.String(), signal.String())
|
|
|
|
|
|
|
|
StopCatch(channel)
|
|
|
|
_, ok := <-channel
|
|
|
|
assert.EqualValues(t, ok, false)
|
|
|
|
}
|