Skip to content

Commit c408b7a

Browse files
committed
feat(procrastinate): auto-wrap tasks registered after system init
1 parent 1a206ba commit c408b7a

1 file changed

Lines changed: 38 additions & 1 deletion

File tree

tests/test_procrastinate_system_integration.py

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import pytest
55
from procrastinate import testing
66

7-
from taskbadger.procrastinate import _INSTRUMENTED_ATTR, TB_TASK_ID_KWARG, _task_cache
7+
from taskbadger.procrastinate import _INSTRUMENTED_ATTR, TB_TASK_ID_KWARG, _task_cache, track
88
from taskbadger.systems.procrastinate import ProcrastinateSystemIntegration
99
from tests.utils import task_for_test
1010

@@ -86,3 +86,40 @@ def flush():
8686
flush.defer()
8787

8888
create.assert_not_called()
89+
90+
91+
@pytest.mark.usefixtures("_bind_settings")
92+
def test_wraps_tasks_registered_after_init(app):
93+
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True)
94+
95+
@app.task(name="late")
96+
def late(a):
97+
return a
98+
99+
assert getattr(late, _INSTRUMENTED_ATTR) is True
100+
101+
tb = task_for_test()
102+
with mock.patch("taskbadger.procrastinate.create_task_safe", return_value=tb) as create:
103+
late.defer(a=1)
104+
105+
create.assert_called_once()
106+
107+
108+
@pytest.mark.usefixtures("_bind_settings")
109+
def test_track_plus_auto_track_no_double_wrap(app):
110+
@track
111+
@app.task(name="manual_plus_auto")
112+
def both():
113+
pass
114+
115+
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True)
116+
117+
# _instrument_task is idempotent — system init must not re-wrap.
118+
tb = task_for_test()
119+
with mock.patch("taskbadger.procrastinate.create_task_safe", return_value=tb) as create:
120+
both.defer()
121+
122+
assert create.call_count == 1
123+
jobs = list(app.connector.jobs.values())
124+
args = jobs[0]["args"]
125+
assert list(args).count(TB_TASK_ID_KWARG) == 1

0 commit comments

Comments
 (0)