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

Function TestIsReconnectingAndStatus

test/reconnect_test.go:428–496  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

426}
427
428func TestIsReconnectingAndStatus(t *testing.T) {
429 ts := startReconnectServer(t)
430 defer ts.Shutdown()
431
432 disconnectedch := make(chan bool, 3)
433 reconnectch := make(chan bool, 2)
434 opts := nats.GetDefaultOptions()
435 opts.Url = fmt.Sprintf("nats://127.0.0.1:%d", TEST_PORT)
436 opts.AllowReconnect = true
437 opts.MaxReconnect = 10000
438 opts.ReconnectWait = 100 * time.Millisecond
439 nats.ReconnectJitter(0, 0)(&opts)
440
441 opts.DisconnectedErrCB = func(_ *nats.Conn, _ error) {
442 disconnectedch <- true
443 }
444 opts.ReconnectedCB = func(_ *nats.Conn) {
445 reconnectch <- true
446 }
447
448 // Connect, verify initial reconnecting state check, then stop the server
449 nc, err := opts.Connect()
450 if err != nil {
451 t.Fatalf("Should have connected ok: %v", err)
452 }
453 defer nc.Close()
454
455 if nc.IsReconnecting() {
456 t.Fatalf("IsReconnecting returned true when the connection is still open.")
457 }
458 if status := nc.Status(); status != nats.CONNECTED {
459 t.Fatalf("Status returned %d when connected instead of CONNECTED", status)
460 }
461 ts.Shutdown()
462
463 // Wait until we get the disconnected callback
464 if e := Wait(disconnectedch); e != nil {
465 t.Fatalf("Disconnect callback wasn't triggered: %v", e)
466 }
467 if !nc.IsReconnecting() {
468 t.Fatalf("IsReconnecting returned false when the client is reconnecting.")
469 }
470 if status := nc.Status(); status != nats.RECONNECTING {
471 t.Fatalf("Status returned %d when reconnecting instead of CONNECTED", status)
472 }
473
474 ts = startReconnectServer(t)
475 defer ts.Shutdown()
476
477 // Wait until we get the reconnect callback
478 if e := Wait(reconnectch); e != nil {
479 t.Fatalf("Reconnect callback wasn't triggered: %v", e)
480 }
481 if nc.IsReconnecting() {
482 t.Fatalf("IsReconnecting returned true after the connection was reconnected.")
483 }
484 if status := nc.Status(); status != nats.CONNECTED {
485 t.Fatalf("Status returned %d when reconnected instead of CONNECTED", status)

Callers

nothing calls this directly

Calls 7

startReconnectServerFunction · 0.85
ConnectMethod · 0.80
FatalfMethod · 0.80
IsReconnectingMethod · 0.80
WaitFunction · 0.70
StatusMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected