-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathreload_test.go
More file actions
320 lines (256 loc) · 6.94 KB
/
reload_test.go
File metadata and controls
320 lines (256 loc) · 6.94 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
// Copyright 2025 The Rivaas Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//go:build !integration
package app
import (
"context"
"errors"
"net/http"
"sync"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestOnReload_Sequential(t *testing.T) {
t.Parallel()
app, err := New(
WithServiceName("test-reload-sequential"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
var executionOrder []int
var mu sync.Mutex
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
executionOrder = append(executionOrder, 1)
mu.Unlock()
return nil
}))
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
executionOrder = append(executionOrder, 2)
mu.Unlock()
return nil
}))
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
executionOrder = append(executionOrder, 3)
mu.Unlock()
return nil
}))
ctx := context.Background()
err = app.Reload(ctx)
require.NoError(t, err)
mu.Lock()
defer mu.Unlock()
assert.Equal(t, []int{1, 2, 3}, executionOrder, "hooks should execute in registration order")
}
func TestOnReload_StopsOnError(t *testing.T) {
t.Parallel()
app, err := New(
WithServiceName("test-reload-stops-on-error"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
var executed []int
var mu sync.Mutex
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
executed = append(executed, 1)
mu.Unlock()
return nil
}))
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
executed = append(executed, 2)
mu.Unlock()
return errors.New("hook 2 failed")
}))
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
executed = append(executed, 3)
mu.Unlock()
return nil
}))
ctx := context.Background()
err = app.Reload(ctx)
require.Error(t, err)
assert.Contains(t, err.Error(), "OnReload hook 1 failed")
assert.Contains(t, err.Error(), "hook 2 failed")
mu.Lock()
defer mu.Unlock()
assert.Equal(t, []int{1, 2}, executed, "subsequent hooks should not execute after error")
}
func TestOnReload_ServerContinuesOnError(t *testing.T) {
t.Parallel()
app, err := New(
WithServiceName("test-reload-server-continues"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
require.NoError(t, app.OnReload(func(ctx context.Context) error {
return errors.New("reload failed")
}))
app.GET("/test", func(c *Context) {
_ = c.String(http.StatusOK, "ok") //nolint:errcheck // Test code
})
// Simulate reload failure
ctx := context.Background()
err = app.Reload(ctx)
require.Error(t, err)
// Server should still be able to handle requests
resp, err := app.TestJSON(context.Background(), http.MethodGet, "/test", nil)
require.NoError(t, err)
defer func() { _ = resp.Body.Close() }() //nolint:errcheck // Test code
assert.Equal(t, http.StatusOK, resp.StatusCode, "server should continue serving after reload error")
}
func TestOnReload_ReturnsErrorIfFrozen(t *testing.T) {
t.Parallel()
app, err := New(
WithServiceName("test-reload-returns-error-frozen"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
// Freeze the router
app.router.Freeze()
// Registering OnReload after freeze should return error
err = app.OnReload(func(ctx context.Context) error {
return nil
})
require.Error(t, err)
assert.True(t, errors.Is(err, ErrRouterFrozen), "expected ErrRouterFrozen")
}
func TestReload_Serialized(t *testing.T) {
t.Parallel()
app, err := New(
WithServiceName("test-reload-serialized"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
var activeReloads int32
var maxConcurrent int32
var mu sync.Mutex
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
activeReloads++
if activeReloads > maxConcurrent {
maxConcurrent = activeReloads
}
mu.Unlock()
// Simulate some work
time.Sleep(50 * time.Millisecond)
mu.Lock()
activeReloads--
mu.Unlock()
return nil
}))
ctx := context.Background()
var wg sync.WaitGroup
// Launch multiple concurrent reloads
for range 5 {
wg.Go(func() {
_ = app.Reload(ctx) //nolint:errcheck // Test is checking serialization, not error handling
})
}
wg.Wait()
mu.Lock()
defer mu.Unlock()
assert.Equal(t, int32(1), maxConcurrent, "only one reload should execute at a time (serialized)")
}
func TestReload_Programmatic(t *testing.T) {
t.Parallel()
// Test that Reload() works programmatically
app, err := New(
WithServiceName("test-reload-programmatic"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
var reloadCount int
var mu sync.Mutex
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
reloadCount++
mu.Unlock()
return nil
}))
ctx := context.Background()
// Call Reload() programmatically multiple times
for range 3 {
err = app.Reload(ctx)
require.NoError(t, err)
}
mu.Lock()
defer mu.Unlock()
assert.Equal(t, 3, reloadCount, "reload should work programmatically")
}
func TestOnReload_NoHooksRegistered(t *testing.T) {
t.Parallel()
app, err := New(
WithServiceName("test-reload-no-hooks"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
// Reload with no hooks registered should succeed
ctx := context.Background()
err = app.Reload(ctx)
require.NoError(t, err)
}
func TestOnReload_ContextCancellation(t *testing.T) {
t.Parallel()
app, err := New(
WithServiceName("test-reload-context-cancel"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
require.NoError(t, app.OnReload(func(ctx context.Context) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(100 * time.Millisecond):
return nil
}
}))
// Create a context that's already canceled
ctx, cancel := context.WithCancel(context.Background())
cancel()
err = app.Reload(ctx)
require.Error(t, err)
assert.Contains(t, err.Error(), "context canceled")
}
func TestOnReload_MultipleReloads(t *testing.T) {
t.Parallel()
app, err := New(
WithServiceName("test-reload-multiple"),
WithServiceVersion("1.0.0"),
)
require.NoError(t, err)
var reloadCount int
var mu sync.Mutex
require.NoError(t, app.OnReload(func(ctx context.Context) error {
mu.Lock()
reloadCount++
mu.Unlock()
return nil
}))
ctx := context.Background()
// Perform multiple reloads
for range 10 {
err = app.Reload(ctx)
require.NoError(t, err)
}
mu.Lock()
defer mu.Unlock()
assert.Equal(t, 10, reloadCount, "all reloads should execute")
}