forked from nsqio/go-nsq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconsumer_unix_socket_test.go
193 lines (166 loc) · 4.63 KB
/
consumer_unix_socket_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
package nsq
import (
"bytes"
"context"
"crypto/tls"
"fmt"
"net"
"net/http"
"strconv"
"testing"
"time"
)
func SendMessageToUnixSocket(t *testing.T, addr string, topic string, method string, body []byte) {
httpclient := http.Client{
Transport: &http.Transport{
DialContext: func(_ context.Context, _, _ string) (net.Conn, error) {
return net.Dial("unix", addr)
},
},
}
endpoint := fmt.Sprintf("http://unix/%s?topic=%s", method, topic)
resp, err := httpclient.Post(endpoint, "application/octet-stream", bytes.NewBuffer(body))
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 TestUnixSocketConsumer(t *testing.T) {
consumerUnixSocketTest(t, nil)
}
func TestUnixSocketConsumerTLS(t *testing.T) {
consumerUnixSocketTest(t, func(c *Config) {
c.TlsV1 = true
c.TlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
})
}
func TestUnixSocketConsumerDeflate(t *testing.T) {
consumerUnixSocketTest(t, func(c *Config) {
c.Deflate = true
})
}
func TestUnixSocketConsumerSnappy(t *testing.T) {
consumerUnixSocketTest(t, func(c *Config) {
c.Snappy = true
})
}
func TestUnixSocketConsumerTLSDeflate(t *testing.T) {
consumerUnixSocketTest(t, func(c *Config) {
c.TlsV1 = true
c.TlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
c.Deflate = true
})
}
func TestUnixSocketConsumerTLSSnappy(t *testing.T) {
consumerUnixSocketTest(t, func(c *Config) {
c.TlsV1 = true
c.TlsConfig = &tls.Config{
InsecureSkipVerify: true,
}
c.Snappy = true
})
}
func TestUnixSocketConsumerTLSClientCert(t *testing.T) {
cert, _ := tls.LoadX509KeyPair("./test/client.pem", "./test/client.key")
consumerUnixSocketTest(t, func(c *Config) {
c.TlsV1 = true
c.TlsConfig = &tls.Config{
Certificates: []tls.Certificate{cert},
InsecureSkipVerify: true,
}
})
}
func TestUnixSocketConsumerTLSClientCertViaSet(t *testing.T) {
consumerUnixSocketTest(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 consumerUnixSocketTest(t *testing.T, cb func(c *Config)) {
// unix addresses of nsqd are specified in test.sh
addr := "/tmp/nsqd.sock"
httpAddr := "/tmp/nsqd-http.sock"
config := NewConfig()
config.DefaultRequeueDelay = 0
// so that the test won't 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)
SendMessageToUnixSocket(t, httpAddr, topicName, "pub", []byte(`{"msg":"single"}`))
SendMessageToUnixSocket(t, httpAddr, topicName, "mpub", []byte("{\"msg\":\"double\"}\n{\"msg\":\"double\"}"))
SendMessageToUnixSocket(t, httpAddr, topicName, "pub", []byte("TOBEFAILED"))
h.messagesSent = 4
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 conn.String() != addr {
t.Fatal("connection should be bound to the specified address:", addr)
}
err = q.DisconnectFromNSQD("/tmp/doesntexist.sock")
if err == nil {
t.Fatal("should not be able to disconnect from an unknown nsqd")
}
err = q.ConnectToNSQD("/tmp/doesntexist.sock")
if err == nil {
t.Fatal("should not be able to connect to non-existent nsqd")
}
err = q.DisconnectFromNSQD("/tmp/doesntexist.sock")
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")
}
}