| 1 |
import json |
| 2 |
import math |
| 3 |
from collections.abc import Iterable |
| 4 |
from unittest.mock import ANY, MagicMock, Mock, call, patch, sentinel |
| 5 |
|
| 6 |
import pytest |
| 7 |
|
| 8 |
from celery._state import _task_stack |
| 9 |
from celery.canvas import (Signature, _chain, _maybe_group, _merge_dictionaries, chain, chord, chunks, group, |
| 10 |
maybe_signature, maybe_unroll_group, signature, xmap, xstarmap) |
| 11 |
from celery.result import AsyncResult, EagerResult, GroupResult |
| 12 |
|
| 13 |
SIG = Signature({ |
| 14 |
'task': 'TASK', |
| 15 |
'args': ('A1',), |
| 16 |
'kwargs': {'K1': 'V1'}, |
| 17 |
'options': {'task_id': 'TASK_ID'}, |
| 18 |
'subtask_type': ''}, |
| 19 |
) |
| 20 |
|
| 21 |
|
| 22 |
def return_True(*args, **kwargs): |
| 23 |
# Task run functions can't be closures/lambdas, as they're pickled. |
| 24 |
return True |
| 25 |
|
| 26 |
|
| 27 |
class test_maybe_unroll_group: |
| 28 |
|
| 29 |
def test_when_no_len_and_no_length_hint(self): |
| 30 |
g = MagicMock(name='group') |
| 31 |
g.tasks.__len__.side_effect = TypeError() |
| 32 |
g.tasks.__length_hint__ = Mock() |
| 33 |
g.tasks.__length_hint__.return_value = 0 |
| 34 |
assert maybe_unroll_group(g) is g |
| 35 |
g.tasks.__length_hint__.side_effect = AttributeError() |
| 36 |
assert maybe_unroll_group(g) is g |
| 37 |
|
| 38 |
|
| 39 |
class CanvasCase: |
| 40 |
|
| 41 |
def setup_method(self): |
| 42 |
@self.app.task(shared=False) |
| 43 |
def add(x, y): |
| 44 |
return x + y |
| 45 |
|
| 46 |
self.add = add |
| 47 |
|
| 48 |
@self.app.task(shared=False) |
| 49 |
def mul(x, y): |
| 50 |
return x * y |
| 51 |
|
| 52 |
self.mul = mul |
| 53 |
|
| 54 |
@self.app.task(shared=False) |
| 55 |
def div(x, y): |
| 56 |
return x / y |
| 57 |
|
| 58 |
self.div = div |
| 59 |
|
| 60 |
@self.app.task(shared=False) |
| 61 |
def xsum(numbers): |
| 62 |
return sum(sum(num) if isinstance(num, Iterable) else num for num in numbers) |
| 63 |
|
| 64 |
self.xsum = xsum |
| 65 |
|
| 66 |
@self.app.task(shared=False, bind=True) |
| 67 |
def replaced(self, x, y): |
| 68 |
return self.replace(add.si(x, y)) |
| 69 |
|
| 70 |
self.replaced = replaced |
| 71 |
|
| 72 |
@self.app.task(shared=False, bind=True) |
| 73 |
def replaced_group(self, x, y): |
| 74 |
return self.replace(group(add.si(x, y), mul.si(x, y))) |
| 75 |
|
| 76 |
self.replaced_group = replaced_group |
| 77 |
|
| 78 |
@self.app.task(shared=False, bind=True) |
| 79 |
def replace_with_group(self, x, y): |
| 80 |
return self.replace(group(add.si(x, y), mul.si(x, y))) |
| 81 |
|
| 82 |
self.replace_with_group = replace_with_group |
| 83 |
|
| 84 |
@self.app.task(shared=False, bind=True) |
| 85 |
def replace_with_chain(self, x, y): |
| 86 |
return self.replace(group(add.si(x, y) | mul.s(y), add.si(x, y))) |
| 87 |
|
| 88 |
self.replace_with_chain = replace_with_chain |
| 89 |
|
| 90 |
@self.app.task(shared=False) |
| 91 |
def xprod(numbers): |
| 92 |
return math.prod(numbers) |
| 93 |
|
| 94 |
self.xprod = xprod |
| 95 |
|
| 96 |
|
| 97 |
@Signature.register_type() |
| 98 |
class chord_subclass(chord): |
| 99 |
def __init__(self, *args, **kwargs): |
| 100 |
super().__init__(*args, **kwargs) |
| 101 |
self.subtask_type = "chord_subclass" |
| 102 |
|
| 103 |
|
| 104 |
@Signature.register_type() |
| 105 |
class group_subclass(group): |
| 106 |
def __init__(self, *args, **kwargs): |
| 107 |
super().__init__(*args, **kwargs) |
| 108 |
self.subtask_type = "group_subclass" |
| 109 |
|
| 110 |
|
| 111 |
@Signature.register_type() |
| 112 |
class chain_subclass(chain): |
| 113 |
def __init__(self, *args, **kwargs): |
| 114 |
super().__init__(*args, **kwargs) |
| 115 |
self.subtask_type = "chain_subclass" |
| 116 |
|
| 117 |
|
| 118 |
@Signature.register_type() |
| 119 |
class chunks_subclass(chunks): |
| 120 |
def __init__(self, *args, **kwargs): |
| 121 |
super().__init__(*args, **kwargs) |
| 122 |
self.subtask_type = "chunks_subclass" |
| 123 |
|
| 124 |
|
| 125 |
class test_Signature(CanvasCase): |
| 126 |
def test_getitem_property_class(self): |
| 127 |
assert Signature.task |
| 128 |
assert Signature.args |
| 129 |
assert Signature.kwargs |
| 130 |
assert Signature.options |
| 131 |
assert Signature.subtask_type |
| 132 |
|
| 133 |
def test_getitem_property(self): |
| 134 |
assert SIG.task == 'TASK' |
| 135 |
assert SIG.args == ('A1',) |
| 136 |
assert SIG.kwargs == {'K1': 'V1'} |
| 137 |
assert SIG.options == {'task_id': 'TASK_ID'} |
| 138 |
assert SIG.subtask_type == '' |
| 139 |
|
| 140 |
def test_call(self): |
| 141 |
x = Signature('foo', (1, 2), {'arg1': 33}, app=self.app) |
| 142 |
x.type = Mock(name='type') |
| 143 |
x(3, 4, arg2=66) |
| 144 |
x.type.assert_called_with(3, 4, 1, 2, arg1=33, arg2=66) |
| 145 |
|
| 146 |
def test_link_on_scalar(self): |
| 147 |
x = Signature('TASK', link=Signature('B')) |
| 148 |
assert x.options['link'] |
| 149 |
x.link(Signature('C')) |
| 150 |
assert isinstance(x.options['link'], list) |
| 151 |
assert Signature('B') in x.options['link'] |
| 152 |
assert Signature('C') in x.options['link'] |
| 153 |
|
| 154 |
def test_json(self): |
| 155 |
x = Signature('TASK', link=Signature('B', app=self.app), app=self.app) |
| 156 |
assert x.__json__() == dict(x) |
| 157 |
|
| 158 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 159 |
def test_reduce(self): |
| 160 |
x = Signature('TASK', (2, 4), app=self.app) |
| 161 |
fun, args = x.__reduce__() |
| 162 |
assert fun(*args) == x |
| 163 |
|
| 164 |
def test_replace(self): |
| 165 |
x = Signature('TASK', ('A',), {}) |
| 166 |
assert x.replace(args=('B',)).args == ('B',) |
| 167 |
assert x.replace(kwargs={'FOO': 'BAR'}).kwargs == { |
| 168 |
'FOO': 'BAR', |
| 169 |
} |
| 170 |
assert x.replace(options={'task_id': '123'}).options == { |
| 171 |
'task_id': '123', |
| 172 |
} |
| 173 |
|
| 174 |
def test_set(self): |
| 175 |
assert Signature('TASK', x=1).set(task_id='2').options == { |
| 176 |
'x': 1, 'task_id': '2', |
| 177 |
} |
| 178 |
|
| 179 |
def test_link(self): |
| 180 |
x = signature(SIG) |
| 181 |
x.link(SIG) |
| 182 |
x.link(SIG) |
| 183 |
assert SIG in x.options['link'] |
| 184 |
assert len(x.options['link']) == 1 |
| 185 |
|
| 186 |
def test_link_error(self): |
| 187 |
x = signature(SIG) |
| 188 |
x.link_error(SIG) |
| 189 |
x.link_error(SIG) |
| 190 |
assert SIG in x.options['link_error'] |
| 191 |
assert len(x.options['link_error']) == 1 |
| 192 |
|
| 193 |
def test_flatten_links(self): |
| 194 |
tasks = [self.add.s(2, 2), self.mul.s(4), self.div.s(2)] |
| 195 |
tasks[0].link(tasks[1]) |
| 196 |
tasks[1].link(tasks[2]) |
| 197 |
assert tasks[0].flatten_links() == tasks |
| 198 |
|
| 199 |
def test_OR(self, subtests): |
| 200 |
x = self.add.s(2, 2) | self.mul.s(4) |
| 201 |
assert isinstance(x, _chain) |
| 202 |
y = self.add.s(4, 4) | self.div.s(2) |
| 203 |
z = x | y |
| 204 |
assert isinstance(y, _chain) |
| 205 |
assert isinstance(z, _chain) |
| 206 |
assert len(z.tasks) == 4 |
| 207 |
with pytest.raises(TypeError): |
| 208 |
x | 10 |
| 209 |
ax = self.add.s(2, 2) | (self.add.s(4) | self.add.s(8)) |
| 210 |
assert isinstance(ax, _chain) |
| 211 |
assert len(ax.tasks), 3 == 'consolidates chain to chain' |
| 212 |
|
| 213 |
with subtests.test('Test chaining with a non-signature object'): |
| 214 |
with pytest.raises(TypeError): |
| 215 |
assert signature('foo') | None |
| 216 |
|
| 217 |
def test_INVERT(self): |
| 218 |
x = self.add.s(2, 2) |
| 219 |
x.apply_async = Mock() |
| 220 |
x.apply_async.return_value = Mock() |
| 221 |
x.apply_async.return_value.get = Mock() |
| 222 |
x.apply_async.return_value.get.return_value = 4 |
| 223 |
assert ~x == 4 |
| 224 |
x.apply_async.assert_called() |
| 225 |
|
| 226 |
def test_merge_immutable(self): |
| 227 |
x = self.add.si(2, 2, foo=1) |
| 228 |
args, kwargs, options = x._merge((4,), {'bar': 2}, {'task_id': 3}) |
| 229 |
assert args == (2, 2) |
| 230 |
assert kwargs == {'foo': 1} |
| 231 |
assert options == {'task_id': 3} |
| 232 |
|
| 233 |
def test_merge_options__none(self): |
| 234 |
sig = self.add.si() |
| 235 |
_, _, new_options = sig._merge() |
| 236 |
assert new_options is sig.options |
| 237 |
_, _, new_options = sig._merge(options=None) |
| 238 |
assert new_options is sig.options |
| 239 |
|
| 240 |
@pytest.mark.parametrize("immutable_sig", (True, False)) |
| 241 |
def test_merge_options__group_id(self, immutable_sig): |
| 242 |
# This is to avoid testing the behaviour in `test_set_immutable()` |
| 243 |
if immutable_sig: |
| 244 |
sig = self.add.si() |
| 245 |
else: |
| 246 |
sig = self.add.s() |
| 247 |
# If the signature has no group ID, it can be set |
| 248 |
assert not sig.options |
| 249 |
_, _, new_options = sig._merge(options={"group_id": sentinel.gid}) |
| 250 |
assert new_options == {"group_id": sentinel.gid} |
| 251 |
# But if one is already set, the new one is silently ignored |
| 252 |
sig.set(group_id=sentinel.old_gid) |
| 253 |
_, _, new_options = sig._merge(options={"group_id": sentinel.new_gid}) |
| 254 |
assert new_options == {"group_id": sentinel.old_gid} |
| 255 |
|
| 256 |
def test_set_immutable(self): |
| 257 |
x = self.add.s(2, 2) |
| 258 |
assert not x.immutable |
| 259 |
x.set(immutable=True) |
| 260 |
assert x.immutable |
| 261 |
x.set(immutable=False) |
| 262 |
assert not x.immutable |
| 263 |
|
| 264 |
def test_election(self): |
| 265 |
x = self.add.s(2, 2) |
| 266 |
x.freeze('foo') |
| 267 |
x.type.app.control = Mock() |
| 268 |
r = x.election() |
| 269 |
x.type.app.control.election.assert_called() |
| 270 |
assert r.id == 'foo' |
| 271 |
|
| 272 |
def test_AsyncResult_when_not_registered(self): |
| 273 |
s = signature('xxx.not.registered', app=self.app) |
| 274 |
assert s.AsyncResult |
| 275 |
|
| 276 |
def test_apply_async_when_not_registered(self): |
| 277 |
s = signature('xxx.not.registered', app=self.app) |
| 278 |
assert s._apply_async |
| 279 |
|
| 280 |
def test_keeping_link_error_on_chaining(self): |
| 281 |
x = self.add.s(2, 2) | self.mul.s(4) |
| 282 |
assert isinstance(x, _chain) |
| 283 |
x.link_error(SIG) |
| 284 |
assert SIG in x.options['link_error'] |
| 285 |
|
| 286 |
t = signature(SIG) |
| 287 |
z = x | t |
| 288 |
assert isinstance(z, _chain) |
| 289 |
assert t in z.tasks |
| 290 |
assert not z.options.get('link_error') |
| 291 |
assert SIG in z.tasks[0].options['link_error'] |
| 292 |
assert not z.tasks[2].options.get('link_error') |
| 293 |
assert SIG in x.options['link_error'] |
| 294 |
assert t not in x.tasks |
| 295 |
assert not x.tasks[0].options.get('link_error') |
| 296 |
|
| 297 |
z = t | x |
| 298 |
assert isinstance(z, _chain) |
| 299 |
assert t in z.tasks |
| 300 |
assert not z.options.get('link_error') |
| 301 |
assert SIG in z.tasks[1].options['link_error'] |
| 302 |
assert not z.tasks[0].options.get('link_error') |
| 303 |
assert SIG in x.options['link_error'] |
| 304 |
assert t not in x.tasks |
| 305 |
assert not x.tasks[0].options.get('link_error') |
| 306 |
|
| 307 |
y = self.add.s(4, 4) | self.div.s(2) |
| 308 |
assert isinstance(y, _chain) |
| 309 |
|
| 310 |
z = x | y |
| 311 |
assert isinstance(z, _chain) |
| 312 |
assert not z.options.get('link_error') |
| 313 |
assert SIG in z.tasks[0].options['link_error'] |
| 314 |
assert not z.tasks[2].options.get('link_error') |
| 315 |
assert SIG in x.options['link_error'] |
| 316 |
assert not x.tasks[0].options.get('link_error') |
| 317 |
|
| 318 |
z = y | x |
| 319 |
assert isinstance(z, _chain) |
| 320 |
assert not z.options.get('link_error') |
| 321 |
assert SIG in z.tasks[3].options['link_error'] |
| 322 |
assert not z.tasks[1].options.get('link_error') |
| 323 |
assert SIG in x.options['link_error'] |
| 324 |
assert not x.tasks[0].options.get('link_error') |
| 325 |
|
| 326 |
def test_signature_on_error_adds_error_callback(self): |
| 327 |
sig = signature('sig').on_error(signature('on_error')) |
| 328 |
assert sig.options['link_error'] == [signature('on_error')] |
| 329 |
|
| 330 |
@pytest.mark.parametrize('_id, group_id, chord, root_id, parent_id, group_index', [ |
| 331 |
('_id', 'group_id', 'chord', 'root_id', 'parent_id', 1), |
| 332 |
]) |
| 333 |
def test_freezing_args_set_in_options(self, _id, group_id, chord, root_id, parent_id, group_index): |
| 334 |
sig = self.add.s(1, 1) |
| 335 |
sig.freeze( |
| 336 |
_id=_id, |
| 337 |
group_id=group_id, |
| 338 |
chord=chord, |
| 339 |
root_id=root_id, |
| 340 |
parent_id=parent_id, |
| 341 |
group_index=group_index, |
| 342 |
) |
| 343 |
options = sig.options |
| 344 |
|
| 345 |
assert options['task_id'] == _id |
| 346 |
assert options['group_id'] == group_id |
| 347 |
assert options['chord'] == chord |
| 348 |
assert options['root_id'] == root_id |
| 349 |
assert options['parent_id'] == parent_id |
| 350 |
assert options['group_index'] == group_index |
| 351 |
|
| 352 |
|
| 353 |
class test_xmap_xstarmap(CanvasCase): |
| 354 |
|
| 355 |
def test_apply(self): |
| 356 |
for type, attr in [(xmap, 'map'), (xstarmap, 'starmap')]: |
| 357 |
args = [(i, i) for i in range(10)] |
| 358 |
s = getattr(self.add, attr)(args) |
| 359 |
s.type = Mock() |
| 360 |
|
| 361 |
s.apply_async(foo=1) |
| 362 |
s.type.apply_async.assert_called_with( |
| 363 |
(), {'task': self.add.s(), 'it': args}, foo=1, |
| 364 |
route_name=self.add.name, |
| 365 |
) |
| 366 |
|
| 367 |
assert type.from_dict(dict(s)) == s |
| 368 |
assert repr(s) |
| 369 |
|
| 370 |
|
| 371 |
class test_chunks(CanvasCase): |
| 372 |
|
| 373 |
def test_chunks_preserves_state(self): |
| 374 |
x = self.add.chunks(range(100), 10) |
| 375 |
d = dict(x) |
| 376 |
d['subtask_type'] = "chunks_subclass" |
| 377 |
isinstance(chunks_subclass.from_dict(d), chunks_subclass) |
| 378 |
isinstance(chunks_subclass.from_dict(d).clone(), chunks_subclass) |
| 379 |
|
| 380 |
def test_chunks(self): |
| 381 |
x = self.add.chunks(range(100), 10) |
| 382 |
assert dict(chunks.from_dict(dict(x), app=self.app)) == dict(x) |
| 383 |
|
| 384 |
assert x.group() |
| 385 |
assert len(x.group().tasks) == 10 |
| 386 |
|
| 387 |
x.group = Mock() |
| 388 |
gr = x.group.return_value = Mock() |
| 389 |
|
| 390 |
x.apply_async() |
| 391 |
gr.apply_async.assert_called_with((), {}, route_name=self.add.name) |
| 392 |
gr.apply_async.reset_mock() |
| 393 |
x() |
| 394 |
gr.apply_async.assert_called_with((), {}, route_name=self.add.name) |
| 395 |
|
| 396 |
self.app.conf.task_always_eager = True |
| 397 |
chunks.apply_chunks(app=self.app, **x['kwargs']) |
| 398 |
|
| 399 |
|
| 400 |
class test_chain(CanvasCase): |
| 401 |
|
| 402 |
def test_chain_of_chain_with_a_single_task(self): |
| 403 |
s = self.add.s(1, 1) |
| 404 |
assert chain([chain(s)]).tasks == list(chain(s).tasks) |
| 405 |
|
| 406 |
@pytest.mark.parametrize("chain_type", (_chain, chain_subclass)) |
| 407 |
def test_clone_preserves_state(self, chain_type): |
| 408 |
x = chain_type(self.add.s(i, i) for i in range(10)) |
| 409 |
assert x.clone().tasks == x.tasks |
| 410 |
assert x.clone().kwargs == x.kwargs |
| 411 |
assert x.clone().args == x.args |
| 412 |
assert isinstance(x.clone(), chain_type) |
| 413 |
|
| 414 |
def test_repr(self): |
| 415 |
x = self.add.s(2, 2) | self.add.s(2) |
| 416 |
assert repr(x) == f'{self.add.name}(2, 2) | add(2)' |
| 417 |
|
| 418 |
def test_apply_async(self): |
| 419 |
c = self.add.s(2, 2) | self.add.s(4) | self.add.s(8) |
| 420 |
result = c.apply_async() |
| 421 |
assert result.parent |
| 422 |
assert result.parent.parent |
| 423 |
assert result.parent.parent.parent is None |
| 424 |
|
| 425 |
@pytest.mark.parametrize("chain_type", (_chain, chain_subclass)) |
| 426 |
def test_splices_chains(self, chain_type): |
| 427 |
c = chain_type( |
| 428 |
self.add.s(5, 5), |
| 429 |
chain_type(self.add.s(6), self.add.s(7), self.add.s(8), app=self.app), |
| 430 |
app=self.app, |
| 431 |
) |
| 432 |
c.freeze() |
| 433 |
tasks, _ = c._frozen |
| 434 |
assert len(tasks) == 4 |
| 435 |
assert isinstance(c, chain_type) |
| 436 |
|
| 437 |
@pytest.mark.parametrize("chain_type", [_chain, chain_subclass]) |
| 438 |
def test_from_dict_no_tasks(self, chain_type): |
| 439 |
assert chain_type.from_dict(dict(chain_type(app=self.app)), app=self.app) |
| 440 |
assert isinstance(chain_type.from_dict(dict(chain_type(app=self.app)), app=self.app), chain_type) |
| 441 |
|
| 442 |
@pytest.mark.parametrize("chain_type", [_chain, chain_subclass]) |
| 443 |
def test_from_dict_full_subtasks(self, chain_type): |
| 444 |
c = chain_type(self.add.si(1, 2), self.add.si(3, 4), self.add.si(5, 6)) |
| 445 |
serialized = json.loads(json.dumps(c)) |
| 446 |
deserialized = chain_type.from_dict(serialized) |
| 447 |
assert all(isinstance(task, Signature) for task in deserialized.tasks) |
| 448 |
assert isinstance(deserialized, chain_type) |
| 449 |
|
| 450 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 451 |
def test_app_falls_back_to_default(self): |
| 452 |
from celery._state import current_app |
| 453 |
assert chain().app is current_app |
| 454 |
|
| 455 |
def test_handles_dicts(self): |
| 456 |
c = chain( |
| 457 |
self.add.s(5, 5), dict(self.add.s(8)), app=self.app, |
| 458 |
) |
| 459 |
c.freeze() |
| 460 |
tasks, _ = c._frozen |
| 461 |
assert all(isinstance(task, Signature) for task in tasks) |
| 462 |
assert all(task.app is self.app for task in tasks) |
| 463 |
|
| 464 |
def test_groups_in_chain_to_chord(self): |
| 465 |
g1 = group([self.add.s(2, 2), self.add.s(4, 4)]) |
| 466 |
g2 = group([self.add.s(3, 3), self.add.s(5, 5)]) |
| 467 |
c = g1 | g2 |
| 468 |
assert isinstance(c, chord) |
| 469 |
|
| 470 |
def test_prepare_steps_set_last_task_id_to_chain(self): |
| 471 |
last_task = self.add.s(2).set(task_id='42') |
| 472 |
c = self.add.s(4) | last_task |
| 473 |
assert c.id is None |
| 474 |
tasks, _ = c.prepare_steps((), {}, c.tasks, last_task_id=last_task.id) |
| 475 |
assert c.id == last_task.id |
| 476 |
|
| 477 |
def test_group_to_chord(self): |
| 478 |
c = ( |
| 479 |
self.add.s(5) | |
| 480 |
group([self.add.s(i, i) for i in range(5)], app=self.app) | |
| 481 |
self.add.s(10) | |
| 482 |
self.add.s(20) | |
| 483 |
self.add.s(30) |
| 484 |
) |
| 485 |
c._use_link = True |
| 486 |
tasks, results = c.prepare_steps((), {}, c.tasks) |
| 487 |
|
| 488 |
assert tasks[-1].args[0] == 5 |
| 489 |
assert isinstance(tasks[-2], chord) |
| 490 |
assert len(tasks[-2].tasks) == 5 |
| 491 |
|
| 492 |
body = tasks[-2].body |
| 493 |
assert len(body.tasks) == 3 |
| 494 |
assert body.tasks[0].args[0] == 10 |
| 495 |
assert body.tasks[1].args[0] == 20 |
| 496 |
assert body.tasks[2].args[0] == 30 |
| 497 |
|
| 498 |
c2 = self.add.s(2, 2) | group(self.add.s(i, i) for i in range(10)) |
| 499 |
c2._use_link = True |
| 500 |
tasks2, _ = c2.prepare_steps((), {}, c2.tasks) |
| 501 |
assert isinstance(tasks2[0], group) |
| 502 |
|
| 503 |
def test_group_to_chord__protocol_2__or(self): |
| 504 |
c = ( |
| 505 |
group([self.add.s(i, i) for i in range(5)], app=self.app) | |
| 506 |
self.add.s(10) | |
| 507 |
self.add.s(20) | |
| 508 |
self.add.s(30) |
| 509 |
) |
| 510 |
assert isinstance(c, chord) |
| 511 |
|
| 512 |
def test_group_to_chord__protocol_2(self): |
| 513 |
c = chain( |
| 514 |
group([self.add.s(i, i) for i in range(5)], app=self.app), |
| 515 |
self.add.s(10), |
| 516 |
self.add.s(20), |
| 517 |
self.add.s(30) |
| 518 |
) |
| 519 |
assert isinstance(c, chord) |
| 520 |
assert isinstance(c.body, _chain) |
| 521 |
assert len(c.body.tasks) == 3 |
| 522 |
|
| 523 |
c2 = self.add.s(2, 2) | group(self.add.s(i, i) for i in range(10)) |
| 524 |
c2._use_link = False |
| 525 |
tasks2, _ = c2.prepare_steps((), {}, c2.tasks) |
| 526 |
assert isinstance(tasks2[0], group) |
| 527 |
|
| 528 |
def test_chord_to_chain(self): |
| 529 |
c = ( |
| 530 |
chord([self.add.s('x0', 'y0'), self.add.s('x1', 'y1')], |
| 531 |
self.add.s(['foo'])) | |
| 532 |
chain(self.add.s(['y']), self.add.s(['z'])) |
| 533 |
) |
| 534 |
assert isinstance(c, _chain) |
| 535 |
assert c.apply().get() == ['x0y0', 'x1y1', 'foo', 'y', 'z'] |
| 536 |
|
| 537 |
def test_chord_to_group(self): |
| 538 |
c = ( |
| 539 |
chord([self.add.s('x0', 'y0'), self.add.s('x1', 'y1')], |
| 540 |
self.add.s(['foo'])) | |
| 541 |
group([self.add.s(['y']), self.add.s(['z'])]) |
| 542 |
) |
| 543 |
assert isinstance(c, _chain) |
| 544 |
assert c.apply().get() == [ |
| 545 |
['x0y0', 'x1y1', 'foo', 'y'], |
| 546 |
['x0y0', 'x1y1', 'foo', 'z'] |
| 547 |
] |
| 548 |
|
| 549 |
def test_chain_of_chord__or__group_of_single_task(self): |
| 550 |
c = chord([signature('header')], signature('body')) |
| 551 |
c = chain(c) |
| 552 |
g = group(signature('t')) |
| 553 |
new_chain = c | g # g should be chained with the body of c[0] |
| 554 |
assert isinstance(new_chain, _chain) |
| 555 |
assert isinstance(new_chain.tasks[0].body, _chain) |
| 556 |
|
| 557 |
def test_chain_of_chord_upgrade_on_chaining(self): |
| 558 |
c = chord([signature('header')], group(signature('body'))) |
| 559 |
c = chain(c) |
| 560 |
t = signature('t') |
| 561 |
new_chain = c | t # t should be chained with the body of c[0] and create a new chord |
| 562 |
assert isinstance(new_chain, _chain) |
| 563 |
assert isinstance(new_chain.tasks[0].body, chord) |
| 564 |
|
| 565 |
@pytest.mark.parametrize( |
| 566 |
"group_last_task", |
| 567 |
[False, True], |
| 568 |
) |
| 569 |
def test_chain_of_chord_upgrade_on_chaining__protocol_2( |
| 570 |
self, group_last_task): |
| 571 |
c = chain( |
| 572 |
group([self.add.s(i, i) for i in range(5)], app=self.app), |
| 573 |
group([self.add.s(i, i) for i in range(10, 15)], app=self.app), |
| 574 |
group([self.add.s(i, i) for i in range(20, 25)], app=self.app), |
| 575 |
self.add.s(30) if not group_last_task else group(self.add.s(30), |
| 576 |
app=self.app)) |
| 577 |
assert isinstance(c, _chain) |
| 578 |
assert len( |
| 579 |
c.tasks |
| 580 |
) == 1, "Consecutive chords should be further upgraded to a single chord." |
| 581 |
assert isinstance(c.tasks[0], chord) |
| 582 |
|
| 583 |
def test_chain_of_chord_upgrade_on_chaining__protocol_3(self): |
| 584 |
c = chain( |
| 585 |
chain([self.add.s(i, i) for i in range(5)]), |
| 586 |
group([self.add.s(i, i) for i in range(10, 15)], app=self.app), |
| 587 |
chord([signature('header')], signature('body'), app=self.app), |
| 588 |
group([self.add.s(i, i) for i in range(20, 25)], app=self.app)) |
| 589 |
assert isinstance(c, _chain) |
| 590 |
assert isinstance( |
| 591 |
c.tasks[-1], chord |
| 592 |
), "Chord followed by a group should be upgraded to a single chord with chained body." |
| 593 |
assert len(c.tasks) == 6 |
| 594 |
|
| 595 |
def test_chain_of_chords_stays_flat(self): |
| 596 |
c = chain( |
| 597 |
chord([signature('h1'), signature('h2')], signature('b1'), app=self.app), |
| 598 |
chord([signature('h3'), signature('h4')], signature('b2'), app=self.app), |
| 599 |
chord([signature('h5'), signature('h6')], signature('b3'), app=self.app), |
| 600 |
) |
| 601 |
assert isinstance(c, _chain) |
| 602 |
assert len(c.tasks) == 3 |
| 603 |
for task in c.tasks: |
| 604 |
assert isinstance(task, chord) |
| 605 |
assert not isinstance(c.tasks[0].body, _chain) |
| 606 |
assert not isinstance(c.tasks[1].body, _chain) |
| 607 |
assert not isinstance(c.tasks[2].body, _chain) |
| 608 |
|
| 609 |
def test_chain_of_chords_serialized_size_constant(self): |
| 610 |
chords = [ |
| 611 |
chord([signature(f'h{i}_{j}') for j in range(3)], |
| 612 |
signature(f'b{i}'), app=self.app) |
| 613 |
for i in range(6) |
| 614 |
] |
| 615 |
c = chain(*chords) |
| 616 |
assert isinstance(c, _chain) |
| 617 |
sizes = [len(json.dumps(task.__json__())) for task in c.tasks] |
| 618 |
assert max(sizes) == min(sizes), ( |
| 619 |
f"Chord sizes not constant across chain: {sizes}" |
| 620 |
) |
| 621 |
|
| 622 |
def test_chord_or_task_still_nests(self): |
| 623 |
c = chord([signature('h1')], signature('b1'), app=self.app) |
| 624 |
t = signature('t1') |
| 625 |
result = chain(c) | t |
| 626 |
assert isinstance(result, _chain) |
| 627 |
assert isinstance(result.tasks[0].body, _chain) |
| 628 |
|
| 629 |
def test_apply_options(self): |
| 630 |
|
| 631 |
class static(Signature): |
| 632 |
|
| 633 |
def clone(self, *args, **kwargs): |
| 634 |
return self |
| 635 |
|
| 636 |
def s(*args, **kwargs): |
| 637 |
return static(self.add, args, kwargs, type=self.add, app=self.app) |
| 638 |
|
| 639 |
c = s(2, 2) | s(4) | s(8) |
| 640 |
r1 = c.apply_async(task_id='some_id') |
| 641 |
assert r1.id == 'some_id' |
| 642 |
|
| 643 |
c.apply_async(group_id='some_group_id') |
| 644 |
assert c.tasks[-1].options['group_id'] == 'some_group_id' |
| 645 |
|
| 646 |
c.apply_async(chord='some_chord_id') |
| 647 |
assert c.tasks[-1].options['chord'] == 'some_chord_id' |
| 648 |
|
| 649 |
c.apply_async(link=[s(32)]) |
| 650 |
assert c.tasks[-1].options['link'] == [s(32)] |
| 651 |
|
| 652 |
c.apply_async(link_error=[s('error')]) |
| 653 |
for task in c.tasks: |
| 654 |
assert task.options['link_error'] == [s('error')] |
| 655 |
|
| 656 |
def test_apply_options_none(self): |
| 657 |
class static(Signature): |
| 658 |
|
| 659 |
def clone(self, *args, **kwargs): |
| 660 |
return self |
| 661 |
|
| 662 |
def _apply_async(self, *args, **kwargs): |
| 663 |
self.args = args |
| 664 |
self.kwargs = kwargs |
| 665 |
|
| 666 |
c = static(self.add, (2, 2), type=self.add, app=self.app, priority=5) |
| 667 |
|
| 668 |
c.apply_async(priority=4) |
| 669 |
assert c.kwargs['priority'] == 4 |
| 670 |
|
| 671 |
c.apply_async(priority=None) |
| 672 |
assert c.kwargs['priority'] == 5 |
| 673 |
|
| 674 |
def test_reverse(self): |
| 675 |
x = self.add.s(2, 2) | self.add.s(2) |
| 676 |
assert isinstance(signature(x), _chain) |
| 677 |
assert isinstance(signature(dict(x)), _chain) |
| 678 |
|
| 679 |
def test_always_eager(self): |
| 680 |
self.app.conf.task_always_eager = True |
| 681 |
assert ~(self.add.s(4, 4) | self.add.s(8)) == 16 |
| 682 |
|
| 683 |
def test_chain_always_eager(self): |
| 684 |
self.app.conf.task_always_eager = True |
| 685 |
from celery import _state, result |
| 686 |
|
| 687 |
fixture_task_join_will_block = _state.task_join_will_block |
| 688 |
try: |
| 689 |
_state.task_join_will_block = _state.orig_task_join_will_block |
| 690 |
result.task_join_will_block = _state.orig_task_join_will_block |
| 691 |
|
| 692 |
@self.app.task(shared=False) |
| 693 |
def chain_add(): |
| 694 |
return (self.add.s(4, 4) | self.add.s(8)).apply_async() |
| 695 |
|
| 696 |
r = chain_add.apply_async(throw=True).get() |
| 697 |
assert r.get() == 16 |
| 698 |
finally: |
| 699 |
_state.task_join_will_block = fixture_task_join_will_block |
| 700 |
result.task_join_will_block = fixture_task_join_will_block |
| 701 |
|
| 702 |
def test_apply(self): |
| 703 |
x = chain(self.add.s(4, 4), self.add.s(8), self.add.s(10)) |
| 704 |
res = x.apply() |
| 705 |
assert isinstance(res, EagerResult) |
| 706 |
assert res.get() == 26 |
| 707 |
|
| 708 |
assert res.parent.get() == 16 |
| 709 |
assert res.parent.parent.get() == 8 |
| 710 |
assert res.parent.parent.parent is None |
| 711 |
|
| 712 |
def test_kwargs_apply(self): |
| 713 |
x = chain(self.add.s(), self.add.s(8), self.add.s(10)) |
| 714 |
res = x.apply(kwargs={'x': 1, 'y': 1}).get() |
| 715 |
assert res == 20 |
| 716 |
|
| 717 |
def test_single_expresion(self): |
| 718 |
x = chain(self.add.s(1, 2)).apply() |
| 719 |
assert x.get() == 3 |
| 720 |
assert x.parent is None |
| 721 |
|
| 722 |
def test_empty_chain_returns_none(self): |
| 723 |
assert chain(app=self.app)() is None |
| 724 |
assert chain(app=self.app).apply_async() is None |
| 725 |
|
| 726 |
def test_call_no_tasks(self): |
| 727 |
x = chain() |
| 728 |
assert not x() |
| 729 |
|
| 730 |
def test_call_with_tasks(self): |
| 731 |
x = self.add.s(2, 2) | self.add.s(4) |
| 732 |
x.apply_async = Mock() |
| 733 |
x(2, 2, foo=1) |
| 734 |
x.apply_async.assert_called_with((2, 2), {'foo': 1}) |
| 735 |
|
| 736 |
def test_from_dict_no_args__with_args(self): |
| 737 |
x = dict(self.add.s(2, 2) | self.add.s(4)) |
| 738 |
x['args'] = None |
| 739 |
assert isinstance(chain.from_dict(x), _chain) |
| 740 |
x['args'] = (2,) |
| 741 |
assert isinstance(chain.from_dict(x), _chain) |
| 742 |
|
| 743 |
def test_accepts_generator_argument(self): |
| 744 |
x = chain(self.add.s(i) for i in range(10)) |
| 745 |
assert x.tasks[0].type, self.add |
| 746 |
assert x.type |
| 747 |
|
| 748 |
def test_chord_sets_result_parent(self): |
| 749 |
g = (self.add.s(0, 0) | |
| 750 |
group(self.add.s(i, i) for i in range(1, 10)) | |
| 751 |
self.add.s(2, 2) | |
| 752 |
self.add.s(4, 4)) |
| 753 |
res = g.freeze() |
| 754 |
|
| 755 |
assert isinstance(res, AsyncResult) |
| 756 |
assert not isinstance(res, GroupResult) |
| 757 |
assert isinstance(res.parent, AsyncResult) |
| 758 |
assert not isinstance(res.parent, GroupResult) |
| 759 |
assert isinstance(res.parent.parent, GroupResult) |
| 760 |
assert isinstance(res.parent.parent.parent, AsyncResult) |
| 761 |
assert not isinstance(res.parent.parent.parent, GroupResult) |
| 762 |
assert res.parent.parent.parent.parent is None |
| 763 |
|
| 764 |
seen = set() |
| 765 |
node = res |
| 766 |
while node: |
| 767 |
assert node.id not in seen |
| 768 |
seen.add(node.id) |
| 769 |
node = node.parent |
| 770 |
|
| 771 |
def test_append_to_empty_chain(self): |
| 772 |
x = chain() |
| 773 |
x |= self.add.s(1, 1) |
| 774 |
x |= self.add.s(1) |
| 775 |
x.freeze() |
| 776 |
tasks, _ = x._frozen |
| 777 |
assert len(tasks) == 2 |
| 778 |
|
| 779 |
assert x.apply().get() == 3 |
| 780 |
|
| 781 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 782 |
def test_chain_single_child_result(self): |
| 783 |
child_sig = self.add.si(1, 1) |
| 784 |
chain_sig = chain(child_sig) |
| 785 |
assert chain_sig.tasks[0] is child_sig |
| 786 |
|
| 787 |
with patch.object( |
| 788 |
# We want to get back the result of actually applying the task |
| 789 |
child_sig, "apply_async", |
| 790 |
) as mock_apply, patch.object( |
| 791 |
# The child signature may be clone by `chain.prepare_steps()` |
| 792 |
child_sig, "clone", return_value=child_sig, |
| 793 |
): |
| 794 |
res = chain_sig() |
| 795 |
# `_prepare_chain_from_options()` sets this `chain` kwarg with the |
| 796 |
# subsequent tasks which would be run - nothing in this case |
| 797 |
mock_apply.assert_called_once_with(chain=[]) |
| 798 |
assert res is mock_apply.return_value |
| 799 |
|
| 800 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 801 |
def test_chain_single_child_group_result(self): |
| 802 |
child_sig = self.add.si(1, 1) |
| 803 |
# The group will `clone()` the child during instantiation so mock it |
| 804 |
with patch.object(child_sig, "clone", return_value=child_sig): |
| 805 |
group_sig = group(child_sig) |
| 806 |
# Now we can construct the chain signature which is actually under test |
| 807 |
chain_sig = chain(group_sig) |
| 808 |
assert chain_sig.tasks[0].tasks[0] is child_sig |
| 809 |
|
| 810 |
with patch.object( |
| 811 |
# We want to get back the result of actually applying the task |
| 812 |
child_sig, "apply_async", |
| 813 |
) as mock_apply, patch.object( |
| 814 |
# The child signature may be clone by `chain.prepare_steps()` |
| 815 |
child_sig, "clone", return_value=child_sig, |
| 816 |
): |
| 817 |
res = chain_sig() |
| 818 |
# `_prepare_chain_from_options()` sets this `chain` kwarg with the |
| 819 |
# subsequent tasks which would be run - nothing in this case |
| 820 |
mock_apply.assert_called_once_with(chain=[]) |
| 821 |
assert res is mock_apply.return_value |
| 822 |
|
| 823 |
def test_chain_flattening_keep_links_of_inner_chain(self): |
| 824 |
def link_chain(sig): |
| 825 |
sig.link(signature('link_b')) |
| 826 |
sig.link_error(signature('link_ab')) |
| 827 |
return sig |
| 828 |
|
| 829 |
inner_chain = link_chain(chain(signature('a'), signature('b'))) |
| 830 |
assert inner_chain.options['link'][0] == signature('link_b') |
| 831 |
assert inner_chain.options['link_error'][0] == signature('link_ab') |
| 832 |
assert inner_chain.tasks[0] == signature('a') |
| 833 |
assert inner_chain.tasks[0].options == {} |
| 834 |
assert inner_chain.tasks[1] == signature('b') |
| 835 |
assert inner_chain.tasks[1].options == {} |
| 836 |
|
| 837 |
flat_chain = chain(inner_chain, signature('c')) |
| 838 |
assert flat_chain.options == {} |
| 839 |
assert flat_chain.tasks[0].name == 'a' |
| 840 |
assert 'link' not in flat_chain.tasks[0].options |
| 841 |
assert signature(flat_chain.tasks[0].options['link_error'][0]) == signature('link_ab') |
| 842 |
assert flat_chain.tasks[1].name == 'b' |
| 843 |
assert 'link' in flat_chain.tasks[1].options, "b is missing the link from inner_chain.options['link'][0]" |
| 844 |
assert signature(flat_chain.tasks[1].options['link'][0]) == signature('link_b') |
| 845 |
assert signature(flat_chain.tasks[1].options['link_error'][0]) == signature('link_ab') |
| 846 |
|
| 847 |
def test_group_in_center_of_chain(self): |
| 848 |
t1 = chain(self.add.si(1, 1), group(self.add.si(1, 1), self.add.si(1, 1)), |
| 849 |
self.add.si(1, 1) | self.add.si(1, 1)) |
| 850 |
t2 = chord([self.add.si(1, 1), self.add.si(1, 1)], t1) |
| 851 |
t2.freeze() # should not raise |
| 852 |
|
| 853 |
def test_upgrade_to_chord_on_chain(self): |
| 854 |
group1 = group(self.add.si(10, 10), self.add.si(10, 10)) |
| 855 |
group2 = group(self.xsum.s(), self.xsum.s()) |
| 856 |
chord1 = group1 | group2 |
| 857 |
chain1 = (self.xsum.si([5]) | self.add.s(1)) |
| 858 |
final_task = chain(chord1, chain1) |
| 859 |
assert len(final_task.tasks) == 1 and isinstance(final_task.tasks[0], chord) |
| 860 |
assert isinstance(final_task.tasks[0].body, chord) |
| 861 |
assert final_task.tasks[0].body.body == chain1 |
| 862 |
|
| 863 |
|
| 864 |
class test_group(CanvasCase): |
| 865 |
def test_repr(self): |
| 866 |
x = group([self.add.s(2, 2), self.add.s(4, 4)]) |
| 867 |
assert repr(x) |
| 868 |
|
| 869 |
def test_repr_empty_group(self): |
| 870 |
x = group([]) |
| 871 |
assert repr(x) == 'group(<empty>)' |
| 872 |
|
| 873 |
def test_reverse(self): |
| 874 |
x = group([self.add.s(2, 2), self.add.s(4, 4)]) |
| 875 |
assert isinstance(signature(x), group) |
| 876 |
assert isinstance(signature(dict(x)), group) |
| 877 |
|
| 878 |
def test_reverse_with_subclass(self): |
| 879 |
x = group_subclass([self.add.s(2, 2), self.add.s(4, 4)]) |
| 880 |
assert isinstance(signature(x), group_subclass) |
| 881 |
assert isinstance(signature(dict(x)), group_subclass) |
| 882 |
|
| 883 |
def test_cannot_link_on_group(self): |
| 884 |
x = group([self.add.s(2, 2), self.add.s(4, 4)]) |
| 885 |
with pytest.raises(TypeError): |
| 886 |
x.apply_async(link=self.add.s(2, 2)) |
| 887 |
|
| 888 |
def test_cannot_link_error_on_group(self): |
| 889 |
x = group([self.add.s(2, 2), self.add.s(4, 4)]) |
| 890 |
with pytest.raises(TypeError): |
| 891 |
x.apply_async(link_error=self.add.s(2, 2)) |
| 892 |
|
| 893 |
def test_group_with_group_argument(self): |
| 894 |
g1 = group(self.add.s(2, 2), self.add.s(4, 4), app=self.app) |
| 895 |
g2 = group(g1, app=self.app) |
| 896 |
assert g2.tasks is g1.tasks |
| 897 |
|
| 898 |
def test_maybe_group_sig(self): |
| 899 |
assert _maybe_group(self.add.s(2, 2), self.app) == [self.add.s(2, 2)] |
| 900 |
|
| 901 |
def test_apply(self): |
| 902 |
x = group([self.add.s(4, 4), self.add.s(8, 8)]) |
| 903 |
res = x.apply() |
| 904 |
assert res.get(), [8 == 16] |
| 905 |
|
| 906 |
def test_apply_async(self): |
| 907 |
x = group([self.add.s(4, 4), self.add.s(8, 8)]) |
| 908 |
x.apply_async() |
| 909 |
|
| 910 |
def test_prepare_with_dict(self): |
| 911 |
x = group([self.add.s(4, 4), dict(self.add.s(8, 8))], app=self.app) |
| 912 |
x.apply_async() |
| 913 |
|
| 914 |
def test_group_in_group(self): |
| 915 |
g1 = group(self.add.s(2, 2), self.add.s(4, 4), app=self.app) |
| 916 |
g2 = group(self.add.s(8, 8), g1, self.add.s(16, 16), app=self.app) |
| 917 |
g2.apply_async() |
| 918 |
|
| 919 |
def test_set_immutable(self): |
| 920 |
g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app) |
| 921 |
g1.set_immutable(True) |
| 922 |
for task in g1.tasks: |
| 923 |
task.set_immutable.assert_called_with(True) |
| 924 |
|
| 925 |
def test_link(self): |
| 926 |
g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app) |
| 927 |
sig = Mock(name='sig') |
| 928 |
g1.link(sig) |
| 929 |
# Only the first child signature of a group will be given the callback |
| 930 |
# and it is cloned and made immutable to avoid passing results to it, |
| 931 |
# since that first task can't pass along its siblings' return values |
| 932 |
g1.tasks[0].link.assert_called_with(sig.clone().set(immutable=True)) |
| 933 |
|
| 934 |
def test_link_error(self): |
| 935 |
g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app) |
| 936 |
sig = Mock(name='sig') |
| 937 |
g1.link_error(sig) |
| 938 |
# We expect that all group children will be given the errback to ensure |
| 939 |
# it gets called |
| 940 |
for child_sig in g1.tasks: |
| 941 |
child_sig.link_error.assert_called_with(sig.clone(immutable=True)) |
| 942 |
|
| 943 |
def test_link_error_with_dict_sig(self): |
| 944 |
g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app) |
| 945 |
errback = signature('tcb') |
| 946 |
errback_dict = dict(errback) |
| 947 |
g1.link_error(errback_dict) |
| 948 |
# We expect that all group children will be given the errback to ensure |
| 949 |
# it gets called |
| 950 |
for child_sig in g1.tasks: |
| 951 |
child_sig.link_error.assert_called_with(errback.clone(immutable=True)) |
| 952 |
|
| 953 |
def test_apply_empty(self): |
| 954 |
x = group(app=self.app) |
| 955 |
x.apply() |
| 956 |
res = x.apply_async() |
| 957 |
assert res |
| 958 |
assert not res.results |
| 959 |
|
| 960 |
def test_apply_async_with_parent(self): |
| 961 |
_task_stack.push(self.add) |
| 962 |
try: |
| 963 |
self.add.push_request(called_directly=False) |
| 964 |
try: |
| 965 |
assert not self.add.request.children |
| 966 |
x = group([self.add.s(4, 4), self.add.s(8, 8)]) |
| 967 |
res = x() |
| 968 |
assert self.add.request.children |
| 969 |
assert res in self.add.request.children |
| 970 |
assert len(self.add.request.children) == 1 |
| 971 |
finally: |
| 972 |
self.add.pop_request() |
| 973 |
finally: |
| 974 |
_task_stack.pop() |
| 975 |
|
| 976 |
@pytest.mark.parametrize("group_type", (group, group_subclass)) |
| 977 |
def test_from_dict(self, group_type): |
| 978 |
x = group_type([self.add.s(2, 2), self.add.s(4, 4)]) |
| 979 |
x['args'] = (2, 2) |
| 980 |
value = group_type.from_dict(dict(x)) |
| 981 |
assert value and isinstance(value, group_type) |
| 982 |
x['args'] = None |
| 983 |
value = group_type.from_dict(dict(x)) |
| 984 |
assert value and isinstance(value, group_type) |
| 985 |
|
| 986 |
@pytest.mark.parametrize("group_type", (group, group_subclass)) |
| 987 |
def test_from_dict_deep_deserialize(self, group_type): |
| 988 |
original_group = group_type([self.add.s(1, 2)] * 42) |
| 989 |
serialized_group = json.loads(json.dumps(original_group)) |
| 990 |
deserialized_group = group_type.from_dict(serialized_group) |
| 991 |
assert isinstance(deserialized_group, group_type) |
| 992 |
assert all( |
| 993 |
isinstance(child_task, Signature) |
| 994 |
for child_task in deserialized_group.tasks |
| 995 |
) |
| 996 |
|
| 997 |
@pytest.mark.parametrize("group_type", (group, group_subclass)) |
| 998 |
def test_from_dict_deeper_deserialize(self, group_type): |
| 999 |
inner_group = group_type([self.add.s(1, 2)] * 42) |
| 1000 |
outer_group = group_type([inner_group] * 42) |
| 1001 |
serialized_group = json.loads(json.dumps(outer_group)) |
| 1002 |
deserialized_group = group_type.from_dict(serialized_group) |
| 1003 |
assert isinstance(deserialized_group, group_type) |
| 1004 |
assert all( |
| 1005 |
isinstance(child_task, group_type) |
| 1006 |
for child_task in deserialized_group.tasks |
| 1007 |
) |
| 1008 |
assert all( |
| 1009 |
isinstance(grandchild_task, Signature) |
| 1010 |
for child_task in deserialized_group.tasks |
| 1011 |
for grandchild_task in child_task.tasks |
| 1012 |
) |
| 1013 |
|
| 1014 |
def test_call_empty_group(self): |
| 1015 |
x = group(app=self.app) |
| 1016 |
assert not len(x()) |
| 1017 |
x.delay() |
| 1018 |
x.apply_async() |
| 1019 |
x() |
| 1020 |
|
| 1021 |
def test_skew(self): |
| 1022 |
g = group([self.add.s(i, i) for i in range(10)]) |
| 1023 |
g.skew(start=1, stop=10, step=1) |
| 1024 |
for i, task in enumerate(g.tasks): |
| 1025 |
assert task.options['countdown'] == i + 1 |
| 1026 |
|
| 1027 |
def test_iter(self): |
| 1028 |
g = group([self.add.s(i, i) for i in range(10)]) |
| 1029 |
assert list(iter(g)) == list(g.keys()) |
| 1030 |
|
| 1031 |
def test_single_task(self): |
| 1032 |
g = group([self.add.s(1, 1)]) |
| 1033 |
assert isinstance(g, group) |
| 1034 |
assert len(g.tasks) == 1 |
| 1035 |
g = group(self.add.s(1, 1)) |
| 1036 |
assert isinstance(g, group) |
| 1037 |
assert len(g.tasks) == 1 |
| 1038 |
|
| 1039 |
@staticmethod |
| 1040 |
def helper_test_get_delay(result): |
| 1041 |
import time |
| 1042 |
t0 = time.time() |
| 1043 |
while not result.ready(): |
| 1044 |
time.sleep(0.01) |
| 1045 |
if time.time() - t0 > 1: |
| 1046 |
return None |
| 1047 |
return result.get() |
| 1048 |
|
| 1049 |
def test_kwargs_direct(self): |
| 1050 |
res = [self.add(x=1, y=1), self.add(x=1, y=1)] |
| 1051 |
assert res == [2, 2] |
| 1052 |
|
| 1053 |
def test_kwargs_apply(self): |
| 1054 |
x = group([self.add.s(), self.add.s()]) |
| 1055 |
res = x.apply(kwargs={'x': 1, 'y': 1}).get() |
| 1056 |
assert res == [2, 2] |
| 1057 |
|
| 1058 |
def test_kwargs_apply_async(self): |
| 1059 |
self.app.conf.task_always_eager = True |
| 1060 |
x = group([self.add.s(), self.add.s()]) |
| 1061 |
res = self.helper_test_get_delay( |
| 1062 |
x.apply_async(kwargs={'x': 1, 'y': 1}) |
| 1063 |
) |
| 1064 |
assert res == [2, 2] |
| 1065 |
|
| 1066 |
def test_kwargs_delay(self): |
| 1067 |
self.app.conf.task_always_eager = True |
| 1068 |
x = group([self.add.s(), self.add.s()]) |
| 1069 |
res = self.helper_test_get_delay(x.delay(x=1, y=1)) |
| 1070 |
assert res == [2, 2] |
| 1071 |
|
| 1072 |
def test_kwargs_delay_partial(self): |
| 1073 |
self.app.conf.task_always_eager = True |
| 1074 |
x = group([self.add.s(1), self.add.s(x=1)]) |
| 1075 |
res = self.helper_test_get_delay(x.delay(y=1)) |
| 1076 |
assert res == [2, 2] |
| 1077 |
|
| 1078 |
def test_apply_from_generator(self): |
| 1079 |
child_count = 42 |
| 1080 |
child_sig = self.add.si(0, 0) |
| 1081 |
child_sigs_gen = (child_sig for _ in range(child_count)) |
| 1082 |
group_sig = group(child_sigs_gen) |
| 1083 |
with patch("celery.canvas.Signature.apply_async") as mock_apply_async: |
| 1084 |
res_obj = group_sig.apply_async() |
| 1085 |
assert mock_apply_async.call_count == child_count |
| 1086 |
assert len(res_obj.children) == child_count |
| 1087 |
|
| 1088 |
# This needs the current app for some reason not worth digging into |
| 1089 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 1090 |
def test_apply_from_generator_empty(self): |
| 1091 |
empty_gen = (False for _ in range(0)) |
| 1092 |
group_sig = group(empty_gen) |
| 1093 |
with patch("celery.canvas.Signature.apply_async") as mock_apply_async: |
| 1094 |
res_obj = group_sig.apply_async() |
| 1095 |
assert mock_apply_async.call_count == 0 |
| 1096 |
assert len(res_obj.children) == 0 |
| 1097 |
|
| 1098 |
# In the following tests, getting the group ID is a pain so we just use |
| 1099 |
# `ANY` to wildcard it when we're checking on calls made to our mocks |
| 1100 |
def test_apply_contains_chord(self): |
| 1101 |
gchild_count = 42 |
| 1102 |
gchild_sig = self.add.si(0, 0) |
| 1103 |
gchild_sigs = (gchild_sig,) * gchild_count |
| 1104 |
child_chord = chord(gchild_sigs, gchild_sig) |
| 1105 |
group_sig = group((child_chord,)) |
| 1106 |
with patch.object( |
| 1107 |
self.app.backend, "set_chord_size", |
| 1108 |
) as mock_set_chord_size, patch( |
| 1109 |
"celery.canvas.Signature.apply_async", |
| 1110 |
) as mock_apply_async: |
| 1111 |
res_obj = group_sig.apply_async() |
| 1112 |
# We only see applies for the header grandchildren because the tasks |
| 1113 |
# are never actually run due to our mocking of `apply_async()` |
| 1114 |
assert mock_apply_async.call_count == gchild_count |
| 1115 |
assert len(res_obj.children) == len(group_sig.tasks) |
| 1116 |
# We must have set the chord size for the group of tasks which makes up |
| 1117 |
# the header of the `child_chord`, just before we apply the last task. |
| 1118 |
mock_set_chord_size.assert_called_once_with(ANY, gchild_count) |
| 1119 |
|
| 1120 |
def test_apply_contains_chords_containing_chain(self): |
| 1121 |
ggchild_count = 42 |
| 1122 |
ggchild_sig = self.add.si(0, 0) |
| 1123 |
gchild_sig = chain((ggchild_sig,) * ggchild_count) |
| 1124 |
child_count = 24 |
| 1125 |
child_chord = chord((gchild_sig,), ggchild_sig) |
| 1126 |
group_sig = group((child_chord,) * child_count) |
| 1127 |
with patch.object( |
| 1128 |
self.app.backend, "set_chord_size", |
| 1129 |
) as mock_set_chord_size, patch( |
| 1130 |
"celery.canvas.Signature.apply_async", |
| 1131 |
) as mock_apply_async: |
| 1132 |
res_obj = group_sig.apply_async() |
| 1133 |
# We only see applies for the header grandchildren because the tasks |
| 1134 |
# are never actually run due to our mocking of `apply_async()` |
| 1135 |
assert mock_apply_async.call_count == child_count |
| 1136 |
assert len(res_obj.children) == child_count |
| 1137 |
# We must have set the chord sizes based on the number of tail tasks of |
| 1138 |
# the encapsulated chains - in this case 1 for each child chord |
| 1139 |
mock_set_chord_size.assert_has_calls((call(ANY, 1),) * child_count) |
| 1140 |
|
| 1141 |
@pytest.mark.xfail(reason="Invalid canvas setup with bad exception") |
| 1142 |
def test_apply_contains_chords_containing_empty_chain(self): |
| 1143 |
gchild_sig = chain(tuple()) |
| 1144 |
child_count = 24 |
| 1145 |
child_chord = chord((gchild_sig,), self.add.si(0, 0)) |
| 1146 |
group_sig = group((child_chord,) * child_count) |
| 1147 |
# This is an invalid setup because we can't complete a chord header if |
| 1148 |
# there are no actual tasks which will run in it. However, the current |
| 1149 |
# behaviour of an `IndexError` isn't particularly helpful to a user. |
| 1150 |
group_sig.apply_async() |
| 1151 |
|
| 1152 |
def test_apply_contains_chords_containing_chain_with_empty_tail(self): |
| 1153 |
ggchild_count = 42 |
| 1154 |
ggchild_sig = self.add.si(0, 0) |
| 1155 |
tail_count = 24 |
| 1156 |
gchild_sig = chain( |
| 1157 |
(ggchild_sig,) * ggchild_count + |
| 1158 |
(group((ggchild_sig,) * tail_count), group(tuple()),), |
| 1159 |
) |
| 1160 |
child_chord = chord((gchild_sig,), ggchild_sig) |
| 1161 |
group_sig = group((child_chord,)) |
| 1162 |
with patch.object( |
| 1163 |
self.app.backend, "set_chord_size", |
| 1164 |
) as mock_set_chord_size, patch( |
| 1165 |
"celery.canvas.Signature.apply_async", |
| 1166 |
) as mock_apply_async: |
| 1167 |
res_obj = group_sig.apply_async() |
| 1168 |
# We only see applies for the header grandchildren because the tasks |
| 1169 |
# are never actually run due to our mocking of `apply_async()` |
| 1170 |
assert mock_apply_async.call_count == 1 |
| 1171 |
assert len(res_obj.children) == 1 |
| 1172 |
# We must have set the chord sizes based on the size of the last |
| 1173 |
# non-empty task in the encapsulated chains - in this case `tail_count` |
| 1174 |
# for the group preceding the empty one in each grandchild chain |
| 1175 |
mock_set_chord_size.assert_called_once_with(ANY, tail_count) |
| 1176 |
|
| 1177 |
def test_apply_contains_chords_containing_group(self): |
| 1178 |
ggchild_count = 42 |
| 1179 |
ggchild_sig = self.add.si(0, 0) |
| 1180 |
gchild_sig = group((ggchild_sig,) * ggchild_count) |
| 1181 |
child_count = 24 |
| 1182 |
child_chord = chord((gchild_sig,), ggchild_sig) |
| 1183 |
group_sig = group((child_chord,) * child_count) |
| 1184 |
with patch.object( |
| 1185 |
self.app.backend, "set_chord_size", |
| 1186 |
) as mock_set_chord_size, patch( |
| 1187 |
"celery.canvas.Signature.apply_async", |
| 1188 |
) as mock_apply_async: |
| 1189 |
res_obj = group_sig.apply_async() |
| 1190 |
# We see applies for all of the header grandchildren because the tasks |
| 1191 |
# are never actually run due to our mocking of `apply_async()` |
| 1192 |
assert mock_apply_async.call_count == child_count * ggchild_count |
| 1193 |
assert len(res_obj.children) == child_count |
| 1194 |
# We must have set the chord sizes based on the number of tail tasks of |
| 1195 |
# the encapsulated groups - in this case `ggchild_count` |
| 1196 |
mock_set_chord_size.assert_has_calls( |
| 1197 |
(call(ANY, ggchild_count),) * child_count, |
| 1198 |
) |
| 1199 |
|
| 1200 |
@pytest.mark.xfail(reason="Invalid canvas setup but poor behaviour") |
| 1201 |
def test_apply_contains_chords_containing_empty_group(self): |
| 1202 |
gchild_sig = group(tuple()) |
| 1203 |
child_count = 24 |
| 1204 |
child_chord = chord((gchild_sig,), self.add.si(0, 0)) |
| 1205 |
group_sig = group((child_chord,) * child_count) |
| 1206 |
with patch.object( |
| 1207 |
self.app.backend, "set_chord_size", |
| 1208 |
) as mock_set_chord_size, patch( |
| 1209 |
"celery.canvas.Signature.apply_async", |
| 1210 |
) as mock_apply_async: |
| 1211 |
res_obj = group_sig.apply_async() |
| 1212 |
# We only see applies for the header grandchildren because the tasks |
| 1213 |
# are never actually run due to our mocking of `apply_async()` |
| 1214 |
assert mock_apply_async.call_count == child_count |
| 1215 |
assert len(res_obj.children) == child_count |
| 1216 |
# This is actually kind of meaningless because, similar to the empty |
| 1217 |
# chain test, this is an invalid setup. However, we should probably |
| 1218 |
# expect that the chords are dealt with in some other way the probably |
| 1219 |
# being left incomplete forever... |
| 1220 |
mock_set_chord_size.assert_has_calls((call(ANY, 0),) * child_count) |
| 1221 |
|
| 1222 |
def test_apply_contains_chords_containing_chord(self): |
| 1223 |
ggchild_count = 42 |
| 1224 |
ggchild_sig = self.add.si(0, 0) |
| 1225 |
gchild_sig = chord((ggchild_sig,) * ggchild_count, ggchild_sig) |
| 1226 |
child_count = 24 |
| 1227 |
child_chord = chord((gchild_sig,), ggchild_sig) |
| 1228 |
group_sig = group((child_chord,) * child_count) |
| 1229 |
with patch.object( |
| 1230 |
self.app.backend, "set_chord_size", |
| 1231 |
) as mock_set_chord_size, patch( |
| 1232 |
"celery.canvas.Signature.apply_async", |
| 1233 |
) as mock_apply_async: |
| 1234 |
res_obj = group_sig.apply_async() |
| 1235 |
# We see applies for all of the header great-grandchildren because the |
| 1236 |
# tasks are never actually run due to our mocking of `apply_async()` |
| 1237 |
assert mock_apply_async.call_count == child_count * ggchild_count |
| 1238 |
assert len(res_obj.children) == child_count |
| 1239 |
# We must have set the chord sizes based on the number of tail tasks of |
| 1240 |
# the deeply encapsulated chords' header tasks, as well as for each |
| 1241 |
# child chord. This means we have `child_count` interleaved calls to |
| 1242 |
# set chord sizes of 1 and `ggchild_count`. |
| 1243 |
mock_set_chord_size.assert_has_calls( |
| 1244 |
(call(ANY, 1), call(ANY, ggchild_count),) * child_count, |
| 1245 |
) |
| 1246 |
|
| 1247 |
def test_apply_contains_chords_containing_empty_chord(self): |
| 1248 |
gchild_sig = chord(tuple(), self.add.si(0, 0)) |
| 1249 |
child_count = 24 |
| 1250 |
child_chord = chord((gchild_sig,), self.add.si(0, 0)) |
| 1251 |
group_sig = group((child_chord,) * child_count) |
| 1252 |
with patch.object( |
| 1253 |
self.app.backend, "set_chord_size", |
| 1254 |
) as mock_set_chord_size, patch( |
| 1255 |
"celery.canvas.Signature.apply_async", |
| 1256 |
) as mock_apply_async: |
| 1257 |
res_obj = group_sig.apply_async() |
| 1258 |
# We only see applies for the header grandchildren because the tasks |
| 1259 |
# are never actually run due to our mocking of `apply_async()` |
| 1260 |
assert mock_apply_async.call_count == child_count |
| 1261 |
assert len(res_obj.children) == child_count |
| 1262 |
# We must have set the chord sizes based on the number of tail tasks of |
| 1263 |
# the encapsulated chains - in this case 1 for each child chord |
| 1264 |
mock_set_chord_size.assert_has_calls((call(ANY, 1),) * child_count) |
| 1265 |
|
| 1266 |
def test_group_prepared(self): |
| 1267 |
# Using both partial and dict based signatures |
| 1268 |
sig = group(dict(self.add.s(0)), self.add.s(0)) |
| 1269 |
_, group_id, root_id = sig._freeze_gid({}) |
| 1270 |
tasks = sig._prepared(sig.tasks, [42], group_id, root_id, self.app) |
| 1271 |
|
| 1272 |
for task, result, group_id in tasks: |
| 1273 |
assert isinstance(task, Signature) |
| 1274 |
assert task.args[0] == 42 |
| 1275 |
assert task.args[1] == 0 |
| 1276 |
assert isinstance(result, AsyncResult) |
| 1277 |
assert group_id is not None |
| 1278 |
|
| 1279 |
def test_task_replace_with_group_preserves_group_order(self): |
| 1280 |
self.app.conf.task_always_eager = True |
| 1281 |
sig = self.replace_with_group.s(1, 2) |
| 1282 |
res = self.helper_test_get_delay(sig.delay()) |
| 1283 |
assert res == [3, 2] |
| 1284 |
|
| 1285 |
|
| 1286 |
class test_chord(CanvasCase): |
| 1287 |
def test__get_app_does_not_exhaust_generator(self): |
| 1288 |
def build_generator(): |
| 1289 |
yield self.add.s(1, 1) |
| 1290 |
self.second_item_returned = True |
| 1291 |
yield self.add.s(2, 2) |
| 1292 |
raise pytest.fail("This should never be reached") |
| 1293 |
|
| 1294 |
self.second_item_returned = False |
| 1295 |
c = chord(build_generator(), self.add.s(3)) |
| 1296 |
c.app |
| 1297 |
# The second task gets returned due to lookahead in `regen()` |
| 1298 |
assert self.second_item_returned |
| 1299 |
# Access it again to make sure the generator is not further evaluated |
| 1300 |
c.app |
| 1301 |
|
| 1302 |
@pytest.mark.parametrize("chord_type", [chord, chord_subclass]) |
| 1303 |
def test_reverse(self, chord_type): |
| 1304 |
x = chord_type([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4)) |
| 1305 |
assert isinstance(signature(x), chord_type) |
| 1306 |
assert isinstance(signature(dict(x)), chord_type) |
| 1307 |
|
| 1308 |
def test_clone_clones_body(self): |
| 1309 |
x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4)) |
| 1310 |
y = x.clone() |
| 1311 |
assert x.kwargs['body'] is not y.kwargs['body'] |
| 1312 |
y.kwargs.pop('body') |
| 1313 |
z = y.clone() |
| 1314 |
assert z.kwargs.get('body') is None |
| 1315 |
|
| 1316 |
def test_argument_is_group(self): |
| 1317 |
x = chord(group(self.add.s(2, 2), self.add.s(4, 4), app=self.app)) |
| 1318 |
assert x.tasks |
| 1319 |
|
| 1320 |
def test_app_when_app(self): |
| 1321 |
app = Mock(name='app') |
| 1322 |
x = chord([self.add.s(4, 4)], app=app) |
| 1323 |
assert x.app is app |
| 1324 |
|
| 1325 |
def test_app_when_app_in_task(self): |
| 1326 |
t1 = Mock(name='t1') |
| 1327 |
t2 = Mock(name='t2') |
| 1328 |
x = chord([t1, self.add.s(4, 4)]) |
| 1329 |
assert x.app is x.tasks[0].app |
| 1330 |
t1.app = None |
| 1331 |
x = chord([t1], body=t2) |
| 1332 |
assert x.app is t2._app |
| 1333 |
|
| 1334 |
def test_app_when_header_is_empty(self): |
| 1335 |
x = chord([], self.add.s(4, 4)) |
| 1336 |
assert x.app is self.add.app |
| 1337 |
|
| 1338 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 1339 |
def test_app_fallback_to_current(self): |
| 1340 |
from celery._state import current_app |
| 1341 |
t1 = Mock(name='t1') |
| 1342 |
t1.app = t1._app = None |
| 1343 |
x = chord([t1], body=t1) |
| 1344 |
assert x.app is current_app |
| 1345 |
|
| 1346 |
def test_chord_size_simple(self): |
| 1347 |
sig = chord(self.add.s()) |
| 1348 |
assert sig.__length_hint__() == 1 |
| 1349 |
|
| 1350 |
def test_chord_size_with_body(self): |
| 1351 |
sig = chord(self.add.s(), self.add.s()) |
| 1352 |
assert sig.__length_hint__() == 1 |
| 1353 |
|
| 1354 |
def test_chord_size_explicit_group_single(self): |
| 1355 |
sig = chord(group(self.add.s())) |
| 1356 |
assert sig.__length_hint__() == 1 |
| 1357 |
|
| 1358 |
def test_chord_size_explicit_group_many(self): |
| 1359 |
sig = chord(group([self.add.s()] * 42)) |
| 1360 |
assert sig.__length_hint__() == 42 |
| 1361 |
|
| 1362 |
def test_chord_size_implicit_group_single(self): |
| 1363 |
sig = chord([self.add.s()]) |
| 1364 |
assert sig.__length_hint__() == 1 |
| 1365 |
|
| 1366 |
def test_chord_size_implicit_group_many(self): |
| 1367 |
sig = chord([self.add.s()] * 42) |
| 1368 |
assert sig.__length_hint__() == 42 |
| 1369 |
|
| 1370 |
def test_chord_size_chain_single(self): |
| 1371 |
sig = chord(chain(self.add.s())) |
| 1372 |
assert sig.__length_hint__() == 1 |
| 1373 |
|
| 1374 |
def test_chord_size_chain_many(self): |
| 1375 |
# Chains get flattened into the encapsulating chord so even though the |
| 1376 |
# chain would only count for 1, the tasks we pulled into the chord's |
| 1377 |
# header and are counted as a bunch of simple signature objects |
| 1378 |
sig = chord(chain([self.add.s()] * 42)) |
| 1379 |
assert sig.__length_hint__() == 42 |
| 1380 |
|
| 1381 |
def test_chord_size_nested_chain_chain_single(self): |
| 1382 |
sig = chord(chain(chain(self.add.s()))) |
| 1383 |
assert sig.__length_hint__() == 1 |
| 1384 |
|
| 1385 |
def test_chord_size_nested_chain_chain_many(self): |
| 1386 |
# The outer chain will be pulled up into the chord but the lower one |
| 1387 |
# remains and will only count as a single final element |
| 1388 |
sig = chord(chain(chain([self.add.s()] * 42))) |
| 1389 |
assert sig.__length_hint__() == 1 |
| 1390 |
|
| 1391 |
def test_chord_size_implicit_chain_single(self): |
| 1392 |
sig = chord([self.add.s()]) |
| 1393 |
assert sig.__length_hint__() == 1 |
| 1394 |
|
| 1395 |
def test_chord_size_implicit_chain_many(self): |
| 1396 |
# This isn't a chain object so the `tasks` attribute can't be lifted |
| 1397 |
# into the chord - this isn't actually valid and would blow up we tried |
| 1398 |
# to run it but it sanity checks our recursion |
| 1399 |
sig = chord([[self.add.s()] * 42]) |
| 1400 |
assert sig.__length_hint__() == 1 |
| 1401 |
|
| 1402 |
def test_chord_size_nested_implicit_chain_chain_single(self): |
| 1403 |
sig = chord([chain(self.add.s())]) |
| 1404 |
assert sig.__length_hint__() == 1 |
| 1405 |
|
| 1406 |
def test_chord_size_nested_implicit_chain_chain_many(self): |
| 1407 |
sig = chord([chain([self.add.s()] * 42)]) |
| 1408 |
assert sig.__length_hint__() == 1 |
| 1409 |
|
| 1410 |
def test_chord_size_nested_chord_body_simple(self): |
| 1411 |
sig = chord(chord(tuple(), self.add.s())) |
| 1412 |
assert sig.__length_hint__() == 1 |
| 1413 |
|
| 1414 |
def test_chord_size_nested_chord_body_implicit_group_single(self): |
| 1415 |
sig = chord(chord(tuple(), [self.add.s()])) |
| 1416 |
assert sig.__length_hint__() == 1 |
| 1417 |
|
| 1418 |
def test_chord_size_nested_chord_body_implicit_group_many(self): |
| 1419 |
sig = chord(chord(tuple(), [self.add.s()] * 42)) |
| 1420 |
assert sig.__length_hint__() == 42 |
| 1421 |
|
| 1422 |
# Nested groups in a chain only affect the chord size if they are the last |
| 1423 |
# element in the chain - in that case each group element is counted |
| 1424 |
def test_chord_size_nested_group_chain_group_head_single(self): |
| 1425 |
x = chord( |
| 1426 |
group( |
| 1427 |
[group(self.add.s()) | self.add.s()] * 42 |
| 1428 |
), |
| 1429 |
body=self.add.s() |
| 1430 |
) |
| 1431 |
assert x.__length_hint__() == 42 |
| 1432 |
|
| 1433 |
def test_chord_size_nested_group_chain_group_head_many(self): |
| 1434 |
x = chord( |
| 1435 |
group( |
| 1436 |
[group([self.add.s()] * 4) | self.add.s()] * 2 |
| 1437 |
), |
| 1438 |
body=self.add.s() |
| 1439 |
) |
| 1440 |
assert x.__length_hint__() == 2 |
| 1441 |
|
| 1442 |
def test_chord_size_nested_group_chain_group_mid_single(self): |
| 1443 |
x = chord( |
| 1444 |
group( |
| 1445 |
[self.add.s() | group(self.add.s()) | self.add.s()] * 42 |
| 1446 |
), |
| 1447 |
body=self.add.s() |
| 1448 |
) |
| 1449 |
assert x.__length_hint__() == 42 |
| 1450 |
|
| 1451 |
def test_chord_size_nested_group_chain_group_mid_many(self): |
| 1452 |
x = chord( |
| 1453 |
group( |
| 1454 |
[self.add.s() | group([self.add.s()] * 4) | self.add.s()] * 2 |
| 1455 |
), |
| 1456 |
body=self.add.s() |
| 1457 |
) |
| 1458 |
assert x.__length_hint__() == 2 |
| 1459 |
|
| 1460 |
def test_chord_size_nested_group_chain_group_tail_single(self): |
| 1461 |
x = chord( |
| 1462 |
group( |
| 1463 |
[self.add.s() | group(self.add.s())] * 42 |
| 1464 |
), |
| 1465 |
body=self.add.s() |
| 1466 |
) |
| 1467 |
assert x.__length_hint__() == 42 |
| 1468 |
|
| 1469 |
def test_chord_size_nested_group_chain_group_tail_many(self): |
| 1470 |
x = chord( |
| 1471 |
group( |
| 1472 |
[self.add.s() | group([self.add.s()] * 4)] * 2 |
| 1473 |
), |
| 1474 |
body=self.add.s() |
| 1475 |
) |
| 1476 |
assert x.__length_hint__() == 4 * 2 |
| 1477 |
|
| 1478 |
def test_chord_size_nested_implicit_group_chain_group_tail_single(self): |
| 1479 |
x = chord( |
| 1480 |
[self.add.s() | group(self.add.s())] * 42, |
| 1481 |
body=self.add.s() |
| 1482 |
) |
| 1483 |
assert x.__length_hint__() == 42 |
| 1484 |
|
| 1485 |
def test_chord_size_nested_implicit_group_chain_group_tail_many(self): |
| 1486 |
x = chord( |
| 1487 |
[self.add.s() | group([self.add.s()] * 4)] * 2, |
| 1488 |
body=self.add.s() |
| 1489 |
) |
| 1490 |
assert x.__length_hint__() == 4 * 2 |
| 1491 |
|
| 1492 |
def test_chord_size_deserialized_element_single(self): |
| 1493 |
child_sig = self.add.s() |
| 1494 |
deserialized_child_sig = json.loads(json.dumps(child_sig)) |
| 1495 |
# We have to break in to be sure that a child remains as a `dict` so we |
| 1496 |
# can confirm that the length hint will instantiate a `Signature` |
| 1497 |
# object and then descend as expected |
| 1498 |
chord_sig = chord(tuple()) |
| 1499 |
chord_sig.tasks = [deserialized_child_sig] |
| 1500 |
with patch( |
| 1501 |
"celery.canvas.Signature.from_dict", return_value=child_sig |
| 1502 |
) as mock_from_dict: |
| 1503 |
assert chord_sig.__length_hint__() == 1 |
| 1504 |
mock_from_dict.assert_called_once_with(deserialized_child_sig) |
| 1505 |
|
| 1506 |
def test_chord_size_deserialized_element_many(self): |
| 1507 |
child_sig = self.add.s() |
| 1508 |
deserialized_child_sig = json.loads(json.dumps(child_sig)) |
| 1509 |
# We have to break in to be sure that a child remains as a `dict` so we |
| 1510 |
# can confirm that the length hint will instantiate a `Signature` |
| 1511 |
# object and then descend as expected |
| 1512 |
chord_sig = chord(tuple()) |
| 1513 |
chord_sig.tasks = [deserialized_child_sig] * 42 |
| 1514 |
with patch( |
| 1515 |
"celery.canvas.Signature.from_dict", return_value=child_sig |
| 1516 |
) as mock_from_dict: |
| 1517 |
assert chord_sig.__length_hint__() == 42 |
| 1518 |
mock_from_dict.assert_has_calls([call(deserialized_child_sig)] * 42) |
| 1519 |
|
| 1520 |
def test_set_immutable(self): |
| 1521 |
x = chord([Mock(name='t1'), Mock(name='t2')], app=self.app) |
| 1522 |
x.set_immutable(True) |
| 1523 |
|
| 1524 |
def test_links_to_body(self): |
| 1525 |
x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4)) |
| 1526 |
x.link(self.div.s(2)) |
| 1527 |
assert not x.options.get('link') |
| 1528 |
assert x.kwargs['body'].options['link'] |
| 1529 |
|
| 1530 |
x.link_error(self.div.s(2)) |
| 1531 |
assert not x.options.get('link_error') |
| 1532 |
assert x.kwargs['body'].options['link_error'] |
| 1533 |
|
| 1534 |
assert x.tasks |
| 1535 |
assert x.body |
| 1536 |
|
| 1537 |
def test_repr(self): |
| 1538 |
x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4)) |
| 1539 |
assert repr(x) |
| 1540 |
x.kwargs['body'] = None |
| 1541 |
assert 'without body' in repr(x) |
| 1542 |
|
| 1543 |
@pytest.mark.parametrize("group_type", [group, group_subclass]) |
| 1544 |
def test_freeze_tasks_body_is_group(self, subtests, group_type): |
| 1545 |
# Confirm that `group index` values counting up from 0 are set for |
| 1546 |
# elements of a chord's body when the chord is encapsulated in a group |
| 1547 |
body_elem = self.add.s() |
| 1548 |
chord_body = group_type([body_elem] * 42) |
| 1549 |
chord_obj = chord(self.add.s(), body=chord_body) |
| 1550 |
top_group = group_type([chord_obj]) |
| 1551 |
|
| 1552 |
# We expect the body to be the signature we passed in before we freeze |
| 1553 |
with subtests.test(msg="Validate body type and tasks are retained"): |
| 1554 |
assert isinstance(chord_obj.body, group_type) |
| 1555 |
assert all( |
| 1556 |
embedded_body_elem is body_elem |
| 1557 |
for embedded_body_elem in chord_obj.body.tasks |
| 1558 |
) |
| 1559 |
# We also expect the body to have no initial options - since all of the |
| 1560 |
# embedded body elements are confirmed to be `body_elem` this is valid |
| 1561 |
assert body_elem.options == {} |
| 1562 |
# When we freeze the chord, its body will be cloned and options set |
| 1563 |
top_group.freeze() |
| 1564 |
with subtests.test( |
| 1565 |
msg="Validate body group indices count from 0 after freezing" |
| 1566 |
): |
| 1567 |
assert isinstance(chord_obj.body, group_type) |
| 1568 |
|
| 1569 |
assert all( |
| 1570 |
embedded_body_elem is not body_elem |
| 1571 |
for embedded_body_elem in chord_obj.body.tasks |
| 1572 |
) |
| 1573 |
assert all( |
| 1574 |
embedded_body_elem.options["group_index"] == i |
| 1575 |
for i, embedded_body_elem in enumerate(chord_obj.body.tasks) |
| 1576 |
) |
| 1577 |
|
| 1578 |
def test_freeze_tasks_is_not_group(self): |
| 1579 |
x = chord([self.add.s(2, 2)], body=self.add.s(), app=self.app) |
| 1580 |
x.freeze() |
| 1581 |
x.tasks = [self.add.s(2, 2)] |
| 1582 |
x.freeze() |
| 1583 |
|
| 1584 |
def test_chain_always_eager(self): |
| 1585 |
self.app.conf.task_always_eager = True |
| 1586 |
from celery import _state, result |
| 1587 |
|
| 1588 |
fixture_task_join_will_block = _state.task_join_will_block |
| 1589 |
try: |
| 1590 |
_state.task_join_will_block = _state.orig_task_join_will_block |
| 1591 |
result.task_join_will_block = _state.orig_task_join_will_block |
| 1592 |
|
| 1593 |
@self.app.task(shared=False) |
| 1594 |
def finalize(*args): |
| 1595 |
pass |
| 1596 |
|
| 1597 |
@self.app.task(shared=False) |
| 1598 |
def chord_add(): |
| 1599 |
return chord([self.add.s(4, 4)], finalize.s()).apply_async() |
| 1600 |
|
| 1601 |
chord_add.apply_async(throw=True).get() |
| 1602 |
finally: |
| 1603 |
_state.task_join_will_block = fixture_task_join_will_block |
| 1604 |
result.task_join_will_block = fixture_task_join_will_block |
| 1605 |
|
| 1606 |
@pytest.mark.parametrize("chord_type", [chord, chord_subclass]) |
| 1607 |
def test_from_dict(self, chord_type): |
| 1608 |
header = self.add.s(1, 2) |
| 1609 |
original_chord = chord_type(header=header) |
| 1610 |
rebuilt_chord = chord_type.from_dict(dict(original_chord)) |
| 1611 |
assert isinstance(rebuilt_chord, chord_type) |
| 1612 |
|
| 1613 |
@pytest.mark.parametrize("chord_type", [chord, chord_subclass]) |
| 1614 |
def test_from_dict_with_body(self, chord_type): |
| 1615 |
header = body = self.add.s(1, 2) |
| 1616 |
original_chord = chord_type(header=header, body=body) |
| 1617 |
rebuilt_chord = chord_type.from_dict(dict(original_chord)) |
| 1618 |
assert isinstance(rebuilt_chord, chord_type) |
| 1619 |
|
| 1620 |
def test_from_dict_deep_deserialize(self, subtests): |
| 1621 |
header = body = self.add.s(1, 2) |
| 1622 |
original_chord = chord(header=header, body=body) |
| 1623 |
serialized_chord = json.loads(json.dumps(original_chord)) |
| 1624 |
deserialized_chord = chord.from_dict(serialized_chord) |
| 1625 |
with subtests.test(msg="Verify chord is deserialized"): |
| 1626 |
assert isinstance(deserialized_chord, chord) |
| 1627 |
with subtests.test(msg="Validate chord header tasks is deserialized"): |
| 1628 |
assert all( |
| 1629 |
isinstance(child_task, Signature) |
| 1630 |
for child_task in deserialized_chord.tasks |
| 1631 |
) |
| 1632 |
with subtests.test(msg="Verify chord body is deserialized"): |
| 1633 |
assert isinstance(deserialized_chord.body, Signature) |
| 1634 |
|
| 1635 |
@pytest.mark.parametrize("group_type", [group, group_subclass]) |
| 1636 |
def test_from_dict_deep_deserialize_group(self, subtests, group_type): |
| 1637 |
header = body = group_type([self.add.s(1, 2)] * 42) |
| 1638 |
original_chord = chord(header=header, body=body) |
| 1639 |
serialized_chord = json.loads(json.dumps(original_chord)) |
| 1640 |
deserialized_chord = chord.from_dict(serialized_chord) |
| 1641 |
with subtests.test(msg="Verify chord is deserialized"): |
| 1642 |
assert isinstance(deserialized_chord, chord) |
| 1643 |
# A header which is a group gets unpacked into the chord's `tasks` |
| 1644 |
with subtests.test( |
| 1645 |
msg="Validate chord header tasks are deserialized and unpacked" |
| 1646 |
): |
| 1647 |
assert all( |
| 1648 |
isinstance(child_task, Signature) |
| 1649 |
and not isinstance(child_task, group_type) |
| 1650 |
for child_task in deserialized_chord.tasks |
| 1651 |
) |
| 1652 |
# A body which is a group remains as it we passed in |
| 1653 |
with subtests.test( |
| 1654 |
msg="Validate chord body is deserialized and not unpacked" |
| 1655 |
): |
| 1656 |
assert isinstance(deserialized_chord.body, group_type) |
| 1657 |
assert all( |
| 1658 |
isinstance(body_child_task, Signature) |
| 1659 |
for body_child_task in deserialized_chord.body.tasks |
| 1660 |
) |
| 1661 |
|
| 1662 |
@pytest.mark.parametrize("group_type", [group, group_subclass]) |
| 1663 |
def test_from_dict_deeper_deserialize_group(self, subtests, group_type): |
| 1664 |
inner_group = group_type([self.add.s(1, 2)] * 42) |
| 1665 |
header = body = group_type([inner_group] * 42) |
| 1666 |
original_chord = chord(header=header, body=body) |
| 1667 |
serialized_chord = json.loads(json.dumps(original_chord)) |
| 1668 |
deserialized_chord = chord.from_dict(serialized_chord) |
| 1669 |
with subtests.test(msg="Verify chord is deserialized"): |
| 1670 |
assert isinstance(deserialized_chord, chord) |
| 1671 |
# A header which is a group gets unpacked into the chord's `tasks` |
| 1672 |
with subtests.test( |
| 1673 |
msg="Validate chord header tasks are deserialized and unpacked" |
| 1674 |
): |
| 1675 |
assert all( |
| 1676 |
isinstance(child_task, group_type) |
| 1677 |
for child_task in deserialized_chord.tasks |
| 1678 |
) |
| 1679 |
assert all( |
| 1680 |
isinstance(grandchild_task, Signature) |
| 1681 |
for child_task in deserialized_chord.tasks |
| 1682 |
for grandchild_task in child_task.tasks |
| 1683 |
) |
| 1684 |
# A body which is a group remains as it we passed in |
| 1685 |
with subtests.test( |
| 1686 |
msg="Validate chord body is deserialized and not unpacked" |
| 1687 |
): |
| 1688 |
assert isinstance(deserialized_chord.body, group) |
| 1689 |
assert all( |
| 1690 |
isinstance(body_child_task, group) |
| 1691 |
for body_child_task in deserialized_chord.body.tasks |
| 1692 |
) |
| 1693 |
assert all( |
| 1694 |
isinstance(body_grandchild_task, Signature) |
| 1695 |
for body_child_task in deserialized_chord.body.tasks |
| 1696 |
for body_grandchild_task in body_child_task.tasks |
| 1697 |
) |
| 1698 |
|
| 1699 |
def test_from_dict_deep_deserialize_chain(self, subtests): |
| 1700 |
header = body = chain([self.add.s(1, 2)] * 42) |
| 1701 |
original_chord = chord(header=header, body=body) |
| 1702 |
serialized_chord = json.loads(json.dumps(original_chord)) |
| 1703 |
deserialized_chord = chord.from_dict(serialized_chord) |
| 1704 |
with subtests.test(msg="Verify chord is deserialized"): |
| 1705 |
assert isinstance(deserialized_chord, chord) |
| 1706 |
# A header which is a chain gets unpacked into the chord's `tasks` |
| 1707 |
with subtests.test( |
| 1708 |
msg="Validate chord header tasks are deserialized and unpacked" |
| 1709 |
): |
| 1710 |
assert all( |
| 1711 |
isinstance(child_task, Signature) |
| 1712 |
and not isinstance(child_task, chain) |
| 1713 |
for child_task in deserialized_chord.tasks |
| 1714 |
) |
| 1715 |
# A body which is a chain gets mutatated into the hidden `_chain` class |
| 1716 |
with subtests.test( |
| 1717 |
msg="Validate chord body is deserialized and not unpacked" |
| 1718 |
): |
| 1719 |
assert isinstance(deserialized_chord.body, _chain) |
| 1720 |
|
| 1721 |
def test_chord_clone_kwargs(self, subtests): |
| 1722 |
""" Test that chord clone ensures the kwargs are the same """ |
| 1723 |
|
| 1724 |
with subtests.test(msg='Verify chord cloning clones kwargs correctly'): |
| 1725 |
c = chord([signature('g'), signature('h')], signature('i'), kwargs={'U': 6}) |
| 1726 |
c2 = c.clone() |
| 1727 |
assert c2.kwargs == c.kwargs |
| 1728 |
|
| 1729 |
with subtests.test(msg='Cloning the chord with overridden kwargs'): |
| 1730 |
override_kw = {'X': 2} |
| 1731 |
c3 = c.clone(args=(1,), kwargs=override_kw) |
| 1732 |
|
| 1733 |
with subtests.test(msg='Verify the overridden kwargs were cloned correctly'): |
| 1734 |
new_kw = c.kwargs.copy() |
| 1735 |
new_kw.update(override_kw) |
| 1736 |
assert c3.kwargs == new_kw |
| 1737 |
|
| 1738 |
def test_flag_allow_error_cb_on_chord_header(self, subtests): |
| 1739 |
header_mock = [Mock(name='t1'), Mock(name='t2')] |
| 1740 |
header = group(header_mock) |
| 1741 |
body = Mock(name='tbody') |
| 1742 |
errback_sig = Mock(name='errback_sig') |
| 1743 |
chord_sig = chord(header, body, app=self.app) |
| 1744 |
|
| 1745 |
with subtests.test(msg='Verify the errback is not linked'): |
| 1746 |
# header |
| 1747 |
for child_sig in header_mock: |
| 1748 |
child_sig.link_error.assert_not_called() |
| 1749 |
# body |
| 1750 |
body.link_error.assert_not_called() |
| 1751 |
|
| 1752 |
with subtests.test(msg='Verify flag turned off links only the body'): |
| 1753 |
self.app.conf.task_allow_error_cb_on_chord_header = False |
| 1754 |
chord_sig.link_error(errback_sig) |
| 1755 |
# header |
| 1756 |
for child_sig in header_mock: |
| 1757 |
child_sig.link_error.assert_not_called() |
| 1758 |
# body |
| 1759 |
body.link_error.assert_called_once_with(errback_sig) |
| 1760 |
|
| 1761 |
with subtests.test(msg='Verify flag turned on links the header'): |
| 1762 |
self.app.conf.task_allow_error_cb_on_chord_header = True |
| 1763 |
chord_sig.link_error(errback_sig) |
| 1764 |
# header |
| 1765 |
for child_sig in header_mock: |
| 1766 |
child_sig.link_error.assert_called_once_with(errback_sig.clone(immutable=True)) |
| 1767 |
# body |
| 1768 |
body.link_error.assert_has_calls([call(errback_sig), call(errback_sig)]) |
| 1769 |
|
| 1770 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 1771 |
def test_flag_allow_error_cb_on_chord_header_various_header_types(self): |
| 1772 |
""" Test chord link_error with various header types. """ |
| 1773 |
self.app.conf.task_allow_error_cb_on_chord_header = True |
| 1774 |
headers = [ |
| 1775 |
signature('t'), |
| 1776 |
[signature('t'), signature('t')], |
| 1777 |
group(signature('t'), signature('t')) |
| 1778 |
] |
| 1779 |
for chord_header in headers: |
| 1780 |
c = chord(chord_header, signature('t'), app=self.app) |
| 1781 |
sig = signature('t') |
| 1782 |
errback = c.link_error(sig) |
| 1783 |
assert errback == sig |
| 1784 |
|
| 1785 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 1786 |
def test_flag_allow_error_cb_on_chord_header_with_dict_callback(self): |
| 1787 |
self.app.conf.task_allow_error_cb_on_chord_header = True |
| 1788 |
c = chord(group(signature('th1'), signature('th2')), signature('tbody'), app=self.app) |
| 1789 |
errback_dict = dict(signature('tcb')) |
| 1790 |
errback = c.link_error(errback_dict) |
| 1791 |
assert errback == errback_dict |
| 1792 |
|
| 1793 |
def test_chord__or__group_of_single_task(self): |
| 1794 |
""" Test chaining a chord to a group of a single task. """ |
| 1795 |
c = chord([signature('header')], signature('body')) |
| 1796 |
g = group(signature('t')) |
| 1797 |
stil_chord = c | g # g should be chained with the body of c |
| 1798 |
assert isinstance(stil_chord, chord) |
| 1799 |
assert isinstance(stil_chord.body, _chain) |
| 1800 |
|
| 1801 |
def test_chord_upgrade_on_chaining(self): |
| 1802 |
""" Test that chaining a chord with a group body upgrades to a new chord """ |
| 1803 |
c = chord([signature('header')], group(signature('body'))) |
| 1804 |
t = signature('t') |
| 1805 |
stil_chord = c | t # t should be chained with the body of c and create a new chord |
| 1806 |
assert isinstance(stil_chord, chord) |
| 1807 |
assert isinstance(stil_chord.body, chord) |
| 1808 |
|
| 1809 |
@pytest.mark.parametrize('header', [ |
| 1810 |
[signature('s1'), signature('s2')], |
| 1811 |
group(signature('s1'), signature('s2')) |
| 1812 |
]) |
| 1813 |
@pytest.mark.usefixtures('depends_on_current_app') |
| 1814 |
def test_link_error_on_chord_header(self, header): |
| 1815 |
""" Test that link_error on a chord also links the header """ |
| 1816 |
self.app.conf.task_allow_error_cb_on_chord_header = True |
| 1817 |
c = chord(header, signature('body'), app=self.app) |
| 1818 |
err = signature('err') |
| 1819 |
errback = c.link_error(err) |
| 1820 |
assert errback == err |
| 1821 |
for header_task in c.tasks: |
| 1822 |
assert header_task.options['link_error'] == [err.clone(immutable=True)] |
| 1823 |
assert c.body.options["link_error"] == [err] |
| 1824 |
|
| 1825 |
def test_chord_run_ensures_body_has_valid_task_id(self): |
| 1826 |
"""Test that chord.run() ensures body always gets a valid task ID. |
| 1827 |
|
| 1828 |
This is the unit test for the fix to issue #9773. The chord body should always |
| 1829 |
be frozen with a valid task ID to prevent "task_id must not be empty" errors. |
| 1830 |
""" |
| 1831 |
# Create a chord with header group and body chain |
| 1832 |
header = group([self.add.s(1, 1), self.add.s(2, 2)]) |
| 1833 |
body = chain(self.add.s(10, 10), self.add.s(20, 20)) |
| 1834 |
test_chord = chord(header, body) |
| 1835 |
|
| 1836 |
# Set up specific IDs for testing |
| 1837 |
chord_task_id = "test-chord-id" |
| 1838 |
group_task_id = "test-group-id" |
| 1839 |
header.options["task_id"] = group_task_id |
| 1840 |
|
| 1841 |
# Use patch to spy on body.freeze method |
| 1842 |
with patch.object(body, "freeze", wraps=body.freeze) as mock_freeze: |
| 1843 |
test_chord.run(header, body, (), task_id=chord_task_id) |
| 1844 |
|
| 1845 |
# Assert that body.freeze was called with the provided task_id and group_id |
| 1846 |
mock_freeze.assert_called_once_with( |
| 1847 |
chord_task_id, group_id=group_task_id, root_id=None |
| 1848 |
) |
| 1849 |
|
| 1850 |
def test_chord_run_generates_task_id_when_none_provided(self): |
| 1851 |
"""Test that chord.run() generates a task_id when none is provided.""" |
| 1852 |
# Create a chord with header group and body chain (no task_id set) |
| 1853 |
header = group([self.add.s(1, 1), self.add.s(2, 2)]) |
| 1854 |
body = chain(self.add.s(10, 10), self.add.s(20, 20)) |
| 1855 |
test_chord = chord(header, body) |
| 1856 |
|
| 1857 |
# Set group ID |
| 1858 |
group_id = "test-group-id" |
| 1859 |
header.options["task_id"] = group_id |
| 1860 |
|
| 1861 |
# Use patch to spy on body.freeze method |
| 1862 |
with patch.object(body, "freeze", wraps=body.freeze) as mock_freeze: |
| 1863 |
test_chord.run(header, body, (), task_id=None) |
| 1864 |
|
| 1865 |
# Assert that body.freeze was called with a generated UUID and group_id |
| 1866 |
mock_freeze.assert_called_once() |
| 1867 |
args, kwargs = mock_freeze.call_args |
| 1868 |
body_task_id = args[0] if args else kwargs.get("_id") |
| 1869 |
passed_group_id = kwargs.get("group_id") |
| 1870 |
|
| 1871 |
# Body should get a unique task ID (not None, not group_id) |
| 1872 |
assert body_task_id is not None |
| 1873 |
assert body_task_id != group_id # Should be different from group_id |
| 1874 |
assert passed_group_id == group_id # But should know its group |
| 1875 |
|
| 1876 |
def test_chord_run_body_freeze_prevents_task_id_empty_error(self): |
| 1877 |
"""Test that proper body.freeze() call prevents 'task_id must not be empty' error. |
| 1878 |
|
| 1879 |
This test ensures that when chord body is frozen with a valid task ID, |
| 1880 |
subsequent error handling won't encounter the "task_id must not be empty" error. |
| 1881 |
""" |
| 1882 |
# Create chord components |
| 1883 |
header = group([self.add.s(1, 1), self.add.s(2, 2)]) |
| 1884 |
body = chain(self.add.s(10, 10), self.add.s(20, 20)) |
| 1885 |
test_chord = chord(header, body) |
| 1886 |
|
| 1887 |
# Set a group task ID |
| 1888 |
group_id = "test-group-12345" |
| 1889 |
header.options["task_id"] = group_id |
| 1890 |
|
| 1891 |
# Run the chord with external task ID |
| 1892 |
external_task_id = "external-task-id" |
| 1893 |
result = test_chord.run(header, body, (), task_id=external_task_id) |
| 1894 |
|
| 1895 |
# Verify the frozen result has the external task ID, not group_id |
| 1896 |
assert result.id == external_task_id |
| 1897 |
assert body.id is not None |
| 1898 |
assert result.parent is not None |
| 1899 |
|
| 1900 |
# Body should know its group but have its own ID |
| 1901 |
assert body.options.get('group_id') == group_id or body.id != group_id |
| 1902 |
|
| 1903 |
def test_chord_run_body_freeze_with_no_external_task_id(self): |
| 1904 |
"""Test chord body gets unique ID when no external task_id provided.""" |
| 1905 |
header = group([self.add.s(1, 1), self.add.s(2, 2)]) |
| 1906 |
body = chain(self.add.s(10, 10), self.add.s(20, 20)) |
| 1907 |
test_chord = chord(header, body) |
| 1908 |
|
| 1909 |
group_id = "test-group-12345" |
| 1910 |
header.options["task_id"] = group_id |
| 1911 |
|
| 1912 |
# Run chord without external task ID |
| 1913 |
result = test_chord.run(header, body, (), task_id=None) |
| 1914 |
|
| 1915 |
# Body should get unique ID, different from group_id |
| 1916 |
assert result.id is not None |
| 1917 |
assert result.id != group_id |
| 1918 |
assert body.id is not None |
| 1919 |
assert body.id != group_id |
| 1920 |
|
| 1921 |
|
| 1922 |
class test_maybe_signature(CanvasCase): |
| 1923 |
|
| 1924 |
def test_is_None(self): |
| 1925 |
assert maybe_signature(None, app=self.app) is None |
| 1926 |
|
| 1927 |
def test_is_dict(self): |
| 1928 |
assert isinstance(maybe_signature(dict(self.add.s()), app=self.app), |
| 1929 |
Signature) |
| 1930 |
|
| 1931 |
def test_when_sig(self): |
| 1932 |
s = self.add.s() |
| 1933 |
assert maybe_signature(s, app=self.app) is s |
| 1934 |
|
| 1935 |
|
| 1936 |
class test_merge_dictionaries(CanvasCase): |
| 1937 |
|
| 1938 |
def test_docstring_example(self): |
| 1939 |
d1 = {'dict': {'a': 1}, 'list': [1, 2], 'tuple': (1, 2)} |
| 1940 |
d2 = {'dict': {'b': 2}, 'list': [3, 4], 'set': {'a', 'b'}} |
| 1941 |
_merge_dictionaries(d1, d2) |
| 1942 |
assert d1 == { |
| 1943 |
'dict': {'a': 1, 'b': 2}, |
| 1944 |
'list': [1, 2, 3, 4], |
| 1945 |
'tuple': (1, 2), |
| 1946 |
'set': {'a', 'b'} |
| 1947 |
} |
| 1948 |
|
| 1949 |
@pytest.mark.parametrize('d1,d2,expected_result', [ |
| 1950 |
( |
| 1951 |
{'None': None}, |
| 1952 |
{'None': None}, |
| 1953 |
{'None': [None]} |
| 1954 |
), |
| 1955 |
( |
| 1956 |
{'None': None}, |
| 1957 |
{'None': [None]}, |
| 1958 |
{'None': [[None]]} |
| 1959 |
), |
| 1960 |
( |
| 1961 |
{'None': None}, |
| 1962 |
{'None': 'Not None'}, |
| 1963 |
{'None': ['Not None']} |
| 1964 |
), |
| 1965 |
( |
| 1966 |
{'None': None}, |
| 1967 |
{'None': ['Not None']}, |
| 1968 |
{'None': [['Not None']]} |
| 1969 |
), |
| 1970 |
( |
| 1971 |
{'None': [None]}, |
| 1972 |
{'None': None}, |
| 1973 |
{'None': [None, None]} |
| 1974 |
), |
| 1975 |
( |
| 1976 |
{'None': [None]}, |
| 1977 |
{'None': [None]}, |
| 1978 |
{'None': [None, None]} |
| 1979 |
), |
| 1980 |
( |
| 1981 |
{'None': [None]}, |
| 1982 |
{'None': 'Not None'}, |
| 1983 |
{'None': [None, 'Not None']} |
| 1984 |
), |
| 1985 |
( |
| 1986 |
{'None': [None]}, |
| 1987 |
{'None': ['Not None']}, |
| 1988 |
{'None': [None, 'Not None']} |
| 1989 |
), |
| 1990 |
]) |
| 1991 |
def test_none_values(self, d1, d2, expected_result): |
| 1992 |
_merge_dictionaries(d1, d2) |
| 1993 |
assert d1 == expected_result |