-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_test.go
More file actions
454 lines (370 loc) · 9.08 KB
/
app_test.go
File metadata and controls
454 lines (370 loc) · 9.08 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
package web
import (
"context"
"errors"
"io"
"log/slog"
"net/http"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/jacobbrewer1/web/logging"
)
func newTestApp(t *testing.T) *App {
t.Helper()
app, err := NewApp(logging.NewLoggerWithWriter(io.Discard))
require.NoError(t, err)
return app
}
func newTestServer(t *testing.T, addr string) *http.Server {
t.Helper()
return &http.Server{
Addr: addr,
ReadHeaderTimeout: httpReadHeaderTimeout,
}
}
func TestNewApp(t *testing.T) {
t.Parallel()
tests := []struct {
name string
logger *slog.Logger
wantErr bool
}{
{
name: "nil logger",
logger: nil,
wantErr: true,
},
{
name: "valid logger",
logger: logging.NewLoggerWithWriter(io.Discard),
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
app, err := NewApp(tt.logger)
if tt.wantErr {
require.Error(t, err)
require.Nil(t, app)
return
}
require.NoError(t, err)
require.NotNil(t, app)
require.NotNil(t, app.baseCtx)
require.NotNil(t, app.baseCtxCancel)
require.NotNil(t, app.shutdownWg)
require.True(t, app.metricsEnabled)
})
}
}
func TestApp_Shutdown(t *testing.T) {
t.Parallel()
t.Run("single shutdown", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
server := newTestServer(t, ":0")
err := app.StartServer("test", server)
require.NoError(t, err)
// Give the server time to start
time.Sleep(100 * time.Millisecond)
// Shutdown should complete without error
app.Shutdown()
})
t.Run("multiple shutdowns", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
// Call shutdown multiple times
done := make(chan struct{})
go func() {
app.Shutdown()
app.Shutdown()
app.Shutdown()
close(done)
}()
select {
case <-done:
// Success - multiple shutdowns completed
case <-time.After(2 * time.Second):
t.Fatal("multiple shutdowns timed out")
}
})
}
func TestApp_StartServer(t *testing.T) {
t.Parallel()
app := newTestApp(t)
svr1 := newTestServer(t, ":8080")
svr2 := newTestServer(t, ":8081")
err := app.StartServer("test1", svr1)
require.NoError(t, err)
err = app.StartServer("test2", svr2)
require.NoError(t, err)
// Try and start the same server again
err = app.StartServer("test1", svr1)
require.EqualError(t, err, "server test1 already exists")
}
func TestApp_ChildContext(t *testing.T) {
t.Parallel()
app := newTestApp(t)
ctx, cancel := app.ChildContext()
defer cancel()
require.NotNil(t, ctx)
require.NotNil(t, cancel)
// Verify context inheritance
app.baseCtxCancel()
select {
case <-ctx.Done():
// Success - child context was cancelled
case <-time.After(100 * time.Millisecond):
t.Fatal("child context not cancelled when parent cancelled")
}
}
func TestApp_WaitForEnd(t *testing.T) {
t.Parallel()
app := newTestApp(t)
callbackCalled := false
callback := func() {
callbackCalled = true
}
done := make(chan struct{})
go func() {
app.WaitForEnd(callback)
close(done)
}()
// Cancel the context after a short delay
time.Sleep(100 * time.Millisecond)
app.baseCtxCancel()
select {
case <-done:
require.True(t, callbackCalled, "callback should have been called")
case <-time.After(100 * time.Millisecond):
t.Fatal("WaitForEnd did not complete")
}
}
func TestApp_IsLeader(t *testing.T) {
t.Parallel()
app := newTestApp(t)
// When no leader election is configured
require.True(t, app.IsLeader(), "should be leader when no election configured")
}
func TestApp_Panics(t *testing.T) {
t.Parallel()
tests := []struct {
name string
testFunc func(*App)
panicMsg string
}{
{
name: "Logger",
testFunc: func(a *App) {
a.l = nil
a.Logger()
},
panicMsg: "logger has not been registered",
},
{
name: "VaultClient",
testFunc: func(a *App) {
a.VaultClient()
},
panicMsg: "vault client has not been registered",
},
{
name: "Viper",
testFunc: func(a *App) {
a.Viper()
},
panicMsg: "viper instance has not been registered",
},
{
name: "DBConn",
testFunc: func(a *App) {
a.DBConn()
},
panicMsg: "database connection has not been registered",
},
{
name: "KubeClient",
testFunc: func(a *App) {
a.KubeClient()
},
panicMsg: "kubernetes client has not been registered",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
require.PanicsWithValue(t, tt.panicMsg, func() {
tt.testFunc(app)
})
})
}
}
func TestApp_Start(t *testing.T) {
t.Parallel()
t.Run("successful start", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
err := app.Start()
require.NoError(t, err)
})
t.Run("multiple starts - options not executed", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
// First start
err := app.Start()
require.NoError(t, err)
// Second start with option
optionCalled := false
err = app.Start(func(a *App) error {
optionCalled = true
return nil
})
require.NoError(t, err)
require.False(t, optionCalled, "option should not be called on second start")
})
t.Run("multiple starts - error is maintained", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
// First start
err := app.Start(func(a *App) error {
return errors.New("error during startup")
})
require.EqualError(t, err, "failed to apply option: error during startup")
err = app.Start()
require.EqualError(t, err, "failed to apply option: error during startup")
})
t.Run("failing option aborts startup", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
called := false
err := app.Start(
func(a *App) error {
return errors.New("first error")
},
func(a *App) error {
called = true
return nil
},
)
require.Error(t, err)
require.Contains(t, err.Error(), "first error")
require.False(t, called, "subsequent options should not be called")
})
t.Run("with async tasks", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
called := make(chan struct{})
app.indefiniteAsyncTasks.Store("test", func(ctx context.Context) {
called <- struct{}{}
// Wait for app to complete
<-ctx.Done()
})
err := app.Start()
require.NoError(t, err)
app.waitUntilStarted()
// Wait for async task to be called or timeout after 3 seconds
select {
case <-called:
// Success - async task was called
case <-time.After(3 * time.Second):
t.Fatal("async task was not called in time")
}
})
t.Run("with async tasks bad func", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
app.indefiniteAsyncTasks.Store("test", func(ctx context.Context) error {
return nil
})
err := app.Start()
require.EqualError(t, err, "async task initialization error: failed to cast task function test to AsyncTaskFunc")
})
t.Run("async shutdown on error", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
app.servers.Store("test", newTestServer(t, ":8080"))
err := app.Start(
func(a *App) error {
return errors.New("error during startup")
},
)
require.EqualError(t, err, "failed to apply option: error during startup")
app.WaitForEnd(app.Shutdown)
})
}
func TestApp_WaitUntilStarted(t *testing.T) {
t.Parallel()
app := newTestApp(t)
done := make(chan struct{})
// Start the app in a goroutine
errChan := make(chan error, 1)
go func() {
errChan <- app.Start()
}()
err := <-errChan
require.NoError(t, err)
// Wait for startup in a separate goroutine
go func() {
app.waitUntilStarted()
close(done)
}()
// First wait should complete within timeout
select {
case <-done:
// Success - app started
case <-time.After(2 * time.Second):
t.Fatal("first waitUntilStarted call timed out")
}
// Second wait should return immediately
secondWaitDone := make(chan struct{})
go func() {
app.waitUntilStarted()
close(secondWaitDone)
}()
select {
case <-secondWaitDone:
// Success - second wait completed immediately
case <-time.After(100 * time.Millisecond):
t.Fatal("second waitUntilStarted call blocked")
}
}
func TestApp_StartAsyncTask(t *testing.T) {
t.Parallel()
t.Run("start_async_task_runs_successfully", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
taskCompleted := make(chan struct{})
app.startAsyncTask("test-task", false, func(ctx context.Context) {
close(taskCompleted)
})
select {
case <-taskCompleted:
// Success - task completed
case <-time.After(100 * time.Millisecond):
t.Fatal("task did not complete in time")
}
})
t.Run("start_async_task_indefinite_ends_unexpectedly_triggers_shutdown", func(t *testing.T) {
t.Parallel()
app := newTestApp(t)
shutdownTriggered := make(chan struct{})
// Override baseCtxCancel to detect shutdown
originalCancel := app.baseCtxCancel
app.baseCtxCancel = func() {
close(shutdownTriggered)
originalCancel()
}
app.startAsyncTask("indefinite-task", true, func(ctx context.Context) {
// Simulate unexpected end
})
select {
case <-shutdownTriggered:
// Success - shutdown triggered
case <-time.After(100 * time.Millisecond):
t.Fatal("shutdown was not triggered for indefinite task")
}
})
}