Poll predicate until True or timeout. Replaces brittle ``asyncio.sleep`` waits. Used in unit tests where we dispatch an event and need to wait for the consumer coroutine to invoke a handler and (sometimes) for the handler to issue an RPC that our mock captures. Polling at 5ms means fast
(predicate: Callable[[], bool], timeout: float = 2.0)
| 26 | |
| 27 | |
| 28 | async def _wait_for(predicate: Callable[[], bool], timeout: float = 2.0) -> None: |
| 29 | """Poll predicate until True or timeout. Replaces brittle ``asyncio.sleep`` waits. |
| 30 | |
| 31 | Used in unit tests where we dispatch an event and need to wait for the consumer |
| 32 | coroutine to invoke a handler and (sometimes) for the handler to issue an RPC |
| 33 | that our mock captures. Polling at 5ms means fast machines exit quickly while |
| 34 | slow machines still get up to ``timeout`` seconds before the test fails. |
| 35 | """ |
| 36 | deadline = asyncio.get_event_loop().time() + timeout |
| 37 | while not predicate(): |
| 38 | if asyncio.get_event_loop().time() >= deadline: |
| 39 | raise AssertionError(f"Condition not met within {timeout}s") |
| 40 | await asyncio.sleep(0.005) |
| 41 | |
| 42 | |
| 43 | # ============================================================================ |
no test coverage detected
searching dependent graphs…