示例#1
0
 def test_timer_case1d(self) -> None:
     """Test timer case1d."""
     print('mainline entered')
     timer = Timer(timeout=None, default_timeout=None)
     time.sleep(1)
     assert not timer.is_expired()
     time.sleep(1)
     assert not timer.is_expired()
     print('mainline exiting')
示例#2
0
 def test_timer_case1a(self) -> None:
     """Test timer case1a."""
     print('mainline entered')
     timer = Timer()
     time.sleep(1)
     assert not timer.is_expired()
     time.sleep(1)
     assert not timer.is_expired()
     print('mainline exiting')
示例#3
0
    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')
示例#4
0
    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')
示例#5
0
    def test_timer_case4a(self,
                          zero_or_less_timeout_arg: IntFloat) -> None:
        """Test timer case4a.

        Args:
            zero_or_less_timeout_arg: pytest fixture for timeout seconds

        """
        print('mainline entered')
        timer = Timer(timeout=zero_or_less_timeout_arg)
        time.sleep(abs(zero_or_less_timeout_arg * 0.9))
        assert not timer.is_expired()
        time.sleep(abs(zero_or_less_timeout_arg))
        assert not timer.is_expired()
        print('mainline exiting')
示例#6
0
    def test_timer_case7a(self,
                          greater_than_zero_timeout_arg: IntFloat) -> None:
        """Test timer case7a.

        Args:
            greater_than_zero_timeout_arg: pytest fixture for timeout
                                             seconds

        """
        print('mainline entered')
        timer = Timer(timeout=greater_than_zero_timeout_arg)
        time.sleep(greater_than_zero_timeout_arg * 0.9)
        assert not timer.is_expired()
        time.sleep(greater_than_zero_timeout_arg)
        assert timer.is_expired()
        print('mainline exiting')
示例#7
0
    def test_timer_example1(self,
                            capsys: Any) -> None:
        """Test timer example1.

        Args:
            capsys: pytest fixture to capture print output

        """
        print('mainline entered')
        timer = Timer(timeout=3)
        for idx in range(10):
            print(f'idx = {idx}')
            time.sleep(1)
            if timer.is_expired():
                print('timer has expired')
                break
        print('mainline exiting')

        expected_result = 'mainline entered\n'
        expected_result += 'idx = 0\n'
        expected_result += 'idx = 1\n'
        expected_result += 'idx = 2\n'
        expected_result += 'timer has expired\n'
        expected_result += 'mainline exiting\n'

        captured = capsys.readouterr().out

        assert captured == expected_result
示例#8
0
 def m1(self,
        sleep_time: float,
        timeout: Optional[float] = None) -> bool:
     timer = Timer(timeout=timeout,
                   default_timeout=self.default_timeout)
     time.sleep(sleep_time)
     if timer.is_expired():
         return False
     return True
示例#9
0
    def get_msg(self,
                recipient: str,
                timeout: OptIntFloat = GET_CMD_TIMEOUT) -> Any:
        """Get the next message in the queue.

        Args:
            recipient: arbitrary name that designates the target of the
                   message and which will be used with the queue_msg
                   method to identify the intended recipient of the
                   message
            timeout: number of seconds allowed for msg response. A
                       negative value, zero, or None means no timeout
                       will happen. If timeout is not specified, then
                       the default timeout value will be used.

        Returns:
            the received message

        Raises:
            GetMsgTimedOut: {recipient} timed out waiting for msg

        """
        # get a timer (the clock is started when instantiated)
        timer = Timer(timeout=timeout)

        # shared/excl lock here could improve performance
        with self.msg_lock:
            if recipient not in self.msg_array:
                # we need to add the message target if this is the first
                # time the recipient is calling get_msg
                self.msg_array[recipient] = queue.Queue(
                    maxsize=Msgs.CMD_Q_MAX_SIZE)

        while True:
            try:
                msg = self.msg_array[recipient].get(block=True, timeout=0.1)
                return msg
            except queue.Empty:
                pass

            if timer.is_expired():
                caller_info = get_formatted_call_sequence(latest=1, depth=1)
                err_msg = (f'Thread {threading.current_thread()} '
                           f'timed out on get_msg for recipient: {recipient} '
                           f'{caller_info}')
                self.logger.debug(err_msg)
                raise GetMsgTimedOut(err_msg)
示例#10
0
 def m1(self, sleep_time: float, timeout: float) -> bool:
     timer = Timer(timeout=timeout)
     time.sleep(sleep_time)
     if timer.is_expired():
         return False
     return True