test_canvas.py 22 KB

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