Example #1
0
    def run_test(self, test_input, shard_name):
        self._batch_count += 1

        stop_when_done = False
        if 0 < self._batch_size <= self._batch_count:
            self._batch_count = 0
            stop_when_done = True

        test_timeout_sec = self._timeout(test_input)
        start = time.time()

        TaskPool.Process.queue.send(
            TaskPool.Task(
                handle_started_test,
                None,
                TaskPool.Process.name,
                test_input.test_name,
            ))

        result = self._run_test_with_or_without_timeout(
            test_input, test_timeout_sec, stop_when_done)
        result.shard_name = shard_name
        result.worker_name = TaskPool.Process.name
        result.total_run_time = time.time() - start
        result.test_number = self._num_tests
        self._num_tests += 1

        TaskPool.Process.queue.send(
            TaskPool.Task(
                handle_finished_test,
                None,
                TaskPool.Process.name,
                result,
            ))

        self._clean_up_after_test(test_input, result)
Example #2
0
    def run(self, name, *tests):
        binary_name = name.split('.')[0]
        remaining_tests = ['.'.join(test.split('.')[1:]) for test in tests]

        # Try to run the shard in a single process.
        while remaining_tests and not self._port.get_option('run_singly'):
            starting_length = len(remaining_tests)
            server_process = ServerProcess(
                self._port,
                binary_name,
                Runner.command_for_port(self._port, [
                    self._port._build_path(binary_name),
                    '--gtest_filter={}'.format(':'.join(remaining_tests))
                ]),
                env=self._port.environment_for_api_tests())

            try:
                deadline = time.time() + self._timeout
                last_test = None
                last_status = None
                stdout_buffer = ''

                server_process.start()
                while remaining_tests:
                    stdout = string_utils.decode(
                        server_process.read_stdout_line(deadline),
                        target_type=str)

                    # If we've triggered a timeout, we don't know which test caused it. Break out and run singly.
                    if stdout is None and server_process.timed_out:
                        break

                    if stdout is None and server_process.has_crashed():
                        # It's possible we crashed before printing anything.
                        if last_status == Runner.STATUS_PASSED:
                            last_test = None
                        else:
                            last_status = Runner.STATUS_CRASHED
                        break

                    assert stdout is not None
                    stdout_split = stdout.rstrip().split(' ')
                    if len(stdout_split) != 2 or not (
                            stdout_split[0].startswith('**')
                            and stdout_split[0].endswith('**')):
                        stdout_buffer += stdout
                        continue
                    if last_test is not None:
                        remaining_tests.remove(last_test)

                        for line in stdout_buffer.splitlines(False):
                            _log.error(line)
                        TaskPool.Process.queue.send(
                            TaskPool.Task(
                                report_result,
                                None,
                                TaskPool.Process.name,
                                '{}.{}'.format(binary_name, last_test),
                                last_status,
                                stdout_buffer,
                            ))
                        deadline = time.time() + self._timeout
                        stdout_buffer = ''

                    if '**PASS**' == stdout_split[0]:
                        last_status = Runner.STATUS_PASSED
                    else:
                        last_status = Runner.STATUS_FAILED
                    last_test = stdout_split[1]

                # We assume that stderr is only relevant if there is a crash (meaning we triggered an assert)
                if last_test:
                    remaining_tests.remove(last_test)
                    stdout_buffer += string_utils.decode(
                        server_process.pop_all_buffered_stdout(),
                        target_type=str)
                    stderr_buffer = string_utils.decode(
                        server_process.pop_all_buffered_stderr(),
                        target_type=str
                    ) if last_status == Runner.STATUS_CRASHED else ''
                    for line in (stdout_buffer +
                                 stderr_buffer).splitlines(keepends=False):
                        _log.error(line)

                    TaskPool.Process.queue.send(
                        TaskPool.Task(
                            report_result,
                            None,
                            TaskPool.Process.name,
                            '{}.{}'.format(binary_name, last_test),
                            last_status,
                            self._filter_noisy_output(stdout_buffer +
                                                      stderr_buffer),
                        ))

                if server_process.timed_out:
                    break

                # If we weren't able to determine the results for any tests, we need to run what remains singly.
                if starting_length == len(remaining_tests):
                    break
            finally:
                server_process.stop()

        # Now, just try and run the rest of the tests singly.
        for test in remaining_tests:
            self._run_single_test(binary_name, test)
Example #3
0
    def _run_single_test(self, binary_name, test):
        server_process = ServerProcess(
            self._port,
            binary_name,
            Runner.command_for_port(self._port, [
                self._port._build_path(binary_name),
                '--gtest_filter={}'.format(test)
            ]),
            env=self._port.environment_for_api_tests())

        status = Runner.STATUS_RUNNING
        if test.split('.')[1].startswith(
                'DISABLED_') and not self._port.get_option('force'):
            status = Runner.STATUS_DISABLED

        stdout_buffer = ''
        stderr_buffer = ''

        try:
            deadline = time.time() + self._timeout
            if status != Runner.STATUS_DISABLED:
                server_process.start()

            while status == Runner.STATUS_RUNNING:
                stdout_line, stderr_line = server_process.read_either_stdout_or_stderr_line(
                    deadline)
                if not stderr_line and not stdout_line:
                    break

                if stderr_line:
                    stderr_line = string_utils.decode(stderr_line,
                                                      target_type=str)
                    stderr_buffer += stderr_line
                    _log.error(stderr_line[:-1])
                if stdout_line:
                    stdout_line = string_utils.decode(stdout_line,
                                                      target_type=str)
                    if '**PASS**' in stdout_line:
                        status = Runner.STATUS_PASSED
                    elif '**FAIL**' in stdout_line:
                        status = Runner.STATUS_FAILED
                    else:
                        stdout_buffer += stdout_line
                        _log.error(stdout_line[:-1])

            if status == Runner.STATUS_DISABLED:
                pass
            elif server_process.timed_out:
                status = Runner.STATUS_TIMEOUT
            elif server_process.has_crashed():
                status = Runner.STATUS_CRASHED
            elif status == Runner.STATUS_RUNNING:
                status = Runner.STATUS_FAILED

        finally:
            remaining_stderr = string_utils.decode(
                server_process.pop_all_buffered_stderr(), target_type=str)
            remaining_stdout = string_utils.decode(
                server_process.pop_all_buffered_stdout(), target_type=str)
            for line in (remaining_stdout +
                         remaining_stderr).splitlines(False):
                _log.error(line)
            output_buffer = stderr_buffer + stdout_buffer + remaining_stderr + remaining_stdout
            server_process.stop()

        TaskPool.Process.queue.send(
            TaskPool.Task(
                report_result,
                None,
                TaskPool.Process.name,
                '{}.{}'.format(binary_name, test),
                status,
                self._filter_noisy_output(output_buffer),
            ))