Debian Codegraph @

tests/test_lock_expiry.py

raw
1 from __future__ import annotations
2
3 import os
4 import socket
5 import time
6 from typing import TYPE_CHECKING, Final
7
8 import pytest
9
10 from filelock import AsyncFileLock, AsyncSoftFileLock, FileLock, SoftFileLock, Timeout
11
12 if TYPE_CHECKING:
13 from pathlib import Path
14
15 from pytest_mock import MockerFixture
16
17 _NATIVE_IGNORES: Final[str] = "lifetime is ignored"
18 pytestmark: Final[pytest.MarkDecorator] = pytest.mark.filterwarnings("ignore::filelock.SoftFileLockLifetimeWarning")
19
20
21 def test_expired_soft_lock_is_broken(tmp_path: Path) -> None:
22 lock_path = tmp_path / "test.lock"
23 lock_path.touch()
24 os.utime(lock_path, (0, 0))
25
26 lock = SoftFileLock(lock_path, lifetime=0.1, timeout=1)
27 with lock:
28 assert lock.is_locked
29
30
31 @pytest.mark.parametrize("lock_type", [FileLock, AsyncFileLock])
32 def test_native_lifetime_warns_and_is_ignored(lock_type: type[FileLock | AsyncFileLock], tmp_path: Path) -> None:
33 with pytest.warns(UserWarning, match=_NATIVE_IGNORES):
34 lock = lock_type(tmp_path / "test.lock", lifetime=10.0)
35 assert lock.lifetime is None
36
37
38 def test_native_lifetime_setter_warns_and_is_ignored(tmp_path: Path) -> None:
39 lock = FileLock(tmp_path / "test.lock")
40 with pytest.warns(UserWarning, match=_NATIVE_IGNORES):
41 lock.lifetime = 10.0
42 assert lock.lifetime is None
43
44
45 def test_native_lifetime_does_not_break_live_holder(tmp_path: Path) -> None:
46 lock_path = tmp_path / "test.lock"
47 with FileLock(lock_path, timeout=0):
48 os.utime(lock_path, (0, 0)) # a broken age-based break would evict this live holder
49 with pytest.warns(UserWarning, match=_NATIVE_IGNORES):
50 contender = FileLock(lock_path, lifetime=0.1, timeout=0.3)
51 with pytest.raises(Timeout):
52 contender.acquire()
53
54
55 def test_soft_non_expired_lock_not_broken(tmp_path: Path) -> None:
56 lock_path = tmp_path / "test.lock"
57 lock_path.write_text(f"{os.getpid()}\n{socket.gethostname()}\n", encoding="utf-8")
58
59 with pytest.raises(TimeoutError):
60 SoftFileLock(lock_path, lifetime=9999, timeout=0.2).acquire()
61
62
63 def test_soft_lifetime_none_no_expiry(tmp_path: Path) -> None:
64 lock_path = tmp_path / "test.lock"
65 lock_path.write_text(f"{os.getpid()}\n{socket.gethostname()}\n", encoding="utf-8")
66 os.utime(lock_path, (0, 0))
67
68 with pytest.raises(TimeoutError):
69 SoftFileLock(lock_path, lifetime=None, timeout=0.2).acquire()
70
71
72 def test_expired_lock_race_rename_fails(tmp_path: Path, mocker: MockerFixture) -> None:
73 lock_path = tmp_path / "test.lock"
74 lock_path.touch()
75 os.utime(lock_path, (0, 0))
76
77 mocker.patch("filelock._util.Path.rename", side_effect=FileNotFoundError)
78
79 with pytest.raises(TimeoutError):
80 SoftFileLock(lock_path, lifetime=0.1, timeout=0.5).acquire()
81
82
83 def test_lifetime_property_getter_setter(tmp_path: Path) -> None:
84 lock = SoftFileLock(tmp_path / "test.lock", lifetime=10.0)
85 assert lock.lifetime == pytest.approx(10.0)
86
87 lock.lifetime = 20.0
88 assert lock.lifetime == pytest.approx(20.0)
89
90 lock.lifetime = None
91 assert lock.lifetime is None
92
93
94 def test_lifetime_default_none(tmp_path: Path) -> None:
95 assert FileLock(tmp_path / "test.lock").lifetime is None
96
97
98 @pytest.mark.parametrize("bad_value", [-1, -0.5, -1e9])
99 def test_lifetime_setter_rejects_negative_number(bad_value: float, tmp_path: Path) -> None:
100 lock = FileLock(tmp_path / "test.lock")
101 with pytest.raises(ValueError, match="non-negative"):
102 lock.lifetime = bad_value
103
104
105 @pytest.mark.parametrize("bad_value", ["5", b"5", [1], {1: 2}, complex(1, 0)])
106 def test_lifetime_setter_rejects_non_numeric(
107 bad_value: str | bytes | list[int] | dict[int, int] | complex, tmp_path: Path
108 ) -> None:
109 lock = FileLock(tmp_path / "test.lock")
110 with pytest.raises(TypeError, match="lifetime must be"):
111 lock.lifetime = bad_value # ty: ignore[invalid-assignment] # non-numeric input to hit the setter's TypeError
112
113
114 @pytest.mark.parametrize("bad_value", [True, False]) # bool is an int subclass; reject it so it can't read as 1s/0s
115 def test_lifetime_setter_rejects_bool(bad_value: bool, tmp_path: Path) -> None:
116 lock = FileLock(tmp_path / "test.lock")
117 with pytest.raises(TypeError, match="lifetime must be"):
118 lock.lifetime = bad_value
119
120
121 @pytest.mark.parametrize("value", [0, 0.0])
122 def test_lifetime_setter_accepts_zero(value: float, tmp_path: Path) -> None:
123 lock = SoftFileLock(tmp_path / "test.lock")
124 lock.lifetime = value
125 assert lock.lifetime == 0
126
127
128 def test_lifetime_singleton_mismatch(tmp_path: Path) -> None:
129 lock_path = tmp_path / "test.lock"
130 lock1 = SoftFileLock(lock_path, is_singleton=True, lifetime=10.0)
131 assert lock1.lifetime == pytest.approx(10.0)
132
133 with pytest.raises(ValueError, match="lifetime"):
134 SoftFileLock(lock_path, is_singleton=True, lifetime=20.0)
135
136
137 def test_lifetime_singleton_match(tmp_path: Path) -> None:
138 lock_path = tmp_path / "test.lock"
139 lock1 = SoftFileLock(lock_path, is_singleton=True, lifetime=10.0)
140 lock2 = SoftFileLock(lock_path, is_singleton=True, lifetime=10.0)
141 assert lock1 is lock2
142
143
144 def test_lock_file_missing_during_expiry_check(tmp_path: Path) -> None:
145 lock = SoftFileLock(tmp_path / "test.lock", lifetime=0.1, timeout=1)
146 with lock:
147 assert lock.is_locked
148
149
150 def test_expired_lock_becomes_acquirable(tmp_path: Path) -> None:
151 lock_path = tmp_path / "test.lock"
152 lock_path.touch()
153 os.utime(lock_path, (0, 0))
154
155 lock = SoftFileLock(lock_path, lifetime=0.5, timeout=1)
156 with lock:
157 assert lock.is_locked
158 assert not lock.is_locked
159
160
161 @pytest.mark.asyncio
162 async def test_async_expired_lock_is_broken(tmp_path: Path) -> None:
163 lock_path = tmp_path / "test.lock"
164 lock_path.touch()
165 os.utime(lock_path, (0, 0))
166
167 lock = AsyncSoftFileLock(lock_path, lifetime=0.1, timeout=1)
168 async with lock:
169 assert lock.is_locked
170
171
172 @pytest.mark.asyncio
173 async def test_async_soft_non_expired_lock_not_broken(tmp_path: Path) -> None:
174 lock_path = tmp_path / "test.lock"
175 lock_path.touch()
176
177 with pytest.raises(TimeoutError):
178 await AsyncSoftFileLock(lock_path, lifetime=9999, timeout=0.2).acquire()
179
180
181 def test_lock_mtime_updated_on_acquire(tmp_path: Path) -> None:
182 lock_path = tmp_path / "test.lock"
183 before = time.time()
184 with SoftFileLock(lock_path, lifetime=60):
185 assert lock_path.exists() # the soft marker exists while held
186 assert lock_path.stat().st_mtime >= before - 1