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_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')