Exemple #1
0
    def run(self, test):
        session_start = time.monotonic()
        cases = tb.get_test_cases([test])
        setup = tb.get_test_cases_setup(cases)
        bootstrap_time_taken = 0
        tests_time_taken = 0
        result = None
        cluster = None
        conn = None

        try:
            if setup:
                if self.verbosity >= 1:
                    self._echo(
                        'Populating test databases... ',
                        fg='white',
                        nl=False,
                    )

                if self.verbosity > 1:
                    self._echo(
                        '\n -> Bootstrapping EdgeDB instance...',
                        fg='white',
                        nl=False,
                    )

                cluster = tb._init_cluster(cleanup_atexit=False)

                if self.verbosity > 1:
                    self._echo(' OK')

                conn = cluster.get_connect_args()
                tb.setup_test_cases(
                    cases,
                    conn,
                    self.num_workers,
                    verbose=self.verbosity > 1,
                )

                os.environ.update({
                    'EDGEDB_TEST_CASES_SET_UP': "1"
                })

                bootstrap_time_taken = time.monotonic() - session_start

                if self.verbosity >= 1:
                    self._echo(' OK')

            start = time.monotonic()

            all_tests = list(itertools.chain.from_iterable(
                tests for tests in cases.values()))

            if self.num_workers > 1:
                suite = ParallelTestSuite(
                    self._sort_tests(cases),
                    conn,
                    self.num_workers)
            else:
                suite = SequentialTestSuite(
                    self._sort_tests(cases),
                    conn
                )

            result = ParallelTextTestResult(
                stream=self.stream, verbosity=self.verbosity,
                warnings=self.warnings, failfast=self.failfast,
                output_format=self.output_format,
                tests=all_tests, suite=suite)
            unittest.signals.registerResult(result)

            self._echo()
            suite.run(result)

            tests_time_taken = time.monotonic() - start

        except KeyboardInterrupt:
            raise

        finally:
            if self.verbosity == 1:
                self._echo()

            if setup:
                self._echo()
                self._echo('Shutting down test cluster... ', nl=False)
                tb._shutdown_cluster(cluster, destroy=True)
                self._echo('OK.')

        if result is not None:
            self._render_result(
                result, bootstrap_time_taken, tests_time_taken)

        return result
Exemple #2
0
    def run(self, test, selected_shard, total_shards, running_times_log_file):
        session_start = time.monotonic()
        cases = tb.get_test_cases([test])
        stats = {}
        if running_times_log_file:
            running_times_log_file.seek(0)
            stats = {
                k: (float(v), int(c))
                for k, v, c in csv.reader(running_times_log_file)
            }
        cases = tb.get_cases_by_shard(
            cases,
            selected_shard,
            total_shards,
            self.verbosity,
            stats,
        )
        setup = tb.get_test_cases_setup(cases)
        bootstrap_time_taken = 0
        tests_time_taken = 0
        result = None
        cluster = None
        conn = None
        setup_stats = []

        try:
            if setup:
                if self.verbosity >= 1:
                    self._echo(
                        'Populating test databases... ',
                        fg='white',
                        nl=False,
                    )

                if self.verbosity > 1:
                    self._echo(
                        '\n -> Bootstrapping EdgeDB instance...',
                        fg='white',
                        nl=False,
                    )

                cluster = tb._init_cluster(postgres_dsn=self.postgres_dsn,
                                           cleanup_atexit=False)

                if self.verbosity > 1:
                    self._echo(' OK')

                conn = cluster.get_connect_args()
                setup_stats = tb.setup_test_cases(
                    cases,
                    conn,
                    self.num_workers,
                    verbose=self.verbosity > 1,
                )

                os.environ.update({'EDGEDB_TEST_CASES_SET_UP': "1"})

                bootstrap_time_taken = time.monotonic() - session_start

                if self.verbosity >= 1:
                    self._echo('OK')

            start = time.monotonic()

            all_tests = list(
                itertools.chain.from_iterable(tests
                                              for tests in cases.values()))

            if self.num_workers > 1:
                suite = ParallelTestSuite(
                    self._sort_tests(cases),
                    conn,
                    self.num_workers,
                    self.postgres_dsn,
                )
            else:
                suite = SequentialTestSuite(
                    self._sort_tests(cases),
                    conn,
                    self.postgres_dsn,
                )

            result = ParallelTextTestResult(stream=self.stream,
                                            verbosity=self.verbosity,
                                            warnings=self.warnings,
                                            failfast=self.failfast,
                                            output_format=self.output_format,
                                            tests=all_tests,
                                            suite=suite)
            unittest.signals.registerResult(result)

            self._echo()
            suite.run(result)

            if running_times_log_file:
                for test, stat in result.test_stats + setup_stats:
                    name = str(test)
                    t = stat['running-time']
                    at, c = stats.get(name, (0, 0))
                    stats[name] = (at + (t - at) / (c + 1), c + 1)
                running_times_log_file.seek(0)
                running_times_log_file.truncate()
                writer = csv.writer(running_times_log_file)
                for k, v in stats.items():
                    writer.writerow((k, ) + v)
            tests_time_taken = time.monotonic() - start

        except KeyboardInterrupt:
            raise

        finally:
            if self.verbosity == 1:
                self._echo()

            if setup:
                self._echo()
                self._echo('Shutting down test cluster... ', nl=False)
                tb._shutdown_cluster(cluster, destroy=True)
                self._echo('OK.')

        if result is not None:
            self._render_result(result, bootstrap_time_taken, tests_time_taken)

        return result