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

Function TestReceiveInfoRightAfterFirstPong

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

Source from the content-addressed store, hash-verified

2496}
2497
2498func TestReceiveInfoRightAfterFirstPong(t *testing.T) {
2499 l, err := net.Listen("tcp", "127.0.0.1:0")
2500 if err != nil {
2501 t.Fatalf("Error on listen: %v", err)
2502 }
2503 tl := l.(*net.TCPListener)
2504 defer tl.Close()
2505 addr := tl.Addr().(*net.TCPAddr)
2506
2507 wg := sync.WaitGroup{}
2508 wg.Add(1)
2509 go func() {
2510 defer wg.Done()
2511
2512 c, err := tl.Accept()
2513 if err != nil {
2514 return
2515 }
2516 defer c.Close()
2517 // Send the initial INFO
2518 c.Write([]byte("INFO {}\r\n"))
2519 buf := make([]byte, 0, 100)
2520 b := make([]byte, 100)
2521 for {
2522 n, err := c.Read(b)
2523 if err != nil {
2524 return
2525 }
2526 buf = append(buf, b[:n]...)
2527 if bytes.Contains(buf, []byte("PING\r\n")) {
2528 break
2529 }
2530 }
2531 // Send PONG and following INFO in one go (or at least try).
2532 // The processing of PONG in sendConnect() should leave the
2533 // rest for the readLoop to process.
2534 c.Write([]byte(fmt.Sprintf("PONG\r\nINFO {\"connect_urls\":[\"127.0.0.1:%d\", \"me:1\"]}\r\n", addr.Port)))
2535 // Wait for client to disconnect
2536 for {
2537 if _, err := c.Read(buf); err != nil {
2538 return
2539 }
2540 }
2541 }()
2542
2543 nc, err := nats.Connect(fmt.Sprintf("nats://127.0.0.1:%d", addr.Port))
2544 if err != nil {
2545 t.Fatalf("Error on connect: %v", err)
2546 }
2547 defer nc.Close()
2548 var (
2549 ds []string
2550 timeout = time.Now().Add(2 * time.Second)
2551 ok = false
2552 )
2553 for time.Now().Before(timeout) {
2554 ds = nc.DiscoveredServers()
2555 if len(ds) == 1 && ds[0] == "nats://me:1" {

Callers

nothing calls this directly

Calls 8

FatalfMethod · 0.80
ConnectMethod · 0.80
DiscoveredServersMethod · 0.80
AddMethod · 0.65
DoneMethod · 0.65
CloseMethod · 0.45
WriteMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected