-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraceful_test.go
332 lines (276 loc) · 7.41 KB
/
graceful_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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package graceful
import (
"context"
"errors"
"os"
"os/signal"
"sync"
"syscall"
"testing"
"time"
)
func TestNewManager(t *testing.T) {
timeout := 5 * time.Second
manager := NewManager(timeout)
if manager.timeout != timeout {
t.Errorf("Expected timeout %v, got %v", timeout, manager.timeout)
}
if len(manager.components) != 0 {
t.Errorf("Expected empty components slice, got %d components", len(manager.components))
}
if len(manager.signals) != 2 {
t.Errorf("Expected 2 default signals, got %d", len(manager.signals))
}
}
func TestRegister(t *testing.T) {
manager := NewManager(time.Second)
shutdownFunc := func(ctx context.Context) error { return nil }
manager.Register("test-component", shutdownFunc, 1)
if len(manager.components) != 1 {
t.Errorf("Expected 1 component, got %d", len(manager.components))
}
component := manager.components[0]
if component.name != "test-component" {
t.Errorf("Expected name 'test-component', got '%s'", component.name)
}
if component.order != 1 {
t.Errorf("Expected order 1, got %d", component.order)
}
}
func TestWithSignals(t *testing.T) {
manager := NewManager(time.Second)
customSignals := []os.Signal{syscall.SIGUSR1, syscall.SIGUSR2}
manager = manager.WithSignals(customSignals...)
if len(manager.signals) != len(customSignals) {
t.Errorf("Expected %d signals, got %d", len(customSignals), len(manager.signals))
}
}
func TestShutdownOrder(t *testing.T) {
// Create manager with test timeout
manager := NewManager(5 * time.Second)
// Create signal channel
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, syscall.SIGTERM)
defer signal.Stop(sigChan)
var mu sync.Mutex
shutdownOrder := make([]string, 0)
// Register components in non-sequential order
manager.Register("third", func(ctx context.Context) error {
mu.Lock()
defer mu.Unlock()
shutdownOrder = append(shutdownOrder, "third")
return nil
}, 3)
manager.Register("first", func(ctx context.Context) error {
mu.Lock()
defer mu.Unlock()
shutdownOrder = append(shutdownOrder, "first")
return nil
}, 1)
manager.Register("second", func(ctx context.Context) error {
mu.Lock()
defer mu.Unlock()
shutdownOrder = append(shutdownOrder, "second")
return nil
}, 2)
// Create a done channel
done := make(chan struct{})
// Start the manager in a goroutine
go func() {
manager.Start()
close(done)
}()
// Give the manager time to set up
time.Sleep(100 * time.Millisecond)
// Send termination signal
p, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatal(err)
}
err = p.Signal(syscall.SIGTERM)
if err != nil {
t.Fatal(err)
}
// Wait for shutdown to complete
select {
case <-done:
// Success - continue with verification
case <-time.After(2 * time.Second):
t.Fatal("Shutdown did not complete within timeout")
}
// Verify shutdown order
expected := []string{"first", "second", "third"}
mu.Lock()
defer mu.Unlock()
if len(shutdownOrder) != len(expected) {
t.Errorf("Expected %d shutdowns, got %d. Order: %v",
len(expected), len(shutdownOrder), shutdownOrder)
}
for i, name := range expected {
if i >= len(shutdownOrder) || shutdownOrder[i] != name {
t.Errorf("Expected shutdown %d to be %s, got %s. Full order: %v",
i, name, shutdownOrder[i], shutdownOrder)
}
}
}
func TestShutdownTimeout(t *testing.T) {
timeout := 2 * time.Second
manager := NewManager(timeout)
timeoutOccurred := make(chan bool, 1)
// Register a component that takes longer than the timeout
manager.Register("slow-component", func(ctx context.Context) error {
select {
case <-ctx.Done():
timeoutOccurred <- true
t.Log("slow-component: context done")
return ctx.Err()
case <-time.After(timeout * 2):
timeoutOccurred <- false
t.Log("slow-component: completed without timeout")
return nil
}
}, 1)
// Create done channel
done := make(chan struct{})
// Start manager in a goroutine
go func() {
t.Log("Starting manager")
manager.Start()
t.Log("Manager stopped")
close(done)
}()
// Give the manager time to set up
time.Sleep(100 * time.Millisecond)
// Send termination signal
p, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatal(err)
}
t.Log("Sending SIGTERM signal")
err = p.Signal(syscall.SIGTERM)
if err != nil {
t.Fatal(err)
}
// Wait for shutdown to complete
select {
case <-done:
t.Log("Shutdown completed")
select {
case didTimeout := <-timeoutOccurred:
if !didTimeout {
t.Error("Expected timeout to occur but it didn't")
} else {
t.Log("Timeout occurred as expected")
}
case <-time.After(timeout * 3):
t.Error("Didn't receive timeout status")
}
case <-time.After(timeout * 4):
t.Error("Shutdown did not complete within expected timeframe")
}
}
func TestParallelShutdown(t *testing.T) {
manager := NewManager(10 * time.Second) // Increased timeout
var mu sync.Mutex
startTimes := make([]time.Time, 0, 3)
endTimes := make([]time.Time, 0, 3)
// Register multiple components with the same order
for i := 0; i < 3; i++ {
manager.Register("component", func(ctx context.Context) error {
mu.Lock()
startTimes = append(startTimes, time.Now())
mu.Unlock()
time.Sleep(100 * time.Millisecond)
mu.Lock()
endTimes = append(endTimes, time.Now())
mu.Unlock()
return nil
}, 1)
}
// Create done channel
done := make(chan struct{})
// Start manager in a goroutine
go func() {
t.Log("Starting manager")
manager.Start()
t.Log("Manager stopped")
close(done)
}()
// Give the manager time to set up
time.Sleep(100 * time.Millisecond)
// Send termination signal
p, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatal(err)
}
t.Log("Sending SIGTERM signal")
err = p.Signal(syscall.SIGTERM)
if err != nil {
t.Fatal(err)
}
// Wait for shutdown to complete
select {
case <-done:
t.Log("Shutdown completed")
case <-time.After(10 * time.Second): // Increased timeout
t.Fatal("Shutdown timeout")
}
mu.Lock()
defer mu.Unlock()
// Verify components shut down in parallel
if len(startTimes) != 3 || len(endTimes) != 3 {
t.Fatal("Expected 3 start and end times")
}
// Check that the components started at roughly the same time
for i := 1; i < len(startTimes); i++ {
diff := startTimes[i].Sub(startTimes[0])
if diff > 50*time.Millisecond {
t.Errorf("Components did not start in parallel: time difference %v", diff)
}
}
}
func TestErrorHandling(t *testing.T) {
manager := NewManager(time.Second)
expectedError := errors.New("shutdown error")
errorChan := make(chan error, 1)
manager.Register("error-component", func(ctx context.Context) error {
errorChan <- expectedError
return expectedError
}, 1)
// Create done channel
done := make(chan struct{})
// Start manager in a goroutine
go func() {
t.Log("Starting manager")
manager.Start()
t.Log("Manager stopped")
close(done)
}()
// Give the manager time to set up
time.Sleep(100 * time.Millisecond)
// Send termination signal
p, err := os.FindProcess(os.Getpid())
if err != nil {
t.Fatal(err)
}
t.Log("Sending SIGTERM signal")
err = p.Signal(syscall.SIGTERM)
if err != nil {
t.Fatal(err)
}
// Wait for shutdown to complete
select {
case <-done:
t.Log("Shutdown completed")
select {
case err := <-errorChan:
if err != expectedError {
t.Errorf("Expected error %v, got %v", expectedError, err)
}
case <-time.After(time.Second):
t.Error("Did not receive expected error")
}
case <-time.After(2 * time.Second):
t.Error("Shutdown did not complete after error")
}
}