Skip to content

Commit

Permalink
Add test case for auto reconnect on max_connections exceeded from ser…
Browse files Browse the repository at this point in the history
…ver.
  • Loading branch information
tbeets committed Mar 25, 2022
1 parent d8b839d commit 17302cd
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions nats_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,69 @@ var testServers = []string{
"nats://localhost:1228",
}

func TestMaxConnectionsReconnect(t *testing.T) {
// Join cluster on first server
routes := []*url.URL{&url.URL{Scheme: "nats", Host: fmt.Sprintf("127.0.0.1:%d", TEST_PORT)}}

// Start first server
s1Opts := natsserver.DefaultTestOptions
s1Opts.Port = -1
s1Opts.MaxConn = 2
s1Opts.Cluster = server.ClusterOpts{Name: "test", Host: "127.0.0.1", Port: TEST_PORT}
s1Opts.Routes = routes
s1 := RunServerWithOptions(&s1Opts)
defer s1.Shutdown()

// Start second server
s2Opts := natsserver.DefaultTestOptions
s2Opts.Port = -1
s2Opts.MaxConn = 2
s2Opts.Cluster = server.ClusterOpts{Name: "test", Host: "127.0.0.1", Port: TEST_PORT + 1}
s2Opts.Routes = routes
s2 := RunServerWithOptions(&s2Opts)
defer s2.Shutdown()

// Only explicitly connect to first server
var opts = Options{
Url: s1.ClientURL(),
AllowReconnect: true,
MaxReconnect: 2,
ReconnectWait: 10 * time.Millisecond,
Timeout: 200 * time.Millisecond,
}

// Create two connections (the current max) for first server
nc1, _ := opts.Connect()
defer nc1.Close()
nc1.Flush()

nc2, _ := opts.Connect()
defer nc2.Close()
nc2.Flush()

if s1.NumClients() != 2 {
t.Fatalf("Expected 2 client connections to first server. Got %d\n", s1.NumClients())
}

if s2.NumClients() > 0 {
t.Fatalf("Expected 0 client connections to second server. Got %d\n", s2.NumClients())
}

// Kick one of our two server connections off first server. One client should reconnect to second server.
newS1Opts := s1Opts
newS1Opts.MaxConn = 1
err := s1.ReloadOptions(&newS1Opts)
if err != nil {
t.Fatalf("Unexpected error changing max_connections [%s]", err)
}

time.Sleep(200 * time.Millisecond)

if s2.NumClients() <= 0 || s1.NumClients() > 1 {
t.Fatalf("Expected client reconnection to second server.\n")
}
}

func TestSimplifiedURLs(t *testing.T) {
for _, test := range []struct {
name string
Expand Down

0 comments on commit 17302cd

Please sign in to comment.