-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjob_release_test.go
More file actions
397 lines (339 loc) · 9.78 KB
/
job_release_test.go
File metadata and controls
397 lines (339 loc) · 9.78 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
package cq
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestWithRelease(t *testing.T) {
t.Run("release_on_error", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
releaseErr := errors.New("release me")
job := WithRelease(
func(ctx context.Context) error {
if calls.Add(1) < 2 {
return releaseErr
}
return nil
},
queue,
10*time.Millisecond,
2,
func(err error) bool {
return errors.Is(err, releaseErr)
},
)
// First call... should release.
if err := job(context.Background()); err != nil {
t.Errorf("WithRelease(): got %v, want nil (on release)", err)
}
// Wait for re-enqueue.
time.Sleep(20 * time.Millisecond)
// Should have been called twice (initial + 1 release).
if got := calls.Load(); got < 2 {
t.Errorf("WithRelease(): calls: got %d, want >= 2", got)
}
})
t.Run("max_releases_exceeded", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
releaseErr := errors.New("release me")
maxReleases := 2
job := WithRelease(
func(ctx context.Context) error {
calls.Add(1)
return releaseErr
},
queue,
10*time.Millisecond,
maxReleases,
func(err error) bool {
return errors.Is(err, releaseErr)
},
)
// Call until max releases.
for i := 0; i <= maxReleases; i++ {
err := job(context.Background())
if i < maxReleases {
if err != nil {
t.Errorf("WithRelease(): release %d: got %v, want nil", i, err)
}
} else {
// Should return error after max releases.
if err == nil {
t.Error("WithRelease(): should return error after max releases")
}
}
}
})
t.Run("no_release_on_different_error", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
releaseErr := errors.New("release me")
otherErr := errors.New("other error")
job := WithRelease(
func(ctx context.Context) error {
return otherErr
},
queue,
10*time.Millisecond,
2,
func(err error) bool {
return errors.Is(err, releaseErr)
},
)
// Should return error, not release.
err := job(context.Background())
if err == nil {
t.Error("WithRelease(): should return error for non-release error")
}
if !errors.Is(err, otherErr) {
t.Errorf("WithRelease(): got error %v, want %v", err, otherErr)
}
})
t.Run("release_counter_persists_through_queue", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls int
var mu sync.Mutex
releaseErr := errors.New("release me")
maxReleases := 2
job := WithRelease(
func(ctx context.Context) error {
mu.Lock()
calls++
mu.Unlock()
return releaseErr // Always fails to test max releases.
},
queue,
20*time.Millisecond,
maxReleases,
func(err error) bool {
return errors.Is(err, releaseErr)
},
)
// Enqueue the wrapped job (not calling directly).
queue.Enqueue(job)
// Wait for all releases to process through the queue.
// Should be: initial call + 2 releases = 3 total calls.
time.Sleep(150 * time.Millisecond)
mu.Lock()
totalCalls := calls
mu.Unlock()
expectedCalls := maxReleases + 1 // Initial + max releases.
if totalCalls != expectedCalls {
t.Errorf("WithRelease(): calls through queue: got %d, want %d", totalCalls, expectedCalls)
}
// Final tally should show 1 failed job (after max releases exceeded).
failed := queue.TallyOf(JobStateFailed)
if failed != 1 {
t.Errorf("WithRelease(): failed jobs: got %d, want 1", failed)
}
})
}
func TestWithReleaseSelf(t *testing.T) {
t.Run("requests_release_and_reenqueues", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
done := make(chan struct{}, 1)
job := WithReleaseSelf(func(ctx context.Context) error {
n := calls.Add(1)
if n == 1 {
if ok := RequestRelease(ctx, 10*time.Millisecond); !ok {
t.Error("RequestRelease(): expected true")
}
return nil // First run, return nil to indicate release request was successful.
}
// Second run, signal completion. This tells us it was re-enqueued.
select {
case done <- struct{}{}:
default:
}
return nil
}, queue, 1)
queue.Enqueue(job)
select {
case <-done:
case <-time.After(200 * time.Millisecond):
t.Fatalf("WithReleaseSelf(): timed out waiting for re-enqueued run, calls=%d", calls.Load())
}
})
t.Run("release_wins_over_error", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
done := make(chan struct{}, 1)
job := WithReleaseSelf(func(ctx context.Context) error {
n := calls.Add(1)
if n == 1 {
_ = RequestRelease(ctx, 10*time.Millisecond)
return errors.New("transient")
}
select {
case done <- struct{}{}:
default:
}
return nil
}, queue, 1)
// Direct call with metadata to assert release wins (returns nil).
if err := job(contextWithMeta(context.Background(), JobMeta{ID: "self-release-win"})); err != nil {
t.Fatalf("WithReleaseSelf(): got %v, want nil when release requested", err)
}
select {
case <-done:
case <-time.After(200 * time.Millisecond):
t.Fatalf("WithReleaseSelf(): timed out waiting for release re-enqueue, calls=%d", calls.Load())
}
})
t.Run("max_releases_cap", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
job := WithReleaseSelf(func(ctx context.Context) error {
calls.Add(1)
_ = RequestRelease(ctx, 10*time.Millisecond)
return nil
}, queue, 1) // Only one release allowed.
queue.Enqueue(job)
time.Sleep(120 * time.Millisecond)
if got := calls.Load(); got != 2 {
t.Fatalf("WithReleaseSelf(): calls: got %d, want 2 (initial + 1 release)", got)
}
})
t.Run("request_without_release_self_returns_false", func(t *testing.T) {
if ok := RequestRelease(context.Background(), 10*time.Millisecond); ok {
t.Fatal("RequestRelease(): got true, want false without wrapper context")
}
})
t.Run("last_write_wins_for_delay", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
done := make(chan struct{}, 1)
job := WithReleaseSelf(func(ctx context.Context) error {
n := calls.Add(1)
if n == 1 {
_ = RequestRelease(ctx, 80*time.Millisecond)
_ = RequestRelease(ctx, 5*time.Millisecond) // Last write should win.
return nil
}
select {
case done <- struct{}{}:
default:
}
return nil
}, queue, 1)
queue.Enqueue(job)
select {
case <-done:
// Should arrive quickly if 5ms won.
case <-time.After(40 * time.Millisecond):
t.Fatal("WithReleaseSelf(): expected second run before 40ms (last write wins)")
}
})
t.Run("negative_max_releases_treated_as_unlimited", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
done := make(chan struct{}, 1)
job := WithReleaseSelf(func(ctx context.Context) error {
n := calls.Add(1)
if n <= 3 {
_ = RequestRelease(ctx, 5*time.Millisecond)
}
if n == 4 {
select {
case done <- struct{}{}:
default:
}
}
return nil
}, queue, -1) // Should behave as unlimited (0).
queue.Enqueue(job)
select {
case <-done:
case <-time.After(300 * time.Millisecond):
t.Fatalf("WithReleaseSelf(): expected repeated releases with negative max, calls=%d", calls.Load())
}
})
t.Run("panic_after_request_does_not_leak_state", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
job := WithReleaseSelf(func(ctx context.Context) error {
n := calls.Add(1)
if n == 1 {
_ = RequestRelease(ctx, 5*time.Millisecond)
panic("boom")
}
return nil
}, queue, 1)
func() {
defer func() { _ = recover() }()
_ = job(contextWithMeta(context.Background(), JobMeta{ID: "panic-cleanup-id"}))
}()
// Same ID: should not inherit stale request from panic run.
if err := job(contextWithMeta(context.Background(), JobMeta{ID: "panic-cleanup-id"})); err != nil {
t.Fatalf("WithReleaseSelf(): second call got %v, want nil", err)
}
time.Sleep(30 * time.Millisecond)
if got := calls.Load(); got != 2 {
t.Fatalf("WithReleaseSelf(): stale request leaked after panic: got calls=%d, want 2", got)
}
})
t.Run("late_async_request_is_rejected", func(t *testing.T) {
queue := NewQueue(1, 2, 10)
queue.Start()
defer queue.Stop(true)
var calls atomic.Int32
asyncResult := make(chan bool, 1)
job := WithReleaseSelf(func(ctx context.Context) error {
n := calls.Add(1)
if n == 1 {
go func(c context.Context) {
time.Sleep(20 * time.Millisecond) // After run has ended.
ok := RequestRelease(c, 5*time.Millisecond)
asyncResult <- ok
}(ctx)
}
return nil
}, queue, 1)
// First run schedules async late RequestRelease.
if err := job(contextWithMeta(context.Background(), JobMeta{ID: "late-async-id"})); err != nil {
t.Fatalf("WithReleaseSelf(): first call got %v, want nil", err)
}
// Wait for async attempt... should be rejected.
select {
case ok := <-asyncResult:
if ok {
t.Fatal("RequestRelease(): late async call should be rejected")
}
case <-time.After(200 * time.Millisecond):
t.Fatal("WithReleaseSelf(): timed out waiting for async request result")
}
// Second run with same ID should not consume stale request.
if err := job(contextWithMeta(context.Background(), JobMeta{ID: "late-async-id"})); err != nil {
t.Fatalf("WithReleaseSelf(): second call got %v, want nil", err)
}
time.Sleep(40 * time.Millisecond)
if got := calls.Load(); got != 2 {
t.Fatalf("WithReleaseSelf(): stale async request affected later run: got calls=%d, want 2", got)
}
})
}