1 # Copyright (C) 2017 Apple Inc. All rights reserved.
3 # Redistribution and use in source and binary forms, with or without
4 # modification, are permitted provided that the following conditions
6 # 1. Redistributions of source code must retain the above copyright
7 # notice, this list of conditions and the following disclaimer.
8 # 2. Redistributions in binary form must reproduce the above copyright
9 # notice, this list of conditions and the following disclaimer in the
10 # documentation and/or other materials provided with the distribution.
12 # THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' AND
13 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
14 # WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
15 # DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS BE LIABLE FOR
16 # ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
17 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
18 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
19 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
20 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
21 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 from webkitpy.common.timeout_context import Timeout
28 from webkitpy.port.server_process import ServerProcess
31 class SimulatorProcess(ServerProcess):
35 def __init__(self, pid, stdin, stdout, stderr, device):
40 self.returncode = None
45 return self.returncode
46 if self._device.executive.check_running_pid(self.pid):
47 self.returncode = None
50 return self.returncode
53 while not self.poll():
54 time.sleep(0.01) # In seconds
55 return self.returncode
57 # Python 2's implementation of makefile does not return a non-blocking file.
58 class NonBlockingFileFromSocket(object):
60 def __init__(self, sock, type):
62 self._file = os.fdopen(sock.fileno(), type, 0)
63 ServerProcess._set_file_nonblocking(self._file)
65 def __getattr__(self, name):
66 return getattr(self._file, name)
69 result = self._file.close()
73 def __init__(self, port_obj, name, cmd, env=None, universal_newlines=False, treat_no_data_as_crash=False, target_host=None):
74 env['PORT'] = str(target_host.listening_port()) # The target_host should be a device.
75 super(SimulatorProcess, self).__init__(port_obj, name, cmd, env, universal_newlines, treat_no_data_as_crash, target_host)
77 self._bundle_id = port_obj.app_identifier_from_bundle(cmd[0])
79 def process_name(self):
80 return self._port.app_executable_from_bundle(self._cmd[0])
83 def _accept_connection_create_file(server, type):
84 connection, address = server.accept()
85 assert address[0] == '127.0.0.1'
86 return SimulatorProcess.NonBlockingFileFromSocket(connection, type)
90 raise ValueError('{} already running'.format(self._name))
93 # Each device has a listening socket intitilaized during the port's setup_test_run.
94 # 3 client connections will be accepted for stdin, stdout and stderr in that order.
95 self._target_host.listening_socket.listen(3)
96 self._pid = self._target_host.launch_app(self._bundle_id, self._cmd[1:], env=self._env)
98 with Timeout(6, RuntimeError('Timed out waiting for pid {} to connect at port {}'.format(self._pid, self._target_host.listening_port()))):
103 # This order matches the client side connections in Tools/TestRunnerShared/IOSLayoutTestCommunication.cpp setUpIOSLayoutTestCommunication()
104 stdin = SimulatorProcess._accept_connection_create_file(self._target_host.listening_socket, 'w')
105 stdout = SimulatorProcess._accept_connection_create_file(self._target_host.listening_socket, 'rb')
106 stderr = SimulatorProcess._accept_connection_create_file(self._target_host.listening_socket, 'rb')
108 # We set self._proc as _reset() and _kill() depend on it.
109 self._proc = SimulatorProcess.Popen(self._pid, stdin, stdout, stderr, self._target_host)
110 if self._proc.poll() is not None:
112 raise Exception('App {} with pid {} crashed before stdin could be attached'.format(os.path.basename(self._cmd[0]), self._pid))
117 self._proc = SimulatorProcess.Popen(self._pid, stdin, stdout, stderr, self._target_host)
119 def stop(self, timeout_secs=3.0):
120 # Only bother to check for leaks or stderr if the process is still running.
121 if self.poll() is None:
122 self._port.check_for_leaks(self.process_name(), self.pid())
124 if self._proc and self._proc.pid:
125 self._target_host.executive.kill_process(self._proc.pid)
127 return self._wait_for_stop(timeout_secs)