async def test_callback_called_with_correct_args(self, scheduler: MockScheduler): callback_mock = mock.Mock() now = await scheduler.get_now() handle = await scheduler.insert_schedule('', now + datetime.timedelta(seconds=1), callback_mock, False, None, arg1='asdf', arg2='qwerty') scheduler.sim_fast_forward(datetime.timedelta(seconds=10)) callback_mock.assert_called_once_with({'arg1': 'asdf', 'arg2': 'qwerty'})
async def test_callback_called_after_timeout(self, scheduler: MockScheduler): callback_mock = mock.Mock() now = await scheduler.get_now() await scheduler.insert_schedule('', now + datetime.timedelta(seconds=10), callback_mock, False, None), scheduler.sim_fast_forward(datetime.timedelta(seconds=20)) callback_mock.assert_called()
async def test_canceled_timer_does_not_run_callback(self, scheduler: MockScheduler): callback_mock = mock.Mock() now = await scheduler.get_now() handle = await scheduler.insert_schedule('', now + datetime.timedelta(seconds=10), callback_mock, False, None) scheduler.sim_fast_forward(datetime.timedelta(seconds=5)) await scheduler.cancel_timer('', handle) scheduler.sim_fast_forward(datetime.timedelta(seconds=10)) callback_mock.assert_not_called()
async def test_callbacks_are_run_in_time_order(self, scheduler:MockScheduler): first_mock = mock.Mock() second_mock = mock.Mock() third_mock = mock.Mock() manager = mock.Mock() manager.attach_mock(first_mock, 'first_mock') manager.attach_mock(second_mock, 'second_mock') manager.attach_mock(third_mock, 'third_mock') now = await scheduler.get_now() # Note: insert them out of order to try and expose possible bugs await asyncio.gather( scheduler.insert_schedule('', now + datetime.timedelta(seconds=10), first_mock, False, None), scheduler.insert_schedule('', now + datetime.timedelta(seconds=30), third_mock, False, None), scheduler.insert_schedule('', now + datetime.timedelta(seconds=20), second_mock, False, None)) scheduler.sim_fast_forward(datetime.timedelta(seconds=30)) expected_call_order = [mock.call.first_mock({}), mock.call.second_mock({}), mock.call.third_mock({})] assert manager.mock_calls == expected_call_order
async def test_time_is_correct_when_callback_it_run(self, scheduler: MockScheduler): scheduler.sim_set_start_time(datetime.datetime(2020, 1, 1, 12, 0)) time_when_called = [] def callback(kwargs): nonlocal time_when_called time_when_called.append(scheduler.get_now_sync()) now = await scheduler.get_now() await asyncio.gather( scheduler.insert_schedule('', now + datetime.timedelta(seconds=1), callback, False, None), scheduler.insert_schedule('', now + datetime.timedelta(seconds=15), callback, False, None), scheduler.insert_schedule('', now + datetime.timedelta(seconds=65), callback, False, None)) scheduler.sim_fast_forward(datetime.timedelta(seconds=90)) expected_call_times = [ pytz.utc.localize(datetime.datetime(2020, 1, 1, 12, 0, 1)), pytz.utc.localize(datetime.datetime(2020, 1, 1, 12, 0, 15)), pytz.utc.localize(datetime.datetime(2020, 1, 1, 12, 1, 5)), ] assert expected_call_times == time_when_called
async def test_callback_with_interval_is_rescheduled_after_being_run(self, scheduler: MockScheduler): callback_mock = mock.Mock() now = await scheduler.get_now() handle = await scheduler.insert_schedule('', now + datetime.timedelta(seconds=10), callback_mock, False, None, interval=10) # Advance 3 time and make sure it's called each time scheduler.sim_fast_forward(datetime.timedelta(seconds=10)) assert callback_mock.call_count == 1 scheduler.sim_fast_forward(datetime.timedelta(seconds=10)) assert callback_mock.call_count == 2 scheduler.sim_fast_forward(datetime.timedelta(seconds=10)) assert callback_mock.call_count == 3