github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/signal/signal_linux_test.go (about)

     1  // +build darwin linux
     2  
     3  package signal // import "github.com/demonoid81/moby/pkg/signal"
     4  
     5  import (
     6  	"os"
     7  	"syscall"
     8  	"testing"
     9  
    10  	"gotest.tools/v3/assert"
    11  	is "gotest.tools/v3/assert/cmp"
    12  )
    13  
    14  func TestCatchAll(t *testing.T) {
    15  	sigs := make(chan os.Signal, 1)
    16  	CatchAll(sigs)
    17  	defer StopCatch(sigs)
    18  
    19  	listOfSignals := map[string]string{
    20  		"CONT": syscall.SIGCONT.String(),
    21  		"HUP":  syscall.SIGHUP.String(),
    22  		"CHLD": syscall.SIGCHLD.String(),
    23  		"ILL":  syscall.SIGILL.String(),
    24  		"FPE":  syscall.SIGFPE.String(),
    25  		"CLD":  syscall.SIGCLD.String(),
    26  	}
    27  
    28  	for sigStr := range listOfSignals {
    29  		if signal, ok := SignalMap[sigStr]; ok {
    30  			syscall.Kill(syscall.Getpid(), signal)
    31  			s := <-sigs
    32  			assert.Check(t, is.Equal(s.String(), signal.String()))
    33  		}
    34  	}
    35  }
    36  
    37  func TestStopCatch(t *testing.T) {
    38  	signal := SignalMap["HUP"]
    39  	channel := make(chan os.Signal, 1)
    40  	CatchAll(channel)
    41  	syscall.Kill(syscall.Getpid(), signal)
    42  	signalString := <-channel
    43  	assert.Check(t, is.Equal(signalString.String(), signal.String()))
    44  
    45  	StopCatch(channel)
    46  	_, ok := <-channel
    47  	assert.Check(t, is.Equal(ok, false))
    48  }