def test_no_last_run(self): call = ScheduledCall('PT1M', 'pulp.tasks.dosomething') entry = call.as_schedule_entry() # celery actually calculates it, so we don't need to test the value self.assertTrue(isinstance(entry.last_run_at, datetime))
def test_captures_scheduled_call(self): call = ScheduledCall('2014-01-19T17:15Z/PT1H', 'pulp.tasks.dosomething') entry = call.as_schedule_entry() self.assertTrue(hasattr(entry, '_scheduled_call')) self.assertTrue(entry._scheduled_call is call)
def test_no_runs(self): call = ScheduledCall('PT1H', 'pulp.tasks.dosomething') entry = call.as_schedule_entry() is_due, seconds = entry.is_due() self.assertTrue(is_due) # make sure this is very close to one hour self.assertTrue(3600 - seconds < 1)
class TestScheduleEntryIsDue(unittest.TestCase): def setUp(self): super(TestScheduleEntryIsDue, self).setUp() self.call = ScheduledCall('2014-01-19T17:15Z/PT1H', 'pulp.tasks.dosomething', remaining_runs=5) self.entry = self.call.as_schedule_entry() @mock.patch('time.time') def test_first_run_future(self, mock_time): mock_time.return_value = 1389307330 is_due, seconds = self.entry.is_due() self.assertFalse(is_due) self.assertEqual(seconds, 844370) def test_no_runs(self): call = ScheduledCall('PT1H', 'pulp.tasks.dosomething') entry = call.as_schedule_entry() is_due, seconds = entry.is_due() self.assertTrue(is_due) # make sure this is very close to one hour self.assertTrue(3600 - seconds < 1) @mock.patch('time.time') def test_past_runs_due(self, mock_time): mock_time.return_value = 1389389758 # 2014-01-10T21:35:58 # This call did not run at the top of the hour, so it is overdue and should # run now. Its next run will be back on the normal hourly schedule, at # the top of the next hour. call = ScheduledCall('2014-01-10T20:00Z/PT1H', 'pulp.tasks.dosomething', last_run_at='2014-01-10T20:00Z', total_run_count=1) entry = call.as_schedule_entry() is_due, seconds = entry.is_due() self.assertTrue(is_due) # this was hand-calculated as the remaining time until the next hourly run self.assertEqual(seconds, 1442) @mock.patch('time.time') def test_past_runs_not_due(self, mock_time): mock_time.return_value = 1389389758 # 2014-01-10T21:35:58 # This call ran at the top of the hour, so it does not need to run again # until the top of the next hour. call = ScheduledCall('2014-01-10T20:00Z/PT1H', 'pulp.tasks.dosomething', last_run_at='2014-01-10T21:00Z', total_run_count=2) entry = call.as_schedule_entry() is_due, seconds = entry.is_due() self.assertFalse(is_due) # this was hand-calculated as the remaining time until the next hourly run self.assertEqual(seconds, 1442)
def test_past_runs_not_due(self, mock_time): mock_time.return_value = 1389389758 # 2014-01-10T21:35:58 # This call ran at the top of the hour, so it does not need to run again # until the top of the next hour. call = ScheduledCall('2014-01-10T20:00Z/PT1H', 'pulp.tasks.dosomething', last_run_at='2014-01-10T21:00Z', total_run_count=2) entry = call.as_schedule_entry() is_due, seconds = entry.is_due() self.assertFalse(is_due) # this was hand-calculated as the remaining time until the next hourly run self.assertEqual(seconds, 1442)
def test_past_runs_due(self, mock_time): mock_time.return_value = 1389389758 # 2014-01-10T21:35:58 # This call did not run at the top of the hour, so it is overdue and should # run now. Its next run will be back on the normal hourly schedule, at # the top of the next hour. call = ScheduledCall('2014-01-10T20:00Z/PT1H', 'pulp.tasks.dosomething', last_run_at='2014-01-10T20:00Z', total_run_count=1) entry = call.as_schedule_entry() is_due, seconds = entry.is_due() self.assertTrue(is_due) # this was hand-calculated as the remaining time until the next hourly run self.assertEqual(seconds, 1442)
class TestScheduleEntryNextInstance(unittest.TestCase): def setUp(self): super(TestScheduleEntryNextInstance, self).setUp() self.call = ScheduledCall('2014-01-19T17:15Z/PT1H', 'pulp.tasks.dosomething', remaining_runs=5) self.entry = self.call.as_schedule_entry() def test_increments_last_run(self, mock_save): next_entry = next(self.entry) now = datetime.utcnow().replace(tzinfo=dateutils.utc_tz()) self.assertTrue(now - next_entry.last_run_at < timedelta(seconds=1)) def test_increments_run_count(self, mock_save): next_entry = next(self.entry) self.assertEqual(self.entry.total_run_count + 1, next_entry.total_run_count) def test_decrements_remaining_runs(self, mock_save): remaining = self.call.remaining_runs next(self.entry) self.assertEqual(remaining - 1, self.call.remaining_runs) def test_disables_for_remaining_runs(self, mock_save): self.call.remaining_runs = 1 # just verify that we have the correct starting state self.assertTrue(self.call.enabled) next(self.entry) # call should have been disabled because the remaining_runs hit 0 self.assertFalse(self.call.enabled) def test_calls_save(self, mock_save): next(self.entry) mock_save.assert_called_once_with() def test_returns_entry(self, mock_save): next_entry = next(self.entry) self.assertTrue(isinstance(next_entry, ScheduleEntry)) self.assertEqual(self.entry.name, next_entry.name) self.assertFalse(self.entry is next_entry)