Skip to content

Commit

Permalink
If we time out on call (or initial connect) because we're disconnecte…
Browse files Browse the repository at this point in the history
…d, raiser ClientDisconnected.
  • Loading branch information
Jim Fulton committed May 26, 2016
1 parent f656c5b commit b79c721
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
10 changes: 8 additions & 2 deletions src/ZEO/asyncio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,13 @@ def thread_done_connecting(future):
self.connected.set_result(None)

def wait_for_result(self, future, timeout):
return future.result(self.timeout if timeout is False else timeout)
try:
return future.result(self.timeout if timeout is False else timeout)
except concurrent.futures.TimeoutError:
if not self.client.ready:
raise ClientDisconnected("timed out waiting for connection")
else:
raise

def call(self, method, *args, timeout=None):
return self.__call(self.call_threadsafe, method, args)
Expand Down Expand Up @@ -781,7 +787,7 @@ def start(self, wait=True):
if self.exception:
raise self.exception
if wait:
self.connected.result(self.timeout)
self.wait_for_result(self.connected, self.timeout)

closed = False
def close(self):
Expand Down
10 changes: 9 additions & 1 deletion src/ZEO/asyncio/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def start(self,

wrapper = mock.Mock()
cache = MemoryCache()
self.set_options(addrs, wrapper, cache, 'TEST', read_only)
self.set_options(addrs, wrapper, cache, 'TEST', read_only, timeout=1)

# We can also provide an event loop. We'll use a testing loop
# so we don't have to actually make any network connection.
Expand Down Expand Up @@ -604,6 +604,14 @@ def test_call_async_from_same_thread(self):
client.call_async_from_same_thread('foo', 1)
self.assertEqual(self.parse(transport.pop()), (0, True, 'foo', (1, )))

def test_ClientDisconnected_on_call_timeout(self):
wrapper, cache, loop, client, protocol, transport, send, respond = (
self.start())
self.wait_for_result = super().wait_for_result
self.assertRaises(ClientDisconnected, self.call, 'foo')
client.ready = False
self.assertRaises(ClientDisconnected, self.call, 'foo')

def unsized(self, data, unpickle=False):
result = []
while data:
Expand Down

0 comments on commit b79c721

Please sign in to comment.