Skip to content

Commit

Permalink
Cleanup partial message on reconnect
Browse files Browse the repository at this point in the history
If a connection error occurs when part after a partial message is
received, the partial message needs to be cleaned up to prevent a
'protocol error' from occuring.

Signed-off-by: Michael Fraenkel <fraenkel@us.ibm.com>
  • Loading branch information
sykesm committed Jun 26, 2014
1 parent 3a36f25 commit 1782bb5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .gitignore
Expand Up @@ -6,4 +6,4 @@
.bundle
*.gem
.DS_Store

.idea
1 change: 1 addition & 0 deletions lib/nats/client.rb
Expand Up @@ -671,6 +671,7 @@ def unbind #:nodoc:
@reconnecting = true if connected?
@connected = false
@pending = @pongs = nil
@buf = nil
cancel_ping_timer

schedule_primary_and_connect
Expand Down
47 changes: 47 additions & 0 deletions spec/partial_message_spec.rb
@@ -0,0 +1,47 @@
require 'spec_helper'

describe 'partial message behavior' do
before do
@s = NatsServerControl.new
@s.start_server
end

after do
@s.kill_server
end

it 'should not hold stale message data across a reconnect' do
got_message = false
expect do
EM.run do
timeout_em_on_failure(2)
timeout_nats_on_failure(2)

c1 = NATS.connect(:uri => @s.uri, :reconnect_time_wait => 0.25)
wait_on_connections([c1]) do
c1.subscribe('subject') do |msg|
got_message = true
msg.should eq('complete message')
EM.stop
NATS.stop
end

# Client receives partial message before server terminates
c1.receive_data("MSG subject 2 32\r\nincomplete")

@s.kill_server
@s.start_server

NATS.connect(:uri => @s.uri) do |c2|
EM.add_timer(0.25) do
c1.connected_server.should == @s.uri
c1.flush { c2.publish('subject', 'complete message') }
end
end
end
end
end.to_not raise_exception

got_message.should be_true
end
end

0 comments on commit 1782bb5

Please sign in to comment.