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

Function TestErrStaleConnection

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

Source from the content-addressed store, hash-verified

1508}
1509
1510func TestErrStaleConnection(t *testing.T) {
1511 serverInfo := "INFO {\"server_id\":\"foobar\",\"host\":\"%s\",\"port\":%d,\"auth_required\":false,\"tls_required\":false,\"max_payload\":1048576}\r\n"
1512
1513 l, e := net.Listen("tcp", "127.0.0.1:0")
1514 if e != nil {
1515 t.Fatal("Could not listen on an ephemeral port")
1516 }
1517 tl := l.(*net.TCPListener)
1518 defer tl.Close()
1519
1520 addr := tl.Addr().(*net.TCPAddr)
1521 done := make(chan bool)
1522 dch := make(chan bool)
1523 rch := make(chan bool)
1524 cch := make(chan bool)
1525 sch := make(chan bool)
1526
1527 firstDisconnect := true
1528
1529 errCh := make(chan error, 1)
1530 go func() {
1531 for i := 0; i < 2; i++ {
1532 conn, err := l.Accept()
1533 if err != nil {
1534 errCh <- fmt.Errorf("error accepting client connection: %v", err)
1535 return
1536 }
1537 defer conn.Close()
1538 info := fmt.Sprintf(serverInfo, addr.IP, addr.Port)
1539 conn.Write([]byte(info))
1540
1541 // Read connect and ping commands sent from the client
1542 br := bufio.NewReaderSize(conn, 1024)
1543 if _, err := br.ReadString('\n'); err != nil {
1544 errCh <- fmt.Errorf("expected CONNECT from client, got: %s", err)
1545 return
1546 }
1547 if _, err := br.ReadString('\n'); err != nil {
1548 errCh <- fmt.Errorf("expected PING from client, got: %s", err)
1549 return
1550 }
1551 conn.Write([]byte("PONG\r\n"))
1552
1553 if i == 0 {
1554 // Wait a tiny, and simulate a Stale Connection
1555 time.Sleep(50 * time.Millisecond)
1556 conn.Write([]byte("-ERR 'Stale Connection'\r\n"))
1557
1558 // The client should try to reconnect. When getting the
1559 // disconnected callback, it will close this channel.
1560 <-sch
1561
1562 // Close the connection and go back to accept the new
1563 // connection.
1564 conn.Close()
1565 } else {
1566 // Hang around a bit
1567 <-done

Callers

nothing calls this directly

Calls 8

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

Tested by

no test coverage detected