Debian Codegraph @

Lib/test/lock_tests.py

raw
1 """
2 Various tests for synchronization primitives.
3 """
4
5 import gc
6 import sys
7 import time
8 from _thread import start_new_thread, TIMEOUT_MAX
9 import threading
10 import unittest
11 import weakref
12
13 from test import support
14 from test.support import threading_helper
15
16
17 requires_fork = unittest.skipUnless(support.has_fork_support,
18 "platform doesn't support fork "
19 "(no _at_fork_reinit method)")
20
21
22 def wait_threads_blocked(nthread):
23 # Arbitrary sleep to wait until N threads are blocked,
24 # like waiting for a lock.
25 time.sleep(0.010 * nthread)
26
27
28 class Bunch(object):
29 """
30 A bunch of threads.
31 """
32 def __init__(self, func, nthread, wait_before_exit=False):
33 """
34 Construct a bunch of `nthread` threads running the same function `func`.
35 If `wait_before_exit` is True, the threads won't terminate until
36 do_finish() is called.
37 """
38 self.func = func
39 self.nthread = nthread
40 self.started = []
41 self.finished = []
42 self.exceptions = []
43 self._can_exit = not wait_before_exit
44 self._wait_thread = None
45
46 def task(self):
47 tid = threading.get_ident()
48 self.started.append(tid)
49 try:
50 self.func()
51 except BaseException as exc:
52 self.exceptions.append(exc)
53 finally:
54 self.finished.append(tid)
55 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
56 if self._can_exit:
57 break
58
59 def __enter__(self):
60 self._wait_thread = threading_helper.wait_threads_exit(support.SHORT_TIMEOUT)
61 self._wait_thread.__enter__()
62
63 try:
64 for _ in range(self.nthread):
65 start_new_thread(self.task, ())
66 except:
67 self._can_exit = True
68 raise
69
70 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
71 if len(self.started) >= self.nthread:
72 break
73
74 return self
75
76 def __exit__(self, exc_type, exc_value, traceback):
77 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
78 if len(self.finished) >= self.nthread:
79 break
80
81 # Wait until threads completely exit according to _thread._count()
82 self._wait_thread.__exit__(None, None, None)
83
84 # Break reference cycle
85 exceptions = self.exceptions
86 self.exceptions = None
87 if exceptions:
88 raise ExceptionGroup(f"{self.func} threads raised exceptions",
89 exceptions)
90
91 def do_finish(self):
92 self._can_exit = True
93
94
95 class BaseTestCase(unittest.TestCase):
96 def setUp(self):
97 self._threads = threading_helper.threading_setup()
98
99 def tearDown(self):
100 threading_helper.threading_cleanup(*self._threads)
101 support.reap_children()
102
103 def assertTimeout(self, actual, expected):
104 # The waiting and/or time.monotonic() can be imprecise, which
105 # is why comparing to the expected value would sometimes fail
106 # (especially under Windows).
107 self.assertGreaterEqual(actual, expected * 0.6)
108 # Test nothing insane happened
109 self.assertLess(actual, expected * 10.0)
110
111
112 class BaseLockTests(BaseTestCase):
113 """
114 Tests for both recursive and non-recursive locks.
115 """
116
117 def wait_phase(self, phase, expected):
118 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
119 if len(phase) >= expected:
120 break
121 self.assertEqual(len(phase), expected)
122
123 def test_constructor(self):
124 lock = self.locktype()
125 del lock
126
127 def test_repr(self):
128 lock = self.locktype()
129 self.assertRegex(repr(lock), "<unlocked .* object (.*)?at .*>")
130 del lock
131
132 def test_locked_repr(self):
133 lock = self.locktype()
134 lock.acquire()
135 self.assertRegex(repr(lock), "<locked .* object (.*)?at .*>")
136 del lock
137
138 def test_acquire_destroy(self):
139 lock = self.locktype()
140 lock.acquire()
141 del lock
142
143 def test_acquire_release(self):
144 lock = self.locktype()
145 lock.acquire()
146 lock.release()
147 del lock
148
149 def test_try_acquire(self):
150 lock = self.locktype()
151 self.assertTrue(lock.acquire(False))
152 lock.release()
153
154 def test_try_acquire_contended(self):
155 lock = self.locktype()
156 lock.acquire()
157 result = []
158 def f():
159 result.append(lock.acquire(False))
160 with Bunch(f, 1):
161 pass
162 self.assertFalse(result[0])
163 lock.release()
164
165 def test_acquire_contended(self):
166 lock = self.locktype()
167 lock.acquire()
168 def f():
169 lock.acquire()
170 lock.release()
171
172 N = 5
173 with Bunch(f, N) as bunch:
174 # Threads block on lock.acquire()
175 wait_threads_blocked(N)
176 self.assertEqual(len(bunch.finished), 0)
177
178 # Threads unblocked
179 lock.release()
180
181 self.assertEqual(len(bunch.finished), N)
182
183 def test_with(self):
184 lock = self.locktype()
185 def f():
186 lock.acquire()
187 lock.release()
188
189 def with_lock(err=None):
190 with lock:
191 if err is not None:
192 raise err
193
194 # Acquire the lock, do nothing, with releases the lock
195 with lock:
196 pass
197
198 # Check that the lock is unacquired
199 with Bunch(f, 1):
200 pass
201
202 # Acquire the lock, raise an exception, with releases the lock
203 with self.assertRaises(TypeError):
204 with lock:
205 raise TypeError
206
207 # Check that the lock is unacquired even if after an exception
208 # was raised in the previous "with lock:" block
209 with Bunch(f, 1):
210 pass
211
212 def test_thread_leak(self):
213 # The lock shouldn't leak a Thread instance when used from a foreign
214 # (non-threading) thread.
215 lock = self.locktype()
216 def f():
217 lock.acquire()
218 lock.release()
219
220 # We run many threads in the hope that existing threads ids won't
221 # be recycled.
222 with Bunch(f, 15):
223 pass
224
225 def test_timeout(self):
226 lock = self.locktype()
227 # Can't set timeout if not blocking
228 self.assertRaises(ValueError, lock.acquire, False, 1)
229 # Invalid timeout values
230 self.assertRaises(ValueError, lock.acquire, timeout=-100)
231 self.assertRaises(OverflowError, lock.acquire, timeout=1e100)
232 self.assertRaises(OverflowError, lock.acquire, timeout=TIMEOUT_MAX + 1)
233 # TIMEOUT_MAX is ok
234 lock.acquire(timeout=TIMEOUT_MAX)
235 lock.release()
236 t1 = time.monotonic()
237 self.assertTrue(lock.acquire(timeout=5))
238 t2 = time.monotonic()
239 # Just a sanity test that it didn't actually wait for the timeout.
240 self.assertLess(t2 - t1, 5)
241 results = []
242 def f():
243 t1 = time.monotonic()
244 results.append(lock.acquire(timeout=0.5))
245 t2 = time.monotonic()
246 results.append(t2 - t1)
247 with Bunch(f, 1):
248 pass
249 self.assertFalse(results[0])
250 self.assertTimeout(results[1], 0.5)
251
252 def test_weakref_exists(self):
253 lock = self.locktype()
254 ref = weakref.ref(lock)
255 self.assertIsNotNone(ref())
256
257 def test_weakref_deleted(self):
258 lock = self.locktype()
259 ref = weakref.ref(lock)
260 del lock
261 gc.collect() # For PyPy or other GCs.
262 self.assertIsNone(ref())
263
264
265 class LockTests(BaseLockTests):
266 """
267 Tests for non-recursive, weak locks
268 (which can be acquired and released from different threads).
269 """
270 def test_reacquire(self):
271 # Lock needs to be released before re-acquiring.
272 lock = self.locktype()
273 phase = []
274
275 def f():
276 lock.acquire()
277 phase.append(None)
278 lock.acquire()
279 phase.append(None)
280
281 with threading_helper.wait_threads_exit():
282 # Thread blocked on lock.acquire()
283 start_new_thread(f, ())
284 self.wait_phase(phase, 1)
285
286 # Thread unblocked
287 lock.release()
288 self.wait_phase(phase, 2)
289
290 def test_different_thread(self):
291 # Lock can be released from a different thread.
292 lock = self.locktype()
293 lock.acquire()
294 def f():
295 lock.release()
296 with Bunch(f, 1):
297 pass
298 lock.acquire()
299 lock.release()
300
301 def test_state_after_timeout(self):
302 # Issue #11618: check that lock is in a proper state after a
303 # (non-zero) timeout.
304 lock = self.locktype()
305 lock.acquire()
306 self.assertFalse(lock.acquire(timeout=0.01))
307 lock.release()
308 self.assertFalse(lock.locked())
309 self.assertTrue(lock.acquire(blocking=False))
310
311 @requires_fork
312 def test_at_fork_reinit(self):
313 def use_lock(lock):
314 # make sure that the lock still works normally
315 # after _at_fork_reinit()
316 lock.acquire()
317 lock.release()
318
319 # unlocked
320 lock = self.locktype()
321 lock._at_fork_reinit()
322 use_lock(lock)
323
324 # locked: _at_fork_reinit() resets the lock to the unlocked state
325 lock2 = self.locktype()
326 lock2.acquire()
327 lock2._at_fork_reinit()
328 use_lock(lock2)
329
330
331 class RLockTests(BaseLockTests):
332 """
333 Tests for recursive locks.
334 """
335 def test_repr_count(self):
336 # see gh-134322: check that count values are correct:
337 # when a rlock is just created,
338 # in a second thread when rlock is acquired in the main thread.
339 lock = self.locktype()
340 self.assertIn("count=0", repr(lock))
341 self.assertIn("<unlocked", repr(lock))
342 lock.acquire()
343 lock.acquire()
344 self.assertIn("count=2", repr(lock))
345 self.assertIn("<locked", repr(lock))
346
347 result = []
348 def call_repr():
349 result.append(repr(lock))
350 with Bunch(call_repr, 1):
351 pass
352 self.assertIn("count=2", result[0])
353 self.assertIn("<locked", result[0])
354
355 def test_reacquire(self):
356 lock = self.locktype()
357 lock.acquire()
358 lock.acquire()
359 lock.release()
360 lock.acquire()
361 lock.release()
362 lock.release()
363
364 def test_release_unacquired(self):
365 # Cannot release an unacquired lock
366 lock = self.locktype()
367 self.assertRaises(RuntimeError, lock.release)
368 lock.acquire()
369 lock.acquire()
370 lock.release()
371 lock.acquire()
372 lock.release()
373 lock.release()
374 self.assertRaises(RuntimeError, lock.release)
375
376 def test_locked(self):
377 lock = self.locktype()
378 self.assertFalse(lock.locked())
379 lock.acquire()
380 self.assertTrue(lock.locked())
381 lock.acquire()
382 self.assertTrue(lock.locked())
383 lock.release()
384 self.assertTrue(lock.locked())
385 lock.release()
386 self.assertFalse(lock.locked())
387
388 def test_locked_with_2threads(self):
389 # see gh-134323: check that a rlock which
390 # is acquired in a different thread,
391 # is still locked in the main thread.
392 result = []
393 rlock = self.locktype()
394 self.assertFalse(rlock.locked())
395 def acquire():
396 result.append(rlock.locked())
397 rlock.acquire()
398 result.append(rlock.locked())
399
400 with Bunch(acquire, 1):
401 pass
402 self.assertTrue(rlock.locked())
403 self.assertFalse(result[0])
404 self.assertTrue(result[1])
405
406 def test_release_save_unacquired(self):
407 # Cannot _release_save an unacquired lock
408 lock = self.locktype()
409 self.assertRaises(RuntimeError, lock._release_save)
410 lock.acquire()
411 lock.acquire()
412 lock.release()
413 lock.acquire()
414 lock.release()
415 lock.release()
416 self.assertRaises(RuntimeError, lock._release_save)
417
418 def test_recursion_count(self):
419 lock = self.locktype()
420 self.assertEqual(0, lock._recursion_count())
421 lock.acquire()
422 self.assertEqual(1, lock._recursion_count())
423 lock.acquire()
424 lock.acquire()
425 self.assertEqual(3, lock._recursion_count())
426 lock.release()
427 self.assertEqual(2, lock._recursion_count())
428 lock.release()
429 lock.release()
430 self.assertEqual(0, lock._recursion_count())
431
432 phase = []
433
434 def f():
435 lock.acquire()
436 phase.append(None)
437
438 self.wait_phase(phase, 2)
439 lock.release()
440 phase.append(None)
441
442 with threading_helper.wait_threads_exit():
443 # Thread blocked on lock.acquire()
444 start_new_thread(f, ())
445 self.wait_phase(phase, 1)
446 self.assertEqual(0, lock._recursion_count())
447
448 # Thread unblocked
449 phase.append(None)
450 self.wait_phase(phase, 3)
451 self.assertEqual(0, lock._recursion_count())
452
453 def test_different_thread(self):
454 # Cannot release from a different thread
455 lock = self.locktype()
456 def f():
457 lock.acquire()
458
459 with Bunch(f, 1, True) as bunch:
460 try:
461 self.assertRaises(RuntimeError, lock.release)
462 finally:
463 bunch.do_finish()
464
465 def test__is_owned(self):
466 lock = self.locktype()
467 self.assertFalse(lock._is_owned())
468 lock.acquire()
469 self.assertTrue(lock._is_owned())
470 lock.acquire()
471 self.assertTrue(lock._is_owned())
472 result = []
473 def f():
474 result.append(lock._is_owned())
475 with Bunch(f, 1):
476 pass
477 self.assertFalse(result[0])
478 lock.release()
479 self.assertTrue(lock._is_owned())
480 lock.release()
481 self.assertFalse(lock._is_owned())
482
483
484 class EventTests(BaseTestCase):
485 """
486 Tests for Event objects.
487 """
488
489 def test_is_set(self):
490 evt = self.eventtype()
491 self.assertFalse(evt.is_set())
492 evt.set()
493 self.assertTrue(evt.is_set())
494 evt.set()
495 self.assertTrue(evt.is_set())
496 evt.clear()
497 self.assertFalse(evt.is_set())
498 evt.clear()
499 self.assertFalse(evt.is_set())
500
501 def _check_notify(self, evt):
502 # All threads get notified
503 N = 5
504 results1 = []
505 results2 = []
506 def f():
507 results1.append(evt.wait())
508 results2.append(evt.wait())
509
510 with Bunch(f, N):
511 # Threads blocked on first evt.wait()
512 wait_threads_blocked(N)
513 self.assertEqual(len(results1), 0)
514
515 # Threads unblocked
516 evt.set()
517
518 self.assertEqual(results1, [True] * N)
519 self.assertEqual(results2, [True] * N)
520
521 def test_notify(self):
522 evt = self.eventtype()
523 self._check_notify(evt)
524 # Another time, after an explicit clear()
525 evt.set()
526 evt.clear()
527 self._check_notify(evt)
528
529 def test_timeout(self):
530 evt = self.eventtype()
531 results1 = []
532 results2 = []
533 N = 5
534 def f():
535 results1.append(evt.wait(0.0))
536 t1 = time.monotonic()
537 r = evt.wait(0.5)
538 t2 = time.monotonic()
539 results2.append((r, t2 - t1))
540
541 with Bunch(f, N):
542 pass
543
544 self.assertEqual(results1, [False] * N)
545 for r, dt in results2:
546 self.assertFalse(r)
547 self.assertTimeout(dt, 0.5)
548
549 # The event is set
550 results1 = []
551 results2 = []
552 evt.set()
553 with Bunch(f, N):
554 pass
555
556 self.assertEqual(results1, [True] * N)
557 for r, dt in results2:
558 self.assertTrue(r)
559
560 def test_set_and_clear(self):
561 # gh-57711: check that wait() returns true even when the event is
562 # cleared before the waiting thread is woken up.
563 event = self.eventtype()
564 results = []
565 def f():
566 results.append(event.wait(support.LONG_TIMEOUT))
567
568 N = 5
569 with Bunch(f, N):
570 # Threads blocked on event.wait()
571 wait_threads_blocked(N)
572
573 # Threads unblocked
574 event.set()
575 event.clear()
576
577 self.assertEqual(results, [True] * N)
578
579 @requires_fork
580 def test_at_fork_reinit(self):
581 # ensure that condition is still using a Lock after reset
582 evt = self.eventtype()
583 with evt._cond:
584 self.assertFalse(evt._cond.acquire(False))
585 evt._at_fork_reinit()
586 with evt._cond:
587 self.assertFalse(evt._cond.acquire(False))
588
589 def test_repr(self):
590 evt = self.eventtype()
591 self.assertRegex(repr(evt), r"<\w+\.Event at .*: unset>")
592 evt.set()
593 self.assertRegex(repr(evt), r"<\w+\.Event at .*: set>")
594
595
596 class ConditionTests(BaseTestCase):
597 """
598 Tests for condition variables.
599 """
600
601 def test_acquire(self):
602 cond = self.condtype()
603 # Be default we have an RLock: the condition can be acquired multiple
604 # times.
605 cond.acquire()
606 cond.acquire()
607 cond.release()
608 cond.release()
609 lock = threading.Lock()
610 cond = self.condtype(lock)
611 cond.acquire()
612 self.assertFalse(lock.acquire(False))
613 cond.release()
614 self.assertTrue(lock.acquire(False))
615 self.assertFalse(cond.acquire(False))
616 lock.release()
617 with cond:
618 self.assertFalse(lock.acquire(False))
619
620 def test_unacquired_wait(self):
621 cond = self.condtype()
622 self.assertRaises(RuntimeError, cond.wait)
623
624 def test_unacquired_notify(self):
625 cond = self.condtype()
626 self.assertRaises(RuntimeError, cond.notify)
627
628 def _check_notify(self, cond):
629 # Note that this test is sensitive to timing. If the worker threads
630 # don't execute in a timely fashion, the main thread may think they
631 # are further along then they are. The main thread therefore issues
632 # wait_threads_blocked() statements to try to make sure that it doesn't
633 # race ahead of the workers.
634 # Secondly, this test assumes that condition variables are not subject
635 # to spurious wakeups. The absence of spurious wakeups is an implementation
636 # detail of Condition Variables in current CPython, but in general, not
637 # a guaranteed property of condition variables as a programming
638 # construct. In particular, it is possible that this can no longer
639 # be conveniently guaranteed should their implementation ever change.
640 ready = []
641 results1 = []
642 results2 = []
643 phase_num = 0
644 def f():
645 cond.acquire()
646 ready.append(phase_num)
647 result = cond.wait()
648
649 cond.release()
650 results1.append((result, phase_num))
651
652 cond.acquire()
653 ready.append(phase_num)
654
655 result = cond.wait()
656 cond.release()
657 results2.append((result, phase_num))
658
659 N = 5
660 with Bunch(f, N):
661 # first wait, to ensure all workers settle into cond.wait() before
662 # we continue. See issues #8799 and #30727.
663 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
664 if len(ready) >= N:
665 break
666
667 ready.clear()
668 self.assertEqual(results1, [])
669
670 # Notify 3 threads at first
671 count1 = 3
672 cond.acquire()
673 cond.notify(count1)
674 wait_threads_blocked(count1)
675
676 # Phase 1
677 phase_num = 1
678 cond.release()
679 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
680 if len(results1) >= count1:
681 break
682
683 self.assertEqual(results1, [(True, 1)] * count1)
684 self.assertEqual(results2, [])
685
686 # Wait until awaken workers are blocked on cond.wait()
687 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
688 if len(ready) >= count1 :
689 break
690
691 # Notify 5 threads: they might be in their first or second wait
692 cond.acquire()
693 cond.notify(5)
694 wait_threads_blocked(N)
695
696 # Phase 2
697 phase_num = 2
698 cond.release()
699 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
700 if len(results1) + len(results2) >= (N + count1):
701 break
702
703 count2 = N - count1
704 self.assertEqual(results1, [(True, 1)] * count1 + [(True, 2)] * count2)
705 self.assertEqual(results2, [(True, 2)] * count1)
706
707 # Make sure all workers settle into cond.wait()
708 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
709 if len(ready) >= N:
710 break
711
712 # Notify all threads: they are all in their second wait
713 cond.acquire()
714 cond.notify_all()
715 wait_threads_blocked(N)
716
717 # Phase 3
718 phase_num = 3
719 cond.release()
720 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
721 if len(results2) >= N:
722 break
723 self.assertEqual(results1, [(True, 1)] * count1 + [(True, 2)] * count2)
724 self.assertEqual(results2, [(True, 2)] * count1 + [(True, 3)] * count2)
725
726 def test_notify(self):
727 cond = self.condtype()
728 self._check_notify(cond)
729 # A second time, to check internal state is still ok.
730 self._check_notify(cond)
731
732 def test_timeout(self):
733 cond = self.condtype()
734 timeout = 0.5
735 results = []
736 def f():
737 cond.acquire()
738 t1 = time.monotonic()
739 result = cond.wait(timeout)
740 t2 = time.monotonic()
741 cond.release()
742 results.append((t2 - t1, result))
743
744 N = 5
745 with Bunch(f, N):
746 pass
747 self.assertEqual(len(results), N)
748
749 for dt, result in results:
750 self.assertTimeout(dt, timeout)
751 # Note that conceptually (that"s the condition variable protocol)
752 # a wait() may succeed even if no one notifies us and before any
753 # timeout occurs. Spurious wakeups can occur.
754 # This makes it hard to verify the result value.
755 # In practice, this implementation has no spurious wakeups.
756 self.assertFalse(result)
757
758 def test_waitfor(self):
759 cond = self.condtype()
760 state = 0
761 def f():
762 with cond:
763 result = cond.wait_for(lambda: state == 4)
764 self.assertTrue(result)
765 self.assertEqual(state, 4)
766
767 with Bunch(f, 1):
768 for i in range(4):
769 time.sleep(0.010)
770 with cond:
771 state += 1
772 cond.notify()
773
774 def test_waitfor_timeout(self):
775 cond = self.condtype()
776 state = 0
777 success = []
778 def f():
779 with cond:
780 dt = time.monotonic()
781 result = cond.wait_for(lambda : state==4, timeout=0.1)
782 dt = time.monotonic() - dt
783 self.assertFalse(result)
784 self.assertTimeout(dt, 0.1)
785 success.append(None)
786
787 with Bunch(f, 1):
788 # Only increment 3 times, so state == 4 is never reached.
789 for i in range(3):
790 time.sleep(0.010)
791 with cond:
792 state += 1
793 cond.notify()
794
795 self.assertEqual(len(success), 1)
796
797
798 class BaseSemaphoreTests(BaseTestCase):
799 """
800 Common tests for {bounded, unbounded} semaphore objects.
801 """
802
803 def test_constructor(self):
804 self.assertRaises(ValueError, self.semtype, value = -1)
805 self.assertRaises(ValueError, self.semtype, value = -sys.maxsize)
806
807 def test_acquire(self):
808 sem = self.semtype(1)
809 sem.acquire()
810 sem.release()
811 sem = self.semtype(2)
812 sem.acquire()
813 sem.acquire()
814 sem.release()
815 sem.release()
816
817 def test_acquire_destroy(self):
818 sem = self.semtype()
819 sem.acquire()
820 del sem
821
822 def test_acquire_contended(self):
823 sem_value = 7
824 sem = self.semtype(sem_value)
825 sem.acquire()
826
827 sem_results = []
828 results1 = []
829 results2 = []
830 phase_num = 0
831
832 def func():
833 sem_results.append(sem.acquire())
834 results1.append(phase_num)
835
836 sem_results.append(sem.acquire())
837 results2.append(phase_num)
838
839 def wait_count(count):
840 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
841 if len(results1) + len(results2) >= count:
842 break
843
844 N = 10
845 with Bunch(func, N):
846 # Phase 0
847 count1 = sem_value - 1
848 wait_count(count1)
849 self.assertEqual(results1 + results2, [0] * count1)
850
851 # Phase 1
852 phase_num = 1
853 for i in range(sem_value):
854 sem.release()
855 count2 = sem_value
856 wait_count(count1 + count2)
857 self.assertEqual(sorted(results1 + results2),
858 [0] * count1 + [1] * count2)
859
860 # Phase 2
861 phase_num = 2
862 count3 = (sem_value - 1)
863 for i in range(count3):
864 sem.release()
865 wait_count(count1 + count2 + count3)
866 self.assertEqual(sorted(results1 + results2),
867 [0] * count1 + [1] * count2 + [2] * count3)
868 # The semaphore is still locked
869 self.assertFalse(sem.acquire(False))
870
871 # Final release, to let the last thread finish
872 count4 = 1
873 sem.release()
874
875 self.assertEqual(sem_results,
876 [True] * (count1 + count2 + count3 + count4))
877
878 def test_multirelease(self):
879 sem_value = 7
880 sem = self.semtype(sem_value)
881 sem.acquire()
882
883 results1 = []
884 results2 = []
885 phase_num = 0
886 def func():
887 sem.acquire()
888 results1.append(phase_num)
889
890 sem.acquire()
891 results2.append(phase_num)
892
893 def wait_count(count):
894 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
895 if len(results1) + len(results2) >= count:
896 break
897
898 with Bunch(func, 10):
899 # Phase 0
900 count1 = sem_value - 1
901 wait_count(count1)
902 self.assertEqual(results1 + results2, [0] * count1)
903
904 # Phase 1
905 phase_num = 1
906 count2 = sem_value
907 sem.release(count2)
908 wait_count(count1 + count2)
909 self.assertEqual(sorted(results1 + results2),
910 [0] * count1 + [1] * count2)
911
912 # Phase 2
913 phase_num = 2
914 count3 = sem_value - 1
915 sem.release(count3)
916 wait_count(count1 + count2 + count3)
917 self.assertEqual(sorted(results1 + results2),
918 [0] * count1 + [1] * count2 + [2] * count3)
919 # The semaphore is still locked
920 self.assertFalse(sem.acquire(False))
921
922 # Final release, to let the last thread finish
923 sem.release()
924
925 def test_try_acquire(self):
926 sem = self.semtype(2)
927 self.assertTrue(sem.acquire(False))
928 self.assertTrue(sem.acquire(False))
929 self.assertFalse(sem.acquire(False))
930 sem.release()
931 self.assertTrue(sem.acquire(False))
932
933 def test_try_acquire_contended(self):
934 sem = self.semtype(4)
935 sem.acquire()
936 results = []
937 def f():
938 results.append(sem.acquire(False))
939 results.append(sem.acquire(False))
940 with Bunch(f, 5):
941 pass
942 # There can be a thread switch between acquiring the semaphore and
943 # appending the result, therefore results will not necessarily be
944 # ordered.
945 self.assertEqual(sorted(results), [False] * 7 + [True] * 3 )
946
947 def test_acquire_timeout(self):
948 sem = self.semtype(2)
949 self.assertRaises(ValueError, sem.acquire, False, timeout=1.0)
950 self.assertTrue(sem.acquire(timeout=0.005))
951 self.assertTrue(sem.acquire(timeout=0.005))
952 self.assertFalse(sem.acquire(timeout=0.005))
953 sem.release()
954 self.assertTrue(sem.acquire(timeout=0.005))
955 t = time.monotonic()
956 self.assertFalse(sem.acquire(timeout=0.5))
957 dt = time.monotonic() - t
958 self.assertTimeout(dt, 0.5)
959
960 def test_default_value(self):
961 # The default initial value is 1.
962 sem = self.semtype()
963 sem.acquire()
964 def f():
965 sem.acquire()
966 sem.release()
967
968 with Bunch(f, 1) as bunch:
969 # Thread blocked on sem.acquire()
970 wait_threads_blocked(1)
971 self.assertFalse(bunch.finished)
972
973 # Thread unblocked
974 sem.release()
975
976 def test_with(self):
977 sem = self.semtype(2)
978 def _with(err=None):
979 with sem:
980 self.assertTrue(sem.acquire(False))
981 sem.release()
982 with sem:
983 self.assertFalse(sem.acquire(False))
984 if err:
985 raise err
986 _with()
987 self.assertTrue(sem.acquire(False))
988 sem.release()
989 self.assertRaises(TypeError, _with, TypeError)
990 self.assertTrue(sem.acquire(False))
991 sem.release()
992
993 class SemaphoreTests(BaseSemaphoreTests):
994 """
995 Tests for unbounded semaphores.
996 """
997
998 def test_release_unacquired(self):
999 # Unbounded releases are allowed and increment the semaphore's value
1000 sem = self.semtype(1)
1001 sem.release()
1002 sem.acquire()
1003 sem.acquire()
1004 sem.release()
1005
1006 def test_repr(self):
1007 sem = self.semtype(3)
1008 self.assertRegex(repr(sem), r"<\w+\.Semaphore at .*: value=3>")
1009 sem.acquire()
1010 self.assertRegex(repr(sem), r"<\w+\.Semaphore at .*: value=2>")
1011 sem.release()
1012 sem.release()
1013 self.assertRegex(repr(sem), r"<\w+\.Semaphore at .*: value=4>")
1014
1015
1016 class BoundedSemaphoreTests(BaseSemaphoreTests):
1017 """
1018 Tests for bounded semaphores.
1019 """
1020
1021 def test_release_unacquired(self):
1022 # Cannot go past the initial value
1023 sem = self.semtype()
1024 self.assertRaises(ValueError, sem.release)
1025 sem.acquire()
1026 sem.release()
1027 self.assertRaises(ValueError, sem.release)
1028
1029 def test_repr(self):
1030 sem = self.semtype(3)
1031 self.assertRegex(repr(sem), r"<\w+\.BoundedSemaphore at .*: value=3/3>")
1032 sem.acquire()
1033 self.assertRegex(repr(sem), r"<\w+\.BoundedSemaphore at .*: value=2/3>")
1034
1035
1036 class BarrierTests(BaseTestCase):
1037 """
1038 Tests for Barrier objects.
1039 """
1040 N = 5
1041 defaultTimeout = 2.0
1042
1043 def setUp(self):
1044 self.barrier = self.barriertype(self.N, timeout=self.defaultTimeout)
1045
1046 def tearDown(self):
1047 self.barrier.abort()
1048
1049 def run_threads(self, f):
1050 with Bunch(f, self.N):
1051 pass
1052
1053 def multipass(self, results, n):
1054 m = self.barrier.parties
1055 self.assertEqual(m, self.N)
1056 for i in range(n):
1057 results[0].append(True)
1058 self.assertEqual(len(results[1]), i * m)
1059 self.barrier.wait()
1060 results[1].append(True)
1061 self.assertEqual(len(results[0]), (i + 1) * m)
1062 self.barrier.wait()
1063 self.assertEqual(self.barrier.n_waiting, 0)
1064 self.assertFalse(self.barrier.broken)
1065
1066 def test_constructor(self):
1067 self.assertRaises(ValueError, self.barriertype, parties=0)
1068 self.assertRaises(ValueError, self.barriertype, parties=-1)
1069
1070 def test_barrier(self, passes=1):
1071 """
1072 Test that a barrier is passed in lockstep
1073 """
1074 results = [[],[]]
1075 def f():
1076 self.multipass(results, passes)
1077 self.run_threads(f)
1078
1079 def test_barrier_10(self):
1080 """
1081 Test that a barrier works for 10 consecutive runs
1082 """
1083 return self.test_barrier(10)
1084
1085 def test_wait_return(self):
1086 """
1087 test the return value from barrier.wait
1088 """
1089 results = []
1090 def f():
1091 r = self.barrier.wait()
1092 results.append(r)
1093
1094 self.run_threads(f)
1095 self.assertEqual(sum(results), sum(range(self.N)))
1096
1097 def test_action(self):
1098 """
1099 Test the 'action' callback
1100 """
1101 results = []
1102 def action():
1103 results.append(True)
1104 barrier = self.barriertype(self.N, action)
1105 def f():
1106 barrier.wait()
1107 self.assertEqual(len(results), 1)
1108
1109 self.run_threads(f)
1110
1111 def test_abort(self):
1112 """
1113 Test that an abort will put the barrier in a broken state
1114 """
1115 results1 = []
1116 results2 = []
1117 def f():
1118 try:
1119 i = self.barrier.wait()
1120 if i == self.N//2:
1121 raise RuntimeError
1122 self.barrier.wait()
1123 results1.append(True)
1124 except threading.BrokenBarrierError:
1125 results2.append(True)
1126 except RuntimeError:
1127 self.barrier.abort()
1128 pass
1129
1130 self.run_threads(f)
1131 self.assertEqual(len(results1), 0)
1132 self.assertEqual(len(results2), self.N-1)
1133 self.assertTrue(self.barrier.broken)
1134
1135 def test_reset(self):
1136 """
1137 Test that a 'reset' on a barrier frees the waiting threads
1138 """
1139 results1 = []
1140 results2 = []
1141 results3 = []
1142 def f():
1143 i = self.barrier.wait()
1144 if i == self.N//2:
1145 # Wait until the other threads are all in the barrier.
1146 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
1147 if self.barrier.n_waiting >= (self.N - 1):
1148 break
1149 self.barrier.reset()
1150 else:
1151 try:
1152 self.barrier.wait()
1153 results1.append(True)
1154 except threading.BrokenBarrierError:
1155 results2.append(True)
1156 # Now, pass the barrier again
1157 self.barrier.wait()
1158 results3.append(True)
1159
1160 self.run_threads(f)
1161 self.assertEqual(len(results1), 0)
1162 self.assertEqual(len(results2), self.N-1)
1163 self.assertEqual(len(results3), self.N)
1164
1165
1166 def test_abort_and_reset(self):
1167 """
1168 Test that a barrier can be reset after being broken.
1169 """
1170 results1 = []
1171 results2 = []
1172 results3 = []
1173 barrier2 = self.barriertype(self.N)
1174 def f():
1175 try:
1176 i = self.barrier.wait()
1177 if i == self.N//2:
1178 raise RuntimeError
1179 self.barrier.wait()
1180 results1.append(True)
1181 except threading.BrokenBarrierError:
1182 results2.append(True)
1183 except RuntimeError:
1184 self.barrier.abort()
1185 pass
1186 # Synchronize and reset the barrier. Must synchronize first so
1187 # that everyone has left it when we reset, and after so that no
1188 # one enters it before the reset.
1189 if barrier2.wait() == self.N//2:
1190 self.barrier.reset()
1191 barrier2.wait()
1192 self.barrier.wait()
1193 results3.append(True)
1194
1195 self.run_threads(f)
1196 self.assertEqual(len(results1), 0)
1197 self.assertEqual(len(results2), self.N-1)
1198 self.assertEqual(len(results3), self.N)
1199
1200 def test_timeout(self):
1201 """
1202 Test wait(timeout)
1203 """
1204 def f():
1205 i = self.barrier.wait()
1206 if i == self.N // 2:
1207 # One thread is late!
1208 time.sleep(self.defaultTimeout / 2)
1209 # Default timeout is 2.0, so this is shorter.
1210 self.assertRaises(threading.BrokenBarrierError,
1211 self.barrier.wait, self.defaultTimeout / 4)
1212 self.run_threads(f)
1213
1214 def test_default_timeout(self):
1215 """
1216 Test the barrier's default timeout
1217 """
1218 timeout = 0.100
1219 barrier = self.barriertype(2, timeout=timeout)
1220 def f():
1221 self.assertRaises(threading.BrokenBarrierError,
1222 barrier.wait)
1223
1224 start_time = time.monotonic()
1225 with Bunch(f, 1):
1226 pass
1227 dt = time.monotonic() - start_time
1228 self.assertGreaterEqual(dt, timeout)
1229
1230 def test_single_thread(self):
1231 b = self.barriertype(1)
1232 b.wait()
1233 b.wait()
1234
1235 def test_repr(self):
1236 barrier = self.barriertype(3)
1237 timeout = support.LONG_TIMEOUT
1238 self.assertRegex(repr(barrier), r"<\w+\.Barrier at .*: waiters=0/3>")
1239 def f():
1240 barrier.wait(timeout)
1241
1242 N = 2
1243 with Bunch(f, N):
1244 # Threads blocked on barrier.wait()
1245 for _ in support.sleeping_retry(support.SHORT_TIMEOUT):
1246 if barrier.n_waiting >= N:
1247 break
1248 self.assertRegex(repr(barrier),
1249 r"<\w+\.Barrier at .*: waiters=2/3>")
1250
1251 # Threads unblocked
1252 barrier.wait(timeout)
1253
1254 self.assertRegex(repr(barrier),
1255 r"<\w+\.Barrier at .*: waiters=0/3>")
1256
1257 # Abort the barrier
1258 barrier.abort()
1259 self.assertRegex(repr(barrier),
1260 r"<\w+\.Barrier at .*: broken>")