forked from segmentio/kafka-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_test.go
91 lines (79 loc) · 2.08 KB
/
client_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
package kafka
import (
"context"
"testing"
"time"
)
func TestClient(t *testing.T) {
t.Parallel()
tests := []struct {
scenario string
function func(*testing.T, context.Context, *Client)
}{
{
scenario: "retrieve committed offsets for a consumer group and topic",
function: testConsumerGroupFetchOffsets,
},
}
for _, test := range tests {
testFunc := test.function
t.Run(test.scenario, func(t *testing.T) {
t.Parallel()
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()
c := NewClient("localhost:9092")
testFunc(t, ctx, c)
})
}
}
func testConsumerGroupFetchOffsets(t *testing.T, ctx context.Context, c *Client) {
const totalMessages = 144
const partitions = 12
const msgPerPartition = totalMessages / partitions
topic := makeTopic()
groupId := makeGroupID()
createTopic(t, topic, partitions)
brokers := []string{"localhost:9092"}
writer := NewWriter(WriterConfig{
Brokers: brokers,
Topic: topic,
Dialer: DefaultDialer,
Balancer: &RoundRobin{},
BatchSize: 1,
})
if err := writer.WriteMessages(ctx, makeTestSequence(totalMessages)...); err != nil {
t.Fatalf("bad write messages: %v", err)
}
if err := writer.Close(); err != nil {
t.Fatalf("bad write err: %v", err)
}
r := NewReader(ReaderConfig{
Brokers: brokers,
Topic: topic,
GroupID: groupId,
MinBytes: 1,
MaxBytes: 10e6,
MaxWait: 100 * time.Millisecond,
})
defer r.Close()
for i := 0; i < totalMessages; i++ {
m, err := r.FetchMessage(ctx)
if err != nil {
t.Errorf("error fetching message: %s", err)
}
r.CommitMessages(context.Background(), m)
}
offsets, err := c.ConsumerOffsets(ctx, TopicAndGroup{GroupId: groupId, Topic: topic})
if err != nil {
t.Fatal(err)
}
if len(offsets) != partitions {
t.Fatalf("expected %d partitions but only received offsets for %d", partitions, len(offsets))
}
for i := 0; i < partitions; i++ {
committedOffset := offsets[i]
if committedOffset != msgPerPartition {
t.Fatalf("expected committed offset of %d but received %d", msgPerPartition, committedOffset)
}
}
}