Skip to content

Commit

Permalink
Merge pull request #10 from wallyqs/fix-initial-reconnect-attempts
Browse files Browse the repository at this point in the history
Fix reconnection attempts count on first attempt
  • Loading branch information
Waldemar Quevedo authored Oct 5, 2016
2 parents 961e3dc + e851751 commit 7a3b997
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 2 deletions.
3 changes: 2 additions & 1 deletion nats/io/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ def connect(self,
if s is None:
raise ErrNoServers

# Mark that we have attempted to connect
s.reconnects += 1
yield self._server_connect(s)
self._current_server = s
s.did_connect = True
Expand All @@ -209,7 +211,6 @@ def connect(self,
self._error_cb(ErrServerConnect(e))
if not self.options["allow_reconnect"]:
raise ErrNoServers
self._current_server.reconnects += 1

# Flush pending data before continuing in connected status.
# FIXME: Could use future here and wait for an error result
Expand Down
24 changes: 23 additions & 1 deletion tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def disconnected_cb(self):
self.disconnected_cb_called = True

client = SampleClient()
with self.assertRaises(NatsError):
with self.assertRaises(ErrNoServers):
options = {
"servers": ["nats://127.0.0.1:4223"],
"close_cb": client.disconnected_cb,
Expand All @@ -278,6 +278,28 @@ def disconnected_cb(self):
yield client.nc.connect(**options)
self.assertFalse(client.disconnected_cb_called)

@tornado.testing.gen_test(timeout=5)
def test_connect_fails_allow_reconnect(self):

class SampleClient():
def __init__(self):
self.nc = Client()
self.disconnected_cb_called = False

def disconnected_cb(self):
self.disconnected_cb_called = True

client = SampleClient()
with self.assertRaises(ErrNoServers):
options = {
"servers": ["nats://127.0.0.1:4223"],
"close_cb": client.disconnected_cb,
"allow_reconnect": True,
"io_loop": self.io_loop
}
yield client.nc.connect(**options)
self.assertFalse(client.disconnected_cb_called)

@tornado.testing.gen_test
def test_subscribe(self):
nc = Client()
Expand Down

0 comments on commit 7a3b997

Please sign in to comment.