summaryrefslogtreecommitdiff
path: root/Lib/test/lock_tests.py
diff options
context:
space:
mode:
authorVictor Stinner <vstinner@redhat.com>2018-06-01 15:51:02 +0200
committerGitHub <noreply@github.com>2018-06-01 15:51:02 +0200
commit5dbb48aaac0ff74648b355ebdde222856004b1ef (patch)
treec47bd93d6333eb31b1461d75c5463ef058d666b2 /Lib/test/lock_tests.py
parent95681c7a7ddd436ba7d6c10d1202c33dd6bd648b (diff)
downloadcpython-git-5dbb48aaac0ff74648b355ebdde222856004b1ef.tar.gz
[3.6] bpo-31234: Add test.support.wait_threads_exit() (GH-3578) (GH-7315)
* bpo-31234: Add test.support.wait_threads_exit() (GH-3578) Use _thread.count() to wait until threads exit. The new context manager prevents the "dangling thread" warning. (cherry picked from commit ff40ecda73178dfcad24e26240d684356ef20793) * bpo-31234: Try to fix lock_tests warning (#3557) Try to fix the "Warning -- threading_cleanup() failed to cleanup 1 threads" warning in test.lock_tests: wait a little bit longer to give time to the threads to complete. Warning seen on test_thread and test_importlib. (cherry picked from commit 096ae3373abac2c8b3a26a3fe33cc8bd4cbccd4e)
Diffstat (limited to 'Lib/test/lock_tests.py')
-rw-r--r--Lib/test/lock_tests.py26
1 files changed, 18 insertions, 8 deletions
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py
index c177570248..5b1f033c6f 100644
--- a/Lib/test/lock_tests.py
+++ b/Lib/test/lock_tests.py
@@ -31,6 +31,9 @@ class Bunch(object):
self.started = []
self.finished = []
self._can_exit = not wait_before_exit
+ self.wait_thread = support.wait_threads_exit()
+ self.wait_thread.__enter__()
+
def task():
tid = threading.get_ident()
self.started.append(tid)
@@ -40,6 +43,7 @@ class Bunch(object):
self.finished.append(tid)
while not self._can_exit:
_wait()
+
try:
for i in range(n):
start_new_thread(task, ())
@@ -54,6 +58,8 @@ class Bunch(object):
def wait_for_finished(self):
while len(self.finished) < self.n:
_wait()
+ # Wait for threads exit
+ self.wait_thread.__exit__(None, None, None)
def do_finish(self):
self._can_exit = True
@@ -220,20 +226,23 @@ class LockTests(BaseLockTests):
# Lock needs to be released before re-acquiring.
lock = self.locktype()
phase = []
+
def f():
lock.acquire()
phase.append(None)
lock.acquire()
phase.append(None)
- start_new_thread(f, ())
- while len(phase) == 0:
- _wait()
- _wait()
- self.assertEqual(len(phase), 1)
- lock.release()
- while len(phase) == 1:
+
+ with support.wait_threads_exit():
+ start_new_thread(f, ())
+ while len(phase) == 0:
+ _wait()
_wait()
- self.assertEqual(len(phase), 2)
+ self.assertEqual(len(phase), 1)
+ lock.release()
+ while len(phase) == 1:
+ _wait()
+ self.assertEqual(len(phase), 2)
def test_different_thread(self):
# Lock can be released from a different thread.
@@ -304,6 +313,7 @@ class RLockTests(BaseLockTests):
self.assertRaises(RuntimeError, lock.release)
finally:
b.do_finish()
+ b.wait_for_finished()
def test__is_owned(self):
lock = self.locktype()