def testRunStoryAndProcessErrorIfNeeded_tryTimeout_finallyException(self): root_mock = self._CreateErrorProcessingMock( method_exceptions={ 'state.RunStory': exceptions.TimeoutException('foo'), 'state.DidRunStory': Exception('bar') }) story_runner._RunStoryAndProcessErrorIfNeeded(root_mock.story, root_mock.results, root_mock.state, root_mock.test, self.finder_options) self.assertEquals(root_mock.method_calls, [ mock.call.results.CreateArtifact('logs.txt'), mock.call.test.WillRunStory(root_mock.state.platform, root_mock.story), mock.call.state.WillRunStory(root_mock.story), mock.call.state.CanRunStory(root_mock.story), mock.call.state.RunStory(root_mock.results), mock.call.state.DumpStateUponStoryRunFailure(root_mock.results), mock.call.results.Fail( 'Exception raised running %s' % root_mock.story.name), mock.call.test.DidRunStory(root_mock.state.platform, root_mock.results), mock.call.state.DidRunStory(root_mock.results), ])
def _ConvertExceptionFromInspectorWebsocket(self, error): """Converts an Exception from inspector_websocket. This method always raises a Telemetry exception. It appends debugging information. The exact exception raised depends on |error|. Args: error: An instance of socket.error or websocket.WebSocketException. Raises: exceptions.TimeoutException: A timeout occurred. exceptions.DevtoolsTargetCrashException: On any other error, the most likely explanation is that the devtool's target crashed. """ if isinstance(error, websocket.WebSocketTimeoutException): new_error = exceptions.TimeoutException() new_error.AddDebuggingMessage( exceptions.AppCrashException(self.app, 'The app is probably crashed:\n')) else: new_error = exceptions.DevtoolsTargetCrashException(self.app) original_error_msg = 'Original exception:\n' + str(error) new_error.AddDebuggingMessage(original_error_msg) self._AddDebuggingInformation(new_error) raise new_error, None, sys.exc_info()[2]
def WaitForJavaScriptExpression(self, expr, timeout, dump_page_state_on_timeout=True): """Waits for the given JavaScript expression to be True. This method is robust against any given Evaluation timing out. Args: expr: The expression to evaluate. timeout: The number of seconds to wait for the expression to be True. dump_page_state_on_timeout: Whether to provide additional information on the page state if a TimeoutException is thrown. Raises: exceptions.TimeoutException: On a timeout. exceptions.Error: See EvaluateJavaScript() for a detailed list of possible exceptions. """ def IsJavaScriptExpressionTrue(): try: return bool(self.EvaluateJavaScript(expr)) except exceptions.TimeoutException: # If the main thread is busy for longer than Evaluate's timeout, we # may time out here early. Instead, we want to wait for the full # timeout of this method. return False try: util.WaitFor(IsJavaScriptExpressionTrue, timeout) except exceptions.TimeoutException as e: if not dump_page_state_on_timeout: raise # Try to make timeouts a little more actionable by dumping |this|. raise exceptions.TimeoutException(e.message + self.EvaluateJavaScript(""" (function() { var error = '\\n\\nJavaScript |this|:\\n'; for (name in this) { try { error += '\\t' + name + ': ' + this[name] + '\\n'; } catch (e) { error += '\\t' + name + ': ???\\n'; } } if (window && window.document) { error += '\\n\\nJavaScript window.document:\\n'; for (name in window.document) { try { error += '\\t' + name + ': ' + window.document[name] + '\\n'; } catch (e) { error += '\\t' + name + ': ???\\n'; } } } return error; })(); """))
def testRunStoryAndProcessErrorIfNeeded_tryTimeout(self): root_mock = self._CreateErrorProcessingMock(method_exceptions={ 'state.WillRunStory': exceptions.TimeoutException('foo') }) story_runner._RunStoryAndProcessErrorIfNeeded( root_mock.story, root_mock.results, root_mock.state, root_mock.test) self.assertEquals(root_mock.method_calls, [ mock.call.test.WillRunStory(root_mock.state.platform), mock.call.state.WillRunStory(root_mock.story), mock.call.state.DumpStateUponFailure(root_mock.story, root_mock.results), mock.call.results.AddValue(FailureValueMatcher('foo')), mock.call.state.DidRunStory(root_mock.results), mock.call.test.DidRunStory(root_mock.state.platform) ])
def WaitFor(condition, timeout): """Waits for up to |timeout| secs for the function |condition| to return True. Polling frequency is (elapsed_time / 10), with a min of .1s and max of 5s. Returns: Result of |condition| function (if present). """ min_poll_interval = 0.1 max_poll_interval = 5 output_interval = 300 def GetConditionString(): if condition.__name__ == '<lambda>': try: return inspect.getsource(condition).strip() except IOError: pass return condition.__name__ start_time = time.time() last_output_time = start_time while True: res = condition() if res: return res now = time.time() elapsed_time = now - start_time last_output_elapsed_time = now - last_output_time if elapsed_time > timeout: raise exceptions.TimeoutException( 'Timed out while waiting %ds for %s.' % (timeout, GetConditionString())) if last_output_elapsed_time > output_interval: logging.info('Continuing to wait %ds for %s. Elapsed: %ds.', timeout, GetConditionString(), elapsed_time) last_output_time = time.time() poll_interval = min(max(elapsed_time / 10., min_poll_interval), max_poll_interval) time.sleep(poll_interval)
def WaitForJavaScriptExpression(self, expr, timeout): """Waits for the given JavaScript expression to be True. This method is robust against any given Evaluation timing out. Args: expr: The expression to evaluate. timeout: The number of seconds to wait for the expression to be True. Raises: exceptions.TimeoutException: On a timeout. exceptions.Error: See EvaluateJavaScript() for a detailed list of possible exceptions. """ def IsJavaScriptExpressionTrue(): try: return bool(self.EvaluateJavaScript(expr)) except exceptions.TimeoutException: # If the main thread is busy for longer than Evaluate's timeout, we # may time out here early. Instead, we want to wait for the full # timeout of this method. return False try: util.WaitFor(IsJavaScriptExpressionTrue, timeout) except exceptions.TimeoutException as e: # Try to make timeouts a little more actionable by dumping console output. debug_message = None try: debug_message = ( 'Console output:\n%s' % self._inspector_backend.GetCurrentConsoleOutputBuffer()) except Exception as e: debug_message = ( 'Exception thrown when trying to capture console output: %s' % repr(e)) raise exceptions.TimeoutException( e.message + '\n' + debug_message)
def failed_run(): logging.warning('This will fail gracefully') raise exceptions.TimeoutException('karma!')