timeout instead of cancel + minor fixes

This commit is contained in:
Victor Shyba 2019-08-08 04:48:52 -03:00
parent 4a749f6c38
commit 9ee2f30df4
4 changed files with 11 additions and 7 deletions

View file

@ -29,6 +29,7 @@ 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
await self.assertBalance(self.account, '1.1337')
# is it real? are we rich!? let me see this tx...
@ -37,6 +38,7 @@ class ReconnectTests(IntegrationTestCase):
self.ledger.network.client.connection_lost(Exception())
with self.assertRaises((asyncio.TimeoutError, asyncio.CancelledError)):
await d
self.assertGreater(self.ledger.network.client.latency, 1000) # latency skyrockets 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)

View file

@ -74,6 +74,7 @@ class ClientSession(BaseClientSession):
controller.add(request.args)
def connection_lost(self, exc):
log.debug("Connection lost: %s:%d", *self.server)
super().connection_lost(exc)
self.latency = 1 << 32
self._on_disconnect_controller.add(True)
@ -136,10 +137,10 @@ class BaseNetwork:
return self.client and not self.client.is_closing()
def rpc(self, list_or_method, args):
fastest = self.session_pool.fastest_session
if fastest is not None and self.client != fastest:
self.switch_event.set()
if self.is_connected:
fastest = self.session_pool.fastest_session
if self.client != fastest:
self.switch_event.set()
return self.client.send_request(list_or_method, args)
else:
raise ConnectionError("Attempting to send rpc request when connection is not available.")

View file

@ -745,9 +745,10 @@ class JSONRPCConnection(object):
self._protocol = item
return self.receive_message(message)
def cancel_pending_requests(self):
"""Cancel all pending requests."""
exception = CancelledError()
def time_out_pending_requests(self):
"""Times out all pending requests."""
# this used to be CancelledError, but thats confusing as in are we closing the whole sdk or failing?
exception = TimeoutError()
for request, event in self._requests.values():
event.result = exception
event.set()

View file

@ -456,7 +456,7 @@ class RPCSession(SessionBase):
def connection_lost(self, exc):
# Cancel pending requests and message processing
self.connection.cancel_pending_requests()
self.connection.time_out_pending_requests()
super().connection_lost(exc)
# External API