-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_test.go
More file actions
386 lines (303 loc) · 12.3 KB
/
worker_test.go
File metadata and controls
386 lines (303 loc) · 12.3 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
package workers
import (
"context"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestNewWorker(t *testing.T) {
called := false
w := NewWorker("test").HandlerFunc(func(_ context.Context, info *WorkerInfo) error {
called = true
assert.Equal(t, "test", info.GetName())
assert.Equal(t, 0, info.GetAttempt())
return nil
})
require.NotNil(t, w)
assert.Equal(t, "test", w.name)
assert.True(t, w.restartOnFail, "restart should be true by default")
// Run the handler directly to verify.
info := &WorkerInfo{name: "test", attempt: 0}
err := w.handler.RunCycle(context.Background(), info)
assert.NoError(t, err)
assert.True(t, called)
}
func TestWorker_WithRestart(t *testing.T) {
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
assert.True(t, w.restartOnFail, "default should be true")
w.WithRestart(false)
assert.False(t, w.restartOnFail)
}
func TestWorker_Every(t *testing.T) {
w := NewWorker("ticker").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error {
return nil
}).Every(10 * time.Millisecond)
assert.Equal(t, 10*time.Millisecond, w.interval, "Every should store interval as data")
assert.NotNil(t, w.handler, "Every should NOT replace the handler (wrapping is deferred)")
}
func TestWorker_WithJitter(t *testing.T) {
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
assert.Equal(t, -1, w.jitterPercent, "default jitter should be -1 (inherit)")
w.WithJitter(10)
assert.Equal(t, 10, w.jitterPercent)
w.WithJitter(0)
assert.Equal(t, 0, w.jitterPercent, "0 explicitly disables jitter")
}
func TestWorker_WithInitialDelay(t *testing.T) {
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
w.WithInitialDelay(5 * time.Second)
assert.Equal(t, 5*time.Second, w.initialDelay)
}
func TestWorker_Interceptors(t *testing.T) {
mw1 := func(_ context.Context, _ *WorkerInfo, next CycleFunc) error { return next(nil, nil) }
mw2 := func(_ context.Context, _ *WorkerInfo, next CycleFunc) error { return next(nil, nil) }
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
assert.Empty(t, w.interceptors)
w.Interceptors(mw1)
assert.Len(t, w.interceptors, 1)
// Interceptors replaces.
w.Interceptors(mw2)
assert.Len(t, w.interceptors, 1)
// AddInterceptors appends.
w.AddInterceptors(mw1)
assert.Len(t, w.interceptors, 2)
}
func TestWorkerInfo(t *testing.T) {
info := &WorkerInfo{name: "myworker", attempt: 3}
assert.Equal(t, "myworker", info.GetName())
assert.Equal(t, 3, info.GetAttempt())
}
func TestWorkerInfo_Children_Nil(t *testing.T) {
info := &WorkerInfo{name: "test"}
assert.Empty(t, info.GetChildren(), "Children on nil supervisor should return empty slice")
}
func TestCycleFunc_Close(t *testing.T) {
fn := CycleFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
assert.NoError(t, fn.Close(), "CycleFunc.Close should be a no-op")
}
func TestWorker_GetName(t *testing.T) {
w := NewWorker("my-worker").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
assert.Equal(t, "my-worker", w.GetName())
}
func TestWorker_GetHandler(t *testing.T) {
fn := CycleFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
w := NewWorker("test").HandlerFunc(fn)
assert.NotNil(t, w.GetHandler())
w2 := NewWorker("no-handler")
assert.Nil(t, w2.GetHandler())
}
func TestWorker_WithFailureDecay(t *testing.T) {
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
w.WithFailureDecay(2.0)
assert.Equal(t, 2.0, w.failureDecay)
}
func TestWorker_WithFailureThreshold(t *testing.T) {
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
w.WithFailureThreshold(10)
assert.Equal(t, 10.0, w.failureThreshold)
}
func TestWorker_WithFailureBackoff(t *testing.T) {
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
w.WithFailureBackoff(5 * time.Second)
assert.Equal(t, 5*time.Second, w.failureBackoff)
}
func TestWorker_WithTimeout(t *testing.T) {
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
w.WithTimeout(30 * time.Second)
assert.Equal(t, 30*time.Second, w.timeout)
}
func TestWorker_WithBackoffJitter(t *testing.T) {
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
w.WithBackoffJitter(func(d time.Duration) time.Duration { return d / 2 })
assert.NotNil(t, w.backoffJitter)
}
func TestWorkerInfo_GetHandler(t *testing.T) {
fn := CycleFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
info := NewWorkerInfo("test", 0, WithTestHandler(fn))
assert.NotNil(t, info.GetHandler())
}
func TestWorkerInfo_GetHandler_Nil(t *testing.T) {
info := NewWorkerInfo("test", 0)
assert.Nil(t, info.GetHandler())
}
func TestWorkerInfo_GetChild(t *testing.T) {
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
// No child yet.
_, ok := info.GetChild("nonexistent")
assert.False(t, ok)
// Add a child.
info.Add(NewWorker("child").HandlerFunc(func(ctx context.Context, _ *WorkerInfo) error {
<-ctx.Done()
return ctx.Err()
}))
time.Sleep(20 * time.Millisecond) // let child start
child, ok := info.GetChild("child")
assert.True(t, ok)
assert.Equal(t, "child", child.GetName())
// Verify it's a copy — mutations don't affect the running worker.
child.WithRestart(false)
child2, _ := info.GetChild("child")
assert.True(t, child2.restartOnFail, "mutation on copy should not affect original")
}
func TestWorkerInfo_Add_SkipsExisting(t *testing.T) {
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
childFn := CycleFunc(func(ctx context.Context, _ *WorkerInfo) error {
<-ctx.Done()
return ctx.Err()
})
added := info.Add(NewWorker("child").HandlerFunc(childFn))
assert.True(t, added, "first Add should succeed")
time.Sleep(20 * time.Millisecond)
// Second Add with same name is a no-op.
added = info.Add(NewWorker("child").HandlerFunc(childFn))
assert.False(t, added, "duplicate Add should be skipped")
assert.Equal(t, []string{"child"}, info.GetChildren())
}
func TestWorkerInfo_Add_AfterRemove(t *testing.T) {
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
childFn := CycleFunc(func(ctx context.Context, _ *WorkerInfo) error {
<-ctx.Done()
return ctx.Err()
})
info.Add(NewWorker("child").HandlerFunc(childFn))
time.Sleep(20 * time.Millisecond)
info.Remove("child")
time.Sleep(20 * time.Millisecond)
// After Remove, Add should succeed again.
added := info.Add(NewWorker("child").HandlerFunc(childFn))
assert.True(t, added, "Add after Remove should succeed")
}
func TestNewWorkerInfo_WithTestChildren(t *testing.T) {
info := NewWorkerInfo("test-parent", 0, WithTestChildren(t.Context()))
// Verify Add/Remove/GetChildren work.
info.Add(NewWorker("a").HandlerFunc(func(ctx context.Context, _ *WorkerInfo) error {
<-ctx.Done()
return ctx.Err()
}))
info.Add(NewWorker("b").HandlerFunc(func(ctx context.Context, _ *WorkerInfo) error {
<-ctx.Done()
return ctx.Err()
}))
time.Sleep(20 * time.Millisecond)
assert.Equal(t, []string{"a", "b"}, info.GetChildren())
info.Remove("a")
time.Sleep(20 * time.Millisecond)
assert.Equal(t, []string{"b"}, info.GetChildren())
}
func TestWorkerInfo_ZombieChild_AutoCleanup(t *testing.T) {
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
// Add a child that returns nil immediately (no restart).
info.Add(NewWorker("child").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error {
return nil
}).WithRestart(false))
time.Sleep(100 * time.Millisecond) // let child stop
// Done channel is closed on permanent stop — lazy prune removes the child.
assert.Equal(t, 0, len(info.GetChildren()))
assert.Empty(t, info.GetChildren())
}
func TestWorkerInfo_ZombieChild_WithGrandchildren(t *testing.T) {
// A child that spawns grandchildren and then permanently stops
// should not remain visible to the parent.
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
info.Add(NewWorker("child").HandlerFunc(func(ctx context.Context, childInfo *WorkerInfo) error {
// Spawn a grandchild that stays alive until context is cancelled.
childInfo.Add(NewWorker("grandchild").HandlerFunc(func(ctx context.Context, _ *WorkerInfo) error {
<-ctx.Done()
return ctx.Err()
}))
time.Sleep(20 * time.Millisecond) // let grandchild start
return ErrDoNotRestart // child permanently stops
}))
time.Sleep(200 * time.Millisecond)
assert.Empty(t, info.GetChildren(), "stopped child should not appear even with live grandchildren")
}
func TestWorkerInfo_ZombieChild_ErrDoNotRestart(t *testing.T) {
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
info.Add(NewWorker("child").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error {
return ErrDoNotRestart
}))
time.Sleep(100 * time.Millisecond)
assert.Equal(t, 0, len(info.GetChildren()))
assert.Empty(t, info.GetChildren())
}
func TestWorkerInfo_ZombieChild_ReAdd(t *testing.T) {
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
// Add a child that stops immediately.
info.Add(NewWorker("child").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error {
return nil
}).WithRestart(false))
time.Sleep(100 * time.Millisecond)
// Stopped child is pruned — Add with same name should succeed.
added := info.Add(NewWorker("child").HandlerFunc(func(ctx context.Context, _ *WorkerInfo) error {
<-ctx.Done()
return ctx.Err()
}))
assert.True(t, added, "re-Add after zombie prune should succeed")
time.Sleep(20 * time.Millisecond)
assert.Equal(t, 1, len(info.GetChildren()))
}
func TestWorkerInfo_ZombieChild_ReAdd_NoRead(t *testing.T) {
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
// Add a child that stops immediately.
info.Add(NewWorker("child").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error {
return nil
}).WithRestart(false))
time.Sleep(100 * time.Millisecond)
// Re-Add directly — Add prunes the stale entry before checking.
added := info.Add(NewWorker("child").HandlerFunc(func(ctx context.Context, _ *WorkerInfo) error {
<-ctx.Done()
return ctx.Err()
}))
assert.True(t, added, "Add should succeed after stopped child is pruned")
time.Sleep(20 * time.Millisecond)
assert.Equal(t, 1, len(info.GetChildren()))
}
func TestWorkerInfo_ZombieChild_GetChild(t *testing.T) {
info := NewWorkerInfo("parent", 0, WithTestChildren(t.Context()))
info.Add(NewWorker("child").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error {
return nil
}).WithRestart(false))
time.Sleep(100 * time.Millisecond)
// GetChild prunes stopped child and returns false.
_, ok := info.GetChild("child")
assert.False(t, ok)
}
func TestNewWorkerInfo_Minimal(t *testing.T) {
// Without options, Add/Remove/GetChildren are safe no-ops.
info := NewWorkerInfo("test", 5)
assert.Equal(t, "test", info.GetName())
assert.Equal(t, 5, info.GetAttempt())
assert.Empty(t, info.GetChildren())
// Add on nil sup is a no-op.
info.Add(NewWorker("child").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil }))
assert.Empty(t, info.GetChildren())
}
func TestWorker_ConfigGetters(t *testing.T) {
w := NewWorker("test").
HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil }).
Every(30 * time.Second).
WithJitter(15).
WithInitialDelay(5 * time.Second).
WithRestart(false)
assert.Equal(t, 30*time.Second, w.GetInterval())
assert.Equal(t, 15, w.GetJitterPercent())
assert.Equal(t, 5*time.Second, w.GetInitialDelay())
assert.False(t, w.GetRestartOnFail())
}
func TestWorker_ConfigGetters_Defaults(t *testing.T) {
w := NewWorker("test")
assert.Equal(t, time.Duration(0), w.GetInterval())
assert.Equal(t, -1, w.GetJitterPercent())
assert.Equal(t, time.Duration(0), w.GetInitialDelay())
assert.True(t, w.GetRestartOnFail())
}
func TestWorker_InterceptorsCopiesSlice(t *testing.T) {
mw := func(_ context.Context, _ *WorkerInfo, next CycleFunc) error { return next(nil, nil) }
original := []Middleware{mw}
w := NewWorker("test").HandlerFunc(func(_ context.Context, _ *WorkerInfo) error { return nil })
w.Interceptors(original...)
// Mutate the original slice — should not affect the worker.
original[0] = nil
assert.NotNil(t, w.interceptors[0], "Interceptors should copy the slice")
}