2021-02-05 01:44:03 +00:00
|
|
|
// +build linux
|
2021-03-08 16:46:23 +00:00
|
|
|
|
2019-07-31 20:24:57 +00:00
|
|
|
package token
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestSignalHandler(t *testing.T) {
|
|
|
|
sigHandler := signalHandler{signals: []os.Signal{syscall.SIGUSR1}}
|
|
|
|
handlerRan := false
|
|
|
|
done := make(chan struct{})
|
|
|
|
timer := time.NewTimer(time.Second)
|
2021-03-23 14:30:43 +00:00
|
|
|
sigHandler.register(func() {
|
2019-07-31 20:24:57 +00:00
|
|
|
handlerRan = true
|
|
|
|
done <- struct{}{}
|
|
|
|
})
|
|
|
|
|
|
|
|
p, err := os.FindProcess(os.Getpid())
|
|
|
|
require.Nil(t, err)
|
|
|
|
p.Signal(syscall.SIGUSR1)
|
|
|
|
|
|
|
|
// Blocks for up to one second to make sure the handler callback runs before the assert.
|
|
|
|
select {
|
2021-03-23 14:30:43 +00:00
|
|
|
case <-done:
|
|
|
|
assert.True(t, handlerRan)
|
|
|
|
case <-timer.C:
|
|
|
|
t.Fail()
|
2019-07-31 20:24:57 +00:00
|
|
|
}
|
|
|
|
sigHandler.deregister()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSignalHandlerClose(t *testing.T) {
|
|
|
|
sigHandler := signalHandler{signals: []os.Signal{syscall.SIGUSR1}}
|
|
|
|
done := make(chan struct{})
|
|
|
|
timer := time.NewTimer(time.Second)
|
2021-03-23 14:30:43 +00:00
|
|
|
sigHandler.register(func() { done <- struct{}{} })
|
2019-07-31 20:24:57 +00:00
|
|
|
sigHandler.deregister()
|
|
|
|
|
|
|
|
p, err := os.FindProcess(os.Getpid())
|
|
|
|
require.Nil(t, err)
|
|
|
|
p.Signal(syscall.SIGUSR1)
|
|
|
|
select {
|
2021-03-23 14:30:43 +00:00
|
|
|
case <-done:
|
2019-07-31 20:24:57 +00:00
|
|
|
t.Fail()
|
2021-03-23 14:30:43 +00:00
|
|
|
case <-timer.C:
|
2019-07-31 20:24:57 +00:00
|
|
|
}
|
|
|
|
}
|