From 9d0b9f0dede66e92d7414a5ff9e33a35a00032f7 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 3 Jun 2019 16:03:20 -0300 Subject: [PATCH] improve server picking tests --- .../integration/test_reconnect.py | 33 ++++++++++++++----- torba/client/basenetwork.py | 1 - 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/tests/client_tests/integration/test_reconnect.py b/tests/client_tests/integration/test_reconnect.py index 8a8153475..1bebdc9d3 100644 --- a/tests/client_tests/integration/test_reconnect.py +++ b/tests/client_tests/integration/test_reconnect.py @@ -1,7 +1,9 @@ import logging import asyncio +from torba.client.basenetwork import BaseNetwork from torba.rpc import RPCSession +from torba.stream import StreamController from torba.testcase import IntegrationTestCase @@ -41,15 +43,28 @@ class ReconnectTests(IntegrationTestCase): await self.ledger.start() self.assertTrue(self.ledger.network.is_connected) - async def test_pick_fastest(self): - # local server that is listening but wont reply + async def _make_fake_server(self, latency=1.0, port=1337): + # local fake server with artificial latency proto = RPCSession() - proto.handle_request = lambda _: asyncio.sleep(10) - server = await self.loop.create_server(lambda: proto, host='127.0.0.1', port=1337) + proto.handle_request = lambda _: asyncio.sleep(latency) + server = await self.loop.create_server(lambda: proto, host='127.0.0.1', port=port) + self.addCleanup(server.close) + + async def test_pick_fastest(self): await self.ledger.stop() - conf = self.ledger.config - self.ledger.config['default_servers'] = [('127.0.0.1', 1337)] + list(conf['default_servers']) + original_servers = self.ledger.config['default_servers'] + original_servers.clear() + for index in reversed(range(4)): # reversed so the slowest is the first + port = 1337 + index + await self._make_fake_server(latency=index, port=port) + original_servers.append(('127.0.0.1', port)) + + fastest = ('127.0.0.1', 1337) + self.ledger.config['default_servers'] = original_servers self.ledger.config['connect_timeout'] = 30 - await asyncio.wait_for(self.ledger.start(), timeout=1) - self.assertTrue(self.ledger.network.is_connected) - self.assertEqual(self.ledger.network.client.server, conf['default_servers'][-1]) + + network = BaseNetwork(self.ledger) + asyncio.ensure_future(network.start()) + await asyncio.wait_for(network.on_connected.first, timeout=1) + self.assertTrue(network.is_connected) + self.assertEqual(network.client.server, fastest) diff --git a/torba/client/basenetwork.py b/torba/client/basenetwork.py index 9fb1891c3..692b6b312 100644 --- a/torba/client/basenetwork.py +++ b/torba/client/basenetwork.py @@ -101,7 +101,6 @@ class BaseNetwork: self.client = await self.pick_fastest_server(connect_timeout) connection_string = '{}:{}'.format(*self.client.server) try: - await self.client.create_connection(connect_timeout) await self.ensure_server_version() log.info("Successfully connected to SPV wallet server: %s", connection_string) self._on_connected_controller.add(True)