def test_metadata_timezone_processor_doesnt_run_multiply() -> None: mock_callable = mock.Mock() trigger = MetadataTimezoneAwareTrigger(datetime.time(12, 0), ['tz']) with freezegun.freeze_time('2019-08-01 11:58 UTC'): processor = MetadataTimezoneAwareProcessor(mock_callable, trigger) with freezegun.freeze_time('2019-08-01 12:05 UTC') as frozen_time: with mock_cron_processors_functools_partial() as mock_partial: processor() frozen_time.tick(delta=datetime.timedelta(microseconds=10)) processor() mock_partial.assert_called_once_with( labels_in_state_with_metadata, path=['tz'], values=mock.ANY, ) timezones = mock_partial.call_args[1]['values'] assert 'Etc/UTC' in timezones assert 'Europe/London' not in timezones mock_callable.assert_called_once_with(label_provider=mock.ANY)
def test_metadata_timezone_processor_runs_if_delayed_since_last_run() -> None: mock_callable = mock.Mock() trigger = MetadataTimezoneAwareTrigger(datetime.time(12, 0), ['tz']) with freezegun.freeze_time('2019-08-01 11:55 UTC'): processor = MetadataTimezoneAwareProcessor(mock_callable, trigger) with freezegun.freeze_time('2019-08-01 11:58 UTC'): with mock_cron_processors_functools_partial() as mock_partial: processor() mock_partial.assert_not_called() # not yet with mock_cron_processors_functools_partial() as mock_partial: processor() mock_partial.assert_called_once_with( labels_in_state_with_metadata, path=['tz'], values=mock.ANY, ) timezones = mock_partial.call_args[1]['values'] assert 'Etc/UTC' in timezones assert 'Europe/London' not in timezones mock_callable.assert_called_once_with(label_provider=mock.ANY)
def _configure_schedule_for_state( scheduler: schedule.Scheduler, processor: StateSpecificCronProcessor, state: State, ) -> None: if isinstance(state, Action): scheduler.every().minute.do( processor, fn=process_action, label_provider=labels_in_state, ) elif isinstance(state, Gate): for trigger in state.triggers: if isinstance(trigger, SystemTimeTrigger): scheduler.every().day.at( f"{trigger.time.hour:02d}:{trigger.time.minute:02d}", ).do( processor, fn=process_gate, label_provider=labels_in_state, ) elif isinstance(trigger, TimezoneAwareTrigger): func = functools.partial( processor, fn=process_gate, label_provider=labels_in_state, ) scheduler.every().minute.do( TimezoneAwareProcessor(func, trigger), ) elif isinstance(trigger, MetadataTimezoneAwareTrigger): scheduler.every().minute.do( MetadataTimezoneAwareProcessor( functools.partial(processor, fn=process_gate), trigger, ), ) elif isinstance(trigger, IntervalTrigger): scheduler.every( trigger.interval.total_seconds(), ).seconds.do( processor, fn=process_gate, label_provider=labels_in_state, ) elif isinstance(trigger, MetadataTrigger): # pragma: no branch label_provider = labels_needing_metadata_update_retry_in_gate scheduler.every().minute.do( processor, fn=process_gate, label_provider=label_provider, ) else: # We only care about time based triggers and retries here. pass # pragma: no cover else: raise RuntimeError( # pragma: no cover f"Unsupported state type {state}", )
def test_metadata_timezone_aware_processor_repr() -> None: mock_callable = mock.Mock() trigger = MetadataTimezoneAwareTrigger(datetime.time(12, 0), ['tz']) with freezegun.freeze_time(recently()): processor = MetadataTimezoneAwareProcessor(mock_callable, trigger) assert 'tz' in repr(processor) assert '12:00' in repr(processor)
def test_metadata_timezone_processor_doesnt_run_at_wrong_time() -> None: mock_callable = mock.Mock() trigger = MetadataTimezoneAwareTrigger(datetime.time(12, 0), ['tz']) with freezegun.freeze_time(recently()): processor = MetadataTimezoneAwareProcessor(mock_callable, trigger) with mock_cron_processors_functools_partial() as mock_partial: processor() mock_partial.assert_not_called() mock_callable.assert_not_called()
def test_metadata_timezone_processor_doesnt_bubble_internal_exceptions( ) -> None: # Note: the processor assumes that the callable they're passed won't raise, # because that is assumed to be `cron.process_job` which has its own error # handling. The processor does howver need to ensure that other errors it # may encounter while checking whether to run are handled. mock_callable = mock.Mock() trigger = MetadataTimezoneAwareTrigger(datetime.time(12, 0), ['tz']) with freezegun.freeze_time('2019-08-01 11:58 UTC'): processor = MetadataTimezoneAwareProcessor(mock_callable, trigger) with freezegun.freeze_time('2019-08-01 12:05 UTC'): processor() # Deliberately reproduce the impossible scenario which # test_metadata_timezone_processor_doesnt_run_multiply carefully # avoids processor() mock_callable.assert_called_once_with(label_provider=mock.ANY)
def test_metadata_timezone_aware_processor_runs_on_time() -> None: mock_callable = mock.Mock() trigger = MetadataTimezoneAwareTrigger(datetime.time(12, 0), ['tz']) with freezegun.freeze_time(recently()): processor = MetadataTimezoneAwareProcessor(mock_callable, trigger) with mock_cron_processors_functools_partial() as mock_partial: processor() mock_partial.assert_called_once_with( labels_in_state_with_metadata, path=['tz'], values=mock.ANY, ) timezones = mock_partial.call_args[1]['values'] assert 'Etc/UTC' in timezones assert 'Europe/London' in timezones mock_callable.assert_called_once_with(label_provider=mock.ANY)