forked from NeowayLabs/wabbit
-
Notifications
You must be signed in to change notification settings - Fork 2
/
amqp_test.go
297 lines (226 loc) · 4.89 KB
/
amqp_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
package wabbit_test
import (
"fmt"
"testing"
"time"
"github.com/PeriscopeData/wabbit"
"github.com/PeriscopeData/wabbit/amqptest"
"github.com/PeriscopeData/wabbit/amqptest/server"
)
func TestBasicUsage(t *testing.T) {
mockConn, err := amqptest.Dial("amqp://localhost:5672/%2f") // will fail
if err == nil {
t.Errorf("First Dial must fail because no fake server is running...")
return
}
fakeServer := server.NewServer("amqp://localhost:5672/%2f")
if fakeServer == nil {
t.Errorf("Failed to instantiate fake server")
return
}
err = fakeServer.Start()
if err != nil {
t.Errorf("Failed to start fake server")
}
mockConn, err = amqptest.Dial("amqp://localhost:5672/%2f") // now it works =D
if err != nil {
t.Error(err)
return
}
if mockConn == nil {
t.Error("Invalid mockConn")
return
}
}
func pub(conn wabbit.Conn, t *testing.T, done chan bool) {
var (
publisher wabbit.Publisher
confirm chan wabbit.Confirmation
)
// helper function to verify publisher confirms
checkConfirm := func(expected uint64) error {
c := <-confirm
if !c.Ack() {
return fmt.Errorf("confirmation ack should be true")
}
if c.DeliveryTag() != expected {
return fmt.Errorf("confirmation delivery tag should be %d (got: %d)", expected, c.DeliveryTag())
}
return nil
}
channel, err := conn.Channel()
if err != nil {
t.Error(err)
return
}
err = channel.ExchangeDeclare(
"test", // name of the exchange
"topic", // type
wabbit.Option{
"durable": true,
"delete": false,
"internal": false,
"noWait": false,
},
)
if err != nil {
goto PubError
}
err = channel.Confirm(false)
if err != nil {
goto PubError
}
confirm = channel.NotifyPublish(make(chan wabbit.Confirmation, 1))
publisher, err = amqptest.NewPublisher(conn, channel)
if err != nil {
goto PubError
}
err = publisher.Publish("test", "wabbit-test-route", []byte("msg1"), nil)
if err != nil {
goto PubError
}
err = checkConfirm(1)
if err != nil {
goto PubError
}
err = publisher.Publish("test", "wabbit-test-route", []byte("msg2"), nil)
if err != nil {
goto PubError
}
err = checkConfirm(2)
if err != nil {
goto PubError
}
err = publisher.Publish("test", "wabbit-test-route", []byte("msg3"), nil)
if err != nil {
goto PubError
}
err = checkConfirm(3)
if err != nil {
goto PubError
}
goto PubSuccess
PubError:
t.Error(err)
PubSuccess:
done <- true
}
func sub(conn wabbit.Conn, t *testing.T, done chan bool, bindDone chan bool) {
var (
err error
queue wabbit.Queue
deliveries <-chan wabbit.Delivery
timer <-chan time.Time
deliveryDone chan bool
)
channel, err := conn.Channel()
if err != nil {
goto SubError
}
err = channel.ExchangeDeclare(
"test", // name of the exchange
"topic", // type
wabbit.Option{
"durable": true,
"delete": false,
"internal": false,
"noWait": false,
},
)
if err != nil {
goto SubError
}
queue, err = channel.QueueDeclare(
"sub-queue", // name of the queue
wabbit.Option{
"durable": true,
"delete": false,
"exclusive": false,
"noWait": false,
},
)
if err != nil {
goto SubError
}
err = channel.QueueBind(
queue.Name(), // name of the queue
"wabbit-test-route", // bindingKey
"test", // sourceExchange
wabbit.Option{
"noWait": false,
},
)
if err != nil {
goto SubError
}
bindDone <- true
deliveries, err = channel.Consume(
queue.Name(), // name
"anyname", // consumerTag,
wabbit.Option{
"noAck": false,
"exclusive": false,
"noLocal": false,
"noWait": false,
},
)
if err != nil {
goto SubError
}
timer = time.After(5 * time.Second)
deliveryDone = make(chan bool)
go func() {
msg1 := <-deliveries
if string(msg1.Body()) != "msg1" {
t.Errorf("Unexpected message: %s", string(msg1.Body()))
deliveryDone <- true
return
}
msg2 := <-deliveries
if string(msg2.Body()) != "msg2" {
t.Errorf("Unexpected message: %s", string(msg2.Body()))
deliveryDone <- true
return
}
msg3 := <-deliveries
if string(msg3.Body()) != "msg3" {
t.Errorf("Unexpected message: %s", string(msg3.Body()))
deliveryDone <- true
return
}
deliveryDone <- true
}()
select {
case <-deliveryDone:
goto SubSuccess
case <-timer:
err = fmt.Errorf("No data received in sub")
goto SubError
}
SubError:
t.Error(err)
SubSuccess:
done <- true
}
func TestPubSub(t *testing.T) {
var err error
connString := "amqp://anyhost:anyport/%2fanyVHost"
fakeServer := server.NewServer(connString)
err = fakeServer.Start()
if err != nil {
t.Error(err)
return
}
conn, err := amqptest.Dial(connString)
if err != nil {
t.Error(err)
return
}
done := make(chan bool)
bindingDone := make(chan bool)
go sub(conn, t, done, bindingDone)
<-bindingDone // AMQP will silently discards messages with no route binding.
go pub(conn, t, done)
<-done
<-done
}