def test_contsant_sleep(self): time = TestingTimeFunction() manager = TimerManager(_time_function=time) c5 = MockCallback() manager.add_timer(5, c5, repeat=True) c7 = MockCallback() manager.add_timer(7, c7, repeat=True) c13 = MockCallback() manager.add_timer(13, c13, repeat=True) for time.time in xrange(100): # manager.sleep_time() is optimal, but calling run() more often # also works. (Here every "second") manager.run() if time.time == 42: def check_time(): assert time.time == 48 one_shot = MockCallback() manager.add_timer(6, check_time) manager.add_timer(6, one_shot) assert c5.nb_calls == 19 assert c7.nb_calls == 14 assert c13.nb_calls == 7 assert one_shot.nb_calls == 1
def test_single_timer(self): time = TestingTimeFunction() manager = TimerManager(_time_function=time) callback = MockCallback() manager.add_timer(30, callback) assert manager.sleep_time() == 30 assert callback.nb_calls == 0 manager.run() assert callback.nb_calls == 0 time.time = 22 assert manager.sleep_time() == 8 manager.run() assert callback.nb_calls == 0 time.time = 30 assert manager.sleep_time() == 0 manager.run() assert callback.nb_calls == 1 # Timer was removed from the list assert manager.sleep_time() == Decimal('inf') time.time = 100 manager.run() # not called a second time assert callback.nb_calls == 1
def test_long_callback(self): time = TestingTimeFunction() manager = TimerManager(_time_function=time) def loose_time(): time.time += 11 manager.add_timer(2, loose_time, repeat=True) manager.run() assert time.time == 0 time.time = 2 manager.run() assert time.time == 13 # t = 4, 6, 8 were skipped, t = 12 is late but ready assert manager.sleep_time() == 0
def test_many(self): time = TestingTimeFunction() manager = TimerManager(_time_function=time) c5 = MockCallback() manager.add_timer(5, c5, repeat=True) c7 = MockCallback() manager.add_timer(7, c7, repeat=True) c13 = MockCallback() manager.add_timer(13, c13, repeat=True) nb_sleeps = 0 while 1: time.time += manager.sleep_time() if time.time >= 100: break nb_sleeps += 1 manager.run() assert c5.nb_calls == 19 assert c7.nb_calls == 14 assert c13.nb_calls == 7 # 4 is the number of times (t = 35, 65, 70, 91) where 2 timers # trigger at the same time. assert nb_sleeps == (c5.nb_calls + c7.nb_calls + c13.nb_calls - 4)
def test_single_repeating_timer(self): time = TestingTimeFunction() manager = TimerManager(_time_function=time) callback = MockCallback() manager.add_timer(30, callback, repeat=True) assert manager.sleep_time() == 30 assert callback.nb_calls == 0 manager.run() assert callback.nb_calls == 0 time.time = 22 assert manager.sleep_time() == 8 manager.run() assert callback.nb_calls == 0 time.time = 30 assert manager.sleep_time() == 0 manager.run() assert callback.nb_calls == 1 # Timer was NOT removed from the list assert manager.sleep_time() == 30 time.time = 71 assert manager.sleep_time() == 0 manager.run() assert callback.nb_calls == 2 assert manager.sleep_time() == 19 # "Wait" more than one interval time.time = 200 assert manager.sleep_time() == 0 manager.run() # Only called once more assert callback.nb_calls == 3 # Next is at t = 21 assert manager.sleep_time() == 10