-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_procrastinate_system_integration.py
More file actions
163 lines (117 loc) · 5.16 KB
/
Copy pathtest_procrastinate_system_integration.py
File metadata and controls
163 lines (117 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import asyncio
from unittest import mock
import procrastinate
import pytest
from procrastinate import testing
from taskbadger.procrastinate import _INSTRUMENTED_ATTR, TB_TASK_ID_KWARG, track
from taskbadger.systems.procrastinate import ProcrastinateSystemIntegration
from tests.utils import task_for_test
@pytest.fixture
def app():
in_memory = testing.InMemoryConnector()
app = procrastinate.App(connector=in_memory)
with app.open():
yield app
@pytest.mark.parametrize(
("include", "exclude", "expected"),
[
(None, None, True),
(["myapp.tasks.export_data"], None, True),
([".*export_data"], [], True),
([".*export_da"], [], False),
(["myapp.tasks.export_data"], ["myapp.tasks.export_data"], False),
([".*"], ["myapp.tasks.export_data"], False),
([".*"], [".*tasks.*"], False),
],
)
def test_task_name_matching(app, include, exclude, expected):
integration = ProcrastinateSystemIntegration(app=app, includes=include, excludes=exclude)
assert integration.track_task("myapp.tasks.export_data") is expected
def test_auto_track_off_returns_false(app):
integration = ProcrastinateSystemIntegration(app=app, auto_track_tasks=False)
assert integration.track_task("anything") is False
def test_wraps_existing_tasks(app):
@app.task(name="pre_existing")
def pre_existing(a):
return a
assert not getattr(pre_existing, _INSTRUMENTED_ATTR, False)
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True)
assert getattr(pre_existing, _INSTRUMENTED_ATTR) is True
@pytest.mark.usefixtures("_bind_settings")
def test_auto_track_creates_pending(app):
@app.task(name="auto_target")
def auto_target(a):
return a
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True)
tb = task_for_test()
with mock.patch("taskbadger.procrastinate.create_task_safe", return_value=tb) as create:
auto_target.defer(a=1)
create.assert_called_once()
# InMemoryConnector.jobs is a dict keyed by int; kwargs under "args"
jobs = list(app.connector.jobs.values())
assert jobs[0]["args"][TB_TASK_ID_KWARG] == tb.id
@pytest.mark.usefixtures("_bind_settings")
def test_auto_track_excludes_skip(app):
@app.task(name="myapp.cleanup.flush")
def flush():
pass
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True, excludes=[r"myapp\.cleanup\..*"])
with mock.patch("taskbadger.procrastinate.create_task_safe") as create:
flush.defer()
create.assert_not_called()
@pytest.mark.usefixtures("_bind_settings")
def test_wraps_tasks_registered_after_init(app):
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True)
@app.task(name="late")
def late(a):
return a
assert getattr(late, _INSTRUMENTED_ATTR) is True
tb = task_for_test()
with mock.patch("taskbadger.procrastinate.create_task_safe", return_value=tb) as create:
late.defer(a=1)
create.assert_called_once()
@pytest.mark.usefixtures("_bind_settings")
def test_periodic_defer_creates_pending(app):
"""Periodic tasks are deferred via ``app.job_manager.defer_periodic_job``,
which bypasses ``task.defer``/``defer_async`` entirely. The system
integration must hook this path too, otherwise periodic jobs are invisible
to TaskBadger."""
@app.task(name="periodic_target")
def periodic_target(timestamp):
return timestamp
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True)
timestamp = 1700000000
job = periodic_target.configure(task_kwargs={"timestamp": timestamp}).job
tb = task_for_test()
with mock.patch("taskbadger.procrastinate.create_task_safe", return_value=tb) as create:
asyncio.run(app.job_manager.defer_periodic_job(job=job, periodic_id="every-min", defer_timestamp=timestamp))
create.assert_called_once()
jobs_stored = list(app.connector.jobs.values())
assert jobs_stored[0]["args"][TB_TASK_ID_KWARG] == tb.id
@pytest.mark.usefixtures("_bind_settings")
def test_periodic_defer_skips_excluded(app):
"""Excludes apply on the periodic path too."""
@app.task(name="myapp.cleanup.flush")
def flush(timestamp):
pass
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True, excludes=[r"myapp\.cleanup\..*"])
timestamp = 1700000000
job = flush.configure(task_kwargs={"timestamp": timestamp}).job
with mock.patch("taskbadger.procrastinate.create_task_safe") as create:
asyncio.run(app.job_manager.defer_periodic_job(job=job, periodic_id="every-min", defer_timestamp=timestamp))
create.assert_not_called()
@pytest.mark.usefixtures("_bind_settings")
def test_track_plus_auto_track_no_double_wrap(app):
@track
@app.task(name="manual_plus_auto")
def both():
pass
ProcrastinateSystemIntegration(app=app, auto_track_tasks=True)
# _instrument_task is idempotent — system init must not re-wrap.
tb = task_for_test()
with mock.patch("taskbadger.procrastinate.create_task_safe", return_value=tb) as create:
both.defer()
assert create.call_count == 1
jobs = list(app.connector.jobs.values())
args = jobs[0]["args"]
assert list(args).count(TB_TASK_ID_KWARG) == 1