-
Notifications
You must be signed in to change notification settings - Fork 0
/
gorqs_test.go
639 lines (568 loc) · 15.5 KB
/
gorqs_test.go
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
package gorqs
import (
"context"
"errors"
"reflect"
"strings"
"sync"
"testing"
"time"
)
// Ensure concrete type Queue satisfies Queuer interface.
func TestQueuerInterface(t *testing.T) {
var i interface{} = new(Queue)
if _, ok := i.(Queuer); !ok {
t.Fatalf("expected %T to implement Queuer", i)
}
}
// Ensure concrete type job satisfies jobber interface.
func TestJobberInterface(t *testing.T) {
var i interface{} = new(job)
if _, ok := i.(jobber); !ok {
t.Fatalf("expected %T to implement jobber", i)
}
}
// Ensure calling Clear on queue empty results cache.
func TestClear(t *testing.T) {
q := &Queue{}
q.records.Store(1, errors.New("job id 1 execution error"))
q.Clear()
if _, has := q.records.Load(1); has {
t.Fatalf("expected to not find job id 1")
}
}
// Ensure call to Start a queue evaluate passed flag mode.
func TestStart(t *testing.T) {
t.Run("queue: sync mode", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
q := New(SyncMode)
errCh := make(chan error)
go func() {
err := q.Start(ctx)
errCh <- err
}()
cancel()
select {
case err := <-errCh:
if err != context.Canceled {
t.Errorf("expected %v but got %v", context.Canceled, err)
}
case <-time.After(time.Second):
t.Error("started sync queue did not exit on context cancellation")
}
})
t.Run("queue: async mode", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
q := New(AsyncMode)
errCh := make(chan error)
go func() {
err := q.Start(ctx)
errCh <- err
}()
cancel()
select {
case err := <-errCh:
if err != context.Canceled {
t.Errorf("expected %v but got %v", context.Canceled, err)
}
case <-time.After(time.Second):
t.Error("started async queue did not exit on context cancellation")
}
})
t.Run("queue: invalid mode", func(t *testing.T) {
q := &Queue{}
q.mode = TrackJobs
errCh := make(chan error)
go func() {
err := q.Start(context.Background())
errCh <- err
}()
select {
case err := <-errCh:
if err != ErrInvalidMode {
t.Errorf("expected %v but got %v", ErrInvalidMode, err)
}
case <-time.After(time.Second):
t.Error("expected queue to not start but did not failed onstart")
}
})
t.Run("queue: unnkown mode", func(t *testing.T) {
q := &Queue{}
q.mode = Flag(64)
errCh := make(chan error)
go func() {
err := q.Start(context.Background())
errCh <- err
}()
select {
case err := <-errCh:
if err != ErrUnknownMode {
t.Errorf("expected %v but got %v", ErrUnknownMode, err)
}
case <-time.After(time.Second):
t.Error("expected queue to not start but did not failed onstart")
}
})
}
// Ensure queuing a job returns expected id and error values.
func TestPush(t *testing.T) {
t.Run("queue not running", func(t *testing.T) {
q := &Queue{}
id, err := q.Push(context.Background(), &job{})
if err != ErrQueueClosed {
t.Errorf("expected %v but got %v", ErrQueueClosed, err)
}
if id != -1 {
t.Errorf("expected %d but got %d", -1, id)
}
})
t.Run("success: sync queue intializes job with pending state", func(t *testing.T) {
q := New(SyncMode | TrackJobs)
q.running.Store(true)
id, err := q.Push(context.Background(), &job{})
if err != nil {
t.Fatalf("expected %v but got %v", nil, err)
}
if id != 1 {
t.Fatalf("expected %d but got %d", 1, id)
}
if r := q.fetcher(context.Background(), 1); r != ErrPending {
t.Errorf("expected %v but got %v", ErrPending, r)
}
})
t.Run("success: async queue does not intializes job state", func(t *testing.T) {
q := New(AsyncMode | TrackJobs)
q.running.Store(true)
id, err := q.Push(context.Background(), &job{})
if err != nil {
t.Fatalf("expected %v but got %v", nil, err)
}
if id != q.counter.Load() {
t.Fatalf("expected %d but got %d", 1, id)
}
if r := q.fetcher(context.Background(), int64(1)); r != ErrNotFound {
t.Errorf("expected %v but got %v", ErrNotFound, r)
}
})
t.Run("context cancelled", func(t *testing.T) {
q := New(SyncMode | TrackJobs)
q.running.Store(true)
// prefill the single buffer slot to prevent select clause to queue job.
q.jobsChan <- &job{}
ctx, cancel := context.WithCancel(context.Background())
cancel()
id, err := q.Push(ctx, &job{})
if err != context.Canceled {
t.Fatalf("expected %v but got %v", context.Canceled, err)
}
if id != -1 {
t.Fatalf("expected %d but got %d", -1, id)
}
if r := q.fetcher(context.Background(), int64(1)); r != ErrNotFound {
t.Errorf("expected %v but got %v", ErrNotFound, r)
}
})
t.Run("operation timed out", func(t *testing.T) {
q := New(SyncMode | TrackJobs)
q.running.Store(true)
// prefill the single buffer slot to prevent select clause to queue job.
q.jobsChan <- &job{}
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
var id int64
errCh := make(chan error)
go func(id *int64) {
val, err := q.Push(ctx, &job{})
*id = val
errCh <- err
}(&id)
select {
case err := <-errCh:
if err != ErrTimeout {
t.Fatalf("expected %v but got %v", ErrTimeout, err)
}
if id != -1 {
t.Fatalf("expected %d but got %d", -1, id)
}
if r := q.fetcher(context.Background(), q.counter.Load()); r != ErrNotFound {
t.Errorf("expected %v but got %v", ErrNotFound, r)
}
case <-ctx.Done():
t.Error("push operation on queue took too long")
}
})
}
// Ensure call to stop the queue operates as expected.
func TestStop(t *testing.T) {
t.Run("context cancel", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
q := &Queue{}
q.running.Store(true)
cancel()
if err := q.Stop(ctx); err != context.Canceled {
t.Errorf("expected %v but got %v", context.Canceled, err)
}
if status := q.running.Load(); !status {
t.Errorf("expected queue running status to be %v but got %v", true, status)
}
})
t.Run("success", func(t *testing.T) {
q := &Queue{}
q.running.Store(true)
if err := q.Stop(context.Background()); err != nil {
t.Errorf("expected %v but got %v", nil, err)
}
if status := q.running.Load(); status {
t.Errorf("expected queue running status to be %v but got %v", false, status)
}
})
}
// Ensure Fetch method returns exact cached job execution error result.
func TestFetch(t *testing.T) {
id := int64(1)
t.Run("not implemented", func(t *testing.T) {
qq := New(SyncMode)
qq.records.Store(id, nil)
result := qq.Fetch(context.Background(), id)
if result != ErrNotImplemented {
t.Fatalf("expect %v but got %v", ErrNotImplemented, result)
}
})
t.Run("no found", func(t *testing.T) {
q := New(SyncMode | TrackJobs)
err := q.Fetch(context.Background(), 1)
if err != ErrNotFound {
t.Fatalf("expect ErrNotFound but got %v", err)
}
})
t.Run("nil", func(t *testing.T) {
q := New(SyncMode | TrackJobs)
q.records.Store(id, nil)
r := q.Fetch(context.Background(), id)
if r != nil {
t.Fatalf("expect <nil> but got %v", r)
}
})
t.Run("invalid", func(t *testing.T) {
q := New(SyncMode | TrackJobs)
q.records.Store(id, "no error type")
err := q.Fetch(context.Background(), id)
if err != ErrInvalid {
t.Fatalf("expect ErrInvalid but got %v", err)
}
})
t.Run("pending", func(t *testing.T) {
q := New(SyncMode | TrackJobs)
q.records.Store(id, ErrPending)
err := q.Fetch(context.Background(), id)
if err != ErrPending {
t.Fatalf("expect ErrPending but got %v", err)
}
err = q.Fetch(context.Background(), id)
if err != ErrPending {
t.Fatalf("expect not deleted entry so again ErrPending but got %v", err)
}
})
t.Run("running", func(t *testing.T) {
q := New(SyncMode | TrackJobs)
q.records.Store(id, ErrRunning)
err := q.Fetch(context.Background(), id)
if err != ErrRunning {
t.Fatalf("expect ErrRunning but got %v", err)
}
err = q.Fetch(context.Background(), id)
if err != ErrRunning {
t.Fatalf("expect not deleted entry so again ErrRunning but got %v", err)
}
})
t.Run("ok", func(t *testing.T) {
err := errors.New("job execution error")
qq := New(SyncMode | TrackJobs)
qq.records.Store(id, err)
result := qq.Fetch(context.Background(), id)
if result != err {
t.Fatalf("expect %v but got %v", err, result)
}
result = qq.Fetch(context.Background(), id)
if result != ErrNotFound {
t.Fatalf("expect deleted entry so ErrNotFound but got %v", result)
}
})
}
// Ensure all jobs added are queued and executed in the order they were added.
func TestSyncQueue_Basic(t *testing.T) {
queue := New(SyncMode)
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
done := make(chan struct{}, 1)
go func() {
if err := queue.Start(ctx); err != nil {
t.Errorf("expected no error but got %v", err)
}
done <- struct{}{}
}()
mu := &sync.Mutex{}
results := make([]string, 0, 3)
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(10 * time.Millisecond)
mu.Lock()
results = append(results, id)
mu.Unlock()
return nil
})
}
waitUntilStarted(t, queue)
id, err := queue.Push(ctx, makeJob("job1"))
check(t, 1, id, err)
id, err = queue.Push(ctx, makeJob("job2"))
check(t, 2, id, err)
id, err = queue.Push(ctx, makeJob("job3"))
check(t, 3, id, err)
// give more time for above josb to finish.
time.Sleep(100 * time.Millisecond)
if err := queue.Stop(ctx); err != nil {
t.Errorf("expected <nil> but got %v", err)
}
select {
case <-done:
case <-time.After(time.Millisecond * 500):
t.Error("running queue did not exit.")
}
if lg := len(results); lg != 3 {
t.Fatalf("invalid results length. expected 3 but got %d", lg)
return
}
if equal := reflect.DeepEqual([]string{"job1", "job2", "job3"}, results); !equal {
t.Errorf("expected %v but got %v", []string{"job1", "job2", "job3"}, results)
}
}
// Ensure a sync queue service provides exact executed jobs results.
func TestSyncQueue_Fetch(t *testing.T) {
queue := New(SyncMode | TrackJobs)
ctx := context.Background()
go func() {
err := queue.Start(ctx)
if err != nil {
t.Errorf("expected no error but got %v", err)
}
}()
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(1 * time.Millisecond)
return nil
})
}
waitUntilStarted(t, queue)
id, err := queue.Push(ctx, makeJob("job1"))
check(t, 1, id, err)
id, err = queue.Push(ctx, makeJob("job2"))
check(t, 2, id, err)
id, err = queue.Push(ctx, makeJob("job3"))
check(t, 3, id, err)
time.Sleep(100 * time.Millisecond)
if err := queue.Stop(ctx); err != nil {
t.Errorf("expected no error but got %v", err)
}
if r := queue.Fetch(context.Background(), 1); r != nil {
t.Errorf("expected <nil> but got %v", r)
}
if r := queue.Fetch(context.Background(), 2); r != nil {
t.Errorf("expected <nil> but got %v", r)
}
if r := queue.Fetch(context.Background(), 3); r != nil {
t.Errorf("expected <nil> but got %v", r)
}
}
// Ensure all jobs pushed are immediately handled and executed concurrently.
func TestAsyncQueue_Basic(t *testing.T) {
queue := New(AsyncMode)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
done := make(chan struct{}, 1)
go func() {
err := queue.Start(ctx)
if err != nil {
t.Errorf("expected no error but got %v", err)
}
done <- struct{}{}
}()
mu := &sync.Mutex{}
results := make([]string, 0, 3)
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(time.Second)
mu.Lock()
results = append(results, id)
mu.Unlock()
return nil
})
}
waitUntilStarted(t, queue)
id, err := queue.Push(ctx, makeJob("job1"))
check(t, 1, id, err)
id, err = queue.Push(ctx, makeJob("job2"))
check(t, 2, id, err)
id, err = queue.Push(ctx, makeJob("job3"))
check(t, 3, id, err)
time.Sleep(1500 * time.Millisecond)
if err := queue.Stop(ctx); err != nil {
t.Errorf("expected no error but got %v", err)
}
select {
case <-done:
case <-time.After(2 * time.Second):
t.Error("running queue did not exit.")
}
mu.Lock()
lg := len(results)
mu.Unlock()
if lg != 3 {
t.Fatalf("invalid results length. expected 3 but got %d", lg)
return
}
expect := []string{"job1", "job2", "job3"}
resultsStr := strings.Join(results, " ")
for _, e := range expect {
if !strings.Contains(resultsStr, e) {
t.Errorf("expected %v but got %v", expect, results)
return
}
}
}
// Ensure pending job into the internal queue will not be executed once the Queue is stopped.
func TestSyncQueue_StopOngoing(t *testing.T) {
queue := New(SyncMode)
ctx, cancel := context.WithTimeout(context.Background(), time.Second*5)
defer cancel()
done := make(chan struct{}, 1)
go func() {
err := queue.Start(ctx)
if err != nil {
t.Errorf("expected no error but got %v", err)
}
done <- struct{}{}
}()
mu := &sync.Mutex{}
results := make([]string, 0, 2)
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(time.Second)
mu.Lock()
results = append(results, id)
mu.Unlock()
return nil
})
}
waitUntilStarted(t, queue)
id, err := queue.Push(ctx, makeJob("job1"))
check(t, 1, id, err)
id, err = queue.Push(ctx, makeJob("job2"))
check(t, 2, id, err)
id, err = queue.Push(ctx, makeJob("job3"))
check(t, 3, id, err)
time.Sleep(2500 * time.Millisecond)
if err := queue.Stop(ctx); err != nil {
t.Errorf("expected no error but got %v", err)
}
select {
case <-done:
case <-time.After(2 * time.Second):
t.Error("running queue did not exit.")
}
mu.Lock()
lg := len(results)
mu.Unlock()
if lg != 2 {
t.Fatalf("invalid results length. expected 2 but got %d", lg)
return
}
mu.Lock()
r := results[0]
mu.Unlock()
if r != "job1" {
t.Errorf("expected %q but got %s", "job1", results[0])
}
mu.Lock()
r = results[1]
mu.Unlock()
if r != "job2" {
t.Errorf("expected %q but got %s", "job2", results[1])
}
}
// Ensure pending job into the internal queue will not be executed once the Queue max uptime reached.
func TestSyncQueue_TimeoutOngoing(t *testing.T) {
queue := New(SyncMode)
ctx, cancel := context.WithTimeout(context.Background(), 2500*time.Millisecond)
defer cancel()
done := make(chan struct{}, 1)
go func() {
err := queue.Start(ctx)
if err != context.DeadlineExceeded {
t.Errorf("expected error %v but got %v", context.DeadlineExceeded, err)
}
done <- struct{}{}
}()
mu := &sync.Mutex{}
results := make([]string, 0, 2)
makeJob := func(id string) Runner {
return basicTestJob(func() error {
time.Sleep(time.Second)
mu.Lock()
results = append(results, id)
mu.Unlock()
return nil
})
}
waitUntilStarted(t, queue)
id, err := queue.Push(ctx, makeJob("job1"))
check(t, 1, id, err)
id, err = queue.Push(ctx, makeJob("job2"))
check(t, 2, id, err)
id, err = queue.Push(ctx, makeJob("job3"))
check(t, 3, id, err)
select {
case <-done:
case <-time.After(3 * time.Second):
t.Error("running queue did not exit.")
}
mu.Lock()
lg := len(results)
mu.Unlock()
if lg != 2 {
t.Fatalf("invalid results. expected 2 items but got %d", lg)
return
}
mu.Lock()
r := results[0]
mu.Unlock()
if r != "job1" {
t.Errorf("expected %q but got %s", "job1", results[0])
}
mu.Lock()
r = results[1]
mu.Unlock()
if r != "job2" {
t.Errorf("expected %q but got %s", "job2", results[1])
}
}
type basicTestJob func() error
func (b basicTestJob) Run() error {
return b()
}
// check verifies if err is nil and if id equals expect.
func check(t *testing.T, expect int64, id int64, err error) {
t.Helper()
if err != nil {
t.Errorf("expected no error but got %v", err)
}
if id != expect {
t.Errorf("expected %d but got %d", expect, id)
}
}
func waitUntilStarted(t *testing.T, queue Queuer) {
t.Helper()
for !queue.IsRunning() {
time.Sleep(1 * time.Microsecond)
}
}