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

Function TestMoreErrOnConnect

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

Source from the content-addressed store, hash-verified

639}
640
641func TestMoreErrOnConnect(t *testing.T) {
642 l, e := net.Listen("tcp", "127.0.0.1:0")
643 if e != nil {
644 t.Fatal("Could not listen on an ephemeral port")
645 }
646 tl := l.(*net.TCPListener)
647 defer tl.Close()
648
649 addr := tl.Addr().(*net.TCPAddr)
650
651 done := make(chan bool)
652 case1 := make(chan bool)
653 case2 := make(chan bool)
654 case3 := make(chan bool)
655 case4 := make(chan bool)
656
657 errCh := make(chan error, 5)
658 go func() {
659 for i := 0; i < 5; i++ {
660 conn, err := l.Accept()
661 if err != nil {
662 errCh <- fmt.Errorf("error accepting client connection: %v", err)
663 return
664 }
665 switch i {
666 case 0:
667 // Send back a partial INFO and close the connection.
668 conn.Write([]byte("INFO"))
669 case 1:
670 // Send just INFO
671 conn.Write([]byte("INFO\r\n"))
672 // Stick around a bit
673 <-case1
674 case 2:
675 info := fmt.Sprintf("INFO {\"server_id\":\"foobar\",\"host\":\"%s\",\"port\":%d,\"auth_required\":false,\"tls_required\":false,\"max_payload\":1048576}\r\n", addr.IP, addr.Port)
676 // Send complete INFO
677 conn.Write([]byte(info))
678 // Read connect and ping commands sent from the client
679 br := bufio.NewReaderSize(conn, 1024)
680 if _, err := br.ReadString('\n'); err != nil {
681 errCh <- fmt.Errorf("expected CONNECT from client, got: %s", err)
682 return
683 }
684 if _, err := br.ReadString('\n'); err != nil {
685 errCh <- fmt.Errorf("expected PING from client, got: %s", err)
686 return
687 }
688 // Client expect +OK, send it but then something else than PONG
689 conn.Write([]byte("+OK\r\n"))
690 // Stick around a bit
691 <-case2
692 case 3:
693 info := fmt.Sprintf("INFO {\"server_id\":\"foobar\",\"host\":\"%s\",\"port\":%d,\"auth_required\":false,\"tls_required\":false,\"max_payload\":1048576}\r\n", addr.IP, addr.Port)
694 // Send complete INFO
695 conn.Write([]byte(info))
696 // Read connect and ping commands sent from the client
697 br := bufio.NewReaderSize(conn, 1024)
698 if _, err := br.ReadString('\n'); err != nil {

Callers

nothing calls this directly

Calls 7

ErrorfMethod · 0.80
ReadStringMethod · 0.80
ConnectMethod · 0.80
checkErrChannelFunction · 0.70
TimeoutMethod · 0.65
CloseMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected