-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshutdown_test.go
393 lines (315 loc) · 9.79 KB
/
shutdown_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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
package coda_test
import (
"context"
"errors"
"slices"
"sync"
"testing"
"time"
"github.com/marnixbouhuis/coda"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// testLogger is a coda logger that automatically fails the test if an error is logged.
type testLogger struct {
t *testing.T
expectedErrors []string
}
var _ coda.Logger = &testLogger{}
func newTestLogger(t *testing.T, expectedErrors []string) *testLogger {
t.Helper()
return &testLogger{
t: t,
expectedErrors: expectedErrors,
}
}
func (l *testLogger) Info(str string) {
l.t.Helper()
l.t.Log("INFO:", str)
}
func (l *testLogger) Error(str string) {
l.t.Helper()
l.t.Log("ERROR:", str)
if !slices.Contains(l.expectedErrors, str) {
l.t.Log("Error is unexpected")
l.t.Fail()
}
}
func TestShutdown(t *testing.T) {
t.Parallel()
t.Run("it should block on Wait until Stop is called when no groups are present", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
done := make(chan struct{})
go func() {
err := s.Wait()
assert.NoError(t, err)
close(done)
}()
select {
case <-done:
assert.Fail(t, "Wait should block until Stop is called")
case <-time.After(time.Second):
// Expected behavior - Wait is blocking
}
s.Stop()
select {
case <-done:
// Expected behavior - Wait returned after Stop
case <-time.After(time.Second):
assert.Fail(t, "Wait should return after Stop is called")
}
})
t.Run("it should properly shut down a single group", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
g, err := s.NewGroup("test", nil)
require.NoError(t, err)
var stopped bool
g.Go(func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
stopped = true
return nil
})
s.Stop()
require.NoError(t, s.Wait())
assert.True(t, stopped, "group should have been stopped")
})
t.Run("it should properly shut down multiple groups with no dependencies", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
g1, err := s.NewGroup("group1", nil)
require.NoError(t, err)
g2, err := s.NewGroup("group2", nil)
require.NoError(t, err)
var stopped1, stopped2 bool
g1.Go(func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
stopped1 = true
return nil
})
g2.Go(func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
stopped2 = true
return nil
})
s.Stop()
require.NoError(t, s.Wait())
assert.True(t, stopped1, "group1 should have been stopped")
assert.True(t, stopped2, "group2 should have been stopped")
})
t.Run("it should properly shut down groups in dependency order", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
g1, err := s.NewGroup("group1", nil)
require.NoError(t, err)
g2, err := s.NewGroup("group2", []*coda.Group{g1})
require.NoError(t, err)
g3, err := s.NewGroup("group3", []*coda.Group{g2})
require.NoError(t, err)
var order []string
g1.Go(func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
order = append(order, "group1")
return nil
})
g2.Go(func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
order = append(order, "group2")
return nil
})
g3.Go(func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
order = append(order, "group3")
return nil
})
s.Stop()
require.NoError(t, s.Wait())
assert.Equal(t, []string{"group3", "group2", "group1"}, order)
})
t.Run("it should properly handle complex dependency chains during shutdown", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
// Create a more complex dependency graph:
// g1 <- g2 <- g4
// <- g3 <-/
g1, err := s.NewGroup("g1", nil)
require.NoError(t, err)
g2, err := s.NewGroup("g2", []*coda.Group{g1})
require.NoError(t, err)
g3, err := s.NewGroup("g3", []*coda.Group{g1})
require.NoError(t, err)
g4, err := s.NewGroup("g4", []*coda.Group{g2, g3})
require.NoError(t, err)
var order []string
var orderLock sync.Mutex
groupFunc := func(name string) coda.GroupFunc {
return func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
orderLock.Lock()
order = append(order, name)
orderLock.Unlock()
return nil
}
}
g1.Go(groupFunc("g1"))
g2.Go(groupFunc("g2"))
g3.Go(groupFunc("g3"))
g4.Go(groupFunc("g4"))
s.Stop()
require.NoError(t, s.Wait())
// g4 should stop first, then g2 and g3 (order between them doesn't matter), then g1
assert.Equal(t, "g4", order[0], "g4 should stop first")
assert.Contains(t, order[1:3], "g2", "g2 should stop after g4")
assert.Contains(t, order[1:3], "g3", "g3 should stop after g4")
assert.Equal(t, "g1", order[3], "g1 should stop last")
})
t.Run("it should support multiple calls to Wait", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{
"Stopping with error: test error",
})))
testErr := errors.New("test error")
// Start multiple goroutines waiting on shutdown
var wg sync.WaitGroup
wg.Add(3)
for range 3 {
go func() {
defer wg.Done()
err := s.Wait()
assert.ErrorIs(t, err, testErr)
}()
}
s.StopWithError(testErr)
// Wait for all goroutines to complete
done := make(chan struct{})
go func() {
wg.Wait()
close(done)
}()
select {
case <-done:
// Expected - all Wait calls returned
case <-time.After(time.Second):
assert.Fail(t, "Wait calls did not all return in time")
}
})
t.Run("Wait should return immediately after shutdown has been stopped", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
s.Stop()
done := make(chan struct{})
go func() {
assert.NoError(t, s.Wait())
close(done)
}()
select {
case <-done:
// Expected behavior - Wait returned immediately
case <-time.After(time.Second):
assert.Fail(t, "Wait should return immediately after shutdown is stopped")
}
})
t.Run("multiple calls to Stop should be supported", func(t *testing.T) {
t.Parallel()
t.Run("with no errors", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
// Call Stop multiple times
for range 3 {
s.Stop()
}
require.NoError(t, s.Wait())
})
t.Run("with multiple errors", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{
"Stopping with error: error 1",
"Stopping with error: error 2",
"Stopping with error: error 3",
})))
err1 := errors.New("error 1")
err2 := errors.New("error 2")
err3 := errors.New("error 3")
s.StopWithError(err1)
s.StopWithError(err2)
s.StopWithError(err3)
err := s.Wait()
require.ErrorIs(t, err, err1)
require.ErrorIs(t, err, err2)
require.ErrorIs(t, err, err3)
})
t.Run("with mixed Stop and StopWithError calls", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{
"Stopping with error: error 1",
"Stopping with error: error 2",
})))
err1 := errors.New("error 1")
err2 := errors.New("error 2")
s.Stop() // No error
s.StopWithError(err1) // With error
s.Stop() // No error
s.StopWithError(err2) // With error
s.Stop() // No error
err := s.Wait()
require.ErrorIs(t, err, err1)
require.ErrorIs(t, err, err2)
})
})
t.Run("it should error when adding a dependency from another shutdown manager", func(t *testing.T) {
t.Parallel()
s1 := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
s2 := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
g1, err := s1.NewGroup("group1", nil)
require.NoError(t, err)
_, err = s2.NewGroup("group2", []*coda.Group{g1})
require.ErrorIs(t, err, coda.ErrInvalidDependency)
assert.Contains(t, err.Error(), "dependency \"group1\" is not part of this shutdown instance")
})
t.Run("it should error when adding duplicate dependencies", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
g1, err := s.NewGroup("group1", nil)
require.NoError(t, err)
_, err = s.NewGroup("group2", []*coda.Group{g1, g1})
require.ErrorIs(t, err, coda.ErrInvalidDependency)
require.Contains(t, err.Error(), "duplicate group \"group1\" in dependency list")
})
t.Run("it should error when adding a new group to a stopped shutdown handler", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{})))
s.Stop()
require.NoError(t, s.Wait())
_, err := s.NewGroup("group1", nil)
require.ErrorIs(t, err, coda.ErrAlreadyStopped)
})
t.Run("it should respect group shutdown timeout when shutting down", func(t *testing.T) {
t.Parallel()
s := coda.NewShutdown(coda.WithShutdownLogger(newTestLogger(t, []string{
"Timed-out while waiting for group \"test\" to stop, continuing without waiting...",
})))
g, err := s.NewGroup("test", nil, coda.WithGroupShutdownTimeout(100*time.Millisecond))
require.NoError(t, err)
g.Go(func(ctx context.Context, ready func()) error {
ready()
<-ctx.Done()
// Sleep longer than the shutdown timeout
time.Sleep(10 * time.Second)
return nil
})
start := time.Now()
s.Stop()
require.NoError(t, s.Wait())
duration := time.Since(start)
// Verify shutdown completed within expected timeout (with some extra margin)
assert.Less(t, duration, 200*time.Millisecond, "shutdown took longer than timeout")
})
}