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
def test_repeating(self): time = TestingTimeFunction() callback = MockCallback() timer = Timer(callback, 10, repeat=True, _time_function=time) time.time = 7 still_alive = timer.run() assert still_alive assert callback.nb_calls == 0 assert timer.sleep_time() == 3 time.time = 34 still_alive = timer.run() assert still_alive assert callback.nb_calls == 1 assert timer.sleep_time() == 6 time.time = 40 still_alive = timer.run() assert still_alive assert callback.nb_calls == 2 assert timer.sleep_time() == 10
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')
def test_cancel(self): time = TestingTimeFunction() callback = MockCallback() timer = Timer(callback, 10, _time_function=time) time.time = 9 still_alive = timer.run() assert still_alive assert timer.sleep_time() == 1 timer.cancel() still_alive = timer.run() assert not still_alive assert callback.nb_calls == 0 assert timer.sleep_time() == Decimal('inf')
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')
def test_cancel_repeating(self): time = TestingTimeFunction() callback = MockCallback() timer = Timer(callback, 10, repeat=True, _time_function=time) time.time = 10 timer.run() time.time = 34 still_alive = timer.run() assert still_alive assert callback.nb_calls == 2 assert timer.sleep_time() == 6 time.time = 40 timer.cancel() for time.time in (40, 43, 60, 138): still_alive = timer.run() assert not still_alive assert callback.nb_calls == 2 assert timer.sleep_time() == Decimal('inf')
def _non_repeating(self, decorate): for delay in (0, 1): time = TestingTimeFunction() callback = MockCallback() if decorate: timer = Timer.decorate(10, _time_function=time)(callback) else: timer = Timer(callback, 10, _time_function=time) still_alive = timer.run() assert still_alive assert callback.nb_calls == 0 assert timer.sleep_time() == 10 # Not yet time.time = 9 still_alive = timer.run() assert still_alive assert callback.nb_calls == 0 assert timer.sleep_time() == 1 # Either just on time or after time.time = 10 + delay assert timer.sleep_time() == 0 still_alive = timer.run() assert not still_alive assert callback.nb_calls == 1 assert timer.sleep_time() == Decimal('inf') # More runs do nothing still_alive = timer.run() assert not still_alive assert callback.nb_calls == 1 assert timer.sleep_time() == Decimal('inf') time.time = 15 still_alive = timer.run() assert not still_alive assert callback.nb_calls == 1 assert timer.sleep_time() == Decimal('inf')