Skip to content

Commit

Permalink
Merge bfad9db into a7e5795
Browse files Browse the repository at this point in the history
  • Loading branch information
simonfagerholm committed Apr 27, 2020
2 parents a7e5795 + bfad9db commit 4d227ea
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
26 changes: 19 additions & 7 deletions pytest_asyncio/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,24 +125,34 @@ def pytest_pyfunc_call(pyfuncitem):
if 'asyncio' in pyfuncitem.keywords:
if getattr(pyfuncitem.obj, 'is_hypothesis_test', False):
pyfuncitem.obj.hypothesis.inner_test = wrap_in_sync(
pyfuncitem.obj.hypothesis.inner_test
pyfuncitem.obj.hypothesis.inner_test,
_loop=pyfuncitem.funcargs['event_loop']
)
else:
pyfuncitem.obj = wrap_in_sync(pyfuncitem.obj)
pyfuncitem.obj = wrap_in_sync(
pyfuncitem.obj,
_loop=pyfuncitem.funcargs['event_loop']
)
yield


def wrap_in_sync(func):
def wrap_in_sync(func, _loop):
"""Return a sync wrapper around an async function executing it in the
current event loop."""

@functools.wraps(func)
def inner(**kwargs):
coro = func(**kwargs)
if coro is not None:
task = asyncio.ensure_future(coro)
try:
asyncio.get_event_loop().run_until_complete(task)
loop = asyncio.get_event_loop()
except RuntimeError as exc:
if 'no current event loop' not in str(exc):
raise
loop = _loop
task = asyncio.ensure_future(coro, loop=loop)
try:
loop.run_until_complete(task)
except BaseException:
# run_until_complete doesn't get the result from exceptions
# that are not subclasses of `Exception`. Consume all
Expand All @@ -154,9 +164,11 @@ def inner(**kwargs):


def pytest_runtest_setup(item):
if 'asyncio' in item.keywords and 'event_loop' not in item.fixturenames:
if 'asyncio' in item.keywords:
# inject an event loop fixture for all async tests
item.fixturenames.append('event_loop')
if 'event_loop' in item.fixturenames:
item.fixturenames.remove('event_loop')
item.fixturenames.insert(0, 'event_loop')
if item.get_closest_marker("asyncio") is not None \
and not getattr(item.obj, 'hypothesis', False) \
and getattr(item.obj, 'is_hypothesis_test', False):
Expand Down
23 changes: 23 additions & 0 deletions tests/test_simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,29 @@ async def test_asyncio_marker_without_loop(self, remove_loop):
assert ret == 'ok'


class TestEventLoopStartedBeforeFixtures:
@pytest.fixture
async def loop(self):
return asyncio.get_event_loop()

@staticmethod
def foo():
return 1

@pytest.mark.asyncio
async def test_no_event_loop(self, loop):
assert await loop.run_in_executor(None, self.foo) == 1

@pytest.mark.asyncio
async def test_event_loop_after_fixture(self, loop, event_loop):
assert await loop.run_in_executor(None, self.foo) == 1

@pytest.mark.asyncio
async def test_event_loop_before_fixture(self, event_loop, loop):
assert await loop.run_in_executor(None, self.foo) == 1



@pytest.mark.asyncio
async def test_no_warning_on_skip():
pytest.skip("Test a skip error inside asyncio")

0 comments on commit 4d227ea

Please sign in to comment.