-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parents.py
More file actions
497 lines (383 loc) · 15.6 KB
/
Copy pathtest_parents.py
File metadata and controls
497 lines (383 loc) · 15.6 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
"""Tests for parent/child task nesting.
Tasks nest a single level deep. A task created while another one is running is
attached to it via `parent`; if the running task is itself a child, the new task
is attached to the same root rather than to the child.
"""
import copy
import logging
from unittest import mock
import celery
import procrastinate
import pytest
from procrastinate import testing
# imported as a module, not by name: `test_celery_system_integration` drops
# `taskbadger.celery` from `sys.modules` and lets it be re-imported, so anything
# bound at import time can end up referring to a stale module object that
# `mock.patch` no longer reaches
import taskbadger.celery
from taskbadger import StatusEnum, track
from taskbadger._current_task import current_task_id, enter_task, exit_task, parent_id
from taskbadger.procrastinate import _instrument_task
from taskbadger.sdk import Task, create_task, list_tasks, update_task
from tests.utils import task_for_test
@pytest.fixture(autouse=True)
def _check_log_errors(caplog):
yield
errors = [r.getMessage() for r in caplog.get_records("call") if r.levelno == logging.ERROR]
if errors:
pytest.fail(f"log errors during tests: {errors}")
@pytest.fixture
def app():
app = procrastinate.App(connector=testing.InMemoryConnector())
with app.open():
yield app
def _json_task_response(**kwargs):
response = {
"id": "test_id",
"organization": "org",
"project": "proj",
"name": "demo task",
"status": "pending",
"value": None,
"value_max": 100,
"value_percent": None,
"data": None,
"created": "2022-09-22T06:53:40.683555Z",
"updated": "2022-09-22T06:53:40.683555Z",
"url": None,
"public_url": None,
"tags": {},
}
response.update(kwargs)
return response
# --- the ambient current-task record -----------------------------------------
def test_no_parent_outside_a_tracked_task():
assert current_task_id() is None
assert parent_id() is None
def test_tasks_created_while_a_root_task_runs_nest_under_it():
token = enter_task("root")
try:
assert current_task_id() == "root"
assert parent_id() == "root"
finally:
exit_task(token)
assert current_task_id() is None
assert parent_id() is None
def test_grandchildren_are_flattened_onto_the_root():
root = enter_task("root")
child = enter_task("child", parent="root")
try:
# the child is what's running, but nesting stops at one level so anything
# it creates joins it under the root instead of hanging off it
assert current_task_id() == "child"
assert parent_id() == "root"
finally:
exit_task(child)
exit_task(root)
# --- the SDK surface ----------------------------------------------------------
@pytest.mark.usefixtures("_bind_settings")
def test_create_task_sends_parent(httpx_mock):
httpx_mock.add_response(
url="https://taskbadger.net/api/org/proj/tasks/",
method="POST",
match_json={"name": "child", "status": "pending", "parent": "parent_id"},
json=_json_task_response(parent="parent_id"),
status_code=201,
)
task = create_task("child", parent="parent_id")
assert task.parent == "parent_id"
@pytest.mark.usefixtures("_bind_settings")
def test_task_create_sends_parent(httpx_mock):
httpx_mock.add_response(
url="https://taskbadger.net/api/org/proj/tasks/",
method="POST",
match_json={"name": "child", "status": "pending", "parent": "parent_id"},
json=_json_task_response(parent="parent_id"),
status_code=201,
)
assert Task.create("child", parent="parent_id").parent == "parent_id"
@pytest.mark.usefixtures("_bind_settings")
def test_update_task_sends_parent(httpx_mock):
"""The API allows setting a parent on a task that doesn't have one yet."""
httpx_mock.add_response(
url="https://taskbadger.net/api/org/proj/tasks/test_id/",
method="PATCH",
match_json={"parent": "parent_id"},
json=_json_task_response(parent="parent_id"),
status_code=200,
)
assert update_task("test_id", parent="parent_id").parent == "parent_id"
@pytest.mark.usefixtures("_bind_settings")
def test_list_tasks_filters_by_parent(httpx_mock):
httpx_mock.add_response(
url="https://taskbadger.net/api/org/proj/tasks/?parent=parent_id",
method="GET",
json={"next": None, "previous": None, "results": [_json_task_response(parent="parent_id")]},
status_code=200,
)
tasks = list_tasks(parent="parent_id")
(child,) = tasks.results
assert isinstance(child, Task)
assert child.parent == "parent_id"
assert list(tasks) == tasks.results
assert len(tasks) == 1
# a TaskList must survive copy / pickle: both probe for dunders that
# `__getattr__` must not try to delegate
assert len(copy.deepcopy(tasks)) == 1
@pytest.mark.usefixtures("_bind_settings")
def test_create_task_omits_parent_when_not_given(httpx_mock):
"""A bare `create_task` stays top-level even inside a running task — only the
integrations nest automatically."""
httpx_mock.add_response(
url="https://taskbadger.net/api/org/proj/tasks/",
method="POST",
match_json={"name": "solo", "status": "pending"},
json=_json_task_response(),
status_code=201,
)
token = enter_task("root")
try:
create_task("solo")
finally:
exit_task(token)
# --- the @track decorator -----------------------------------------------------
@mock.patch("taskbadger.decorators._update_safe")
@mock.patch("taskbadger.decorators.create_task_safe")
def test_track_makes_its_task_current(create, update):
create.return_value = task_for_test(id="outer_id")
seen = {}
@track
def outer():
seen["parent"] = parent_id()
outer()
assert "parent" not in create.call_args.kwargs
assert seen["parent"] == "outer_id"
@mock.patch("taskbadger.decorators._update_safe")
@mock.patch("taskbadger.decorators.create_task_safe")
def test_track_nests_under_an_enclosing_tracked_task(create, update):
create.side_effect = [task_for_test(id="outer_id"), task_for_test(id="inner_id", parent="outer_id")]
@track
def inner():
pass
@track
def outer():
inner()
outer()
assert create.call_args_list[1].kwargs["parent"] == "outer_id"
@mock.patch("taskbadger.decorators._update_safe")
@mock.patch("taskbadger.decorators.create_task_safe")
def test_track_explicit_parent_wins(create, update):
create.return_value = task_for_test(id="outer_id")
@track(parent="chosen")
def inner():
pass
token = enter_task("root")
try:
inner()
finally:
exit_task(token)
assert create.call_args.kwargs["parent"] == "chosen"
# --- Celery -------------------------------------------------------------------
def _publish(name="child.task", **headers):
headers = {"id": "abc123", "task": name, "taskbadger_track": True, **headers}
taskbadger.celery.task_publish_handler(sender=name, headers=headers, body=[[], {}, {}])
@pytest.mark.usefixtures("_bind_settings")
def test_celery_publish_nests_under_the_running_task():
with mock.patch("taskbadger.celery.create_task_safe") as create:
create.return_value = task_for_test()
token = enter_task("root_id")
try:
_publish()
finally:
exit_task(token)
assert create.call_args.kwargs["parent"] == "root_id"
@pytest.mark.usefixtures("_bind_settings")
def test_celery_publish_flattens_grandchildren():
with mock.patch("taskbadger.celery.create_task_safe") as create:
create.return_value = task_for_test()
root = enter_task("root_id")
child = enter_task("child_id", parent="root_id")
try:
_publish()
finally:
exit_task(child)
exit_task(root)
assert create.call_args.kwargs["parent"] == "root_id"
@pytest.mark.usefixtures("_bind_settings")
def test_celery_publish_has_no_parent_outside_a_task():
with mock.patch("taskbadger.celery.create_task_safe") as create:
create.return_value = task_for_test()
_publish()
assert "parent" not in create.call_args.kwargs
def _running_task(chain=None, callbacks=None):
"""Pretend a task with these successors is mid-run."""
request = mock.Mock(chain=chain, callbacks=callbacks)
return mock.patch("celery.current_task", mock.Mock(request=request))
@pytest.mark.usefixtures("_bind_settings")
@pytest.mark.parametrize(
"successors",
[
{"chain": [{"task": "child.task"}]},
{"callbacks": [{"task": "child.task"}]},
# a group of callbacks arrives as one signature wrapping the real tasks
{"callbacks": [{"task": "celery.group", "kwargs": {"tasks": [{"task": "child.task"}]}}]},
],
ids=["chain", "callback", "callback_group"],
)
def test_celery_workflow_successors_are_not_nested(successors):
"""Chain links and callbacks are dispatched while the task that precedes
them still looks current, but they follow it rather than belong to it."""
with mock.patch("taskbadger.celery.create_task_safe") as create, _running_task(**successors):
create.return_value = task_for_test()
token = enter_task("root_id")
try:
_publish()
finally:
exit_task(token)
assert "parent" not in create.call_args.kwargs
@pytest.mark.usefixtures("_bind_settings")
def test_celery_publish_from_a_task_body_still_nests_while_a_chain_is_pending():
"""Only the successor itself is exempt — a task the body enqueues is not."""
with mock.patch("taskbadger.celery.create_task_safe") as create, _running_task(chain=[{"task": "next.task"}]):
create.return_value = task_for_test()
token = enter_task("root_id")
try:
_publish()
finally:
exit_task(token)
assert create.call_args.kwargs["parent"] == "root_id"
@pytest.mark.usefixtures("_bind_settings")
def test_celery_publish_explicit_parent_wins():
with mock.patch("taskbadger.celery.create_task_safe") as create:
create.return_value = task_for_test()
token = enter_task("root_id")
try:
_publish(**{taskbadger.celery.TB_KWARGS_ARG: {"parent": "chosen"}})
finally:
exit_task(token)
assert create.call_args.kwargs["parent"] == "chosen"
def _celery_app(**conf):
"""A standalone app backed by in-memory transports, so no broker is needed."""
app = celery.Celery("test_parents", broker="memory://", backend="cache+memory://", **conf)
@app.task(bind=True, base=taskbadger.celery.Task, name="test_parents.add")
def add(self, a, b):
return a + b
return add
@pytest.mark.usefixtures("_bind_settings")
def test_celery_apply_async_parent():
"""`taskbadger_parent` on `apply_async` reaches the task created at publish time."""
add = _celery_app()
with (
mock.patch("taskbadger.celery.create_task_safe") as create,
mock.patch("taskbadger.sdk.get_task"),
):
create.return_value = task_for_test()
add.apply_async((2, 2), taskbadger_parent="chosen")
assert create.call_args.kwargs["parent"] == "chosen"
@pytest.mark.usefixtures("_bind_settings")
def test_celery_apply_async_parent_beats_the_running_task():
add = _celery_app()
with (
mock.patch("taskbadger.celery.create_task_safe") as create,
mock.patch("taskbadger.sdk.get_task"),
):
create.return_value = task_for_test()
token = enter_task("root_id")
try:
add.apply_async((2, 2), taskbadger_parent="chosen")
finally:
exit_task(token)
assert create.call_args.kwargs["parent"] == "chosen"
@pytest.mark.usefixtures("_bind_settings")
def test_celery_eager_apply_async_parent():
"""Eager tasks are created in `task_prerun` rather than at publish time, but
the explicit parent still has to make it through."""
add = _celery_app(task_always_eager=True, task_eager_propagates=True)
with (
mock.patch("taskbadger.celery.create_task_safe") as create,
mock.patch("taskbadger.celery.update_task_safe"),
mock.patch("taskbadger.sdk.get_task"),
):
create.return_value = task_for_test()
assert add.apply_async((2, 2), taskbadger_parent="chosen").get() == 4
assert create.call_args.kwargs["parent"] == "chosen"
@pytest.mark.usefixtures("_bind_settings")
def test_celery_eager_nests_under_the_running_task():
add = _celery_app(task_always_eager=True, task_eager_propagates=True)
with (
mock.patch("taskbadger.celery.create_task_safe") as create,
mock.patch("taskbadger.celery.update_task_safe"),
mock.patch("taskbadger.sdk.get_task"),
):
create.return_value = task_for_test()
token = enter_task("root_id")
try:
add.apply_async((2, 2))
finally:
exit_task(token)
assert create.call_args.kwargs["parent"] == "root_id"
@pytest.mark.usefixtures("_bind_settings")
def test_celery_eager_explicit_none_parent_makes_a_root_task():
"""`taskbadger_parent=None` asks for a root task, as it does at publish time."""
add = _celery_app(task_always_eager=True, task_eager_propagates=True)
with (
mock.patch("taskbadger.celery.create_task_safe") as create,
mock.patch("taskbadger.celery.update_task_safe"),
mock.patch("taskbadger.sdk.get_task"),
):
create.return_value = task_for_test()
token = enter_task("root_id")
try:
add.apply_async((2, 2), taskbadger_parent=None)
finally:
exit_task(token)
assert create.call_args.kwargs["parent"] is None
# --- Procrastinate ------------------------------------------------------------
@pytest.mark.usefixtures("_bind_settings")
def test_procrastinate_defer_nests_under_the_running_task(app):
@app.task(name="child")
def child(x):
return x
_instrument_task(child, system=None, manual=True)
with (
mock.patch("taskbadger.procrastinate.create_task_safe") as create,
mock.patch("taskbadger.procrastinate.update_task_safe"),
):
create.return_value = task_for_test(id="child_tb")
token = enter_task("root_id")
try:
child.defer(x=1)
finally:
exit_task(token)
assert create.call_args.kwargs["parent"] == "root_id"
@pytest.mark.usefixtures("_bind_settings")
def test_procrastinate_defer_has_no_parent_outside_a_task(app):
@app.task(name="solo")
def solo(x):
return x
_instrument_task(solo, system=None, manual=True)
with (
mock.patch("taskbadger.procrastinate.create_task_safe") as create,
mock.patch("taskbadger.procrastinate.update_task_safe"),
):
create.return_value = task_for_test(id="solo_tb")
solo.defer(x=1)
assert "parent" not in create.call_args.kwargs
@pytest.mark.usefixtures("_bind_settings")
def test_procrastinate_worker_makes_its_task_current(app):
"""The worker side marks the running job's task as current so anything it
defers nests under it."""
seen = {}
@app.task(name="records_parent")
def records_parent():
seen["parent"] = parent_id()
_instrument_task(records_parent, system=None, manual=True)
with (
mock.patch("taskbadger.procrastinate.update_task_safe") as update,
mock.patch("taskbadger.sdk.get_task") as get,
):
update.return_value = task_for_test(id="job_tb", status=StatusEnum.PROCESSING)
get.return_value = update.return_value
records_parent.func(**{"__taskbadger_task_id__": "job_tb"})
assert seen["parent"] == "job_tb"
assert current_task_id() is None