Skip to content

Commit

Permalink
Do not try to initialize async fixtures without explicit asyncio mark…
Browse files Browse the repository at this point in the history
… in strict mode (#307)

* test: Package-scoped event_loop fixture no longer leaks into other tests.

The expected behaviour is that the `event_loop` fixture defined in `tests/sessionloop/conftest.py` is torn down when all tests in `tests/sessionloop` are complete. Running the tests with the pytest option --setup-show pointed out that the fixture is torn down at the end of the test session, instead. This is an unintended side effect of the sessionloop test which may affect other tests in the test suite.

Reducing the fixture scope from "package" to "module" results in the expected behaviour. The module was renamed to reflect the fact that the tests do not use a session scope.

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>

* test: Removed test with obsolete "forbid_global_loop".

forbid_global_loop was an option to pytest.mark.asyncio which was removed in v0.6.0. The two subprocess tests are otherwise identical. Therefore, one of the tests was removed along with the obsolete option.

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>

* test: Ignore subprocess tests when running on CPython 3.7.

When run with Python 3.7 asyncio.subprocess.create_subprocess_exec seems to be
affected by an issue that prevents correct cleanup. Tests using pytest-trio
will report that signal handling is already performed by another library and
fail. [1] This is possibly a bug in CPython 3.7, so we ignore this test for
that Python version.

CPython 3.7 uses asyncio.streams.StreamReader and asyncio.streams.StreamWriter
to implement asyncio.streams.StreamReaderProtocol and
asyncio.subprocess.SubprocessStreamProtocol. StreamReaderProtocol contained
cyclic references between the reader and the protocol, which prevented
garbage collection. While StreamReaderProtocol received a patch [2],
SubprocessStreamProtocol, which is used by create_subprocess_exec, possibly
has the same problem, but was not patched as part of CPython 3.7.

That's why we ignore this test for CPython 3.7.

[1] python-trio/pytest-trio#126
[2] python/cpython#9201

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>

* build: Added pytest-trio to the test dependencies.

This allows testing compatibility between pytest-trio and pytest-asyncio.

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>

* fix: Do not try to initialize async fixtures without explicit asyncio mark in strict mode.

This fixes a bug that breaks compatibility with pytest_trio.

Closes #298

Signed-off-by: Michael Seifert <m.seifert@digitalernachschub.de>
  • Loading branch information
seifertm committed Mar 15, 2022
1 parent 133d8a8 commit f979af9
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 16 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Changelog
=========

UNRELEASED
=================
- Adds `pytest-trio <https://pypi.org/project/pytest-trio/>`_ to the test dependencies
- Fixes a bug that caused pytest-asyncio to try to set up async pytest_trio fixtures in strict mode. `#298 <https://github.com/pytest-dev/pytest-asyncio/issues/298>`_

0.18.2 (22-03-03)
=================
- Fix asyncio auto mode not marking static methods. `#295 <https://github.com/pytest-dev/pytest-asyncio/issues/295>`_
Expand Down
6 changes: 5 additions & 1 deletion pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ def _preprocess_async_fixtures(config: Config, holder: Set[FixtureDef]) -> None:
# Nothing to do with a regular fixture function
continue
if not _has_explicit_asyncio_mark(func):
if asyncio_mode == Mode.AUTO:
if asyncio_mode == Mode.STRICT:
# Ignore async fixtures without explicit asyncio mark in strict mode
# This applies to pytest_trio fixtures, for example
continue
elif asyncio_mode == Mode.AUTO:
# Enforce asyncio mode if 'auto'
_set_explicit_asyncio_mark(func)
elif asyncio_mode == Mode.LEGACY:
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ testing =
hypothesis >= 5.7.1
flaky >= 3.5.0
mypy == 0.931
pytest-trio >= 0.7.0

[options.entry_points]
pytest11 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@
import pytest


class CustomSelectorLoopSession(asyncio.SelectorEventLoop):
class CustomSelectorLoop(asyncio.SelectorEventLoop):
"""A subclass with no overrides, just to test for presence."""


loop = CustomSelectorLoopSession()
loop = CustomSelectorLoop()


@pytest.fixture(scope="package")
@pytest.fixture(scope="module")
def event_loop():
"""Create an instance of the default event loop for each test case."""
yield loop
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Unit tests for overriding the event loop with a session scoped one."""
"""Unit tests for overriding the event loop with a larger scoped one."""
import asyncio

import pytest
Expand All @@ -8,7 +8,7 @@
async def test_for_custom_loop():
"""This test should be executed using the custom loop."""
await asyncio.sleep(0.01)
assert type(asyncio.get_event_loop()).__name__ == "CustomSelectorLoopSession"
assert type(asyncio.get_event_loop()).__name__ == "CustomSelectorLoop"


@pytest.mark.asyncio
Expand Down
23 changes: 13 additions & 10 deletions tests/test_subprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@ def event_loop():
loop.close()


@pytest.mark.asyncio(forbid_global_loop=False)
async def test_subprocess(event_loop):
"""Starting a subprocess should be possible."""
proc = await asyncio.subprocess.create_subprocess_exec(
sys.executable, "--version", stdout=asyncio.subprocess.PIPE
)
await proc.communicate()
@pytest.mark.skipif(
sys.version_info < (3, 8),
reason="""
When run with Python 3.7 asyncio.subprocess.create_subprocess_exec seems to be
affected by an issue that prevents correct cleanup. Tests using pytest-trio
will report that signal handling is already performed by another library and
fail. [1] This is possibly a bug in CPython 3.7, so we ignore this test for
that Python version.

@pytest.mark.asyncio(forbid_global_loop=True)
async def test_subprocess_forbid(event_loop):
[1] https://github.com/python-trio/pytest-trio/issues/126
""",
)
@pytest.mark.asyncio
async def test_subprocess(event_loop):
"""Starting a subprocess should be possible."""
proc = await asyncio.subprocess.create_subprocess_exec(
sys.executable, "--version", stdout=asyncio.subprocess.PIPE
Expand Down
25 changes: 25 additions & 0 deletions tests/trio/test_fixtures.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from textwrap import dedent


def test_strict_mode_ignores_trio_fixtures(testdir):
testdir.makepyfile(
dedent(
"""\
import pytest
import pytest_asyncio
import pytest_trio
pytest_plugins = ["pytest_asyncio", "pytest_trio"]
@pytest_trio.trio_fixture
async def any_fixture():
return True
@pytest.mark.trio
async def test_anything(any_fixture):
pass
"""
)
)
result = testdir.runpytest("--asyncio-mode=strict")
result.assert_outcomes(passed=1)

0 comments on commit f979af9

Please sign in to comment.