def test_polled_job(self): """ Test features of the specialized job class that we overloaded. """ now = datetime.datetime.now() min_interval = PolledScheduler.interval(seconds=1) max_interval = PolledScheduler.interval(seconds=3) trigger = PolledIntervalTrigger(min_interval, max_interval, now) job = PolledIntervalJob(trigger, self._callback, [], {}, 1, 1, name='test_job') self.assertIsNotNone(job) log.debug("H: %s" % repr(job)) next_time = job.compute_next_run_time(now) self.assert_datetime_close(next_time, now + max_interval) self.assertEqual(job.name, 'test_job') self.assertTrue(job.ready_to_run()) next_time = job.compute_next_run_time(now) self.assertFalse(job.ready_to_run()) self.assert_datetime_close(next_time, now + max_interval) time.sleep(2) now = datetime.datetime.now() self.assertTrue(job.ready_to_run()) next_time = job.compute_next_run_time(now) self.assertFalse(job.ready_to_run()) self.assert_datetime_close(next_time, now + max_interval)
def test_trigger_string(self): """ test the str and repr methods """ now = datetime.datetime.now() trigger = PolledIntervalTrigger(PolledScheduler.interval(seconds=1), PolledScheduler.interval(seconds=3), now) self.assertEqual(str(trigger), "min_interval[0:00:01] max_interval[0:00:03]") self.assertEqual( repr(trigger), "<PolledIntervalTrigger (min_interval=datetime.timedelta(0, 1), max_interval=datetime.timedelta(0, 3))>" ) trigger = PolledIntervalTrigger(PolledScheduler.interval(seconds=1), None, now) self.assertEqual(str(trigger), "min_interval[0:00:01] max_interval[None]") self.assertEqual( repr(trigger), "<PolledIntervalTrigger (min_interval=datetime.timedelta(0, 1), max_interval=None)>" )
def test_polled_interval_trigger(self): """ test the trigger mechanism. """ ### # Test all constructors and exceptions ### trigger = PolledIntervalTrigger(PolledScheduler.interval(seconds=1)) self.assertEqual(trigger.min_interval_length, 1) self.assertIsNone(trigger.max_interval) self.assertIsNone(trigger.max_interval_length) self.assertIsInstance(trigger.next_min_date, datetime.datetime) self.assertIsNone(trigger.next_max_date) trigger = PolledIntervalTrigger( PolledScheduler.interval(seconds=1), PolledScheduler.interval(seconds=3) ) self.assertEqual(trigger.min_interval_length, 1) self.assertEqual(trigger.max_interval_length, 3) trigger = PolledIntervalTrigger( PolledScheduler.interval(seconds=1), PolledScheduler.interval(seconds=3), datetime.datetime.now() ) self.assertEqual(trigger.min_interval_length, 1) self.assertEqual(trigger.max_interval_length, 3) # Test Type Error Exception with self.assertRaises(TypeError): trigger = PolledIntervalTrigger('boom') with self.assertRaises(TypeError): trigger = PolledIntervalTrigger( PolledScheduler.interval(seconds=3), 'boom' ) # Test Value Error Exception with self.assertRaises(ValueError): trigger = PolledIntervalTrigger( PolledScheduler.interval(seconds=3), PolledScheduler.interval(seconds=1) ) ### # Verify min and max dates are incremented correctly. ### now = datetime.datetime.now() log.debug("Now: %s" % now) min_interval = PolledScheduler.interval(seconds=1) max_interval = PolledScheduler.interval(seconds=3) trigger = PolledIntervalTrigger(min_interval, max_interval, now) # Initialized correctly? self.assert_datetime_close(trigger.next_min_date, now) self.assert_datetime_close(trigger.next_max_date, now + max_interval) self.assert_datetime_close(trigger.get_next_fire_time(now), now + max_interval) # First call should be successful, but second should not. self.assertTrue(trigger.pull_trigger()) self.assertFalse(trigger.pull_trigger()) self.assert_datetime_close(trigger.next_min_date, now + min_interval) self.assert_datetime_close(trigger.next_max_date, now + max_interval) self.assert_datetime_close(trigger.get_next_fire_time(now), now + max_interval) # Wait for the minimum interval and it should succeed again! time.sleep(2) now = datetime.datetime.now() self.assertTrue(trigger.pull_trigger()) self.assertFalse(trigger.pull_trigger()) ### # Now do the same sequence, but with no max_interval ### now = datetime.datetime.now() log.debug("Now: %s" % now) min_interval = PolledScheduler.interval(seconds=1) max_interval = None trigger = PolledIntervalTrigger(min_interval, max_interval, now) # Initialized correctly? self.assert_datetime_close(trigger.next_min_date, now) self.assertIsNone(trigger.next_max_date) self.assertIsNone(trigger.get_next_fire_time(now)) # First call should be successful, but second should not. self.assertTrue(trigger.pull_trigger()) self.assertFalse(trigger.pull_trigger()) self.assert_datetime_close(trigger.next_min_date, now + min_interval) self.assertIsNone(trigger.next_max_date) self.assertIsNone(trigger.get_next_fire_time(now)) # Wait for the minimum interval and it should succeed again! time.sleep(2) now = datetime.datetime.now() self.assertTrue(trigger.pull_trigger()) self.assertFalse(trigger.pull_trigger())
def test_polled_interval_trigger(self): """ test the trigger mechanism. """ ### # Test all constructors and exceptions ### trigger = PolledIntervalTrigger(PolledScheduler.interval(seconds=1)) self.assertEqual(trigger.min_interval_length, 1) self.assertIsNone(trigger.max_interval) self.assertIsNone(trigger.max_interval_length) self.assertIsInstance(trigger.next_min_date, datetime.datetime) self.assertIsNone(trigger.next_max_date) trigger = PolledIntervalTrigger(PolledScheduler.interval(seconds=1), PolledScheduler.interval(seconds=3)) self.assertEqual(trigger.min_interval_length, 1) self.assertEqual(trigger.max_interval_length, 3) trigger = PolledIntervalTrigger(PolledScheduler.interval(seconds=1), PolledScheduler.interval(seconds=3), datetime.datetime.now()) self.assertEqual(trigger.min_interval_length, 1) self.assertEqual(trigger.max_interval_length, 3) # Test Type Error Exception with self.assertRaises(TypeError): trigger = PolledIntervalTrigger('boom') with self.assertRaises(TypeError): trigger = PolledIntervalTrigger( PolledScheduler.interval(seconds=3), 'boom') # Test Value Error Exception with self.assertRaises(ValueError): trigger = PolledIntervalTrigger( PolledScheduler.interval(seconds=3), PolledScheduler.interval(seconds=1)) ### # Verify min and max dates are incremented correctly. ### now = datetime.datetime.now() log.debug("Now: %s" % now) min_interval = PolledScheduler.interval(seconds=1) max_interval = PolledScheduler.interval(seconds=3) trigger = PolledIntervalTrigger(min_interval, max_interval, now) # Initialized correctly? self.assert_datetime_close(trigger.next_min_date, now) self.assert_datetime_close(trigger.next_max_date, now + max_interval) self.assert_datetime_close(trigger.get_next_fire_time(now), now + max_interval) # First call should be successful, but second should not. self.assertTrue(trigger.pull_trigger()) self.assertFalse(trigger.pull_trigger()) self.assert_datetime_close(trigger.next_min_date, now + min_interval) self.assert_datetime_close(trigger.next_max_date, now + max_interval) self.assert_datetime_close(trigger.get_next_fire_time(now), now + max_interval) # Wait for the minimum interval and it should succeed again! time.sleep(2) now = datetime.datetime.now() self.assertTrue(trigger.pull_trigger()) self.assertFalse(trigger.pull_trigger()) ### # Now do the same sequence, but with no max_interval ### now = datetime.datetime.now() log.debug("Now: %s" % now) min_interval = PolledScheduler.interval(seconds=1) max_interval = None trigger = PolledIntervalTrigger(min_interval, max_interval, now) # Initialized correctly? self.assert_datetime_close(trigger.next_min_date, now) self.assertIsNone(trigger.next_max_date) self.assertIsNone(trigger.get_next_fire_time(now)) # First call should be successful, but second should not. self.assertTrue(trigger.pull_trigger()) self.assertFalse(trigger.pull_trigger()) self.assert_datetime_close(trigger.next_min_date, now + min_interval) self.assertIsNone(trigger.next_max_date) self.assertIsNone(trigger.get_next_fire_time(now)) # Wait for the minimum interval and it should succeed again! time.sleep(2) now = datetime.datetime.now() self.assertTrue(trigger.pull_trigger()) self.assertFalse(trigger.pull_trigger())