1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
|
From: Yuri Konotopov <ykonotopov@gnome.org>
Description: Bundle `timeout` method from aiomisc package
aiomisc package is missing in Debian and we need only
single method for tests.
Forwarded: not-needed
--- caio.orig/tests/test_asyncio_adapter.py
+++ caio/tests/test_asyncio_adapter.py
@@ -1,13 +1,50 @@
import asyncio
+from functools import wraps
import hashlib
import os
+import sys
+from typing import Any, Callable, Coroutine, TypeVar, Union
from unittest.mock import Mock
-import aiomisc
import pytest
+if sys.version_info >= (3, 10):
+ from typing import ParamSpec
+else:
+ from typing_extensions import ParamSpec
+
+# timeout() function and typing code above with some imports was copied from
+# the aiomisc project:
+# https://github.com/aiokitchen/aiomisc/blob/master/aiomisc/timeout.py
+# aiomisc is licensed under the MIT license with the copyright:
+# Copyright © 2023 Dmitry Orlov <me@mosquito.su>
+
+T = TypeVar("T")
+P = ParamSpec("P")
+Number = Union[int, float]
+
+def timeout(
+ value: Number,
+) -> Callable[
+ [Callable[P, Coroutine[Any, Any, T]]],
+ Callable[P, Coroutine[Any, Any, T]],
+]:
+ def decorator(
+ func: Callable[P, Coroutine[Any, Any, T]],
+ ) -> Callable[P, Coroutine[Any, Any, T]]:
+ if not asyncio.iscoroutinefunction(func):
+ raise TypeError("Function is not a coroutine function")
+
+ @wraps(func)
+ async def wrap(*args: P.args, **kwargs: P.kwargs) -> T:
+ return await asyncio.wait_for(
+ func(*args, **kwargs),
+ timeout=value,
+ )
+ return wrap
+ return decorator
-@aiomisc.timeout(5)
+@timeout(5)
async def test_adapter(tmp_path, async_context_maker):
async with async_context_maker() as context:
with open(str(tmp_path / "temp.bin"), "wb+") as fp:
@@ -38,7 +75,7 @@ async def test_adapter(tmp_path, async_c
assert hashlib.md5(bytes(data)).hexdigest() == expected_hash
-@aiomisc.timeout(3)
+@timeout(3)
async def test_bad_file_descritor(tmp_path, async_context_maker):
async with async_context_maker() as context:
with open(str(tmp_path / "temp.bin"), "wb+") as fp:
@@ -62,7 +99,7 @@ async def asyncio_exception_handler(even
event_loop.set_exception_handler(current_handler)
-@aiomisc.timeout(3)
+@timeout(3)
async def test_operations_cancel_cleanly(
tmp_path, async_context_maker, asyncio_exception_handler
):
|