From be6c133657cdca7c1d4b66bcdfe405dad8316c44 Mon Sep 17 00:00:00 2001 From: Vlada Dusek Date: Tue, 14 Jul 2026 16:21:18 +0200 Subject: [PATCH] test: fix flaky test_runs_concurrently by asserting peak concurrency instead of wall time --- tests/unit/_autoscaling/test_autoscaled_pool.py | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/tests/unit/_autoscaling/test_autoscaled_pool.py b/tests/unit/_autoscaling/test_autoscaled_pool.py index 5af4e4b8e7..bd543b287e 100644 --- a/tests/unit/_autoscaling/test_autoscaled_pool.py +++ b/tests/unit/_autoscaling/test_autoscaled_pool.py @@ -37,10 +37,15 @@ def future(value: T, /) -> Awaitable[T]: @pytest.mark.run_alone async def test_runs_concurrently(system_status: SystemStatus | Mock) -> None: done_count = 0 + running_count = 0 + max_running_count = 0 async def run() -> None: + nonlocal done_count, running_count, max_running_count + running_count += 1 + max_running_count = max(max_running_count, running_count) await asyncio.sleep(0.1) - nonlocal done_count + running_count -= 1 done_count += 1 pool = AutoscaledPool( @@ -54,12 +59,12 @@ async def run() -> None: ), ) - with measure_time() as elapsed: - await pool.run() - - assert elapsed.wall is not None - assert elapsed.wall < 0.3 + await pool.run() + # Assert the tasks actually ran concurrently by checking the peak number that overlapped in time. + # A sequential (or only partially concurrent) pool would peak below the configured concurrency. + # Previously this was inferred from the total wall time, which was flaky under load on slow CI runners. + assert max_running_count == 10 assert done_count >= 10