Skip to content

Commit

Permalink
asyncio.timeout context managers
Browse files Browse the repository at this point in the history
  • Loading branch information
taddes committed Mar 8, 2024
1 parent 70c997a commit 93cd596
Showing 1 changed file with 7 additions and 13 deletions.
20 changes: 7 additions & 13 deletions tests/integration/async_push_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,31 +233,27 @@ async def send_notification(
else:
return resp

def get_notification(self, timeout=1):
async def get_notification(self, timeout=1):
"""Get notification."""
if not self.ws:
raise WebSocketException("WebSocket client not available as expected")

orig_timeout = self.ws.time
self.ws.settimeout(timeout)
try:
d = self.ws.recv()
log.debug(f"Recv: {d}")
async with asyncio.timeout(timeout):
d = await self.ws.recv()
log.debug(f"Recv: {d!r}")
return json.loads(d)
except Exception:
return None
finally:
self.ws.settimeout(orig_timeout)

def get_broadcast(self, timeout=1): # pragma: no cover
async def get_broadcast(self, timeout=1): # pragma: no cover
"""Get broadcast."""
if not self.ws:
raise WebSocketException("WebSocket client not available as expected")

orig_timeout = self.ws.gettimeout()
self.ws.settimeout(timeout)
try:
d = self.ws.recv()
async with asyncio.timeout(timeout):
d = await self.ws.recv()
log.debug(f"Recv: {d}")
result = json.loads(d)
# ASK JR
Expand All @@ -266,8 +262,6 @@ def get_broadcast(self, timeout=1): # pragma: no cover
except WebSocketException as ex: # pragma: no cover
log.error(f"Error: {ex}")
return None
finally:
self.ws.settimeout(orig_timeout)

async def ping(self):
"""Test ping."""
Expand Down

0 comments on commit 93cd596

Please sign in to comment.