def test_timezone_aware_processor_doesnt_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 = TimezoneAwareTrigger(datetime.time(12, 0), 'Etc/UTC') with freezegun.freeze_time('2019-08-01 01:00 UTC'): processor = TimezoneAwareProcessor(mock_callable, trigger) with freezegun.freeze_time('2019-08-01 11:00 UTC'): processor() mock_callable.assert_not_called() # not yet with freezegun.freeze_time('2019-08-01 15:00 UTC'): processor() # Deliberately reproduce the impossible scenario which # test_timezone_aware_processor_doesnt_run_multiple_times carefully # avoids processor() mock_callable.assert_called_once_with()
def test_gate_at_fixed_time_with_specific_timezone(custom_app): gate = Gate( 'fixed_time_gate', next_states=NoNextStates(), exit_condition=ExitConditionProgram('false'), triggers=[TimezoneAwareTrigger( datetime.time(12, 1), timezone='Europe/London', )], ) app = create_app(custom_app, [gate]) def processor(*, state, **kwargs): assert state == gate processor.called = True processor.called = False scheduler = schedule.Scheduler() configure_schedule(app, scheduler, processor) assert len(scheduler.jobs) == 1, "Should have scheduled a single job" job, = scheduler.jobs assert job.next_run == datetime.datetime(2018, 1, 1, 12, 1) assert processor.called is False with freezegun.freeze_time(job.next_run): job.run() assert processor.called is True assert job.next_run == datetime.datetime(2018, 1, 1, 12, 2)
def test_timezone_aware_processor_repr() -> None: mock_callable = mock.Mock() trigger = TimezoneAwareTrigger(datetime.time(12, 0), 'Etc/UTC') processor = TimezoneAwareProcessor(mock_callable, trigger) assert 'Etc/UTC' in repr(processor) assert '12:00' in repr(processor)
def test_timezone_aware_processor_doesnt_run_at_wrong_time() -> None: mock_callable = mock.Mock() trigger = TimezoneAwareTrigger(datetime.time(12, 0), 'Etc/UTC') with freezegun.freeze_time(recently()): processor = TimezoneAwareProcessor(mock_callable, trigger) processor() mock_callable.assert_not_called()
def test_timezone_aware_processor_runs_on_time_other_timezone() -> None: mock_callable = mock.Mock() trigger = TimezoneAwareTrigger(datetime.time(13, 0), 'Europe/London') with freezegun.freeze_time(recently()): processor = TimezoneAwareProcessor(mock_callable, trigger) processor() mock_callable.assert_called_once_with()
def test_timezone_aware_processor_runs_if_delayed_since_construction() -> None: mock_callable = mock.Mock() trigger = TimezoneAwareTrigger(datetime.time(12, 0), 'Etc/UTC') with freezegun.freeze_time('2019-08-01 11:00 UTC'): processor = TimezoneAwareProcessor(mock_callable, trigger) processor() mock_callable.assert_called_once_with()
def test_timezone_aware_processor_doesnt_run_when_timezone_doesnt_match( ) -> None: mock_callable = mock.Mock() trigger = TimezoneAwareTrigger(datetime.time(12, 0), 'Europe/London') with freezegun.freeze_time(recently()): processor = TimezoneAwareProcessor(mock_callable, trigger) processor() mock_callable.assert_not_called()
def test_timezone_aware_processor_doesnt_run_multiple_times() -> None: mock_callable = mock.Mock() trigger = TimezoneAwareTrigger(datetime.time(12, 0), 'Etc/UTC') with freezegun.freeze_time('2019-08-01 01:00 UTC'): processor = TimezoneAwareProcessor(mock_callable, trigger) with freezegun.freeze_time('2019-08-01 11:00 UTC'): processor() mock_callable.assert_not_called() # not yet with freezegun.freeze_time('2019-08-01 15:00 UTC') as frozen_time: processor() frozen_time.tick(delta=datetime.timedelta(microseconds=10)) processor() mock_callable.assert_called_once_with()
def test_realistic_config(): data = yaml_data('realistic') expected = Config( state_machines={ 'example': StateMachine( name='example', feeds=[ FeedConfig(name='data_feed', url='http://localhost/<label>'), ], webhooks=[ Webhook( match=re.compile('.+\\.example\\.com'), headers={ 'x-api-key': 'Rahfew7eed1ierae0moa2sho3ieB1et3ohhum0Ei', }, ), ], states=[ Gate( name='start', triggers=[ SystemTimeTrigger(time=datetime.time(18, 30)), TimezoneAwareTrigger( time=datetime.time(12, 25), timezone='Europe/London', ), MetadataTimezoneAwareTrigger( time=datetime.time(13, 37), timezone_metadata_path=['timezone'], ), MetadataTrigger(metadata_path='foo.bar'), IntervalTrigger( interval=datetime.timedelta(hours=1), ), OnEntryTrigger(), ], next_states=ConstantNextState(state='stage2'), exit_condition=ExitConditionProgram('true'), ), Gate( name='stage2', triggers=[], next_states=ContextNextStates( path='metadata.foo.bar', destinations=[ ContextNextStatesOption( state='stage3', value='1', ), ContextNextStatesOption( state='stage3', value='2', ), ], default='end', ), exit_condition=ExitConditionProgram( 'metadata.foo.bar is defined', ), ), Action( name='stage3', webhook='https://localhost/hook', next_states=ConstantNextState(state='end'), ), Gate( name='end', triggers=[], exit_condition=ExitConditionProgram('false'), next_states=NoNextStates(), ), ], ), }, database=DatabaseConfig( host='localhost', port=5432, name='routemaster', username='******', password='', ), logging_plugins=[ LoggingPluginConfig( dotted_path='routemaster_prometheus:PrometheusLogger', kwargs={'prometheus_gateway': 'localhost'}, ), LoggingPluginConfig( dotted_path='routemaster_sentry:SentryLogger', kwargs={'raven_dsn': 'nai8ioca4zeeb2ahgh4V'}, ), ], ) with reset_environment(): assert load_config(data) == expected