forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
200 lines (165 loc) · 4.26 KB
/
consumer_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
package sarama
import (
"fmt"
"testing"
"time"
)
func TestDefaultConsumerConfigValidates(t *testing.T) {
config := NewConsumerConfig()
if err := config.Validate(); err != nil {
t.Error(err)
}
}
func TestSimpleConsumer(t *testing.T) {
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddTopicPartition("my_topic", 0, 2, nil, nil, NoError)
mb1.Returns(mdr)
for i := 0; i < 10; i++ {
fr := new(FetchResponse)
fr.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(i))
mb2.Returns(fr)
}
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
defer safeClose(t, client)
consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", nil)
if err != nil {
t.Fatal(err)
}
defer safeClose(t, consumer)
defer mb1.Close()
defer mb2.Close()
for i := 0; i < 10; i++ {
event := <-consumer.Events()
if event.Err != nil {
t.Error(event.Err)
}
if event.Offset != int64(i) {
t.Error("Incorrect message offset!")
}
}
}
func TestConsumerRawOffset(t *testing.T) {
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddTopicPartition("my_topic", 0, 2, nil, nil, NoError)
mb1.Returns(mdr)
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
defer safeClose(t, client)
config := NewConsumerConfig()
config.OffsetMethod = OffsetMethodManual
config.OffsetValue = 1234
consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", config)
if err != nil {
t.Fatal(err)
}
defer safeClose(t, consumer)
defer mb1.Close()
defer mb2.Close()
if consumer.offset != 1234 {
t.Error("Raw offset not set correctly")
}
}
func TestConsumerLatestOffset(t *testing.T) {
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddTopicPartition("my_topic", 0, 2, nil, nil, NoError)
mb1.Returns(mdr)
or := new(OffsetResponse)
or.AddTopicPartition("my_topic", 0, 0x010101)
mb2.Returns(or)
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
defer safeClose(t, client)
config := NewConsumerConfig()
config.OffsetMethod = OffsetMethodNewest
consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", config)
if err != nil {
t.Fatal(err)
}
defer safeClose(t, consumer)
defer mb2.Close()
defer mb1.Close()
if consumer.offset != 0x010101 {
t.Error("Latest offset not fetched correctly")
}
}
func TestConsumerPrelude(t *testing.T) {
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddTopicPartition("my_topic", 0, 2, nil, nil, NoError)
mb1.Returns(mdr)
fr := new(FetchResponse)
fr.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(0))
fr.AddMessage("my_topic", 0, nil, ByteEncoder([]byte{0x00, 0x0E}), int64(1))
mb2.Returns(fr)
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
defer client.Close()
config := NewConsumerConfig()
config.OffsetMethod = OffsetMethodManual
config.OffsetValue = 1
consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", config)
if err != nil {
t.Fatal(err)
}
defer consumer.Close()
defer mb1.Close()
defer mb2.Close()
event := <-consumer.Events()
if event.Err != nil {
t.Error(event.Err)
}
if event.Offset != 1 {
t.Error("Incorrect message offset!")
}
}
func ExampleConsumer() {
client, err := NewClient("my_client", []string{"localhost:9092"}, nil)
if err != nil {
panic(err)
} else {
fmt.Println("> connected")
}
defer client.Close()
consumer, err := NewConsumer(client, "my_topic", 0, "my_consumer_group", NewConsumerConfig())
if err != nil {
panic(err)
} else {
fmt.Println("> consumer ready")
}
defer consumer.Close()
msgCount := 0
consumerLoop:
for {
select {
case event := <-consumer.Events():
if event.Err != nil {
panic(event.Err)
}
msgCount++
case <-time.After(5 * time.Second):
fmt.Println("> timed out")
break consumerLoop
}
}
fmt.Println("Got", msgCount, "messages.")
}