From e8517513dde8b201c301989b5869aac153c96052 Mon Sep 17 00:00:00 2001 From: Waldemar Quevedo Date: Tue, 4 Oct 2016 14:14:16 -0700 Subject: [PATCH] Fix reconnection attempts count after first connect --- nats/io/client.py | 3 ++- tests/client_test.py | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/nats/io/client.py b/nats/io/client.py index 487ae75..5901d36 100644 --- a/nats/io/client.py +++ b/nats/io/client.py @@ -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 @@ -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 diff --git a/tests/client_test.py b/tests/client_test.py index ca715b4..b73f0a3 100644 --- a/tests/client_test.py +++ b/tests/client_test.py @@ -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, @@ -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()