test_postpone.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. # -*- coding: utf-8 -*-
  2. from __future__ import absolute_import
  3. import datetime
  4. import logging
  5. import pytest
  6. from tests.unit.utils.scheduler.base import SchedulerTestsBase
  7. try:
  8. import dateutil.parser
  9. HAS_DATEUTIL_PARSER = True
  10. except ImportError:
  11. HAS_DATEUTIL_PARSER = False
  12. log = logging.getLogger(__name__)
  13. @pytest.mark.skipif(
  14. HAS_DATEUTIL_PARSER is False,
  15. reason="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. @pytest.mark.slow_test(seconds=5) # Test takes >1 and <=5 seconds
  25. def test_postpone(self):
  26. """
  27. verify that scheduled job is postponed until the specified time.
  28. """
  29. job = {
  30. "schedule": {"job1": {"function": "test.ping", "when": "11/29/2017 4pm"}}
  31. }
  32. # 11/29/2017 4pm
  33. run_time = dateutil.parser.parse("11/29/2017 4:00pm")
  34. # 5 minute delay
  35. delay = 300
  36. # Add job to schedule
  37. self.schedule.opts.update(job)
  38. # Postpone the job by 5 minutes
  39. self.schedule.postpone_job(
  40. "job1",
  41. {
  42. "time": run_time.strftime("%Y-%m-%dT%H:%M:%S"),
  43. "new_time": (run_time + datetime.timedelta(seconds=delay)).strftime(
  44. "%Y-%m-%dT%H:%M:%S"
  45. ),
  46. },
  47. )
  48. # Run at the original time
  49. self.schedule.eval(now=run_time)
  50. ret = self.schedule.job_status("job1")
  51. self.assertNotIn("_last_run", ret)
  52. # Run 5 minutes later
  53. self.schedule.eval(now=run_time + datetime.timedelta(seconds=delay))
  54. ret = self.schedule.job_status("job1")
  55. self.assertEqual(ret["_last_run"], run_time + datetime.timedelta(seconds=delay))
  56. # Run 6 minutes later
  57. self.schedule.eval(now=run_time + datetime.timedelta(seconds=delay + 1))
  58. ret = self.schedule.job_status("job1")
  59. self.assertEqual(ret["_last_run"], run_time + datetime.timedelta(seconds=delay))