bpo-40275: Adding threading_helper submodule in test.support (GH-20263)

This commit is contained in:
Hai Shi 2020-05-28 06:10:27 +08:00 committed by GitHub
parent 7d80b35af1
commit e80697d687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
46 changed files with 483 additions and 428 deletions

View File

@ -838,18 +838,6 @@ The :mod:`test.support` module defines the following functions:
.. versionadded:: 3.9 .. versionadded:: 3.9
.. function:: wait_threads_exit(timeout=60.0)
Context manager to wait until all threads created in the ``with`` statement
exit.
.. function:: start_threads(threads, unlock=None)
Context manager to start *threads*. It attempts to join the threads upon
exit.
.. function:: calcobjsize(fmt) .. function:: calcobjsize(fmt)
Return :func:`struct.calcsize` for ``nP{fmt}0n`` or, if ``gettotalrefcount`` Return :func:`struct.calcsize` for ``nP{fmt}0n`` or, if ``gettotalrefcount``
@ -988,11 +976,6 @@ The :mod:`test.support` module defines the following functions:
the trace function. the trace function.
.. decorator:: reap_threads(func)
Decorator to ensure the threads are cleaned up even if the test fails.
.. decorator:: bigmemtest(size, memuse, dry_run=True) .. decorator:: bigmemtest(size, memuse, dry_run=True)
Decorator for bigmem tests. Decorator for bigmem tests.
@ -1110,23 +1093,6 @@ The :mod:`test.support` module defines the following functions:
preserve internal cache. preserve internal cache.
.. function:: threading_setup()
Return current thread count and copy of dangling threads.
.. function:: threading_cleanup(*original_values)
Cleanup up threads not specified in *original_values*. Designed to emit
a warning if a test leaves running threads in the background.
.. function:: join_thread(thread, timeout=30.0)
Join a *thread* within *timeout*. Raise an :exc:`AssertionError` if thread
is still alive after *timeout* seconds.
.. function:: reap_children() .. function:: reap_children()
Use this at the end of ``test_main`` whenever sub-processes are started. Use this at the end of ``test_main`` whenever sub-processes are started.
@ -1140,39 +1106,6 @@ The :mod:`test.support` module defines the following functions:
is raised. is raised.
.. function:: catch_threading_exception()
Context manager catching :class:`threading.Thread` exception using
:func:`threading.excepthook`.
Attributes set when an exception is catched:
* ``exc_type``
* ``exc_value``
* ``exc_traceback``
* ``thread``
See :func:`threading.excepthook` documentation.
These attributes are deleted at the context manager exit.
Usage::
with support.catch_threading_exception() as cm:
# code spawning a thread which raises an exception
...
# check the thread exception, use cm attributes:
# exc_type, exc_value, exc_traceback, thread
...
# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
# exists at this point
# (to avoid reference cycles)
.. versionadded:: 3.8
.. function:: catch_unraisable_exception() .. function:: catch_unraisable_exception()
Context manager catching unraisable exception using Context manager catching unraisable exception using
@ -1628,3 +1561,81 @@ The module defines the following class:
.. method:: BytecodeTestCase.assertNotInBytecode(x, opname, argval=_UNSPECIFIED) .. method:: BytecodeTestCase.assertNotInBytecode(x, opname, argval=_UNSPECIFIED)
Throws :exc:`AssertionError` if *opname* is found. Throws :exc:`AssertionError` if *opname* is found.
:mod:`test.support.threading_helper` --- Utilities for threading tests
======================================================================
.. module:: test.support.threading_helper
:synopsis: Support for threading tests.
The :mod:`test.support.threading_helper` module provides support for threading tests.
.. versionadded:: 3.10
.. function:: join_thread(thread, timeout=None)
Join a *thread* within *timeout*. Raise an :exc:`AssertionError` if thread
is still alive after *timeout* seconds.
.. decorator:: reap_threads(func)
Decorator to ensure the threads are cleaned up even if the test fails.
.. function:: start_threads(threads, unlock=None)
Context manager to start *threads*. It attempts to join the threads upon
exit.
.. function:: threading_cleanup(*original_values)
Cleanup up threads not specified in *original_values*. Designed to emit
a warning if a test leaves running threads in the background.
.. function:: threading_setup()
Return current thread count and copy of dangling threads.
.. function:: wait_threads_exit(timeout=None)
Context manager to wait until all threads created in the ``with`` statement
exit.
.. function:: catch_threading_exception()
Context manager catching :class:`threading.Thread` exception using
:func:`threading.excepthook`.
Attributes set when an exception is catched:
* ``exc_type``
* ``exc_value``
* ``exc_traceback``
* ``thread``
See :func:`threading.excepthook` documentation.
These attributes are deleted at the context manager exit.
Usage::
with threading_helper.catch_threading_exception() as cm:
# code spawning a thread which raises an exception
...
# check the thread exception, use cm attributes:
# exc_type, exc_value, exc_traceback, thread
...
# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
# exists at this point
# (to avoid reference cycles)
.. versionadded:: 3.8

View File

@ -27,6 +27,7 @@ import test.support
import test.support.script_helper import test.support.script_helper
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
# Skip tests if _multiprocessing wasn't built. # Skip tests if _multiprocessing wasn't built.
@ -81,7 +82,7 @@ def close_queue(queue):
def join_process(process): def join_process(process):
# Since multiprocessing.Process has the same API than threading.Thread # Since multiprocessing.Process has the same API than threading.Thread
# (join() and is_alive(), the support function can be reused # (join() and is_alive(), the support function can be reused
support.join_thread(process) threading_helper.join_thread(process)
if os.name == "posix": if os.name == "posix":
@ -4234,7 +4235,7 @@ class _TestFinalize(BaseTestCase):
gc.set_threshold(5, 5, 5) gc.set_threshold(5, 5, 5)
threads = [threading.Thread(target=run_finalizers), threads = [threading.Thread(target=run_finalizers),
threading.Thread(target=make_finalizers)] threading.Thread(target=make_finalizers)]
with test.support.start_threads(threads): with threading_helper.start_threads(threads):
time.sleep(4.0) # Wait a bit to trigger race condition time.sleep(4.0) # Wait a bit to trigger race condition
finish = True finish = True
if exc is not None: if exc is not None:

View File

@ -12,6 +12,7 @@ active threads survive in the child after a fork(); this is an error.
import os, sys, time, unittest import os, sys, time, unittest
import threading import threading
from test import support from test import support
from test.support import threading_helper
LONGSLEEP = 2 LONGSLEEP = 2
@ -21,7 +22,7 @@ NUM_THREADS = 4
class ForkWait(unittest.TestCase): class ForkWait(unittest.TestCase):
def setUp(self): def setUp(self):
self._threading_key = support.threading_setup() self._threading_key = threading_helper.threading_setup()
self.alive = {} self.alive = {}
self.stop = 0 self.stop = 0
self.threads = [] self.threads = []
@ -33,7 +34,7 @@ class ForkWait(unittest.TestCase):
thread.join() thread.join()
thread = None thread = None
self.threads.clear() self.threads.clear()
support.threading_cleanup(*self._threading_key) threading_helper.threading_cleanup(*self._threading_key)
def f(self, id): def f(self, id):
while not self.stop: while not self.stop:

View File

@ -11,6 +11,7 @@ import unittest
import weakref import weakref
from test import support from test import support
from test.support import threading_helper
requires_fork = unittest.skipUnless(hasattr(os, 'fork'), requires_fork = unittest.skipUnless(hasattr(os, 'fork'),
@ -37,7 +38,7 @@ class Bunch(object):
self.started = [] self.started = []
self.finished = [] self.finished = []
self._can_exit = not wait_before_exit self._can_exit = not wait_before_exit
self.wait_thread = support.wait_threads_exit() self.wait_thread = threading_helper.wait_threads_exit()
self.wait_thread.__enter__() self.wait_thread.__enter__()
def task(): def task():
@ -73,10 +74,10 @@ class Bunch(object):
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self._threads = support.threading_setup() self._threads = threading_helper.threading_setup()
def tearDown(self): def tearDown(self):
support.threading_cleanup(*self._threads) threading_helper.threading_cleanup(*self._threads)
support.reap_children() support.reap_children()
def assertTimeout(self, actual, expected): def assertTimeout(self, actual, expected):
@ -239,7 +240,7 @@ class LockTests(BaseLockTests):
lock.acquire() lock.acquire()
phase.append(None) phase.append(None)
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
start_new_thread(f, ()) start_new_thread(f, ())
while len(phase) == 0: while len(phase) == 0:
_wait() _wait()

View File

@ -29,8 +29,9 @@ except ImportError:
from test import support from test import support
from test.support import ( from test.support import (
TestFailed, TESTFN, run_with_locale, no_tracing, TestFailed, TESTFN, run_with_locale, no_tracing,
_2G, _4G, bigmemtest, reap_threads, forget, _2G, _4G, bigmemtest, forget,
) )
from test.support import threading_helper
from pickle import bytes_types from pickle import bytes_types
@ -1350,7 +1351,7 @@ class AbstractUnpickleTests(unittest.TestCase):
for p in badpickles: for p in badpickles:
self.check_unpickling_error(self.truncated_errors, p) self.check_unpickling_error(self.truncated_errors, p)
@reap_threads @threading_helper.reap_threads
def test_unpickle_module_race(self): def test_unpickle_module_race(self):
# https://bugs.python.org/issue34572 # https://bugs.python.org/issue34572
locker_module = dedent(""" locker_module = dedent("""

View File

@ -19,8 +19,6 @@ import struct
import subprocess import subprocess
import sys import sys
import sysconfig import sysconfig
import _thread
import threading
import time import time
import types import types
import unittest import unittest
@ -62,8 +60,6 @@ __all__ = [
"open_urlresource", "open_urlresource",
# processes # processes
'temp_umask', "reap_children", 'temp_umask', "reap_children",
# threads
"threading_setup", "threading_cleanup", "reap_threads", "start_threads",
# miscellaneous # miscellaneous
"check_warnings", "check_no_resource_warning", "check_no_warnings", "check_warnings", "check_no_resource_warning", "check_no_warnings",
"EnvironmentVarGuard", "EnvironmentVarGuard",
@ -1991,120 +1987,14 @@ def modules_cleanup(oldmodules):
# Implicitly imported *real* modules should be left alone (see issue 10556). # Implicitly imported *real* modules should be left alone (see issue 10556).
sys.modules.update(oldmodules) sys.modules.update(oldmodules)
#=======================================================================
# Threading support to prevent reporting refleaks when running regrtest.py -R
# Flag used by saved_test_environment of test.libregrtest.save_env, # Flag used by saved_test_environment of test.libregrtest.save_env,
# to check if a test modified the environment. The flag should be set to False # to check if a test modified the environment. The flag should be set to False
# before running a new test. # before running a new test.
# #
# For example, threading_cleanup() sets the flag is the function fails # For example, threading_helper.threading_cleanup() sets the flag is the function fails
# to cleanup threads. # to cleanup threads.
environment_altered = False environment_altered = False
# NOTE: we use thread._count() rather than threading.enumerate() (or the
# moral equivalent thereof) because a threading.Thread object is still alive
# until its __bootstrap() method has returned, even after it has been
# unregistered from the threading module.
# thread._count(), on the other hand, only gets decremented *after* the
# __bootstrap() method has returned, which gives us reliable reference counts
# at the end of a test run.
def threading_setup():
return _thread._count(), threading._dangling.copy()
def threading_cleanup(*original_values):
global environment_altered
_MAX_COUNT = 100
for count in range(_MAX_COUNT):
values = _thread._count(), threading._dangling
if values == original_values:
break
if not count:
# Display a warning at the first iteration
environment_altered = True
dangling_threads = values[1]
print_warning(f"threading_cleanup() failed to cleanup "
f"{values[0] - original_values[0]} threads "
f"(count: {values[0]}, "
f"dangling: {len(dangling_threads)})")
for thread in dangling_threads:
print_warning(f"Dangling thread: {thread!r}")
# Don't hold references to threads
dangling_threads = None
values = None
time.sleep(0.01)
gc_collect()
def reap_threads(func):
"""Use this function when threads are being used. This will
ensure that the threads are cleaned up even when the test fails.
"""
@functools.wraps(func)
def decorator(*args):
key = threading_setup()
try:
return func(*args)
finally:
threading_cleanup(*key)
return decorator
@contextlib.contextmanager
def wait_threads_exit(timeout=None):
"""
bpo-31234: Context manager to wait until all threads created in the with
statement exit.
Use _thread.count() to check if threads exited. Indirectly, wait until
threads exit the internal t_bootstrap() C function of the _thread module.
threading_setup() and threading_cleanup() are designed to emit a warning
if a test leaves running threads in the background. This context manager
is designed to cleanup threads started by the _thread.start_new_thread()
which doesn't allow to wait for thread exit, whereas thread.Thread has a
join() method.
"""
if timeout is None:
timeout = SHORT_TIMEOUT
old_count = _thread._count()
try:
yield
finally:
start_time = time.monotonic()
deadline = start_time + timeout
while True:
count = _thread._count()
if count <= old_count:
break
if time.monotonic() > deadline:
dt = time.monotonic() - start_time
msg = (f"wait_threads() failed to cleanup {count - old_count} "
f"threads after {dt:.1f} seconds "
f"(count: {count}, old count: {old_count})")
raise AssertionError(msg)
time.sleep(0.010)
gc_collect()
def join_thread(thread, timeout=None):
"""Join a thread. Raise an AssertionError if the thread is still alive
after timeout seconds.
"""
if timeout is None:
timeout = SHORT_TIMEOUT
thread.join(timeout)
if thread.is_alive():
msg = f"failed to join the thread in {timeout:.1f} seconds"
raise AssertionError(msg)
def reap_children(): def reap_children():
"""Use this function at the end of test_main() whenever sub-processes """Use this function at the end of test_main() whenever sub-processes
are started. This will help ensure that no extra children (zombies) are started. This will help ensure that no extra children (zombies)
@ -2133,43 +2023,6 @@ def reap_children():
environment_altered = True environment_altered = True
@contextlib.contextmanager
def start_threads(threads, unlock=None):
import faulthandler
threads = list(threads)
started = []
try:
try:
for t in threads:
t.start()
started.append(t)
except:
if verbose:
print("Can't start %d threads, only %d threads started" %
(len(threads), len(started)))
raise
yield
finally:
try:
if unlock:
unlock()
endtime = starttime = time.monotonic()
for timeout in range(1, 16):
endtime += 60
for t in started:
t.join(max(endtime - time.monotonic(), 0.01))
started = [t for t in started if t.is_alive()]
if not started:
break
if verbose:
print('Unable to join %d threads during a period of '
'%d minutes' % (len(started), timeout))
finally:
started = [t for t in started if t.is_alive()]
if started:
faulthandler.dump_traceback(sys.stdout)
raise AssertionError('Unable to join %d threads' % len(started))
@contextlib.contextmanager @contextlib.contextmanager
def swap_attr(obj, attr, new_val): def swap_attr(obj, attr, new_val):
"""Temporary swap out an attribute with a new object. """Temporary swap out an attribute with a new object.
@ -3023,63 +2876,6 @@ class catch_unraisable_exception:
del self.unraisable del self.unraisable
class catch_threading_exception:
"""
Context manager catching threading.Thread exception using
threading.excepthook.
Attributes set when an exception is catched:
* exc_type
* exc_value
* exc_traceback
* thread
See threading.excepthook() documentation for these attributes.
These attributes are deleted at the context manager exit.
Usage:
with support.catch_threading_exception() as cm:
# code spawning a thread which raises an exception
...
# check the thread exception, use cm attributes:
# exc_type, exc_value, exc_traceback, thread
...
# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
# exists at this point
# (to avoid reference cycles)
"""
def __init__(self):
self.exc_type = None
self.exc_value = None
self.exc_traceback = None
self.thread = None
self._old_hook = None
def _hook(self, args):
self.exc_type = args.exc_type
self.exc_value = args.exc_value
self.exc_traceback = args.exc_traceback
self.thread = args.thread
def __enter__(self):
self._old_hook = threading.excepthook
threading.excepthook = self._hook
return self
def __exit__(self, *exc_info):
threading.excepthook = self._old_hook
del self.exc_type
del self.exc_value
del self.exc_traceback
del self.thread
def wait_process(pid, *, exitcode, timeout=None): def wait_process(pid, *, exitcode, timeout=None):
""" """
Wait until process pid completes and check that the process exit code is Wait until process pid completes and check that the process exit code is

View File

@ -0,0 +1,208 @@
import contextlib
import functools
import _thread
import threading
import time
from test import support
#=======================================================================
# Threading support to prevent reporting refleaks when running regrtest.py -R
# NOTE: we use thread._count() rather than threading.enumerate() (or the
# moral equivalent thereof) because a threading.Thread object is still alive
# until its __bootstrap() method has returned, even after it has been
# unregistered from the threading module.
# thread._count(), on the other hand, only gets decremented *after* the
# __bootstrap() method has returned, which gives us reliable reference counts
# at the end of a test run.
def threading_setup():
return _thread._count(), threading._dangling.copy()
def threading_cleanup(*original_values):
_MAX_COUNT = 100
for count in range(_MAX_COUNT):
values = _thread._count(), threading._dangling
if values == original_values:
break
if not count:
# Display a warning at the first iteration
support.environment_altered = True
dangling_threads = values[1]
support.print_warning(f"threading_cleanup() failed to cleanup "
f"{values[0] - original_values[0]} threads "
f"(count: {values[0]}, "
f"dangling: {len(dangling_threads)})")
for thread in dangling_threads:
support.print_warning(f"Dangling thread: {thread!r}")
# Don't hold references to threads
dangling_threads = None
values = None
time.sleep(0.01)
gc_collect()
def reap_threads(func):
"""Use this function when threads are being used. This will
ensure that the threads are cleaned up even when the test fails.
"""
@functools.wraps(func)
def decorator(*args):
key = threading_setup()
try:
return func(*args)
finally:
threading_cleanup(*key)
return decorator
@contextlib.contextmanager
def wait_threads_exit(timeout=None):
"""
bpo-31234: Context manager to wait until all threads created in the with
statement exit.
Use _thread.count() to check if threads exited. Indirectly, wait until
threads exit the internal t_bootstrap() C function of the _thread module.
threading_setup() and threading_cleanup() are designed to emit a warning
if a test leaves running threads in the background. This context manager
is designed to cleanup threads started by the _thread.start_new_thread()
which doesn't allow to wait for thread exit, whereas thread.Thread has a
join() method.
"""
if timeout is None:
timeout = support.SHORT_TIMEOUT
old_count = _thread._count()
try:
yield
finally:
start_time = time.monotonic()
deadline = start_time + timeout
while True:
count = _thread._count()
if count <= old_count:
break
if time.monotonic() > deadline:
dt = time.monotonic() - start_time
msg = (f"wait_threads() failed to cleanup {count - old_count} "
f"threads after {dt:.1f} seconds "
f"(count: {count}, old count: {old_count})")
raise AssertionError(msg)
time.sleep(0.010)
gc_collect()
def join_thread(thread, timeout=None):
"""Join a thread. Raise an AssertionError if the thread is still alive
after timeout seconds.
"""
if timeout is None:
timeout = support.SHORT_TIMEOUT
thread.join(timeout)
if thread.is_alive():
msg = f"failed to join the thread in {timeout:.1f} seconds"
raise AssertionError(msg)
@contextlib.contextmanager
def start_threads(threads, unlock=None):
import faulthandler
threads = list(threads)
started = []
try:
try:
for t in threads:
t.start()
started.append(t)
except:
if verbose:
print("Can't start %d threads, only %d threads started" %
(len(threads), len(started)))
raise
yield
finally:
try:
if unlock:
unlock()
endtime = starttime = time.monotonic()
for timeout in range(1, 16):
endtime += 60
for t in started:
t.join(max(endtime - time.monotonic(), 0.01))
started = [t for t in started if t.is_alive()]
if not started:
break
if verbose:
print('Unable to join %d threads during a period of '
'%d minutes' % (len(started), timeout))
finally:
started = [t for t in started if t.is_alive()]
if started:
faulthandler.dump_traceback(sys.stdout)
raise AssertionError('Unable to join %d threads' % len(started))
class catch_threading_exception:
"""
Context manager catching threading.Thread exception using
threading.excepthook.
Attributes set when an exception is catched:
* exc_type
* exc_value
* exc_traceback
* thread
See threading.excepthook() documentation for these attributes.
These attributes are deleted at the context manager exit.
Usage:
with threading_helper.catch_threading_exception() as cm:
# code spawning a thread which raises an exception
...
# check the thread exception, use cm attributes:
# exc_type, exc_value, exc_traceback, thread
...
# exc_type, exc_value, exc_traceback, thread attributes of cm no longer
# exists at this point
# (to avoid reference cycles)
"""
def __init__(self):
self.exc_type = None
self.exc_value = None
self.exc_traceback = None
self.thread = None
self._old_hook = None
def _hook(self, args):
self.exc_type = args.exc_type
self.exc_value = args.exc_value
self.exc_traceback = args.exc_traceback
self.thread = args.thread
def __enter__(self):
self._old_hook = threading.excepthook
threading.excepthook = self._hook
return self
def __exit__(self, *exc_info):
threading.excepthook = self._old_hook
del self.exc_type
del self.exc_value
del self.exc_traceback
del self.thread

View File

@ -2,6 +2,7 @@
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
import asynchat import asynchat
import asyncore import asyncore
@ -103,10 +104,10 @@ class TestAsynchat(unittest.TestCase):
usepoll = False usepoll = False
def setUp(self): def setUp(self):
self._threads = support.threading_setup() self._threads = threading_helper.threading_setup()
def tearDown(self): def tearDown(self):
support.threading_cleanup(*self._threads) threading_helper.threading_cleanup(*self._threads)
def line_terminator_check(self, term, server_chunk): def line_terminator_check(self, term, server_chunk):
event = threading.Event() event = threading.Event()
@ -122,7 +123,7 @@ class TestAsynchat(unittest.TestCase):
c.push(b"I'm not dead yet!" + term) c.push(b"I'm not dead yet!" + term)
c.push(SERVER_QUIT) c.push(SERVER_QUIT)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
support.join_thread(s) threading_helper.join_thread(s)
self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
@ -153,7 +154,7 @@ class TestAsynchat(unittest.TestCase):
c.push(data) c.push(data)
c.push(SERVER_QUIT) c.push(SERVER_QUIT)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
support.join_thread(s) threading_helper.join_thread(s)
self.assertEqual(c.contents, [data[:termlen]]) self.assertEqual(c.contents, [data[:termlen]])
@ -173,7 +174,7 @@ class TestAsynchat(unittest.TestCase):
c.push(data) c.push(data)
c.push(SERVER_QUIT) c.push(SERVER_QUIT)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
support.join_thread(s) threading_helper.join_thread(s)
self.assertEqual(c.contents, []) self.assertEqual(c.contents, [])
self.assertEqual(c.buffer, data) self.assertEqual(c.buffer, data)
@ -185,7 +186,7 @@ class TestAsynchat(unittest.TestCase):
p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8) p = asynchat.simple_producer(data+SERVER_QUIT, buffer_size=8)
c.push_with_producer(p) c.push_with_producer(p)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
support.join_thread(s) threading_helper.join_thread(s)
self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
@ -195,7 +196,7 @@ class TestAsynchat(unittest.TestCase):
data = b"hello world\nI'm not dead yet!\n" data = b"hello world\nI'm not dead yet!\n"
c.push_with_producer(data+SERVER_QUIT) c.push_with_producer(data+SERVER_QUIT)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
support.join_thread(s) threading_helper.join_thread(s)
self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"]) self.assertEqual(c.contents, [b"hello world", b"I'm not dead yet!"])
@ -206,7 +207,7 @@ class TestAsynchat(unittest.TestCase):
c.push(b"hello world\n\nI'm not dead yet!\n") c.push(b"hello world\n\nI'm not dead yet!\n")
c.push(SERVER_QUIT) c.push(SERVER_QUIT)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
support.join_thread(s) threading_helper.join_thread(s)
self.assertEqual(c.contents, self.assertEqual(c.contents,
[b"hello world", b"", b"I'm not dead yet!"]) [b"hello world", b"", b"I'm not dead yet!"])
@ -225,7 +226,7 @@ class TestAsynchat(unittest.TestCase):
# where the server echoes all of its data before we can check that it # where the server echoes all of its data before we can check that it
# got any down below. # got any down below.
s.start_resend_event.set() s.start_resend_event.set()
support.join_thread(s) threading_helper.join_thread(s)
self.assertEqual(c.contents, []) self.assertEqual(c.contents, [])
# the server might have been able to send a byte or two back, but this # the server might have been able to send a byte or two back, but this
@ -246,7 +247,7 @@ class TestAsynchat(unittest.TestCase):
self.assertRaises(TypeError, c.push, 'unicode') self.assertRaises(TypeError, c.push, 'unicode')
c.push(SERVER_QUIT) c.push(SERVER_QUIT)
asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01) asyncore.loop(use_poll=self.usepoll, count=300, timeout=.01)
support.join_thread(s) threading_helper.join_thread(s)
self.assertEqual(c.contents, [b'bytes', b'bytes', b'bytes']) self.assertEqual(c.contents, [b'bytes', b'bytes', b'bytes'])

View File

@ -33,6 +33,7 @@ from asyncio import selector_events
from test.test_asyncio import utils as test_utils from test.test_asyncio import utils as test_utils
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
from test.support import ALWAYS_EQ, LARGEST, SMALLEST from test.support import ALWAYS_EQ, LARGEST, SMALLEST
@ -706,7 +707,7 @@ class EventLoopTestsMixin:
proto.transport.close() proto.transport.close()
lsock.close() lsock.close()
support.join_thread(thread) threading_helper.join_thread(thread)
self.assertFalse(thread.is_alive()) self.assertFalse(thread.is_alive())
self.assertEqual(proto.state, 'CLOSED') self.assertEqual(proto.state, 'CLOSED')
self.assertEqual(proto.nbytes, len(message)) self.assertEqual(proto.nbytes, len(message))

View File

@ -34,6 +34,7 @@ from asyncio import futures
from asyncio import tasks from asyncio import tasks
from asyncio.log import logger from asyncio.log import logger
from test import support from test import support
from test.support import threading_helper
def data_file(filename): def data_file(filename):
@ -546,7 +547,7 @@ class TestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self._get_running_loop = events._get_running_loop self._get_running_loop = events._get_running_loop
events._get_running_loop = lambda: None events._get_running_loop = lambda: None
self._thread_cleanup = support.threading_setup() self._thread_cleanup = threading_helper.threading_setup()
def tearDown(self): def tearDown(self):
self.unpatch_get_running_loop() self.unpatch_get_running_loop()
@ -558,7 +559,7 @@ class TestCase(unittest.TestCase):
self.assertEqual(sys.exc_info(), (None, None, None)) self.assertEqual(sys.exc_info(), (None, None, None))
self.doCleanups() self.doCleanups()
support.threading_cleanup(*self._thread_cleanup) threading_helper.threading_cleanup(*self._thread_cleanup)
support.reap_children() support.reap_children()

View File

@ -11,6 +11,7 @@ import threading
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
from io import BytesIO from io import BytesIO
if support.PGO: if support.PGO:
@ -323,7 +324,7 @@ class DispatcherWithSendTests(unittest.TestCase):
def tearDown(self): def tearDown(self):
asyncore.close_all() asyncore.close_all()
@support.reap_threads @threading_helper.reap_threads
def test_send(self): def test_send(self):
evt = threading.Event() evt = threading.Event()
sock = socket.socket() sock = socket.socket()
@ -360,7 +361,7 @@ class DispatcherWithSendTests(unittest.TestCase):
self.assertEqual(cap.getvalue(), data*2) self.assertEqual(cap.getvalue(), data*2)
finally: finally:
support.join_thread(t) threading_helper.join_thread(t)
@unittest.skipUnless(hasattr(asyncore, 'file_wrapper'), @unittest.skipUnless(hasattr(asyncore, 'file_wrapper'),
@ -766,7 +767,7 @@ class BaseTestAPI:
self.assertTrue(s.socket.getsockopt(socket.SOL_SOCKET, self.assertTrue(s.socket.getsockopt(socket.SOL_SOCKET,
socket.SO_REUSEADDR)) socket.SO_REUSEADDR))
@support.reap_threads @threading_helper.reap_threads
def test_quick_connect(self): def test_quick_connect(self):
# see: http://bugs.python.org/issue10340 # see: http://bugs.python.org/issue10340
if self.family not in (socket.AF_INET, getattr(socket, "AF_INET6", object())): if self.family not in (socket.AF_INET, getattr(socket, "AF_INET6", object())):
@ -788,7 +789,7 @@ class BaseTestAPI:
except OSError: except OSError:
pass pass
finally: finally:
support.join_thread(t) threading_helper.join_thread(t)
class TestAPI_UseIPv4Sockets(BaseTestAPI): class TestAPI_UseIPv4Sockets(BaseTestAPI):
family = socket.AF_INET family = socket.AF_INET

View File

@ -12,6 +12,7 @@ import random
import shutil import shutil
import subprocess import subprocess
import threading import threading
from test.support import threading_helper
from test.support import unlink from test.support import unlink
import _compression import _compression
import sys import sys
@ -502,7 +503,7 @@ class BZ2FileTest(BaseTest):
for i in range(5): for i in range(5):
f.write(data) f.write(data)
threads = [threading.Thread(target=comp) for i in range(nthreads)] threads = [threading.Thread(target=comp) for i in range(nthreads)]
with support.start_threads(threads): with threading_helper.start_threads(threads):
pass pass
def testMixedIterationAndReads(self): def testMixedIterationAndReads(self):

View File

@ -17,6 +17,7 @@ import importlib.machinery
import importlib.util import importlib.util
from test import support from test import support
from test.support import MISSING_C_DOCSTRINGS from test.support import MISSING_C_DOCSTRINGS
from test.support import threading_helper
from test.support.script_helper import assert_python_failure, assert_python_ok from test.support.script_helper import assert_python_failure, assert_python_ok
try: try:
import _posixsubprocess import _posixsubprocess
@ -575,7 +576,7 @@ class TestPendingCalls(unittest.TestCase):
threads = [threading.Thread(target=self.pendingcalls_thread, threads = [threading.Thread(target=self.pendingcalls_thread,
args=(context,)) args=(context,))
for i in range(context.nThreads)] for i in range(context.nThreads)]
with support.start_threads(threads): with threading_helper.start_threads(threads):
self.pendingcalls_wait(context.l, n, context) self.pendingcalls_wait(context.l, n, context)
def pendingcalls_thread(self, context): def pendingcalls_thread(self, context):
@ -634,7 +635,7 @@ class SubinterpreterTest(unittest.TestCase):
class TestThreadState(unittest.TestCase): class TestThreadState(unittest.TestCase):
@support.reap_threads @threading_helper.reap_threads
def test_thread_state(self): def test_thread_state(self):
# some extra thread-state tests driven via _testcapi # some extra thread-state tests driven via _testcapi
def target(): def target():

View File

@ -1,4 +1,5 @@
from test import support from test import support
from test.support import threading_helper
# Skip tests if _multiprocessing wasn't built. # Skip tests if _multiprocessing wasn't built.
support.import_module('_multiprocessing') support.import_module('_multiprocessing')
@ -100,11 +101,11 @@ def make_dummy_object(_):
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self._thread_key = support.threading_setup() self._thread_key = threading_helper.threading_setup()
def tearDown(self): def tearDown(self):
support.reap_children() support.reap_children()
support.threading_cleanup(*self._thread_key) threading_helper.threading_cleanup(*self._thread_key)
class ExecutorMixin: class ExecutorMixin:
@ -1496,11 +1497,11 @@ _threads_key = None
def setUpModule(): def setUpModule():
global _threads_key global _threads_key
_threads_key = support.threading_setup() _threads_key = threading_helper.threading_setup()
def tearDownModule(): def tearDownModule():
support.threading_cleanup(*_threads_key) threading_helper.threading_cleanup(*_threads_key)
multiprocessing.util._cleanup_tests() multiprocessing.util._cleanup_tests()

View File

@ -37,7 +37,8 @@ from email import iterators
from email import base64mime from email import base64mime
from email import quoprimime from email import quoprimime
from test.support import unlink, start_threads from test.support import threading_helper
from test.support import unlink
from test.test_email import openfile, TestEmailBase from test.test_email import openfile, TestEmailBase
# These imports are documented to work, but we are testing them using a # These imports are documented to work, but we are testing them using a
@ -3241,7 +3242,7 @@ Foo
append(make_msgid(domain='testdomain-string')) append(make_msgid(domain='testdomain-string'))
threads = [MsgidsThread() for i in range(5)] threads = [MsgidsThread() for i in range(5)]
with start_threads(threads): with threading_helper.start_threads(threads):
pass pass
all_ids = sum([t.msgids for t in threads], []) all_ids = sum([t.msgids for t in threads], [])
self.assertEqual(len(set(all_ids)), len(all_ids)) self.assertEqual(len(set(all_ids)), len(all_ids))

View File

@ -10,6 +10,7 @@ from io import StringIO
from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL from pickle import dumps, loads, PicklingError, HIGHEST_PROTOCOL
from test import support from test import support
from test.support import ALWAYS_EQ from test.support import ALWAYS_EQ
from test.support import threading_helper
from datetime import timedelta from datetime import timedelta
@ -2333,7 +2334,7 @@ class TestFlag(unittest.TestCase):
self.assertEqual(Color.ALL.value, 7) self.assertEqual(Color.ALL.value, 7)
self.assertEqual(str(Color.BLUE), 'blue') self.assertEqual(str(Color.BLUE), 'blue')
@support.reap_threads @threading_helper.reap_threads
def test_unique_composite(self): def test_unique_composite(self):
# override __eq__ to be identity only # override __eq__ to be identity only
class TestFlag(Flag): class TestFlag(Flag):
@ -2363,7 +2364,7 @@ class TestFlag(unittest.TestCase):
threading.Thread(target=cycle_enum) threading.Thread(target=cycle_enum)
for _ in range(8) for _ in range(8)
] ]
with support.start_threads(threads): with threading_helper.start_threads(threads):
pass pass
# check that only 248 members were created # check that only 248 members were created
self.assertFalse( self.assertFalse(
@ -2751,7 +2752,7 @@ class TestIntFlag(unittest.TestCase):
self.assertEqual(Color.ALL.value, 7) self.assertEqual(Color.ALL.value, 7)
self.assertEqual(str(Color.BLUE), 'blue') self.assertEqual(str(Color.BLUE), 'blue')
@support.reap_threads @threading_helper.reap_threads
def test_unique_composite(self): def test_unique_composite(self):
# override __eq__ to be identity only # override __eq__ to be identity only
class TestFlag(IntFlag): class TestFlag(IntFlag):
@ -2781,7 +2782,7 @@ class TestIntFlag(unittest.TestCase):
threading.Thread(target=cycle_enum) threading.Thread(target=cycle_enum)
for _ in range(8) for _ in range(8)
] ]
with support.start_threads(threads): with threading_helper.start_threads(threads):
pass pass
# check that only 248 members were created # check that only 248 members were created
self.assertFalse( self.assertFalse(

View File

@ -19,6 +19,7 @@ except ImportError:
from unittest import TestCase, skipUnless from unittest import TestCase, skipUnless
from test import support from test import support
from test.support import threading_helper
from test.support import socket_helper from test.support import socket_helper
from test.support.socket_helper import HOST, HOSTv6 from test.support.socket_helper import HOST, HOSTv6
@ -1117,11 +1118,11 @@ def test_main():
TestTLS_FTPClassMixin, TestTLS_FTPClass, TestTLS_FTPClassMixin, TestTLS_FTPClass,
MiscTestCase] MiscTestCase]
thread_info = support.threading_setup() thread_info = threading_helper.threading_setup()
try: try:
support.run_unittest(*tests) support.run_unittest(*tests)
finally: finally:
support.threading_cleanup(*thread_info) threading_helper.threading_cleanup(*thread_info)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -19,6 +19,7 @@ import gc
from weakref import proxy from weakref import proxy
import contextlib import contextlib
from test.support import threading_helper
from test.support.script_helper import assert_python_ok from test.support.script_helper import assert_python_ok
import functools import functools
@ -1798,7 +1799,7 @@ class TestLRU:
# create n threads in order to fill cache # create n threads in order to fill cache
threads = [threading.Thread(target=full, args=[k]) threads = [threading.Thread(target=full, args=[k])
for k in range(n)] for k in range(n)]
with support.start_threads(threads): with threading_helper.start_threads(threads):
start.set() start.set()
hits, misses, maxsize, currsize = f.cache_info() hits, misses, maxsize, currsize = f.cache_info()
@ -1816,7 +1817,7 @@ class TestLRU:
threads += [threading.Thread(target=full, args=[k]) threads += [threading.Thread(target=full, args=[k])
for k in range(n)] for k in range(n)]
start.clear() start.clear()
with support.start_threads(threads): with threading_helper.start_threads(threads):
start.set() start.set()
finally: finally:
sys.setswitchinterval(orig_si) sys.setswitchinterval(orig_si)
@ -1838,7 +1839,7 @@ class TestLRU:
self.assertEqual(f(i), 3 * i) self.assertEqual(f(i), 3 * i)
stop.wait(10) stop.wait(10)
threads = [threading.Thread(target=test) for k in range(n)] threads = [threading.Thread(target=test) for k in range(n)]
with support.start_threads(threads): with threading_helper.start_threads(threads):
for i in range(m): for i in range(m):
start.wait(10) start.wait(10)
stop.reset() stop.reset()
@ -1858,7 +1859,7 @@ class TestLRU:
self.assertEqual(f(x), 3 * x, i) self.assertEqual(f(x), 3 * x, i)
threads = [threading.Thread(target=test, args=(i, v)) threads = [threading.Thread(target=test, args=(i, v))
for i, v in enumerate([1, 2, 2, 3, 2])] for i, v in enumerate([1, 2, 2, 3, 2])]
with support.start_threads(threads): with threading_helper.start_threads(threads):
pass pass
def test_need_for_rlock(self): def test_need_for_rlock(self):
@ -2792,7 +2793,7 @@ class TestCachedProperty(unittest.TestCase):
threading.Thread(target=lambda: item.cost) threading.Thread(target=lambda: item.cost)
for k in range(num_threads) for k in range(num_threads)
] ]
with support.start_threads(threads): with threading_helper.start_threads(threads):
go.set() go.set()
finally: finally:
sys.setswitchinterval(orig_si) sys.setswitchinterval(orig_si)

View File

@ -1,10 +1,10 @@
import unittest import unittest
import unittest.mock import unittest.mock
from test.support import (verbose, refcount_test, run_unittest, from test.support import (verbose, refcount_test, run_unittest,
cpython_only, start_threads, cpython_only, temp_dir, TESTFN, unlink,
temp_dir, TESTFN, unlink,
import_module) import_module)
from test.support.script_helper import assert_python_ok, make_script from test.support.script_helper import assert_python_ok, make_script
from test.support import threading_helper
import gc import gc
import sys import sys
@ -415,7 +415,7 @@ class GCTests(unittest.TestCase):
for i in range(N_THREADS): for i in range(N_THREADS):
t = threading.Thread(target=run_thread) t = threading.Thread(target=run_thread)
threads.append(t) threads.append(t)
with start_threads(threads, lambda: exit.append(1)): with threading_helper.start_threads(threads, lambda: exit.append(1)):
time.sleep(1.0) time.sleep(1.0)
finally: finally:
sys.setswitchinterval(old_switchinterval) sys.setswitchinterval(old_switchinterval)

View File

@ -19,6 +19,7 @@ import unittest
import warnings import warnings
from test import support from test import support
from test.support import _4G, bigmemtest, import_fresh_module from test.support import _4G, bigmemtest, import_fresh_module
from test.support import threading_helper
from http.client import HTTPException from http.client import HTTPException
# Were we compiled --with-pydebug or with #define Py_DEBUG? # Were we compiled --with-pydebug or with #define Py_DEBUG?
@ -870,7 +871,7 @@ class HashLibTestCase(unittest.TestCase):
'1cfceca95989f51f658e3f3ffe7f1cd43726c9e088c13ee10b46f57cef135b94' '1cfceca95989f51f658e3f3ffe7f1cd43726c9e088c13ee10b46f57cef135b94'
) )
@support.reap_threads @threading_helper.reap_threads
def test_threaded_hashing(self): def test_threaded_hashing(self):
# Updating the same hash object from several threads at once # Updating the same hash object from several threads at once
# using data chunk sizes containing the same byte sequences. # using data chunk sizes containing the same byte sequences.

View File

@ -30,6 +30,7 @@ from io import BytesIO
import unittest import unittest
from test import support from test import support
from test.support import threading_helper
class NoLogRequestHandler: class NoLogRequestHandler:
@ -64,7 +65,7 @@ class TestServerThread(threading.Thread):
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self._threads = support.threading_setup() self._threads = threading_helper.threading_setup()
os.environ = support.EnvironmentVarGuard() os.environ = support.EnvironmentVarGuard()
self.server_started = threading.Event() self.server_started = threading.Event()
self.thread = TestServerThread(self, self.request_handler) self.thread = TestServerThread(self, self.request_handler)
@ -75,7 +76,7 @@ class BaseTestCase(unittest.TestCase):
self.thread.stop() self.thread.stop()
self.thread = None self.thread = None
os.environ.__exit__() os.environ.__exit__()
support.threading_cleanup(*self._threads) threading_helper.threading_cleanup(*self._threads)
def request(self, uri, method='GET', body=None, headers={}): def request(self, uri, method='GET', body=None, headers={}):
self.connection = http.client.HTTPConnection(self.HOST, self.PORT) self.connection = http.client.HTTPConnection(self.HOST, self.PORT)

View File

@ -10,9 +10,10 @@ import calendar
import threading import threading
import socket import socket
from test.support import (reap_threads, verbose, from test.support import (verbose,
run_with_tz, run_with_locale, cpython_only) run_with_tz, run_with_locale, cpython_only)
from test.support import hashlib_helper from test.support import hashlib_helper
from test.support import threading_helper
import unittest import unittest
from unittest import mock from unittest import mock
from datetime import datetime, timezone, timedelta from datetime import datetime, timezone, timedelta
@ -252,7 +253,7 @@ class NewIMAPTestsMixin():
# cleanup the server # cleanup the server
self.server.shutdown() self.server.shutdown()
self.server.server_close() self.server.server_close()
support.join_thread(self.thread) threading_helper.join_thread(self.thread)
# Explicitly clear the attribute to prevent dangling thread # Explicitly clear the attribute to prevent dangling thread
self.thread = None self.thread = None
@ -641,13 +642,13 @@ class ThreadedNetworkedTests(unittest.TestCase):
finally: finally:
client.logout() client.logout()
@reap_threads @threading_helper.reap_threads
def test_connect(self): def test_connect(self):
with self.reaped_server(SimpleIMAPHandler) as server: with self.reaped_server(SimpleIMAPHandler) as server:
client = self.imap_class(*server.server_address) client = self.imap_class(*server.server_address)
client.shutdown() client.shutdown()
@reap_threads @threading_helper.reap_threads
def test_bracket_flags(self): def test_bracket_flags(self):
# This violates RFC 3501, which disallows ']' characters in tag names, # This violates RFC 3501, which disallows ']' characters in tag names,
@ -696,7 +697,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
typ, [data] = client.response('PERMANENTFLAGS') typ, [data] = client.response('PERMANENTFLAGS')
self.assertIn(b'[test]', data) self.assertIn(b'[test]', data)
@reap_threads @threading_helper.reap_threads
def test_issue5949(self): def test_issue5949(self):
class EOFHandler(socketserver.StreamRequestHandler): class EOFHandler(socketserver.StreamRequestHandler):
@ -708,7 +709,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
self.assertRaises(imaplib.IMAP4.abort, self.assertRaises(imaplib.IMAP4.abort,
self.imap_class, *server.server_address) self.imap_class, *server.server_address)
@reap_threads @threading_helper.reap_threads
def test_line_termination(self): def test_line_termination(self):
class BadNewlineHandler(SimpleIMAPHandler): class BadNewlineHandler(SimpleIMAPHandler):
@ -732,7 +733,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
self.server.response = yield self.server.response = yield
self._send_tagged(tag, 'OK', 'FAKEAUTH successful') self._send_tagged(tag, 'OK', 'FAKEAUTH successful')
@reap_threads @threading_helper.reap_threads
def test_enable_raises_error_if_not_AUTH(self): def test_enable_raises_error_if_not_AUTH(self):
with self.reaped_pair(self.UTF8Server) as (server, client): with self.reaped_pair(self.UTF8Server) as (server, client):
self.assertFalse(client.utf8_enabled) self.assertFalse(client.utf8_enabled)
@ -741,14 +742,14 @@ class ThreadedNetworkedTests(unittest.TestCase):
# XXX Also need a test that enable after SELECT raises an error. # XXX Also need a test that enable after SELECT raises an error.
@reap_threads @threading_helper.reap_threads
def test_enable_raises_error_if_no_capability(self): def test_enable_raises_error_if_no_capability(self):
class NoEnableServer(self.UTF8Server): class NoEnableServer(self.UTF8Server):
capabilities = 'AUTH' capabilities = 'AUTH'
with self.reaped_pair(NoEnableServer) as (server, client): with self.reaped_pair(NoEnableServer) as (server, client):
self.assertRaises(imaplib.IMAP4.error, client.enable, 'foo') self.assertRaises(imaplib.IMAP4.error, client.enable, 'foo')
@reap_threads @threading_helper.reap_threads
def test_enable_UTF8_raises_error_if_not_supported(self): def test_enable_UTF8_raises_error_if_not_supported(self):
class NonUTF8Server(SimpleIMAPHandler): class NonUTF8Server(SimpleIMAPHandler):
pass pass
@ -759,7 +760,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
client.enable('UTF8=ACCEPT') client.enable('UTF8=ACCEPT')
pass pass
@reap_threads @threading_helper.reap_threads
def test_enable_UTF8_True_append(self): def test_enable_UTF8_True_append(self):
class UTF8AppendServer(self.UTF8Server): class UTF8AppendServer(self.UTF8Server):
@ -789,7 +790,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
# XXX also need a test that makes sure that the Literal and Untagged_status # XXX also need a test that makes sure that the Literal and Untagged_status
# regexes uses unicode in UTF8 mode instead of the default ASCII. # regexes uses unicode in UTF8 mode instead of the default ASCII.
@reap_threads @threading_helper.reap_threads
def test_search_disallows_charset_in_utf8_mode(self): def test_search_disallows_charset_in_utf8_mode(self):
with self.reaped_pair(self.UTF8Server) as (server, client): with self.reaped_pair(self.UTF8Server) as (server, client):
typ, _ = client.authenticate('MYAUTH', lambda x: b'fake') typ, _ = client.authenticate('MYAUTH', lambda x: b'fake')
@ -799,7 +800,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
self.assertTrue(client.utf8_enabled) self.assertTrue(client.utf8_enabled)
self.assertRaises(imaplib.IMAP4.error, client.search, 'foo', 'bar') self.assertRaises(imaplib.IMAP4.error, client.search, 'foo', 'bar')
@reap_threads @threading_helper.reap_threads
def test_bad_auth_name(self): def test_bad_auth_name(self):
class MyServer(SimpleIMAPHandler): class MyServer(SimpleIMAPHandler):
@ -812,7 +813,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
with self.assertRaises(imaplib.IMAP4.error): with self.assertRaises(imaplib.IMAP4.error):
client.authenticate('METHOD', lambda: 1) client.authenticate('METHOD', lambda: 1)
@reap_threads @threading_helper.reap_threads
def test_invalid_authentication(self): def test_invalid_authentication(self):
class MyServer(SimpleIMAPHandler): class MyServer(SimpleIMAPHandler):
@ -826,7 +827,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
with self.assertRaises(imaplib.IMAP4.error): with self.assertRaises(imaplib.IMAP4.error):
code, data = client.authenticate('MYAUTH', lambda x: b'fake') code, data = client.authenticate('MYAUTH', lambda x: b'fake')
@reap_threads @threading_helper.reap_threads
def test_valid_authentication(self): def test_valid_authentication(self):
class MyServer(SimpleIMAPHandler): class MyServer(SimpleIMAPHandler):
@ -848,7 +849,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
self.assertEqual(server.response, self.assertEqual(server.response,
b'ZmFrZQ==\r\n') # b64 encoded 'fake' b'ZmFrZQ==\r\n') # b64 encoded 'fake'
@reap_threads @threading_helper.reap_threads
@hashlib_helper.requires_hashdigest('md5') @hashlib_helper.requires_hashdigest('md5')
def test_login_cram_md5(self): def test_login_cram_md5(self):
@ -877,7 +878,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
self.assertEqual(ret, "OK") self.assertEqual(ret, "OK")
@reap_threads @threading_helper.reap_threads
def test_aborted_authentication(self): def test_aborted_authentication(self):
class MyServer(SimpleIMAPHandler): class MyServer(SimpleIMAPHandler):
@ -906,14 +907,14 @@ class ThreadedNetworkedTests(unittest.TestCase):
self.assertRaises(imaplib.IMAP4.error, self.assertRaises(imaplib.IMAP4.error,
self.imap_class, *server.server_address) self.imap_class, *server.server_address)
@reap_threads @threading_helper.reap_threads
def test_simple_with_statement(self): def test_simple_with_statement(self):
# simplest call # simplest call
with self.reaped_server(SimpleIMAPHandler) as server: with self.reaped_server(SimpleIMAPHandler) as server:
with self.imap_class(*server.server_address): with self.imap_class(*server.server_address):
pass pass
@reap_threads @threading_helper.reap_threads
def test_with_statement(self): def test_with_statement(self):
with self.reaped_server(SimpleIMAPHandler) as server: with self.reaped_server(SimpleIMAPHandler) as server:
with self.imap_class(*server.server_address) as imap: with self.imap_class(*server.server_address) as imap:
@ -921,7 +922,7 @@ class ThreadedNetworkedTests(unittest.TestCase):
self.assertEqual(server.logged, 'user') self.assertEqual(server.logged, 'user')
self.assertIsNone(server.logged) self.assertIsNone(server.logged)
@reap_threads @threading_helper.reap_threads
def test_with_statement_logout(self): def test_with_statement_logout(self):
# what happens if already logout in the block? # what happens if already logout in the block?
with self.reaped_server(SimpleIMAPHandler) as server: with self.reaped_server(SimpleIMAPHandler) as server:
@ -938,7 +939,7 @@ class ThreadedNetworkedTestsSSL(ThreadedNetworkedTests):
server_class = SecureTCPServer server_class = SecureTCPServer
imap_class = IMAP4_SSL imap_class = IMAP4_SSL
@reap_threads @threading_helper.reap_threads
def test_ssl_verified(self): def test_ssl_verified(self):
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
ssl_context.load_verify_locations(CAFILE) ssl_context.load_verify_locations(CAFILE)

View File

@ -25,6 +25,7 @@ from test.support import (
unlink, unload, cpython_only, TESTFN_UNENCODABLE, unlink, unload, cpython_only, TESTFN_UNENCODABLE,
temp_dir, DirsOnSysPath) temp_dir, DirsOnSysPath)
from test.support import script_helper from test.support import script_helper
from test.support import threading_helper
from test.test_importlib.util import uncache from test.test_importlib.util import uncache
from types import ModuleType from types import ModuleType
@ -459,7 +460,7 @@ class ImportTests(unittest.TestCase):
event = threading.Event() event = threading.Event()
threads = [threading.Thread(target=run) for x in range(2)] threads = [threading.Thread(target=run) for x in range(2)]
try: try:
with test.support.start_threads(threads, event.set): with threading_helper.start_threads(threads, event.set):
time.sleep(0) time.sleep(0)
finally: finally:
sys.modules.pop('package', None) sys.modules.pop('package', None)

View File

@ -7,6 +7,7 @@ import threading
import weakref import weakref
from test import support from test import support
from test.support import threading_helper
from test import lock_tests from test import lock_tests
@ -138,7 +139,7 @@ class LifetimeTests:
) = test_util.test_both(LifetimeTests, init=init) ) = test_util.test_both(LifetimeTests, init=init)
@support.reap_threads @threading_helper.reap_threads
def test_main(): def test_main():
support.run_unittest(Frozen_ModuleLockAsRLockTests, support.run_unittest(Frozen_ModuleLockAsRLockTests,
Source_ModuleLockAsRLockTests, Source_ModuleLockAsRLockTests,

View File

@ -15,8 +15,9 @@ import threading
import unittest import unittest
from unittest import mock from unittest import mock
from test.support import ( from test.support import (
verbose, run_unittest, TESTFN, reap_threads, verbose, run_unittest, TESTFN,
forget, unlink, rmtree, start_threads) forget, unlink, rmtree)
from test.support import threading_helper
def task(N, done, done_tasks, errors): def task(N, done, done_tasks, errors):
try: try:
@ -124,9 +125,9 @@ class ThreadedImportTests(unittest.TestCase):
done_tasks = [] done_tasks = []
done.clear() done.clear()
t0 = time.monotonic() t0 = time.monotonic()
with start_threads(threading.Thread(target=task, with threading_helper.start_threads(
args=(N, done, done_tasks, errors,)) threading.Thread(target=task, args=(N, done, done_tasks, errors,))
for i in range(N)): for i in range(N)):
pass pass
completed = done.wait(10 * 60) completed = done.wait(10 * 60)
dt = time.monotonic() - t0 dt = time.monotonic() - t0
@ -245,7 +246,7 @@ class ThreadedImportTests(unittest.TestCase):
del sys.modules[TESTFN] del sys.modules[TESTFN]
@reap_threads @threading_helper.reap_threads
def test_main(): def test_main():
old_switchinterval = None old_switchinterval = None
try: try:

View File

@ -40,6 +40,7 @@ from itertools import cycle, count
from test import support from test import support
from test.support.script_helper import ( from test.support.script_helper import (
assert_python_ok, assert_python_failure, run_python_until_end) assert_python_ok, assert_python_failure, run_python_until_end)
from test.support import threading_helper
from test.support import FakePath from test.support import FakePath
import codecs import codecs
@ -1472,7 +1473,7 @@ class BufferedReaderTest(unittest.TestCase, CommonBufferedTests):
errors.append(e) errors.append(e)
raise raise
threads = [threading.Thread(target=f) for x in range(20)] threads = [threading.Thread(target=f) for x in range(20)]
with support.start_threads(threads): with threading_helper.start_threads(threads):
time.sleep(0.02) # yield time.sleep(0.02) # yield
self.assertFalse(errors, self.assertFalse(errors,
"the following exceptions were caught: %r" % errors) "the following exceptions were caught: %r" % errors)
@ -1836,7 +1837,7 @@ class BufferedWriterTest(unittest.TestCase, CommonBufferedTests):
errors.append(e) errors.append(e)
raise raise
threads = [threading.Thread(target=f) for x in range(20)] threads = [threading.Thread(target=f) for x in range(20)]
with support.start_threads(threads): with threading_helper.start_threads(threads):
time.sleep(0.02) # yield time.sleep(0.02) # yield
self.assertFalse(errors, self.assertFalse(errors,
"the following exceptions were caught: %r" % errors) "the following exceptions were caught: %r" % errors)
@ -3270,7 +3271,7 @@ class TextIOWrapperTest(unittest.TestCase):
f.write(text) f.write(text)
threads = [threading.Thread(target=run, args=(x,)) threads = [threading.Thread(target=run, args=(x,))
for x in range(20)] for x in range(20)]
with support.start_threads(threads, event.set): with threading_helper.start_threads(threads, event.set):
time.sleep(0.02) time.sleep(0.02)
with self.open(support.TESTFN) as f: with self.open(support.TESTFN) as f:
content = f.read() content = f.read()

View File

@ -43,6 +43,7 @@ import tempfile
from test.support.script_helper import assert_python_ok, assert_python_failure from test.support.script_helper import assert_python_ok, assert_python_failure
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
from test.support.logging_helper import TestHandler from test.support.logging_helper import TestHandler
import textwrap import textwrap
import threading import threading
@ -79,7 +80,7 @@ class BaseTest(unittest.TestCase):
def setUp(self): def setUp(self):
"""Setup the default logging stream to an internal StringIO instance, """Setup the default logging stream to an internal StringIO instance,
so that we can examine log output as we want.""" so that we can examine log output as we want."""
self._threading_key = support.threading_setup() self._threading_key = threading_helper.threading_setup()
logger_dict = logging.getLogger().manager.loggerDict logger_dict = logging.getLogger().manager.loggerDict
logging._acquireLock() logging._acquireLock()
@ -150,7 +151,7 @@ class BaseTest(unittest.TestCase):
logging._releaseLock() logging._releaseLock()
self.doCleanups() self.doCleanups()
support.threading_cleanup(*self._threading_key) threading_helper.threading_cleanup(*self._threading_key)
def assert_log_lines(self, expected_values, stream=None, pat=None): def assert_log_lines(self, expected_values, stream=None, pat=None):
"""Match the collected log lines against the regular expression """Match the collected log lines against the regular expression
@ -865,7 +866,7 @@ class TestSMTPServer(smtpd.SMTPServer):
Wait for the server thread to terminate. Wait for the server thread to terminate.
""" """
self.close() self.close()
support.join_thread(self._thread) threading_helper.join_thread(self._thread)
self._thread = None self._thread = None
asyncore.close_all(map=self._map, ignore_all=True) asyncore.close_all(map=self._map, ignore_all=True)
@ -915,7 +916,7 @@ class ControlMixin(object):
""" """
self.shutdown() self.shutdown()
if self._thread is not None: if self._thread is not None:
support.join_thread(self._thread) threading_helper.join_thread(self._thread)
self._thread = None self._thread = None
self.server_close() self.server_close()
self.ready.clear() self.ready.clear()
@ -3212,7 +3213,7 @@ class ConfigDictTest(BaseTest):
finally: finally:
t.ready.wait(2.0) t.ready.wait(2.0)
logging.config.stopListening() logging.config.stopListening()
support.join_thread(t) threading_helper.join_thread(t)
def test_listen_config_10_ok(self): def test_listen_config_10_ok(self):
with support.captured_stdout() as output: with support.captured_stdout() as output:

View File

@ -31,6 +31,7 @@ import uuid
import warnings import warnings
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
from platform import win32_is_iot from platform import win32_is_iot
try: try:
@ -3163,12 +3164,12 @@ class TestSendfile(unittest.TestCase):
@classmethod @classmethod
def setUpClass(cls): def setUpClass(cls):
cls.key = support.threading_setup() cls.key = threading_helper.threading_setup()
create_file(support.TESTFN, cls.DATA) create_file(support.TESTFN, cls.DATA)
@classmethod @classmethod
def tearDownClass(cls): def tearDownClass(cls):
support.threading_cleanup(*cls.key) threading_helper.threading_cleanup(*cls.key)
support.unlink(support.TESTFN) support.unlink(support.TESTFN)
def setUp(self): def setUp(self):

View File

@ -7,7 +7,8 @@ import select
import threading import threading
import time import time
import unittest import unittest
from test.support import TESTFN, run_unittest, reap_threads, cpython_only from test.support import TESTFN, run_unittest, cpython_only
from test.support import threading_helper
try: try:
select.poll select.poll
@ -175,7 +176,7 @@ class PollTests(unittest.TestCase):
self.assertRaises(OverflowError, pollster.poll, INT_MAX + 1) self.assertRaises(OverflowError, pollster.poll, INT_MAX + 1)
self.assertRaises(OverflowError, pollster.poll, UINT_MAX + 1) self.assertRaises(OverflowError, pollster.poll, UINT_MAX + 1)
@reap_threads @threading_helper.reap_threads
def test_threaded_poll(self): def test_threaded_poll(self):
r, w = os.pipe() r, w = os.pipe()
self.addCleanup(os.close, r) self.addCleanup(os.close, r)
@ -204,7 +205,7 @@ class PollTests(unittest.TestCase):
t.join() t.join()
@unittest.skipUnless(threading, 'Threading required for this test.') @unittest.skipUnless(threading, 'Threading required for this test.')
@reap_threads @threading_helper.reap_threads
def test_poll_blocks_with_negative_ms(self): def test_poll_blocks_with_negative_ms(self):
for timeout_ms in [None, -1000, -1, -1.0, -0.1, -1e-100]: for timeout_ms in [None, -1000, -1, -1.0, -0.1, -1e-100]:
# Create two file descriptors. This will be used to unlock # Create two file descriptors. This will be used to unlock

View File

@ -15,6 +15,7 @@ from unittest import TestCase, skipUnless
from test import support as test_support from test import support as test_support
from test.support import hashlib_helper from test.support import hashlib_helper
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
HOST = socket_helper.HOST HOST = socket_helper.HOST
PORT = 0 PORT = 0
@ -536,11 +537,11 @@ class TestTimeouts(TestCase):
def test_main(): def test_main():
tests = [TestPOP3Class, TestTimeouts, tests = [TestPOP3Class, TestTimeouts,
TestPOP3_SSLClass, TestPOP3_TLSClass] TestPOP3_SSLClass, TestPOP3_TLSClass]
thread_info = test_support.threading_setup() thread_info = threading_helper.threading_setup()
try: try:
test_support.run_unittest(*tests) test_support.run_unittest(*tests)
finally: finally:
test_support.threading_cleanup(*thread_info) threading_helper.threading_cleanup(*thread_info)
if __name__ == '__main__': if __name__ == '__main__':

View File

@ -24,9 +24,10 @@ import textwrap
from io import StringIO from io import StringIO
from collections import namedtuple from collections import namedtuple
from test.support.script_helper import assert_python_ok from test.support.script_helper import assert_python_ok
from test.support import threading_helper
from test.support import ( from test.support import (
TESTFN, rmtree, TESTFN, rmtree,
reap_children, reap_threads, captured_output, captured_stdout, reap_children, captured_output, captured_stdout,
captured_stderr, unlink, requires_docstrings captured_stderr, unlink, requires_docstrings
) )
from test import pydoc_mod from test import pydoc_mod
@ -1575,7 +1576,7 @@ class TestInternalUtilities(unittest.TestCase):
self.assertIsNone(self._get_revised_path(trailing_argv0dir)) self.assertIsNone(self._get_revised_path(trailing_argv0dir))
@reap_threads @threading_helper.reap_threads
def test_main(): def test_main():
try: try:
test.support.run_unittest(PydocDocTest, test.support.run_unittest(PydocDocTest,

View File

@ -7,6 +7,7 @@ import time
import unittest import unittest
import weakref import weakref
from test import support from test import support
from test.support import threading_helper
py_queue = support.import_fresh_module('queue', blocked=['_queue']) py_queue = support.import_fresh_module('queue', blocked=['_queue'])
c_queue = support.import_fresh_module('queue', fresh=['_queue']) c_queue = support.import_fresh_module('queue', fresh=['_queue'])
@ -63,7 +64,7 @@ class BlockingTestMixin:
block_func) block_func)
return self.result return self.result
finally: finally:
support.join_thread(thread) # make sure the thread terminates threading_helper.join_thread(thread) # make sure the thread terminates
# Call this instead if block_func is supposed to raise an exception. # Call this instead if block_func is supposed to raise an exception.
def do_exceptional_blocking_test(self,block_func, block_args, trigger_func, def do_exceptional_blocking_test(self,block_func, block_args, trigger_func,
@ -79,7 +80,7 @@ class BlockingTestMixin:
self.fail("expected exception of kind %r" % self.fail("expected exception of kind %r" %
expected_exception_class) expected_exception_class)
finally: finally:
support.join_thread(thread) # make sure the thread terminates threading_helper.join_thread(thread) # make sure the thread terminates
if not thread.startedEvent.is_set(): if not thread.startedEvent.is_set():
self.fail("trigger thread ended but event never set") self.fail("trigger thread ended but event never set")
@ -484,7 +485,7 @@ class BaseSimpleQueueTest:
args=(q, results, sentinel)) args=(q, results, sentinel))
for i in range(n_consumers)] for i in range(n_consumers)]
with support.start_threads(feeders + consumers): with threading_helper.start_threads(feeders + consumers):
pass pass
self.assertFalse(exceptions) self.assertFalse(exceptions)

View File

@ -5,6 +5,7 @@ import unittest
import urllib.robotparser import urllib.robotparser
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
from http.server import BaseHTTPRequestHandler, HTTPServer from http.server import BaseHTTPRequestHandler, HTTPServer
@ -330,7 +331,7 @@ class PasswordProtectedSiteTestCase(unittest.TestCase):
self.t.join() self.t.join()
self.server.server_close() self.server.server_close()
@support.reap_threads @threading_helper.reap_threads
def testPasswordProtectedSite(self): def testPasswordProtectedSite(self):
addr = self.server.server_address addr = self.server.server_address
url = 'http://' + socket_helper.HOST + ':' + str(addr[1]) url = 'http://' + socket_helper.HOST + ':' + str(addr[1])

View File

@ -4,6 +4,7 @@ import threading
import time import time
import unittest import unittest
from test import support from test import support
from test.support import threading_helper
TIMEOUT = support.SHORT_TIMEOUT TIMEOUT = support.SHORT_TIMEOUT
@ -82,7 +83,7 @@ class TestCase(unittest.TestCase):
self.assertEqual(q.get(timeout=TIMEOUT), 5) self.assertEqual(q.get(timeout=TIMEOUT), 5)
self.assertTrue(q.empty()) self.assertTrue(q.empty())
timer.advance(1000) timer.advance(1000)
support.join_thread(t) threading_helper.join_thread(t)
self.assertTrue(q.empty()) self.assertTrue(q.empty())
self.assertEqual(timer.time(), 5) self.assertEqual(timer.time(), 5)
@ -137,7 +138,7 @@ class TestCase(unittest.TestCase):
self.assertEqual(q.get(timeout=TIMEOUT), 4) self.assertEqual(q.get(timeout=TIMEOUT), 4)
self.assertTrue(q.empty()) self.assertTrue(q.empty())
timer.advance(1000) timer.advance(1000)
support.join_thread(t) threading_helper.join_thread(t)
self.assertTrue(q.empty()) self.assertTrue(q.empty())
self.assertEqual(timer.time(), 4) self.assertEqual(timer.time(), 4)

View File

@ -22,7 +22,7 @@ import unittest
from test import support, mock_socket from test import support, mock_socket
from test.support import hashlib_helper from test.support import hashlib_helper
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_setup, threading_cleanup, join_thread from test.support import threading_helper
from unittest.mock import Mock from unittest.mock import Mock
HOST = socket_helper.HOST HOST = socket_helper.HOST
@ -217,7 +217,7 @@ class DebuggingServerTests(unittest.TestCase):
maxDiff = None maxDiff = None
def setUp(self): def setUp(self):
self.thread_key = threading_setup() self.thread_key = threading_helper.threading_setup()
self.real_getfqdn = socket.getfqdn self.real_getfqdn = socket.getfqdn
socket.getfqdn = mock_socket.getfqdn socket.getfqdn = mock_socket.getfqdn
# temporarily replace sys.stdout to capture DebuggingServer output # temporarily replace sys.stdout to capture DebuggingServer output
@ -249,7 +249,7 @@ class DebuggingServerTests(unittest.TestCase):
self.client_evt.set() self.client_evt.set()
# wait for the server thread to terminate # wait for the server thread to terminate
self.serv_evt.wait() self.serv_evt.wait()
join_thread(self.thread) threading_helper.join_thread(self.thread)
# restore sys.stdout # restore sys.stdout
sys.stdout = self.old_stdout sys.stdout = self.old_stdout
# restore DEBUGSTREAM # restore DEBUGSTREAM
@ -257,7 +257,7 @@ class DebuggingServerTests(unittest.TestCase):
smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM smtpd.DEBUGSTREAM = self.old_DEBUGSTREAM
del self.thread del self.thread
self.doCleanups() self.doCleanups()
threading_cleanup(*self.thread_key) threading_helper.threading_cleanup(*self.thread_key)
def get_output_without_xpeer(self): def get_output_without_xpeer(self):
test_output = self.output.getvalue() test_output = self.output.getvalue()
@ -704,7 +704,7 @@ class TooLongLineTests(unittest.TestCase):
respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n' respdata = b'250 OK' + (b'.' * smtplib._MAXLINE * 2) + b'\n'
def setUp(self): def setUp(self):
self.thread_key = threading_setup() self.thread_key = threading_helper.threading_setup()
self.old_stdout = sys.stdout self.old_stdout = sys.stdout
self.output = io.StringIO() self.output = io.StringIO()
sys.stdout = self.output sys.stdout = self.output
@ -722,10 +722,10 @@ class TooLongLineTests(unittest.TestCase):
def tearDown(self): def tearDown(self):
self.evt.wait() self.evt.wait()
sys.stdout = self.old_stdout sys.stdout = self.old_stdout
join_thread(self.thread) threading_helper.join_thread(self.thread)
del self.thread del self.thread
self.doCleanups() self.doCleanups()
threading_cleanup(*self.thread_key) threading_helper.threading_cleanup(*self.thread_key)
def testLineTooLong(self): def testLineTooLong(self):
self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP, self.assertRaises(smtplib.SMTPResponseException, smtplib.SMTP,
@ -955,7 +955,7 @@ class SimSMTPServer(smtpd.SMTPServer):
class SMTPSimTests(unittest.TestCase): class SMTPSimTests(unittest.TestCase):
def setUp(self): def setUp(self):
self.thread_key = threading_setup() self.thread_key = threading_helper.threading_setup()
self.real_getfqdn = socket.getfqdn self.real_getfqdn = socket.getfqdn
socket.getfqdn = mock_socket.getfqdn socket.getfqdn = mock_socket.getfqdn
self.serv_evt = threading.Event() self.serv_evt = threading.Event()
@ -978,10 +978,10 @@ class SMTPSimTests(unittest.TestCase):
self.client_evt.set() self.client_evt.set()
# wait for the server thread to terminate # wait for the server thread to terminate
self.serv_evt.wait() self.serv_evt.wait()
join_thread(self.thread) threading_helper.join_thread(self.thread)
del self.thread del self.thread
self.doCleanups() self.doCleanups()
threading_cleanup(*self.thread_key) threading_helper.threading_cleanup(*self.thread_key)
def testBasic(self): def testBasic(self):
# smoke test # smoke test
@ -1268,7 +1268,7 @@ class SMTPUTF8SimTests(unittest.TestCase):
maxDiff = None maxDiff = None
def setUp(self): def setUp(self):
self.thread_key = threading_setup() self.thread_key = threading_helper.threading_setup()
self.real_getfqdn = socket.getfqdn self.real_getfqdn = socket.getfqdn
socket.getfqdn = mock_socket.getfqdn socket.getfqdn = mock_socket.getfqdn
self.serv_evt = threading.Event() self.serv_evt = threading.Event()
@ -1293,10 +1293,10 @@ class SMTPUTF8SimTests(unittest.TestCase):
self.client_evt.set() self.client_evt.set()
# wait for the server thread to terminate # wait for the server thread to terminate
self.serv_evt.wait() self.serv_evt.wait()
join_thread(self.thread) threading_helper.join_thread(self.thread)
del self.thread del self.thread
self.doCleanups() self.doCleanups()
threading_cleanup(*self.thread_key) threading_helper.threading_cleanup(*self.thread_key)
def test_test_server_supports_extensions(self): def test_test_server_supports_extensions(self):
smtp = smtplib.SMTP( smtp = smtplib.SMTP(
@ -1397,7 +1397,7 @@ class SimSMTPAUTHInitialResponseServer(SimSMTPServer):
class SMTPAUTHInitialResponseSimTests(unittest.TestCase): class SMTPAUTHInitialResponseSimTests(unittest.TestCase):
def setUp(self): def setUp(self):
self.thread_key = threading_setup() self.thread_key = threading_helper.threading_setup()
self.real_getfqdn = socket.getfqdn self.real_getfqdn = socket.getfqdn
socket.getfqdn = mock_socket.getfqdn socket.getfqdn = mock_socket.getfqdn
self.serv_evt = threading.Event() self.serv_evt = threading.Event()
@ -1421,10 +1421,10 @@ class SMTPAUTHInitialResponseSimTests(unittest.TestCase):
self.client_evt.set() self.client_evt.set()
# wait for the server thread to terminate # wait for the server thread to terminate
self.serv_evt.wait() self.serv_evt.wait()
join_thread(self.thread) threading_helper.join_thread(self.thread)
del self.thread del self.thread
self.doCleanups() self.doCleanups()
threading_cleanup(*self.thread_key) threading_helper.threading_cleanup(*self.thread_key)
def testAUTH_PLAIN_initial_response_login(self): def testAUTH_PLAIN_initial_response_login(self):
self.serv.add_feature('AUTH PLAIN') self.serv.add_feature('AUTH PLAIN')

View File

@ -1,6 +1,7 @@
import unittest import unittest
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
import errno import errno
import io import io
@ -336,7 +337,7 @@ class ThreadableTest:
self.server_ready.set() self.server_ready.set()
def _setUp(self): def _setUp(self):
self.wait_threads = support.wait_threads_exit() self.wait_threads = threading_helper.wait_threads_exit()
self.wait_threads.__enter__() self.wait_threads.__enter__()
self.server_ready = threading.Event() self.server_ready = threading.Event()
@ -6665,9 +6666,9 @@ def test_main():
]) ])
tests.append(TestMSWindowsTCPFlags) tests.append(TestMSWindowsTCPFlags)
thread_info = support.threading_setup() thread_info = threading_helper.threading_setup()
support.run_unittest(*tests) support.run_unittest(*tests)
support.threading_cleanup(*thread_info) threading_helper.threading_cleanup(*thread_info)
if __name__ == "__main__": if __name__ == "__main__":

View File

@ -14,8 +14,9 @@ import unittest
import socketserver import socketserver
import test.support import test.support
from test.support import reap_children, reap_threads, verbose from test.support import reap_children, verbose
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
test.support.requires("network") test.support.requires("network")
@ -120,7 +121,7 @@ class SocketServerTest(unittest.TestCase):
self.assertEqual(server.server_address, server.socket.getsockname()) self.assertEqual(server.server_address, server.socket.getsockname())
return server return server
@reap_threads @threading_helper.reap_threads
def run_server(self, svrcls, hdlrbase, testfunc): def run_server(self, svrcls, hdlrbase, testfunc):
server = self.make_server(self.pickaddr(svrcls.address_family), server = self.make_server(self.pickaddr(svrcls.address_family),
svrcls, hdlrbase) svrcls, hdlrbase)
@ -249,7 +250,7 @@ class SocketServerTest(unittest.TestCase):
socketserver.DatagramRequestHandler, socketserver.DatagramRequestHandler,
self.dgram_examine) self.dgram_examine)
@reap_threads @threading_helper.reap_threads
def test_shutdown(self): def test_shutdown(self):
# Issue #2302: shutdown() should always succeed in making an # Issue #2302: shutdown() should always succeed in making an
# other thread leave serve_forever(). # other thread leave serve_forever().

View File

@ -5,6 +5,7 @@ import unittest
import unittest.mock import unittest.mock
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
import socket import socket
import select import select
import time import time
@ -4429,7 +4430,7 @@ class TestPostHandshakeAuth(unittest.TestCase):
# Ignore expected SSLError in ConnectionHandler of ThreadedEchoServer # Ignore expected SSLError in ConnectionHandler of ThreadedEchoServer
# (it is only raised sometimes on Windows) # (it is only raised sometimes on Windows)
with support.catch_threading_exception() as cm: with threading_helper.catch_threading_exception() as cm:
server = ThreadedEchoServer(context=server_context, chatty=False) server = ThreadedEchoServer(context=server_context, chatty=False)
with server: with server:
with client_context.wrap_socket(socket.socket(), with client_context.wrap_socket(socket.socket(),
@ -4750,11 +4751,11 @@ def test_main(verbose=False):
if support.is_resource_enabled('network'): if support.is_resource_enabled('network'):
tests.append(NetworkedTests) tests.append(NetworkedTests)
thread_info = support.threading_setup() thread_info = threading_helper.threading_setup()
try: try:
support.run_unittest(*tests) support.run_unittest(*tests)
finally: finally:
support.threading_cleanup(*thread_info) threading_helper.threading_cleanup(*thread_info)
if __name__ == "__main__": if __name__ == "__main__":
test_main() test_main()

View File

@ -1,5 +1,3 @@
from test import support
from test.support.script_helper import assert_python_ok, assert_python_failure
import builtins import builtins
import codecs import codecs
import gc import gc
@ -11,6 +9,9 @@ import subprocess
import sys import sys
import sysconfig import sysconfig
import test.support import test.support
from test import support
from test.support.script_helper import assert_python_ok, assert_python_failure
from test.support import threading_helper
import textwrap import textwrap
import unittest import unittest
import warnings import warnings
@ -365,7 +366,7 @@ class SysModuleTest(unittest.TestCase):
) )
# sys._current_frames() is a CPython-only gimmick. # sys._current_frames() is a CPython-only gimmick.
@test.support.reap_threads @threading_helper.reap_threads
def test_current_frames(self): def test_current_frames(self):
import threading import threading
import traceback import traceback

View File

@ -2,6 +2,7 @@ import os
import unittest import unittest
import random import random
from test import support from test import support
from test.support import threading_helper
import _thread as thread import _thread as thread
import time import time
import weakref import weakref
@ -32,8 +33,8 @@ class BasicThreadTest(unittest.TestCase):
self.running = 0 self.running = 0
self.next_ident = 0 self.next_ident = 0
key = support.threading_setup() key = threading_helper.threading_setup()
self.addCleanup(support.threading_cleanup, *key) self.addCleanup(threading_helper.threading_cleanup, *key)
class ThreadRunningTests(BasicThreadTest): class ThreadRunningTests(BasicThreadTest):
@ -58,7 +59,7 @@ class ThreadRunningTests(BasicThreadTest):
self.done_mutex.release() self.done_mutex.release()
def test_starting_threads(self): def test_starting_threads(self):
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
# Basic test for thread creation. # Basic test for thread creation.
for i in range(NUMTASKS): for i in range(NUMTASKS):
self.newtask() self.newtask()
@ -94,7 +95,7 @@ class ThreadRunningTests(BasicThreadTest):
verbose_print("trying stack_size = (%d)" % tss) verbose_print("trying stack_size = (%d)" % tss)
self.next_ident = 0 self.next_ident = 0
self.created = 0 self.created = 0
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
for i in range(NUMTASKS): for i in range(NUMTASKS):
self.newtask() self.newtask()
@ -116,7 +117,7 @@ class ThreadRunningTests(BasicThreadTest):
mut.acquire() mut.acquire()
mut.release() mut.release()
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
thread.start_new_thread(task, ()) thread.start_new_thread(task, ())
while not started: while not started:
time.sleep(POLL_SLEEP) time.sleep(POLL_SLEEP)
@ -140,7 +141,7 @@ class ThreadRunningTests(BasicThreadTest):
started = thread.allocate_lock() started = thread.allocate_lock()
with support.catch_unraisable_exception() as cm: with support.catch_unraisable_exception() as cm:
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
started.acquire() started.acquire()
thread.start_new_thread(task, ()) thread.start_new_thread(task, ())
started.acquire() started.acquire()
@ -180,7 +181,7 @@ class Barrier:
class BarrierTest(BasicThreadTest): class BarrierTest(BasicThreadTest):
def test_barrier(self): def test_barrier(self):
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
self.bar = Barrier(NUMTASKS) self.bar = Barrier(NUMTASKS)
self.running = NUMTASKS self.running = NUMTASKS
for i in range(NUMTASKS): for i in range(NUMTASKS):
@ -223,7 +224,7 @@ class TestForkInThread(unittest.TestCase):
self.read_fd, self.write_fd = os.pipe() self.read_fd, self.write_fd = os.pipe()
@unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork') @unittest.skipUnless(hasattr(os, 'fork'), 'need os.fork')
@support.reap_threads @threading_helper.reap_threads
def test_forkinthread(self): def test_forkinthread(self):
pid = None pid = None
@ -243,7 +244,7 @@ class TestForkInThread(unittest.TestCase):
finally: finally:
os._exit(0) os._exit(0)
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
thread.start_new_thread(fork_thread, (self.read_fd, self.write_fd)) thread.start_new_thread(fork_thread, (self.read_fd, self.write_fd))
self.assertEqual(os.read(self.read_fd, 2), b"OK") self.assertEqual(os.read(self.read_fd, 2), b"OK")
os.close(self.write_fd) os.close(self.write_fd)

View File

@ -15,7 +15,7 @@ provoking a 2.0 failure under Linux.
import tempfile import tempfile
from test.support import start_threads from test.support import threading_helper
import unittest import unittest
import io import io
import threading import threading
@ -50,7 +50,7 @@ class TempFileGreedy(threading.Thread):
class ThreadedTempFileTest(unittest.TestCase): class ThreadedTempFileTest(unittest.TestCase):
def test_main(self): def test_main(self):
threads = [TempFileGreedy() for i in range(NUM_THREADS)] threads = [TempFileGreedy() for i in range(NUM_THREADS)]
with start_threads(threads, startEvent.set): with threading_helper.start_threads(threads, startEvent.set):
pass pass
ok = sum(t.ok_count for t in threads) ok = sum(t.ok_count for t in threads)
errors = [str(t.name) + str(t.errors.getvalue()) errors = [str(t.name) + str(t.errors.getvalue())

View File

@ -3,6 +3,7 @@ Tests for the threading module.
""" """
import test.support import test.support
from test.support import threading_helper
from test.support import verbose, import_module, cpython_only from test.support import verbose, import_module, cpython_only
from test.support.script_helper import assert_python_ok, assert_python_failure from test.support.script_helper import assert_python_ok, assert_python_failure
@ -75,10 +76,10 @@ class TestThread(threading.Thread):
class BaseTestCase(unittest.TestCase): class BaseTestCase(unittest.TestCase):
def setUp(self): def setUp(self):
self._threads = test.support.threading_setup() self._threads = threading_helper.threading_setup()
def tearDown(self): def tearDown(self):
test.support.threading_cleanup(*self._threads) threading_helper.threading_cleanup(*self._threads)
test.support.reap_children() test.support.reap_children()
@ -130,7 +131,7 @@ class ThreadTests(BaseTestCase):
done.set() done.set()
done = threading.Event() done = threading.Event()
ident = [] ident = []
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
tid = _thread.start_new_thread(f, ()) tid = _thread.start_new_thread(f, ())
done.wait() done.wait()
self.assertEqual(ident[0], tid) self.assertEqual(ident[0], tid)
@ -171,7 +172,7 @@ class ThreadTests(BaseTestCase):
mutex = threading.Lock() mutex = threading.Lock()
mutex.acquire() mutex.acquire()
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
tid = _thread.start_new_thread(f, (mutex,)) tid = _thread.start_new_thread(f, (mutex,))
# Wait for the thread to finish. # Wait for the thread to finish.
mutex.acquire() mutex.acquire()

View File

@ -2,6 +2,7 @@ import sys
import unittest import unittest
from doctest import DocTestSuite from doctest import DocTestSuite
from test import support from test import support
from test.support import threading_helper
import weakref import weakref
import gc import gc
@ -65,8 +66,8 @@ class BaseLocalTest:
# Simply check that the variable is correctly set # Simply check that the variable is correctly set
self.assertEqual(local.x, i) self.assertEqual(local.x, i)
with support.start_threads(threading.Thread(target=f, args=(i,)) with threading_helper.start_threads(threading.Thread(target=f, args=(i,))
for i in range(10)): for i in range(10)):
pass pass
def test_derived_cycle_dealloc(self): def test_derived_cycle_dealloc(self):

View File

@ -5,6 +5,7 @@ import signal
import os import os
import sys import sys
from test import support from test import support
from test.support import threading_helper
import _thread as thread import _thread as thread
import time import time
@ -39,7 +40,7 @@ def send_signals():
class ThreadSignals(unittest.TestCase): class ThreadSignals(unittest.TestCase):
def test_signals(self): def test_signals(self):
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
# Test signal handling semantics of threads. # Test signal handling semantics of threads.
# We spawn a thread, have the thread send two signals, and # We spawn a thread, have the thread send two signals, and
# wait for it to finish. Check that we got both signals # wait for it to finish. Check that we got both signals
@ -129,7 +130,7 @@ class ThreadSignals(unittest.TestCase):
def other_thread(): def other_thread():
rlock.acquire() rlock.acquire()
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
thread.start_new_thread(other_thread, ()) thread.start_new_thread(other_thread, ())
# Wait until we can't acquire it without blocking... # Wait until we can't acquire it without blocking...
while rlock.acquire(blocking=False): while rlock.acquire(blocking=False):
@ -165,7 +166,7 @@ class ThreadSignals(unittest.TestCase):
time.sleep(0.5) time.sleep(0.5)
lock.release() lock.release()
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
thread.start_new_thread(other_thread, ()) thread.start_new_thread(other_thread, ())
# Wait until we can't acquire it without blocking... # Wait until we can't acquire it without blocking...
while lock.acquire(blocking=False): while lock.acquire(blocking=False):
@ -212,7 +213,7 @@ class ThreadSignals(unittest.TestCase):
os.kill(process_pid, signal.SIGUSR1) os.kill(process_pid, signal.SIGUSR1)
done.release() done.release()
with support.wait_threads_exit(): with threading_helper.wait_threads_exit():
# Send the signals from the non-main thread, since the main thread # Send the signals from the non-main thread, since the main thread
# is the only one that can process signals. # is the only one that can process signals.
thread.start_new_thread(send_signals, ()) thread.start_new_thread(send_signals, ())

View File

@ -10,6 +10,7 @@ import hashlib
from test import support from test import support
from test.support import hashlib_helper from test.support import hashlib_helper
from test.support import threading_helper
try: try:
import ssl import ssl
@ -666,11 +667,11 @@ def setUpModule():
# Store the threading_setup in a key and ensure that it is cleaned up # Store the threading_setup in a key and ensure that it is cleaned up
# in the tearDown # in the tearDown
global threads_key global threads_key
threads_key = support.threading_setup() threads_key = threading_helper.threading_setup()
def tearDownModule(): def tearDownModule():
if threads_key: if threads_key:
support.threading_cleanup(*threads_key) threading_helper.threading_cleanup(*threads_key)
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()

View File

@ -16,6 +16,7 @@ import io
import contextlib import contextlib
from test import support from test import support
from test.support import socket_helper from test.support import socket_helper
from test.support import threading_helper
from test.support import ALWAYS_EQ, LARGEST, SMALLEST from test.support import ALWAYS_EQ, LARGEST, SMALLEST
try: try:
@ -1464,7 +1465,7 @@ class UseBuiltinTypesTestCase(unittest.TestCase):
self.assertTrue(server.use_builtin_types) self.assertTrue(server.use_builtin_types)
@support.reap_threads @threading_helper.reap_threads
def test_main(): def test_main():
support.run_unittest(XMLRPCTestCase, HelperTestCase, DateTimeTestCase, support.run_unittest(XMLRPCTestCase, HelperTestCase, DateTimeTestCase,
BinaryTestCase, FaultTestCase, UseBuiltinTypesTestCase, BinaryTestCase, FaultTestCase, UseBuiltinTypesTestCase,