-
Notifications
You must be signed in to change notification settings - Fork 1
/
signals_test.go
88 lines (65 loc) · 2.25 KB
/
signals_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package lu
import (
"context"
"syscall"
"testing"
"time"
"github.com/luno/jettison/errors"
"github.com/luno/jettison/jtest"
"github.com/stretchr/testify/assert"
)
func TestAppContext_QuitOnlyEndsTheAppContext(t *testing.T) {
ac := NewAppContext(context.Background())
t.Cleanup(ac.Stop)
ac.signals <- syscall.SIGQUIT
assert.Eventually(t, func() bool {
return errors.Is(ac.AppContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
assert.Never(t, func() bool {
return errors.Is(ac.TerminationContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
}
func TestAppContext_IntEndsBothContexts(t *testing.T) {
ac := NewAppContext(context.Background())
t.Cleanup(ac.Stop)
ac.signals <- syscall.SIGINT
assert.Eventually(t, func() bool {
return errors.Is(ac.AppContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
assert.Eventually(t, func() bool {
return errors.Is(ac.TerminationContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
}
func TestAppContext_QuitThenTerminate(t *testing.T) {
// This is the sequence of signals we will receive in kubernetes (when using the stop script)
ac := NewAppContext(context.Background())
t.Cleanup(ac.Stop)
ac.signals <- syscall.SIGQUIT
assert.Eventually(t, func() bool {
return errors.Is(ac.AppContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
jtest.AssertNil(t, ac.TerminationContext.Err())
ac.signals <- syscall.SIGTERM
assert.Eventually(t, func() bool {
return errors.Is(ac.TerminationContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
}
func TestAppContext_TerminateEndsEverything(t *testing.T) {
ac := NewAppContext(context.Background())
t.Cleanup(ac.Stop)
ac.signals <- syscall.SIGTERM
assert.Eventually(t, func() bool {
return errors.Is(ac.AppContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
assert.Eventually(t, func() bool {
return errors.Is(ac.TerminationContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
}
func TestAppContext_CancelledContext(t *testing.T) {
ac := NewAppContext(context.Background())
t.Cleanup(ac.Stop)
ac.appCancel()
assert.Eventually(t, func() bool {
return errors.Is(ac.AppContext.Err(), context.Canceled)
}, time.Second, time.Millisecond)
}