Debian Codegraph @

pypy/module/thread/test/test_lock.py

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 from __future__ import with_statement
2 import py
3 import sys, os
4 from pypy.module.thread.test.support import GenericTestThread
5 from rpython.translator.c.test.test_genc import compile
6 from platform import machine
7
8
9 class AppTestLock(GenericTestThread):
10
11 def test_lock_basic(self):
12 import _thread
13 lock = _thread.allocate_lock()
14 assert type(lock) is _thread.LockType
15 assert lock.locked() is False
16 raises(RuntimeError, lock.release)
17 assert lock.locked() is False
18 r = lock.acquire()
19 assert r is True
20 r = lock.acquire(False)
21 assert r is False
22 assert lock.locked() is True
23 lock.release()
24 assert lock.locked() is False
25 raises(RuntimeError, lock.release)
26 assert lock.locked() is False
27 feedback = []
28 lock.acquire()
29 def f():
30 self.busywait(0.25)
31 feedback.append(42)
32 lock.release()
33 assert lock.locked() is True
34 _thread.start_new_thread(f, ())
35 lock.acquire()
36 assert lock.locked() is True
37 assert feedback == [42]
38
39 def test_lock_in_with(self):
40 import _thread
41 lock = _thread.allocate_lock()
42 feedback = []
43 lock.acquire()
44 def f():
45 self.busywait(0.25)
46 feedback.append(42)
47 lock.release()
48 assert lock.locked() is True
49 _thread.start_new_thread(f, ())
50 with lock:
51 assert lock.locked() is True
52 assert feedback == [42]
53 assert lock.locked() is False
54
55 def test_weakrefable(self):
56 import _thread, weakref
57 weakref.ref(_thread.allocate_lock())
58
59 def test_timeout(self):
60 import _thread
61 assert isinstance(_thread.TIMEOUT_MAX, float)
62 assert _thread.TIMEOUT_MAX > 1000
63 lock = _thread.allocate_lock()
64 assert lock.acquire() is True
65 assert lock.acquire(False) is False
66 assert lock.acquire(True, timeout=.1) is False
67
68 def test_timeout_overflow(self):
69 import _thread
70 lock = _thread.allocate_lock()
71 maxint = 2**63 - 1
72 for i in [-100000, -10000, -1000, -100, -10, -1, 0,
73 1, 10, 100, 1000, 10000, 100000]:
74 timeout = (maxint + i) * 1e-6
75 try:
76 lock.acquire(True, timeout=timeout)
77 except OverflowError:
78 got_ovf = True
79 else:
80 got_ovf = False
81 lock.release()
82 assert (i, got_ovf) == (i, int(timeout * 1e6) > maxint)
83
84 @py.test.mark.xfail(machine()=='s390x', reason='may fail under heavy load')
85 def test_ping_pong(self):
86 # The purpose of this test is that doing a large number of ping-pongs
87 # between two threads, using locks, should complete in a reasonable
88 # time on a translated pypy with -A. If the GIL logic causes too
89 # much sleeping, then it will fail.
90 import _thread as thread, time
91 COUNT = 100000 if self.runappdirect else 50
92 lock1 = thread.allocate_lock()
93 lock2 = thread.allocate_lock()
94 def fn():
95 for i in range(COUNT):
96 lock1.acquire()
97 lock2.release()
98 lock2.acquire()
99 print("STARTING")
100 start = time.time()
101 thread.start_new_thread(fn, ())
102 for i in range(COUNT):
103 lock2.acquire()
104 lock1.release()
105 stop = time.time()
106 assert stop - start < 30.0 # ~0.6 sec on pypy-c-jit
107
108 def test_at_fork_reinit(self):
109 import _thread as thread
110 def use_lock(lock):
111 # make sure that the lock still works normally
112 # after _at_fork_reinit()
113 lock.acquire()
114 lock.release()
115
116 # unlocked
117 for constr in [thread.allocate_lock, thread.RLock]:
118 lock = constr()
119 lock._at_fork_reinit()
120 use_lock(lock)
121
122 # locked: _at_fork_reinit() resets the lock to the unlocked state
123 lock2 = constr()
124 lock2.acquire()
125 lock2._at_fork_reinit()
126 use_lock(lock2)
127
128 def test_compile_lock():
129 from rpython.rlib import rgc
130 from rpython.rlib.rthread import allocate_lock
131 def g():
132 l = allocate_lock()
133 ok1 = l.acquire(True)
134 ok2 = l.acquire(False)
135 l.release()
136 ok3 = l.acquire(False)
137 res = ok1 and not ok2 and ok3
138 return res
139 g._dont_inline_ = True
140 def f():
141 res = g()
142 # the lock must have been freed by now - we use refcounting
143 return res
144 fn = compile(f, [], gcpolicy='ref')
145 res = fn()
146 assert res
147
148
149 class AppTestLockAgain(GenericTestThread):
150 # test it at app-level again to detect strange interactions
151 test_lock_again = AppTestLock.test_lock_basic.im_func
152
153
154 class AppTestRLock(GenericTestThread):
155 """
156 Tests for recursive locks.
157 """
158 def test_reacquire(self):
159 import _thread
160 lock = _thread.RLock()
161 lock.acquire()
162 lock.acquire()
163 lock.release()
164 lock.acquire()
165 lock.release()
166 lock.release()
167
168 def test_release_unacquired(self):
169 # Cannot release an unacquired lock
170 import _thread
171 lock = _thread.RLock()
172 raises(RuntimeError, lock.release)
173 lock.acquire()
174 lock.acquire()
175 lock.release()
176 lock.acquire()
177 lock.release()
178 lock.release()
179 raises(RuntimeError, lock.release)
180
181 def test_release_save(self):
182 import _thread
183 lock = _thread.RLock()
184 raises(RuntimeError, lock._release_save)
185 lock.acquire()
186 state = lock._release_save()
187 lock._acquire_restore(state)
188 lock.release()
189
190 def test__is_owned(self):
191 import _thread
192 lock = _thread.RLock()
193 assert lock._is_owned() is False
194 lock.acquire()
195 assert lock._is_owned() is True
196 lock.acquire()
197 assert lock._is_owned() is True
198 lock.release()
199 assert lock._is_owned() is True
200 lock.release()
201 assert lock._is_owned() is False
202
203 def test_context_manager(self):
204 import _thread
205 lock = _thread.RLock()
206 with lock:
207 assert lock._is_owned() is True
208
209 def test_timeout(self):
210 import _thread
211 lock = _thread.RLock()
212 assert lock.acquire() is True
213 assert lock.acquire(False) is True
214 assert lock.acquire(True, timeout=.1) is True
215
216
217 class AppTestLockSignals(GenericTestThread):
218 pytestmark = py.test.mark.skipif("os.name != 'posix'")
219
220 def w_acquire_retries_on_intr(self, lock):
221 import _thread, os, signal, time
222 self.sig_recvd = False
223 def my_handler(signal, frame):
224 self.sig_recvd = True
225 old_handler = signal.signal(signal.SIGUSR1, my_handler)
226 try:
227 ready = _thread.allocate_lock()
228 ready.acquire()
229 def other_thread():
230 # Acquire the lock in a non-main thread, so this test works for
231 # RLocks.
232 lock.acquire()
233 # Notify the main thread that we're ready
234 ready.release()
235 # Wait for 5 seconds here
236 for n in range(50):
237 time.sleep(0.1)
238 # Send the signal
239 os.kill(os.getpid(), signal.SIGUSR1)
240 # Let the main thread take the interrupt, handle it, and retry
241 # the lock acquisition. Then we'll let it run.
242 for n in range(50):
243 time.sleep(0.1)
244 lock.release()
245 _thread.start_new_thread(other_thread, ())
246 ready.acquire()
247 result = lock.acquire() # Block while we receive a signal.
248 assert self.sig_recvd
249 assert result
250 finally:
251 signal.signal(signal.SIGUSR1, old_handler)
252 for i in range(50):
253 time.sleep(0.1)
254
255 def test_lock_acquire_retries_on_intr(self):
256 import _thread
257 self.acquire_retries_on_intr(_thread.allocate_lock())
258
259 def test_rlock_acquire_retries_on_intr(self):
260 import _thread
261 self.acquire_retries_on_intr(_thread.RLock())
262
263 def w_alarm_interrupt(self, sig, frame):
264 raise KeyboardInterrupt
265
•266 @py.test.mark.skipif("sys.platform == 'gnu0'")
267 def test_lock_acquire_interruption(self):
268 import _thread, signal, time
269 # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck
270 # in a deadlock.
271 # XXX this test can fail when the legacy (non-semaphore) implementation
272 # of locks is used in thread_pthread.h, see issue #11223.
273 oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt)
274 try:
275 lock = _thread.allocate_lock()
276 lock.acquire()
277 signal.alarm(1)
278 t1 = time.time()
279 # XXX: raises doesn't work here?
280 #raises(KeyboardInterrupt, lock.acquire, timeout=5)
281 try:
282 lock.acquire(timeout=5)
283 except KeyboardInterrupt:
284 pass
285 else:
286 assert False, 'Expected KeyboardInterrupt'
287 dt = time.time() - t1
288 # Checking that KeyboardInterrupt was raised is not sufficient.
289 # We want to assert that lock.acquire() was interrupted because
290 # of the signal, not that the signal handler was called immediately
291 # after timeout return of lock.acquire() (which can fool assertRaises).
292 assert dt < 3.0
293 finally:
294 signal.signal(signal.SIGALRM, oldalrm)
295
296 def test_rlock_acquire_interruption(self):
297 import _thread, signal, time
298 # Mimic receiving a SIGINT (KeyboardInterrupt) with SIGALRM while stuck
299 # in a deadlock.
300 # XXX this test can fail when the legacy (non-semaphore) implementation
301 # of locks is used in thread_pthread.h, see issue #11223.
302 oldalrm = signal.signal(signal.SIGALRM, self.alarm_interrupt)
303 try:
304 rlock = _thread.RLock()
305 # For reentrant locks, the initial acquisition must be in another
306 # thread.
307 def other_thread():
308 rlock.acquire()
309 _thread.start_new_thread(other_thread, ())
310 # Wait until we can't acquire it without blocking...
311 while rlock.acquire(blocking=False):
312 rlock.release()
313 time.sleep(0.01)
314 signal.alarm(1)
315 t1 = time.time()
316 #raises(KeyboardInterrupt, rlock.acquire, timeout=5)
317 try:
318 rlock.acquire(timeout=5)
319 except KeyboardInterrupt:
320 pass
321 else:
322 assert False, 'Expected KeyboardInterrupt'
323 dt = time.time() - t1
324 # See rationale above in test_lock_acquire_interruption
325 assert dt < 3.0
326 finally:
327 signal.signal(signal.SIGALRM, oldalrm)
328
329
330 class AppTestLockRepr(GenericTestThread):
331
332 def test_lock_repr(self):
333 import _thread
334 lock = _thread.allocate_lock()
335 assert repr(lock).startswith("<unlocked _thread.lock object at ")
336 lock.acquire()
337 assert repr(lock).startswith("<locked _thread.lock object at ")
338
339 def test_rlock_repr(self):
340 import _thread
341 rlock = _thread.RLock()
342 assert repr(rlock).startswith(
343 "<unlocked _thread.RLock object owner=0 count=0 at ")
344 rlock.acquire()
345 rlock.acquire()
346 assert repr(rlock).startswith("<locked _thread.RLock object owner=")
347 assert 'owner=0' not in repr(rlock)
348 assert " count=2 at " in repr(rlock)