@@ -4,9 +4,9 @@ package test
4
4
5
5
import (
6
6
"fmt"
7
+ "net"
7
8
"strings"
8
9
"testing"
9
- "time"
10
10
11
11
"github.com/nats-io/nats"
12
12
)
@@ -15,22 +15,58 @@ func TestMaxPayload(t *testing.T) {
15
15
srv , opts := RunServerWithConfig ("./configs/override.conf" )
16
16
defer srv .Shutdown ()
17
17
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 ))
19
20
if err != nil {
20
21
t .Fatalf ("Could not connect to server: %v" , err )
21
22
}
22
23
defer nc .Close ()
23
24
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 )
29
47
}
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 ))
32
57
}
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." )
35
71
}
36
72
}
0 commit comments