test_result.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721
  1. from __future__ import absolute_import
  2. from contextlib import contextmanager
  3. from celery import states
  4. from celery.exceptions import IncompleteStream, TimeoutError
  5. from celery.five import range
  6. from celery.result import (
  7. AsyncResult,
  8. EagerResult,
  9. TaskSetResult,
  10. result_from_tuple,
  11. )
  12. from celery.utils import uuid
  13. from celery.utils.serialization import pickle
  14. from celery.tests.case import AppCase, Mock, depends_on_current_app, patch
  15. def mock_task(name, state, result):
  16. return dict(id=uuid(), name=name, state=state, result=result)
  17. def save_result(app, task):
  18. traceback = 'Some traceback'
  19. if task['state'] == states.SUCCESS:
  20. app.backend.mark_as_done(task['id'], task['result'])
  21. elif task['state'] == states.RETRY:
  22. app.backend.mark_as_retry(
  23. task['id'], task['result'], traceback=traceback,
  24. )
  25. else:
  26. app.backend.mark_as_failure(
  27. task['id'], task['result'], traceback=traceback,
  28. )
  29. def make_mock_group(app, size=10):
  30. tasks = [mock_task('ts%d' % i, states.SUCCESS, i) for i in range(size)]
  31. [save_result(app, task) for task in tasks]
  32. return [app.AsyncResult(task['id']) for task in tasks]
  33. class test_AsyncResult(AppCase):
  34. def setup(self):
  35. self.task1 = mock_task('task1', states.SUCCESS, 'the')
  36. self.task2 = mock_task('task2', states.SUCCESS, 'quick')
  37. self.task3 = mock_task('task3', states.FAILURE, KeyError('brown'))
  38. self.task4 = mock_task('task3', states.RETRY, KeyError('red'))
  39. for task in (self.task1, self.task2, self.task3, self.task4):
  40. save_result(self.app, task)
  41. @self.app.task(shared=False)
  42. def mytask():
  43. pass
  44. self.mytask = mytask
  45. def test_compat_properties(self):
  46. x = self.app.AsyncResult('1')
  47. self.assertEqual(x.task_id, x.id)
  48. x.task_id = '2'
  49. self.assertEqual(x.id, '2')
  50. def test_children(self):
  51. x = self.app.AsyncResult('1')
  52. children = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  53. x._cache = {'children': children, 'status': states.SUCCESS}
  54. x.backend = Mock()
  55. self.assertTrue(x.children)
  56. self.assertEqual(len(x.children), 3)
  57. def test_propagates_for_parent(self):
  58. x = self.app.AsyncResult(uuid())
  59. x.backend = Mock()
  60. x.backend.get_task_meta.return_value = {}
  61. x.parent = EagerResult(uuid(), KeyError('foo'), states.FAILURE)
  62. with self.assertRaises(KeyError):
  63. x.get(propagate=True)
  64. self.assertFalse(x.backend.wait_for.called)
  65. x.parent = EagerResult(uuid(), 42, states.SUCCESS)
  66. x.get(propagate=True)
  67. self.assertTrue(x.backend.wait_for.called)
  68. def test_get_children(self):
  69. tid = uuid()
  70. x = self.app.AsyncResult(tid)
  71. child = [self.app.AsyncResult(uuid()).as_tuple()
  72. for i in range(10)]
  73. x._cache = {'children': child}
  74. self.assertTrue(x.children)
  75. self.assertEqual(len(x.children), 10)
  76. x._cache = {'status': states.SUCCESS}
  77. x.backend._cache[tid] = {'result': None}
  78. self.assertIsNone(x.children)
  79. def test_build_graph_get_leaf_collect(self):
  80. x = self.app.AsyncResult('1')
  81. x.backend._cache['1'] = {'status': states.SUCCESS, 'result': None}
  82. c = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  83. x.iterdeps = Mock()
  84. x.iterdeps.return_value = (
  85. (None, x),
  86. (x, c[0]),
  87. (c[0], c[1]),
  88. (c[1], c[2])
  89. )
  90. x.backend.READY_STATES = states.READY_STATES
  91. self.assertTrue(x.graph)
  92. self.assertIs(x.get_leaf(), 2)
  93. it = x.collect()
  94. self.assertListEqual(list(it), [
  95. (x, None),
  96. (c[0], 0),
  97. (c[1], 1),
  98. (c[2], 2),
  99. ])
  100. def test_iterdeps(self):
  101. x = self.app.AsyncResult('1')
  102. c = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  103. x._cache = {'status': states.SUCCESS, 'result': None, 'children': c}
  104. for child in c:
  105. child.backend = Mock()
  106. child.backend.get_children.return_value = []
  107. it = x.iterdeps()
  108. self.assertListEqual(list(it), [
  109. (None, x),
  110. (x, c[0]),
  111. (x, c[1]),
  112. (x, c[2]),
  113. ])
  114. x._cache = None
  115. x.ready = Mock()
  116. x.ready.return_value = False
  117. with self.assertRaises(IncompleteStream):
  118. list(x.iterdeps())
  119. list(x.iterdeps(intermediate=True))
  120. def test_eq_not_implemented(self):
  121. self.assertFalse(self.app.AsyncResult('1') == object())
  122. @depends_on_current_app
  123. def test_reduce(self):
  124. a1 = self.app.AsyncResult('uuid', task_name=self.mytask.name)
  125. restored = pickle.loads(pickle.dumps(a1))
  126. self.assertEqual(restored.id, 'uuid')
  127. self.assertEqual(restored.task_name, self.mytask.name)
  128. a2 = self.app.AsyncResult('uuid')
  129. self.assertEqual(pickle.loads(pickle.dumps(a2)).id, 'uuid')
  130. def test_successful(self):
  131. ok_res = self.app.AsyncResult(self.task1['id'])
  132. nok_res = self.app.AsyncResult(self.task3['id'])
  133. nok_res2 = self.app.AsyncResult(self.task4['id'])
  134. self.assertTrue(ok_res.successful())
  135. self.assertFalse(nok_res.successful())
  136. self.assertFalse(nok_res2.successful())
  137. pending_res = self.app.AsyncResult(uuid())
  138. self.assertFalse(pending_res.successful())
  139. def test_str(self):
  140. ok_res = self.app.AsyncResult(self.task1['id'])
  141. ok2_res = self.app.AsyncResult(self.task2['id'])
  142. nok_res = self.app.AsyncResult(self.task3['id'])
  143. self.assertEqual(str(ok_res), self.task1['id'])
  144. self.assertEqual(str(ok2_res), self.task2['id'])
  145. self.assertEqual(str(nok_res), self.task3['id'])
  146. pending_id = uuid()
  147. pending_res = self.app.AsyncResult(pending_id)
  148. self.assertEqual(str(pending_res), pending_id)
  149. def test_repr(self):
  150. ok_res = self.app.AsyncResult(self.task1['id'])
  151. ok2_res = self.app.AsyncResult(self.task2['id'])
  152. nok_res = self.app.AsyncResult(self.task3['id'])
  153. self.assertEqual(repr(ok_res), '<AsyncResult: %s>' % (
  154. self.task1['id']))
  155. self.assertEqual(repr(ok2_res), '<AsyncResult: %s>' % (
  156. self.task2['id']))
  157. self.assertEqual(repr(nok_res), '<AsyncResult: %s>' % (
  158. self.task3['id']))
  159. pending_id = uuid()
  160. pending_res = self.app.AsyncResult(pending_id)
  161. self.assertEqual(repr(pending_res), '<AsyncResult: %s>' % (
  162. pending_id))
  163. def test_hash(self):
  164. self.assertEqual(hash(self.app.AsyncResult('x0w991')),
  165. hash(self.app.AsyncResult('x0w991')))
  166. self.assertNotEqual(hash(self.app.AsyncResult('x0w991')),
  167. hash(self.app.AsyncResult('x1w991')))
  168. def test_get_traceback(self):
  169. ok_res = self.app.AsyncResult(self.task1['id'])
  170. nok_res = self.app.AsyncResult(self.task3['id'])
  171. nok_res2 = self.app.AsyncResult(self.task4['id'])
  172. self.assertFalse(ok_res.traceback)
  173. self.assertTrue(nok_res.traceback)
  174. self.assertTrue(nok_res2.traceback)
  175. pending_res = self.app.AsyncResult(uuid())
  176. self.assertFalse(pending_res.traceback)
  177. def test_get(self):
  178. ok_res = self.app.AsyncResult(self.task1['id'])
  179. ok2_res = self.app.AsyncResult(self.task2['id'])
  180. nok_res = self.app.AsyncResult(self.task3['id'])
  181. nok2_res = self.app.AsyncResult(self.task4['id'])
  182. self.assertEqual(ok_res.get(), 'the')
  183. self.assertEqual(ok2_res.get(), 'quick')
  184. with self.assertRaises(KeyError):
  185. nok_res.get()
  186. self.assertTrue(nok_res.get(propagate=False))
  187. self.assertIsInstance(nok2_res.result, KeyError)
  188. self.assertEqual(ok_res.info, 'the')
  189. def test_get_timeout(self):
  190. res = self.app.AsyncResult(self.task4['id']) # has RETRY state
  191. with self.assertRaises(TimeoutError):
  192. res.get(timeout=0.001)
  193. pending_res = self.app.AsyncResult(uuid())
  194. with patch('celery.result.time') as _time:
  195. with self.assertRaises(TimeoutError):
  196. pending_res.get(timeout=0.001, interval=0.001)
  197. _time.sleep.assert_called_with(0.001)
  198. def test_get_timeout_longer(self):
  199. res = self.app.AsyncResult(self.task4['id']) # has RETRY state
  200. with patch('celery.result.time') as _time:
  201. with self.assertRaises(TimeoutError):
  202. res.get(timeout=1, interval=1)
  203. _time.sleep.assert_called_with(1)
  204. def test_ready(self):
  205. oks = (self.app.AsyncResult(self.task1['id']),
  206. self.app.AsyncResult(self.task2['id']),
  207. self.app.AsyncResult(self.task3['id']))
  208. self.assertTrue(all(result.ready() for result in oks))
  209. self.assertFalse(self.app.AsyncResult(self.task4['id']).ready())
  210. self.assertFalse(self.app.AsyncResult(uuid()).ready())
  211. class test_ResultSet(AppCase):
  212. def test_resultset_repr(self):
  213. self.assertTrue(repr(self.app.ResultSet(
  214. [self.app.AsyncResult(t) for t in ['1', '2', '3']])))
  215. def test_eq_other(self):
  216. self.assertFalse(self.app.ResultSet([1, 3, 3]) == 1)
  217. self.assertTrue(self.app.ResultSet([1]) == self.app.ResultSet([1]))
  218. def test_get(self):
  219. x = self.app.ResultSet([self.app.AsyncResult(t) for t in [1, 2, 3]])
  220. b = x.results[0].backend = Mock()
  221. b.supports_native_join = False
  222. x.join_native = Mock()
  223. x.join = Mock()
  224. x.get()
  225. self.assertTrue(x.join.called)
  226. b.supports_native_join = True
  227. x.get()
  228. self.assertTrue(x.join_native.called)
  229. def test_add(self):
  230. x = self.app.ResultSet([1])
  231. x.add(2)
  232. self.assertEqual(len(x), 2)
  233. x.add(2)
  234. self.assertEqual(len(x), 2)
  235. @contextmanager
  236. def dummy_copy(self):
  237. with patch('celery.result.copy') as copy:
  238. def passt(arg):
  239. return arg
  240. copy.side_effect = passt
  241. yield
  242. def test_iterate_respects_subpolling_interval(self):
  243. r1 = self.app.AsyncResult(uuid())
  244. r2 = self.app.AsyncResult(uuid())
  245. backend = r1.backend = r2.backend = Mock()
  246. backend.subpolling_interval = 10
  247. ready = r1.ready = r2.ready = Mock()
  248. def se(*args, **kwargs):
  249. ready.side_effect = KeyError()
  250. return False
  251. ready.return_value = False
  252. ready.side_effect = se
  253. x = self.app.ResultSet([r1, r2])
  254. with self.dummy_copy():
  255. with patch('celery.result.time') as _time:
  256. with self.assertPendingDeprecation():
  257. with self.assertRaises(KeyError):
  258. list(x.iterate())
  259. _time.sleep.assert_called_with(10)
  260. backend.subpolling_interval = 0
  261. with patch('celery.result.time') as _time:
  262. with self.assertPendingDeprecation():
  263. with self.assertRaises(KeyError):
  264. ready.return_value = False
  265. ready.side_effect = se
  266. list(x.iterate())
  267. self.assertFalse(_time.sleep.called)
  268. def test_times_out(self):
  269. r1 = self.app.AsyncResult(uuid)
  270. r1.ready = Mock()
  271. r1.ready.return_value = False
  272. x = self.app.ResultSet([r1])
  273. with self.dummy_copy():
  274. with patch('celery.result.time'):
  275. with self.assertPendingDeprecation():
  276. with self.assertRaises(TimeoutError):
  277. list(x.iterate(timeout=1))
  278. def test_add_discard(self):
  279. x = self.app.ResultSet([])
  280. x.add(self.app.AsyncResult('1'))
  281. self.assertIn(self.app.AsyncResult('1'), x.results)
  282. x.discard(self.app.AsyncResult('1'))
  283. x.discard(self.app.AsyncResult('1'))
  284. x.discard('1')
  285. self.assertNotIn(self.app.AsyncResult('1'), x.results)
  286. x.update([self.app.AsyncResult('2')])
  287. def test_clear(self):
  288. x = self.app.ResultSet([])
  289. r = x.results
  290. x.clear()
  291. self.assertIs(x.results, r)
  292. class MockAsyncResultFailure(AsyncResult):
  293. @property
  294. def result(self):
  295. return KeyError('baz')
  296. @property
  297. def state(self):
  298. return states.FAILURE
  299. def get(self, propagate=True, **kwargs):
  300. if propagate:
  301. raise self.result
  302. return self.result
  303. class MockAsyncResultSuccess(AsyncResult):
  304. forgotten = False
  305. def forget(self):
  306. self.forgotten = True
  307. @property
  308. def result(self):
  309. return 42
  310. @property
  311. def state(self):
  312. return states.SUCCESS
  313. def get(self, **kwargs):
  314. return self.result
  315. class SimpleBackend(object):
  316. ids = []
  317. def __init__(self, ids=[]):
  318. self.ids = ids
  319. def get_many(self, *args, **kwargs):
  320. return ((id, {'result': i, 'status': states.SUCCESS})
  321. for i, id in enumerate(self.ids))
  322. class test_TaskSetResult(AppCase):
  323. def setup(self):
  324. self.size = 10
  325. self.ts = TaskSetResult(uuid(), make_mock_group(self.app, self.size))
  326. def test_total(self):
  327. self.assertEqual(self.ts.total, self.size)
  328. def test_compat_properties(self):
  329. self.assertEqual(self.ts.taskset_id, self.ts.id)
  330. self.ts.taskset_id = 'foo'
  331. self.assertEqual(self.ts.taskset_id, 'foo')
  332. def test_compat_subtasks_kwarg(self):
  333. x = TaskSetResult(uuid(), subtasks=[1, 2, 3])
  334. self.assertEqual(x.results, [1, 2, 3])
  335. def test_itersubtasks(self):
  336. it = self.ts.itersubtasks()
  337. for i, t in enumerate(it):
  338. self.assertEqual(t.get(), i)
  339. class test_GroupResult(AppCase):
  340. def setup(self):
  341. self.size = 10
  342. self.ts = self.app.GroupResult(
  343. uuid(), make_mock_group(self.app, self.size),
  344. )
  345. @depends_on_current_app
  346. def test_is_pickleable(self):
  347. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  348. self.assertEqual(pickle.loads(pickle.dumps(ts)), ts)
  349. ts2 = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  350. self.assertEqual(pickle.loads(pickle.dumps(ts2)), ts2)
  351. def test_len(self):
  352. self.assertEqual(len(self.ts), self.size)
  353. def test_eq_other(self):
  354. self.assertFalse(self.ts == 1)
  355. @depends_on_current_app
  356. def test_reduce(self):
  357. self.assertTrue(pickle.loads(pickle.dumps(self.ts)))
  358. def test_iterate_raises(self):
  359. ar = MockAsyncResultFailure(uuid(), app=self.app)
  360. ts = self.app.GroupResult(uuid(), [ar])
  361. with self.assertPendingDeprecation():
  362. it = ts.iterate()
  363. with self.assertRaises(KeyError):
  364. next(it)
  365. def test_forget(self):
  366. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  367. MockAsyncResultSuccess(uuid(), app=self.app)]
  368. ts = self.app.GroupResult(uuid(), subs)
  369. ts.forget()
  370. for sub in subs:
  371. self.assertTrue(sub.forgotten)
  372. def test_getitem(self):
  373. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  374. MockAsyncResultSuccess(uuid(), app=self.app)]
  375. ts = self.app.GroupResult(uuid(), subs)
  376. self.assertIs(ts[0], subs[0])
  377. def test_save_restore(self):
  378. subs = [MockAsyncResultSuccess(uuid(), app=self.app),
  379. MockAsyncResultSuccess(uuid(), app=self.app)]
  380. ts = self.app.GroupResult(uuid(), subs)
  381. ts.save()
  382. with self.assertRaises(AttributeError):
  383. ts.save(backend=object())
  384. self.assertEqual(self.app.GroupResult.restore(ts.id).subtasks,
  385. ts.subtasks)
  386. ts.delete()
  387. self.assertIsNone(self.app.GroupResult.restore(ts.id))
  388. with self.assertRaises(AttributeError):
  389. self.app.GroupResult.restore(ts.id, backend=object())
  390. def test_join_native(self):
  391. backend = SimpleBackend()
  392. subtasks = [self.app.AsyncResult(uuid(), backend=backend)
  393. for i in range(10)]
  394. ts = self.app.GroupResult(uuid(), subtasks)
  395. ts.app.backend = backend
  396. backend.ids = [subtask.id for subtask in subtasks]
  397. res = ts.join_native()
  398. self.assertEqual(res, list(range(10)))
  399. def test_join_native_raises(self):
  400. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  401. ts.iter_native = Mock()
  402. ts.iter_native.return_value = iter([
  403. (uuid(), {'status': states.FAILURE, 'result': KeyError()})
  404. ])
  405. with self.assertRaises(KeyError):
  406. ts.join_native(propagate=True)
  407. def test_failed_join_report(self):
  408. res = Mock()
  409. ts = self.app.GroupResult(uuid(), [res])
  410. res.state = states.FAILURE
  411. res.backend.is_cached.return_value = True
  412. self.assertIs(next(ts._failed_join_report()), res)
  413. res.backend.is_cached.return_value = False
  414. with self.assertRaises(StopIteration):
  415. next(ts._failed_join_report())
  416. def test_repr(self):
  417. self.assertTrue(repr(
  418. self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  419. ))
  420. def test_children_is_results(self):
  421. ts = self.app.GroupResult(uuid(), [self.app.AsyncResult(uuid())])
  422. self.assertIs(ts.children, ts.results)
  423. def test_iter_native(self):
  424. backend = SimpleBackend()
  425. subtasks = [self.app.AsyncResult(uuid(), backend=backend)
  426. for i in range(10)]
  427. ts = self.app.GroupResult(uuid(), subtasks)
  428. ts.app.backend = backend
  429. backend.ids = [subtask.id for subtask in subtasks]
  430. self.assertEqual(len(list(ts.iter_native())), 10)
  431. def test_iterate_yields(self):
  432. ar = MockAsyncResultSuccess(uuid(), app=self.app)
  433. ar2 = MockAsyncResultSuccess(uuid(), app=self.app)
  434. ts = self.app.GroupResult(uuid(), [ar, ar2])
  435. with self.assertPendingDeprecation():
  436. it = ts.iterate()
  437. self.assertEqual(next(it), 42)
  438. self.assertEqual(next(it), 42)
  439. def test_iterate_eager(self):
  440. ar1 = EagerResult(uuid(), 42, states.SUCCESS)
  441. ar2 = EagerResult(uuid(), 42, states.SUCCESS)
  442. ts = self.app.GroupResult(uuid(), [ar1, ar2])
  443. with self.assertPendingDeprecation():
  444. it = ts.iterate()
  445. self.assertEqual(next(it), 42)
  446. self.assertEqual(next(it), 42)
  447. def test_join_timeout(self):
  448. ar = MockAsyncResultSuccess(uuid(), app=self.app)
  449. ar2 = MockAsyncResultSuccess(uuid(), app=self.app)
  450. ar3 = self.app.AsyncResult(uuid())
  451. ts = self.app.GroupResult(uuid(), [ar, ar2, ar3])
  452. with self.assertRaises(TimeoutError):
  453. ts.join(timeout=0.0000001)
  454. ar4 = self.app.AsyncResult(uuid())
  455. ar4.get = Mock()
  456. ts2 = self.app.GroupResult(uuid(), [ar4])
  457. self.assertTrue(ts2.join(timeout=0.1))
  458. def test_iter_native_when_empty_group(self):
  459. ts = self.app.GroupResult(uuid(), [])
  460. self.assertListEqual(list(ts.iter_native()), [])
  461. def test_iterate_simple(self):
  462. with self.assertPendingDeprecation():
  463. it = self.ts.iterate()
  464. results = sorted(list(it))
  465. self.assertListEqual(results, list(range(self.size)))
  466. def test___iter__(self):
  467. self.assertListEqual(list(iter(self.ts)), self.ts.results)
  468. def test_join(self):
  469. joined = self.ts.join()
  470. self.assertListEqual(joined, list(range(self.size)))
  471. def test_successful(self):
  472. self.assertTrue(self.ts.successful())
  473. def test_failed(self):
  474. self.assertFalse(self.ts.failed())
  475. def test_waiting(self):
  476. self.assertFalse(self.ts.waiting())
  477. def test_ready(self):
  478. self.assertTrue(self.ts.ready())
  479. def test_completed_count(self):
  480. self.assertEqual(self.ts.completed_count(), len(self.ts))
  481. class test_pending_AsyncResult(AppCase):
  482. def setup(self):
  483. self.task = self.app.AsyncResult(uuid())
  484. def test_result(self):
  485. self.assertIsNone(self.task.result)
  486. class test_failed_AsyncResult(test_GroupResult):
  487. def setup(self):
  488. self.size = 11
  489. subtasks = make_mock_group(self.app, 10)
  490. failed = mock_task('ts11', states.FAILURE, KeyError('Baz'))
  491. save_result(self.app, failed)
  492. failed_res = self.app.AsyncResult(failed['id'])
  493. self.ts = self.app.GroupResult(uuid(), subtasks + [failed_res])
  494. def test_completed_count(self):
  495. self.assertEqual(self.ts.completed_count(), len(self.ts) - 1)
  496. def test_iterate_simple(self):
  497. with self.assertPendingDeprecation():
  498. it = self.ts.iterate()
  499. def consume():
  500. return list(it)
  501. with self.assertRaises(KeyError):
  502. consume()
  503. def test_join(self):
  504. with self.assertRaises(KeyError):
  505. self.ts.join()
  506. def test_successful(self):
  507. self.assertFalse(self.ts.successful())
  508. def test_failed(self):
  509. self.assertTrue(self.ts.failed())
  510. class test_pending_Group(AppCase):
  511. def setup(self):
  512. self.ts = self.app.GroupResult(
  513. uuid(), [self.app.AsyncResult(uuid()),
  514. self.app.AsyncResult(uuid())])
  515. def test_completed_count(self):
  516. self.assertEqual(self.ts.completed_count(), 0)
  517. def test_ready(self):
  518. self.assertFalse(self.ts.ready())
  519. def test_waiting(self):
  520. self.assertTrue(self.ts.waiting())
  521. def x_join(self):
  522. with self.assertRaises(TimeoutError):
  523. self.ts.join(timeout=0.001)
  524. def x_join_longer(self):
  525. with self.assertRaises(TimeoutError):
  526. self.ts.join(timeout=1)
  527. class test_EagerResult(AppCase):
  528. def setup(self):
  529. @self.app.task(shared=False)
  530. def raising(x, y):
  531. raise KeyError(x, y)
  532. self.raising = raising
  533. def test_wait_raises(self):
  534. res = self.raising.apply(args=[3, 3])
  535. with self.assertRaises(KeyError):
  536. res.wait()
  537. self.assertTrue(res.wait(propagate=False))
  538. def test_wait(self):
  539. res = EagerResult('x', 'x', states.RETRY)
  540. res.wait()
  541. self.assertEqual(res.state, states.RETRY)
  542. self.assertEqual(res.status, states.RETRY)
  543. def test_forget(self):
  544. res = EagerResult('x', 'x', states.RETRY)
  545. res.forget()
  546. def test_revoke(self):
  547. res = self.raising.apply(args=[3, 3])
  548. self.assertFalse(res.revoke())
  549. class test_tuples(AppCase):
  550. def test_AsyncResult(self):
  551. x = self.app.AsyncResult(uuid())
  552. self.assertEqual(x, result_from_tuple(x.as_tuple(), self.app))
  553. self.assertEqual(x, result_from_tuple(x, self.app))
  554. def test_with_parent(self):
  555. x = self.app.AsyncResult(uuid())
  556. x.parent = self.app.AsyncResult(uuid())
  557. y = result_from_tuple(x.as_tuple(), self.app)
  558. self.assertEqual(y, x)
  559. self.assertEqual(y.parent, x.parent)
  560. self.assertIsInstance(y.parent, AsyncResult)
  561. def test_compat(self):
  562. uid = uuid()
  563. x = result_from_tuple([uid, []], app=self.app)
  564. self.assertEqual(x.id, uid)
  565. def test_GroupResult(self):
  566. x = self.app.GroupResult(
  567. uuid(), [self.app.AsyncResult(uuid()) for _ in range(10)],
  568. )
  569. self.assertEqual(x, result_from_tuple(x.as_tuple(), self.app))
  570. self.assertEqual(x, result_from_tuple(x, self.app))