Skip to content

Commit 489d91c

Browse files
authored
bpo-31249: test_concurrent_futures checks dangling threads (#3167)
Add a BaseTestCase class to test_concurrent_futures to check for dangling threads and processes on all tests, not only tests using ExecutorMixin.
1 parent 58cf748 commit 489d91c

File tree

1 file changed

+20
-12
lines changed

1 file changed

+20
-12
lines changed

‎Lib/test/test_concurrent_futures.py‎

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,20 @@ def my_method(self):
5959
pass
6060

6161

62+
class BaseTestCase(unittest.TestCase):
63+
def setUp(self):
64+
self._thread_key = test.support.threading_setup()
65+
66+
def tearDown(self):
67+
test.support.reap_children()
68+
test.support.threading_cleanup(*self._thread_key)
69+
70+
6271
class ExecutorMixin:
6372
worker_count = 5
6473

6574
def setUp(self):
66-
self._thread_cleanup = test.support.threading_setup()
75+
super().setUp()
6776

6877
self.t1 = time.time()
6978
try:
@@ -81,8 +90,7 @@ def tearDown(self):
8190
print("%.2fs" % dt, end=' ')
8291
self.assertLess(dt, 60, "synchronization issue: test lasted too long")
8392

84-
test.support.threading_cleanup(*self._thread_cleanup)
85-
test.support.reap_children()
93+
super().tearDown()
8694

8795
def _prime_executor(self):
8896
# Make sure that the executor is ready to do work before running the
@@ -130,7 +138,7 @@ def test_hang_issue12364(self):
130138
f.result()
131139

132140

133-
class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, unittest.TestCase):
141+
class ThreadPoolShutdownTest(ThreadPoolMixin, ExecutorShutdownTest, BaseTestCase):
134142
def _prime_executor(self):
135143
pass
136144

@@ -185,7 +193,7 @@ def test_thread_names_default(self):
185193
t.join()
186194

187195

188-
class ProcessPoolShutdownTest(ProcessPoolMixin, ExecutorShutdownTest, unittest.TestCase):
196+
class ProcessPoolShutdownTest(ProcessPoolMixin, ExecutorShutdownTest, BaseTestCase):
189197
def _prime_executor(self):
190198
pass
191199

@@ -322,7 +330,7 @@ def test_timeout(self):
322330
self.assertEqual(set([future2]), pending)
323331

324332

325-
class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests, unittest.TestCase):
333+
class ThreadPoolWaitTests(ThreadPoolMixin, WaitTests, BaseTestCase):
326334

327335
def test_pending_calls_race(self):
328336
# Issue #14406: multi-threaded race condition when waiting on all
@@ -340,7 +348,7 @@ def future_func():
340348
sys.setswitchinterval(oldswitchinterval)
341349

342350

343-
class ProcessPoolWaitTests(ProcessPoolMixin, WaitTests, unittest.TestCase):
351+
class ProcessPoolWaitTests(ProcessPoolMixin, WaitTests, BaseTestCase):
344352
pass
345353

346354

@@ -389,11 +397,11 @@ def test_duplicate_futures(self):
389397
self.assertEqual(len(completed), 1)
390398

391399

392-
class ThreadPoolAsCompletedTests(ThreadPoolMixin, AsCompletedTests, unittest.TestCase):
400+
class ThreadPoolAsCompletedTests(ThreadPoolMixin, AsCompletedTests, BaseTestCase):
393401
pass
394402

395403

396-
class ProcessPoolAsCompletedTests(ProcessPoolMixin, AsCompletedTests, unittest.TestCase):
404+
class ProcessPoolAsCompletedTests(ProcessPoolMixin, AsCompletedTests, BaseTestCase):
397405
pass
398406

399407

@@ -464,7 +472,7 @@ def test_max_workers_negative(self):
464472
self.executor_type(max_workers=number)
465473

466474

467-
class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, unittest.TestCase):
475+
class ThreadPoolExecutorTest(ThreadPoolMixin, ExecutorTest, BaseTestCase):
468476
def test_map_submits_without_iteration(self):
469477
"""Tests verifying issue 11777."""
470478
finished = []
@@ -481,7 +489,7 @@ def test_default_workers(self):
481489
(os.cpu_count() or 1) * 5)
482490

483491

484-
class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest, unittest.TestCase):
492+
class ProcessPoolExecutorTest(ProcessPoolMixin, ExecutorTest, BaseTestCase):
485493
def test_killed_child(self):
486494
# When a child process is abruptly terminated, the whole pool gets
487495
# "broken".
@@ -537,7 +545,7 @@ def test_traceback(self):
537545
f1.getvalue())
538546

539547

540-
class FutureTests(unittest.TestCase):
548+
class FutureTests(BaseTestCase):
541549
def test_done_callback_with_result(self):
542550
callback_result = None
543551
def fn(callback_future):

0 commit comments

Comments
 (0)