MCPcopy
hub / github.com/nats-io/nats.go / TestMaxConnectionsReconnect

Function TestMaxConnectionsReconnect

test/nats_test.go:35–112  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

33)
34
35func TestMaxConnectionsReconnect(t *testing.T) {
36 // Start first server
37 s1Opts := natsserver.DefaultTestOptions
38 s1Opts.Port = -1
39 s1Opts.MaxConn = 2
40 s1Opts.Cluster = server.ClusterOpts{Name: "test", Host: "127.0.0.1", Port: -1}
41 s1 := RunServerWithOptions(&s1Opts)
42 defer s1.Shutdown()
43
44 // Start second server
45 s2Opts := natsserver.DefaultTestOptions
46 s2Opts.Port = -1
47 s2Opts.MaxConn = 2
48 s2Opts.Cluster = server.ClusterOpts{Name: "test", Host: "127.0.0.1", Port: -1}
49 s2Opts.Routes = server.RoutesFromStr(fmt.Sprintf("nats://127.0.0.1:%d", s1Opts.Cluster.Port))
50 s2 := RunServerWithOptions(&s2Opts)
51 defer s2.Shutdown()
52
53 errCh := make(chan error, 2)
54 reconnectCh := make(chan struct{})
55 opts := []nats.Option{
56 nats.MaxReconnects(2),
57 nats.ReconnectWait(10 * time.Millisecond),
58 nats.Timeout(200 * time.Millisecond),
59 nats.DisconnectErrHandler(func(_ *nats.Conn, err error) {
60 if err != nil {
61 errCh <- err
62 }
63 }),
64 nats.ReconnectHandler(func(_ *nats.Conn) {
65 reconnectCh <- struct{}{}
66 }),
67 }
68
69 // Create two connections (the current max) to first server
70 nc1, _ := nats.Connect(s1.ClientURL(), opts...)
71 defer nc1.Close()
72 nc1.Flush()
73
74 nc2, _ := nats.Connect(s1.ClientURL(), opts...)
75 defer nc2.Close()
76 nc2.Flush()
77
78 if s1.NumClients() != 2 {
79 t.Fatalf("Expected 2 client connections to first server. Got %d", s1.NumClients())
80 }
81
82 if s2.NumClients() > 0 {
83 t.Fatalf("Expected 0 client connections to second server. Got %d", s2.NumClients())
84 }
85
86 // Kick one of our two server connections off first server. One client should reconnect to second server
87 newS1Opts := s1Opts
88 newS1Opts.MaxConn = 1
89 err := s1.ReloadOptions(&newS1Opts)
90 if err != nil {
91 t.Fatalf("Unexpected error changing max_connections [%s]", err)
92 }

Callers

nothing calls this directly

Calls 8

DisconnectErrHandlerMethod · 0.80
ReconnectHandlerMethod · 0.80
ConnectMethod · 0.80
FatalfMethod · 0.80
RunServerWithOptionsFunction · 0.70
TimeoutMethod · 0.65
CloseMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected