forked from IBM/sarama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproducer_test.go
382 lines (317 loc) · 9.14 KB
/
producer_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
package sarama
import (
"fmt"
"testing"
"time"
)
const TestMessage = "ABC THE MESSAGE"
func defaultProducerConfig() *ProducerConfig {
config := NewProducerConfig()
config.MaxBufferTime = 1000000 * time.Millisecond // don't flush based on time
config.MaxBufferedBytes = uint32((len(TestMessage) * 10) - 1) // flush after 10 messages
return config
}
func TestDefaultProducerConfigValidates(t *testing.T) {
config := NewProducerConfig()
if err := config.Validate(); err != nil {
t.Error(err)
}
}
func TestSimpleProducer(t *testing.T) {
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
defer mb1.Close()
defer mb2.Close()
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddTopicPartition("my_topic", 0, 2)
mb1.Returns(mdr)
pr := new(ProduceResponse)
pr.AddTopicPartition("my_topic", 0, NoError)
mb2.Returns(pr)
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
producer, err := NewProducer(client, defaultProducerConfig())
if err != nil {
t.Fatal(err)
}
defer producer.Close()
// flush only on 10th and final message
returns := []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
for _, f := range returns {
sendMessage(t, producer, "my_topic", TestMessage, f)
}
}
func TestSimpleSyncProducer(t *testing.T) {
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
defer mb1.Close()
defer mb2.Close()
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddTopicPartition("my_topic", 1, 2)
mb1.Returns(mdr)
pr := new(ProduceResponse)
pr.AddTopicPartition("my_topic", 1, NoError)
for i := 0; i < 10; i++ {
mb2.Returns(pr)
}
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
producer, err := NewProducer(client, defaultProducerConfig())
if err != nil {
t.Fatal(err)
}
defer producer.Close()
for i := 0; i < 10; i++ {
sendSyncMessage(t, producer, "my_topic", TestMessage)
}
}
func TestMultipleFlushes(t *testing.T) {
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
defer mb1.Close()
defer mb2.Close()
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddTopicPartition("my_topic", 0, 2)
mb1.Returns(mdr)
pr := new(ProduceResponse)
pr.AddTopicPartition("my_topic", 0, NoError)
pr.AddTopicPartition("my_topic", 0, NoError)
mb2.Returns(pr)
mb2.Returns(pr) // yes, twice.
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
config := defaultProducerConfig()
// So that we flush after the 2nd message.
config.MaxBufferedBytes = uint32((len(TestMessage) * 5) - 1)
producer, err := NewProducer(client, config)
if err != nil {
t.Fatal(err)
}
defer producer.Close()
returns := []int{0, 0, 0, 0, 1, 0, 0, 0, 0, 1}
for _, f := range returns {
sendMessage(t, producer, "my_topic", TestMessage, f)
}
}
func TestMultipleProducer(t *testing.T) {
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
mb3 := NewMockBroker(t, 3)
defer mb1.Close()
defer mb2.Close()
defer mb3.Close()
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddBroker(mb3.Addr(), mb3.BrokerID())
mdr.AddTopicPartition("topic_a", 0, 2)
mdr.AddTopicPartition("topic_b", 0, 3)
mdr.AddTopicPartition("topic_c", 0, 3)
mb1.Returns(mdr)
pr1 := new(ProduceResponse)
pr1.AddTopicPartition("topic_a", 0, NoError)
mb2.Returns(pr1)
pr2 := new(ProduceResponse)
pr2.AddTopicPartition("topic_b", 0, NoError)
pr2.AddTopicPartition("topic_c", 0, NoError)
mb3.Returns(pr2)
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
producer, err := NewProducer(client, defaultProducerConfig())
if err != nil {
t.Fatal(err)
}
defer producer.Close()
// flush only on 10th and final message
returns := []int{0, 0, 0, 0, 0, 0, 0, 0, 0, 1}
for _, f := range returns {
sendMessage(t, producer, "topic_a", TestMessage, f)
}
// no flushes
returns = []int{0, 0, 0, 0, 0}
for _, f := range returns {
sendMessage(t, producer, "topic_b", TestMessage, f)
}
// flush both topic_b and topic_c on 5th (ie. 10th for this broker)
returns = []int{0, 0, 0, 0, 2}
for _, f := range returns {
sendMessage(t, producer, "topic_c", TestMessage, f)
}
}
// Here we test that when two messages are sent in the same buffered request,
// and more messages are enqueued while the request is pending, everything
// happens correctly; that is, the first messages are retried before the next
// batch is allowed to submit.
func TestFailureRetry(t *testing.T) {
t.Skip("not yet working after mockbroker refactor")
mb1 := NewMockBroker(t, 1)
mb2 := NewMockBroker(t, 2)
mb3 := NewMockBroker(t, 3)
mdr := new(MetadataResponse)
mdr.AddBroker(mb2.Addr(), mb2.BrokerID())
mdr.AddBroker(mb3.Addr(), mb3.BrokerID())
mdr.AddTopicPartition("topic_a", 0, 2)
mdr.AddTopicPartition("topic_b", 0, 3)
mdr.AddTopicPartition("topic_c", 0, 3)
mb1.Returns(mdr)
/* mb1.ExpectMetadataRequest(). */
/* AddBroker(mb2). */
/* AddBroker(mb3). */
/* AddTopicPartition("topic_a", 0, 2). */
/* AddTopicPartition("topic_b", 0, 2). */
/* AddTopicPartition("topic_c", 0, 3) */
pr := new(ProduceResponse)
pr.AddTopicPartition("topic_a", 0, NoError)
pr.AddTopicPartition("topic_b", 0, NotLeaderForPartition)
mb2.Returns(pr)
/* mb2.ExpectProduceRequest(). */
/* AddTopicPartition("topic_a", 0, 1, NoError). */
/* AddTopicPartition("topic_b", 0, 1, NotLeaderForPartition) */
// The fact that mb2 is chosen here is not well-defined. In theory,
// it's a random choice between mb1, mb2, and mb3. Go's hash iteration
// isn't quite as random as claimed, though, it seems. Maybe because
// the same random seed is used each time?
mdr2 := new(MetadataResponse)
mdr2.AddBroker(mb3.Addr(), mb3.BrokerID())
mdr2.AddTopicPartition("topic_b", 0, 3)
mb2.Returns(mdr2)
/* mb2.ExpectMetadataRequest(). */
/* AddBroker(mb3). */
/* AddTopicPartition("topic_b", 0, 3) */
pr2 := new(ProduceResponse)
pr2.AddTopicPartition("topic_c", 0, NoError)
pr2.AddTopicPartition("topic_b", 0, NoError)
mb3.Returns(pr2)
/* mb3.ExpectProduceRequest(). */
/* AddTopicPartition("topic_c", 0, 1, NoError). */
/* AddTopicPartition("topic_b", 0, 1, NoError) */
client, err := NewClient("client_id", []string{mb1.Addr()}, nil)
if err != nil {
t.Fatal(err)
}
defer client.Close()
producer, err := NewProducer(client, defaultProducerConfig())
if err != nil {
t.Fatal(err)
}
defer producer.Close()
// Sent to mb3; does not flush because it's only half the cap.
// mb1: [__]
// mb2: [__]
// mb3: [__]
sendMessage(t, producer, "topic_c", TestMessage, 0)
// mb1: [__]
// mb2: [__]
// mb3: [X_]
// Sent to mb2; does not flush because it's only half the cap.
sendMessage(t, producer, "topic_a", TestMessage, 0)
// mb1: [__]
// mb2: [X_]
// mb3: [X_]
// Sent to mb2; flushes, errors (retriable).
// Three messages will be received:
// * NoError for topic_a;
// * NoError for topic_b;
// * NoError for topic_c.
sendMessage(t, producer, "topic_b", TestMessage, 2)
// mb1: [__]
// mb2: [XX] <- flush!
// mb3: [X_]
// The topic_b message errors, and we should wind up here:
// mb1: [__]
// mb2: [__]
// mb3: [XX] <- topic_b reassigned to mb3 by metadata refresh, flushes.
defer mb1.Close()
defer mb2.Close()
}
func readMessage(t *testing.T, ch chan error) {
select {
case err := <-ch:
if err != nil {
t.Error(err)
}
case <-time.After(1 * time.Second):
t.Error(fmt.Errorf("Message was never received"))
}
}
func assertNoMessages(t *testing.T, ch chan error) {
select {
case x := <-ch:
t.Fatal(fmt.Errorf("unexpected value received: %#v", x))
case <-time.After(1 * time.Millisecond):
}
}
func ExampleProducer() {
client, err := NewClient("client_id", []string{"localhost:9092"}, NewClientConfig())
if err != nil {
panic(err)
} else {
fmt.Println("> connected")
}
defer client.Close()
producer, err := NewProducer(client, nil)
if err != nil {
panic(err)
}
defer producer.Close()
err = producer.SendMessage("my_topic", nil, StringEncoder("testing 123"))
if err != nil {
panic(err)
} else {
fmt.Println("> message sent")
}
}
func ExampleAsyncProducer() {
client, err := NewClient("client_id", []string{"localhost:9092"}, NewClientConfig())
if err != nil {
panic(err)
} else {
fmt.Println("> connected")
}
defer client.Close()
producer, err := NewProducer(client, nil)
if err != nil {
panic(err)
}
defer producer.Close()
go func() {
for err := range producer.Errors() {
panic(err)
}
}()
for {
err = producer.QueueMessage("my_topic", nil, StringEncoder("testing 123"))
if err != nil {
panic(err)
} else {
fmt.Println("> message sent")
}
}
}
func sendMessage(t *testing.T, producer *Producer, topic string, key string, expectedResponses int) {
err := producer.QueueMessage(topic, nil, StringEncoder(key))
if err != nil {
t.Error(err)
}
for i := 0; i < expectedResponses; i++ {
readMessage(t, producer.Errors())
}
assertNoMessages(t, producer.Errors())
}
func sendSyncMessage(t *testing.T, producer *Producer, topic string, key string) {
err := producer.SendMessage(topic, nil, StringEncoder(key))
if err != nil {
t.Error(err)
}
assertNoMessages(t, producer.Errors())
}