def stop_agent(self, device_id): '''关闭Agent ''' if device_id in self._agents: self._agents[device_id].stop() PortManager.del_port('agent', device_id) self._agents.pop(device_id)
def stop_all_agents(cls): '''关闭全部Agent ''' for device_id in cls._agents: cls._agents[device_id].stop() PortManager.del_port('agent', device_id) logger.get_logger("xctest_%s" % device_id).info('stop_agent') cls._agents = {}
def _modify_wi_port(self, udid): port = PortManager.get_port('web', udid) if self.xcode_version.startswith('9'): wi_plist_path = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/Library/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/LaunchDaemons/com.apple.webinspectord.plist' else: wi_plist_path = '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/LaunchDaemons/com.apple.webinspectord.plist' cmd = ['/usr/libexec/PlistBuddy', '-c', 'Set :Sockets:com.apple.webinspectord.socket:SockServiceName %s' % port, wi_plist_path] subprocess.call(cmd)
def _get_webinspector_socket_below_xcode93(self, udid): sock = socket.socket(socket.AF_INET6, socket.SOCK_STREAM) wi_port = PortManager.get_port('web', udid) if wi_port is None: wi_port = 27753 self.logger.info('webport: %s' % wi_port) sock.connect(('::1', wi_port)) return sock
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 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 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]
class XCUITestAgentManager(object): '''XCUITest Agent Manager 单例类 ''' __metaclass__ = Singleton _agents = {} # 维护Agents的实例,key为设备的udid,value为Agent实例 _port_manager = PortManager() # 维护映射端口字典 _lock = threading.Lock() @classmethod def stop_all_agents(cls): '''关闭全部Agent ''' for device_id in cls._agents: cls._agents[device_id].stop() cls._port_manager.del_port('agent', device_id) logger.get_logger("xctest_%s" % device_id).info('stop_agent') cls._agents = {} def __init__(self, *args, **kwargs): pass 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: self._port_manager.set_port('agent', device_id, server_port) server_port = self._port_manager.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') self._port_manager.del_port('agent', device_id) raise return self._agents[device_id] def restart_agent(self, device_id): '''重启Agent ''' if device_id in self._agents: self._agents[device_id].stop() self._agents[device_id].start() else: self.start_agent(device_id) def stop_agent(self, device_id): '''关闭Agent ''' if device_id in self._agents: self._agents[device_id].stop() self._port_manager.del_port('agent', device_id) self._agents.pop(device_id) def get_agent(self, device_id): '''根据udid获取Agent对象 :param device_id: 设备udid :type device_id: str :returns: XCUITestAgent ''' if device_id in self._agents: return self._agents[device_id] else: return self.start_agent(device_id) @property def agents(self): '''获取Agent列表 :returns: list ''' return self._agents.values() def has_agent(self, device_id): '''查询设备是否存在对应的agent :returns: boolean ''' return device_id in self._agents