Merge pull request #229 from nats-io/fix_pong

Fixed handling of unprompted PONG protocols
This commit is contained in:
Derek Collison
2016-03-23 15:11:29 -07:00
3 changed files with 59 additions and 4 deletions

View File

@@ -356,7 +356,7 @@ func (c *client) processPing() {
func (c *client) processPong() {
c.traceInOp("PONG", nil)
c.mu.Lock()
c.pout--
c.pout = 0
c.mu.Unlock()
}

View File

@@ -93,10 +93,16 @@ func TestParsePong(t *testing.T) {
if err != nil || c.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.state, err)
}
if c.pout != 0 {
t.Fatalf("Unexpected pout value: %d vs 0\n", c.pout)
}
err = c.parse(pong)
if err != nil || c.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.state, err)
}
if c.pout != 0 {
t.Fatalf("Unexpected pout value: %d vs 0\n", c.pout)
}
// Should tolerate spaces
pong = []byte("PONG \r")
err = c.parse(pong)
@@ -109,8 +115,11 @@ func TestParsePong(t *testing.T) {
if err != nil || c.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.state, err)
}
if c.pout != 0 {
t.Fatalf("Unexpected pout value: %d vs 0\n", c.pout)
}
// Should be adjusting c.pout, Pings Outstanding
// Should be adjusting c.pout (Pings Outstanding): reset to 0
c.state = OP_START
c.pout = 10
pong = []byte("PONG\r\n")
@@ -118,8 +127,8 @@ func TestParsePong(t *testing.T) {
if err != nil || c.state != OP_START {
t.Fatalf("Unexpected: %d : %v\n", c.state, err)
}
if c.pout != 9 {
t.Fatalf("Unexpected pout: %d vs %d\n", c.pout, 9)
if c.pout != 0 {
t.Fatalf("Unexpected pout: %d vs 0\n", c.pout)
}
}

View File

@@ -65,3 +65,49 @@ func TestPingInterval(t *testing.T) {
t.Fatal("timeout: Expected to have connection closed")
}
}
func TestUnpromptedPong(t *testing.T) {
s := runPingServer()
defer s.Shutdown()
c := createClientConn(t, "localhost", PING_TEST_PORT)
defer c.Close()
doConnect(t, c, false, false, false)
expect := expectCommand(t, c)
// Send lots of PONGs in a row...
for i := 0; i < 100; i++ {
c.Write([]byte("PONG\r\n"))
}
// The server should still send the max number of PINGs and then
// close the connection.
for i := 0; i < PING_MAX; i++ {
time.Sleep(PING_INTERVAL / 2)
expect(pingRe)
}
// We should get an error from the server
time.Sleep(PING_INTERVAL)
expect(errRe)
// Server should close the connection at this point..
c.SetWriteDeadline(time.Now().Add(PING_INTERVAL))
var err error
for {
_, err = c.Write([]byte("PING\r\n"))
if err != nil {
break
}
}
c.SetWriteDeadline(time.Time{})
if err == nil {
t.Fatal("No error: Expected to have connection closed")
}
if ne, ok := err.(net.Error); ok && ne.Timeout() {
t.Fatal("timeout: Expected to have connection closed")
}
}