Debian Codegraph @

Lib/test/test_importlib/test_locks.py

raw
1 from test.test_importlib import util as test_util
2
3 init = test_util.import_importlib('importlib')
4
5 import sys
6 import threading
7 import unittest
8 import weakref
9
10 from test import support
11 from test.support import threading_helper
12 from test import lock_tests
13
14
15 threading_helper.requires_working_threading(module=True)
16
17
18 class ModuleLockAsRLockTests:
19 locktype = classmethod(lambda cls: cls.LockType("some_lock"))
20
21 # _is_owned() unsupported
22 test__is_owned = None
23 # acquire(blocking=False) unsupported
24 test_try_acquire = None
25 test_try_acquire_contended = None
26 # `with` unsupported
27 test_with = None
28 # acquire(timeout=...) unsupported
29 test_timeout = None
30 # _release_save() unsupported
31 test_release_save_unacquired = None
32 # _recursion_count() unsupported
33 test_recursion_count = None
34 # lock status in repr unsupported
35 test_repr = None
36 test_locked_repr = None
37
38 def tearDown(self):
39 for splitinit in init.values():
40 splitinit._bootstrap._blocking_on.clear()
41
42
43 LOCK_TYPES = {kind: splitinit._bootstrap._ModuleLock
44 for kind, splitinit in init.items()}
45
46 (Frozen_ModuleLockAsRLockTests,
47 Source_ModuleLockAsRLockTests
48 ) = test_util.test_both(ModuleLockAsRLockTests, lock_tests.RLockTests,
49 LockType=LOCK_TYPES)
50
51
52 class DeadlockAvoidanceTests:
53
54 def setUp(self):
55 try:
56 self.old_switchinterval = sys.getswitchinterval()
57 support.setswitchinterval(0.000001)
58 except AttributeError:
59 self.old_switchinterval = None
60
61 def tearDown(self):
62 if self.old_switchinterval is not None:
63 sys.setswitchinterval(self.old_switchinterval)
64
65 def run_deadlock_avoidance_test(self, create_deadlock):
66 NLOCKS = 10
67 locks = [self.LockType(str(i)) for i in range(NLOCKS)]
68 pairs = [(locks[i], locks[(i+1)%NLOCKS]) for i in range(NLOCKS)]
69 if create_deadlock:
70 NTHREADS = NLOCKS
71 else:
72 NTHREADS = NLOCKS - 1
73 barrier = threading.Barrier(NTHREADS)
74 results = []
75
76 def _acquire(lock):
77 """Try to acquire the lock. Return True on success,
78 False on deadlock."""
79 try:
80 lock.acquire()
81 except self.DeadlockError:
82 return False
83 else:
84 return True
85
86 def f():
87 a, b = pairs.pop()
88 ra = _acquire(a)
89 barrier.wait()
90 rb = _acquire(b)
91 results.append((ra, rb))
92 if rb:
93 b.release()
94 if ra:
95 a.release()
96 with lock_tests.Bunch(f, NTHREADS):
97 pass
98 self.assertEqual(len(results), NTHREADS)
99 return results
100
101 def test_deadlock(self):
102 results = self.run_deadlock_avoidance_test(True)
103 # At least one of the threads detected a potential deadlock on its
104 # second acquire() call. It may be several of them, because the
105 # deadlock avoidance mechanism is conservative.
106 nb_deadlocks = results.count((True, False))
107 self.assertGreaterEqual(nb_deadlocks, 1)
108 self.assertEqual(results.count((True, True)), len(results) - nb_deadlocks)
109
110 def test_no_deadlock(self):
111 results = self.run_deadlock_avoidance_test(False)
112 self.assertEqual(results.count((True, False)), 0)
113 self.assertEqual(results.count((True, True)), len(results))
114
115
116 DEADLOCK_ERRORS = {kind: splitinit._bootstrap._DeadlockError
117 for kind, splitinit in init.items()}
118
119 (Frozen_DeadlockAvoidanceTests,
120 Source_DeadlockAvoidanceTests
121 ) = test_util.test_both(DeadlockAvoidanceTests,
122 LockType=LOCK_TYPES,
123 DeadlockError=DEADLOCK_ERRORS)
124
125
126 class LifetimeTests:
127
128 @property
129 def bootstrap(self):
130 return self.init._bootstrap
131
132 def test_lock_lifetime(self):
133 name = "xyzzy"
134 self.assertNotIn(name, self.bootstrap._module_locks)
135 lock = self.bootstrap._get_module_lock(name)
136 self.assertIn(name, self.bootstrap._module_locks)
137 wr = weakref.ref(lock)
138 del lock
139 support.gc_collect()
140 self.assertNotIn(name, self.bootstrap._module_locks)
141 self.assertIsNone(wr())
142
143 def test_all_locks(self):
144 support.gc_collect()
145 self.assertEqual(0, len(self.bootstrap._module_locks),
146 self.bootstrap._module_locks)
147
148
149 (Frozen_LifetimeTests,
150 Source_LifetimeTests
151 ) = test_util.test_both(LifetimeTests, init=init)
152
153
154 def setUpModule():
155 thread_info = threading_helper.threading_setup()
156 unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info)
157
158
159 if __name__ == '__main__':
160 unittest.main()