def test_timer_off_triggers_blocking_func(self, mocker): blocking_callback = mocker.MagicMock() timer = Timer(callback=blocking_callback) assert not blocking_callback.called timer.off(OffType.time_out) assert blocking_callback.called
def test_timer_off_and_exception_in_blocking_func_does_not_break_process( self, mocker): blocking_callback = mocker.MagicMock() blocking_callback.side_effect = RuntimeError("Call is back!!") timer = Timer(callback=blocking_callback) assert not blocking_callback.called timer.off(OffType.time_out) assert blocking_callback.called
def test_timer_off_triggers_coroutine_func(self, mocker): coro_call_checker = mocker.MagicMock() async def coro_callback(**kwargs): coro_call_checker() timer = Timer(callback=coro_callback) timer.off(OffType.time_out) asyncio.get_event_loop().run_until_complete( asyncio.sleep(0)) # Give a chance for coroutine to run assert coro_call_checker.called
def test_timer_off_and_exception_in_coroutine_func_does_not_break_process( self, mocker): coro_call_checker = mocker.MagicMock() coro_call_checker.side_effect = RuntimeError("Call is back!!") async def coro_callback(**kwargs): coro_call_checker() timer = Timer(callback=coro_callback) timer.off(OffType.time_out) asyncio.get_event_loop().run_until_complete( asyncio.sleep(0)) # Give a chance for coroutine to run assert coro_call_checker.called
def test_stop_timer_with_invalid_key(self, timer_service: TimerService, mocker): mock_timer_off = mocker.MagicMock() timer = Timer(duration=1) timer.off = mock_timer_off timer_service.add_timer(TIMER_KEY, timer) assert TIMER_KEY in timer_service.timer_list timer_service.stop_timer(INVALID_TIMER_KEY, OffType.normal) assert TIMER_KEY in timer_service.timer_list assert not mock_timer_off.called
def test_stop_timer_calls_timer_off_and_remove(self, timer_service: TimerService, mocker): mock_timer_off = mocker.MagicMock() timer = Timer(duration=1) timer.off = mock_timer_off timer_service.add_timer(TIMER_KEY, timer) assert TIMER_KEY in timer_service.timer_list timer_service.stop_timer(TIMER_KEY, OffType.normal) assert TIMER_KEY not in timer_service.timer_list assert mock_timer_off.called
def test_restart_timer_turnoff_timer_and_reset(self, timer_service: TimerService, mocker): mock_timer_off = mocker.MagicMock() mock_timer_reset = mocker.MagicMock() timer = Timer(duration=1) timer.off = mock_timer_off timer.reset = mock_timer_reset timer_service.add_timer(TIMER_KEY, timer) timer_service.restart_timer(TIMER_KEY) assert mock_timer_off.called assert mock_timer_reset.called
def test_restart_timer_with_invalid_key(self, timer_service: TimerService, mocker): mock_timer_off = mocker.MagicMock() mock_timer_reset = mocker.MagicMock() timer = Timer(duration=1) timer.off = mock_timer_off timer.reset = mock_timer_reset timer_service.add_timer(TIMER_KEY, timer) timer_service.restart_timer(INVALID_TIMER_KEY) assert not mock_timer_off.called assert not mock_timer_reset.called