Skip to content

Commit

Permalink
Merge 093548c into 139ce42
Browse files Browse the repository at this point in the history
  • Loading branch information
Waldemar Quevedo committed Sep 10, 2015
2 parents 139ce42 + 093548c commit 89293d0
Showing 1 changed file with 47 additions and 11 deletions.
58 changes: 47 additions & 11 deletions test/maxpayload_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ package test

import (
"fmt"
"net"
"strings"
"testing"
"time"

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

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

big := sizedBytes(4 * 1024 * 1024)
nc.Publish("foo", big)
err = nc.FlushTimeout(1 * time.Second)
if err == nil {
t.Fatalf("Expected an error from flush")
size := 4 * 1024 * 1024
big := sizedBytes(size)
err = nc.Publish("foo", big)

if err != nats.ErrMaxPayload {
t.Fatalf("Expected a Max Payload error")
}

conn, err := net.DialTimeout("tcp", endpoint, nc.Opts.Timeout)
if err != nil {
t.Fatalf("Could not make a raw connection to the server: %v", err)
}
info := make([]byte, 200)
_, err = conn.Read(info)
if err != nil {
t.Fatalf("Expected an info message to be sent by the server: %s", err)
}

pub := fmt.Sprintf("PUB bar %d\r\n", size)
conn.Write([]byte(pub))
if err != nil {
t.Fatalf("Could not publish event to the server: %s", err)
}
if strings.Contains(err.Error(), "Maximum Payload Violation") != true {
t.Fatalf("Received wrong error message (%v)\n", err)

errMsg := make([]byte, 35)
_, err = conn.Read(errMsg)
if err != nil {
t.Fatalf("Expected an error message to be sent by the server: %s", err)
}

if strings.Contains(string(errMsg), "Maximum Payload Violation") != true {
t.Errorf("Received wrong error message (%v)\n", string(errMsg))
}
if !nc.IsClosed() {
t.Fatalf("Expected connection to be closed")

// Client proactively omits sending the message so server
// does not close the connection.
if nc.IsClosed() {
t.Errorf("Expected connection to not be closed.")
}

// On the other hand client which did not proactively omitted
// publishing the bytes following what is suggested by server
// in the info message has its connection closed.
_, err = conn.Write(big)
if err == nil {
t.Errorf("Expected error due to maximum payload transgression.")
}
}

0 comments on commit 89293d0

Please sign in to comment.