| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import asyncio |
| 4 |
import sys |
| 5 |
import threading |
| 6 |
from typing import TYPE_CHECKING, Final |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from filelock import AsyncFileLock, Timeout |
| 11 |
from tests.async_filelock_cancellation_helpers import ( |
| 12 |
assert_cancellation_message, |
| 13 |
assert_file_lock_state, |
| 14 |
start_file_lock_holder, |
| 15 |
) |
| 16 |
from tests.capability_marks import XFAIL_WITHOUT_COROUTINE_CANCELLATION |
| 17 |
|
| 18 |
if TYPE_CHECKING: |
| 19 |
from pathlib import Path |
| 20 |
from typing import Literal |
| 21 |
|
| 22 |
_UNIX_FLOCK_ONLY: Final[pytest.MarkDecorator] = pytest.mark.skipif( |
| 23 |
sys.platform == "win32", reason="native flock semantics are Unix-only" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
@pytest.mark.parametrize( |
| 28 |
"admission", |
| 29 |
[ |
| 30 |
pytest.param("nonblocking", id="nonblocking"), |
| 31 |
pytest.param("deadline", id="finite-deadline"), |
| 32 |
pytest.param("cancel-check", id="cancel-check"), |
| 33 |
], |
| 34 |
) |
| 35 |
@pytest.mark.asyncio |
| 36 |
@XFAIL_WITHOUT_COROUTINE_CANCELLATION |
| 37 |
async def test_queued_acquire_honors_own_admission_policy( |
| 38 |
tmp_path: Path, admission: Literal["nonblocking", "deadline", "cancel-check"] |
| 39 |
) -> None: |
| 40 |
first_polled = asyncio.Event() |
| 41 |
|
| 42 |
def observe_first_poll() -> bool: |
| 43 |
first_polled.set() |
| 44 |
return False |
| 45 |
|
| 46 |
holder, holder_started, finish_holder = start_file_lock_holder(str(tmp_path / "a")) |
| 47 |
assert await asyncio.to_thread(holder_started.wait, 5) |
| 48 |
lock = AsyncFileLock(tmp_path / "a") |
| 49 |
first_task = asyncio.create_task(lock.acquire(cancel_check=observe_first_poll, poll_interval=0.001)) |
| 50 |
try: |
| 51 |
await first_polled.wait() |
| 52 |
if admission == "nonblocking": |
| 53 |
with pytest.raises(Timeout): |
| 54 |
await lock.acquire(blocking=False) |
| 55 |
elif admission == "deadline": |
| 56 |
with pytest.raises(Timeout): |
| 57 |
await lock.acquire(timeout=0.01) |
| 58 |
else: |
| 59 |
cancel_second = asyncio.Event() |
| 60 |
second_started = asyncio.Event() |
| 61 |
|
| 62 |
async def acquire_second() -> None: |
| 63 |
second_started.set() |
| 64 |
await lock.acquire(cancel_check=cancel_second.is_set, poll_interval=0.001) |
| 65 |
|
| 66 |
second_task = asyncio.create_task(acquire_second()) |
| 67 |
await second_started.wait() |
| 68 |
cancel_second.set() |
| 69 |
with pytest.raises(Timeout): |
| 70 |
await second_task |
| 71 |
assert not first_task.done() |
| 72 |
finally: |
| 73 |
finish_holder.set() |
| 74 |
await asyncio.to_thread(holder.join, 5) |
| 75 |
|
| 76 |
assert not holder.is_alive() |
| 77 |
await first_task |
| 78 |
await lock.release() |
| 79 |
assert (lock.is_locked, lock.lock_counter) == (False, 0) |
| 80 |
assert_file_lock_state(str(tmp_path / "a"), available=True) |
| 81 |
|
| 82 |
|
| 83 |
@_UNIX_FLOCK_ONLY |
| 84 |
@pytest.mark.asyncio |
| 85 |
@XFAIL_WITHOUT_COROUTINE_CANCELLATION |
| 86 |
async def test_queued_acquire_proceeds_after_prior_waiter_cancels(tmp_path: Path) -> None: # pragma: win32 no cover |
| 87 |
hook_started = asyncio.Event() |
| 88 |
finish_hook = threading.Event() |
| 89 |
loop = asyncio.get_running_loop() |
| 90 |
|
| 91 |
def block_hook(_fd: int) -> None: # pragma: win32 no cover |
| 92 |
loop.call_soon_threadsafe(hook_started.set) |
| 93 |
assert finish_hook.wait(timeout=5) |
| 94 |
|
| 95 |
lock = AsyncFileLock(tmp_path / "a", on_acquired=block_hook) |
| 96 |
first_task = asyncio.create_task(lock.acquire()) |
| 97 |
await hook_started.wait() |
| 98 |
second_task, second_started = _start_signaled_acquire(lock) |
| 99 |
await second_started.wait() |
| 100 |
third_task, third_started = _start_signaled_acquire(lock) |
| 101 |
await third_started.wait() |
| 102 |
second_task.cancel("abandon queued acquire") |
| 103 |
try: # pragma: win32 no cover |
| 104 |
with pytest.raises(asyncio.CancelledError) as info: # pragma: win32 no cover |
| 105 |
await second_task |
| 106 |
assert_cancellation_message(info.value, "abandon queued acquire") |
| 107 |
finally: |
| 108 |
finish_hook.set() |
| 109 |
|
| 110 |
await first_task |
| 111 |
await third_task |
| 112 |
assert (lock.is_locked, lock.lock_counter) == (True, 2) |
| 113 |
await lock.release() |
| 114 |
await lock.release() |
| 115 |
assert (lock.is_locked, lock.lock_counter) == (False, 0) |
| 116 |
assert_file_lock_state(str(tmp_path / "a"), available=True) |
| 117 |
|
| 118 |
|
| 119 |
def _start_signaled_acquire(lock: AsyncFileLock) -> tuple[asyncio.Task[None], asyncio.Event]: # pragma: win32 no cover |
| 120 |
started = asyncio.Event() |
| 121 |
|
| 122 |
async def acquire() -> None: # pragma: win32 no cover |
| 123 |
started.set() |
| 124 |
await lock.acquire() |
| 125 |
|
| 126 |
return asyncio.create_task(acquire()), started |