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

Function TestAuthFailAllowReconnect

test/auth_test.go:99–162  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

97}
98
99func TestAuthFailAllowReconnect(t *testing.T) {
100 ts := RunServerOnPort(23232)
101 defer ts.Shutdown()
102
103 var servers = []string{
104 "nats://127.0.0.1:23232",
105 "nats://127.0.0.1:23233",
106 "nats://127.0.0.1:23234",
107 }
108
109 opts2 := test.DefaultTestOptions
110 opts2.Port = 23233
111 opts2.Username = "ivan"
112 opts2.Password = "foo"
113 ts2 := RunServerWithOptions(&opts2)
114 defer ts2.Shutdown()
115
116 ts3 := RunServerOnPort(23234)
117 defer ts3.Shutdown()
118
119 reconnectch := make(chan bool)
120 defer close(reconnectch)
121
122 copts := nats.GetDefaultOptions()
123 copts.Servers = servers
124 copts.AllowReconnect = true
125 copts.NoRandomize = true
126 copts.MaxReconnect = 10
127 copts.ReconnectWait = 100 * time.Millisecond
128 nats.ReconnectJitter(0, 0)(&copts)
129
130 copts.ReconnectedCB = func(_ *nats.Conn) {
131 reconnectch <- true
132 }
133
134 // Connect
135 nc, err := copts.Connect()
136 if err != nil {
137 t.Fatalf("Should have connected ok: %v", err)
138 }
139 defer nc.Close()
140
141 // Override default handler for test.
142 nc.SetErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, _ error) {})
143
144 // Stop the server
145 ts.Shutdown()
146
147 // The client will try to connect to the second server, and that
148 // should fail. It should then try to connect to the third and succeed.
149
150 // Wait for the reconnect CB.
151 if e := Wait(reconnectch); e != nil {
152 t.Fatal("Reconnect callback should have been triggered")
153 }
154
155 if nc.IsClosed() {
156 t.Fatal("Should have reconnected")

Callers

nothing calls this directly

Calls 9

ConnectMethod · 0.80
FatalfMethod · 0.80
SetErrorHandlerMethod · 0.80
IsClosedMethod · 0.80
ConnectedUrlMethod · 0.80
RunServerOnPortFunction · 0.70
RunServerWithOptionsFunction · 0.70
WaitFunction · 0.70
CloseMethod · 0.45

Tested by

no test coverage detected