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

Function TestTimeoutOnNoServers

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

Source from the content-addressed store, hash-verified

544}
545
546func TestTimeoutOnNoServers(t *testing.T) {
547 s1 := RunServerOnPort(1222)
548 defer s1.Shutdown()
549
550 opts := nats.GetDefaultOptions()
551 if runtime.GOOS == "windows" {
552 opts.Servers = testServers[:2]
553 opts.MaxReconnect = 2
554 opts.Timeout = 100 * time.Millisecond
555 opts.ReconnectWait = (100 * time.Millisecond)
556 nats.ReconnectJitter(0, 0)(&opts)
557 } else {
558 opts.Servers = testServers
559 // 1 second total time wait
560 opts.MaxReconnect = 10
561 opts.ReconnectWait = (100 * time.Millisecond)
562 nats.ReconnectJitter(0, 0)(&opts)
563 }
564 opts.NoRandomize = true
565
566 dch := make(chan bool)
567 opts.DisconnectedErrCB = func(nc *nats.Conn, _ error) {
568 // Suppress any additional calls
569 nc.SetDisconnectErrHandler(nil)
570 dch <- true
571 }
572
573 cch := make(chan bool)
574 opts.ClosedCB = func(_ *nats.Conn) {
575 cch <- true
576 }
577
578 nc, err := opts.Connect()
579 if err != nil {
580 t.Fatalf("Expected to connect, got err: %v\n", err)
581 }
582 defer nc.Close()
583
584 s1.Shutdown()
585
586 // On Windows, creating a connection to a non-running server takes
587 // more than a second. So be generous with WaitTime
588
589 // wait for disconnect
590 if e := WaitTime(dch, 5*time.Second); e != nil {
591 t.Fatal("Did not receive a disconnect callback message")
592 }
593
594 startWait := time.Now()
595
596 // Wait for ClosedCB
597 if e := WaitTime(cch, 5*time.Second); e != nil {
598 t.Fatal("Did not receive a closed callback message")
599 }
600
601 if runtime.GOOS != "windows" {
602 timeWait := time.Since(startWait)
603

Callers

nothing calls this directly

Calls 7

ConnectMethod · 0.80
FatalfMethod · 0.80
DurationMethod · 0.80
RunServerOnPortFunction · 0.70
WaitTimeFunction · 0.70
CloseMethod · 0.45

Tested by

no test coverage detected