Exemplo n.º 1
0
    def test_reset_late(self):
        time = TestingTimeFunction()
        callback = MockCallback()
        timer = Timer(callback, 10, _time_function=time)
        
        time.time = 13

        assert timer.sleep_time() == 0
        still_alive = timer.run()
        assert not still_alive
        assert callback.nb_calls == 1
        assert timer.sleep_time() == Decimal('inf')
        
        timer.reset()

        # reset brings back a "dead" timer.
        still_alive = timer.run()
        assert still_alive
        assert callback.nb_calls == 1
        assert timer.sleep_time() == 10

        time.time = 21
        
        still_alive = timer.run()
        assert still_alive
        assert callback.nb_calls == 1
        assert timer.sleep_time() == 2

        time.time = 30

        assert timer.sleep_time() == 0
        still_alive = timer.run()
        assert not still_alive
        assert callback.nb_calls == 2
        assert timer.sleep_time() == Decimal('inf')
Exemplo n.º 2
0
    def test_reset_repeating(self):
        time = TestingTimeFunction()
        callback = MockCallback()
        timer = Timer(callback, 10, repeat=True, _time_function=time)
        
        time.time = 12

        still_alive = timer.run()
        assert still_alive
        assert callback.nb_calls == 1
        assert timer.sleep_time() == 8

        timer.reset()
        assert timer.sleep_time() == 10

        time.time = 34
        assert timer.sleep_time() == 0
        
        timer.reset()
        assert timer.sleep_time() == 10
        
        still_alive = timer.run()
        assert still_alive
        assert callback.nb_calls == 1
        assert timer.sleep_time() == 10
Exemplo n.º 3
0
    def test_reset_early(self):
        time = TestingTimeFunction()
        callback = MockCallback()
        timer = Timer(callback, 10, _time_function=time)
        
        time.time = 7

        assert timer.sleep_time() == 3
        timer.reset()
        assert timer.sleep_time() == 10
        
        time.time = 16
        
        still_alive = timer.run()
        assert still_alive
        # reset delayed the trigger.
        assert callback.nb_calls == 0
        assert timer.sleep_time() == 1

        time.time = 17

        assert timer.sleep_time() == 0
        still_alive = timer.run()
        assert not still_alive
        assert callback.nb_calls == 1
        assert timer.sleep_time() == Decimal('inf')