Repository navigation
Expand file tree
/
Copy pathqueue_test.go
More file actions
547 lines (464 loc) · 15.1 KB
/
Copy pathqueue_test.go
File metadata and controls
547 lines (464 loc) · 15.1 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
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
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
package main
import (
"context"
"errors"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
func newTestQueue(t *testing.T) *Queue {
t.Helper()
store := NewInMemoryStore()
q := NewQueue(store, 1) // single worker: makes ordering assertions deterministic
return q
}
func TestPriorityOrdering(t *testing.T) {
q := newTestQueue(t)
low := NewJob("noop", nil, PriorityLow, 0)
normal := NewJob("noop", nil, PriorityNormal, 0)
high := NewJob("noop", nil, PriorityHigh, 0)
// Enqueue in low->normal->high order; fetchJob should still return
// high first regardless of arrival order.
for _, j := range []*Job{low, normal, high} {
if err := q.Enqueue(j); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
}
got := q.fetchJob()
if got.ID != high.ID {
t.Fatalf("expected high priority job first, got priority %v", got.Priority)
}
got = q.fetchJob()
if got.ID != normal.ID {
t.Fatalf("expected normal priority job second, got priority %v", got.Priority)
}
got = q.fetchJob()
if got.ID != low.ID {
t.Fatalf("expected low priority job third, got priority %v", got.Priority)
}
}
func TestRetrySucceedsAfterFailures(t *testing.T) {
q := newTestQueue(t)
var attempts int32
q.RegisterHandler("flaky", func(ctx context.Context, job *Job) error {
n := atomic.AddInt32(&attempts, 1)
if n < 3 {
return errors.New("simulated transient failure")
}
return nil
})
job := NewJob("flaky", nil, PriorityNormal, 5)
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
q.processJob(job)
if job.Status != StatusDone {
t.Fatalf("expected job to finish Done, got %v (error: %s)", job.Status, job.Error)
}
if atomic.LoadInt32(&attempts) != 3 {
t.Fatalf("expected exactly 3 attempts, got %d", attempts)
}
}
func TestRetriesExhausted(t *testing.T) {
q := newTestQueue(t)
q.RegisterHandler("always_fails", func(ctx context.Context, job *Job) error {
return errors.New("permanent failure")
})
job := NewJob("always_fails", nil, PriorityNormal, 2)
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
q.processJob(job)
if job.Status != StatusFailed {
t.Fatalf("expected job to end Failed, got %v", job.Status)
}
if job.Retries != job.MaxRetries+1 {
t.Fatalf("expected %d recorded attempts, got %d", job.MaxRetries+1, job.Retries)
}
}
func TestPerJobTimeoutIsEnforced(t *testing.T) {
q := newTestQueue(t)
q.RegisterHandler("slow", func(ctx context.Context, job *Job) error {
select {
case <-time.After(2 * time.Second):
return nil // would eventually succeed if not cancelled
case <-ctx.Done():
return ctx.Err()
}
})
job := NewJob("slow", nil, PriorityNormal, 0)
job.Timeout = 50 * time.Millisecond
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
start := time.Now()
q.processJob(job)
elapsed := time.Since(start)
if job.Status != StatusFailed {
t.Fatalf("expected job to fail due to timeout, got %v", job.Status)
}
if elapsed > 1*time.Second {
t.Fatalf("job took %v — timeout was not enforced (handler ran to completion)", elapsed)
}
}
func TestScheduledJobIsHeldUntilDue(t *testing.T) {
q := newTestQueue(t)
scheduler := NewScheduler(q.release, 20*time.Millisecond)
q.SetScheduler(scheduler)
scheduler.Start()
defer scheduler.Stop()
job := NewJob("noop", nil, PriorityNormal, 0)
job.RunAt = time.Now().UTC().Add(150 * time.Millisecond)
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
// Immediately after enqueue, the job must NOT be sitting in a
// worker-facing channel yet — it should still be held by the scheduler.
select {
case <-q.normalQueue:
t.Fatal("delayed job was dispatched before its RunAt")
default:
}
if scheduler.Len() != 1 {
t.Fatalf("expected scheduler to be holding 1 job, got %d", scheduler.Len())
}
time.Sleep(250 * time.Millisecond)
select {
case got := <-q.normalQueue:
if got.ID != job.ID {
t.Fatalf("dispatched wrong job")
}
default:
t.Fatal("job was never released to the queue after its RunAt passed")
}
}
func TestEnqueueRejectsDelayedJobWithoutScheduler(t *testing.T) {
q := newTestQueue(t) // no scheduler wired up
job := NewJob("noop", nil, PriorityNormal, 0)
job.RunAt = time.Now().UTC().Add(time.Hour)
if err := q.Enqueue(job); err == nil {
t.Fatal("expected an error enqueueing a delayed job with no scheduler configured")
}
}
func TestFailedJobMovesToDeadLetter(t *testing.T) {
q := newTestQueue(t)
q.RegisterHandler("always_fails", func(ctx context.Context, job *Job) error {
return errors.New("boom")
})
job := NewJob("always_fails", map[string]string{"x": "1"}, PriorityNormal, 0)
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
q.processJob(job)
entries, err := q.store.ListDeadLetters()
if err != nil {
t.Fatalf("ListDeadLetters failed: %v", err)
}
if len(entries) != 1 {
t.Fatalf("expected 1 dead letter, got %d", len(entries))
}
if entries[0].JobID != job.ID {
t.Fatalf("dead letter references wrong job id: got %s want %s", entries[0].JobID, job.ID)
}
if entries[0].Payload["x"] != "1" {
t.Fatalf("dead letter did not preserve payload")
}
}
func TestDeadLetterReplay(t *testing.T) {
q := newTestQueue(t)
var calls int32
q.RegisterHandler("flaky_then_ok", func(ctx context.Context, job *Job) error {
return errors.New("still failing")
})
job := NewJob("flaky_then_ok", nil, PriorityNormal, 0)
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
q.processJob(job)
entries, _ := q.store.ListDeadLetters()
if len(entries) != 1 {
t.Fatalf("expected 1 dead letter before replay, got %d", len(entries))
}
// Simulate the operator fixing the downstream issue, then replaying.
q.handlers["flaky_then_ok"] = func(ctx context.Context, job *Job) error {
atomic.AddInt32(&calls, 1)
return nil
}
replayed := NewJob(entries[0].Type, entries[0].Payload, entries[0].Priority, entries[0].MaxRetries)
if err := q.Enqueue(replayed); err != nil {
t.Fatalf("replay enqueue failed: %v", err)
}
q.processJob(replayed)
if replayed.Status != StatusDone {
t.Fatalf("expected replayed job to succeed, got %v", replayed.Status)
}
if atomic.LoadInt32(&calls) != 1 {
t.Fatalf("expected the fixed handler to run once, got %d", calls)
}
}
func TestConcurrencyLimitIsEnforced(t *testing.T) {
q := newTestQueue(t)
q.SetConcurrencyLimit("limited", 2)
var inFlight int32
var maxObserved int32
release := make(chan struct{})
var wg sync.WaitGroup
q.RegisterHandler("limited", func(ctx context.Context, job *Job) error {
n := atomic.AddInt32(&inFlight, 1)
for {
old := atomic.LoadInt32(&maxObserved)
if n <= old || atomic.CompareAndSwapInt32(&maxObserved, old, n) {
break
}
}
<-release
atomic.AddInt32(&inFlight, -1)
return nil
})
jobs := make([]*Job, 5)
for i := range jobs {
jobs[i] = NewJob("limited", nil, PriorityNormal, 0)
if err := q.Enqueue(jobs[i]); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
}
for _, j := range jobs {
wg.Add(1)
go func(job *Job) {
defer wg.Done()
q.processJob(job)
}(j)
}
// Give the limited handlers time to pile up against the semaphore.
time.Sleep(200 * time.Millisecond)
if got := atomic.LoadInt32(&maxObserved); got > 2 {
t.Fatalf("concurrency limit of 2 was exceeded: observed %d simultaneous executions", got)
}
close(release)
wg.Wait()
}
func TestDeduplicationSuppressesRepeat(t *testing.T) {
q := newTestQueue(t)
q.RegisterHandler("noop", func(ctx context.Context, job *Job) error { return nil })
first := NewJob("noop", nil, PriorityNormal, 0)
first.DedupeKey = "email:alice@example.com"
first.DedupeWindow = time.Minute
second := NewJob("noop", nil, PriorityNormal, 0)
second.DedupeKey = "email:alice@example.com"
second.DedupeWindow = time.Minute
if err := q.Enqueue(first); err != nil {
t.Fatalf("first enqueue should have succeeded: %v", err)
}
err := q.Enqueue(second)
if err == nil {
t.Fatal("expected second enqueue with the same dedupe key to be rejected")
}
if !errors.Is(err, ErrDuplicateJob) {
t.Fatalf("expected ErrDuplicateJob, got: %v", err)
}
// A different key must not be affected by someone else's dedupe window.
unrelated := NewJob("noop", nil, PriorityNormal, 0)
unrelated.DedupeKey = "email:bob@example.com"
unrelated.DedupeWindow = time.Minute
if err := q.Enqueue(unrelated); err != nil {
t.Fatalf("unrelated dedupe key should not be blocked: %v", err)
}
}
func TestDeduplicationExpiresAfterWindow(t *testing.T) {
q := newTestQueue(t)
q.RegisterHandler("noop", func(ctx context.Context, job *Job) error { return nil })
first := NewJob("noop", nil, PriorityNormal, 0)
first.DedupeKey = "sms:+1555"
first.DedupeWindow = 50 * time.Millisecond
if err := q.Enqueue(first); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
time.Sleep(100 * time.Millisecond)
second := NewJob("noop", nil, PriorityNormal, 0)
second.DedupeKey = "sms:+1555"
second.DedupeWindow = 50 * time.Millisecond
if err := q.Enqueue(second); err != nil {
t.Fatalf("expected dedupe window to have expired, got: %v", err)
}
}
func TestOnSuccessChainFires(t *testing.T) {
q := newTestQueue(t)
followUpDone := make(chan struct{})
q.RegisterHandler("step_one", func(ctx context.Context, job *Job) error { return nil })
q.RegisterHandler("step_two", func(ctx context.Context, job *Job) error {
close(followUpDone)
return nil
})
job := NewJob("step_one", nil, PriorityNormal, 0)
job.OnSuccess = &JobTemplate{Type: "step_two", Priority: PriorityNormal}
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
dispatched := q.fetchJob()
if dispatched == nil || dispatched.ID != job.ID {
t.Fatal("failed to fetch the originally enqueued job")
}
q.processJob(dispatched)
chained := q.fetchJob()
if chained == nil || chained.Type != "step_two" {
t.Fatal("expected the OnSuccess job to have been enqueued")
}
q.processJob(chained)
select {
case <-followUpDone:
default:
t.Fatal("chained step_two handler never ran")
}
}
func TestOnFailureChainFiresNotOnSuccess(t *testing.T) {
q := newTestQueue(t)
q.RegisterHandler("will_fail", func(ctx context.Context, job *Job) error {
return errors.New("nope")
})
q.RegisterHandler("alert", func(ctx context.Context, job *Job) error { return nil })
job := NewJob("will_fail", nil, PriorityNormal, 0)
job.OnSuccess = &JobTemplate{Type: "should_not_run", Priority: PriorityNormal}
job.OnFailure = &JobTemplate{Type: "alert", Priority: PriorityNormal}
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
dispatched := q.fetchJob()
if dispatched == nil || dispatched.ID != job.ID {
t.Fatal("failed to fetch the originally enqueued job")
}
q.processJob(dispatched)
chained := q.fetchJob()
if chained == nil {
t.Fatal("expected an OnFailure job to be enqueued")
}
if chained.Type != "alert" {
t.Fatalf("expected OnFailure job (alert), got %s — OnSuccess must not fire on a failed job", chained.Type)
}
}
func TestWebhookNotifiesOnCompletion(t *testing.T) {
q := newTestQueue(t)
q.RegisterHandler("noop", func(ctx context.Context, job *Job) error { return nil })
received := make(chan string, 1)
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
received <- "called"
w.WriteHeader(http.StatusOK)
}))
defer srv.Close()
job := NewJob("noop", nil, PriorityNormal, 0)
job.WebhookURL = srv.URL
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue failed: %v", err)
}
q.processJob(job)
select {
case <-received:
case <-time.After(2 * time.Second):
t.Fatal("webhook was never called after job completion")
}
}
func TestPaginationLimitAndOffset(t *testing.T) {
store := NewInMemoryStore()
for i := 0; i < 5; i++ {
j := NewJob("noop", nil, PriorityNormal, 0)
j.ID = "job_" + strconv.Itoa(i)
j.CreatedAt = time.Now().UTC().Add(time.Duration(i) * time.Second) // ascending creation time
_ = store.Save(j)
}
// Newest first (CreatedAt desc), page size 2.
page1, err := store.List(JobFilter{Limit: 2})
if err != nil {
t.Fatalf("List failed: %v", err)
}
if len(page1) != 2 {
t.Fatalf("expected 2 results, got %d", len(page1))
}
if page1[0].ID != "job_4" || page1[1].ID != "job_3" {
t.Fatalf("expected newest-first ordering [job_4, job_3], got [%s, %s]", page1[0].ID, page1[1].ID)
}
page2, err := store.List(JobFilter{Limit: 2, Offset: 2})
if err != nil {
t.Fatalf("List failed: %v", err)
}
if len(page2) != 2 || page2[0].ID != "job_2" {
t.Fatalf("expected second page to start at job_2, got %+v", page2)
}
beyondEnd, err := store.List(JobFilter{Limit: 2, Offset: 10})
if err != nil {
t.Fatalf("List failed: %v", err)
}
if len(beyondEnd) != 0 {
t.Fatalf("expected empty result past the end, got %d", len(beyondEnd))
}
}
func TestMetricsExposesJobCounts(t *testing.T) {
q := newTestQueue(t)
q.RegisterHandler("noop", func(ctx context.Context, job *Job) error { return nil })
q.RegisterHandler("bad", func(ctx context.Context, job *Job) error { return errors.New("x") })
ok := NewJob("noop", nil, PriorityNormal, 0)
_ = q.Enqueue(ok)
q.processJob(ok)
bad := NewJob("bad", nil, PriorityNormal, 0)
_ = q.Enqueue(bad)
q.processJob(bad)
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "/metrics", nil)
q.Metrics().ServeHTTP(rec, req)
body := rec.Body.String()
if !strings.Contains(body, `goqueue_jobs_total{type="noop",status="done"} 1`) {
t.Fatalf("missing expected done counter, got:\n%s", body)
}
if !strings.Contains(body, `goqueue_jobs_total{type="bad",status="failed"} 1`) {
t.Fatalf("missing expected failed counter, got:\n%s", body)
}
if !strings.Contains(body, "goqueue_job_duration_seconds_bucket") {
t.Fatalf("missing duration histogram, got:\n%s", body)
}
}
func TestStoreDispatchEnqueueOnlyPersists(t *testing.T) {
store := NewInMemoryStore()
q := NewQueue(store, 1)
q.UseStoreDispatch()
job := NewJob("noop", nil, PriorityHigh, 0)
job.RunAt = time.Now().UTC().Add(time.Hour) // no scheduler: must still be accepted
if err := q.Enqueue(job); err != nil {
t.Fatalf("enqueue: %v", err)
}
if n := q.buffered(); n != 0 {
t.Fatalf("store-dispatch enqueue put %d jobs in local channels; only the leader's dispatcher should", n)
}
saved, err := store.Get(job.ID)
if err != nil || saved.Status != StatusPending {
t.Fatalf("want job persisted as pending, got %+v (err %v)", saved, err)
}
}
func TestStoreDispatchStopHandsBackUnstartedJobs(t *testing.T) {
store := NewInMemoryStore()
q := NewQueue(store, 1)
q.UseStoreDispatch()
var ran atomic.Bool
q.RegisterHandler("noop", func(ctx context.Context, job *Job) error {
ran.Store(true)
return nil
})
// Simulate a dispatcher claim that no worker picked up yet.
job := NewJob("noop", nil, PriorityNormal, 0)
job.Status = StatusRunning
job.StartedAt = timePtr(time.Now().UTC())
_ = store.Save(job)
if err := q.release(job); err != nil {
t.Fatal(err)
}
q.Stop() // workers never started, so the job is still buffered
if ran.Load() {
t.Fatal("resigning leader ran a buffered job instead of handing it back")
}
saved, _ := store.Get(job.ID)
if saved.Status != StatusPending || saved.StartedAt != nil {
t.Fatalf("want job handed back as pending, got status %s", saved.Status)
}
}