Skip to content

Commit 7620f4a

Browse files
committed
Merge pull request #115 from wallyqs/update-max-payload-test
Update max payload test to reflect behavior change in go client.
2 parents 139ce42 + 093548c commit 7620f4a

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

test/maxpayload_test.go

+47-11
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ package test
44

55
import (
66
"fmt"
7+
"net"
78
"strings"
89
"testing"
9-
"time"
1010

1111
"github.com/nats-io/nats"
1212
)
@@ -15,22 +15,58 @@ func TestMaxPayload(t *testing.T) {
1515
srv, opts := RunServerWithConfig("./configs/override.conf")
1616
defer srv.Shutdown()
1717

18-
nc, err := nats.Connect(fmt.Sprintf("nats://%s:%d/", opts.Host, opts.Port))
18+
endpoint := fmt.Sprintf("%s:%d", opts.Host, opts.Port)
19+
nc, err := nats.Connect(fmt.Sprintf("nats://%s/", endpoint))
1920
if err != nil {
2021
t.Fatalf("Could not connect to server: %v", err)
2122
}
2223
defer nc.Close()
2324

24-
big := sizedBytes(4 * 1024 * 1024)
25-
nc.Publish("foo", big)
26-
err = nc.FlushTimeout(1 * time.Second)
27-
if err == nil {
28-
t.Fatalf("Expected an error from flush")
25+
size := 4 * 1024 * 1024
26+
big := sizedBytes(size)
27+
err = nc.Publish("foo", big)
28+
29+
if err != nats.ErrMaxPayload {
30+
t.Fatalf("Expected a Max Payload error")
31+
}
32+
33+
conn, err := net.DialTimeout("tcp", endpoint, nc.Opts.Timeout)
34+
if err != nil {
35+
t.Fatalf("Could not make a raw connection to the server: %v", err)
36+
}
37+
info := make([]byte, 200)
38+
_, err = conn.Read(info)
39+
if err != nil {
40+
t.Fatalf("Expected an info message to be sent by the server: %s", err)
41+
}
42+
43+
pub := fmt.Sprintf("PUB bar %d\r\n", size)
44+
conn.Write([]byte(pub))
45+
if err != nil {
46+
t.Fatalf("Could not publish event to the server: %s", err)
2947
}
30-
if strings.Contains(err.Error(), "Maximum Payload Violation") != true {
31-
t.Fatalf("Received wrong error message (%v)\n", err)
48+
49+
errMsg := make([]byte, 35)
50+
_, err = conn.Read(errMsg)
51+
if err != nil {
52+
t.Fatalf("Expected an error message to be sent by the server: %s", err)
53+
}
54+
55+
if strings.Contains(string(errMsg), "Maximum Payload Violation") != true {
56+
t.Errorf("Received wrong error message (%v)\n", string(errMsg))
3257
}
33-
if !nc.IsClosed() {
34-
t.Fatalf("Expected connection to be closed")
58+
59+
// Client proactively omits sending the message so server
60+
// does not close the connection.
61+
if nc.IsClosed() {
62+
t.Errorf("Expected connection to not be closed.")
63+
}
64+
65+
// On the other hand client which did not proactively omitted
66+
// publishing the bytes following what is suggested by server
67+
// in the info message has its connection closed.
68+
_, err = conn.Write(big)
69+
if err == nil {
70+
t.Errorf("Expected error due to maximum payload transgression.")
3571
}
3672
}

0 commit comments

Comments
 (0)