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

Function TestDrainConnDuringReconnect

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

Source from the content-addressed store, hash-verified

456}
457
458func TestDrainConnDuringReconnect(t *testing.T) {
459 s := RunDefaultServer()
460 defer s.Shutdown()
461
462 done := make(chan bool, 1)
463 closedCb := func(nc *nats.Conn) {
464 done <- true
465 }
466
467 nc, err := nats.Connect(nats.DefaultURL,
468 nats.ClosedHandler(closedCb),
469 nats.DrainTimeout(20*time.Millisecond))
470 if err != nil {
471 t.Fatalf("Failed to create default connection: %v", err)
472 }
473 defer nc.Close()
474
475 // Shutdown the server.
476 s.Shutdown()
477
478 waitFor(t, time.Second, 10*time.Millisecond, func() error {
479 if nc.IsReconnecting() {
480 return nil
481 }
482 return errors.New("Not reconnecting yet")
483 })
484
485 // This should work correctly.
486 if err := nc.Drain(); err != nats.ErrConnectionReconnecting {
487 t.Fatalf("Unexpected error on drain: %v", err)
488 }
489
490 // Closed should still fire.
491 select {
492 case <-done:
493 case <-time.After(2 * time.Second):
494 t.Fatalf("Timeout waiting for closed state for connection")
495 }
496}
497
498func TestDrainClosedHandlerRace(t *testing.T) {
499 s := RunDefaultServer()

Callers

nothing calls this directly

Calls 8

waitForFunction · 0.85
ConnectMethod · 0.80
ClosedHandlerMethod · 0.80
FatalfMethod · 0.80
IsReconnectingMethod · 0.80
RunDefaultServerFunction · 0.70
DrainMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected