Debian Codegraph @

tests/test_strict_soft_paths.py

raw
1 from __future__ import annotations
2
3 import asyncio
4 import os
5 import threading
6 from concurrent.futures import ThreadPoolExecutor
7 from contextlib import suppress
8 from functools import partial
9 from typing import TYPE_CHECKING, Final
10
11 import pytest
12
13 from filelock import AsyncStrictSoftFileLock, StrictSoftFileLock, Timeout
14 from tests.capability_marks import NEEDS_PARENT_SYMLINK_COLLAPSE, NEEDS_SYMLINK
15
16 if TYPE_CHECKING:
17 from pathlib import Path
18
19 _STRICT_SENTINEL: Final[bytes] = b"1\nfilelock-strict-v1\x00\n0\n"
20
21
22 # Windows resolves a lock's parent with abspath, so a symlinked parent stays a distinct key and never collapses.
23
24
25 pytestmark = pytest.mark.requires_hard_links
26
27
28 def test_strict_soft_relative_release_uses_acquisition_directory(
29 working_directories: tuple[Path, Path],
30 ) -> None:
31 first, second = working_directories
32 lock = StrictSoftFileLock("resource.lock")
33 lock.acquire()
34 assert tuple(claim.state for claim in StrictSoftFileLock(first / "resource.lock").claims) == ("held", "intent")
35
36 os.chdir(second)
37 lock.release()
38
39 assert (
40 StrictSoftFileLock(first / "resource.lock").claims,
41 StrictSoftFileLock(second / "resource.lock").claims,
42 ) == ((), ())
43
44
45 @pytest.mark.asyncio
46 async def test_async_strict_soft_relative_release_uses_acquisition_directory(
47 working_directories: tuple[Path, Path],
48 ) -> None:
49 first, second = working_directories
50 lock = AsyncStrictSoftFileLock("resource.lock", run_in_executor=False)
51 await lock.acquire()
52 assert tuple(claim.state for claim in StrictSoftFileLock(first / "resource.lock").claims) == ("held", "intent")
53
54 os.chdir(second)
55 await lock.release()
56
57 assert (
58 StrictSoftFileLock(first / "resource.lock").claims,
59 StrictSoftFileLock(second / "resource.lock").claims,
60 ) == ((), ())
61
62
63 def test_strict_soft_waiter_keeps_acquisition_directory(
64 working_directories: tuple[Path, Path],
65 ) -> None:
66 first, second = working_directories
67 holder = StrictSoftFileLock(first / "resource.lock")
68 holder.acquire()
69 polling = threading.Event()
70 acquired = threading.Event()
71 release = threading.Event()
72 try:
73 with ThreadPoolExecutor(max_workers=1) as executor:
74 future = executor.submit(_hold_relative_lock, polling, acquired, release)
75 assert polling.wait(timeout=2)
76 os.chdir(second)
77 holder.release()
78 assert acquired.wait(timeout=2)
79 assert (
80 tuple(claim.state for claim in StrictSoftFileLock(first / "resource.lock").claims),
81 StrictSoftFileLock(second / "resource.lock").claims,
82 ) == (("held", "intent"), ())
83 release.set()
84 future.result(timeout=2)
85 finally:
86 release.set()
87 holder.release(force=True)
88
89
90 @pytest.mark.asyncio
91 async def test_async_strict_soft_waiter_keeps_acquisition_directory(
92 working_directories: tuple[Path, Path],
93 ) -> None:
94 first, second = working_directories
95 holder = AsyncStrictSoftFileLock(first / "resource.lock", run_in_executor=False)
96 await holder.acquire()
97 polling = asyncio.Event()
98 contender = AsyncStrictSoftFileLock("resource.lock", timeout=2, poll_interval=0.001, run_in_executor=False)
99 task = asyncio.create_task(contender.acquire(cancel_check=partial(_signal_async_poll, polling)))
100 try:
101 await asyncio.wait_for(polling.wait(), timeout=2)
102 os.chdir(second)
103 await holder.release()
104 await asyncio.wait_for(task, timeout=2)
105 assert (
106 tuple(claim.state for claim in StrictSoftFileLock(first / "resource.lock").claims),
107 StrictSoftFileLock(second / "resource.lock").claims,
108 ) == (("held", "intent"), ())
109 finally:
110 if not task.done(): # pragma: no cover # the contender wins before teardown, so cancellation cleanup is rare
111 task.cancel()
112 with suppress(asyncio.CancelledError):
113 await task
114 await holder.release(force=True)
115 await contender.release(force=True)
116
117
118 @NEEDS_PARENT_SYMLINK_COLLAPSE # pragma: win32 no cover
119 def test_strict_soft_release_uses_original_symlink_parent(tmp_path: Path) -> None:
120 original = tmp_path / "original"
121 replacement = tmp_path / "replacement"
122 original.mkdir()
123 replacement.mkdir()
124 link = tmp_path / "link"
125 link.symlink_to(original, target_is_directory=True)
126 original_holder = StrictSoftFileLock(link / "resource.lock")
127 replacement_holder = StrictSoftFileLock(replacement / "resource.lock")
128 original_holder.acquire()
129 replacement_holder.acquire()
130 try:
131 link.unlink()
132 link.symlink_to(replacement, target_is_directory=True)
133 original_holder.release()
134
135 assert (
136 StrictSoftFileLock(original / "resource.lock").claims,
137 tuple(claim.state for claim in replacement_holder.claims),
138 ) == ((), ("held", "intent"))
139 finally:
140 original_holder.release(force=True)
141 replacement_holder.release(force=True)
142
143
144 @NEEDS_SYMLINK # pragma: needs symlink
145 def test_strict_soft_final_symlink_fails_closed_without_touching_target(tmp_path: Path) -> None:
146 target = tmp_path / "target"
147 target.write_bytes(_STRICT_SENTINEL)
148 lock_path = tmp_path / "resource.lock"
149 lock_path.symlink_to(target)
150 lock = StrictSoftFileLock(lock_path, timeout=0)
151
152 with pytest.raises(Timeout):
153 lock.acquire()
154
155 assert (target.read_bytes(), lock_path.is_symlink(), lock.claims) == (_STRICT_SENTINEL, True, ())
156
157
158 def _hold_relative_lock(polling: threading.Event, acquired: threading.Event, release: threading.Event) -> None:
159 with StrictSoftFileLock("resource.lock", timeout=2, poll_interval=0.001).acquire(
160 cancel_check=partial(_signal_thread_poll, polling)
161 ):
162 acquired.set()
163 assert release.wait(timeout=2)
164
165
166 def _signal_thread_poll(polling: threading.Event) -> bool:
167 polling.set()
168 return False
169
170
171 def _signal_async_poll(polling: asyncio.Event) -> bool:
172 polling.set()
173 return False
174
175
176 @pytest.fixture
177 def working_directories(tmp_path: Path, monkeypatch: pytest.MonkeyPatch) -> tuple[Path, Path]:
178 first = tmp_path / "first"
179 second = tmp_path / "second"
180 first.mkdir()
181 second.mkdir()
182 monkeypatch.chdir(first)
183 return first, second