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
|
// Copyright 2012-2018 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package test
import (
"encoding/json"
"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, "127.0.0.1", 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)
// Could arrive in any order
checkMsg(t, matches[0], "foo", "", "", "2", "ok")
checkMsg(t, matches[1], "foo", "", "", "2", "ok")
}
func TestProtoErr(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "127.0.0.1", 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, "127.0.0.1", 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, "127.0.0.1", 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[sidIndex])]++
}
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, "127.0.0.1", 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[sidIndex])]++
}
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, "127.0.0.1", 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, "127.0.0.1", 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, "127.0.0.1", PROTO_TEST_PORT)
defer c.Close()
send, expect := sendCommand(t, c), expectCommand(t, c)
checkInfoMsg(t, c)
send("CONNECT {\"verbose\":true,\"tls_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, "127.0.0.1", 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, "127.0.0.1", 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)
}
func TestControlLineMaximums(t *testing.T) {
s := runProtoServer()
defer s.Shutdown()
c := createClientConn(t, "127.0.0.1", PROTO_TEST_PORT)
defer c.Close()
send, expect := setupConn(t, c)
pubTooLong := "PUB foo "
for i := 0; i < 32; i++ {
pubTooLong += "2222222222"
}
send(pubTooLong)
expect(errRe)
}
func TestServerInfoWithClientAdvertise(t *testing.T) {
opts := DefaultTestOptions
opts.Port = PROTO_TEST_PORT
opts.ClientAdvertise = "me:1"
s := RunServer(&opts)
defer s.Shutdown()
c := createClientConn(t, opts.Host, PROTO_TEST_PORT)
defer c.Close()
buf := expectResult(t, c, infoRe)
js := infoRe.FindAllSubmatch(buf, 1)[0][1]
var sinfo server.Info
err := json.Unmarshal(js, &sinfo)
if err != nil {
t.Fatalf("Could not unmarshal INFO json: %v\n", err)
}
if sinfo.Host != "me" || sinfo.Port != 1 {
t.Fatalf("Expected INFO Host:Port to be me:1, got %s:%d", sinfo.Host, sinfo.Port)
}
}
|