diff --git a/CHANGES/2804.feature b/CHANGES/2804.feature new file mode 100644 index 00000000000..a92b0051e93 --- /dev/null +++ b/CHANGES/2804.feature @@ -0,0 +1,3 @@ +Install a test event loop as default by +``asyncio.set_event_loop()``. The change affects aiohttp test utils +but backward compatibility is not broken for 99.99% of use cases. \ No newline at end of file diff --git a/aiohttp/pytest_plugin.py b/aiohttp/pytest_plugin.py index 86c658f3165..5c9d3288c0c 100644 --- a/aiohttp/pytest_plugin.py +++ b/aiohttp/pytest_plugin.py @@ -203,8 +203,8 @@ def loop(loop_factory, fast, loop_debug): with loop_context(loop_factory, fast=fast) as _loop: if loop_debug: _loop.set_debug(True) # pragma: no cover + asyncio.set_event_loop(_loop) yield _loop - asyncio.set_event_loop(None) @pytest.fixture diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py index 0253daf80e7..b08e2aca786 100644 --- a/aiohttp/test_utils.py +++ b/aiohttp/test_utils.py @@ -410,7 +410,7 @@ def setup_test_loop(loop_factory=asyncio.new_event_loop): once they are done with the loop. """ loop = loop_factory() - asyncio.set_event_loop(None) + asyncio.set_event_loop(loop) if sys.platform != "win32": policy = asyncio.get_event_loop_policy() watcher = asyncio.SafeChildWatcher() diff --git a/docs/testing.rst b/docs/testing.rst index 845bd6825cd..ed728d4f568 100644 --- a/docs/testing.rst +++ b/docs/testing.rst @@ -788,6 +788,20 @@ Utilities The caller should also call teardown_test_loop, once they are done with the loop. + .. note:: + + As side effect the function changes asyncio *default loop* by + :func:`asyncio.set_event_loop` call. + + Previous default loop is not restored. + + It should not be a problem for test suite: every test expects a + new test loop instance anyway. + + .. versionchanged:: 3.1 + + The function installs a created event loop as *default*. + .. function:: teardown_test_loop(loop) Teardown and cleanup an event_loop created by setup_test_loop. diff --git a/tests/test_loop.py b/tests/test_loop.py index ceb719433a4..246fe4ed3ed 100644 --- a/tests/test_loop.py +++ b/tests/test_loop.py @@ -29,4 +29,11 @@ async def on_startup_hook(self, app): @unittest_run_loop async def test_on_startup_hook(self): - assert self.startup_loop is not None + self.assertIsNotNone(self.startup_loop) + + def test_default_loop(self): + self.assertIs(self.loop, asyncio.get_event_loop()) + + +def test_default_loop(loop): + assert asyncio.get_event_loop() is loop