def RestartHttpServerForwarderIfNecessary(self): """Restarts the forwarder if it's not open.""" # Checks to see if the http server port is being used. If not forwards the # request. # TODO(dtrainor): This is not always reliable because sometimes the port # will be left open even after the forwarder has been killed. if not ports.IsDevicePortUsed(self.adb, self._forwarder_device_port): self.StartForwarderForHttpServer()
def run(self): logging.info('Start running the thread!') self.wait_event.clear() self._GenerateCommandLineArguments() command = constants.CHROME_DIR if self.arguments['server-type'] == 'sync': command = [ os.path.join(command, 'sync', 'tools', 'testserver', 'sync_testserver.py') ] + self.command_line else: command = [ os.path.join(command, 'net', 'tools', 'testserver', 'testserver.py') ] + self.command_line logging.info('Running: %s', command) self.process = subprocess.Popen(command) if self.process: if self.pipe_out: self.is_ready = self._WaitToStartAndGetPortFromTestServer() else: self.is_ready = _CheckPortStatus(self.host_port, True) if self.is_ready: self._test_server_forwarder = Forwarder(self.adb, self.build_type) self._test_server_forwarder.Run([(0, self.host_port)], self.tool, '127.0.0.1') # Check whether the forwarder is ready on the device. self.is_ready = False device_port = self._test_server_forwarder.DevicePortForHostPort( self.host_port) if device_port: for timeout in range(1, 5): if ports.IsDevicePortUsed(self.adb, device_port, 'LISTEN'): self.is_ready = True self.forwarder_device_port = device_port break time.sleep(timeout) # Wake up the request handler thread. self.ready_event.set() # Keep thread running until Stop() gets called. while not self.stop_flag: time.sleep(1) if self.process.poll() is None: self.process.kill() if self._test_server_forwarder: self._test_server_forwarder.Close() self.process = None self.is_ready = False if self.pipe_out: os.close(self.pipe_in) os.close(self.pipe_out) self.pipe_in = None self.pipe_out = None logging.info('Test-server has died.') self.wait_event.set()
def _CheckDevicePortStatus(adb, port): """Returns whether the provided port is used.""" return _WaitUntil(lambda: ports.IsDevicePortUsed(adb, port))