Skip to content

Commit 41c53e1

Browse files
committed
Teardown of the event_loop fixture no longer replaces the event loop policy.
Signed-off-by: Michael Seifert <[email protected]>
1 parent 4e2e6bd commit 41c53e1

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed

README.rst

+1
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ Changelog
177177
---------
178178
0.15.0 (UNRELEASED)
179179
~~~~~~~~~~~~~~~~~~~
180+
- `pytest-asyncio` no longer alters existing event loop policies. `#168 <https://github.com/pytest-dev/pytest-asyncio/issues/168>`_, `#188 <https://github.com/pytest-dev/pytest-asyncio/issues/168>`_
180181

181182
0.14.0 (2020-06-24)
182183
~~~~~~~~~~~~~~~~~~~

pytest_asyncio/plugin.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,9 @@ def get_and_strip_from(self, name, data_dict):
7676
def pytest_fixture_post_finalizer(fixturedef, request):
7777
"""Called after fixture teardown"""
7878
if fixturedef.argname == "event_loop":
79-
# Set empty loop policy, so that subsequent get_event_loop() provides a new loop
80-
asyncio.set_event_loop_policy(None)
79+
policy = asyncio.get_event_loop_policy()
80+
new_loop = policy.new_event_loop() # Replace existing event loop
81+
policy.set_event_loop(new_loop) # Ensure subsequent calls to get_event_loop() succeed
8182

8283

8384

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""Defines and sets a custom event loop policy"""
2+
import asyncio
3+
from asyncio import DefaultEventLoopPolicy, SelectorEventLoop
4+
5+
6+
class TestEventLoop(SelectorEventLoop):
7+
pass
8+
9+
10+
class TestEventLoopPolicy(DefaultEventLoopPolicy):
11+
def new_event_loop(self):
12+
return TestEventLoop()
13+
14+
15+
# This statement represents a code which sets a custom event loop policy
16+
asyncio.set_event_loop_policy(TestEventLoopPolicy())
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
"""Tests that any externally provided event loop policy remains unaltered."""
2+
import asyncio
3+
4+
import pytest
5+
6+
7+
@pytest.mark.asyncio
8+
async def test_uses_loop_provided_by_custom_policy():
9+
"""Asserts that test cases use the event loop provided by the custom event loop policy"""
10+
assert type(asyncio.get_event_loop()).__name__ == "TestEventLoop"
11+
12+
13+
@pytest.mark.asyncio
14+
async def test_custom_policy_is_not_overwritten():
15+
"""Asserts that any custom event loop policy stays the same across test cases"""
16+
assert type(asyncio.get_event_loop()).__name__ == "TestEventLoop"
17+

0 commit comments

Comments
 (0)