test_compat.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. from __future__ import absolute_import, unicode_literals
  2. import pytest
  3. from datetime import timedelta
  4. from celery.five import bytes_if_py2
  5. from celery.schedules import schedule
  6. from celery.task import (
  7. periodic_task,
  8. PeriodicTask
  9. )
  10. class test_periodic_tasks:
  11. def setup(self):
  12. self.app.set_current() # @depends_on_current_app
  13. @periodic_task(app=self.app, shared=False,
  14. run_every=schedule(timedelta(hours=1), app=self.app))
  15. def my_periodic():
  16. pass
  17. self.my_periodic = my_periodic
  18. def now(self):
  19. return self.app.now()
  20. def test_must_have_run_every(self):
  21. with pytest.raises(NotImplementedError):
  22. type(bytes_if_py2('Foo'), (PeriodicTask,), {
  23. '__module__': __name__,
  24. })
  25. def test_remaining_estimate(self):
  26. s = self.my_periodic.run_every
  27. assert isinstance(
  28. s.remaining_estimate(s.maybe_make_aware(self.now())),
  29. timedelta)
  30. def test_is_due_not_due(self):
  31. due, remaining = self.my_periodic.run_every.is_due(self.now())
  32. assert not due
  33. # This assertion may fail if executed in the
  34. # first minute of an hour, thus 59 instead of 60
  35. assert remaining > 59
  36. def test_is_due(self):
  37. p = self.my_periodic
  38. due, remaining = p.run_every.is_due(
  39. self.now() - p.run_every.run_every,
  40. )
  41. assert due
  42. assert remaining == p.run_every.run_every.total_seconds()
  43. def test_schedule_repr(self):
  44. p = self.my_periodic
  45. assert repr(p.run_every)