From 0ec7ce4472f582d8d5f91ef74400106f1d6721dc Mon Sep 17 00:00:00 2001 From: Bryan Van Deusen Date: Tue, 6 Oct 2026 13:48:50 -0400 Subject: [PATCH] test(alerts): wait for the alert's tasks and the run, not every task (#5063) CI 8343: the wait-for-all-tasks loop also waited on the executor's 5-minute run-output cleanup task (_schedule_cleanup) and hit its 120s deadline. Wait on alerts._background, then poll the run to a final status. Co-Authored-By: Claude Opus 5.5 --- .../integration/test_alert_ansible_action.py | 20 ++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/tests/integration/test_alert_ansible_action.py b/tests/integration/test_alert_ansible_action.py index e41db95..0658414 100644 --- a/tests/integration/test_alert_ansible_action.py +++ b/tests/integration/test_alert_ansible_action.py @@ -83,13 +83,23 @@ def test_firing_triggers_ansible_action(app, tmp_path): async with s.begin(): await record_metric(s, "host_agent", "srv1", "cpu_pct", 95.0) - # Let the dispatch, the action and the run it starts finish. The run's - # task is created by the action's task, so wait until none are left. + # Let the notification and the action finish, then the run the action + # started. Not "every task": a finished run leaves a cleanup task that + # sleeps for minutes (executor._schedule_cleanup). + from steward.core import alerts loop = asyncio.get_running_loop() deadline = loop.time() + 120 - while pending := [t for t in asyncio.all_tasks() if t is not asyncio.current_task()]: - assert loop.time() < deadline, f"tasks still running: {pending}" - await asyncio.wait(pending, timeout=deadline - loop.time()) + while alerts._background: + assert loop.time() < deadline, "the alert's tasks did not finish" + await asyncio.wait(set(alerts._background), timeout=deadline - loop.time()) + while True: + async with app.db_sessionmaker() as s: + status = (await s.execute( + select(AnsibleRun.status).where(AnsibleRun.source_name == "t"))).scalar() + if status not in (None, AnsibleRunStatus.queued, AnsibleRunStatus.running): + break + assert loop.time() < deadline, f"the run is still {status}" + await asyncio.sleep(0.5) async with app.db_sessionmaker() as s: run = (await s.execute(