| 1 |
import tempfile |
| 2 |
import threading |
| 3 |
import time |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from caio import python_aio |
| 8 |
|
| 9 |
|
| 10 |
def test_submit_without_callback_completes_normally(): |
| 11 |
"""set_callback() is optional - a missing callback must be a normal |
| 12 |
case, not something that trips up the result-handler thread.""" |
| 13 |
with tempfile.NamedTemporaryFile() as f: |
| 14 |
ctx = python_aio.Context() |
| 15 |
op = python_aio.Operation.write(b"hello", f.fileno(), 0) |
| 16 |
ctx.submit(op) |
| 17 |
|
| 18 |
deadline = time.monotonic() + 5.0 |
| 19 |
while ctx._in_progress and time.monotonic() < deadline: |
| 20 |
time.sleep(0.001) |
| 21 |
|
| 22 |
assert op.get_value() == 5 |
| 23 |
|
| 24 |
|
| 25 |
def test_raising_callback_does_not_kill_the_result_handler_thread(): |
| 26 |
"""A callback that raises must not take down ThreadPool's single |
| 27 |
result-handler thread - if it did, every operation submitted after the |
| 28 |
raising one would never have its result/callback delivered.""" |
| 29 |
with tempfile.NamedTemporaryFile() as f: |
| 30 |
ctx = python_aio.Context() |
| 31 |
|
| 32 |
unraisable = [] |
| 33 |
original_hook = threading.excepthook |
| 34 |
threading.excepthook = unraisable.append |
| 35 |
try: |
| 36 |
failing_op = python_aio.Operation.write(b"first", f.fileno(), 0) |
| 37 |
failing_op.set_callback(lambda _: 1 / 0) |
| 38 |
ctx.submit(failing_op) |
| 39 |
|
| 40 |
deadline = time.monotonic() + 5.0 |
| 41 |
while failing_op.written == 0 and time.monotonic() < deadline: |
| 42 |
time.sleep(0.001) |
| 43 |
|
| 44 |
assert len(unraisable) == 1 |
| 45 |
assert unraisable[0].exc_type is ZeroDivisionError |
| 46 |
|
| 47 |
results = [] |
| 48 |
following_op = python_aio.Operation.write(b"second", f.fileno(), 5) |
| 49 |
following_op.set_callback(results.append) |
| 50 |
ctx.submit(following_op) |
| 51 |
|
| 52 |
deadline = time.monotonic() + 5.0 |
| 53 |
while not results and time.monotonic() < deadline: |
| 54 |
time.sleep(0.001) |
| 55 |
|
| 56 |
assert results == [6] |
| 57 |
finally: |
| 58 |
threading.excepthook = original_hook |
| 59 |
|
| 60 |
|
| 61 |
def test_max_requests_one_never_admits_a_second_concurrent_submit(): |
| 62 |
"""The capacity check used to compare with `>` instead of `>=`, so |
| 63 |
max_requests=1 silently admitted a second in-flight operation - simulate |
| 64 |
"one already in flight" directly rather than racing a real background |
| 65 |
job, since a real read/write completes far too fast to reliably catch |
| 66 |
the window.""" |
| 67 |
ctx = python_aio.Context(max_requests=1, pool_size=1) |
| 68 |
ctx._in_progress = 1 |
| 69 |
with pytest.raises(RuntimeError): |
| 70 |
ctx.submit(python_aio.Operation.fsync(0)) |
| 71 |
|
| 72 |
|
| 73 |
def test_bounds_are_validated_at_construction(): |
| 74 |
with pytest.raises(ValueError): |
| 75 |
python_aio.Context(pool_size=0) |
| 76 |
|
| 77 |
with pytest.raises(ValueError): |
| 78 |
python_aio.Context(max_requests=0) |
| 79 |
|
| 80 |
with pytest.raises(ValueError): |
| 81 |
python_aio.Context(pool_size=python_aio.Context.MAX_POOL_SIZE) |
| 82 |
|
| 83 |
|
| 84 |
def test_set_callback_rejects_non_callable(): |
| 85 |
op = python_aio.Operation.fsync(0) |
| 86 |
with pytest.raises(ValueError): |
| 87 |
op.set_callback("not callable") |
| 88 |
|
| 89 |
|
| 90 |
def test_close_is_idempotent(): |
| 91 |
ctx = python_aio.Context() |
| 92 |
ctx.close() |
| 93 |
ctx.close() |
| 94 |
with pytest.raises(RuntimeError): |
| 95 |
ctx.submit(python_aio.Operation.fsync(0)) |
| 96 |
|
| 97 |
|
| 98 |
def test_scheduling_failure_rolls_back_the_reserved_slot(): |
| 99 |
"""If apply_async() itself raises after the capacity slot was already |
| 100 |
reserved (e.g. a concurrent close() tore down the underlying pool |
| 101 |
between the capacity check and the actual scheduling call), the slot |
| 102 |
must be given back - otherwise _in_progress stays permanently inflated |
| 103 |
even though no job was ever actually running.""" |
| 104 |
ctx = python_aio.Context(max_requests=1) |
| 105 |
ctx.pool.close() # underlying pool gone, but ctx._state is still OPEN |
| 106 |
|
| 107 |
with pytest.raises(Exception): # noqa: B017 (whatever ThreadPool itself raises) |
| 108 |
ctx.submit(python_aio.Operation.fsync(0)) |
| 109 |
|
| 110 |
assert ctx._in_progress == 0 |