forked from nats-io/nats-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproto_test.go
265 lines (204 loc) · 5.26 KB
/
proto_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
// Copyright 2012-2014 Apcera Inc. All rights reserved.
package test
import (
"testing"
"time"
"github.com/nats-io/gnatsd/server"
)
const PROTO_TEST_PORT = 9922
func runProtoServer() *server.Server {
opts := DefaultTestOptions
opts.Port = PROTO_TEST_PORT
return RunServer(&opts)
}
func TestProtoBasics(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
expectMsgs := expectMsgsCommand(t, expect)
// Ping
send("PING\r\n")
expect(pongRe)
// Single Msg
send("SUB foo 1\r\nPUB foo 5\r\nhello\r\n")
matches := expectMsgs(1)
checkMsg(t, matches[0], "foo", "1", "", "5", "hello")
// 2 Messages
send("SUB * 2\r\nPUB foo 2\r\nok\r\n")
matches = expectMsgs(2)
checkMsg(t, matches[0], "foo", "1", "", "2", "ok")
checkMsg(t, matches[1], "foo", "2", "", "2", "ok")
}
func TestProtoErr(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
// Make sure we get an error on bad proto
send("ZZZ")
expect(errRe)
}
func TestUnsubMax(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
expectMsgs := expectMsgsCommand(t, expect)
send("SUB foo 22\r\n")
send("UNSUB 22 2\r\n")
for i := 0; i < 100; i++ {
send("PUB foo 2\r\nok\r\n")
}
time.Sleep(50 * time.Millisecond)
matches := expectMsgs(2)
checkMsg(t, matches[0], "foo", "22", "", "2", "ok")
checkMsg(t, matches[1], "foo", "22", "", "2", "ok")
}
func TestQueueSub(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
expectMsgs := expectMsgsCommand(t, expect)
sent := 100
send("SUB foo qgroup1 22\r\n")
send("SUB foo qgroup1 32\r\n")
for i := 0; i < sent; i++ {
send("PUB foo 2\r\nok\r\n")
}
// Wait for responses
time.Sleep(250 * time.Millisecond)
matches := expectMsgs(sent)
sids := make(map[string]int)
for _, m := range matches {
sids[string(m[SID_INDEX])]++
}
if len(sids) != 2 {
t.Fatalf("Expected only 2 sids, got %d\n", len(sids))
}
for k, c := range sids {
if c < 35 {
t.Fatalf("Expected ~50 (+-15) msgs for sid:'%s', got %d\n", k, c)
}
}
}
func TestMultipleQueueSub(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
expectMsgs := expectMsgsCommand(t, expect)
sent := 100
send("SUB foo g1 1\r\n")
send("SUB foo g1 2\r\n")
send("SUB foo g2 3\r\n")
send("SUB foo g2 4\r\n")
for i := 0; i < sent; i++ {
send("PUB foo 2\r\nok\r\n")
}
// Wait for responses
time.Sleep(250 * time.Millisecond)
matches := expectMsgs(sent * 2)
sids := make(map[string]int)
for _, m := range matches {
sids[string(m[SID_INDEX])]++
}
if len(sids) != 4 {
t.Fatalf("Expected 4 sids, got %d\n", len(sids))
}
for k, c := range sids {
if c < 35 {
t.Fatalf("Expected ~50 (+-15) msgs for '%s', got %d\n", k, c)
}
}
}
func TestPubToArgState(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
send("PUBS foo 2\r\nok\r\n")
expect(errRe)
}
func TestSubToArgState(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
send("SUBZZZ foo 1\r\n")
expect(errRe)
}
// Issue #63
func TestProtoCrash(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := sendCommand(t, c), expectCommand(t, c)
checkInfoMsg(t, c)
send("CONNECT {\"verbose\":true,\"ssl_required\":false,\"user\":\"test\",\"pedantic\":true,\"pass\":\"password\"}")
time.Sleep(100 * time.Millisecond)
send("\r\n")
expect(okRe)
}
// Issue #136
func TestDuplicateProtoSub(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
send("PING\r\n")
expect(pongRe)
send("SUB foo 1\r\n")
send("SUB foo 1\r\n")
ns := 0
for i := 0; i < 5; i++ {
ns = int(s.NumSubscriptions())
if ns == 0 {
time.Sleep(50 * time.Millisecond)
} else {
break
}
}
if ns != 1 {
t.Fatalf("Expected 1 subscription, got %d\n", ns)
}
}
func TestIncompletePubArg(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
size := 10000
goodBuf := ""
for i := 0; i < size; i++ {
goodBuf += "A"
}
goodBuf += "\r\n"
badSize := 3371
badBuf := ""
for i := 0; i < badSize; i++ {
badBuf += "B"
}
// Message is corrupted and since we are still reading from client,
// next PUB accidentally becomes part of the payload of the
// incomplete message thus breaking the protocol.
badBuf2 := ""
for i := 0; i < size; i++ {
badBuf2 += "C"
}
badBuf2 += "\r\n"
pub := "PUB example 10000\r\n"
send(pub + goodBuf + pub + goodBuf + pub + badBuf + pub + badBuf2)
expect(errRe)
}