2018-07-18 02:47:02 +00:00
|
|
|
package signals
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"syscall"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func ExampleWithSignals() {
|
|
|
|
ctx := WithSignals(context.Background(), syscall.SIGUSR1)
|
|
|
|
go func() {
|
|
|
|
time.Sleep(500 * time.Millisecond) // after some time SIGUSR1 is sent
|
|
|
|
// mimicking a signal from the outside
|
|
|
|
syscall.Kill(syscall.Getpid(), syscall.SIGUSR1)
|
|
|
|
}()
|
|
|
|
|
2018-11-01 17:26:18 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
fmt.Println("finished")
|
2018-07-18 02:47:02 +00:00
|
|
|
// Output:
|
|
|
|
// finished
|
|
|
|
}
|
|
|
|
|
2018-07-19 14:28:59 +00:00
|
|
|
func Example_withUnregisteredSignals() {
|
2018-07-18 14:51:50 +00:00
|
|
|
dctx, cancel := context.WithTimeout(context.TODO(), time.Millisecond*100)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
ctx := WithSignals(dctx, syscall.SIGUSR1)
|
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Millisecond) // after some time SIGUSR2 is sent
|
|
|
|
// mimicking a signal from the outside, WithSignals will not handle it
|
|
|
|
syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
|
|
|
|
}()
|
|
|
|
|
2018-11-01 17:26:18 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
fmt.Println("finished")
|
2018-07-18 14:51:50 +00:00
|
|
|
// Output:
|
|
|
|
// finished
|
|
|
|
}
|
|
|
|
|
2018-07-18 02:47:02 +00:00
|
|
|
func TestWithSignals(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
ctx context.Context
|
|
|
|
sigs []os.Signal
|
|
|
|
wantSignal bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "sending signal SIGUSR2 should exit context.",
|
|
|
|
ctx: context.Background(),
|
|
|
|
sigs: []os.Signal{syscall.SIGUSR2},
|
|
|
|
wantSignal: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "sending signal SIGUSR2 should NOT exit context.",
|
|
|
|
ctx: context.Background(),
|
|
|
|
sigs: []os.Signal{syscall.SIGUSR1},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
ctx := WithSignals(tt.ctx, tt.sigs...)
|
|
|
|
syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
|
|
|
|
timer := time.NewTimer(500 * time.Millisecond)
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
if !tt.wantSignal {
|
|
|
|
t.Errorf("unexpected exit with signal")
|
|
|
|
}
|
|
|
|
case <-timer.C:
|
|
|
|
if tt.wantSignal {
|
|
|
|
t.Errorf("expected to exit with signal but did not")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|