9 "github.com/stretchr/testify/assert"
12 func TestReceivesSignal(t *testing.T) {
13 ctx := UntilSignal(context.Background(), syscall.SIGUSR2)
14 assert.NoError(t, ctx.Err())
15 syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
17 assert.Equal(t, SignalError{syscall.SIGUSR2}, ctx.Err())
18 assert.EqualError(t, ctx.Err(),
19 "received signal: "+syscall.SIGUSR2.String())
22 func TestForwardsParent(t *testing.T) {
23 parent, _ := context.WithTimeout(
24 context.WithValue(context.Background(), t, "test"),
26 ctx := UntilSignal(parent, syscall.SIGUSR2)
27 assert.NoError(t, ctx.Err())
28 dl, ok := ctx.Deadline()
30 assert.WithinDuration(t, time.Now(), dl, time.Millisecond)
31 assert.EqualValues(t, "test", ctx.Value(t))
33 assert.Equal(t, context.DeadlineExceeded, ctx.Err())
36 func TestChildForwardsErr(t *testing.T) {
37 ctx := UntilSignal(context.Background(), syscall.SIGUSR2)
38 child, cancel := context.WithTimeout(ctx, time.Second)
39 syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
43 assert.Equal(t, SignalError{syscall.SIGUSR2}, ctx.Err())
44 assert.Equal(t, SignalError{syscall.SIGUSR2}, child.Err())
47 func TestSignalAfterCancel(t *testing.T) {
48 ctx := UntilSignal(context.Background(), syscall.SIGUSR2)
49 assert.NoError(t, ctx.Err())
52 assert.Equal(t, context.Canceled, ctx.Err())
53 syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
54 time.Sleep(5 * time.Millisecond)
55 assert.Equal(t, context.Canceled, ctx.Err())
58 func TestCancelAfterSignal(t *testing.T) {
59 ctx := UntilSignal(context.Background(), syscall.SIGUSR2)
60 assert.NoError(t, ctx.Err())
61 syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
63 assert.Equal(t, SignalError{syscall.SIGUSR2}, ctx.Err())
65 time.Sleep(5 * time.Millisecond)
66 assert.Equal(t, SignalError{syscall.SIGUSR2}, ctx.Err())
69 func TestImmediateCompletion(t *testing.T) {
70 parent, cancel := context.WithCancel(context.Background())
73 ctx := UntilSignal(parent, syscall.SIGUSR2)
74 // peek inside to be certain we never set up the signal channel.
77 case _, ok := <-ctx.Done():
78 assert.False(t, ok, "Done() should be closed")
80 assert.False(t, true, "context should be complete")
82 assert.Equal(t, context.Canceled, ctx.Err())
85 func BenchmarkReceivesSignal(b *testing.B) {
86 for i := 0; i < b.N; i++ {
87 ctx := UntilSignal(context.Background(), syscall.SIGUSR2)
88 syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
93 func BenchmarkCancelChildren(b *testing.B) {
94 children := make([]context.Context, b.N)
95 cancels := make([]context.CancelFunc, b.N)
97 ctx := UntilSignal(context.Background(), syscall.SIGUSR2)
98 for i := range children {
99 children[i], cancels[i] = context.WithTimeout(ctx, time.Hour)
101 syscall.Kill(syscall.Getpid(), syscall.SIGUSR2)
103 for i := range children {
107 for i := range cancels {
112 func BenchmarkCanceledAsChild(b *testing.B) {
113 children := make([]context.Context, b.N)
114 parent, cancel := context.WithCancel(context.Background())
116 for i := range children {
117 children[i] = UntilSignal(parent, syscall.SIGUSR2)
121 for i := range children {