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

Function TestReconnectAllowedFlags

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

Source from the content-addressed store, hash-verified

80}
81
82func TestReconnectAllowedFlags(t *testing.T) {
83 ts := startReconnectServer(t)
84 defer ts.Shutdown()
85 ch := make(chan bool)
86 dch := make(chan bool)
87 opts := nats.GetDefaultOptions()
88 opts.Url = fmt.Sprintf("nats://127.0.0.1:%d", TEST_PORT)
89 opts.AllowReconnect = true
90 opts.MaxReconnect = 2
91 opts.ReconnectWait = 1 * time.Second
92 nats.ReconnectJitter(0, 0)(&opts)
93
94 opts.ClosedCB = func(_ *nats.Conn) {
95 ch <- true
96 }
97 opts.DisconnectedErrCB = func(_ *nats.Conn, _ error) {
98 dch <- true
99 }
100 nc, err := opts.Connect()
101 if err != nil {
102 t.Fatalf("Should have connected ok: %v", err)
103 }
104 defer nc.Close()
105
106 ts.Shutdown()
107
108 // We want wait to timeout here, and the connection
109 // should not trigger the Close CB.
110 if e := WaitTime(ch, 500*time.Millisecond); e == nil {
111 t.Fatal("Triggered ClosedCB incorrectly")
112 }
113
114 // We should wait to get the disconnected callback to ensure
115 // that we are in the process of reconnecting.
116 if e := Wait(dch); e != nil {
117 t.Fatal("DisconnectedErrCB should have been triggered")
118 }
119
120 if !nc.IsReconnecting() {
121 t.Fatal("Expected to be in a reconnecting state")
122 }
123
124 // clear the CloseCB since ch will block
125 nc.Opts.ClosedCB = nil
126}
127
128func TestConnCloseBreaksReconnectLoop(t *testing.T) {
129 ts := startReconnectServer(t)

Callers

nothing calls this directly

Calls 7

startReconnectServerFunction · 0.85
ConnectMethod · 0.80
FatalfMethod · 0.80
IsReconnectingMethod · 0.80
WaitTimeFunction · 0.70
WaitFunction · 0.70
CloseMethod · 0.45

Tested by

no test coverage detected