1
0
Fork 0
mirror of https://github.com/moby/moby.git synced 2022-11-09 12:21:53 -05:00

Merge pull request #33451 from naveed-jamil-tenpearls/pkg/signal/signal

Add test coverage to pkg/signal
This commit is contained in:
Victor Vieux 2017-06-08 16:51:23 -07:00 committed by GitHub
commit 1a2d2f75f4
2 changed files with 91 additions and 0 deletions

View file

@ -0,0 +1,58 @@
// +build darwin linux solaris
package signal
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) {
signal, _ := SignalMap["HUP"]
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)
}

33
pkg/signal/signal_test.go Normal file
View file

@ -0,0 +1,33 @@
package signal
import (
"syscall"
"testing"
"github.com/stretchr/testify/assert"
)
func TestParseSignal(t *testing.T) {
_, checkAtoiError := ParseSignal("0")
assert.EqualError(t, checkAtoiError, "Invalid signal: 0")
_, error := ParseSignal("SIG")
assert.EqualError(t, error, "Invalid signal: SIG")
for sigStr := range SignalMap {
responseSignal, error := ParseSignal(sigStr)
assert.NoError(t, error)
signal := SignalMap[sigStr]
assert.EqualValues(t, signal, responseSignal)
}
}
func TestValidSignalForPlatform(t *testing.T) {
isValidSignal := ValidSignalForPlatform(syscall.Signal(0))
assert.EqualValues(t, false, isValidSignal)
for _, sigN := range SignalMap {
isValidSignal = ValidSignalForPlatform(syscall.Signal(sigN))
assert.EqualValues(t, true, isValidSignal)
}
}