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

Function TestWSGossipAndReconnect

test/ws_test.go:411–456  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

409}
410
411func TestWSGossipAndReconnect(t *testing.T) {
412 o1 := testWSGetDefaultOptions(t, false)
413 o1.ServerName = "A"
414 o1.Cluster.Host = "127.0.0.1"
415 o1.Cluster.Name = "abc"
416 o1.Cluster.Port = -1
417 s1 := RunServerWithOptions(o1)
418 defer s1.Shutdown()
419
420 o2 := testWSGetDefaultOptions(t, false)
421 o2.ServerName = "B"
422 o2.Cluster.Host = "127.0.0.1"
423 o2.Cluster.Name = "abc"
424 o2.Cluster.Port = -1
425 o2.Routes = server.RoutesFromStr(fmt.Sprintf("nats://127.0.0.1:%d", o1.Cluster.Port))
426 s2 := RunServerWithOptions(o2)
427 defer s2.Shutdown()
428
429 rch := make(chan bool, 10)
430 url := fmt.Sprintf("ws://127.0.0.1:%d", o1.Websocket.Port)
431 nc, err := nats.Connect(url,
432 nats.ReconnectWait(50*time.Millisecond),
433 nats.ReconnectHandler(func(_ *nats.Conn) { rch <- true }),
434 )
435 if err != nil {
436 t.Fatalf("Error on connect: %v", err)
437 }
438 defer nc.Close()
439
440 timeout := time.Now().Add(time.Second)
441 for time.Now().Before(timeout) {
442 if len(nc.Servers()) > 1 {
443 break
444 }
445 time.Sleep(15 * time.Millisecond)
446 }
447 if len(nc.Servers()) == 1 {
448 t.Fatal("Did not discover server 2")
449 }
450 s1.Shutdown()
451
452 // Wait for reconnect
453 if err := Wait(rch); err != nil {
454 t.Fatalf("Did not reconnect: %v", err)
455 }
456}
457
458func TestWSStress(t *testing.T) {
459 // Enable this test only when wanting to stress test the system, say after

Callers

nothing calls this directly

Calls 9

testWSGetDefaultOptionsFunction · 0.85
ConnectMethod · 0.80
ReconnectHandlerMethod · 0.80
FatalfMethod · 0.80
ServersMethod · 0.80
RunServerWithOptionsFunction · 0.70
WaitFunction · 0.70
AddMethod · 0.65
CloseMethod · 0.45

Tested by

no test coverage detected