Debian Codegraph @

test/new_routes_test.go

raw
1 // Copyright 2018-2024 The NATS Authors
2 // Licensed under the Apache License, Version 2.0 (the "License");
3 // you may not use this file except in compliance with the License.
4 // You may obtain a copy of the License at
5 //
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 package test
15
16 import (
17 "encoding/json"
18 "fmt"
19 "net"
20 "testing"
21 "time"
22
23 "github.com/nats-io/nats-server/v2/logger"
24 "github.com/nats-io/nats-server/v2/server"
25 "github.com/nats-io/nats.go"
26 )
27
28 func runNewRouteServer(t *testing.T) (*server.Server, *server.Options) {
29 return RunServerWithConfig("./configs/new_cluster.conf")
30 }
31
32 func TestNewRouteInfoOnConnect(t *testing.T) {
33 s, opts := runNewRouteServer(t)
34 defer s.Shutdown()
35
36 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
37 defer rc.Close()
38
39 info := checkInfoMsg(t, rc)
40 if info.Port != opts.Cluster.Port {
41 t.Fatalf("Received wrong information for port, expected %d, got %d",
42 info.Port, opts.Cluster.Port)
43 }
44
45 // Make sure we advertise new proto.
46 if info.Proto < server.RouteProtoV2 {
47 t.Fatalf("Expected routeProtoV2 or above, got %d", info.Proto)
48 }
49 // New proto should always send nonce too.
50 if info.Nonce == "" {
51 t.Fatalf("Expected a non empty nonce in new route INFO")
52 }
53 // By default headers should be true.
54 if !info.Headers {
55 t.Fatalf("Expected to have headers on by default")
56 }
57 // Leafnode origin cluster support.
58 if !info.LNOC {
59 t.Fatalf("Expected to have leafnode origin cluster support")
60 }
61 if !info.LNOCU {
62 t.Fatalf("Expected to have leafnode origin cluster in unsub protocol support")
63 }
64 }
65
66 func TestNewRouteHeaderSupport(t *testing.T) {
67 srvA, srvB, optsA, optsB := runServers(t)
68 defer srvA.Shutdown()
69 defer srvB.Shutdown()
70
71 clientA := createClientConn(t, optsA.Host, optsA.Port)
72 defer clientA.Close()
73
74 clientB := createClientConn(t, optsB.Host, optsB.Port)
75 defer clientB.Close()
76
77 sendA, expectA := setupHeaderConn(t, clientA)
78 sendA("SUB foo bar 22\r\n")
79 sendA("PING\r\n")
80 expectA(pongRe)
81
82 if err := checkExpectedSubs(1, srvA, srvB); err != nil {
83 t.Fatalf("%v", err)
84 }
85
86 sendB, expectB := setupHeaderConn(t, clientB)
87 // Can not have \r\n in payload fyi for regex.
88 sendB("HPUB foo reply 12 14\r\nK1:V1,K2:V2 ok\r\n")
89 sendB("PING\r\n")
90 expectB(pongRe)
91
92 expectHeaderMsgs := expectHeaderMsgsCommand(t, expectA)
93 matches := expectHeaderMsgs(1)
94 checkHmsg(t, matches[0], "foo", "22", "reply", "12", "14", "K1:V1,K2:V2 ", "ok")
95 }
96
97 func TestNewRouteHeaderSupportOldAndNew(t *testing.T) {
98 optsA := LoadConfig("./configs/srv_a.conf")
99 optsA.NoHeaderSupport = true
100 srvA := RunServer(optsA)
101 defer srvA.Shutdown()
102
103 srvB, optsB := RunServerWithConfig("./configs/srv_b.conf")
104 defer srvB.Shutdown()
105
106 checkClusterFormed(t, srvA, srvB)
107
108 clientA := createClientConn(t, optsA.Host, optsA.Port)
109 defer clientA.Close()
110
111 clientB := createClientConn(t, optsB.Host, optsB.Port)
112 defer clientB.Close()
113
114 sendA, expectA := setupHeaderConn(t, clientA)
115 sendA("SUB foo bar 22\r\n")
116 sendA("PING\r\n")
117 expectA(pongRe)
118
119 if err := checkExpectedSubs(1, srvA, srvB); err != nil {
120 t.Fatalf("%v", err)
121 }
122
123 sendB, expectB := setupHeaderConn(t, clientB)
124 // Can not have \r\n in payload fyi for regex.
125 sendB("HPUB foo reply 12 14\r\nK1:V1,K2:V2 ok\r\n")
126 sendB("PING\r\n")
127 expectB(pongRe)
128
129 expectMsgs := expectMsgsCommand(t, expectA)
130 matches := expectMsgs(1)
131 checkMsg(t, matches[0], "foo", "22", "reply", "2", "ok")
132 }
133
134 func sendRouteInfo(t *testing.T, rc net.Conn, routeSend sendFun, routeID string) {
135 info := checkInfoMsg(t, rc)
136 info.ID = routeID
137 info.Name = ""
138 b, err := json.Marshal(info)
139 if err != nil {
140 t.Fatalf("Could not marshal test route info: %v", err)
141 }
142 routeSend(fmt.Sprintf("INFO %s\r\n", b))
143 }
144
145 func TestNewRouteConnectSubs(t *testing.T) {
146 s, opts := runNewRouteServer(t)
147 defer s.Shutdown()
148
149 c := createClientConn(t, opts.Host, opts.Port)
150 defer c.Close()
151
152 send, expect := setupConn(t, c)
153
154 // Create 10 normal subs and 10 queue subscribers.
155 for i := 0; i < 10; i++ {
156 send(fmt.Sprintf("SUB foo %d\r\n", i))
157 send(fmt.Sprintf("SUB foo bar %d\r\n", 100+i))
158 }
159 send("PING\r\n")
160 expect(pongRe)
161
162 // This client should not be considered active since no subscriptions or
163 // messages have been published.
164 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
165 defer rc.Close()
166
167 routeID := "RTEST_NEW:22"
168 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
169
170 sendRouteInfo(t, rc, routeSend, routeID)
171 buf := routeExpect(rsubRe)
172
173 matches := rsubRe.FindAllSubmatch(buf, -1)
174 if len(matches) != 2 {
175 t.Fatalf("Expected 2 results, got %d", len(matches))
176 }
177 for _, m := range matches {
178 if string(m[1]) != "$G" {
179 t.Fatalf("Expected global account name of '$G', got %q", m[1])
180 }
181 if string(m[2]) != "foo" {
182 t.Fatalf("Expected subject of 'foo', got %q", m[2])
183 }
184 if m[3] != nil {
185 if string(m[3]) != "bar" {
186 t.Fatalf("Expected group of 'bar', got %q", m[3])
187 }
188 // Expect a weighted count for the queue group
189 if len(m) != 5 {
190 t.Fatalf("Expected a weight for the queue group")
191 }
192 if m[4] == nil || string(m[4]) != "10" {
193 t.Fatalf("Expected Weight of '10', got %q", m[4])
194 }
195 }
196 }
197
198 // Close the client connection, check the results.
199 c.Close()
200
201 // Expect 2
202 for numUnSubs := 0; numUnSubs != 2; {
203 buf := routeExpect(runsubRe)
204 numUnSubs += len(runsubRe.FindAllSubmatch(buf, -1))
205 }
206 }
207
208 func TestNewRouteConnectSubsWithAccount(t *testing.T) {
209 s, opts := runNewRouteServer(t)
210 defer s.Shutdown()
211
212 accName := "$FOO"
213 s.RegisterAccount(accName)
214
215 c := createClientConn(t, opts.Host, opts.Port)
216 defer c.Close()
217
218 send, expect := setupConnWithAccount(t, s, c, accName)
219
220 // Create 10 normal subs and 10 queue subscribers.
221 for i := 0; i < 10; i++ {
222 send(fmt.Sprintf("SUB foo %d\r\n", i))
223 send(fmt.Sprintf("SUB foo bar %d\r\n", 100+i))
224 }
225 send("PING\r\n")
226 expect(pongRe)
227
228 // This client should not be considered active since no subscriptions or
229 // messages have been published.
230 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
231 defer rc.Close()
232
233 routeID := "RTEST_NEW:22"
234 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
235
236 sendRouteInfo(t, rc, routeSend, routeID)
237 buf := routeExpect(rsubRe)
238
239 matches := rsubRe.FindAllSubmatch(buf, -1)
240 if len(matches) != 2 {
241 t.Fatalf("Expected 2 results, got %d", len(matches))
242 }
243 for _, m := range matches {
244 if string(m[1]) != accName {
245 t.Fatalf("Expected global account name of %q, got %q", accName, m[1])
246 }
247 if string(m[2]) != "foo" {
248 t.Fatalf("Expected subject of 'foo', got %q", m[2])
249 }
250 if m[3] != nil {
251 if string(m[3]) != "bar" {
252 t.Fatalf("Expected group of 'bar', got %q", m[3])
253 }
254 // Expect the SID to be the total weighted count for the queue group
255 if len(m) != 5 {
256 t.Fatalf("Expected a weight for the queue group")
257 }
258 if m[4] == nil || string(m[4]) != "10" {
259 t.Fatalf("Expected Weight of '10', got %q", m[4])
260 }
261 }
262 }
263
264 // Close the client connection, check the results.
265 c.Close()
266
267 // Expect 2
268 for numUnSubs := 0; numUnSubs != 2; {
269 buf := routeExpect(runsubRe)
270 numUnSubs += len(runsubRe.FindAllSubmatch(buf, -1))
271 }
272 }
273
274 func TestNewRouteRSubs(t *testing.T) {
275 s, opts := runNewRouteServer(t)
276 defer s.Shutdown()
277
278 foo, err := s.RegisterAccount("$foo")
279 if err != nil {
280 t.Fatalf("Error creating account '$foo': %v", err)
281 }
282 bar, err := s.RegisterAccount("$bar")
283 if err != nil {
284 t.Fatalf("Error creating account '$bar': %v", err)
285 }
286
287 // Create a client an account foo.
288 clientA := createClientConn(t, opts.Host, opts.Port)
289 sendA, expectA := setupConnWithAccount(t, s, clientA, "$foo")
290 defer clientA.Close()
291 sendA("PING\r\n")
292 expectA(pongRe)
293
294 if foonc := foo.NumConnections(); foonc != 1 {
295 t.Fatalf("Expected foo account to have 1 client, got %d", foonc)
296 }
297 if barnc := bar.NumConnections(); barnc != 0 {
298 t.Fatalf("Expected bar account to have 0 clients, got %d", barnc)
299 }
300
301 // Create a routeConn
302 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
303 defer rc.Close()
304
305 routeID := "RTEST_NEW:33"
306 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
307
308 sendRouteInfo(t, rc, routeSend, routeID)
309 routeSend("PING\r\n")
310 routeExpect(pongRe)
311
312 // Have the client listen on foo.
313 sendA("SUB foo 1\r\nPING\r\n")
314 expectA(pongRe)
315
316 // Now create a new client for account $bar and have them subscribe.
317 clientB := createClientConn(t, opts.Host, opts.Port)
318 sendB, expectB := setupConnWithAccount(t, s, clientB, "$bar")
319 defer clientB.Close()
320
321 sendB("PING\r\n")
322 expectB(pongRe)
323
324 if foonc := foo.NumConnections(); foonc != 1 {
325 t.Fatalf("Expected foo account to have 1 client, got %d", foonc)
326 }
327 if barnc := bar.NumConnections(); barnc != 1 {
328 t.Fatalf("Expected bar account to have 1 client, got %d", barnc)
329 }
330
331 // Have the client listen on foo.
332 sendB("SUB foo 1\r\nPING\r\n")
333 expectB(pongRe)
334
335 routeExpect(rsubRe)
336
337 // Unsubscribe on clientA from foo subject.
338 sendA("UNSUB 1\r\nPING\r\n")
339 expectA(pongRe)
340
341 // We should get an RUSUB here.
342 routeExpect(runsubRe)
343
344 // Now unsubscribe clientB, which should trigger an RS-.
345 sendB("UNSUB 1\r\nPING\r\n")
346 expectB(pongRe)
347 // We should get an RUSUB here.
348 routeExpect(runsubRe)
349
350 // Now close down the clients.
351 clientA.Close()
352
353 sendB("SUB foo 2\r\nPING\r\n")
354 expectB(pongRe)
355
356 routeExpect(rsubRe)
357
358 // Now close down client B.
359 clientB.Close()
360
361 // This should trigger an RS-
362 routeExpect(runsubRe)
363 }
364
365 func TestNewRouteProgressiveNormalSubs(t *testing.T) {
366 s, opts := runNewRouteServer(t)
367 defer s.Shutdown()
368
369 c := createClientConn(t, opts.Host, opts.Port)
370 defer c.Close()
371
372 send, expect := setupConn(t, c)
373
374 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
375 defer rc.Close()
376
377 routeID := "RTEST_NEW:33"
378 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
379
380 sendRouteInfo(t, rc, routeSend, routeID)
381 routeSend("PING\r\n")
382 routeExpect(pongRe)
383
384 // For progressive we will expect to receive first normal sub but
385 // not subsequent ones.
386 send("SUB foo 1\r\nPING\r\n")
387 expect(pongRe)
388
389 routeExpect(rsubRe)
390
391 send("SUB foo 2\r\nPING\r\n")
392 expect(pongRe)
393 expectNothing(t, rc)
394
395 var buf []byte
396
397 // Check that sid is showing us total number of subscriptions.
398 checkQueueSub := func(n string) {
399 matches := rsubRe.FindAllSubmatch(buf, -1)
400 if len(matches) != 1 {
401 t.Fatalf("Expected 1 result, got %d", len(matches))
402 }
403 m := matches[0]
404 if len(m) != 5 {
405 t.Fatalf("Expected a SID for the queue group, only got %d elements", len(m))
406 }
407 if string(m[4]) != n {
408 t.Fatalf("Expected %q, got %q", n, m[4])
409 }
410 }
411
412 // We should always get the SUB info for QUEUES.
413 send("SUB foo bar 3\r\nPING\r\n")
414 expect(pongRe)
415 buf = routeExpect(rsubRe)
416 checkQueueSub("1")
417
418 send("SUB foo bar 4\r\nPING\r\n")
419 expect(pongRe)
420 buf = routeExpect(rsubRe)
421 checkQueueSub("2")
422
423 send("SUB foo bar 5\r\nPING\r\n")
424 expect(pongRe)
425 buf = routeExpect(rsubRe)
426 checkQueueSub("3")
427
428 // Now walk them back down.
429 // Again we should always get updates for queue subscribers.
430 // And these will be RS+ protos walking the weighted count back down.
431 send("UNSUB 5\r\nPING\r\n")
432 expect(pongRe)
433 buf = routeExpect(rsubRe)
434 checkQueueSub("2")
435
436 send("UNSUB 4\r\nPING\r\n")
437 expect(pongRe)
438 buf = routeExpect(rsubRe)
439 checkQueueSub("1")
440
441 // This one should send UNSUB
442 send("UNSUB 3\r\nPING\r\n")
443 expect(pongRe)
444 routeExpect(runsubRe)
445
446 // Now normal ones.
447 send("UNSUB 1\r\nPING\r\n")
448 expect(pongRe)
449 expectNothing(t, rc)
450
451 send("UNSUB 2\r\nPING\r\n")
452 expect(pongRe)
453 routeExpect(runsubRe)
454 }
455
456 func TestNewRouteClientClosedWithNormalSubscriptions(t *testing.T) {
457 s, opts := runNewRouteServer(t)
458 defer s.Shutdown()
459
460 c := createClientConn(t, opts.Host, opts.Port)
461 defer c.Close()
462
463 send, expect := setupConn(t, c)
464
465 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
466 defer rc.Close()
467
468 routeID := "RTEST_NEW:44"
469 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
470
471 sendRouteInfo(t, rc, routeSend, routeID)
472 routeSend("PING\r\n")
473 routeExpect(pongRe)
474
475 send("SUB foo 1\r\nPING\r\n")
476 expect(pongRe)
477 routeExpect(rsubRe)
478
479 for i := 2; i < 100; i++ {
480 send(fmt.Sprintf("SUB foo %d\r\n", i))
481 }
482 send("PING\r\n")
483 expect(pongRe)
484
485 // Expect nothing from the route.
486 expectNothing(t, rc)
487
488 // Now close connection.
489 c.Close()
490 expectNothing(t, c)
491
492 buf := routeExpect(runsubRe)
493 matches := runsubRe.FindAllSubmatch(buf, -1)
494 if len(matches) != 1 {
495 t.Fatalf("Expected only 1 unsub response when closing client connection, got %d", len(matches))
496 }
497 }
498
499 func TestNewRouteClientClosedWithQueueSubscriptions(t *testing.T) {
500 s, opts := runNewRouteServer(t)
501 defer s.Shutdown()
502
503 c := createClientConn(t, opts.Host, opts.Port)
504 defer c.Close()
505
506 send, expect := setupConn(t, c)
507
508 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
509 defer rc.Close()
510
511 routeID := "RTEST_NEW:44"
512 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
513
514 sendRouteInfo(t, rc, routeSend, routeID)
515 routeSend("PING\r\n")
516 routeExpect(pongRe)
517
518 for i := 0; i < 100; i++ {
519 send(fmt.Sprintf("SUB foo bar %d\r\n", i))
520 }
521 send("PING\r\n")
522 expect(pongRe)
523
524 // Queue subscribers will send all updates.
525 for numRSubs := 0; numRSubs != 100; {
526 buf := routeExpect(rsubRe)
527 numRSubs += len(rsubRe.FindAllSubmatch(buf, -1))
528 }
529
530 // Now close connection.
531 c.Close()
532 expectNothing(t, c)
533
534 // We should only get one unsub for the queue subscription.
535 matches := runsubRe.FindAllSubmatch(routeExpect(runsubRe), -1)
536 if len(matches) != 1 {
537 t.Fatalf("Expected only 1 unsub response when closing client connection, got %d", len(matches))
538 }
539 }
540
541 func TestNewRouteRUnsubAccountSpecific(t *testing.T) {
542 s, opts := runNewRouteServer(t)
543 defer s.Shutdown()
544
545 // Create a routeConn
546 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
547 defer rc.Close()
548
549 routeID := "RTEST_NEW:77"
550 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
551
552 sendRouteInfo(t, rc, routeSend, routeID)
553
554 // Now create 500 subs on same subject but all different accounts.
555 for i := 0; i < 500; i++ {
556 account := fmt.Sprintf("$foo.account.%d", i)
557 s.RegisterAccount(account)
558 routeSend(fmt.Sprintf("RS+ %s foo\r\n", account))
559 }
560 routeSend("PING\r\n")
561 routeExpect(pongRe)
562
563 routeSend("RS- $foo.account.22 foo\r\nPING\r\n")
564 routeExpect(pongRe)
565
566 // Do not expect a message on that account.
567 c := createClientConn(t, opts.Host, opts.Port)
568 defer c.Close()
569
570 send, expect := setupConnWithAccount(t, s, c, "$foo.account.22")
571 send("PUB foo 2\r\nok\r\nPING\r\n")
572 expect(pongRe)
573 c.Close()
574
575 // But make sure we still receive on others
576 c = createClientConn(t, opts.Host, opts.Port)
577 defer c.Close()
578 send, expect = setupConnWithAccount(t, s, c, "$foo.account.33")
579 send("PUB foo 2\r\nok\r\nPING\r\n")
580 expect(pongRe)
581
582 matches := rmsgRe.FindAllSubmatch(routeExpect(rmsgRe), -1)
583 if len(matches) != 1 {
584 t.Fatalf("Expected only 1 msg, got %d", len(matches))
585 }
586 checkRmsg(t, matches[0], "$foo.account.33", "foo", "", "2", "ok")
587 }
588
589 func TestNewRouteRSubCleanupOnDisconnect(t *testing.T) {
590 s, opts := runNewRouteServer(t)
591 defer s.Shutdown()
592
593 // Create a routeConn
594 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
595 defer rc.Close()
596
597 routeID := "RTEST_NEW:77"
598 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
599
600 sendRouteInfo(t, rc, routeSend, routeID)
601
602 // Now create 100 subs on 3 different accounts.
603 for i := 0; i < 100; i++ {
604 subject := fmt.Sprintf("foo.%d", i)
605 routeSend(fmt.Sprintf("RS+ $foo %s\r\n", subject))
606 routeSend(fmt.Sprintf("RS+ $bar %s\r\n", subject))
607 routeSend(fmt.Sprintf("RS+ $baz %s bar %d\r\n", subject, i+1))
608 }
609 routeSend("PING\r\n")
610 routeExpect(pongRe)
611
612 rc.Close()
613
614 checkFor(t, time.Second, 10*time.Millisecond, func() error {
615 if ns := s.NumSubscriptions(); ns != 0 {
616 return fmt.Errorf("Number of subscriptions is %d", ns)
617 }
618 return nil
619 })
620 }
621
622 func TestNewRouteSendSubsAndMsgs(t *testing.T) {
623 s, opts := runNewRouteServer(t)
624 defer s.Shutdown()
625
626 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
627 defer rc.Close()
628
629 routeID := "RTEST_NEW:44"
630 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
631
632 sendRouteInfo(t, rc, routeSend, routeID)
633 routeSend("PING\r\n")
634 routeExpect(pongRe)
635
636 // Now let's send in interest from the new protocol.
637 // Normal Subscription
638 routeSend("RS+ $G foo\r\n")
639 // Make sure things were processed.
640 routeSend("PING\r\n")
641 routeExpect(pongRe)
642
643 // Now create a client and send a message, make sure we receive it
644 // over the route connection.
645 c := createClientConn(t, opts.Host, opts.Port)
646 defer c.Close()
647
648 send, expect := setupConn(t, c)
649 send("PUB foo 2\r\nok\r\nPING\r\n")
650 expect(pongRe)
651
652 buf := routeExpect(rmsgRe)
653 matches := rmsgRe.FindAllSubmatch(buf, -1)
654 if len(matches) != 1 {
655 t.Fatalf("Expected only 1 msg, got %d", len(matches))
656 }
657 checkRmsg(t, matches[0], "$G", "foo", "", "2", "ok")
658
659 // Queue Subscription
660 routeSend("RS+ $G foo bar 1\r\n")
661 // Make sure things were processed.
662 routeSend("PING\r\n")
663 routeExpect(pongRe)
664
665 send("PUB foo reply 2\r\nok\r\nPING\r\n")
666 expect(pongRe)
667
668 matches = rmsgRe.FindAllSubmatch(routeExpect(rmsgRe), -1)
669 if len(matches) != 1 {
670 t.Fatalf("Expected only 1 msg, got %d", len(matches))
671 }
672 checkRmsg(t, matches[0], "$G", "foo", "+ reply bar", "2", "ok")
673
674 // Another Queue Subscription
675 routeSend("RS+ $G foo baz 1\r\n")
676 // Make sure things were processed.
677 routeSend("PING\r\n")
678 routeExpect(pongRe)
679
680 send("PUB foo reply 2\r\nok\r\nPING\r\n")
681 expect(pongRe)
682
683 matches = rmsgRe.FindAllSubmatch(routeExpect(rmsgRe), -1)
684 if len(matches) != 1 {
685 t.Fatalf("Expected only 1 msg, got %d", len(matches))
686 }
687 checkRmsg(t, matches[0], "$G", "foo", "+ reply bar baz", "2", "ok")
688
689 // Matching wildcard
690 routeSend("RS+ $G *\r\n")
691 // Make sure things were processed.
692 routeSend("PING\r\n")
693 routeExpect(pongRe)
694
695 send("PUB foo reply 2\r\nok\r\nPING\r\n")
696 expect(pongRe)
697
698 matches = rmsgRe.FindAllSubmatch(routeExpect(rmsgRe), -1)
699 if len(matches) != 1 {
700 t.Fatalf("Expected only 1 msg, got %d", len(matches))
701 }
702 checkRmsg(t, matches[0], "$G", "foo", "+ reply bar baz", "2", "ok")
703
704 // No reply
705 send("PUB foo 2\r\nok\r\nPING\r\n")
706 expect(pongRe)
707
708 matches = rmsgRe.FindAllSubmatch(routeExpect(rmsgRe), -1)
709 if len(matches) != 1 {
710 t.Fatalf("Expected only 1 msg, got %d", len(matches))
711 }
712 checkRmsg(t, matches[0], "$G", "foo", "| bar baz", "2", "ok")
713
714 // Now unsubscribe from the queue group.
715 routeSend("RS- $G foo baz\r\n")
716 routeSend("RS- $G foo bar\r\n")
717
718 routeSend("PING\r\n")
719 routeExpect(pongRe)
720
721 // Now send and make sure they are removed. We should still get the message.
722 send("PUB foo 2\r\nok\r\nPING\r\n")
723 expect(pongRe)
724
725 matches = rmsgRe.FindAllSubmatch(routeExpect(rmsgRe), -1)
726 if len(matches) != 1 {
727 t.Fatalf("Expected only 1 msg, got %d", len(matches))
728 }
729 checkRmsg(t, matches[0], "$G", "foo", "", "2", "ok")
730
731 routeSend("RS- $G foo\r\n")
732 routeSend("RS- $G *\r\n")
733
734 routeSend("PING\r\n")
735 routeExpect(pongRe)
736
737 // Now we should not receive messages anymore.
738 send("PUB foo 2\r\nok\r\nPING\r\n")
739 expect(pongRe)
740
741 expectNothing(t, rc)
742 }
743
744 func TestNewRouteProcessRoutedMsgs(t *testing.T) {
745 s, opts := runNewRouteServer(t)
746 defer s.Shutdown()
747
748 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
749 defer rc.Close()
750
751 routeID := "RTEST_NEW:55"
752 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
753
754 sendRouteInfo(t, rc, routeSend, routeID)
755 routeSend("PING\r\n")
756 routeExpect(pongRe)
757
758 // Create a client
759 c := createClientConn(t, opts.Host, opts.Port)
760 defer c.Close()
761
762 send, expect := setupConn(t, c)
763
764 // Normal sub to start
765 send("SUB foo 1\r\nPING\r\n")
766 expect(pongRe)
767 routeExpect(rsubRe)
768
769 expectMsgs := expectMsgsCommand(t, expect)
770
771 // Now send in a RMSG to the route and make sure its delivered to the client.
772 routeSend("RMSG $G foo 2\r\nok\r\nPING\r\n")
773 routeExpect(pongRe)
774 matches := expectMsgs(1)
775 checkMsg(t, matches[0], "foo", "1", "", "2", "ok")
776
777 // Now send in a RMSG to the route with a reply and make sure its delivered to the client.
778 routeSend("RMSG $G foo reply 2\r\nok\r\nPING\r\n")
779 routeExpect(pongRe)
780
781 matches = expectMsgs(1)
782 checkMsg(t, matches[0], "foo", "1", "reply", "2", "ok")
783
784 // Now add in a queue subscriber for the client.
785 send("SUB foo bar 11\r\nPING\r\n")
786 expect(pongRe)
787 routeExpect(rsubRe)
788
789 // Now add in another queue subscriber for the client.
790 send("SUB foo baz 22\r\nPING\r\n")
791 expect(pongRe)
792 routeExpect(rsubRe)
793
794 // If we send from a route with no queues. Should only get one message.
795 routeSend("RMSG $G foo 2\r\nok\r\nPING\r\n")
796 routeExpect(pongRe)
797 matches = expectMsgs(1)
798 checkMsg(t, matches[0], "foo", "1", "", "2", "ok")
799
800 // Now send to a specific queue group. We should get multiple messages now.
801 routeSend("RMSG $G foo | bar 2\r\nok\r\nPING\r\n")
802 routeExpect(pongRe)
803 matches = expectMsgs(2)
804 checkMsg(t, matches[0], "foo", "1", "", "2", "ok")
805
806 // Now send to both queue groups. We should get all messages now.
807 routeSend("RMSG $G foo | bar baz 2\r\nok\r\nPING\r\n")
808 routeExpect(pongRe)
809 matches = expectMsgs(3)
810 checkMsg(t, matches[0], "foo", "1", "", "2", "ok")
811
812 // Make sure we do the right thing with reply.
813 routeSend("RMSG $G foo + reply bar baz 2\r\nok\r\nPING\r\n")
814 routeExpect(pongRe)
815 matches = expectMsgs(3)
816 checkMsg(t, matches[0], "foo", "1", "reply", "2", "ok")
817 }
818
819 func TestNewRouteQueueSubsDistribution(t *testing.T) {
820 srvA, srvB, optsA, optsB := runServers(t)
821 defer srvA.Shutdown()
822 defer srvB.Shutdown()
823
824 clientA := createClientConn(t, optsA.Host, optsA.Port)
825 defer clientA.Close()
826
827 clientB := createClientConn(t, optsB.Host, optsB.Port)
828 defer clientB.Close()
829
830 sendA, expectA := setupConn(t, clientA)
831 sendB, expectB := setupConn(t, clientB)
832
833 // Create 100 subscribers on each server.
834 for i := 0; i < 100; i++ {
835 sproto := fmt.Sprintf("SUB foo bar %d\r\n", i)
836 sendA(sproto)
837 sendB(sproto)
838 }
839 sendA("PING\r\n")
840 expectA(pongRe)
841 sendB("PING\r\n")
842 expectB(pongRe)
843
844 // Each server should have its 100 local subscriptions, plus 1 for the route.
845 if err := checkExpectedSubs(101, srvA, srvB); err != nil {
846 t.Fatal(err.Error())
847 }
848
849 sender := createClientConn(t, optsA.Host, optsA.Port)
850 defer sender.Close()
851 send, expect := setupConn(t, sender)
852
853 // Send 100 messages from Sender
854 for i := 0; i < 100; i++ {
855 send("PUB foo 2\r\nok\r\n")
856 }
857 send("PING\r\n")
858 expect(pongRe)
859
860 numAReceived := len(msgRe.FindAllSubmatch(expectA(msgRe), -1))
861 numBReceived := len(msgRe.FindAllSubmatch(expectB(msgRe), -1))
862
863 // We may not be able to properly time all messages being ready.
864 for numAReceived+numBReceived != 100 {
865 if buf := peek(clientB); buf != nil {
866 numBReceived += len(msgRe.FindAllSubmatch(buf, -1))
867 }
868 if buf := peek(clientA); buf != nil {
869 numAReceived += len(msgRe.FindAllSubmatch(buf, -1))
870 }
871 }
872 // These should be close to 50/50
873 if numAReceived < 30 || numBReceived < 30 {
874 t.Fatalf("Expected numbers to be close to 50/50, got %d/%d", numAReceived, numBReceived)
875 }
876 }
877
878 // Since we trade interest in accounts now, we have a potential issue with a new client
879 // connecting via a brand new account, publishing and properly doing a flush, then exiting.
880 // If existing subscribers were present but on a remote server they may not get the message.
881 func TestNewRouteSinglePublishOnNewAccount(t *testing.T) {
882 srvA, srvB, optsA, optsB := runServers(t)
883 defer srvA.Shutdown()
884 defer srvB.Shutdown()
885
886 srvA.RegisterAccount("$TEST22")
887 srvB.RegisterAccount("$TEST22")
888
889 // Create and establish a listener on foo for $TEST22 account.
890 clientA := createClientConn(t, optsA.Host, optsA.Port)
891 defer clientA.Close()
892
893 sendA, expectA := setupConnWithAccount(t, srvA, clientA, "$TEST22")
894 sendA("SUB foo 1\r\nPING\r\n")
895 expectA(pongRe)
896
897 if err := checkExpectedSubs(1, srvB); err != nil {
898 t.Fatal(err.Error())
899 }
900
901 clientB := createClientConn(t, optsB.Host, optsB.Port)
902 defer clientB.Close()
903
904 // Send a message, flush to make sure server processed and close connection.
905 sendB, expectB := setupConnWithAccount(t, srvB, clientB, "$TEST22")
906 sendB("PUB foo 2\r\nok\r\nPING\r\n")
907 expectB(pongRe)
908 clientB.Close()
909
910 expectMsgs := expectMsgsCommand(t, expectA)
911 matches := expectMsgs(1)
912 checkMsg(t, matches[0], "foo", "1", "", "2", "ok")
913 }
914
915 // Same as above but make sure it works for queue subscribers as well.
916 func TestNewRouteSinglePublishToQueueSubscriberOnNewAccount(t *testing.T) {
917 srvA, srvB, optsA, optsB := runServers(t)
918 defer srvA.Shutdown()
919 defer srvB.Shutdown()
920
921 srvA.RegisterAccount("$TEST22")
922 srvB.RegisterAccount("$TEST22")
923
924 // Create and establish a listener on foo for $TEST22 account.
925 clientA := createClientConn(t, optsA.Host, optsA.Port)
926 defer clientA.Close()
927
928 sendA, expectA := setupConnWithAccount(t, srvA, clientA, "$TEST22")
929 sendA("SUB foo bar 1\r\nPING\r\n")
930 expectA(pongRe)
931
932 clientB := createClientConn(t, optsB.Host, optsB.Port)
933 defer clientB.Close()
934
935 // Send a message, flush to make sure server processed and close connection.
936 sendB, expectB := setupConnWithAccount(t, srvB, clientB, "$TEST22")
937 sendB("PUB foo bar 2\r\nok\r\nPING\r\n")
938 expectB(pongRe)
939 defer clientB.Close()
940
941 expectMsgs := expectMsgsCommand(t, expectA)
942 matches := expectMsgs(1)
943 checkMsg(t, matches[0], "foo", "1", "bar", "2", "ok")
944
945 sendB("PUB foo bar 2\r\nok\r\nPING\r\n")
946 expectB(pongRe)
947 matches = expectMsgs(1)
948 checkMsg(t, matches[0], "foo", "1", "bar", "2", "ok")
949 }
950
951 // Same as above but make sure it works for queue subscribers over multiple routes as well.
952 func TestNewRouteSinglePublishToMultipleQueueSubscriberOnNewAccount(t *testing.T) {
953 srvA, srvB, srvC, optsA, optsB, optsC := runThreeServers(t)
954 defer srvA.Shutdown()
955 defer srvB.Shutdown()
956 defer srvC.Shutdown()
957
958 srvA.RegisterAccount("$TEST22")
959 srvB.RegisterAccount("$TEST22")
960 srvC.RegisterAccount("$TEST22")
961
962 // Create and establish a listener on foo/bar for $TEST22 account. Do this on ClientA and ClientC.
963 clientA := createClientConn(t, optsA.Host, optsA.Port)
964 defer clientA.Close()
965
966 sendA, expectA := setupConnWithAccount(t, srvA, clientA, "$TEST22")
967 sendA("SUB foo bar 11\r\nPING\r\n")
968 expectA(pongRe)
969
970 clientC := createClientConn(t, optsC.Host, optsC.Port)
971 defer clientC.Close()
972
973 sendC, expectC := setupConnWithAccount(t, srvC, clientC, "$TEST22")
974 sendC("SUB foo bar 33\r\nPING\r\n")
975 expectC(pongRe)
976
977 sendA("PING\r\n")
978 expectA(pongRe)
979 sendC("PING\r\n")
980 expectC(pongRe)
981
982 clientB := createClientConn(t, optsB.Host, optsB.Port)
983 defer clientB.Close()
984
985 time.Sleep(100 * time.Millisecond)
986
987 // Send a message, flush to make sure server processed and close connection.
988 sendB, expectB := setupConnWithAccount(t, srvB, clientB, "$TEST22")
989 sendB("PUB foo 2\r\nok\r\nPING\r\n")
990 expectB(pongRe)
991 defer clientB.Close()
992
993 // This should trigger either clientA or clientC, but not both..
994 bufA := peek(clientA)
995 bufC := peek(clientC)
996
997 if bufA != nil && bufC != nil {
998 t.Fatalf("Expected one or the other, but got something on both")
999 }
1000 numReceived := len(msgRe.FindAllSubmatch(bufA, -1))
1001 numReceived += len(msgRe.FindAllSubmatch(bufC, -1))
1002 if numReceived != 1 {
1003 t.Fatalf("Expected only 1 msg, got %d", numReceived)
1004 }
1005
1006 // Now make sure that we are distributing correctly between A and C
1007 // Send 100 messages from Sender
1008 for i := 0; i < 100; i++ {
1009 sendB("PUB foo 2\r\nok\r\n")
1010 }
1011 sendB("PING\r\n")
1012 expectB(pongRe)
1013
1014 numAReceived := len(msgRe.FindAllSubmatch(expectA(msgRe), -1))
1015 numCReceived := len(msgRe.FindAllSubmatch(expectC(msgRe), -1))
1016
1017 // We may not be able to properly time all messages being ready.
1018
1019 for numAReceived+numCReceived != 100 {
1020 if buf := peek(clientC); buf != nil {
1021 numCReceived += len(msgRe.FindAllSubmatch(buf, -1))
1022 }
1023 if buf := peek(clientA); buf != nil {
1024 numAReceived += len(msgRe.FindAllSubmatch(buf, -1))
1025 }
1026 }
1027
1028 // These should be close to 50/50
1029 if numAReceived < 30 || numCReceived < 30 {
1030 t.Fatalf("Expected numbers to be close to 50/50, got %d/%d", numAReceived, numCReceived)
1031 }
1032 }
1033
1034 func registerAccounts(t *testing.T, s *server.Server) (*server.Account, *server.Account) {
1035 // Now create two accounts.
1036 f, err := s.RegisterAccount("$foo")
1037 if err != nil {
1038 t.Fatalf("Error creating account '$foo': %v", err)
1039 }
1040 b, err := s.RegisterAccount("$bar")
1041 if err != nil {
1042 t.Fatalf("Error creating account '$bar': %v", err)
1043 }
1044 return f, b
1045 }
1046
1047 func addStreamExport(subject string, authorized []*server.Account, targets ...*server.Account) {
1048 for _, acc := range targets {
1049 acc.AddStreamExport(subject, authorized)
1050 }
1051 }
1052
1053 func addServiceExport(subject string, authorized []*server.Account, targets ...*server.Account) {
1054 for _, acc := range targets {
1055 acc.AddServiceExport(subject, authorized)
1056 }
1057 }
1058
1059 var isPublic = []*server.Account(nil)
1060
1061 func TestNewRouteStreamImport(t *testing.T) {
1062 testNewRouteStreamImport(t, false)
1063 }
1064
1065 func testNewRouteStreamImport(t *testing.T, duplicateSub bool) {
1066 t.Helper()
1067 srvA, srvB, optsA, optsB := runServers(t)
1068 defer srvA.Shutdown()
1069 defer srvB.Shutdown()
1070
1071 // Do Accounts for the servers.
1072 fooA, _ := registerAccounts(t, srvA)
1073 fooB, barB := registerAccounts(t, srvB)
1074
1075 // Add export to both.
1076 addStreamExport("foo", isPublic, fooA, fooB)
1077 // Add import abilities to server B's bar account from foo.
1078 barB.AddStreamImport(fooB, "foo", "")
1079
1080 // clientA will be connected to srvA and be the stream producer.
1081 clientA := createClientConn(t, optsA.Host, optsA.Port)
1082 defer clientA.Close()
1083
1084 sendA, expectA := setupConnWithAccount(t, srvA, clientA, "$foo")
1085
1086 // Now setup client B on srvB who will do a sub from account $bar
1087 // that should map account $foo's foo subject.
1088 clientB := createClientConn(t, optsB.Host, optsB.Port)
1089 defer clientB.Close()
1090
1091 sendB, expectB := setupConnWithAccount(t, srvB, clientB, "$bar")
1092 sendB("SUB foo 1\r\n")
1093 if duplicateSub {
1094 sendB("SUB foo 1\r\n")
1095 }
1096 sendB("PING\r\n")
1097 expectB(pongRe)
1098
1099 // The subscription on "foo" for account $bar will also become
1100 // a subscription on "foo" for account $foo due to import.
1101 // So total of 2 subs.
1102 if err := checkExpectedSubs(2, srvA); err != nil {
1103 t.Fatal(err.Error())
1104 }
1105
1106 // Send on clientA
1107 sendA("PING\r\n")
1108 expectA(pongRe)
1109
1110 sendA("PUB foo 2\r\nok\r\nPING\r\n")
1111 expectA(pongRe)
1112
1113 expectMsgs := expectMsgsCommand(t, expectB)
1114 matches := expectMsgs(1)
1115 checkMsg(t, matches[0], "foo", "1", "", "2", "ok")
1116
1117 // Send Again on clientA
1118 sendA("PUB foo 2\r\nok\r\nPING\r\n")
1119 expectA(pongRe)
1120
1121 matches = expectMsgs(1)
1122 checkMsg(t, matches[0], "foo", "1", "", "2", "ok")
1123
1124 sendB("UNSUB 1\r\nPING\r\n")
1125 expectB(pongRe)
1126
1127 if err := checkExpectedSubs(0, srvA); err != nil {
1128 t.Fatal(err.Error())
1129 }
1130
1131 sendA("PUB foo 2\r\nok\r\nPING\r\n")
1132 expectA(pongRe)
1133 expectNothing(t, clientA)
1134 }
1135
1136 func TestNewRouteStreamImportLargeFanout(t *testing.T) {
1137 srvA, srvB, optsA, optsB := runServers(t)
1138 defer srvA.Shutdown()
1139 defer srvB.Shutdown()
1140
1141 // Do Accounts for the servers.
1142 // This account will export a stream.
1143 fooA, err := srvA.RegisterAccount("$foo")
1144 if err != nil {
1145 t.Fatalf("Error creating account '$foo': %v", err)
1146 }
1147 fooB, err := srvB.RegisterAccount("$foo")
1148 if err != nil {
1149 t.Fatalf("Error creating account '$foo': %v", err)
1150 }
1151
1152 // Add export to both.
1153 addStreamExport("foo", isPublic, fooA, fooB)
1154
1155 // Now we will create 100 accounts who will all import from foo.
1156 fanout := 100
1157 barA := make([]*server.Account, fanout)
1158 for i := 0; i < fanout; i++ {
1159 acc := fmt.Sprintf("$bar-%d", i)
1160 barA[i], err = srvB.RegisterAccount(acc)
1161 if err != nil {
1162 t.Fatalf("Error creating account %q: %v", acc, err)
1163 }
1164 // Add import abilities to server B's bar account from foo.
1165 barA[i].AddStreamImport(fooB, "foo", "")
1166 }
1167
1168 // clientA will be connected to srvA and be the stream producer.
1169 clientA := createClientConn(t, optsA.Host, optsA.Port)
1170 defer clientA.Close()
1171
1172 // Now setup fanout clients on srvB who will do a sub from account $bar
1173 // that should map account $foo's foo subject.
1174 clientB := make([]net.Conn, fanout)
1175 sendB := make([]sendFun, fanout)
1176 expectB := make([]expectFun, fanout)
1177
1178 for i := 0; i < fanout; i++ {
1179 clientB[i] = createClientConn(t, optsB.Host, optsB.Port)
1180 defer clientB[i].Close()
1181 sendB[i], expectB[i] = setupConnWithAccount(t, srvB, clientB[i], barA[i].Name)
1182 sendB[i]("SUB foo 1\r\nPING\r\n")
1183 expectB[i](pongRe)
1184 }
1185
1186 // Since we do not shadow all the bar acounts on srvA they will be dropped
1187 // when they hit the other side, which means we could only have one sub for
1188 // all the imports on srvA, and srvB will have 2*fanout, one normal and one
1189 // that represents the import.
1190 checkFor(t, time.Second, 10*time.Millisecond, func() error {
1191 if ns := srvA.NumSubscriptions(); ns != uint32(1) {
1192 return fmt.Errorf("Number of subscriptions is %d", ns)
1193 }
1194 if ns := srvB.NumSubscriptions(); ns != uint32(2*fanout) {
1195 return fmt.Errorf("Number of subscriptions is %d", ns)
1196 }
1197 return nil
1198 })
1199 }
1200
1201 func TestNewRouteReservedReply(t *testing.T) {
1202 s, opts := runNewRouteServer(t)
1203 defer s.Shutdown()
1204
1205 c := createClientConn(t, opts.Host, opts.Port)
1206 defer c.Close()
1207
1208 send, expect := setupConn(t, c)
1209
1210 // Test that clients can't send to reserved service import replies.
1211 send("PUB foo _R_.foo 2\r\nok\r\nPING\r\n")
1212 expect(errRe)
1213 }
1214
1215 func TestNewRouteServiceImport(t *testing.T) {
1216 // To quickly enable trace and debug logging
1217 //doLog, doTrace, doDebug = true, true, true
1218 srvA, srvB, optsA, optsB := runServers(t)
1219 defer srvA.Shutdown()
1220 defer srvB.Shutdown()
1221
1222 // Make so we can tell the two apart since in same PID.
1223 if doLog {
1224 srvA.SetLogger(logger.NewTestLogger("[SRV-A] - ", false), true, true)
1225 srvB.SetLogger(logger.NewTestLogger("[SRV-B] - ", false), true, true)
1226 }
1227
1228 // Do Accounts for the servers.
1229 fooA, barA := registerAccounts(t, srvA)
1230 fooB, barB := registerAccounts(t, srvB)
1231
1232 // Add export to both.
1233 addServiceExport("test.request", isPublic, fooA, fooB)
1234
1235 // Add import abilities to server B's bar account from foo.
1236 // Meaning that when a user sends a request on foo.request from account bar,
1237 // the request will be mapped to be received by the responder on account foo.
1238 if err := barB.AddServiceImport(fooB, "foo.request", "test.request"); err != nil {
1239 t.Fatalf("Error adding service import: %v", err)
1240 }
1241 // Do same on A.
1242 if err := barA.AddServiceImport(fooA, "foo.request", "test.request"); err != nil {
1243 t.Fatalf("Error adding service import: %v", err)
1244 }
1245
1246 // clientA will be connected to srvA and be the service endpoint and responder.
1247 clientA := createClientConn(t, optsA.Host, optsA.Port)
1248 defer clientA.Close()
1249
1250 sendA, expectA := setupConnWithAccount(t, srvA, clientA, "$foo")
1251 sendA("SUB test.request 1\r\nPING\r\n")
1252 expectA(pongRe)
1253
1254 // Now setup client B on srvB who will do a sub from account $bar
1255 // that should map account $foo's foo subject.
1256 clientB := createClientConn(t, optsB.Host, optsB.Port)
1257 defer clientB.Close()
1258
1259 sendB, expectB := setupConnWithAccount(t, srvB, clientB, "$bar")
1260 sendB("SUB reply 1\r\nPING\r\n")
1261 expectB(pongRe)
1262
1263 // Wait for all subs to be propagated. (1 on foo, 2 on bar)
1264 if err := checkExpectedSubs(3, srvA, srvB); err != nil {
1265 t.Fatal(err.Error())
1266 }
1267
1268 // Send the request from clientB on foo.request,
1269 sendB("PUB foo.request reply 2\r\nhi\r\nPING\r\n")
1270 expectB(pongRe)
1271
1272 expectMsgsA := expectMsgsCommand(t, expectA)
1273 expectMsgsB := expectMsgsCommand(t, expectB)
1274
1275 // Expect the request on A
1276 matches := expectMsgsA(1)
1277 reply := string(matches[0][replyIndex])
1278 checkMsg(t, matches[0], "test.request", "1", reply, "2", "hi")
1279 if reply == "reply" {
1280 t.Fatalf("Expected randomized reply, but got original")
1281 }
1282
1283 sendA(fmt.Sprintf("PUB %s 2\r\nok\r\nPING\r\n", reply))
1284 expectA(pongRe)
1285
1286 matches = expectMsgsB(1)
1287 checkMsg(t, matches[0], "reply", "1", "", "2", "ok")
1288
1289 // This will be the responder and the wildcard for all service replies.
1290 if ts := fooA.TotalSubs(); ts != 2 {
1291 t.Fatalf("Expected two subs to be left on fooA, but got %d", ts)
1292 }
1293
1294 routez, _ := srvA.Routez(&server.RoutezOptions{Subscriptions: true})
1295 r := routez.Routes[0]
1296 if r == nil {
1297 t.Fatalf("Expected 1 route, got none")
1298 }
1299 if r.NumSubs != 2 {
1300 t.Fatalf("Expected 2 subs in the route connection, got %v", r.NumSubs)
1301 }
1302 }
1303
1304 func TestNewRouteServiceExportWithWildcards(t *testing.T) {
1305 for _, test := range []struct {
1306 name string
1307 public bool
1308 }{
1309 {
1310 name: "public",
1311 public: true,
1312 },
1313 {
1314 name: "private",
1315 public: false,
1316 },
1317 } {
1318 t.Run(test.name, func(t *testing.T) {
1319 srvA, srvB, optsA, optsB := runServers(t)
1320 defer srvA.Shutdown()
1321 defer srvB.Shutdown()
1322
1323 // Do Accounts for the servers.
1324 fooA, barA := registerAccounts(t, srvA)
1325 fooB, barB := registerAccounts(t, srvB)
1326
1327 var accs []*server.Account
1328 // Add export to both.
1329 if !test.public {
1330 accs = []*server.Account{barA}
1331 }
1332 addServiceExport("ngs.update.*", accs, fooA)
1333 if !test.public {
1334 accs = []*server.Account{barB}
1335 }
1336 addServiceExport("ngs.update.*", accs, fooB)
1337
1338 // Add import abilities to server B's bar account from foo.
1339 if err := barB.AddServiceImport(fooB, "ngs.update", "ngs.update.$bar"); err != nil {
1340 t.Fatalf("Error adding service import: %v", err)
1341 }
1342 // Do same on A.
1343 if err := barA.AddServiceImport(fooA, "ngs.update", "ngs.update.$bar"); err != nil {
1344 t.Fatalf("Error adding service import: %v", err)
1345 }
1346
1347 // clientA will be connected to srvA and be the service endpoint and responder.
1348 clientA := createClientConn(t, optsA.Host, optsA.Port)
1349 defer clientA.Close()
1350
1351 sendA, expectA := setupConnWithAccount(t, srvA, clientA, "$foo")
1352 sendA("SUB ngs.update.* 1\r\nPING\r\n")
1353 expectA(pongRe)
1354
1355 // Now setup client B on srvB who will do a sub from account $bar
1356 // that should map account $foo's foo subject.
1357 clientB := createClientConn(t, optsB.Host, optsB.Port)
1358 defer clientB.Close()
1359
1360 sendB, expectB := setupConnWithAccount(t, srvB, clientB, "$bar")
1361 sendB("SUB reply 1\r\nPING\r\n")
1362 expectB(pongRe)
1363
1364 // Wait for all subs to be propagated. (1 on foo, 2 on bar)
1365 if err := checkExpectedSubs(3, srvA, srvB); err != nil {
1366 t.Fatal(err.Error())
1367 }
1368
1369 // Send the request from clientB on foo.request,
1370 sendB("PUB ngs.update reply 2\r\nhi\r\nPING\r\n")
1371 expectB(pongRe)
1372
1373 expectMsgsA := expectMsgsCommand(t, expectA)
1374 expectMsgsB := expectMsgsCommand(t, expectB)
1375
1376 // Expect the request on A
1377 matches := expectMsgsA(1)
1378 reply := string(matches[0][replyIndex])
1379 checkMsg(t, matches[0], "ngs.update.$bar", "1", reply, "2", "hi")
1380 if reply == "reply" {
1381 t.Fatalf("Expected randomized reply, but got original")
1382 }
1383
1384 sendA(fmt.Sprintf("PUB %s 2\r\nok\r\nPING\r\n", reply))
1385 expectA(pongRe)
1386
1387 matches = expectMsgsB(1)
1388 checkMsg(t, matches[0], "reply", "1", "", "2", "ok")
1389
1390 if ts := fooA.TotalSubs(); ts != 2 {
1391 t.Fatalf("Expected two subs to be left on fooA, but got %d", ts)
1392 }
1393
1394 routez, _ := srvA.Routez(&server.RoutezOptions{Subscriptions: true})
1395 r := routez.Routes[0]
1396 if r == nil {
1397 t.Fatalf("Expected 1 route, got none")
1398 }
1399 if r.NumSubs != 2 {
1400 t.Fatalf("Expected 2 subs in the route connection, got %v", r.NumSubs)
1401 }
1402 })
1403 }
1404 }
1405
1406 func TestNewRouteServiceImportQueueGroups(t *testing.T) {
1407 srvA, srvB, optsA, optsB := runServers(t)
1408 defer srvA.Shutdown()
1409 defer srvB.Shutdown()
1410
1411 // Do Accounts for the servers.
1412 fooA, barA := registerAccounts(t, srvA)
1413 fooB, barB := registerAccounts(t, srvB)
1414
1415 // Add export to both.
1416 addServiceExport("test.request", isPublic, fooA, fooB)
1417
1418 // Add import abilities to server B's bar account from foo.
1419 if err := barB.AddServiceImport(fooB, "foo.request", "test.request"); err != nil {
1420 t.Fatalf("Error adding service import: %v", err)
1421 }
1422 // Do same on A.
1423 if err := barA.AddServiceImport(fooA, "foo.request", "test.request"); err != nil {
1424 t.Fatalf("Error adding service import: %v", err)
1425 }
1426
1427 // clientA will be connected to srvA and be the service endpoint and responder.
1428 clientA := createClientConn(t, optsA.Host, optsA.Port)
1429 defer clientA.Close()
1430
1431 sendA, expectA := setupConnWithAccount(t, srvA, clientA, "$foo")
1432 sendA("SUB test.request QGROUP 1\r\nPING\r\n")
1433 expectA(pongRe)
1434
1435 // Now setup client B on srvB who will do a sub from account $bar
1436 // that should map account $foo's foo subject.
1437 clientB := createClientConn(t, optsB.Host, optsB.Port)
1438 defer clientB.Close()
1439
1440 sendB, expectB := setupConnWithAccount(t, srvB, clientB, "$bar")
1441 sendB("SUB reply QGROUP_TOO 1\r\nPING\r\n")
1442 expectB(pongRe)
1443
1444 // Wait for all subs to be propagated. (1 on foo, 2 on bar)
1445 if err := checkExpectedSubs(3, srvA, srvB); err != nil {
1446 t.Fatal(err.Error())
1447 }
1448
1449 // Send the request from clientB on foo.request,
1450 sendB("PUB foo.request reply 2\r\nhi\r\nPING\r\n")
1451 expectB(pongRe)
1452
1453 expectMsgsA := expectMsgsCommand(t, expectA)
1454 expectMsgsB := expectMsgsCommand(t, expectB)
1455
1456 // Expect the request on A
1457 matches := expectMsgsA(1)
1458 reply := string(matches[0][replyIndex])
1459 checkMsg(t, matches[0], "test.request", "1", reply, "2", "hi")
1460 if reply == "reply" {
1461 t.Fatalf("Expected randomized reply, but got original")
1462 }
1463
1464 sendA(fmt.Sprintf("PUB %s 2\r\nok\r\nPING\r\n", reply))
1465 expectA(pongRe)
1466
1467 matches = expectMsgsB(1)
1468 checkMsg(t, matches[0], "reply", "1", "", "2", "ok")
1469
1470 if ts := fooA.TotalSubs(); ts != 2 {
1471 t.Fatalf("Expected two subs to be left on fooA, but got %d", ts)
1472 }
1473
1474 routez, _ := srvA.Routez(&server.RoutezOptions{Subscriptions: true})
1475 r := routez.Routes[0]
1476 if r == nil {
1477 t.Fatalf("Expected 1 route, got none")
1478 }
1479 if r.NumSubs != 2 {
1480 t.Fatalf("Expected 2 subs in the route connection, got %v", r.NumSubs)
1481 }
1482 }
1483
1484 func TestNewRouteServiceImportDanglingRemoteSubs(t *testing.T) {
1485 srvA, srvB, optsA, optsB := runServers(t)
1486 defer srvA.Shutdown()
1487 defer srvB.Shutdown()
1488
1489 // Do Accounts for the servers.
1490 fooA, _ := registerAccounts(t, srvA)
1491 fooB, barB := registerAccounts(t, srvB)
1492
1493 // Add in the service export for the requests. Make it public.
1494 if err := fooA.AddServiceExport("test.request", nil); err != nil {
1495 t.Fatalf("Error adding account service export to client foo: %v", err)
1496 }
1497
1498 // Add export to both.
1499 addServiceExport("test.request", isPublic, fooA, fooB)
1500
1501 // Add import abilities to server B's bar account from foo.
1502 if err := barB.AddServiceImport(fooB, "foo.request", "test.request"); err != nil {
1503 t.Fatalf("Error adding service import: %v", err)
1504 }
1505
1506 // clientA will be connected to srvA and be the service endpoint, but will not send responses.
1507 clientA := createClientConn(t, optsA.Host, optsA.Port)
1508 defer clientA.Close()
1509
1510 sendA, expectA := setupConnWithAccount(t, srvA, clientA, "$foo")
1511 // Express interest.
1512 sendA("SUB test.request 1\r\nPING\r\n")
1513 expectA(pongRe)
1514
1515 // Now setup client B on srvB who will do a sub from account $bar
1516 // that should map account $foo's foo subject.
1517 clientB := createClientConn(t, optsB.Host, optsB.Port)
1518 defer clientB.Close()
1519
1520 sendB, expectB := setupConnWithAccount(t, srvB, clientB, "$bar")
1521 sendB("SUB reply 1\r\nPING\r\n")
1522 expectB(pongRe)
1523
1524 // Wait for all subs to be propagated (1 on foo and 1 on bar on srvA)
1525 // (note that srvA is not importing)
1526 if err := checkExpectedSubs(2, srvA); err != nil {
1527 t.Fatal(err.Error())
1528 }
1529 // Wait for all subs to be propagated (1 on foo and 2 on bar)
1530 if err := checkExpectedSubs(3, srvB); err != nil {
1531 t.Fatal(err.Error())
1532 }
1533
1534 // Send 100 requests from clientB on foo.request,
1535 for i := 0; i < 100; i++ {
1536 sendB("PUB foo.request reply 2\r\nhi\r\n")
1537 }
1538 sendB("PING\r\n")
1539 expectB(pongRe)
1540
1541 numRequests := 0
1542 // Expect the request on A
1543 checkFor(t, time.Second, 10*time.Millisecond, func() error {
1544 buf := expectA(msgRe)
1545 matches := msgRe.FindAllSubmatch(buf, -1)
1546 numRequests += len(matches)
1547 if numRequests != 100 {
1548 return fmt.Errorf("Number of requests is %d", numRequests)
1549 }
1550 return nil
1551 })
1552
1553 expectNothing(t, clientB)
1554
1555 // These reply subjects will be dangling off of $foo account on serverA.
1556 // Remove our service endpoint and wait for the dangling replies to go to zero.
1557 sendA("UNSUB 1\r\nPING\r\n")
1558 expectA(pongRe)
1559
1560 checkFor(t, time.Second, 10*time.Millisecond, func() error {
1561 if ts := fooA.TotalSubs(); ts != 1 {
1562 return fmt.Errorf("Number of subs is %d, should be only 1", ts)
1563 }
1564 return nil
1565 })
1566 }
1567
1568 func TestNewRouteNoQueueSubscribersBounce(t *testing.T) {
1569 srvA, srvB, optsA, optsB := runServers(t)
1570 defer srvA.Shutdown()
1571 defer srvB.Shutdown()
1572
1573 urlA := fmt.Sprintf("nats://%s:%d/", optsA.Host, optsA.Port)
1574 urlB := fmt.Sprintf("nats://%s:%d/", optsB.Host, optsB.Port)
1575
1576 ncA, err := nats.Connect(urlA)
1577 if err != nil {
1578 t.Fatalf("Failed to create connection for ncA: %v\n", err)
1579 }
1580 defer ncA.Close()
1581
1582 ncB, err := nats.Connect(urlB)
1583 if err != nil {
1584 t.Fatalf("Failed to create connection for ncB: %v\n", err)
1585 }
1586 defer ncB.Close()
1587
1588 response := []byte("I will help you")
1589
1590 // Create a lot of queue subscribers on A, and have one on B.
1591 ncB.QueueSubscribe("foo.request", "workers", func(m *nats.Msg) {
1592 ncB.Publish(m.Reply, response)
1593 })
1594
1595 for i := 0; i < 100; i++ {
1596 ncA.QueueSubscribe("foo.request", "workers", func(m *nats.Msg) {
1597 ncA.Publish(m.Reply, response)
1598 })
1599 }
1600 ncB.Flush()
1601 ncA.Flush()
1602
1603 // Send all requests from B
1604 numAnswers := 0
1605 for i := 0; i < 500; i++ {
1606 if _, err := ncB.Request("foo.request", []byte("Help Me"), time.Second); err != nil {
1607 t.Fatalf("Received an error on Request test [%d]: %s", i, err)
1608 }
1609 numAnswers++
1610 // After we have sent 20 close the ncA client.
1611 if i == 20 {
1612 ncA.Close()
1613 }
1614 }
1615
1616 if numAnswers != 500 {
1617 t.Fatalf("Expect to get all 500 responses, got %d", numAnswers)
1618 }
1619 }
1620
1621 func TestNewRouteLargeDistinctQueueSubscribers(t *testing.T) {
1622 srvA, srvB, optsA, optsB := runServers(t)
1623 defer srvA.Shutdown()
1624 defer srvB.Shutdown()
1625
1626 urlA := fmt.Sprintf("nats://%s:%d/", optsA.Host, optsA.Port)
1627 urlB := fmt.Sprintf("nats://%s:%d/", optsB.Host, optsB.Port)
1628
1629 ncA, err := nats.Connect(urlA)
1630 if err != nil {
1631 t.Fatalf("Failed to create connection for ncA: %v\n", err)
1632 }
1633 defer ncA.Close()
1634
1635 ncB, err := nats.Connect(urlB)
1636 if err != nil {
1637 t.Fatalf("Failed to create connection for ncB: %v\n", err)
1638 }
1639 defer ncB.Close()
1640
1641 const nqsubs = 100
1642
1643 qsubs := make([]*nats.Subscription, 100)
1644
1645 // Create 100 queue subscribers on B all with different queue groups.
1646 for i := 0; i < nqsubs; i++ {
1647 qg := fmt.Sprintf("worker-%d", i)
1648 qsubs[i], _ = ncB.QueueSubscribeSync("foo", qg)
1649 }
1650 ncB.Flush()
1651 checkFor(t, time.Second, 10*time.Millisecond, func() error {
1652 if ns := srvA.NumSubscriptions(); ns != 100 {
1653 return fmt.Errorf("Number of subscriptions is %d", ns)
1654 }
1655 return nil
1656 })
1657
1658 // Send 10 messages. We should receive 1000 responses.
1659 for i := 0; i < 10; i++ {
1660 ncA.Publish("foo", nil)
1661 }
1662 ncA.Flush()
1663
1664 checkFor(t, 2*time.Second, 10*time.Millisecond, func() error {
1665 for i := 0; i < nqsubs; i++ {
1666 if n, _, _ := qsubs[i].Pending(); n != 10 {
1667 return fmt.Errorf("Number of messages is %d", n)
1668 }
1669 }
1670 return nil
1671 })
1672 }
1673
1674 func TestNewRouteLeafNodeOriginSupport(t *testing.T) {
1675 content := `
1676 listen: 127.0.0.1:-1
1677 cluster { name: xyz, listen: 127.0.0.1:-1 }
1678 leafnodes { listen: 127.0.0.1:-1 }
1679 no_sys_acc: true
1680 `
1681 conf := createConfFile(t, []byte(content))
1682
1683 s, opts := RunServerWithConfig(conf)
1684 defer s.Shutdown()
1685
1686 gacc, _ := s.LookupAccount("$G")
1687
1688 lcontent := `
1689 listen: 127.0.0.1:-1
1690 cluster { name: ln1, listen: 127.0.0.1:-1 }
1691 leafnodes { remotes = [{ url: nats-leaf://127.0.0.1:%d }] }
1692 no_sys_acc: true
1693 `
1694 lconf := createConfFile(t, []byte(fmt.Sprintf(lcontent, opts.LeafNode.Port)))
1695
1696 ln, _ := RunServerWithConfig(lconf)
1697 defer ln.Shutdown()
1698
1699 checkLeafNodeConnected(t, s)
1700
1701 lgacc, _ := ln.LookupAccount("$G")
1702
1703 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
1704 defer rc.Close()
1705
1706 routeID := "LNOC:22"
1707 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
1708
1709 pingPong := func() {
1710 t.Helper()
1711 routeSend("PING\r\n")
1712 routeExpect(pongRe)
1713 }
1714
1715 info := checkInfoMsg(t, rc)
1716 info.ID = routeID
1717 info.Name = ""
1718 info.LNOC = true
1719 // Overwrite to false to check that we are getting LS- without origin
1720 // if we are an old server.
1721 info.LNOCU = false
1722 b, err := json.Marshal(info)
1723 if err != nil {
1724 t.Fatalf("Could not marshal test route info: %v", err)
1725 }
1726
1727 routeSend(fmt.Sprintf("INFO %s\r\n", b))
1728 routeExpect(rlsubRe)
1729 pingPong()
1730
1731 sendLSProtosFromRoute := func(lnocu bool) {
1732 t.Helper()
1733
1734 // Make sure it can process and LS+
1735 routeSend("LS+ ln1 $G foo\r\n")
1736 pingPong()
1737
1738 // Check interest is registered on remote server.
1739 if !gacc.SubscriptionInterest("foo") {
1740 t.Fatalf("Expected interest on \"foo\"")
1741 }
1742
1743 // This should not have been sent to the leafnode since same origin cluster.
1744 time.Sleep(10 * time.Millisecond)
1745 if lgacc.SubscriptionInterest("foo") {
1746 t.Fatalf("Did not expect interest on \"foo\"")
1747 }
1748
1749 // Now unsub. Either act as an old server that does not support origin
1750 // in the LS- or as a new server.
1751 if lnocu {
1752 routeSend("LS- ln1 $G foo\r\n")
1753 } else {
1754 routeSend("LS- $G foo\r\n")
1755 }
1756 pingPong()
1757
1758 // Interest should be gone.
1759 if gacc.SubscriptionInterest("foo") {
1760 t.Fatalf("Expected no interest on \"foo\"")
1761 }
1762
1763 // Make sure we did not incorrectly send an interest to the leaf.
1764 time.Sleep(10 * time.Millisecond)
1765 if lgacc.SubscriptionInterest("foo") {
1766 t.Fatalf("Did not expect interest on \"foo\"")
1767 }
1768
1769 // Repeat with a queue.
1770 routeSend("LS+ ln1 $G foo bar 1\r\n")
1771 pingPong()
1772
1773 if !gacc.SubscriptionInterest("foo") {
1774 t.Fatalf("Expected interest on \"foo\"")
1775 }
1776
1777 // This should not have been sent to the leafnode since same origin cluster.
1778 time.Sleep(10 * time.Millisecond)
1779 if lgacc.SubscriptionInterest("foo") {
1780 t.Fatalf("Did not expect interest on \"foo\"")
1781 }
1782
1783 // Now unsub.
1784 if lnocu {
1785 routeSend("LS- ln1 $G foo bar\r\n")
1786 } else {
1787 routeSend("LS- $G foo bar\r\n")
1788 }
1789 pingPong()
1790
1791 // Subscription should be gone.
1792 if gacc.SubscriptionInterest("foo") {
1793 t.Fatalf("Expected no interest on \"foo\"")
1794 }
1795
1796 // Make sure we did not incorrectly send an interest to the leaf.
1797 time.Sleep(10 * time.Millisecond)
1798 if lgacc.SubscriptionInterest("foo") {
1799 t.Fatalf("Did not expect interest on \"foo\"")
1800 }
1801 }
1802
1803 // Check the LS+/- when not supporting origin in LS-
1804 sendLSProtosFromRoute(false)
1805
1806 // Create a connection on the leafnode server.
1807 nc, err := nats.Connect(ln.ClientURL())
1808 if err != nil {
1809 t.Fatalf("Unexpected error connecting %v", err)
1810 }
1811 defer nc.Close()
1812
1813 sub, _ := nc.SubscribeSync("bar")
1814 // Let it propagate to the main server
1815 checkFor(t, time.Second, 10*time.Millisecond, func() error {
1816 if !gacc.SubscriptionInterest("bar") {
1817 return fmt.Errorf("No interest")
1818 }
1819 return nil
1820 })
1821 // For "bar"
1822 routeExpect(rlsubRe)
1823
1824 // Now pretend like we send a message to the main server over the
1825 // route but from the same origin cluster, should not be delivered
1826 // to the leafnode.
1827
1828 // Make sure it can process and LMSG.
1829 // LMSG for routes is like HMSG with an origin cluster before the account.
1830 routeSend("LMSG ln1 $G bar 0 2\r\nok\r\n")
1831 pingPong()
1832
1833 // Let it propagate if not properly truncated.
1834 time.Sleep(10 * time.Millisecond)
1835 if n, _, _ := sub.Pending(); n != 0 {
1836 t.Fatalf("Should not have received the message on bar")
1837 }
1838
1839 // Try one with all the bells and whistles.
1840 routeSend("LMSG ln1 $G foo + reply bar baz 0 2\r\nok\r\n")
1841 pingPong()
1842
1843 // Let it propagate if not properly truncated.
1844 time.Sleep(10 * time.Millisecond)
1845 if n, _, _ := sub.Pending(); n != 0 {
1846 t.Fatalf("Should not have received the message on bar")
1847 }
1848
1849 // Now unsubscribe, we should receive an LS- without origin.
1850 sub.Unsubscribe()
1851 routeExpect(lunsubRe)
1852
1853 // Quick check for queues
1854 sub, _ = nc.QueueSubscribeSync("baz", "bat")
1855 // Let it propagate to the main server
1856 checkFor(t, time.Second, 10*time.Millisecond, func() error {
1857 if !gacc.SubscriptionInterest("baz") {
1858 return fmt.Errorf("No interest")
1859 }
1860 return nil
1861 })
1862 // For "baz"
1863 routeExpect(rlsubRe)
1864 sub.Unsubscribe()
1865 routeExpect(lunsubRe)
1866
1867 // Restart our routed server, but this time indicate support
1868 // for LS- with origin cluster.
1869 rc.Close()
1870 rc = createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
1871 defer rc.Close()
1872
1873 routeSend, routeExpect = setupRouteEx(t, rc, opts, routeID)
1874
1875 info = checkInfoMsg(t, rc)
1876 info.ID = routeID
1877 info.Name = ""
1878 // These should be already set to true since the server that sends the
1879 // INFO has them enabled, but just be explicit.
1880 info.LNOC = true
1881 info.LNOCU = true
1882 b, err = json.Marshal(info)
1883 if err != nil {
1884 t.Fatalf("Could not marshal test route info: %v", err)
1885 }
1886
1887 routeSend(fmt.Sprintf("INFO %s\r\n", b))
1888 routeExpect(rlsubRe)
1889 pingPong()
1890
1891 // Check the LS+/LS-
1892 sendLSProtosFromRoute(true)
1893
1894 sub, _ = nc.SubscribeSync("bar")
1895 routeExpect(rlsubRe)
1896 sub.Unsubscribe()
1897 routeExpect(rlunsubRe)
1898
1899 sub, _ = nc.QueueSubscribeSync("baz", "bat")
1900 routeExpect(rlsubRe)
1901 sub.Unsubscribe()
1902 routeExpect(rlunsubRe)
1903 }
1904
1905 // Check that real duplicate subscription (that is, sent by client with same sid)
1906 // are ignored and do not register multiple shadow subscriptions.
1907 func TestNewRouteDuplicateSubscription(t *testing.T) {
1908 // This is same test than TestNewRouteStreamImport but calling "SUB foo 1" twice.
1909 testNewRouteStreamImport(t, true)
1910
1911 opts := LoadConfig("./configs/new_cluster.conf")
1912 opts.DisableShortFirstPing = true
1913 s := RunServer(opts)
1914 defer s.Shutdown()
1915
1916 rc := createRouteConn(t, opts.Cluster.Host, opts.Cluster.Port)
1917 defer rc.Close()
1918
1919 routeID := "RTEST_DUPLICATE:22"
1920 routeSend, routeExpect := setupRouteEx(t, rc, opts, routeID)
1921 sendRouteInfo(t, rc, routeSend, routeID)
1922 routeSend("PING\r\n")
1923 routeExpect(pongRe)
1924
1925 c := createClientConn(t, opts.Host, opts.Port)
1926 defer c.Close()
1927 send, expect := setupConn(t, c)
1928
1929 // Create a real duplicate subscriptions (same sid)
1930 send("SUB foo 1\r\nSUB foo 1\r\nPING\r\n")
1931 expect(pongRe)
1932
1933 // Route should receive single RS+
1934 routeExpect(rsubRe)
1935
1936 // Unsubscribe.
1937 send("UNSUB 1\r\nPING\r\n")
1938 expect(pongRe)
1939
1940 // Route should receive RS-.
1941 // With defect, only 1 subscription would be found during the unsubscribe,
1942 // however route map would have been updated twice when processing the
1943 // duplicate SUB, which means that the RS- would not be received because
1944 // the count would still be 1.
1945 routeExpect(runsubRe)
1946 }