Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Currently the reconnect logic will only retry once if EM.reconnect throw... #63

Merged
merged 1 commit into from
Jan 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion lib/nats/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -606,7 +606,10 @@ def process_disconnect #:nodoc:

def attempt_reconnect #:nodoc:
process_disconnect and return if (@reconnect_attempts += 1) > @options[:max_reconnect_attempts]
EM.reconnect(@uri.host, @uri.port, self)
begin
EM.reconnect(@uri.host, @uri.port, self)
rescue
end
@reconnect_cb.call unless @reconnect_cb.nil?
end

Expand Down
23 changes: 23 additions & 0 deletions spec/reconnect_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@
R_PASS = 'mypassword'
R_TEST_AUTH_SERVER = "nats://#{R_USER}:#{R_PASS}@localhost:9333"
R_TEST_SERVER_PID = '/tmp/nats_reconnect_authorization.pid'
E_TEST_SERVER = "nats://localhost:9666"
E_TEST_SERVER_PID = '/tmp/nats_reconnect_exception_test.pid'

@as = NatsServerControl.new(R_TEST_AUTH_SERVER, R_TEST_SERVER_PID)
@as.start_server
@s = NatsServerControl.new
@s.start_server
@es = NatsServerControl.new(E_TEST_SERVER, E_TEST_SERVER_PID)
@es.start_server
end

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

it 'should properly report connected after connect callback' do
Expand All @@ -26,6 +32,23 @@
end
end

it 'should properly handle exceptions thrown by eventmachine during reconnects' do
reconnect_cb = false
NATS.start(:uri => E_TEST_SERVER, :reconnect_time_wait => 0.25) do |c|
# Change the uri to simulate a DNS failure which will make EM.reconnect throw an exception
c.instance_eval('@uri = URI.parse("nats://does.not.exist:4222/")')
timer=timeout_nats_on_failure(1)
c.on_reconnect do
reconnect_cb = true
NATS.connected?.should be_false
NATS.reconnecting?.should be_true
NATS.stop
end
@es.kill_server
end
reconnect_cb.should be_true
end

it 'should report a reconnecting event when trying to reconnect' do
reconnect_cb = false
NATS.start(:reconnect_time_wait => 0.25) do |c|
Expand Down