-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlifecycle_helpers_test.go
More file actions
176 lines (149 loc) · 5.58 KB
/
lifecycle_helpers_test.go
File metadata and controls
176 lines (149 loc) · 5.58 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
package forge
import (
"context"
"testing"
"time"
"github.com/xraph/forge/internal/logger"
)
func TestOnStarted(t *testing.T) {
lm := NewLifecycleManager(logger.NewTestLogger())
app := newMockAppWithLifecycle(lm)
called := false
err := OnStarted(app, "test-started", func(_ context.Context, _ App) error {
called = true
return nil
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
hooks := lm.GetHooks(PhaseAfterStart)
if len(hooks) != 1 {
t.Fatalf("expected 1 hook, got %d", len(hooks))
}
if hooks[0].Name != "test-started" {
t.Errorf("expected hook name 'test-started', got '%s'", hooks[0].Name)
}
// Execute to verify function runs
_ = lm.ExecuteHooks(context.Background(), PhaseAfterStart, app)
if !called {
t.Error("OnStarted hook was not called")
}
}
func TestOnClose(t *testing.T) {
lm := NewLifecycleManager(logger.NewTestLogger())
app := newMockAppWithLifecycle(lm)
called := false
err := OnClose(app, "test-close", func(_ context.Context, _ App) error {
called = true
return nil
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
hooks := lm.GetHooks(PhaseBeforeStop)
if len(hooks) != 1 {
t.Fatalf("expected 1 hook, got %d", len(hooks))
}
if hooks[0].Name != "test-close" {
t.Errorf("expected hook name 'test-close', got '%s'", hooks[0].Name)
}
_ = lm.ExecuteHooks(context.Background(), PhaseBeforeStop, app)
if !called {
t.Error("OnClose hook was not called")
}
}
func TestOnBeforeRun(t *testing.T) {
lm := NewLifecycleManager(logger.NewTestLogger())
app := newMockAppWithLifecycle(lm)
err := OnBeforeRun(app, "test-before-run", func(_ context.Context, _ App) error {
return nil
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
hooks := lm.GetHooks(PhaseBeforeRun)
if len(hooks) != 1 {
t.Fatalf("expected 1 hook at PhaseBeforeRun, got %d", len(hooks))
}
if hooks[0].Name != "test-before-run" {
t.Errorf("expected 'test-before-run', got '%s'", hooks[0].Name)
}
}
func TestOnAfterRun(t *testing.T) {
lm := NewLifecycleManager(logger.NewTestLogger())
app := newMockAppWithLifecycle(lm)
err := OnAfterRun(app, "test-after-run", func(_ context.Context, _ App) error {
return nil
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
hooks := lm.GetHooks(PhaseAfterRun)
if len(hooks) != 1 {
t.Fatalf("expected 1 hook at PhaseAfterRun, got %d", len(hooks))
}
}
func TestOnAfterRegister(t *testing.T) {
lm := NewLifecycleManager(logger.NewTestLogger())
app := newMockAppWithLifecycle(lm)
err := OnAfterRegister(app, "test-after-register", func(_ context.Context, _ App) error {
return nil
})
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
hooks := lm.GetHooks(PhaseAfterRegister)
if len(hooks) != 1 {
t.Fatalf("expected 1 hook at PhaseAfterRegister, got %d", len(hooks))
}
}
func TestLifecycleHelpers_DuplicateNameError(t *testing.T) {
lm := NewLifecycleManager(logger.NewTestLogger())
app := newMockAppWithLifecycle(lm)
hook := func(_ context.Context, _ App) error { return nil }
err := OnStarted(app, "dup", hook)
if err != nil {
t.Fatalf("first registration failed: %v", err)
}
err = OnStarted(app, "dup", hook)
if err == nil {
t.Fatal("expected error for duplicate hook name")
}
}
// mockAppForLifecycle is a minimal mock that delegates RegisterHookFn to a LifecycleManager.
type mockAppForLifecycle struct {
lm LifecycleManager
}
func newMockAppWithLifecycle(lm LifecycleManager) *mockAppForLifecycle {
return &mockAppForLifecycle{lm: lm}
}
func (m *mockAppForLifecycle) RegisterHookFn(phase LifecyclePhase, name string, hook LifecycleHook) error {
return m.lm.RegisterHookFn(phase, name, hook)
}
// Implement the rest of App as no-ops to satisfy the interface.
func (m *mockAppForLifecycle) Container() Container { return nil }
func (m *mockAppForLifecycle) Router() Router { return nil }
func (m *mockAppForLifecycle) Config() ConfigManager { return nil }
func (m *mockAppForLifecycle) Logger() Logger { return NewNoopLogger() }
func (m *mockAppForLifecycle) Metrics() Metrics { return nil }
func (m *mockAppForLifecycle) HealthManager() HealthManager { return nil }
func (m *mockAppForLifecycle) LifecycleManager() LifecycleManager { return m.lm }
func (m *mockAppForLifecycle) Start(_ context.Context) error { return nil }
func (m *mockAppForLifecycle) Stop(_ context.Context) error { return nil }
func (m *mockAppForLifecycle) Run() error { return nil }
func (m *mockAppForLifecycle) RegisterService(_ string, _ Factory, _ ...RegisterOption) error {
return nil
}
func (m *mockAppForLifecycle) RegisterController(_ Controller) error { return nil }
func (m *mockAppForLifecycle) RegisterExtension(_ Extension) error { return nil }
func (m *mockAppForLifecycle) RegisterHook(_ LifecyclePhase, _ LifecycleHook, _ LifecycleHookOptions) error {
return nil
}
func (m *mockAppForLifecycle) Name() string { return "test-app" }
func (m *mockAppForLifecycle) Version() string { return "1.0.0" }
func (m *mockAppForLifecycle) Environment() string { return "test" }
func (m *mockAppForLifecycle) StartTime() time.Time { return time.Time{} }
func (m *mockAppForLifecycle) Uptime() time.Duration { return 0 }
func (m *mockAppForLifecycle) Extensions() []Extension { return nil }
func (m *mockAppForLifecycle) GetExtension(_ string) (Extension, error) { return nil, nil }
func (m *mockAppForLifecycle) MigrationsDisabled() bool { return false }