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

Function TestConnCloseBreaksReconnectLoop

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

Source from the content-addressed store, hash-verified

126}
127
128func TestConnCloseBreaksReconnectLoop(t *testing.T) {
129 ts := startReconnectServer(t)
130 defer ts.Shutdown()
131
132 cch := make(chan bool)
133
134 opts := reconnectOpts
135 // Bump the max reconnect attempts
136 opts.MaxReconnect = 100
137 opts.ClosedCB = func(_ *nats.Conn) {
138 cch <- true
139 }
140 nc, err := opts.Connect()
141 if err != nil {
142 t.Fatalf("Should have connected ok: %v", err)
143 }
144 defer nc.Close()
145 nc.Flush()
146
147 // Shutdown the server
148 ts.Shutdown()
149
150 // Wait a second, then close the connection
151 time.Sleep(time.Second)
152
153 // Close the connection, this should break the reconnect loop.
154 // Do this in a go routine since the issue was that Close()
155 // would block until the reconnect loop is done.
156 go nc.Close()
157
158 // Even on Windows (where a createConn takes more than a second)
159 // we should be able to break the reconnect loop with the following
160 // timeout.
161 if err := WaitTime(cch, 3*time.Second); err != nil {
162 t.Fatal("Did not get a closed callback")
163 }
164}
165
166func TestBasicReconnectFunctionality(t *testing.T) {
167 ts := startReconnectServer(t)

Callers

nothing calls this directly

Calls 6

startReconnectServerFunction · 0.85
ConnectMethod · 0.80
FatalfMethod · 0.80
WaitTimeFunction · 0.70
CloseMethod · 0.45
FlushMethod · 0.45

Tested by

no test coverage detected