]> granicus.if.org Git - python/commitdiff
[3.6] bpo-31250: test_asyncio: fix dangling threads (#3517)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 12 Sep 2017 21:18:23 +0000 (14:18 -0700)
committerGitHub <noreply@github.com>
Tue, 12 Sep 2017 21:18:23 +0000 (14:18 -0700)
* bpo-31250, test_asyncio: fix dangling threads (#3252)

* Explicitly call shutdown(wait=True) on executors to wait until all
  threads complete to prevent side effects between tests.
* Fix test_loop_self_reading_exception(): don't mock loop.close().
  Previously, the original close() method was called rather than the
  mock, because how set_event_loop() registered loop.close().

(cherry picked from commit 16432beadb8eba079c9786cc0c0eaacfd9fd2f7b)

* bpo-31250, test_asyncio: fix EventLoopTestsMixin.tearDown() (#3264)

Call doCleanups() to close the loop after calling
executor.shutdown(wait=True): see TestCase.set_event_loop() of
asyncio.test_utils.

Replace also gc.collect() with support.gc_collect().

(cherry picked from commit e8a533fbc734af6eeb389202ba6c6e9c2548027f)

Lib/asyncio/test_utils.py
Lib/test/test_asyncio/test_events.py
Lib/test/test_asyncio/test_futures.py
Lib/test/test_asyncio/test_proactor_events.py

index 94d48e1361a3988e233e232aa2977a3ccbf6b3d5..8b8c22a74757de5efb0379b855953f7590d4962a 100644 (file)
@@ -438,12 +438,19 @@ def get_function_source(func):
 
 
 class TestCase(unittest.TestCase):
+    @staticmethod
+    def close_loop(loop):
+        executor = loop._default_executor
+        if executor is not None:
+            executor.shutdown(wait=True)
+        loop.close()
+
     def set_event_loop(self, loop, *, cleanup=True):
         assert loop is not None
         # ensure that the event loop is passed explicitly in asyncio
         events.set_event_loop(None)
         if cleanup:
-            self.addCleanup(loop.close)
+            self.addCleanup(self.close_loop, loop)
 
     def new_test_loop(self, gen=None):
         loop = TestLoop(gen)
index 9746678607c936fcbbd6938b6deab853867447c3..27781a2d91b336de08a18d2d16afc8851848c741 100644 (file)
@@ -258,8 +258,8 @@ class EventLoopTestsMixin:
         if not self.loop.is_closed():
             test_utils.run_briefly(self.loop)
 
-        self.loop.close()
-        gc.collect()
+        self.doCleanups()
+        support.gc_collect()
         super().tearDown()
 
     def test_run_until_complete_nesting(self):
index a06059dc9b7cf0efa6717cc61a47184468e2b002..4320a901f49cf63c1af73c821821efb0de7651f8 100644 (file)
@@ -413,6 +413,7 @@ class BaseFutureTests:
         self.assertTrue(asyncio.isfuture(f2))
         self.assertEqual(res, 'oi')
         self.assertNotEqual(ident, threading.get_ident())
+        ex.shutdown(wait=True)
 
     def test_wrap_future_future(self):
         f1 = self._new_future(loop=self.loop)
@@ -428,6 +429,7 @@ class BaseFutureTests:
             f1 = ex.submit(run, 'oi')
             f2 = asyncio.wrap_future(f1)
             self.assertIs(self.loop, f2._loop)
+            ex.shutdown(wait=True)
 
     def test_wrap_future_cancel(self):
         f1 = concurrent.futures.Future()
index 4dfc61259f873120cdb90eeaa38004ec488a2d4f..d76da661ab92dadb7d073ef06b6af52e78f466dc 100644 (file)
@@ -529,7 +529,6 @@ class BaseProactorEventLoopTests(test_utils.TestCase):
             self.loop._loop_self_reading)
 
     def test_loop_self_reading_exception(self):
-        self.loop.close = mock.Mock()
         self.loop.call_exception_handler = mock.Mock()
         self.proactor.recv.side_effect = OSError()
         self.loop._loop_self_reading()