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

Function TestAuthErrorOnReconnect

test/nats_test.go:737–798  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

735}
736
737func TestAuthErrorOnReconnect(t *testing.T) {
738 // This is a bit of an artificial test, but it is to demonstrate
739 // that if the client is disconnected from a server (not due to an auth error),
740 // it will still correctly stop the reconnection logic if it gets twice an
741 // auth error from the same server.
742
743 o1 := natsserver.DefaultTestOptions
744 o1.Port = -1
745 s1 := RunServerWithOptions(&o1)
746 defer s1.Shutdown()
747
748 o2 := natsserver.DefaultTestOptions
749 o2.Port = -1
750 o2.Username = "ivan"
751 o2.Password = "pwd"
752 s2 := RunServerWithOptions(&o2)
753 defer s2.Shutdown()
754
755 dch := make(chan bool)
756 cch := make(chan bool)
757
758 urls := fmt.Sprintf("nats://%s:%d, nats://%s:%d", o1.Host, o1.Port, o2.Host, o2.Port)
759 nc, err := nats.Connect(urls,
760 nats.ReconnectWait(25*time.Millisecond),
761 nats.ReconnectJitter(0, 0),
762 nats.MaxReconnects(-1),
763 nats.DontRandomize(),
764 nats.ErrorHandler(func(_ *nats.Conn, _ *nats.Subscription, _ error) {}),
765 nats.DisconnectErrHandler(func(_ *nats.Conn, e error) {
766 dch <- true
767 }),
768 nats.ClosedHandler(func(_ *nats.Conn) {
769 cch <- true
770 }))
771 if err != nil {
772 t.Fatalf("Expected to connect, got err: %v\n", err)
773 }
774 defer nc.Close()
775
776 s1.Shutdown()
777
778 // wait for disconnect
779 if e := WaitTime(dch, 5*time.Second); e != nil {
780 t.Fatal("Did not receive a disconnect callback message")
781 }
782
783 // Wait for ClosedCB
784 if e := WaitTime(cch, 5*time.Second); e != nil {
785 reconnects := nc.Stats().Reconnects
786 t.Fatalf("Did not receive a closed callback message, #reconnects: %v", reconnects)
787 }
788
789 // We should have stopped after 2 reconnects.
790 if reconnects := nc.Stats().Reconnects; reconnects != 2 {
791 t.Fatalf("Expected 2 reconnects, got %v", reconnects)
792 }
793
794 // Expect connection to be closed...

Callers

nothing calls this directly

Calls 11

ConnectMethod · 0.80
ErrorHandlerMethod · 0.80
DisconnectErrHandlerMethod · 0.80
ClosedHandlerMethod · 0.80
FatalfMethod · 0.80
IsClosedMethod · 0.80
RunServerWithOptionsFunction · 0.70
WaitTimeFunction · 0.70
StatsMethod · 0.65
StatusMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected