| 1 |
import collections |
| 2 |
import re |
| 3 |
import tempfile |
| 4 |
import uuid |
| 5 |
from datetime import datetime, timedelta, timezone |
| 6 |
from time import monotonic, sleep |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from celery import chain, chord, group, signature |
| 11 |
from celery.backends.base import BaseKeyValueStoreBackend |
| 12 |
from celery.canvas import StampingVisitor |
| 13 |
from celery.exceptions import ImproperlyConfigured, TimeoutError |
| 14 |
from celery.result import AsyncResult, GroupResult, ResultSet |
| 15 |
from celery.signals import before_task_publish, task_received |
| 16 |
|
| 17 |
from . import tasks |
| 18 |
from .conftest import TEST_BACKEND, check_for_logs, flaky, get_active_redis_channels, get_redis_connection |
| 19 |
from .tasks import (ExpectedException, StampOnReplace, add, add_chord_to_chord, add_replaced, add_to_all, |
| 20 |
add_to_all_to_chord, build_chain_inside_task, collect_ids, delayed_sum, |
| 21 |
delayed_sum_with_soft_guard, errback_new_style, errback_old_style, fail, fail_replaced, identity, |
| 22 |
ids, mul, print_unicode, raise_error, redis_count, redis_echo, redis_echo_group_id, |
| 23 |
replace_with_chain, replace_with_chain_which_raises, replace_with_empty_chain, |
| 24 |
replace_with_stamped_task, retry_once, return_exception, return_priority, second_order_replace1, |
| 25 |
tsum, write_to_file_and_return_int, xsum) |
| 26 |
|
| 27 |
TIMEOUT = 60 |
| 28 |
|
| 29 |
|
| 30 |
def await_redis_echo(expected_msgs, redis_key="redis-echo", timeout=TIMEOUT): |
| 31 |
""" |
| 32 |
Helper to wait for a specified or well-known redis key to contain a string. |
| 33 |
""" |
| 34 |
redis_connection = get_redis_connection() |
| 35 |
|
| 36 |
if isinstance(expected_msgs, (str, bytes, bytearray)): |
| 37 |
expected_msgs = (expected_msgs,) |
| 38 |
expected_msgs = collections.Counter( |
| 39 |
e if not isinstance(e, str) else e.encode("utf-8") |
| 40 |
for e in expected_msgs |
| 41 |
) |
| 42 |
|
| 43 |
# This can technically wait for `len(expected_msg_or_msgs) * timeout` :/ |
| 44 |
while +expected_msgs: |
| 45 |
maybe_key_msg = redis_connection.blpop(redis_key, timeout) |
| 46 |
if maybe_key_msg is None: |
| 47 |
raise TimeoutError( |
| 48 |
"Fetching from {!r} timed out - still awaiting {!r}" |
| 49 |
.format(redis_key, dict(+expected_msgs)) |
| 50 |
) |
| 51 |
retrieved_key, msg = maybe_key_msg |
| 52 |
assert retrieved_key.decode("utf-8") == redis_key |
| 53 |
expected_msgs[msg] -= 1 # silently accepts unexpected messages |
| 54 |
|
| 55 |
# There should be no more elements - block momentarily |
| 56 |
assert redis_connection.blpop(redis_key, min(1, timeout)) is None |
| 57 |
|
| 58 |
|
| 59 |
def await_redis_list_message_length(expected_length, redis_key="redis-group-ids", timeout=TIMEOUT): |
| 60 |
""" |
| 61 |
Helper to wait for a specified or well-known redis key to contain a string. |
| 62 |
""" |
| 63 |
sleep(1) |
| 64 |
redis_connection = get_redis_connection() |
| 65 |
|
| 66 |
check_interval = 0.1 |
| 67 |
check_max = int(timeout / check_interval) |
| 68 |
|
| 69 |
for i in range(check_max + 1): |
| 70 |
length = redis_connection.llen(redis_key) |
| 71 |
|
| 72 |
if length == expected_length: |
| 73 |
break |
| 74 |
|
| 75 |
sleep(check_interval) |
| 76 |
else: |
| 77 |
raise TimeoutError(f'{redis_key!r} has length of {length}, but expected to be of length {expected_length}') |
| 78 |
|
| 79 |
sleep(min(1, timeout)) |
| 80 |
assert redis_connection.llen(redis_key) == expected_length |
| 81 |
|
| 82 |
|
| 83 |
def await_redis_count(expected_count, redis_key="redis-count", timeout=TIMEOUT): |
| 84 |
""" |
| 85 |
Helper to wait for a specified or well-known redis key to count to a value. |
| 86 |
""" |
| 87 |
redis_connection = get_redis_connection() |
| 88 |
|
| 89 |
check_interval = 0.1 |
| 90 |
check_max = int(timeout / check_interval) |
| 91 |
for i in range(check_max + 1): |
| 92 |
maybe_count = redis_connection.get(redis_key) |
| 93 |
# It's either `None` or a base-10 integer |
| 94 |
if maybe_count is not None: |
| 95 |
count = int(maybe_count) |
| 96 |
if count == expected_count: |
| 97 |
break |
| 98 |
elif i >= check_max: |
| 99 |
assert count == expected_count |
| 100 |
# try again later |
| 101 |
sleep(check_interval) |
| 102 |
else: |
| 103 |
raise TimeoutError(f"{redis_key!r} was never incremented") |
| 104 |
|
| 105 |
# There should be no more increments - block momentarily |
| 106 |
sleep(min(1, timeout)) |
| 107 |
assert int(redis_connection.get(redis_key)) == expected_count |
| 108 |
|
| 109 |
|
| 110 |
def compare_group_ids_in_redis(redis_key='redis-group-ids'): |
| 111 |
redis_connection = get_redis_connection() |
| 112 |
actual = redis_connection.lrange(redis_key, 0, -1) |
| 113 |
assert len(actual) >= 2, 'Expected at least 2 group ids in redis' |
| 114 |
assert actual[0] == actual[1], 'Expected group ids to be equal' |
| 115 |
|
| 116 |
|
| 117 |
class test_link_error: |
| 118 |
@flaky |
| 119 |
def test_link_error_eager(self): |
| 120 |
exception = ExpectedException("Task expected to fail", "test") |
| 121 |
result = fail.apply(args=("test",), link_error=return_exception.s()) |
| 122 |
actual = result.get(timeout=TIMEOUT, propagate=False) |
| 123 |
assert actual == exception |
| 124 |
|
| 125 |
@flaky |
| 126 |
def test_link_error(self): |
| 127 |
exception = ExpectedException("Task expected to fail", "test") |
| 128 |
result = fail.apply(args=("test",), link_error=return_exception.s()) |
| 129 |
actual = result.get(timeout=TIMEOUT, propagate=False) |
| 130 |
assert actual == exception |
| 131 |
|
| 132 |
@flaky |
| 133 |
def test_link_error_callback_error_callback_retries_eager(self): |
| 134 |
exception = ExpectedException("Task expected to fail", "test") |
| 135 |
result = fail.apply( |
| 136 |
args=("test",), |
| 137 |
link_error=retry_once.s(countdown=None) |
| 138 |
) |
| 139 |
assert result.get(timeout=TIMEOUT, propagate=False) == exception |
| 140 |
|
| 141 |
@flaky |
| 142 |
def test_link_error_callback_retries(self, manager): |
| 143 |
exception = ExpectedException("Task expected to fail", "test") |
| 144 |
result = fail.apply_async( |
| 145 |
args=("test",), |
| 146 |
link_error=retry_once.s(countdown=None) |
| 147 |
) |
| 148 |
assert result.get(timeout=TIMEOUT / 10, propagate=False) == exception |
| 149 |
|
| 150 |
@flaky |
| 151 |
def test_link_error_using_signature_eager(self): |
| 152 |
fail = signature('t.integration.tasks.fail', args=("test",)) |
| 153 |
return_exception = signature('t.integration.tasks.return_exception') |
| 154 |
|
| 155 |
fail.link_error(return_exception) |
| 156 |
|
| 157 |
exception = ExpectedException("Task expected to fail", "test") |
| 158 |
assert (fail.apply().get(timeout=TIMEOUT, propagate=False), True) == ( |
| 159 |
exception, True) |
| 160 |
|
| 161 |
def test_link_error_using_signature(self, manager): |
| 162 |
fail = signature('t.integration.tasks.fail', args=("test",)) |
| 163 |
return_exception = signature('t.integration.tasks.return_exception') |
| 164 |
|
| 165 |
fail.link_error(return_exception) |
| 166 |
|
| 167 |
exception = ExpectedException("Task expected to fail", "test") |
| 168 |
assert (fail.delay().get(timeout=TIMEOUT / 10, propagate=False), True) == ( |
| 169 |
exception, True) |
| 170 |
|
| 171 |
|
| 172 |
class test_chain: |
| 173 |
|
| 174 |
@flaky |
| 175 |
def test_simple_chain(self, manager): |
| 176 |
c = add.s(4, 4) | add.s(8) | add.s(16) |
| 177 |
assert c().get(timeout=TIMEOUT) == 32 |
| 178 |
|
| 179 |
@flaky |
| 180 |
def test_single_chain(self, manager): |
| 181 |
c = chain(add.s(3, 4))() |
| 182 |
assert c.get(timeout=TIMEOUT) == 7 |
| 183 |
|
| 184 |
@flaky |
| 185 |
def test_complex_chain(self, manager): |
| 186 |
g = group(add.s(i) for i in range(4)) |
| 187 |
c = ( |
| 188 |
add.s(2, 2) | ( |
| 189 |
add.s(4) | add_replaced.s(8) | add.s(16) | add.s(32) |
| 190 |
) | g |
| 191 |
) |
| 192 |
res = c() |
| 193 |
assert res.get(timeout=TIMEOUT) == [64, 65, 66, 67] |
| 194 |
|
| 195 |
@pytest.mark.xfail(raises=TimeoutError, reason="Task is timeout") |
| 196 |
def test_group_results_in_chain(self, manager): |
| 197 |
# This adds in an explicit test for the special case added in commit |
| 198 |
# 1e3fcaa969de6ad32b52a3ed8e74281e5e5360e6 |
| 199 |
c = ( |
| 200 |
group( |
| 201 |
add.s(1, 2) | group( |
| 202 |
add.s(1), add.s(2) |
| 203 |
) |
| 204 |
) |
| 205 |
) |
| 206 |
res = c() |
| 207 |
assert res.get(timeout=TIMEOUT / 10) == [4, 5] |
| 208 |
|
| 209 |
def test_chain_of_chain_with_a_single_task(self, manager): |
| 210 |
sig = signature('any_taskname', queue='any_q') |
| 211 |
chain([chain(sig)]).apply_async() |
| 212 |
|
| 213 |
def test_chain_on_error(self, manager): |
| 214 |
from .tasks import ExpectedException |
| 215 |
|
| 216 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 217 |
raise pytest.skip('Requires redis result backend.') |
| 218 |
|
| 219 |
# Run the chord and wait for the error callback to finish. |
| 220 |
c1 = chain( |
| 221 |
add.s(1, 2), fail.s(), add.s(3, 4), |
| 222 |
) |
| 223 |
res = c1() |
| 224 |
|
| 225 |
with pytest.raises(ExpectedException): |
| 226 |
res.get(propagate=True) |
| 227 |
|
| 228 |
with pytest.raises(ExpectedException): |
| 229 |
res.parent.get(propagate=True) |
| 230 |
|
| 231 |
@flaky |
| 232 |
def test_chain_inside_group_receives_arguments(self, manager): |
| 233 |
c = ( |
| 234 |
add.s(5, 6) | |
| 235 |
group((add.s(1) | add.s(2), add.s(3))) |
| 236 |
) |
| 237 |
res = c() |
| 238 |
assert res.get(timeout=TIMEOUT) == [14, 14] |
| 239 |
|
| 240 |
@flaky |
| 241 |
def test_eager_chain_inside_task(self, manager): |
| 242 |
from .tasks import chain_add |
| 243 |
|
| 244 |
prev = chain_add.app.conf.task_always_eager |
| 245 |
chain_add.app.conf.task_always_eager = True |
| 246 |
|
| 247 |
chain_add.apply_async(args=(4, 8), throw=True).get() |
| 248 |
|
| 249 |
chain_add.app.conf.task_always_eager = prev |
| 250 |
|
| 251 |
@flaky |
| 252 |
def test_group_chord_group_chain(self, manager): |
| 253 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 254 |
raise pytest.skip('Requires redis result backend.') |
| 255 |
redis_connection = get_redis_connection() |
| 256 |
redis_connection.delete('redis-echo') |
| 257 |
before = group(redis_echo.si(f'before {i}') for i in range(3)) |
| 258 |
connect = redis_echo.si('connect') |
| 259 |
after = group(redis_echo.si(f'after {i}') for i in range(2)) |
| 260 |
|
| 261 |
result = (before | connect | after).delay() |
| 262 |
result.get(timeout=TIMEOUT) |
| 263 |
redis_messages = list(redis_connection.lrange('redis-echo', 0, -1)) |
| 264 |
before_items = {b'before 0', b'before 1', b'before 2'} |
| 265 |
after_items = {b'after 0', b'after 1'} |
| 266 |
|
| 267 |
assert set(redis_messages[:3]) == before_items |
| 268 |
assert redis_messages[3] == b'connect' |
| 269 |
assert set(redis_messages[4:]) == after_items |
| 270 |
redis_connection.delete('redis-echo') |
| 271 |
|
| 272 |
@flaky |
| 273 |
def test_group_result_not_has_cache(self, manager): |
| 274 |
t1 = identity.si(1) |
| 275 |
t2 = identity.si(2) |
| 276 |
gt = group([identity.si(3), identity.si(4)]) |
| 277 |
ct = chain(identity.si(5), gt) |
| 278 |
task = group(t1, t2, ct) |
| 279 |
result = task.delay() |
| 280 |
assert result.get(timeout=TIMEOUT) == [1, 2, [3, 4]] |
| 281 |
|
| 282 |
@flaky |
| 283 |
def test_second_order_replace(self, manager): |
| 284 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 285 |
raise pytest.skip('Requires redis result backend.') |
| 286 |
|
| 287 |
redis_connection = get_redis_connection() |
| 288 |
redis_connection.delete('redis-echo') |
| 289 |
|
| 290 |
result = second_order_replace1.delay() |
| 291 |
result.get(timeout=TIMEOUT) |
| 292 |
redis_messages = list(redis_connection.lrange('redis-echo', 0, -1)) |
| 293 |
|
| 294 |
expected_messages = [b'In A', b'In B', b'In/Out C', b'Out B', |
| 295 |
b'Out A'] |
| 296 |
assert redis_messages == expected_messages |
| 297 |
|
| 298 |
@flaky |
| 299 |
def test_parent_ids(self, manager, num=10): |
| 300 |
assert_ping(manager) |
| 301 |
|
| 302 |
c = chain(ids.si(i=i) for i in range(num)) |
| 303 |
c.freeze() |
| 304 |
res = c() |
| 305 |
try: |
| 306 |
res.get(timeout=TIMEOUT) |
| 307 |
except TimeoutError: |
| 308 |
print(manager.inspect().active()) |
| 309 |
print(manager.inspect().reserved()) |
| 310 |
print(manager.inspect().stats()) |
| 311 |
raise |
| 312 |
self.assert_ids(res, num - 1) |
| 313 |
|
| 314 |
def assert_ids(self, res, size): |
| 315 |
i, root = size, res |
| 316 |
while root.parent: |
| 317 |
root = root.parent |
| 318 |
node = res |
| 319 |
while node: |
| 320 |
root_id, parent_id, value = node.get(timeout=30) |
| 321 |
assert value == i |
| 322 |
if node.parent: |
| 323 |
assert parent_id == node.parent.id |
| 324 |
assert root_id == root.id |
| 325 |
node = node.parent |
| 326 |
i -= 1 |
| 327 |
|
| 328 |
def test_chord_soft_timeout_recuperation(self, manager): |
| 329 |
"""Test that if soft timeout happens in task but is managed by task, |
| 330 |
chord still get results normally |
| 331 |
""" |
| 332 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 333 |
raise pytest.skip('Requires redis result backend.') |
| 334 |
|
| 335 |
c = chord([ |
| 336 |
# return 3 |
| 337 |
add.s(1, 2), |
| 338 |
# return 0 after managing soft timeout |
| 339 |
delayed_sum_with_soft_guard.s( |
| 340 |
[100], pause_time=2 |
| 341 |
).set( |
| 342 |
soft_time_limit=1 |
| 343 |
), |
| 344 |
]) |
| 345 |
result = c(delayed_sum.s(pause_time=0)).get() |
| 346 |
assert result == 3 |
| 347 |
|
| 348 |
def test_chain_error_handler_with_eta(self, manager): |
| 349 |
try: |
| 350 |
manager.app.backend.ensure_chords_allowed() |
| 351 |
except NotImplementedError as e: |
| 352 |
raise pytest.skip(e.args[0]) |
| 353 |
|
| 354 |
eta = datetime.now(timezone.utc) + timedelta(seconds=10) |
| 355 |
c = chain( |
| 356 |
group( |
| 357 |
add.s(1, 2), |
| 358 |
add.s(3, 4), |
| 359 |
), |
| 360 |
tsum.s() |
| 361 |
).on_error(print_unicode.s()).apply_async(eta=eta) |
| 362 |
|
| 363 |
result = c.get() |
| 364 |
assert result == 10 |
| 365 |
|
| 366 |
@flaky |
| 367 |
def test_groupresult_serialization(self, manager): |
| 368 |
"""Test GroupResult is correctly serialized |
| 369 |
to save in the result backend""" |
| 370 |
try: |
| 371 |
manager.app.backend.ensure_chords_allowed() |
| 372 |
except NotImplementedError as e: |
| 373 |
raise pytest.skip(e.args[0]) |
| 374 |
|
| 375 |
async_result = build_chain_inside_task.delay() |
| 376 |
result = async_result.get() |
| 377 |
assert len(result) == 2 |
| 378 |
assert isinstance(result[0][1], list) |
| 379 |
|
| 380 |
@flaky |
| 381 |
def test_chain_of_task_a_group_and_a_chord(self, manager): |
| 382 |
try: |
| 383 |
manager.app.backend.ensure_chords_allowed() |
| 384 |
except NotImplementedError as e: |
| 385 |
raise pytest.skip(e.args[0]) |
| 386 |
|
| 387 |
c = add.si(1, 0) |
| 388 |
c = c | group(add.s(1), add.s(1)) |
| 389 |
c = c | group(tsum.s(), tsum.s()) |
| 390 |
c = c | tsum.s() |
| 391 |
|
| 392 |
res = c() |
| 393 |
assert res.get(timeout=TIMEOUT) == 8 |
| 394 |
|
| 395 |
@flaky |
| 396 |
def test_chain_of_chords_as_groups_chained_to_a_task_with_two_tasks(self, |
| 397 |
manager): |
| 398 |
try: |
| 399 |
manager.app.backend.ensure_chords_allowed() |
| 400 |
except NotImplementedError as e: |
| 401 |
raise pytest.skip(e.args[0]) |
| 402 |
|
| 403 |
c = add.si(1, 0) |
| 404 |
c = c | group(add.s(1), add.s(1)) |
| 405 |
c = c | tsum.s() |
| 406 |
c = c | add.s(1) |
| 407 |
c = c | group(add.s(1), add.s(1)) |
| 408 |
c = c | tsum.s() |
| 409 |
|
| 410 |
res = c() |
| 411 |
assert res.get(timeout=TIMEOUT) == 12 |
| 412 |
|
| 413 |
@flaky |
| 414 |
def test_chain_of_chords_with_two_tasks(self, manager): |
| 415 |
try: |
| 416 |
manager.app.backend.ensure_chords_allowed() |
| 417 |
except NotImplementedError as e: |
| 418 |
raise pytest.skip(e.args[0]) |
| 419 |
|
| 420 |
c = add.si(1, 0) |
| 421 |
c = c | group(add.s(1), add.s(1)) |
| 422 |
c = c | tsum.s() |
| 423 |
c = c | add.s(1) |
| 424 |
c = c | chord(group(add.s(1), add.s(1)), tsum.s()) |
| 425 |
|
| 426 |
res = c() |
| 427 |
assert res.get(timeout=TIMEOUT) == 12 |
| 428 |
|
| 429 |
@flaky |
| 430 |
def test_chain_of_explicit_chords(self, manager): |
| 431 |
try: |
| 432 |
manager.app.backend.ensure_chords_allowed() |
| 433 |
except NotImplementedError as e: |
| 434 |
raise pytest.skip(e.args[0]) |
| 435 |
|
| 436 |
c1 = chain( |
| 437 |
chord(group(add.si(1, 0), add.si(1, 0)), tsum.s()), |
| 438 |
chord(group(add.s(1), add.s(1)), tsum.s()), |
| 439 |
chord(group(add.s(0), add.s(0)), tsum.s()), |
| 440 |
) |
| 441 |
c2 = chain( |
| 442 |
chord(group(add.s(10), add.s(10)), tsum.s()), |
| 443 |
chord(group(add.s(0), add.s(0)), tsum.s()), |
| 444 |
chord(group(add.s(1), add.s(1)), tsum.s()), |
| 445 |
) |
| 446 |
c = c1 | c2 |
| 447 |
res = c() |
| 448 |
assert res.get(timeout=TIMEOUT) == 178 |
| 449 |
|
| 450 |
@flaky |
| 451 |
def test_chain_of_nine_chords(self, manager): |
| 452 |
try: |
| 453 |
manager.app.backend.ensure_chords_allowed() |
| 454 |
except NotImplementedError as e: |
| 455 |
raise pytest.skip(e.args[0]) |
| 456 |
|
| 457 |
c = chain( |
| 458 |
chord(group(add.si(1, 0), add.si(1, 0), add.si(1, 0)), tsum.s()), |
| 459 |
chord(group(add.s(1), add.s(1), add.s(1)), tsum.s()), |
| 460 |
chord(group(add.s(1), add.s(1), add.s(1)), tsum.s()), |
| 461 |
chord(group(add.s(1), add.s(1), add.s(1)), tsum.s()), |
| 462 |
chord(group(add.s(1), add.s(1), add.s(1)), tsum.s()), |
| 463 |
chord(group(add.s(1), add.s(1), add.s(1)), tsum.s()), |
| 464 |
chord(group(add.s(1), add.s(1), add.s(1)), tsum.s()), |
| 465 |
chord(group(add.s(1), add.s(1), add.s(1)), tsum.s()), |
| 466 |
chord(group(add.s(0), add.s(0), add.s(0)), tsum.s()), |
| 467 |
) |
| 468 |
res = c() |
| 469 |
assert res.get(timeout=TIMEOUT) == 29520 |
| 470 |
|
| 471 |
@flaky |
| 472 |
def test_chain_of_a_chord_and_a_group_with_two_tasks(self, manager): |
| 473 |
try: |
| 474 |
manager.app.backend.ensure_chords_allowed() |
| 475 |
except NotImplementedError as e: |
| 476 |
raise pytest.skip(e.args[0]) |
| 477 |
|
| 478 |
c = add.si(1, 0) |
| 479 |
c = c | group(add.s(1), add.s(1)) |
| 480 |
c = c | tsum.s() |
| 481 |
c = c | add.s(1) |
| 482 |
c = c | group(add.s(1), add.s(1)) |
| 483 |
|
| 484 |
res = c() |
| 485 |
assert res.get(timeout=TIMEOUT) == [6, 6] |
| 486 |
|
| 487 |
@flaky |
| 488 |
def test_chain_of_a_chord_and_a_task_and_a_group(self, manager): |
| 489 |
try: |
| 490 |
manager.app.backend.ensure_chords_allowed() |
| 491 |
except NotImplementedError as e: |
| 492 |
raise pytest.skip(e.args[0]) |
| 493 |
|
| 494 |
c = group(add.s(1, 1), add.s(1, 1)) |
| 495 |
c = c | tsum.s() |
| 496 |
c = c | add.s(1) |
| 497 |
c = c | group(add.s(1), add.s(1)) |
| 498 |
|
| 499 |
res = c() |
| 500 |
assert res.get(timeout=TIMEOUT) == [6, 6] |
| 501 |
|
| 502 |
@flaky |
| 503 |
def test_chain_of_a_chord_and_two_tasks_and_a_group(self, manager): |
| 504 |
try: |
| 505 |
manager.app.backend.ensure_chords_allowed() |
| 506 |
except NotImplementedError as e: |
| 507 |
raise pytest.skip(e.args[0]) |
| 508 |
|
| 509 |
c = group(add.s(1, 1), add.s(1, 1)) |
| 510 |
c = c | tsum.s() |
| 511 |
c = c | add.s(1) |
| 512 |
c = c | add.s(1) |
| 513 |
c = c | group(add.s(1), add.s(1)) |
| 514 |
|
| 515 |
res = c() |
| 516 |
assert res.get(timeout=TIMEOUT) == [7, 7] |
| 517 |
|
| 518 |
@flaky |
| 519 |
def test_chain_of_a_chord_and_three_tasks_and_a_group(self, manager): |
| 520 |
try: |
| 521 |
manager.app.backend.ensure_chords_allowed() |
| 522 |
except NotImplementedError as e: |
| 523 |
raise pytest.skip(e.args[0]) |
| 524 |
|
| 525 |
c = group(add.s(1, 1), add.s(1, 1)) |
| 526 |
c = c | tsum.s() |
| 527 |
c = c | add.s(1) |
| 528 |
c = c | add.s(1) |
| 529 |
c = c | add.s(1) |
| 530 |
c = c | group(add.s(1), add.s(1)) |
| 531 |
|
| 532 |
res = c() |
| 533 |
assert res.get(timeout=TIMEOUT) == [8, 8] |
| 534 |
|
| 535 |
@pytest.mark.xfail(raises=TimeoutError, reason="Task is timeout") |
| 536 |
def test_nested_chain_group_lone(self, manager): # Fails with Redis 5.x |
| 537 |
""" |
| 538 |
Test that a lone group in a chain completes. |
| 539 |
""" |
| 540 |
sig = chain( |
| 541 |
group(identity.s(42), identity.s(42)), # [42, 42] |
| 542 |
) |
| 543 |
res = sig.delay() |
| 544 |
assert res.get(timeout=TIMEOUT / 10) == [42, 42] |
| 545 |
|
| 546 |
def test_nested_chain_group_mid(self, manager): |
| 547 |
""" |
| 548 |
Test that a mid-point group in a chain completes. |
| 549 |
""" |
| 550 |
try: |
| 551 |
manager.app.backend.ensure_chords_allowed() |
| 552 |
except NotImplementedError as e: |
| 553 |
raise pytest.skip(e.args[0]) |
| 554 |
|
| 555 |
sig = chain( |
| 556 |
identity.s(42), # 42 |
| 557 |
group(identity.s(), identity.s()), # [42, 42] |
| 558 |
identity.s(), # [42, 42] |
| 559 |
) |
| 560 |
res = sig.delay() |
| 561 |
assert res.get(timeout=TIMEOUT) == [42, 42] |
| 562 |
|
| 563 |
def test_nested_chain_group_last(self, manager): |
| 564 |
""" |
| 565 |
Test that a final group in a chain with preceding tasks completes. |
| 566 |
""" |
| 567 |
sig = chain( |
| 568 |
identity.s(42), # 42 |
| 569 |
group(identity.s(), identity.s()), # [42, 42] |
| 570 |
) |
| 571 |
res = sig.delay() |
| 572 |
assert res.get(timeout=TIMEOUT) == [42, 42] |
| 573 |
|
| 574 |
def test_chain_replaced_with_a_chain_and_a_callback(self, manager): |
| 575 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 576 |
raise pytest.skip('Requires redis result backend.') |
| 577 |
|
| 578 |
redis_connection = get_redis_connection() |
| 579 |
redis_connection.delete('redis-echo') |
| 580 |
|
| 581 |
link_msg = 'Internal chain callback' |
| 582 |
c = chain( |
| 583 |
identity.s('Hello '), |
| 584 |
# The replacement chain will pass its args though |
| 585 |
replace_with_chain.s(link_msg=link_msg), |
| 586 |
add.s('world'), |
| 587 |
) |
| 588 |
res = c.delay() |
| 589 |
|
| 590 |
assert res.get(timeout=TIMEOUT) == 'Hello world' |
| 591 |
await_redis_echo({link_msg, }) |
| 592 |
|
| 593 |
def test_chain_replaced_with_a_chain_and_an_error_callback(self, manager): |
| 594 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 595 |
raise pytest.skip('Requires redis result backend.') |
| 596 |
|
| 597 |
redis_connection = get_redis_connection() |
| 598 |
redis_connection.delete('redis-echo') |
| 599 |
|
| 600 |
link_msg = 'Internal chain errback' |
| 601 |
c = chain( |
| 602 |
identity.s('Hello '), |
| 603 |
replace_with_chain_which_raises.s(link_msg=link_msg), |
| 604 |
add.s(' will never be seen :(') |
| 605 |
) |
| 606 |
res = c.delay() |
| 607 |
|
| 608 |
with pytest.raises(ValueError): |
| 609 |
res.get(timeout=TIMEOUT) |
| 610 |
await_redis_echo({link_msg, }) |
| 611 |
|
| 612 |
def test_chain_with_cb_replaced_with_chain_with_cb(self, manager): |
| 613 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 614 |
raise pytest.skip('Requires redis result backend.') |
| 615 |
|
| 616 |
redis_connection = get_redis_connection() |
| 617 |
redis_connection.delete('redis-echo') |
| 618 |
|
| 619 |
link_msg = 'Internal chain callback' |
| 620 |
c = chain( |
| 621 |
identity.s('Hello '), |
| 622 |
# The replacement chain will pass its args though |
| 623 |
replace_with_chain.s(link_msg=link_msg), |
| 624 |
add.s('world'), |
| 625 |
) |
| 626 |
c.link(redis_echo.s()) |
| 627 |
res = c.delay() |
| 628 |
|
| 629 |
assert res.get(timeout=TIMEOUT) == 'Hello world' |
| 630 |
await_redis_echo({link_msg, 'Hello world'}) |
| 631 |
|
| 632 |
def test_chain_flattening_keep_links_of_inner_chain(self, manager): |
| 633 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 634 |
raise pytest.skip('Requires redis result backend.') |
| 635 |
|
| 636 |
redis_connection = get_redis_connection() |
| 637 |
|
| 638 |
link_b_msg = 'link_b called' |
| 639 |
link_b_key = 'echo_link_b' |
| 640 |
link_b_sig = redis_echo.si(link_b_msg, redis_key=link_b_key) |
| 641 |
|
| 642 |
def link_chain(sig): |
| 643 |
sig.link(link_b_sig) |
| 644 |
sig.link_error(identity.s('link_ab')) |
| 645 |
return sig |
| 646 |
|
| 647 |
inner_chain = link_chain(chain(identity.s('a'), add.s('b'))) |
| 648 |
flat_chain = chain(inner_chain, add.s('c')) |
| 649 |
redis_connection.delete(link_b_key) |
| 650 |
res = flat_chain.delay() |
| 651 |
|
| 652 |
assert res.get(timeout=TIMEOUT) == 'abc' |
| 653 |
await_redis_echo((link_b_msg,), redis_key=link_b_key) |
| 654 |
|
| 655 |
def test_chain_with_eb_replaced_with_chain_with_eb( |
| 656 |
self, manager, subtests |
| 657 |
): |
| 658 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 659 |
raise pytest.skip('Requires redis result backend.') |
| 660 |
|
| 661 |
redis_connection = get_redis_connection() |
| 662 |
redis_connection.delete('redis-echo') |
| 663 |
|
| 664 |
inner_link_msg = 'Internal chain errback' |
| 665 |
outer_link_msg = 'External chain errback' |
| 666 |
c = chain( |
| 667 |
identity.s('Hello '), |
| 668 |
# The replacement chain will die and break the encapsulating chain |
| 669 |
replace_with_chain_which_raises.s(link_msg=inner_link_msg), |
| 670 |
add.s('world'), |
| 671 |
) |
| 672 |
c.link_error(redis_echo.si(outer_link_msg)) |
| 673 |
res = c.delay() |
| 674 |
|
| 675 |
with subtests.test(msg="Chain fails due to a child task dying"): |
| 676 |
with pytest.raises(ValueError): |
| 677 |
res.get(timeout=TIMEOUT) |
| 678 |
with subtests.test(msg="Chain and child task callbacks are called"): |
| 679 |
await_redis_echo({inner_link_msg, outer_link_msg}) |
| 680 |
|
| 681 |
def test_replace_chain_with_empty_chain(self, manager): |
| 682 |
r = chain(identity.s(1), replace_with_empty_chain.s()).delay() |
| 683 |
|
| 684 |
with pytest.raises(ImproperlyConfigured, |
| 685 |
match="Cannot replace with an empty chain"): |
| 686 |
r.get(timeout=TIMEOUT) |
| 687 |
|
| 688 |
def test_chain_children_with_callbacks(self, manager, subtests): |
| 689 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 690 |
raise pytest.skip("Requires redis result backend.") |
| 691 |
redis_connection = get_redis_connection() |
| 692 |
|
| 693 |
redis_key = str(uuid.uuid4()) |
| 694 |
callback = redis_count.si(redis_key=redis_key) |
| 695 |
|
| 696 |
child_task_count = 42 |
| 697 |
child_sig = identity.si(1337) |
| 698 |
child_sig.link(callback) |
| 699 |
chain_sig = chain(child_sig for _ in range(child_task_count)) |
| 700 |
|
| 701 |
redis_connection.delete(redis_key) |
| 702 |
with subtests.test(msg="Chain executes as expected"): |
| 703 |
res_obj = chain_sig() |
| 704 |
assert res_obj.get(timeout=TIMEOUT) == 1337 |
| 705 |
with subtests.test(msg="Chain child task callbacks are called"): |
| 706 |
await_redis_count(child_task_count, redis_key=redis_key) |
| 707 |
redis_connection.delete(redis_key) |
| 708 |
|
| 709 |
def test_chain_children_with_errbacks(self, manager, subtests): |
| 710 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 711 |
raise pytest.skip("Requires redis result backend.") |
| 712 |
redis_connection = get_redis_connection() |
| 713 |
|
| 714 |
redis_key = str(uuid.uuid4()) |
| 715 |
errback = redis_count.si(redis_key=redis_key) |
| 716 |
|
| 717 |
child_task_count = 42 |
| 718 |
child_sig = fail.si() |
| 719 |
child_sig.link_error(errback) |
| 720 |
chain_sig = chain(child_sig for _ in range(child_task_count)) |
| 721 |
|
| 722 |
redis_connection.delete(redis_key) |
| 723 |
with subtests.test(msg="Chain fails due to a child task dying"): |
| 724 |
res_obj = chain_sig() |
| 725 |
with pytest.raises(ExpectedException): |
| 726 |
res_obj.get(timeout=TIMEOUT) |
| 727 |
with subtests.test(msg="Chain child task errbacks are called"): |
| 728 |
# Only the first child task gets a change to run and fail |
| 729 |
await_redis_count(1, redis_key=redis_key) |
| 730 |
redis_connection.delete(redis_key) |
| 731 |
|
| 732 |
def test_chain_with_callback_child_replaced(self, manager, subtests): |
| 733 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 734 |
raise pytest.skip("Requires redis result backend.") |
| 735 |
redis_connection = get_redis_connection() |
| 736 |
|
| 737 |
redis_key = str(uuid.uuid4()) |
| 738 |
callback = redis_count.si(redis_key=redis_key) |
| 739 |
|
| 740 |
chain_sig = chain(add_replaced.si(42, 1337), identity.s()) |
| 741 |
chain_sig.link(callback) |
| 742 |
|
| 743 |
redis_connection.delete(redis_key) |
| 744 |
with subtests.test(msg="Chain executes as expected"): |
| 745 |
res_obj = chain_sig() |
| 746 |
assert res_obj.get(timeout=TIMEOUT) == 42 + 1337 |
| 747 |
with subtests.test(msg="Callback is called after chain finishes"): |
| 748 |
await_redis_count(1, redis_key=redis_key) |
| 749 |
redis_connection.delete(redis_key) |
| 750 |
|
| 751 |
def test_chain_with_errback_child_replaced(self, manager, subtests): |
| 752 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 753 |
raise pytest.skip("Requires redis result backend.") |
| 754 |
redis_connection = get_redis_connection() |
| 755 |
|
| 756 |
redis_key = str(uuid.uuid4()) |
| 757 |
errback = redis_count.si(redis_key=redis_key) |
| 758 |
|
| 759 |
chain_sig = chain(add_replaced.si(42, 1337), fail.s()) |
| 760 |
chain_sig.link_error(errback) |
| 761 |
|
| 762 |
redis_connection.delete(redis_key) |
| 763 |
with subtests.test(msg="Chain executes as expected"): |
| 764 |
res_obj = chain_sig() |
| 765 |
with pytest.raises(ExpectedException): |
| 766 |
res_obj.get(timeout=TIMEOUT) |
| 767 |
with subtests.test(msg="Errback is called after chain finishes"): |
| 768 |
await_redis_count(1, redis_key=redis_key) |
| 769 |
redis_connection.delete(redis_key) |
| 770 |
|
| 771 |
def test_chain_child_with_callback_replaced(self, manager, subtests): |
| 772 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 773 |
raise pytest.skip("Requires redis result backend.") |
| 774 |
redis_connection = get_redis_connection() |
| 775 |
|
| 776 |
redis_key = str(uuid.uuid4()) |
| 777 |
callback = redis_count.si(redis_key=redis_key) |
| 778 |
|
| 779 |
child_sig = add_replaced.si(42, 1337) |
| 780 |
child_sig.link(callback) |
| 781 |
chain_sig = chain(child_sig, identity.s()) |
| 782 |
|
| 783 |
redis_connection.delete(redis_key) |
| 784 |
with subtests.test(msg="Chain executes as expected"): |
| 785 |
res_obj = chain_sig() |
| 786 |
assert res_obj.get(timeout=TIMEOUT) == 42 + 1337 |
| 787 |
with subtests.test(msg="Callback is called after chain finishes"): |
| 788 |
await_redis_count(1, redis_key=redis_key) |
| 789 |
redis_connection.delete(redis_key) |
| 790 |
|
| 791 |
def test_chain_child_with_errback_replaced(self, manager, subtests): |
| 792 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 793 |
raise pytest.skip("Requires redis result backend.") |
| 794 |
redis_connection = get_redis_connection() |
| 795 |
|
| 796 |
redis_key = str(uuid.uuid4()) |
| 797 |
errback = redis_count.si(redis_key=redis_key) |
| 798 |
|
| 799 |
child_sig = fail_replaced.si() |
| 800 |
child_sig.link_error(errback) |
| 801 |
chain_sig = chain(child_sig, identity.si(42)) |
| 802 |
|
| 803 |
redis_connection.delete(redis_key) |
| 804 |
with subtests.test(msg="Chain executes as expected"): |
| 805 |
res_obj = chain_sig() |
| 806 |
with pytest.raises(ExpectedException): |
| 807 |
res_obj.get(timeout=TIMEOUT) |
| 808 |
with subtests.test(msg="Errback is called after chain finishes"): |
| 809 |
await_redis_count(1, redis_key=redis_key) |
| 810 |
redis_connection.delete(redis_key) |
| 811 |
|
| 812 |
@pytest.mark.xfail(raises=TimeoutError, |
| 813 |
reason="Task is timeout instead of returning exception on rpc backend", |
| 814 |
strict=False) |
| 815 |
def test_task_replaced_with_chain(self, manager): |
| 816 |
orig_sig = replace_with_chain.si(42) |
| 817 |
res_obj = orig_sig.delay() |
| 818 |
assert res_obj.get(timeout=TIMEOUT) == 42 |
| 819 |
|
| 820 |
def test_chain_child_replaced_with_chain_first(self, manager): |
| 821 |
orig_sig = chain(replace_with_chain.si(42), identity.s()) |
| 822 |
res_obj = orig_sig.delay() |
| 823 |
assert res_obj.get(timeout=TIMEOUT) == 42 |
| 824 |
|
| 825 |
def test_chain_child_replaced_with_chain_middle(self, manager): |
| 826 |
orig_sig = chain( |
| 827 |
identity.s(42), replace_with_chain.s(), identity.s() |
| 828 |
) |
| 829 |
res_obj = orig_sig.delay() |
| 830 |
assert res_obj.get(timeout=TIMEOUT) == 42 |
| 831 |
|
| 832 |
@pytest.mark.xfail(raises=TimeoutError, |
| 833 |
reason="Task is timeout instead of returning exception on rpc backend", |
| 834 |
strict=False) |
| 835 |
def test_chain_child_replaced_with_chain_last(self, manager): |
| 836 |
orig_sig = chain(identity.s(42), replace_with_chain.s()) |
| 837 |
res_obj = orig_sig.delay() |
| 838 |
assert res_obj.get(timeout=TIMEOUT) == 42 |
| 839 |
|
| 840 |
@pytest.mark.parametrize('redis_key', ['redis-group-ids']) |
| 841 |
def test_chord_header_id_duplicated_on_rabbitmq_msg_duplication(self, manager, subtests, celery_session_app, |
| 842 |
redis_key): |
| 843 |
""" |
| 844 |
When a task that predates a chord in a chain was duplicated by Rabbitmq (for whatever reason), |
| 845 |
the chord header id was not duplicated. This caused the chord header to have a different id. |
| 846 |
This test ensures that the chord header's id preserves itself in face of such an edge case. |
| 847 |
To validate the correct behavior is implemented, we collect the original and duplicated chord header ids |
| 848 |
in redis, to ensure that they are the same. |
| 849 |
""" |
| 850 |
|
| 851 |
try: |
| 852 |
manager.app.backend.ensure_chords_allowed() |
| 853 |
except NotImplementedError as e: |
| 854 |
raise pytest.skip(e.args[0]) |
| 855 |
|
| 856 |
if manager.app.conf.broker_url.startswith('redis'): |
| 857 |
raise pytest.xfail('Redis broker does not duplicate the task (t1)') |
| 858 |
|
| 859 |
# Republish t1 to cause the chain to be executed twice |
| 860 |
@before_task_publish.connect |
| 861 |
def before_task_publish_handler(sender=None, body=None, exchange=None, routing_key=None, headers=None, |
| 862 |
properties=None, |
| 863 |
declare=None, retry_policy=None, **kwargs): |
| 864 |
""" We want to republish t1 to ensure that the chain is executed twice """ |
| 865 |
|
| 866 |
metadata = { |
| 867 |
'body': body, |
| 868 |
'exchange': exchange, |
| 869 |
'routing_key': routing_key, |
| 870 |
'properties': properties, |
| 871 |
'headers': headers, |
| 872 |
} |
| 873 |
|
| 874 |
with celery_session_app.producer_pool.acquire(block=True) as producer: |
| 875 |
# Publish t1 to the message broker, just before it's going to be published which causes duplication |
| 876 |
return producer.publish( |
| 877 |
metadata['body'], |
| 878 |
exchange=metadata['exchange'], |
| 879 |
routing_key=metadata['routing_key'], |
| 880 |
retry=None, |
| 881 |
retry_policy=retry_policy, |
| 882 |
serializer='json', |
| 883 |
delivery_mode=None, |
| 884 |
headers=headers, |
| 885 |
**kwargs |
| 886 |
) |
| 887 |
|
| 888 |
# Clean redis key |
| 889 |
redis_connection = get_redis_connection() |
| 890 |
if redis_connection.exists(redis_key): |
| 891 |
redis_connection.delete(redis_key) |
| 892 |
|
| 893 |
# Prepare tasks |
| 894 |
t1, t2, t3, t4 = identity.s(42), redis_echo_group_id.s(), identity.s(), identity.s() |
| 895 |
c = chain(t1, chord([t2, t3], t4)) |
| 896 |
|
| 897 |
# Delay chain |
| 898 |
r1 = c.delay() |
| 899 |
r1.get(timeout=TIMEOUT) |
| 900 |
|
| 901 |
# Cleanup |
| 902 |
before_task_publish.disconnect(before_task_publish_handler) |
| 903 |
|
| 904 |
with subtests.test(msg='Compare group ids via redis list'): |
| 905 |
await_redis_list_message_length(2, redis_key=redis_key, timeout=15) |
| 906 |
compare_group_ids_in_redis(redis_key=redis_key) |
| 907 |
|
| 908 |
# Cleanup |
| 909 |
redis_connection = get_redis_connection() |
| 910 |
redis_connection.delete(redis_key) |
| 911 |
|
| 912 |
def test_chaining_upgraded_chords_pure_groups(self, manager, subtests): |
| 913 |
""" This test is built to reproduce the github issue https://github.com/celery/celery/issues/5958 |
| 914 |
|
| 915 |
The issue describes a canvas where a chain of groups are executed multiple times instead of once. |
| 916 |
This test is built to reproduce the issue and to verify that the issue is fixed. |
| 917 |
""" |
| 918 |
try: |
| 919 |
manager.app.backend.ensure_chords_allowed() |
| 920 |
except NotImplementedError as e: |
| 921 |
raise pytest.skip(e.args[0]) |
| 922 |
|
| 923 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 924 |
raise pytest.skip('Requires redis result backend.') |
| 925 |
|
| 926 |
redis_connection = get_redis_connection() |
| 927 |
redis_key = 'echo_chamber' |
| 928 |
|
| 929 |
c = chain( |
| 930 |
# letting the chain upgrade the chord, reproduces the issue in _chord.__or__ |
| 931 |
group( |
| 932 |
redis_echo.si('1', redis_key=redis_key), |
| 933 |
redis_echo.si('2', redis_key=redis_key), |
| 934 |
redis_echo.si('3', redis_key=redis_key), |
| 935 |
), |
| 936 |
group( |
| 937 |
redis_echo.si('4', redis_key=redis_key), |
| 938 |
redis_echo.si('5', redis_key=redis_key), |
| 939 |
redis_echo.si('6', redis_key=redis_key), |
| 940 |
), |
| 941 |
group( |
| 942 |
redis_echo.si('7', redis_key=redis_key), |
| 943 |
), |
| 944 |
group( |
| 945 |
redis_echo.si('8', redis_key=redis_key), |
| 946 |
), |
| 947 |
redis_echo.si('9', redis_key=redis_key), |
| 948 |
redis_echo.si('Done', redis_key='Done'), |
| 949 |
) |
| 950 |
|
| 951 |
with subtests.test(msg='Run the chain and wait for completion'): |
| 952 |
redis_connection.delete(redis_key, 'Done') |
| 953 |
c.delay().get(timeout=TIMEOUT) |
| 954 |
await_redis_list_message_length(1, redis_key='Done', timeout=10) |
| 955 |
|
| 956 |
with subtests.test(msg='All tasks are executed once'): |
| 957 |
actual = [sig.decode('utf-8') for sig in redis_connection.lrange(redis_key, 0, -1)] |
| 958 |
expected = [str(i) for i in range(1, 10)] |
| 959 |
with subtests.test(msg='All tasks are executed once'): |
| 960 |
assert sorted(actual) == sorted(expected) |
| 961 |
|
| 962 |
# Cleanup |
| 963 |
redis_connection.delete(redis_key, 'Done') |
| 964 |
|
| 965 |
def test_chaining_upgraded_chords_starting_with_chord(self, manager, subtests): |
| 966 |
""" This test is built to reproduce the github issue https://github.com/celery/celery/issues/5958 |
| 967 |
|
| 968 |
The issue describes a canvas where a chain of groups are executed multiple times instead of once. |
| 969 |
This test is built to reproduce the issue and to verify that the issue is fixed. |
| 970 |
""" |
| 971 |
try: |
| 972 |
manager.app.backend.ensure_chords_allowed() |
| 973 |
except NotImplementedError as e: |
| 974 |
raise pytest.skip(e.args[0]) |
| 975 |
|
| 976 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 977 |
raise pytest.skip('Requires redis result backend.') |
| 978 |
|
| 979 |
redis_connection = get_redis_connection() |
| 980 |
redis_key = 'echo_chamber' |
| 981 |
|
| 982 |
c = chain( |
| 983 |
# by manually upgrading the chord to a group, we can reproduce the issue in _chain.__or__ |
| 984 |
chord(group([redis_echo.si('1', redis_key=redis_key), |
| 985 |
redis_echo.si('2', redis_key=redis_key), |
| 986 |
redis_echo.si('3', redis_key=redis_key)]), |
| 987 |
group([redis_echo.si('4', redis_key=redis_key), |
| 988 |
redis_echo.si('5', redis_key=redis_key), |
| 989 |
redis_echo.si('6', redis_key=redis_key)])), |
| 990 |
group( |
| 991 |
redis_echo.si('7', redis_key=redis_key), |
| 992 |
), |
| 993 |
group( |
| 994 |
redis_echo.si('8', redis_key=redis_key), |
| 995 |
), |
| 996 |
redis_echo.si('9', redis_key=redis_key), |
| 997 |
redis_echo.si('Done', redis_key='Done'), |
| 998 |
) |
| 999 |
|
| 1000 |
with subtests.test(msg='Run the chain and wait for completion'): |
| 1001 |
redis_connection.delete(redis_key, 'Done') |
| 1002 |
c.delay().get(timeout=TIMEOUT) |
| 1003 |
await_redis_list_message_length(1, redis_key='Done', timeout=10) |
| 1004 |
|
| 1005 |
with subtests.test(msg='All tasks are executed once'): |
| 1006 |
actual = [sig.decode('utf-8') for sig in redis_connection.lrange(redis_key, 0, -1)] |
| 1007 |
expected = [str(i) for i in range(1, 10)] |
| 1008 |
with subtests.test(msg='All tasks are executed once'): |
| 1009 |
assert sorted(actual) == sorted(expected) |
| 1010 |
|
| 1011 |
# Cleanup |
| 1012 |
redis_connection.delete(redis_key, 'Done') |
| 1013 |
|
| 1014 |
def test_chaining_upgraded_chords_mixed_canvas(self, manager, subtests): |
| 1015 |
""" This test is built to reproduce the github issue https://github.com/celery/celery/issues/5958 |
| 1016 |
|
| 1017 |
The issue describes a canvas where a chain of groups are executed multiple times instead of once. |
| 1018 |
This test is built to reproduce the issue and to verify that the issue is fixed. |
| 1019 |
""" |
| 1020 |
try: |
| 1021 |
manager.app.backend.ensure_chords_allowed() |
| 1022 |
except NotImplementedError as e: |
| 1023 |
raise pytest.skip(e.args[0]) |
| 1024 |
|
| 1025 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1026 |
raise pytest.skip('Requires redis result backend.') |
| 1027 |
|
| 1028 |
redis_connection = get_redis_connection() |
| 1029 |
redis_key = 'echo_chamber' |
| 1030 |
|
| 1031 |
c = chain( |
| 1032 |
chord(group([redis_echo.si('1', redis_key=redis_key), |
| 1033 |
redis_echo.si('2', redis_key=redis_key), |
| 1034 |
redis_echo.si('3', redis_key=redis_key)]), |
| 1035 |
group([redis_echo.si('4', redis_key=redis_key), |
| 1036 |
redis_echo.si('5', redis_key=redis_key), |
| 1037 |
redis_echo.si('6', redis_key=redis_key)])), |
| 1038 |
redis_echo.si('7', redis_key=redis_key), |
| 1039 |
group( |
| 1040 |
redis_echo.si('8', redis_key=redis_key), |
| 1041 |
), |
| 1042 |
redis_echo.si('9', redis_key=redis_key), |
| 1043 |
redis_echo.si('Done', redis_key='Done'), |
| 1044 |
) |
| 1045 |
|
| 1046 |
with subtests.test(msg='Run the chain and wait for completion'): |
| 1047 |
redis_connection.delete(redis_key, 'Done') |
| 1048 |
c.delay().get(timeout=TIMEOUT) |
| 1049 |
await_redis_list_message_length(1, redis_key='Done', timeout=10) |
| 1050 |
|
| 1051 |
with subtests.test(msg='All tasks are executed once'): |
| 1052 |
actual = [sig.decode('utf-8') for sig in redis_connection.lrange(redis_key, 0, -1)] |
| 1053 |
expected = [str(i) for i in range(1, 10)] |
| 1054 |
with subtests.test(msg='All tasks are executed once'): |
| 1055 |
assert sorted(actual) == sorted(expected) |
| 1056 |
|
| 1057 |
# Cleanup |
| 1058 |
redis_connection.delete(redis_key, 'Done') |
| 1059 |
|
| 1060 |
def test_freezing_chain_sets_id_of_last_task(self, manager): |
| 1061 |
last_task = add.s(2).set(task_id='42') |
| 1062 |
c = add.s(4) | last_task |
| 1063 |
assert c.id is None |
| 1064 |
c.freeze(last_task.id) |
| 1065 |
assert c.id == last_task.id |
| 1066 |
|
| 1067 |
@pytest.mark.parametrize( |
| 1068 |
"group_last_task", |
| 1069 |
[False, True], |
| 1070 |
) |
| 1071 |
def test_chaining_upgraded_chords_mixed_canvas_protocol_2( |
| 1072 |
self, manager, subtests, group_last_task): |
| 1073 |
""" This test is built to reproduce the github issue https://github.com/celery/celery/issues/8662 |
| 1074 |
|
| 1075 |
The issue describes a canvas where a chain of groups are executed multiple times instead of once. |
| 1076 |
This test is built to reproduce the issue and to verify that the issue is fixed. |
| 1077 |
""" |
| 1078 |
try: |
| 1079 |
manager.app.backend.ensure_chords_allowed() |
| 1080 |
except NotImplementedError as e: |
| 1081 |
raise pytest.skip(e.args[0]) |
| 1082 |
|
| 1083 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1084 |
raise pytest.skip('Requires redis result backend.') |
| 1085 |
|
| 1086 |
redis_connection = get_redis_connection() |
| 1087 |
redis_key = 'echo_chamber' |
| 1088 |
|
| 1089 |
c = chain( |
| 1090 |
group([ |
| 1091 |
redis_echo.si('1', redis_key=redis_key), |
| 1092 |
redis_echo.si('2', redis_key=redis_key) |
| 1093 |
]), |
| 1094 |
group([ |
| 1095 |
redis_echo.si('3', redis_key=redis_key), |
| 1096 |
redis_echo.si('4', redis_key=redis_key), |
| 1097 |
redis_echo.si('5', redis_key=redis_key) |
| 1098 |
]), |
| 1099 |
group([ |
| 1100 |
redis_echo.si('6', redis_key=redis_key), |
| 1101 |
redis_echo.si('7', redis_key=redis_key), |
| 1102 |
redis_echo.si('8', redis_key=redis_key), |
| 1103 |
redis_echo.si('9', redis_key=redis_key) |
| 1104 |
]), |
| 1105 |
redis_echo.si('Done', redis_key='Done') if not group_last_task else |
| 1106 |
group(redis_echo.si('Done', redis_key='Done')), |
| 1107 |
) |
| 1108 |
|
| 1109 |
with subtests.test(msg='Run the chain and wait for completion'): |
| 1110 |
redis_connection.delete(redis_key, 'Done') |
| 1111 |
c.delay().get(timeout=TIMEOUT) |
| 1112 |
await_redis_list_message_length(1, redis_key='Done', timeout=10) |
| 1113 |
|
| 1114 |
with subtests.test(msg='All tasks are executed once'): |
| 1115 |
actual = [ |
| 1116 |
sig.decode('utf-8') |
| 1117 |
for sig in redis_connection.lrange(redis_key, 0, -1) |
| 1118 |
] |
| 1119 |
expected = [str(i) for i in range(1, 10)] |
| 1120 |
with subtests.test(msg='All tasks are executed once'): |
| 1121 |
assert sorted(actual) == sorted(expected) |
| 1122 |
|
| 1123 |
# Cleanup |
| 1124 |
redis_connection.delete(redis_key, 'Done') |
| 1125 |
|
| 1126 |
def test_group_in_center_of_chain(self, manager): |
| 1127 |
try: |
| 1128 |
manager.app.backend.ensure_chords_allowed() |
| 1129 |
except NotImplementedError as e: |
| 1130 |
raise pytest.skip(e.args[0]) |
| 1131 |
|
| 1132 |
t1 = chain(tsum.s(), group(add.s(8), add.s(16)), tsum.s() | add.s(32)) |
| 1133 |
t2 = chord([tsum, tsum], t1) |
| 1134 |
t3 = chord([add.s(0, 1)], t2) |
| 1135 |
res = t3.apply_async() # should not raise |
| 1136 |
assert res.get(timeout=TIMEOUT) == 60 |
| 1137 |
|
| 1138 |
def test_upgrade_to_chord_inside_chains(self, manager): |
| 1139 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1140 |
raise pytest.skip("Requires redis result backend.") |
| 1141 |
try: |
| 1142 |
manager.app.backend.ensure_chords_allowed() |
| 1143 |
except NotImplementedError as e: |
| 1144 |
raise pytest.skip(e.args[0]) |
| 1145 |
|
| 1146 |
redis_key = str(uuid.uuid4()) |
| 1147 |
group1 = group(redis_echo.si('a', redis_key), redis_echo.si('a', redis_key)) |
| 1148 |
group2 = group(redis_echo.si('a', redis_key), redis_echo.si('a', redis_key)) |
| 1149 |
chord1 = group1 | group2 |
| 1150 |
chain1 = chain(chord1, (redis_echo.si('a', redis_key) | redis_echo.si('b', redis_key))) |
| 1151 |
chain1.apply_async().get(timeout=TIMEOUT) |
| 1152 |
redis_connection = get_redis_connection() |
| 1153 |
actual = redis_connection.lrange(redis_key, 0, -1) |
| 1154 |
assert actual.count(b'b') == 1 |
| 1155 |
redis_connection.delete(redis_key) |
| 1156 |
|
| 1157 |
|
| 1158 |
class test_result_set: |
| 1159 |
|
| 1160 |
@flaky |
| 1161 |
def test_result_set(self, manager): |
| 1162 |
assert_ping(manager) |
| 1163 |
|
| 1164 |
rs = ResultSet([add.delay(1, 1), add.delay(2, 2)]) |
| 1165 |
assert rs.get(timeout=TIMEOUT) == [2, 4] |
| 1166 |
|
| 1167 |
@flaky |
| 1168 |
def test_result_set_error(self, manager): |
| 1169 |
assert_ping(manager) |
| 1170 |
|
| 1171 |
rs = ResultSet([raise_error.delay(), add.delay(1, 1)]) |
| 1172 |
rs.get(timeout=TIMEOUT, propagate=False) |
| 1173 |
|
| 1174 |
assert rs.results[0].failed() |
| 1175 |
assert rs.results[1].successful() |
| 1176 |
|
| 1177 |
|
| 1178 |
class test_group: |
| 1179 |
@flaky |
| 1180 |
def test_ready_with_exception(self, manager): |
| 1181 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1182 |
raise pytest.skip('Requires redis result backend.') |
| 1183 |
|
| 1184 |
g = group([add.s(1, 2), raise_error.s()]) |
| 1185 |
result = g.apply_async() |
| 1186 |
while not result.ready(): |
| 1187 |
pass |
| 1188 |
|
| 1189 |
@flaky |
| 1190 |
def test_empty_group_result(self, manager): |
| 1191 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1192 |
raise pytest.skip('Requires redis result backend.') |
| 1193 |
|
| 1194 |
task = group([]) |
| 1195 |
result = task.apply_async() |
| 1196 |
|
| 1197 |
GroupResult.save(result) |
| 1198 |
task = GroupResult.restore(result.id) |
| 1199 |
assert task.results == [] |
| 1200 |
|
| 1201 |
@flaky |
| 1202 |
def test_parent_ids(self, manager): |
| 1203 |
assert_ping(manager) |
| 1204 |
|
| 1205 |
g = ( |
| 1206 |
ids.si(i=1) | |
| 1207 |
ids.si(i=2) | |
| 1208 |
group(ids.si(i=i) for i in range(2, 50)) |
| 1209 |
) |
| 1210 |
res = g() |
| 1211 |
expected_root_id = res.parent.parent.id |
| 1212 |
expected_parent_id = res.parent.id |
| 1213 |
values = res.get(timeout=TIMEOUT) |
| 1214 |
|
| 1215 |
for i, r in enumerate(values): |
| 1216 |
root_id, parent_id, value = r |
| 1217 |
assert root_id == expected_root_id |
| 1218 |
assert parent_id == expected_parent_id |
| 1219 |
assert value == i + 2 |
| 1220 |
|
| 1221 |
@flaky |
| 1222 |
def test_nested_group(self, manager): |
| 1223 |
assert_ping(manager) |
| 1224 |
|
| 1225 |
c = group( |
| 1226 |
add.si(1, 10), |
| 1227 |
group( |
| 1228 |
add.si(1, 100), |
| 1229 |
group( |
| 1230 |
add.si(1, 1000), |
| 1231 |
add.si(1, 2000), |
| 1232 |
), |
| 1233 |
), |
| 1234 |
) |
| 1235 |
res = c() |
| 1236 |
|
| 1237 |
assert res.get(timeout=TIMEOUT) == [11, 101, 1001, 2001] |
| 1238 |
|
| 1239 |
@flaky |
| 1240 |
def test_large_group(self, manager): |
| 1241 |
assert_ping(manager) |
| 1242 |
|
| 1243 |
c = group(identity.s(i) for i in range(1000)) |
| 1244 |
res = c.delay() |
| 1245 |
|
| 1246 |
assert res.get(timeout=TIMEOUT) == list(range(1000)) |
| 1247 |
|
| 1248 |
def test_group_lone(self, manager): |
| 1249 |
""" |
| 1250 |
Test that a simple group completes. |
| 1251 |
""" |
| 1252 |
sig = group(identity.s(42), identity.s(42)) # [42, 42] |
| 1253 |
res = sig.delay() |
| 1254 |
assert res.get(timeout=TIMEOUT) == [42, 42] |
| 1255 |
|
| 1256 |
def test_nested_group_group(self, manager): |
| 1257 |
""" |
| 1258 |
Confirm that groups nested inside groups get unrolled. |
| 1259 |
""" |
| 1260 |
sig = group( |
| 1261 |
group(identity.s(42), identity.s(42)), # [42, 42] |
| 1262 |
) # [42, 42] due to unrolling |
| 1263 |
res = sig.delay() |
| 1264 |
assert res.get(timeout=TIMEOUT) == [42, 42] |
| 1265 |
|
| 1266 |
def test_nested_group_chord_counting_simple(self, manager): |
| 1267 |
try: |
| 1268 |
manager.app.backend.ensure_chords_allowed() |
| 1269 |
except NotImplementedError as e: |
| 1270 |
raise pytest.skip(e.args[0]) |
| 1271 |
|
| 1272 |
gchild_sig = identity.si(42) |
| 1273 |
child_chord = chord((gchild_sig,), identity.s()) |
| 1274 |
group_sig = group((child_chord,)) |
| 1275 |
res = group_sig.delay() |
| 1276 |
# Wait for the result to land and confirm its value is as expected |
| 1277 |
assert res.get(timeout=TIMEOUT) == [[42]] |
| 1278 |
|
| 1279 |
def test_nested_group_chord_counting_chain(self, manager): |
| 1280 |
try: |
| 1281 |
manager.app.backend.ensure_chords_allowed() |
| 1282 |
except NotImplementedError as e: |
| 1283 |
raise pytest.skip(e.args[0]) |
| 1284 |
|
| 1285 |
gchild_count = 42 |
| 1286 |
gchild_sig = chain((identity.si(1337),) * gchild_count) |
| 1287 |
child_chord = chord((gchild_sig,), identity.s()) |
| 1288 |
group_sig = group((child_chord,)) |
| 1289 |
res = group_sig.delay() |
| 1290 |
# Wait for the result to land and confirm its value is as expected |
| 1291 |
assert res.get(timeout=TIMEOUT) == [[1337]] |
| 1292 |
|
| 1293 |
def test_nested_group_chord_counting_group(self, manager): |
| 1294 |
try: |
| 1295 |
manager.app.backend.ensure_chords_allowed() |
| 1296 |
except NotImplementedError as e: |
| 1297 |
raise pytest.skip(e.args[0]) |
| 1298 |
|
| 1299 |
gchild_count = 42 |
| 1300 |
gchild_sig = group((identity.si(1337),) * gchild_count) |
| 1301 |
child_chord = chord((gchild_sig,), identity.s()) |
| 1302 |
group_sig = group((child_chord,)) |
| 1303 |
res = group_sig.delay() |
| 1304 |
# Wait for the result to land and confirm its value is as expected |
| 1305 |
assert res.get(timeout=TIMEOUT) == [[1337] * gchild_count] |
| 1306 |
|
| 1307 |
def test_nested_group_chord_counting_chord(self, manager): |
| 1308 |
try: |
| 1309 |
manager.app.backend.ensure_chords_allowed() |
| 1310 |
except NotImplementedError as e: |
| 1311 |
raise pytest.skip(e.args[0]) |
| 1312 |
|
| 1313 |
gchild_count = 42 |
| 1314 |
gchild_sig = chord( |
| 1315 |
(identity.si(1337),) * gchild_count, identity.si(31337), |
| 1316 |
) |
| 1317 |
child_chord = chord((gchild_sig,), identity.s()) |
| 1318 |
group_sig = group((child_chord,)) |
| 1319 |
res = group_sig.delay() |
| 1320 |
# Wait for the result to land and confirm its value is as expected |
| 1321 |
assert res.get(timeout=TIMEOUT) == [[31337]] |
| 1322 |
|
| 1323 |
def test_nested_group_chord_counting_mixed(self, manager): |
| 1324 |
try: |
| 1325 |
manager.app.backend.ensure_chords_allowed() |
| 1326 |
except NotImplementedError as e: |
| 1327 |
raise pytest.skip(e.args[0]) |
| 1328 |
|
| 1329 |
gchild_count = 42 |
| 1330 |
child_chord = chord( |
| 1331 |
( |
| 1332 |
identity.si(42), |
| 1333 |
chain((identity.si(42),) * gchild_count), |
| 1334 |
group((identity.si(42),) * gchild_count), |
| 1335 |
chord((identity.si(42),) * gchild_count, identity.si(1337)), |
| 1336 |
), |
| 1337 |
identity.s(), |
| 1338 |
) |
| 1339 |
group_sig = group((child_chord,)) |
| 1340 |
res = group_sig.delay() |
| 1341 |
# Wait for the result to land and confirm its value is as expected. The |
| 1342 |
# group result gets unrolled into the encapsulating chord, hence the |
| 1343 |
# weird unpacking below |
| 1344 |
assert res.get(timeout=TIMEOUT) == [ |
| 1345 |
[42, 42, *((42,) * gchild_count), 1337] |
| 1346 |
] |
| 1347 |
|
| 1348 |
@pytest.mark.xfail(raises=TimeoutError, reason="#6734") |
| 1349 |
def test_nested_group_chord_body_chain(self, manager): |
| 1350 |
try: |
| 1351 |
manager.app.backend.ensure_chords_allowed() |
| 1352 |
except NotImplementedError as e: |
| 1353 |
raise pytest.skip(e.args[0]) |
| 1354 |
|
| 1355 |
child_chord = chord(identity.si(42), chain((identity.s(),))) |
| 1356 |
group_sig = group((child_chord,)) |
| 1357 |
res = group_sig.delay() |
| 1358 |
# The result can be expected to timeout since it seems like its |
| 1359 |
# underlying promise might not be getting fulfilled (ref #6734). Pick a |
| 1360 |
# short timeout since we don't want to block for ages and this is a |
| 1361 |
# fairly simple signature which should run pretty quickly. |
| 1362 |
expected_result = [[42]] |
| 1363 |
with pytest.raises(TimeoutError) as expected_excinfo: |
| 1364 |
res.get(timeout=TIMEOUT / 10) |
| 1365 |
# Get the child `AsyncResult` manually so that we don't have to wait |
| 1366 |
# again for the `GroupResult` |
| 1367 |
assert res.children[0].get(timeout=TIMEOUT) == expected_result[0] |
| 1368 |
assert res.get(timeout=TIMEOUT) == expected_result |
| 1369 |
# Re-raise the expected exception so this test will XFAIL |
| 1370 |
raise expected_excinfo.value |
| 1371 |
|
| 1372 |
def test_callback_called_by_group(self, manager, subtests): |
| 1373 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1374 |
raise pytest.skip("Requires redis result backend.") |
| 1375 |
redis_connection = get_redis_connection() |
| 1376 |
|
| 1377 |
callback_msg = str(uuid.uuid4()).encode() |
| 1378 |
redis_key = str(uuid.uuid4()) |
| 1379 |
callback = redis_echo.si(callback_msg, redis_key=redis_key) |
| 1380 |
|
| 1381 |
group_sig = group(identity.si(42), identity.si(1337)) |
| 1382 |
group_sig.link(callback) |
| 1383 |
redis_connection.delete(redis_key) |
| 1384 |
with subtests.test(msg="Group result is returned"): |
| 1385 |
res = group_sig.delay() |
| 1386 |
assert res.get(timeout=TIMEOUT) == [42, 1337] |
| 1387 |
with subtests.test(msg="Callback is called after group is completed"): |
| 1388 |
await_redis_echo({callback_msg, }, redis_key=redis_key) |
| 1389 |
redis_connection.delete(redis_key) |
| 1390 |
|
| 1391 |
def test_errback_called_by_group_fail_first(self, manager, subtests): |
| 1392 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1393 |
raise pytest.skip("Requires redis result backend.") |
| 1394 |
redis_connection = get_redis_connection() |
| 1395 |
|
| 1396 |
errback_msg = str(uuid.uuid4()).encode() |
| 1397 |
redis_key = str(uuid.uuid4()) |
| 1398 |
errback = redis_echo.si(errback_msg, redis_key=redis_key) |
| 1399 |
|
| 1400 |
group_sig = group(fail.s(), identity.si(42)) |
| 1401 |
group_sig.link_error(errback) |
| 1402 |
redis_connection.delete(redis_key) |
| 1403 |
with subtests.test(msg="Error propagates from group"): |
| 1404 |
res = group_sig.delay() |
| 1405 |
with pytest.raises(ExpectedException): |
| 1406 |
res.get(timeout=TIMEOUT) |
| 1407 |
with subtests.test(msg="Errback is called after group task fails"): |
| 1408 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 1409 |
redis_connection.delete(redis_key) |
| 1410 |
|
| 1411 |
def test_errback_called_by_group_fail_last(self, manager, subtests): |
| 1412 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1413 |
raise pytest.skip("Requires redis result backend.") |
| 1414 |
redis_connection = get_redis_connection() |
| 1415 |
|
| 1416 |
errback_msg = str(uuid.uuid4()).encode() |
| 1417 |
redis_key = str(uuid.uuid4()) |
| 1418 |
errback = redis_echo.si(errback_msg, redis_key=redis_key) |
| 1419 |
|
| 1420 |
group_sig = group(identity.si(42), fail.s()) |
| 1421 |
group_sig.link_error(errback) |
| 1422 |
redis_connection.delete(redis_key) |
| 1423 |
with subtests.test(msg="Error propagates from group"): |
| 1424 |
res = group_sig.delay() |
| 1425 |
with pytest.raises(ExpectedException): |
| 1426 |
res.get(timeout=TIMEOUT) |
| 1427 |
with subtests.test(msg="Errback is called after group task fails"): |
| 1428 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 1429 |
redis_connection.delete(redis_key) |
| 1430 |
|
| 1431 |
def test_errback_called_by_group_fail_multiple(self, manager, subtests): |
| 1432 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1433 |
raise pytest.skip("Requires redis result backend.") |
| 1434 |
redis_connection = get_redis_connection() |
| 1435 |
|
| 1436 |
expected_errback_count = 42 |
| 1437 |
redis_key = str(uuid.uuid4()) |
| 1438 |
errback = redis_count.si(redis_key=redis_key) |
| 1439 |
|
| 1440 |
# Include a mix of passing and failing tasks |
| 1441 |
group_sig = group( |
| 1442 |
*(identity.si(42) for _ in range(24)), # arbitrary task count |
| 1443 |
*(fail.s() for _ in range(expected_errback_count)), |
| 1444 |
) |
| 1445 |
group_sig.link_error(errback) |
| 1446 |
|
| 1447 |
redis_connection.delete(redis_key) |
| 1448 |
with subtests.test(msg="Error propagates from group"): |
| 1449 |
res = group_sig.delay() |
| 1450 |
with pytest.raises(ExpectedException): |
| 1451 |
res.get(timeout=TIMEOUT) |
| 1452 |
with subtests.test(msg="Errback is called after group task fails"): |
| 1453 |
await_redis_count(expected_errback_count, redis_key=redis_key) |
| 1454 |
redis_connection.delete(redis_key) |
| 1455 |
|
| 1456 |
def test_group_children_with_callbacks(self, manager, subtests): |
| 1457 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1458 |
raise pytest.skip("Requires redis result backend.") |
| 1459 |
redis_connection = get_redis_connection() |
| 1460 |
|
| 1461 |
redis_key = str(uuid.uuid4()) |
| 1462 |
callback = redis_count.si(redis_key=redis_key) |
| 1463 |
|
| 1464 |
child_task_count = 42 |
| 1465 |
child_sig = identity.si(1337) |
| 1466 |
child_sig.link(callback) |
| 1467 |
group_sig = group(child_sig for _ in range(child_task_count)) |
| 1468 |
|
| 1469 |
redis_connection.delete(redis_key) |
| 1470 |
with subtests.test(msg="Chain executes as expected"): |
| 1471 |
res_obj = group_sig() |
| 1472 |
assert res_obj.get(timeout=TIMEOUT) == [1337] * child_task_count |
| 1473 |
with subtests.test(msg="Chain child task callbacks are called"): |
| 1474 |
await_redis_count(child_task_count, redis_key=redis_key) |
| 1475 |
redis_connection.delete(redis_key) |
| 1476 |
|
| 1477 |
def test_group_children_with_errbacks(self, manager, subtests): |
| 1478 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1479 |
raise pytest.skip("Requires redis result backend.") |
| 1480 |
redis_connection = get_redis_connection() |
| 1481 |
|
| 1482 |
redis_key = str(uuid.uuid4()) |
| 1483 |
errback = redis_count.si(redis_key=redis_key) |
| 1484 |
|
| 1485 |
child_task_count = 42 |
| 1486 |
child_sig = fail.si() |
| 1487 |
child_sig.link_error(errback) |
| 1488 |
group_sig = group(child_sig for _ in range(child_task_count)) |
| 1489 |
|
| 1490 |
redis_connection.delete(redis_key) |
| 1491 |
with subtests.test(msg="Chain fails due to a child task dying"): |
| 1492 |
res_obj = group_sig() |
| 1493 |
with pytest.raises(ExpectedException): |
| 1494 |
res_obj.get(timeout=TIMEOUT) |
| 1495 |
with subtests.test(msg="Chain child task errbacks are called"): |
| 1496 |
await_redis_count(child_task_count, redis_key=redis_key) |
| 1497 |
redis_connection.delete(redis_key) |
| 1498 |
|
| 1499 |
def test_group_with_callback_child_replaced(self, manager, subtests): |
| 1500 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1501 |
raise pytest.skip("Requires redis result backend.") |
| 1502 |
redis_connection = get_redis_connection() |
| 1503 |
|
| 1504 |
redis_key = str(uuid.uuid4()) |
| 1505 |
callback = redis_count.si(redis_key=redis_key) |
| 1506 |
|
| 1507 |
group_sig = group(add_replaced.si(42, 1337), identity.si(31337)) |
| 1508 |
group_sig.link(callback) |
| 1509 |
|
| 1510 |
redis_connection.delete(redis_key) |
| 1511 |
with subtests.test(msg="Chain executes as expected"): |
| 1512 |
res_obj = group_sig() |
| 1513 |
assert res_obj.get(timeout=TIMEOUT) == [42 + 1337, 31337] |
| 1514 |
with subtests.test(msg="Callback is called after group finishes"): |
| 1515 |
await_redis_count(1, redis_key=redis_key) |
| 1516 |
redis_connection.delete(redis_key) |
| 1517 |
|
| 1518 |
def test_group_with_errback_child_replaced(self, manager, subtests): |
| 1519 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1520 |
raise pytest.skip("Requires redis result backend.") |
| 1521 |
redis_connection = get_redis_connection() |
| 1522 |
|
| 1523 |
redis_key = str(uuid.uuid4()) |
| 1524 |
errback = redis_count.si(redis_key=redis_key) |
| 1525 |
|
| 1526 |
group_sig = group(add_replaced.si(42, 1337), fail.s()) |
| 1527 |
group_sig.link_error(errback) |
| 1528 |
|
| 1529 |
redis_connection.delete(redis_key) |
| 1530 |
with subtests.test(msg="Chain executes as expected"): |
| 1531 |
res_obj = group_sig() |
| 1532 |
with pytest.raises(ExpectedException): |
| 1533 |
res_obj.get(timeout=TIMEOUT) |
| 1534 |
with subtests.test(msg="Errback is called after group finishes"): |
| 1535 |
await_redis_count(1, redis_key=redis_key) |
| 1536 |
redis_connection.delete(redis_key) |
| 1537 |
|
| 1538 |
def test_group_child_with_callback_replaced(self, manager, subtests): |
| 1539 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1540 |
raise pytest.skip("Requires redis result backend.") |
| 1541 |
redis_connection = get_redis_connection() |
| 1542 |
|
| 1543 |
redis_key = str(uuid.uuid4()) |
| 1544 |
callback = redis_count.si(redis_key=redis_key) |
| 1545 |
|
| 1546 |
child_sig = add_replaced.si(42, 1337) |
| 1547 |
child_sig.link(callback) |
| 1548 |
group_sig = group(child_sig, identity.si(31337)) |
| 1549 |
|
| 1550 |
redis_connection.delete(redis_key) |
| 1551 |
with subtests.test(msg="Chain executes as expected"): |
| 1552 |
res_obj = group_sig() |
| 1553 |
assert res_obj.get(timeout=TIMEOUT) == [42 + 1337, 31337] |
| 1554 |
with subtests.test(msg="Callback is called after group finishes"): |
| 1555 |
await_redis_count(1, redis_key=redis_key) |
| 1556 |
redis_connection.delete(redis_key) |
| 1557 |
|
| 1558 |
def test_group_child_with_errback_replaced(self, manager, subtests): |
| 1559 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 1560 |
raise pytest.skip("Requires redis result backend.") |
| 1561 |
redis_connection = get_redis_connection() |
| 1562 |
|
| 1563 |
redis_key = str(uuid.uuid4()) |
| 1564 |
errback = redis_count.si(redis_key=redis_key) |
| 1565 |
|
| 1566 |
child_sig = fail_replaced.si() |
| 1567 |
child_sig.link_error(errback) |
| 1568 |
group_sig = group(child_sig, identity.si(42)) |
| 1569 |
|
| 1570 |
redis_connection.delete(redis_key) |
| 1571 |
with subtests.test(msg="Chain executes as expected"): |
| 1572 |
res_obj = group_sig() |
| 1573 |
with pytest.raises(ExpectedException): |
| 1574 |
res_obj.get(timeout=TIMEOUT) |
| 1575 |
with subtests.test(msg="Errback is called after group finishes"): |
| 1576 |
await_redis_count(1, redis_key=redis_key) |
| 1577 |
redis_connection.delete(redis_key) |
| 1578 |
|
| 1579 |
@pytest.mark.xfail(raises=TimeoutError, |
| 1580 |
reason="Task is timeout instead of returning exception on rpc backend", |
| 1581 |
strict=False) |
| 1582 |
def test_group_child_replaced_with_chain_first(self, manager): |
| 1583 |
orig_sig = group(replace_with_chain.si(42), identity.s(1337)) |
| 1584 |
res_obj = orig_sig.delay() |
| 1585 |
assert res_obj.get(timeout=TIMEOUT) == [42, 1337] |
| 1586 |
|
| 1587 |
@pytest.mark.xfail(raises=TimeoutError, |
| 1588 |
reason="Task is timeout instead of returning exception on rpc backend", |
| 1589 |
strict=False) |
| 1590 |
def test_group_child_replaced_with_chain_middle(self, manager): |
| 1591 |
orig_sig = group( |
| 1592 |
identity.s(42), replace_with_chain.s(1337), identity.s(31337) |
| 1593 |
) |
| 1594 |
res_obj = orig_sig.delay() |
| 1595 |
assert res_obj.get(timeout=TIMEOUT) == [42, 1337, 31337] |
| 1596 |
|
| 1597 |
@pytest.mark.xfail(raises=TimeoutError, |
| 1598 |
reason="Task is timeout instead of returning exception on rpc backend", |
| 1599 |
strict=False) |
| 1600 |
def test_group_child_replaced_with_chain_last(self, manager): |
| 1601 |
orig_sig = group(identity.s(42), replace_with_chain.s(1337)) |
| 1602 |
res_obj = orig_sig.delay() |
| 1603 |
assert res_obj.get(timeout=TIMEOUT) == [42, 1337] |
| 1604 |
|
| 1605 |
def test_task_replace_with_group_preserves_group_order(self, manager): |
| 1606 |
if manager.app.conf.result_backend.startswith("rpc"): |
| 1607 |
raise pytest.skip("RPC result backend does not support replacing with a group") |
| 1608 |
orig_sig = group([add_to_all.s([2, 1], 1), add_to_all.s([4, 3], 1)] * 10) |
| 1609 |
res_obj = orig_sig.delay() |
| 1610 |
assert res_obj.get(timeout=TIMEOUT) == [[3, 2], [5, 4]] * 10 |
| 1611 |
|
| 1612 |
|
| 1613 |
def assert_ids(r, expected_value, expected_root_id, expected_parent_id): |
| 1614 |
root_id, parent_id, value = r.get(timeout=TIMEOUT) |
| 1615 |
assert expected_value == value |
| 1616 |
assert root_id == expected_root_id |
| 1617 |
assert parent_id == expected_parent_id |
| 1618 |
|
| 1619 |
|
| 1620 |
def assert_ping(manager): |
| 1621 |
ping_result = manager.inspect().ping() |
| 1622 |
assert ping_result |
| 1623 |
ping_val = list(ping_result.values())[0] |
| 1624 |
assert ping_val == {"ok": "pong"} |
| 1625 |
|
| 1626 |
|
| 1627 |
class test_chord: |
| 1628 |
@flaky |
| 1629 |
def test_simple_chord_with_a_delay_in_group_save(self, manager, monkeypatch): |
| 1630 |
try: |
| 1631 |
manager.app.backend.ensure_chords_allowed() |
| 1632 |
except NotImplementedError as e: |
| 1633 |
raise pytest.skip(e.args[0]) |
| 1634 |
|
| 1635 |
if not isinstance(manager.app.backend, BaseKeyValueStoreBackend): |
| 1636 |
raise pytest.skip("The delay may only occur in the cache backend") |
| 1637 |
|
| 1638 |
x = BaseKeyValueStoreBackend._apply_chord_incr |
| 1639 |
|
| 1640 |
def apply_chord_incr_with_sleep(self, *args, **kwargs): |
| 1641 |
sleep(1) |
| 1642 |
x(self, *args, **kwargs) |
| 1643 |
|
| 1644 |
monkeypatch.setattr(BaseKeyValueStoreBackend, |
| 1645 |
'_apply_chord_incr', |
| 1646 |
apply_chord_incr_with_sleep) |
| 1647 |
|
| 1648 |
c = chord(header=[add.si(1, 1), add.si(1, 1)], body=tsum.s()) |
| 1649 |
|
| 1650 |
result = c() |
| 1651 |
assert result.get(timeout=TIMEOUT) == 4 |
| 1652 |
|
| 1653 |
def test_chord_order(self, manager): |
| 1654 |
try: |
| 1655 |
manager.app.backend.ensure_chords_allowed() |
| 1656 |
except NotImplementedError as e: |
| 1657 |
raise pytest.skip(e.args[0]) |
| 1658 |
|
| 1659 |
inputs = [i for i in range(10)] |
| 1660 |
|
| 1661 |
c = chord((identity.si(i) for i in inputs), identity.s()) |
| 1662 |
result = c() |
| 1663 |
assert result.get() == inputs |
| 1664 |
|
| 1665 |
@pytest.mark.xfail(reason="async_results aren't performed in async way") |
| 1666 |
def test_redis_subscribed_channels_leak(self, manager): |
| 1667 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1668 |
raise pytest.skip('Requires redis result backend.') |
| 1669 |
|
| 1670 |
manager.app.backend.result_consumer.on_after_fork() |
| 1671 |
initial_channels = get_active_redis_channels() |
| 1672 |
initial_channels_count = len(initial_channels) |
| 1673 |
total_chords = 10 |
| 1674 |
async_results = [ |
| 1675 |
chord([add.s(5, 6), add.s(6, 7)])(delayed_sum.s()) |
| 1676 |
for _ in range(total_chords) |
| 1677 |
] |
| 1678 |
|
| 1679 |
channels_before = get_active_redis_channels() |
| 1680 |
manager.assert_result_tasks_in_progress_or_completed(async_results) |
| 1681 |
|
| 1682 |
channels_before_count = len(channels_before) |
| 1683 |
assert set(channels_before) != set(initial_channels) |
| 1684 |
assert channels_before_count > initial_channels_count |
| 1685 |
|
| 1686 |
# The total number of active Redis channels at this point |
| 1687 |
# is the number of chord header tasks multiplied by the |
| 1688 |
# total chord tasks, plus the initial channels |
| 1689 |
# (existing from previous tests). |
| 1690 |
chord_header_task_count = 2 |
| 1691 |
assert channels_before_count <= \ |
| 1692 |
chord_header_task_count * total_chords + initial_channels_count |
| 1693 |
|
| 1694 |
result_values = [ |
| 1695 |
result.get(timeout=TIMEOUT) |
| 1696 |
for result in async_results |
| 1697 |
] |
| 1698 |
assert result_values == [24] * total_chords |
| 1699 |
|
| 1700 |
channels_after = get_active_redis_channels() |
| 1701 |
channels_after_count = len(channels_after) |
| 1702 |
|
| 1703 |
assert channels_after_count == initial_channels_count |
| 1704 |
assert set(channels_after) == set(initial_channels) |
| 1705 |
|
| 1706 |
@flaky |
| 1707 |
def test_replaced_nested_chord(self, manager): |
| 1708 |
try: |
| 1709 |
manager.app.backend.ensure_chords_allowed() |
| 1710 |
except NotImplementedError as e: |
| 1711 |
raise pytest.skip(e.args[0]) |
| 1712 |
|
| 1713 |
c1 = chord([ |
| 1714 |
chord( |
| 1715 |
[add.s(1, 2), add_replaced.s(3, 4)], |
| 1716 |
add_to_all.s(5), |
| 1717 |
) | tsum.s(), |
| 1718 |
chord( |
| 1719 |
[add_replaced.s(6, 7), add.s(0, 0)], |
| 1720 |
add_to_all.s(8), |
| 1721 |
) | tsum.s(), |
| 1722 |
], add_to_all.s(9)) |
| 1723 |
res1 = c1() |
| 1724 |
assert res1.get(timeout=TIMEOUT) == [29, 38] |
| 1725 |
|
| 1726 |
@flaky |
| 1727 |
def test_add_to_chord(self, manager): |
| 1728 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1729 |
raise pytest.skip('Requires redis result backend.') |
| 1730 |
|
| 1731 |
c = group([add_to_all_to_chord.s([1, 2, 3], 4)]) | identity.s() |
| 1732 |
res = c() |
| 1733 |
assert sorted(res.get()) == [0, 5, 6, 7] |
| 1734 |
|
| 1735 |
@flaky |
| 1736 |
def test_add_chord_to_chord(self, manager): |
| 1737 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1738 |
raise pytest.skip('Requires redis result backend.') |
| 1739 |
|
| 1740 |
c = group([add_chord_to_chord.s([1, 2, 3], 4)]) | identity.s() |
| 1741 |
res = c() |
| 1742 |
assert sorted(res.get()) == [0, 5 + 6 + 7] |
| 1743 |
|
| 1744 |
@flaky |
| 1745 |
def test_eager_chord_inside_task(self, manager): |
| 1746 |
from .tasks import chord_add |
| 1747 |
|
| 1748 |
prev = chord_add.app.conf.task_always_eager |
| 1749 |
chord_add.app.conf.task_always_eager = True |
| 1750 |
|
| 1751 |
chord_add.apply_async(args=(4, 8), throw=True).get() |
| 1752 |
|
| 1753 |
chord_add.app.conf.task_always_eager = prev |
| 1754 |
|
| 1755 |
def test_group_chain(self, manager): |
| 1756 |
try: |
| 1757 |
manager.app.backend.ensure_chords_allowed() |
| 1758 |
except NotImplementedError as e: |
| 1759 |
raise pytest.skip(e.args[0]) |
| 1760 |
|
| 1761 |
c = ( |
| 1762 |
add.s(2, 2) | |
| 1763 |
group(add.s(i) for i in range(4)) | |
| 1764 |
add_to_all.s(8) |
| 1765 |
) |
| 1766 |
res = c() |
| 1767 |
assert res.get(timeout=TIMEOUT) == [12, 13, 14, 15] |
| 1768 |
|
| 1769 |
def test_group_kwargs(self, manager): |
| 1770 |
try: |
| 1771 |
manager.app.backend.ensure_chords_allowed() |
| 1772 |
except NotImplementedError as e: |
| 1773 |
raise pytest.skip(e.args[0]) |
| 1774 |
c = ( |
| 1775 |
add.s(2, 2) | |
| 1776 |
group(add.s(i) for i in range(4)) | |
| 1777 |
add_to_all.s(8) |
| 1778 |
) |
| 1779 |
res = c.apply_async(kwargs={"z": 1}) |
| 1780 |
assert res.get(timeout=TIMEOUT) == [13, 14, 15, 16] |
| 1781 |
|
| 1782 |
def test_group_args_and_kwargs(self, manager): |
| 1783 |
try: |
| 1784 |
manager.app.backend.ensure_chords_allowed() |
| 1785 |
except NotImplementedError as e: |
| 1786 |
raise pytest.skip(e.args[0]) |
| 1787 |
c = ( |
| 1788 |
group(add.s(i) for i in range(4)) | |
| 1789 |
add_to_all.s(8) |
| 1790 |
) |
| 1791 |
res = c.apply_async(args=(4,), kwargs={"z": 1}) |
| 1792 |
if manager.app.conf.result_backend.startswith('redis'): |
| 1793 |
# for a simple chord like the one above, redis does not guarantee |
| 1794 |
# the ordering of the results as a performance trade off. |
| 1795 |
assert set(res.get(timeout=TIMEOUT)) == {13, 14, 15, 16} |
| 1796 |
else: |
| 1797 |
assert res.get(timeout=TIMEOUT) == [13, 14, 15, 16] |
| 1798 |
|
| 1799 |
def test_nested_group_chain(self, manager): |
| 1800 |
try: |
| 1801 |
manager.app.backend.ensure_chords_allowed() |
| 1802 |
except NotImplementedError as e: |
| 1803 |
raise pytest.skip(e.args[0]) |
| 1804 |
|
| 1805 |
c = chain( |
| 1806 |
add.si(1, 0), |
| 1807 |
group( |
| 1808 |
add.si(1, 100), |
| 1809 |
chain( |
| 1810 |
add.si(1, 200), |
| 1811 |
group( |
| 1812 |
add.si(1, 1000), |
| 1813 |
add.si(1, 2000), |
| 1814 |
), |
| 1815 |
), |
| 1816 |
), |
| 1817 |
add.si(1, 10), |
| 1818 |
) |
| 1819 |
res = c() |
| 1820 |
assert res.get(timeout=TIMEOUT) == 11 |
| 1821 |
|
| 1822 |
@flaky |
| 1823 |
def test_single_task_header(self, manager): |
| 1824 |
try: |
| 1825 |
manager.app.backend.ensure_chords_allowed() |
| 1826 |
except NotImplementedError as e: |
| 1827 |
raise pytest.skip(e.args[0]) |
| 1828 |
|
| 1829 |
c1 = chord([add.s(2, 5)], body=add_to_all.s(9)) |
| 1830 |
res1 = c1() |
| 1831 |
assert res1.get(timeout=TIMEOUT) == [16] |
| 1832 |
|
| 1833 |
c2 = group([add.s(2, 5)]) | add_to_all.s(9) |
| 1834 |
res2 = c2() |
| 1835 |
assert res2.get(timeout=TIMEOUT) == [16] |
| 1836 |
|
| 1837 |
def test_empty_header_chord(self, manager): |
| 1838 |
try: |
| 1839 |
manager.app.backend.ensure_chords_allowed() |
| 1840 |
except NotImplementedError as e: |
| 1841 |
raise pytest.skip(e.args[0]) |
| 1842 |
|
| 1843 |
c1 = chord([], body=add_to_all.s(9)) |
| 1844 |
res1 = c1() |
| 1845 |
assert res1.get(timeout=TIMEOUT) == [] |
| 1846 |
|
| 1847 |
c2 = group([]) | add_to_all.s(9) |
| 1848 |
res2 = c2() |
| 1849 |
assert res2.get(timeout=TIMEOUT) == [] |
| 1850 |
|
| 1851 |
@flaky |
| 1852 |
def test_nested_chord(self, manager): |
| 1853 |
try: |
| 1854 |
manager.app.backend.ensure_chords_allowed() |
| 1855 |
except NotImplementedError as e: |
| 1856 |
raise pytest.skip(e.args[0]) |
| 1857 |
|
| 1858 |
c1 = chord([ |
| 1859 |
chord([add.s(1, 2), add.s(3, 4)], add.s([5])), |
| 1860 |
chord([add.s(6, 7)], add.s([10])) |
| 1861 |
], add_to_all.s(['A'])) |
| 1862 |
res1 = c1() |
| 1863 |
assert res1.get(timeout=TIMEOUT) == [[3, 7, 5, 'A'], [13, 10, 'A']] |
| 1864 |
|
| 1865 |
c2 = group([ |
| 1866 |
group([add.s(1, 2), add.s(3, 4)]) | add.s([5]), |
| 1867 |
group([add.s(6, 7)]) | add.s([10]), |
| 1868 |
]) | add_to_all.s(['A']) |
| 1869 |
res2 = c2() |
| 1870 |
assert res2.get(timeout=TIMEOUT) == [[3, 7, 5, 'A'], [13, 10, 'A']] |
| 1871 |
|
| 1872 |
c = group([ |
| 1873 |
group([ |
| 1874 |
group([ |
| 1875 |
group([ |
| 1876 |
add.s(1, 2) |
| 1877 |
]) | add.s([3]) |
| 1878 |
]) | add.s([4]) |
| 1879 |
]) | add.s([5]) |
| 1880 |
]) | add.s([6]) |
| 1881 |
|
| 1882 |
res = c() |
| 1883 |
assert [[[[3, 3], 4], 5], 6] == res.get(timeout=TIMEOUT) |
| 1884 |
|
| 1885 |
@flaky |
| 1886 |
def test_parent_ids(self, manager): |
| 1887 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1888 |
raise pytest.skip('Requires redis result backend.') |
| 1889 |
root = ids.si(i=1) |
| 1890 |
expected_root_id = root.freeze().id |
| 1891 |
g = chain( |
| 1892 |
root, ids.si(i=2), |
| 1893 |
chord( |
| 1894 |
group(ids.si(i=i) for i in range(3, 50)), |
| 1895 |
chain(collect_ids.s(i=50) | ids.si(i=51)), |
| 1896 |
), |
| 1897 |
) |
| 1898 |
self.assert_parentids_chord(g(), expected_root_id) |
| 1899 |
|
| 1900 |
@flaky |
| 1901 |
def test_parent_ids__OR(self, manager): |
| 1902 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1903 |
raise pytest.skip('Requires redis result backend.') |
| 1904 |
root = ids.si(i=1) |
| 1905 |
expected_root_id = root.freeze().id |
| 1906 |
g = ( |
| 1907 |
root | |
| 1908 |
ids.si(i=2) | |
| 1909 |
group(ids.si(i=i) for i in range(3, 50)) | |
| 1910 |
collect_ids.s(i=50) | |
| 1911 |
ids.si(i=51) |
| 1912 |
) |
| 1913 |
self.assert_parentids_chord(g(), expected_root_id) |
| 1914 |
|
| 1915 |
def assert_parentids_chord(self, res, expected_root_id): |
| 1916 |
assert isinstance(res, AsyncResult) |
| 1917 |
assert isinstance(res.parent, AsyncResult) |
| 1918 |
assert isinstance(res.parent.parent, GroupResult) |
| 1919 |
assert isinstance(res.parent.parent.parent, AsyncResult) |
| 1920 |
assert isinstance(res.parent.parent.parent.parent, AsyncResult) |
| 1921 |
|
| 1922 |
# first we check the last task |
| 1923 |
assert_ids(res, 51, expected_root_id, res.parent.id) |
| 1924 |
|
| 1925 |
# then the chord callback |
| 1926 |
prev, (root_id, parent_id, value) = res.parent.get(timeout=30) |
| 1927 |
assert value == 50 |
| 1928 |
assert root_id == expected_root_id |
| 1929 |
# started by one of the chord header tasks. |
| 1930 |
assert parent_id in res.parent.parent.results |
| 1931 |
|
| 1932 |
# check what the chord callback recorded |
| 1933 |
for i, p in enumerate(prev): |
| 1934 |
root_id, parent_id, value = p |
| 1935 |
assert root_id == expected_root_id |
| 1936 |
assert parent_id == res.parent.parent.parent.id |
| 1937 |
|
| 1938 |
# ids(i=2) |
| 1939 |
root_id, parent_id, value = res.parent.parent.parent.get(timeout=30) |
| 1940 |
assert value == 2 |
| 1941 |
assert parent_id == res.parent.parent.parent.parent.id |
| 1942 |
assert root_id == expected_root_id |
| 1943 |
|
| 1944 |
# ids(i=1) |
| 1945 |
root_id, parent_id, value = res.parent.parent.parent.parent.get( |
| 1946 |
timeout=30) |
| 1947 |
assert value == 1 |
| 1948 |
assert root_id == expected_root_id |
| 1949 |
assert parent_id is None |
| 1950 |
|
| 1951 |
def test_chord_on_error(self, manager): |
| 1952 |
from celery import states |
| 1953 |
|
| 1954 |
from .tasks import ExpectedException |
| 1955 |
|
| 1956 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 1957 |
raise pytest.skip('Requires redis result backend.') |
| 1958 |
|
| 1959 |
# Run the chord and wait for the error callback to finish. Note that |
| 1960 |
# this only works for old style callbacks since they get dispatched to |
| 1961 |
# run async while new style errbacks are called synchronously so that |
| 1962 |
# they can be passed the request object for the failing task. |
| 1963 |
c1 = chord( |
| 1964 |
header=[add.s(1, 2), add.s(3, 4), fail.s()], |
| 1965 |
body=print_unicode.s('This should not be called').on_error( |
| 1966 |
errback_old_style.s()), |
| 1967 |
) |
| 1968 |
res = c1() |
| 1969 |
with pytest.raises(ExpectedException): |
| 1970 |
res.get(propagate=True) |
| 1971 |
|
| 1972 |
# Got to wait for children to populate. |
| 1973 |
check = ( |
| 1974 |
lambda: res.children, |
| 1975 |
lambda: res.children[0].children, |
| 1976 |
lambda: res.children[0].children[0].result, |
| 1977 |
) |
| 1978 |
start = monotonic() |
| 1979 |
while not all(f() for f in check): |
| 1980 |
if monotonic() > start + TIMEOUT: |
| 1981 |
raise TimeoutError("Timed out waiting for children") |
| 1982 |
sleep(0.1) |
| 1983 |
|
| 1984 |
# Extract the results of the successful tasks from the chord. |
| 1985 |
# |
| 1986 |
# We could do this inside the error handler, and probably would in a |
| 1987 |
# real system, but for the purposes of the test it's obnoxious to get |
| 1988 |
# data out of the error handler. |
| 1989 |
# |
| 1990 |
# So for clarity of our test, we instead do it here. |
| 1991 |
|
| 1992 |
# Use the error callback's result to find the failed task. |
| 1993 |
uuid_patt = re.compile( |
| 1994 |
r"[0-9A-Fa-f]{8}-([0-9A-Fa-f]{4}-){3}[0-9A-Fa-f]{12}" |
| 1995 |
) |
| 1996 |
callback_chord_exc = AsyncResult( |
| 1997 |
res.children[0].children[0].result |
| 1998 |
).result |
| 1999 |
failed_task_id = uuid_patt.search(str(callback_chord_exc)) |
| 2000 |
assert (failed_task_id is not None), "No task ID in %r" % callback_chord_exc |
| 2001 |
failed_task_id = failed_task_id.group() |
| 2002 |
|
| 2003 |
# Use new group_id result metadata to get group ID. |
| 2004 |
failed_task_result = AsyncResult(failed_task_id) |
| 2005 |
original_group_id = failed_task_result._get_task_meta()['group_id'] |
| 2006 |
|
| 2007 |
# Use group ID to get preserved group result. |
| 2008 |
backend = fail.app.backend |
| 2009 |
j_key = backend.get_key_for_group(original_group_id, '.j') |
| 2010 |
redis_connection = get_redis_connection() |
| 2011 |
# The redis key is either a list or a zset (a redis sorted set) depending on configuration |
| 2012 |
if manager.app.conf.result_backend_transport_options.get( |
| 2013 |
'result_chord_ordered', True |
| 2014 |
): |
| 2015 |
job_results = redis_connection.zrange(j_key, 0, 3) |
| 2016 |
else: |
| 2017 |
job_results = redis_connection.lrange(j_key, 0, 3) |
| 2018 |
chord_results = [backend.decode(t) for t in job_results] |
| 2019 |
|
| 2020 |
# Validate group result |
| 2021 |
assert [cr[3] for cr in chord_results if cr[2] == states.SUCCESS] == \ |
| 2022 |
[3, 7] |
| 2023 |
|
| 2024 |
assert len([cr for cr in chord_results if cr[2] != states.SUCCESS] |
| 2025 |
) == 1 |
| 2026 |
|
| 2027 |
@flaky |
| 2028 |
@pytest.mark.parametrize('size', [3, 4, 5, 6, 7, 8, 9]) |
| 2029 |
def test_generator(self, manager, size): |
| 2030 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 2031 |
raise pytest.skip('Requires redis result backend.') |
| 2032 |
|
| 2033 |
def assert_generator(file_name): |
| 2034 |
for i in range(size): |
| 2035 |
sleep(1) |
| 2036 |
if i == size - 1: |
| 2037 |
with open(file_name) as file_handle: |
| 2038 |
# ensures chord header generators tasks are processed incrementally #3021 |
| 2039 |
assert file_handle.readline() == '0\n', "Chord header was unrolled too early" |
| 2040 |
|
| 2041 |
yield write_to_file_and_return_int.s(file_name, i) |
| 2042 |
|
| 2043 |
with tempfile.NamedTemporaryFile(mode='w', delete=False) as tmp_file: |
| 2044 |
file_name = tmp_file.name |
| 2045 |
c = chord(assert_generator(file_name), tsum.s()) |
| 2046 |
assert c().get(timeout=TIMEOUT) == size * (size - 1) // 2 |
| 2047 |
|
| 2048 |
@flaky |
| 2049 |
def test_parallel_chords(self, manager): |
| 2050 |
try: |
| 2051 |
manager.app.backend.ensure_chords_allowed() |
| 2052 |
except NotImplementedError as e: |
| 2053 |
raise pytest.skip(e.args[0]) |
| 2054 |
|
| 2055 |
c1 = chord(group(add.s(1, 2), add.s(3, 4)), tsum.s()) |
| 2056 |
c2 = chord(group(add.s(1, 2), add.s(3, 4)), tsum.s()) |
| 2057 |
g = group(c1, c2) |
| 2058 |
r = g.delay() |
| 2059 |
|
| 2060 |
assert r.get(timeout=TIMEOUT) == [10, 10] |
| 2061 |
|
| 2062 |
@flaky |
| 2063 |
def test_chord_in_chords_with_chains(self, manager): |
| 2064 |
try: |
| 2065 |
manager.app.backend.ensure_chords_allowed() |
| 2066 |
except NotImplementedError as e: |
| 2067 |
raise pytest.skip(e.args[0]) |
| 2068 |
|
| 2069 |
c = chord( |
| 2070 |
group([ |
| 2071 |
chain( |
| 2072 |
add.si(1, 2), |
| 2073 |
chord( |
| 2074 |
group([add.si(1, 2), add.si(1, 2)]), |
| 2075 |
add.si(1, 2), |
| 2076 |
), |
| 2077 |
), |
| 2078 |
chain( |
| 2079 |
add.si(1, 2), |
| 2080 |
chord( |
| 2081 |
group([add.si(1, 2), add.si(1, 2)]), |
| 2082 |
add.si(1, 2), |
| 2083 |
), |
| 2084 |
), |
| 2085 |
]), |
| 2086 |
add.si(2, 2) |
| 2087 |
) |
| 2088 |
|
| 2089 |
r = c.delay() |
| 2090 |
|
| 2091 |
assert r.get(timeout=TIMEOUT) == 4 |
| 2092 |
|
| 2093 |
@flaky |
| 2094 |
def test_chain_chord_chain_chord(self, manager): |
| 2095 |
# test for #2573 |
| 2096 |
try: |
| 2097 |
manager.app.backend.ensure_chords_allowed() |
| 2098 |
except NotImplementedError as e: |
| 2099 |
raise pytest.skip(e.args[0]) |
| 2100 |
c = chain( |
| 2101 |
identity.si(1), |
| 2102 |
chord( |
| 2103 |
[ |
| 2104 |
identity.si(2), |
| 2105 |
chain( |
| 2106 |
identity.si(3), |
| 2107 |
chord( |
| 2108 |
[identity.si(4), identity.si(5)], |
| 2109 |
identity.si(6) |
| 2110 |
) |
| 2111 |
) |
| 2112 |
], |
| 2113 |
identity.si(7) |
| 2114 |
) |
| 2115 |
) |
| 2116 |
res = c.delay() |
| 2117 |
assert res.get(timeout=TIMEOUT) == 7 |
| 2118 |
|
| 2119 |
@pytest.mark.xfail(reason="Issue #6176") |
| 2120 |
def test_chord_in_chain_with_args(self, manager): |
| 2121 |
try: |
| 2122 |
manager.app.backend.ensure_chords_allowed() |
| 2123 |
except NotImplementedError as e: |
| 2124 |
raise pytest.skip(e.args[0]) |
| 2125 |
|
| 2126 |
c1 = chain( |
| 2127 |
chord( |
| 2128 |
[identity.s(), identity.s()], |
| 2129 |
identity.s(), |
| 2130 |
), |
| 2131 |
identity.s(), |
| 2132 |
) |
| 2133 |
res1 = c1.apply_async(args=(1,)) |
| 2134 |
assert res1.get(timeout=TIMEOUT) == [1, 1] |
| 2135 |
res1 = c1.apply(args=(1,)) |
| 2136 |
assert res1.get(timeout=TIMEOUT) == [1, 1] |
| 2137 |
|
| 2138 |
@pytest.mark.xfail(reason="Issue #6200") |
| 2139 |
def test_chain_in_chain_with_args(self, manager): |
| 2140 |
try: |
| 2141 |
manager.app.backend.ensure_chords_allowed() |
| 2142 |
except NotImplementedError as e: |
| 2143 |
raise pytest.skip(e.args[0]) |
| 2144 |
|
| 2145 |
c1 = chain( # NOTE: This chain should have only 1 chain inside it |
| 2146 |
chain( |
| 2147 |
identity.s(), |
| 2148 |
identity.s(), |
| 2149 |
), |
| 2150 |
) |
| 2151 |
|
| 2152 |
res1 = c1.apply_async(args=(1,)) |
| 2153 |
assert res1.get(timeout=TIMEOUT) == 1 |
| 2154 |
res1 = c1.apply(args=(1,)) |
| 2155 |
assert res1.get(timeout=TIMEOUT) == 1 |
| 2156 |
|
| 2157 |
@flaky |
| 2158 |
def test_large_header(self, manager): |
| 2159 |
try: |
| 2160 |
manager.app.backend.ensure_chords_allowed() |
| 2161 |
except NotImplementedError as e: |
| 2162 |
raise pytest.skip(e.args[0]) |
| 2163 |
|
| 2164 |
c = group(identity.si(i) for i in range(1000)) | tsum.s() |
| 2165 |
res = c.delay() |
| 2166 |
assert res.get(timeout=TIMEOUT) == 499500 |
| 2167 |
|
| 2168 |
@flaky |
| 2169 |
def test_chain_to_a_chord_with_large_header(self, manager): |
| 2170 |
try: |
| 2171 |
manager.app.backend.ensure_chords_allowed() |
| 2172 |
except NotImplementedError as e: |
| 2173 |
raise pytest.skip(e.args[0]) |
| 2174 |
|
| 2175 |
c = identity.si(1) | group( |
| 2176 |
identity.s() for _ in range(1000)) | tsum.s() |
| 2177 |
res = c.delay() |
| 2178 |
assert res.get(timeout=TIMEOUT) == 1000 |
| 2179 |
|
| 2180 |
@flaky |
| 2181 |
def test_priority(self, manager): |
| 2182 |
c = chain(return_priority.signature(priority=3))() |
| 2183 |
assert c.get(timeout=TIMEOUT) == "Priority: 3" |
| 2184 |
|
| 2185 |
@flaky |
| 2186 |
def test_priority_chain(self, manager): |
| 2187 |
c = return_priority.signature(priority=3) | return_priority.signature( |
| 2188 |
priority=5) |
| 2189 |
assert c().get(timeout=TIMEOUT) == "Priority: 5" |
| 2190 |
|
| 2191 |
def test_nested_chord_group(self, manager): |
| 2192 |
""" |
| 2193 |
Confirm that groups nested inside chords get unrolled. |
| 2194 |
""" |
| 2195 |
try: |
| 2196 |
manager.app.backend.ensure_chords_allowed() |
| 2197 |
except NotImplementedError as e: |
| 2198 |
raise pytest.skip(e.args[0]) |
| 2199 |
|
| 2200 |
sig = chord( |
| 2201 |
( |
| 2202 |
group(identity.s(42), identity.s(42)), # [42, 42] |
| 2203 |
), |
| 2204 |
identity.s() # [42, 42] |
| 2205 |
) |
| 2206 |
res = sig.delay() |
| 2207 |
assert res.get(timeout=TIMEOUT) == [42, 42] |
| 2208 |
|
| 2209 |
def test_nested_chord_group_chain_group_tail(self, manager): |
| 2210 |
""" |
| 2211 |
Sanity check that a deeply nested group is completed as expected. |
| 2212 |
|
| 2213 |
Groups at the end of chains nested in chords have had issues and this |
| 2214 |
simple test sanity check that such a task structure can be completed. |
| 2215 |
""" |
| 2216 |
try: |
| 2217 |
manager.app.backend.ensure_chords_allowed() |
| 2218 |
except NotImplementedError as e: |
| 2219 |
raise pytest.skip(e.args[0]) |
| 2220 |
|
| 2221 |
sig = chord( |
| 2222 |
group( |
| 2223 |
chain( |
| 2224 |
identity.s(42), # 42 |
| 2225 |
group( |
| 2226 |
identity.s(), # 42 |
| 2227 |
identity.s(), # 42 |
| 2228 |
), # [42, 42] |
| 2229 |
), # [42, 42] |
| 2230 |
), # [[42, 42]] since the chain prevents unrolling |
| 2231 |
identity.s(), # [[42, 42]] |
| 2232 |
) |
| 2233 |
res = sig.delay() |
| 2234 |
assert res.get(timeout=TIMEOUT) == [[42, 42]] |
| 2235 |
|
| 2236 |
@pytest.mark.xfail(TEST_BACKEND.startswith('redis://'), reason="Issue #6437") |
| 2237 |
def test_error_propagates_from_chord(self, manager): |
| 2238 |
try: |
| 2239 |
manager.app.backend.ensure_chords_allowed() |
| 2240 |
except NotImplementedError as e: |
| 2241 |
raise pytest.skip(e.args[0]) |
| 2242 |
|
| 2243 |
sig = add.s(1, 1) | fail.s() | group(add.s(1), add.s(1)) |
| 2244 |
res = sig.delay() |
| 2245 |
|
| 2246 |
with pytest.raises(ExpectedException): |
| 2247 |
res.get(timeout=TIMEOUT) |
| 2248 |
|
| 2249 |
def test_error_propagates_from_chord2(self, manager): |
| 2250 |
try: |
| 2251 |
manager.app.backend.ensure_chords_allowed() |
| 2252 |
except NotImplementedError as e: |
| 2253 |
raise pytest.skip(e.args[0]) |
| 2254 |
|
| 2255 |
sig = add.s(1, 1) | add.s(1) | group(add.s(1), fail.s()) |
| 2256 |
res = sig.delay() |
| 2257 |
|
| 2258 |
with pytest.raises(ExpectedException): |
| 2259 |
res.get(timeout=TIMEOUT) |
| 2260 |
|
| 2261 |
def test_error_propagates_to_chord_from_simple(self, manager, subtests): |
| 2262 |
try: |
| 2263 |
manager.app.backend.ensure_chords_allowed() |
| 2264 |
except NotImplementedError as e: |
| 2265 |
raise pytest.skip(e.args[0]) |
| 2266 |
|
| 2267 |
child_sig = fail.s() |
| 2268 |
|
| 2269 |
chord_sig = chord((child_sig,), identity.s()) |
| 2270 |
with subtests.test(msg="Error propagates from simple header task"): |
| 2271 |
res = chord_sig.delay() |
| 2272 |
with pytest.raises(ExpectedException): |
| 2273 |
res.get(timeout=TIMEOUT) |
| 2274 |
|
| 2275 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2276 |
with subtests.test(msg="Error propagates from simple body task"): |
| 2277 |
res = chord_sig.delay() |
| 2278 |
with pytest.raises(ExpectedException): |
| 2279 |
res.get(timeout=TIMEOUT) |
| 2280 |
|
| 2281 |
def test_immutable_errback_called_by_chord_from_simple( |
| 2282 |
self, manager, subtests |
| 2283 |
): |
| 2284 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2285 |
raise pytest.skip("Requires redis result backend.") |
| 2286 |
redis_connection = get_redis_connection() |
| 2287 |
|
| 2288 |
errback_msg = str(uuid.uuid4()).encode() |
| 2289 |
redis_key = str(uuid.uuid4()) |
| 2290 |
errback = redis_echo.si(errback_msg, redis_key=redis_key) |
| 2291 |
child_sig = fail.s() |
| 2292 |
|
| 2293 |
chord_sig = chord((child_sig,), identity.s()) |
| 2294 |
chord_sig.link_error(errback) |
| 2295 |
redis_connection.delete(redis_key) |
| 2296 |
with subtests.test(msg="Error propagates from simple header task"): |
| 2297 |
res = chord_sig.delay() |
| 2298 |
with pytest.raises(ExpectedException): |
| 2299 |
res.get(timeout=TIMEOUT) |
| 2300 |
with subtests.test( |
| 2301 |
msg="Errback is called after simple header task fails" |
| 2302 |
): |
| 2303 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 2304 |
|
| 2305 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2306 |
chord_sig.link_error(errback) |
| 2307 |
redis_connection.delete(redis_key) |
| 2308 |
with subtests.test(msg="Error propagates from simple body task"): |
| 2309 |
res = chord_sig.delay() |
| 2310 |
with pytest.raises(ExpectedException): |
| 2311 |
res.get(timeout=TIMEOUT) |
| 2312 |
with subtests.test( |
| 2313 |
msg="Errback is called after simple body task fails" |
| 2314 |
): |
| 2315 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 2316 |
redis_connection.delete(redis_key) |
| 2317 |
|
| 2318 |
@pytest.mark.parametrize( |
| 2319 |
"errback_task", [errback_old_style, errback_new_style, ], |
| 2320 |
) |
| 2321 |
def test_mutable_errback_called_by_chord_from_simple( |
| 2322 |
self, errback_task, manager, subtests |
| 2323 |
): |
| 2324 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2325 |
raise pytest.skip("Requires redis result backend.") |
| 2326 |
redis_connection = get_redis_connection() |
| 2327 |
|
| 2328 |
errback = errback_task.s() |
| 2329 |
child_sig = fail.s() |
| 2330 |
|
| 2331 |
chord_sig = chord((child_sig,), identity.s()) |
| 2332 |
chord_sig.link_error(errback) |
| 2333 |
expected_redis_key = chord_sig.body.freeze().id |
| 2334 |
redis_connection.delete(expected_redis_key) |
| 2335 |
with subtests.test(msg="Error propagates from simple header task"): |
| 2336 |
res = chord_sig.delay() |
| 2337 |
with pytest.raises(ExpectedException): |
| 2338 |
res.get(timeout=TIMEOUT) |
| 2339 |
with subtests.test( |
| 2340 |
msg="Errback is called after simple header task fails" |
| 2341 |
): |
| 2342 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2343 |
|
| 2344 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2345 |
chord_sig.link_error(errback) |
| 2346 |
expected_redis_key = chord_sig.body.freeze().id |
| 2347 |
redis_connection.delete(expected_redis_key) |
| 2348 |
with subtests.test(msg="Error propagates from simple body task"): |
| 2349 |
res = chord_sig.delay() |
| 2350 |
with pytest.raises(ExpectedException): |
| 2351 |
res.get(timeout=TIMEOUT) |
| 2352 |
with subtests.test( |
| 2353 |
msg="Errback is called after simple body task fails" |
| 2354 |
): |
| 2355 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2356 |
redis_connection.delete(expected_redis_key) |
| 2357 |
|
| 2358 |
def test_error_propagates_to_chord_from_chain(self, manager, subtests): |
| 2359 |
try: |
| 2360 |
manager.app.backend.ensure_chords_allowed() |
| 2361 |
except NotImplementedError as e: |
| 2362 |
raise pytest.skip(e.args[0]) |
| 2363 |
|
| 2364 |
child_sig = chain(identity.si(42), fail.s(), identity.si(42)) |
| 2365 |
|
| 2366 |
chord_sig = chord((child_sig,), identity.s()) |
| 2367 |
with subtests.test( |
| 2368 |
msg="Error propagates from header chain which fails before the end" |
| 2369 |
): |
| 2370 |
res = chord_sig.delay() |
| 2371 |
with pytest.raises(ExpectedException): |
| 2372 |
res.get(timeout=TIMEOUT) |
| 2373 |
|
| 2374 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2375 |
with subtests.test( |
| 2376 |
msg="Error propagates from body chain which fails before the end" |
| 2377 |
): |
| 2378 |
res = chord_sig.delay() |
| 2379 |
with pytest.raises(ExpectedException): |
| 2380 |
res.get(timeout=TIMEOUT) |
| 2381 |
|
| 2382 |
def test_immutable_errback_called_by_chord_from_chain( |
| 2383 |
self, manager, subtests |
| 2384 |
): |
| 2385 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2386 |
raise pytest.skip("Requires redis result backend.") |
| 2387 |
redis_connection = get_redis_connection() |
| 2388 |
|
| 2389 |
errback_msg = str(uuid.uuid4()).encode() |
| 2390 |
redis_key = str(uuid.uuid4()) |
| 2391 |
errback = redis_echo.si(errback_msg, redis_key=redis_key) |
| 2392 |
child_sig = chain(identity.si(42), fail.s(), identity.si(42)) |
| 2393 |
|
| 2394 |
chord_sig = chord((child_sig,), identity.s()) |
| 2395 |
chord_sig.link_error(errback) |
| 2396 |
redis_connection.delete(redis_key) |
| 2397 |
with subtests.test( |
| 2398 |
msg="Error propagates from header chain which fails before the end" |
| 2399 |
): |
| 2400 |
res = chord_sig.delay() |
| 2401 |
with pytest.raises(ExpectedException): |
| 2402 |
res.get(timeout=TIMEOUT) |
| 2403 |
with subtests.test( |
| 2404 |
msg="Errback is called after header chain which fails before the end" |
| 2405 |
): |
| 2406 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 2407 |
|
| 2408 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2409 |
chord_sig.link_error(errback) |
| 2410 |
redis_connection.delete(redis_key) |
| 2411 |
with subtests.test( |
| 2412 |
msg="Error propagates from body chain which fails before the end" |
| 2413 |
): |
| 2414 |
res = chord_sig.delay() |
| 2415 |
with pytest.raises(ExpectedException): |
| 2416 |
res.get(timeout=TIMEOUT) |
| 2417 |
with subtests.test( |
| 2418 |
msg="Errback is called after body chain which fails before the end" |
| 2419 |
): |
| 2420 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 2421 |
redis_connection.delete(redis_key) |
| 2422 |
|
| 2423 |
@pytest.mark.parametrize( |
| 2424 |
"errback_task", [errback_old_style, errback_new_style, ], |
| 2425 |
) |
| 2426 |
def test_mutable_errback_called_by_chord_from_chain( |
| 2427 |
self, errback_task, manager, subtests |
| 2428 |
): |
| 2429 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2430 |
raise pytest.skip("Requires redis result backend.") |
| 2431 |
redis_connection = get_redis_connection() |
| 2432 |
|
| 2433 |
errback = errback_task.s() |
| 2434 |
fail_sig = fail.s() |
| 2435 |
fail_sig_id = fail_sig.freeze().id |
| 2436 |
child_sig = chain(identity.si(42), fail_sig, identity.si(42)) |
| 2437 |
|
| 2438 |
chord_sig = chord((child_sig,), identity.s()) |
| 2439 |
chord_sig.link_error(errback) |
| 2440 |
expected_redis_key = chord_sig.body.freeze().id |
| 2441 |
redis_connection.delete(expected_redis_key) |
| 2442 |
with subtests.test( |
| 2443 |
msg="Error propagates from header chain which fails before the end" |
| 2444 |
): |
| 2445 |
res = chord_sig.delay() |
| 2446 |
with pytest.raises(ExpectedException): |
| 2447 |
res.get(timeout=TIMEOUT) |
| 2448 |
with subtests.test( |
| 2449 |
msg="Errback is called after header chain which fails before the end" |
| 2450 |
): |
| 2451 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2452 |
|
| 2453 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2454 |
chord_sig.link_error(errback) |
| 2455 |
expected_redis_key = fail_sig_id |
| 2456 |
redis_connection.delete(expected_redis_key) |
| 2457 |
with subtests.test( |
| 2458 |
msg="Error propagates from body chain which fails before the end" |
| 2459 |
): |
| 2460 |
res = chord_sig.delay() |
| 2461 |
with pytest.raises(ExpectedException): |
| 2462 |
res.get(timeout=TIMEOUT) |
| 2463 |
with subtests.test( |
| 2464 |
msg="Errback is called after body chain which fails before the end" |
| 2465 |
): |
| 2466 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2467 |
redis_connection.delete(expected_redis_key) |
| 2468 |
|
| 2469 |
def test_error_propagates_to_chord_from_chain_tail(self, manager, subtests): |
| 2470 |
try: |
| 2471 |
manager.app.backend.ensure_chords_allowed() |
| 2472 |
except NotImplementedError as e: |
| 2473 |
raise pytest.skip(e.args[0]) |
| 2474 |
|
| 2475 |
child_sig = chain(identity.si(42), fail.s()) |
| 2476 |
|
| 2477 |
chord_sig = chord((child_sig,), identity.s()) |
| 2478 |
with subtests.test( |
| 2479 |
msg="Error propagates from header chain which fails at the end" |
| 2480 |
): |
| 2481 |
res = chord_sig.delay() |
| 2482 |
with pytest.raises(ExpectedException): |
| 2483 |
res.get(timeout=TIMEOUT) |
| 2484 |
|
| 2485 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2486 |
with subtests.test( |
| 2487 |
msg="Error propagates from body chain which fails at the end" |
| 2488 |
): |
| 2489 |
res = chord_sig.delay() |
| 2490 |
with pytest.raises(ExpectedException): |
| 2491 |
res.get(timeout=TIMEOUT) |
| 2492 |
|
| 2493 |
def test_immutable_errback_called_by_chord_from_chain_tail( |
| 2494 |
self, manager, subtests |
| 2495 |
): |
| 2496 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2497 |
raise pytest.skip("Requires redis result backend.") |
| 2498 |
redis_connection = get_redis_connection() |
| 2499 |
|
| 2500 |
errback_msg = str(uuid.uuid4()).encode() |
| 2501 |
redis_key = str(uuid.uuid4()) |
| 2502 |
errback = redis_echo.si(errback_msg, redis_key=redis_key) |
| 2503 |
child_sig = chain(identity.si(42), fail.s()) |
| 2504 |
|
| 2505 |
chord_sig = chord((child_sig,), identity.s()) |
| 2506 |
chord_sig.link_error(errback) |
| 2507 |
redis_connection.delete(redis_key) |
| 2508 |
with subtests.test( |
| 2509 |
msg="Error propagates from header chain which fails at the end" |
| 2510 |
): |
| 2511 |
res = chord_sig.delay() |
| 2512 |
with pytest.raises(ExpectedException): |
| 2513 |
res.get(timeout=TIMEOUT) |
| 2514 |
with subtests.test( |
| 2515 |
msg="Errback is called after header chain which fails at the end" |
| 2516 |
): |
| 2517 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 2518 |
|
| 2519 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2520 |
chord_sig.link_error(errback) |
| 2521 |
redis_connection.delete(redis_key) |
| 2522 |
with subtests.test( |
| 2523 |
msg="Error propagates from body chain which fails at the end" |
| 2524 |
): |
| 2525 |
res = chord_sig.delay() |
| 2526 |
with pytest.raises(ExpectedException): |
| 2527 |
res.get(timeout=TIMEOUT) |
| 2528 |
with subtests.test( |
| 2529 |
msg="Errback is called after body chain which fails at the end" |
| 2530 |
): |
| 2531 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 2532 |
redis_connection.delete(redis_key) |
| 2533 |
|
| 2534 |
@pytest.mark.parametrize( |
| 2535 |
"errback_task", [errback_old_style, errback_new_style, ], |
| 2536 |
) |
| 2537 |
def test_mutable_errback_called_by_chord_from_chain_tail( |
| 2538 |
self, errback_task, manager, subtests |
| 2539 |
): |
| 2540 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2541 |
raise pytest.skip("Requires redis result backend.") |
| 2542 |
redis_connection = get_redis_connection() |
| 2543 |
|
| 2544 |
errback = errback_task.s() |
| 2545 |
fail_sig = fail.s() |
| 2546 |
fail_sig_id = fail_sig.freeze().id |
| 2547 |
child_sig = chain(identity.si(42), fail_sig) |
| 2548 |
|
| 2549 |
chord_sig = chord((child_sig,), identity.s()) |
| 2550 |
chord_sig.link_error(errback) |
| 2551 |
expected_redis_key = chord_sig.body.freeze().id |
| 2552 |
redis_connection.delete(expected_redis_key) |
| 2553 |
with subtests.test( |
| 2554 |
msg="Error propagates from header chain which fails at the end" |
| 2555 |
): |
| 2556 |
res = chord_sig.delay() |
| 2557 |
with pytest.raises(ExpectedException): |
| 2558 |
res.get(timeout=TIMEOUT) |
| 2559 |
with subtests.test( |
| 2560 |
msg="Errback is called after header chain which fails at the end" |
| 2561 |
): |
| 2562 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2563 |
|
| 2564 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2565 |
chord_sig.link_error(errback) |
| 2566 |
expected_redis_key = fail_sig_id |
| 2567 |
redis_connection.delete(expected_redis_key) |
| 2568 |
with subtests.test( |
| 2569 |
msg="Error propagates from header chain which fails at the end" |
| 2570 |
): |
| 2571 |
res = chord_sig.delay() |
| 2572 |
with pytest.raises(ExpectedException): |
| 2573 |
res.get(timeout=TIMEOUT) |
| 2574 |
with subtests.test( |
| 2575 |
msg="Errback is called after header chain which fails at the end" |
| 2576 |
): |
| 2577 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2578 |
redis_connection.delete(expected_redis_key) |
| 2579 |
|
| 2580 |
def test_error_propagates_to_chord_from_group(self, manager, subtests): |
| 2581 |
try: |
| 2582 |
manager.app.backend.ensure_chords_allowed() |
| 2583 |
except NotImplementedError as e: |
| 2584 |
raise pytest.skip(e.args[0]) |
| 2585 |
|
| 2586 |
child_sig = group(identity.si(42), fail.s()) |
| 2587 |
|
| 2588 |
chord_sig = chord((child_sig,), identity.s()) |
| 2589 |
with subtests.test(msg="Error propagates from header group"): |
| 2590 |
res = chord_sig.delay() |
| 2591 |
with pytest.raises(ExpectedException): |
| 2592 |
res.get(timeout=TIMEOUT) |
| 2593 |
|
| 2594 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2595 |
with subtests.test(msg="Error propagates from body group"): |
| 2596 |
res = chord_sig.delay() |
| 2597 |
with pytest.raises(ExpectedException): |
| 2598 |
res.get(timeout=TIMEOUT) |
| 2599 |
|
| 2600 |
def test_immutable_errback_called_by_chord_from_group( |
| 2601 |
self, manager, subtests |
| 2602 |
): |
| 2603 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2604 |
raise pytest.skip("Requires redis result backend.") |
| 2605 |
redis_connection = get_redis_connection() |
| 2606 |
|
| 2607 |
errback_msg = str(uuid.uuid4()).encode() |
| 2608 |
redis_key = str(uuid.uuid4()) |
| 2609 |
errback = redis_echo.si(errback_msg, redis_key=redis_key) |
| 2610 |
child_sig = group(identity.si(42), fail.s()) |
| 2611 |
|
| 2612 |
chord_sig = chord((child_sig,), identity.s()) |
| 2613 |
chord_sig.link_error(errback) |
| 2614 |
redis_connection.delete(redis_key) |
| 2615 |
with subtests.test(msg="Error propagates from header group"): |
| 2616 |
res = chord_sig.delay() |
| 2617 |
with pytest.raises(ExpectedException): |
| 2618 |
res.get(timeout=TIMEOUT) |
| 2619 |
with subtests.test(msg="Errback is called after header group fails"): |
| 2620 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 2621 |
|
| 2622 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2623 |
chord_sig.link_error(errback) |
| 2624 |
redis_connection.delete(redis_key) |
| 2625 |
with subtests.test(msg="Error propagates from body group"): |
| 2626 |
res = chord_sig.delay() |
| 2627 |
with pytest.raises(ExpectedException): |
| 2628 |
res.get(timeout=TIMEOUT) |
| 2629 |
with subtests.test(msg="Errback is called after body group fails"): |
| 2630 |
await_redis_echo({errback_msg, }, redis_key=redis_key) |
| 2631 |
redis_connection.delete(redis_key) |
| 2632 |
|
| 2633 |
@flaky |
| 2634 |
@pytest.mark.parametrize( |
| 2635 |
"errback_task", [errback_old_style, errback_new_style, ], |
| 2636 |
) |
| 2637 |
def test_mutable_errback_called_by_chord_from_group( |
| 2638 |
self, errback_task, manager, subtests |
| 2639 |
): |
| 2640 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2641 |
raise pytest.skip("Requires redis result backend.") |
| 2642 |
redis_connection = get_redis_connection() |
| 2643 |
|
| 2644 |
errback = errback_task.s() |
| 2645 |
fail_sig = fail.s() |
| 2646 |
fail_sig_id = fail_sig.freeze().id |
| 2647 |
child_sig = group(identity.si(42), fail_sig) |
| 2648 |
|
| 2649 |
chord_sig = chord((child_sig,), identity.s()) |
| 2650 |
chord_sig.link_error(errback) |
| 2651 |
expected_redis_key = chord_sig.body.freeze().id |
| 2652 |
redis_connection.delete(expected_redis_key) |
| 2653 |
with subtests.test(msg="Error propagates from header group"): |
| 2654 |
res = chord_sig.delay() |
| 2655 |
with pytest.raises(ExpectedException): |
| 2656 |
res.get(timeout=TIMEOUT) |
| 2657 |
with subtests.test(msg="Errback is called after header group fails"): |
| 2658 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2659 |
|
| 2660 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2661 |
chord_sig.link_error(errback) |
| 2662 |
expected_redis_key = fail_sig_id |
| 2663 |
redis_connection.delete(expected_redis_key) |
| 2664 |
with subtests.test(msg="Error propagates from body group"): |
| 2665 |
res = chord_sig.delay() |
| 2666 |
with pytest.raises(ExpectedException): |
| 2667 |
res.get(timeout=TIMEOUT) |
| 2668 |
with subtests.test(msg="Errback is called after body group fails"): |
| 2669 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2670 |
redis_connection.delete(expected_redis_key) |
| 2671 |
|
| 2672 |
def test_immutable_errback_called_by_chord_from_group_fail_multiple( |
| 2673 |
self, manager, subtests |
| 2674 |
): |
| 2675 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2676 |
raise pytest.skip("Requires redis result backend.") |
| 2677 |
redis_connection = get_redis_connection() |
| 2678 |
|
| 2679 |
fail_task_count = 42 |
| 2680 |
redis_key = str(uuid.uuid4()) |
| 2681 |
errback = redis_count.si(redis_key=redis_key) |
| 2682 |
# Include a mix of passing and failing tasks |
| 2683 |
child_sig = group( |
| 2684 |
*(identity.si(42) for _ in range(24)), # arbitrary task count |
| 2685 |
*(fail.s() for _ in range(fail_task_count)), |
| 2686 |
) |
| 2687 |
|
| 2688 |
chord_sig = chord((child_sig,), identity.s()) |
| 2689 |
chord_sig.link_error(errback) |
| 2690 |
redis_connection.delete(redis_key) |
| 2691 |
with subtests.test(msg="Error propagates from header group"): |
| 2692 |
redis_connection.delete(redis_key) |
| 2693 |
res = chord_sig.delay() |
| 2694 |
with pytest.raises(ExpectedException): |
| 2695 |
res.get(timeout=TIMEOUT) |
| 2696 |
with subtests.test(msg="Errback is called after header group fails"): |
| 2697 |
# NOTE: Here we only expect the errback to be called once since it |
| 2698 |
# is attached to the chord body which is a single task! |
| 2699 |
await_redis_count(1, redis_key=redis_key) |
| 2700 |
|
| 2701 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2702 |
chord_sig.link_error(errback) |
| 2703 |
redis_connection.delete(redis_key) |
| 2704 |
with subtests.test(msg="Error propagates from body group"): |
| 2705 |
res = chord_sig.delay() |
| 2706 |
with pytest.raises(ExpectedException): |
| 2707 |
res.get(timeout=TIMEOUT) |
| 2708 |
with subtests.test(msg="Errback is called after body group fails"): |
| 2709 |
# NOTE: Here we expect the errback to be called once per failing |
| 2710 |
# task in the chord body since it is a group |
| 2711 |
await_redis_count(fail_task_count, redis_key=redis_key) |
| 2712 |
redis_connection.delete(redis_key) |
| 2713 |
|
| 2714 |
@pytest.mark.parametrize("errback_task", [errback_old_style, errback_new_style]) |
| 2715 |
def test_mutable_errback_called_by_chord_from_group_fail_multiple_on_header_failure( |
| 2716 |
self, errback_task, manager, subtests |
| 2717 |
): |
| 2718 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2719 |
raise pytest.skip("Requires redis result backend.") |
| 2720 |
redis_connection = get_redis_connection() |
| 2721 |
|
| 2722 |
fail_task_count = 42 |
| 2723 |
# We have to use failing task signatures with unique task IDs to ensure |
| 2724 |
# the chord can complete when they are used as part of its header! |
| 2725 |
fail_sigs = tuple( |
| 2726 |
fail.s() for _ in range(fail_task_count) |
| 2727 |
) |
| 2728 |
errback = errback_task.s() |
| 2729 |
# Include a mix of passing and failing tasks |
| 2730 |
child_sig = group( |
| 2731 |
*(identity.si(42) for _ in range(8)), # arbitrary task count |
| 2732 |
*fail_sigs, |
| 2733 |
) |
| 2734 |
|
| 2735 |
chord_sig = chord((child_sig,), identity.s()) |
| 2736 |
chord_sig.link_error(errback) |
| 2737 |
expected_redis_key = chord_sig.body.freeze().id |
| 2738 |
redis_connection.delete(expected_redis_key) |
| 2739 |
with subtests.test(msg="Error propagates from header group"): |
| 2740 |
res = chord_sig.delay() |
| 2741 |
sleep(1) |
| 2742 |
with pytest.raises(ExpectedException): |
| 2743 |
res.get(timeout=TIMEOUT) |
| 2744 |
with subtests.test(msg="Errback is called after header group fails"): |
| 2745 |
# NOTE: Here we only expect the errback to be called once since it |
| 2746 |
# is attached to the chord body which is a single task! |
| 2747 |
await_redis_count(1, redis_key=expected_redis_key) |
| 2748 |
|
| 2749 |
@pytest.mark.parametrize("errback_task", [errback_old_style, errback_new_style]) |
| 2750 |
def test_mutable_errback_called_by_chord_from_group_fail_multiple_on_body_failure( |
| 2751 |
self, errback_task, manager, subtests |
| 2752 |
): |
| 2753 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2754 |
raise pytest.skip("Requires redis result backend.") |
| 2755 |
redis_connection = get_redis_connection() |
| 2756 |
|
| 2757 |
fail_task_count = 42 |
| 2758 |
# We have to use failing task signatures with unique task IDs to ensure |
| 2759 |
# the chord can complete when they are used as part of its header! |
| 2760 |
fail_sigs = tuple( |
| 2761 |
fail.s() for _ in range(fail_task_count) |
| 2762 |
) |
| 2763 |
fail_sig_ids = tuple(s.freeze().id for s in fail_sigs) |
| 2764 |
errback = errback_task.s() |
| 2765 |
# Include a mix of passing and failing tasks |
| 2766 |
child_sig = group( |
| 2767 |
*(identity.si(42) for _ in range(8)), # arbitrary task count |
| 2768 |
*fail_sigs, |
| 2769 |
) |
| 2770 |
|
| 2771 |
chord_sig = chord((identity.si(42),), child_sig) |
| 2772 |
chord_sig.link_error(errback) |
| 2773 |
for fail_sig_id in fail_sig_ids: |
| 2774 |
redis_connection.delete(fail_sig_id) |
| 2775 |
with subtests.test(msg="Error propagates from body group"): |
| 2776 |
res = chord_sig.delay() |
| 2777 |
sleep(1) |
| 2778 |
with pytest.raises(ExpectedException): |
| 2779 |
res.get(timeout=TIMEOUT) |
| 2780 |
with subtests.test(msg="Errback is called after body group fails"): |
| 2781 |
# NOTE: Here we expect the errback to be called once per failing |
| 2782 |
# task in the chord body since it is a group, and each task has a |
| 2783 |
# unique task ID |
| 2784 |
for i, fail_sig_id in enumerate(fail_sig_ids): |
| 2785 |
await_redis_count( |
| 2786 |
1, redis_key=fail_sig_id, |
| 2787 |
# After the first one is seen, check the rest with no |
| 2788 |
# timeout since waiting to confirm that each one doesn't |
| 2789 |
# get over-incremented will take a long time |
| 2790 |
timeout=TIMEOUT if i == 0 else 0, |
| 2791 |
) |
| 2792 |
for fail_sig_id in fail_sig_ids: |
| 2793 |
redis_connection.delete(fail_sig_id) |
| 2794 |
|
| 2795 |
def test_chord_header_task_replaced_with_chain(self, manager): |
| 2796 |
try: |
| 2797 |
manager.app.backend.ensure_chords_allowed() |
| 2798 |
except NotImplementedError as e: |
| 2799 |
raise pytest.skip(e.args[0]) |
| 2800 |
|
| 2801 |
orig_sig = chord( |
| 2802 |
replace_with_chain.si(42), |
| 2803 |
identity.s(), |
| 2804 |
) |
| 2805 |
res_obj = orig_sig.delay() |
| 2806 |
assert res_obj.get(timeout=TIMEOUT) == [42] |
| 2807 |
|
| 2808 |
def test_chord_header_child_replaced_with_chain_first(self, manager): |
| 2809 |
try: |
| 2810 |
manager.app.backend.ensure_chords_allowed() |
| 2811 |
except NotImplementedError as e: |
| 2812 |
raise pytest.skip(e.args[0]) |
| 2813 |
|
| 2814 |
orig_sig = chord( |
| 2815 |
(replace_with_chain.si(42), identity.s(1337),), |
| 2816 |
identity.s(), |
| 2817 |
) |
| 2818 |
res_obj = orig_sig.delay() |
| 2819 |
assert res_obj.get(timeout=TIMEOUT) == [42, 1337] |
| 2820 |
|
| 2821 |
def test_chord_header_child_replaced_with_chain_middle(self, manager): |
| 2822 |
try: |
| 2823 |
manager.app.backend.ensure_chords_allowed() |
| 2824 |
except NotImplementedError as e: |
| 2825 |
raise pytest.skip(e.args[0]) |
| 2826 |
|
| 2827 |
orig_sig = chord( |
| 2828 |
(identity.s(42), replace_with_chain.s(1337), identity.s(31337),), |
| 2829 |
identity.s(), |
| 2830 |
) |
| 2831 |
res_obj = orig_sig.delay() |
| 2832 |
assert res_obj.get(timeout=TIMEOUT) == [42, 1337, 31337] |
| 2833 |
|
| 2834 |
def test_chord_header_child_replaced_with_chain_last(self, manager): |
| 2835 |
try: |
| 2836 |
manager.app.backend.ensure_chords_allowed() |
| 2837 |
except NotImplementedError as e: |
| 2838 |
raise pytest.skip(e.args[0]) |
| 2839 |
|
| 2840 |
orig_sig = chord( |
| 2841 |
(identity.s(42), replace_with_chain.s(1337),), |
| 2842 |
identity.s(), |
| 2843 |
) |
| 2844 |
res_obj = orig_sig.delay() |
| 2845 |
assert res_obj.get(timeout=TIMEOUT) == [42, 1337] |
| 2846 |
|
| 2847 |
def test_chord_body_task_replaced_with_chain(self, manager): |
| 2848 |
try: |
| 2849 |
manager.app.backend.ensure_chords_allowed() |
| 2850 |
except NotImplementedError as e: |
| 2851 |
raise pytest.skip(e.args[0]) |
| 2852 |
|
| 2853 |
orig_sig = chord( |
| 2854 |
identity.s(42), |
| 2855 |
replace_with_chain.s(), |
| 2856 |
) |
| 2857 |
res_obj = orig_sig.delay() |
| 2858 |
assert res_obj.get(timeout=TIMEOUT) == [42] |
| 2859 |
|
| 2860 |
def test_chord_body_chain_child_replaced_with_chain_first(self, manager): |
| 2861 |
try: |
| 2862 |
manager.app.backend.ensure_chords_allowed() |
| 2863 |
except NotImplementedError as e: |
| 2864 |
raise pytest.skip(e.args[0]) |
| 2865 |
|
| 2866 |
orig_sig = chord( |
| 2867 |
identity.s(42), |
| 2868 |
chain(replace_with_chain.s(), identity.s(), ), |
| 2869 |
) |
| 2870 |
res_obj = orig_sig.delay() |
| 2871 |
assert res_obj.get(timeout=TIMEOUT) == [42] |
| 2872 |
|
| 2873 |
def test_chord_body_chain_child_replaced_with_chain_middle(self, manager): |
| 2874 |
try: |
| 2875 |
manager.app.backend.ensure_chords_allowed() |
| 2876 |
except NotImplementedError as e: |
| 2877 |
raise pytest.skip(e.args[0]) |
| 2878 |
|
| 2879 |
orig_sig = chord( |
| 2880 |
identity.s(42), |
| 2881 |
chain(identity.s(), replace_with_chain.s(), identity.s(), ), |
| 2882 |
) |
| 2883 |
res_obj = orig_sig.delay() |
| 2884 |
assert res_obj.get(timeout=TIMEOUT) == [42] |
| 2885 |
|
| 2886 |
def test_chord_body_chain_child_replaced_with_chain_last(self, manager): |
| 2887 |
try: |
| 2888 |
manager.app.backend.ensure_chords_allowed() |
| 2889 |
except NotImplementedError as e: |
| 2890 |
raise pytest.skip(e.args[0]) |
| 2891 |
|
| 2892 |
orig_sig = chord( |
| 2893 |
identity.s(42), |
| 2894 |
chain(identity.s(), replace_with_chain.s(), ), |
| 2895 |
) |
| 2896 |
res_obj = orig_sig.delay() |
| 2897 |
assert res_obj.get(timeout=TIMEOUT) == [42] |
| 2898 |
|
| 2899 |
def test_nested_chord_header_link_error(self, manager, subtests): |
| 2900 |
try: |
| 2901 |
manager.app.backend.ensure_chords_allowed() |
| 2902 |
except NotImplementedError as e: |
| 2903 |
raise pytest.skip(e.args[0]) |
| 2904 |
|
| 2905 |
if not manager.app.conf.result_backend.startswith("redis"): |
| 2906 |
raise pytest.skip("Requires redis result backend.") |
| 2907 |
redis_connection = get_redis_connection() |
| 2908 |
|
| 2909 |
errback_msg = "errback called" |
| 2910 |
errback_key = "echo_errback" |
| 2911 |
errback_sig = redis_echo.si(errback_msg, redis_key=errback_key) |
| 2912 |
|
| 2913 |
body_msg = "chord body called" |
| 2914 |
body_key = "echo_body" |
| 2915 |
body_sig = redis_echo.si(body_msg, redis_key=body_key) |
| 2916 |
|
| 2917 |
redis_connection.delete(errback_key, body_key) |
| 2918 |
|
| 2919 |
manager.app.conf.task_allow_error_cb_on_chord_header = False |
| 2920 |
|
| 2921 |
chord_inner = chord( |
| 2922 |
[identity.si("t1"), fail.si()], |
| 2923 |
identity.si("t2 (body)"), |
| 2924 |
) |
| 2925 |
chord_outer = chord( |
| 2926 |
group( |
| 2927 |
[ |
| 2928 |
identity.si("t3"), |
| 2929 |
chord_inner, |
| 2930 |
], |
| 2931 |
), |
| 2932 |
body_sig, |
| 2933 |
) |
| 2934 |
chord_outer.link_error(errback_sig) |
| 2935 |
chord_outer.delay() |
| 2936 |
|
| 2937 |
with subtests.test(msg="Confirm the body was not executed"): |
| 2938 |
with pytest.raises(TimeoutError): |
| 2939 |
# confirm the chord body was not called |
| 2940 |
await_redis_echo((body_msg,), redis_key=body_key, timeout=10) |
| 2941 |
# Double check |
| 2942 |
assert not redis_connection.exists(body_key), "Chord body was called when it should have not" |
| 2943 |
|
| 2944 |
with subtests.test(msg="Confirm only one errback was called"): |
| 2945 |
await_redis_echo((errback_msg,), redis_key=errback_key, timeout=10) |
| 2946 |
with pytest.raises(TimeoutError): |
| 2947 |
# Double check |
| 2948 |
await_redis_echo((errback_msg,), redis_key=errback_key, timeout=10) |
| 2949 |
|
| 2950 |
# Cleanup |
| 2951 |
redis_connection.delete(errback_key) |
| 2952 |
|
| 2953 |
def test_enabling_flag_allow_error_cb_on_chord_header(self, manager, subtests): |
| 2954 |
""" |
| 2955 |
Test that the flag allow_error_callback_on_chord_header works as |
| 2956 |
expected. To confirm this, we create a chord with a failing header |
| 2957 |
task, and check that the body does not execute when the header task fails. |
| 2958 |
This allows preventing the body from executing when the chord header fails |
| 2959 |
when the flag is turned on. In addition, we make sure the body error callback |
| 2960 |
is also executed when the header fails and the flag is turned on. |
| 2961 |
""" |
| 2962 |
try: |
| 2963 |
manager.app.backend.ensure_chords_allowed() |
| 2964 |
except NotImplementedError as e: |
| 2965 |
raise pytest.skip(e.args[0]) |
| 2966 |
|
| 2967 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 2968 |
raise pytest.skip('Requires redis result backend.') |
| 2969 |
redis_connection = get_redis_connection() |
| 2970 |
|
| 2971 |
manager.app.conf.task_allow_error_cb_on_chord_header = True |
| 2972 |
|
| 2973 |
header_errback_msg = 'header errback called' |
| 2974 |
header_errback_key = 'echo_header_errback' |
| 2975 |
header_errback_sig = redis_echo.si(header_errback_msg, redis_key=header_errback_key) |
| 2976 |
|
| 2977 |
body_errback_msg = 'body errback called' |
| 2978 |
body_errback_key = 'echo_body_errback' |
| 2979 |
body_errback_sig = redis_echo.si(body_errback_msg, redis_key=body_errback_key) |
| 2980 |
|
| 2981 |
body_msg = 'chord body called' |
| 2982 |
body_key = 'echo_body' |
| 2983 |
body_sig = redis_echo.si(body_msg, redis_key=body_key) |
| 2984 |
|
| 2985 |
headers = ( |
| 2986 |
(fail.si(),), |
| 2987 |
(fail.si(), fail.si(), fail.si()), |
| 2988 |
(fail.si(), identity.si(42)), |
| 2989 |
(fail.si(), identity.si(42), identity.si(42)), |
| 2990 |
(fail.si(), identity.si(42), fail.si()), |
| 2991 |
(fail.si(), identity.si(42), fail.si(), identity.si(42)), |
| 2992 |
(fail.si(), identity.si(42), fail.si(), identity.si(42), fail.si()), |
| 2993 |
) |
| 2994 |
|
| 2995 |
# for some reason using parametrize breaks the test so we do it manually unfortunately |
| 2996 |
for header in headers: |
| 2997 |
chord_sig = chord(header, body_sig) |
| 2998 |
# link error to chord header ONLY |
| 2999 |
[header_task.link_error(header_errback_sig) for header_task in chord_sig.tasks] |
| 3000 |
# link error to chord body ONLY |
| 3001 |
chord_sig.body.link_error(body_errback_sig) |
| 3002 |
redis_connection.delete(header_errback_key, body_errback_key, body_key) |
| 3003 |
|
| 3004 |
with subtests.test(msg='Error propagates from failure in header'): |
| 3005 |
res = chord_sig.delay() |
| 3006 |
with pytest.raises(ExpectedException): |
| 3007 |
res.get(timeout=TIMEOUT) |
| 3008 |
|
| 3009 |
with subtests.test(msg='Confirm the body was not executed'): |
| 3010 |
with pytest.raises(TimeoutError): |
| 3011 |
# confirm the chord body was not called |
| 3012 |
await_redis_echo((body_msg,), redis_key=body_key, timeout=10) |
| 3013 |
# Double check |
| 3014 |
assert not redis_connection.exists(body_key), 'Chord body was called when it should have not' |
| 3015 |
|
| 3016 |
with subtests.test(msg='Confirm the errback was called for each failed header task + body'): |
| 3017 |
# confirm the errback was called for each task in the chord header |
| 3018 |
failed_header_tasks_count = len(list(filter(lambda f_sig: f_sig == fail.si(), header))) |
| 3019 |
expected_header_errbacks = tuple(header_errback_msg for _ in range(failed_header_tasks_count)) |
| 3020 |
await_redis_echo(expected_header_errbacks, redis_key=header_errback_key) |
| 3021 |
|
| 3022 |
# confirm the errback was called for the chord body |
| 3023 |
await_redis_echo((body_errback_msg,), redis_key=body_errback_key) |
| 3024 |
|
| 3025 |
redis_connection.delete(header_errback_key, body_errback_key) |
| 3026 |
|
| 3027 |
def test_disabling_flag_allow_error_cb_on_chord_header(self, manager, subtests): |
| 3028 |
""" |
| 3029 |
Confirm that when allow_error_callback_on_chord_header is disabled, the default |
| 3030 |
behavior is kept. |
| 3031 |
""" |
| 3032 |
try: |
| 3033 |
manager.app.backend.ensure_chords_allowed() |
| 3034 |
except NotImplementedError as e: |
| 3035 |
raise pytest.skip(e.args[0]) |
| 3036 |
|
| 3037 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 3038 |
raise pytest.skip('Requires redis result backend.') |
| 3039 |
redis_connection = get_redis_connection() |
| 3040 |
|
| 3041 |
manager.app.conf.task_allow_error_cb_on_chord_header = False |
| 3042 |
|
| 3043 |
errback_msg = 'errback called' |
| 3044 |
errback_key = 'echo_errback' |
| 3045 |
errback_sig = redis_echo.si(errback_msg, redis_key=errback_key) |
| 3046 |
|
| 3047 |
body_msg = 'chord body called' |
| 3048 |
body_key = 'echo_body' |
| 3049 |
body_sig = redis_echo.si(body_msg, redis_key=body_key) |
| 3050 |
|
| 3051 |
headers = ( |
| 3052 |
(fail.si(),), |
| 3053 |
(fail.si(), fail.si(), fail.si()), |
| 3054 |
(fail.si(), identity.si(42)), |
| 3055 |
(fail.si(), identity.si(42), identity.si(42)), |
| 3056 |
(fail.si(), identity.si(42), fail.si()), |
| 3057 |
(fail.si(), identity.si(42), fail.si(), identity.si(42)), |
| 3058 |
(fail.si(), identity.si(42), fail.si(), identity.si(42), fail.si()), |
| 3059 |
) |
| 3060 |
|
| 3061 |
# for some reason using parametrize breaks the test so we do it manually unfortunately |
| 3062 |
for header in headers: |
| 3063 |
chord_sig = chord(header, body_sig) |
| 3064 |
chord_sig.link_error(errback_sig) |
| 3065 |
redis_connection.delete(errback_key, body_key) |
| 3066 |
|
| 3067 |
with subtests.test(msg='Error propagates from failure in header'): |
| 3068 |
res = chord_sig.delay() |
| 3069 |
with pytest.raises(ExpectedException): |
| 3070 |
res.get(timeout=TIMEOUT) |
| 3071 |
|
| 3072 |
with subtests.test(msg='Confirm the body was not executed'): |
| 3073 |
with pytest.raises(TimeoutError): |
| 3074 |
# confirm the chord body was not called |
| 3075 |
await_redis_echo((body_msg,), redis_key=body_key, timeout=10) |
| 3076 |
# Double check |
| 3077 |
assert not redis_connection.exists(body_key), 'Chord body was called when it should have not' |
| 3078 |
|
| 3079 |
with subtests.test(msg='Confirm only one errback was called'): |
| 3080 |
await_redis_echo((errback_msg,), redis_key=errback_key, timeout=10) |
| 3081 |
with pytest.raises(TimeoutError): |
| 3082 |
await_redis_echo((errback_msg,), redis_key=errback_key, timeout=10) |
| 3083 |
|
| 3084 |
# Cleanup |
| 3085 |
redis_connection.delete(errback_key) |
| 3086 |
|
| 3087 |
def test_flag_allow_error_cb_on_chord_header_on_upgraded_chord(self, manager, subtests): |
| 3088 |
""" |
| 3089 |
Confirm that allow_error_callback_on_chord_header flag supports upgraded chords |
| 3090 |
""" |
| 3091 |
try: |
| 3092 |
manager.app.backend.ensure_chords_allowed() |
| 3093 |
except NotImplementedError as e: |
| 3094 |
raise pytest.skip(e.args[0]) |
| 3095 |
|
| 3096 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 3097 |
raise pytest.skip('Requires redis result backend.') |
| 3098 |
redis_connection = get_redis_connection() |
| 3099 |
|
| 3100 |
manager.app.conf.task_allow_error_cb_on_chord_header = True |
| 3101 |
|
| 3102 |
errback_msg = 'errback called' |
| 3103 |
errback_key = 'echo_errback' |
| 3104 |
errback_sig = redis_echo.si(errback_msg, redis_key=errback_key) |
| 3105 |
|
| 3106 |
body_msg = 'chord body called' |
| 3107 |
body_key = 'echo_body' |
| 3108 |
body_sig = redis_echo.si(body_msg, redis_key=body_key) |
| 3109 |
|
| 3110 |
headers = ( |
| 3111 |
# (fail.si(),), <-- this is not supported because it's not a valid chord header (only one task) |
| 3112 |
(fail.si(), fail.si(), fail.si()), |
| 3113 |
(fail.si(), identity.si(42)), |
| 3114 |
(fail.si(), identity.si(42), identity.si(42)), |
| 3115 |
(fail.si(), identity.si(42), fail.si()), |
| 3116 |
(fail.si(), identity.si(42), fail.si(), identity.si(42)), |
| 3117 |
(fail.si(), identity.si(42), fail.si(), identity.si(42), fail.si()), |
| 3118 |
) |
| 3119 |
|
| 3120 |
# for some reason using parametrize breaks the test so we do it manually unfortunately |
| 3121 |
for header in headers: |
| 3122 |
implicit_chord_sig = chain(group(list(header)), body_sig) |
| 3123 |
implicit_chord_sig.link_error(errback_sig) |
| 3124 |
redis_connection.delete(errback_key, body_key) |
| 3125 |
|
| 3126 |
with subtests.test(msg='Error propagates from failure in header'): |
| 3127 |
res = implicit_chord_sig.delay() |
| 3128 |
with pytest.raises(ExpectedException): |
| 3129 |
res.get(timeout=TIMEOUT) |
| 3130 |
|
| 3131 |
with subtests.test(msg='Confirm the body was not executed'): |
| 3132 |
with pytest.raises(TimeoutError): |
| 3133 |
# confirm the chord body was not called |
| 3134 |
await_redis_echo((body_msg,), redis_key=body_key, timeout=10) |
| 3135 |
# Double check |
| 3136 |
assert not redis_connection.exists(body_key), 'Chord body was called when it should have not' |
| 3137 |
|
| 3138 |
with subtests.test(msg='Confirm the errback was called for each failed header task + body'): |
| 3139 |
# confirm the errback was called for each task in the chord header |
| 3140 |
failed_header_tasks_count = len(list(filter(lambda f_sig: f_sig.name == fail.si().name, header))) |
| 3141 |
expected_errbacks_count = failed_header_tasks_count + 1 # +1 for the body |
| 3142 |
expected_errbacks = tuple(errback_msg for _ in range(expected_errbacks_count)) |
| 3143 |
await_redis_echo(expected_errbacks, redis_key=errback_key) |
| 3144 |
|
| 3145 |
# confirm there are not leftovers |
| 3146 |
assert not redis_connection.exists(errback_key) |
| 3147 |
|
| 3148 |
# Cleanup |
| 3149 |
redis_connection.delete(errback_key) |
| 3150 |
|
| 3151 |
def test_upgraded_chord_link_error_with_header_errback_enabled(self, manager, subtests): |
| 3152 |
try: |
| 3153 |
manager.app.backend.ensure_chords_allowed() |
| 3154 |
except NotImplementedError as e: |
| 3155 |
raise pytest.skip(e.args[0]) |
| 3156 |
|
| 3157 |
if not manager.app.conf.result_backend.startswith('redis'): |
| 3158 |
raise pytest.skip('Requires redis result backend.') |
| 3159 |
redis_connection = get_redis_connection() |
| 3160 |
|
| 3161 |
manager.app.conf.task_allow_error_cb_on_chord_header = True |
| 3162 |
|
| 3163 |
body_msg = 'chord body called' |
| 3164 |
body_key = 'echo_body' |
| 3165 |
body_sig = redis_echo.si(body_msg, redis_key=body_key) |
| 3166 |
|
| 3167 |
errback_msg = 'errback called' |
| 3168 |
errback_key = 'echo_errback' |
| 3169 |
errback_sig = redis_echo.si(errback_msg, redis_key=errback_key) |
| 3170 |
|
| 3171 |
redis_connection.delete(errback_key, body_key) |
| 3172 |
|
| 3173 |
sig = chain( |
| 3174 |
identity.si(42), |
| 3175 |
group( |
| 3176 |
fail.si(), |
| 3177 |
fail.si(), |
| 3178 |
), |
| 3179 |
body_sig, |
| 3180 |
).on_error(errback_sig) |
| 3181 |
|
| 3182 |
with subtests.test(msg='Error propagates from failure in header'): |
| 3183 |
with pytest.raises(ExpectedException): |
| 3184 |
sig.apply_async().get(timeout=TIMEOUT) |
| 3185 |
|
| 3186 |
redis_connection.delete(errback_key, body_key) |
| 3187 |
|
| 3188 |
@flaky |
| 3189 |
@pytest.mark.parametrize( |
| 3190 |
"input_body", |
| 3191 |
[ |
| 3192 |
(lambda: add.si(9, 7)), |
| 3193 |
( |
| 3194 |
lambda: chain( |
| 3195 |
add.si(9, 7), |
| 3196 |
add.si(5, 7), |
| 3197 |
) |
| 3198 |
), |
| 3199 |
( |
| 3200 |
lambda: group( |
| 3201 |
[ |
| 3202 |
add.si(9, 7), |
| 3203 |
add.si(5, 7), |
| 3204 |
] |
| 3205 |
) |
| 3206 |
), |
| 3207 |
( |
| 3208 |
lambda: chord( |
| 3209 |
group( |
| 3210 |
[ |
| 3211 |
add.si(1, 1), |
| 3212 |
add.si(2, 2), |
| 3213 |
] |
| 3214 |
), |
| 3215 |
add.si(10, 10), |
| 3216 |
) |
| 3217 |
), |
| 3218 |
], |
| 3219 |
ids=[ |
| 3220 |
"body is a single_task", |
| 3221 |
"body is a chain", |
| 3222 |
"body is a group", |
| 3223 |
"body is a chord", |
| 3224 |
], |
| 3225 |
) |
| 3226 |
def test_chord_error_propagation_with_different_body_types( |
| 3227 |
self, manager, caplog, input_body |
| 3228 |
) -> None: |
| 3229 |
"""Integration test for issue #9773: task_id must not be empty on chain of groups. |
| 3230 |
|
| 3231 |
This test reproduces the exact scenario from GitHub issue #9773 where a chord |
| 3232 |
with a failing group task and a chain body causes a ValueError during error handling. |
| 3233 |
|
| 3234 |
The test verifies that: |
| 3235 |
1. The chord executes without the "task_id must not be empty" error |
| 3236 |
2. The failure from the group properly propagates to the chain body |
| 3237 |
3. Error handling works correctly with proper task IDs |
| 3238 |
|
| 3239 |
Args: |
| 3240 |
input_body (callable): A callable that returns a Celery signature for the body of the chord. |
| 3241 |
""" |
| 3242 |
try: |
| 3243 |
manager.app.backend.ensure_chords_allowed() |
| 3244 |
except NotImplementedError as e: |
| 3245 |
raise pytest.skip(e.args[0]) |
| 3246 |
|
| 3247 |
# Create the failing group header (same for all tests) |
| 3248 |
failing_chord = chain( |
| 3249 |
group( |
| 3250 |
[ |
| 3251 |
add.si(15, 7), |
| 3252 |
# failing task |
| 3253 |
fail.si(), |
| 3254 |
] |
| 3255 |
), |
| 3256 |
# dynamic parametrized body |
| 3257 |
input_body(), |
| 3258 |
) |
| 3259 |
|
| 3260 |
result = failing_chord.apply_async() |
| 3261 |
|
| 3262 |
# The chain should fail due to the failing task in the group |
| 3263 |
with pytest.raises(ExpectedException): |
| 3264 |
result.get(timeout=TIMEOUT) |
| 3265 |
|
| 3266 |
# Verify that error propagation worked correctly without the task_id error |
| 3267 |
# This test passes if no "task_id must not be empty" error was logged |
| 3268 |
# Check if the message appears in the logs (it shouldn't) |
| 3269 |
error_found = check_for_logs(caplog=caplog, message="ValueError: task_id must not be empty") |
| 3270 |
assert not error_found, "The 'task_id must not be empty' error was found in the logs" |
| 3271 |
|
| 3272 |
|
| 3273 |
class test_signature_serialization: |
| 3274 |
""" |
| 3275 |
Confirm nested signatures can be rebuilt after passing through a backend. |
| 3276 |
|
| 3277 |
These tests are expected to finish and return `None` or raise an exception |
| 3278 |
in the error case. The exception indicates that some element of a nested |
| 3279 |
signature object was not properly deserialized from its dictionary |
| 3280 |
representation, and would explode later on if it were used as a signature. |
| 3281 |
""" |
| 3282 |
|
| 3283 |
def test_rebuild_nested_chain_chain(self, manager): |
| 3284 |
sig = chain( |
| 3285 |
tasks.return_nested_signature_chain_chain.s(), |
| 3286 |
tasks.rebuild_signature.s() |
| 3287 |
) |
| 3288 |
sig.delay().get(timeout=TIMEOUT) |
| 3289 |
|
| 3290 |
def test_rebuild_nested_chain_group(self, manager): |
| 3291 |
sig = chain( |
| 3292 |
tasks.return_nested_signature_chain_group.s(), |
| 3293 |
tasks.rebuild_signature.s() |
| 3294 |
) |
| 3295 |
sig.delay().get(timeout=TIMEOUT) |
| 3296 |
|
| 3297 |
def test_rebuild_nested_chain_chord(self, manager): |
| 3298 |
try: |
| 3299 |
manager.app.backend.ensure_chords_allowed() |
| 3300 |
except NotImplementedError as e: |
| 3301 |
raise pytest.skip(e.args[0]) |
| 3302 |
|
| 3303 |
sig = chain( |
| 3304 |
tasks.return_nested_signature_chain_chord.s(), |
| 3305 |
tasks.rebuild_signature.s() |
| 3306 |
) |
| 3307 |
sig.delay().get(timeout=TIMEOUT) |
| 3308 |
|
| 3309 |
def test_rebuild_nested_group_chain(self, manager): |
| 3310 |
sig = chain( |
| 3311 |
tasks.return_nested_signature_group_chain.s(), |
| 3312 |
tasks.rebuild_signature.s() |
| 3313 |
) |
| 3314 |
sig.delay().get(timeout=TIMEOUT) |
| 3315 |
|
| 3316 |
def test_rebuild_nested_group_group(self, manager): |
| 3317 |
sig = chain( |
| 3318 |
tasks.return_nested_signature_group_group.s(), |
| 3319 |
tasks.rebuild_signature.s() |
| 3320 |
) |
| 3321 |
sig.delay().get(timeout=TIMEOUT) |
| 3322 |
|
| 3323 |
def test_rebuild_nested_group_chord(self, manager): |
| 3324 |
try: |
| 3325 |
manager.app.backend.ensure_chords_allowed() |
| 3326 |
except NotImplementedError as e: |
| 3327 |
raise pytest.skip(e.args[0]) |
| 3328 |
|
| 3329 |
sig = chain( |
| 3330 |
tasks.return_nested_signature_group_chord.s(), |
| 3331 |
tasks.rebuild_signature.s() |
| 3332 |
) |
| 3333 |
sig.delay().get(timeout=TIMEOUT) |
| 3334 |
|
| 3335 |
def test_rebuild_nested_chord_chain(self, manager): |
| 3336 |
try: |
| 3337 |
manager.app.backend.ensure_chords_allowed() |
| 3338 |
except NotImplementedError as e: |
| 3339 |
raise pytest.skip(e.args[0]) |
| 3340 |
|
| 3341 |
sig = chain( |
| 3342 |
tasks.return_nested_signature_chord_chain.s(), |
| 3343 |
tasks.rebuild_signature.s() |
| 3344 |
) |
| 3345 |
sig.delay().get(timeout=TIMEOUT) |
| 3346 |
|
| 3347 |
def test_rebuild_nested_chord_group(self, manager): |
| 3348 |
try: |
| 3349 |
manager.app.backend.ensure_chords_allowed() |
| 3350 |
except NotImplementedError as e: |
| 3351 |
raise pytest.skip(e.args[0]) |
| 3352 |
|
| 3353 |
sig = chain( |
| 3354 |
tasks.return_nested_signature_chord_group.s(), |
| 3355 |
tasks.rebuild_signature.s() |
| 3356 |
) |
| 3357 |
sig.delay().get(timeout=TIMEOUT) |
| 3358 |
|
| 3359 |
def test_rebuild_nested_chord_chord(self, manager): |
| 3360 |
try: |
| 3361 |
manager.app.backend.ensure_chords_allowed() |
| 3362 |
except NotImplementedError as e: |
| 3363 |
raise pytest.skip(e.args[0]) |
| 3364 |
|
| 3365 |
sig = chain( |
| 3366 |
tasks.return_nested_signature_chord_chord.s(), |
| 3367 |
tasks.rebuild_signature.s() |
| 3368 |
) |
| 3369 |
sig.delay().get(timeout=TIMEOUT) |
| 3370 |
|
| 3371 |
|
| 3372 |
class test_stamping_mechanism: |
| 3373 |
def test_stamping_workflow(self, manager, subtests): |
| 3374 |
try: |
| 3375 |
manager.app.backend.ensure_chords_allowed() |
| 3376 |
except NotImplementedError as e: |
| 3377 |
raise pytest.skip(e.args[0]) |
| 3378 |
|
| 3379 |
workflow = group( |
| 3380 |
add.s(1, 2) | add.s(3), |
| 3381 |
add.s(4, 5) | add.s(6), |
| 3382 |
identity.si(21), |
| 3383 |
) | group( |
| 3384 |
xsum.s(), |
| 3385 |
xsum.s(), |
| 3386 |
) |
| 3387 |
|
| 3388 |
@task_received.connect |
| 3389 |
def task_received_handler(request=None, **kwargs): |
| 3390 |
nonlocal assertion_result |
| 3391 |
link = None |
| 3392 |
if request._Request__payload[2]["callbacks"]: |
| 3393 |
link = signature(request._Request__payload[2]["callbacks"][0]) |
| 3394 |
link_error = None |
| 3395 |
if request._Request__payload[2]["errbacks"]: |
| 3396 |
link_error = signature(request._Request__payload[2]["errbacks"][0]) |
| 3397 |
|
| 3398 |
assertion_result = all( |
| 3399 |
[ |
| 3400 |
assertion_result, |
| 3401 |
[stamped_header in request.stamps for stamped_header in request.stamped_headers], |
| 3402 |
[ |
| 3403 |
stamped_header in link.options |
| 3404 |
for stamped_header in link.options["stamped_headers"] |
| 3405 |
if link # the link itself doesn't have a link |
| 3406 |
], |
| 3407 |
[ |
| 3408 |
stamped_header in link_error.options |
| 3409 |
for stamped_header in link_error.options["stamped_headers"] |
| 3410 |
if link_error # the link_error itself doesn't have a link_error |
| 3411 |
], |
| 3412 |
] |
| 3413 |
) |
| 3414 |
|
| 3415 |
@before_task_publish.connect |
| 3416 |
def before_task_publish_handler( |
| 3417 |
body=None, |
| 3418 |
headers=None, |
| 3419 |
**kwargs, |
| 3420 |
): |
| 3421 |
nonlocal assertion_result |
| 3422 |
|
| 3423 |
assertion_result = all( |
| 3424 |
[stamped_header in headers["stamps"] for stamped_header in headers["stamped_headers"]] |
| 3425 |
) |
| 3426 |
|
| 3427 |
class CustomStampingVisitor(StampingVisitor): |
| 3428 |
def on_signature(self, sig, **headers) -> dict: |
| 3429 |
return {"on_signature": 42} |
| 3430 |
|
| 3431 |
with subtests.test("Prepare canvas workflow and stamp it"): |
| 3432 |
link_sig = identity.si("link") |
| 3433 |
link_error_sig = identity.si("link_error") |
| 3434 |
canvas_workflow = workflow |
| 3435 |
canvas_workflow.link(link_sig) |
| 3436 |
canvas_workflow.link_error(link_error_sig) |
| 3437 |
canvas_workflow.stamp(visitor=CustomStampingVisitor()) |
| 3438 |
|
| 3439 |
with subtests.test("Check canvas was executed successfully"): |
| 3440 |
assertion_result = False |
| 3441 |
assert canvas_workflow.apply_async().get() == [42] * 2 |
| 3442 |
assert assertion_result |
| 3443 |
|
| 3444 |
def test_stamping_example_canvas(self, manager): |
| 3445 |
"""Test the stamping example canvas from the examples directory""" |
| 3446 |
try: |
| 3447 |
manager.app.backend.ensure_chords_allowed() |
| 3448 |
except NotImplementedError as e: |
| 3449 |
raise pytest.skip(e.args[0]) |
| 3450 |
|
| 3451 |
c = chain( |
| 3452 |
group(identity.s(i) for i in range(1, 4)) | xsum.s(), |
| 3453 |
chord(group(mul.s(10) for _ in range(1, 4)), xsum.s()), |
| 3454 |
) |
| 3455 |
|
| 3456 |
res = c() |
| 3457 |
assert res.get(timeout=TIMEOUT) == 180 |
| 3458 |
|
| 3459 |
def test_stamp_value_type_defined_by_visitor(self, manager, subtests): |
| 3460 |
"""Test that the visitor can define the type of the stamped value""" |
| 3461 |
|
| 3462 |
@before_task_publish.connect |
| 3463 |
def before_task_publish_handler( |
| 3464 |
sender=None, |
| 3465 |
body=None, |
| 3466 |
exchange=None, |
| 3467 |
routing_key=None, |
| 3468 |
headers=None, |
| 3469 |
properties=None, |
| 3470 |
declare=None, |
| 3471 |
retry_policy=None, |
| 3472 |
**kwargs, |
| 3473 |
): |
| 3474 |
nonlocal task_headers |
| 3475 |
task_headers = headers.copy() |
| 3476 |
|
| 3477 |
with subtests.test(msg="Test stamping a single value"): |
| 3478 |
|
| 3479 |
class CustomStampingVisitor(StampingVisitor): |
| 3480 |
def on_signature(self, sig, **headers) -> dict: |
| 3481 |
return {"stamp": 42} |
| 3482 |
|
| 3483 |
stamped_task = add.si(1, 1) |
| 3484 |
stamped_task.stamp(visitor=CustomStampingVisitor()) |
| 3485 |
result = stamped_task.freeze() |
| 3486 |
task_headers = None |
| 3487 |
stamped_task.apply_async() |
| 3488 |
assert task_headers is not None |
| 3489 |
assert result.get() == 2 |
| 3490 |
assert "stamps" in task_headers |
| 3491 |
assert "stamp" in task_headers["stamps"] |
| 3492 |
assert not isinstance(task_headers["stamps"]["stamp"], list) |
| 3493 |
|
| 3494 |
with subtests.test(msg="Test stamping a list of values"): |
| 3495 |
|
| 3496 |
class CustomStampingVisitor(StampingVisitor): |
| 3497 |
def on_signature(self, sig, **headers) -> dict: |
| 3498 |
return {"stamp": [4, 2]} |
| 3499 |
|
| 3500 |
stamped_task = add.si(1, 1) |
| 3501 |
stamped_task.stamp(visitor=CustomStampingVisitor()) |
| 3502 |
result = stamped_task.freeze() |
| 3503 |
task_headers = None |
| 3504 |
stamped_task.apply_async() |
| 3505 |
assert task_headers is not None |
| 3506 |
assert result.get() == 2 |
| 3507 |
assert "stamps" in task_headers |
| 3508 |
assert "stamp" in task_headers["stamps"] |
| 3509 |
assert isinstance(task_headers["stamps"]["stamp"], list) |
| 3510 |
|
| 3511 |
def test_properties_not_affected_from_stamping(self, manager, subtests): |
| 3512 |
"""Test that the task properties are not dirty with stamping visitor entries""" |
| 3513 |
|
| 3514 |
@before_task_publish.connect |
| 3515 |
def before_task_publish_handler( |
| 3516 |
sender=None, |
| 3517 |
body=None, |
| 3518 |
exchange=None, |
| 3519 |
routing_key=None, |
| 3520 |
headers=None, |
| 3521 |
properties=None, |
| 3522 |
declare=None, |
| 3523 |
retry_policy=None, |
| 3524 |
**kwargs, |
| 3525 |
): |
| 3526 |
nonlocal task_headers |
| 3527 |
nonlocal task_properties |
| 3528 |
task_headers = headers.copy() |
| 3529 |
task_properties = properties.copy() |
| 3530 |
|
| 3531 |
class CustomStampingVisitor(StampingVisitor): |
| 3532 |
def on_signature(self, sig, **headers) -> dict: |
| 3533 |
return {"stamp": 42} |
| 3534 |
|
| 3535 |
stamped_task = add.si(1, 1) |
| 3536 |
stamped_task.stamp(visitor=CustomStampingVisitor()) |
| 3537 |
result = stamped_task.freeze() |
| 3538 |
task_headers = None |
| 3539 |
task_properties = None |
| 3540 |
stamped_task.apply_async() |
| 3541 |
assert task_properties is not None |
| 3542 |
assert result.get() == 2 |
| 3543 |
assert "stamped_headers" in task_headers |
| 3544 |
stamped_headers = task_headers["stamped_headers"] |
| 3545 |
|
| 3546 |
with subtests.test(msg="Test that the task properties are not dirty with stamping visitor entries"): |
| 3547 |
assert "stamped_headers" not in task_properties, "stamped_headers key should not be in task properties" |
| 3548 |
for stamp in stamped_headers: |
| 3549 |
assert stamp not in task_properties, f'The stamp "{stamp}" should not be in the task properties' |
| 3550 |
|
| 3551 |
def test_task_received_has_access_to_stamps(self, manager): |
| 3552 |
"""Make sure that the request has the stamps using the task_received signal""" |
| 3553 |
|
| 3554 |
assertion_result = False |
| 3555 |
|
| 3556 |
@task_received.connect |
| 3557 |
def task_received_handler(sender=None, request=None, signal=None, **kwargs): |
| 3558 |
nonlocal assertion_result |
| 3559 |
assertion_result = all([stamped_header in request.stamps for stamped_header in request.stamped_headers]) |
| 3560 |
|
| 3561 |
class CustomStampingVisitor(StampingVisitor): |
| 3562 |
def on_signature(self, sig, **headers) -> dict: |
| 3563 |
return {"stamp": 42} |
| 3564 |
|
| 3565 |
stamped_task = add.si(1, 1) |
| 3566 |
stamped_task.stamp(visitor=CustomStampingVisitor()) |
| 3567 |
stamped_task.apply_async().get() |
| 3568 |
assert assertion_result |
| 3569 |
|
| 3570 |
def test_all_tasks_of_canvas_are_stamped(self, manager, subtests): |
| 3571 |
"""Test that complex canvas are stamped correctly""" |
| 3572 |
try: |
| 3573 |
manager.app.backend.ensure_chords_allowed() |
| 3574 |
except NotImplementedError as e: |
| 3575 |
raise pytest.skip(e.args[0]) |
| 3576 |
|
| 3577 |
@task_received.connect |
| 3578 |
def task_received_handler(**kwargs): |
| 3579 |
request = kwargs["request"] |
| 3580 |
nonlocal assertion_result |
| 3581 |
|
| 3582 |
assertion_result = all( |
| 3583 |
[ |
| 3584 |
assertion_result, |
| 3585 |
all([stamped_header in request.stamps for stamped_header in request.stamped_headers]), |
| 3586 |
request.stamps["stamp"] == 42, |
| 3587 |
] |
| 3588 |
) |
| 3589 |
|
| 3590 |
# Using a list because pytest.mark.parametrize does not play well |
| 3591 |
canvas = [ |
| 3592 |
add.s(1, 1), |
| 3593 |
group(add.s(1, 1), add.s(2, 2)), |
| 3594 |
chain(add.s(1, 1), add.s(2, 2)), |
| 3595 |
chord([add.s(1, 1), add.s(2, 2)], xsum.s()), |
| 3596 |
chain(group(add.s(0, 0)), add.s(-1)), |
| 3597 |
add.s(1, 1) | add.s(10), |
| 3598 |
group(add.s(1, 1) | add.s(10), add.s(2, 2) | add.s(20)), |
| 3599 |
chain(add.s(1, 1) | add.s(10), add.s(2) | add.s(20)), |
| 3600 |
chord([add.s(1, 1) | add.s(10), add.s(2, 2) | add.s(20)], xsum.s()), |
| 3601 |
chain( |
| 3602 |
chain(add.s(1, 1) | add.s(10), add.s(2) | add.s(20)), |
| 3603 |
add.s(3) | add.s(30), |
| 3604 |
), |
| 3605 |
chord( |
| 3606 |
group( |
| 3607 |
chain(add.s(1, 1), add.s(2)), |
| 3608 |
chord([add.s(3, 3), add.s(4, 4)], xsum.s()), |
| 3609 |
), |
| 3610 |
xsum.s(), |
| 3611 |
), |
| 3612 |
] |
| 3613 |
|
| 3614 |
for sig in canvas: |
| 3615 |
with subtests.test(msg="Assert all tasks are stamped"): |
| 3616 |
|
| 3617 |
class CustomStampingVisitor(StampingVisitor): |
| 3618 |
def on_signature(self, sig, **headers) -> dict: |
| 3619 |
return {"stamp": 42} |
| 3620 |
|
| 3621 |
stamped_task = sig |
| 3622 |
stamped_task.stamp(visitor=CustomStampingVisitor()) |
| 3623 |
assertion_result = True |
| 3624 |
stamped_task.apply_async().get() |
| 3625 |
assert assertion_result |
| 3626 |
|
| 3627 |
def test_replace_merge_stamps(self, manager): |
| 3628 |
"""Test that replacing a task keeps the previous and new stamps""" |
| 3629 |
|
| 3630 |
@task_received.connect |
| 3631 |
def task_received_handler(**kwargs): |
| 3632 |
request = kwargs["request"] |
| 3633 |
nonlocal assertion_result |
| 3634 |
expected_stamp_key = list(StampOnReplace.stamp.keys())[0] |
| 3635 |
expected_stamp_value = list(StampOnReplace.stamp.values())[0] |
| 3636 |
|
| 3637 |
assertion_result = all( |
| 3638 |
[ |
| 3639 |
assertion_result, |
| 3640 |
all([stamped_header in request.stamps for stamped_header in request.stamped_headers]), |
| 3641 |
request.stamps["stamp"] == 42, |
| 3642 |
request.stamps[expected_stamp_key] == expected_stamp_value |
| 3643 |
if "replaced_with_me" in request.task_name |
| 3644 |
else True, |
| 3645 |
] |
| 3646 |
) |
| 3647 |
|
| 3648 |
class CustomStampingVisitor(StampingVisitor): |
| 3649 |
def on_signature(self, sig, **headers) -> dict: |
| 3650 |
return {"stamp": 42} |
| 3651 |
|
| 3652 |
stamped_task = replace_with_stamped_task.s() |
| 3653 |
stamped_task.stamp(visitor=CustomStampingVisitor()) |
| 3654 |
assertion_result = False |
| 3655 |
stamped_task.delay() |
| 3656 |
assertion_result = True |
| 3657 |
sleep(1) |
| 3658 |
# stamped_task needs to be stamped with CustomStampingVisitor |
| 3659 |
# and the replaced task with both CustomStampingVisitor and StampOnReplace |
| 3660 |
assert assertion_result, "All of the tasks should have been stamped" |
| 3661 |
|
| 3662 |
def test_linking_stamped_sig(self, manager): |
| 3663 |
"""Test that linking a callback after stamping will stamp the callback correctly""" |
| 3664 |
|
| 3665 |
assertion_result = False |
| 3666 |
|
| 3667 |
@task_received.connect |
| 3668 |
def task_received_handler(sender=None, request=None, signal=None, **kwargs): |
| 3669 |
nonlocal assertion_result |
| 3670 |
link = request._Request__payload[2]["callbacks"][0] |
| 3671 |
assertion_result = all( |
| 3672 |
[stamped_header in link["options"] for stamped_header in link["options"]["stamped_headers"]] |
| 3673 |
) |
| 3674 |
|
| 3675 |
class FixedMonitoringIdStampingVisitor(StampingVisitor): |
| 3676 |
def __init__(self, msg_id): |
| 3677 |
self.msg_id = msg_id |
| 3678 |
|
| 3679 |
def on_signature(self, sig, **headers): |
| 3680 |
mtask_id = self.msg_id |
| 3681 |
return {"mtask_id": mtask_id} |
| 3682 |
|
| 3683 |
link_sig = identity.si("link_sig") |
| 3684 |
stamped_pass_sig = identity.si("passing sig") |
| 3685 |
stamped_pass_sig.stamp(visitor=FixedMonitoringIdStampingVisitor(str(uuid.uuid4()))) |
| 3686 |
stamped_pass_sig.link(link_sig) |
| 3687 |
stamped_pass_sig.stamp(visitor=FixedMonitoringIdStampingVisitor("1234")) |
| 3688 |
stamped_pass_sig.apply_async().get(timeout=2) |
| 3689 |
assert assertion_result |
| 3690 |
|
| 3691 |
def test_err_linking_stamped_sig(self, manager): |
| 3692 |
"""Test that linking an error after stamping will stamp the errlink correctly""" |
| 3693 |
|
| 3694 |
assertion_result = False |
| 3695 |
|
| 3696 |
@task_received.connect |
| 3697 |
def task_received_handler(sender=None, request=None, signal=None, **kwargs): |
| 3698 |
nonlocal assertion_result |
| 3699 |
link_error = request.errbacks[0] |
| 3700 |
assertion_result = all( |
| 3701 |
[ |
| 3702 |
stamped_header in link_error["options"] |
| 3703 |
for stamped_header in link_error["options"]["stamped_headers"] |
| 3704 |
] |
| 3705 |
) |
| 3706 |
|
| 3707 |
class FixedMonitoringIdStampingVisitor(StampingVisitor): |
| 3708 |
def __init__(self, msg_id): |
| 3709 |
self.msg_id = msg_id |
| 3710 |
|
| 3711 |
def on_signature(self, sig, **headers): |
| 3712 |
mtask_id = self.msg_id |
| 3713 |
return {"mtask_id": mtask_id} |
| 3714 |
|
| 3715 |
link_error_sig = identity.si("link_error") |
| 3716 |
stamped_fail_sig = fail.si() |
| 3717 |
stamped_fail_sig.stamp(visitor=FixedMonitoringIdStampingVisitor(str(uuid.uuid4()))) |
| 3718 |
stamped_fail_sig.link_error(link_error_sig) |
| 3719 |
with pytest.raises(ExpectedException): |
| 3720 |
stamped_fail_sig.stamp(visitor=FixedMonitoringIdStampingVisitor("1234")) |
| 3721 |
stamped_fail_sig.apply_async().get() |
| 3722 |
assert assertion_result |
| 3723 |
|
| 3724 |
@flaky |
| 3725 |
def test_stamps_remain_on_task_retry(self, manager): |
| 3726 |
@task_received.connect |
| 3727 |
def task_received_handler(request, **kwargs): |
| 3728 |
nonlocal assertion_result |
| 3729 |
|
| 3730 |
try: |
| 3731 |
assertion_result = all( |
| 3732 |
[ |
| 3733 |
assertion_result, |
| 3734 |
all([stamped_header in request.stamps for stamped_header in request.stamped_headers]), |
| 3735 |
request.stamps["stamp"] == 42, |
| 3736 |
] |
| 3737 |
) |
| 3738 |
except Exception: |
| 3739 |
assertion_result = False |
| 3740 |
|
| 3741 |
class CustomStampingVisitor(StampingVisitor): |
| 3742 |
def on_signature(self, sig, **headers) -> dict: |
| 3743 |
return {"stamp": 42} |
| 3744 |
|
| 3745 |
stamped_task = retry_once.si() |
| 3746 |
stamped_task.stamp(visitor=CustomStampingVisitor()) |
| 3747 |
assertion_result = True |
| 3748 |
res = stamped_task.delay() |
| 3749 |
res.get(timeout=TIMEOUT) |
| 3750 |
assert assertion_result |
| 3751 |
|
| 3752 |
def test_stamp_canvas_with_dictionary_link(self, manager, subtests): |
| 3753 |
class CustomStampingVisitor(StampingVisitor): |
| 3754 |
def on_signature(self, sig, **headers) -> dict: |
| 3755 |
return {"on_signature": 42} |
| 3756 |
|
| 3757 |
with subtests.test("Stamp canvas with dictionary link"): |
| 3758 |
canvas = identity.si(42) |
| 3759 |
canvas.options["link"] = dict(identity.si(42)) |
| 3760 |
canvas.stamp(visitor=CustomStampingVisitor()) |
| 3761 |
|
| 3762 |
def test_stamp_canvas_with_dictionary_link_error(self, manager, subtests): |
| 3763 |
class CustomStampingVisitor(StampingVisitor): |
| 3764 |
def on_signature(self, sig, **headers) -> dict: |
| 3765 |
return {"on_signature": 42} |
| 3766 |
|
| 3767 |
with subtests.test("Stamp canvas with dictionary link error"): |
| 3768 |
canvas = fail.si() |
| 3769 |
canvas.options["link_error"] = dict(fail.si()) |
| 3770 |
canvas.stamp(visitor=CustomStampingVisitor()) |
| 3771 |
|
| 3772 |
with subtests.test(msg="Expect canvas to fail"): |
| 3773 |
with pytest.raises(ExpectedException): |
| 3774 |
canvas.apply_async().get(timeout=TIMEOUT) |