fix reconnect and improve test

This commit is contained in:
Victor Shyba 2019-06-04 02:34:58 -03:00 committed by Lex Berezhny
parent a14b91cdda
commit 8e43e40810
2 changed files with 18 additions and 17 deletions

View file

@ -37,11 +37,10 @@ class ReconnectTests(IntegrationTestCase):
await self.ledger.network.get_transaction(sendtxid) await self.ledger.network.get_transaction(sendtxid)
async def test_timeout_then_reconnect(self): async def test_timeout_then_reconnect(self):
await self.ledger.stop() await self.conductor.spv_node.stop()
conf = self.ledger.config self.assertFalse(self.ledger.network.is_connected)
self.ledger.config['connect_timeout'] = 1 await self.conductor.spv_node.start(self.conductor.blockchain_node)
self.ledger.config['default_servers'] = [('10.0.0.1', 12)] + list(conf['default_servers']) await self.ledger.network.on_connected.first
await self.ledger.start()
self.assertTrue(self.ledger.network.is_connected) self.assertTrue(self.ledger.network.is_connected)

View file

@ -84,7 +84,9 @@ class BaseNetwork:
except (asyncio.TimeoutError, asyncio.CancelledError) as error: except (asyncio.TimeoutError, asyncio.CancelledError) as error:
if not client.is_closing(): if not client.is_closing():
client.abort() client.abort()
raise error raise
except Exception: # pylint: disable=broad-except
log.exception("Connecting to %s:%d raised an exception:", *server)
futures = [] futures = []
for server in self.config['default_servers']: for server in self.config['default_servers']:
futures.append(__probe(server)) futures.append(__probe(server))
@ -99,27 +101,27 @@ class BaseNetwork:
delay = 0.0 delay = 0.0
connect_timeout = self.config.get('connect_timeout', 6) connect_timeout = self.config.get('connect_timeout', 6)
while True: while True:
self.client = await self.pick_fastest_server(connect_timeout)
connection_string = '{}:{}'.format(*self.client.server)
try: try:
await self.ensure_server_version() self.client = await self.pick_fastest_server(connect_timeout)
log.info("Successfully connected to SPV wallet server: %s", connection_string) if self.is_connected:
self._on_connected_controller.add(True) await self.ensure_server_version()
delay = 0.0 log.info("Successfully connected to SPV wallet server: %s:%d", *self.client.server)
await self.client.on_disconnected.first self._on_connected_controller.add(True)
delay = 0.0
await self.client.on_disconnected.first
except CancelledError: except CancelledError:
self.running = False self.running = False
except asyncio.TimeoutError: 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 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: if not self.running:
return return
elif self.client: elif self.client:
await self.client.close() await self.client.close()
self.client.connection.cancel_pending_requests() self.client.connection.cancel_pending_requests()
await asyncio.sleep(delay) await asyncio.sleep(delay)
delay = min(delay + 1.0, 10.0) delay = min(delay + 1.0, 10.0)
async def stop(self): async def stop(self):
self.running = False self.running = False