| 1 |
""" |
| 2 |
Behavior specific to thread_aio's own native worker-thread-pool design - |
| 3 |
not a bug pattern shared with the other backends, so these don't belong in |
| 4 |
the cross-backend parametrized suite. Skipped outright wherever thread_aio |
| 5 |
itself isn't available. |
| 6 |
""" |
| 7 |
import threading |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from caio import thread_aio |
| 12 |
|
| 13 |
if thread_aio is None: |
| 14 |
pytest.skip("thread_aio backend not available on this platform", allow_module_level=True) |
| 15 |
|
| 16 |
|
| 17 |
def test_queue_overflow_allows_retry_with_original_data(tmp_path): |
| 18 |
"""An operation rejected because the worker pool's queue is full must |
| 19 |
remain fully retryable afterward - not permanently stuck (as "already |
| 20 |
in progress" forever) and not silently missing its original payload on |
| 21 |
the later, successful resubmit. |
| 22 |
|
| 23 |
Submits 5 ops at once with capacity=1 (not just 2): the single worker |
| 24 |
thread *can* race to dequeue the very first one almost immediately |
| 25 |
(dequeuing is a plain C-side queue pop, not gated by the GIL at all) - |
| 26 |
but it can't dequeue a second one until the first fully *completes*, |
| 27 |
which needs the GIL this whole submit() call holds throughout. So at |
| 28 |
most one op can ever be raced away like that, making the LAST op in a |
| 29 |
5-op batch guaranteed to overflow regardless of exactly how that race |
| 30 |
resolves for the first one. |
| 31 |
""" |
| 32 |
with open(str(tmp_path / "temp.bin"), "wb+") as f: |
| 33 |
fd = f.fileno() |
| 34 |
ctx = thread_aio.Context(max_requests=1, pool_size=1) |
| 35 |
|
| 36 |
payload = b"hello" |
| 37 |
ops = [thread_aio.Operation.write(payload, fd, i * len(payload)) for i in range(5)] |
| 38 |
|
| 39 |
# ops[0] is deterministically the very first one checked, so it's |
| 40 |
# always accepted (capacity=1, nothing else has run yet) - |
| 41 |
# retrying the rejected one only has anywhere to go once this one |
| 42 |
# actually finishes and the worker is free again. |
| 43 |
first_done = threading.Event() |
| 44 |
ops[0].set_callback(lambda _r: first_done.set()) |
| 45 |
|
| 46 |
with pytest.raises(RuntimeError): |
| 47 |
ctx.submit(*ops) |
| 48 |
|
| 49 |
assert first_done.wait(timeout=30.0), "the first op should have been accepted and completed" |
| 50 |
|
| 51 |
rejected = ops[-1] |
| 52 |
|
| 53 |
done = threading.Event() |
| 54 |
rejected.set_callback(lambda _r: done.set()) |
| 55 |
resubmitted = ctx.submit(rejected) |
| 56 |
assert resubmitted == 1, "a queue-rejected operation must not be permanently stuck" |
| 57 |
assert done.wait(timeout=30.0), "retried operation must actually run" |
| 58 |
assert rejected.result == len(payload), f"expected a full write, got result={rejected.result}" |
| 59 |
|
| 60 |
with open(str(tmp_path / "temp.bin"), "rb") as rf: |
| 61 |
rf.seek(4 * len(payload)) |
| 62 |
assert rf.read(len(payload)) == payload, ( |
| 63 |
"resubmitted operation must write its ORIGINAL payload, not lost/empty data" |
| 64 |
) |