def launch_app(self, bundle_id, args, env=None, timeout=300): environment_to_use = {} SIMCTL_ENV_PREFIX = 'SIMCTL_CHILD_' for value in (env or {}): if not value.startswith(SIMCTL_ENV_PREFIX): environment_to_use[SIMCTL_ENV_PREFIX + value] = env[value] else: environment_to_use[value] = env[value] # FIXME: This is a workaround for <rdar://problem/30172453>. def _log_debug_error(error): _log.debug(error.message_with_output()) output = None with Timeout(timeout, RuntimeError(u'Timed out waiting for process to open {} on {}'.format(bundle_id, self.udid))): while True: output = self.executive.run_command( ['xcrun', 'simctl', 'launch', self.udid, bundle_id] + args, env=environment_to_use, error_handler=_log_debug_error, ) match = re.match(r'(?P<bundle>[^:]+): (?P<pid>\d+)\n', output) # FIXME: We shouldn't need to check the PID <rdar://problem/31154075>. if match and self.executive.check_running_pid(int(match.group('pid'))): break if match: _log.debug(u'simctl launch reported pid {}, but this process is not running'.format(match.group('pid'))) else: _log.debug('simctl launch did not report a pid') if match.group('bundle') != bundle_id: raise RuntimeError(u'Failed to find process id for {}: {}'.format(bundle_id, output)) _log.debug(u'Returning pid {} of launched process'.format(match.group('pid'))) return int(match.group('pid'))
def wait_until_device_is_booted(udid, timeout_seconds=60 * 15): Simulator.wait_until_device_is_in_state(udid, Simulator.DeviceState.BOOTED, timeout_seconds) with Timeout(seconds=timeout_seconds): while True: try: state = subprocess.check_output([ 'xcrun', 'simctl', 'spawn', udid, 'launchctl', 'print', 'system' ]).strip() _log.debug('xcrun simctl spawn %s', udid) if re.search("A[\s]+com.apple.springboard.services", state): return except subprocess.CalledProcessError: if Simulator.device_state( udid) != Simulator.DeviceState.BOOTED: raise RuntimeError( 'Simuator device quit unexpectedly.') _log.warn( "Error in checking Simulator boot status. Will retry in 1 second." ) time.sleep(1)
def _run_google_test(self, test_program, subtest): command = [test_program, '--gtest_filter=%s' % (subtest)] timeout = self._options.timeout if self._expectations.is_slow(os.path.basename(test_program), subtest): timeout *= 10 pid, fd = os.forkpty() if pid == 0: os.execvpe(command[0], command, self._test_env) sys.exit(0) with Timeout(timeout): try: common.parse_output_lines(fd, sys.stdout.write) status = self._waitpid(pid) except RuntimeError: self._kill_process(pid) sys.stdout.write("**TIMEOUT** %s\n" % subtest) sys.stdout.flush() return {subtest: "TIMEOUT"} if status == -SIGSEGV: sys.stdout.write("**CRASH** %s\n" % subtest) sys.stdout.flush() return {subtest: "CRASH"} if status != 0: return {subtest: "FAIL"} return {subtest: "PASS"}
def _start(self): if self._proc: raise ValueError('{} already running'.format(self._name)) self._reset() # Each device has a listening socket intitilaized during the port's setup_test_run. # 3 client connections will be accepted for stdin, stdout and stderr in that order. self._target_host.listening_socket.listen(3) self._pid = self._target_host.launch_app(self._bundle_id, self._cmd[1:], env=self._env) with Timeout(6, RuntimeError('Timed out waiting for pid {} to connect at port {}'.format(self._pid, self._target_host.listening_port()))): stdin = None stdout = None stderr = None try: # This order matches the client side connections in Tools/TestRunnerShared/IOSLayoutTestCommunication.cpp setUpIOSLayoutTestCommunication() stdin = SimulatorProcess._accept_connection_create_file(self._target_host.listening_socket, 'w') stdout = SimulatorProcess._accept_connection_create_file(self._target_host.listening_socket, 'rb') stderr = SimulatorProcess._accept_connection_create_file(self._target_host.listening_socket, 'rb') except: # We set self._proc as _reset() and _kill() depend on it. self._proc = SimulatorProcess.Popen(self._pid, stdin, stdout, stderr, self._target_host) if self._proc.poll() is not None: self._reset() raise Exception('App {} with pid {} crashed before stdin could be attached'.format(os.path.basename(self._cmd[0]), self._pid)) self._kill() self._reset() raise self._proc = SimulatorProcess.Popen(self._pid, stdin, stdout, stderr, self._target_host)
def test_timeout_data(self): tmp = Timeout(1) self.assertEqual(None, tmp.data) with tmp: self.assertNotEqual(None, tmp.data) self.assertEqual(threading.current_thread().ident, tmp.data.thread_id) self.assertGreater(time.time() + 1, tmp.data.alarm_time) self.assertEqual(None, tmp.data)
def wait_until_device_is_in_state(udid, wait_until_state, timeout_seconds=60 * 15): _log.debug('waiting for device %s to enter state %s with timeout %s', udid, Simulator.device_state_description(wait_until_state), timeout_seconds) with Timeout(seconds=timeout_seconds): device_state = Simulator.device_state(udid) while (device_state != wait_until_state): device_state = Simulator.device_state(udid) _log.debug(' device state %s', Simulator.device_state_description(device_state)) time.sleep(0.5) end_state = Simulator.device_state(udid) if (end_state != wait_until_state): raise RuntimeError('Timed out waiting for simulator device to enter state {0}; current state is {1}'.format(Simulator.device_state_description(wait_until_state), Simulator.device_state_description(end_state)))
def test_nested_outer_precedence(self): tmp_outer = Timeout(1) tmp_inner = Timeout(2) with tmp_outer: self.assertEqual(tmp_outer.data, Timeout.current()) with tmp_inner: self.assertEqual(tmp_outer.data, Timeout.current()) self.assertEqual(tmp_outer.data, Timeout.current()) self.assertEqual(None, Timeout.current())
def _run_one_test(self, web_root, test_file): result = None try: self._http_server_driver.serve(web_root) url = urlparse.urljoin(self._http_server_driver.base_url(), self._plan_name + '/' + test_file) self._browser_driver.launch_url(url, self._plan['options'], self._build_dir) with Timeout(self._plan['timeout']): result = self._get_result(url) finally: self._browser_driver.close_browsers() self._http_server_driver.kill_server() return json.loads(result)
def _run_one_test(self, web_root, test_file): result = None try: self._http_server_driver.serve(web_root) url = urljoin(self._http_server_driver.base_url(), self._plan_name + '/' + test_file) self._browser_driver.launch_url(url, self._plan['options'], self._build_dir, self._browser_path) with Timeout(self._plan['timeout']): result = self._get_result(url) except Exception as error: self._browser_driver.diagnose_test_failure(self._diagnose_dir, error) raise error finally: self._browser_driver.close_browsers() self._http_server_driver.kill_server() return json.loads(result)
def should_timeout(): with Timeout(1): with Timeout(3, Exception("This shouldn't be raised")): time.sleep(2)
def should_timeout(): with Timeout(1, Exception('This should be raised')): time.sleep(2)
def should_timeout(): with Timeout(1): time.sleep(2)
def test_no_timeout(self): with Timeout(2): time.sleep(1)
def test_current_timeout(self): self.assertEqual(None, Timeout.current()) with Timeout(1) as tmp: self.assertEqual(tmp.data, Timeout.current()) self.assertEqual(None, Timeout.current())