Skip to content

Commit

Permalink
docstrings for clearer documentation of client logic for sending
Browse files Browse the repository at this point in the history
  • Loading branch information
taddes committed Mar 9, 2024
1 parent 93cd596 commit 474cba3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 14 deletions.
29 changes: 21 additions & 8 deletions tests/integration/async_push_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ async def unregister(self, chid) -> Any:
return result

async def delete_notification(self, channel, message=None, status=204) -> httpx.Response:
"""Delete notification."""
"""Sender (non-client) notification delete.
From perspective of sender, not the client. Implements HTTP client to interact with
notification.
"""
messages = self.messages[channel]
if not message:
message = random.choice(messages)
Expand All @@ -178,7 +181,11 @@ async def send_notification(
topic: str | None = None,
headers: dict = {},
):
"""Send notification."""
"""Sender (not-client) sent notification.
Not part of responsibility of client but a subscribed sender.
Calling from PushTestClient provides introspection of values in
both client interface and the sender.
"""
if not channel:
channel = random.choice(list(self.channels.keys()))

Expand Down Expand Up @@ -227,14 +234,20 @@ async def send_notification(
self.messages[channel].append(location)
else:
self.messages[channel] = [location]
# Pull the notification if connected
# Pull the sent notification immediately if connected.
# Calls `get_notification` to get response from websocket.
if self.ws and self.ws.is_client: # check back on this after integration
return object.__getattribute__(self, "get_notification")(timeout)
else:
return resp

async def get_notification(self, timeout=1):
"""Get notification."""
"""Get most recent notification from websocket server.
Typically called after a `send_notification` is sent from client to server.
Method to recieve response from websocket.
Includes ability to define a timeout to simulate latency.
"""
if not self.ws:
raise WebSocketException("WebSocket client not available as expected")

Expand All @@ -256,15 +269,14 @@ async def get_broadcast(self, timeout=1): # pragma: no cover
d = await self.ws.recv()
log.debug(f"Recv: {d}")
result = json.loads(d)
# ASK JR
# assert result.get("messageType") == ClientMessageType.BROADCAST.value
assert result.get("messageType") == ClientMessageType.BROADCAST.value
return result
except WebSocketException as ex: # pragma: no cover
log.error(f"Error: {ex}")
return None

async def ping(self):
"""Test ping."""
"""Test websocket ping."""
if not self.ws:
raise Exception("WebSocket client not available as expected.")

Expand Down Expand Up @@ -305,10 +317,11 @@ async def send_bad_data(self) -> None:
"""
if not self.ws:
raise WebSocketException("WebSocket client not available as expected.")

await self.ws.send("bad-data")

async def wait_for(self, func) -> None:
"""Wait several seconds for a function to return True"""
"""Wait several seconds for a function to return True."""
# This function currently not used for anything so may be removable.
# However, it may have had historic value when dealing with latency.
times = 0
Expand Down
25 changes: 19 additions & 6 deletions tests/integration/push_test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ def unregister(self, chid) -> Any:
return result

def delete_notification(self, channel, message=None, status=204) -> httpx.Response:
"""Delete notification."""
"""Sender (non-client) notification delete.
From perspective of sender, not the client. Implements HTTP client to interact with
notification.
"""
messages = self.messages[channel]
if not message:
message = random.choice(messages)
Expand All @@ -186,7 +189,11 @@ def send_notification(
topic: str | None = None,
headers: dict = {},
):
"""Send notification."""
"""Sender (not-client) sent notification.
Not part of responsibility of client but a subscribed sender.
Calling from PushTestClient provides introspection of values in
both client interface and the sender.
"""
if not channel:
channel = random.choice(list(self.channels.keys()))

Expand Down Expand Up @@ -232,14 +239,20 @@ def send_notification(
self.messages[channel].append(location)
else:
self.messages[channel] = [location]
# Pull the notification if connected
# Pull the sent notification immediately if connected.
# Calls `get_notification` to get response from websocket.
if self.ws and self.ws.connected:
return object.__getattribute__(self, "get_notification")(timeout)
else:
return resp

def get_notification(self, timeout=1):
"""Get notification."""
"""Get most recent notification from websocket server.
Typically called after a `send_notification` is sent from client to server.
Method to recieve response from websocket.
Includes ability to define a timeout to simulate latency.
"""
if not self.ws:
raise Exception("WebSocket client not available as expected")

Expand All @@ -265,8 +278,7 @@ def get_broadcast(self, timeout=1): # pragma: no cover
d = self.ws.recv()
log.debug(f"Recv: {d}")
result = json.loads(d)
# ASK JR
# assert result.get("messageType") == ClientMessageType.BROADCAST.value
assert result.get("messageType") == ClientMessageType.BROADCAST.value
return result
except Exception as ex: # pragma: no cover
log.error(f"Error: {ex}")
Expand Down Expand Up @@ -317,6 +329,7 @@ def send_bad_data(self) -> None:
"""
if not self.ws:
raise Exception("WebSocket client not available as expected.")

self.ws.send("bad-data")

def wait_for(self, func):
Expand Down

0 comments on commit 474cba3

Please sign in to comment.