Skip to content

The agent that grows with you

test(tests): cover cron fallback delivery idempotency and interrupt skip

fangliquanflqdb696c798a6e
diff --git a/tests/cron/test_run_one_job.py b/tests/cron/test_run_one_job.py
index d63fe0c9350..190d6049c8d 100644
--- a/tests/cron/test_run_one_job.py
+++ b/tests/cron/test_run_one_job.py
@@ -10,6 +10,8 @@ The first test characterizes the sequence as driven through `tick()` (proving
the extraction didn't change `tick`'s behavior); the rest unit-test the
extracted helper directly.
"""
+import pytest
+
import cron.scheduler as s
@@ -159,6 +161,96 @@ def test_run_one_job_exception_records_failure_alert_delivery_error(monkeypatch)
]
+def test_run_one_job_exception_after_delivery_does_not_redeliver(monkeypatch):
+ """Once delivery has been attempted, the outer handler must not send again."""
+ delivered = []
+ mark_calls = []
+
+ monkeypatch.setattr(
+ s, "create_execution", lambda *_a, **_kw: {"id": "exec-j5"}
+ )
+ monkeypatch.setattr(s, "claim_dispatch", lambda _job_id: True)
+ monkeypatch.setattr(s, "mark_execution_running", lambda _execution_id: None)
+ monkeypatch.setattr(
+ s,
+ "run_job",
+ lambda *_a, **_kw: (True, "out", "final response", None),
+ )
+ monkeypatch.setattr(s, "save_job_output", lambda jid, out: f"/tmp/{jid}.txt")
+ monkeypatch.setattr(
+ s,
+ "_deliver_result",
+ lambda job, content, **_kw: delivered.append((job["id"], content)) or None,
+ )
+
+ def fake_mark(*args, **kwargs):
+ mark_calls.append((args, kwargs))
+ if len(mark_calls) == 1:
+ raise RuntimeError("bookkeeping boom")
+
+ monkeypatch.setattr(s, "mark_job_run", fake_mark)
+ monkeypatch.setattr(s, "finish_execution", lambda *_a, **_kw: None)
+
+ ok = s.run_one_job({"id": "j5", "name": "once", "deliver": "telegram"})
+
+ assert ok is False
+ assert delivered == [("j5", "final response")]
+ assert mark_calls[0] == (("j5", True, None), {"delivery_error": None})
+ assert mark_calls[1] == (
+ ("j5", False, "bookkeeping boom"),
+ {"delivery_error": None},
+ )
+
+
+def test_run_one_job_keyboard_interrupt_skips_delivery_and_reraises(monkeypatch):
+ """Hard interrupts must not attempt failure delivery; they re-raise."""
+ delivered = []
+ marked = []
+ finished = []
+
+ monkeypatch.setattr(
+ s, "create_execution", lambda *_a, **_kw: {"id": "exec-j6"}
+ )
+ monkeypatch.setattr(s, "claim_dispatch", lambda _job_id: True)
+ monkeypatch.setattr(s, "mark_execution_running", lambda _execution_id: None)
+ monkeypatch.setattr(
+ s,
+ "run_job",
+ lambda *_a, **_kw: (_ for _ in ()).throw(KeyboardInterrupt()),
+ )
+ monkeypatch.setattr(
+ s,
+ "_deliver_result",
+ lambda job, content, **_kw: delivered.append((job["id"], content)) or None,
+ )
+ monkeypatch.setattr(
+ s,
+ "mark_job_run",
+ lambda *args, **kwargs: marked.append((args, kwargs)),
+ )
+ monkeypatch.setattr(
+ s,
+ "finish_execution",
+ lambda *args, **kwargs: finished.append((args, kwargs)),
+ )
+
+ with pytest.raises(KeyboardInterrupt):
+ s.run_one_job({"id": "j6", "name": "interrupt", "deliver": "telegram"})
+
+ assert delivered == []
+ assert marked == [(("j6", False, "KeyboardInterrupt"), {})]
+ assert finished == [
+ (
+ ("exec-j6",),
+ {
+ "success": False,
+ "error": "KeyboardInterrupt",
+ "delivery_outcome": "suppressed",
+ },
+ )
+ ]
+
+
def test_run_one_job_installs_secret_scope_under_multiplex(monkeypatch, tmp_path):
"""Regression: under profile isolation (multiplex active), run_one_job must
execute run_job inside a profile secret scope so credential reads