async def test_lifespan_return() -> None: lifespan = Lifespan(return_app, Config()) lifespan_task = asyncio.ensure_future(lifespan.handle_lifespan()) await lifespan.wait_for_startup() await lifespan.wait_for_shutdown() # Should complete (not hang) assert lifespan_task.done()
async def test_lifespan_return(event_loop: asyncio.AbstractEventLoop) -> None: lifespan = Lifespan(return_app, Config()) lifespan_task = event_loop.create_task(lifespan.handle_lifespan()) await lifespan.wait_for_startup() await lifespan.wait_for_shutdown() # Should complete (not hang) assert lifespan_task.done()
async def test_startup_failure(event_loop: asyncio.AbstractEventLoop) -> None: lifespan = Lifespan(lifespan_failure, Config()) lifespan_task = event_loop.create_task(lifespan.handle_lifespan()) await lifespan.wait_for_startup() assert lifespan_task.done() exception = lifespan_task.exception() assert isinstance(exception, LifespanFailureError) assert str(exception) == "Lifespan failure in startup. 'Failure'"
async def test_ensure_no_race_condition( event_loop: asyncio.AbstractEventLoop) -> None: config = Config() config.startup_timeout = 0.2 lifespan = Lifespan(no_lifespan_app, config) task = event_loop.create_task(lifespan.handle_lifespan()) await lifespan.wait_for_startup() # Raises if there is a race condition await task
async def test_startup_failure() -> None: lifespan = Lifespan(lifespan_failure, Config()) lifespan_task = asyncio.ensure_future(lifespan.handle_lifespan()) await lifespan.wait_for_startup() assert lifespan_task.done() exception = lifespan_task.exception() assert isinstance(exception, LifespanFailure) assert str(exception) == "Lifespan failure in startup. 'Failure'"
async def test_startup_timeout_error() -> None: config = Config() config.startup_timeout = 0.01 lifespan = Lifespan(SlowLifespanFramework(0.02, asyncio.sleep), config) # type: ignore asyncio.ensure_future(lifespan.handle_lifespan()) with pytest.raises(LifespanTimeout) as exc_info: await lifespan.wait_for_startup() assert str(exc_info.value).startswith("Timeout whilst awaiting startup")
async def test_startup_timeout_error( event_loop: asyncio.AbstractEventLoop) -> None: config = Config() config.startup_timeout = 0.01 lifespan = Lifespan(SlowLifespanFramework(0.02, asyncio.sleep), config) # type: ignore task = event_loop.create_task(lifespan.handle_lifespan()) with pytest.raises(LifespanTimeoutError) as exc_info: await lifespan.wait_for_startup() assert str(exc_info.value).startswith("Timeout whilst awaiting startup") await task
async def test_ensure_no_race_condition() -> None: config = Config() config.startup_timeout = 0.2 lifespan = Lifespan(no_lifespan_app, config) asyncio.ensure_future(lifespan.handle_lifespan()) await lifespan.wait_for_startup() # Raises if there is a race condition
async def test_startup_failure() -> None: lifespan = Lifespan(lifespan_failure, Config()) # type: ignore with pytest.raises(LifespanFailure) as exc_info: await lifespan.handle_lifespan() assert str(exc_info.value) == "Lifespan failure in startup. Failure"