-
Notifications
You must be signed in to change notification settings - Fork 107
/
groups_test.go
801 lines (689 loc) · 21.4 KB
/
groups_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
package server
import (
"container/heap"
"context"
"strconv"
"testing"
"time"
lift "github.com/liftbridge-io/go-liftbridge/v2"
proto "github.com/liftbridge-io/liftbridge/server/protocol"
natsdTest "github.com/nats-io/nats-server/v2/test"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func waitForGroup(t *testing.T, timeout time.Duration, id string, servers ...*Server) {
deadline := time.Now().Add(timeout)
LOOP:
for time.Now().Before(deadline) {
for _, s := range servers {
group := s.metadata.GetConsumerGroup(id)
if group == nil {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
}
return
}
stackFatalf(t, "Cluster did not create group [id=%s]", id)
}
func waitForConsumer(t *testing.T, timeout time.Duration, group, cons string, servers ...*Server) {
deadline := time.Now().Add(timeout)
LOOP:
for time.Now().Before(deadline) {
for _, s := range servers {
group := s.metadata.GetConsumerGroup(group)
if group == nil {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
if !group.IsMember(cons) {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
}
return
}
stackFatalf(t, "Group [id=%s] does not have consumer [id=%s]", group, cons)
}
func waitForGroupMembers(t *testing.T, timeout time.Duration, group string, members int, servers ...*Server) {
deadline := time.Now().Add(timeout)
LOOP:
for time.Now().Before(deadline) {
for _, s := range servers {
group := s.metadata.GetConsumerGroup(group)
if group == nil {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
if len(group.GetMembers()) != members {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
}
return
}
stackFatalf(t, "Group [id=%s] does not have %d members", group, members)
}
func waitForConsumerSubscribed(t *testing.T, timeout time.Duration, group, cons, stream string,
partition int32, server *Server) {
deadline := time.Now().Add(timeout)
LOOP:
for time.Now().Before(deadline) {
partition := server.metadata.GetPartition(stream, partition)
if partition == nil {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
member := partition.GetGroupConsumer(group)
if member == nil {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
if member.consumerID != cons {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
return
}
stackFatalf(t,
"Group [id=%s] does not have consumer [id=%s] subscribed to [stream=%s, partition=%d]",
group, cons, stream, partition)
}
func getGroupCoordinator(t *testing.T, timeout time.Duration, groupName string, servers ...*Server) (
*Server, uint64) {
var (
epoch uint64
coordinator string
deadline = time.Now().Add(timeout)
)
LOOP:
for time.Now().Before(deadline) {
for _, s := range servers {
group := s.metadata.GetConsumerGroup(groupName)
if group == nil {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
coor, ep := group.GetCoordinator()
if ep >= epoch {
coordinator = coor
epoch = ep
}
}
if coordinator == "" {
time.Sleep(15 * time.Millisecond)
continue LOOP
}
for _, s := range servers {
if s.config.Clustering.ServerID == coordinator {
return s, epoch
}
}
}
stackFatalf(t, "No coordinator found for group [id=%s]", groupName)
return nil, 0
}
// Ensure assignPartition and removeStreamAssignments update the consumer's
// assignments and assignedCount appropriately.
func TestConsumerGroupAssignments(t *testing.T) {
c := &consumer{assignments: make(partitionAssignments)}
assert.Equal(t, 0, c.assignedCount)
assert.Len(t, c.assignments, 0)
c.assignPartition("foo", 0)
c.assignPartition("foo", 1)
c.assignPartition("foo", 2)
c.assignPartition("bar", 0)
assert.Equal(t, 4, c.assignedCount)
assert.Len(t, c.assignments, 2)
assert.Len(t, c.assignments["foo"], 3)
assert.Len(t, c.assignments["bar"], 1)
c.removeStreamAssignments("foo")
assert.Len(t, c.assignments, 1)
assert.Len(t, c.assignments["bar"], 1)
assert.Equal(t, 1, c.assignedCount)
c.removeStreamAssignments("bar")
assert.Equal(t, 0, c.assignedCount)
assert.Len(t, c.assignments, 0)
}
// Ensure consumerHeap implements the heap interface correctly ordering by
// assignment count.
func TestConsumerGroupHeap(t *testing.T) {
h := new(consumerHeap)
heap.Push(h, &consumer{id: "a", assignedCount: 5})
heap.Push(h, &consumer{id: "b", assignedCount: 2})
heap.Push(h, &consumer{id: "c", assignedCount: 10})
heap.Push(h, &consumer{id: "d", assignedCount: 3})
heap.Push(h, &consumer{id: "e", assignedCount: 0})
heap.Push(h, &consumer{id: "f", assignedCount: 3})
assert.Equal(t, 6, h.Len())
p := h.Peek()
assert.Equal(t, "e", p.id)
c := heap.Pop(h).(*consumer)
assert.Equal(t, p, c)
assert.Equal(t, 5, h.Len())
heap.Push(h, &consumer{id: "g", assignedCount: 1})
assert.Equal(t, 6, h.Len())
c = heap.Pop(h).(*consumer)
assert.Equal(t, "g", c.id)
assert.Equal(t, 5, h.Len())
c = heap.Pop(h).(*consumer)
assert.Equal(t, "b", c.id)
assert.Equal(t, 4, h.Len())
c = heap.Pop(h).(*consumer)
assert.Equal(t, "d", c.id)
assert.Equal(t, 3, h.Len())
c = heap.Pop(h).(*consumer)
assert.Equal(t, "f", c.id)
assert.Equal(t, 2, h.Len())
c = heap.Pop(h).(*consumer)
assert.Equal(t, "a", c.id)
assert.Equal(t, 1, h.Len())
c = heap.Pop(h).(*consumer)
assert.Equal(t, "c", c.id)
assert.Equal(t, 0, h.Len())
}
// Ensure rangeStreamsOrdered applies the supplied function to the map in
// order.
func TestConsumerGroupRangeStreamsOrdered(t *testing.T) {
streams := map[string]struct{}{
"b": {},
"d": {},
"a": {},
"c": {},
"e": {},
}
applied := []string{}
rangeStreamsOrdered(streams, func(stream string) {
applied = append(applied, stream)
})
assert.Len(t, applied, 5)
assert.Equal(t, "a", applied[0])
assert.Equal(t, "b", applied[1])
assert.Equal(t, "c", applied[2])
assert.Equal(t, "d", applied[3])
assert.Equal(t, "e", applied[4])
}
// Ensure consumer groups are created, assign partitions, and are deleted
// correctly.
func TestConsumerGroupLifecycle(t *testing.T) {
defer cleanupStorage(t)
// Configure server.
s1Config := getTestConfig("a", true, 5050)
s1Config.CursorsStream.Partitions = 1
s1 := runServerWithConfig(t, s1Config)
defer s1.Stop()
getMetadataLeader(t, 10*time.Second, s1)
client, err := lift.Connect([]string{"localhost:5050"})
require.NoError(t, err)
defer client.Close()
// Create streams.
err = client.CreateStream(context.Background(), "foo", "foo", lift.Partitions(2))
require.NoError(t, err)
err = client.CreateStream(context.Background(), "bar", "bar", lift.Partitions(2))
require.NoError(t, err)
var (
group = "my-group"
cons1ID = "consumer-1"
cons2ID = "consumer-2"
)
// Create consumer.
cons1, err := client.CreateConsumer(group, lift.ConsumerID(cons1ID),
lift.FetchAssignmentsInterval(func(timeout time.Duration) time.Duration {
return 50 * time.Millisecond
}))
require.NoError(t, err)
var (
cons1FooPartition0 = make(chan *lift.Message)
cons1FooPartition1 = make(chan *lift.Message)
cons1BarPartition0 = make(chan *lift.Message)
cons1BarPartition1 = make(chan *lift.Message)
)
err = cons1.Subscribe(context.Background(), []string{"foo", "bar"},
func(msg *lift.Message, err error) {
if err != nil {
return
}
if msg.Stream() == "foo" {
if msg.Partition() == 0 {
cons1FooPartition0 <- msg
} else {
cons1FooPartition1 <- msg
}
} else {
if msg.Partition() == 0 {
cons1BarPartition0 <- msg
} else {
cons1BarPartition1 <- msg
}
}
})
require.NoError(t, err)
waitForGroup(t, 5*time.Second, group, s1)
waitForConsumer(t, 5*time.Second, group, cons1ID, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons1ID, "foo", 0, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons1ID, "foo", 1, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons1ID, "bar", 0, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons1ID, "bar", 1, s1)
// Publish some messages and validate we receive them.
for _, stream := range []string{"foo", "bar"} {
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
_, err = client.Publish(context.Background(), stream, []byte(strconv.Itoa(j)),
lift.ToPartition(int32(i)))
require.NoError(t, err)
}
}
}
for partition, ch := range []chan *lift.Message{cons1FooPartition0, cons1FooPartition1} {
for i := 0; i < 3; i++ {
select {
case msg := <-ch:
require.Equal(t, "foo", msg.Stream())
require.Equal(t, int32(partition), msg.Partition())
require.Equal(t, int64(i), msg.Offset())
require.Equal(t, []byte(strconv.Itoa(i)), msg.Value())
case <-time.After(5 * time.Second):
t.Fatal("Did not receive expected message")
}
}
}
for partition, ch := range []chan *lift.Message{cons1BarPartition0, cons1BarPartition1} {
for i := 0; i < 3; i++ {
select {
case msg := <-ch:
require.Equal(t, "bar", msg.Stream())
require.Equal(t, int32(partition), msg.Partition())
require.Equal(t, int64(i), msg.Offset())
require.Equal(t, []byte(strconv.Itoa(i)), msg.Value())
case <-time.After(5 * time.Second):
t.Fatal("Did not receive expected message")
}
}
}
// Create another consumer.
cons2, err := client.CreateConsumer(group, lift.ConsumerID(cons2ID))
require.NoError(t, err)
var (
cons2FooPartition0 = make(chan *lift.Message)
cons2FooPartition1 = make(chan *lift.Message)
)
// Consumer 2 should take over subscriptions for stream foo.
err = cons2.Subscribe(context.Background(), []string{"foo"},
func(msg *lift.Message, err error) {
if err != nil {
return
}
require.Equal(t, "foo", msg.Stream())
if msg.Partition() == 0 {
cons2FooPartition0 <- msg
} else {
cons2FooPartition1 <- msg
}
})
require.NoError(t, err)
waitForConsumer(t, 5*time.Second, group, cons2ID, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons2ID, "foo", 0, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons2ID, "foo", 1, s1)
// Publish some more messages.
for _, stream := range []string{"foo", "bar"} {
for i := 0; i < 2; i++ {
for j := 0; j < 3; j++ {
_, err = client.Publish(context.Background(), stream, []byte(strconv.Itoa(j)),
lift.ToPartition(int32(i)))
require.NoError(t, err)
}
}
}
// Ensure consumer 2 received messages on foo.
for partition, ch := range []chan *lift.Message{cons2FooPartition0, cons2FooPartition1} {
for i := 0; i < 3; i++ {
select {
case msg := <-ch:
require.Equal(t, "foo", msg.Stream())
require.Equal(t, int32(partition), msg.Partition())
require.Equal(t, int64(i+3), msg.Offset())
require.Equal(t, []byte(strconv.Itoa(i)), msg.Value())
case <-time.After(5 * time.Second):
t.Fatal("Did not receive expected message")
}
}
}
// Ensure consumer 1 did not receive messages on foo.
for _, ch := range []chan *lift.Message{cons1FooPartition0, cons1FooPartition1} {
select {
case <-ch:
t.Fatal("Received unexpected message")
default:
}
}
// Ensure consumer 1 received messages on bar.
for partition, ch := range []chan *lift.Message{cons1BarPartition0, cons1BarPartition1} {
for i := 0; i < 3; i++ {
select {
case msg := <-ch:
require.Equal(t, "bar", msg.Stream())
require.Equal(t, int32(partition), msg.Partition())
require.Equal(t, int64(i+3), msg.Offset())
require.Equal(t, []byte(strconv.Itoa(i)), msg.Value())
case <-time.After(5 * time.Second):
t.Fatal("Did not receive expected message")
}
}
}
// Remove consumer 2 from group and validate consumer 1's assignments are
// updated.
require.NoError(t, cons2.Close())
waitForConsumerSubscribed(t, 5*time.Second, group, cons1ID, "foo", 0, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons1ID, "foo", 1, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons1ID, "bar", 0, s1)
waitForConsumerSubscribed(t, 5*time.Second, group, cons1ID, "bar", 1, s1)
// Remove consumer 1 from group. This should remove the group since it's
// the last member.
require.NoError(t, cons1.Close())
require.Nil(t, s1.metadata.GetConsumerGroup(group))
}
// Ensure joining a consumer group fails if the consumer subscribes to a stream
// that doesn't exist or if a consumer with the same ID is already a member.
func TestConsumerGroupAddMember(t *testing.T) {
defer cleanupStorage(t)
// Configure server.
s1Config := getTestConfig("a", true, 5050)
s1Config.CursorsStream.Partitions = 1
s1 := runServerWithConfig(t, s1Config)
defer s1.Stop()
getMetadataLeader(t, 10*time.Second, s1)
client, err := lift.Connect([]string{"localhost:5050"})
require.NoError(t, err)
defer client.Close()
var (
group = "my-group"
consumerID = "cons"
)
// Create consumer.
cons, err := client.CreateConsumer(group, lift.ConsumerID(consumerID))
require.NoError(t, err)
// This should error because stream foo does not exist.
err = cons.Subscribe(context.Background(), []string{"foo"}, func(msg *lift.Message, err error) {
})
require.Error(t, err)
// Create stream.
err = client.CreateStream(context.Background(), "foo", "foo")
require.NoError(t, err)
// This should now succeed.
err = cons.Subscribe(context.Background(), []string{"foo"}, func(msg *lift.Message, err error) {
})
require.NoError(t, err)
// Create consumer with same ID.
cons2, err := client.CreateConsumer(group, lift.ConsumerID(consumerID))
require.NoError(t, err)
// This should error because a consumer with the same ID is already a
// member.
err = cons2.Subscribe(context.Background(), []string{"foo"}, func(msg *lift.Message, err error) {
})
require.Error(t, err)
}
// Ensure when a stream that a consumer is subscribed to is deleted, the stream
// is removed from the consumer.
func TestConsumerGroupStreamDeleted(t *testing.T) {
defer cleanupStorage(t)
// Configure server.
s1Config := getTestConfig("a", true, 5050)
s1Config.CursorsStream.Partitions = 1
s1 := runServerWithConfig(t, s1Config)
defer s1.Stop()
getMetadataLeader(t, 10*time.Second, s1)
client, err := lift.Connect([]string{"localhost:5050"})
require.NoError(t, err)
defer client.Close()
var (
group = "my-group"
consumerID1 = "cons1"
)
// Create streams.
err = client.CreateStream(context.Background(), "foo", "foo")
require.NoError(t, err)
err = client.CreateStream(context.Background(), "bar", "bar")
require.NoError(t, err)
// Create consumer.
cons1, err := client.CreateConsumer(group, lift.ConsumerID(consumerID1))
require.NoError(t, err)
err = cons1.Subscribe(context.Background(), []string{"foo", "bar"},
func(msg *lift.Message, err error) {
})
require.NoError(t, err)
g := s1.metadata.GetConsumerGroup(group)
require.NotNil(t, g)
members := g.GetMembers()
require.Len(t, members, 1)
streams := members[consumerID1]
require.Len(t, streams, 2)
require.Contains(t, streams, "foo")
require.Contains(t, streams, "bar")
client.DeleteStream(context.Background(), "foo")
members = g.GetMembers()
require.Len(t, members, 1)
streams = members[consumerID1]
require.Len(t, streams, 1)
require.Equal(t, "bar", streams[0])
}
// Ensure when a group coordinator becomes unavailable, the consumer reports it
// and it fails over to another server.
func TestConsumerGroupCoordinatorFailover(t *testing.T) {
defer cleanupStorage(t)
// Use an external NATS server.
ns := natsdTest.RunDefaultServer()
defer ns.Shutdown()
// Configure server.
s1Config := getTestConfig("a", true, 5050)
s1Config.EmbeddedNATS = false
s1Config.CursorsStream.Partitions = 1
s1Config.Groups.CoordinatorTimeout = 50 * time.Millisecond
s1 := runServerWithConfig(t, s1Config)
defer s1.Stop()
// Configure second server.
s2Config := getTestConfig("b", false, 5051)
s2Config.CursorsStream.Partitions = 1
s2Config.Groups.CoordinatorTimeout = 50 * time.Millisecond
s2 := runServerWithConfig(t, s2Config)
defer s2.Stop()
// Configure third server.
s3Config := getTestConfig("c", false, 5052)
s3Config.CursorsStream.Partitions = 1
s3Config.Groups.CoordinatorTimeout = 50 * time.Millisecond
s3 := runServerWithConfig(t, s3Config)
defer s3.Stop()
getMetadataLeader(t, 10*time.Second, s1, s2, s3)
client, err := lift.Connect([]string{"localhost:5050", "localhost:5051", "localhost:5052"})
require.NoError(t, err)
defer client.Close()
// Create stream.
err = client.CreateStream(context.Background(), "foo", "foo")
require.NoError(t, err)
var (
group = "my-group"
consID = "cons"
)
// Create consumer.
cons, err := client.CreateConsumer(group, lift.ConsumerID(consID),
lift.FetchAssignmentsInterval(func(timeout time.Duration) time.Duration {
return 10 * time.Millisecond
}))
require.NoError(t, err)
err = cons.Subscribe(context.Background(), []string{"foo"},
func(msg *lift.Message, err error) {
})
require.NoError(t, err)
waitForConsumer(t, 5*time.Second, group, consID, s1, s2, s3)
coordinator, epoch := getGroupCoordinator(t, 5*time.Second, group, s1, s2, s3)
// Stop coordinator to force a failover.
require.NoError(t, coordinator.Stop())
var followers []*Server
if coordinator == s1 {
followers = []*Server{s2, s3}
} else if coordinator == s2 {
followers = []*Server{s1, s3}
} else {
followers = []*Server{s1, s2}
}
// Wait for new coordinator.
var (
deadline = time.Now().Add(5 * time.Second)
newCoordinator *Server
newEpoch uint64
)
for time.Now().Before(deadline) {
newCoordinator, newEpoch = getGroupCoordinator(t, 10*time.Second, group, followers...)
if newCoordinator == coordinator {
time.Sleep(15 * time.Millisecond)
continue
}
break
}
require.NotEqual(t, newCoordinator, coordinator)
require.Greater(t, newEpoch, epoch)
}
// Ensure when a consumer's timeout is reached the group invokes the expired
// handler.
func TestConsumerGroupConsumerTimeout(t *testing.T) {
protoGroup := &proto.ConsumerGroup{
Id: "my-group",
Coordinator: "a",
}
called := 0
ch := make(chan struct{}, 1)
handler := func(groupID, consumerID string) error {
require.Equal(t, "my-group", groupID)
require.Equal(t, "cons1", consumerID)
if called == 0 {
called++
return errors.New("error")
}
called++
select {
case ch <- struct{}{}:
default:
}
return nil
}
getPartitions := func(stream string) int32 {
return 1
}
group := newConsumerGroup("a", time.Millisecond, protoGroup, false,
noopLogger(), handler, getPartitions)
defer group.Close()
require.NoError(t, group.AddMember("cons1", []string{"foo"}, 1))
select {
case <-ch:
require.Equal(t, 2, called)
case <-time.After(5 * time.Second):
t.Fatal("handler not called")
}
}
// Ensure when StartRecovered is called on a group that is not being recovered
// it returns false. When the group is being recovered and it's the
// coordinator, member timers are started and true is returned.
func TestConsumerGroupStartRecovered(t *testing.T) {
protoGroup := &proto.ConsumerGroup{
Id: "my-group",
Coordinator: "a",
Members: []*proto.Consumer{
&proto.Consumer{
Id: "cons1",
Streams: []string{"foo"},
},
},
}
called := 0
ch := make(chan struct{}, 1)
handler := func(groupID, consumerID string) error {
require.Equal(t, "my-group", groupID)
require.Equal(t, "cons1", consumerID)
called++
select {
case ch <- struct{}{}:
default:
}
return nil
}
getPartitions := func(stream string) int32 {
return 1
}
group := newConsumerGroup("a", time.Minute, protoGroup, false,
noopLogger(), handler, getPartitions)
defer group.Close()
// Group not in recovery should return false.
require.False(t, group.StartRecovered())
group = newConsumerGroup("a", time.Millisecond, protoGroup, true,
noopLogger(), handler, getPartitions)
defer group.Close()
// Group in recovery should return true.
require.True(t, group.StartRecovered())
select {
case <-ch:
require.Equal(t, 1, called)
case <-time.After(5 * time.Second):
t.Fatal("handler not called")
}
require.False(t, group.recovered)
}
// Ensure SetCoordinator returns an error if the provided epoch is less than
// the current epoch. Otherwise it updates the coordinator and epoch and, if
// the new coordinator is the current server, starts member timers.
func TestConsumerGroupSetCoordinator(t *testing.T) {
protoGroup := &proto.ConsumerGroup{
Id: "my-group",
Coordinator: "a",
Epoch: 1,
Members: []*proto.Consumer{
&proto.Consumer{
Id: "cons1",
Streams: []string{"foo"},
},
},
}
called := 0
ch := make(chan struct{}, 1)
handler := func(groupID, consumerID string) error {
require.Equal(t, "my-group", groupID)
require.Equal(t, "cons1", consumerID)
called++
select {
case ch <- struct{}{}:
default:
}
return nil
}
getPartitions := func(stream string) int32 {
return 1
}
group := newConsumerGroup("a", time.Millisecond, protoGroup, false,
noopLogger(), handler, getPartitions)
defer group.Close()
// SetCoordinator with an epoch less than current epoch should return
// an error.
require.Error(t, group.SetCoordinator("b", 0))
require.NoError(t, group.SetCoordinator("b", 2))
coordinator, epoch := group.GetCoordinator()
require.Equal(t, "b", coordinator)
require.Equal(t, uint64(2), epoch)
// Changing to this server should start timers.
require.NoError(t, group.SetCoordinator("a", 3))
select {
case <-ch:
require.Equal(t, 1, called)
case <-time.After(5 * time.Second):
t.Fatal("handler not called")
}
}
// Ensure GetID returns the group ID.
func TestConsumerGroupGetID(t *testing.T) {
protoGroup := &proto.ConsumerGroup{
Id: "my-group",
Coordinator: "a",
}
group := newConsumerGroup("a", time.Millisecond, protoGroup, false,
noopLogger(), nil, nil)
defer group.Close()
require.Equal(t, "my-group", group.GetID())
}