14
14
package test
15
15
16
16
import (
17
+ "bytes"
17
18
"context"
18
19
"encoding/json"
19
20
"fmt"
@@ -28,6 +29,14 @@ import (
28
29
"github.com/nats-io/nuid"
29
30
)
30
31
32
+ func routeSendInfo (b []byte , routeSend sendFun , routeExpect expectFun ) {
33
+ routeSend (fmt .Sprintf ("INFO %s\r \n " , b ))
34
+ // When a server receives an INFO from a route, it will send a PING
35
+ // to compute the RTT.
36
+ routeExpect (pingRe )
37
+ routeSend ("PONG\r \n " )
38
+ }
39
+
31
40
func runNewRouteServer (t * testing.T ) (* server.Server , * server.Options ) {
32
41
return RunServerWithConfig ("./configs/new_cluster.conf" )
33
42
}
@@ -55,6 +64,18 @@ func TestNewRouteInfoOnConnect(t *testing.T) {
55
64
}
56
65
}
57
66
67
+ func getRSubBuffer (t * testing.T , routeExpect expectFun ) (bool , []byte ) {
68
+ t .Helper ()
69
+ buf := routeExpect (rsubRe )
70
+ // See if PING has been read along with the RS+'s
71
+ gotPing := false
72
+ if idx := bytes .Index (buf , []byte ("PING\r \n " )); idx != - 1 {
73
+ buf = buf [:idx ]
74
+ gotPing = true
75
+ }
76
+ return gotPing , buf
77
+ }
78
+
58
79
func TestNewRouteConnectSubs (t * testing.T ) {
59
80
s , opts := runNewRouteServer (t )
60
81
defer s .Shutdown ()
@@ -89,7 +110,7 @@ func TestNewRouteConnectSubs(t *testing.T) {
89
110
}
90
111
routeSend (fmt .Sprintf ("INFO %s\r \n " , b ))
91
112
92
- buf := routeExpect ( rsubRe )
113
+ gotPing , buf := getRSubBuffer ( t , routeExpect )
93
114
94
115
matches := rsubRe .FindAllSubmatch (buf , - 1 )
95
116
if len (matches ) != 2 {
@@ -116,6 +137,11 @@ func TestNewRouteConnectSubs(t *testing.T) {
116
137
}
117
138
}
118
139
140
+ // Wait for PING if we did not get it yet.
141
+ if ! gotPing {
142
+ routeExpect (pingRe )
143
+ }
144
+
119
145
// Close the client connection, check the results.
120
146
c .Close ()
121
147
@@ -163,7 +189,7 @@ func TestNewRouteConnectSubsWithAccount(t *testing.T) {
163
189
}
164
190
routeSend (fmt .Sprintf ("INFO %s\r \n " , b ))
165
191
166
- buf := routeExpect ( rsubRe )
192
+ gotPing , buf := getRSubBuffer ( t , routeExpect )
167
193
168
194
matches := rsubRe .FindAllSubmatch (buf , - 1 )
169
195
if len (matches ) != 2 {
@@ -190,6 +216,11 @@ func TestNewRouteConnectSubsWithAccount(t *testing.T) {
190
216
}
191
217
}
192
218
219
+ // Wait for PING if we did not get it yet.
220
+ if ! gotPing {
221
+ routeExpect (pingRe )
222
+ }
223
+
193
224
// Close the client connection, check the results.
194
225
c .Close ()
195
226
@@ -240,7 +271,8 @@ func TestNewRouteRSubs(t *testing.T) {
240
271
if err != nil {
241
272
t .Fatalf ("Could not marshal test route info: %v" , err )
242
273
}
243
- routeSend (fmt .Sprintf ("INFO %s\r \n PING\r \n " , b ))
274
+ routeSendInfo (b , routeSend , routeExpect )
275
+ routeSend ("PING\r \n " )
244
276
routeExpect (pongRe )
245
277
246
278
// Have the client listen on foo.
@@ -317,7 +349,7 @@ func TestNewRouteProgressiveNormalSubs(t *testing.T) {
317
349
if err != nil {
318
350
t .Fatalf ("Could not marshal test route info: %v" , err )
319
351
}
320
- routeSend ( fmt . Sprintf ( "INFO %s \r \n " , b ) )
352
+ routeSendInfo ( b , routeSend , routeExpect )
321
353
routeSend ("PING\r \n " )
322
354
routeExpect (pongRe )
323
355
@@ -414,8 +446,7 @@ func TestNewRouteClientClosedWithNormalSubscriptions(t *testing.T) {
414
446
if err != nil {
415
447
t .Fatalf ("Could not marshal test route info: %v" , err )
416
448
}
417
- routeSend (fmt .Sprintf ("INFO %s\r \n " , b ))
418
-
449
+ routeSendInfo (b , routeSend , routeExpect )
419
450
routeSend ("PING\r \n " )
420
451
routeExpect (pongRe )
421
452
@@ -464,8 +495,7 @@ func TestNewRouteClientClosedWithQueueSubscriptions(t *testing.T) {
464
495
if err != nil {
465
496
t .Fatalf ("Could not marshal test route info: %v" , err )
466
497
}
467
- routeSend (fmt .Sprintf ("INFO %s\r \n " , b ))
468
-
498
+ routeSendInfo (b , routeSend , routeExpect )
469
499
routeSend ("PING\r \n " )
470
500
routeExpect (pongRe )
471
501
@@ -512,7 +542,7 @@ func TestNewRouteRUnsubAccountSpecific(t *testing.T) {
512
542
if err != nil {
513
543
t .Fatalf ("Could not marshal test route info: %v" , err )
514
544
}
515
- routeSend ( fmt . Sprintf ( "INFO %s \r \n " , b ) )
545
+ routeSendInfo ( b , routeSend , routeExpect )
516
546
517
547
// Now create 500 subs on same subject but all different accounts.
518
548
for i := 0 ; i < 500 ; i ++ {
@@ -568,7 +598,7 @@ func TestNewRouteRSubCleanupOnDisconnect(t *testing.T) {
568
598
if err != nil {
569
599
t .Fatalf ("Could not marshal test route info: %v" , err )
570
600
}
571
- routeSend ( fmt . Sprintf ( "INFO %s \r \n " , b ) )
601
+ routeSendInfo ( b , routeSend , routeExpect )
572
602
573
603
// Now create 100 subs on 3 different accounts.
574
604
for i := 0 ; i < 100 ; i ++ {
@@ -606,7 +636,8 @@ func TestNewRouteSendSubsAndMsgs(t *testing.T) {
606
636
if err != nil {
607
637
t .Fatalf ("Could not marshal test route info: %v" , err )
608
638
}
609
- routeSend (fmt .Sprintf ("INFO %s\r \n PING\r \n " , b ))
639
+ routeSendInfo (b , routeSend , routeExpect )
640
+ routeSend ("PING\r \n " )
610
641
routeExpect (pongRe )
611
642
612
643
// Now let's send in interest from the new protocol.
@@ -733,7 +764,8 @@ func TestNewRouteProcessRoutedMsgs(t *testing.T) {
733
764
if err != nil {
734
765
t .Fatalf ("Could not marshal test route info: %v" , err )
735
766
}
736
- routeSend (fmt .Sprintf ("INFO %s\r \n PING\r \n " , b ))
767
+ routeSendInfo (b , routeSend , routeExpect )
768
+ routeSend ("PING\r \n " )
737
769
routeExpect (pongRe )
738
770
739
771
// Create a client
0 commit comments