-
Notifications
You must be signed in to change notification settings - Fork 46
/
consumer_test.go
322 lines (261 loc) · 5.35 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
package cony
import (
"bytes"
"errors"
"testing"
"time"
"github.com/streadway/amqp"
)
func TestAutoAck(t *testing.T) {
c := newTestConsumer(AutoAck())
if !c.autoAck {
t.Error("autoAck shoud be true")
}
}
func TestAutoTag(t *testing.T) {
c := newTestConsumer(AutoTag())
if c.tag == "" {
t.Error("tag should be set")
}
}
func TestConsumer_Cancel(t *testing.T) {
var stopped bool
done := make(chan bool)
c := newTestConsumer()
go func() {
<-done
select {
case <-c.stop:
stopped = true
case <-time.After(1 * time.Millisecond):
return
}
done <- true
}()
done <- true
c.Cancel()
<-done
if !stopped {
t.Error("Cancel() should send stop signal")
}
}
func TestConsumer_Cancel_willNotBlock(t *testing.T) {
var ok bool
c := newTestConsumer()
go func() {
c.Cancel()
c.Cancel()
c.Cancel()
ok = true
}()
time.Sleep(1 * time.Microsecond) // let goroutine to work
if !ok {
t.Error("shold not block")
}
}
func TestConsumer_Deliveries(t *testing.T) {
c := newTestConsumer()
go func() {
select {
case c.deliveries <- amqp.Delivery{Body: []byte("hello")}:
case <-time.After(1 * time.Millisecond):
t.Error("timeout")
}
}()
select {
case d := <-c.Deliveries():
if bytes.Compare(d.Body, []byte("hello")) != 0 {
t.Error("amqp.Delivery should be the same")
}
case <-time.After(1 * time.Millisecond):
t.Error("Deliveries() channel should deliver amqp.Delivery{}")
}
}
func TestConsumer_Errors(t *testing.T) {
c := newTestConsumer()
go func() {
select {
case c.errs <- errors.New("Hello error"):
case <-time.After(1 * time.Millisecond):
t.Error("timeout")
}
}()
select {
case err := <-c.Errors():
if err.Error() != "Hello error" {
t.Error("Error message should match")
}
case <-time.After(1 * time.Millisecond):
t.Error("Errors() channel should deliver errors")
}
}
func TestConsumer_reportErr(t *testing.T) {
var (
okDefault, okNil bool
)
c := newTestConsumer()
testErr := errors.New("test error")
go func() {
for i := 0; i <= 101; i++ {
c.reportErr(testErr)
}
okDefault = c.reportErr(testErr)
okNil = !c.reportErr(nil)
}()
err := <-c.errs
if err != testErr {
t.Error("error should be the same")
}
if !okDefault {
t.Error("reportErr should not block")
}
if !okNil {
t.Error("reportErr should return false on nil error")
}
}
func TestConsumer_serve_qos(t *testing.T) {
var (
qos int
qosError = errors.New("Qos")
runSync = make(chan bool)
)
c := newTestConsumer(Qos(10))
ch1 := &mqChannelTest{
_Qos: func(prefetchCount int, prefetchSize int, global bool) error {
qos = prefetchCount
return qosError
},
}
go func() {
<-runSync
c.serve(nil, ch1)
runSync <- true
}()
runSync <- true
err := <-c.errs
<-runSync
if qos != 10 {
t.Error("consumer should declare qos")
}
if err != qosError {
t.Error("reported error should be qos")
}
}
func TestConsumer_serve_Consume(t *testing.T) {
var (
runSync = make(chan bool)
consumeError = errors.New("consume error")
)
c := newTestConsumer()
ch1 := &mqChannelTest{
_Qos: func(int, int, bool) error {
return nil
},
_Consume: func(name string, tag string, autoAck bool, exclusive bool, noLocal bool, noWait bool, args amqp.Table) (<-chan amqp.Delivery, error) {
return nil, consumeError
},
}
go func() {
<-runSync
c.serve(nil, ch1)
runSync <- true
}()
runSync <- true
err := <-c.errs
<-runSync
if err != consumeError {
t.Error("reported error should be consume")
}
}
func TestConsumer_serve_for(t *testing.T) {
var (
runSync = make(chan bool)
deleted bool
closed bool
deliveries = make(chan amqp.Delivery)
)
c := newTestConsumer()
cli := &mqDeleterTest{
_deleteConsumer: func(*Consumer) {
deleted = true
},
}
ch1 := &mqChannelTest{
_Qos: func(int, int, bool) error {
return nil
},
_Consume: func(string, string, bool, bool, bool, bool, amqp.Table) (<-chan amqp.Delivery, error) {
return deliveries, nil
},
_Close: func() error {
closed = true
return nil
},
}
go func() {
<-runSync
c.serve(cli, ch1)
runSync <- true
}()
runSync <- true
deliveries <- amqp.Delivery{Body: []byte("test1")}
msg := <-c.Deliveries()
go func() {
c.Cancel()
}()
<-runSync
if bytes.Compare(msg.Body, []byte("test1")) != 0 {
t.Error("should deliver correct message")
}
if !deleted {
t.Error("should delete consumer")
}
if !closed {
t.Error("should close channel")
}
go func() {
<-runSync
c.serve(cli, ch1)
runSync <- true
}()
runSync <- true
close(deliveries) // immitate close of amqp.Channel
<-runSync
}
func TestExclusive(t *testing.T) {
c := newTestConsumer(Exclusive())
if !c.exclusive {
t.Error("exclusive should be set")
}
}
func TestNewConsumer(t *testing.T) {
var called bool
NewConsumer(&Queue{}, func(*Consumer) {
called = true
})
if !called {
t.Error("NewConsumer should call input functional options")
}
}
func TestNoLocal(t *testing.T) {
c := newTestConsumer(NoLocal())
if !c.noLocal {
t.Error("noLocal should be set")
}
}
func TestQos(t *testing.T) {
c := newTestConsumer(Qos(10))
if c.qos != 10 {
t.Error("qos should be set to 10")
}
}
func TestTag(t *testing.T) {
c := newTestConsumer(Tag("hello"))
if c.tag != "hello" {
t.Error("tag should be set to `hello`")
}
}
func newTestConsumer(opts ...ConsumerOpt) *Consumer {
q := &Queue{}
return NewConsumer(q, opts...)
}