-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathapplication_test.go
485 lines (420 loc) · 9.04 KB
/
application_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
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
package gone_test
import (
"errors"
"reflect"
"sync"
"testing"
"time"
"github.com/gone-io/gone/v2"
)
// MockDaemon implements Daemon interface for testing
type MockDaemon struct {
gone.Flag
startCalled bool
stopCalled bool
startError error
stopError error
mu sync.Mutex
}
func (m *MockDaemon) Start() error {
m.mu.Lock()
defer m.mu.Unlock()
m.startCalled = true
return m.startError
}
func (m *MockDaemon) Stop() error {
m.mu.Lock()
defer m.mu.Unlock()
m.stopCalled = true
return m.stopError
}
func TestPreparer_Lifecycle(t *testing.T) {
var hooksCalled []string
var mu sync.Mutex
addHookCall := func(name string) {
mu.Lock()
defer mu.Unlock()
hooksCalled = append(hooksCalled, name)
}
preparer := gone.NewApp()
// Register hooks
preparer.BeforeStart(func() {
addHookCall("beforeStart")
})
preparer.AfterStart(func() {
addHookCall("afterStart")
})
preparer.BeforeStop(func() {
addHookCall("beforeStop")
})
preparer.AfterStop(func() {
addHookCall("afterStop")
})
// Add a test daemon
daemon := &MockDaemon{}
preparer.Load(daemon)
// Run in a goroutine and end after a short delay
go func() {
time.Sleep(100 * time.Millisecond)
preparer.End()
}()
preparer.Serve()
// Verify hook execution order
expectedOrder := []string{
"beforeStart",
"afterStart",
"beforeStop",
"afterStop",
}
if !reflect.DeepEqual(expectedOrder, hooksCalled) {
t.Errorf("Hooks called in wrong order. Expected %v, got %v", expectedOrder, hooksCalled)
}
// Verify daemon methods were called
if !daemon.startCalled {
t.Error("Daemon Start() was not called")
}
if !daemon.stopCalled {
t.Error("Daemon Stop() was not called")
}
}
func TestPreparer_DaemonErrors(t *testing.T) {
tests := []struct {
name string
daemon *MockDaemon
wantPanic bool
}{
{
name: "Start error",
daemon: &MockDaemon{
startError: errors.New("start error"),
},
wantPanic: true,
},
{
name: "Stop error",
daemon: &MockDaemon{
stopError: errors.New("stop error"),
},
wantPanic: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
preparer := gone.NewApp()
preparer.Load(tt.daemon)
if tt.wantPanic {
defer func() {
if r := recover(); r == nil {
t.Error("Expected panic but got none")
}
}()
go func() {
time.Sleep(100 * time.Millisecond)
preparer.End()
}()
preparer.Serve()
}
})
}
}
func TestPreparer_SignalHandling(t *testing.T) {
preparer := gone.NewApp()
// Test SIGINT
go func() {
time.Sleep(100 * time.Millisecond)
preparer.End()
}()
start := time.Now()
preparer.Serve()
duration := time.Since(start)
if duration >= 200*time.Millisecond {
t.Errorf("Signal handling took too long: %v", duration)
}
}
func TestPreparer_MultipleHooks(t *testing.T) {
preparer := gone.NewApp()
var counter int
var mu sync.Mutex
increment := func() {
mu.Lock()
defer mu.Unlock()
counter++
}
// Register multiple hooks for each phase
for i := 0; i < 3; i++ {
preparer.BeforeStart(increment)
preparer.AfterStart(increment)
preparer.BeforeStop(increment)
preparer.AfterStop(increment)
}
go func() {
time.Sleep(100 * time.Millisecond)
preparer.End()
}()
preparer.Serve()
if counter != 12 {
t.Errorf("Not all hooks were called, expected 12, got %d", counter)
}
}
func TestPreparer_LoadErrors(t *testing.T) {
tests := []struct {
name string
setup func(*gone.Application)
wantPanic bool
}{
{
name: "Duplicate named component",
setup: func(p *gone.Application) {
p.Load(&Worker{name: "test"})
p.Load(&Worker{name: "test"})
},
wantPanic: true,
},
{
name: "Valid components",
setup: func(p *gone.Application) {
p.Load(&Worker{name: "worker1"})
p.Load(&Worker{name: "worker2"})
},
wantPanic: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
preparer := gone.NewApp()
didPanic := false
func() {
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
tt.setup(preparer)
}()
if tt.wantPanic != didPanic {
t.Errorf("Test %s: wantPanic = %v, got panic = %v", tt.name, tt.wantPanic, didPanic)
}
})
}
}
func TestPreparer_RunWithDependencies(t *testing.T) {
preparer := gone.NewApp()
worker1 := &Worker{name: "worker1"}
worker2 := &Worker{name: "worker2"}
boss := &Boss{name: "boss"}
preparer.Load(worker1).
Load(worker2).
Load(boss)
var executed bool
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
preparer.Run(func(b *Boss) {
if b.name != "boss" {
t.Errorf("Expected boss name to be 'boss', got %s", b.name)
}
if b.first == nil {
t.Error("Expected first worker to not be nil")
}
if b.second == nil {
t.Error("Expected second worker to not be nil")
}
if len(b.workers) != 2 {
t.Errorf("Expected 2 workers, got %d", len(b.workers))
}
executed = true
})
}()
if !executed {
t.Error("Run function was not executed")
}
}
func TestPreparer_DefaultInstance(t *testing.T) {
if gone.Default == nil {
t.Error("Default preparer instance should not be nil")
}
worker := &Worker{name: "test"}
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
gone.Default.Load(worker)
}()
}
func TestPreparer_Loads(t *testing.T) {
preparer := gone.NewApp()
// Test successful loads
loadFn1 := func(core gone.Loader) error {
return core.Load(&Worker{name: "worker1"})
}
loadFn2 := func(core gone.Loader) error {
return core.Load(&Worker{name: "worker2"})
}
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
preparer.Loads(loadFn1, loadFn2)
}()
// Test load function that returns error
errorLoadFn := func(core gone.Loader) error {
return errors.New("load error")
}
didPanic := false
func() {
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
preparer.Loads(errorLoadFn)
}()
if !didPanic {
t.Error("Expected Loads to panic with error load function")
}
}
func TestPreparer_Test(t *testing.T) {
var testFuncCalled bool
testFunc := func(flag gone.TestFlag) {
if flag == nil {
t.Error("TestFlag should not be nil")
}
testFuncCalled = true
}
preparer := gone.NewApp()
preparer.Test(testFunc)
if !testFuncCalled {
t.Error("Test function was not called")
}
}
func TestPreparer_GlobalFunctions(t *testing.T) {
// Test global Load function
worker := &Worker{name: "global-worker"}
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
gone.Load(worker)
}()
// Test global Loads function
loadFn := func(core gone.Loader) error {
return core.Load(&Worker{name: "global-worker2"})
}
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
gone.Loads(loadFn)
}()
// Test global Run function
var runCalled bool
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
gone.Run(func() {
runCalled = true
})
}()
if !runCalled {
t.Error("Global Run function did not execute")
}
// Test global Test function
var testCalled bool
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
gone.Test(func(flag gone.TestFlag) {
if flag == nil {
t.Error("TestFlag should not be nil")
}
testCalled = true
})
}()
if !testCalled {
t.Error("Global Test function did not execute")
}
}
func TestPreparer_RunTest(t *testing.T) {
var testFuncCalled bool
testFunc := func(flag gone.TestFlag) {
if flag == nil {
t.Error("TestFlag should not be nil")
}
testFuncCalled = true
}
loadFn := func(core gone.Loader) error {
return core.Load(&Worker{name: "test-worker"})
}
gone.RunTest(testFunc, loadFn)
if !testFuncCalled {
t.Error("RunTest function was not called")
}
}
func TestPreparer_PrepareWithLoads(t *testing.T) {
loadFn1 := func(core gone.Loader) error {
return core.Load(&Worker{name: "worker1"})
}
loadFn2 := func(core gone.Loader) error {
return core.Load(&Worker{name: "worker2"})
}
var preparer *gone.Application
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
preparer = gone.NewApp(loadFn1, loadFn2)
}()
if preparer == nil {
t.Error("Application should not be nil")
}
// Test prepare with error load function
errorLoadFn := func(core gone.Loader) error {
return errors.New("load error")
}
didPanic := false
func() {
defer func() {
if r := recover(); r != nil {
didPanic = true
}
}()
gone.NewApp(errorLoadFn)
}()
if !didPanic {
t.Error("Expected NewApp to panic with error load function")
}
}
func TestPreparer_ServeGlobal(t *testing.T) {
// Test global Serve function
go func() {
time.Sleep(100 * time.Millisecond)
gone.Default.End()
}()
func() {
defer func() {
if r := recover(); r != nil {
t.Errorf("Unexpected panic: %v", r)
}
}()
gone.Serve()
}()
}