test_tasks.py 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. from __future__ import absolute_import, unicode_literals
  2. import socket
  3. import tempfile
  4. from datetime import datetime, timedelta
  5. import pytest
  6. from case import ANY, ContextMock, MagicMock, Mock, patch
  7. from kombu import Queue
  8. from celery import Task, group, uuid
  9. from celery.app.task import _reprtask
  10. from celery.exceptions import Ignore, ImproperlyConfigured, Retry
  11. from celery.five import items, range, string_t
  12. from celery.result import EagerResult
  13. from celery.task.base import Task as OldTask
  14. from celery.utils.time import parse_iso8601
  15. try:
  16. from urllib.error import HTTPError
  17. except ImportError: # pragma: no cover
  18. from urllib2 import HTTPError
  19. def return_True(*args, **kwargs):
  20. # Task run functions can't be closures/lambdas, as they're pickled.
  21. return True
  22. class MockApplyTask(Task):
  23. abstract = True
  24. applied = 0
  25. def run(self, x, y):
  26. return x * y
  27. def apply_async(self, *args, **kwargs):
  28. self.applied += 1
  29. class TasksCase:
  30. def setup(self):
  31. self.mytask = self.app.task(shared=False)(return_True)
  32. @self.app.task(bind=True, count=0, shared=False)
  33. def increment_counter(self, increment_by=1):
  34. self.count += increment_by or 1
  35. return self.count
  36. self.increment_counter = increment_counter
  37. @self.app.task(shared=False)
  38. def raising():
  39. raise KeyError('foo')
  40. self.raising = raising
  41. @self.app.task(bind=True, max_retries=3, iterations=0, shared=False)
  42. def retry_task(self, arg1, arg2, kwarg=1, max_retries=None, care=True):
  43. self.iterations += 1
  44. rmax = self.max_retries if max_retries is None else max_retries
  45. assert repr(self.request)
  46. retries = self.request.retries
  47. if care and retries >= rmax:
  48. return arg1
  49. else:
  50. raise self.retry(countdown=0, max_retries=rmax)
  51. self.retry_task = retry_task
  52. @self.app.task(bind=True, max_retries=3, iterations=0, shared=False)
  53. def retry_task_noargs(self, **kwargs):
  54. self.iterations += 1
  55. if self.request.retries >= 3:
  56. return 42
  57. else:
  58. raise self.retry(countdown=0)
  59. self.retry_task_noargs = retry_task_noargs
  60. @self.app.task(bind=True, max_retries=3, iterations=0,
  61. base=MockApplyTask, shared=False)
  62. def retry_task_mockapply(self, arg1, arg2, kwarg=1):
  63. self.iterations += 1
  64. retries = self.request.retries
  65. if retries >= 3:
  66. return arg1
  67. raise self.retry(countdown=0)
  68. self.retry_task_mockapply = retry_task_mockapply
  69. @self.app.task(bind=True, max_retries=3, iterations=0, shared=False)
  70. def retry_task_customexc(self, arg1, arg2, kwarg=1, **kwargs):
  71. self.iterations += 1
  72. retries = self.request.retries
  73. if retries >= 3:
  74. return arg1 + kwarg
  75. else:
  76. try:
  77. raise MyCustomException('Elaine Marie Benes')
  78. except MyCustomException as exc:
  79. kwargs.update(kwarg=kwarg)
  80. raise self.retry(countdown=0, exc=exc)
  81. self.retry_task_customexc = retry_task_customexc
  82. @self.app.task(bind=True, autoretry_for=(ZeroDivisionError,),
  83. shared=False)
  84. def autoretry_task_no_kwargs(self, a, b):
  85. self.iterations += 1
  86. return a / b
  87. self.autoretry_task_no_kwargs = autoretry_task_no_kwargs
  88. @self.app.task(bind=True, autoretry_for=(ZeroDivisionError,),
  89. retry_kwargs={'max_retries': 5}, shared=False)
  90. def autoretry_task(self, a, b):
  91. self.iterations += 1
  92. return a / b
  93. self.autoretry_task = autoretry_task
  94. @self.app.task(bind=True, autoretry_for=(HTTPError,),
  95. retry_backoff=True, shared=False)
  96. def autoretry_backoff_task(self, url):
  97. self.iterations += 1
  98. if "error" in url:
  99. fp = tempfile.TemporaryFile()
  100. raise HTTPError(url, '500', 'Error', '', fp)
  101. return url
  102. self.autoretry_backoff_task = autoretry_backoff_task
  103. @self.app.task(bind=True, autoretry_for=(HTTPError,),
  104. retry_backoff=True, retry_jitter=True, shared=False)
  105. def autoretry_backoff_jitter_task(self, url):
  106. self.iterations += 1
  107. if "error" in url:
  108. fp = tempfile.TemporaryFile()
  109. raise HTTPError(url, '500', 'Error', '', fp)
  110. return url
  111. self.autoretry_backoff_jitter_task = autoretry_backoff_jitter_task
  112. @self.app.task(bind=True)
  113. def task_check_request_context(self):
  114. assert self.request.hostname == socket.gethostname()
  115. self.task_check_request_context = task_check_request_context
  116. @self.app.task(ignore_result=True)
  117. def task_with_ignored_result():
  118. pass
  119. self.task_with_ignored_result = task_with_ignored_result
  120. # Remove all messages from memory-transport
  121. from kombu.transport.memory import Channel
  122. Channel.queues.clear()
  123. class MyCustomException(Exception):
  124. """Random custom exception."""
  125. class test_task_retries(TasksCase):
  126. def test_retry(self):
  127. self.retry_task.max_retries = 3
  128. self.retry_task.iterations = 0
  129. self.retry_task.apply([0xFF, 0xFFFF])
  130. assert self.retry_task.iterations == 4
  131. self.retry_task.max_retries = 3
  132. self.retry_task.iterations = 0
  133. self.retry_task.apply([0xFF, 0xFFFF], {'max_retries': 10})
  134. assert self.retry_task.iterations == 11
  135. def test_retry_no_args(self):
  136. self.retry_task_noargs.max_retries = 3
  137. self.retry_task_noargs.iterations = 0
  138. self.retry_task_noargs.apply(propagate=True).get()
  139. assert self.retry_task_noargs.iterations == 4
  140. def test_signature_from_request__passes_headers(self):
  141. self.retry_task.push_request()
  142. self.retry_task.request.headers = {'custom': 10.1}
  143. sig = self.retry_task.signature_from_request()
  144. assert sig.options['headers']['custom'] == 10.1
  145. def test_signature_from_request__delivery_info(self):
  146. self.retry_task.push_request()
  147. self.retry_task.request.delivery_info = {
  148. 'exchange': 'testex',
  149. 'routing_key': 'testrk',
  150. }
  151. sig = self.retry_task.signature_from_request()
  152. assert sig.options['exchange'] == 'testex'
  153. assert sig.options['routing_key'] == 'testrk'
  154. def test_retry_kwargs_can_be_empty(self):
  155. self.retry_task_mockapply.push_request()
  156. try:
  157. with pytest.raises(Retry):
  158. import sys
  159. try:
  160. sys.exc_clear()
  161. except AttributeError:
  162. pass
  163. self.retry_task_mockapply.retry(args=[4, 4], kwargs=None)
  164. finally:
  165. self.retry_task_mockapply.pop_request()
  166. def test_retry_not_eager(self):
  167. self.retry_task_mockapply.push_request()
  168. try:
  169. self.retry_task_mockapply.request.called_directly = False
  170. exc = Exception('baz')
  171. try:
  172. self.retry_task_mockapply.retry(
  173. args=[4, 4], kwargs={'task_retries': 0},
  174. exc=exc, throw=False,
  175. )
  176. assert self.retry_task_mockapply.applied
  177. finally:
  178. self.retry_task_mockapply.applied = 0
  179. try:
  180. with pytest.raises(Retry):
  181. self.retry_task_mockapply.retry(
  182. args=[4, 4], kwargs={'task_retries': 0},
  183. exc=exc, throw=True)
  184. assert self.retry_task_mockapply.applied
  185. finally:
  186. self.retry_task_mockapply.applied = 0
  187. finally:
  188. self.retry_task_mockapply.pop_request()
  189. def test_retry_with_kwargs(self):
  190. self.retry_task_customexc.max_retries = 3
  191. self.retry_task_customexc.iterations = 0
  192. self.retry_task_customexc.apply([0xFF, 0xFFFF], {'kwarg': 0xF})
  193. assert self.retry_task_customexc.iterations == 4
  194. def test_retry_with_custom_exception(self):
  195. self.retry_task_customexc.max_retries = 2
  196. self.retry_task_customexc.iterations = 0
  197. result = self.retry_task_customexc.apply(
  198. [0xFF, 0xFFFF], {'kwarg': 0xF},
  199. )
  200. with pytest.raises(MyCustomException):
  201. result.get()
  202. assert self.retry_task_customexc.iterations == 3
  203. def test_max_retries_exceeded(self):
  204. self.retry_task.max_retries = 2
  205. self.retry_task.iterations = 0
  206. result = self.retry_task.apply([0xFF, 0xFFFF], {'care': False})
  207. with pytest.raises(self.retry_task.MaxRetriesExceededError):
  208. result.get()
  209. assert self.retry_task.iterations == 3
  210. self.retry_task.max_retries = 1
  211. self.retry_task.iterations = 0
  212. result = self.retry_task.apply([0xFF, 0xFFFF], {'care': False})
  213. with pytest.raises(self.retry_task.MaxRetriesExceededError):
  214. result.get()
  215. assert self.retry_task.iterations == 2
  216. def test_autoretry_no_kwargs(self):
  217. self.autoretry_task_no_kwargs.max_retries = 3
  218. self.autoretry_task_no_kwargs.iterations = 0
  219. self.autoretry_task_no_kwargs.apply((1, 0))
  220. assert self.autoretry_task_no_kwargs.iterations == 4
  221. def test_autoretry(self):
  222. self.autoretry_task.max_retries = 3
  223. self.autoretry_task.iterations = 0
  224. self.autoretry_task.apply((1, 0))
  225. assert self.autoretry_task.iterations == 6
  226. @patch('random.randrange', side_effect=lambda i: i - 1)
  227. def test_autoretry_backoff(self, randrange):
  228. task = self.autoretry_backoff_task
  229. task.max_retries = 3
  230. task.iterations = 0
  231. with patch.object(task, 'retry', wraps=task.retry) as fake_retry:
  232. task.apply(("http://httpbin.org/error",))
  233. assert task.iterations == 4
  234. retry_call_countdowns = [
  235. call[1]['countdown'] for call in fake_retry.call_args_list
  236. ]
  237. assert retry_call_countdowns == [1, 2, 4, 8]
  238. @patch('random.randrange', side_effect=lambda i: i - 2)
  239. def test_autoretry_backoff_jitter(self, randrange):
  240. task = self.autoretry_backoff_jitter_task
  241. task.max_retries = 3
  242. task.iterations = 0
  243. with patch.object(task, 'retry', wraps=task.retry) as fake_retry:
  244. task.apply(("http://httpbin.org/error",))
  245. assert task.iterations == 4
  246. retry_call_countdowns = [
  247. call[1]['countdown'] for call in fake_retry.call_args_list
  248. ]
  249. assert retry_call_countdowns == [0, 1, 3, 7]
  250. def test_retry_wrong_eta_when_not_enable_utc(self):
  251. """Issue #3753"""
  252. self.app.conf.enable_utc = False
  253. self.app.conf.timezone = 'US/Eastern'
  254. self.autoretry_task.iterations = 0
  255. self.autoretry_task.default_retry_delay = 2
  256. self.autoretry_task.apply((1, 0))
  257. assert self.autoretry_task.iterations == 6
  258. class test_canvas_utils(TasksCase):
  259. def test_si(self):
  260. assert self.retry_task.si()
  261. assert self.retry_task.si().immutable
  262. def test_chunks(self):
  263. assert self.retry_task.chunks(range(100), 10)
  264. def test_map(self):
  265. assert self.retry_task.map(range(100))
  266. def test_starmap(self):
  267. assert self.retry_task.starmap(range(100))
  268. def test_on_success(self):
  269. self.retry_task.on_success(1, 1, (), {})
  270. class test_tasks(TasksCase):
  271. def now(self):
  272. return self.app.now()
  273. def test_typing(self):
  274. @self.app.task()
  275. def add(x, y, kw=1):
  276. pass
  277. with pytest.raises(TypeError):
  278. add.delay(1)
  279. with pytest.raises(TypeError):
  280. add.delay(1, kw=2)
  281. with pytest.raises(TypeError):
  282. add.delay(1, 2, foobar=3)
  283. add.delay(2, 2)
  284. def test_shadow_name(self):
  285. def shadow_name(task, args, kwargs, options):
  286. return 'fooxyz'
  287. @self.app.task(shadow_name=shadow_name)
  288. def shadowed():
  289. pass
  290. old_send_task = self.app.send_task
  291. self.app.send_task = Mock()
  292. shadowed.delay()
  293. self.app.send_task.assert_called_once_with(ANY, ANY, ANY,
  294. compression=ANY,
  295. delivery_mode=ANY,
  296. exchange=ANY,
  297. expires=ANY,
  298. immediate=ANY,
  299. link=ANY,
  300. link_error=ANY,
  301. mandatory=ANY,
  302. priority=ANY,
  303. producer=ANY,
  304. queue=ANY,
  305. result_cls=ANY,
  306. routing_key=ANY,
  307. serializer=ANY,
  308. soft_time_limit=ANY,
  309. task_id=ANY,
  310. task_type=ANY,
  311. time_limit=ANY,
  312. shadow='fooxyz',
  313. ignore_result=False)
  314. self.app.send_task = old_send_task
  315. def test_shadow_name_old_task_class(self):
  316. def shadow_name(task, args, kwargs, options):
  317. return 'fooxyz'
  318. @self.app.task(base=OldTask, shadow_name=shadow_name)
  319. def shadowed():
  320. pass
  321. old_send_task = self.app.send_task
  322. self.app.send_task = Mock()
  323. shadowed.delay()
  324. self.app.send_task.assert_called_once_with(ANY, ANY, ANY,
  325. compression=ANY,
  326. delivery_mode=ANY,
  327. exchange=ANY,
  328. expires=ANY,
  329. immediate=ANY,
  330. link=ANY,
  331. link_error=ANY,
  332. mandatory=ANY,
  333. priority=ANY,
  334. producer=ANY,
  335. queue=ANY,
  336. result_cls=ANY,
  337. routing_key=ANY,
  338. serializer=ANY,
  339. soft_time_limit=ANY,
  340. task_id=ANY,
  341. task_type=ANY,
  342. time_limit=ANY,
  343. shadow='fooxyz',
  344. ignore_result=False)
  345. self.app.send_task = old_send_task
  346. def test_typing__disabled(self):
  347. @self.app.task(typing=False)
  348. def add(x, y, kw=1):
  349. pass
  350. add.delay(1)
  351. add.delay(1, kw=2)
  352. add.delay(1, 2, foobar=3)
  353. def test_typing__disabled_by_app(self):
  354. with self.Celery(set_as_current=False, strict_typing=False) as app:
  355. @app.task()
  356. def add(x, y, kw=1):
  357. pass
  358. assert not add.typing
  359. add.delay(1)
  360. add.delay(1, kw=2)
  361. add.delay(1, 2, foobar=3)
  362. @pytest.mark.usefixtures('depends_on_current_app')
  363. def test_unpickle_task(self):
  364. import pickle
  365. @self.app.task(shared=True)
  366. def xxx():
  367. pass
  368. assert pickle.loads(pickle.dumps(xxx)) is xxx.app.tasks[xxx.name]
  369. @patch('celery.app.task.current_app')
  370. @pytest.mark.usefixtures('depends_on_current_app')
  371. def test_bind__no_app(self, current_app):
  372. class XTask(Task):
  373. _app = None
  374. XTask._app = None
  375. XTask.__bound__ = False
  376. XTask.bind = Mock(name='bind')
  377. assert XTask.app is current_app
  378. XTask.bind.assert_called_with(current_app)
  379. def test_reprtask__no_fmt(self):
  380. assert _reprtask(self.mytask)
  381. def test_AsyncResult(self):
  382. task_id = uuid()
  383. result = self.retry_task.AsyncResult(task_id)
  384. assert result.backend == self.retry_task.backend
  385. assert result.id == task_id
  386. def assert_next_task_data_equal(self, consumer, presult, task_name,
  387. test_eta=False, test_expires=False,
  388. properties=None, headers=None, **kwargs):
  389. next_task = consumer.queues[0].get(accept=['pickle', 'json'])
  390. task_properties = next_task.properties
  391. task_headers = next_task.headers
  392. task_body = next_task.decode()
  393. task_args, task_kwargs, embed = task_body
  394. assert task_headers['id'] == presult.id
  395. assert task_headers['task'] == task_name
  396. if test_eta:
  397. assert isinstance(task_headers.get('eta'), string_t)
  398. to_datetime = parse_iso8601(task_headers.get('eta'))
  399. assert isinstance(to_datetime, datetime)
  400. if test_expires:
  401. assert isinstance(task_headers.get('expires'), string_t)
  402. to_datetime = parse_iso8601(task_headers.get('expires'))
  403. assert isinstance(to_datetime, datetime)
  404. properties = properties or {}
  405. for arg_name, arg_value in items(properties):
  406. assert task_properties.get(arg_name) == arg_value
  407. headers = headers or {}
  408. for arg_name, arg_value in items(headers):
  409. assert task_headers.get(arg_name) == arg_value
  410. for arg_name, arg_value in items(kwargs):
  411. assert task_kwargs.get(arg_name) == arg_value
  412. def test_incomplete_task_cls(self):
  413. class IncompleteTask(Task):
  414. app = self.app
  415. name = 'c.unittest.t.itask'
  416. with pytest.raises(NotImplementedError):
  417. IncompleteTask().run()
  418. def test_task_kwargs_must_be_dictionary(self):
  419. with pytest.raises(TypeError):
  420. self.increment_counter.apply_async([], 'str')
  421. def test_task_args_must_be_list(self):
  422. with pytest.raises(TypeError):
  423. self.increment_counter.apply_async('s', {})
  424. def test_regular_task(self):
  425. assert isinstance(self.mytask, Task)
  426. assert self.mytask.run()
  427. assert callable(self.mytask)
  428. assert self.mytask(), 'Task class runs run() when called'
  429. with self.app.connection_or_acquire() as conn:
  430. consumer = self.app.amqp.TaskConsumer(conn)
  431. with pytest.raises(NotImplementedError):
  432. consumer.receive('foo', 'foo')
  433. consumer.purge()
  434. assert consumer.queues[0].get() is None
  435. self.app.amqp.TaskConsumer(conn, queues=[Queue('foo')])
  436. # Without arguments.
  437. presult = self.mytask.delay()
  438. self.assert_next_task_data_equal(
  439. consumer, presult, self.mytask.name)
  440. # With arguments.
  441. presult2 = self.mytask.apply_async(
  442. kwargs={'name': 'George Costanza'},
  443. )
  444. self.assert_next_task_data_equal(
  445. consumer, presult2, self.mytask.name, name='George Costanza',
  446. )
  447. # send_task
  448. sresult = self.app.send_task(self.mytask.name,
  449. kwargs={'name': 'Elaine M. Benes'})
  450. self.assert_next_task_data_equal(
  451. consumer, sresult, self.mytask.name, name='Elaine M. Benes',
  452. )
  453. # With ETA.
  454. presult2 = self.mytask.apply_async(
  455. kwargs={'name': 'George Costanza'},
  456. eta=self.now() + timedelta(days=1),
  457. expires=self.now() + timedelta(days=2),
  458. )
  459. self.assert_next_task_data_equal(
  460. consumer, presult2, self.mytask.name,
  461. name='George Costanza', test_eta=True, test_expires=True,
  462. )
  463. # With countdown.
  464. presult2 = self.mytask.apply_async(
  465. kwargs={'name': 'George Costanza'}, countdown=10, expires=12,
  466. )
  467. self.assert_next_task_data_equal(
  468. consumer, presult2, self.mytask.name,
  469. name='George Costanza', test_eta=True, test_expires=True,
  470. )
  471. # Default argsrepr/kwargsrepr behavior
  472. presult2 = self.mytask.apply_async(
  473. args=('spam',), kwargs={'name': 'Jerry Seinfeld'}
  474. )
  475. self.assert_next_task_data_equal(
  476. consumer, presult2, self.mytask.name,
  477. headers={'argsrepr': "('spam',)",
  478. 'kwargsrepr': "{'name': 'Jerry Seinfeld'}"},
  479. )
  480. # With argsrepr/kwargsrepr
  481. presult2 = self.mytask.apply_async(
  482. args=('secret',), argsrepr="'***'",
  483. kwargs={'password': 'foo'}, kwargsrepr="{'password': '***'}",
  484. )
  485. self.assert_next_task_data_equal(
  486. consumer, presult2, self.mytask.name,
  487. headers={'argsrepr': "'***'",
  488. 'kwargsrepr': "{'password': '***'}"},
  489. )
  490. # Discarding all tasks.
  491. consumer.purge()
  492. self.mytask.apply_async()
  493. assert consumer.purge() == 1
  494. assert consumer.queues[0].get() is None
  495. assert not presult.successful()
  496. self.mytask.backend.mark_as_done(presult.id, result=None)
  497. assert presult.successful()
  498. def test_send_event(self):
  499. mytask = self.mytask._get_current_object()
  500. mytask.app.events = Mock(name='events')
  501. mytask.app.events.attach_mock(ContextMock(), 'default_dispatcher')
  502. mytask.request.id = 'fb'
  503. mytask.send_event('task-foo', id=3122)
  504. mytask.app.events.default_dispatcher().send.assert_called_with(
  505. 'task-foo', uuid='fb', id=3122,
  506. retry=True, retry_policy=self.app.conf.task_publish_retry_policy)
  507. def test_replace(self):
  508. sig1 = Mock(name='sig1')
  509. sig1.options = {}
  510. with pytest.raises(Ignore):
  511. self.mytask.replace(sig1)
  512. def test_replace_with_chord(self):
  513. sig1 = Mock(name='sig1')
  514. sig1.options = {'chord': None}
  515. with pytest.raises(ImproperlyConfigured):
  516. self.mytask.replace(sig1)
  517. @pytest.mark.usefixtures('depends_on_current_app')
  518. def test_replace_callback(self):
  519. c = group([self.mytask.s()], app=self.app)
  520. c.freeze = Mock(name='freeze')
  521. c.delay = Mock(name='delay')
  522. self.mytask.request.id = 'id'
  523. self.mytask.request.group = 'group'
  524. self.mytask.request.root_id = 'root_id'
  525. self.mytask.request.callbacks = 'callbacks'
  526. self.mytask.request.errbacks = 'errbacks'
  527. class JsonMagicMock(MagicMock):
  528. parent = None
  529. def __json__(self):
  530. return 'whatever'
  531. def reprcall(self, *args, **kwargs):
  532. return 'whatever2'
  533. mocked_signature = JsonMagicMock(name='s')
  534. accumulate_mock = JsonMagicMock(name='accumulate', s=mocked_signature)
  535. self.mytask.app.tasks['celery.accumulate'] = accumulate_mock
  536. try:
  537. self.mytask.replace(c)
  538. except Ignore:
  539. mocked_signature.return_value.set.assert_called_with(
  540. link='callbacks',
  541. link_error='errbacks',
  542. )
  543. def test_replace_group(self):
  544. c = group([self.mytask.s()], app=self.app)
  545. c.freeze = Mock(name='freeze')
  546. c.delay = Mock(name='delay')
  547. self.mytask.request.id = 'id'
  548. self.mytask.request.group = 'group'
  549. self.mytask.request.root_id = 'root_id',
  550. with pytest.raises(Ignore):
  551. self.mytask.replace(c)
  552. def test_add_trail__no_trail(self):
  553. mytask = self.increment_counter._get_current_object()
  554. mytask.trail = False
  555. mytask.add_trail('foo')
  556. def test_repr_v2_compat(self):
  557. self.mytask.__v2_compat__ = True
  558. assert 'v2 compatible' in repr(self.mytask)
  559. def test_context_get(self):
  560. self.mytask.push_request()
  561. try:
  562. request = self.mytask.request
  563. request.foo = 32
  564. assert request.get('foo') == 32
  565. assert request.get('bar', 36) == 36
  566. request.clear()
  567. finally:
  568. self.mytask.pop_request()
  569. def test_annotate(self):
  570. with patch('celery.app.task.resolve_all_annotations') as anno:
  571. anno.return_value = [{'FOO': 'BAR'}]
  572. @self.app.task(shared=False)
  573. def task():
  574. pass
  575. task.annotate()
  576. assert task.FOO == 'BAR'
  577. def test_after_return(self):
  578. self.mytask.push_request()
  579. try:
  580. self.mytask.request.chord = self.mytask.s()
  581. self.mytask.after_return('SUCCESS', 1.0, 'foobar', (), {}, None)
  582. self.mytask.request.clear()
  583. finally:
  584. self.mytask.pop_request()
  585. def test_update_state(self):
  586. @self.app.task(shared=False)
  587. def yyy():
  588. pass
  589. yyy.push_request()
  590. try:
  591. tid = uuid()
  592. yyy.update_state(tid, 'FROBULATING', {'fooz': 'baaz'})
  593. assert yyy.AsyncResult(tid).status == 'FROBULATING'
  594. assert yyy.AsyncResult(tid).result == {'fooz': 'baaz'}
  595. yyy.request.id = tid
  596. yyy.update_state(state='FROBUZATING', meta={'fooz': 'baaz'})
  597. assert yyy.AsyncResult(tid).status == 'FROBUZATING'
  598. assert yyy.AsyncResult(tid).result == {'fooz': 'baaz'}
  599. finally:
  600. yyy.pop_request()
  601. def test_repr(self):
  602. @self.app.task(shared=False)
  603. def task_test_repr():
  604. pass
  605. assert 'task_test_repr' in repr(task_test_repr)
  606. def test_has___name__(self):
  607. @self.app.task(shared=False)
  608. def yyy2():
  609. pass
  610. assert yyy2.__name__
  611. class test_apply_task(TasksCase):
  612. def test_apply_throw(self):
  613. with pytest.raises(KeyError):
  614. self.raising.apply(throw=True)
  615. def test_apply_with_task_eager_propagates(self):
  616. self.app.conf.task_eager_propagates = True
  617. with pytest.raises(KeyError):
  618. self.raising.apply()
  619. def test_apply_request_context_is_ok(self):
  620. self.app.conf.task_eager_propagates = True
  621. self.task_check_request_context.apply()
  622. def test_apply(self):
  623. self.increment_counter.count = 0
  624. e = self.increment_counter.apply()
  625. assert isinstance(e, EagerResult)
  626. assert e.get() == 1
  627. e = self.increment_counter.apply(args=[1])
  628. assert e.get() == 2
  629. e = self.increment_counter.apply(kwargs={'increment_by': 4})
  630. assert e.get() == 6
  631. assert e.successful()
  632. assert e.ready()
  633. assert repr(e).startswith('<EagerResult:')
  634. f = self.raising.apply()
  635. assert f.ready()
  636. assert not f.successful()
  637. assert f.traceback
  638. with pytest.raises(KeyError):
  639. f.get()
  640. class test_apply_async(TasksCase):
  641. def common_send_task_arguments(self):
  642. return (ANY, ANY, ANY), dict(
  643. compression=ANY,
  644. delivery_mode=ANY,
  645. exchange=ANY,
  646. expires=ANY,
  647. immediate=ANY,
  648. link=ANY,
  649. link_error=ANY,
  650. mandatory=ANY,
  651. priority=ANY,
  652. producer=ANY,
  653. queue=ANY,
  654. result_cls=ANY,
  655. routing_key=ANY,
  656. serializer=ANY,
  657. soft_time_limit=ANY,
  658. task_id=ANY,
  659. task_type=ANY,
  660. time_limit=ANY,
  661. shadow=None,
  662. ignore_result=False
  663. )
  664. def test_task_with_ignored_result(self):
  665. with patch.object(self.app, 'send_task') as send_task:
  666. self.task_with_ignored_result.apply_async()
  667. expected_args, expected_kwargs = self.common_send_task_arguments()
  668. expected_kwargs['ignore_result'] = True
  669. send_task.assert_called_once_with(
  670. *expected_args,
  671. **expected_kwargs
  672. )
  673. def test_task_with_result(self):
  674. with patch.object(self.app, 'send_task') as send_task:
  675. self.mytask.apply_async()
  676. expected_args, expected_kwargs = self.common_send_task_arguments()
  677. send_task.assert_called_once_with(
  678. *expected_args,
  679. **expected_kwargs
  680. )
  681. def test_task_with_result_ignoring_on_call(self):
  682. with patch.object(self.app, 'send_task') as send_task:
  683. self.mytask.apply_async(ignore_result=True)
  684. expected_args, expected_kwargs = self.common_send_task_arguments()
  685. expected_kwargs['ignore_result'] = True
  686. send_task.assert_called_once_with(
  687. *expected_args,
  688. **expected_kwargs
  689. )