def test_purge_timer_rescheduling(self): """ Tests if the purge timer is rescheduled every day. """ purge_notifications_timer = self.store.get_notification_timer(self.purge_notifications_timer_name) previous_callback_at = purge_notifications_timer.callback_at background_notification_check.Command().handle() # Fetch the timer again since it should be updated. purge_notifications_timer = self.store.get_notification_timer(self.purge_notifications_timer_name) current_callback_at = purge_notifications_timer.callback_at self.assertIsNone(purge_notifications_timer.executed_at) self.assertEqual(previous_callback_at, current_callback_at) # now reset the time to 1 day from now in future # in order to execute the daily digest timer again reset_time = (datetime.now(pytz.UTC) + timedelta(days=1)).replace(hour=1, minute=0, second=0) with freeze_time(reset_time): # call digest command handle again background_notification_check.Command().handle() # fetch the timer from the DB as it should be updated purge_notifications_timer = self.store.get_notification_timer(self.purge_notifications_timer_name) self.assertIsNone(purge_notifications_timer.executed_at) # allow for some slight time arthimetic skew expected_callback_at = purge_notifications_timer.callback_at.replace(second=0, microsecond=0) actual_callback_at = (reset_time + timedelta(days=1)).replace(second=0, microsecond=0) self.assertEqual(expected_callback_at, actual_callback_at)
def test_weekly_digest_timers(self): """ test to check for weekly digest timers calling back each week """ weekly_digest_timer_name = self.store.get_notification_timer(self.weekly_digest_timer_name) previous_callback_at = weekly_digest_timer_name.callback_at background_notification_check.Command().handle() # fetch the timer from the DB as it should be updated weekly_digest_timer_name = self.store.get_notification_timer(self.weekly_digest_timer_name) current_callback_at = weekly_digest_timer_name.callback_at self.assertIsNone(weekly_digest_timer_name.executed_at) self.assertEqual(previous_callback_at, current_callback_at) # now reset the time to 7 days(1 Week) from now in future # in order to execute the daily digest timer again reset_time = datetime.now(pytz.UTC) + timedelta(days=7) with freeze_time(reset_time): # call digest command handle again background_notification_check.Command().handle() # fetch the timer from the DB as it should be updated weekly_digest_timer_name = self.store.get_notification_timer(self.weekly_digest_timer_name) self.assertIn('last_ran', weekly_digest_timer_name.context) self.assertTrue(isinstance(weekly_digest_timer_name.context['last_ran'], datetime)) self.assertTrue(weekly_digest_timer_name.context['last_ran'] - reset_time < timedelta(seconds=1)) freeze_time_callback_at = weekly_digest_timer_name.callback_at self.assertIsNone(weekly_digest_timer_name.executed_at) self.assertEqual(current_callback_at, freeze_time_callback_at - timedelta(days=7))
def test_background_check(self): """ Invoke the Management Command """ background_notification_check.Command().handle() self.assertTrue(_SIGNAL_RAISED)
def test_timer_execution(self): """ Make sure that Django management command runs through the timers """ timer = NotificationCallbackTimer( name='foo', class_name='edx_notifications.tests.test_timer.NullNotificationCallbackTimerHandler', callback_at=datetime.now(pytz.UTC) - timedelta(days=1), context={}, is_active=True, ) notification_store().save_notification_timer(timer) background_notification_check.Command().handle() readback_timer = notification_store().get_notification_timer(timer.name) self.assertIsNotNone(readback_timer.executed_at) self.assertIsNone(readback_timer.err_msg)