Debian Codegraph @

tests/test_soft_lifetime_warning.py

raw
1 from __future__ import annotations
2
3 import sys
4 import warnings
5 from typing import TYPE_CHECKING, Literal
6
7 import pytest
8
9 from filelock import (
10 AsyncSoftFileLock,
11 AsyncUnixFileLock,
12 AsyncWindowsFileLock,
13 SoftFileLock,
14 SoftFileLockLifetimeWarning,
15 Timeout,
16 UnixFileLock,
17 WindowsFileLock,
18 )
19
20 if TYPE_CHECKING:
21 from pathlib import Path
22
23
24 @pytest.mark.parametrize(
25 ("lock_type", "strict_lock", "lease"),
26 [
27 pytest.param(SoftFileLock, "StrictSoftFileLock", "SoftFileLease", id="sync"),
28 pytest.param(AsyncSoftFileLock, "AsyncStrictSoftFileLock", "AsyncSoftFileLease", id="async"),
29 ],
30 )
31 @pytest.mark.parametrize(
32 "entry_point",
33 [pytest.param("constructor", id="constructor"), pytest.param("setter", id="setter")],
34 )
35 def test_soft_lifetime_warning_identifies_caller(
36 lock_type: type[SoftFileLock | AsyncSoftFileLock],
37 strict_lock: str,
38 lease: str,
39 entry_point: Literal["constructor", "setter"],
40 tmp_path: Path,
41 ) -> None:
42 with warnings.catch_warnings(record=True) as caught:
43 warnings.simplefilter("always")
44 if entry_point == "constructor":
45 warning_line = sys._getframe().f_lineno + 1
46 lock_type(tmp_path / "test.lock", lifetime=10)
47 else:
48 lock = lock_type(tmp_path / "test.lock")
49 warning_line = sys._getframe().f_lineno + 1
50 lock.lifetime = 10
51
52 assert [(item.category, str(item.message), item.filename, item.lineno) for item in caught] == [
53 (
54 SoftFileLockLifetimeWarning,
55 (
56 f"{lock_type.__name__}(lifetime=...) uses age-based expiry and can overlap a live holder; "
57 f"use {lease} for expiry or {strict_lock} for fail-closed locking"
58 ),
59 __file__,
60 warning_line,
61 )
62 ]
63
64
65 @pytest.mark.parametrize(
66 "lock_type",
67 [pytest.param(SoftFileLock, id="sync"), pytest.param(AsyncSoftFileLock, id="async")],
68 )
69 @pytest.mark.parametrize(
70 "entry_point",
71 [pytest.param("constructor", id="constructor"), pytest.param("setter", id="setter")],
72 )
73 def test_soft_lifetime_none_does_not_warn(
74 lock_type: type[SoftFileLock | AsyncSoftFileLock], entry_point: Literal["constructor", "setter"], tmp_path: Path
75 ) -> None:
76 with warnings.catch_warnings(record=True) as caught:
77 warnings.simplefilter("always")
78 if entry_point == "constructor":
79 lock_type(tmp_path / "test.lock", lifetime=None)
80 else:
81 lock = lock_type(tmp_path / "test.lock")
82 lock.lifetime = None
83
84 assert caught == []
85
86
87 @pytest.mark.parametrize(
88 "lock_type",
89 [pytest.param(SoftFileLock, id="sync"), pytest.param(AsyncSoftFileLock, id="async")],
90 )
91 @pytest.mark.parametrize(
92 "entry_point",
93 [pytest.param("constructor", id="constructor"), pytest.param("setter", id="setter")],
94 )
95 def test_invalid_soft_lifetime_does_not_warn(
96 lock_type: type[SoftFileLock | AsyncSoftFileLock], entry_point: Literal["constructor", "setter"], tmp_path: Path
97 ) -> None:
98 with warnings.catch_warnings(record=True) as caught:
99 warnings.simplefilter("always")
100 with pytest.raises(ValueError, match="finite and non-negative"):
101 _set_invalid_lifetime(lock_type, entry_point, tmp_path / "test.lock")
102
103 assert caught == []
104
105
106 @pytest.mark.parametrize(
107 "lock_type",
108 [
109 pytest.param(UnixFileLock, marks=pytest.mark.skipif(sys.platform == "win32", reason="Unix backend"), id="sync"),
110 pytest.param(
111 AsyncUnixFileLock,
112 marks=pytest.mark.skipif(sys.platform == "win32", reason="Unix backend"),
113 id="async",
114 ),
115 pytest.param(
116 WindowsFileLock, marks=pytest.mark.skipif(sys.platform != "win32", reason="Windows backend"), id="sync"
117 ),
118 pytest.param(
119 AsyncWindowsFileLock,
120 marks=pytest.mark.skipif(sys.platform != "win32", reason="Windows backend"),
121 id="async",
122 ),
123 ],
124 )
125 @pytest.mark.parametrize(
126 "entry_point",
127 [pytest.param("constructor", id="constructor"), pytest.param("setter", id="setter")],
128 )
129 def test_native_lifetime_warning_identifies_caller(
130 lock_type: type[UnixFileLock | AsyncUnixFileLock | WindowsFileLock | AsyncWindowsFileLock],
131 entry_point: Literal["constructor", "setter"],
132 tmp_path: Path,
133 ) -> None:
134 with warnings.catch_warnings(record=True) as caught:
135 warnings.simplefilter("always")
136 if entry_point == "constructor":
137 warning_line = sys._getframe().f_lineno + 1
138 lock_type(tmp_path / "test.lock", lifetime=10)
139 else:
140 lock = lock_type(tmp_path / "test.lock")
141 warning_line = sys._getframe().f_lineno + 1
142 lock.lifetime = 10
143
144 assert [(item.category, str(item.message), item.filename, item.lineno) for item in caught] == [
145 (
146 UserWarning,
147 (
148 f"lifetime is ignored for {lock_type.__name__}: a native OS lock cannot be broken safely by file age; "
149 "only SoftFileLock supports lifetime-based expiry"
150 ),
151 __file__,
152 warning_line,
153 )
154 ]
155
156
157 @pytest.mark.parametrize(
158 "lock_type",
159 [pytest.param(SoftFileLock, id="sync"), pytest.param(AsyncSoftFileLock, id="async")],
160 )
161 def test_soft_lifetime_singleton_match_warns_for_each_configuration(
162 lock_type: type[SoftFileLock | AsyncSoftFileLock], tmp_path: Path
163 ) -> None:
164 with warnings.catch_warnings(record=True) as caught:
165 warnings.simplefilter("always")
166 first = lock_type(tmp_path / "test.lock", lifetime=10, is_singleton=True)
167 second = lock_type(tmp_path / "test.lock", lifetime=10, is_singleton=True)
168
169 assert (first is second, len(caught)) == (True, 2)
170
171
172 @pytest.mark.parametrize(
173 "lock_type",
174 [pytest.param(SoftFileLock, id="sync"), pytest.param(AsyncSoftFileLock, id="async")],
175 )
176 def test_soft_lifetime_singleton_mismatch_warns_before_rejection(
177 lock_type: type[SoftFileLock | AsyncSoftFileLock], tmp_path: Path
178 ) -> None:
179 with warnings.catch_warnings(record=True) as caught:
180 warnings.simplefilter("always")
181 first = lock_type(tmp_path / "test.lock", lifetime=10, is_singleton=True)
182 with pytest.raises(ValueError, match="lifetime"):
183 lock_type(tmp_path / "test.lock", lifetime=20, is_singleton=True)
184
185 assert (first.lifetime, len(caught)) == (10, 2)
186
187
188 def test_soft_lifetime_polling_does_not_repeat_warning(tmp_path: Path) -> None:
189 lock_path = tmp_path / "test.lock"
190 holder = SoftFileLock(lock_path)
191 with pytest.warns(SoftFileLockLifetimeWarning):
192 contender = SoftFileLock(lock_path, lifetime=60, poll_interval=0.001, timeout=0.01)
193
194 with holder, warnings.catch_warnings(record=True) as caught:
195 warnings.simplefilter("always")
196 with pytest.raises(Timeout):
197 contender.acquire()
198
199 assert caught == []
200
201
202 @pytest.mark.asyncio
203 async def test_async_soft_lifetime_polling_does_not_repeat_warning(tmp_path: Path) -> None:
204 lock_path = tmp_path / "test.lock"
205 holder = AsyncSoftFileLock(lock_path)
206 with pytest.warns(SoftFileLockLifetimeWarning):
207 contender = AsyncSoftFileLock(lock_path, lifetime=60, poll_interval=0.001, timeout=0.01)
208
209 async with holder:
210 with warnings.catch_warnings(record=True) as caught:
211 warnings.simplefilter("always")
212 # The holder never releases here, so the acquire always times out.
213 with pytest.raises(Timeout): # pragma: no branch
214 await contender.acquire()
215
216 assert caught == []
217
218
219 def _set_invalid_lifetime(
220 lock_type: type[SoftFileLock | AsyncSoftFileLock],
221 entry_point: Literal["constructor", "setter"],
222 lock_path: Path,
223 ) -> None:
224 if entry_point == "constructor":
225 lock_type(lock_path, lifetime=-1)
226 else:
227 lock = lock_type(lock_path)
228 lock.lifetime = -1