|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
3 | 3 | import asyncio |
| 4 | +import inspect |
4 | 5 | import sys |
5 | | -from typing import TYPE_CHECKING |
| 6 | +import time |
| 7 | +from typing import TYPE_CHECKING, TypeVar, cast, overload |
6 | 8 |
|
7 | 9 | import pytest |
8 | 10 |
|
9 | 11 | if TYPE_CHECKING: |
10 | | - from collections.abc import Callable |
| 12 | + from collections.abc import Awaitable, Callable |
| 13 | + |
| 14 | +T = TypeVar('T') |
11 | 15 |
|
12 | 16 | run_alone_on_mac = pytest.mark.run_alone if sys.platform == 'darwin' else lambda x: x |
13 | 17 |
|
14 | 18 |
|
15 | | -async def wait_for_condition( |
16 | | - condition: Callable[[], bool], |
| 19 | +async def maybe_await(value: Awaitable[T] | T) -> T: |
| 20 | + """Await `value` if it is awaitable, otherwise return it unchanged. |
| 21 | +
|
| 22 | + Lets `poll_until_condition` accept both sync and async callables. |
| 23 | + """ |
| 24 | + if inspect.isawaitable(value): |
| 25 | + return await cast('Awaitable[T]', value) |
| 26 | + return cast('T', value) |
| 27 | + |
| 28 | + |
| 29 | +@overload |
| 30 | +async def poll_until_condition( |
| 31 | + fn: Callable[[], Awaitable[T]], |
| 32 | + condition: Callable[[T], bool] = ..., |
| 33 | + *, |
| 34 | + timeout: float = ..., |
| 35 | + poll_interval: float = ..., |
| 36 | + backoff_factor: float = ..., |
| 37 | +) -> T: ... |
| 38 | +@overload |
| 39 | +async def poll_until_condition( |
| 40 | + fn: Callable[[], T], |
| 41 | + condition: Callable[[T], bool] = ..., |
17 | 42 | *, |
18 | | - timeout: float = 5.0, |
| 43 | + timeout: float = ..., |
| 44 | + poll_interval: float = ..., |
| 45 | + backoff_factor: float = ..., |
| 46 | +) -> T: ... |
| 47 | +async def poll_until_condition( |
| 48 | + fn: Callable[[], Awaitable[T] | T], |
| 49 | + condition: Callable[[T], bool] = bool, |
| 50 | + *, |
| 51 | + timeout: float = 5, |
19 | 52 | poll_interval: float = 0.05, |
20 | | -) -> None: |
21 | | - """Poll `condition` until it returns True, or raise `AssertionError` on timeout. |
| 53 | + backoff_factor: float = 1, |
| 54 | +) -> T: |
| 55 | + """Poll `fn` until `condition(result)` is True or the timeout expires. |
| 56 | +
|
| 57 | + Polls `fn` at `poll_interval`-second intervals until `condition` is satisfied or `timeout` seconds have elapsed. |
| 58 | + Returns the last polled result regardless of whether the condition was met, so the caller can run its own |
| 59 | + assertion. The default condition checks for a truthy result. |
22 | 60 |
|
23 | | - Args: |
24 | | - condition: A callable that returns True when the desired state is reached. |
25 | | - timeout: Maximum time in seconds to wait before raising. |
26 | | - poll_interval: Time in seconds between condition checks. |
| 61 | + Use this instead of a fixed `asyncio.sleep` when waiting for some state to settle (e.g. autoscaling |
| 62 | + concurrency) that may take a variable amount of time. For highly variable waits, pass `backoff_factor` > 1 |
| 63 | + to multiply the interval after each poll, covering a long timeout with few calls. |
27 | 64 | """ |
28 | | - loop = asyncio.get_running_loop() |
29 | | - deadline = loop.time() + timeout |
30 | | - while loop.time() < deadline: |
31 | | - if condition(): |
32 | | - return |
33 | | - await asyncio.sleep(poll_interval) |
34 | | - raise AssertionError(f'Condition not met within {timeout}s') |
| 65 | + deadline = time.monotonic() + timeout |
| 66 | + delay = poll_interval |
| 67 | + result = await maybe_await(fn()) |
| 68 | + while not condition(result): |
| 69 | + remaining = deadline - time.monotonic() |
| 70 | + if remaining <= 0: |
| 71 | + break |
| 72 | + await asyncio.sleep(min(delay, remaining)) |
| 73 | + delay *= backoff_factor |
| 74 | + result = await maybe_await(fn()) |
| 75 | + return result |
0 commit comments