test_canvas.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  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_repr(self):
  189. x = self.add.s(2, 2) | self.add.s(2)
  190. assert repr(x) == '%s(2, 2) | %s(2)' % (self.add.name, self.add.name)
  191. def test_apply_async(self):
  192. c = self.add.s(2, 2) | self.add.s(4) | self.add.s(8)
  193. result = c.apply_async()
  194. assert result.parent
  195. assert result.parent.parent
  196. assert result.parent.parent.parent is None
  197. def test_group_to_chord__freeze_parent_id(self):
  198. def using_freeze(c):
  199. c.freeze(parent_id='foo', root_id='root')
  200. return c._frozen[0]
  201. self.assert_group_to_chord_parent_ids(using_freeze)
  202. def assert_group_to_chord_parent_ids(self, freezefun):
  203. c = (
  204. self.add.s(5, 5) |
  205. group([self.add.s(i, i) for i in range(5)], app=self.app) |
  206. self.add.si(10, 10) |
  207. self.add.si(20, 20) |
  208. self.add.si(30, 30)
  209. )
  210. tasks = freezefun(c)
  211. assert tasks[-1].parent_id == 'foo'
  212. assert tasks[-1].root_id == 'root'
  213. assert tasks[-2].parent_id == tasks[-1].id
  214. assert tasks[-2].root_id == 'root'
  215. assert tasks[-2].body.parent_id == tasks[-2].tasks.id
  216. assert tasks[-2].body.parent_id == tasks[-2].id
  217. assert tasks[-2].body.root_id == 'root'
  218. assert tasks[-2].tasks.tasks[0].parent_id == tasks[-1].id
  219. assert tasks[-2].tasks.tasks[0].root_id == 'root'
  220. assert tasks[-2].tasks.tasks[1].parent_id == tasks[-1].id
  221. assert tasks[-2].tasks.tasks[1].root_id == 'root'
  222. assert tasks[-2].tasks.tasks[2].parent_id == tasks[-1].id
  223. assert tasks[-2].tasks.tasks[2].root_id == 'root'
  224. assert tasks[-2].tasks.tasks[3].parent_id == tasks[-1].id
  225. assert tasks[-2].tasks.tasks[3].root_id == 'root'
  226. assert tasks[-2].tasks.tasks[4].parent_id == tasks[-1].id
  227. assert tasks[-2].tasks.tasks[4].root_id == 'root'
  228. assert tasks[-3].parent_id == tasks[-2].body.id
  229. assert tasks[-3].root_id == 'root'
  230. assert tasks[-4].parent_id == tasks[-3].id
  231. assert tasks[-4].root_id == 'root'
  232. def test_splices_chains(self):
  233. c = chain(
  234. self.add.s(5, 5),
  235. chain(self.add.s(6), self.add.s(7), self.add.s(8), app=self.app),
  236. app=self.app,
  237. )
  238. c.freeze()
  239. tasks, _ = c._frozen
  240. assert len(tasks) == 4
  241. def test_from_dict_no_tasks(self):
  242. assert chain.from_dict(dict(chain(app=self.app)), app=self.app)
  243. @pytest.mark.usefixtures('depends_on_current_app')
  244. def test_app_falls_back_to_default(self):
  245. from celery._state import current_app
  246. assert chain().app is current_app
  247. def test_handles_dicts(self):
  248. c = chain(
  249. self.add.s(5, 5), dict(self.add.s(8)), app=self.app,
  250. )
  251. c.freeze()
  252. tasks, _ = c._frozen
  253. for task in tasks:
  254. assert isinstance(task, Signature)
  255. assert task.app is self.app
  256. def test_group_to_chord(self):
  257. c = (
  258. self.add.s(5) |
  259. group([self.add.s(i, i) for i in range(5)], app=self.app) |
  260. self.add.s(10) |
  261. self.add.s(20) |
  262. self.add.s(30)
  263. )
  264. c._use_link = True
  265. tasks, results = c.prepare_steps((), c.tasks)
  266. assert tasks[-1].args[0] == 5
  267. assert isinstance(tasks[-2], chord)
  268. assert len(tasks[-2].tasks) == 5
  269. assert tasks[-2].parent_id == tasks[-1].id
  270. assert tasks[-2].root_id == tasks[-1].id
  271. assert tasks[-2].body.args[0] == 10
  272. assert tasks[-2].body.parent_id == tasks[-2].id
  273. assert tasks[-3].args[0] == 20
  274. assert tasks[-3].root_id == tasks[-1].id
  275. assert tasks[-3].parent_id == tasks[-2].body.id
  276. assert tasks[-4].args[0] == 30
  277. assert tasks[-4].parent_id == tasks[-3].id
  278. assert tasks[-4].root_id == tasks[-1].id
  279. assert tasks[-2].body.options['link']
  280. assert tasks[-2].body.options['link'][0].options['link']
  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(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. c._use_link = False
  293. tasks, _ = c.prepare_steps((), c.tasks)
  294. assert isinstance(tasks[-1], chord)
  295. c2 = self.add.s(2, 2) | group(self.add.s(i, i) for i in range(10))
  296. c2._use_link = False
  297. tasks2, _ = c2.prepare_steps((), c2.tasks)
  298. assert isinstance(tasks2[0], group)
  299. def test_apply_options(self):
  300. class static(Signature):
  301. def clone(self, *args, **kwargs):
  302. return self
  303. def s(*args, **kwargs):
  304. return static(self.add, args, kwargs, type=self.add, app=self.app)
  305. c = s(2, 2) | s(4) | s(8)
  306. r1 = c.apply_async(task_id='some_id')
  307. assert r1.id == 'some_id'
  308. c.apply_async(group_id='some_group_id')
  309. assert c.tasks[-1].options['group_id'] == 'some_group_id'
  310. c.apply_async(chord='some_chord_id')
  311. assert c.tasks[-1].options['chord'] == 'some_chord_id'
  312. c.apply_async(link=[s(32)])
  313. assert c.tasks[-1].options['link'] == [s(32)]
  314. c.apply_async(link_error=[s('error')])
  315. for task in c.tasks:
  316. assert task.options['link_error'] == [s('error')]
  317. def test_reverse(self):
  318. x = self.add.s(2, 2) | self.add.s(2)
  319. assert isinstance(signature(x), chain)
  320. assert isinstance(signature(dict(x)), chain)
  321. def test_always_eager(self):
  322. self.app.conf.task_always_eager = True
  323. assert ~(self.add.s(4, 4) | self.add.s(8)) == 16
  324. def test_apply(self):
  325. x = chain(self.add.s(4, 4), self.add.s(8), self.add.s(10))
  326. res = x.apply()
  327. assert isinstance(res, EagerResult)
  328. assert res.get() == 26
  329. assert res.parent.get() == 16
  330. assert res.parent.parent.get() == 8
  331. assert res.parent.parent.parent is None
  332. def test_empty_chain_returns_none(self):
  333. assert chain(app=self.app)() is None
  334. assert chain(app=self.app).apply_async() is None
  335. def test_root_id_parent_id(self):
  336. self.app.conf.task_protocol = 2
  337. c = chain(self.add.si(i, i) for i in range(4))
  338. c.freeze()
  339. tasks, _ = c._frozen
  340. for i, task in enumerate(tasks):
  341. assert task.root_id == tasks[-1].id
  342. try:
  343. assert task.parent_id == tasks[i + 1].id
  344. except IndexError:
  345. assert i == len(tasks) - 1
  346. else:
  347. valid_parents = i
  348. assert valid_parents == len(tasks) - 2
  349. self.assert_sent_with_ids(tasks[-1], tasks[-1].id, 'foo',
  350. parent_id='foo')
  351. assert tasks[-2].options['parent_id']
  352. self.assert_sent_with_ids(tasks[-2], tasks[-1].id, tasks[-1].id)
  353. self.assert_sent_with_ids(tasks[-3], tasks[-1].id, tasks[-2].id)
  354. self.assert_sent_with_ids(tasks[-4], tasks[-1].id, tasks[-3].id)
  355. def assert_sent_with_ids(self, task, rid, pid, **options):
  356. self.app.amqp.send_task_message = Mock(name='send_task_message')
  357. self.app.backend = Mock()
  358. self.app.producer_or_acquire = ContextMock()
  359. task.apply_async(**options)
  360. self.app.amqp.send_task_message.assert_called()
  361. message = self.app.amqp.send_task_message.call_args[0][2]
  362. assert message.headers['parent_id'] == pid
  363. assert message.headers['root_id'] == rid
  364. def test_call_no_tasks(self):
  365. x = chain()
  366. assert not x()
  367. def test_call_with_tasks(self):
  368. x = self.add.s(2, 2) | self.add.s(4)
  369. x.apply_async = Mock()
  370. x(2, 2, foo=1)
  371. x.apply_async.assert_called_with((2, 2), {'foo': 1})
  372. def test_from_dict_no_args__with_args(self):
  373. x = dict(self.add.s(2, 2) | self.add.s(4))
  374. x['args'] = None
  375. assert isinstance(chain.from_dict(x), chain)
  376. x['args'] = (2,)
  377. assert isinstance(chain.from_dict(x), chain)
  378. def test_accepts_generator_argument(self):
  379. x = chain(self.add.s(i) for i in range(10))
  380. assert x.tasks[0].type, self.add
  381. assert x.type
  382. class test_group(CanvasCase):
  383. def test_repr(self):
  384. x = group([self.add.s(2, 2), self.add.s(4, 4)])
  385. assert repr(x)
  386. def test_reverse(self):
  387. x = group([self.add.s(2, 2), self.add.s(4, 4)])
  388. assert isinstance(signature(x), group)
  389. assert isinstance(signature(dict(x)), group)
  390. def test_group_with_group_argument(self):
  391. g1 = group(self.add.s(2, 2), self.add.s(4, 4), app=self.app)
  392. g2 = group(g1, app=self.app)
  393. assert g2.tasks is g1.tasks
  394. def test_maybe_group_sig(self):
  395. assert _maybe_group(self.add.s(2, 2), self.app) == [self.add.s(2, 2)]
  396. def test_apply(self):
  397. x = group([self.add.s(4, 4), self.add.s(8, 8)])
  398. res = x.apply()
  399. assert res.get(), [8 == 16]
  400. def test_apply_async(self):
  401. x = group([self.add.s(4, 4), self.add.s(8, 8)])
  402. x.apply_async()
  403. def test_prepare_with_dict(self):
  404. x = group([self.add.s(4, 4), dict(self.add.s(8, 8))], app=self.app)
  405. x.apply_async()
  406. def test_group_in_group(self):
  407. g1 = group(self.add.s(2, 2), self.add.s(4, 4), app=self.app)
  408. g2 = group(self.add.s(8, 8), g1, self.add.s(16, 16), app=self.app)
  409. g2.apply_async()
  410. def test_set_immutable(self):
  411. g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app)
  412. g1.set_immutable(True)
  413. for task in g1.tasks:
  414. task.set_immutable.assert_called_with(True)
  415. def test_link(self):
  416. g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app)
  417. sig = Mock(name='sig')
  418. g1.link(sig)
  419. g1.tasks[0].link.assert_called_with(sig.clone().set(immutable=True))
  420. def test_link_error(self):
  421. g1 = group(Mock(name='t1'), Mock(name='t2'), app=self.app)
  422. sig = Mock(name='sig')
  423. g1.link_error(sig)
  424. g1.tasks[0].link_error.assert_called_with(
  425. sig.clone().set(immutable=True),
  426. )
  427. def test_apply_empty(self):
  428. x = group(app=self.app)
  429. x.apply()
  430. res = x.apply_async()
  431. assert res
  432. assert not res.results
  433. def test_apply_async_with_parent(self):
  434. _task_stack.push(self.add)
  435. try:
  436. self.add.push_request(called_directly=False)
  437. try:
  438. assert not self.add.request.children
  439. x = group([self.add.s(4, 4), self.add.s(8, 8)])
  440. res = x()
  441. assert self.add.request.children
  442. assert res in self.add.request.children
  443. assert len(self.add.request.children) == 1
  444. finally:
  445. self.add.pop_request()
  446. finally:
  447. _task_stack.pop()
  448. def test_from_dict(self):
  449. x = group([self.add.s(2, 2), self.add.s(4, 4)])
  450. x['args'] = (2, 2)
  451. assert group.from_dict(dict(x))
  452. x['args'] = None
  453. assert group.from_dict(dict(x))
  454. def test_call_empty_group(self):
  455. x = group(app=self.app)
  456. assert not len(x())
  457. x.delay()
  458. x.apply_async()
  459. x()
  460. def test_skew(self):
  461. g = group([self.add.s(i, i) for i in range(10)])
  462. g.skew(start=1, stop=10, step=1)
  463. for i, task in enumerate(g.tasks):
  464. assert task.options['countdown'] == i + 1
  465. def test_iter(self):
  466. g = group([self.add.s(i, i) for i in range(10)])
  467. assert list(iter(g)) == g.tasks
  468. @staticmethod
  469. def helper_test_get_delay(result):
  470. import time
  471. t0 = time.time()
  472. while not result.ready():
  473. time.sleep(0.01)
  474. if time.time() - t0 > 1:
  475. return None
  476. return result.get()
  477. def test_kwargs_direct(self):
  478. res = [self.add(x=1, y=1), self.add(x=1, y=1)]
  479. assert res == [2, 2]
  480. def test_kwargs_apply(self):
  481. x = group([self.add.s(), self.add.s()])
  482. res = x.apply(kwargs=dict(x=1, y=1)).get()
  483. assert res == [2, 2]
  484. def test_kwargs_apply_async(self):
  485. self.app.conf.task_always_eager = True
  486. x = group([self.add.s(), self.add.s()])
  487. res = self.helper_test_get_delay(x.apply_async(kwargs=dict(x=1, y=1)))
  488. assert res == [2, 2]
  489. def test_kwargs_delay(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.delay(x=1, y=1))
  493. assert res == [2, 2]
  494. def test_kwargs_delay_partial(self):
  495. self.app.conf.task_always_eager = True
  496. x = group([self.add.s(1), self.add.s(x=1)])
  497. res = self.helper_test_get_delay(x.delay(y=1))
  498. assert res == [2, 2]
  499. class test_chord(CanvasCase):
  500. def test_reverse(self):
  501. x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4))
  502. assert isinstance(signature(x), chord)
  503. assert isinstance(signature(dict(x)), chord)
  504. def test_clone_clones_body(self):
  505. x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4))
  506. y = x.clone()
  507. assert x.kwargs['body'] is not y.kwargs['body']
  508. y.kwargs.pop('body')
  509. z = y.clone()
  510. assert z.kwargs.get('body') is None
  511. def test_argument_is_group(self):
  512. x = chord(group(self.add.s(2, 2), self.add.s(4, 4), app=self.app))
  513. assert x.tasks
  514. def test_set_parent_id(self):
  515. x = chord(group(self.add.s(2, 2)))
  516. x.tasks = [self.add.s(2, 2)]
  517. x.set_parent_id('pid')
  518. def test_app_when_app(self):
  519. app = Mock(name='app')
  520. x = chord([self.add.s(4, 4)], app=app)
  521. assert x.app is app
  522. def test_app_when_app_in_task(self):
  523. t1 = Mock(name='t1')
  524. t2 = Mock(name='t2')
  525. x = chord([t1, self.add.s(4, 4)])
  526. assert x.app is x.tasks[0].app
  527. t1.app = None
  528. x = chord([t1], body=t2)
  529. assert x.app is t2._app
  530. @pytest.mark.usefixtures('depends_on_current_app')
  531. def test_app_fallback_to_current(self):
  532. from celery._state import current_app
  533. t1 = Mock(name='t1')
  534. t1.app = t1._app = None
  535. x = chord([t1], body=t1)
  536. assert x.app is current_app
  537. def test_set_immutable(self):
  538. x = chord([Mock(name='t1'), Mock(name='t2')], app=self.app)
  539. x.set_immutable(True)
  540. def test_links_to_body(self):
  541. x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4))
  542. x.link(self.div.s(2))
  543. assert not x.options.get('link')
  544. assert x.kwargs['body'].options['link']
  545. x.link_error(self.div.s(2))
  546. assert not x.options.get('link_error')
  547. assert x.kwargs['body'].options['link_error']
  548. assert x.tasks
  549. assert x.body
  550. def test_repr(self):
  551. x = chord([self.add.s(2, 2), self.add.s(4, 4)], body=self.mul.s(4))
  552. assert repr(x)
  553. x.kwargs['body'] = None
  554. assert 'without body' in repr(x)
  555. def test_freeze_tasks_is_not_group(self):
  556. x = chord([self.add.s(2, 2)], body=self.add.s(), app=self.app)
  557. x.freeze()
  558. x.tasks = [self.add.s(2, 2)]
  559. x.freeze()
  560. class test_maybe_signature(CanvasCase):
  561. def test_is_None(self):
  562. assert maybe_signature(None, app=self.app) is None
  563. def test_is_dict(self):
  564. assert isinstance(maybe_signature(dict(self.add.s()), app=self.app),
  565. Signature)
  566. def test_when_sig(self):
  567. s = self.add.s()
  568. assert maybe_signature(s, app=self.app) is s