import asyncio from tests.test_web import app # noqa: F401 async def test_lifespan_starts_and_cancels_listener_tasks(app): runtime = app.state.runtime before = {t for t in asyncio.all_tasks()} async with runtime.lifespan(app): await asyncio.sleep(0.05) started = {t for t in asyncio.all_tasks()} - before assert len(started) >= 4 await asyncio.sleep(0) leftover = [t for t in asyncio.all_tasks() - before if not t.done()] assert leftover == []