diff --git a/Lib/test/test_asyncio/test_base_events.py b/Lib/test/test_asyncio/test_base_events.py index ccdd3bae733..66191d9d1c1 100644 --- a/Lib/test/test_asyncio/test_base_events.py +++ b/Lib/test/test_asyncio/test_base_events.py @@ -216,6 +216,9 @@ class BaseEventLoopTests(test_utils.TestCase): raise NotImplementedError( 'cannot submit into a dummy executor') + self.loop._process_events = mock.Mock() + self.loop._write_to_self = mock.Mock() + executor = DummyExecutor() self.loop.set_default_executor(executor) self.assertIs(executor, self.loop._default_executor) @@ -226,6 +229,9 @@ class BaseEventLoopTests(test_utils.TestCase): with self.assertWarns(DeprecationWarning): self.loop.set_default_executor(executor) + # Avoid cleaning up the executor mock + self.loop._default_executor = None + def test_call_soon(self): def cb(): pass diff --git a/Lib/test/test_asyncio/test_tasks.py b/Lib/test/test_asyncio/test_tasks.py index 6e832eab6d5..576714faa88 100644 --- a/Lib/test/test_asyncio/test_tasks.py +++ b/Lib/test/test_asyncio/test_tasks.py @@ -3232,6 +3232,8 @@ class RunCoroutineThreadsafeTests(test_utils.TestCase): self.loop.set_exception_handler(callback) # Set corrupted task factory + self.addCleanup(self.loop.set_task_factory, + self.loop.get_task_factory()) self.loop.set_task_factory(task_factory) # Run event loop diff --git a/Lib/test/test_asyncio/utils.py b/Lib/test/test_asyncio/utils.py index 5b4bb123a9e..ea608f4a10c 100644 --- a/Lib/test/test_asyncio/utils.py +++ b/Lib/test/test_asyncio/utils.py @@ -509,9 +509,11 @@ 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) + if loop._default_executor is not None: + if not loop.is_closed(): + loop.run_until_complete(loop.shutdown_default_executor()) + else: + loop._default_executor.shutdown(wait=True) loop.close() policy = support.maybe_get_event_loop_policy() if policy is not None: