|
13 | 13 | # Helper to load binary fixture |
14 | 14 | async def _read_binary_fixture(fixture_name: str) -> bytes: |
15 | 15 | """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. |
18 | 16 | fixture_dir = os.path.join(os.path.dirname(__file__), "../fixtures") |
19 | 17 | path = os.path.join(fixture_dir, fixture_name) |
20 | 18 | 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) |
27 | 25 | except FileNotFoundError: |
28 | 26 | pytest.fail(f"Fixture file not found: {path}") |
29 | 27 | except Exception as e: |
@@ -121,10 +119,15 @@ async def test_datagram_received_calls_callback(mock_airos_packet): |
121 | 119 | protocol = AirosDiscoveryProtocol(mock_callback) |
122 | 120 | host_ip = "192.168.1.3" # Sender IP |
123 | 121 |
|
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] |
125 | 128 |
|
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 |
128 | 131 |
|
129 | 132 | mock_callback.assert_called_once() |
130 | 133 | called_args, _ = mock_callback.call_args |
|
0 commit comments