| 1 |
import math |
| 2 |
import uuid |
| 3 |
from collections.abc import Iterable |
| 4 |
|
| 5 |
import pytest |
| 6 |
|
| 7 |
from celery import Task |
| 8 |
from celery.canvas import Signature, StampingVisitor, _chain, _chord, chain, chord, group, signature |
| 9 |
from celery.exceptions import Ignore |
| 10 |
|
| 11 |
|
| 12 |
class LinkingVisitor(StampingVisitor): |
| 13 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 14 |
link_workflow = chain( |
| 15 |
group(signature("task1"), signature("task2")), |
| 16 |
signature("task3"), |
| 17 |
) |
| 18 |
link = signature(f"{actual_sig.name}_link") | link_workflow.clone() |
| 19 |
actual_sig.link(link) |
| 20 |
link_error = signature(f"{actual_sig.name}_link_error") | link_workflow.clone() |
| 21 |
actual_sig.link_error(link_error) |
| 22 |
return super().on_signature(actual_sig, **headers) |
| 23 |
|
| 24 |
|
| 25 |
class CleanupVisitor(StampingVisitor): |
| 26 |
def clean_stamps(self, actual_sig: Signature) -> None: |
| 27 |
if "stamped_headers" in actual_sig.options and actual_sig.options["stamped_headers"]: |
| 28 |
for stamp in actual_sig.options["stamped_headers"]: |
| 29 |
if stamp in actual_sig.options: |
| 30 |
actual_sig.options.pop(stamp) |
| 31 |
|
| 32 |
def clean_links(self, actual_sig: Signature) -> None: |
| 33 |
if "link" in actual_sig.options: |
| 34 |
actual_sig.options.pop("link") |
| 35 |
if "link_error" in actual_sig.options: |
| 36 |
actual_sig.options.pop("link_error") |
| 37 |
|
| 38 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 39 |
self.clean_stamps(actual_sig) |
| 40 |
self.clean_links(actual_sig) |
| 41 |
return super().on_signature(actual_sig, **headers) |
| 42 |
|
| 43 |
|
| 44 |
class BooleanStampingVisitor(StampingVisitor): |
| 45 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 46 |
return {"on_signature": True} |
| 47 |
|
| 48 |
def on_group_start(self, actual_sig: Signature, **headers) -> dict: |
| 49 |
return {"on_group_start": True} |
| 50 |
|
| 51 |
def on_chain_start(self, actual_sig: Signature, **headers) -> dict: |
| 52 |
return {"on_chain_start": True} |
| 53 |
|
| 54 |
def on_chord_header_start(self, actual_sig: Signature, **header) -> dict: |
| 55 |
s = super().on_chord_header_start(actual_sig, **header) |
| 56 |
s.update({"on_chord_header_start": True}) |
| 57 |
return s |
| 58 |
|
| 59 |
def on_chord_body(self, actual_sig: Signature, **header) -> dict: |
| 60 |
return {"on_chord_body": True} |
| 61 |
|
| 62 |
def on_callback(self, actual_sig: Signature, **header) -> dict: |
| 63 |
return {"on_callback": True} |
| 64 |
|
| 65 |
def on_errback(self, actual_sig: Signature, **header) -> dict: |
| 66 |
return {"on_errback": True} |
| 67 |
|
| 68 |
|
| 69 |
class ListStampingVisitor(StampingVisitor): |
| 70 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 71 |
return { |
| 72 |
"on_signature": ["ListStampingVisitor: on_signature-item1", "ListStampingVisitor: on_signature-item2"] |
| 73 |
} |
| 74 |
|
| 75 |
def on_group_start(self, actual_sig: Signature, **headers) -> dict: |
| 76 |
return { |
| 77 |
"on_group_start": [ |
| 78 |
"ListStampingVisitor: on_group_start-item1", |
| 79 |
"ListStampingVisitor: on_group_start-item2", |
| 80 |
] |
| 81 |
} |
| 82 |
|
| 83 |
def on_chain_start(self, actual_sig: Signature, **headers) -> dict: |
| 84 |
return { |
| 85 |
"on_chain_start": [ |
| 86 |
"ListStampingVisitor: on_chain_start-item1", |
| 87 |
"ListStampingVisitor: on_chain_start-item2", |
| 88 |
] |
| 89 |
} |
| 90 |
|
| 91 |
def on_chord_header_start(self, actual_sig: Signature, **header) -> dict: |
| 92 |
s = super().on_chord_header_start(actual_sig, **header) |
| 93 |
s.update( |
| 94 |
{ |
| 95 |
"on_chord_header_start": [ |
| 96 |
"ListStampingVisitor: on_chord_header_start-item1", |
| 97 |
"ListStampingVisitor: on_chord_header_start-item2", |
| 98 |
] |
| 99 |
} |
| 100 |
) |
| 101 |
return s |
| 102 |
|
| 103 |
def on_chord_body(self, actual_sig: Signature, **header) -> dict: |
| 104 |
return { |
| 105 |
"on_chord_body": ["ListStampingVisitor: on_chord_body-item1", "ListStampingVisitor: on_chord_body-item2"] |
| 106 |
} |
| 107 |
|
| 108 |
def on_callback(self, actual_sig: Signature, **header) -> dict: |
| 109 |
return {"on_callback": ["ListStampingVisitor: on_callback-item1", "ListStampingVisitor: on_callback-item2"]} |
| 110 |
|
| 111 |
def on_errback(self, actual_sig: Signature, **header) -> dict: |
| 112 |
return {"on_errback": ["ListStampingVisitor: on_errback-item1", "ListStampingVisitor: on_errback-item2"]} |
| 113 |
|
| 114 |
|
| 115 |
class SetStampingVisitor(StampingVisitor): |
| 116 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 117 |
return { |
| 118 |
"on_signature": { |
| 119 |
"SetStampingVisitor: on_signature-item1", |
| 120 |
"SetStampingVisitor: on_signature-item2", |
| 121 |
"SetStampingVisitor: on_signature-item3", |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
def on_group_start(self, actual_sig: Signature, **headers) -> dict: |
| 126 |
return { |
| 127 |
"on_group_start": { |
| 128 |
"SetStampingVisitor: on_group_start-item1", |
| 129 |
"SetStampingVisitor: on_group_start-item2", |
| 130 |
"SetStampingVisitor: on_group_start-item3", |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
def on_chain_start(self, actual_sig: Signature, **headers) -> dict: |
| 135 |
return { |
| 136 |
"on_chain_start": { |
| 137 |
"SetStampingVisitor: on_chain_start-item1", |
| 138 |
"SetStampingVisitor: on_chain_start-item2", |
| 139 |
"SetStampingVisitor: on_chain_start-item3", |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
def on_chord_header_start(self, actual_sig: Signature, **header) -> dict: |
| 144 |
s = super().on_chord_header_start(actual_sig, **header) |
| 145 |
s.update( |
| 146 |
{ |
| 147 |
"on_chord_header_start": { |
| 148 |
"SetStampingVisitor: on_chord_header_start-item1", |
| 149 |
"SetStampingVisitor: on_chord_header_start-item2", |
| 150 |
"SetStampingVisitor: on_chord_header_start-item3", |
| 151 |
} |
| 152 |
} |
| 153 |
) |
| 154 |
return s |
| 155 |
|
| 156 |
def on_chord_body(self, actual_sig: Signature, **header) -> dict: |
| 157 |
return { |
| 158 |
"on_chord_body": { |
| 159 |
"SetStampingVisitor: on_chord_body-item1", |
| 160 |
"SetStampingVisitor: on_chord_body-item2", |
| 161 |
"SetStampingVisitor: on_chord_body-item3", |
| 162 |
} |
| 163 |
} |
| 164 |
|
| 165 |
def on_callback(self, actual_sig: Signature, **header) -> dict: |
| 166 |
return { |
| 167 |
"on_callback": { |
| 168 |
"SetStampingVisitor: on_callback-item1", |
| 169 |
"SetStampingVisitor: on_callback-item2", |
| 170 |
"SetStampingVisitor: on_callback-item3", |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
def on_errback(self, actual_sig: Signature, **header) -> dict: |
| 175 |
return { |
| 176 |
"on_errback": { |
| 177 |
"SetStampingVisitor: on_errback-item1", |
| 178 |
"SetStampingVisitor: on_errback-item2", |
| 179 |
"SetStampingVisitor: on_errback-item3", |
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
|
| 184 |
class StringStampingVisitor(StampingVisitor): |
| 185 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 186 |
return {"on_signature": "StringStampingVisitor: on_signature-item1"} |
| 187 |
|
| 188 |
def on_group_start(self, actual_sig: Signature, **headers) -> dict: |
| 189 |
return {"on_group_start": "StringStampingVisitor: on_group_start-item1"} |
| 190 |
|
| 191 |
def on_chain_start(self, actual_sig: Signature, **headers) -> dict: |
| 192 |
return {"on_chain_start": "StringStampingVisitor: on_chain_start-item1"} |
| 193 |
|
| 194 |
def on_chord_header_start(self, actual_sig: Signature, **header) -> dict: |
| 195 |
s = super().on_chord_header_start(actual_sig, **header) |
| 196 |
s.update({"on_chord_header_start": "StringStampingVisitor: on_chord_header_start-item1"}) |
| 197 |
return s |
| 198 |
|
| 199 |
def on_chord_body(self, actual_sig: Signature, **header) -> dict: |
| 200 |
return {"on_chord_body": "StringStampingVisitor: on_chord_body-item1"} |
| 201 |
|
| 202 |
def on_callback(self, actual_sig: Signature, **header) -> dict: |
| 203 |
return {"on_callback": "StringStampingVisitor: on_callback-item1"} |
| 204 |
|
| 205 |
def on_errback(self, actual_sig: Signature, **header) -> dict: |
| 206 |
return {"on_errback": "StringStampingVisitor: on_errback-item1"} |
| 207 |
|
| 208 |
|
| 209 |
class UUIDStampingVisitor(StampingVisitor): |
| 210 |
frozen_uuid = str(uuid.uuid4()) |
| 211 |
|
| 212 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 213 |
return {"on_signature": UUIDStampingVisitor.frozen_uuid} |
| 214 |
|
| 215 |
def on_group_start(self, actual_sig: Signature, **headers) -> dict: |
| 216 |
return {"on_group_start": UUIDStampingVisitor.frozen_uuid} |
| 217 |
|
| 218 |
def on_chain_start(self, actual_sig: Signature, **headers) -> dict: |
| 219 |
return {"on_chain_start": UUIDStampingVisitor.frozen_uuid} |
| 220 |
|
| 221 |
def on_chord_header_start(self, actual_sig: Signature, **header) -> dict: |
| 222 |
s = super().on_chord_header_start(actual_sig, **header) |
| 223 |
s.update({"on_chord_header_start": UUIDStampingVisitor.frozen_uuid}) |
| 224 |
return s |
| 225 |
|
| 226 |
def on_chord_body(self, actual_sig: Signature, **header) -> dict: |
| 227 |
return {"on_chord_body": UUIDStampingVisitor.frozen_uuid} |
| 228 |
|
| 229 |
def on_callback(self, actual_sig: Signature, **header) -> dict: |
| 230 |
return {"on_callback": UUIDStampingVisitor.frozen_uuid} |
| 231 |
|
| 232 |
def on_errback(self, actual_sig: Signature, **header) -> dict: |
| 233 |
return {"on_errback": UUIDStampingVisitor.frozen_uuid} |
| 234 |
|
| 235 |
|
| 236 |
class StampsAssertionVisitor(StampingVisitor): |
| 237 |
""" |
| 238 |
The canvas stamping mechanism traverses the canvas automatically, so we can ride |
| 239 |
it to traverse the canvas recursively and assert that all signatures have the correct stamp in options |
| 240 |
""" |
| 241 |
|
| 242 |
def __init__(self, visitor: StampingVisitor, subtests): |
| 243 |
self.visitor = visitor |
| 244 |
self.subtests = subtests |
| 245 |
|
| 246 |
def assertion_check(self, actual_sig: Signature, method: str, **headers) -> None: |
| 247 |
if any( |
| 248 |
[ |
| 249 |
isinstance(actual_sig, group), |
| 250 |
isinstance(actual_sig, _chain), |
| 251 |
isinstance(actual_sig, _chord), |
| 252 |
] |
| 253 |
): |
| 254 |
return |
| 255 |
|
| 256 |
expected_stamp = getattr(self.visitor, method)(actual_sig, **headers)[method] |
| 257 |
actual_stamp = actual_sig.options[method] |
| 258 |
with self.subtests.test(f"Check if {actual_sig} has stamp: {expected_stamp}"): |
| 259 |
if isinstance(self.visitor, ListStampingVisitor) or isinstance(self.visitor, SetStampingVisitor): |
| 260 |
assertion_check = all([actual in expected_stamp for actual in actual_stamp]) |
| 261 |
else: |
| 262 |
assertion_check = actual_stamp == expected_stamp |
| 263 |
assertion_error = f"{actual_sig} has stamp {actual_stamp} instead of: {expected_stamp}" |
| 264 |
assert assertion_check, assertion_error |
| 265 |
|
| 266 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 267 |
self.assertion_check(actual_sig, "on_signature", **headers) |
| 268 |
return super().on_signature(actual_sig, **headers) |
| 269 |
|
| 270 |
def on_group_start(self, actual_sig: Signature, **headers) -> dict: |
| 271 |
self.assertion_check(actual_sig, "on_group_start", **headers) |
| 272 |
return super().on_group_start(actual_sig, **headers) |
| 273 |
|
| 274 |
def on_chain_start(self, actual_sig: Signature, **headers) -> dict: |
| 275 |
self.assertion_check(actual_sig, "on_chain_start", **headers) |
| 276 |
return super().on_chain_start(actual_sig, **headers) |
| 277 |
|
| 278 |
def on_chord_header_start(self, actual_sig: Signature, **header) -> dict: |
| 279 |
self.assertion_check(actual_sig, "on_chord_header_start", **header) |
| 280 |
if issubclass(type(actual_sig.tasks), Signature): |
| 281 |
self.assertion_check(actual_sig.tasks, "on_chord_header_start", **header) |
| 282 |
return super().on_chord_header_start(actual_sig, **header) |
| 283 |
|
| 284 |
def on_chord_body(self, actual_sig: chord, **header) -> dict: |
| 285 |
self.assertion_check(actual_sig.body, "on_chord_body", **header) |
| 286 |
return super().on_chord_body(actual_sig, **header) |
| 287 |
|
| 288 |
def on_callback(self, actual_link_sig: Signature, **header) -> dict: |
| 289 |
self.assertion_check(actual_link_sig, "on_callback", **header) |
| 290 |
return super().on_callback(actual_link_sig, **header) |
| 291 |
|
| 292 |
def on_errback(self, actual_linkerr_sig: Signature, **header) -> dict: |
| 293 |
self.assertion_check(actual_linkerr_sig, "on_errback", **header) |
| 294 |
return super().on_errback(actual_linkerr_sig, **header) |
| 295 |
|
| 296 |
|
| 297 |
class StampedHeadersAssertionVisitor(StampingVisitor): |
| 298 |
""" |
| 299 |
The canvas stamping mechanism traverses the canvas automatically, so we can ride |
| 300 |
it to traverse the canvas recursively and assert that all signatures have the correct |
| 301 |
stamp in options["stamped_headers"] |
| 302 |
""" |
| 303 |
|
| 304 |
def __init__(self, visitor: StampingVisitor, subtests): |
| 305 |
self.visitor = visitor |
| 306 |
self.subtests = subtests |
| 307 |
|
| 308 |
def assertion_check(self, actual_sig: Signature, expected_stamped_header: str) -> None: |
| 309 |
if any( |
| 310 |
[ |
| 311 |
isinstance(actual_sig, group), |
| 312 |
isinstance(actual_sig, _chain), |
| 313 |
isinstance(actual_sig, _chord), |
| 314 |
] |
| 315 |
): |
| 316 |
with self.subtests.test(f'Check if "stamped_headers" is not in {actual_sig.options}'): |
| 317 |
assertion_check = "stamped_headers" not in actual_sig.options |
| 318 |
assertion_error = f"{actual_sig} should not have stamped_headers in options" |
| 319 |
assert assertion_check, assertion_error |
| 320 |
return |
| 321 |
|
| 322 |
actual_stamped_headers = actual_sig.options["stamped_headers"] |
| 323 |
with self.subtests.test(f'Check if {actual_sig}["stamped_headers"] has: {expected_stamped_header}'): |
| 324 |
assertion_check = expected_stamped_header in actual_stamped_headers |
| 325 |
assertion_error = ( |
| 326 |
f'{actual_sig}["stamped_headers"] {actual_stamped_headers} does ' |
| 327 |
f"not contain {expected_stamped_header}" |
| 328 |
) |
| 329 |
assert assertion_check, assertion_error |
| 330 |
|
| 331 |
def on_signature(self, actual_sig: Signature, **headers) -> dict: |
| 332 |
self.assertion_check(actual_sig, "on_signature") |
| 333 |
return super().on_signature(actual_sig, **headers) |
| 334 |
|
| 335 |
def on_group_start(self, actual_sig: Signature, **headers) -> dict: |
| 336 |
self.assertion_check(actual_sig, "on_group_start") |
| 337 |
return super().on_group_start(actual_sig, **headers) |
| 338 |
|
| 339 |
def on_chain_start(self, actual_sig: Signature, **headers) -> dict: |
| 340 |
self.assertion_check(actual_sig, "on_chain_start") |
| 341 |
return super().on_chain_start(actual_sig, **headers) |
| 342 |
|
| 343 |
def on_chord_header_start(self, actual_sig: Signature, **header) -> dict: |
| 344 |
self.assertion_check(actual_sig, "on_chord_header_start") |
| 345 |
if issubclass(type(actual_sig.tasks), Signature): |
| 346 |
self.assertion_check(actual_sig.tasks, "on_chord_header_start") |
| 347 |
return super().on_chord_header_start(actual_sig, **header) |
| 348 |
|
| 349 |
def on_chord_body(self, actual_sig: chord, **header) -> dict: |
| 350 |
self.assertion_check(actual_sig.body, "on_chord_body") |
| 351 |
return super().on_chord_body(actual_sig, **header) |
| 352 |
|
| 353 |
def on_callback(self, actual_link_sig: Signature, **header) -> dict: |
| 354 |
self.assertion_check(actual_link_sig, "on_callback") |
| 355 |
return super().on_callback(actual_link_sig, **header) |
| 356 |
|
| 357 |
def on_errback(self, actual_linkerr_sig: Signature, **header) -> dict: |
| 358 |
self.assertion_check(actual_linkerr_sig, "on_errback") |
| 359 |
return super().on_errback(actual_linkerr_sig, **header) |
| 360 |
|
| 361 |
|
| 362 |
def return_True(*args, **kwargs): |
| 363 |
return True |
| 364 |
|
| 365 |
|
| 366 |
class CanvasCase: |
| 367 |
def setup_method(self): |
| 368 |
@self.app.task(shared=False) |
| 369 |
def identity(x): |
| 370 |
return x |
| 371 |
|
| 372 |
self.identity = identity |
| 373 |
|
| 374 |
@self.app.task(shared=False) |
| 375 |
def fail(*args): |
| 376 |
args = ("Task expected to fail",) + args |
| 377 |
raise Exception(*args) |
| 378 |
|
| 379 |
self.fail = fail |
| 380 |
|
| 381 |
@self.app.task(shared=False) |
| 382 |
def add(x, y): |
| 383 |
return x + y |
| 384 |
|
| 385 |
self.add = add |
| 386 |
|
| 387 |
@self.app.task(shared=False) |
| 388 |
def mul(x, y): |
| 389 |
return x * y |
| 390 |
|
| 391 |
self.mul = mul |
| 392 |
|
| 393 |
@self.app.task(shared=False) |
| 394 |
def div(x, y): |
| 395 |
return x / y |
| 396 |
|
| 397 |
self.div = div |
| 398 |
|
| 399 |
@self.app.task(shared=False) |
| 400 |
def xsum(numbers): |
| 401 |
return sum(sum(num) if isinstance(num, Iterable) else num for num in numbers) |
| 402 |
|
| 403 |
self.xsum = xsum |
| 404 |
|
| 405 |
@self.app.task(shared=False, bind=True) |
| 406 |
def replaced(self, x, y): |
| 407 |
return self.replace(add.si(x, y)) |
| 408 |
|
| 409 |
self.replaced = replaced |
| 410 |
|
| 411 |
@self.app.task(shared=False, bind=True) |
| 412 |
def replaced_group(self, x, y): |
| 413 |
return self.replace(group(add.si(x, y), mul.si(x, y))) |
| 414 |
|
| 415 |
self.replaced_group = replaced_group |
| 416 |
|
| 417 |
@self.app.task(shared=False, bind=True) |
| 418 |
def replace_with_group(self, x, y): |
| 419 |
return self.replace(group(add.si(x, y), mul.si(x, y))) |
| 420 |
|
| 421 |
self.replace_with_group = replace_with_group |
| 422 |
|
| 423 |
@self.app.task(shared=False, bind=True) |
| 424 |
def replace_with_chain(self, x, y): |
| 425 |
return self.replace(group(add.si(x, y) | mul.s(y), add.si(x, y))) |
| 426 |
|
| 427 |
self.replace_with_chain = replace_with_chain |
| 428 |
|
| 429 |
@self.app.task(shared=False) |
| 430 |
def xprod(numbers): |
| 431 |
try: |
| 432 |
return math.prod(numbers) |
| 433 |
except AttributeError: |
| 434 |
# TODO: Drop this backport once |
| 435 |
# we drop support for Python 3.7 |
| 436 |
import operator |
| 437 |
from functools import reduce |
| 438 |
|
| 439 |
return reduce(operator.mul, numbers) |
| 440 |
|
| 441 |
self.xprod = xprod |
| 442 |
|
| 443 |
@self.app.task(bind=True, max_retries=3, iterations=0, shared=False) |
| 444 |
def retry_task(self, arg1, arg2, kwarg=1, max_retries=None, care=True): |
| 445 |
self.iterations += 1 |
| 446 |
rmax = self.max_retries if max_retries is None else max_retries |
| 447 |
|
| 448 |
assert repr(self.request) |
| 449 |
retries = self.request.retries |
| 450 |
if care and retries >= rmax: |
| 451 |
return arg1 |
| 452 |
else: |
| 453 |
raise self.retry(countdown=0, max_retries=rmax) |
| 454 |
|
| 455 |
self.retry_task = retry_task |
| 456 |
|
| 457 |
|
| 458 |
@pytest.mark.parametrize( |
| 459 |
"stamping_visitor", |
| 460 |
[ |
| 461 |
BooleanStampingVisitor(), |
| 462 |
ListStampingVisitor(), |
| 463 |
SetStampingVisitor(), |
| 464 |
StringStampingVisitor(), |
| 465 |
UUIDStampingVisitor(), |
| 466 |
], |
| 467 |
) |
| 468 |
@pytest.mark.parametrize( |
| 469 |
"canvas_workflow", |
| 470 |
[ |
| 471 |
signature("sig"), |
| 472 |
group(signature("sig")), |
| 473 |
group(signature("sig1", signature("sig2"))), |
| 474 |
group(signature(f"sig{i}") for i in range(2)), |
| 475 |
chord((signature(f"sig{i}") for i in range(2)), signature("sig3")), |
| 476 |
chord(group(signature(f"sig{i}") for i in range(2)), signature("sig3")), |
| 477 |
chord(group(signature(f"sig{i}") for i in range(2)), signature("sig3") | signature("sig4")), |
| 478 |
chord(signature("sig1"), signature("sig2") | signature("sig3")), |
| 479 |
chain( |
| 480 |
signature("sig"), |
| 481 |
chord((signature(f"sig{i}") for i in range(2)), signature("sig3")), |
| 482 |
chord(group(signature(f"sig{i}") for i in range(2)), signature("sig3")), |
| 483 |
chord(group(signature(f"sig{i}") for i in range(2)), signature("sig3") | signature("sig4")), |
| 484 |
chord(signature("sig1"), signature("sig2") | signature("sig3")), |
| 485 |
), |
| 486 |
chain( |
| 487 |
signature("sig1") | signature("sig2"), |
| 488 |
group(signature("sig3"), signature("sig4")) | group(signature(f"sig{i}") for i in range(5, 6)), |
| 489 |
chord(group(signature(f"sig{i}") for i in range(6, 8)), signature("sig8")) | signature("sig9"), |
| 490 |
), |
| 491 |
chain( |
| 492 |
signature("sig"), |
| 493 |
chord( |
| 494 |
group(signature(f"sig{i}") for i in range(2)), |
| 495 |
chain( |
| 496 |
signature("sig3"), |
| 497 |
chord( |
| 498 |
(signature(f"sig{i}") for i in range(4, 6)), |
| 499 |
chain( |
| 500 |
signature("sig6"), |
| 501 |
chord( |
| 502 |
group(signature(f"sig{i}") for i in range(7, 9)), |
| 503 |
chain( |
| 504 |
signature("sig9"), |
| 505 |
chord(group(signature("sig10"), signature("sig11")), signature("sig12")), |
| 506 |
), |
| 507 |
), |
| 508 |
), |
| 509 |
), |
| 510 |
), |
| 511 |
), |
| 512 |
), |
| 513 |
group( |
| 514 |
signature("sig"), |
| 515 |
group(signature("sig1")), |
| 516 |
group(signature("sig1"), signature("sig2")), |
| 517 |
group(signature(f"sig{i}") for i in range(2)), |
| 518 |
group([signature("sig1"), signature("sig2")]), |
| 519 |
group((signature("sig1"), signature("sig2"))), |
| 520 |
chain(signature("sig1"), signature("sig2")), |
| 521 |
chord(group(signature("sig1"), signature("sig2")), signature("sig3")), |
| 522 |
chord(group(signature(f"sig{i}") for i in range(2)), group(signature("sig3"), signature("sig4"))), |
| 523 |
chain( |
| 524 |
group(signature("sig1"), signature("sig2")), |
| 525 |
group(signature("sig3"), signature("sig4")), |
| 526 |
signature("sig5"), |
| 527 |
), |
| 528 |
chain( |
| 529 |
signature("sig1"), |
| 530 |
group(signature("sig2"), signature("sig3")), |
| 531 |
group(signature("sig4"), signature("sig5")), |
| 532 |
), |
| 533 |
chain( |
| 534 |
group( |
| 535 |
signature("sig1"), |
| 536 |
group(signature("sig2")), |
| 537 |
group([signature("sig3"), signature("sig4")]), |
| 538 |
group(signature(f"sig{i}") for i in range(5, 7)), |
| 539 |
), |
| 540 |
chain( |
| 541 |
signature("sig8"), |
| 542 |
group(signature("sig9"), signature("sig10")), |
| 543 |
), |
| 544 |
), |
| 545 |
), |
| 546 |
chain( |
| 547 |
signature("sig"), |
| 548 |
group(signature("sig1")), |
| 549 |
group(signature("sig1"), signature("sig2")), |
| 550 |
group(signature(f"sig{i}") for i in range(2)), |
| 551 |
group([signature("sig1"), signature("sig2")]), |
| 552 |
group((signature("sig1"), signature("sig2"))), |
| 553 |
chain(signature("sig1"), signature("sig2")), |
| 554 |
chord(group(signature("sig1"), signature("sig2")), signature("sig3")), |
| 555 |
chord(group(signature(f"sig{i}") for i in range(2)), group(signature("sig3"), signature("sig4"))), |
| 556 |
chain( |
| 557 |
group(signature("sig1"), signature("sig2")), |
| 558 |
group(signature("sig3"), signature("sig4")), |
| 559 |
signature("sig5"), |
| 560 |
), |
| 561 |
chain( |
| 562 |
signature("sig1"), |
| 563 |
group(signature("sig2"), signature("sig3")), |
| 564 |
group(signature("sig4"), signature("sig5")), |
| 565 |
), |
| 566 |
chain( |
| 567 |
group( |
| 568 |
signature("sig1"), |
| 569 |
group(signature("sig2")), |
| 570 |
group([signature("sig3"), signature("sig4")]), |
| 571 |
group(signature(f"sig{i}") for i in range(5, 7)), |
| 572 |
), |
| 573 |
chain( |
| 574 |
signature("sig8"), |
| 575 |
group(signature("sig9"), signature("sig10")), |
| 576 |
), |
| 577 |
), |
| 578 |
), |
| 579 |
chord( |
| 580 |
group( |
| 581 |
group(signature(f"sig{i}") for i in range(2)), |
| 582 |
group(signature(f"sig{i}") for i in range(2, 4)), |
| 583 |
group(signature(f"sig{i}") for i in range(4, 6)), |
| 584 |
group(signature(f"sig{i}") for i in range(6, 8)), |
| 585 |
), |
| 586 |
chain( |
| 587 |
chain( |
| 588 |
signature("sig8") | signature("sig9"), |
| 589 |
group(signature("sig10"), signature("sig11")) |
| 590 |
| group(signature(f"sig{i}") for i in range(12, 14)), |
| 591 |
chord(group(signature(f"sig{i}") for i in range(14, 16)), signature("sig16")) |
| 592 |
| signature("sig17"), |
| 593 |
), |
| 594 |
signature("sig1") | signature("sig2"), |
| 595 |
group(signature("sig3"), signature("sig4")) | group(signature(f"sig{i}") for i in range(5, 7)), |
| 596 |
chord(group(signature(f"sig{i}") for i in range(7, 9)), signature("sig9")) | signature("sig10"), |
| 597 |
), |
| 598 |
), |
| 599 |
], |
| 600 |
) |
| 601 |
class test_canvas_stamping(CanvasCase): |
| 602 |
@pytest.fixture |
| 603 |
def stamped_canvas(self, stamping_visitor: StampingVisitor, canvas_workflow: Signature) -> Signature: |
| 604 |
workflow = canvas_workflow.clone() |
| 605 |
workflow.stamp(CleanupVisitor()) |
| 606 |
workflow.stamp(stamping_visitor, append_stamps=False) |
| 607 |
return workflow |
| 608 |
|
| 609 |
@pytest.fixture |
| 610 |
def stamped_linked_canvas(self, stamping_visitor: StampingVisitor, canvas_workflow: Signature) -> Signature: |
| 611 |
workflow = canvas_workflow.clone() |
| 612 |
workflow.stamp(CleanupVisitor()) |
| 613 |
workflow.stamp(LinkingVisitor()) |
| 614 |
workflow.stamp(stamping_visitor, append_stamps=False) |
| 615 |
return workflow |
| 616 |
|
| 617 |
@pytest.fixture(params=["stamped_canvas", "stamped_linked_canvas"]) |
| 618 |
def workflow(self, request, canvas_workflow: Signature) -> Signature: |
| 619 |
return request.getfixturevalue(request.param) |
| 620 |
|
| 621 |
@pytest.mark.usefixtures("depends_on_current_app") |
| 622 |
def test_stamp_in_options(self, workflow: Signature, stamping_visitor: StampingVisitor, subtests): |
| 623 |
"""Test that all canvas signatures gets the stamp in options""" |
| 624 |
workflow.stamp(StampsAssertionVisitor(stamping_visitor, subtests)) |
| 625 |
|
| 626 |
@pytest.mark.usefixtures("depends_on_current_app") |
| 627 |
def test_stamping_headers_in_options(self, workflow: Signature, stamping_visitor: StampingVisitor, subtests): |
| 628 |
"""Test that all canvas signatures gets the stamp in options["stamped_headers"]""" |
| 629 |
workflow.stamp(StampedHeadersAssertionVisitor(stamping_visitor, subtests)) |
| 630 |
|
| 631 |
@pytest.mark.usefixtures("depends_on_current_app") |
| 632 |
def test_stamping_with_replace(self, workflow: Signature, stamping_visitor: StampingVisitor, subtests): |
| 633 |
class AssertionTask(Task): |
| 634 |
def on_replace(self, sig: Signature): |
| 635 |
nonlocal assertion_result |
| 636 |
assertion_result = True |
| 637 |
return super().on_replace(sig) |
| 638 |
|
| 639 |
@self.app.task(shared=False, bind=True, base=AssertionTask) |
| 640 |
def assert_using_replace(self: AssertionTask): |
| 641 |
assert self.request.stamped_headers is None, "stamped_headers should not pass via replace" |
| 642 |
assert self.request.stamps is None, "stamps should not pass via replace" |
| 643 |
return self.replace(workflow) |
| 644 |
|
| 645 |
@self.app.task(shared=False, bind=True) |
| 646 |
def stamp_using_replace(self: Task): |
| 647 |
assert self.request.stamped_headers is not None |
| 648 |
assert self.request.stamps is not None |
| 649 |
return self.replace(assert_using_replace.s()) |
| 650 |
|
| 651 |
replaced_sig = stamp_using_replace.s() |
| 652 |
replaced_sig.stamp(stamping_visitor, append_stamps=False) |
| 653 |
assertion_result = False |
| 654 |
replaced_sig.apply() |
| 655 |
assert assertion_result |
| 656 |
|
| 657 |
|
| 658 |
class test_stamping_mechanism(CanvasCase): |
| 659 |
"""These tests were extracted (and fixed) from the canvas unit tests.""" |
| 660 |
|
| 661 |
def test_on_signature_gets_the_signature(self): |
| 662 |
expected_sig = self.add.s(4, 2) |
| 663 |
|
| 664 |
class CustomStampingVisitor(StampingVisitor): |
| 665 |
def on_signature(self, actual_sig, **headers) -> dict: |
| 666 |
assert actual_sig == expected_sig |
| 667 |
return {"header": "value"} |
| 668 |
|
| 669 |
sig = expected_sig.clone() |
| 670 |
sig.stamp(CustomStampingVisitor()) |
| 671 |
assert sig.options["header"] == "value" |
| 672 |
|
| 673 |
def test_double_stamping(self, subtests): |
| 674 |
""" |
| 675 |
Test manual signature stamping with two different stamps. |
| 676 |
""" |
| 677 |
self.app.conf.task_always_eager = True |
| 678 |
self.app.conf.task_store_eager_result = True |
| 679 |
self.app.conf.result_extended = True |
| 680 |
|
| 681 |
sig_1 = self.add.s(2, 2) |
| 682 |
sig_1.stamp(stamp1="stamp1") |
| 683 |
sig_1.stamp(stamp2="stamp2") |
| 684 |
sig_1_res = sig_1.freeze() |
| 685 |
sig_1.apply() |
| 686 |
|
| 687 |
with subtests.test("sig_1_res is stamped with stamp1", stamp1=["stamp1"]): |
| 688 |
assert sig_1_res._get_task_meta()["stamp1"] == ["stamp1"] |
| 689 |
|
| 690 |
with subtests.test("sig_1_res is stamped with stamp2", stamp2=["stamp2"]): |
| 691 |
assert sig_1_res._get_task_meta()["stamp2"] == ["stamp2"] |
| 692 |
|
| 693 |
with subtests.test("sig_1_res is stamped twice", stamped_headers=["stamp2", "stamp1"]): |
| 694 |
assert sorted(sig_1_res._get_task_meta()["stamped_headers"]) == sorted(["stamp2", "stamp1"]) |
| 695 |
|
| 696 |
def test_twice_stamping(self, subtests): |
| 697 |
""" |
| 698 |
Test manual signature stamping with two stamps twice. |
| 699 |
""" |
| 700 |
self.app.conf.task_always_eager = True |
| 701 |
self.app.conf.task_store_eager_result = True |
| 702 |
self.app.conf.result_extended = True |
| 703 |
|
| 704 |
sig_1 = self.add.s(2, 2) |
| 705 |
sig_1.stamp(stamp1="stamp1") |
| 706 |
sig_1.stamp(stamp2="stamp") |
| 707 |
sig_1.stamp(stamp2="stamp2", append_stamps=True) |
| 708 |
sig_1.stamp(stamp3=["stamp3"]) |
| 709 |
sig_1_res = sig_1.freeze() |
| 710 |
sig_1.apply() |
| 711 |
|
| 712 |
with subtests.test("sig_1_res is stamped twice", stamps=["stamp2", "stamp1"]): |
| 713 |
assert sorted(sig_1_res._get_task_meta()["stamp1"]) == ["stamp1"] |
| 714 |
assert sorted(sig_1_res._get_task_meta()["stamp2"]) == sorted(["stamp", "stamp2"]) |
| 715 |
assert sorted(sig_1_res._get_task_meta()["stamp3"]) == ["stamp3"] |
| 716 |
|
| 717 |
with subtests.test("sig_1_res is stamped twice", stamped_headers=["stamp2", "stamp1"]): |
| 718 |
assert sorted(sig_1_res._get_task_meta()["stamped_headers"]) == sorted(["stamp1", "stamp2", "stamp3"]) |
| 719 |
|
| 720 |
def test_manual_stamping(self): |
| 721 |
""" |
| 722 |
Test manual signature stamping. |
| 723 |
""" |
| 724 |
self.app.conf.task_always_eager = True |
| 725 |
self.app.conf.task_store_eager_result = True |
| 726 |
self.app.conf.result_extended = True |
| 727 |
|
| 728 |
sig_1 = self.add.s(2, 2) |
| 729 |
stamps = ["stamp1", "stamp2"] |
| 730 |
sig_1.stamp(visitor=None, groups=[stamps[1]]) |
| 731 |
sig_1.stamp(visitor=None, groups=stamps[0], append_stamps=True) |
| 732 |
sig_1_res = sig_1.freeze() |
| 733 |
sig_1.apply() |
| 734 |
assert sorted(sig_1_res._get_task_meta()["groups"]) == sorted(stamps) |
| 735 |
|
| 736 |
def test_custom_stamping_visitor(self, subtests): |
| 737 |
""" |
| 738 |
Test manual signature stamping with a custom visitor class. |
| 739 |
""" |
| 740 |
self.app.conf.task_always_eager = True |
| 741 |
self.app.conf.task_store_eager_result = True |
| 742 |
self.app.conf.result_extended = True |
| 743 |
|
| 744 |
class CustomStampingVisitor1(StampingVisitor): |
| 745 |
def on_signature(self, sig, **headers) -> dict: |
| 746 |
# without using stamped_headers key explicitly |
| 747 |
# the key will be calculated from the headers implicitly |
| 748 |
return {"header": "value"} |
| 749 |
|
| 750 |
class CustomStampingVisitor2(StampingVisitor): |
| 751 |
def on_signature(self, sig, **headers) -> dict: |
| 752 |
return {"header": "value", "stamped_headers": ["header"]} |
| 753 |
|
| 754 |
sig_1 = self.add.s(2, 2) |
| 755 |
sig_1.stamp(visitor=CustomStampingVisitor1()) |
| 756 |
sig_1_res = sig_1.freeze() |
| 757 |
sig_1.apply() |
| 758 |
sig_2 = self.add.s(2, 2) |
| 759 |
sig_2.stamp(visitor=CustomStampingVisitor2()) |
| 760 |
sig_2_res = sig_2.freeze() |
| 761 |
sig_2.apply() |
| 762 |
|
| 763 |
with subtests.test("sig_1 is stamped with custom visitor", stamped_headers=["header"]): |
| 764 |
assert sorted(sig_1_res._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 765 |
|
| 766 |
with subtests.test("sig_2 is stamped with custom visitor", stamped_headers=["header"]): |
| 767 |
assert sorted(sig_2_res._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 768 |
|
| 769 |
with subtests.test("sig_1 is stamped with custom visitor", header=["value"]): |
| 770 |
assert sig_1_res._get_task_meta()["header"] == ["value"] |
| 771 |
|
| 772 |
with subtests.test("sig_2 is stamped with custom visitor", header=["value"]): |
| 773 |
assert sig_2_res._get_task_meta()["header"] == ["value"] |
| 774 |
|
| 775 |
def test_callback_stamping(self, subtests): |
| 776 |
self.app.conf.task_always_eager = True |
| 777 |
self.app.conf.task_store_eager_result = True |
| 778 |
self.app.conf.result_extended = True |
| 779 |
|
| 780 |
class CustomStampingVisitor(StampingVisitor): |
| 781 |
def on_signature(self, sig, **headers) -> dict: |
| 782 |
return {"header": "value"} |
| 783 |
|
| 784 |
def on_callback(self, callback, **header) -> dict: |
| 785 |
return {"on_callback": True} |
| 786 |
|
| 787 |
def on_errback(self, errback, **header) -> dict: |
| 788 |
return {"on_errback": True} |
| 789 |
|
| 790 |
sig_1 = self.add.s(0, 1) |
| 791 |
sig_1_res = sig_1.freeze() |
| 792 |
group_sig = group([self.add.s(3), self.add.s(4)]) |
| 793 |
group_sig_res = group_sig.freeze() |
| 794 |
chord_sig = chord([self.xsum.s(), self.xsum.s()], self.xsum.s()) |
| 795 |
chord_sig_res = chord_sig.freeze() |
| 796 |
sig_2 = self.add.s(2) |
| 797 |
sig_2_res = sig_2.freeze() |
| 798 |
chain_sig = chain( |
| 799 |
sig_1, # --> 1 |
| 800 |
group_sig, # --> [1+3, 1+4] --> [4, 5] |
| 801 |
chord_sig, # --> [4+5, 4+5] --> [9, 9] --> 9+9 --> 18 |
| 802 |
sig_2, # --> 18 + 2 --> 20 |
| 803 |
) |
| 804 |
callback = signature("callback_task") |
| 805 |
errback = signature("errback_task") |
| 806 |
chain_sig.link(callback) |
| 807 |
chain_sig.link_error(errback) |
| 808 |
chain_sig.stamp(visitor=CustomStampingVisitor()) |
| 809 |
chain_sig_res = chain_sig.apply_async() |
| 810 |
chain_sig_res.get() |
| 811 |
|
| 812 |
with subtests.test("Confirm the chain was executed correctly", result=20): |
| 813 |
# Before we run our assertions, let's confirm the base functionality of the chain is working |
| 814 |
# as expected including the links stamping. |
| 815 |
assert chain_sig_res.result == 20 |
| 816 |
|
| 817 |
with subtests.test("sig_1 is stamped with custom visitor", stamped_headers=["header"]): |
| 818 |
assert sorted(sig_1_res._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 819 |
|
| 820 |
with subtests.test("group_sig is stamped with custom visitor", stamped_headers=["header"]): |
| 821 |
for result in group_sig_res.results: |
| 822 |
assert sorted(result._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 823 |
|
| 824 |
with subtests.test("chord_sig is stamped with custom visitor", stamped_headers=["header"]): |
| 825 |
assert sorted(chord_sig_res._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 826 |
|
| 827 |
with subtests.test("sig_2 is stamped with custom visitor", stamped_headers=["header"]): |
| 828 |
assert sorted(sig_2_res._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 829 |
|
| 830 |
with subtests.test( |
| 831 |
"callback is stamped with custom visitor", |
| 832 |
stamped_headers=["header", "on_callback"], |
| 833 |
): |
| 834 |
callback_link = chain_sig.options["link"][0] |
| 835 |
headers = callback_link.options |
| 836 |
stamped_headers = headers["stamped_headers"] |
| 837 |
assert sorted(stamped_headers) == sorted(["header", "on_callback"]) |
| 838 |
assert headers["on_callback"] is True |
| 839 |
assert headers["header"] == "value" |
| 840 |
|
| 841 |
with subtests.test( |
| 842 |
"errback is stamped with custom visitor", |
| 843 |
stamped_headers=["header", "on_errback"], |
| 844 |
): |
| 845 |
errback_link = chain_sig.options["link_error"][0] |
| 846 |
headers = errback_link.options |
| 847 |
stamped_headers = headers["stamped_headers"] |
| 848 |
assert sorted(stamped_headers) == sorted(["header", "on_errback"]) |
| 849 |
assert headers["on_errback"] is True |
| 850 |
assert headers["header"] == "value" |
| 851 |
|
| 852 |
def test_callback_stamping_link_after_stamp(self, subtests): |
| 853 |
self.app.conf.task_always_eager = True |
| 854 |
self.app.conf.task_store_eager_result = True |
| 855 |
self.app.conf.result_extended = True |
| 856 |
|
| 857 |
class CustomStampingVisitor(StampingVisitor): |
| 858 |
def on_signature(self, sig, **headers) -> dict: |
| 859 |
return {"header": "value"} |
| 860 |
|
| 861 |
def on_callback(self, callback, **header) -> dict: |
| 862 |
return {"on_callback": True} |
| 863 |
|
| 864 |
def on_errback(self, errback, **header) -> dict: |
| 865 |
return {"on_errback": True} |
| 866 |
|
| 867 |
sig_1 = self.add.s(0, 1) |
| 868 |
sig_1_res = sig_1.freeze() |
| 869 |
group_sig = group([self.add.s(3), self.add.s(4)]) |
| 870 |
group_sig_res = group_sig.freeze() |
| 871 |
chord_sig = chord([self.xsum.s(), self.xsum.s()], self.xsum.s()) |
| 872 |
chord_sig_res = chord_sig.freeze() |
| 873 |
sig_2 = self.add.s(2) |
| 874 |
sig_2_res = sig_2.freeze() |
| 875 |
chain_sig = chain( |
| 876 |
sig_1, # --> 1 |
| 877 |
group_sig, # --> [1+3, 1+4] --> [4, 5] |
| 878 |
chord_sig, # --> [4+5, 4+5] --> [9, 9] --> 9+9 --> 18 |
| 879 |
sig_2, # --> 18 + 2 --> 20 |
| 880 |
) |
| 881 |
callback = signature("callback_task") |
| 882 |
errback = signature("errback_task") |
| 883 |
chain_sig.stamp(visitor=CustomStampingVisitor()) |
| 884 |
chain_sig.link(callback) |
| 885 |
chain_sig.link_error(errback) |
| 886 |
chain_sig_res = chain_sig.apply_async() |
| 887 |
chain_sig_res.get() |
| 888 |
|
| 889 |
with subtests.test("Confirm the chain was executed correctly", result=20): |
| 890 |
# Before we run our assertions, let's confirm the base functionality of the chain is working |
| 891 |
# as expected including the links stamping. |
| 892 |
assert chain_sig_res.result == 20 |
| 893 |
|
| 894 |
with subtests.test("sig_1 is stamped with custom visitor", stamped_headers=["header"]): |
| 895 |
assert sorted(sig_1_res._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 896 |
|
| 897 |
with subtests.test("group_sig is stamped with custom visitor", stamped_headers=["header"]): |
| 898 |
for result in group_sig_res.results: |
| 899 |
assert sorted(result._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 900 |
|
| 901 |
with subtests.test("chord_sig is stamped with custom visitor", stamped_headers=["header"]): |
| 902 |
assert sorted(chord_sig_res._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 903 |
|
| 904 |
with subtests.test("sig_2 is stamped with custom visitor", stamped_headers=["header"]): |
| 905 |
assert sorted(sig_2_res._get_task_meta()["stamped_headers"]) == sorted(["header"]) |
| 906 |
|
| 907 |
with subtests.test("callback is not stamped"): |
| 908 |
callback_link = chain_sig.options["link"][0] |
| 909 |
headers = callback_link.options |
| 910 |
stamped_headers = headers.get("stamped_headers", []) |
| 911 |
assert "on_callback" not in stamped_headers, "Linking after stamping should not stamp the callback" |
| 912 |
assert stamped_headers == [] |
| 913 |
|
| 914 |
with subtests.test("errback is not stamped"): |
| 915 |
errback_link = chain_sig.options["link_error"][0] |
| 916 |
headers = errback_link.options |
| 917 |
stamped_headers = headers.get("stamped_headers", []) |
| 918 |
assert "on_callback" not in stamped_headers, "Linking after stamping should not stamp the errback" |
| 919 |
assert stamped_headers == [] |
| 920 |
|
| 921 |
def test_callback_stamping_link_multiple_visitors(self, subtests): |
| 922 |
self.app.conf.task_always_eager = True |
| 923 |
self.app.conf.task_store_eager_result = True |
| 924 |
self.app.conf.result_extended = True |
| 925 |
|
| 926 |
class CustomStampingVisitor(StampingVisitor): |
| 927 |
def on_signature(self, sig, **headers) -> dict: |
| 928 |
return {"header": "value"} |
| 929 |
|
| 930 |
def on_callback(self, callback, **header) -> dict: |
| 931 |
return {"on_callback": True} |
| 932 |
|
| 933 |
def on_errback(self, errback, **header) -> dict: |
| 934 |
return {"on_errback": True} |
| 935 |
|
| 936 |
class CustomStampingVisitor2(StampingVisitor): |
| 937 |
def on_signature(self, sig, **headers) -> dict: |
| 938 |
return {"header2": "value2"} |
| 939 |
|
| 940 |
def on_callback(self, callback, **header) -> dict: |
| 941 |
return {"on_callback2": "True"} |
| 942 |
|
| 943 |
def on_errback(self, errback, **header) -> dict: |
| 944 |
return {"on_errback2": "True"} |
| 945 |
|
| 946 |
sig_1 = self.add.s(0, 1) |
| 947 |
sig_1_res = sig_1.freeze() |
| 948 |
group_sig = group([self.add.s(3), self.add.s(4)]) |
| 949 |
group_sig_res = group_sig.freeze() |
| 950 |
chord_sig = chord([self.xsum.s(), self.xsum.s()], self.xsum.s()) |
| 951 |
chord_sig_res = chord_sig.freeze() |
| 952 |
sig_2 = self.add.s(2) |
| 953 |
sig_2_res = sig_2.freeze() |
| 954 |
chain_sig = chain( |
| 955 |
sig_1, # --> 1 |
| 956 |
group_sig, # --> [1+3, 1+4] --> [4, 5] |
| 957 |
chord_sig, # --> [4+5, 4+5] --> [9, 9] --> 9+9 --> 18 |
| 958 |
sig_2, # --> 18 + 2 --> 20 |
| 959 |
) |
| 960 |
callback = signature("callback_task") |
| 961 |
errback = signature("errback_task") |
| 962 |
chain_sig.stamp(visitor=CustomStampingVisitor()) |
| 963 |
chain_sig.link(callback) |
| 964 |
chain_sig.link_error(errback) |
| 965 |
chain_sig.stamp(visitor=CustomStampingVisitor2()) |
| 966 |
chain_sig_res = chain_sig.apply_async() |
| 967 |
chain_sig_res.get() |
| 968 |
|
| 969 |
with subtests.test("Confirm the chain was executed correctly", result=20): |
| 970 |
# Before we run our assertions, let's confirm the base functionality of the chain is working |
| 971 |
# as expected including the links stamping. |
| 972 |
assert chain_sig_res.result == 20 |
| 973 |
|
| 974 |
with subtests.test("sig_1 is stamped with custom visitor", stamped_headers=["header", "header2"]): |
| 975 |
assert sorted(sig_1_res._get_task_meta()["stamped_headers"]) == sorted(["header", "header2"]) |
| 976 |
|
| 977 |
with subtests.test("group_sig is stamped with custom visitor", stamped_headers=["header", "header2"]): |
| 978 |
for result in group_sig_res.results: |
| 979 |
assert sorted(result._get_task_meta()["stamped_headers"]) == sorted(["header", "header2"]) |
| 980 |
|
| 981 |
with subtests.test("chord_sig is stamped with custom visitor", stamped_headers=["header", "header2"]): |
| 982 |
assert sorted(chord_sig_res._get_task_meta()["stamped_headers"]) == sorted(["header", "header2"]) |
| 983 |
|
| 984 |
with subtests.test("sig_2 is stamped with custom visitor", stamped_headers=["header", "header2"]): |
| 985 |
assert sorted(sig_2_res._get_task_meta()["stamped_headers"]) == sorted(["header", "header2"]) |
| 986 |
|
| 987 |
with subtests.test("callback is stamped"): |
| 988 |
callback_link = chain_sig.options["link"][0] |
| 989 |
headers = callback_link.options |
| 990 |
stamped_headers = headers.get("stamped_headers", []) |
| 991 |
assert "on_callback2" in stamped_headers, "Linking after stamping should stamp the callback" |
| 992 |
expected_stamped_headers = list(CustomStampingVisitor2().on_signature(None).keys()) |
| 993 |
expected_stamped_headers.extend(list(CustomStampingVisitor2().on_callback(None).keys())) |
| 994 |
assert sorted(stamped_headers) == sorted(expected_stamped_headers) |
| 995 |
|
| 996 |
with subtests.test("errback is stamped"): |
| 997 |
errback_link = chain_sig.options["link_error"][0] |
| 998 |
headers = errback_link.options |
| 999 |
stamped_headers = headers.get("stamped_headers", []) |
| 1000 |
assert "on_errback2" in stamped_headers, "Linking after stamping should stamp the errback" |
| 1001 |
expected_stamped_headers = list(CustomStampingVisitor2().on_signature(None).keys()) |
| 1002 |
expected_stamped_headers.extend(list(CustomStampingVisitor2().on_errback(None).keys())) |
| 1003 |
assert sorted(stamped_headers) == sorted(expected_stamped_headers) |
| 1004 |
|
| 1005 |
@pytest.mark.usefixtures("depends_on_current_app") |
| 1006 |
def test_callback_stamping_on_replace(self, subtests): |
| 1007 |
class CustomStampingVisitor(StampingVisitor): |
| 1008 |
def on_signature(self, sig, **headers) -> dict: |
| 1009 |
return {"header": "value"} |
| 1010 |
|
| 1011 |
def on_callback(self, callback, **header) -> dict: |
| 1012 |
return {"on_callback": True} |
| 1013 |
|
| 1014 |
def on_errback(self, errback, **header) -> dict: |
| 1015 |
return {"on_errback": True} |
| 1016 |
|
| 1017 |
class MyTask(Task): |
| 1018 |
def on_replace(self, sig): |
| 1019 |
sig.stamp(CustomStampingVisitor()) |
| 1020 |
return super().on_replace(sig) |
| 1021 |
|
| 1022 |
mytask = self.app.task(shared=False, base=MyTask)(return_True) |
| 1023 |
|
| 1024 |
sig1 = signature("sig1") |
| 1025 |
callback = signature("callback_task") |
| 1026 |
errback = signature("errback_task") |
| 1027 |
sig1.link(callback) |
| 1028 |
sig1.link_error(errback) |
| 1029 |
|
| 1030 |
with subtests.test("callback is not stamped with custom visitor yet"): |
| 1031 |
callback_link = sig1.options["link"][0] |
| 1032 |
headers = callback_link.options |
| 1033 |
assert "on_callback" not in headers |
| 1034 |
assert "header" not in headers |
| 1035 |
|
| 1036 |
with subtests.test("errback is not stamped with custom visitor yet"): |
| 1037 |
errback_link = sig1.options["link_error"][0] |
| 1038 |
headers = errback_link.options |
| 1039 |
assert "on_errback" not in headers |
| 1040 |
assert "header" not in headers |
| 1041 |
|
| 1042 |
with pytest.raises(Ignore): |
| 1043 |
mytask.replace(sig1) |
| 1044 |
|
| 1045 |
with subtests.test( |
| 1046 |
"callback is stamped with custom visitor", |
| 1047 |
stamped_headers=["header", "on_callback"], |
| 1048 |
): |
| 1049 |
callback_link = sig1.options["link"][0] |
| 1050 |
headers = callback_link.options |
| 1051 |
stamped_headers = headers["stamped_headers"] |
| 1052 |
assert sorted(stamped_headers) == sorted(["header", "on_callback"]) |
| 1053 |
assert headers["on_callback"] is True |
| 1054 |
assert headers["header"] == "value" |
| 1055 |
|
| 1056 |
with subtests.test( |
| 1057 |
"errback is stamped with custom visitor", |
| 1058 |
stamped_headers=["header", "on_errback"], |
| 1059 |
): |
| 1060 |
errback_link = sig1.options["link_error"][0] |
| 1061 |
headers = errback_link.options |
| 1062 |
stamped_headers = headers["stamped_headers"] |
| 1063 |
assert sorted(stamped_headers) == sorted(["header", "on_errback"]) |
| 1064 |
assert headers["on_errback"] is True |
| 1065 |
assert headers["header"] == "value" |
| 1066 |
|
| 1067 |
@pytest.mark.parametrize( |
| 1068 |
"sig_to_replace", |
| 1069 |
[ |
| 1070 |
group(signature(f"sig{i}") for i in range(2)), |
| 1071 |
group([signature("sig1"), signature("sig2")]), |
| 1072 |
group((signature("sig1"), signature("sig2"))), |
| 1073 |
group(signature("sig1"), signature("sig2")), |
| 1074 |
chain(signature("sig1"), signature("sig2")), |
| 1075 |
], |
| 1076 |
) |
| 1077 |
@pytest.mark.usefixtures("depends_on_current_app") |
| 1078 |
def test_replacing_stamped_canvas_with_tasks(self, subtests, sig_to_replace): |
| 1079 |
class CustomStampingVisitor(StampingVisitor): |
| 1080 |
def on_signature(self, sig, **headers) -> dict: |
| 1081 |
return {"header": "value"} |
| 1082 |
|
| 1083 |
class MyTask(Task): |
| 1084 |
def on_replace(self, sig): |
| 1085 |
nonlocal assertion_result |
| 1086 |
nonlocal failed_task |
| 1087 |
tasks = sig.tasks.tasks if isinstance(sig.tasks, group) else sig.tasks |
| 1088 |
assertion_result = len(tasks) == 2 |
| 1089 |
for task in tasks: |
| 1090 |
assertion_result = all( |
| 1091 |
[ |
| 1092 |
assertion_result, |
| 1093 |
"header" in task.options["stamped_headers"], |
| 1094 |
all([header in task.options for header in task.options["stamped_headers"]]), |
| 1095 |
] |
| 1096 |
) |
| 1097 |
if not assertion_result: |
| 1098 |
failed_task = task |
| 1099 |
break |
| 1100 |
|
| 1101 |
return super().on_replace(sig) |
| 1102 |
|
| 1103 |
@self.app.task(shared=False, bind=True, base=MyTask) |
| 1104 |
def replace_from_MyTask(self): |
| 1105 |
# Allows easy assertion for the test without using Mock |
| 1106 |
return self.replace(sig_to_replace) |
| 1107 |
|
| 1108 |
sig = replace_from_MyTask.s() |
| 1109 |
sig.stamp(CustomStampingVisitor()) |
| 1110 |
assertion_result = False |
| 1111 |
failed_task = None |
| 1112 |
sig.apply() |
| 1113 |
assert assertion_result, f"Task {failed_task} was not stamped correctly" |
| 1114 |
|
| 1115 |
@pytest.mark.usefixtures("depends_on_current_app") |
| 1116 |
def test_replacing_stamped_canvas_with_tasks_with_links(self): |
| 1117 |
class CustomStampingVisitor(StampingVisitor): |
| 1118 |
def on_signature(self, sig, **headers) -> dict: |
| 1119 |
return {"header": "value"} |
| 1120 |
|
| 1121 |
class MyTask(Task): |
| 1122 |
def on_replace(self, sig): |
| 1123 |
nonlocal assertion_result |
| 1124 |
nonlocal failed_task |
| 1125 |
nonlocal failed_task_link |
| 1126 |
tasks = sig.tasks.tasks if isinstance(sig.tasks, group) else sig.tasks |
| 1127 |
assertion_result = True |
| 1128 |
for task in tasks: |
| 1129 |
links = task.options["link"] |
| 1130 |
links.extend(task.options["link_error"]) |
| 1131 |
for link in links: |
| 1132 |
assertion_result = all( |
| 1133 |
[ |
| 1134 |
assertion_result, |
| 1135 |
all( |
| 1136 |
[ |
| 1137 |
stamped_header in link["options"] |
| 1138 |
for stamped_header in link["options"]["stamped_headers"] |
| 1139 |
] |
| 1140 |
), |
| 1141 |
] |
| 1142 |
) |
| 1143 |
else: |
| 1144 |
if not assertion_result: |
| 1145 |
failed_task_link = link |
| 1146 |
break |
| 1147 |
|
| 1148 |
assertion_result = all( |
| 1149 |
[ |
| 1150 |
assertion_result, |
| 1151 |
task.options["stamped_headers"]["header"] == "value", |
| 1152 |
all([header in task.options for header in task.options["stamped_headers"]]), |
| 1153 |
] |
| 1154 |
) |
| 1155 |
|
| 1156 |
if not assertion_result: |
| 1157 |
failed_task = task |
| 1158 |
break |
| 1159 |
|
| 1160 |
return super().on_replace(sig) |
| 1161 |
|
| 1162 |
@self.app.task(shared=False, bind=True, base=MyTask) |
| 1163 |
def replace_from_MyTask(self): |
| 1164 |
# Allows easy assertion for the test without using Mock |
| 1165 |
return self.replace(sig_to_replace) |
| 1166 |
|
| 1167 |
s1 = chain(signature("foo11"), signature("foo12")) |
| 1168 |
s1.link(signature("link_foo1")) |
| 1169 |
s1.link_error(signature("link_error_foo1")) |
| 1170 |
|
| 1171 |
s2 = chain(signature("foo21"), signature("foo22")) |
| 1172 |
s2.link(signature("link_foo2")) |
| 1173 |
s2.link_error(signature("link_error_foo2")) |
| 1174 |
|
| 1175 |
sig_to_replace = group([s1, s2]) |
| 1176 |
sig = replace_from_MyTask.s() |
| 1177 |
sig.stamp(CustomStampingVisitor()) |
| 1178 |
assertion_result = False |
| 1179 |
failed_task = None |
| 1180 |
failed_task_link = None |
| 1181 |
sig.apply() |
| 1182 |
|
| 1183 |
err_msg = ( |
| 1184 |
f"Task {failed_task} was not stamped correctly" |
| 1185 |
if failed_task |
| 1186 |
else f"Task link {failed_task_link} was not stamped correctly" |
| 1187 |
if failed_task_link |
| 1188 |
else "Assertion failed" |
| 1189 |
) |
| 1190 |
assert assertion_result, err_msg |
| 1191 |
|
| 1192 |
def test_group_stamping_one_level(self, subtests): |
| 1193 |
self.app.conf.task_always_eager = True |
| 1194 |
self.app.conf.task_store_eager_result = True |
| 1195 |
self.app.conf.result_extended = True |
| 1196 |
|
| 1197 |
sig_1 = self.add.s(2, 2) |
| 1198 |
sig_2 = self.add.s(4, 4) |
| 1199 |
sig_1_res = sig_1.freeze() |
| 1200 |
sig_2_res = sig_2.freeze() |
| 1201 |
|
| 1202 |
g = group(sig_1, sig_2, app=self.app) |
| 1203 |
g.stamp(stamp="stamp") |
| 1204 |
g.apply() |
| 1205 |
|
| 1206 |
with subtests.test("sig_1_res is stamped manually", stamp=["stamp"]): |
| 1207 |
assert sig_1_res._get_task_meta()["stamp"] == ["stamp"] |
| 1208 |
|
| 1209 |
with subtests.test("sig_2_res is stamped manually", stamp=["stamp"]): |
| 1210 |
assert sig_2_res._get_task_meta()["stamp"] == ["stamp"] |
| 1211 |
|
| 1212 |
with subtests.test("sig_1_res has stamped_headers", stamped_headers=["stamp"]): |
| 1213 |
assert sorted(sig_1_res._get_task_meta()["stamped_headers"]) == sorted(["stamp"]) |
| 1214 |
|
| 1215 |
with subtests.test("sig_2_res has stamped_headers", stamped_headers=["stamp"]): |
| 1216 |
assert sorted(sig_2_res._get_task_meta()["stamped_headers"]) == sorted(["stamp"]) |
| 1217 |
|
| 1218 |
def test_chord_stamping_one_level(self, subtests): |
| 1219 |
""" |
| 1220 |
In the case of group within a chord that is from another canvas |
| 1221 |
element, ensure that chord stamps are added correctly when chord are |
| 1222 |
run in parallel. |
| 1223 |
""" |
| 1224 |
self.app.conf.task_always_eager = True |
| 1225 |
self.app.conf.task_store_eager_result = True |
| 1226 |
self.app.conf.result_extended = True |
| 1227 |
|
| 1228 |
sig_1 = self.add.s(2, 2) |
| 1229 |
sig_2 = self.add.s(4, 4) |
| 1230 |
sig_1_res = sig_1.freeze() |
| 1231 |
sig_2_res = sig_2.freeze() |
| 1232 |
sig_sum = self.xsum.s() |
| 1233 |
|
| 1234 |
g = chord([sig_1, sig_2], sig_sum, app=self.app) |
| 1235 |
g.stamp(stamp="stamp") |
| 1236 |
g.freeze() |
| 1237 |
g.apply() |
| 1238 |
|
| 1239 |
with subtests.test("sig_1_res is stamped manually", stamp=["stamp"]): |
| 1240 |
assert sig_1_res._get_task_meta()["stamp"] == ["stamp"] |
| 1241 |
|
| 1242 |
with subtests.test("sig_2_res is stamped manually", stamp=["stamp"]): |
| 1243 |
assert sig_2_res._get_task_meta()["stamp"] == ["stamp"] |
| 1244 |
|
| 1245 |
with subtests.test("sig_1_res has stamped_headers", stamped_headers=["stamp"]): |
| 1246 |
assert sorted(sig_1_res._get_task_meta()["stamped_headers"]) == sorted(["stamp"]) |
| 1247 |
|
| 1248 |
with subtests.test("sig_2_res has stamped_headers", stamped_headers=["stamp"]): |
| 1249 |
assert sorted(sig_2_res._get_task_meta()["stamped_headers"]) == sorted(["stamp"]) |
| 1250 |
|
| 1251 |
def test_retry_stamping(self): |
| 1252 |
self.retry_task.push_request() |
| 1253 |
self.retry_task.request.stamped_headers = ['stamp'] |
| 1254 |
self.retry_task.request.stamps = {'stamp': 'value'} |
| 1255 |
sig = self.retry_task.signature_from_request() |
| 1256 |
assert sig.options['stamped_headers'] == ['stamp'] |
| 1257 |
assert sig.options['stamp'] == 'value' |
| 1258 |
|
| 1259 |
def test_link_error_does_not_duplicate_stamps(self, subtests): |
| 1260 |
class CustomStampingVisitor(StampingVisitor): |
| 1261 |
def on_group_start(self, group, **headers): |
| 1262 |
return {} |
| 1263 |
|
| 1264 |
def on_chain_start(self, chain, **headers): |
| 1265 |
return {} |
| 1266 |
|
| 1267 |
def on_signature(self, sig, **headers): |
| 1268 |
existing_headers = sig.options.get("headers") or {} |
| 1269 |
existing_stamps = existing_headers.get("stamps") or {} |
| 1270 |
existing_stamp = existing_stamps.get("stamp") |
| 1271 |
existing_stamp = existing_stamp or sig.options.get("stamp") |
| 1272 |
if existing_stamp is None: |
| 1273 |
stamp = str(uuid.uuid4()) |
| 1274 |
return {"stamp": stamp} |
| 1275 |
else: |
| 1276 |
assert False, "stamp already exists" |
| 1277 |
|
| 1278 |
def s(n, fail_flag=False): |
| 1279 |
if not fail_flag: |
| 1280 |
return self.identity.si(str(n)) |
| 1281 |
return self.fail.si(str(n)) |
| 1282 |
|
| 1283 |
def tasks(): |
| 1284 |
tasks = [] |
| 1285 |
for i in range(0, 4): |
| 1286 |
fail_flag = False |
| 1287 |
if i: |
| 1288 |
fail_flag = True |
| 1289 |
sig = s(i, fail_flag) |
| 1290 |
sig.link(s(f"link{str(i)}")) |
| 1291 |
sig.link_error(s(f"link_error{str(i)}")) |
| 1292 |
tasks.append(sig) |
| 1293 |
return tasks |
| 1294 |
|
| 1295 |
with subtests.test("group"): |
| 1296 |
canvas = group(tasks()) |
| 1297 |
canvas.link_error(s("group_link_error")) |
| 1298 |
canvas.stamp(CustomStampingVisitor()) |
| 1299 |
|
| 1300 |
with subtests.test("chord header"): |
| 1301 |
self.app.conf.task_allow_error_cb_on_chord_header = True |
| 1302 |
canvas = chord(tasks(), self.identity.si("body"), app=self.app) |
| 1303 |
canvas.link_error(s("group_link_error")) |
| 1304 |
canvas.stamp(CustomStampingVisitor()) |
| 1305 |
|
| 1306 |
with subtests.test("chord body"): |
| 1307 |
self.app.conf.task_allow_error_cb_on_chord_header = False |
| 1308 |
canvas = chord(tasks(), self.identity.si("body"), app=self.app) |
| 1309 |
canvas.link_error(s("group_link_error")) |
| 1310 |
canvas.stamp(CustomStampingVisitor()) |
| 1311 |
|
| 1312 |
with subtests.test("chain"): |
| 1313 |
canvas = chain(tasks()) |
| 1314 |
canvas.link_error(s("chain_link_error")) |
| 1315 |
canvas.stamp(CustomStampingVisitor()) |