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