def _is_in_fastboot_mode(self, timeout: Optional[float] = None) -> bool: """Returns if device in fastboot mode. Waits up to timeout time. We try to verify whether the device is in fastboot mode in the {timeout} duration, with retry interval 0.5 sec. If timeout is None (not provided), there will be no retry. Args: timeout: Maximum wait time in seconds. If timeout is None, we will not retry. Returns: Whether device is in fastboot mode. """ # No retry when timeout is None. Setting timeout to 0.1 is equivalent to # "no retry" since interval is 0.5 sec. timeout = timeout or 0.1 try: retry.retry(adb_utils.is_fastboot_mode, func_args=(self._fastboot_serial, ), is_successful=bool, timeout=timeout, interval=0.5) logger.info("{} is in fastboot mode.", self._device_name) return True except errors.CommunicationTimeoutError: return False
def set_mode(self, mode, port): """Sets the given port to the mode specified. Args: mode (str): mode to set. Options: 'off', 'on' port (int): The port to set. Raises: DeviceError: invalid port, or mode. """ self._validate_mode(mode) logger.info("{} setting power mode to {} for port {}".format( self._device_name, mode, port)) if mode == ON: self._turn_on_port_func(port) else: self._turn_off_port_func(port) try: retry.retry(func=self._verify_mode_change, func_args=[port, mode], timeout=TIMEOUTS["STATE_CHANGE"], is_successful=retry.is_true, interval=1, reraise=False) except errors.CommunicationTimeoutError: raise errors.DeviceError( "{} failed to verify that ethernet connection is {}".format( self._device_name, mode))
def test_retry_timeout_custom_error(self): """Test retry with a timeout; error type is provided.""" with self.assertRaisesRegex(RuntimeError, "Timeout"): retry.retry(_waste_time, is_successful=_not, timeout=1, interval=0.25, exc_type=RuntimeError)
def test_retry_timeout_default_error(self): """Test retry with a timeout; error type left as default.""" with self.assertRaisesRegex(errors.CommunicationTimeoutError, "Timeout"): retry.retry(_waste_time, is_successful=_not, timeout=1, interval=0.25)
def test_reraise_false(self): """Test calling a function which raises an Exception with reraise=False.""" mock_is_successful = mock.Mock() with self.assertRaisesRegex( errors.CommunicationTimeoutError, r"Timeout.*{}".format(_func_raises.__name__)): retry.retry(_func_raises, is_successful=mock_is_successful, timeout=1, interval=0.25, reraise=False) mock_is_successful.assert_not_called()
def check_device_connected(self): """Checks that device shows up as a connection on the host machine. Raises: DeviceNotConnectedError: if device is not connected. """ device_config = {"persistent": self.props["persistent_identifiers"]} try: retry.retry(func=self.is_connected, func_args=(device_config, ), is_successful=bool, timeout=self.timeouts["CONNECTED"]) except errors.CommunicationTimeoutError: raise errors.DeviceNotConnectedError( self.name, msg="device not reachable from host machine.")
def check_ping_responsiveness(self): """Check if the auxiliary device responds to pings. Raises: DeviceNotResponsiveError: if no response to ping before the timeout. """ try: retry.retry(self._ping, is_successful=bool, timeout=self._PING_TIMEOUT, reraise=True) except Exception as err: raise errors.DeviceNotResponsiveError(self.name, "failed to respond to ping", timeout=self._PING_TIMEOUT, details=str(err))
def test_retry_return_value(self): """Test retry returns the function return value.""" ret_val = "egret" def _func(): return ret_val result = retry.retry(_func, timeout=1, interval=0.25) self.assertEqual(result, ret_val)
def test_retry_success_with_retries(self): """Test retry with failure on first tries, but success later.""" ret_val = "egret" def _func(): _func.called_count += 1 if _func.called_count >= 3: return ret_val _func.called_count = 0 def _is_success(val): return val == ret_val result = retry.retry(_func, is_successful=_is_success, interval=0.25, timeout=2) self.assertEqual(result, ret_val)
def test_retry_success(self): """Test retry with a success on first try.""" def _func(): return True retry.retry(_func, timeout=1, interval=0.25)
def test_reraise_true(self): """Test calling a function which raises an Exception with reraise=True.""" with self.assertRaisesRegex(RuntimeError, "Foo too bar"): retry.retry(_func_raises, timeout=1, reraise=True)