Skip to content

Commit

Permalink
Teardown of the event_loop fixture no longer replaces the event loop …
Browse files Browse the repository at this point in the history
…policy.

Signed-off-by: Michael Seifert <[email protected]>
  • Loading branch information
seifertm authored and Tinche committed Jan 4, 2022
1 parent dceeb68 commit a6758a1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,10 @@ Only test coroutines will be affected (by default, coroutines prefixed by

Changelog
---------
0.17.0 (UNRELEASED)
~~~~~~~~~~~~~~~~~~~
- `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>`_

0.16.0 (2021-10-16)
~~~~~~~~~~~~~~~~~~~
- Add support for Python 3.10
Expand Down
7 changes: 5 additions & 2 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,11 @@ def get_and_strip_from(self, name, data_dict):
def pytest_fixture_post_finalizer(fixturedef, request):
"""Called after fixture teardown"""
if fixturedef.argname == "event_loop":
# Set empty loop policy, so that subsequent get_event_loop() provides a new loop
asyncio.set_event_loop_policy(None)
policy = asyncio.get_event_loop_policy()
policy.get_event_loop().close() # Clean up existing loop to avoid ResourceWarnings
new_loop = policy.new_event_loop() # Replace existing event loop
# Ensure subsequent calls to get_event_loop() succeed
policy.set_event_loop(new_loop)


@pytest.hookimpl(hookwrapper=True)
Expand Down
16 changes: 16 additions & 0 deletions tests/respect_event_loop_policy/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Defines and sets a custom event loop policy"""
import asyncio
from asyncio import DefaultEventLoopPolicy, SelectorEventLoop


class TestEventLoop(SelectorEventLoop):
pass


class TestEventLoopPolicy(DefaultEventLoopPolicy):
def new_event_loop(self):
return TestEventLoop()


# This statement represents a code which sets a custom event loop policy
asyncio.set_event_loop_policy(TestEventLoopPolicy())
16 changes: 16 additions & 0 deletions tests/respect_event_loop_policy/test_respects_event_loop_policy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"""Tests that any externally provided event loop policy remains unaltered."""
import asyncio

import pytest


@pytest.mark.asyncio
async def test_uses_loop_provided_by_custom_policy():
"""Asserts that test cases use the event loop provided by the custom event loop policy"""
assert type(asyncio.get_event_loop()).__name__ == "TestEventLoop"


@pytest.mark.asyncio
async def test_custom_policy_is_not_overwritten():
"""Asserts that any custom event loop policy stays the same across test cases"""
assert type(asyncio.get_event_loop()).__name__ == "TestEventLoop"

0 comments on commit a6758a1

Please sign in to comment.