-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeeper_test.go
162 lines (140 loc) · 3.89 KB
/
keeper_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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package shutdownKeeper
import (
"context"
"os"
"sync/atomic"
"syscall"
"testing"
"time"
)
func TestShutdownKeeper_SignalShutdown(t *testing.T) {
var actual int32
keeper := NewKeeper(KeeperOpts{
Signals: []os.Signal{syscall.SIGINT},
OnSignalShutdown: func(_ os.Signal) {
atomic.StoreInt32(&actual, 1)
},
})
go func() {
time.Sleep(50 * time.Millisecond)
keeper.signalChan <- syscall.SIGINT
}()
keeper.Wait()
actualVal := atomic.LoadInt32(&actual)
if actualVal != 1 {
t.Fatalf("expect: 1, actual: %d", actualVal)
}
}
func TestShutdownKeeper_ContextDownShutdown(t *testing.T) {
var actual int32
ctx, cancel := context.WithCancel(context.Background())
keeper := NewKeeper(KeeperOpts{
Context: ctx,
OnContextDone: func() {
atomic.StoreInt32(&actual, 1)
},
})
go func() {
time.Sleep(50 * time.Millisecond)
cancel()
}()
keeper.Wait()
actualVal := atomic.LoadInt32(&actual)
if actualVal != 1 {
t.Fatalf("expect: 1, actual: %d", actualVal)
}
}
func TestShutdownKeeper_HoldToken(t *testing.T) {
keeper := NewKeeper(KeeperOpts{})
var actual int32
go func(token HoldToken) {
defer token.Release()
atomic.AddInt32(&actual, 1)
}(keeper.AllocHoldToken())
go func(token HoldToken) {
defer token.Release()
atomic.AddInt32(&actual, 1)
}(keeper.AllocHoldToken())
keeper.Wait()
actualVal := atomic.LoadInt32(&actual)
if actualVal != 2 {
t.Fatalf("expect: 2, actual: %d", actualVal)
}
}
func TestShutdownKeeper_OnShuttingDown(t *testing.T) {
var actual int32
ctx, cancel := context.WithCancel(context.Background())
keeper := NewKeeper(KeeperOpts{
Context: ctx,
})
keeper.OnShuttingDown(func() {
atomic.StoreInt32(&actual, 1)
})
go func() {
time.Sleep(50 * time.Millisecond)
cancel()
}()
keeper.Wait()
actualVal := atomic.LoadInt32(&actual)
if actualVal != 1 {
t.Fatalf("expect: 1, actual: %d", actualVal)
}
}
func TestShutdownKeeper_WaitMultipleTimes(t *testing.T) {
keeper := NewKeeper(KeeperOpts{
MaxHoldTime: 2 * time.Second,
ForceHold: true,
})
token := keeper.AllocHoldToken()
go token.Release()
keeper.Wait()
keeper.maxHoldTime = 10 * time.Second
startTime := time.Now()
keeper.Wait()
actual := int(time.Now().Sub(startTime).Seconds())
if actual != 0 {
t.Fatalf("expect: 0, actual: %d", actual)
}
}
func TestShutdownKeeper_ForceHold(t *testing.T) {
keeper := NewKeeper(KeeperOpts{
ForceHold: true,
MaxHoldTime: 2 * time.Second,
})
token := keeper.AllocHoldToken()
go token.Release()
startTime := time.Now()
keeper.Wait()
actual := int(time.Now().Sub(startTime).Seconds())
if actual != 2 {
t.Fatalf("expect: 2, actual: %d", actual)
}
}
func TestShutdownKeeper_CornerCases(t *testing.T) {
// case 1
keeper := NewKeeper(KeeperOpts{})
token := keeper.AllocHoldToken()
token.Release()
keeper.Wait()
// case 2: allocate a token and never release it. so when the keeper receives a SIGINT signal, it will hold for MaxHoldTime before shutdown.
keeper = NewKeeper(KeeperOpts{
Signals: make([]os.Signal, syscall.SIGINT),
MaxHoldTime: time.Second,
})
token = keeper.AllocHoldToken()
go func() { keeper.signalChan <- syscall.SIGINT }()
keeper.Wait()
// case 3: call OnShuttingDown after shutdown does not work
keeper = NewKeeper(KeeperOpts{})
token = keeper.AllocHoldToken()
token.Release()
keeper.Wait()
var actual int32 = 0
keeper.OnShuttingDown(func() {
actual = 1
})
keeper.Wait()
if actual != 0 {
t.Fatalf("expect: 0, actual: %d", actual)
}
}