From 62ccea86a1373361f04e2728efdf63a25151c58b Mon Sep 17 00:00:00 2001 From: Ivana Kellyer Date: Thu, 22 Oct 2026 10:59:59 +0200 Subject: [PATCH] test(arq): Fix flaky cron job tests The cron job tests used fixed schedules together with run_at_startup. arq enqueues a cron job if its next run is within about a second, so when a test started right before a scheduled time, the job ran twice and the tests saw extra check-ins. Schedule the jobs half an hour from now instead. --- tests/integrations/arq/test_arq_crons.py | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/tests/integrations/arq/test_arq_crons.py b/tests/integrations/arq/test_arq_crons.py index 3087e6e594..5f55961ef9 100644 --- a/tests/integrations/arq/test_arq_crons.py +++ b/tests/integrations/arq/test_arq_crons.py @@ -1,5 +1,5 @@ import time -from datetime import timedelta, timezone +from datetime import datetime, timedelta, timezone import arq.worker import pytest @@ -37,6 +37,13 @@ def utc_local_time(monkeypatch): monkeypatch.setattr(time, "daylight", 0) +def _minute_far_from_now(): + # arq enqueues a cron job if its next run is about a second away. Schedule + # it half an hour from now so that it doesn't run again right after the + # startup run. + return (datetime.now(timezone.utc).minute + 30) % 60 + + def _check_ins(envelopes): return [ item.payload.json @@ -76,15 +83,16 @@ async def test_cron_job_ok(sentry_init, capture_envelopes): sentry_init(integrations=[ArqIntegration(monitor_cron_jobs=True)]) envelopes = capture_envelopes() + minute = _minute_far_from_now() await _run_cron_jobs( - cron(ok_job, minute={0, 30}, run_at_startup=True), timezone=timezone.utc + cron(ok_job, minute=minute, run_at_startup=True), timezone=timezone.utc ) in_progress, ok = _check_ins(envelopes) assert in_progress["monitor_slug"] == "ok_job" assert in_progress["status"] == "in_progress" assert in_progress["monitor_config"] == { - "schedule": {"type": "crontab", "value": "0,30 * * * *"}, + "schedule": {"type": "crontab", "value": f"{minute} * * * *"}, "timezone": "UTC", } assert ok["monitor_slug"] == "ok_job" @@ -99,7 +107,13 @@ async def test_cron_job_error(sentry_init, capture_envelopes): envelopes = capture_envelopes() await _run_cron_jobs( - cron(failing_job, name="nightly", hour=3, minute=0, run_at_startup=True), + cron( + failing_job, + name="nightly", + hour=3, + minute=_minute_far_from_now(), + run_at_startup=True, + ), timezone=timezone.utc, )