Skip to content

Commit

Permalink
Fixed reconnect error to an authorized server with pending items
Browse files Browse the repository at this point in the history
  • Loading branch information
derekcollison committed Jun 26, 2012
1 parent f9a933d commit c1ded8c
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/nats/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ def send_connect_command #:nodoc:
cs[:pass] = @uri.password
end
cs[:ssl_required] = @ssl if @ssl
send_command("CONNECT #{cs.to_json}#{CR_LF}")
send_command("CONNECT #{cs.to_json}#{CR_LF}", true)
end

def queue_server_rt(&cb) #:nodoc:
Expand Down Expand Up @@ -613,9 +613,11 @@ def attempt_reconnect #:nodoc:
@reconnect_cb.call unless @reconnect_cb.nil?
end

def send_command(command) #:nodoc:
def send_command(command, priority = false) #:nodoc:
EM.next_tick { flush_pending } if (connected? && @pending.nil?)
(@pending ||= []) << command
@pending ||= []
@pending << command unless priority
@pending.unshift(command) if priority
@pending_size += command.bytesize
flush_pending if (connected? && @pending_size > MAX_PENDING_SIZE)
if (@options[:fast_producer_error] && pending_data_size > FAST_PRODUCER_THRESHOLD)
Expand Down
20 changes: 20 additions & 0 deletions spec/reconnect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@
describe 'client specification' do

before(:all) do
R_USER = 'derek'
R_PASS = 'mypassword'
R_TEST_AUTH_SERVER = "nats://#{R_USER}:#{R_PASS}@localhost:9333"
R_TEST_SERVER_PID = '/tmp/nats_reconnect_authorization.pid'
@as = NatsServerControl.new(R_TEST_AUTH_SERVER, R_TEST_SERVER_PID)
@as.start_server
@s = NatsServerControl.new
@s.start_server
end

after(:all) do
@s.kill_server
@as.kill_server
end

it 'should properly report connected after connect callback' do
Expand All @@ -34,4 +41,17 @@
reconnect_cb.should be_true
end

it 'should do publish without error even if reconnected to an authorized server' do
NATS.start(:uri => R_TEST_AUTH_SERVER, :reconnect_time_wait => 0.25) do |c|
c.on_reconnect do
expect do
NATS.publish('reconnect test')
end.to_not raise_error
end
@as.kill_server
EM.add_timer(0.25) { @as.start_server }
EM.add_timer(1.0) { NATS.stop }
end
end

end

0 comments on commit c1ded8c

Please sign in to comment.