test_postpone.py 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import datetime
  4. import logging
  5. from tests.support.helpers import slowTest
  6. from tests.support.unit import skipIf
  7. from tests.unit.utils.scheduler.base import SchedulerTestsBase
  8. try:
  9. import dateutil.parser
  10. HAS_DATEUTIL_PARSER = True
  11. except ImportError:
  12. HAS_DATEUTIL_PARSER = False
  13. log = logging.getLogger(__name__)
  14. @skipIf(
  15. HAS_DATEUTIL_PARSER is False, "The 'dateutil.parser' library is not available",
  16. )
  17. class SchedulerPostponeTest(SchedulerTestsBase):
  18. """
  19. Validate the pkg module
  20. """
  21. def setUp(self):
  22. super(SchedulerPostponeTest, self).setUp()
  23. self.schedule.opts["loop_interval"] = 1
  24. def tearDown(self):
  25. self.schedule.reset()
  26. super(SchedulerPostponeTest, self).tearDown()
  27. @slowTest
  28. def test_postpone(self):
  29. """
  30. verify that scheduled job is postponed until the specified time.
  31. """
  32. job = {
  33. "schedule": {"job1": {"function": "test.ping", "when": "11/29/2017 4pm"}}
  34. }
  35. # 11/29/2017 4pm
  36. run_time = dateutil.parser.parse("11/29/2017 4:00pm")
  37. # 5 minute delay
  38. delay = 300
  39. # Add job to schedule
  40. self.schedule.opts.update(job)
  41. # Postpone the job by 5 minutes
  42. self.schedule.postpone_job(
  43. "job1",
  44. {
  45. "time": run_time.strftime("%Y-%m-%dT%H:%M:%S"),
  46. "new_time": (run_time + datetime.timedelta(seconds=delay)).strftime(
  47. "%Y-%m-%dT%H:%M:%S"
  48. ),
  49. },
  50. )
  51. # Run at the original time
  52. self.schedule.eval(now=run_time)
  53. ret = self.schedule.job_status("job1")
  54. self.assertNotIn("_last_run", ret)
  55. # Run 5 minutes later
  56. self.schedule.eval(now=run_time + datetime.timedelta(seconds=delay))
  57. ret = self.schedule.job_status("job1")
  58. self.assertEqual(ret["_last_run"], run_time + datetime.timedelta(seconds=delay))
  59. # Run 6 minutes later
  60. self.schedule.eval(now=run_time + datetime.timedelta(seconds=delay + 1))
  61. ret = self.schedule.job_status("job1")
  62. self.assertEqual(ret["_last_run"], run_time + datetime.timedelta(seconds=delay))