-
Notifications
You must be signed in to change notification settings - Fork 161
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
1) A new test case that fails with 0.12.0, and pass with this commit.
test_async_fixtures_with_finalizer_scope.py: 2) Main problem is due to side effects of asyncio.get_event_loop(). See: https://github.com/python/cpython/blob/3.8/Lib/asyncio/events.py#L636 This method could either return an existing loop or create a new one. This commit replaces all asyncio.get_event_loop() with previous pytest-asyncio code (0.10.0), so plugin uses the loop provided by event_loop fixture (instead of calling asyncio.get_event_loop()) Except following block, that has not been modified in this commit (as it behaves similar to 0.10.0) https://github.com/pytest-dev/pytest-asyncio/blob/v0.12.0/pytest_asyncio/plugin.py#L54-L66 Changes are for using always the new loop provided by event_loop fixture. Instead of calling get_event_loop() that: - either returns global recorded loop (with set_event_loop()) in case there is one, - or otherwise creates and record a new one
- Loading branch information
Showing
2 changed files
with
59 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
tests/async_fixtures/test_async_fixtures_with_finalizer_scope.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import asyncio | ||
import contextlib | ||
import functools | ||
import pytest | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_module_scope(port): | ||
await asyncio.sleep(0.01) | ||
assert port | ||
|
||
@pytest.fixture(scope="module") | ||
def event_loop(): | ||
"""Change event_loop fixture to module level.""" | ||
policy = asyncio.get_event_loop_policy() | ||
loop = policy.new_event_loop() | ||
yield loop | ||
loop.close() | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
async def port(request, event_loop): | ||
def port_finalizer(finalizer): | ||
async def port_afinalizer(): | ||
await finalizer(None, None, None) | ||
event_loop.run_until_complete(port_afinalizer()) | ||
|
||
context_manager = port_map() | ||
port = await context_manager.__aenter__() | ||
request.addfinalizer(functools.partial(port_finalizer, context_manager.__aexit__)) | ||
return True | ||
|
||
|
||
@contextlib.asynccontextmanager | ||
async def port_map(): | ||
worker = asyncio.create_task(asyncio.sleep(0.2)) | ||
yield | ||
await worker |