def test_port_manager(self): port_type = "agent" udid1 = "aaaaaaaaaaaa" udid2 = "bbbbbbbbbbbb" PortManager.set_port(port_type, udid1) port1 = PortManager.get_port(port_type, udid1) port2 = PortManager.get_port(port_type, udid2) self.assertEqual(port1, 8100, 'PortManager分配端口错误') self.assertNotEqual(port1, port2, 'PortManager分配端口重复') PortManager.del_port(port_type, udid1) self.assertTrue(udid1 not in PortManager.ports(port_type), 'PortManager清理端口失败')
def run(self): if self._udid: if self._web_port: # 单例类PortManager,设置WebInspector端口 from qt4i.driver.tools.sched import PortManager PortManager.set_port(port_type='web', udid=self._udid, port=self._web_port, base=27753) # 单例类XCUITestAgentManager,启动XCUITestAgent from qt4i.driver.xctest.agent import XCUITestAgentManager agent_manager = XCUITestAgentManager() agent_manager.start_agent(device_id=self._udid, server_port=self._agent_port, retry=1) server = SimpleJSONRPCServer(urls=urls, addr=(self._driver_address, self._driver_port)) logger.info('DriverServer(%s:%s) - started' % (self._driver_address, self._driver_port)) server.serve_forever()
def start_agent(self, device_id, server_ip=DEFAULT_IP, server_port=DEFAULT_PORT, keep_alive=False, retry=3, timeout=60): '''启动Agent并返回 :param device_id: 设备ID :type device_id: str :param server_ip: 设备IP地址 :type server_ip: str :param server_port: 设备端口号 :type server_port: int :param keep_alive: HTTP远程连接是否使用keep-alive,默认为False :type keep_alive: boolean :param retry: 启动尝试次数,默认3次 :type retry: int :param timeout: 单次启动超时 (秒) :type timeout: int :returns: XCUITestAgent ''' self.log = logger.get_logger("xctest_%s" % device_id) if device_id not in self._agents: try: PortManager.set_port('agent', device_id, server_port) server_port = PortManager.get_port('agent', device_id) self.log.info('start_agent, port: %d' % server_port) with self._lock: self._agents[device_id] = XCUITestAgent( device_id, server_ip, server_port, keep_alive, retry, timeout) except: self.log.exception('start_agent') PortManager.del_port('agent', device_id) raise return self._agents[device_id]