def loop( request, services, loop_debug, default_context, entrypoint_kwargs, thread_pool_size, thread_pool_executor, loop: asyncio.AbstractEventLoop, caplog: pytest.LogCaptureFixture, ): from aiomisc.context import get_context from aiomisc.entrypoint import entrypoint if LOG_LEVEL: LOG_LEVEL.set(logging.getLogger().getEffectiveLevel()) pool = thread_pool_executor(thread_pool_size) loop.set_default_executor(pool) get_marker = request.node.get_closest_marker forbid_loop_getter_marker = get_marker("forbid_get_event_loop") catch_unhandled_marker = get_marker("catch_loop_exceptions") exceptions = list() if catch_unhandled_marker: loop.set_exception_handler(lambda l, c: exceptions.append(c)) try: with entrypoint(*services, loop=loop, **entrypoint_kwargs): ctx = get_context(loop) for key, value in default_context.items(): ctx[key] = value if forbid_loop_getter_marker: asyncio.get_event_loop.side_effect = partial( pytest.fail, "get_event_loop is forbidden", ) yield loop if exceptions: logging.error( "Unhandled exceptions found:\n\n\t%s", "\n\t".join(("Message: {m}\n\t" "Future: {f}\n\t" "Exception: {e}").format( m=e["message"], f=repr(e.get("future")), e=repr(e.get("exception")), ) for e in exceptions), ) pytest.fail("Unhandled exceptions found. See logs.") finally: asyncio.get_event_loop.side_effect = get_event_loop del loop
def loop(request, services, loop_debug, default_context, entrypoint_kwargs, thread_pool_size, thread_pool_executor, loop): from aiomisc.context import get_context from aiomisc.entrypoint import entrypoint asyncio.set_event_loop(loop) pool = thread_pool_executor(thread_pool_size) loop.set_default_executor(pool) get_marker = request.node.get_closest_marker forbid_loop_getter_marker = get_marker('forbid_get_event_loop') catch_unhandled_marker = get_marker('catch_loop_exceptions') exceptions = list() if catch_unhandled_marker: loop.set_exception_handler(lambda l, c: exceptions.append(c)) try: with entrypoint(*services, pool_size=thread_pool_size, debug=loop_debug, loop=loop, **entrypoint_kwargs): ctx = get_context(loop) for key, value in default_context.items(): ctx[key] = value if forbid_loop_getter_marker: asyncio.get_event_loop.side_effect = partial( pytest.fail, "get_event_loop is forbidden") yield loop if exceptions: logging.error( 'Unhandled exceptions found:\n\n\t%s', "\n\t".join( ("Message: {m}\n\t" "Future: {f}\n\t" "Exception: {e}").format(m=e['message'], f=repr(e.get('future')), e=repr(e.get('exception'))) for e in exceptions)) pytest.fail("Unhandled exceptions found. See logs.") finally: with suppress(Exception): pool.shutdown(True) asyncio.get_event_loop.side_effect = get_event_loop del loop
def context(self) -> Context: if self.__context is None: self.__context = get_context() return self.__context