Skip to content

Commit

Permalink
Handle non existing result futures on pop (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
raman325 authored Sep 20, 2023
1 parent d1faffe commit 221a038
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 4 deletions.
6 changes: 4 additions & 2 deletions test/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,11 @@ async def receive():
"""Return a websocket message."""
await asyncio.sleep(0)

message = messages.popleft()
if not messages:
try:
message = messages.popleft()
except IndexError:
ws_client.closed = True
return WSMessage(WSMsgType.CLOSED, None, None)

return message

Expand Down
15 changes: 15 additions & 0 deletions test/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -461,3 +461,18 @@ async def test_additional_user_agent_components(client_session, url):
},
}
)


async def test_pop_future_none(client_session, url, driver_ready):
"""Test when a future has been cleared from futures dict, popping still works."""
client = Client(url, client_session)
await client.connect()

assert client.connected

asyncio.create_task(client.listen(driver_ready))

await driver_ready.wait()

with pytest.raises(asyncio.CancelledError):
await client.async_send_command({"command": "some_command"})
2 changes: 1 addition & 1 deletion test/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ def test_connect(client_session, url, ws_client):
main()

assert sys_exit.value.code == 0
assert ws_client.receive.call_count == 4
assert ws_client.receive.call_count == 5
2 changes: 1 addition & 1 deletion zwave_js_server/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ async def async_send_command(
try:
return await future
finally:
self._result_futures.pop(message_id)
self._result_futures.pop(message_id, None)

async def async_send_command_no_wait(
self, message: dict[str, Any], require_schema: int | None = None
Expand Down

0 comments on commit 221a038

Please sign in to comment.