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

Function TestReceiveInfoWithEmptyConnectURLs

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

Source from the content-addressed store, hash-verified

2566}
2567
2568func TestReceiveInfoWithEmptyConnectURLs(t *testing.T) {
2569 ready := make(chan error, 2)
2570 ch := make(chan bool, 1)
2571 wg := sync.WaitGroup{}
2572 wg.Add(1)
2573
2574 go func() {
2575 defer wg.Done()
2576
2577 ports := []int{4222, 4223}
2578 for i := 0; i < 2; i++ {
2579 l, err := net.Listen("tcp", fmt.Sprintf("127.0.0.1:%d", ports[i]))
2580 if err != nil {
2581 ready <- fmt.Errorf("error on listen: %v", err)
2582 return
2583 }
2584 tl := l.(*net.TCPListener)
2585 defer tl.Close()
2586
2587 ready <- nil
2588
2589 c, err := tl.Accept()
2590 if err != nil {
2591 return
2592 }
2593 defer c.Close()
2594
2595 // Send the initial INFO
2596 c.Write([]byte(fmt.Sprintf("INFO {\"server_id\":\"server%d\"}\r\n", (i + 1))))
2597 buf := make([]byte, 0, 100)
2598 b := make([]byte, 100)
2599 for {
2600 n, err := c.Read(b)
2601 if err != nil {
2602 return
2603 }
2604 buf = append(buf, b[:n]...)
2605 if bytes.Contains(buf, []byte("PING\r\n")) {
2606 break
2607 }
2608 }
2609 if i == 0 {
2610 // Send PONG and following INFO in one go (or at least try).
2611 // The processing of PONG in sendConnect() should leave the
2612 // rest for the readLoop to process.
2613 c.Write([]byte("PONG\r\nINFO {\"server_id\":\"server1\",\"connect_urls\":[\"127.0.0.1:4222\", \"127.0.0.1:4223\", \"127.0.0.1:4224\"]}\r\n"))
2614 // Wait for the notification
2615 <-ch
2616 // Close the connection in our side and go back into accept
2617 c.Close()
2618 } else {
2619 // Send no connect ULRs (as if this was an older server that could in some cases
2620 // send an empty array)
2621 c.Write([]byte("PONG\r\nINFO {\"server_id\":\"server2\"}\r\n"))
2622 // Wait for client to disconnect
2623 for {
2624 if _, err := c.Read(buf); err != nil {
2625 return

Callers

nothing calls this directly

Calls 12

ErrorfMethod · 0.80
ConnectMethod · 0.80
ReconnectHandlerMethod · 0.80
FatalfMethod · 0.80
DiscoveredServersMethod · 0.80
WaitFunction · 0.70
AddMethod · 0.65
DoneMethod · 0.65
ErrorMethod · 0.65
CloseMethod · 0.45
WriteMethod · 0.45
ReadMethod · 0.45

Tested by

no test coverage detected