-
Notifications
You must be signed in to change notification settings - Fork 139
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
FIX #162 "attached to a different loop", and add a new test case #164
Changes from 4 commits
b96c042
bf4538c
a151a60
341c369
f246a8c
882c11e
81af289
1ee279e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import asyncio | ||
import contextlib | ||
import functools | ||
import pytest | ||
|
||
|
||
@pytest.mark.asyncio | ||
async def test_module_with_event_loop_finalizer(port1): | ||
await asyncio.sleep(0.01) | ||
assert port1 | ||
|
||
@pytest.mark.asyncio | ||
async def test_module_with_get_event_loop_finalizer(port2): | ||
await asyncio.sleep(0.01) | ||
assert port2 | ||
|
||
@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 port1(request, event_loop): | ||
def port_finalizer(finalizer): | ||
async def port_afinalizer(): | ||
# await task inside get_event_loop() | ||
# RantimeError is raised if task is created on a different loop | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Typo: RuntimeError There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fixed |
||
await finalizer | ||
event_loop.run_until_complete(port_afinalizer()) | ||
|
||
worker = asyncio.ensure_future(asyncio.sleep(0.2)) | ||
request.addfinalizer(functools.partial(port_finalizer, worker)) | ||
return True | ||
|
||
|
||
@pytest.fixture(scope="module") | ||
async def port2(request, event_loop): | ||
def port_finalizer(finalizer): | ||
async def port_afinalizer(): | ||
# await task inside get_event_loop() | ||
# if loop is different a RuntimeError is raised | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to align the two comments from There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
await finalizer | ||
asyncio.get_event_loop().run_until_complete(port_afinalizer()) | ||
|
||
worker = asyncio.ensure_future(asyncio.sleep(0.2)) | ||
request.addfinalizer(functools.partial(port_finalizer, worker)) | ||
return True |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This task is not awaited inside
get_event_loop()
, but inloop.run_until_complete()
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed