Ejemplo n.º 1
0
    def test_timeout_after_duration_sec_passed(self, duration):
        timer = Timer(duration=duration)
        assert not timer.is_timeout()

        with freeze_time(datetime.datetime.utcnow() +
                         datetime.timedelta(seconds=duration)):
            assert timer.is_timeout()
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
    def test_reset_timer_with_invalid_key(self, timer_service: TimerService, mocker):
        timer = Timer(duration=1)
        mock_timer_reset = mocker.MagicMock()
        timer.reset = mock_timer_reset
        timer_service.add_timer(TIMER_KEY, timer)

        timer_service.reset_timer(INVALID_TIMER_KEY)
        assert not mock_timer_reset.called
Ejemplo n.º 4
0
    def test_reset_timer_calls_timer_reset(self, timer_service: TimerService, mocker):
        timer = Timer(duration=1)
        mock_timer_reset = mocker.MagicMock()
        timer.reset = mock_timer_reset
        timer_service.add_timer(TIMER_KEY, timer)

        timer_service.reset_timer(TIMER_KEY)
        assert mock_timer_reset.called
Ejemplo n.º 5
0
    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
Ejemplo n.º 6
0
    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
Ejemplo n.º 7
0
    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
Ejemplo n.º 8
0
    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
Ejemplo n.º 9
0
    def test_remain_time_returns_positive_if_not_passed_duration_sec(
            self, duration):
        timer = Timer(duration=duration)

        remain_time = timer.remain_time()
        assert 0 < remain_time < duration

        with freeze_time(datetime.datetime.utcnow() +
                         datetime.timedelta(seconds=duration)) as frozen_time:
            frozen_time.tick(TICK_INTERVAL)
            remain_time = timer.remain_time()

            assert remain_time == 0
Ejemplo n.º 10
0
    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
Ejemplo n.º 11
0
    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
Ejemplo n.º 12
0
    def test_remove_timer_deletes_timer_key(self, timer_service: TimerService):
        timer = Timer(duration=1)
        timer_service.add_timer(TIMER_KEY, timer)

        timer_service.remove_timer(TIMER_KEY)
        assert len(timer_service.timer_list) == 0
        assert not timer_service.get_timer(TIMER_KEY)
Ejemplo n.º 13
0
    def test_clean(self, timer_service: TimerService):
        timer = Timer(duration=1)
        timer_service.add_timer(TIMER_KEY, timer)
        assert len(timer_service.timer_list) == 1

        timer_service.clean()
        assert not timer_service.timer_list
Ejemplo n.º 14
0
 def __start_broadcast_send_unconfirmed_block_timer(broadcast_func):
     timer_key = TimerService.TIMER_KEY_BROADCAST_SEND_UNCONFIRMED_BLOCK
     timer_service = ObjectManager().channel_service.timer_service
     timer_service.add_timer(
         timer_key,
         Timer(target=timer_key,
               duration=conf.INTERVAL_BROADCAST_SEND_UNCONFIRMED_BLOCK,
               is_repeat=True,
               repeat_timeout=conf.TIMEOUT_FOR_LEADER_COMPLAIN,
               is_run_at_start=True,
               callback=broadcast_func))
Ejemplo n.º 15
0
    def test_add_timer_without_is_run_at_start(self, timer_service: TimerService, mocker):
        timer = Timer(duration=1)

        mock_run = mocker.MagicMock()
        mock_run_immediate = mocker.MagicMock()
        timer_service._TimerService__run = mock_run
        timer_service._TimerService__run_immediate = mock_run_immediate

        mocker.patch.object(asyncio, "run_coroutine_threadsafe")
        timer_service.add_timer(TIMER_KEY, timer)
        assert not mock_run_immediate.called
        assert mock_run.called
Ejemplo n.º 16
0
    def test_reset_timer_restores_its_start_time(self):
        duration = 5
        offset = 1
        timer = Timer(duration=duration)

        with freeze_time(datetime.datetime.utcnow() + datetime.timedelta(
                seconds=duration - offset)) as frozen_time:
            frozen_time.tick(TICK_INTERVAL)
            assert 0 < timer.remain_time() < offset

            timer.reset()
            frozen_time.tick(TICK_INTERVAL)

            assert offset < timer.remain_time() < duration
Ejemplo n.º 17
0
    def test_is_repeat_returns_true_until_repeat_timeout(self):
        duration = 2
        repeat_timeout = 5

        timer = Timer(duration=duration,
                      is_repeat=True,
                      repeat_timeout=repeat_timeout)

        # Timer repeats unless the timer does not reach to repeat_timeout
        for sec in range(repeat_timeout):
            with freeze_time(
                    datetime.datetime.now(tz=datetime.timezone.utc) +
                    datetime.timedelta(seconds=sec)):
                assert timer.is_repeat

        # But after passed repeat_timeout sec, the timer does not repeat itself
        with freeze_time(
                datetime.datetime.now(tz=datetime.timezone.utc) +
                datetime.timedelta(seconds=repeat_timeout)):
            assert not timer.is_repeat
Ejemplo n.º 18
0
 def test_timer_on(self):
     timer = Timer()
     timer.on()
Ejemplo n.º 19
0
 def test_is_repeat_returns_true_with_infinite_repeat_timeout(self):
     timer = Timer(is_repeat=True, repeat_timeout=0)
     assert timer.is_repeat
Ejemplo n.º 20
0
    def test_remove_timer_with_invalid_key(self, timer_service: TimerService):
        timer = Timer(duration=1)
        timer_service.add_timer(TIMER_KEY, timer)

        assert not timer_service.remove_timer(INVALID_TIMER_KEY)
        assert TIMER_KEY in timer_service.timer_list
Ejemplo n.º 21
0
 def test_is_repeat_returns_false_in_default(self):
     timer = Timer(is_repeat=False)
     assert not timer.is_repeat
Ejemplo n.º 22
0
    def test_add_timer_adds_timer_key(self, timer_service: TimerService):
        timer = Timer(duration=1)

        timer_service.add_timer(TIMER_KEY, timer)
        assert len(timer_service.timer_list) == 1
        assert timer_service.get_timer(TIMER_KEY)