test_abortable.py 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. from __future__ import absolute_import, unicode_literals
  2. from celery.contrib.abortable import AbortableAsyncResult, AbortableTask
  3. class test_AbortableTask:
  4. def setup(self):
  5. @self.app.task(base=AbortableTask, shared=False)
  6. def abortable():
  7. return True
  8. self.abortable = abortable
  9. def test_async_result_is_abortable(self):
  10. result = self.abortable.apply_async()
  11. tid = result.id
  12. assert isinstance(
  13. self.abortable.AsyncResult(tid), AbortableAsyncResult)
  14. def test_is_not_aborted(self):
  15. self.abortable.push_request()
  16. try:
  17. result = self.abortable.apply_async()
  18. tid = result.id
  19. assert not self.abortable.is_aborted(task_id=tid)
  20. finally:
  21. self.abortable.pop_request()
  22. def test_is_aborted_not_abort_result(self):
  23. self.abortable.AsyncResult = self.app.AsyncResult
  24. self.abortable.push_request()
  25. try:
  26. self.abortable.request.id = 'foo'
  27. assert not self.abortable.is_aborted()
  28. finally:
  29. self.abortable.pop_request()
  30. def test_abort_yields_aborted(self):
  31. self.abortable.push_request()
  32. try:
  33. result = self.abortable.apply_async()
  34. result.abort()
  35. tid = result.id
  36. assert self.abortable.is_aborted(task_id=tid)
  37. finally:
  38. self.abortable.pop_request()