-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathconsumer_test.go
348 lines (294 loc) · 7.63 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
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
package nsq
import (
"fmt"
"strconv"
"testing"
"time"
)
func TestConsumer(t *testing.T) {
for _, n := range []int{1, 10, 100, 1000} {
count := n
topic := fmt.Sprintf("test-consumer-%d", n)
t.Run(topic, func(t *testing.T) {
t.Parallel()
nodes := make([]*Client, len(nsqd))
// Create the clients used for publishing messages.
for i, addr := range nsqd {
nodes[i] = &Client{Address: addr}
// stack cleanup callbacks
defer nodes[i].DeleteTopic(topic)
}
// Publish messages to the NSQ nodes in a round robin fashion.
for i := 0; i != count; i++ {
node := nodes[i%len(nodes)]
if err := node.Publish(topic, []byte(strconv.Itoa(i))); err != nil {
t.Error(err)
return
}
}
// Allow some time for the nsqd nodes to inform nsqlookupd that they host
// a specific topic.
time.Sleep(100 * time.Millisecond)
// Each bucket should have a value of 1 after the test.
buckets := make([]int, count)
// Start the consumer which looks for nsq nodes from the given nsqlookup
// addresses.
consumer, _ := StartConsumer(ConsumerConfig{
Topic: topic,
Channel: "buckets",
Lookup: nsqlookup,
MaxInFlight: count / 10,
})
defer consumer.Stop()
deadline := time.NewTimer(10 * time.Second)
defer deadline.Stop()
// Consume messages until we've (hopefully) got them all.
for i := 0; i != count; i++ {
select {
case msg := <-consumer.Messages():
if b, err := strconv.Atoi(string(msg.Body)); err != nil {
t.Error("invalid message:", msg)
} else {
buckets[b]++
}
msg.Finish()
case <-deadline.C:
t.Error("timeout")
return
}
}
// Check that we've got the expected results.
for i, b := range buckets {
if b != 1 {
t.Errorf("bucket at index %d has value %d", i, b)
}
}
})
}
}
func TestRequeue(t *testing.T) {
c := &Client{Address: "localhost:4151"}
if err := c.Publish("test-requeue", []byte("Hello World!")); err != nil {
t.Error(err)
return
}
defer c.DeleteTopic("test-requeue")
consumer, _ := StartConsumer(ConsumerConfig{
Topic: "test-requeue",
Channel: "channel",
Lookup: nsqlookup,
})
defer consumer.Stop()
deadline := time.NewTimer(10 * time.Second)
defer deadline.Stop()
for i := 1; i <= 10; i++ {
select {
case msg := <-consumer.Messages():
if msg.Attempts != uint16(i) {
t.Error("invalid attempt count:", msg.Attempts, "!=", i)
}
if s := string(msg.Body); s != "Hello World!" {
t.Error("invalid message body:", s)
}
msg.Requeue(NoTimeout)
case <-deadline.C:
t.Error("timeout")
return
}
}
select {
case msg := <-consumer.Messages():
msg.Finish()
case <-deadline.C:
t.Error("timeout")
return
}
consumer.Stop()
//Make sure the channel gets closed at some point.
for msg := range consumer.Messages() {
t.Error("unexpected message:", msg)
msg.Finish()
}
}
func TestDrainAndRequeueOnStop(t *testing.T) {
p, _ := NewProducer(ProducerConfig{
Topic: "test-stop-requeue",
Address: "localhost:4150",
DialTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
WriteTimeout: time.Second * 60,
})
p.Start()
for i := 0; i < 10; i++ {
if err := p.Publish([]byte(strconv.Itoa(i))); err != nil {
t.Error(err)
return
}
}
consumer, err := NewConsumer(ConsumerConfig{
Topic: "test-stop-requeue",
Channel: "foo",
Address: "localhost:4150",
DialTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
WriteTimeout: time.Second * 60,
MaxInFlight: 10,
})
if err != nil {
t.Fatal(err)
}
consumer.Start()
deadline := time.NewTimer(10 * time.Second)
defer deadline.Stop()
// Consume 5 messages and then stop the client
// to incur a requeue on remaining in flight
msgNum := 0
for msgNum < 5 {
select {
case msg := <-consumer.Messages():
msg.Finish()
fmt.Printf("handling message %s\n", string(msg.Body))
msgNum++
case <-deadline.C:
t.Error("timeout")
return
}
}
consumer.Stop()
//Make sure the channel gets closed at some point.
for msg := range consumer.Messages() {
t.Error("unexpected message:", msg)
msg.Finish()
}
consumer2, _ := NewConsumer(ConsumerConfig{
Topic: "test-stop-requeue",
Channel: "foo",
Address: "localhost:4150",
DialTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
WriteTimeout: time.Second * 60,
MaxInFlight: 100,
})
deadline = time.NewTimer(10 * time.Second)
defer deadline.Stop()
consumer2.Start()
msgNum = 5
for msgNum < 10 {
select {
case msg := <-consumer2.Messages():
fmt.Printf("handling message %s\n", string(msg.Body))
if s := string(msg.Body); s != strconv.Itoa(msgNum) {
t.Error("invalid message body:", s)
}
msg.Finish()
msgNum++
case <-deadline.C:
t.Error("timeout")
return
}
}
consumer2.Stop()
//Make sure the channel gets closed at some point.
for msg := range consumer2.Messages() {
t.Error("unexpected message:", msg)
msg.Finish()
}
}
func TestDrainAndRequeueOnStopWithMessageTimeout(t *testing.T) {
TopicName := "test-stop-requeue-message-timeout"
numberMessages := 25
p, _ := NewProducer(ProducerConfig{
Topic: TopicName,
Address: "localhost:4150",
DialTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
WriteTimeout: time.Second * 60,
})
sentMessages := make(map[string]bool)
p.Start()
for i := 0; i < numberMessages; i++ {
msg := strconv.Itoa(i)
if err := p.Publish([]byte(msg)); err != nil {
t.Error(err)
return
}
sentMessages[msg] = true
}
consumer, err := NewConsumer(ConsumerConfig{
Topic: TopicName,
Channel: "foo",
Address: "localhost:4150",
DialTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
WriteTimeout: time.Second * 60,
MaxInFlight: 5,
Identify: Identify{MessageTimeout: time.Second * 2},
})
if err != nil {
t.Fatal(err)
}
consumer.Start()
deadline := time.NewTimer(10 * time.Second)
defer deadline.Stop()
// Consume 5 messages do not ack them
// to incur a re-queue on remaining in-flight
msgNum := 0
for msgNum < 5 {
select {
case msg := <-consumer.Messages():
fmt.Printf("start handling message %s\n", string(msg.Body))
msgNum++
case <-deadline.C:
t.Error("timeout")
return
}
}
fmt.Printf("Allow some time for the nsqd to timeout received messages")
// Allow some time for the nsqd to timeout received messages and send 10+ more messages
// to client to deadlock runConn
time.Sleep(6 * time.Second)
// At this point runConn should have deadlock when Stop is executing
consumer.Stop()
//Make sure the channel gets closed at some point.
for msg := range consumer.Messages() {
t.Error("unexpected message:", msg)
msg.Finish()
}
consumer2, _ := NewConsumer(ConsumerConfig{
Topic: TopicName,
Channel: "foo",
Address: "localhost:4150",
DialTimeout: time.Second * 60,
ReadTimeout: time.Second * 60,
WriteTimeout: time.Second * 60,
MaxInFlight: 100,
})
deadline = time.NewTimer(10 * time.Second)
defer deadline.Stop()
consumer2.Start()
msgNum = 0
for msgNum < numberMessages {
select {
case msg := <-consumer2.Messages():
fmt.Printf("handling message %s\n", string(msg.Body))
msg.Finish()
msgNum++
delete(sentMessages, string(msg.Body))
case <-deadline.C:
t.Error("timeout")
return
}
}
consumer2.Stop()
// Make sure the channel gets closed at some point.
for msg := range consumer2.Messages() {
t.Error("unexpected message:", msg)
msg.Finish()
}
// Make sure we received all sent messages
if len(sentMessages) > 0 {
for msg := range sentMessages {
t.Error("Client did not receive message: ", msg)
}
}
}