示例#1
0
    def testCollisionDetection(self):
        ONE_YEAR = 365 * 24 * 60 * 60

        def _mock_callback(signum, frame):
            del signum, frame  # unused.

        flag = False
        with self.assertRaises(common_util.TimeoutCollisionError):
            with common_util.TimeoutScope(seconds=ONE_YEAR, error_name=''):
                flag = True
                signal.signal(signal.SIGALRM, _mock_callback)
        self.assertTrue(flag)
        self.assertEquals(0, signal.alarm(0))

        flag = False
        with self.assertRaises(common_util.TimeoutCollisionError):
            with common_util.TimeoutScope(seconds=ONE_YEAR, error_name=''):
                flag = True
                with common_util.TimeoutScope(seconds=ONE_YEAR, error_name=''):
                    self.fail()
        self.assertTrue(flag)
        self.assertEquals(0, signal.alarm(0))

        signal.alarm(ONE_YEAR)
        with self.assertRaises(common_util.TimeoutCollisionError):
            with common_util.TimeoutScope(seconds=ONE_YEAR, error_name=''):
                self.fail()
        self.assertEquals(0, signal.alarm(0))
示例#2
0
    def testTimeoutRaise(self):
        self.assertEquals(0, signal.alarm(0))

        with self.assertRaisesRegexp(common_util.TimeoutError, 'hello'):
            with common_util.TimeoutScope(seconds=1, error_name='hello'):
                signal.pause()
                self.fail()
        self.assertEquals(0, signal.alarm(0))

        with self.assertRaisesRegexp(common_util.TimeoutError, 'world'):
            with common_util.TimeoutScope(seconds=1, error_name='world'):
                time.sleep(2)
        self.assertEquals(0, signal.alarm(0))
示例#3
0
    def _RunNavigation(self, clear_cache, repeat_id=None):
        """Run a page navigation to the given URL.

    Args:
      clear_cache: Whether if the cache should be cleared before navigation.
      repeat_id: Id of the run in the output directory. If it is None, then no
        trace or video will be saved.
    """
        run_path = None
        if repeat_id is not None:
            run_path = os.path.join(self.output_dir, str(repeat_id))
            if not os.path.isdir(run_path):
                os.makedirs(run_path)
        self._chrome_ctl.SetNetworkEmulation(
            self._GetEmulatorNetworkCondition('browser'))
        categories = _TRACING_CATEGORIES
        if self.record_memory_dumps:
            categories += [MEMORY_DUMP_CATEGORY]
        if self.record_first_meaningful_paint:
            categories += TTFMP_ADDITIONAL_CATEGORIES
        stop_delay_multiplier = 0
        if self.wpr_record or self.cache_operation == CacheOperation.SAVE:
            stop_delay_multiplier = self._STOP_DELAY_MULTIPLIER
        # TODO(gabadie): add a way to avoid recording a trace.
        with common_util.TimeoutScope(self._ABORT_RUN_TIMEOUT_SECONDS,
                                      'Sandwich run overdue.'):
            with self._chrome_ctl.Open() as connection:
                if clear_cache:
                    connection.ClearCache()

                # Binds all parameters of RecordUrlNavigation() to avoid repetition.
                def RecordTrace():
                    return loading_trace.LoadingTrace.RecordUrlNavigation(
                        url=self.url,
                        connection=connection,
                        chrome_metadata=self._chrome_ctl.ChromeMetadata(),
                        categories=categories,
                        timeout_seconds=_DEVTOOLS_TIMEOUT,
                        stop_delay_multiplier=stop_delay_multiplier)

                if run_path is not None and self.record_video:
                    device = self._chrome_ctl.GetDevice()
                    if device is None:
                        raise RuntimeError(
                            'Can only record video on a remote device.')
                    video_recording_path = os.path.join(
                        run_path, VIDEO_FILENAME)
                    with device_setup.RemoteSpeedIndexRecorder(
                            device, connection, video_recording_path):
                        trace = RecordTrace()
                else:
                    trace = RecordTrace()
                for event in trace.request_track.GetEvents():
                    if event.failed:
                        logging.warning('request to %s failed: %s', event.url,
                                        event.error_text)
                if not trace.tracing_track.HasLoadingSucceeded():
                    raise SandwichRunnerError('Page load has failed.')
        if run_path is not None:
            trace_path = os.path.join(run_path, TRACE_FILENAME)
            trace.ToJsonFile(trace_path)