| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import os |
| 4 |
import threading |
| 5 |
from typing import TYPE_CHECKING, Literal |
| 6 |
|
| 7 |
import pytest |
| 8 |
|
| 9 |
from tests.capability_marks import NEEDS_GENERATOR_EXCEPTION_CONTEXT |
| 10 |
|
| 11 |
pytest.importorskip("sqlite3") |
| 12 |
|
| 13 |
import sqlite3 |
| 14 |
from pathlib import Path |
| 15 |
|
| 16 |
from filelock import Timeout |
| 17 |
from filelock._read_write import ( |
| 18 |
_FORKED_DATABASES, |
| 19 |
_MAX_SQLITE_TIMEOUT_MS, |
| 20 |
ReadWriteLock, |
| 21 |
_ForkedDatabaseRegistry, |
| 22 |
_track_sqlite_use, |
| 23 |
timeout_for_sqlite, |
| 24 |
) |
| 25 |
|
| 26 |
if TYPE_CHECKING: |
| 27 |
from collections.abc import Generator |
| 28 |
|
| 29 |
from pytest_mock import MockerFixture |
| 30 |
|
| 31 |
|
| 32 |
@pytest.fixture(autouse=True) |
| 33 |
def _clear_singleton_cache() -> Generator[None]: |
| 34 |
ReadWriteLock._instances.clear() |
| 35 |
yield |
| 36 |
for instance in list(ReadWriteLock._instances.valuerefs()): |
| 37 |
if (lock := instance()) is not None: # pragma: no cover # cache is normally emptied before teardown |
| 38 |
lock.close() |
| 39 |
ReadWriteLock._instances.clear() |
| 40 |
|
| 41 |
|
| 42 |
@pytest.fixture |
| 43 |
def lock_file(tmp_path: Path) -> str: |
| 44 |
return str(tmp_path / "test_lock.db") |
| 45 |
|
| 46 |
|
| 47 |
def test_timeout_for_sqlite_non_blocking() -> None: |
| 48 |
assert timeout_for_sqlite(10.0, blocking=False, already_waited=0.0) == 0 |
| 49 |
|
| 50 |
|
| 51 |
def test_timeout_for_sqlite_infinite_timeout() -> None: |
| 52 |
assert timeout_for_sqlite(-1, blocking=True, already_waited=0.0) == _MAX_SQLITE_TIMEOUT_MS |
| 53 |
|
| 54 |
|
| 55 |
def test_timeout_for_sqlite_negative_timeout_raises() -> None: |
| 56 |
with pytest.raises(ValueError, match="timeout must be a non-negative number or -1"): |
| 57 |
timeout_for_sqlite(-2, blocking=True, already_waited=0.0) |
| 58 |
|
| 59 |
|
| 60 |
def test_timeout_for_sqlite_positive_timeout_subtracts_waited() -> None: |
| 61 |
assert timeout_for_sqlite(5.0, blocking=True, already_waited=2.0) == 3000 |
| 62 |
|
| 63 |
|
| 64 |
def test_timeout_for_sqlite_waited_exceeds_timeout() -> None: |
| 65 |
assert timeout_for_sqlite(1.0, blocking=True, already_waited=2.0) == 0 |
| 66 |
|
| 67 |
|
| 68 |
def test_timeout_for_sqlite_zero_timeout() -> None: |
| 69 |
assert timeout_for_sqlite(0.0, blocking=True, already_waited=0.0) == 0 |
| 70 |
|
| 71 |
|
| 72 |
def test_timeout_for_sqlite_huge_timeout_clamped() -> None: |
| 73 |
assert timeout_for_sqlite(3_000_000.0, blocking=True, already_waited=0.0) == _MAX_SQLITE_TIMEOUT_MS |
| 74 |
|
| 75 |
|
| 76 |
def test_singleton_returns_same_instance(lock_file: str) -> None: |
| 77 |
lock1 = ReadWriteLock(lock_file) |
| 78 |
lock2 = ReadWriteLock(lock_file) |
| 79 |
assert lock1 is lock2 |
| 80 |
|
| 81 |
|
| 82 |
def test_singleton_different_files(tmp_path: Path) -> None: |
| 83 |
lock1 = ReadWriteLock(str(tmp_path / "a.db")) |
| 84 |
lock2 = ReadWriteLock(str(tmp_path / "b.db")) |
| 85 |
assert lock1 is not lock2 |
| 86 |
|
| 87 |
|
| 88 |
def test_singleton_rejects_different_timeout(lock_file: str) -> None: |
| 89 |
lock = ReadWriteLock(lock_file, timeout=1.0) |
| 90 |
with pytest.raises(ValueError, match="Singleton lock created with"): |
| 91 |
ReadWriteLock(lock_file, timeout=2.0) |
| 92 |
del lock |
| 93 |
|
| 94 |
|
| 95 |
def test_singleton_rejects_different_blocking(lock_file: str) -> None: |
| 96 |
lock = ReadWriteLock(lock_file, blocking=True) |
| 97 |
with pytest.raises(ValueError, match="Singleton lock created with"): |
| 98 |
ReadWriteLock(lock_file, blocking=False) |
| 99 |
del lock |
| 100 |
|
| 101 |
|
| 102 |
def test_non_singleton_creates_new_instance(lock_file: str) -> None: |
| 103 |
lock1 = ReadWriteLock(lock_file, is_singleton=False) |
| 104 |
lock2 = ReadWriteLock(lock_file, is_singleton=False) |
| 105 |
assert lock1 is not lock2 |
| 106 |
|
| 107 |
|
| 108 |
def test_init_sets_attributes(lock_file: str) -> None: |
| 109 |
lock = ReadWriteLock(lock_file, timeout=5.0, blocking=False, is_singleton=False) |
| 110 |
assert lock.lock_file == lock_file |
| 111 |
assert lock.timeout == pytest.approx(5.0) |
| 112 |
assert lock.blocking is False |
| 113 |
assert lock._lock_level == 0 |
| 114 |
assert lock._current_mode is None |
| 115 |
assert lock._write_thread_id is None |
| 116 |
assert Path(lock_file).is_file() |
| 117 |
|
| 118 |
|
| 119 |
def test_acquire_release_read(lock_file: str) -> None: |
| 120 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 121 |
proxy = lock.acquire_read() |
| 122 |
assert lock._lock_level == 1 |
| 123 |
assert lock._current_mode == "read" |
| 124 |
lock.release() |
| 125 |
assert lock._lock_level == 0 |
| 126 |
assert lock._current_mode is None |
| 127 |
assert isinstance(proxy, object) |
| 128 |
|
| 129 |
|
| 130 |
def test_acquire_release_write(lock_file: str) -> None: |
| 131 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 132 |
lock.acquire_write() |
| 133 |
assert lock._lock_level == 1 |
| 134 |
assert lock._current_mode == "write" |
| 135 |
assert lock._write_thread_id == threading.get_ident() |
| 136 |
lock.release() |
| 137 |
assert lock._lock_level == 0 |
| 138 |
assert lock._current_mode is None |
| 139 |
assert lock._write_thread_id is None |
| 140 |
|
| 141 |
|
| 142 |
@pytest.mark.parametrize( |
| 143 |
"mode", |
| 144 |
[ |
| 145 |
pytest.param("read", id="read"), |
| 146 |
pytest.param("write", id="write"), |
| 147 |
], |
| 148 |
) |
| 149 |
def test_reentrant_lock(lock_file: str, mode: Literal["read", "write"]) -> None: |
| 150 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 151 |
acquire = lock.acquire_read if mode == "read" else lock.acquire_write |
| 152 |
acquire() |
| 153 |
acquire() |
| 154 |
assert lock._lock_level == 2 |
| 155 |
lock.release() |
| 156 |
assert lock._lock_level == 1 |
| 157 |
assert lock._current_mode == mode |
| 158 |
lock.release() |
| 159 |
assert lock._lock_level == 0 |
| 160 |
|
| 161 |
|
| 162 |
def test_upgrade_read_to_write_prohibited(lock_file: str) -> None: |
| 163 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 164 |
lock.acquire_read() |
| 165 |
with pytest.raises(RuntimeError, match=r"already holding a read lock.*upgrade not allowed"): |
| 166 |
lock.acquire_write() |
| 167 |
lock.release() |
| 168 |
|
| 169 |
|
| 170 |
def test_downgrade_write_to_read_prohibited(lock_file: str) -> None: |
| 171 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 172 |
lock.acquire_write() |
| 173 |
with pytest.raises(RuntimeError, match=r"already holding a write lock.*downgrade not allowed"): |
| 174 |
lock.acquire_read() |
| 175 |
lock.release() |
| 176 |
|
| 177 |
|
| 178 |
def test_release_unheld_lock_raises(lock_file: str) -> None: |
| 179 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 180 |
with pytest.raises(RuntimeError, match="not held"): |
| 181 |
lock.release() |
| 182 |
|
| 183 |
|
| 184 |
def test_release_force_unheld_is_noop(lock_file: str) -> None: |
| 185 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 186 |
lock.release(force=True) |
| 187 |
|
| 188 |
|
| 189 |
def test_release_force_resets_level(lock_file: str) -> None: |
| 190 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 191 |
lock.acquire_write() |
| 192 |
lock.acquire_write() |
| 193 |
assert lock._lock_level == 2 |
| 194 |
lock.release(force=True) |
| 195 |
assert lock._lock_level == 0 |
| 196 |
assert lock._current_mode is None |
| 197 |
|
| 198 |
|
| 199 |
def test_release_force_closes_lingering_connection_at_zero_level(lock_file: str) -> None: |
| 200 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 201 |
lock.acquire_write() |
| 202 |
lock._lock_level = 0 |
| 203 |
lock.release(force=True) |
| 204 |
assert lock._con is None |
| 205 |
assert lock._lock_level == 0 |
| 206 |
|
| 207 |
|
| 208 |
@pytest.mark.parametrize( |
| 209 |
"mode", |
| 210 |
[ |
| 211 |
pytest.param("read", id="read"), |
| 212 |
pytest.param("write", id="write"), |
| 213 |
], |
| 214 |
) |
| 215 |
def test_lock_context_manager(lock_file: str, mode: Literal["read", "write"]) -> None: |
| 216 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 217 |
with lock.read_lock() if mode == "read" else lock.write_lock(): |
| 218 |
assert lock._lock_level == 1 |
| 219 |
assert lock._current_mode == mode |
| 220 |
assert lock._lock_level == 0 |
| 221 |
assert lock._current_mode is None |
| 222 |
|
| 223 |
|
| 224 |
@pytest.mark.parametrize( |
| 225 |
"mode", |
| 226 |
[ |
| 227 |
pytest.param("read", id="read"), |
| 228 |
pytest.param("write", id="write"), |
| 229 |
], |
| 230 |
) |
| 231 |
def test_lock_uses_instance_defaults(lock_file: str, mode: Literal["read", "write"]) -> None: |
| 232 |
lock = ReadWriteLock(lock_file, timeout=3.0, blocking=True, is_singleton=False) |
| 233 |
with lock.read_lock() if mode == "read" else lock.write_lock(): |
| 234 |
assert lock._current_mode == mode |
| 235 |
|
| 236 |
|
| 237 |
def test_read_lock_custom_timeout(lock_file: str) -> None: |
| 238 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 239 |
with lock.read_lock(timeout=5.0): |
| 240 |
assert lock._current_mode == "read" |
| 241 |
|
| 242 |
|
| 243 |
def test_write_lock_custom_blocking(lock_file: str) -> None: |
| 244 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 245 |
with lock.write_lock(blocking=True): |
| 246 |
assert lock._current_mode == "write" |
| 247 |
|
| 248 |
|
| 249 |
def test_read_lock_context_manager_overrides_defaults(lock_file: str) -> None: |
| 250 |
lock = ReadWriteLock(lock_file, timeout=10.0, blocking=False, is_singleton=False) |
| 251 |
with lock.read_lock(timeout=5.0, blocking=True): |
| 252 |
assert lock._current_mode == "read" |
| 253 |
|
| 254 |
|
| 255 |
def test_close_releases_lock_and_closes_connection(lock_file: str) -> None: |
| 256 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 257 |
lock.acquire_write() |
| 258 |
assert lock._lock_level == 1 |
| 259 |
lock.close() |
| 260 |
assert lock._lock_level == 0 |
| 261 |
with pytest.raises(sqlite3.ProgrammingError, match="Cannot operate on a closed database"): |
| 262 |
lock.acquire_read() |
| 263 |
|
| 264 |
|
| 265 |
def test_close_on_unheld_lock_closes_connection(lock_file: str) -> None: |
| 266 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 267 |
lock.close() |
| 268 |
with pytest.raises(sqlite3.ProgrammingError, match="Cannot operate on a closed database"): |
| 269 |
lock.acquire_read() |
| 270 |
|
| 271 |
|
| 272 |
def test_write_lock_reentrant_from_different_thread_prohibited(lock_file: str) -> None: |
| 273 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 274 |
lock.acquire_write() |
| 275 |
error: RuntimeError | None = None |
| 276 |
|
| 277 |
def try_reenter() -> None: |
| 278 |
nonlocal error |
| 279 |
try: |
| 280 |
lock.acquire_write() |
| 281 |
except RuntimeError as exc: |
| 282 |
error = exc |
| 283 |
|
| 284 |
thread = threading.Thread(target=try_reenter) |
| 285 |
thread.start() |
| 286 |
thread.join(timeout=5) |
| 287 |
lock.release() |
| 288 |
assert error is not None |
| 289 |
assert "while it is held by thread" in str(error) |
| 290 |
|
| 291 |
|
| 292 |
@pytest.mark.parametrize( |
| 293 |
("first", "second"), |
| 294 |
[ |
| 295 |
pytest.param("read", "write", id="read-then-write"), |
| 296 |
pytest.param("write", "read", id="write-then-read"), |
| 297 |
], |
| 298 |
) |
| 299 |
def test_sequential_mode_switch(lock_file: str, first: str, second: str) -> None: |
| 300 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 301 |
with lock.read_lock() if first == "read" else lock.write_lock(): |
| 302 |
pass |
| 303 |
with lock.read_lock() if second == "read" else lock.write_lock(): |
| 304 |
pass |
| 305 |
|
| 306 |
|
| 307 |
def test_non_blocking_read_fails_when_write_held(lock_file: str) -> None: |
| 308 |
holder_lock = ReadWriteLock(lock_file, is_singleton=False) |
| 309 |
contender_lock = ReadWriteLock(lock_file, is_singleton=False) |
| 310 |
acquired = threading.Event() |
| 311 |
|
| 312 |
def hold_write() -> None: |
| 313 |
with holder_lock.write_lock(): |
| 314 |
acquired.set() |
| 315 |
threading.Event().wait(timeout=2) |
| 316 |
|
| 317 |
thread = threading.Thread(target=hold_write) |
| 318 |
thread.start() |
| 319 |
acquired.wait(timeout=5) |
| 320 |
|
| 321 |
with pytest.raises(Timeout): |
| 322 |
contender_lock.acquire_read(blocking=False) |
| 323 |
|
| 324 |
thread.join(timeout=5) |
| 325 |
|
| 326 |
|
| 327 |
def test_non_blocking_write_fails_when_read_held(lock_file: str) -> None: |
| 328 |
holder_lock = ReadWriteLock(lock_file, is_singleton=False) |
| 329 |
contender_lock = ReadWriteLock(lock_file, is_singleton=False) |
| 330 |
acquired = threading.Event() |
| 331 |
|
| 332 |
def hold_read() -> None: |
| 333 |
with holder_lock.read_lock(): |
| 334 |
acquired.set() |
| 335 |
threading.Event().wait(timeout=2) |
| 336 |
|
| 337 |
thread = threading.Thread(target=hold_read) |
| 338 |
thread.start() |
| 339 |
acquired.wait(timeout=5) |
| 340 |
|
| 341 |
with pytest.raises(Timeout): |
| 342 |
contender_lock.acquire_write(blocking=False) |
| 343 |
|
| 344 |
thread.join(timeout=5) |
| 345 |
|
| 346 |
|
| 347 |
def test_timeout_read_expires(lock_file: str) -> None: |
| 348 |
holder_lock = ReadWriteLock(lock_file, is_singleton=False) |
| 349 |
contender_lock = ReadWriteLock(lock_file, is_singleton=False) |
| 350 |
acquired = threading.Event() |
| 351 |
|
| 352 |
def hold_write() -> None: |
| 353 |
with holder_lock.write_lock(): |
| 354 |
acquired.set() |
| 355 |
threading.Event().wait(timeout=3) |
| 356 |
|
| 357 |
thread = threading.Thread(target=hold_write) |
| 358 |
thread.start() |
| 359 |
acquired.wait(timeout=5) |
| 360 |
|
| 361 |
with pytest.raises(Timeout): |
| 362 |
contender_lock.acquire_read(timeout=0.2) |
| 363 |
|
| 364 |
thread.join(timeout=5) |
| 365 |
|
| 366 |
|
| 367 |
def test_nested_read_context_managers(lock_file: str) -> None: |
| 368 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 369 |
with lock.read_lock(): |
| 370 |
assert lock._lock_level == 1 |
| 371 |
with lock.read_lock(): |
| 372 |
assert lock._lock_level == 2 |
| 373 |
with lock.read_lock(): |
| 374 |
assert lock._lock_level == 3 |
| 375 |
assert lock._lock_level == 2 |
| 376 |
assert lock._lock_level == 1 |
| 377 |
assert lock._lock_level == 0 |
| 378 |
|
| 379 |
|
| 380 |
def test_nested_write_context_managers(lock_file: str) -> None: |
| 381 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 382 |
with lock.write_lock(): |
| 383 |
assert lock._lock_level == 1 |
| 384 |
with lock.write_lock(): |
| 385 |
assert lock._lock_level == 2 |
| 386 |
assert lock._lock_level == 1 |
| 387 |
assert lock._lock_level == 0 |
| 388 |
|
| 389 |
|
| 390 |
@pytest.mark.parametrize( |
| 391 |
"mode", |
| 392 |
[ |
| 393 |
pytest.param("read", id="read"), |
| 394 |
pytest.param("write", id="write"), |
| 395 |
], |
| 396 |
) |
| 397 |
def test_non_blocking_transaction_lock_timeout(lock_file: str, mode: Literal["read", "write"]) -> None: |
| 398 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 399 |
lock._transaction_lock.acquire() |
| 400 |
try: |
| 401 |
with pytest.raises(Timeout): |
| 402 |
(lock.acquire_read if mode == "read" else lock.acquire_write)(blocking=False) |
| 403 |
finally: |
| 404 |
lock._transaction_lock.release() |
| 405 |
|
| 406 |
|
| 407 |
@pytest.mark.parametrize( |
| 408 |
"mode", |
| 409 |
[ |
| 410 |
pytest.param("read", id="read"), |
| 411 |
pytest.param("write", id="write"), |
| 412 |
], |
| 413 |
) |
| 414 |
def test_non_blocking_with_timeout_no_value_error(lock_file: str, mode: Literal["read", "write"]) -> None: |
| 415 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 416 |
lock._transaction_lock.acquire() |
| 417 |
try: |
| 418 |
with pytest.raises(Timeout): |
| 419 |
(lock.acquire_read if mode == "read" else lock.acquire_write)(blocking=False, timeout=5.0) |
| 420 |
finally: |
| 421 |
lock._transaction_lock.release() |
| 422 |
|
| 423 |
|
| 424 |
@pytest.mark.parametrize( |
| 425 |
"mode", |
| 426 |
[ |
| 427 |
pytest.param("read", id="read"), |
| 428 |
pytest.param("write", id="write"), |
| 429 |
], |
| 430 |
) |
| 431 |
def test_finite_timeout_transaction_lock(lock_file: str, mode: Literal["read", "write"]) -> None: |
| 432 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 433 |
lock._transaction_lock.acquire() |
| 434 |
try: |
| 435 |
with pytest.raises(Timeout): |
| 436 |
(lock.acquire_read if mode == "read" else lock.acquire_write)(timeout=0.1) |
| 437 |
finally: |
| 438 |
lock._transaction_lock.release() |
| 439 |
|
| 440 |
|
| 441 |
def test_double_check_read_reentrance(lock_file: str, mocker: MockerFixture) -> None: |
| 442 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 443 |
real_lock = lock._transaction_lock |
| 444 |
mock_lock = mocker.MagicMock() |
| 445 |
|
| 446 |
def fake_acquire(blocking: bool = True, timeout: float = -1) -> bool: |
| 447 |
lock._lock_level = 1 |
| 448 |
lock._current_mode = "read" |
| 449 |
return real_lock.acquire(blocking, timeout) |
| 450 |
|
| 451 |
mock_lock.acquire = fake_acquire |
| 452 |
mock_lock.release = real_lock.release |
| 453 |
lock._transaction_lock = mock_lock |
| 454 |
proxy = lock.acquire_read() |
| 455 |
assert lock._lock_level == 2 |
| 456 |
assert proxy is not None |
| 457 |
lock._lock_level = 0 |
| 458 |
lock._current_mode = None |
| 459 |
lock.release(force=True) |
| 460 |
|
| 461 |
|
| 462 |
def test_double_check_write_reentrance(lock_file: str, mocker: MockerFixture) -> None: |
| 463 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 464 |
real_lock = lock._transaction_lock |
| 465 |
mock_lock = mocker.MagicMock() |
| 466 |
|
| 467 |
def fake_acquire(blocking: bool = True, timeout: float = -1) -> bool: |
| 468 |
lock._lock_level = 1 |
| 469 |
lock._current_mode = "write" |
| 470 |
lock._write_thread_id = threading.get_ident() |
| 471 |
return real_lock.acquire(blocking, timeout) |
| 472 |
|
| 473 |
mock_lock.acquire = fake_acquire |
| 474 |
mock_lock.release = real_lock.release |
| 475 |
lock._transaction_lock = mock_lock |
| 476 |
proxy = lock.acquire_write() |
| 477 |
assert lock._lock_level == 2 |
| 478 |
assert proxy is not None |
| 479 |
lock._lock_level = 0 |
| 480 |
lock._current_mode = None |
| 481 |
lock._write_thread_id = None |
| 482 |
lock.release(force=True) |
| 483 |
|
| 484 |
|
| 485 |
@pytest.mark.parametrize( |
| 486 |
("acquire_mode", "conflicting_mode", "match"), |
| 487 |
[ |
| 488 |
pytest.param("read", "write", "downgrade not allowed", id="read-blocked-by-write"), |
| 489 |
pytest.param("write", "read", "upgrade not allowed", id="write-blocked-by-read"), |
| 490 |
], |
| 491 |
) |
| 492 |
def test_double_check_wrong_mode_raises( |
| 493 |
lock_file: str, |
| 494 |
mocker: MockerFixture, |
| 495 |
acquire_mode: Literal["read", "write"], |
| 496 |
conflicting_mode: Literal["read", "write"], |
| 497 |
match: str, |
| 498 |
) -> None: |
| 499 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 500 |
real_lock = lock._transaction_lock |
| 501 |
mock_lock = mocker.MagicMock() |
| 502 |
|
| 503 |
def fake_acquire(blocking: bool = True, timeout: float = -1) -> bool: |
| 504 |
lock._lock_level = 1 |
| 505 |
lock._current_mode = conflicting_mode |
| 506 |
return real_lock.acquire(blocking, timeout) |
| 507 |
|
| 508 |
mock_lock.acquire = fake_acquire |
| 509 |
mock_lock.release = real_lock.release |
| 510 |
lock._transaction_lock = mock_lock |
| 511 |
with pytest.raises(RuntimeError, match=match): |
| 512 |
(lock.acquire_read if acquire_mode == "read" else lock.acquire_write)() |
| 513 |
lock._lock_level = 0 |
| 514 |
lock._current_mode = None |
| 515 |
|
| 516 |
|
| 517 |
def test_write_lock_context_manager_overrides_defaults(lock_file: str) -> None: |
| 518 |
lock = ReadWriteLock(lock_file, timeout=10.0, blocking=False, is_singleton=False) |
| 519 |
with lock.write_lock(timeout=5.0, blocking=True): |
| 520 |
assert lock._current_mode == "write" |
| 521 |
|
| 522 |
|
| 523 |
@pytest.mark.parametrize( |
| 524 |
"mode", |
| 525 |
[ |
| 526 |
pytest.param("read", id="read"), |
| 527 |
pytest.param("write", id="write"), |
| 528 |
], |
| 529 |
) |
| 530 |
def test_busy_timeout_recomputed_after_journal_mode( |
| 531 |
lock_file: str, mocker: MockerFixture, mode: Literal["read", "write"] |
| 532 |
) -> None: |
| 533 |
counter = iter([0.0, 0.0, 0.0, 0.5]) |
| 534 |
mocker.patch("filelock._read_write.time.perf_counter", side_effect=counter) |
| 535 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 536 |
(lock.acquire_read if mode == "read" else lock.acquire_write)(timeout=2.0) |
| 537 |
lock.release() |
| 538 |
|
| 539 |
|
| 540 |
def test_acquire_converts_connect_lock_error_to_timeout(lock_file: str, mocker: MockerFixture) -> None: |
| 541 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 542 |
mocker.patch( |
| 543 |
"filelock._read_write._connect", |
| 544 |
side_effect=sqlite3.OperationalError("database is locked"), |
| 545 |
) |
| 546 |
|
| 547 |
with pytest.raises(Timeout): |
| 548 |
lock.acquire_read(timeout=0.1) |
| 549 |
|
| 550 |
|
| 551 |
def test_get_lock_returns_the_singleton(lock_file: str) -> None: |
| 552 |
lock = ReadWriteLock.get_lock(lock_file) |
| 553 |
assert isinstance(lock, ReadWriteLock) |
| 554 |
assert lock is ReadWriteLock(lock_file) |
| 555 |
|
| 556 |
|
| 557 |
def test_note_sqlite_use_records_use_on_pypy(mocker: MockerFixture) -> None: |
| 558 |
mocker.patch("filelock._read_write._IS_PYPY", True) |
| 559 |
registry = _ForkedDatabaseRegistry() |
| 560 |
registry.note_sqlite_use() |
| 561 |
assert registry._sqlite_used is True |
| 562 |
|
| 563 |
|
| 564 |
@pytest.mark.parametrize("identity", [None, (1, 2)], ids=["no-identity", "with-identity"]) |
| 565 |
def test_poison_after_fork_records_path_and_identity(identity: tuple[int, int] | None) -> None: |
| 566 |
registry = _ForkedDatabaseRegistry() |
| 567 |
path = Path("poisoned.db") |
| 568 |
registry.poison_after_fork(path, identity) |
| 569 |
assert path in registry._paths |
| 570 |
assert (identity in registry._identities) is (identity is not None) |
| 571 |
|
| 572 |
|
| 573 |
def test_track_sqlite_use_forwards_connect_event(mocker: MockerFixture) -> None: |
| 574 |
note = mocker.patch.object(_FORKED_DATABASES, "note_sqlite_use") |
| 575 |
_track_sqlite_use("sqlite3.connect", ()) |
| 576 |
note.assert_called_once_with() |
| 577 |
|
| 578 |
|
| 579 |
def test_track_sqlite_use_ignores_other_events(mocker: MockerFixture) -> None: |
| 580 |
note = mocker.patch.object(_FORKED_DATABASES, "note_sqlite_use") |
| 581 |
_track_sqlite_use("open", ()) |
| 582 |
note.assert_not_called() |
| 583 |
|
| 584 |
|
| 585 |
def test_meta_resets_class_state_after_pid_change(lock_file: str) -> None: |
| 586 |
class _Sub(ReadWriteLock): |
| 587 |
pass |
| 588 |
|
| 589 |
_Sub._instances_pid = -1 |
| 590 |
lock = _Sub(lock_file) |
| 591 |
try: |
| 592 |
assert isinstance(lock, _Sub) |
| 593 |
assert _Sub._instances_pid == os.getpid() |
| 594 |
finally: |
| 595 |
lock.close() |
| 596 |
|
| 597 |
|
| 598 |
def test_meta_raises_when_pid_changes_during_singleton_construction(lock_file: str, mocker: MockerFixture) -> None: |
| 599 |
class _Sub(ReadWriteLock): |
| 600 |
pass |
| 601 |
|
| 602 |
pid_box = [os.getpid()] |
| 603 |
mocker.patch("filelock._read_write._GETPID", side_effect=lambda: pid_box[0]) |
| 604 |
original_init = _Sub.__init__ |
| 605 |
|
| 606 |
def _init_then_fork( |
| 607 |
self: _Sub, |
| 608 |
lock_file: str | os.PathLike[str], |
| 609 |
timeout: float = -1, |
| 610 |
*, |
| 611 |
blocking: bool = True, |
| 612 |
is_singleton: bool = True, |
| 613 |
) -> None: |
| 614 |
original_init(self, lock_file, timeout, blocking=blocking, is_singleton=is_singleton) |
| 615 |
pid_box[0] += 1 |
| 616 |
|
| 617 |
mocker.patch.object(_Sub, "__init__", _init_then_fork) |
| 618 |
|
| 619 |
with pytest.raises(RuntimeError, match="construction cannot continue after fork"): |
| 620 |
_Sub(lock_file) |
| 621 |
|
| 622 |
|
| 623 |
@NEEDS_GENERATOR_EXCEPTION_CONTEXT |
| 624 |
def test_finish_connection_chains_rollback_then_close_error(lock_file: str, mocker: MockerFixture) -> None: |
| 625 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 626 |
con = mocker.MagicMock() |
| 627 |
type(con).in_transaction = mocker.PropertyMock(side_effect=[True, False]) |
| 628 |
con.rollback.side_effect = sqlite3.OperationalError("rollback boom") |
| 629 |
con.close.side_effect = sqlite3.OperationalError("close boom") |
| 630 |
lock._con = con |
| 631 |
lock._connection_transaction_released = False |
| 632 |
|
| 633 |
with pytest.raises(sqlite3.OperationalError, match="close boom") as excinfo: |
| 634 |
lock._finish_connection() |
| 635 |
|
| 636 |
assert isinstance(excinfo.value.__context__, sqlite3.OperationalError) |
| 637 |
assert "rollback boom" in str(excinfo.value.__context__) |
| 638 |
con.rollback.assert_called_once_with() |
| 639 |
con.close.assert_called_once_with() |
| 640 |
|
| 641 |
|
| 642 |
def test_finish_connection_skips_rollback_when_already_released(lock_file: str) -> None: |
| 643 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 644 |
lock.acquire_write() |
| 645 |
con = lock._con |
| 646 |
assert con is not None |
| 647 |
lock._connection_transaction_released = True |
| 648 |
|
| 649 |
lock._finish_connection() |
| 650 |
|
| 651 |
assert lock._con is None |
| 652 |
with pytest.raises(sqlite3.ProgrammingError, match="closed database"): |
| 653 |
con.execute("SELECT 1") |
| 654 |
|
| 655 |
|
| 656 |
def test_dropping_a_lock_closes_its_connection(lock_file: str) -> None: |
| 657 |
# Nothing else closes a lock dropped while still holding one, so the connection would outlive it. |
| 658 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 659 |
lock.acquire_read() |
| 660 |
connection = lock._con |
| 661 |
assert connection is not None |
| 662 |
|
| 663 |
lock.__del__() # ruff:ignore[unnecessary-dunder-call] # a finalizer test cannot ride on collection timing |
| 664 |
|
| 665 |
with pytest.raises(sqlite3.ProgrammingError): |
| 666 |
connection.execute("select 1") |
| 667 |
|
| 668 |
|
| 669 |
def test_connection_close_from_another_process_leaves_it_open(lock_file: str, mocker: MockerFixture) -> None: |
| 670 |
# A child inherits the parent's connection, and closing it there would take the parent's down with it. |
| 671 |
lock = ReadWriteLock(lock_file, is_singleton=False) |
| 672 |
lock.acquire_read() |
| 673 |
connection = lock._con |
| 674 |
assert connection is not None |
| 675 |
mocker.patch("filelock._read_write._GETPID", return_value=-1) |
| 676 |
|
| 677 |
connection.close() |
| 678 |
|
| 679 |
mocker.stopall() |
| 680 |
assert connection.execute("select 1").fetchone() == (1,) |
| 681 |
lock.release() |