You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi. Sometimes async tests hangs when background tasks raises some exception but it's not propagated to test function, it's not a bug in pytest-asyncio but a problem in application code. Can the following patch be used to abort current test if some exception is raised in background?
importinspectas_inspectimportasyncioas_asynciofromfunctoolsimportwrapsas_wrapsfrompytest_asyncioimportpluginas_pytestasyncio_plugindef_wrap_in_sync(func, _loop):
"""Return a sync wrapper around an async function executing it in the current event loop."""@_wraps(func)definner(**kwargs):
coro=func(**kwargs)
ifcoroisnotNone:
defforward_exception(loop, context):
if_inspect.getcoroutinestate(coro) !=_inspect.CORO_CLOSED:
coro.throw(context['exception'] if'exception'incontextelseException(context['message']))
else:
loop.default_exception_handler(context)
_loop.set_exception_handler(forward_exception)
task=_asyncio.ensure_future(coro, loop=_loop)
try:
_loop.run_until_complete(task)
exceptBaseException:
# run_until_complete doesn't get the result from exceptions# that are not subclasses of `Exception`. Consume all# exceptions to prevent asyncio's warning from logging.iftask.done() andnottask.cancelled():
task.exception()
raisereturninner_pytestasyncio_plugin.wrap_in_sync=_wrap_in_sync
The text was updated successfully, but these errors were encountered:
Hi. Sometimes async tests hangs when background tasks raises some exception but it's not propagated to test function, it's not a bug in pytest-asyncio but a problem in application code. Can the following patch be used to abort current test if some exception is raised in background?
The text was updated successfully, but these errors were encountered: