| 1 |
import uuid |
| 2 |
|
| 3 |
import pytest |
| 4 |
from pytest_celery import RESULT_TIMEOUT, CeleryTestSetup |
| 5 |
|
| 6 |
from celery.canvas import chain, chord, group, signature |
| 7 |
from t.integration.conftest import get_redis_connection |
| 8 |
from t.integration.tasks import ExpectedException, add, fail, identity, redis_echo |
| 9 |
|
| 10 |
|
| 11 |
class test_signature: |
| 12 |
def test_sanity(self, celery_setup: CeleryTestSetup): |
| 13 |
sig = signature(identity, args=("test_signature",), queue=celery_setup.worker.worker_queue) |
| 14 |
assert sig.delay().get(timeout=RESULT_TIMEOUT) == "test_signature" |
| 15 |
|
| 16 |
|
| 17 |
class test_group: |
| 18 |
def test_sanity(self, celery_setup: CeleryTestSetup): |
| 19 |
sig = group( |
| 20 |
group(add.si(1, 1), add.si(2, 2)), |
| 21 |
group([add.si(1, 1), add.si(2, 2)]), |
| 22 |
group(s for s in [add.si(1, 1), add.si(2, 2)]), |
| 23 |
) |
| 24 |
res = sig.apply_async(queue=celery_setup.worker.worker_queue) |
| 25 |
assert res.get(timeout=RESULT_TIMEOUT) == [2, 4, 2, 4, 2, 4] |
| 26 |
|
| 27 |
|
| 28 |
class test_chain: |
| 29 |
def test_sanity(self, celery_setup: CeleryTestSetup): |
| 30 |
queue = celery_setup.worker.worker_queue |
| 31 |
sig = chain( |
| 32 |
identity.si("chain_task1").set(queue=queue), |
| 33 |
identity.si("chain_task2").set(queue=queue), |
| 34 |
) | identity.si("test_chain").set(queue=queue) |
| 35 |
res = sig.apply_async() |
| 36 |
assert res.get(timeout=RESULT_TIMEOUT) == "test_chain" |
| 37 |
|
| 38 |
def test_chain_gets_last_task_id_with_failing_tasks_in_chain(self, celery_setup: CeleryTestSetup): |
| 39 |
"""https://github.com/celery/celery/issues/8786""" |
| 40 |
queue = celery_setup.worker.worker_queue |
| 41 |
sig = chain( |
| 42 |
identity.si("start").set(queue=queue), |
| 43 |
group( |
| 44 |
identity.si("a").set(queue=queue), |
| 45 |
fail.si().set(queue=queue), |
| 46 |
), |
| 47 |
identity.si("break").set(queue=queue), |
| 48 |
identity.si("end").set(queue=queue), |
| 49 |
) |
| 50 |
res = sig.apply_async() |
| 51 |
celery_setup.worker.assert_log_does_not_exist("ValueError: task_id must not be empty. Got None instead.") |
| 52 |
|
| 53 |
with pytest.raises(ExpectedException): |
| 54 |
res.get(timeout=RESULT_TIMEOUT) |
| 55 |
|
| 56 |
def test_upgrade_to_chord_inside_chains(self, celery_setup: CeleryTestSetup): |
| 57 |
redis_key = str(uuid.uuid4()) |
| 58 |
queue = celery_setup.worker.worker_queue |
| 59 |
group1 = group(redis_echo.si("a", redis_key), redis_echo.si("a", redis_key)) |
| 60 |
group2 = group(redis_echo.si("a", redis_key), redis_echo.si("a", redis_key)) |
| 61 |
chord1 = group1 | group2 |
| 62 |
chain1 = chain(chord1, (redis_echo.si("a", redis_key) | redis_echo.si("b", redis_key).set(queue=queue))) |
| 63 |
chain1.apply_async(queue=queue).get(timeout=RESULT_TIMEOUT) |
| 64 |
redis_connection = get_redis_connection() |
| 65 |
actual = redis_connection.lrange(redis_key, 0, -1) |
| 66 |
assert actual.count(b"a") == 5 |
| 67 |
assert actual.count(b"b") == 1 |
| 68 |
redis_connection.delete(redis_key) |
| 69 |
|
| 70 |
|
| 71 |
class test_chord: |
| 72 |
def test_sanity(self, celery_setup: CeleryTestSetup): |
| 73 |
upgraded_chord = signature( |
| 74 |
group( |
| 75 |
identity.si("header_task1"), |
| 76 |
identity.si("header_task2"), |
| 77 |
) |
| 78 |
| identity.si("body_task"), |
| 79 |
queue=celery_setup.worker.worker_queue, |
| 80 |
) |
| 81 |
|
| 82 |
sig = group( |
| 83 |
[ |
| 84 |
upgraded_chord, |
| 85 |
chord( |
| 86 |
group( |
| 87 |
identity.si("header_task3"), |
| 88 |
identity.si("header_task4"), |
| 89 |
), |
| 90 |
identity.si("body_task"), |
| 91 |
), |
| 92 |
chord( |
| 93 |
( |
| 94 |
sig |
| 95 |
for sig in [ |
| 96 |
identity.si("header_task5"), |
| 97 |
identity.si("header_task6"), |
| 98 |
] |
| 99 |
), |
| 100 |
identity.si("body_task"), |
| 101 |
), |
| 102 |
] |
| 103 |
) |
| 104 |
res = sig.apply_async(queue=celery_setup.worker.worker_queue) |
| 105 |
assert res.get(timeout=RESULT_TIMEOUT) == ["body_task"] * 3 |
| 106 |
|
| 107 |
@pytest.mark.parametrize( |
| 108 |
"input_body", |
| 109 |
[ |
| 110 |
(lambda queue: add.si(9, 7).set(queue=queue)), |
| 111 |
( |
| 112 |
lambda queue: chain( |
| 113 |
add.si(9, 7).set(queue=queue), |
| 114 |
add.si(5, 7).set(queue=queue), |
| 115 |
) |
| 116 |
), |
| 117 |
( |
| 118 |
lambda queue: group( |
| 119 |
[ |
| 120 |
add.si(9, 7).set(queue=queue), |
| 121 |
add.si(5, 7).set(queue=queue), |
| 122 |
] |
| 123 |
) |
| 124 |
), |
| 125 |
( |
| 126 |
lambda queue: chord( |
| 127 |
group( |
| 128 |
[ |
| 129 |
add.si(1, 1).set(queue=queue), |
| 130 |
add.si(2, 2).set(queue=queue), |
| 131 |
] |
| 132 |
), |
| 133 |
add.si(10, 10).set(queue=queue), |
| 134 |
) |
| 135 |
), |
| 136 |
], |
| 137 |
ids=[ |
| 138 |
"body is a single_task", |
| 139 |
"body is a chain", |
| 140 |
"body is a group", |
| 141 |
"body is a chord", |
| 142 |
], |
| 143 |
) |
| 144 |
def test_chord_error_propagation_with_different_body_types( |
| 145 |
self, celery_setup: CeleryTestSetup, input_body |
| 146 |
) -> None: |
| 147 |
"""Reproduce issue #9773 with different chord body types. |
| 148 |
|
| 149 |
This test verifies that the "task_id must not be empty" error is fixed |
| 150 |
regardless of the chord body type. The issue occurs when: |
| 151 |
1. A chord has a group with both succeeding and failing tasks |
| 152 |
2. The chord body can be any signature type (single task, chain, group, chord) |
| 153 |
3. When the group task fails, error propagation should work correctly |
| 154 |
|
| 155 |
Args: |
| 156 |
input_body (callable): A callable that returns a Celery signature for the chord body. |
| 157 |
""" |
| 158 |
queue = celery_setup.worker.worker_queue |
| 159 |
|
| 160 |
# Create the failing group header (same for all tests) |
| 161 |
failing_group = group( |
| 162 |
[ |
| 163 |
add.si(15, 7).set(queue=queue), |
| 164 |
# failing task |
| 165 |
fail.si().set(queue=queue), |
| 166 |
] |
| 167 |
) |
| 168 |
|
| 169 |
# Create the chord |
| 170 |
test_chord = chord(failing_group, input_body(queue)) |
| 171 |
|
| 172 |
result = test_chord.apply_async() |
| 173 |
|
| 174 |
# The worker should not log the "task_id must not be empty" error |
| 175 |
celery_setup.worker.assert_log_does_not_exist( |
| 176 |
"ValueError: task_id must not be empty. Got None instead." |
| 177 |
) |
| 178 |
|
| 179 |
# The chord should fail with the expected exception from the failing task |
| 180 |
with pytest.raises(ExpectedException): |
| 181 |
result.get(timeout=RESULT_TIMEOUT) |
| 182 |
|
| 183 |
|
| 184 |
class test_complex_workflow: |
| 185 |
def test_pending_tasks_released_on_forget(self, celery_setup: CeleryTestSetup): |
| 186 |
sig = add.si(1, 1) | group( |
| 187 |
add.s(1) | group(add.si(1, 1), add.si(2, 2)) | add.si(2, 2), |
| 188 |
add.s(1) | group(add.si(1, 1), add.si(2, 2)) | add.si(2, 2) |
| 189 |
) | add.si(1, 1) |
| 190 |
res = sig.apply_async(queue=celery_setup.worker.worker_queue) |
| 191 |
assert not all(len(mapping) == 0 for mapping in res.backend._pending_results) |
| 192 |
res.forget() |
| 193 |
assert all(len(mapping) == 0 for mapping in res.backend._pending_results) |