-
Notifications
You must be signed in to change notification settings - Fork 443
/
producer_test.go
executable file
·393 lines (317 loc) · 8.55 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
383
384
385
386
387
388
389
390
391
392
393
package nsq
import (
"bytes"
"errors"
"io"
"log"
"net"
"os"
"runtime"
"strconv"
"strings"
"sync"
"sync/atomic"
"testing"
"time"
)
type ConsumerHandler struct {
t *testing.T
q *Consumer
messagesGood int
messagesFailed int
}
func (h *ConsumerHandler) LogFailedMessage(message *Message) {
h.messagesFailed++
h.q.Stop()
}
func (h *ConsumerHandler) HandleMessage(message *Message) error {
msg := string(message.Body)
if msg == "bad_test_case" {
return errors.New("fail this message")
}
if msg != "multipublish_test_case" && msg != "publish_test_case" {
h.t.Error("message 'action' was not correct:", msg)
}
h.messagesGood++
return nil
}
func TestProducerConnection(t *testing.T) {
config := NewConfig()
laddr := "127.0.0.1"
config.LocalAddr, _ = net.ResolveTCPAddr("tcp", laddr+":0")
w, _ := NewProducer("127.0.0.1:4150", config)
w.SetLogger(nullLogger, LogLevelInfo)
err := w.Publish("write_test", []byte("test"))
if err != nil {
t.Fatalf("should lazily connect - %s", err)
}
w.Stop()
err = w.Publish("write_test", []byte("fail test"))
if err != ErrStopped {
t.Fatalf("should not be able to write after Stop()")
}
}
func TestProducerPing(t *testing.T) {
log.SetOutput(io.Discard)
defer log.SetOutput(os.Stdout)
config := NewConfig()
w, _ := NewProducer("127.0.0.1:4150", config)
w.SetLogger(nullLogger, LogLevelInfo)
err := w.Ping()
if err != nil {
t.Fatalf("should connect on ping")
}
w.Stop()
err = w.Ping()
if err != ErrStopped {
t.Fatalf("should not be able to ping after Stop()")
}
}
func TestProducerPublish(t *testing.T) {
topicName := "publish" + strconv.Itoa(int(time.Now().Unix()))
msgCount := 10
config := NewConfig()
w, _ := NewProducer("127.0.0.1:4150", config)
w.SetLogger(nullLogger, LogLevelInfo)
defer w.Stop()
for i := 0; i < msgCount; i++ {
err := w.Publish(topicName, []byte("publish_test_case"))
if err != nil {
t.Fatalf("error %s", err)
}
}
err := w.Publish(topicName, []byte("bad_test_case"))
if err != nil {
t.Fatalf("error %s", err)
}
readMessages(topicName, t, msgCount)
}
func TestProducerMultiPublish(t *testing.T) {
topicName := "multi_publish" + strconv.Itoa(int(time.Now().Unix()))
msgCount := 10
config := NewConfig()
w, _ := NewProducer("127.0.0.1:4150", config)
w.SetLogger(nullLogger, LogLevelInfo)
defer w.Stop()
var testData [][]byte
for i := 0; i < msgCount; i++ {
testData = append(testData, []byte("multipublish_test_case"))
}
err := w.MultiPublish(topicName, testData)
if err != nil {
t.Fatalf("error %s", err)
}
err = w.Publish(topicName, []byte("bad_test_case"))
if err != nil {
t.Fatalf("error %s", err)
}
readMessages(topicName, t, msgCount)
}
func TestProducerPublishAsync(t *testing.T) {
topicName := "async_publish" + strconv.Itoa(int(time.Now().Unix()))
msgCount := 10
config := NewConfig()
w, _ := NewProducer("127.0.0.1:4150", config)
w.SetLogger(nullLogger, LogLevelInfo)
defer w.Stop()
responseChan := make(chan *ProducerTransaction, msgCount)
for i := 0; i < msgCount; i++ {
err := w.PublishAsync(topicName, []byte("publish_test_case"), responseChan, "test")
if err != nil {
t.Fatalf(err.Error())
}
}
for i := 0; i < msgCount; i++ {
trans := <-responseChan
if trans.Error != nil {
t.Fatalf(trans.Error.Error())
}
if trans.Args[0].(string) != "test" {
t.Fatalf(`proxied arg "%s" != "test"`, trans.Args[0].(string))
}
}
err := w.Publish(topicName, []byte("bad_test_case"))
if err != nil {
t.Fatalf("error %s", err)
}
readMessages(topicName, t, msgCount)
}
func TestProducerMultiPublishAsync(t *testing.T) {
topicName := "multi_publish" + strconv.Itoa(int(time.Now().Unix()))
msgCount := 10
config := NewConfig()
w, _ := NewProducer("127.0.0.1:4150", config)
w.SetLogger(nullLogger, LogLevelInfo)
defer w.Stop()
var testData [][]byte
for i := 0; i < msgCount; i++ {
testData = append(testData, []byte("multipublish_test_case"))
}
responseChan := make(chan *ProducerTransaction)
err := w.MultiPublishAsync(topicName, testData, responseChan, "test0", 1)
if err != nil {
t.Fatalf(err.Error())
}
trans := <-responseChan
if trans.Error != nil {
t.Fatalf(trans.Error.Error())
}
if trans.Args[0].(string) != "test0" {
t.Fatalf(`proxied arg "%s" != "test0"`, trans.Args[0].(string))
}
if trans.Args[1].(int) != 1 {
t.Fatalf(`proxied arg %d != 1`, trans.Args[1].(int))
}
err = w.Publish(topicName, []byte("bad_test_case"))
if err != nil {
t.Fatalf("error %s", err)
}
readMessages(topicName, t, msgCount)
}
func TestProducerHeartbeat(t *testing.T) {
topicName := "heartbeat" + strconv.Itoa(int(time.Now().Unix()))
config := NewConfig()
config.HeartbeatInterval = 100 * time.Millisecond
w, _ := NewProducer("127.0.0.1:4150", config)
w.SetLogger(nullLogger, LogLevelInfo)
defer w.Stop()
err := w.Publish(topicName, []byte("publish_test_case"))
if err == nil {
t.Fatalf("error should not be nil")
}
if identifyError, ok := err.(ErrIdentify); !ok ||
identifyError.Reason != "E_BAD_BODY IDENTIFY heartbeat interval (100) is invalid" {
t.Fatalf("wrong error - %s", err)
}
config = NewConfig()
config.HeartbeatInterval = 1000 * time.Millisecond
w, _ = NewProducer("127.0.0.1:4150", config)
w.SetLogger(nullLogger, LogLevelInfo)
defer w.Stop()
err = w.Publish(topicName, []byte("publish_test_case"))
if err != nil {
t.Fatalf(err.Error())
}
time.Sleep(1100 * time.Millisecond)
msgCount := 10
for i := 0; i < msgCount; i++ {
err := w.Publish(topicName, []byte("publish_test_case"))
if err != nil {
t.Fatalf("error %s", err)
}
}
err = w.Publish(topicName, []byte("bad_test_case"))
if err != nil {
t.Fatalf("error %s", err)
}
readMessages(topicName, t, msgCount+1)
}
func TestProducerHTTPConnectionFails(t *testing.T) {
config := NewConfig()
laddr := "127.0.0.1"
config.LocalAddr, _ = net.ResolveTCPAddr("tcp", laddr+":0")
config.MaxMsgSize = 1048576
w, _ := NewProducer("127.0.0.1:4151", config)
w.SetLogger(nullLogger, LogLevelInfo)
err := w.Publish("write_test", []byte("test"))
if err == nil {
t.Fatal("should fail connecting to HTTP endpoint", err)
}
if !strings.Contains(err.Error(), "unexpected HTTP response") {
t.Fatalf("should detect unexpected HTTP response, but got err: %s", err)
}
w.Stop()
}
func readMessages(topicName string, t *testing.T, msgCount int) {
config := NewConfig()
config.DefaultRequeueDelay = 0
config.MaxBackoffDuration = 50 * time.Millisecond
q, _ := NewConsumer(topicName, "ch", config)
q.SetLogger(nullLogger, LogLevelInfo)
h := &ConsumerHandler{
t: t,
q: q,
}
q.AddHandler(h)
err := q.ConnectToNSQD("127.0.0.1:4150")
if err != nil {
t.Fatalf(err.Error())
}
<-q.StopChan
if h.messagesGood != msgCount {
t.Fatalf("end of test. should have handled a diff number of messages %d != %d", h.messagesGood, msgCount)
}
if h.messagesFailed != 1 {
t.Fatal("failed message not done")
}
}
type mockProducerConn struct {
delegate ConnDelegate
closeCh chan struct{}
pubCh chan struct{}
}
func newMockProducerConn(delegate ConnDelegate) producerConn {
m := &mockProducerConn{
delegate: delegate,
closeCh: make(chan struct{}),
pubCh: make(chan struct{}, 4),
}
go m.router()
return m
}
func (m *mockProducerConn) String() string {
return "127.0.0.1:0"
}
func (m *mockProducerConn) SetLogger(logger logger, level LogLevel, prefix string) {}
func (m *mockProducerConn) SetLoggerLevel(lvl LogLevel) {}
func (m *mockProducerConn) SetLoggerForLevel(logger logger, level LogLevel, format string) {}
func (m *mockProducerConn) Connect() (*IdentifyResponse, error) {
return &IdentifyResponse{}, nil
}
func (m *mockProducerConn) Close() error {
close(m.closeCh)
return nil
}
func (m *mockProducerConn) WriteCommand(cmd *Command) error {
if bytes.Equal(cmd.Name, []byte("PUB")) {
m.pubCh <- struct{}{}
}
return nil
}
func (m *mockProducerConn) router() {
for {
select {
case <-m.closeCh:
goto exit
case <-m.pubCh:
m.delegate.OnResponse(nil, framedResponse(FrameTypeResponse, []byte("OK")))
}
}
exit:
}
func BenchmarkProducer(b *testing.B) {
b.StopTimer()
body := make([]byte, 512)
config := NewConfig()
p, _ := NewProducer("127.0.0.1:0", config)
p.conn = newMockProducerConn(&producerConnDelegate{p})
atomic.StoreInt32(&p.state, StateConnected)
p.closeChan = make(chan int)
go p.router()
startCh := make(chan struct{})
var wg sync.WaitGroup
parallel := runtime.GOMAXPROCS(0)
for j := 0; j < parallel; j++ {
wg.Add(1)
go func() {
<-startCh
for i := 0; i < b.N/parallel; i++ {
p.Publish("test", body)
}
wg.Done()
}()
}
b.StartTimer()
close(startCh)
wg.Wait()
}