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

Function TestBasicReconnectFunctionality

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

Source from the content-addressed store, hash-verified

164}
165
166func TestBasicReconnectFunctionality(t *testing.T) {
167 ts := startReconnectServer(t)
168 defer ts.Shutdown()
169
170 ch := make(chan bool)
171 dch := make(chan bool, 2)
172
173 opts := reconnectOpts
174
175 opts.DisconnectedErrCB = func(_ *nats.Conn, _ error) {
176 dch <- true
177 }
178
179 nc, err := opts.Connect()
180 if err != nil {
181 t.Fatalf("Should have connected ok: %v\n", err)
182 }
183 defer nc.Close()
184
185 testString := "bar"
186 nc.Subscribe("foo", func(m *nats.Msg) {
187 if string(m.Data) != testString {
188 t.Fatal("String doesn't match")
189 }
190 ch <- true
191 })
192 nc.Flush()
193
194 ts.Shutdown()
195 // server is stopped here...
196
197 if err := Wait(dch); err != nil {
198 t.Fatalf("Did not get the disconnected callback on time\n")
199 }
200
201 if err := nc.Publish("foo", []byte("bar")); err != nil {
202 t.Fatalf("Failed to publish message: %v\n", err)
203 }
204
205 ts = startReconnectServer(t)
206 defer ts.Shutdown()
207
208 if err := nc.FlushTimeout(5 * time.Second); err != nil {
209 t.Fatalf("Error on Flush: %v", err)
210 }
211
212 if e := Wait(ch); e != nil {
213 t.Fatal("Did not receive our message")
214 }
215
216 expectedReconnectCount := uint64(1)
217 reconnectCount := nc.Stats().Reconnects
218
219 if reconnectCount != expectedReconnectCount {
220 t.Fatalf("Reconnect count incorrect: %d vs %d\n",
221 reconnectCount, expectedReconnectCount)
222 }
223}

Callers

nothing calls this directly

Calls 10

startReconnectServerFunction · 0.85
ConnectMethod · 0.80
FatalfMethod · 0.80
WaitFunction · 0.70
SubscribeMethod · 0.65
PublishMethod · 0.65
StatsMethod · 0.65
CloseMethod · 0.45
FlushMethod · 0.45
FlushTimeoutMethod · 0.45

Tested by

no test coverage detected