| 1 |
From: Yuri Konotopov <[email protected]> |
| 2 |
Description: Bundle `timeout` method from aiomisc package |
| 3 |
aiomisc package is missing in Debian and we need only |
| 4 |
single method for tests. |
| 5 |
Forwarded: not-needed |
| 6 |
--- caio.orig/tests/test_asyncio_adapter.py |
| 7 |
+++ caio/tests/test_asyncio_adapter.py |
| 8 |
@@ -1,13 +1,51 @@ |
| 9 |
import asyncio |
| 10 |
import hashlib |
| 11 |
import os |
| 12 |
+import sys |
| 13 |
+from functools import wraps |
| 14 |
+from typing import Any, Callable, Coroutine, TypeVar, Union |
| 15 |
from unittest.mock import Mock |
| 16 |
|
| 17 |
-import aiomisc |
| 18 |
import pytest |
| 19 |
|
| 20 |
+if sys.version_info >= (3, 10): |
| 21 |
+ from typing import ParamSpec |
| 22 |
+else: |
| 23 |
+ from typing_extensions import ParamSpec |
| 24 |
+ |
| 25 |
+# timeout() function and typing code above with some imports was copied from |
| 26 |
+# the aiomisc project: |
| 27 |
+# https://github.com/aiokitchen/aiomisc/blob/master/aiomisc/timeout.py |
| 28 |
+# aiomisc is licensed under the MIT license with the copyright: |
| 29 |
+# Copyright © 2023 Dmitry Orlov <[email protected]> |
| 30 |
+ |
| 31 |
+T = TypeVar("T") |
| 32 |
+P = ParamSpec("P") |
| 33 |
+Number = Union[int, float] |
| 34 |
+ |
| 35 |
+def timeout( |
| 36 |
+ value: Number, |
| 37 |
+) -> Callable[ |
| 38 |
+ [Callable[P, Coroutine[Any, Any, T]]], |
| 39 |
+ Callable[P, Coroutine[Any, Any, T]], |
| 40 |
+]: |
| 41 |
+ def decorator( |
| 42 |
+ func: Callable[P, Coroutine[Any, Any, T]], |
| 43 |
+ ) -> Callable[P, Coroutine[Any, Any, T]]: |
| 44 |
+ if not asyncio.iscoroutinefunction(func): |
| 45 |
+ raise TypeError("Function is not a coroutine function") |
| 46 |
+ |
| 47 |
+ @wraps(func) |
| 48 |
+ async def wrap(*args: P.args, **kwargs: P.kwargs) -> T: |
| 49 |
+ return await asyncio.wait_for( |
| 50 |
+ func(*args, **kwargs), |
| 51 |
+ timeout=value, |
| 52 |
+ ) |
| 53 |
+ return wrap |
| 54 |
+ return decorator |
| 55 |
+ |
| 56 |
|
| 57 |
[email protected](5) |
| 58 |
+@timeout(5) |
| 59 |
async def test_linux_uring_asyncio_forwards_context_kwargs(): |
| 60 |
uring_asyncio = pytest.importorskip("caio.linux_uring_asyncio") |
| 61 |
|
| 62 |
@@ -24,10 +62,12 @@ async def test_linux_uring_asyncio_forwa |
| 63 |
assert context.deferred is True |
| 64 |
|
| 65 |
|
| 66 |
[email protected](5) |
| 67 |
+@timeout(5) |
| 68 |
async def test_adapter(tmp_path, async_context): |
| 69 |
context = async_context |
| 70 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 71 |
+ with open( |
| 72 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 73 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 74 |
fd = fp.fileno() |
| 75 |
|
| 76 |
assert await context.read(32, fd, 0) == b"" |
| 77 |
@@ -55,10 +95,12 @@ async def test_adapter(tmp_path, async_c |
| 78 |
assert hashlib.md5(bytes(data)).hexdigest() == expected_hash |
| 79 |
|
| 80 |
|
| 81 |
[email protected](3) |
| 82 |
+@timeout(3) |
| 83 |
async def test_bad_file_descritor(tmp_path, async_context): |
| 84 |
context = async_context |
| 85 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 86 |
+ with open( |
| 87 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 88 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 89 |
fd = fp.fileno() |
| 90 |
|
| 91 |
with pytest.raises((SystemError, OSError, AssertionError, ValueError)): |
| 92 |
@@ -70,9 +112,7 @@ async def test_bad_file_descritor(tmp_pa |
| 93 |
|
| 94 |
@pytest.fixture |
| 95 |
async def asyncio_exception_handler(): |
| 96 |
- handler = Mock( |
| 97 |
- side_effect=lambda _loop, ctx: _loop.default_exception_handler(ctx) |
| 98 |
- ) |
| 99 |
+ handler = Mock(side_effect=lambda _loop, ctx: _loop.default_exception_handler(ctx)) |
| 100 |
event_loop = asyncio.get_running_loop() |
| 101 |
current_handler = event_loop.get_exception_handler() |
| 102 |
event_loop.set_exception_handler(handler=handler) |
| 103 |
@@ -80,12 +120,14 @@ async def asyncio_exception_handler(): |
| 104 |
event_loop.set_exception_handler(current_handler) |
| 105 |
|
| 106 |
|
| 107 |
[email protected](3) |
| 108 |
+@timeout(3) |
| 109 |
async def test_operations_cancel_cleanly( |
| 110 |
tmp_path, async_context, asyncio_exception_handler |
| 111 |
): |
| 112 |
context = async_context |
| 113 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 114 |
+ with open( |
| 115 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 116 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 117 |
fd = fp.fileno() |
| 118 |
|
| 119 |
await context.write(b"\x00", fd, 1024**2 - 1) |
| 120 |
@@ -96,9 +138,7 @@ async def test_operations_cancel_cleanly |
| 121 |
asyncio.create_task(context.read(2**16, fd, 2**16 * i)) |
| 122 |
for i in range(16) |
| 123 |
] |
| 124 |
- _, pending = await asyncio.wait( |
| 125 |
- reads, return_when=asyncio.FIRST_COMPLETED |
| 126 |
- ) |
| 127 |
+ _, pending = await asyncio.wait(reads, return_when=asyncio.FIRST_COMPLETED) |
| 128 |
for read in pending: |
| 129 |
read.cancel() |
| 130 |
if pending: |
| 131 |
@@ -106,14 +146,16 @@ async def test_operations_cancel_cleanly |
| 132 |
asyncio_exception_handler.assert_not_called() |
| 133 |
|
| 134 |
|
| 135 |
[email protected](3) |
| 136 |
+@timeout(3) |
| 137 |
async def test_write_operations_cancel_cleanly( |
| 138 |
tmp_path, async_context, asyncio_exception_handler |
| 139 |
): |
| 140 |
"""Mirrors test_operations_cancel_cleanly but for writes - cancellation |
| 141 |
handling shouldn't be read-specific.""" |
| 142 |
context = async_context |
| 143 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 144 |
+ with open( |
| 145 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 146 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 147 |
fd = fp.fileno() |
| 148 |
|
| 149 |
for _ in range(50): |
| 150 |
@@ -123,9 +165,7 @@ async def test_write_operations_cancel_c |
| 151 |
) |
| 152 |
for i in range(16) |
| 153 |
] |
| 154 |
- _, pending = await asyncio.wait( |
| 155 |
- writes, return_when=asyncio.FIRST_COMPLETED |
| 156 |
- ) |
| 157 |
+ _, pending = await asyncio.wait(writes, return_when=asyncio.FIRST_COMPLETED) |
| 158 |
for write in pending: |
| 159 |
write.cancel() |
| 160 |
if pending: |
| 161 |
@@ -133,8 +173,10 @@ async def test_write_operations_cancel_c |
| 162 |
asyncio_exception_handler.assert_not_called() |
| 163 |
|
| 164 |
|
| 165 |
[email protected](3) |
| 166 |
-async def test_cancel_before_first_step_runs(tmp_path, async_context, asyncio_exception_handler): |
| 167 |
+@timeout(3) |
| 168 |
+async def test_cancel_before_first_step_runs( |
| 169 |
+ tmp_path, async_context, asyncio_exception_handler |
| 170 |
+): |
| 171 |
"""Cancelling right after the op's own first step (submit queued, still |
| 172 |
suspended at `await future`) - covers context.cancel() raising ValueError |
| 173 |
for an op the backend never actually got to submit to the kernel yet.""" |
| 174 |
@@ -149,17 +191,19 @@ async def test_cancel_before_first_step_ |
| 175 |
asyncio_exception_handler.assert_not_called() |
| 176 |
|
| 177 |
|
| 178 |
[email protected](5) |
| 179 |
+@timeout(5) |
| 180 |
async def test_zero_byte_read_and_write(tmp_path, async_context): |
| 181 |
context = async_context |
| 182 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 183 |
+ with open( |
| 184 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 185 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 186 |
fd = fp.fileno() |
| 187 |
|
| 188 |
assert await context.write(b"", fd, 0) == 0 |
| 189 |
assert await context.read(0, fd, 0) == b"" |
| 190 |
|
| 191 |
|
| 192 |
[email protected](5) |
| 193 |
+@timeout(5) |
| 194 |
async def test_partial_read_at_eof(tmp_path, async_context): |
| 195 |
"""Requesting more bytes than the file actually has must return exactly |
| 196 |
what's there, not garbage/padding out to the requested size - this |
| 197 |
@@ -167,7 +211,9 @@ async def test_partial_read_at_eof(tmp_p |
| 198 |
that the fast, no-copy path (used when the buffer is filled |
| 199 |
completely) doesn't.""" |
| 200 |
context = async_context |
| 201 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 202 |
+ with open( |
| 203 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 204 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 205 |
fd = fp.fileno() |
| 206 |
|
| 207 |
payload = b"hello" |
| 208 |
@@ -178,10 +224,12 @@ async def test_partial_read_at_eof(tmp_p |
| 209 |
assert len(data) == len(payload) |
| 210 |
|
| 211 |
|
| 212 |
[email protected](5) |
| 213 |
+@timeout(5) |
| 214 |
async def test_fsync_and_fdsync(tmp_path, async_context): |
| 215 |
context = async_context |
| 216 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 217 |
+ with open( |
| 218 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 219 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 220 |
fd = fp.fileno() |
| 221 |
|
| 222 |
await context.write(b"data", fd, 0) |
| 223 |
@@ -191,10 +239,12 @@ async def test_fsync_and_fdsync(tmp_path |
| 224 |
await context.fdsync(fd) |
| 225 |
|
| 226 |
|
| 227 |
[email protected](15) |
| 228 |
+@timeout(15) |
| 229 |
async def test_large_transfer(tmp_path, async_context): |
| 230 |
context = async_context |
| 231 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 232 |
+ with open( |
| 233 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 234 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 235 |
fd = fp.fileno() |
| 236 |
|
| 237 |
payload = os.urandom(4 * 1024 * 1024) |
| 238 |
@@ -208,10 +258,12 @@ async def test_large_transfer(tmp_path, |
| 239 |
assert hashlib.sha256(bytes(data)).hexdigest() == expected_hash |
| 240 |
|
| 241 |
|
| 242 |
[email protected](5) |
| 243 |
+@timeout(5) |
| 244 |
async def test_write_extends_file_sparsely(tmp_path, async_context): |
| 245 |
context = async_context |
| 246 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 247 |
+ with open( |
| 248 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 249 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 250 |
fd = fp.fileno() |
| 251 |
|
| 252 |
hole_size = 4 * 1024 * 1024 |
| 253 |
@@ -222,7 +274,7 @@ async def test_write_extends_file_sparse |
| 254 |
assert hole == b"\x00" * hole_size |
| 255 |
|
| 256 |
|
| 257 |
[email protected](10) |
| 258 |
+@timeout(10) |
| 259 |
async def test_max_requests_backpressure(tmp_path, async_context_maker): |
| 260 |
"""A tiny max_requests must still let far more concurrent operations |
| 261 |
complete correctly - the asyncio-level semaphore is responsible for |
| 262 |
@@ -230,7 +282,9 @@ async def test_max_requests_backpressure |
| 263 |
configured for, so this must not surface a "queue full"/"ring full" |
| 264 |
error from the backend itself.""" |
| 265 |
async with async_context_maker(max_requests=2) as context: |
| 266 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 267 |
+ with open( |
| 268 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 269 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 270 |
fd = fp.fileno() |
| 271 |
|
| 272 |
chunk = 4096 |
| 273 |
@@ -238,10 +292,7 @@ async def test_max_requests_backpressure |
| 274 |
expected = [bytes([i % 256]) * chunk for i in range(count)] |
| 275 |
|
| 276 |
await asyncio.gather( |
| 277 |
- *[ |
| 278 |
- context.write(expected[i], fd, i * chunk) |
| 279 |
- for i in range(count) |
| 280 |
- ] |
| 281 |
+ *[context.write(expected[i], fd, i * chunk) for i in range(count)] |
| 282 |
) |
| 283 |
|
| 284 |
results = await asyncio.gather( |
| 285 |
@@ -250,7 +301,7 @@ async def test_max_requests_backpressure |
| 286 |
assert results == expected |
| 287 |
|
| 288 |
|
| 289 |
[email protected](10) |
| 290 |
+@timeout(10) |
| 291 |
async def test_concurrent_non_overlapping_chunks(tmp_path, async_context): |
| 292 |
"""Writes distinct, non-overlapping regions concurrently, then reads |
| 293 |
them back concurrently - if any backend's buffer handling ever aliased |
| 294 |
@@ -258,20 +309,17 @@ async def test_concurrent_non_overlappin |
| 295 |
somewhere it shouldn't), this would show up as data from the wrong |
| 296 |
chunk appearing in the wrong place.""" |
| 297 |
context = async_context |
| 298 |
- with open(str(tmp_path / "temp.bin"), "wb+") as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 299 |
+ with open( |
| 300 |
+ str(tmp_path / "temp.bin"), "wb+" |
| 301 |
+ ) as fp: # noqa: ASYNC230 (brief sync setup, not the operation under test) |
| 302 |
fd = fp.fileno() |
| 303 |
|
| 304 |
chunk = 8192 |
| 305 |
count = 32 |
| 306 |
- expected = [ |
| 307 |
- bytes([(i * 7 + 3) % 256]) * chunk for i in range(count) |
| 308 |
- ] |
| 309 |
+ expected = [bytes([(i * 7 + 3) % 256]) * chunk for i in range(count)] |
| 310 |
|
| 311 |
await asyncio.gather( |
| 312 |
- *[ |
| 313 |
- context.write(expected[i], fd, i * chunk) |
| 314 |
- for i in range(count) |
| 315 |
- ] |
| 316 |
+ *[context.write(expected[i], fd, i * chunk) for i in range(count)] |
| 317 |
) |
| 318 |
|
| 319 |
results = await asyncio.gather( |