test_result.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716
  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. result_from_tuple,
  10. assert_will_not_block,
  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.app.conf.result_serializer = 'pickle'
  36. self.task1 = mock_task('task1', states.SUCCESS, 'the')
  37. self.task2 = mock_task('task2', states.SUCCESS, 'quick')
  38. self.task3 = mock_task('task3', states.FAILURE, KeyError('brown'))
  39. self.task4 = mock_task('task3', states.RETRY, KeyError('red'))
  40. for task in (self.task1, self.task2, self.task3, self.task4):
  41. save_result(self.app, task)
  42. @self.app.task(shared=False)
  43. def mytask():
  44. pass
  45. self.mytask = mytask
  46. @patch('celery.result.task_join_will_block')
  47. def test_assert_will_not_block(self, task_join_will_block):
  48. task_join_will_block.return_value = True
  49. with self.assertRaises(RuntimeError):
  50. assert_will_not_block()
  51. task_join_will_block.return_value = False
  52. assert_will_not_block()
  53. def test_compat_properties(self):
  54. x = self.app.AsyncResult('1')
  55. self.assertEqual(x.task_id, x.id)
  56. x.task_id = '2'
  57. self.assertEqual(x.id, '2')
  58. def test_children(self):
  59. x = self.app.AsyncResult('1')
  60. children = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  61. x._cache = {'children': children, 'status': states.SUCCESS}
  62. x.backend = Mock()
  63. self.assertTrue(x.children)
  64. self.assertEqual(len(x.children), 3)
  65. def test_propagates_for_parent(self):
  66. x = self.app.AsyncResult(uuid())
  67. x.backend = Mock(name='backend')
  68. x.backend.get_task_meta.return_value = {}
  69. x.backend.wait_for.return_value = {
  70. 'status': states.SUCCESS, 'result': 84,
  71. }
  72. x.parent = EagerResult(uuid(), KeyError('foo'), states.FAILURE)
  73. with self.assertRaises(KeyError):
  74. x.get(propagate=True)
  75. self.assertFalse(x.backend.wait_for.called)
  76. x.parent = EagerResult(uuid(), 42, states.SUCCESS)
  77. self.assertEqual(x.get(propagate=True), 84)
  78. self.assertTrue(x.backend.wait_for.called)
  79. def test_get_children(self):
  80. tid = uuid()
  81. x = self.app.AsyncResult(tid)
  82. child = [self.app.AsyncResult(uuid()).as_tuple()
  83. for i in range(10)]
  84. x._cache = {'children': child}
  85. self.assertTrue(x.children)
  86. self.assertEqual(len(x.children), 10)
  87. x._cache = {'status': states.SUCCESS}
  88. x.backend._cache[tid] = {'result': None}
  89. self.assertIsNone(x.children)
  90. def test_build_graph_get_leaf_collect(self):
  91. x = self.app.AsyncResult('1')
  92. x.backend._cache['1'] = {'status': states.SUCCESS, 'result': None}
  93. c = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  94. x.iterdeps = Mock()
  95. x.iterdeps.return_value = (
  96. (None, x),
  97. (x, c[0]),
  98. (c[0], c[1]),
  99. (c[1], c[2])
  100. )
  101. x.backend.READY_STATES = states.READY_STATES
  102. self.assertTrue(x.graph)
  103. self.assertIs(x.get_leaf(), 2)
  104. it = x.collect()
  105. self.assertListEqual(list(it), [
  106. (x, None),
  107. (c[0], 0),
  108. (c[1], 1),
  109. (c[2], 2),
  110. ])
  111. def test_iterdeps(self):
  112. x = self.app.AsyncResult('1')
  113. c = [EagerResult(str(i), i, states.SUCCESS) for i in range(3)]
  114. x._cache = {'status': states.SUCCESS, 'result': None, 'children': c}
  115. for child in c:
  116. child.backend = Mock()
  117. child.backend.get_children.return_value = []
  118. it = x.iterdeps()
  119. self.assertListEqual(list(it), [
  120. (None, x),
  121. (x, c[0]),
  122. (x, c[1]),
  123. (x, c[2]),
  124. ])
  125. x._cache = None
  126. x.ready = Mock()
  127. x.ready.return_value = False
  128. with self.assertRaises(IncompleteStream):
  129. list(x.iterdeps())
  130. list(x.iterdeps(intermediate=True))
  131. def test_eq_not_implemented(self):
  132. self.assertFalse(self.app.AsyncResult('1') == object())
  133. @depends_on_current_app
  134. def test_reduce(self):
  135. a1 = self.app.AsyncResult('uuid', task_name=self.mytask.name)
  136. restored = pickle.loads(pickle.dumps(a1))
  137. self.assertEqual(restored.id, 'uuid')
  138. self.assertEqual(restored.task_name, self.mytask.name)
  139. a2 = self.app.AsyncResult('uuid')
  140. self.assertEqual(pickle.loads(pickle.dumps(a2)).id, 'uuid')
  141. def test_successful(self):
  142. ok_res = self.app.AsyncResult(self.task1['id'])
  143. nok_res = self.app.AsyncResult(self.task3['id'])
  144. nok_res2 = self.app.AsyncResult(self.task4['id'])
  145. self.assertTrue(ok_res.successful())
  146. self.assertFalse(nok_res.successful())
  147. self.assertFalse(nok_res2.successful())
  148. pending_res = self.app.AsyncResult(uuid())
  149. self.assertFalse(pending_res.successful())
  150. def test_str(self):
  151. ok_res = self.app.AsyncResult(self.task1['id'])
  152. ok2_res = self.app.AsyncResult(self.task2['id'])
  153. nok_res = self.app.AsyncResult(self.task3['id'])
  154. self.assertEqual(str(ok_res), self.task1['id'])
  155. self.assertEqual(str(ok2_res), self.task2['id'])
  156. self.assertEqual(str(nok_res), self.task3['id'])
  157. pending_id = uuid()
  158. pending_res = self.app.AsyncResult(pending_id)
  159. self.assertEqual(str(pending_res), pending_id)
  160. def test_repr(self):
  161. ok_res = self.app.AsyncResult(self.task1['id'])
  162. ok2_res = self.app.AsyncResult(self.task2['id'])
  163. nok_res = self.app.AsyncResult(self.task3['id'])
  164. self.assertEqual(repr(ok_res), '<AsyncResult: %s>' % (
  165. self.task1['id']))
  166. self.assertEqual(repr(ok2_res), '<AsyncResult: %s>' % (
  167. self.task2['id']))
  168. self.assertEqual(repr(nok_res), '<AsyncResult: %s>' % (
  169. self.task3['id']))
  170. pending_id = uuid()
  171. pending_res = self.app.AsyncResult(pending_id)
  172. self.assertEqual(repr(pending_res), '<AsyncResult: %s>' % (
  173. pending_id))
  174. def test_hash(self):
  175. self.assertEqual(hash(self.app.AsyncResult('x0w991')),
  176. hash(self.app.AsyncResult('x0w991')))
  177. self.assertNotEqual(hash(self.app.AsyncResult('x0w991')),
  178. hash(self.app.AsyncResult('x1w991')))
  179. def test_get_traceback(self):
  180. ok_res = self.app.AsyncResult(self.task1['id'])
  181. nok_res = self.app.AsyncResult(self.task3['id'])
  182. nok_res2 = self.app.AsyncResult(self.task4['id'])
  183. self.assertFalse(ok_res.traceback)
  184. self.assertTrue(nok_res.traceback)
  185. self.assertTrue(nok_res2.traceback)
  186. pending_res = self.app.AsyncResult(uuid())
  187. self.assertFalse(pending_res.traceback)
  188. def test_get(self):
  189. ok_res = self.app.AsyncResult(self.task1['id'])
  190. ok2_res = self.app.AsyncResult(self.task2['id'])
  191. nok_res = self.app.AsyncResult(self.task3['id'])
  192. nok2_res = self.app.AsyncResult(self.task4['id'])
  193. self.assertEqual(ok_res.get(), 'the')
  194. self.assertEqual(ok2_res.get(), 'quick')
  195. with self.assertRaises(KeyError):
  196. nok_res.get()
  197. self.assertTrue(nok_res.get(propagate=False))
  198. self.assertIsInstance(nok2_res.result, KeyError)
  199. self.assertEqual(ok_res.info, 'the')
  200. def test_get_timeout(self):
  201. res = self.app.AsyncResult(self.task4['id']) # has RETRY state
  202. with self.assertRaises(TimeoutError):
  203. res.get(timeout=0.001)
  204. pending_res = self.app.AsyncResult(uuid())
  205. with patch('celery.result.time') as _time:
  206. with self.assertRaises(TimeoutError):
  207. pending_res.get(timeout=0.001, interval=0.001)
  208. _time.sleep.assert_called_with(0.001)
  209. def test_get_timeout_longer(self):
  210. res = self.app.AsyncResult(self.task4['id']) # has RETRY state
  211. with patch('celery.result.time') as _time:
  212. with self.assertRaises(TimeoutError):
  213. res.get(timeout=1, interval=1)
  214. _time.sleep.assert_called_with(1)
  215. def test_ready(self):
  216. oks = (self.app.AsyncResult(self.task1['id']),
  217. self.app.AsyncResult(self.task2['id']),
  218. self.app.AsyncResult(self.task3['id']))
  219. self.assertTrue(all(result.ready() for result in oks))
  220. self.assertFalse(self.app.AsyncResult(self.task4['id']).ready())
  221. self.assertFalse(self.app.AsyncResult(uuid()).ready())
  222. class test_ResultSet(AppCase):
  223. def test_resultset_repr(self):
  224. self.assertTrue(repr(self.app.ResultSet(
  225. [self.app.AsyncResult(t) for t in ['1', '2', '3']])))
  226. def test_eq_other(self):
  227. self.assertFalse(self.app.ResultSet([1, 3, 3]) == 1)
  228. self.assertTrue(self.app.ResultSet([1]) == self.app.ResultSet([1]))
  229. def test_get(self):
  230. x = self.app.ResultSet([self.app.AsyncResult(t) for t in [1, 2, 3]])
  231. b = x.results[0].backend = Mock()
  232. b.supports_native_join = False
  233. x.join_native = Mock()
  234. x.join = Mock()
  235. x.get()
  236. self.assertTrue(x.join.called)
  237. b.supports_native_join = True
  238. x.get()
  239. self.assertTrue(x.join_native.called)
  240. def test_get_empty(self):
  241. x = self.app.ResultSet([])
  242. self.assertIsNone(x.supports_native_join)
  243. x.join = Mock(name='join')
  244. x.get()
  245. self.assertTrue(x.join.called)
  246. def test_add(self):
  247. x = self.app.ResultSet([1])
  248. x.add(2)
  249. self.assertEqual(len(x), 2)
  250. x.add(2)
  251. self.assertEqual(len(x), 2)
  252. @contextmanager
  253. def dummy_copy(self):
  254. with patch('celery.result.copy') as copy:
  255. def passt(arg):
  256. return arg
  257. copy.side_effect = passt
  258. yield
  259. def test_iterate_respects_subpolling_interval(self):
  260. r1 = self.app.AsyncResult(uuid())
  261. r2 = self.app.AsyncResult(uuid())
  262. backend = r1.backend = r2.backend = Mock()
  263. backend.subpolling_interval = 10
  264. ready = r1.ready = r2.ready = Mock()
  265. def se(*args, **kwargs):
  266. ready.side_effect = KeyError()
  267. return False
  268. ready.return_value = False
  269. ready.side_effect = se
  270. x = self.app.ResultSet([r1, r2])
  271. with self.dummy_copy():
  272. with patch('celery.result.time') as _time:
  273. with self.assertPendingDeprecation():
  274. with self.assertRaises(KeyError):
  275. list(x.iterate())
  276. _time.sleep.assert_called_with(10)
  277. backend.subpolling_interval = 0
  278. with patch('celery.result.time') as _time:
  279. with self.assertPendingDeprecation():
  280. with self.assertRaises(KeyError):
  281. ready.return_value = False
  282. ready.side_effect = se
  283. list(x.iterate())
  284. self.assertFalse(_time.sleep.called)
  285. def test_times_out(self):
  286. r1 = self.app.AsyncResult(uuid)
  287. r1.ready = Mock()
  288. r1.ready.return_value = False
  289. x = self.app.ResultSet([r1])
  290. with self.dummy_copy():
  291. with patch('celery.result.time'):
  292. with self.assertPendingDeprecation():
  293. with self.assertRaises(TimeoutError):
  294. list(x.iterate(timeout=1))
  295. def test_add_discard(self):
  296. x = self.app.ResultSet([])
  297. x.add(self.app.AsyncResult('1'))
  298. self.assertIn(self.app.AsyncResult('1'), x.results)
  299. x.discard(self.app.AsyncResult('1'))
  300. x.discard(self.app.AsyncResult('1'))
  301. x.discard('1')
  302. self.assertNotIn(self.app.AsyncResult('1'), x.results)
  303. x.update([self.app.AsyncResult('2')])
  304. def test_clear(self):
  305. x = self.app.ResultSet([])
  306. r = x.results
  307. x.clear()
  308. self.assertIs(x.results, r)
  309. class MockAsyncResultFailure(AsyncResult):
  310. @property
  311. def result(self):
  312. return KeyError('baz')
  313. @property
  314. def state(self):
  315. return states.FAILURE
  316. def get(self, propagate=True, **kwargs):
  317. if propagate:
  318. raise self.result
  319. return self.result
  320. class MockAsyncResultSuccess(AsyncResult):
  321. forgotten = False
  322. def forget(self):
  323. self.forgotten = True
  324. @property
  325. def result(self):
  326. return 42
  327. @property
  328. def state(self):
  329. return states.SUCCESS
  330. def get(self, **kwargs):
  331. return self.result
  332. class SimpleBackend(object):
  333. ids = []
  334. def __init__(self, ids=[]):
  335. self.ids = ids
  336. def get_many(self, *args, **kwargs):
  337. return ((id, {'result': i, 'status': states.SUCCESS})
  338. for i, id in enumerate(self.ids))
  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.app.conf.result_serializer = 'pickle'
  489. self.size = 11
  490. subtasks = make_mock_group(self.app, 10)
  491. failed = mock_task('ts11', states.FAILURE, KeyError('Baz'))
  492. save_result(self.app, failed)
  493. failed_res = self.app.AsyncResult(failed['id'])
  494. self.ts = self.app.GroupResult(uuid(), subtasks + [failed_res])
  495. def test_completed_count(self):
  496. self.assertEqual(self.ts.completed_count(), len(self.ts) - 1)
  497. def test_iterate_simple(self):
  498. with self.assertPendingDeprecation():
  499. it = self.ts.iterate()
  500. def consume():
  501. return list(it)
  502. with self.assertRaises(KeyError):
  503. consume()
  504. def test_join(self):
  505. with self.assertRaises(KeyError):
  506. self.ts.join()
  507. def test_successful(self):
  508. self.assertFalse(self.ts.successful())
  509. def test_failed(self):
  510. self.assertTrue(self.ts.failed())
  511. class test_pending_Group(AppCase):
  512. def setup(self):
  513. self.ts = self.app.GroupResult(
  514. uuid(), [self.app.AsyncResult(uuid()),
  515. self.app.AsyncResult(uuid())])
  516. def test_completed_count(self):
  517. self.assertEqual(self.ts.completed_count(), 0)
  518. def test_ready(self):
  519. self.assertFalse(self.ts.ready())
  520. def test_waiting(self):
  521. self.assertTrue(self.ts.waiting())
  522. def x_join(self):
  523. with self.assertRaises(TimeoutError):
  524. self.ts.join(timeout=0.001)
  525. def x_join_longer(self):
  526. with self.assertRaises(TimeoutError):
  527. self.ts.join(timeout=1)
  528. class test_EagerResult(AppCase):
  529. def setup(self):
  530. @self.app.task(shared=False)
  531. def raising(x, y):
  532. raise KeyError(x, y)
  533. self.raising = raising
  534. def test_wait_raises(self):
  535. res = self.raising.apply(args=[3, 3])
  536. with self.assertRaises(KeyError):
  537. res.wait()
  538. self.assertTrue(res.wait(propagate=False))
  539. def test_wait(self):
  540. res = EagerResult('x', 'x', states.RETRY)
  541. res.wait()
  542. self.assertEqual(res.state, states.RETRY)
  543. self.assertEqual(res.status, states.RETRY)
  544. def test_forget(self):
  545. res = EagerResult('x', 'x', states.RETRY)
  546. res.forget()
  547. def test_revoke(self):
  548. res = self.raising.apply(args=[3, 3])
  549. self.assertFalse(res.revoke())
  550. class test_tuples(AppCase):
  551. def test_AsyncResult(self):
  552. x = self.app.AsyncResult(uuid())
  553. self.assertEqual(x, result_from_tuple(x.as_tuple(), self.app))
  554. self.assertEqual(x, result_from_tuple(x, self.app))
  555. def test_with_parent(self):
  556. x = self.app.AsyncResult(uuid())
  557. x.parent = self.app.AsyncResult(uuid())
  558. y = result_from_tuple(x.as_tuple(), self.app)
  559. self.assertEqual(y, x)
  560. self.assertEqual(y.parent, x.parent)
  561. self.assertIsInstance(y.parent, AsyncResult)
  562. def test_compat(self):
  563. uid = uuid()
  564. x = result_from_tuple([uid, []], app=self.app)
  565. self.assertEqual(x.id, uid)
  566. def test_GroupResult(self):
  567. x = self.app.GroupResult(
  568. uuid(), [self.app.AsyncResult(uuid()) for _ in range(10)],
  569. )
  570. self.assertEqual(x, result_from_tuple(x.as_tuple(), self.app))
  571. self.assertEqual(x, result_from_tuple(x, self.app))