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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
from __future__ import print_function
from __future__ import with_statement
import os
import dataclasses
import multiprocessing
import time
import typing
import pytest
import portalocker
from portalocker import utils
from portalocker import LockFlags
def test_exceptions(tmpfile):
# Open the file 2 times
a = open(tmpfile, 'a')
b = open(tmpfile, 'a')
# Lock exclusive non-blocking
lock_flags = portalocker.LOCK_EX | portalocker.LOCK_NB
# First lock file a
portalocker.lock(a, lock_flags)
# Now see if we can lock file b
with pytest.raises(portalocker.LockException):
portalocker.lock(b, lock_flags)
# Cleanup
a.close()
b.close()
def test_utils_base():
class Test(utils.LockBase):
pass
def test_with_timeout(tmpfile):
# Open the file 2 times
with pytest.raises(portalocker.AlreadyLocked):
with portalocker.Lock(tmpfile, timeout=0.1) as fh:
print('writing some stuff to my cache...', file=fh)
with portalocker.Lock(
tmpfile, timeout=0.1, mode='wb',
fail_when_locked=True
):
pass
print('writing more stuff to my cache...', file=fh)
def test_without_timeout(tmpfile):
# Open the file 2 times
with pytest.raises(portalocker.LockException):
with portalocker.Lock(tmpfile, timeout=None) as fh:
print('writing some stuff to my cache...', file=fh)
with portalocker.Lock(tmpfile, timeout=None, mode='w'):
pass
print('writing more stuff to my cache...', file=fh)
def test_without_fail(tmpfile):
# Open the file 2 times
with pytest.raises(portalocker.LockException):
with portalocker.Lock(tmpfile, timeout=0.1) as fh:
print('writing some stuff to my cache...', file=fh)
lock = portalocker.Lock(tmpfile, timeout=0.1)
lock.acquire(check_interval=0.05, fail_when_locked=False)
def test_simple(tmpfile):
with open(tmpfile, 'w') as fh:
fh.write('spam and eggs')
fh = open(tmpfile, 'r+')
portalocker.lock(fh, portalocker.LOCK_EX)
fh.seek(13)
fh.write('foo')
# Make sure we didn't overwrite the original text
fh.seek(0)
assert fh.read(13) == 'spam and eggs'
portalocker.unlock(fh)
fh.close()
def test_truncate(tmpfile):
with open(tmpfile, 'w') as fh:
fh.write('spam and eggs')
with portalocker.Lock(tmpfile, mode='a+') as fh:
# Make sure we didn't overwrite the original text
fh.seek(0)
assert fh.read(13) == 'spam and eggs'
with portalocker.Lock(tmpfile, mode='w+') as fh:
# Make sure we truncated the file
assert fh.read() == ''
def test_class(tmpfile):
lock = portalocker.Lock(tmpfile)
lock2 = portalocker.Lock(tmpfile, fail_when_locked=False, timeout=0.01)
with lock:
lock.acquire()
with pytest.raises(portalocker.LockException):
with lock2:
pass
with lock2:
pass
def test_acquire_release(tmpfile):
lock = portalocker.Lock(tmpfile)
lock2 = portalocker.Lock(tmpfile, fail_when_locked=False)
lock.acquire() # acquire lock when nobody is using it
with pytest.raises(portalocker.LockException):
# another party should not be able to acquire the lock
lock2.acquire(timeout=0.01)
# re-acquire a held lock is a no-op
lock.acquire()
lock.release() # release the lock
lock.release() # second release does nothing
def test_rlock_acquire_release_count(tmpfile):
lock = portalocker.RLock(tmpfile)
# Twice acquire
h = lock.acquire()
assert not h.closed
lock.acquire()
assert not h.closed
# Two release
lock.release()
assert not h.closed
lock.release()
assert h.closed
def test_rlock_acquire_release(tmpfile):
lock = portalocker.RLock(tmpfile)
lock2 = portalocker.RLock(tmpfile, fail_when_locked=False)
lock.acquire() # acquire lock when nobody is using it
with pytest.raises(portalocker.LockException):
# another party should not be able to acquire the lock
lock2.acquire(timeout=0.01)
# Now acquire again
lock.acquire()
lock.release() # release the lock
lock.release() # second release does nothing
def test_release_unacquired(tmpfile):
with pytest.raises(portalocker.LockException):
portalocker.RLock(tmpfile).release()
def test_exlusive(tmpfile):
with open(tmpfile, 'w') as fh:
fh.write('spam and eggs')
fh = open(tmpfile, 'r')
portalocker.lock(fh, portalocker.LOCK_EX | portalocker.LOCK_NB)
# Make sure we can't read the locked file
with pytest.raises(portalocker.LockException):
with open(tmpfile, 'r') as fh2:
portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB)
fh2.read()
# Make sure we can't write the locked file
with pytest.raises(portalocker.LockException):
with open(tmpfile, 'w+') as fh2:
portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB)
fh2.write('surprise and fear')
# Make sure we can explicitly unlock the file
portalocker.unlock(fh)
fh.close()
def test_shared(tmpfile):
with open(tmpfile, 'w') as fh:
fh.write('spam and eggs')
f = open(tmpfile, 'r')
portalocker.lock(f, portalocker.LOCK_SH | portalocker.LOCK_NB)
# Make sure we can read the locked file
with open(tmpfile, 'r') as fh2:
portalocker.lock(fh2, portalocker.LOCK_SH | portalocker.LOCK_NB)
assert fh2.read() == 'spam and eggs'
# Make sure we can't write the locked file
with pytest.raises(portalocker.LockException):
with open(tmpfile, 'w+') as fh2:
portalocker.lock(fh2, portalocker.LOCK_EX | portalocker.LOCK_NB)
fh2.write('surprise and fear')
# Make sure we can explicitly unlock the file
portalocker.unlock(f)
f.close()
def test_blocking_timeout(tmpfile):
flags = LockFlags.SHARED
with pytest.warns(UserWarning):
with portalocker.Lock(tmpfile, timeout=5, flags=flags):
pass
lock = portalocker.Lock(tmpfile, flags=flags)
with pytest.warns(UserWarning):
lock.acquire(timeout=5)
@pytest.mark.skipif(os.name == 'nt',
reason='Windows uses an entirely different lockmechanism')
def test_nonblocking(tmpfile):
with open(tmpfile, 'w') as fh:
with pytest.raises(RuntimeError):
portalocker.lock(fh, LockFlags.NON_BLOCKING)
def shared_lock(filename, **kwargs):
with portalocker.Lock(
filename,
timeout=0.1,
fail_when_locked=False,
flags=LockFlags.SHARED | LockFlags.NON_BLOCKING,
):
time.sleep(0.2)
return True
def shared_lock_fail(filename, **kwargs):
with portalocker.Lock(
filename,
timeout=0.1,
fail_when_locked=True,
flags=LockFlags.SHARED | LockFlags.NON_BLOCKING,
):
time.sleep(0.2)
return True
def exclusive_lock(filename, **kwargs):
with portalocker.Lock(
filename,
timeout=0.1,
fail_when_locked=False,
flags=LockFlags.EXCLUSIVE | LockFlags.NON_BLOCKING,
):
time.sleep(0.2)
return True
@dataclasses.dataclass(order=True)
class LockResult:
exception_class: typing.Union[typing.Type[Exception], None] = None
exception_message: typing.Union[str, None] = None
exception_repr: typing.Union[str, None] = None
def lock(
filename: str,
fail_when_locked: bool,
flags: LockFlags
) -> LockResult:
# Returns a case of True, False or FileNotFound
# https://thedailywtf.com/articles/what_is_truth_0x3f_
# But seriously, the exception properties cannot be safely pickled so we
# only return string representations of the exception properties
try:
with portalocker.Lock(
filename,
timeout=0.1,
fail_when_locked=fail_when_locked,
flags=flags,
):
time.sleep(0.2)
return LockResult()
except Exception as exception:
# The exceptions cannot be pickled so we cannot return them through
# multiprocessing
return LockResult(
type(exception),
str(exception),
repr(exception),
)
@pytest.mark.parametrize('fail_when_locked', [True, False])
def test_shared_processes(tmpfile, fail_when_locked):
flags = LockFlags.SHARED | LockFlags.NON_BLOCKING
with multiprocessing.Pool(processes=2) as pool:
args = tmpfile, fail_when_locked, flags
results = pool.starmap_async(lock, 2 * [args])
for result in results.get(timeout=3):
assert result == LockResult()
@pytest.mark.parametrize('fail_when_locked', [True, False])
def test_exclusive_processes(tmpfile, fail_when_locked):
flags = LockFlags.EXCLUSIVE | LockFlags.NON_BLOCKING
with multiprocessing.Pool(processes=2) as pool:
# filename, fail_when_locked, flags
args = tmpfile, fail_when_locked, flags
a, b = pool.starmap_async(lock, 2 * [args]).get(timeout=3)
assert not a.exception_class or not b.exception_class
assert issubclass(
a.exception_class or b.exception_class,
portalocker.LockException
)
@pytest.mark.skipif(
os.name == 'nt',
reason='Locking on Windows requires a file object',
)
def test_lock_fileno(tmpfile):
# Open the file 2 times
a = open(tmpfile, 'a')
b = open(tmpfile, 'a')
# Lock exclusive non-blocking
flags = LockFlags.SHARED | LockFlags.NON_BLOCKING
# First lock file a
portalocker.lock(a, flags)
# Now see if we can lock using fileno()
portalocker.lock(b.fileno(), flags)
# Cleanup
a.close()
b.close()
|