// Copyright 2012-2026 The NATS Authors // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package test import ( "errors" "fmt" "math" "net" "runtime" "strings" "sync" "testing" "time" "github.com/nats-io/nats.go" ) // tsTestServers returns a slice of fabricated URLs that point at non-existent // NATS servers. Tests that rely on a "list with a single live server and many // dead ones" pattern build their server list by appending the live URL to // (a subset of) these. func tsTestServers() []string { return []string{ "nats://127.0.0.1:11222", "nats://127.0.0.1:11223", "nats://127.0.0.1:11224", "nats://127.0.0.1:11225", "nats://127.0.0.1:11226", "nats://127.0.0.1:11227", "nats://127.0.0.1:11228", } } func TestServersOption(t *testing.T) { c := newTester(t) opts := nats.GetDefaultOptions() opts.NoRandomize = true // Need to lower this for Windows tests, otherwise would take too long. opts.Timeout = 100 * time.Millisecond // When getting "connection refused", we transform to ErrNoServers. // However, on Windows, the connect() will get a i/o timeout, but // we can't really suppress that one since we don't know if it is // a real timeout or a failure to connect. So check differencly. // Not nats.DefaultURL: 4222 is the tester's management endpoint here, so // use a known-dead URL instead. opts.Servers = []string{"nats://127.0.0.1:11221"} _, err := opts.Connect() if runtime.GOOS == "windows" { if err == nil || !strings.Contains(err.Error(), "timeout") { t.Fatalf("Expected timeout, got %v", err) } } else if err != nats.ErrNoServers { t.Fatalf("Wrong error: '%v'", err) } opts.Servers = tsTestServers() _, err = opts.Connect() if runtime.GOOS == "windows" { if err == nil || !strings.Contains(err.Error(), "timeout") { t.Fatalf("Expected timeout, got %v", err) } } else if err == nil || err != nats.ErrNoServers { t.Fatalf("Did not receive proper error: %v", err) } // Make sure we can connect to first server if running s1Inst := c.CreateServer(t, false) s1URL := s1Inst.Servers[0].URL opts.Servers = append([]string{s1URL}, tsTestServers()...) nc, err := opts.Connect() if err != nil { t.Fatalf("Could not connect: %v\n", err) } if nc.ConnectedUrl() != s1URL { nc.Close() t.Fatalf("Does not report correct connection: %s\n", nc.ConnectedUrl()) } nc.Close() s1Inst.Destroy(t) // Make sure we can connect to a non first server if running s2Inst := c.CreateServer(t, false) t.Cleanup(func() { s2Inst.Destroy(t) }) s2URL := s2Inst.Servers[0].URL opts.Servers = append(tsTestServers(), s2URL) nc, err = opts.Connect() if err != nil { t.Fatalf("Could not connect: %v\n", err) } defer nc.Close() if nc.ConnectedUrl() != s2URL { t.Fatalf("Does not report correct connection: %s\n", nc.ConnectedUrl()) } } func TestNewStyleServersOption(t *testing.T) { c := newTester(t) // Not nats.DefaultURL: 4222 is the tester's management endpoint here, so // use a known-dead URL instead. _, err := nats.Connect("nats://127.0.0.1:11221", nats.DontRandomize(), nats.Timeout(100*time.Millisecond)) if runtime.GOOS == "windows" { if err == nil || !strings.Contains(err.Error(), "timeout") { t.Fatalf("Expected timeout, got %v", err) } } else if err != nats.ErrNoServers { t.Fatalf("Wrong error: '%v'\n", err) } deadServers := strings.Join(tsTestServers(), ",") _, err = nats.Connect(deadServers, nats.DontRandomize(), nats.Timeout(100*time.Millisecond)) if runtime.GOOS == "windows" { if err == nil || !strings.Contains(err.Error(), "timeout") { t.Fatalf("Expected timeout, got %v", err) } } else if err == nil || err != nats.ErrNoServers { t.Fatalf("Did not receive proper error: %v\n", err) } // Make sure we can connect to first server if running s1Inst := c.CreateServer(t, false) s1URL := s1Inst.Servers[0].URL srvsWithS1 := strings.Join(append([]string{s1URL}, tsTestServers()...), ",") nc, err := nats.Connect(srvsWithS1, nats.DontRandomize(), nats.Timeout(100*time.Millisecond)) if err != nil { t.Fatalf("Could not connect: %v\n", err) } if nc.ConnectedUrl() != s1URL { nc.Close() t.Fatalf("Does not report correct connection: %s\n", nc.ConnectedUrl()) } nc.Close() s1Inst.Destroy(t) // Make sure we can connect to a non-first server if running s2Inst := c.CreateServer(t, false) t.Cleanup(func() { s2Inst.Destroy(t) }) s2URL := s2Inst.Servers[0].URL srvsWithS2 := strings.Join(append(tsTestServers(), s2URL), ",") nc, err = nats.Connect(srvsWithS2, nats.DontRandomize(), nats.Timeout(100*time.Millisecond)) if err != nil { t.Fatalf("Could not connect: %v\n", err) } defer nc.Close() if nc.ConnectedUrl() != s2URL { t.Fatalf("Does not report correct connection: %s\n", nc.ConnectedUrl()) } } func TestAuthServers(t *testing.T) { c := newTester(t) authBody := `authorization { user: derek password: foo timeout: 1 }` authOpts := singleUserPassOpts(authBody) as1Inst := c.CreateServer(t, false, authOpts...) t.Cleanup(func() { as1Inst.Destroy(t) }) as2Inst := c.CreateServer(t, false, authOpts...) t.Cleanup(func() { as2Inst.Destroy(t) }) as1URL := as1Inst.Servers[0].URL as2URL := as2Inst.Servers[0].URL plainServers := []string{as1URL, as2URL} pservers := strings.Join(plainServers, ",") nc, err := nats.Connect(pservers, nats.DontRandomize(), nats.Timeout(5*time.Second)) if err == nil { nc.Close() t.Fatalf("Expect Auth failure, got no error\n") } if !strings.Contains(err.Error(), "Authorization") { t.Fatalf("Wrong error, wanted Auth failure, got '%s'\n", err) } if !errors.Is(err, nats.ErrAuthorization) { t.Fatalf("Expected error '%v', got '%v'", nats.ErrAuthorization, err) } // Test that we can connect to a subsequent correct server. // Embed creds in the second URL. as2HostPort := strings.TrimPrefix(as2URL, "nats://") authServers := []string{ as1URL, fmt.Sprintf("nats://derek:foo@%s", as2HostPort), } aservers := strings.Join(authServers, ",") nc, err = nats.Connect(aservers, nats.DontRandomize(), nats.Timeout(5*time.Second)) if err != nil { t.Fatalf("Expected to connect properly: %v\n", err) } defer nc.Close() if nc.ConnectedUrl() != authServers[1] { t.Fatalf("Does not report correct connection: %s\n", nc.ConnectedUrl()) } } func TestBasicClusterReconnect(t *testing.T) { c := newTester(t) s1Inst := c.CreateServer(t, false) t.Cleanup(func() { s1Inst.Destroy(t) }) s2Inst := c.CreateServer(t, false) t.Cleanup(func() { s2Inst.Destroy(t) }) s1URL := s1Inst.Servers[0].URL s2URL := s2Inst.Servers[0].URL servers := strings.Join([]string{s1URL, s2URL}, ",") dch := make(chan bool) rch := make(chan bool) dcbCalled := false opts := []nats.Option{nats.DontRandomize(), nats.Timeout(100 * time.Millisecond), nats.DisconnectErrHandler(func(nc *nats.Conn, _ error) { // Suppress any additional callbacks if dcbCalled { return } dcbCalled = true dch <- true }), nats.ReconnectHandler(func(_ *nats.Conn) { rch <- true }), } nc, err := nats.Connect(servers, opts...) if err != nil { t.Fatalf("Expected to connect, got err: %v\n", err) } defer nc.Close() s1Inst.StopServer(t, s1Inst.Servers[0]) // wait for disconnect if e := WaitTime(dch, 2*time.Second); e != nil { t.Fatal("Did not receive a disconnect callback message") } reconnectTimeStart := time.Now() // wait for reconnect if e := WaitTime(rch, 2*time.Second); e != nil { t.Fatal("Did not receive a reconnect callback message") } if nc.ConnectedUrl() != s2URL { t.Fatalf("Does not report correct connection: %s\n", nc.ConnectedUrl()) } // Make sure we did not wait on reconnect for default time. // Reconnect should be fast since it will be a switch to the // second server and not be dependent on server restart time. // On Windows, a failed connect takes more than a second, so // account for that. maxDuration := 100 * time.Millisecond if runtime.GOOS == "windows" { maxDuration = 1100 * time.Millisecond } reconnectTime := time.Since(reconnectTimeStart) if reconnectTime > maxDuration { t.Fatalf("Took longer than expected to reconnect: %v\n", reconnectTime) } } func TestHotSpotReconnect(t *testing.T) { c := newTester(t) s1Inst := c.CreateServer(t, false) t.Cleanup(func() { s1Inst.Destroy(t) }) s1URL := s1Inst.Servers[0].URL numClients := 32 clients := []*nats.Conn{} wg := &sync.WaitGroup{} wg.Add(numClients) opts := []nats.Option{ nats.ReconnectWait(50 * time.Millisecond), nats.ReconnectJitter(0, 0), nats.ReconnectHandler(func(_ *nats.Conn) { wg.Done() }), } // Pre-create s2 and s3 but stop them immediately, so every client lands // on s1 before they are brought up. s2Inst := c.CreateServer(t, false) t.Cleanup(func() { s2Inst.Destroy(t) }) s3Inst := c.CreateServer(t, false) t.Cleanup(func() { s3Inst.Destroy(t) }) s2URL := s2Inst.Servers[0].URL s3URL := s3Inst.Servers[0].URL s2Inst.StopServer(t, s2Inst.Servers[0]) s3Inst.StopServer(t, s3Inst.Servers[0]) var srvrs string allServers := []string{s1URL, s2URL, s3URL} if runtime.GOOS == "windows" { // On Windows, keep the list short to avoid long connect timeouts on // dead URLs. srvrs = strings.Join(allServers, ",") opts = append(opts, nats.Timeout(100*time.Millisecond)) } else { srvrs = strings.Join(allServers, ",") } for range numClients { nc, err := nats.Connect(srvrs, opts...) if err != nil { t.Fatalf("Expected to connect, got err: %v\n", err) } defer nc.Close() if nc.ConnectedUrl() != s1URL { t.Fatalf("Connected to incorrect server: %v\n", nc.ConnectedUrl()) } clients = append(clients, nc) } // Now bring up s2 and s3 so they can absorb clients when s1 shuts down. s2Inst.StartServer(t, s2Inst.Servers[0]) s3Inst.StartServer(t, s3Inst.Servers[0]) s1Inst.StopServer(t, s1Inst.Servers[0]) numServers := 2 // Wait on all reconnects wg.Wait() // Walk the clients and calculate how many of each.. cs := make(map[string]int) for _, nc := range clients { cs[nc.ConnectedUrl()]++ nc.Close() } if len(cs) != numServers { t.Fatalf("Wrong number of reported servers: %d vs %d\n", len(cs), numServers) } expected := numClients / numServers v := uint(float32(expected) * 0.50) // Check that each item is within acceptable range for s, total := range cs { delta := uint(math.Abs(float64(expected - total))) if delta > v { t.Fatalf("Connected clients to server: %s out of range: %d\n", s, total) } } } func TestProperReconnectDelay(t *testing.T) { c := newTester(t) s1Inst := c.CreateServer(t, false) t.Cleanup(func() { s1Inst.Destroy(t) }) s1URL := s1Inst.Servers[0].URL var srvs string opts := nats.GetDefaultOptions() if runtime.GOOS == "windows" { srvs = strings.Join(append([]string{s1URL}, tsTestServers()[:1]...), ",") } else { srvs = strings.Join(append([]string{s1URL}, tsTestServers()...), ",") } opts.NoRandomize = true dcbCalled := false closedCbCalled := false dch := make(chan bool) dcb := func(nc *nats.Conn) { // Suppress any additional calls if dcbCalled { return } dcbCalled = true dch <- true } ccb := func(_ *nats.Conn) { closedCbCalled = true } nc, err := nats.Connect(srvs, nats.DontRandomize(), nats.DisconnectHandler(dcb), nats.ClosedHandler(ccb)) if err != nil { t.Fatalf("Expected to connect, got err: %v\n", err) } defer nc.Close() s1Inst.StopServer(t, s1Inst.Servers[0]) // wait for disconnect if e := WaitTime(dch, 2*time.Second); e != nil { t.Fatal("Did not receive a disconnect callback message") } // Wait, want to make sure we don't spin on reconnect to non-existent servers. time.Sleep(1 * time.Second) // Make sure we are still reconnecting.. if closedCbCalled { t.Fatal("Closed CB was triggered, should not have been.") } if status := nc.Status(); status != nats.RECONNECTING { t.Fatalf("Wrong status: %d\n", status) } } func TestProperFalloutAfterMaxAttempts(t *testing.T) { c := newTester(t) s1Inst := c.CreateServer(t, false) t.Cleanup(func() { s1Inst.Destroy(t) }) s1URL := s1Inst.Servers[0].URL opts := nats.GetDefaultOptions() // Reduce the list of servers for Windows tests if runtime.GOOS == "windows" { opts.Servers = append([]string{s1URL}, tsTestServers()[:1]...) opts.MaxReconnect = 2 opts.Timeout = 100 * time.Millisecond } else { opts.Servers = append([]string{s1URL}, tsTestServers()...) opts.MaxReconnect = 5 } opts.NoRandomize = true opts.ReconnectWait = (25 * time.Millisecond) nats.ReconnectJitter(0, 0)(&opts) // Buffered so the testservice-managed server (which may briefly re-bind // the port between reconnect cycles) cannot stall the async callback // dispatcher by blocking on a second disconnect event. dch := make(chan bool, 16) opts.DisconnectedErrCB = func(_ *nats.Conn, _ error) { select { case dch <- true: default: } } closedCbCalled := false cch := make(chan bool, 1) opts.ClosedCB = func(_ *nats.Conn) { closedCbCalled = true cch <- true } nc, err := opts.Connect() if err != nil { t.Fatalf("Expected to connect, got err: %v\n", err) } defer nc.Close() s1Inst.StopServer(t, s1Inst.Servers[0]) // On Windows, creating a TCP connection to a server not running takes more than // a second. So be generous with the WaitTime. // wait for disconnect if e := WaitTime(dch, 5*time.Second); e != nil { t.Fatal("Did not receive a disconnect callback message") } // Wait for ClosedCB if e := WaitTime(cch, 5*time.Second); e != nil { t.Fatal("Did not receive a closed callback message") } // Make sure we are not still reconnecting.. if !closedCbCalled { t.Logf("%+v\n", nc) t.Fatal("Closed CB was not triggered, should have been.") } // Expect connection to be closed... if !nc.IsClosed() { t.Fatalf("Wrong status: %d\n", nc.Status()) } } func TestProperFalloutAfterMaxAttemptsWithAuthMismatch(t *testing.T) { c := newTester(t) s1Inst := c.CreateServer(t, false) t.Cleanup(func() { s1Inst.Destroy(t) }) s1URL := s1Inst.Servers[0].URL // Second server requires TLS with client cert verification; an // untrusted plain-text client connection will fail auth handshake. s2Inst := c.CreateServer(t, false, managedTLSOpts(t)) t.Cleanup(func() { s2Inst.Destroy(t) }) s2URL := s2Inst.Servers[0].URL myServers := []string{s1URL, s2URL} opts := nats.GetDefaultOptions() opts.Servers = myServers opts.NoRandomize = true if runtime.GOOS == "windows" { opts.MaxReconnect = 2 opts.Timeout = 100 * time.Millisecond } else { opts.MaxReconnect = 5 } opts.ReconnectWait = (25 * time.Millisecond) nats.ReconnectJitter(0, 0)(&opts) // Buffered so the testservice-managed server (which may briefly re-bind // the port between reconnect cycles) cannot stall the async callback // dispatcher by blocking on a second disconnect event. dch := make(chan bool, 16) opts.DisconnectedErrCB = func(_ *nats.Conn, _ error) { select { case dch <- true: default: } } closedCbCalled := false cch := make(chan bool, 1) opts.ClosedCB = func(_ *nats.Conn) { closedCbCalled = true cch <- true } nc, err := opts.Connect() if err != nil { t.Fatalf("Expected to connect, got err: %v\n", err) } defer nc.Close() s1Inst.StopServer(t, s1Inst.Servers[0]) // On Windows, creating a TCP connection to a server not running takes more than // a second. So be generous with the WaitTime. // wait for disconnect if e := WaitTime(dch, 5*time.Second); e != nil { t.Fatal("Did not receive a disconnect callback message") } // Wait for ClosedCB if e := WaitTime(cch, 5*time.Second); e != nil { reconnects := nc.Stats().Reconnects t.Fatalf("Did not receive a closed callback message, #reconnects: %v", reconnects) } // Make sure we have not exceeded the per-server MaxReconnect budget. // nc.Stats().Reconnects is a global counter that increments on every // successful TCP-level reconnect across the whole pool; MaxReconnect caps // attempts PER SERVER. With a 2-server pool where both briefly accept TCP // (StopServer has a short listener-drain window), the global count can // legitimately reach len(servers)*MaxReconnect. The client is not // exceeding its per-server budget; assert the true upper bound. reconnects := nc.Stats().Reconnects if reconnects < uint64(opts.MaxReconnect) || reconnects > uint64(len(myServers)*opts.MaxReconnect) { t.Fatalf("Num reconnects was %v, expected within [%v, %v]", reconnects, opts.MaxReconnect, len(myServers)*opts.MaxReconnect) } // Make sure we are not still reconnecting.. if !closedCbCalled { t.Logf("%+v\n", nc) t.Fatal("Closed CB was not triggered, should have been.") } // Expect connection to be closed... if !nc.IsClosed() { t.Fatalf("Wrong status: %d\n", nc.Status()) } } func TestTimeoutOnNoServers(t *testing.T) { c := newTester(t) s1Inst := c.CreateServer(t, false) t.Cleanup(func() { s1Inst.Destroy(t) }) s1URL := s1Inst.Servers[0].URL opts := nats.GetDefaultOptions() if runtime.GOOS == "windows" { opts.Servers = append([]string{s1URL}, tsTestServers()[:1]...) opts.MaxReconnect = 2 opts.Timeout = 100 * time.Millisecond opts.ReconnectWait = (100 * time.Millisecond) nats.ReconnectJitter(0, 0)(&opts) } else { opts.Servers = append([]string{s1URL}, tsTestServers()...) // 1 second total time wait opts.MaxReconnect = 10 opts.ReconnectWait = (100 * time.Millisecond) nats.ReconnectJitter(0, 0)(&opts) } opts.NoRandomize = true dch := make(chan bool) opts.DisconnectedErrCB = func(nc *nats.Conn, _ error) { // Suppress any additional calls nc.SetDisconnectErrHandler(nil) dch <- true } cch := make(chan bool) opts.ClosedCB = func(_ *nats.Conn) { cch <- true } nc, err := opts.Connect() if err != nil { t.Fatalf("Expected to connect, got err: %v\n", err) } defer nc.Close() s1Inst.StopServer(t, s1Inst.Servers[0]) // On Windows, creating a connection to a non-running server takes // more than a second. So be generous with WaitTime // wait for disconnect if e := WaitTime(dch, 5*time.Second); e != nil { t.Fatal("Did not receive a disconnect callback message") } startWait := time.Now() // Wait for ClosedCB if e := WaitTime(cch, 5*time.Second); e != nil { t.Fatal("Did not receive a closed callback message") } if runtime.GOOS != "windows" { timeWait := time.Since(startWait) // Use 500ms as variable time delta variable := (500 * time.Millisecond) expected := (time.Duration(opts.MaxReconnect) * opts.ReconnectWait) if timeWait > (expected + variable) { t.Fatalf("Waited too long for Closed state: %d\n", timeWait/time.Millisecond) } } } func TestPingReconnect(t *testing.T) { RECONNECTS := 4 c := newTester(t) s1Inst := c.CreateServer(t, false) t.Cleanup(func() { s1Inst.Destroy(t) }) s1URL := s1Inst.Servers[0].URL opts := nats.GetDefaultOptions() opts.Servers = append([]string{s1URL}, tsTestServers()...) opts.NoRandomize = true opts.Timeout = 100 * time.Millisecond opts.ReconnectWait = 200 * time.Millisecond nats.ReconnectJitter(0, 0)(&opts) opts.PingInterval = 50 * time.Millisecond opts.MaxPingsOut = -1 var wg sync.WaitGroup wg.Add(1) rch := make(chan time.Time, RECONNECTS) dch := make(chan time.Time, RECONNECTS) opts.DisconnectedErrCB = func(_ *nats.Conn, _ error) { d := dch select { case d <- time.Now(): default: d = nil } } opts.ReconnectedCB = func(c *nats.Conn) { r := rch select { case r <- time.Now(): default: r = nil wg.Done() } } nc, err := opts.Connect() if err != nil { t.Fatalf("Expected to connect, got err: %v\n", err) } defer nc.Close() wg.Wait() s1Inst.StopServer(t, s1Inst.Servers[0]) <-dch for range RECONNECTS - 1 { disconnectedAt := <-dch reconnectAt := <-rch pingCycle := disconnectedAt.Sub(reconnectAt) if pingCycle > 2*opts.PingInterval { t.Fatalf("Reconnect due to ping took %s", pingCycle.String()) } } } type checkPoolUpdatedDialer struct { conn net.Conn first, final bool ra int } func (d *checkPoolUpdatedDialer) Dial(network, address string) (net.Conn, error) { doReal := false if d.first { d.first = false doReal = true } else if d.final { d.ra++ return nil, errors.New("On purpose") } else { d.ra++ if d.ra == 15 { d.ra = 0 doReal = true } } if doReal { c, err := net.Dial(network, address) if err != nil { return nil, err } d.conn = c return c, nil } return nil, errors.New("On purpose") } func TestServerPoolUpdatedWhenRouteGoesAway(t *testing.T) { c := newTester(t) // Build a 3-node cluster so client-server URL gossip is exercised. // The tester's NATS_ADVERTISE env var makes each node gossip a host:port // the client can dial (localhost in host mode, the service name in CI), // so the gossiped pool URLs match the inst.Servers[i].URL values below. clusterInst := c.CreateCluster(t, 3, false) t.Cleanup(func() { clusterInst.Destroy(t) }) s1 := clusterInst.Servers[0] s2 := clusterInst.Servers[1] s3 := clusterInst.Servers[2] s1Url := s1.URL s2Url := s2.URL s3Url := s3.URL // Stop s2 and s3 initially so the client sees them only once they are // brought up, after the initial connect to s1. clusterInst.StopServer(t, s2) clusterInst.StopServer(t, s3) ch := make(chan bool, 1) chch := make(chan bool, 1) connHandler := func(_ *nats.Conn) { chch <- true } // SkipHostLookup keeps srvPool entries 1:1 with the bootstrap URL and the // gossiped connect_urls. Without it, nats.go's parseServerURL does // net.LookupHost on any non-IP host (the bootstrap "nats://nats:port" and // every gossiped hostname URL) and adds every resolved IP as a separate // pool entry alongside the hostname — inflating the pool with bridge-IP // duplicates in CI's docker bridge mode and on dual-stack hosts that // expand "localhost" to both 127.0.0.1 and ::1. nc, err := nats.Connect(s1Url, nats.SkipHostLookup(), nats.ReconnectHandler(connHandler), nats.DiscoveredServersHandler(func(_ *nats.Conn) { ch <- true })) if err != nil { t.Fatalf("Error on connect") } clusterInst.StartServer(t, s2) // Wait to be notified if err := Wait(ch); err != nil { t.Fatal("New server callback was not invoked") } checkPool := func(expected []string) { // Don't use discovered here, but Servers to have the full list. // Also, there may be cases where the mesh is not formed yet, // so try again on failure. var ( ds []string timeout = time.Now().Add(5 * time.Second) ) for time.Now().Before(timeout) { ds = nc.Servers() if len(ds) == len(expected) { m := make(map[string]struct{}, len(ds)) for _, url := range ds { m[url] = struct{}{} } ok := true for _, url := range expected { if _, present := m[url]; !present { ok = false break } } if ok { return } } time.Sleep(50 * time.Millisecond) } stackFatalf(t, "Expected %v, got %v", expected, ds) } // Verify that we now know about s2 checkPool([]string{s1Url, s2Url}) clusterInst.StartServer(t, s3) // Wait to be notified if err := Wait(ch); err != nil { t.Fatal("New server callback was not invoked") } // Verify that we now know about s3 checkPool([]string{s1Url, s2Url, s3Url}) // Stop s1. Since this was passed to the Connect() call, this one should // still be present. clusterInst.StopServer(t, s1) // Wait for reconnect if err := Wait(chch); err != nil { t.Fatal("Reconnect handler not invoked") } checkPool([]string{s1Url, s2Url, s3Url}) // Check the server we reconnected to. reConnectedTo := nc.ConnectedUrl() expected := []string{s1Url} restartS2 := false if reConnectedTo == s2Url { restartS2 = true clusterInst.StopServer(t, s2) expected = append(expected, s3Url) } else if reConnectedTo == s3Url { clusterInst.StopServer(t, s3) expected = append(expected, s2Url) } else { t.Fatalf("Unexpected server client has reconnected to: %v", reConnectedTo) } // Wait for reconnect if err := Wait(chch); err != nil { t.Fatal("Reconnect handler not invoked") } // The implicit server that we just shutdown should have been removed from the pool checkPool(expected) // Restart the one that was shutdown and check that it is now back in the pool if restartS2 { clusterInst.StartServer(t, s2) expected = append(expected, s2Url) } else { clusterInst.StartServer(t, s3) expected = append(expected, s3Url) } // Since this is not a "new" server, the DiscoveredServersCB won't be invoked. checkPool(expected) nc.Close() // Restart s1 clusterInst.StartServer(t, s1) // We should have all 3 servers running now... // Create a client connection with special dialer. // // SkipHostLookup() keeps the srvPool entries 1:1 with the gossiped URLs. // Without it, hostnames like "localhost" are expanded by net.LookupHost // to both 127.0.0.1 and ::1 — every gossiped URL doubles, and the // reconnect-count assertion below loses its alignment with the pool size. d := &checkPoolUpdatedDialer{first: true} nc, err = nats.Connect(s1Url, nats.MaxReconnects(10), nats.ReconnectWait(15*time.Millisecond), nats.ReconnectJitter(0, 0), nats.SkipHostLookup(), nats.SetCustomDialer(d), nats.ReconnectHandler(connHandler), nats.ClosedHandler(connHandler)) if err != nil { t.Fatalf("Error on connect") } defer nc.Close() // Make sure that we have all 3 servers in the pool (this will wait if required) checkPool(expected) // Cause disconnection between client and server. We are going to reconnect // and we want to check that when we get the INFO again with the list of // servers, we don't lose the knowledge of how many times we tried to // reconnect. d.conn.Close() // Wait for client to reconnect to a server if err := Wait(chch); err != nil { t.Fatal("Reconnect handler not invoked") } // At this point, we should have tried to reconnect 5 times to each server. // For the one we reconnected to, its max reconnect attempts should have been // cleared, not for the other ones. // Cause a disconnect again and ensure we won't reconnect. d.final = true d.conn.Close() // Wait for Close callback to be invoked. if err := Wait(chch); err != nil { t.Fatal("Close handler not invoked") } // Since MaxReconnect is 10, after trying 5 more times on 2 of the servers, // these should have been removed. We have still 5 more tries for the server // we did previously reconnect to. // So total of reconnect attempt should be: 2*5+1*10=20 if d.ra != 20 { t.Fatalf("Should have tried to reconnect 20 more times, got %v", d.ra) } nc.Close() } func TestIgnoreDiscoveredServers(t *testing.T) { c := newTester(t) // CreateCluster brings both servers up connected via routes; stop the // second immediately so each subtest can start it after the client // connects. clusterInst := c.CreateCluster(t, 2, false) t.Cleanup(func() { clusterInst.Destroy(t) }) s1 := clusterInst.Servers[0] s2 := clusterInst.Servers[1] s1URL := s1.URL // Wait for the pool size on nc to reach expected, polling. checkPoolSize := func(nc *nats.Conn, expected int) { t.Helper() timeout := time.Now().Add(5 * time.Second) for time.Now().Before(timeout) { if len(nc.Servers()) == expected { return } time.Sleep(50 * time.Millisecond) } t.Fatalf("Expected %d server(s), got %d: %v", expected, len(nc.Servers()), nc.Servers()) } t.Run("add new servers to pool", func(t *testing.T) { // Ensure starting state: s2 is down so the client sees a 1-server pool. clusterInst.StopServer(t, s2) nc, err := nats.Connect(s1URL) if err != nil { t.Fatalf("Error on connect: %v", err) } defer nc.Close() clusterInst.StartServer(t, s2) checkPoolSize(nc, 2) if len(nc.DiscoveredServers()) != 1 { t.Fatalf("Expected 1 discovered server, got %v", nc.DiscoveredServers()) } }) t.Run("ignore new servers", func(t *testing.T) { // Reset to s2-down baseline before the subtest. clusterInst.StopServer(t, s2) time.Sleep(200 * time.Millisecond) nc, err := nats.Connect(s1URL, nats.IgnoreDiscoveredServers(), ) if err != nil { t.Fatalf("Error on connect: %v", err) } defer nc.Close() // Control connection to the same server, without the option under // test. Waiting for its pool to grow proves the gossip was actually // delivered; without that the negative assertion below races an // arbitrary sleep and passes vacuously on a slow box. control, err := nats.Connect(s1URL) if err != nil { t.Fatalf("Error on connect: %v", err) } defer control.Close() clusterInst.StartServer(t, s2) checkPoolSize(control, 2) // The server writes the async INFO before answering this connection's // PING, so once the flush returns nc has necessarily read it too. nc.Flush() if len(nc.Servers()) != 1 { t.Fatalf("Expected 1 server, got %d: %v", len(nc.Servers()), nc.Servers()) } if len(nc.DiscoveredServers()) != 0 { t.Fatalf("Expected no discovered servers, got %v", nc.DiscoveredServers()) } }) }