-
Notifications
You must be signed in to change notification settings - Fork 46
/
publisher_test.go
357 lines (296 loc) · 6.43 KB
/
publisher_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
package cony
import (
"bytes"
"errors"
"io"
"testing"
"github.com/streadway/amqp"
)
func TestPublisherImplements_io_Writer(t *testing.T) {
var _ io.Writer = &Publisher{}
}
func TestPublisher_Cancel(t *testing.T) {
var (
stopped bool
runSync = make(chan bool)
)
p := newTestPublisher()
go func() {
<-runSync
<-p.stop
stopped = true
runSync <- true
}()
runSync <- true
p.Cancel()
// just make sure that multiple calls will not blow up
p.Cancel()
p.Cancel()
<-runSync
if !stopped {
t.Error("Cancel() should send stop signal")
}
}
func TestPublisher_Cancel_willNotBlock(t *testing.T) {
var (
ok bool
runSync = make(chan bool)
)
p := newTestPublisher()
go func() {
<-runSync
p.Cancel()
p.Cancel()
p.Cancel()
ok = true
runSync <- true
}()
runSync <- true
<-runSync
if !ok {
t.Error("shold not block")
}
}
func TestPublisher_serve(t *testing.T) {
var (
runSync = make(chan bool)
deleted bool
closed bool
notifyClose bool
exchangeName string
routingKey string
testMsg *amqp.Publishing
)
p := newTestPublisher()
cli := &mqDeleterTest{
_deletePublisher: func(*Publisher) {
deleted = true
},
}
ch1 := &mqChannelTest{
_Close: func() error {
closed = true
return nil
},
_NotifyClose: func(errChan chan *amqp.Error) chan *amqp.Error {
notifyClose = true
return errChan
},
_Publish: func(ex string, key string, mandatory bool, immediate bool, msg amqp.Publishing) error {
exchangeName = ex
routingKey = key
testMsg = &msg
return nil
},
}
go func() {
<-runSync
p.serve(cli, ch1)
runSync <- true
}()
runSync <- true
p.Write([]byte("test1"))
p.Cancel()
<-runSync
if !notifyClose {
t.Error("should register notifyClose")
}
if !deleted {
t.Error("should delete publisher")
}
if !closed {
t.Error("should close channel")
}
if exchangeName != "exchange.name" {
t.Error("should set correct routing key")
}
if routingKey != "routing.key" {
t.Error("should set correct routing key")
}
if bytes.Compare(testMsg.Body, []byte("test1")) != 0 {
t.Error("should publish correct messaged")
}
}
func TestPublisher_serve_customRoutingKey(t *testing.T) {
var (
runSync = make(chan bool)
deleted bool
closed bool
notifyClose bool
exchangeName string
routingKey string
testMsg *amqp.Publishing
)
p := newTestPublisher()
cli := &mqDeleterTest{
_deletePublisher: func(*Publisher) {
deleted = true
},
}
ch1 := &mqChannelTest{
_Close: func() error {
closed = true
return nil
},
_NotifyClose: func(errChan chan *amqp.Error) chan *amqp.Error {
notifyClose = true
return errChan
},
_Publish: func(ex string, key string, mandatory bool, immediate bool, msg amqp.Publishing) error {
exchangeName = ex
routingKey = key
testMsg = &msg
return nil
},
}
go func() {
<-runSync
p.serve(cli, ch1)
runSync <- true
}()
runSync <- true
p.PublishWithRoutingKey(amqp.Publishing{Body: []byte("test1")}, "my.routing.key")
p.Cancel()
<-runSync
if !notifyClose {
t.Error("should register notifyClose")
}
if !deleted {
t.Error("should delete publisher")
}
if !closed {
t.Error("should close channel")
}
if exchangeName != "exchange.name" {
t.Error("should set correct routing key")
}
if routingKey != "my.routing.key" {
t.Error("should set correct routing key")
}
if bytes.Compare(testMsg.Body, []byte("test1")) != 0 {
t.Error("should publish correct messaged")
}
}
func TestPublisher_serve_errors(t *testing.T) {
var (
runSync = make(chan bool)
testErrChan chan *amqp.Error
testPublishErr = errors.New("pub err")
_deletePublisher bool
)
p := newTestPublisher()
cli := &mqDeleterTest{
_deletePublisher: func(*Publisher) {
_deletePublisher = true
},
}
ch1 := &mqChannelTest{
_Close: func() error {
return nil
},
_NotifyClose: func(errChan chan *amqp.Error) chan *amqp.Error {
testErrChan = errChan
return errChan
},
_Publish: func(string, string, bool, bool, amqp.Publishing) error {
return testPublishErr
},
}
go func() {
<-runSync
p.serve(cli, ch1)
runSync <- true
}()
runSync <- true
_, err := p.Write([]byte("test1"))
close(testErrChan) // immitate amqp.Channel close
<-runSync
if err != testPublishErr {
t.Error("should return correct error")
}
if _deletePublisher {
t.Error("on errors should not delete publisher")
}
}
func TestNewPublisher(t *testing.T) {
var called bool
NewPublisher("", "", func(*Publisher) {
called = true
})
if !called {
t.Error("NewPublisher should call input functional options")
}
}
func TestPublisher_Publish(t *testing.T) {
var ok bool
testBuf := []byte("test1")
tpl1 := amqp.Publishing{AppId: "app1"}
msg1 := amqp.Publishing{AppId: "app2", Body: testBuf}
p := newTestPublisher(PublishingTemplate(tpl1))
testErr := errors.New("testing error")
go func() {
envelop := <-p.pubChan
msg := <-envelop.pub
if bytes.Compare(msg.Body, testBuf) == 0 {
if msg.AppId == "app2" {
ok = true
}
}
envelop.err <- testErr
}()
err := p.Publish(msg1)
if err != testErr {
t.Error("Publish should receive correct error")
}
if !ok {
t.Error("Publish() sent wrong message")
}
}
func TestPublisher_PublishWithStop(t *testing.T) {
msg1 := amqp.Publishing{Body: []byte("test1")}
p := newTestPublisher()
go func() {
p.Cancel()
}()
err := p.Publish(msg1)
if err != ErrPublisherDead {
t.Error("Publish should receive", ErrPublisherDead)
}
}
func TestPublisher_Write(t *testing.T) {
var ok bool
testBuf := []byte("test1")
p := newTestPublisher(PublishingTemplate(amqp.Publishing{AppId: "app1"}))
testErr := errors.New("testing error")
go func() {
envelop := <-p.pubChan
msg := <-envelop.pub
if bytes.Compare(msg.Body, testBuf) == 0 {
if msg.AppId == "app1" {
ok = true
}
}
envelop.err <- testErr
}()
n, err := p.Write(testBuf)
if n != len(testBuf) {
t.Error("Write() should return len equal to input buffer")
}
if err != testErr {
t.Error("Write() should return correct error")
}
if !ok {
t.Error("Write() send incorrect message")
}
}
func TestPublishingTemplate(t *testing.T) {
p := newTestPublisher()
pub := amqp.Publishing{AppId: "ololoapp"}
PublishingTemplate(pub)(p)
if p.tmpl.AppId != "ololoapp" {
t.Error("PublishingTemplate() should update template")
}
}
func newTestPublisher(opts ...PublisherOpt) *Publisher {
return NewPublisher("exchange.name", "routing.key", opts...)
}