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

Function TestHotSpotReconnect

test/cluster_test.go:283–349  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

281}
282
283func TestHotSpotReconnect(t *testing.T) {
284 s1 := RunServerOnPort(1222)
285 defer s1.Shutdown()
286
287 numClients := 32
288 clients := []*nats.Conn{}
289
290 wg := &sync.WaitGroup{}
291 wg.Add(numClients)
292
293 opts := []nats.Option{
294 nats.ReconnectWait(50 * time.Millisecond),
295 nats.ReconnectJitter(0, 0),
296 nats.ReconnectHandler(func(_ *nats.Conn) { wg.Done() }),
297 }
298 var srvrs string
299 if runtime.GOOS == "windows" {
300 srvrs = strings.Join(testServers[:5], ",")
301 opts = append(opts, nats.Timeout(100*time.Millisecond))
302 } else {
303 srvrs = servers
304 }
305
306 for i := 0; i < numClients; i++ {
307 nc, err := nats.Connect(srvrs, opts...)
308 if err != nil {
309 t.Fatalf("Expected to connect, got err: %v\n", err)
310 }
311 defer nc.Close()
312 if nc.ConnectedUrl() != testServers[0] {
313 t.Fatalf("Connected to incorrect server: %v\n", nc.ConnectedUrl())
314 }
315 clients = append(clients, nc)
316 }
317
318 s2 := RunServerOnPort(1224)
319 defer s2.Shutdown()
320 s3 := RunServerOnPort(1226)
321 defer s3.Shutdown()
322
323 s1.Shutdown()
324
325 numServers := 2
326
327 // Wait on all reconnects
328 wg.Wait()
329
330 // Walk the clients and calculate how many of each..
331 cs := make(map[string]int)
332 for _, nc := range clients {
333 cs[nc.ConnectedUrl()]++
334 nc.Close()
335 }
336 if len(cs) != numServers {
337 t.Fatalf("Wrong number of reported servers: %d vs %d\n", len(cs), numServers)
338 }
339 expected := numClients / numServers
340 v := uint(float32(expected) * 0.50)

Callers

nothing calls this directly

Calls 9

ReconnectHandlerMethod · 0.80
ConnectMethod · 0.80
FatalfMethod · 0.80
ConnectedUrlMethod · 0.80
RunServerOnPortFunction · 0.70
AddMethod · 0.65
DoneMethod · 0.65
TimeoutMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected