Skip to content

Teardown of the event_loop fixture no longer replaces the event loop policy #192

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"