-
Notifications
You must be signed in to change notification settings - Fork 206
/
queue_cluster_test.go
929 lines (774 loc) · 28.4 KB
/
queue_cluster_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
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
package rmq
import (
"fmt"
"math"
"strconv"
"testing"
"time"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestClusterConnections(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connFlush, err := OpenClusterConnection("flush", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
assert.NoError(t, connFlush.stopHeartbeat())
assert.Equal(t, ErrorNotFound, connFlush.stopHeartbeat())
assert.NoError(t, connFlush.flushDb())
connClean, err := OpenClusterConnection("clean", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
require.NotNil(t, connClean)
_, err = NewCleaner(connClean).Clean()
require.NoError(t, err)
connections, err := connClean.getConnections()
connCount := len(connections)
assert.NoError(t, err)
conn1, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
connections, err = connClean.getConnections()
assert.NoError(t, err)
assert.Len(t, connections, connCount+1) // connection q1 was added
assert.Equal(t, ErrorNotFound, connClean.hijackConnection("nope").checkHeartbeat())
assert.NoError(t, conn1.checkHeartbeat())
conn2, err := OpenClusterConnection("conn2", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
connections, err = connClean.getConnections()
assert.NoError(t, err)
assert.Len(t, connections, connCount+2) // connection q2 was added
assert.NoError(t, conn1.checkHeartbeat())
assert.NoError(t, conn2.checkHeartbeat())
assert.Equal(t, ErrorNotFound, connClean.hijackConnection("nope").stopHeartbeat())
assert.NoError(t, conn1.stopHeartbeat())
assert.Equal(t, ErrorNotFound, conn1.checkHeartbeat())
assert.NoError(t, conn2.checkHeartbeat())
connections, err = connClean.getConnections()
assert.NoError(t, err)
assert.Len(t, connections, connCount+2)
assert.NoError(t, conn2.stopHeartbeat())
assert.Equal(t, ErrorNotFound, conn1.checkHeartbeat())
assert.Equal(t, ErrorNotFound, conn2.checkHeartbeat())
connections, err = connClean.getConnections()
assert.NoError(t, err)
assert.Len(t, connections, connCount+2)
assert.NoError(t, connClean.stopHeartbeat())
}
func TestClusterConnectionQueues(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
require.NotNil(t, connection)
assert.NoError(t, connection.unlistAllQueues())
queues, err := connection.GetOpenQueues()
assert.NoError(t, err)
assert.Len(t, queues, 0)
queue1, err := connection.OpenQueue("conn1-q1")
assert.NoError(t, err)
require.NotNil(t, queue1)
queues, err = connection.GetOpenQueues()
assert.NoError(t, err)
assert.Equal(t, []string{"conn1-q1"}, queues)
queues, err = connection.getConsumingQueues()
assert.NoError(t, err)
assert.Len(t, queues, 0)
assert.NoError(t, queue1.StartConsuming(1, time.Millisecond))
queues, err = connection.getConsumingQueues()
assert.NoError(t, err)
assert.Equal(t, []string{"conn1-q1"}, queues)
queue2, err := connection.OpenQueue("conn1-q2")
assert.NoError(t, err)
require.NotNil(t, queue2)
queues, err = connection.GetOpenQueues()
assert.NoError(t, err)
assert.Len(t, queues, 2)
queues, err = connection.getConsumingQueues()
assert.NoError(t, err)
assert.Len(t, queues, 1)
assert.NoError(t, queue2.StartConsuming(1, time.Millisecond))
queues, err = connection.getConsumingQueues()
assert.NoError(t, err)
assert.Len(t, queues, 2)
<-queue2.StopConsuming()
assert.NoError(t, queue2.closeInStaleConnection())
queues, err = connection.GetOpenQueues()
assert.NoError(t, err)
assert.Len(t, queues, 2)
queues, err = connection.getConsumingQueues()
assert.NoError(t, err)
assert.Equal(t, []string{"conn1-q1"}, queues)
<-queue1.StopConsuming()
assert.NoError(t, queue1.closeInStaleConnection())
queues, err = connection.GetOpenQueues()
assert.NoError(t, err)
assert.Len(t, queues, 2)
queues, err = connection.getConsumingQueues()
assert.NoError(t, err)
assert.Len(t, queues, 0)
readyCount, rejectedCount, err := queue1.Destroy()
assert.NoError(t, err)
assert.Equal(t, int64(0), readyCount)
assert.Equal(t, int64(0), rejectedCount)
queues, err = connection.GetOpenQueues()
assert.NoError(t, err)
assert.Equal(t, []string{"conn1-q2"}, queues)
queues, err = connection.getConsumingQueues()
assert.NoError(t, err)
assert.Len(t, queues, 0)
assert.NoError(t, connection.stopHeartbeat())
}
func TestClusterQueueCommon(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
require.NotNil(t, connection)
queue, err := connection.OpenQueue("queue-q")
assert.NoError(t, err)
require.NotNil(t, queue)
_, err = queue.PurgeReady()
assert.NoError(t, err)
eventuallyReady(t, queue, 0)
assert.NoError(t, queue.Publish("queue-d1"))
eventuallyReady(t, queue, 1)
assert.NoError(t, queue.Publish("queue-d2"))
eventuallyReady(t, queue, 2)
count, err := queue.PurgeReady()
assert.Equal(t, int64(2), count)
eventuallyReady(t, queue, 0)
count, err = queue.PurgeReady()
assert.Equal(t, int64(0), count)
queues, err := connection.getConsumingQueues()
assert.NoError(t, err)
assert.Len(t, queues, 0)
assert.NoError(t, queue.StartConsuming(10, time.Millisecond))
assert.Equal(t, ErrorAlreadyConsuming, queue.StartConsuming(10, time.Millisecond))
cons1name, err := queue.AddConsumer("queue-cons1", NewTestConsumer("queue-A"))
assert.NoError(t, err)
time.Sleep(time.Millisecond)
queues, err = connection.getConsumingQueues()
assert.NoError(t, err)
assert.Len(t, queues, 1)
consumers, err := queue.getConsumers()
assert.NoError(t, err)
assert.Equal(t, []string{cons1name}, consumers)
_, err = queue.AddConsumer("queue-cons2", NewTestConsumer("queue-B"))
assert.NoError(t, err)
consumers, err = queue.getConsumers()
assert.NoError(t, err)
assert.Len(t, consumers, 2)
<-queue.StopConsuming()
assert.NoError(t, connection.stopHeartbeat())
}
func TestClusterConsumerCommon(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
require.NotNil(t, connection)
queue1, err := connection.OpenQueue("cons-q")
assert.NoError(t, err)
require.NotNil(t, queue1)
_, err = queue1.PurgeReady()
assert.NoError(t, err)
consumer := NewTestConsumer("cons-A")
consumer.AutoAck = false
assert.NoError(t, queue1.StartConsuming(10, time.Millisecond))
_, err = queue1.AddConsumer("cons-cons", consumer)
assert.NoError(t, err)
assert.Nil(t, consumer.Last())
assert.NoError(t, queue1.Publish("cons-d1"))
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 1)
require.NotNil(t, consumer.Last())
assert.Equal(t, "cons-d1", consumer.Last().Payload())
assert.NoError(t, queue1.Publish("cons-d2"))
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 2)
assert.Equal(t, "cons-d2", consumer.Last().Payload())
assert.NoError(t, consumer.Deliveries()[0].Ack())
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 1)
assert.NoError(t, consumer.Deliveries()[1].Ack())
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 0)
assert.Equal(t, ErrorNotFound, consumer.Deliveries()[0].Ack())
assert.NoError(t, queue1.Publish("cons-d3"))
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 1)
eventuallyRejected(t, queue1, 0)
assert.Equal(t, "cons-d3", consumer.Last().Payload())
assert.NoError(t, consumer.Last().Reject())
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 0)
eventuallyRejected(t, queue1, 1)
assert.NoError(t, queue1.Publish("cons-d4"))
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 1)
eventuallyRejected(t, queue1, 1)
assert.Equal(t, "cons-d4", consumer.Last().Payload())
assert.NoError(t, consumer.Last().Reject())
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 0)
eventuallyRejected(t, queue1, 2)
count, err := queue1.PurgeRejected()
assert.NoError(t, err)
assert.Equal(t, int64(2), count)
eventuallyRejected(t, queue1, 0)
count, err = queue1.PurgeRejected()
assert.NoError(t, err)
assert.Equal(t, int64(0), count)
queue2, err := connection.OpenQueue("cons-func-q")
assert.NoError(t, err)
assert.NoError(t, queue2.StartConsuming(10, time.Millisecond))
payloadChan := make(chan string, 1)
payload := "cons-func-payload"
_, err = queue2.AddConsumerFunc("cons-func", func(delivery Delivery) {
err = delivery.Ack()
assert.NoError(t, err)
payloadChan <- delivery.Payload()
})
assert.NoError(t, err)
assert.NoError(t, queue2.Publish(payload))
eventuallyReady(t, queue2, 0)
eventuallyUnacked(t, queue2, 0)
assert.Equal(t, payload, <-payloadChan)
<-queue1.StopConsuming()
<-queue2.StopConsuming()
assert.NoError(t, connection.stopHeartbeat())
}
func TestClusterMulti(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("multi-q")
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
for i := 0; i < 20; i++ {
err := queue.Publish(fmt.Sprintf("multi-d%d", i))
assert.NoError(t, err)
}
eventuallyReady(t, queue, 20)
eventuallyUnacked(t, queue, 0)
assert.NoError(t, queue.StartConsuming(10, time.Millisecond))
// Assert that eventually the ready count drops to 10 and unacked rises to 10
// TODO use the util funcs instead
assert.Eventually(t, func() bool {
readyCount, err := queue.readyCount()
if err != nil {
return false
}
unackedCount, err := queue.unackedCount()
if err != nil {
return false
}
return readyCount == 10 && unackedCount == 10
}, 10*time.Second, 2*time.Millisecond)
consumer := NewTestConsumer("multi-cons")
consumer.AutoAck = false
consumer.AutoFinish = false
_, err = queue.AddConsumer("multi-cons", consumer)
assert.NoError(t, err)
// After we add the consumer - ready and unacked do not change
eventuallyReady(t, queue, 10)
eventuallyUnacked(t, queue, 10)
assert.NoError(t, consumer.Last().Ack())
// Assert that after the consumer acks a message the ready count drops to 9 and unacked remains at 10
// TODO use util funcs instead
assert.Eventually(t, func() bool {
readyCount, err := queue.readyCount()
if err != nil {
return false
}
unackedCount, err := queue.unackedCount()
if err != nil {
return false
}
return readyCount == 9 && unackedCount == 10
}, 10*time.Second, 2*time.Millisecond)
consumer.Finish()
// Assert that after the consumer finishes processing the first message ready and unacked do not change
eventuallyReady(t, queue, 9)
eventuallyUnacked(t, queue, 10)
assert.NoError(t, consumer.Last().Ack())
// Assert that after the consumer acks a message the ready count drops to 8 and unacked remains at 10
// TODO use the util funcs instead
assert.Eventually(t, func() bool {
readyCount, err := queue.readyCount()
if err != nil {
return false
}
unackedCount, err := queue.unackedCount()
if err != nil {
return false
}
return readyCount == 8 && unackedCount == 10
}, 10*time.Second, 2*time.Millisecond)
consumer.Finish()
// Assert that after the consumer finishes processing the second message ready and unacked do not change
eventuallyReady(t, queue, 8)
eventuallyUnacked(t, queue, 10)
// This prevents the consumer from blocking internally inside a call to Consume, which allows the queue to complete
// the call to StopConsuming
consumer.FinishAll()
<-queue.StopConsuming()
assert.NoError(t, connection.stopHeartbeat())
}
func TestClusterBatch(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("batch-q")
assert.NoError(t, err)
_, err = queue.PurgeRejected()
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
for i := 0; i < 5; i++ {
err := queue.Publish(fmt.Sprintf("batch-d%d", i))
assert.NoError(t, err)
}
assert.NoError(t, queue.StartConsuming(10, time.Millisecond))
eventuallyUnacked(t, queue, 5)
consumer := NewTestBatchConsumer()
_, err = queue.AddBatchConsumer("batch-cons", 2, 50*time.Millisecond, consumer)
assert.NoError(t, err)
assert.Eventually(t, func() bool {
return len(consumer.Last()) == 2
}, 10*time.Second, 2*time.Millisecond)
assert.Equal(t, "batch-d0", consumer.Last()[0].Payload())
assert.Equal(t, "batch-d1", consumer.Last()[1].Payload())
assert.NoError(t, consumer.Last()[0].Reject())
assert.NoError(t, consumer.Last()[1].Ack())
eventuallyUnacked(t, queue, 3)
eventuallyRejected(t, queue, 1)
consumer.Finish()
assert.Eventually(t, func() bool {
return len(consumer.Last()) == 2
}, 10*time.Second, 2*time.Millisecond)
assert.Equal(t, "batch-d2", consumer.Last()[0].Payload())
assert.Equal(t, "batch-d3", consumer.Last()[1].Payload())
assert.NoError(t, consumer.Last()[0].Reject())
assert.NoError(t, consumer.Last()[1].Ack())
eventuallyUnacked(t, queue, 1)
eventuallyRejected(t, queue, 2)
consumer.Finish()
// Last Batch is cleared out
assert.Len(t, consumer.Last(), 0)
eventuallyUnacked(t, queue, 1)
eventuallyRejected(t, queue, 2)
// After a pause the batch consumer will pull down another batch
assert.Eventually(t, func() bool {
return len(consumer.Last()) == 1
}, 10*time.Second, 2*time.Millisecond)
assert.Equal(t, "batch-d4", consumer.Last()[0].Payload())
assert.NoError(t, consumer.Last()[0].Reject())
eventuallyUnacked(t, queue, 0)
eventuallyRejected(t, queue, 3)
}
func TestClusterReturnRejected(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("return-q")
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
for i := 0; i < 6; i++ {
err := queue.Publish(fmt.Sprintf("return-d%d", i))
assert.NoError(t, err)
}
eventuallyReady(t, queue, 6)
eventuallyUnacked(t, queue, 0)
eventuallyRejected(t, queue, 0)
assert.NoError(t, queue.StartConsuming(10, time.Millisecond))
eventuallyReady(t, queue, 0)
eventuallyUnacked(t, queue, 6)
eventuallyRejected(t, queue, 0)
consumer := NewTestConsumer("return-cons")
consumer.AutoAck = false
_, err = queue.AddConsumer("cons", consumer)
assert.NoError(t, err)
eventuallyReady(t, queue, 0)
eventuallyUnacked(t, queue, 6)
eventuallyRejected(t, queue, 0)
assert.Len(t, consumer.Deliveries(), 6)
assert.NoError(t, consumer.Deliveries()[0].Reject())
assert.NoError(t, consumer.Deliveries()[1].Ack())
assert.NoError(t, consumer.Deliveries()[2].Reject())
assert.NoError(t, consumer.Deliveries()[3].Reject())
// delivery 4 still open
assert.NoError(t, consumer.Deliveries()[5].Reject())
eventuallyReady(t, queue, 0)
eventuallyUnacked(t, queue, 1) // delivery 4
eventuallyRejected(t, queue, 4) // delivery 0, 2, 3, 5
<-queue.StopConsuming()
n, err := queue.ReturnRejected(2)
assert.NoError(t, err)
assert.Equal(t, int64(2), n)
eventuallyReady(t, queue, 2)
eventuallyUnacked(t, queue, 1) // delivery 4
eventuallyRejected(t, queue, 2) // delivery 3, 5
n, err = queue.ReturnRejected(math.MaxInt64)
assert.NoError(t, err)
assert.Equal(t, int64(2), n)
eventuallyReady(t, queue, 4)
eventuallyUnacked(t, queue, 1) // delivery 4
eventuallyRejected(t, queue, 0)
}
func TestClusterPushQueue(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
// using random queue names here to make test more robust
queueName1 := RandomString(6)
queueName2 := RandomString(6)
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue1, err := connection.OpenQueue(queueName1)
assert.NoError(t, err)
queue2, err := connection.OpenQueue(queueName2)
assert.NoError(t, err)
queue1.SetPushQueue(queue2)
assert.Equal(t, queue2.(*redisQueue).readyKey, queue1.(*redisQueue).pushKey)
consumer1 := NewTestConsumer("push-cons")
consumer1.AutoAck = false
consumer1.AutoFinish = false
assert.NoError(t, queue1.StartConsuming(10, time.Millisecond))
_, err = queue1.AddConsumer("push-cons", consumer1)
assert.NoError(t, err)
consumer2 := NewTestConsumer("push-cons")
consumer2.AutoAck = false
consumer2.AutoFinish = false
assert.NoError(t, queue2.StartConsuming(10, time.Millisecond))
_, err = queue2.AddConsumer("push-cons", consumer2)
assert.NoError(t, err)
assert.NoError(t, queue1.Publish("d1"))
eventuallyUnacked(t, queue1, 1)
require.Len(t, consumer1.Deliveries(), 1)
assert.NoError(t, consumer1.Last().Push())
eventuallyUnacked(t, queue1, 0)
eventuallyUnacked(t, queue2, 1)
require.Len(t, consumer2.Deliveries(), 1)
assert.NoError(t, consumer2.Last().Push())
eventuallyRejected(t, queue2, 1)
}
func TestClusterStopConsuming_Consumer(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("consume-q")
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
deliveryCount := int64(30)
for i := int64(0); i < deliveryCount; i++ {
err := queue.Publish("d" + strconv.FormatInt(i, 10))
assert.NoError(t, err)
}
assert.NoError(t, queue.StartConsuming(20, time.Millisecond))
var consumers []*TestConsumer
for i := 0; i < 10; i++ {
consumer := NewTestConsumer("c" + strconv.Itoa(i))
consumers = append(consumers, consumer)
_, err = queue.AddConsumer("consume", consumer)
assert.NoError(t, err)
}
finishedChan := queue.StopConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
var consumedCount int64
for i := 0; i < 10; i++ {
consumedCount += int64(len(consumers[i].Deliveries()))
}
// make sure all deliveries are either ready, unacked or consumed (acked)
assert.Eventually(t, func() bool {
readyCount, err := queue.readyCount()
if err != nil {
return false
}
unackedCount, err := queue.unackedCount()
if err != nil {
return false
}
return readyCount+unackedCount+consumedCount == deliveryCount
}, 10*time.Second, 2*time.Millisecond)
assert.NoError(t, connection.stopHeartbeat())
}
func TestClusterStopConsuming_BatchConsumer(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("batchConsume-q")
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
deliveryCount := int64(50)
for i := int64(0); i < deliveryCount; i++ {
err := queue.Publish("d" + strconv.FormatInt(i, 10))
assert.NoError(t, err)
}
assert.NoError(t, queue.StartConsuming(20, time.Millisecond))
var consumers []*TestBatchConsumer
for i := 0; i < 10; i++ {
consumer := NewTestBatchConsumer()
consumer.AutoFinish = true
consumers = append(consumers, consumer)
_, err = queue.AddBatchConsumer("consume", 5, time.Second, consumer)
assert.NoError(t, err)
}
finishedChan := queue.StopConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
var consumedCount int64
for i := 0; i < 10; i++ {
consumedCount += consumers[i].Consumed()
}
// make sure all deliveries are either ready, unacked or consumed (acked)
assert.Eventually(t, func() bool {
readyCount, err := queue.readyCount()
if err != nil {
return false
}
unackedCount, err := queue.unackedCount()
if err != nil {
return false
}
return readyCount+unackedCount+consumedCount == deliveryCount
}, 10*time.Second, 2*time.Millisecond)
assert.NoError(t, connection.stopHeartbeat())
}
func TestClusterConnection_StopAllConsuming_CalledTwice(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
finishedChan := connection.StopAllConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
// check that heartbeat has been stopped
assert.Equal(t, connection.checkHeartbeat(), ErrorNotFound)
// it's safe to call StopAllConsuming again
finishedChan = connection.StopAllConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
// heartbeat is still stopped of course
assert.Equal(t, connection.checkHeartbeat(), ErrorNotFound)
}
func TestClusterConnection_StopAllConsuming_CantOpenQueue(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
finishedChan := connection.StopAllConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
// check that heartbeat has been stopped
assert.Equal(t, connection.checkHeartbeat(), ErrorNotFound)
queue, err := connection.OpenQueue("consume-q")
require.Nil(t, queue)
require.Equal(t, ErrorConsumingStopped, err)
}
func TestClusterConnection_StopAllConsuming_CantStartConsuming(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("consume-q")
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
finishedChan := connection.StopAllConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
// check that heartbeat has been stopped
assert.Equal(t, connection.checkHeartbeat(), ErrorNotFound)
err = queue.StartConsuming(20, time.Millisecond)
require.Equal(t, ErrorConsumingStopped, err)
}
func TestClusterQueue_StopConsuming_CantStartConsuming(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("consume-q")
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
finishedChan := queue.StopConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
err = queue.StartConsuming(20, time.Millisecond)
require.Equal(t, ErrorConsumingStopped, err)
}
func TestClusterConnection_StopAllConsuming_CantAddConsumer(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("consume-q")
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
assert.NoError(t, queue.StartConsuming(20, time.Millisecond))
finishedChan := connection.StopAllConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
// check that heartbeat has been stopped
assert.Equal(t, connection.checkHeartbeat(), ErrorNotFound)
_, err = queue.AddConsumer("late-consume", NewTestConsumer("late-consumer"))
require.Equal(t, ErrorConsumingStopped, err)
}
func TestClusterQueue_StopConsuming_CantAddConsumer(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
queue, err := connection.OpenQueue("consume-q")
assert.NoError(t, err)
_, err = queue.PurgeReady()
assert.NoError(t, err)
assert.NoError(t, queue.StartConsuming(20, time.Millisecond))
finishedChan := queue.StopConsuming()
require.NotNil(t, finishedChan)
<-finishedChan // wait for stopping to finish
_, err = queue.AddConsumer("late-consume", NewTestConsumer("late-consumer"))
require.Equal(t, ErrorConsumingStopped, err)
}
func BenchmarkClusterQueue(b *testing.B) {
// open queue
redisOptions, closer := testClusterRedis(b)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(b, err)
queueName := fmt.Sprintf("bench-q%d", b.N)
queue, err := connection.OpenQueue(queueName)
assert.NoError(b, err)
assert.NoError(b, queue.StartConsuming(10, time.Millisecond))
// add some consumers
numConsumers := 10
var consumers []*TestConsumer
for i := 0; i < numConsumers; i++ {
consumer := NewTestConsumer("bench-A")
// consumer.SleepDuration = time.Microsecond
consumers = append(consumers, consumer)
_, err = queue.AddConsumer("bench-cons", consumer)
assert.NoError(b, err)
}
// publish deliveries
for i := 0; i < b.N; i++ {
err := queue.Publish("bench-d")
assert.NoError(b, err)
}
// wait until all are consumed
for {
ready, err := queue.readyCount()
assert.NoError(b, err)
unacked, err := queue.unackedCount()
assert.NoError(b, err)
fmt.Printf("%d unacked %d %d\n", b.N, ready, unacked)
if ready == 0 && unacked == 0 {
break
}
time.Sleep(time.Millisecond)
}
time.Sleep(time.Millisecond)
sum := 0
for _, consumer := range consumers {
sum += len(consumer.Deliveries())
}
fmt.Printf("consumed %d\n", sum)
assert.NoError(b, connection.stopHeartbeat())
}
func TestCluster_jitteredDuration(t *testing.T) {
dur := 100 * time.Millisecond
for i := 0; i < 5000; i++ {
d := jitteredDuration(dur)
assert.LessOrEqual(t, int64(90*time.Millisecond), int64(d))
assert.GreaterOrEqual(t, int64(110*time.Millisecond), int64(d))
}
}
func TestClusterQueueDrain(t *testing.T) {
redisOptions, closer := testClusterRedis(t)
defer closer()
connection, err := OpenClusterConnection("conn1", redis.NewClusterClient(redisOptions), nil)
assert.NoError(t, err)
require.NotNil(t, connection)
queue, err := connection.OpenQueue("drain-queue")
assert.NoError(t, err)
for x := 0; x < 100; x++ {
queue.Publish(fmt.Sprintf("%d", x))
}
eventuallyReady(t, queue, 100)
for x := 1; x <= 10; x++ {
values, err := queue.Drain(10)
assert.NoError(t, err)
assert.Equal(t, 10, len(values))
eventuallyReady(t, queue, int64(100-x*10))
}
}
// TestClusterMix tests that you could use both OpenConnection() functions and
// OpenClusterConnection() functions at the same time. Note that these connections should use
// independent redis instances: A single redis and a redis cluster respectively. Don't try to use
// a normal rmq connection and an rmq cluster connection on the same Redis database. This will lead
// to a messed up rmq state.
func TestClusterMix(t *testing.T) {
// a normal rmq connection to a single redis instance
redisOptions1, closer1 := testRedis(t)
defer closer1()
connection1, err := OpenConnectionWithRedisOptions("conn1", redisOptions1, nil)
assert.NoError(t, err)
require.NotNil(t, connection1)
// an rmq cluster connection to a redis cluster
redisOptions2, closer2 := testClusterRedis(t)
defer closer2()
connection2, err := OpenClusterConnection("conn2", redis.NewClusterClient(redisOptions2), nil)
assert.NoError(t, err)
require.NotNil(t, connection2)
// publish and consume on the single instance rmq connection
queue1, err := connection1.OpenQueue("cons-q")
assert.NoError(t, err)
require.NotNil(t, queue1)
_, err = queue1.PurgeReady()
assert.NoError(t, err)
consumer1 := NewTestConsumer("cons-A")
consumer1.AutoAck = false
assert.NoError(t, queue1.StartConsuming(10, time.Millisecond))
_, err = queue1.AddConsumer("cons-cons", consumer1)
assert.NoError(t, err)
assert.Nil(t, consumer1.Last())
assert.NoError(t, queue1.Publish("cons-d1"))
eventuallyReady(t, queue1, 0)
eventuallyUnacked(t, queue1, 1)
require.NotNil(t, consumer1.Last())
assert.Regexp(t, // using [queue]
`\[cons-d1 rmq::connection::conn1-\w{6}::queue::\[cons-q\]::unacked\]`,
fmt.Sprintf("%s", consumer1.Last()),
)
// produce and consume on the rmq cluster connection
queue2, err := connection2.OpenQueue("cons-q")
assert.NoError(t, err)
require.NotNil(t, queue2)
_, err = queue2.PurgeReady()
assert.NoError(t, err)
consumer2 := NewTestConsumer("cons-A")
consumer2.AutoAck = false
assert.NoError(t, queue2.StartConsuming(20, time.Millisecond))
_, err = queue2.AddConsumer("cons-cons", consumer2)
assert.NoError(t, err)
assert.Nil(t, consumer2.Last())
assert.NoError(t, queue2.Publish("cons-d2"))
eventuallyReady(t, queue2, 0)
eventuallyUnacked(t, queue2, 1)
require.NotNil(t, consumer2.Last())
assert.Regexp(t, // using {queue}
`\[cons-d2 rmq::connection::conn2-\w{6}::queue::\{cons-q\}::unacked\]`,
fmt.Sprintf("%s", consumer2.Last()),
)
}