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

Function TestProperReconnectDelay

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

Source from the content-addressed store, hash-verified

349}
350
351func TestProperReconnectDelay(t *testing.T) {
352 s1 := RunServerOnPort(1222)
353 defer s1.Shutdown()
354
355 var srvs string
356 opts := nats.GetDefaultOptions()
357 if runtime.GOOS == "windows" {
358 srvs = strings.Join(testServers[:2], ",")
359 } else {
360 srvs = strings.Join(testServers, ",")
361 }
362 opts.NoRandomize = true
363
364 dcbCalled := false
365 closedCbCalled := false
366 dch := make(chan bool)
367
368 dcb := func(nc *nats.Conn) {
369 // Suppress any additional calls
370 if dcbCalled {
371 return
372 }
373 dcbCalled = true
374 dch <- true
375 }
376
377 ccb := func(_ *nats.Conn) {
378 closedCbCalled = true
379 }
380
381 nc, err := nats.Connect(srvs, nats.DontRandomize(), nats.DisconnectHandler(dcb), nats.ClosedHandler(ccb))
382 if err != nil {
383 t.Fatalf("Expected to connect, got err: %v\n", err)
384 }
385 defer nc.Close()
386
387 s1.Shutdown()
388
389 // wait for disconnect
390 if e := WaitTime(dch, 2*time.Second); e != nil {
391 t.Fatal("Did not receive a disconnect callback message")
392 }
393
394 // Wait, want to make sure we don't spin on reconnect to non-existent servers.
395 time.Sleep(1 * time.Second)
396
397 // Make sure we are still reconnecting..
398 if closedCbCalled {
399 t.Fatal("Closed CB was triggered, should not have been.")
400 }
401 if status := nc.Status(); status != nats.RECONNECTING {
402 t.Fatalf("Wrong status: %d\n", status)
403 }
404}
405
406func TestProperFalloutAfterMaxAttempts(t *testing.T) {
407 s1 := RunServerOnPort(1222)

Callers

nothing calls this directly

Calls 7

ConnectMethod · 0.80
ClosedHandlerMethod · 0.80
FatalfMethod · 0.80
RunServerOnPortFunction · 0.70
WaitTimeFunction · 0.70
StatusMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected