test_canvas.py 24 KB

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