diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2017-09-14 20:07:24 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-09-14 20:07:24 (GMT) |
commit | ff40ecda73178dfcad24e26240d684356ef20793 (patch) | |
tree | 0533f9354f838a97a67fc22749b1b1b7374691fb /Lib/test/lock_tests.py | |
parent | b8c7be2c523b012e57915182543d06657161057f (diff) | |
download | cpython-ff40ecda73178dfcad24e26240d684356ef20793.zip cpython-ff40ecda73178dfcad24e26240d684356ef20793.tar.gz cpython-ff40ecda73178dfcad24e26240d684356ef20793.tar.bz2 |
bpo-31234: Add test.support.wait_threads_exit() (#3578)
Use _thread.count() to wait until threads exit. The new context
manager prevents the "dangling thread" warning.
Diffstat (limited to 'Lib/test/lock_tests.py')
-rw-r--r-- | Lib/test/lock_tests.py | 32 |
1 files changed, 17 insertions, 15 deletions
diff --git a/Lib/test/lock_tests.py b/Lib/test/lock_tests.py index e8fa4f9..a1ea96d 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,13 +58,8 @@ class Bunch(object): def wait_for_finished(self): while len(self.finished) < self.n: _wait() - # Wait a little bit longer to prevent the "threading_cleanup() - # failed to cleanup X threads" warning. The loop above is a weak - # synchronization. At the C level, t_bootstrap() can still be - # running and so _thread.count() still accounts the "almost dead" - # thead. - for _ in range(self.n): - _wait() + # Wait for threads exit + self.wait_thread.__exit__(None, None, None) def do_finish(self): self._can_exit = True @@ -227,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. |