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

Function TestBasicClusterReconnect

test/cluster_test.go:218–281  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

216}
217
218func TestBasicClusterReconnect(t *testing.T) {
219 s1 := RunServerOnPort(1222)
220 defer s1.Shutdown()
221 s2 := RunServerOnPort(1224)
222 defer s2.Shutdown()
223
224 dch := make(chan bool)
225 rch := make(chan bool)
226
227 dcbCalled := false
228
229 opts := []nats.Option{nats.DontRandomize(),
230 nats.Timeout(100 * time.Millisecond),
231 nats.DisconnectErrHandler(func(nc *nats.Conn, _ error) {
232 // Suppress any additional callbacks
233 if dcbCalled {
234 return
235 }
236 dcbCalled = true
237 dch <- true
238 }),
239 nats.ReconnectHandler(func(_ *nats.Conn) { rch <- true }),
240 }
241
242 nc, err := nats.Connect(servers, opts...)
243 if err != nil {
244 t.Fatalf("Expected to connect, got err: %v\n", err)
245 }
246 defer nc.Close()
247
248 s1.Shutdown()
249
250 // wait for disconnect
251 if e := WaitTime(dch, 2*time.Second); e != nil {
252 t.Fatal("Did not receive a disconnect callback message")
253 }
254
255 reconnectTimeStart := time.Now()
256
257 // wait for reconnect
258 if e := WaitTime(rch, 2*time.Second); e != nil {
259 t.Fatal("Did not receive a reconnect callback message")
260 }
261
262 if nc.ConnectedUrl() != testServers[2] {
263 t.Fatalf("Does not report correct connection: %s\n",
264 nc.ConnectedUrl())
265 }
266
267 // Make sure we did not wait on reconnect for default time.
268 // Reconnect should be fast since it will be a switch to the
269 // second server and not be dependent on server restart time.
270
271 // On Windows, a failed connect takes more than a second, so
272 // account for that.
273 maxDuration := 100 * time.Millisecond
274 if runtime.GOOS == "windows" {
275 maxDuration = 1100 * time.Millisecond

Callers

nothing calls this directly

Calls 9

DisconnectErrHandlerMethod · 0.80
ReconnectHandlerMethod · 0.80
ConnectMethod · 0.80
FatalfMethod · 0.80
ConnectedUrlMethod · 0.80
RunServerOnPortFunction · 0.70
WaitTimeFunction · 0.70
TimeoutMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected