-
-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathtest_async_read_write.py
More file actions
311 lines (247 loc) · 10.1 KB
/
Copy pathtest_async_read_write.py
File metadata and controls
311 lines (247 loc) · 10.1 KB
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from __future__ import annotations
import gc
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_close_shuts_down_owned_executor(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
assert lock._owns_executor is True
executor = lock.executor
await lock.close()
with pytest.raises(RuntimeError): # submitting after shutdown is rejected
executor.submit(int)
@pytest.mark.asyncio
async def test_close_keeps_provided_executor_open(lock_file: str) -> None:
executor = ThreadPoolExecutor(max_workers=1)
lock = AsyncReadWriteLock(lock_file, is_singleton=False, executor=executor)
assert lock._owns_executor is False
await lock.close()
assert executor.submit(int).result(timeout=5) == 0 # still usable
executor.shutdown(wait=False)
def test_del_shuts_down_owned_executor(lock_file: str) -> None:
lock = AsyncReadWriteLock(lock_file, is_singleton=False)
executor = lock.executor
lock._lock.close() # close the connection so only the executor lifecycle is under test
del lock
gc.collect()
with pytest.raises(RuntimeError):
executor.submit(int)
def test_del_keeps_provided_executor_open(lock_file: str) -> None:
executor = ThreadPoolExecutor(max_workers=1)
lock = AsyncReadWriteLock(lock_file, is_singleton=False, executor=executor)
lock._lock.close()
del lock
gc.collect()
assert executor.submit(int).result(timeout=5) == 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