test_canvas.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. from __future__ import absolute_import, unicode_literals
  2. import json
  3. import pytest
  4. from case import MagicMock, Mock
  5. from celery._state import _task_stack
  6. from celery.canvas import (
  7. Signature,
  8. chain,
  9. _chain,
  10. group,
  11. chord,
  12. signature,
  13. xmap,
  14. xstarmap,
  15. chunks,
  16. _maybe_group,
  17. maybe_signature,
  18. maybe_unroll_group,
  19. )
  20. from celery.result import AsyncResult, GroupResult, EagerResult
  21. SIG = Signature({
  22. 'task': 'TASK',
  23. 'args': ('A1',),
  24. 'kwargs': {'K1': 'V1'},
  25. 'options': {'task_id': 'TASK_ID'},
  26. 'subtask_type': ''},
  27. )
  28. class test_maybe_unroll_group:
  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. class CanvasCase:
  38. def setup(self):
  39. @self.app.task(shared=False)
  40. def add(x, y):
  41. return x + y
  42. self.add = add
  43. @self.app.task(shared=False)
  44. def mul(x, y):
  45. return x * y
  46. self.mul = mul
  47. @self.app.task(shared=False)
  48. def div(x, y):
  49. return x / y
  50. self.div = div
  51. class test_Signature(CanvasCase):
  52. def test_getitem_property_class(self):
  53. assert Signature.task
  54. assert Signature.args
  55. assert Signature.kwargs
  56. assert Signature.options
  57. assert Signature.subtask_type
  58. def test_getitem_property(self):
  59. assert SIG.task == 'TASK'
  60. assert SIG.args == ('A1',)
  61. assert SIG.kwargs == {'K1': 'V1'}
  62. assert SIG.options == {'task_id': 'TASK_ID'}
  63. assert SIG.subtask_type == ''
  64. def test_call(self):
  65. x = Signature('foo', (1, 2), {'arg1': 33}, app=self.app)
  66. x.type = Mock(name='type')
  67. x(3, 4, arg2=66)
  68. x.type.assert_called_with(3, 4, 1, 2, arg1=33, arg2=66)
  69. def test_link_on_scalar(self):
  70. x = Signature('TASK', link=Signature('B'))
  71. assert x.options['link']
  72. x.link(Signature('C'))
  73. assert isinstance(x.options['link'], list)
  74. assert Signature('B') in x.options['link']
  75. assert Signature('C') in x.options['link']
  76. def test_json(self):
  77. x = Signature('TASK', link=Signature('B', app=self.app), app=self.app)
  78. assert x.__json__() == dict(x)
  79. @pytest.mark.usefixtures('depends_on_current_app')
  80. def test_reduce(self):
  81. x = Signature('TASK', (2, 4), app=self.app)
  82. fun, args = x.__reduce__()
  83. assert fun(*args) == x
  84. def test_replace(self):
  85. x = Signature('TASK', ('A'), {})
  86. assert x.replace(args=('B',)).args == ('B',)
  87. assert x.replace(kwargs={'FOO': 'BAR'}).kwargs == {
  88. 'FOO': 'BAR',
  89. }
  90. assert x.replace(options={'task_id': '123'}).options == {
  91. 'task_id': '123',
  92. }
  93. def test_set(self):
  94. assert Signature('TASK', x=1).set(task_id='2').options == {
  95. 'x': 1, 'task_id': '2',
  96. }
  97. def test_link(self):
  98. x = signature(SIG)
  99. x.link(SIG)
  100. x.link(SIG)
  101. assert SIG in x.options['link']
  102. assert len(x.options['link']) == 1
  103. def test_link_error(self):
  104. x = signature(SIG)
  105. x.link_error(SIG)
  106. x.link_error(SIG)
  107. assert SIG in x.options['link_error']
  108. assert len(x.options['link_error']) == 1
  109. def test_flatten_links(self):
  110. tasks = [self.add.s(2, 2), self.mul.s(4), self.div.s(2)]
  111. tasks[0].link(tasks[1])
  112. tasks[1].link(tasks[2])
  113. assert tasks[0].flatten_links() == tasks
  114. def test_OR(self):
  115. x = self.add.s(2, 2) | self.mul.s(4)
  116. assert isinstance(x, _chain)
  117. y = self.add.s(4, 4) | self.div.s(2)
  118. z = x | y
  119. assert isinstance(y, _chain)
  120. assert isinstance(z, _chain)
  121. assert len(z.tasks) == 4
  122. with pytest.raises(TypeError):
  123. x | 10
  124. ax = self.add.s(2, 2) | (self.add.s(4) | self.add.s(8))
  125. assert isinstance(ax, _chain)
  126. assert len(ax.tasks), 3 == 'consolidates chain to chain'
  127. def test_INVERT(self):
  128. x = self.add.s(2, 2)
  129. x.apply_async = Mock()
  130. x.apply_async.return_value = Mock()
  131. x.apply_async.return_value.get = Mock()
  132. x.apply_async.return_value.get.return_value = 4
  133. assert ~x == 4
  134. x.apply_async.assert_called()
  135. def test_merge_immutable(self):
  136. x = self.add.si(2, 2, foo=1)
  137. args, kwargs, options = x._merge((4,), {'bar': 2}, {'task_id': 3})
  138. assert args == (2, 2)
  139. assert kwargs == {'foo': 1}
  140. assert options == {'task_id': 3}
  141. def test_set_immutable(self):
  142. x = self.add.s(2, 2)
  143. assert not x.immutable
  144. x.set(immutable=True)
  145. assert x.immutable
  146. x.set(immutable=False)
  147. assert not x.immutable
  148. def test_election(self):
  149. x = self.add.s(2, 2)
  150. x.freeze('foo')
  151. x.type.app.control = Mock()
  152. r = x.election()
  153. x.type.app.control.election.assert_called()
  154. assert r.id == 'foo'
  155. def test_AsyncResult_when_not_registered(self):
  156. s = signature('xxx.not.registered', app=self.app)
  157. assert s.AsyncResult
  158. def test_apply_async_when_not_registered(self):
  159. s = signature('xxx.not.registered', app=self.app)
  160. assert s._apply_async
  161. def test_keeping_link_error_on_chaining(self):
  162. x = self.add.s(2, 2) | self.mul.s(4)
  163. assert isinstance(x, _chain)
  164. x.link_error(SIG)
  165. assert SIG in x.options['link_error']
  166. t = signature(SIG)
  167. z = x | t
  168. assert isinstance(z, _chain)
  169. assert t in z.tasks
  170. assert not z.options.get('link_error')
  171. assert SIG in z.tasks[0].options['link_error']
  172. assert not z.tasks[2].options.get('link_error')
  173. assert SIG in x.options['link_error']
  174. assert t not in x.tasks
  175. assert not x.tasks[0].options.get('link_error')
  176. z = t | x
  177. assert isinstance(z, _chain)
  178. assert t in z.tasks
  179. assert not z.options.get('link_error')
  180. assert SIG in z.tasks[1].options['link_error']
  181. assert not z.tasks[0].options.get('link_error')
  182. assert SIG in x.options['link_error']
  183. assert t not in x.tasks
  184. assert not x.tasks[0].options.get('link_error')
  185. y = self.add.s(4, 4) | self.div.s(2)
  186. assert isinstance(y, _chain)
  187. z = x | y
  188. assert isinstance(z, _chain)
  189. assert not z.options.get('link_error')
  190. assert SIG in z.tasks[0].options['link_error']
  191. assert not z.tasks[2].options.get('link_error')
  192. assert SIG in x.options['link_error']
  193. assert not x.tasks[0].options.get('link_error')
  194. z = y | x
  195. assert isinstance(z, _chain)
  196. assert not z.options.get('link_error')
  197. assert SIG in z.tasks[3].options['link_error']
  198. assert not z.tasks[1].options.get('link_error')
  199. assert SIG in x.options['link_error']
  200. assert not x.tasks[0].options.get('link_error')
  201. class test_xmap_xstarmap(CanvasCase):
  202. def test_apply(self):
  203. for type, attr in [(xmap, 'map'), (xstarmap, 'starmap')]:
  204. args = [(i, i) for i in range(10)]
  205. s = getattr(self.add, attr)(args)
  206. s.type = Mock()
  207. s.apply_async(foo=1)
  208. s.type.apply_async.assert_called_with(
  209. (), {'task': self.add.s(), 'it': args}, foo=1,
  210. route_name=self.add.name,
  211. )
  212. assert type.from_dict(dict(s)) == s
  213. assert repr(s)
  214. class test_chunks(CanvasCase):
  215. def test_chunks(self):
  216. x = self.add.chunks(range(100), 10)
  217. assert dict(chunks.from_dict(dict(x), app=self.app)) == dict(x)
  218. assert x.group()
  219. assert len(x.group().tasks) == 10
  220. x.group = Mock()
  221. gr = x.group.return_value = Mock()
  222. x.apply_async()
  223. gr.apply_async.assert_called_with((), {}, route_name=self.add.name)
  224. gr.apply_async.reset_mock()
  225. x()
  226. gr.apply_async.assert_called_with((), {}, route_name=self.add.name)
  227. self.app.conf.task_always_eager = True
  228. chunks.apply_chunks(app=self.app, **x['kwargs'])
  229. class test_chain(CanvasCase):
  230. def test_clone_preserves_state(self):
  231. x = chain(self.add.s(i, i) for i in range(10))
  232. assert x.clone().tasks == x.tasks
  233. assert x.clone().kwargs == x.kwargs
  234. assert x.clone().args == x.args
  235. def test_repr(self):
  236. x = self.add.s(2, 2) | self.add.s(2)
  237. assert repr(x) == '%s(2, 2) | add(2)' % (self.add.name,)
  238. def test_apply_async(self):
  239. c = self.add.s(2, 2) | self.add.s(4) | self.add.s(8)
  240. result = c.apply_async()
  241. assert result.parent
  242. assert result.parent.parent
  243. assert result.parent.parent.parent is None
  244. def test_splices_chains(self):
  245. c = chain(
  246. self.add.s(5, 5),
  247. chain(self.add.s(6), self.add.s(7), self.add.s(8), app=self.app),
  248. app=self.app,
  249. )
  250. c.freeze()
  251. tasks, _ = c._frozen
  252. assert len(tasks) == 4
  253. def test_from_dict_no_tasks(self):
  254. assert chain.from_dict(dict(chain(app=self.app)), app=self.app)
  255. def test_from_dict_full_subtasks(self):
  256. c = chain(self.add.si(1, 2), self.add.si(3, 4), self.add.si(5, 6))
  257. serialized = json.loads(json.dumps(c))
  258. deserialized = chain.from_dict(serialized)
  259. for task in deserialized.tasks:
  260. assert isinstance(task, Signature)
  261. @pytest.mark.usefixtures('depends_on_current_app')
  262. def test_app_falls_back_to_default(self):
  263. from celery._state import current_app
  264. assert chain().app is current_app
  265. def test_handles_dicts(self):
  266. c = chain(
  267. self.add.s(5, 5), dict(self.add.s(8)), app=self.app,
  268. )
  269. c.freeze()
  270. tasks, _ = c._frozen
  271. for task in tasks:
  272. assert isinstance(task, Signature)
  273. assert task.app is self.app
  274. def test_group_to_chord(self):
  275. c = (
  276. self.add.s(5) |
  277. group([self.add.s(i, i) for i in range(5)], app=self.app) |
  278. self.add.s(10) |
  279. self.add.s(20) |
  280. self.add.s(30)
  281. )
  282. c._use_link = True
  283. tasks, results = c.prepare_steps((), c.tasks)
  284. assert tasks[-1].args[0] == 5
  285. assert isinstance(tasks[-2], chord)
  286. assert len(tasks[-2].tasks) == 5
  287. body = tasks[-2].body
  288. assert len(body.tasks) == 3
  289. assert body.tasks[0].args[0] == 10
  290. assert body.tasks[1].args[0] == 20
  291. assert body.tasks[2].args[0] == 30
  292. c2 = self.add.s(2, 2) | group(self.add.s(i, i) for i in range(10))
  293. c2._use_link = True
  294. tasks2, _ = c2.prepare_steps((), c2.tasks)
  295. assert isinstance(tasks2[0], group)
  296. def test_group_to_chord__protocol_2__or(self):
  297. c = (
  298. group([self.add.s(i, i) for i in range(5)], app=self.app) |
  299. self.add.s(10) |
  300. self.add.s(20) |
  301. self.add.s(30)
  302. )
  303. assert isinstance(c, chord)
  304. def test_group_to_chord__protocol_2(self):
  305. c = chain(
  306. group([self.add.s(i, i) for i in range(5)], app=self.app),
  307. self.add.s(10),
  308. self.add.s(20),
  309. self.add.s(30)
  310. )
  311. assert isinstance(c, chord)
  312. assert isinstance(c.body, _chain)
  313. assert len(c.body.tasks) == 3
  314. c2 = self.add.s(2, 2) | group(self.add.s(i, i) for i in range(10))
  315. c2._use_link = False
  316. tasks2, _ = c2.prepare_steps((), c2.tasks)
  317. assert isinstance(tasks2[0], group)
  318. def test_apply_options(self):
  319. class static(Signature):
  320. def clone(self, *args, **kwargs):
  321. return self
  322. def s(*args, **kwargs):
  323. return static(self.add, args, kwargs, type=self.add, app=self.app)
  324. c = s(2, 2) | s(4) | s(8)
  325. r1 = c.apply_async(task_id='some_id')
  326. assert r1.id == 'some_id'
  327. c.apply_async(group_id='some_group_id')
  328. assert c.tasks[-1].options['group_id'] == 'some_group_id'
  329. c.apply_async(chord='some_chord_id')
  330. assert c.tasks[-1].options['chord'] == 'some_chord_id'
  331. c.apply_async(link=[s(32)])
  332. assert c.tasks[-1].options['link'] == [s(32)]
  333. c.apply_async(link_error=[s('error')])
  334. for task in c.tasks:
  335. assert task.options['link_error'] == [s('error')]
  336. def test_reverse(self):
  337. x = self.add.s(2, 2) | self.add.s(2)
  338. assert isinstance(signature(x), _chain)
  339. assert isinstance(signature(dict(x)), _chain)
  340. def test_always_eager(self):
  341. self.app.conf.task_always_eager = True
  342. assert ~(self.add.s(4, 4) | self.add.s(8)) == 16
  343. def test_apply(self):
  344. x = chain(self.add.s(4, 4), self.add.s(8), self.add.s(10))
  345. res = x.apply()
  346. assert isinstance(res, EagerResult)
  347. assert res.get() == 26
  348. assert res.parent.get() == 16
  349. assert res.parent.parent.get() == 8
  350. assert res.parent.parent.parent is None
  351. def test_empty_chain_returns_none(self):
  352. assert chain(app=self.app)() is None
  353. assert chain(app=self.app).apply_async() is None
  354. def test_call_no_tasks(self):
  355. x = chain()
  356. assert not x()
  357. def test_call_with_tasks(self):
  358. x = self.add.s(2, 2) | self.add.s(4)
  359. x.apply_async = Mock()
  360. x(2, 2, foo=1)
  361. x.apply_async.assert_called_with((2, 2), {'foo': 1})
  362. def test_from_dict_no_args__with_args(self):
  363. x = dict(self.add.s(2, 2) | self.add.s(4))
  364. x['args'] = None
  365. assert isinstance(chain.from_dict(x), _chain)
  366. x['args'] = (2,)
  367. assert isinstance(chain.from_dict(x), _chain)
  368. def test_accepts_generator_argument(self):
  369. x = chain(self.add.s(i) for i in range(10))
  370. assert x.tasks[0].type, self.add
  371. assert x.type
  372. def test_chord_sets_result_parent(self):
  373. g = (self.add.s(0, 0) |
  374. group(self.add.s(i, i) for i in range(1, 10)) |
  375. self.add.s(2, 2) |
  376. self.add.s(4, 4))
  377. res = g.freeze()
  378. assert isinstance(res, AsyncResult)
  379. assert not isinstance(res, GroupResult)
  380. assert isinstance(res.parent, AsyncResult)
  381. assert not isinstance(res.parent, GroupResult)
  382. assert isinstance(res.parent.parent, GroupResult)
  383. assert isinstance(res.parent.parent.parent, AsyncResult)
  384. assert not isinstance(res.parent.parent.parent, GroupResult)
  385. assert res.parent.parent.parent.parent is None
  386. seen = set()
  387. node = res
  388. while node:
  389. assert node.id not in seen
  390. seen.add(node.id)
  391. node = node.parent
  392. class test_group(CanvasCase):
  393. def test_repr(self):
  394. x = group([self.add.s(2, 2), self.add.s(4, 4)])
  395. assert repr(x)
  396. def test_reverse(self):
  397. x = group([self.add.s(2, 2), self.add.s(4, 4)])
  398. assert isinstance(signature(x), group)
  399. assert isinstance(signature(dict(x)), group)
  400. def test_cannot_link_on_group(self):
  401. x = group([self.add.s(2, 2), self.add.s(4, 4)])
  402. with pytest.raises(TypeError):
  403. x.apply_async(link=self.add.s(2, 2))
  404. def test_cannot_link_error_on_group(self):
  405. x = group([self.add.s(2, 2), self.add.s(4, 4)])
  406. with pytest.raises(TypeError):
  407. x.apply_async(link_error=self.add.s(2, 2))
  408. def test_group_with_group_argument(self):
  409. g1 = group(self.add.s(2, 2), self.add.s(4, 4), app=self.app)
  410. g2 = group(g1, app=self.app)
  411. assert g2.tasks is g1.tasks
  412. def test_maybe_group_sig(self):
  413. assert _maybe_group(self.add.s(2, 2), self.app) == [self.add.s(2, 2)]
  414. def test_apply(self):
  415. x = group([self.add.s(4, 4), self.add.s(8, 8)])
  416. res = x.apply()
  417. assert res.get(), [8 == 16]
  418. def test_apply_async(self):
  419. x = group([self.add.s(4, 4), self.add.s(8, 8)])
  420. x.apply_async()
  421. def test_prepare_with_dict(self):
  422. x = group([self.add.s(4, 4), dict(self.add.s(8, 8))], app=self.app)
  423. x.apply_async()
  424. def test_group_in_group(self):
  425. g1 = group(self.add.s(2, 2), self.add.s(4, 4), app=self.app)
  426. g2 = group(self.add.s(8, 8), g1, self.add.s(16, 16), app=self.app)
  427. g2.apply_async()
  428. def test_set_immutable(self):
  429. g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app)
  430. g1.set_immutable(True)
  431. for task in g1.tasks:
  432. task.set_immutable.assert_called_with(True)
  433. def test_link(self):
  434. g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app)
  435. sig = Mock(name='sig')
  436. g1.link(sig)
  437. g1.tasks[0].link.assert_called_with(sig.clone().set(immutable=True))
  438. def test_link_error(self):
  439. g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app)
  440. sig = Mock(name='sig')
  441. g1.link_error(sig)
  442. g1.tasks[0].link_error.assert_called_with(
  443. sig.clone().set(immutable=True),
  444. )
  445. def test_apply_empty(self):
  446. x = group(app=self.app)
  447. x.apply()
  448. res = x.apply_async()
  449. assert res
  450. assert not res.results
  451. def test_apply_async_with_parent(self):
  452. _task_stack.push(self.add)
  453. try:
  454. self.add.push_request(called_directly=False)
  455. try:
  456. assert not self.add.request.children
  457. x = group([self.add.s(4, 4), self.add.s(8, 8)])
  458. res = x()
  459. assert self.add.request.children
  460. assert res in self.add.request.children
  461. assert len(self.add.request.children) == 1
  462. finally:
  463. self.add.pop_request()
  464. finally:
  465. _task_stack.pop()
  466. def test_from_dict(self):
  467. x = group([self.add.s(2, 2), self.add.s(4, 4)])
  468. x['args'] = (2, 2)
  469. assert group.from_dict(dict(x))
  470. x['args'] = None
  471. assert group.from_dict(dict(x))
  472. def test_call_empty_group(self):
  473. x = group(app=self.app)
  474. assert not len(x())
  475. x.delay()
  476. x.apply_async()
  477. x()
  478. def test_skew(self):
  479. g = group([self.add.s(i, i) for i in range(10)])
  480. g.skew(start=1, stop=10, step=1)
  481. for i, task in enumerate(g.tasks):
  482. assert task.options['countdown'] == i + 1
  483. def test_iter(self):
  484. g = group([self.add.s(i, i) for i in range(10)])
  485. assert list(iter(g)) == list(g.keys())
  486. def test_single_task(self):
  487. g = group([self.add.s(1, 1)])
  488. assert isinstance(g, group)
  489. assert len(g.tasks) == 1
  490. g = group(self.add.s(1, 1))
  491. assert isinstance(g, group)
  492. assert len(g.tasks) == 1
  493. @staticmethod
  494. def helper_test_get_delay(result):
  495. import time
  496. t0 = time.time()
  497. while not result.ready():
  498. time.sleep(0.01)
  499. if time.time() - t0 > 1:
  500. return None
  501. return result.get()
  502. def test_kwargs_direct(self):
  503. res = [self.add(x=1, y=1), self.add(x=1, y=1)]
  504. assert res == [2, 2]
  505. def test_kwargs_apply(self):
  506. x = group([self.add.s(), self.add.s()])
  507. res = x.apply(kwargs={'x': 1, 'y': 1}).get()
  508. assert res == [2, 2]
  509. def test_kwargs_apply_async(self):
  510. self.app.conf.task_always_eager = True
  511. x = group([self.add.s(), self.add.s()])
  512. res = self.helper_test_get_delay(
  513. x.apply_async(kwargs={'x': 1, 'y': 1})
  514. )
  515. assert res == [2, 2]
  516. def test_kwargs_delay(self):
  517. self.app.conf.task_always_eager = True
  518. x = group([self.add.s(), self.add.s()])
  519. res = self.helper_test_get_delay(x.delay(x=1, y=1))
  520. assert res == [2, 2]
  521. def test_kwargs_delay_partial(self):
  522. self.app.conf.task_always_eager = True
  523. x = group([self.add.s(1), self.add.s(x=1)])
  524. res = self.helper_test_get_delay(x.delay(y=1))
  525. assert res == [2, 2]
  526. class test_chord(CanvasCase):
  527. def test_reverse(self):
  528. x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4))
  529. assert isinstance(signature(x), chord)
  530. assert isinstance(signature(dict(x)), chord)
  531. def test_clone_clones_body(self):
  532. x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4))
  533. y = x.clone()
  534. assert x.kwargs['body'] is not y.kwargs['body']
  535. y.kwargs.pop('body')
  536. z = y.clone()
  537. assert z.kwargs.get('body') is None
  538. def test_argument_is_group(self):
  539. x = chord(group(self.add.s(2, 2), self.add.s(4, 4), app=self.app))
  540. assert x.tasks
  541. def test_app_when_app(self):
  542. app = Mock(name='app')
  543. x = chord([self.add.s(4, 4)], app=app)
  544. assert x.app is app
  545. def test_app_when_app_in_task(self):
  546. t1 = Mock(name='t1')
  547. t2 = Mock(name='t2')
  548. x = chord([t1, self.add.s(4, 4)])
  549. assert x.app is x.tasks[0].app
  550. t1.app = None
  551. x = chord([t1], body=t2)
  552. assert x.app is t2._app
  553. def test_app_when_header_is_empty(self):
  554. x = chord([], self.add.s(4, 4))
  555. assert x.app is self.add.app
  556. @pytest.mark.usefixtures('depends_on_current_app')
  557. def test_app_fallback_to_current(self):
  558. from celery._state import current_app
  559. t1 = Mock(name='t1')
  560. t1.app = t1._app = None
  561. x = chord([t1], body=t1)
  562. assert x.app is current_app
  563. def test_set_immutable(self):
  564. x = chord([Mock(name='t1'), Mock(name='t2')], app=self.app)
  565. x.set_immutable(True)
  566. def test_links_to_body(self):
  567. x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4))
  568. x.link(self.div.s(2))
  569. assert not x.options.get('link')
  570. assert x.kwargs['body'].options['link']
  571. x.link_error(self.div.s(2))
  572. assert not x.options.get('link_error')
  573. assert x.kwargs['body'].options['link_error']
  574. assert x.tasks
  575. assert x.body
  576. def test_repr(self):
  577. x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4))
  578. assert repr(x)
  579. x.kwargs['body'] = None
  580. assert 'without body' in repr(x)
  581. def test_freeze_tasks_is_not_group(self):
  582. x = chord([self.add.s(2, 2)], body=self.add.s(), app=self.app)
  583. x.freeze()
  584. x.tasks = [self.add.s(2, 2)]
  585. x.freeze()
  586. class test_maybe_signature(CanvasCase):
  587. def test_is_None(self):
  588. assert maybe_signature(None, app=self.app) is None
  589. def test_is_dict(self):
  590. assert isinstance(maybe_signature(dict(self.add.s()), app=self.app),
  591. Signature)
  592. def test_when_sig(self):
  593. s = self.add.s()
  594. assert maybe_signature(s, app=self.app) is s