|
4 | 4 | import pytest |
5 | 5 | from procrastinate import testing |
6 | 6 |
|
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 |
8 | 8 | from taskbadger.systems.procrastinate import ProcrastinateSystemIntegration |
9 | 9 | from tests.utils import task_for_test |
10 | 10 |
|
@@ -86,3 +86,40 @@ def flush(): |
86 | 86 | flush.defer() |
87 | 87 |
|
88 | 88 | 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