forked from joncrlsn/dque
-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue_test.go
636 lines (518 loc) · 15.7 KB
/
queue_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
// queue_test.go
package dque_test
import (
"fmt"
"math/rand"
"os"
"path/filepath"
"runtime"
"sync"
"testing"
"time"
"github.com/VoIPGRID/dque/v3"
)
// item2 is the thing we'll be storing in the queue
type item2 struct {
Id int
}
// item2Builder creates a new item and returns a pointer to it.
// This is used when we load a segment of the queue from disk.
func item2Builder() interface{} {
return &item2{}
}
// Adds 1 and removes 1 in a loop to ensure that when we've filled
// up the first segment that we delete it and move on to the next segment
func TestQueue_AddRemoveLoop(t *testing.T) {
testQueue_AddRemoveLoop(t, true /* true=turbo */)
testQueue_AddRemoveLoop(t, false /* true=turbo */)
}
func testQueue_AddRemoveLoop(t *testing.T, turbo bool) {
qName := "test1"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory", err)
}
// Create a new queue with segment size of 3
var err error
q := newQ(t, qName, turbo)
for i := 0; i < 4; i++ {
if err := q.Enqueue(&item2{i}); err != nil {
t.Fatal("Error enqueueing", err)
}
_, err = q.Dequeue()
if err != nil {
t.Fatal("Error dequeueing", err)
}
}
assert(t, q.Size() == 0, "Size is not 0")
firstSegNum, lastSegNum := q.SegmentNumbers()
// Assert that we have just one segment
assert(t, firstSegNum == lastSegNum, "The first segment must match the last")
// Assert that the first segment is #2
assert(t, firstSegNum == 2, "The first segment is not 2")
// Now reopen the queue and check our assertions again.
q.Close()
q = openQ(t, qName, turbo)
firstSegNum, lastSegNum = q.SegmentNumbers()
// Assert that we have just one segment
assert(t, firstSegNum == lastSegNum, "After opening, the first segment must match the second")
// Assert that the first segment is #2
assert(t, firstSegNum == 2, "After opening, the first segment is not 2")
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error cleaning up the queue directory", err)
}
}
// Adds 2 and removes 1 in a loop to ensure that when we've filled
// up the first segment that we delete it and move on to the next segment
func TestQueue_Add2Remove1(t *testing.T) {
testQueue_Add2Remove1(t, true /* true=turbo */)
testQueue_Add2Remove1(t, false /* true=turbo */)
}
func testQueue_Add2Remove1(t *testing.T, turbo bool) {
qName := "test1"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory", err)
}
// Create a new queue with segment size of 3
var err error
q := newQ(t, qName, turbo)
// Add 2 and remove one each loop
for i := 0; i < 4; i = i + 2 {
var item interface{}
if err := q.Enqueue(&item2{i}); err != nil {
t.Fatal("Error enqueueing", err)
}
if err := q.Enqueue(&item2{i + 1}); err != nil {
t.Fatal("Error enqueueing", err)
}
item, err = q.Dequeue()
if err != nil {
t.Fatal("Error dequeueing", err)
}
assert(t, item != nil, "Item is nil")
}
firstSegNum, lastSegNum := q.SegmentNumbers()
// Assert that we have more than one segment
assert(t, firstSegNum < lastSegNum, "The first segment cannot match the second")
// Assert that the first segment is #2
assert(t, lastSegNum == 2, "The last segment must be 2")
// Now reopen the queue and check our assertions again.
q.Close()
q = openQ(t, qName, turbo)
firstSegNum, lastSegNum = q.SegmentNumbers()
// Assert that we have more than one segment
assert(t, firstSegNum < lastSegNum, "After opening, the first segment can not match the second")
// Assert that the first segment is #2
assert(t, lastSegNum == 2, "After opening, the last segment must be 2")
// Test Peek to make sure the size doesn't change
assert(t, q.Size() == 2, "Queue size is not 2 before peeking")
obj, err := q.Peek()
if err != nil {
t.Fatal("Error peeking at the queue", err)
}
assert(t, q.Size() == 2, "After peaking, aueue size must still be 2")
assert(t, obj != nil, "Peeked object must not be nil.")
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error cleaning up the queue directory", err)
}
}
// Adds 9 and removes 8
func TestQueue_Add9Remove8(t *testing.T) {
testQueue_Add9Remove8(t, true /* true = turbo */)
testQueue_Add9Remove8(t, false /* true = turbo */)
}
func testQueue_Add9Remove8(t *testing.T, turbo bool) {
qName := "test1"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory", err)
}
// Create new queue with segment size 3
q := newQ(t, qName, turbo)
// Enqueue 9 items
for i := 0; i < 9; i++ {
if err := q.Enqueue(&item2{i}); err != nil {
t.Fatal("Error enqueueing", err)
}
}
// Check the Size calculation
assert(t, q.Size() == 9, "the size is calculated wrong. Should be 9")
firstSegNum, lastSegNum := q.SegmentNumbers()
// Assert that the first segment is #1
assert(t, firstSegNum == 1, "the first segment is not 1")
// Assert that the last segment is #4
assert(t, lastSegNum == 3, "the last segment is not 3")
// Dequeue 8 items
for i := 0; i < 8; i++ {
iface, err := q.Dequeue()
if err != nil {
t.Fatal("Error dequeueing:", err)
}
// Check the Size calculation
assert(t, 8-i == q.Size(), "the size is calculated wrong.")
item, ok := iface.(item2)
if ok {
fmt.Printf("Dequeued %T %t %#v\n", item, ok, item)
assert(t, i == item.Id, "Unexpected itemId")
} else {
item, ok := iface.(*item2)
assert(t, ok, "Dequeued object is not of type *item2")
assert(t, i == item.Id, "Unexpected itemId")
}
}
firstSegNum, lastSegNum = q.SegmentNumbers()
// Assert that we have only one segment
assert(t, firstSegNum == lastSegNum, "The first segment must match the second")
// Assert that the first segment is #3
assert(t, firstSegNum == 3, "The last segment is not 3")
// Now reopen the queue and check our assertions again.
q.Close()
_ = openQ(t, qName, turbo)
// Assert that we have more than one segment
assert(t, firstSegNum == lastSegNum, "After opening, the first segment must match the second")
// Assert that the last segment is #3
assert(t, lastSegNum == 3, "After opening, the last segment is not 3")
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error cleaning up the queue directory:", err)
}
}
func TestQueue_EmptyDequeue(t *testing.T) {
testQueue_EmptyDequeue(t, true /* true=turbo */)
testQueue_EmptyDequeue(t, false /* true=turbo */)
}
func testQueue_EmptyDequeue(t *testing.T, turbo bool) {
qName := "testEmptyDequeue"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
// Create new queue
q := newQ(t, qName, turbo)
assert(t, q.Size() == 0, "Expected an empty queue")
// Dequeue an item from the empty queue
item, err := q.Dequeue()
assert(t, dque.ErrEmpty == err, "Expected an ErrEmpty error")
assert(t, item == nil, "Expected nil because queue is empty")
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error cleaning up the queue directory:", err)
}
}
func TestQueue_NewOrOpen(t *testing.T) {
testQueue_NewOrOpen(t, true /* true=turbo */)
testQueue_NewOrOpen(t, false /* true=turbo */)
}
func testQueue_NewOrOpen(t *testing.T, turbo bool) {
qName := "testNewOrOpen"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
// Create new queue with newOrOpen
q := newOrOpenQ(t, qName, turbo)
q.Close()
// Open the same queue with newOrOpen
q = newOrOpenQ(t, qName, turbo)
q.Close()
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error cleaning up the queue directory:", err)
}
}
func TestQueue_Turbo(t *testing.T) {
qName := "testNewOrOpen"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
// Create new queue
q := newQ(t, qName, false)
if err := q.TurboOff(); err == nil {
t.Fatal("Expected an error")
}
if err := q.TurboSync(); err == nil {
t.Fatal("Expected an error")
}
if err := q.TurboOn(); err != nil {
t.Fatal("Error turning on turbo:", err)
}
if err := q.TurboOn(); err == nil {
t.Fatal("Expected an error")
}
if err := q.TurboSync(); err != nil {
t.Fatal("Error running TurboSync:", err)
}
// Enqueue 1000 items
start := time.Now()
for i := 0; i < 1000; i++ {
if err := q.Enqueue(&item2{i}); err != nil {
t.Fatal("Error enqueueing:", err)
}
}
elapsedTurbo := time.Since(start)
assert(t, q.Turbo(), "Expected turbo to be on")
if err := q.TurboOff(); err != nil {
t.Fatal("Error turning off turbo:", err)
}
// Enqueue 1000 items
start = time.Now()
for i := 0; i < 1000; i++ {
if err := q.Enqueue(&item2{i}); err != nil {
t.Fatal("Error enqueueing:", err)
}
}
elapsedSafe := time.Since(start)
assert(t, elapsedTurbo < elapsedSafe/2, "Turbo time (%v) must be faster than safe mode (%v)", elapsedTurbo, elapsedSafe)
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error cleaning up the queue directory:", err)
}
}
func TestQueue_NewFlock(t *testing.T) {
qName := "testFlock"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error cleaning up the queue directory:", err)
}
// New and Close a DQue properly should work
q, err := dque.New(qName, ".", 3, item2Builder)
if err != nil {
t.Fatal("Error creating dque:", err)
}
err = q.Close()
if err != nil {
t.Fatal("Error closing dque:", err)
}
// Double-open should fail
q, err = dque.Open(qName, ".", 3, item2Builder)
if err != nil {
t.Fatal("Error opening dque:", err)
}
_, err = dque.Open(qName, ".", 3, item2Builder)
if err == nil {
t.Fatal("No error during double-open dque")
}
err = q.Close()
if err != nil {
t.Fatal("Error closing dque:", err)
}
// Double-close should fail
q, err = dque.Open(qName, ".", 3, item2Builder)
if err != nil {
t.Fatal("Error opening dque:", err)
}
err = q.Close()
if err != nil {
t.Fatal("Error closing dque:", err)
}
err = q.Close()
if err == nil {
t.Fatal("No error during double-closing dque")
}
// Cleanup
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
}
func TestQueue_UseAfterClose(t *testing.T) {
qName := "testUseAfterClose"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error cleaning up the queue directory:", err)
}
q, err := dque.New(qName, ".", 3, item2Builder)
if err != nil {
t.Fatal("Error creating dque:", err)
}
err = q.Enqueue(&item2{0})
if err != nil {
t.Fatal("Error enqueing item:", err)
}
err = q.Close()
if err != nil {
t.Fatal("Error closing dque:", err)
}
queueClosedError := "queue is closed"
err = q.Close()
assert(t, err.Error() == queueClosedError, "Expected error not found", err)
err = q.Enqueue(&item2{0})
assert(t, err.Error() == queueClosedError, "Expected error not found", err)
_, err = q.Dequeue()
assert(t, err.Error() == queueClosedError, "Expected error not found", err)
_, err = q.Peek()
assert(t, err.Error() == queueClosedError, "Expected error not found", err)
s := q.Size()
assert(t, s == 0, "Expected error")
s = q.SizeUnsafe()
assert(t, s == 0, "Expected error")
err = q.TurboOn()
assert(t, err.Error() == queueClosedError, "Expected error not found", err)
err = q.TurboOff()
assert(t, err.Error() == queueClosedError, "Expected error not found", err)
err = q.TurboSync()
assert(t, err.Error() == queueClosedError, "Expected error not found", err)
// Cleanup
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
}
func TestQueue_BlockingBehaviour(t *testing.T) {
qName := "testBlocking"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
q := newQ(t, qName, false)
go func() {
err := q.Enqueue(&item2{0})
assert(t, err == nil, "Expected no error")
}()
x, err := q.PeekBlock()
assert(t, err == nil, "Expected no error")
assert(t, x != nil, "Item is nil")
x, err = q.DequeueBlock()
assert(t, err == nil, "Expected no error")
assert(t, x != nil, "Item is nil")
x, err = q.Dequeue()
assert(t, err == dque.ErrEmpty, "Expected error not found")
timeout := time.After(3 * time.Second)
done := make(chan bool)
go func() {
x, err = q.DequeueBlock()
assert(t, err == nil, "Expected no error")
assert(t, x != nil, "Item is nil")
done <- true
}()
go func() {
time.Sleep(1 * time.Second)
err := q.Enqueue(&item2{2})
assert(t, err == nil, "Expected no error")
}()
select {
case <-timeout:
t.Fatal("Test didn't finish in time")
case <-done:
}
// Cleanup
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
}
func TestQueue_BlockingWithClose(t *testing.T) {
qName := "testBlockingWithClose"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
q := newQ(t, qName, false)
go func() {
time.Sleep(1 * time.Second)
err := q.Close()
assert(t, err == nil, "Expected no error")
}()
timeout := time.After(3 * time.Second)
done := make(chan bool)
go func() {
// The queue is empty,
// so DequeueBlock should really block and wait,
// until the other goroutine calls Close,
// and the Close should wake-up this DequeueBlock block,
// and return an error because the queue is now closed.
_, err := q.DequeueBlock()
assert(t, err == dque.ErrQueueClosed, "Expected ErrQueueClosed error")
done <- true
}()
select {
case <-timeout:
t.Fatal("Test didn't finish in time")
case <-done:
}
// Cleanup
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
}
func TestQueue_BlockingAggresive(t *testing.T) {
rand.Seed(0) // ensure we have reproducible sleeps
qName := "testBlockingAggresive"
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
q := newQ(t, qName, false)
numProducers := 5
numItemsPerProducer := 50
numConsumers := 25
done := make(chan bool)
var wg sync.WaitGroup
wg.Add(numProducers * numItemsPerProducer)
go func() {
wg.Wait()
q.Close()
done <- true
}()
// producers
for p := 0; p < numProducers; p++ {
go func(producer int) {
for i := 0; i < numItemsPerProducer; i++ {
s := rand.Intn(150)
time.Sleep(time.Duration(s) * time.Millisecond)
err := q.Enqueue(&item2{i})
assert(t, err == nil, "Expected no error", err)
fmt.Println("Enqueued item", i, "by producer", producer, "after sleeping", s)
}
}(p)
}
// consumers
for c := 0; c < numConsumers; c++ {
go func(consumer int) {
for {
x, err := q.DequeueBlock()
if err == dque.ErrQueueClosed {
return
}
assert(t, err == nil, "Expected no error")
fmt.Println("Dequeued item", x, "by consumer", consumer)
wg.Done()
}
}(c)
}
timeout := time.After(10 * time.Second)
select {
case <-timeout:
t.Fatal("Test didn't finish in time")
case <-done:
}
// Cleanup
if err := os.RemoveAll(qName); err != nil {
t.Fatal("Error removing queue directory:", err)
}
}
func newOrOpenQ(t *testing.T, qName string, turbo bool) *dque.DQue {
// Create a new segment with segment size of 3
q, err := dque.NewOrOpen(qName, ".", 3, item2Builder)
if err != nil {
t.Fatal("Error creating or opening dque:", err)
}
if turbo {
_ = q.TurboOn()
}
return q
}
func newQ(t *testing.T, qName string, turbo bool) *dque.DQue {
// Create a new segment with segment size of 3
q, err := dque.New(qName, ".", 3, item2Builder)
if err != nil {
t.Fatal("Error creating new dque:", err)
}
if turbo {
_ = q.TurboOn()
}
return q
}
func openQ(t *testing.T, qName string, turbo bool) *dque.DQue {
// Open an existing segment with segment size of 3
q, err := dque.Open(qName, ".", 3, item2Builder)
if err != nil {
t.Fatal("Error opening dque:", err)
}
if turbo {
_ = q.TurboOn()
}
return q
}
// assert fails the test if the condition is false.
func assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}