test_compat.py 1.6 KB

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