-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueue_priority_test.go
More file actions
660 lines (547 loc) · 17.5 KB
/
Copy pathqueue_priority_test.go
File metadata and controls
660 lines (547 loc) · 17.5 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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package cq
import (
"context"
"errors"
"sync"
"sync/atomic"
"testing"
"time"
)
func TestPriorityString(t *testing.T) {
tests := []struct {
want string
priority Priority
}{
{
want: "LOWEST",
priority: PriorityLowest,
},
{
want: "LOW",
priority: PriorityLow,
},
{
want: "MEDIUM",
priority: PriorityMedium,
},
{
want: "HIGH",
priority: PriorityHigh,
},
{
want: "HIGHEST",
priority: PriorityHighest,
},
}
for _, tt := range tests {
t.Run(tt.want, func(t *testing.T) {
if got := tt.priority.String(); got != tt.want {
t.Errorf("Priority.String(): got %v, want %v", got, tt.want)
}
})
}
}
func TestPriorityQueue(t *testing.T) {
t.Run("constructor_nil_queue_panics", func(t *testing.T) {
defer func() {
if recover() == nil {
t.Fatal("NewPriorityQueue(nil): expected panic")
}
}()
_ = NewPriorityQueue(nil, 1)
})
t.Run("non_positive_priority_tick_uses_default", func(t *testing.T) {
queue := NewQueue(1, 1, 1)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 1, WithPriorityTick(0))
defer pq.Stop(false)
if pq.priorityTick != defaultPriorityTick {
t.Fatalf("priority tick: got %v, want %v", pq.priorityTick, defaultPriorityTick)
}
})
t.Run("basic_enqueue", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
var executed atomic.Bool
job := func(ctx context.Context) error {
executed.Store(true)
return nil
}
mustPrioritySubmit(t, pq, job, PriorityHigh)
// Wait for job to process.
time.Sleep(50 * time.Millisecond)
if !executed.Load() {
t.Error("PriorityQueue: job should have executed")
}
})
t.Run("priority_ordering", func(t *testing.T) {
queue := NewQueue(1, 1, 100) // Single worker to ensure ordering.
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
var order []int
var mut sync.Mutex
makeJob := func(id int) Job {
return func(ctx context.Context) error {
mut.Lock()
order = append(order, id)
mut.Unlock()
return nil
}
}
// Submit in reverse priority order.
mustPrioritySubmit(t, pq, makeJob(5), PriorityLowest)
mustPrioritySubmit(t, pq, makeJob(4), PriorityLow)
mustPrioritySubmit(t, pq, makeJob(3), PriorityMedium)
mustPrioritySubmit(t, pq, makeJob(2), PriorityHigh)
mustPrioritySubmit(t, pq, makeJob(1), PriorityHighest)
// Wait for all jobs to process.
time.Sleep(100 * time.Millisecond)
mut.Lock()
defer mut.Unlock()
if len(order) != 5 {
t.Fatalf("PriorityQueue: got %d jobs, want 5", len(order))
}
// Highest priority should execute first.
if order[0] != 1 {
t.Errorf("PriorityQueue: first job: got %d, want 1 (highest)", order[0])
}
})
t.Run("stop_with_queue", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
pq := NewPriorityQueue(queue, 10)
// Stop with stopQueue = true.
pq.Stop(true)
// Queue should be stopped.
if !queue.IsStopped() {
t.Error("PriorityQueue.Stop(true): queue should be stopped")
}
})
t.Run("stop_without_queue", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
// Stop with stopQueue = false.
pq.Stop(false)
// Queue should still be running.
if queue.IsStopped() {
t.Error("PriorityQueue.Stop(false): queue should still be running")
}
})
t.Run("weighted_number", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10,
WithWeighting(
NumberWeight(10),
NumberWeight(5),
NumberWeight(3),
NumberWeight(2),
NumberWeight(1),
),
)
defer pq.Stop(false)
if pq.weights.highest != 10 {
t.Errorf("WithWeighting: highest weight: got %d, want 10", pq.weights.highest)
}
if pq.weights.high != 5 {
t.Errorf("WithWeighting: high weight: got %d, want 5", pq.weights.high)
}
})
t.Run("weighted_percent", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10,
WithWeighting(
PercentWeight(50), // 50% of 12 = 6
PercentWeight(25), // 25% of 12 = 3
PercentWeight(17), // 17% of 12 = 2
PercentWeight(8), // 8% of 12 = 0 -> 1 (min)
PercentWeight(8), // 8% of 12 = 0 -> 1 (min)
),
)
defer pq.Stop(false)
if pq.weights.highest != 6 {
t.Errorf("WithWeighting percent: highest weight: got %d, want 6", pq.weights.highest)
}
if pq.weights.high != 3 {
t.Errorf("WithWeighting percent: high weight: got %d, want 3", pq.weights.high)
}
if pq.weights.medium != 2 {
t.Errorf("WithWeighting percent: medium weight: got %d, want 2", pq.weights.medium)
}
// Low and lowest should be at least 1 (floor)
if pq.weights.low < 1 {
t.Errorf("WithWeighting percent: low weight: got %d, want >= 1", pq.weights.low)
}
if pq.weights.lowest < 1 {
t.Errorf("WithWeighting percent: lowest weight: got %d, want >= 1", pq.weights.lowest)
}
})
t.Run("default_weights", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
// Should use default weights (5:3:2:1:1)
if pq.weights.highest != defaultWeightHighest {
t.Errorf("Default weights: highest: got %d, want %d", pq.weights.highest, defaultWeightHighest)
}
if pq.weights.high != defaultWeightHigh {
t.Errorf("Default weights: high: got %d, want %d", pq.weights.high, defaultWeightHigh)
}
if pq.weights.medium != defaultWeightMedium {
t.Errorf("Default weights: medium: got %d, want %d", pq.weights.medium, defaultWeightMedium)
}
if pq.weights.low != defaultWeightLow {
t.Errorf("Default weights: low: got %d, want %d", pq.weights.low, defaultWeightLow)
}
if pq.weights.lowest != defaultWeightLowest {
t.Errorf("Default weights: lowest: got %d, want %d", pq.weights.lowest, defaultWeightLowest)
}
})
t.Run("try_enqueue_success", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
var executed atomic.Bool
job := func(ctx context.Context) error {
executed.Store(true)
return nil
}
// Should succeed as channel has capacity.
if _, err := pq.Submit(context.Background(), job, PriorityHigh, WithNonBlocking()); err != nil {
t.Fatalf("Submit(non-blocking): unexpected err: %v", err)
}
// Wait for job to process.
time.Sleep(50 * time.Millisecond)
if !executed.Load() {
t.Error("Submit(non-blocking): job should have executed")
}
})
t.Run("enqueue_or_error_invalid_falls_back_medium", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
var executed atomic.Bool
job := func(ctx context.Context) error {
executed.Store(true)
return nil
}
if handle, err := pq.Submit(context.Background(), job, Priority(999)); handle != nil || !errors.Is(err, ErrPriorityInvalid) {
t.Fatalf("Submit(invalid): got (%v, %v), want (nil, %v)", handle, err, ErrPriorityInvalid)
}
})
t.Run("try_enqueue_or_error_invalid", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
handle, err := pq.Submit(context.Background(), func(ctx context.Context) error { return nil }, Priority(999), WithNonBlocking())
if handle != nil {
t.Error("Submit(invalid): got handle, want nil")
}
if !errors.Is(err, ErrPriorityInvalid) {
t.Fatalf("Submit(invalid): got err=%v, want %v", err, ErrPriorityInvalid)
}
})
t.Run("try_enqueue_or_error_full", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 1)
defer pq.Stop(false)
job := func(ctx context.Context) error { return nil }
if handle, err := pq.Submit(context.Background(), job, PriorityHigh, WithNonBlocking()); handle == nil || err != nil {
t.Fatalf("Submit(non-blocking): first submission got (%v,%v), want (handle,nil)", handle, err)
}
handle, err := pq.Submit(context.Background(), job, PriorityHigh, WithNonBlocking())
if handle != nil {
t.Error("Submit(non-blocking): got handle on full channel, want nil")
}
if !errors.Is(err, ErrPriorityQueueFull) {
t.Fatalf("Submit(non-blocking): got err=%v, want %v", err, ErrPriorityQueueFull)
}
})
t.Run("enqueue_or_error_stopped", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
pq.Stop(false)
_, err := pq.Submit(context.Background(), func(ctx context.Context) error { return nil }, PriorityHigh)
if !errors.Is(err, ErrPriorityQueueStopped) {
t.Fatalf("Submit(): got err=%v, want %v", err, ErrPriorityQueueStopped)
}
})
t.Run("try_enqueue_full", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 2) // Small capacity.
defer pq.Stop(false)
// Fill the high priority channel.
blockingJob := func(ctx context.Context) error {
time.Sleep(1 * time.Second)
return nil
}
mustPrioritySubmit(t, pq, blockingJob, PriorityHigh)
mustPrioritySubmit(t, pq, blockingJob, PriorityHigh)
// Channel should now be full.
if handle, err := pq.Submit(context.Background(), blockingJob, PriorityHigh, WithNonBlocking()); handle != nil || !errors.Is(err, ErrPriorityQueueFull) {
t.Fatalf("Submit(non-blocking): got (%v, %v), want (nil, %v)", handle, err, ErrPriorityQueueFull)
}
})
t.Run("delay_enqueue_priority", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
var executed atomic.Bool
job := func(ctx context.Context) error {
executed.Store(true)
return nil
}
delay := 100 * time.Millisecond
if _, err := pq.SubmitAfter(context.Background(), job, PriorityHigh, delay); err != nil {
t.Fatalf("SubmitAfter(): unexpected err: %v", err)
}
// Should not be executed immediately.
time.Sleep(50 * time.Millisecond)
if executed.Load() {
t.Error("SubmitAfter(): job should not have executed yet")
}
// Should be executed after delay.
time.Sleep(100 * time.Millisecond)
if !executed.Load() {
t.Error("SubmitAfter(): job should have executed after delay")
}
})
t.Run("count_by_priority", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
slowJob := func(ctx context.Context) error {
time.Sleep(500 * time.Millisecond)
return nil
}
// Submit jobs at different priorities.
mustPrioritySubmit(t, pq, slowJob, PriorityHighest)
mustPrioritySubmit(t, pq, slowJob, PriorityHighest)
mustPrioritySubmit(t, pq, slowJob, PriorityHigh)
mustPrioritySubmit(t, pq, slowJob, PriorityMedium)
// Allow jobs to queue up before dispatcher pulls them.
time.Sleep(10 * time.Millisecond)
// Check counts (some may have been dispatched).
highestCount := pq.CountByPriority(PriorityHighest)
highCount := pq.CountByPriority(PriorityHigh)
mediumCount := pq.CountByPriority(PriorityMedium)
// At least verify the method returns non-negative values.
if highestCount < 0 || highCount < 0 || mediumCount < 0 {
t.Error("CountByPriority(): should return non-negative values")
}
})
t.Run("pending_by_priority", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10)
defer pq.Stop(false)
slowJob := func(ctx context.Context) error {
time.Sleep(500 * time.Millisecond)
return nil
}
// Submit jobs at different priorities.
mustPrioritySubmit(t, pq, slowJob, PriorityHighest)
mustPrioritySubmit(t, pq, slowJob, PriorityHigh)
mustPrioritySubmit(t, pq, slowJob, PriorityMedium)
time.Sleep(10 * time.Millisecond)
// Get pending map.
pending := pq.PendingByPriority()
// Verify map has all priority levels.
if len(pending) != 5 {
t.Errorf("PendingByPriority(): got %d entries, want 5", len(pending))
}
// Verify all priority levels are present.
for _, priority := range []Priority{PriorityHighest, PriorityHigh, PriorityMedium, PriorityLow, PriorityLowest} {
if _, exists := pending[priority]; !exists {
t.Errorf("PendingByPriority(): missing priority %s", priority.String())
}
}
})
t.Run("drain", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
// Use long tick to prevent dispatcher from pulling jobs.
pq := NewPriorityQueue(queue, 10, WithPriorityTick(10*time.Second))
var executed atomic.Int32
job := func(ctx context.Context) error {
executed.Add(1)
return nil
}
// Submit jobs at different priorities.
mustPrioritySubmit(t, pq, job, PriorityHighest)
mustPrioritySubmit(t, pq, job, PriorityHighest)
mustPrioritySubmit(t, pq, job, PriorityHigh)
mustPrioritySubmit(t, pq, job, PriorityMedium)
mustPrioritySubmit(t, pq, job, PriorityLow)
// Drain all buffered jobs.
drained := pq.Drain()
if drained != 5 {
t.Errorf("Drain(): got %d drained, want 5", drained)
}
// Wait for drained jobs to execute.
time.Sleep(100 * time.Millisecond)
if executed.Load() != 5 {
t.Errorf("Drain(): got %d executed, want 5", executed.Load())
}
// Stop without stopping queue (we handle it via defer).
pq.Stop(false)
})
t.Run("drain_empty", func(t *testing.T) {
queue := NewQueue(1, 5, 100)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10, WithPriorityTick(10*time.Second))
defer pq.Stop(false)
// Drain empty queue.
drained := pq.Drain()
if drained != 0 {
t.Errorf("Drain(): got %d drained from empty queue, want 0", drained)
}
})
}
func TestPriorityQueueSubmit_PreservesHandleThroughPriorityBuffer(t *testing.T) {
queue := NewQueue(1, 1, 10)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 10, WithPriorityTick(time.Millisecond))
defer pq.Stop(false)
handle, err := pq.Submit(
context.Background(),
func(context.Context) error { return nil },
PriorityHigh,
WithJobID("priority-job"),
WithJobAttribute("source", "priority"),
)
if err != nil {
t.Fatalf("Submit(): %v", err)
}
if err := handle.Wait(context.Background()); err != nil {
t.Fatalf("Wait(): %v", err)
}
result, ok := handle.Result()
if !ok {
t.Fatal("Result(): submission should be complete")
}
if result.Meta.ID != "priority-job" || result.Meta.Attributes["source"] != "priority" {
t.Fatalf("Result().Meta: got %+v", result.Meta)
}
}
func TestPriorityQueueStop_RejectsBufferedSubmission(t *testing.T) {
queue := NewQueue(0, 0, 0)
queue.Start()
defer queue.Stop(false)
pq := NewPriorityQueue(queue, 1, WithPriorityTick(time.Hour))
handle := mustPrioritySubmit(t, pq, func(context.Context) error { return nil }, PriorityHigh)
pq.Stop(false)
if err := handle.Wait(context.Background()); !errors.Is(err, ErrPriorityQueueStopped) {
t.Fatalf("Wait(): got %v, want %v", err, ErrPriorityQueueStopped)
}
}
func TestPriorityQueueSubmit_ContextCancelsBufferAcceptance(t *testing.T) {
queue := NewQueue(0, 0, 0)
queue.Start()
defer queue.Stop(false)
pq := NewPriorityQueue(queue, 1, WithPriorityTick(time.Hour))
defer pq.Stop(false)
mustPrioritySubmit(t, pq, func(context.Context) error { return nil }, PriorityHigh)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
defer cancel()
handle, err := pq.Submit(ctx, func(context.Context) error { return nil }, PriorityHigh)
if handle != nil {
t.Fatal("Submit(): got handle, want nil")
}
if !errors.Is(err, context.DeadlineExceeded) {
t.Fatalf("Submit(): got %v, want %v", err, context.DeadlineExceeded)
}
}
func TestPriorityQueueSubmit_CancelBufferedPreventsExecution(t *testing.T) {
queue := NewQueue(1, 1, 1)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 1, WithPriorityTick(time.Hour))
defer pq.Stop(false)
ran := make(chan struct{}, 1)
handle := mustPrioritySubmit(t, pq, func(context.Context) error {
ran <- struct{}{}
return nil
}, PriorityHigh)
if !handle.Cancel() {
t.Fatal("Cancel(): got false, want true")
}
if err := handle.Wait(context.Background()); !errors.Is(err, ErrJobCancelled) {
t.Fatalf("Wait(): got %v, want %v", err, ErrJobCancelled)
}
if pq.trySubmit(pq.high) {
t.Fatal("trySubmit(): forwarded cancelled submission")
}
select {
case <-ran:
t.Fatal("cancelled priority job executed")
default:
}
}
func TestPriorityQueueSubmitAfter_CancelPreventsSubmission(t *testing.T) {
queue := NewQueue(1, 1, 1)
queue.Start()
defer queue.Stop(true)
pq := NewPriorityQueue(queue, 1, WithPriorityTick(time.Hour))
defer pq.Stop(false)
ran := make(chan struct{}, 1)
handle, err := pq.SubmitAfter(context.Background(), func(context.Context) error {
ran <- struct{}{}
return nil
}, PriorityHigh, time.Hour)
if err != nil {
t.Fatalf("SubmitAfter(): got err=%v, want nil", err)
}
if !handle.Cancel() {
t.Fatal("Cancel(): got false, want true")
}
if err := handle.Wait(context.Background()); !errors.Is(err, ErrJobCancelled) {
t.Fatalf("Wait(): got %v, want %v", err, ErrJobCancelled)
}
select {
case <-ran:
t.Fatal("cancelled delayed priority job executed")
default:
}
}
func mustPrioritySubmit(t *testing.T, pq *PriorityQueue, job Job, priority Priority, opts ...SubmitOption) *JobHandle {
t.Helper()
handle, err := pq.Submit(context.Background(), job, priority, opts...)
if err != nil {
t.Fatalf("PriorityQueue.Submit(): %v", err)
}
return handle
}