-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle_test.go
More file actions
539 lines (437 loc) · 14.9 KB
/
Copy pathlifecycle_test.go
File metadata and controls
539 lines (437 loc) · 14.9 KB
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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
package controls_test
import (
"context"
"fmt"
"log/slog"
"net/http"
"os"
"runtime"
"sync"
"sync/atomic"
"syscall"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gitlab.com/phpboyscout/go/errors"
"gitlab.com/phpboyscout/go/controls"
)
// newQuietController builds a controller with no signals and a noop logger,
// the standard setup for the lifecycle regression tests.
func newQuietController(t *testing.T, opts ...controls.ControllerOpt) *controls.Controller {
t.Helper()
base := []controls.ControllerOpt{
controls.WithLogger(slog.New(slog.DiscardHandler)),
}
base = append(base, opts...)
return controls.NewController(context.Background(), base...)
}
// TestErrorHandler_NoBusySpinAfterStop is the busy-spin regression. After Stop,
// the error/context handler goroutine must exit (not spin on a permanently-ready
// ctx.Done() case). We assert the goroutine count returns to baseline and stays
// bounded for an idle second.
func TestErrorHandler_NoBusySpinAfterStop(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
c := controls.NewController(ctx,
controls.WithLogger(slog.New(slog.DiscardHandler)),
)
c.Register("svc",
controls.WithStart(func(_ context.Context) error { return nil }),
controls.WithStop(func(_ context.Context) {}),
)
before := runtime.NumGoroutine()
c.Start()
// Trigger shutdown via context cancellation (exercises the ctx.Done() branch).
cancel()
require.Eventually(t, func() bool {
return c.GetState() == controls.Stopped
}, 5*time.Second, 10*time.Millisecond)
c.Wait()
// Allow goroutines to unwind, then confirm we have not leaked the handler
// goroutines and that the count is stable (not climbing from a spin spawning
// or churning work).
require.Eventually(t, func() bool {
return runtime.NumGoroutine() <= before+1
}, 5*time.Second, 20*time.Millisecond, "controller goroutines should terminate after Stop")
stable := runtime.NumGoroutine()
time.Sleep(500 * time.Millisecond)
assert.LessOrEqual(t, runtime.NumGoroutine(), stable+1,
"goroutine count should remain bounded for an idle second post-Stop")
}
// TestRestart_RunOutcomes exercises D1/D2: clean exit, context cancel, and error
// outcomes each produce the correct supervisor behaviour, and a service whose
// Start returns nil immediately (background-serving) is NOT restarted.
func TestRestart_RunOutcomes(t *testing.T) {
t.Parallel()
tests := []struct {
name string
// start returns this; if blockUntilCtx, Start blocks until ctx done then
// returns startErr.
startErr error
blockUntilCtx bool
policy *controls.RestartPolicy
// minStarts/maxStarts bound the number of Start invocations observed
// before we Stop the controller.
wantStarts int32
}{
{
name: "clean-start-not-restarted",
startErr: nil,
policy: &controls.RestartPolicy{MaxRestarts: 5, InitialBackoff: 5 * time.Millisecond},
wantStarts: 1, // returning nil is a clean start, never restarted
},
{
name: "context-cancel-not-restarted",
blockUntilCtx: true,
startErr: context.Canceled,
policy: &controls.RestartPolicy{MaxRestarts: 5, InitialBackoff: 5 * time.Millisecond},
wantStarts: 1, // ctx cancellation is shutdown, not a failure
},
{
name: "error-restarts-up-to-max",
startErr: fmt.Errorf("boom"),
policy: &controls.RestartPolicy{MaxRestarts: 3, InitialBackoff: 5 * time.Millisecond, MaxBackoff: 20 * time.Millisecond},
wantStarts: 4, // 1 initial + 3 restarts
},
}
for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
var starts atomic.Int32
// Collect everything sent on errs and assert no nil ever arrives.
errs := make(chan error, 64)
var sawNil atomic.Bool
c := newQuietController(t)
c.SetErrorsChannel(errs)
c.Register("svc",
controls.WithStart(func(ctx context.Context) error {
starts.Add(1)
if tc.blockUntilCtx {
<-ctx.Done()
}
return tc.startErr
}),
controls.WithStop(func(_ context.Context) {}),
controls.WithRestartPolicy(*tc.policy),
)
// Drain errs, flagging any nil.
go func() {
for e := range errs {
if e == nil {
sawNil.Store(true)
}
}
}()
c.Start()
switch tc.name {
case "error-restarts-up-to-max":
require.Eventually(t, func() bool {
return starts.Load() >= tc.wantStarts
}, 5*time.Second, 10*time.Millisecond)
default:
// Give the supervisor time to (wrongly) restart if buggy.
time.Sleep(150 * time.Millisecond)
}
c.Stop()
c.Wait()
close(errs)
assert.Equal(t, tc.wantStarts, starts.Load(), "unexpected number of Start invocations")
assert.False(t, sawNil.Load(), "nil must never be sent on the error channel")
})
}
}
// TestStart_Idempotent verifies D3: a double Start does not double-start services
// and Wait still returns.
func TestStart_Idempotent(t *testing.T) {
t.Parallel()
var starts atomic.Int32
c := newQuietController(t)
c.Register("svc",
controls.WithStart(func(_ context.Context) error { starts.Add(1); return nil }),
controls.WithStop(func(_ context.Context) {}),
)
c.Start()
c.Start() // second call must be a no-op
require.True(t, c.IsRunning())
// Wait for the single start to land.
require.Eventually(t, func() bool {
return starts.Load() == 1
}, 2*time.Second, 10*time.Millisecond)
// Give a buggy double-start a chance to manifest.
time.Sleep(100 * time.Millisecond)
assert.Equal(t, int32(1), starts.Load(), "service must start exactly once")
c.Stop()
done := make(chan struct{})
go func() { c.Wait(); close(done) }()
select {
case <-done:
case <-time.After(5 * time.Second):
t.Fatal("Wait() hung after idempotent Start")
}
}
// TestNilFuncs_NoPanic verifies D5: a service registered without Start/Stop does
// not panic at start or shutdown.
func TestNilFuncs_NoPanic(t *testing.T) {
t.Parallel()
c := newQuietController(t)
c.Register("no-funcs") // no WithStart / WithStop
require.NotPanics(t, func() {
c.Start()
c.Stop()
c.Wait()
})
assert.True(t, c.IsStopped())
}
// TestSignals_DefaultLeavesDisposition verifies spec 0001 D2: the controller
// installs no handler by default, so SIGINT/SIGTERM keep their default
// disposition and the outermost layer stays the sole signal owner.
func TestSignals_DefaultLeavesDisposition(t *testing.T) {
t.Parallel()
c := controls.NewController(context.Background(),
controls.WithLogger(slog.New(slog.DiscardHandler)),
)
assert.Nil(t, c.Signals(), "NewController must leave the signal channel nil")
}
// TestSignals_CustomChannelReceives verifies a custom channel via SetSignalsChannel
// actually drives shutdown.
func TestSignals_CustomChannelReceives(t *testing.T) {
t.Parallel()
sigs := make(chan os.Signal, 1)
c := controls.NewController(context.Background(),
controls.WithLogger(slog.New(slog.DiscardHandler)),
)
c.SetSignalsChannel(sigs)
c.Register("svc",
controls.WithStart(func(_ context.Context) error { return nil }),
controls.WithStop(func(_ context.Context) {}),
)
c.Start()
sigs <- syscall.SIGTERM
require.Eventually(t, func() bool {
return c.GetState() == controls.Stopped
}, 5*time.Second, 10*time.Millisecond, "custom signal channel should drive shutdown")
}
// TestForceStop_AbandonsContextIgnoringStop verifies D6: a StopFunc that ignores
// its context is abandoned at the shutdown deadline and Wait() returns.
func TestForceStop_AbandonsContextIgnoringStop(t *testing.T) {
t.Parallel()
stopEntered := make(chan struct{})
c := newQuietController(t, controls.WithShutdownTimeout(100*time.Millisecond))
c.Register("stubborn",
controls.WithStart(func(_ context.Context) error { return nil }),
controls.WithStop(func(_ context.Context) {
close(stopEntered)
// Ignore ctx entirely and block far longer than the shutdown timeout.
time.Sleep(10 * time.Second)
}),
)
c.Start()
c.Stop()
done := make(chan struct{})
go func() { c.Wait(); close(done) }()
select {
case <-stopEntered:
case <-time.After(2 * time.Second):
t.Fatal("stop func was never entered")
}
select {
case <-done:
// Wait returned despite the stubborn stop — force-stop worked.
case <-time.After(3 * time.Second):
t.Fatal("Wait() hung: context-ignoring StopFunc was not abandoned at deadline")
}
assert.True(t, c.IsStopped())
}
// TestStop_ReverseOrder verifies D6: services stop in reverse registration order.
func TestStop_ReverseOrder(t *testing.T) {
t.Parallel()
var order []string
var mu sync.Mutex
c := newQuietController(t)
for _, name := range []string{"first", "second", "third"} {
name := name
c.Register(name,
controls.WithStart(func(_ context.Context) error { return nil }),
controls.WithStop(func(_ context.Context) {
mu.Lock()
order = append(order, name)
mu.Unlock()
}),
)
}
c.Start()
c.Stop()
c.Wait()
mu.Lock()
defer mu.Unlock()
assert.Equal(t, []string{"third", "second", "first"}, order,
"services should stop in reverse registration order")
}
// TestReadiness_FailsClosedBeforeFirstAsyncCheck verifies D7: an async check
// reports not-ready until its first run completes.
func TestReadiness_FailsClosedBeforeFirstAsyncCheck(t *testing.T) {
t.Parallel()
release := make(chan struct{})
var ran atomic.Bool
c := newQuietController(t)
require.NoError(t, c.RegisterHealthCheck(controls.HealthCheck{
Name: "slow-async",
Check: func(_ context.Context) controls.CheckResult {
<-release // block the first run until the test releases it
ran.Store(true)
return controls.CheckResult{Status: controls.CheckHealthy}
},
Interval: 10 * time.Millisecond,
Type: controls.CheckTypeReadiness,
}))
c.Start()
t.Cleanup(func() {
close(release)
c.Stop()
c.Wait()
})
// Before the first async run completes, readiness must fail closed.
report := c.Readiness()
assert.False(t, report.OverallHealthy,
"readiness must report not-ready until the first async check completes")
assert.False(t, ran.Load())
}
// TestWithRestartResetInterval_ResetsCounter verifies D2: after running healthily
// for at least the reset interval, the consecutive-failure counter resets so the
// service is not prematurely declared as having exceeded MaxRestarts.
func TestWithRestartResetInterval_ResetsCounter(t *testing.T) {
t.Parallel()
var starts atomic.Int32
// Fail the first two runs quickly, then run "healthily" (block on ctx) so the
// reset interval elapses. With a tiny reset interval the counter resets and
// the service is never declared max-restarts-exceeded.
c := newQuietController(t)
c.Register("flaky",
controls.WithStart(func(ctx context.Context) error {
n := starts.Add(1)
if n <= 2 {
return fmt.Errorf("startup failure %d", n)
}
// Healthy run: block until shutdown.
<-ctx.Done()
return ctx.Err()
}),
controls.WithStop(func(_ context.Context) {}),
controls.WithRestartPolicy(controls.RestartPolicy{
MaxRestarts: 2,
InitialBackoff: 5 * time.Millisecond,
}),
controls.WithRestartResetInterval(1*time.Millisecond),
)
c.Start()
require.Eventually(t, func() bool {
return starts.Load() >= 3
}, 5*time.Second, 10*time.Millisecond, "service should reach a healthy run after transient failures")
info, ok := c.GetServiceInfo("flaky")
require.True(t, ok)
if info.Error != nil {
assert.NotContains(t, info.Error.Error(), "max restarts exceeded",
"counter should have reset during the healthy window")
}
c.Stop()
c.Wait()
}
// TestWithRestartResetInterval_ImpliesPolicy verifies that applying the reset
// interval to a service with no restart policy creates one (so the interval
// takes effect) without otherwise enabling restarts.
func TestWithRestartResetInterval_ImpliesPolicy(t *testing.T) {
t.Parallel()
var starts atomic.Int32
c := newQuietController(t)
c.Register("reset-only",
controls.WithStart(func(_ context.Context) error { starts.Add(1); return nil }),
controls.WithStop(func(_ context.Context) {}),
controls.WithRestartResetInterval(5*time.Millisecond),
)
c.Start()
require.Eventually(t, func() bool { return starts.Load() == 1 }, 2*time.Second, 10*time.Millisecond)
// A clean start is never restarted even though a (default) policy now exists.
time.Sleep(50 * time.Millisecond)
assert.Equal(t, int32(1), starts.Load())
c.Stop()
c.Wait()
}
// TestNilStop_ReachesShutdown ensures the no-op Stop default is invoked at
// shutdown for a service that registered a Start but no Stop.
func TestNilStop_ReachesShutdown(t *testing.T) {
t.Parallel()
var started atomic.Bool
c := newQuietController(t)
c.Register("start-only",
controls.WithStart(func(_ context.Context) error { started.Store(true); return nil }),
// No WithStop: defaults to a no-op that must be safe to call at shutdown.
)
c.Start()
require.Eventually(t, started.Load, 2*time.Second, 10*time.Millisecond)
require.NotPanics(t, func() {
c.Stop()
c.Wait()
})
assert.True(t, c.IsStopped())
}
// TestSignals_SecondSignalForcesHandlerExit verifies D4: a second signal arriving
// while shutdown is in progress forces the signal-handler goroutine to exit
// rather than block.
func TestSignals_SecondSignalForcesHandlerExit(t *testing.T) {
t.Parallel()
sigs := make(chan os.Signal, 2)
c := controls.NewController(context.Background(), controls.WithLogger(slog.New(slog.DiscardHandler)))
c.SetSignalsChannel(sigs)
// A stop that lingers so the second signal lands during shutdown.
c.Register("lingering",
controls.WithStart(func(_ context.Context) error { return nil }),
controls.WithStop(func(_ context.Context) { time.Sleep(50 * time.Millisecond) }),
)
c.Start()
sigs <- syscall.SIGINT // initiates graceful stop
sigs <- syscall.SIGTERM // second signal forces handler exit
require.Eventually(t, func() bool {
return c.GetState() == controls.Stopped
}, 5*time.Second, 10*time.Millisecond)
c.Wait()
}
// TestValidError_ExemptsExpectedTerminalErrors verifies D7/D2: an error matched by
// ValidErrorFunc does not count toward the restart total nor get forwarded as a
// failure.
func TestValidError_ExemptsExpectedTerminalErrors(t *testing.T) {
t.Parallel()
var starts atomic.Int32
errs := make(chan error, 16)
var forwarded atomic.Int32
c := newQuietController(t, controls.WithValidError(func(err error) bool {
return errors.Is(err, http.ErrServerClosed)
}))
c.SetErrorsChannel(errs)
c.Register("http-like",
controls.WithStart(func(_ context.Context) error {
starts.Add(1)
return http.ErrServerClosed
}),
controls.WithStop(func(_ context.Context) {}),
controls.WithRestartPolicy(controls.RestartPolicy{
MaxRestarts: 3,
InitialBackoff: 5 * time.Millisecond,
}),
)
go func() {
for range errs {
forwarded.Add(1)
}
}()
c.Start()
// http.ErrServerClosed is "valid" — must not restart.
time.Sleep(150 * time.Millisecond)
c.Stop()
c.Wait()
close(errs)
assert.Equal(t, int32(1), starts.Load(), "valid terminal error must not trigger a restart")
assert.Equal(t, int32(0), forwarded.Load(), "valid terminal error must not be forwarded as a failure")
}