| 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 |
test_repr_count = None |
| 38 |
|
| 39 |
def tearDown(self): |
| 40 |
for splitinit in init.values(): |
| 41 |
splitinit._bootstrap._blocking_on.clear() |
| 42 |
|
| 43 |
|
| 44 |
LOCK_TYPES = {kind: splitinit._bootstrap._ModuleLock |
| 45 |
for kind, splitinit in init.items()} |
| 46 |
|
| 47 |
(Frozen_ModuleLockAsRLockTests, |
| 48 |
Source_ModuleLockAsRLockTests |
| 49 |
) = test_util.test_both(ModuleLockAsRLockTests, lock_tests.RLockTests, |
| 50 |
LockType=LOCK_TYPES) |
| 51 |
|
| 52 |
|
| 53 |
class DeadlockAvoidanceTests: |
| 54 |
|
| 55 |
def setUp(self): |
| 56 |
try: |
| 57 |
self.old_switchinterval = sys.getswitchinterval() |
| 58 |
support.setswitchinterval(0.000001) |
| 59 |
except AttributeError: |
| 60 |
self.old_switchinterval = None |
| 61 |
|
| 62 |
def tearDown(self): |
| 63 |
if self.old_switchinterval is not None: |
| 64 |
sys.setswitchinterval(self.old_switchinterval) |
| 65 |
|
| 66 |
def run_deadlock_avoidance_test(self, create_deadlock): |
| 67 |
NLOCKS = 10 |
| 68 |
locks = [self.LockType(str(i)) for i in range(NLOCKS)] |
| 69 |
pairs = [(locks[i], locks[(i+1)%NLOCKS]) for i in range(NLOCKS)] |
| 70 |
if create_deadlock: |
| 71 |
NTHREADS = NLOCKS |
| 72 |
else: |
| 73 |
NTHREADS = NLOCKS - 1 |
| 74 |
barrier = threading.Barrier(NTHREADS) |
| 75 |
results = [] |
| 76 |
|
| 77 |
def _acquire(lock): |
| 78 |
"""Try to acquire the lock. Return True on success, |
| 79 |
False on deadlock.""" |
| 80 |
try: |
| 81 |
lock.acquire() |
| 82 |
except self.DeadlockError: |
| 83 |
return False |
| 84 |
else: |
| 85 |
return True |
| 86 |
|
| 87 |
def f(): |
| 88 |
a, b = pairs.pop() |
| 89 |
ra = _acquire(a) |
| 90 |
barrier.wait() |
| 91 |
rb = _acquire(b) |
| 92 |
results.append((ra, rb)) |
| 93 |
if rb: |
| 94 |
b.release() |
| 95 |
if ra: |
| 96 |
a.release() |
| 97 |
with lock_tests.Bunch(f, NTHREADS): |
| 98 |
pass |
| 99 |
self.assertEqual(len(results), NTHREADS) |
| 100 |
return results |
| 101 |
|
| 102 |
def test_deadlock(self): |
| 103 |
results = self.run_deadlock_avoidance_test(True) |
| 104 |
# At least one of the threads detected a potential deadlock on its |
| 105 |
# second acquire() call. It may be several of them, because the |
| 106 |
# deadlock avoidance mechanism is conservative. |
| 107 |
nb_deadlocks = results.count((True, False)) |
| 108 |
self.assertGreaterEqual(nb_deadlocks, 1) |
| 109 |
self.assertEqual(results.count((True, True)), len(results) - nb_deadlocks) |
| 110 |
|
| 111 |
def test_no_deadlock(self): |
| 112 |
results = self.run_deadlock_avoidance_test(False) |
| 113 |
self.assertEqual(results.count((True, False)), 0) |
| 114 |
self.assertEqual(results.count((True, True)), len(results)) |
| 115 |
|
| 116 |
|
| 117 |
DEADLOCK_ERRORS = {kind: splitinit._bootstrap._DeadlockError |
| 118 |
for kind, splitinit in init.items()} |
| 119 |
|
| 120 |
(Frozen_DeadlockAvoidanceTests, |
| 121 |
Source_DeadlockAvoidanceTests |
| 122 |
) = test_util.test_both(DeadlockAvoidanceTests, |
| 123 |
LockType=LOCK_TYPES, |
| 124 |
DeadlockError=DEADLOCK_ERRORS) |
| 125 |
|
| 126 |
|
| 127 |
class LifetimeTests: |
| 128 |
|
| 129 |
@property |
| 130 |
def bootstrap(self): |
| 131 |
return self.init._bootstrap |
| 132 |
|
| 133 |
def test_lock_lifetime(self): |
| 134 |
name = "xyzzy" |
| 135 |
self.assertNotIn(name, self.bootstrap._module_locks) |
| 136 |
lock = self.bootstrap._get_module_lock(name) |
| 137 |
self.assertIn(name, self.bootstrap._module_locks) |
| 138 |
wr = weakref.ref(lock) |
| 139 |
del lock |
| 140 |
support.gc_collect() |
| 141 |
self.assertNotIn(name, self.bootstrap._module_locks) |
| 142 |
self.assertIsNone(wr()) |
| 143 |
|
| 144 |
def test_all_locks(self): |
| 145 |
support.gc_collect() |
| 146 |
self.assertEqual(0, len(self.bootstrap._module_locks), |
| 147 |
self.bootstrap._module_locks) |
| 148 |
|
| 149 |
|
| 150 |
(Frozen_LifetimeTests, |
| 151 |
Source_LifetimeTests |
| 152 |
) = test_util.test_both(LifetimeTests, init=init) |
| 153 |
|
| 154 |
|
| 155 |
def setUpModule(): |
| 156 |
thread_info = threading_helper.threading_setup() |
| 157 |
unittest.addModuleCleanup(threading_helper.threading_cleanup, *thread_info) |
| 158 |
|
| 159 |
|
| 160 |
if __name__ == '__main__': |
| 161 |
unittest.main() |