1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
|
from __future__ import annotations
from concurrent.futures import ThreadPoolExecutor
from typing import TYPE_CHECKING
import pytest
pytest.importorskip("sqlite3")
import sqlite3
from filelock import AsyncReadWriteLock, Timeout
from filelock._read_write import ReadWriteLock
if TYPE_CHECKING:
from collections.abc import Generator
from pathlib import Path
@pytest.fixture(autouse=True)
def _clear_singleton_cache() -> Generator[None]:
ReadWriteLock._instances.clear()
yield
for ref in list(ReadWriteLock._instances.valuerefs()):
if (lock := ref()) is not None:
lock.close()
ReadWriteLock._instances.clear()
@pytest.fixture
def lock_file(tmp_path: Path) -> str:
return str(tmp_path / "test_lock.db")
@pytest.mark.asyncio
async def test_acquire_release_read(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
proxy = await lock.acquire_read()
assert lock._lock._lock_level == 1
assert lock._lock._current_mode == "read"
await lock.release()
assert lock._lock._lock_level == 0
assert lock._lock._current_mode is None
assert isinstance(proxy, object)
@pytest.mark.asyncio
async def test_acquire_release_write(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.acquire_write()
assert lock._lock._lock_level == 1
assert lock._lock._current_mode == "write"
await lock.release()
assert lock._lock._lock_level == 0
assert lock._lock._current_mode is None
@pytest.mark.asyncio
async def test_read_lock_context_manager(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
async with lock.read_lock():
assert lock._lock._lock_level == 1
assert lock._lock._current_mode == "read"
assert lock._lock._lock_level == 0
assert lock._lock._current_mode is None
@pytest.mark.asyncio
async def test_write_lock_context_manager(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
async with lock.write_lock():
assert lock._lock._lock_level == 1
assert lock._lock._current_mode == "write"
assert lock._lock._lock_level == 0
assert lock._lock._current_mode is None
@pytest.mark.asyncio
async def test_reentrant_read(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.acquire_read()
await lock.acquire_read()
assert lock._lock._lock_level == 2
await lock.release()
assert lock._lock._lock_level == 1
assert lock._lock._current_mode == "read"
await lock.release()
assert lock._lock._lock_level == 0
@pytest.mark.asyncio
async def test_reentrant_write(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.acquire_write()
await lock.acquire_write()
assert lock._lock._lock_level == 2
await lock.release()
assert lock._lock._lock_level == 1
assert lock._lock._current_mode == "write"
await lock.release()
assert lock._lock._lock_level == 0
@pytest.mark.asyncio
async def test_upgrade_prohibited(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.acquire_read()
with pytest.raises(RuntimeError, match=r"already holding a read lock.*upgrade not allowed"):
await lock.acquire_write()
await lock.release()
@pytest.mark.asyncio
async def test_downgrade_prohibited(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.acquire_write()
with pytest.raises(RuntimeError, match=r"already holding a write lock.*downgrade not allowed"):
await lock.acquire_read()
await lock.release()
@pytest.mark.asyncio
async def test_non_blocking_read(lock_file: str) -> None:
holder = ReadWriteLock(lock_file, is_singleton=False)
holder.acquire_write()
try:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
with pytest.raises(Timeout):
await lock.acquire_read(blocking=False)
finally:
holder.release()
@pytest.mark.asyncio
async def test_non_blocking_write(lock_file: str) -> None:
holder = ReadWriteLock(lock_file, is_singleton=False)
holder.acquire_read()
try:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
with pytest.raises(Timeout):
await lock.acquire_write(blocking=False)
finally:
holder.release()
@pytest.mark.asyncio
async def test_timeout_expires(lock_file: str) -> None:
holder = ReadWriteLock(lock_file, is_singleton=False)
holder.acquire_write()
try:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
with pytest.raises(Timeout):
await lock.acquire_read(timeout=0.2)
finally:
holder.release()
@pytest.mark.asyncio
async def test_release_unheld_raises(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
with pytest.raises(RuntimeError, match="not held"):
await lock.release()
@pytest.mark.asyncio
async def test_release_force(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.acquire_write()
await lock.acquire_write()
assert lock._lock._lock_level == 2
await lock.release(force=True)
assert lock._lock._lock_level == 0
assert lock._lock._current_mode is None
@pytest.mark.asyncio
async def test_release_force_unheld_is_noop(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.release(force=True)
@pytest.mark.asyncio
async def test_close(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.acquire_write()
assert lock._lock._lock_level == 1
await lock.close()
assert lock._lock._lock_level == 0
with pytest.raises(sqlite3.ProgrammingError, match="Cannot operate on a closed database"):
lock._lock._con.execute("SELECT 1;")
@pytest.mark.asyncio
async def test_close_on_unheld_lock(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
await lock.close()
with pytest.raises(sqlite3.ProgrammingError, match="Cannot operate on a closed database"):
lock._lock._con.execute("SELECT 1;")
def test_properties(lock_file: str) -> None:
executor = ThreadPoolExecutor(max_workers=1)
lock = AsyncReadWriteLock(lock_file, timeout=5.0, blocking=False, is_singleton=False, executor=executor)
assert lock.lock_file == lock_file
assert lock.timeout == pytest.approx(5.0)
assert lock.blocking is False
assert lock.loop is None
assert lock.executor is executor
executor.shutdown(wait=False)
@pytest.mark.asyncio
async def test_custom_executor(lock_file: str) -> None:
executor = ThreadPoolExecutor(max_workers=1)
lock = AsyncReadWriteLock(lock_file, is_singleton=False, executor=executor)
async with lock.read_lock():
assert lock._lock._current_mode == "read"
assert lock._lock._lock_level == 0
executor.shutdown(wait=False)
@pytest.mark.asyncio
async def test_acquire_return_proxy_context_manager(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
async with await lock.acquire_read() as ctx:
assert ctx is lock
assert lock._lock._lock_level == 1
assert lock._lock._lock_level == 0
@pytest.mark.asyncio
async def test_nested_read_context_managers(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
async with lock.read_lock():
assert lock._lock._lock_level == 1
async with lock.read_lock():
assert lock._lock._lock_level == 2
assert lock._lock._lock_level == 1
assert lock._lock._lock_level == 0
@pytest.mark.asyncio
async def test_nested_write_context_managers(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
async with lock.write_lock():
assert lock._lock._lock_level == 1
async with lock.write_lock():
assert lock._lock._lock_level == 2
assert lock._lock._lock_level == 1
assert lock._lock._lock_level == 0
@pytest.mark.asyncio
async def test_context_manager_uses_instance_defaults(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, timeout=3.0, blocking=True, is_singleton=False)
async with lock.read_lock():
assert lock._lock._current_mode == "read"
async with lock.write_lock():
assert lock._lock._current_mode == "write"
@pytest.mark.asyncio
async def test_sequential_mode_switch(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
async with lock.read_lock():
pass
async with lock.write_lock():
pass
async with lock.read_lock():
pass
|