asyncio_pending(future: Awaitable, timeout: float = 0.001) → Awaitable[bool]¶
Gives the given future a chance to complete, and determines if it didn’t.This method is used in tests checking that a future actually depends on somegiven event having happened. The test can assert, before the event, that thefuture is still pending and then assert, after the event, that the futurehas a result.
future – The future that may or may not be able to resolve when given a bit of time.
timeout – The number of seconds to wait for the future. This should generally be a small value (milliseconds) when expecting the future to not resolve, and a large value (seconds) when expecting the future to resolve.
True if the future is still pending after the timeout elapses. False if the future did complete (or fail) or was already completed (or already failed).
>>> import asyncio >>> import pytest >>> @pytest.mark.asyncio ... async def test_completion_only_when_expected(): ... f = asyncio.Future() ... assert await cirq.testing.asyncio_pending(f) ... f.set_result(5) ... assert await f == 5