Add test coverage to pkg/signal

Signed-off-by: Naveed Jamil <naveed.jamil@tenpearls.com>
This commit is contained in:
Naveed Jamil 2017-05-31 21:40:41 +05:00
parent 316681cd2a
commit 4b0df45c1a
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)
}
}