]> granicus.if.org Git - python/commitdiff
bpo-31250, test_asyncio: fix dangling threads (#3252)
authorVictor Stinner <victor.stinner@gmail.com>
Fri, 1 Sep 2017 12:46:06 +0000 (14:46 +0200)
committerGitHub <noreply@github.com>
Fri, 1 Sep 2017 12:46:06 +0000 (14:46 +0200)
* 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().

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

index 15059501070c70810f3cf2e65d440b41d52ad9d1..ecb3fca097b9bb72bf43bd3ad248c69de9911d50 100644 (file)
@@ -437,12 +437,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 ebedfec7fa3f5f9162597ce7a83beef42b753715..f8f614f1c3095bd6532209997003cebc6c356bdf 100644 (file)
@@ -380,6 +380,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)
@@ -395,6 +396,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()