def get_available_host_port(): """Gets a host port number available for adb forward. Returns: An integer representing a port number on the host available for adb forward. Raises: Error: when no port is found after MAX_PORT_ALLOCATION_RETRY times. """ # Only import adb module if needed. from mobly.controllers.android_device_lib import adb port = portpicker.pick_unused_port() if not adb.is_adb_available(): return port for _ in range(MAX_PORT_ALLOCATION_RETRY): # Make sure adb is not using this port so we don't accidentally # interrupt ongoing runs by trying to bind to the port. if port not in adb.list_occupied_adb_ports(): return port port = portpicker.pick_unused_port() raise Error('Failed to find available port after {} retries'.format( MAX_PORT_ALLOCATION_RETRY))
def test_is_adb_available_negative(self, mock_run_command): mock_run_command.return_value = (0, ''.encode('utf-8'), ''.encode('utf-8')) self.assertFalse(adb.is_adb_available())
def test_is_adb_available(self, mock_run_command): mock_run_command.return_value = ( 0, '/usr/local/bin/adb\n'.encode('utf-8'), ''.encode('utf-8')) self.assertTrue(adb.is_adb_available())