(t *testing.T)
| 772 | } |
| 773 | |
| 774 | func TestErrOnMaxPayloadLimit(t *testing.T) { |
| 775 | expectedMaxPayload := int64(10) |
| 776 | serverInfo := "INFO {\"server_id\":\"foobar\",\"host\":\"%s\",\"port\":%d,\"auth_required\":false,\"tls_required\":false,\"max_payload\":%d}\r\n" |
| 777 | |
| 778 | l, e := net.Listen("tcp", "127.0.0.1:0") |
| 779 | if e != nil { |
| 780 | t.Fatal("Could not listen on an ephemeral port") |
| 781 | } |
| 782 | tl := l.(*net.TCPListener) |
| 783 | defer tl.Close() |
| 784 | |
| 785 | addr := tl.Addr().(*net.TCPAddr) |
| 786 | |
| 787 | // Send back an INFO message with custom max payload size on connect. |
| 788 | var conn net.Conn |
| 789 | var err error |
| 790 | |
| 791 | errCh := make(chan error, 1) |
| 792 | go func() { |
| 793 | conn, err = l.Accept() |
| 794 | if err != nil { |
| 795 | errCh <- fmt.Errorf("error accepting client connection: %v", err) |
| 796 | return |
| 797 | } |
| 798 | defer conn.Close() |
| 799 | info := fmt.Sprintf(serverInfo, addr.IP, addr.Port, expectedMaxPayload) |
| 800 | conn.Write([]byte(info)) |
| 801 | |
| 802 | // Read connect and ping commands sent from the client |
| 803 | line := make([]byte, 111) |
| 804 | _, err := conn.Read(line) |
| 805 | if err != nil { |
| 806 | errCh <- fmt.Errorf("expected CONNECT and PING from client, got: %s", err) |
| 807 | return |
| 808 | } |
| 809 | conn.Write([]byte("PONG\r\n")) |
| 810 | // Hang around a bit to not err on EOF in client. |
| 811 | time.Sleep(250 * time.Millisecond) |
| 812 | }() |
| 813 | |
| 814 | // Wait for server mock to start |
| 815 | time.Sleep(100 * time.Millisecond) |
| 816 | |
| 817 | natsURL := fmt.Sprintf("nats://%s:%d", addr.IP, addr.Port) |
| 818 | opts := nats.GetDefaultOptions() |
| 819 | opts.Servers = []string{natsURL} |
| 820 | nc, err := opts.Connect() |
| 821 | if err != nil { |
| 822 | t.Fatalf("Expected INFO message with custom max payload, got: %s", err) |
| 823 | } |
| 824 | defer nc.Close() |
| 825 | |
| 826 | got := nc.MaxPayload() |
| 827 | if got != expectedMaxPayload { |
| 828 | t.Fatalf("Expected MaxPayload to be %d, got: %d", expectedMaxPayload, got) |
| 829 | } |
| 830 | err = nc.Publish("hello", []byte("hello world")) |
| 831 | if err != nats.ErrMaxPayload { |
nothing calls this directly
no test coverage detected