From f3a163b382ef7dba8c963ba9e0e241fbb78e3d41 Mon Sep 17 00:00:00 2001 From: Victor Shyba Date: Mon, 12 Aug 2019 18:16:53 -0300 Subject: [PATCH] fix names and types --- .../integration/test_wallet_server_sessions.py | 2 +- .../client_tests/integration/test_network.py | 6 +++--- torba/torba/client/basenetwork.py | 16 +++++++++------- 3 files changed, 13 insertions(+), 11 deletions(-) diff --git a/lbry/tests/integration/test_wallet_server_sessions.py b/lbry/tests/integration/test_wallet_server_sessions.py index 5234b990c..451006d48 100644 --- a/lbry/tests/integration/test_wallet_server_sessions.py +++ b/lbry/tests/integration/test_wallet_server_sessions.py @@ -26,7 +26,7 @@ class TestSessionBloat(IntegrationTestCase): self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 1) self.assertFalse(session.is_closing()) await asyncio.sleep(1.1) - with self.assertRaises((asyncio.TimeoutError, asyncio.CancelledError)): + with self.assertRaises(asyncio.TimeoutError): await session.send_request('server.banner', ()) self.assertTrue(session.is_closing()) self.assertEqual(len(self.conductor.spv_node.server.session_mgr.sessions), 0) diff --git a/torba/tests/client_tests/integration/test_network.py b/torba/tests/client_tests/integration/test_network.py index d4a9c5d4b..8d0faed2a 100644 --- a/torba/tests/client_tests/integration/test_network.py +++ b/torba/tests/client_tests/integration/test_network.py @@ -29,16 +29,16 @@ class ReconnectTests(IntegrationTestCase): await asyncio.wait_for(self.on_transaction_id(sendtxid), 1.0) # mempool await self.blockchain.generate(1) await self.on_transaction_id(sendtxid) # confirmed - self.assertLess(self.ledger.network.client.latency, 1) # latency properly set lower, we are fine + self.assertLess(self.ledger.network.client.response_time, 1) # response time properly set lower, we are fine await self.assertBalance(self.account, '1.1337') # is it real? are we rich!? let me see this tx... d = self.ledger.network.get_transaction(sendtxid) # what's that smoke on my ethernet cable? oh no! self.ledger.network.client.connection_lost(Exception()) - with self.assertRaises((asyncio.TimeoutError, asyncio.CancelledError)): + with self.assertRaises(asyncio.TimeoutError): await d - self.assertGreater(self.ledger.network.client.latency, 1000) # latency skyrockets as it failed + self.assertIsNone(self.ledger.network.client.response_time) # response time unknown as it failed # rich but offline? no way, no water, let's retry with self.assertRaisesRegex(ConnectionError, 'connection is not available'): await self.ledger.network.get_transaction(sendtxid) diff --git a/torba/torba/client/basenetwork.py b/torba/torba/client/basenetwork.py index 752c2ed90..39ed1fbdf 100644 --- a/torba/torba/client/basenetwork.py +++ b/torba/torba/client/basenetwork.py @@ -23,13 +23,13 @@ class ClientSession(BaseClientSession): self.bw_limit = self.framer.max_size = self.max_errors = 1 << 32 self.timeout = timeout self.max_seconds_idle = timeout * 2 - self.latency = 1 << 32 + self.response_time: Optional[float] = None self._on_connect_cb = on_connect_callback or (lambda: None) self.trigger_urgent_reconnect = asyncio.Event() @property def available(self): - return not self.is_closing() and self._can_send.is_set() and self.latency < 1 << 32 + return not self.is_closing() and self._can_send.is_set() and self.response_time is not None async def send_request(self, method, args=()): try: @@ -37,13 +37,13 @@ class ClientSession(BaseClientSession): result = await asyncio.wait_for( super().send_request(method, args), timeout=self.timeout ) - self.latency = perf_counter() - start + self.response_time = perf_counter() - start return result except RPCError as e: log.warning("Wallet server returned an error. Code: %s Message: %s", *e.args) raise e except TimeoutError: - self.latency = 1 << 32 + self.response_time = None raise async def ensure_session(self): @@ -56,7 +56,7 @@ class ClientSession(BaseClientSession): await self.create_connection(self.timeout) await self.ensure_server_version() self._on_connect_cb() - if (time() - self.last_send) > self.max_seconds_idle or self.latency == 1 << 32: + if (time() - self.last_send) > self.max_seconds_idle or self.response_time is None: await self.send_request('server.banner') retry_delay = default_delay except (asyncio.TimeoutError, OSError): @@ -84,7 +84,7 @@ class ClientSession(BaseClientSession): def connection_lost(self, exc): log.debug("Connection lost: %s:%d", *self.server) super().connection_lost(exc) - self.latency = 1 << 32 + self.response_time = None self._on_disconnect_controller.add(True) @@ -202,7 +202,9 @@ class SessionPool: def fastest_session(self): if not self.available_sessions: return None - return min([(session.latency, session) for session in self.available_sessions], key=itemgetter(0))[1] + return min( + [(session.response_time, session) for session in self.available_sessions], key=itemgetter(0) + )[1] def start(self, default_servers): callback = self.new_connection_event.set