From: Yuri Konotopov 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,51 @@ import asyncio import hashlib import os +import sys +from functools import wraps +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 + +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_linux_uring_asyncio_forwards_context_kwargs(): uring_asyncio = pytest.importorskip("caio.linux_uring_asyncio") @@ -24,10 +62,12 @@ async def test_linux_uring_asyncio_forwa assert context.deferred is True -@aiomisc.timeout(5) +@timeout(5) async def test_adapter(tmp_path, async_context): context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() assert await context.read(32, fd, 0) == b"" @@ -55,10 +95,12 @@ 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): context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() with pytest.raises((SystemError, OSError, AssertionError, ValueError)): @@ -70,9 +112,7 @@ async def test_bad_file_descritor(tmp_pa @pytest.fixture async def asyncio_exception_handler(): - handler = Mock( - side_effect=lambda _loop, ctx: _loop.default_exception_handler(ctx) - ) + handler = Mock(side_effect=lambda _loop, ctx: _loop.default_exception_handler(ctx)) event_loop = asyncio.get_running_loop() current_handler = event_loop.get_exception_handler() event_loop.set_exception_handler(handler=handler) @@ -80,12 +120,14 @@ async def asyncio_exception_handler(): event_loop.set_exception_handler(current_handler) -@aiomisc.timeout(3) +@timeout(3) async def test_operations_cancel_cleanly( tmp_path, async_context, asyncio_exception_handler ): context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() await context.write(b"\x00", fd, 1024**2 - 1) @@ -96,9 +138,7 @@ async def test_operations_cancel_cleanly asyncio.create_task(context.read(2**16, fd, 2**16 * i)) for i in range(16) ] - _, pending = await asyncio.wait( - reads, return_when=asyncio.FIRST_COMPLETED - ) + _, pending = await asyncio.wait(reads, return_when=asyncio.FIRST_COMPLETED) for read in pending: read.cancel() if pending: @@ -106,14 +146,16 @@ async def test_operations_cancel_cleanly asyncio_exception_handler.assert_not_called() -@aiomisc.timeout(3) +@timeout(3) async def test_write_operations_cancel_cleanly( tmp_path, async_context, asyncio_exception_handler ): """Mirrors test_operations_cancel_cleanly but for writes - cancellation handling shouldn't be read-specific.""" context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() for _ in range(50): @@ -123,9 +165,7 @@ async def test_write_operations_cancel_c ) for i in range(16) ] - _, pending = await asyncio.wait( - writes, return_when=asyncio.FIRST_COMPLETED - ) + _, pending = await asyncio.wait(writes, return_when=asyncio.FIRST_COMPLETED) for write in pending: write.cancel() if pending: @@ -133,8 +173,10 @@ async def test_write_operations_cancel_c asyncio_exception_handler.assert_not_called() -@aiomisc.timeout(3) -async def test_cancel_before_first_step_runs(tmp_path, async_context, asyncio_exception_handler): +@timeout(3) +async def test_cancel_before_first_step_runs( + tmp_path, async_context, asyncio_exception_handler +): """Cancelling right after the op's own first step (submit queued, still suspended at `await future`) - covers context.cancel() raising ValueError for an op the backend never actually got to submit to the kernel yet.""" @@ -149,17 +191,19 @@ async def test_cancel_before_first_step_ asyncio_exception_handler.assert_not_called() -@aiomisc.timeout(5) +@timeout(5) async def test_zero_byte_read_and_write(tmp_path, async_context): context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() assert await context.write(b"", fd, 0) == 0 assert await context.read(0, fd, 0) == b"" -@aiomisc.timeout(5) +@timeout(5) async def test_partial_read_at_eof(tmp_path, async_context): """Requesting more bytes than the file actually has must return exactly what's there, not garbage/padding out to the requested size - this @@ -167,7 +211,9 @@ async def test_partial_read_at_eof(tmp_p that the fast, no-copy path (used when the buffer is filled completely) doesn't.""" context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() payload = b"hello" @@ -178,10 +224,12 @@ async def test_partial_read_at_eof(tmp_p assert len(data) == len(payload) -@aiomisc.timeout(5) +@timeout(5) async def test_fsync_and_fdsync(tmp_path, async_context): context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() await context.write(b"data", fd, 0) @@ -191,10 +239,12 @@ async def test_fsync_and_fdsync(tmp_path await context.fdsync(fd) -@aiomisc.timeout(15) +@timeout(15) async def test_large_transfer(tmp_path, async_context): context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() payload = os.urandom(4 * 1024 * 1024) @@ -208,10 +258,12 @@ async def test_large_transfer(tmp_path, assert hashlib.sha256(bytes(data)).hexdigest() == expected_hash -@aiomisc.timeout(5) +@timeout(5) async def test_write_extends_file_sparsely(tmp_path, async_context): context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() hole_size = 4 * 1024 * 1024 @@ -222,7 +274,7 @@ async def test_write_extends_file_sparse assert hole == b"\x00" * hole_size -@aiomisc.timeout(10) +@timeout(10) async def test_max_requests_backpressure(tmp_path, async_context_maker): """A tiny max_requests must still let far more concurrent operations complete correctly - the asyncio-level semaphore is responsible for @@ -230,7 +282,9 @@ async def test_max_requests_backpressure configured for, so this must not surface a "queue full"/"ring full" error from the backend itself.""" async with async_context_maker(max_requests=2) as context: - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() chunk = 4096 @@ -238,10 +292,7 @@ async def test_max_requests_backpressure expected = [bytes([i % 256]) * chunk for i in range(count)] await asyncio.gather( - *[ - context.write(expected[i], fd, i * chunk) - for i in range(count) - ] + *[context.write(expected[i], fd, i * chunk) for i in range(count)] ) results = await asyncio.gather( @@ -250,7 +301,7 @@ async def test_max_requests_backpressure assert results == expected -@aiomisc.timeout(10) +@timeout(10) async def test_concurrent_non_overlapping_chunks(tmp_path, async_context): """Writes distinct, non-overlapping regions concurrently, then reads them back concurrently - if any backend's buffer handling ever aliased @@ -258,20 +309,17 @@ async def test_concurrent_non_overlappin somewhere it shouldn't), this would show up as data from the wrong chunk appearing in the wrong place.""" context = async_context - with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) + with open( + str(tmp_path / "temp.bin"), "wb+" + ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) fd = fp.fileno() chunk = 8192 count = 32 - expected = [ - bytes([(i * 7 + 3) % 256]) * chunk for i in range(count) - ] + expected = [bytes([(i * 7 + 3) % 256]) * chunk for i in range(count)] await asyncio.gather( - *[ - context.write(expected[i], fd, i * chunk) - for i in range(count) - ] + *[context.write(expected[i], fd, i * chunk) for i in range(count)] ) results = await asyncio.gather(