From 8e43e408107bfdcc14eda8b3b00c29f8f33647f5 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Tue, 4 Jun 2019 02:34:58 -0300 Subject: [PATCH] fix reconnect and improve test --- .../integration/test_reconnect.py | 9 +++---- torba/client/basenetwork.py | 26 ++++++++++--------- 2 files changed, 18 insertions(+), 17 deletions(-) diff --git a/tests/client_tests/integration/test_reconnect.py b/tests/client_tests/integration/test_reconnect.py index 4a3b36548..0442cd4cd 100644 --- a/tests/client_tests/integration/test_reconnect.py +++ b/tests/client_tests/integration/test_reconnect.py @@ -37,11 +37,10 @@ class ReconnectTests(IntegrationTestCase): await self.ledger.network.get_transaction(sendtxid) async def test_timeout_then_reconnect(self): - await self.ledger.stop() - conf = self.ledger.config - self.ledger.config['connect_timeout'] = 1 - self.ledger.config['default_servers'] = [('10.0.0.1', 12)] + list(conf['default_servers']) - await self.ledger.start() + await self.conductor.spv_node.stop() + self.assertFalse(self.ledger.network.is_connected) + await self.conductor.spv_node.start(self.conductor.blockchain_node) + await self.ledger.network.on_connected.first self.assertTrue(self.ledger.network.is_connected) diff --git a/torba/client/basenetwork.py b/torba/client/basenetwork.py index 35e82974d..6d0f6a9ca 100644 --- a/torba/client/basenetwork.py +++ b/torba/client/basenetwork.py @@ -84,7 +84,9 @@ class BaseNetwork: except (asyncio.TimeoutError, asyncio.CancelledError) as error: if not client.is_closing(): client.abort() - raise error + raise + except Exception: # pylint: disable=broad-except + log.exception("Connecting to %s:%d raised an exception:", *server) futures = [] for server in self.config['default_servers']: futures.append(__probe(server)) @@ -99,27 +101,27 @@ class BaseNetwork: delay = 0.0 connect_timeout = self.config.get('connect_timeout', 6) while True: - self.client = await self.pick_fastest_server(connect_timeout) - connection_string = '{}:{}'.format(*self.client.server) try: - await self.ensure_server_version() - log.info("Successfully connected to SPV wallet server: %s", connection_string) - self._on_connected_controller.add(True) - delay = 0.0 - await self.client.on_disconnected.first + self.client = await self.pick_fastest_server(connect_timeout) + if self.is_connected: + await self.ensure_server_version() + log.info("Successfully connected to SPV wallet server: %s:%d", *self.client.server) + self._on_connected_controller.add(True) + delay = 0.0 + await self.client.on_disconnected.first except CancelledError: self.running = False except asyncio.TimeoutError: - log.warning("Timed out connecting to: %s", connection_string) + log.warning("Timed out while trying to find a server!") except Exception: # pylint: disable=broad-except - log.exception("Connecting to %s raised an exception:", connection_string) + log.exception("Exception while trying to find a server!") if not self.running: return elif self.client: await self.client.close() self.client.connection.cancel_pending_requests() - await asyncio.sleep(delay) - delay = min(delay + 1.0, 10.0) + await asyncio.sleep(delay) + delay = min(delay + 1.0, 10.0) async def stop(self): self.running = False