def test_timer_example5(self, capsys: Any) -> None: """Test timer example5. Args: capsys: pytest fixture to capture print output """ def f1() -> None: print('f1 entered') time.sleep(1) f1_event.set() time.sleep(1) f1_event.set() time.sleep(1) f1_event.set() print('f1 exiting') print('mainline entered') timer = Timer(timeout=2.5) f1_thread = threading.Thread(target=f1) f1_event = threading.Event() f1_thread.start() wait_result = f1_event.wait(timeout=timer.remaining_time()) print(f'wait1 result = {wait_result}') f1_event.clear() print(f'remaining time = {timer.remaining_time():0.1f}') print(f'timer expired = {timer.is_expired()}') wait_result = f1_event.wait(timeout=timer.remaining_time()) print(f'wait2 result = {wait_result}') f1_event.clear() print(f'remaining time = {timer.remaining_time():0.1f}') print(f'timer expired = {timer.is_expired()}') wait_result = f1_event.wait(timeout=timer.remaining_time()) print(f'wait3 result = {wait_result}') f1_event.clear() print(f'remaining time = {timer.remaining_time():0.4f}') print(f'timer expired = {timer.is_expired()}') f1_thread.join() print('mainline exiting') expected_result = 'mainline entered\n' expected_result += 'f1 entered\n' expected_result += 'wait1 result = True\n' expected_result += 'remaining time = 1.5\n' expected_result += 'timer expired = False\n' expected_result += 'wait2 result = True\n' expected_result += 'remaining time = 0.5\n' expected_result += 'timer expired = False\n' expected_result += 'wait3 result = False\n' expected_result += 'remaining time = 0.0001\n' expected_result += 'timer expired = True\n' expected_result += 'f1 exiting\n' expected_result += 'mainline exiting\n' captured = capsys.readouterr().out assert captured == expected_result
def test_timer_remaining_time1(self, timeout_arg: IntFloat) -> None: """Test timer remaining time1. Args: timeout_arg: number of seconds to use for timer timeout arg """ tolerance_factor = 0.80 logger.debug('mainline entered') stop_watch = StopWatch() sleep_time = timeout_arg/3 exp_remaining_time1: float = timeout_arg - sleep_time exp_remaining_time2: float = timeout_arg - sleep_time * 2 exp_remaining_time3 = 0.0001 timer = Timer(timeout=timeout_arg) stop_watch.start_clock(clock_iter=1) stop_watch.pause(sleep_time, clock_iter=1) rem_time = timer.remaining_time() assert ((exp_remaining_time1 * tolerance_factor) <= cast(float, rem_time) <= exp_remaining_time1) assert not timer.is_expired() logger.debug(f'after third 1: ' f'{exp_remaining_time1=}, {rem_time=}') stop_watch.pause(sleep_time * 2, clock_iter=1) rem_time = timer.remaining_time() assert ((exp_remaining_time2 * tolerance_factor) <= cast(float, rem_time) <= exp_remaining_time2) assert not timer.is_expired() logger.debug(f'after third 2: ' f'{exp_remaining_time2=}, {rem_time=}') time.sleep(sleep_time + 0.1) rem_time = timer.remaining_time() assert exp_remaining_time3 == cast(float, rem_time) assert timer.is_expired() logger.debug(f'after third 3: ' f'{exp_remaining_time3=}, {rem_time=}') logger.debug(f'{stop_watch.start_time=} ' f'{timer.start_time=}') logger.debug('mainline exiting')
def test_timer_remaining_time_none(self) -> None: """Test timer remaining time none2.""" logger.debug('mainline entered') timer = Timer(timeout=None) time.sleep(1) assert timer.remaining_time() is None assert not timer.is_expired() time.sleep(1) assert timer.remaining_time() is None assert not timer.is_expired() logger.debug('mainline exiting')