diff options
author | Patrick Cloke <clokep@users.noreply.github.com> | 2020-09-08 07:26:55 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-08 07:26:55 -0400 |
commit | cef00211c87639b1606f193141318b1dbfc105c4 (patch) | |
tree | 4745aafa7deefb55ebefed4984dc3a091075bd61 /tests/test_utils/__init__.py | |
parent | Rename 'populate_stats_process_rooms_2' background job back to 'populate_stat... (diff) | |
download | synapse-cef00211c87639b1606f193141318b1dbfc105c4.tar.xz |
Allow for make_awaitable's return value to be re-used. (#8261)
Diffstat (limited to 'tests/test_utils/__init__.py')
-rw-r--r-- | tests/test_utils/__init__.py | 13 |
1 files changed, 10 insertions, 3 deletions
diff --git a/tests/test_utils/__init__.py b/tests/test_utils/__init__.py index 508aeba078..a298cc0fd3 100644 --- a/tests/test_utils/__init__.py +++ b/tests/test_utils/__init__.py @@ -17,6 +17,7 @@ """ Utilities for running the unit tests """ +from asyncio import Future from typing import Any, Awaitable, TypeVar TV = TypeVar("TV") @@ -38,6 +39,12 @@ def get_awaitable_result(awaitable: Awaitable[TV]) -> TV: raise Exception("awaitable has not yet completed") -async def make_awaitable(result: Any): - """Create an awaitable that just returns a result.""" - return result +def make_awaitable(result: Any) -> Awaitable[Any]: + """ + Makes an awaitable, suitable for mocking an `async` function. + This uses Futures as they can be awaited multiple times so can be returned + to multiple callers. + """ + future = Future() # type: ignore + future.set_result(result) + return future |