forked from nsqio/go-nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer_test.go
260 lines (225 loc) · 6.04 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
package nsq
import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
"time"
)
type MyTestHandler struct {
t *testing.T
q *Consumer
messagesSent int
messagesReceived int
messagesFailed int
}
var nullLogger = log.New(ioutil.Discard, "", log.LstdFlags)
func (h *MyTestHandler) LogFailedMessage(message *Message) {
h.messagesFailed++
h.q.Stop()
}
func (h *MyTestHandler) HandleMessage(message *Message) error {
if string(message.Body) == "TOBEFAILED" {
h.messagesReceived++
return errors.New("fail this message")
}
data := struct {
Msg string
}{}
err := json.Unmarshal(message.Body, &data)
if err != nil {
return err
}
msg := data.Msg
if msg != "single" && msg != "double" {
h.t.Error("message 'action' was not correct: ", msg, data)
}
h.messagesReceived++
return nil
}
func SendMessage(t *testing.T, port int, topic string, method string, body []byte) {
httpclient := &http.Client{}
endpoint := fmt.Sprintf("http://127.0.0.1:%d/%s?topic=%s", port, method, topic)
req, err := http.NewRequest("POST", endpoint, bytes.NewBuffer(body))
resp, err := httpclient.Do(req)
if err != nil {
t.Fatalf(err.Error())
return
}
if resp.StatusCode != 200 {
t.Fatalf("%s status code: %d", method, resp.StatusCode)
}
resp.Body.Close()
}
func TestConsumer(t *testing.T) {
consumerTest(t, nil)
}
func TestConsumerTLS(t *testing.T) {
consumerTest(t, func(c *Config) {
c.TlsV1 = true
c.TlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
})
}
func TestConsumerDeflate(t *testing.T) {
consumerTest(t, func(c *Config) {
c.Deflate = true
})
}
func TestConsumerSnappy(t *testing.T) {
consumerTest(t, func(c *Config) {
c.Snappy = true
})
}
func TestConsumerTLSDeflate(t *testing.T) {
consumerTest(t, func(c *Config) {
c.TlsV1 = true
c.TlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
c.Deflate = true
})
}
func TestConsumerTLSSnappy(t *testing.T) {
consumerTest(t, func(c *Config) {
c.TlsV1 = true
c.TlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
c.Snappy = true
})
}
func TestConsumerTLSClientCert(t *testing.T) {
cert, _ := tls.LoadX509KeyPair("./test/client.pem", "./test/client.key")
consumerTest(t, func(c *Config) {
c.TlsV1 = true
c.TlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
}
})
}
func TestConsumerLookupdAuthorization(t *testing.T) {
// confirm that LookupAuthorization = true sets Authorization header on lookudp call
config := NewConfig()
config.AuthSecret = "AuthSecret"
topicName := "auth" + strconv.Itoa(int(time.Now().Unix()))
q, _ := NewConsumer(topicName, "ch", config)
q.SetLogger(newTestLogger(t), LogLevelDebug)
var req bool
lookupd := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
req = true
if h := r.Header.Get("Authorization"); h != "Bearer AuthSecret" {
t.Errorf("got Auth header %q", h)
}
w.WriteHeader(404)
}))
defer lookupd.Close()
h := &MyTestHandler{
t: t,
q: q,
}
q.AddHandler(h)
q.ConnectToNSQLookupd(lookupd.URL)
if req == false {
t.Errorf("lookupd call not completed")
}
}
func TestConsumerTLSClientCertViaSet(t *testing.T) {
consumerTest(t, func(c *Config) {
c.Set("tls_v1", true)
c.Set("tls_cert", "./test/client.pem")
c.Set("tls_key", "./test/client.key")
c.Set("tls_insecure_skip_verify", true)
})
}
func consumerTest(t *testing.T, cb func(c *Config)) {
config := NewConfig()
laddr := "127.0.0.1"
// so that the test can simulate binding consumer to specified address
config.LocalAddr, _ = net.ResolveTCPAddr("tcp", laddr+":0")
// so that the test can simulate reaching max requeues and a call to LogFailedMessage
config.DefaultRequeueDelay = 0
// so that the test wont timeout from backing off
config.MaxBackoffDuration = time.Millisecond * 50
if cb != nil {
cb(config)
}
topicName := "rdr_test"
if config.Deflate {
topicName = topicName + "_deflate"
} else if config.Snappy {
topicName = topicName + "_snappy"
}
if config.TlsV1 {
topicName = topicName + "_tls"
}
topicName = topicName + strconv.Itoa(int(time.Now().Unix()))
q, _ := NewConsumer(topicName, "ch", config)
q.SetLogger(newTestLogger(t), LogLevelDebug)
h := &MyTestHandler{
t: t,
q: q,
}
q.AddHandler(h)
SendMessage(t, 4151, topicName, "pub", []byte(`{"msg":"single"}`))
SendMessage(t, 4151, topicName, "mpub", []byte("{\"msg\":\"double\"}\n{\"msg\":\"double\"}"))
SendMessage(t, 4151, topicName, "pub", []byte("TOBEFAILED"))
h.messagesSent = 4
addr := "127.0.0.1:4150"
err := q.ConnectToNSQD(addr)
if err != nil {
t.Fatal(err)
}
stats := q.Stats()
if stats.Connections == 0 {
t.Fatal("stats report 0 connections (should be > 0)")
}
err = q.ConnectToNSQD(addr)
if err == nil {
t.Fatal("should not be able to connect to the same NSQ twice")
}
conn := q.conns()[0]
if !strings.HasPrefix(conn.conn.LocalAddr().String(), laddr) {
t.Fatal("connection should be bound to the specified address:", conn.conn.LocalAddr())
}
err = q.DisconnectFromNSQD("1.2.3.4:4150")
if err == nil {
t.Fatal("should not be able to disconnect from an unknown nsqd")
}
err = q.ConnectToNSQD("1.2.3.4:4150")
if err == nil {
t.Fatal("should not be able to connect to non-existent nsqd")
}
err = q.DisconnectFromNSQD("1.2.3.4:4150")
if err != nil {
t.Fatal("should be able to disconnect from an nsqd - " + err.Error())
}
<-q.StopChan
stats = q.Stats()
if stats.Connections != 0 {
t.Fatalf("stats report %d active connections (should be 0)", stats.Connections)
}
stats = q.Stats()
if stats.MessagesReceived != uint64(h.messagesReceived+h.messagesFailed) {
t.Fatalf("stats report %d messages received (should be %d)",
stats.MessagesReceived,
h.messagesReceived+h.messagesFailed)
}
if h.messagesReceived != 8 || h.messagesSent != 4 {
t.Fatalf("end of test. should have handled a diff number of messages (got %d, sent %d)", h.messagesReceived, h.messagesSent)
}
if h.messagesFailed != 1 {
t.Fatal("failed message not done")
}
}