| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import asyncio |
| 4 |
from concurrent.futures import Future as ConcurrentFuture |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from filelock._async import _AsyncTransitionGate |
| 9 |
from tests.capability_marks import XFAIL_WITHOUT_COROUTINE_CANCELLATION |
| 10 |
|
| 11 |
|
| 12 |
@pytest.mark.asyncio |
| 13 |
async def test_hold_waits_for_a_predecessor_before_entering() -> None: |
| 14 |
gate = _AsyncTransitionGate() |
| 15 |
order: list[str] = [] |
| 16 |
first_holding = asyncio.Event() |
| 17 |
release_first = asyncio.Event() |
| 18 |
|
| 19 |
async def first() -> None: |
| 20 |
async with gate.hold(): |
| 21 |
first_holding.set() |
| 22 |
order.append("first-in") |
| 23 |
await release_first.wait() |
| 24 |
order.append("first-out") |
| 25 |
|
| 26 |
async def second() -> None: |
| 27 |
async with gate.hold(): |
| 28 |
order.append("second-in") |
| 29 |
|
| 30 |
first_task = asyncio.create_task(first()) |
| 31 |
await first_holding.wait() |
| 32 |
second_task = asyncio.create_task(second()) |
| 33 |
await asyncio.sleep(0.05) # second parks on the predecessor instead of entering |
| 34 |
assert order == ["first-in"] |
| 35 |
|
| 36 |
release_first.set() |
| 37 |
await asyncio.gather(first_task, second_task) |
| 38 |
assert order == ["first-in", "first-out", "second-in"] |
| 39 |
|
| 40 |
|
| 41 |
@pytest.mark.asyncio |
| 42 |
@XFAIL_WITHOUT_COROUTINE_CANCELLATION |
| 43 |
async def test_hold_canceled_while_waiting_lets_the_predecessor_free_the_ticket() -> None: |
| 44 |
gate = _AsyncTransitionGate() |
| 45 |
first_holding = asyncio.Event() |
| 46 |
release_first = asyncio.Event() |
| 47 |
|
| 48 |
async def first() -> None: |
| 49 |
async with gate.hold(): |
| 50 |
first_holding.set() |
| 51 |
await release_first.wait() |
| 52 |
|
| 53 |
async def second() -> None: |
| 54 |
async with gate.hold(): # never reached: canceled while waiting on first |
| 55 |
pass # pragma: no cover # the hold body never runs: the waiter is canceled while parked in hold() |
| 56 |
|
| 57 |
first_task = asyncio.create_task(first()) |
| 58 |
await first_holding.wait() |
| 59 |
second_task = asyncio.create_task(second()) |
| 60 |
await asyncio.sleep(0.05) # second is parked on the predecessor |
| 61 |
second_task.cancel() |
| 62 |
with pytest.raises(asyncio.CancelledError): |
| 63 |
await second_task |
| 64 |
|
| 65 |
release_first.set() |
| 66 |
await first_task |
| 67 |
|
| 68 |
# The canceled waiter registered a callback so the predecessor frees its abandoned ticket; a fresh holder must |
| 69 |
# then enter without waiting on a tail that no coroutine will ever leave. |
| 70 |
entered = asyncio.Event() |
| 71 |
|
| 72 |
async def third() -> None: |
| 73 |
async with gate.hold(): |
| 74 |
entered.set() |
| 75 |
|
| 76 |
await asyncio.wait_for(third(), timeout=1) |
| 77 |
assert entered.is_set() |
| 78 |
|
| 79 |
|
| 80 |
@pytest.mark.asyncio |
| 81 |
@pytest.mark.timeout(5) |
| 82 |
async def test_wait_for_predecessor_returns_immediately_when_already_done() -> None: |
| 83 |
predecessor: ConcurrentFuture[None] = ConcurrentFuture() |
| 84 |
predecessor.set_result(None) |
| 85 |
|
| 86 |
await _AsyncTransitionGate._wait_for_predecessor( |
| 87 |
predecessor, blocking=True, cancel_check=None, deadline=None, poll_interval=0.01 |
| 88 |
) |
| 89 |
|
| 90 |
assert predecessor.done() |