-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle_test.go
More file actions
514 lines (395 loc) · 12.5 KB
/
lifecycle_test.go
File metadata and controls
514 lines (395 loc) · 12.5 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
package forge
import (
"context"
"errors"
"sync"
"testing"
"time"
"github.com/xraph/forge/internal/logger"
)
func TestLifecycleManager_RegisterHook(t *testing.T) {
logger := logger.NewTestLogger()
lm := NewLifecycleManager(logger)
// Test successful registration
hook := func(ctx context.Context, app App) error {
return nil
}
opts := DefaultLifecycleHookOptions("test-hook")
err := lm.RegisterHook(PhaseBeforeStart, hook, opts)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// Test duplicate registration
err = lm.RegisterHook(PhaseBeforeStart, hook, opts)
if err == nil {
t.Fatal("expected error for duplicate hook name")
}
// Test nil hook
err = lm.RegisterHook(PhaseBeforeStart, nil, opts)
if err == nil {
t.Fatal("expected error for nil hook")
}
// Test empty name
err = lm.RegisterHook(PhaseBeforeStart, hook, LifecycleHookOptions{})
if err == nil {
t.Fatal("expected error for empty hook name")
}
}
func TestLifecycleManager_RegisterHookFn(t *testing.T) {
logger := logger.NewTestLogger()
lm := NewLifecycleManager(logger)
hook := func(ctx context.Context, app App) error {
return nil
}
err := lm.RegisterHookFn(PhaseBeforeStart, "test-hook", hook)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
hooks := lm.GetHooks(PhaseBeforeStart)
if len(hooks) != 1 {
t.Fatalf("expected 1 hook, got %d", len(hooks))
}
if hooks[0].Name != "test-hook" {
t.Errorf("expected hook name 'test-hook', got %s", hooks[0].Name)
}
}
func TestLifecycleManager_ExecuteHooks(t *testing.T) {
logger := logger.NewTestLogger()
lm := NewLifecycleManager(logger)
// Track execution order
var mu sync.Mutex
executed := []string{}
// Register hooks with different priorities
hook1 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook1")
return nil
}
hook2 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook2")
return nil
}
hook3 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook3")
return nil
}
// Register with different priorities (higher priority runs first)
opts1 := DefaultLifecycleHookOptions("hook1")
opts1.Priority = 10
opts2 := DefaultLifecycleHookOptions("hook2")
opts2.Priority = 50 // Should run first
opts3 := DefaultLifecycleHookOptions("hook3")
opts3.Priority = 30
_ = lm.RegisterHook(PhaseBeforeStart, hook1, opts1)
_ = lm.RegisterHook(PhaseBeforeStart, hook2, opts2)
_ = lm.RegisterHook(PhaseBeforeStart, hook3, opts3)
// Execute hooks
err := lm.ExecuteHooks(context.Background(), PhaseBeforeStart, nil)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// Verify execution order (hook2, hook3, hook1)
if len(executed) != 3 {
t.Fatalf("expected 3 executions, got %d", len(executed))
}
if executed[0] != "hook2" {
t.Errorf("expected hook2 first, got %s", executed[0])
}
if executed[1] != "hook3" {
t.Errorf("expected hook3 second, got %s", executed[1])
}
if executed[2] != "hook1" {
t.Errorf("expected hook1 third, got %s", executed[2])
}
}
func TestLifecycleManager_ExecuteHooks_Error(t *testing.T) {
logger := logger.NewTestLogger()
lm := NewLifecycleManager(logger)
// Track execution
var mu sync.Mutex
executed := []string{}
hook1 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook1")
return nil
}
hook2 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook2")
return errors.New("hook2 failed")
}
hook3 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook3")
return nil
}
// Register hooks (hook2 will fail)
opts1 := DefaultLifecycleHookOptions("hook1")
opts1.Priority = 30
opts2 := DefaultLifecycleHookOptions("hook2")
opts2.Priority = 20
opts3 := DefaultLifecycleHookOptions("hook3")
opts3.Priority = 10
_ = lm.RegisterHook(PhaseBeforeStart, hook1, opts1)
_ = lm.RegisterHook(PhaseBeforeStart, hook2, opts2)
_ = lm.RegisterHook(PhaseBeforeStart, hook3, opts3)
// Execute hooks - should fail on hook2
err := lm.ExecuteHooks(context.Background(), PhaseBeforeStart, nil)
if err == nil {
t.Fatal("expected error from hook2")
}
// Verify hook3 was not executed (stopped on error)
if len(executed) != 2 {
t.Fatalf("expected 2 executions, got %d", len(executed))
}
if executed[0] != "hook1" {
t.Errorf("expected hook1 first, got %s", executed[0])
}
if executed[1] != "hook2" {
t.Errorf("expected hook2 second, got %s", executed[1])
}
}
func TestLifecycleManager_ExecuteHooks_ContinueOnError(t *testing.T) {
logger := logger.NewTestLogger()
lm := NewLifecycleManager(logger)
// Track execution
var mu sync.Mutex
executed := []string{}
hook1 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook1")
return nil
}
hook2 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook2")
return errors.New("hook2 failed")
}
hook3 := func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, "hook3")
return nil
}
// Register hooks (hook2 will fail but continue)
opts1 := DefaultLifecycleHookOptions("hook1")
opts1.Priority = 30
opts2 := DefaultLifecycleHookOptions("hook2")
opts2.Priority = 20
opts2.ContinueOnError = true // Continue execution even on error
opts3 := DefaultLifecycleHookOptions("hook3")
opts3.Priority = 10
_ = lm.RegisterHook(PhaseBeforeStart, hook1, opts1)
_ = lm.RegisterHook(PhaseBeforeStart, hook2, opts2)
_ = lm.RegisterHook(PhaseBeforeStart, hook3, opts3)
// Execute hooks - should continue despite hook2 error
err := lm.ExecuteHooks(context.Background(), PhaseBeforeStart, nil)
if err == nil {
t.Fatal("expected error from hook2")
}
// Verify all hooks executed (continued despite error)
if len(executed) != 3 {
t.Fatalf("expected 3 executions, got %d", len(executed))
}
if executed[0] != "hook1" {
t.Errorf("expected hook1 first, got %s", executed[0])
}
if executed[1] != "hook2" {
t.Errorf("expected hook2 second, got %s", executed[1])
}
if executed[2] != "hook3" {
t.Errorf("expected hook3 third, got %s", executed[2])
}
}
func TestLifecycleManager_RemoveHook(t *testing.T) {
logger := logger.NewTestLogger()
lm := NewLifecycleManager(logger)
hook := func(ctx context.Context, app App) error {
return nil
}
// Register hook
opts := DefaultLifecycleHookOptions("test-hook")
_ = lm.RegisterHook(PhaseBeforeStart, hook, opts)
// Verify it's registered
hooks := lm.GetHooks(PhaseBeforeStart)
if len(hooks) != 1 {
t.Fatalf("expected 1 hook, got %d", len(hooks))
}
// Remove hook
err := lm.RemoveHook(PhaseBeforeStart, "test-hook")
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// Verify it's removed
hooks = lm.GetHooks(PhaseBeforeStart)
if len(hooks) != 0 {
t.Fatalf("expected 0 hooks, got %d", len(hooks))
}
// Try to remove non-existent hook
err = lm.RemoveHook(PhaseBeforeStart, "non-existent")
if err == nil {
t.Fatal("expected error for non-existent hook")
}
}
func TestLifecycleManager_ClearHooks(t *testing.T) {
logger := logger.NewTestLogger()
lm := NewLifecycleManager(logger)
hook := func(ctx context.Context, app App) error {
return nil
}
// Register multiple hooks
_ = lm.RegisterHookFn(PhaseBeforeStart, "hook1", hook)
_ = lm.RegisterHookFn(PhaseBeforeStart, "hook2", hook)
_ = lm.RegisterHookFn(PhaseBeforeStart, "hook3", hook)
// Verify they're registered
hooks := lm.GetHooks(PhaseBeforeStart)
if len(hooks) != 3 {
t.Fatalf("expected 3 hooks, got %d", len(hooks))
}
// Clear all hooks
lm.ClearHooks(PhaseBeforeStart)
// Verify they're cleared
hooks = lm.GetHooks(PhaseBeforeStart)
if len(hooks) != 0 {
t.Fatalf("expected 0 hooks, got %d", len(hooks))
}
}
func TestLifecycleManager_MultiplePhases(t *testing.T) {
logger := logger.NewTestLogger()
lm := NewLifecycleManager(logger)
// Track execution per phase
var mu sync.Mutex
executed := make(map[LifecyclePhase][]string)
createHook := func(phase LifecyclePhase, name string) LifecycleHook {
return func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed[phase] = append(executed[phase], name)
return nil
}
}
// Register hooks for different phases
_ = lm.RegisterHookFn(PhaseBeforeStart, "before-start", createHook(PhaseBeforeStart, "before-start"))
_ = lm.RegisterHookFn(PhaseAfterStart, "after-start", createHook(PhaseAfterStart, "after-start"))
_ = lm.RegisterHookFn(PhaseBeforeRun, "before-run", createHook(PhaseBeforeRun, "before-run"))
_ = lm.RegisterHookFn(PhaseAfterRun, "after-run", createHook(PhaseAfterRun, "after-run"))
// Execute each phase
_ = lm.ExecuteHooks(context.Background(), PhaseBeforeStart, nil)
_ = lm.ExecuteHooks(context.Background(), PhaseAfterStart, nil)
_ = lm.ExecuteHooks(context.Background(), PhaseBeforeRun, nil)
_ = lm.ExecuteHooks(context.Background(), PhaseAfterRun, nil)
// Verify each phase executed correctly
if len(executed[PhaseBeforeStart]) != 1 || executed[PhaseBeforeStart][0] != "before-start" {
t.Error("PhaseBeforeStart hook not executed correctly")
}
if len(executed[PhaseAfterStart]) != 1 || executed[PhaseAfterStart][0] != "after-start" {
t.Error("PhaseAfterStart hook not executed correctly")
}
if len(executed[PhaseBeforeRun]) != 1 || executed[PhaseBeforeRun][0] != "before-run" {
t.Error("PhaseBeforeRun hook not executed correctly")
}
if len(executed[PhaseAfterRun]) != 1 || executed[PhaseAfterRun][0] != "after-run" {
t.Error("PhaseAfterRun hook not executed correctly")
}
}
func TestApp_LifecycleHooks_Integration(t *testing.T) {
// Track execution order
var mu sync.Mutex
executed := []string{}
createHook := func(name string) LifecycleHook {
return func(ctx context.Context, app App) error {
mu.Lock()
defer mu.Unlock()
executed = append(executed, name)
return nil
}
}
// Create app
config := DefaultAppConfig()
config.Logger = logger.NewTestLogger()
config.Metrics = NewNoOpMetrics()
config.Name = "lifecycle-test"
config.HTTPAddress = ":9999" // Use different port to avoid conflicts
app := NewApp(config)
// Register hooks for all phases
_ = app.RegisterHookFn(PhaseBeforeStart, "before-start", createHook("before-start"))
_ = app.RegisterHookFn(PhaseAfterRegister, "after-register", createHook("after-register"))
_ = app.RegisterHookFn(PhaseAfterStart, "after-start", createHook("after-start"))
_ = app.RegisterHookFn(PhaseBeforeStop, "before-stop", createHook("before-stop"))
_ = app.RegisterHookFn(PhaseAfterStop, "after-stop", createHook("after-stop"))
// Start and stop app
ctx := context.Background()
if err := app.Start(ctx); err != nil {
t.Fatalf("failed to start app: %v", err)
}
// Give it a moment to fully start
time.Sleep(100 * time.Millisecond)
if err := app.Stop(ctx); err != nil {
t.Fatalf("failed to stop app: %v", err)
}
// Verify execution order
expectedOrder := []string{
"before-start",
"after-register",
"after-start",
"before-stop",
"after-stop",
}
if len(executed) != len(expectedOrder) {
t.Fatalf("expected %d executions, got %d: %v", len(expectedOrder), len(executed), executed)
}
for i, expected := range expectedOrder {
if executed[i] != expected {
t.Errorf("position %d: expected %s, got %s", i, expected, executed[i])
}
}
}
func TestApp_RegisterHook_Methods(t *testing.T) {
config := DefaultAppConfig()
config.Logger = logger.NewTestLogger()
config.Metrics = NewNoOpMetrics()
app := NewApp(config)
hookCalled := false
hook := func(ctx context.Context, app App) error {
hookCalled = true
return nil
}
// Test RegisterHookFn
err := app.RegisterHookFn(PhaseBeforeStart, "test-hook", hook)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// Test RegisterHook with custom options
opts := DefaultLifecycleHookOptions("test-hook-2")
opts.Priority = 100
opts.ContinueOnError = true
err = app.RegisterHook(PhaseBeforeStart, hook, opts)
if err != nil {
t.Fatalf("expected no error, got %v", err)
}
// Verify hooks are registered
hooks := app.LifecycleManager().GetHooks(PhaseBeforeStart)
if len(hooks) != 2 {
t.Fatalf("expected 2 hooks, got %d", len(hooks))
}
// Start app to trigger hooks
ctx := context.Background()
if err := app.Start(ctx); err != nil {
t.Fatalf("failed to start app: %v", err)
}
if !hookCalled {
t.Error("hook was not called")
}
// Cleanup
_ = app.Stop(ctx)
}