Skip to content

Commit 029b18e

Browse files
committed
CRAI suggestions
1 parent 6867740 commit 029b18e

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

tests/test_discovery.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,17 +13,15 @@
1313
# Helper to load binary fixture
1414
async def _read_binary_fixture(fixture_name: str) -> bytes:
1515
"""Read a binary fixture file."""
16-
# This path is relative to the test file itself.
17-
# Assuming 'fixtures' is a sibling directory to 'tests' and 'airos' modules.
1816
fixture_dir = os.path.join(os.path.dirname(__file__), "../fixtures")
1917
path = os.path.join(fixture_dir, fixture_name)
2018
try:
21-
# Correct usage of asyncio.to_thread for blocking file IO:
22-
# await asyncio.to_thread(...) returns the result of the synchronous call.
23-
# Then, use a regular 'with' statement for the file object.
24-
file_obj = await asyncio.to_thread(open, path, "rb")
25-
with file_obj as f: # Use standard 'with' here for the file object
26-
return f.read()
19+
20+
def _read_file():
21+
with open(path, "rb") as f:
22+
return f.read()
23+
24+
return await asyncio.to_thread(_read_file)
2725
except FileNotFoundError:
2826
pytest.fail(f"Fixture file not found: {path}")
2927
except Exception as e:
@@ -121,10 +119,15 @@ async def test_datagram_received_calls_callback(mock_airos_packet):
121119
protocol = AirosDiscoveryProtocol(mock_callback)
122120
host_ip = "192.168.1.3" # Sender IP
123121

124-
protocol.datagram_received(mock_airos_packet, (host_ip, DISCOVERY_PORT))
122+
with patch("asyncio.create_task") as mock_create_task:
123+
protocol.datagram_received(mock_airos_packet, (host_ip, DISCOVERY_PORT))
124+
125+
# Verify the task was created and get the coroutine
126+
mock_create_task.assert_called_once()
127+
task_coro = mock_create_task.call_args[0][0]
125128

126-
# Give the asyncio task a moment to run
127-
await asyncio.sleep(0.01) # Short delay to allow the task to execute
129+
# Manually await the coroutine to test the callback
130+
await task_coro
128131

129132
mock_callback.assert_called_once()
130133
called_args, _ = mock_callback.call_args

0 commit comments

Comments
 (0)