From 18e95b4176256f100429a806d0455406df98f984 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Sep 2017 08:43:04 -0700 Subject: [PATCH] bpo-31234: Join threads in tests (#3572) Call thread.join() on threads to prevent the "dangling threads" warning. --- Lib/test/test_concurrent_futures.py | 3 +++ Lib/test/test_decimal.py | 3 +++ Lib/test/test_smtplib.py | 4 +++- Lib/test/test_xmlrpc.py | 8 ++++++-- 4 files changed, 15 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py index 7bc733efb1..57dc994d28 100644 --- a/Lib/test/test_concurrent_futures.py +++ b/Lib/test/test_concurrent_futures.py @@ -772,6 +772,7 @@ class FutureTests(BaseTestCase): t.start() self.assertEqual(f1.result(timeout=5), 42) + t.join() def test_result_with_cancel(self): # TODO(brian@sweetapp.com): This test is timing dependent. @@ -785,6 +786,7 @@ class FutureTests(BaseTestCase): t.start() self.assertRaises(futures.CancelledError, f1.result, timeout=5) + t.join() def test_exception_with_timeout(self): self.assertRaises(futures.TimeoutError, @@ -813,6 +815,7 @@ class FutureTests(BaseTestCase): t.start() self.assertTrue(isinstance(f1.exception(timeout=5), OSError)) + t.join() @test.support.reap_threads def test_main(): diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index 5d9da0e374..5aea51e8b7 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -1618,6 +1618,9 @@ class ThreadingTest(unittest.TestCase): for sig in Signals[self.decimal]: self.assertFalse(DefaultContext.flags[sig]) + th1.join() + th2.join() + DefaultContext.prec = save_prec DefaultContext.Emax = save_emax DefaultContext.Emin = save_emin diff --git a/Lib/test/test_smtplib.py b/Lib/test/test_smtplib.py index 4c9b7d367c..040ad4e059 100644 --- a/Lib/test/test_smtplib.py +++ b/Lib/test/test_smtplib.py @@ -611,7 +611,9 @@ class TooLongLineTests(unittest.TestCase): self.sock.settimeout(15) self.port = support.bind_port(self.sock) servargs = (self.evt, self.respdata, self.sock) - threading.Thread(target=server, args=servargs).start() + thread = threading.Thread(target=server, args=servargs) + thread.start() + self.addCleanup(thread.join) self.evt.wait() self.evt.clear() diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py index f2b496ac9a..c9099e0511 100644 --- a/Lib/test/test_xmlrpc.py +++ b/Lib/test/test_xmlrpc.py @@ -755,7 +755,9 @@ class BaseServerTestCase(unittest.TestCase): self.evt = threading.Event() # start server thread to handle requests serv_args = (self.evt, self.request_count, self.requestHandler) - threading.Thread(target=self.threadFunc, args=serv_args).start() + thread = threading.Thread(target=self.threadFunc, args=serv_args) + thread.start() + self.addCleanup(thread.join) # wait for the server to be ready self.evt.wait() @@ -1206,7 +1208,9 @@ class FailingServerTestCase(unittest.TestCase): self.evt = threading.Event() # start server thread to handle requests serv_args = (self.evt, 1) - threading.Thread(target=http_server, args=serv_args).start() + thread = threading.Thread(target=http_server, args=serv_args) + thread.start() + self.addCleanup(thread.join) # wait for the server to be ready self.evt.wait() -- 2.40.0