From 1937ebeb5a9d8b74c966f1e69f71488974d099ef Mon Sep 17 00:00:00 2001 From: jxxghp Date: Fri, 28 Aug 2026 07:11:01 +0800 Subject: [PATCH] test(lifecycle): scope shutdown budget assertion --- tests/test_lifecycle_shutdown.py | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/test_lifecycle_shutdown.py b/tests/test_lifecycle_shutdown.py index 133c5c4fc..47e9d00e0 100644 --- a/tests/test_lifecycle_shutdown.py +++ b/tests/test_lifecycle_shutdown.py @@ -396,13 +396,20 @@ def test_plugin_settlement_cannot_bypass_task_registry_shutdown_budget( lifecycle.init_extra.side_effect = settle_plugins async def run_lifespan() -> None: - """确认 context 能由 TaskRegistry 的失败结果立即结束。""" - async with lifecycle.lifespan(FastAPI()): + """仅对关闭阶段计时,避免覆盖率启动开销污染停机预算断言。""" + lifespan_context = lifecycle.lifespan(FastAPI()) + await lifespan_context.__aenter__() + try: await started.wait() - release.set() - await asyncio.sleep(0) + await asyncio.wait_for( + lifespan_context.__aexit__(None, None, None), + timeout=0.5, + ) + finally: + release.set() + await asyncio.sleep(0) - asyncio.run(asyncio.wait_for(run_lifespan(), timeout=0.5)) + asyncio.run(run_lifespan()) shutdown.assert_awaited_once_with(timeout_seconds=30.0) for name, step in shutdown_steps.items():