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

Function TestServerErrorClosesConnection

test/conn_test.go:1620–1715  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1618}
1619
1620func TestServerErrorClosesConnection(t *testing.T) {
1621 serverInfo := "INFO {\"server_id\":\"foobar\",\"host\":\"%s\",\"port\":%d,\"auth_required\":false,\"tls_required\":false,\"max_payload\":1048576}\r\n"
1622
1623 l, e := net.Listen("tcp", "127.0.0.1:0")
1624 if e != nil {
1625 t.Fatal("Could not listen on an ephemeral port")
1626 }
1627 tl := l.(*net.TCPListener)
1628 defer tl.Close()
1629
1630 addr := tl.Addr().(*net.TCPAddr)
1631 done := make(chan bool)
1632 dch := make(chan bool)
1633 cch := make(chan bool)
1634
1635 serverSentError := "Any Error"
1636 reconnected := int64(0)
1637
1638 errCh := make(chan error, 1)
1639 go func() {
1640 conn, err := l.Accept()
1641 if err != nil {
1642 errCh <- fmt.Errorf("error accepting client connection: %v", err)
1643 return
1644 }
1645 defer conn.Close()
1646 info := fmt.Sprintf(serverInfo, addr.IP, addr.Port)
1647 conn.Write([]byte(info))
1648
1649 // Read connect and ping commands sent from the client
1650 br := bufio.NewReaderSize(conn, 1024)
1651 if _, err := br.ReadString('\n'); err != nil {
1652 errCh <- fmt.Errorf("expected CONNECT from client, got: %s", err)
1653 return
1654 }
1655 if _, err := br.ReadString('\n'); err != nil {
1656 errCh <- fmt.Errorf("expected PING from client, got: %s", err)
1657 return
1658 }
1659 conn.Write([]byte("PONG\r\n"))
1660
1661 // Wait a tiny, and simulate a Stale Connection
1662 time.Sleep(50 * time.Millisecond)
1663 conn.Write([]byte("-ERR '" + serverSentError + "'\r\n"))
1664
1665 // Hang around a bit
1666 <-done
1667 }()
1668
1669 // Wait for server mock to start
1670 time.Sleep(100 * time.Millisecond)
1671
1672 natsURL := fmt.Sprintf("nats://%s:%d", addr.IP, addr.Port)
1673 opts := nats.GetDefaultOptions()
1674 opts.AllowReconnect = true
1675 opts.DisconnectedErrCB = func(_ *nats.Conn, _ error) { dch <- true }
1676 opts.ReconnectedCB = func(_ *nats.Conn) { atomic.AddInt64(&reconnected, 1) }
1677 opts.ClosedCB = func(_ *nats.Conn) { cch <- true }

Callers

nothing calls this directly

Calls 10

ErrorfMethod · 0.80
ReadStringMethod · 0.80
ConnectMethod · 0.80
FatalfMethod · 0.80
WaitFunction · 0.70
checkErrChannelFunction · 0.70
ErrorMethod · 0.65
CloseMethod · 0.45
WriteMethod · 0.45
LastErrorMethod · 0.45

Tested by

no test coverage detected