#ifdef UNSAFE_BUFFERS_BUILD
#pragma allow_unsafe_buffers
#endif
#include "base/test/launcher/test_launcher.h"
#include <stdio.h>
#include <algorithm>
#include <map>
#include <random>
#include <string_view>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include "base/at_exit.h"
#include "base/clang_profiling_buildflags.h"
#include "base/command_line.h"
#include "base/compiler_specific.h"
#include "base/containers/adapters.h"
#include "base/containers/contains.h"
#include "base/environment.h"
#include "base/files/file_enumerator.h"
#include "base/files/file_path.h"
#include "base/files/file_util.h"
#include "base/files/scoped_file.h"
#include "base/files/scoped_temp_dir.h"
#include "base/format_macros.h"
#include "base/functional/bind.h"
#include "base/hash/hash.h"
#include "base/lazy_instance.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/raw_ref.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "base/process/kill.h"
#include "base/process/launch.h"
#include "base/run_loop.h"
#include "base/strings/pattern.h"
#include "base/strings/strcat.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/stringize_macros.h"
#include "base/strings/stringprintf.h"
#include "base/strings/utf_string_conversions.h"
#include "base/system/sys_info.h"
#include "base/task/post_job.h"
#include "base/task/single_thread_task_runner.h"
#include "base/task/thread_pool.h"
#include "base/task/thread_pool/thread_pool_instance.h"
#include "base/test/gtest_util.h"
#include "base/test/gtest_xml_util.h"
#include "base/test/launcher/test_launcher_tracer.h"
#include "base/test/launcher/test_results_tracker.h"
#include "base/test/scoped_logging_settings.h"
#include "base/test/test_file_util.h"
#include "base/test/test_switches.h"
#include "base/test/test_timeouts.h"
#include "base/threading/platform_thread.h"
#include "base/threading/thread_restrictions.h"
#include "base/time/time.h"
#include "build/build_config.h"
#include "testing/gtest/include/gtest/gtest.h"
#if BUILDFLAG(IS_POSIX)
#include <fcntl.h>
#include "base/files/file_descriptor_watcher_posix.h"
#endif
#if BUILDFLAG(IS_APPLE)
#include "base/apple/scoped_nsautorelease_pool.h"
#endif
#if BUILDFLAG(IS_WIN)
#include <windows.h>
#include "base/strings/string_util_win.h"
#undef GetCommandLine
#endif
#if BUILDFLAG(IS_FUCHSIA)
#include <lib/fdio/namespace.h>
#include <lib/zx/job.h>
#include <lib/zx/time.h>
#include "base/atomic_sequence_num.h"
#include "base/fuchsia/default_job.h"
#include "base/fuchsia/file_utils.h"
#include "base/fuchsia/fuchsia_logging.h"
#endif
#if BUILDFLAG(IS_IOS)
#include "base/path_service.h"
#endif
namespace base {
using ::operator<<;
const char kTestTotalShards[] = "GTEST_TOTAL_SHARDS";
const char kTestShardIndex[] = "GTEST_SHARD_INDEX";
const char kPreTestPrefix[] = "PRE_";
const char kDisabledTestPrefix[] = "DISABLED_";
ResultWatcher::ResultWatcher(FilePath result_file, size_t num_tests)
: result_file_(std::move(result_file)), num_tests_(num_tests) {}
bool ResultWatcher::PollUntilDone(TimeDelta timeout_per_test) {
CHECK(timeout_per_test.is_positive());
TimeTicks batch_deadline = TimeTicks::Now() + num_tests_ * timeout_per_test;
TimeDelta time_to_next_check = timeout_per_test;
do {
if (WaitWithTimeout(time_to_next_check)) {
return true;
}
time_to_next_check = PollOnce(timeout_per_test);
} while (TimeTicks::Now() < batch_deadline &&
time_to_next_check.is_positive());
return WaitWithTimeout(TestTimeouts::tiny_timeout());
}
TimeDelta ResultWatcher::PollOnce(TimeDelta timeout_per_test) {
std::vector<TestResult> test_results;
if (!ProcessGTestOutput(result_file_, &test_results, nullptr)) {
return TestTimeouts::tiny_timeout();
}
Time latest_completion = LatestCompletionTimestamp(test_results);
if (latest_completion.is_null()) {
return TimeDelta();
}
TimeDelta time_since_latest_completion = Time::Now() - latest_completion;
if (time_since_latest_completion.is_negative() ||
time_since_latest_completion > kDaylightSavingsThreshold) {
return timeout_per_test;
}
return timeout_per_test - time_since_latest_completion;
}
Time ResultWatcher::LatestCompletionTimestamp(
const std::vector<TestResult>& test_results) {
CHECK_LE(test_results.size(), num_tests_);
for (const TestResult& result : Reversed(test_results)) {
if (result.completed()) {
Time test_start = result.timestamp.value_or(Time());
return test_start + result.elapsed_time;
}
}
return Time();
}
class ProcessResultWatcher : public ResultWatcher {
public:
ProcessResultWatcher(FilePath result_file, size_t num_tests, Process& process)
: ResultWatcher(result_file, num_tests), process_(process) {}
int GetExitCode();
bool WaitWithTimeout(TimeDelta timeout) override;
private:
const raw_ref<Process> process_;
int exit_code_ = -1;
};
int ProcessResultWatcher::GetExitCode() {
return exit_code_;
}
bool ProcessResultWatcher::WaitWithTimeout(TimeDelta timeout) {
return process_->WaitForExitWithTimeout(timeout, &exit_code_);
}
namespace {
const char kUnreliableResultsTag[] = "UNRELIABLE_RESULTS";
constexpr TimeDelta kOutputTimeout = Seconds(15);
const size_t kOutputSnippetLinesLimit = 5000;
const size_t kOutputSnippetBytesLimit = 500 * 1024;
const uint32_t kRandomSeedUpperBound = 100000;
Lock* GetLiveProcessesLock() {
static auto* lock = new Lock;
return lock;
}
std::map<ProcessHandle, CommandLine>* GetLiveProcesses() {
static auto* map = new std::map<ProcessHandle, CommandLine>;
return map;
}
TestLauncherTracer* GetTestLauncherTracer() {
static auto* tracer = new TestLauncherTracer;
return tracer;
}
#if BUILDFLAG(IS_FUCHSIA)
zx_status_t WaitForJobExit(const zx::job& job) {
zx::time deadline =
zx::deadline_after(zx::duration(kOutputTimeout.ToZxDuration()));
zx_signals_t to_wait_for = ZX_JOB_NO_JOBS | ZX_JOB_NO_PROCESSES;
while (to_wait_for) {
zx_signals_t observed = 0;
zx_status_t status = job.wait_one(to_wait_for, deadline, &observed);
if (status != ZX_OK) {
return status;
}
to_wait_for &= ~observed;
}
return ZX_OK;
}
#endif
#if BUILDFLAG(IS_POSIX)
int g_shutdown_pipe[2] = {-1, -1};
void ShutdownPipeSignalHandler(int signal) {
HANDLE_EINTR(write(g_shutdown_pipe[1], "q", 1));
}
void KillSpawnedTestProcesses() {
AutoLock lock(*GetLiveProcessesLock());
fprintf(stdout, "Sending SIGTERM to %zu child processes... ",
GetLiveProcesses()->size());
fflush(stdout);
for (const auto& pair : *GetLiveProcesses()) {
kill((-1) * (pair.first), SIGTERM);
}
fprintf(stdout, "done.\nGiving processes a chance to terminate cleanly... ");
fflush(stdout);
PlatformThread::Sleep(Milliseconds(500));
fprintf(stdout, "done.\n");
fflush(stdout);
fprintf(stdout, "Sending SIGKILL to %zu child processes... ",
GetLiveProcesses()->size());
fflush(stdout);
for (const auto& pair : *GetLiveProcesses()) {
kill((-1) * (pair.first), SIGKILL);
}
fprintf(stdout, "done.\n");
fflush(stdout);
}
#endif
bool TakeInt32FromEnvironment(cstring_view var, int32_t* result) {
std::unique_ptr<Environment> env(Environment::Create());
std::optional<std::string> str_val = env->GetVar(var);
if (!str_val.has_value()) {
return true;
}
if (!env->UnSetVar(var)) {
LOG(ERROR) << "Invalid environment: we could not unset " << var << ".\n";
return false;
}
if (!StringToInt(str_val.value(), result)) {
LOG(ERROR) << "Invalid environment: " << var << " is not an integer.\n";
return false;
}
return true;
}
bool UnsetEnvironmentVariableIfExists(const std::string& name) {
std::unique_ptr<Environment> env(Environment::Create());
if (!env->HasVar(name)) {
return true;
}
return env->UnSetVar(name);
}
bool BotModeEnabled(const CommandLine* command_line) {
std::unique_ptr<Environment> env(Environment::Create());
return command_line->HasSwitch(switches::kTestLauncherBotMode) ||
env->HasVar("CHROMIUM_TEST_LAUNCHER_BOT_MODE");
}
CommandLine PrepareCommandLineForGTest(const CommandLine& command_line,
const std::string& wrapper,
const size_t retries_left) {
CommandLine new_command_line(command_line.GetProgram());
CommandLine::SwitchMap switches = command_line.GetSwitches();
switches.erase(kGTestRepeatFlag);
switches.erase(kIsolatedScriptTestRepeatFlag);
switches.erase(kGTestOutputFlag);
#if BUILDFLAG(IS_IOS)
switches.erase(switches::kEnableRunIOSUnittestsWithXCTest);
#endif
if (switches.find(switches::kTestLauncherRetriesLeft) == switches.end()) {
switches[switches::kTestLauncherRetriesLeft] =
#if BUILDFLAG(IS_WIN)
base::NumberToWString(
#else
base::NumberToString(
#endif
retries_left);
}
for (CommandLine::SwitchMap::const_iterator iter = switches.begin();
iter != switches.end(); ++iter) {
new_command_line.AppendSwitchNative((*iter).first, (*iter).second);
}
#if BUILDFLAG(IS_WIN)
new_command_line.PrependWrapper(UTF8ToWide(wrapper));
#else
new_command_line.PrependWrapper(wrapper);
#endif
return new_command_line;
}
int LaunchChildTestProcessWithOptions(const CommandLine& command_line,
const LaunchOptions& options,
int flags,
const FilePath& result_file,
TimeDelta timeout_per_test,
size_t num_tests,
TestLauncherDelegate* delegate,
bool* was_timeout) {
#if BUILDFLAG(IS_POSIX)
DCHECK(options.new_process_group);
#endif
LaunchOptions new_options(options);
#if BUILDFLAG(IS_WIN)
DCHECK(!new_options.job_handle);
win::ScopedHandle job_handle;
if (flags & TestLauncher::USE_JOB_OBJECTS) {
job_handle.Set(CreateJobObject(NULL, NULL));
if (!job_handle.is_valid()) {
LOG(ERROR) << "Could not create JobObject.";
return -1;
}
DWORD job_flags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (!SetJobObjectLimitFlags(job_handle.get(), job_flags)) {
LOG(ERROR) << "Could not SetJobObjectLimitFlags.";
return -1;
}
new_options.job_handle = job_handle.get();
}
#elif BUILDFLAG(IS_FUCHSIA)
DCHECK(!new_options.job_handle);
new_options.spawn_flags = FDIO_SPAWN_CLONE_STDIO | FDIO_SPAWN_CLONE_JOB;
const base::FilePath kDataPath(base::kPersistedDataDirectoryPath);
const base::FilePath kCachePath(base::kPersistedCacheDirectoryPath);
fdio_flat_namespace_t* flat_namespace = nullptr;
zx_status_t result = fdio_ns_export_root(&flat_namespace);
ZX_CHECK(ZX_OK == result, result) << "fdio_ns_export_root";
for (size_t i = 0; i < flat_namespace->count; ++i) {
base::FilePath path(flat_namespace->path[i]);
if (path == kDataPath || path == kCachePath) {
result = zx_handle_close(flat_namespace->handle[i]);
ZX_CHECK(ZX_OK == result, result) << "zx_handle_close";
} else {
new_options.paths_to_transfer.push_back(
{path, flat_namespace->handle[i]});
}
}
free(flat_namespace);
zx::job job_handle;
result = zx::job::create(*GetDefaultJob(), 0, &job_handle);
ZX_CHECK(ZX_OK == result, result) << "zx_job_create";
new_options.job_handle = job_handle.get();
CHECK(base::PathExists(kDataPath));
static base::AtomicSequenceNumber child_launch_index;
const base::FilePath child_data_path = kDataPath.AppendASCII(
base::StringPrintf("test-%zu-%d", base::Process::Current().Pid(),
child_launch_index.GetNext()));
CHECK(!base::DirectoryExists(child_data_path));
CHECK(base::CreateDirectory(child_data_path));
DCHECK(base::DirectoryExists(child_data_path));
const base::FilePath test_data_dir(child_data_path.AppendASCII("data"));
CHECK(base::CreateDirectory(test_data_dir));
const base::FilePath test_cache_dir(child_data_path.AppendASCII("cache"));
CHECK(base::CreateDirectory(test_cache_dir));
new_options.paths_to_transfer.push_back(
{kDataPath,
base::OpenDirectoryHandle(test_data_dir).TakeChannel().release()});
new_options.paths_to_transfer.push_back(
{kCachePath,
base::OpenDirectoryHandle(test_cache_dir).TakeChannel().release()});
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
new_options.allow_new_privs = true;
#endif
Process process;
{
AutoLock lock(*GetLiveProcessesLock());
#if BUILDFLAG(IS_WIN)
if (new_options.stdout_handle) {
::SetHandleInformation(new_options.stdout_handle, HANDLE_FLAG_INHERIT,
HANDLE_FLAG_INHERIT);
}
#endif
process = LaunchProcess(command_line, new_options);
#if BUILDFLAG(IS_WIN)
if (new_options.stdout_handle) {
::SetHandleInformation(new_options.stdout_handle, HANDLE_FLAG_INHERIT, 0);
}
#endif
if (!process.IsValid()) {
return -1;
}
GetLiveProcesses()->insert(std::make_pair(process.Handle(), command_line));
}
int exit_code = 0;
bool did_exit = false;
{
base::ScopedAllowBaseSyncPrimitivesForTesting allow_base_sync_primitives;
if (num_tests == 1) {
did_exit = process.WaitForExitWithTimeout(timeout_per_test, &exit_code);
} else {
ProcessResultWatcher result_watcher(result_file, num_tests, process);
did_exit = result_watcher.PollUntilDone(timeout_per_test);
exit_code = result_watcher.GetExitCode();
}
}
if (!did_exit) {
if (delegate) {
delegate->OnTestTimedOut(command_line);
}
*was_timeout = true;
exit_code = -1;
{
base::ScopedAllowBaseSyncPrimitivesForTesting allow_base_sync_primitives;
process.Terminate(-1, true);
}
}
#if BUILDFLAG(IS_FUCHSIA)
zx_status_t wait_status = WaitForJobExit(job_handle);
if (wait_status != ZX_OK) {
LOG(ERROR) << "Batch leaked jobs or processes.";
exit_code = -1;
}
#endif
{
AutoLock lock(*GetLiveProcessesLock());
#if BUILDFLAG(IS_FUCHSIA)
zx_status_t status = job_handle.kill();
ZX_CHECK(status == ZX_OK, status);
CHECK(DeletePathRecursively(child_data_path));
#elif BUILDFLAG(IS_POSIX)
kill(-1 * process.Handle(), SIGKILL);
#endif
GetLiveProcesses()->erase(process.Handle());
}
return exit_code;
}
struct ChildProcessResults {
TimeDelta elapsed_time;
std::string output_file_contents;
bool was_timeout = false;
int exit_code;
PlatformThreadId thread_id;
int process_num;
};
FilePath CreateChildTempDirIfSupported(const FilePath& task_temp_dir,
int child_index) {
if (!TestLauncher::SupportsPerChildTempDirs()) {
return FilePath();
}
FilePath child_temp = task_temp_dir.AppendASCII(NumberToString(child_index));
CHECK(CreateDirectoryAndGetError(child_temp, nullptr));
return child_temp;
}
void SetTemporaryDirectory(const FilePath& temp_dir,
EnvironmentMap* environment) {
#if BUILDFLAG(IS_WIN)
environment->emplace(L"TMP", temp_dir.value());
#elif BUILDFLAG(IS_APPLE)
environment->emplace("MAC_CHROMIUM_TMPDIR", temp_dir.value());
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
environment->emplace("TMPDIR", temp_dir.value());
#endif
}
ChildProcessResults DoLaunchChildTestProcess(
const CommandLine& command_line,
const FilePath& process_temp_dir,
const FilePath& result_file,
TimeDelta timeout_per_test,
size_t num_tests,
const TestLauncher::LaunchOptions& test_launch_options,
bool redirect_stdio,
TestLauncherDelegate* delegate) {
TimeTicks start_time = TimeTicks::Now();
ChildProcessResults result;
result.thread_id = PlatformThread::CurrentId();
ScopedFILE output_file;
FilePath output_filename;
if (redirect_stdio) {
output_file = CreateAndOpenTemporaryStream(&output_filename);
CHECK(output_file);
#if BUILDFLAG(IS_WIN)
if (!FILEToFile(output_file.get()).DeleteOnClose(true)) {
PLOG(WARNING) << "Failed to mark " << output_filename.AsUTF8Unsafe()
<< " for deletion on close";
}
#endif
}
LaunchOptions options;
#if BUILDFLAG(IS_IOS)
options.environment.emplace("XPC_FLAGS", "1");
#endif
if (!process_temp_dir.empty()) {
SetTemporaryDirectory(process_temp_dir, &options.environment);
}
#if BUILDFLAG(IS_WIN)
options.inherit_mode = test_launch_options.inherit_mode;
options.handles_to_inherit = test_launch_options.handles_to_inherit;
if (redirect_stdio) {
HANDLE handle =
reinterpret_cast<HANDLE>(_get_osfhandle(_fileno(output_file.get())));
CHECK_NE(INVALID_HANDLE_VALUE, handle);
options.stdin_handle = INVALID_HANDLE_VALUE;
options.stdout_handle = handle;
options.stderr_handle = handle;
if (options.inherit_mode == base::LaunchOptions::Inherit::kSpecific &&
GetFileType(handle) != FILE_TYPE_CHAR) {
options.handles_to_inherit.push_back(handle);
}
}
#else
options.fds_to_remap = test_launch_options.fds_to_remap;
if (redirect_stdio) {
int output_file_fd = fileno(output_file.get());
CHECK_LE(0, output_file_fd);
options.fds_to_remap.emplace_back(output_file_fd, STDOUT_FILENO);
options.fds_to_remap.emplace_back(output_file_fd, STDERR_FILENO);
}
#if !BUILDFLAG(IS_FUCHSIA)
options.new_process_group = true;
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
options.kill_on_parent_death = true;
#endif
#endif
result.exit_code = LaunchChildTestProcessWithOptions(
command_line, options, test_launch_options.flags, result_file,
timeout_per_test, num_tests, delegate, &result.was_timeout);
if (redirect_stdio) {
fflush(output_file.get());
CHECK(ReadStreamToString(output_file.get(), &result.output_file_contents) ||
result.exit_code != 0);
output_file.reset();
#if !BUILDFLAG(IS_WIN)
if (!DeleteFile(output_filename)) {
LOG(WARNING) << "Failed to delete " << output_filename.AsUTF8Unsafe();
}
#endif
}
result.elapsed_time = TimeTicks::Now() - start_time;
result.process_num = GetTestLauncherTracer()->RecordProcessExecution(
start_time, result.elapsed_time);
return result;
}
std::vector<std::string> ExtractTestsFromFilter(const std::string& filter,
bool double_colon_supported) {
std::vector<std::string> tests;
if (double_colon_supported) {
tests =
SplitString(filter, "::", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
}
if (tests.size() <= 1) {
tests =
SplitString(filter, ":", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
}
return tests;
}
class TestRunner {
public:
explicit TestRunner(TestLauncher* launcher,
size_t max_workers = 1u,
size_t batch_size = 1u)
: launcher_(launcher),
max_workers_(max_workers),
batch_size_(batch_size) {}
void Run(const std::vector<std::string>& test_names);
private:
static bool IsPreTestBatch(const std::vector<std::string>& test_names) {
return test_names.size() == 1u &&
test_names.front().find(kPreTestPrefix) != std::string::npos;
}
bool IsSingleThreaded() const { return batch_size_ == 0; }
void WorkerTask(scoped_refptr<TaskRunner> main_task_runner,
base::JobDelegate* delegate);
size_t GetMaxConcurrency(size_t worker_count) {
AutoLock auto_lock(lock_);
if (IsSingleThreaded()) {
return tests_to_run_.empty() ? 0 : 1;
}
return std::min((tests_to_run_.size() + batch_size_ - 1) / batch_size_,
max_workers_);
}
std::vector<std::string> GetNextBatch() EXCLUSIVE_LOCKS_REQUIRED(lock_) {
size_t batch_size;
if (IsSingleThreaded()) {
batch_size = tests_to_run_.size();
}
else {
batch_size = std::min(batch_size_, tests_to_run_.size());
}
std::vector<std::string> batch(tests_to_run_.rbegin(),
tests_to_run_.rbegin() + batch_size);
tests_to_run_.erase(tests_to_run_.end() - batch_size, tests_to_run_.end());
return batch;
}
void CleanupTask(base::ScopedTempDir task_temp_dir, bool done);
ThreadChecker thread_checker_;
const raw_ptr<TestLauncher> launcher_;
JobHandle job_handle_;
const size_t max_workers_;
const size_t batch_size_;
RunLoop run_loop_;
base::Lock lock_;
std::vector<std::string> tests_to_run_ GUARDED_BY(lock_);
base::WeakPtrFactory<TestRunner> weak_ptr_factory_{this};
};
void TestRunner::Run(const std::vector<std::string>& test_names) {
DCHECK(thread_checker_.CalledOnValidThread());
CHECK_GT(max_workers_, 0u);
if (test_names.empty()) {
return;
}
{
AutoLock auto_lock(lock_);
tests_to_run_ = {test_names.rbegin(), test_names.rend()};
}
job_handle_ = base::PostJob(
FROM_HERE, {TaskPriority::USER_BLOCKING, MayBlock()},
BindRepeating(&TestRunner::WorkerTask, Unretained(this),
SingleThreadTaskRunner::GetCurrentDefault()),
BindRepeating(&TestRunner::GetMaxConcurrency, Unretained(this)));
run_loop_.Run();
}
void TestRunner::WorkerTask(scoped_refptr<TaskRunner> main_task_runner,
base::JobDelegate* delegate) {
bool done = false;
while (!done && !delegate->ShouldYield()) {
base::ScopedTempDir task_temp_dir;
CHECK(task_temp_dir.CreateUniqueTempDirUnderPath(GetTempDirForTesting()));
int child_index = 0;
std::vector<std::vector<std::string>> batches;
{
AutoLock auto_lock(lock_);
if (!tests_to_run_.empty()) {
batches.push_back(GetNextBatch());
while (IsPreTestBatch(batches.back())) {
DCHECK(!tests_to_run_.empty());
batches.push_back(GetNextBatch());
}
}
done = tests_to_run_.empty();
}
for (const auto& batch : batches) {
launcher_->LaunchChildGTestProcess(
main_task_runner, batch, task_temp_dir.GetPath(),
CreateChildTempDirIfSupported(task_temp_dir.GetPath(),
child_index++));
}
main_task_runner->PostTask(
FROM_HERE,
BindOnce(&TestRunner::CleanupTask, weak_ptr_factory_.GetWeakPtr(),
std::move(task_temp_dir), done));
}
}
void TestRunner::CleanupTask(base::ScopedTempDir task_temp_dir, bool done) {
DCHECK(thread_checker_.CalledOnValidThread());
if (!task_temp_dir.Delete()) {
LOG(WARNING) << "Failed to delete "
<< task_temp_dir.GetPath().AsUTF8Unsafe();
}
if (!done) {
return;
}
if (job_handle_) {
job_handle_.Cancel();
run_loop_.QuitWhenIdle();
}
}
int CountItemsInDirectory(const FilePath& dir) {
if (dir.empty()) {
return 0;
}
int items = 0;
FileEnumerator file_enumerator(
dir, false,
FileEnumerator::FILES | FileEnumerator::DIRECTORIES);
for (FilePath name = file_enumerator.Next(); !name.empty();
name = file_enumerator.Next()) {
++items;
}
return items;
}
std::string TruncateSnippet(std::string_view snippet, size_t byte_limit) {
if (snippet.length() <= byte_limit) {
return std::string(snippet);
}
std::string truncation_message =
StringPrintf("\n<truncated (%zu bytes)>\n", snippet.length());
if (truncation_message.length() > byte_limit) {
return truncation_message;
}
size_t remaining_limit = byte_limit - truncation_message.length();
size_t first_half = remaining_limit / 2;
return base::StrCat(
{snippet.substr(0, first_half), truncation_message,
snippet.substr(snippet.length() - (remaining_limit - first_half))});
}
}
const char kGTestBreakOnFailure[] = "gtest_break_on_failure";
const char kGTestFilterFlag[] = "gtest_filter";
const char kGTestFlagfileFlag[] = "gtest_flagfile";
const char kGTestHelpFlag[] = "gtest_help";
const char kGTestListTestsFlag[] = "gtest_list_tests";
const char kGTestRepeatFlag[] = "gtest_repeat";
const char kGTestRunDisabledTestsFlag[] = "gtest_also_run_disabled_tests";
const char kGTestOutputFlag[] = "gtest_output";
const char kGTestShuffleFlag[] = "gtest_shuffle";
const char kGTestRandomSeedFlag[] = "gtest_random_seed";
const char kIsolatedScriptRunDisabledTestsFlag[] =
"isolated-script-test-also-run-disabled-tests";
const char kIsolatedScriptTestFilterFlag[] = "isolated-script-test-filter";
const char kIsolatedScriptTestRepeatFlag[] = "isolated-script-test-repeat";
class TestLauncher::TestInfo {
public:
TestInfo() = default;
TestInfo(const TestInfo& other) = default;
explicit TestInfo(const TestIdentifier& test_id);
~TestInfo() = default;
std::string GetDisabledStrippedName() const;
std::string GetFullName() const;
std::string GetPreName() const;
std::string GetPrefixStrippedName() const;
const std::string& test_case_name() const LIFETIME_BOUND {
return test_case_name_;
}
const std::string& test_name() const LIFETIME_BOUND { return test_name_; }
const std::string& file() const LIFETIME_BOUND { return file_; }
int line() const { return line_; }
bool disabled() const { return disabled_; }
bool pre_test() const { return pre_test_; }
private:
std::string test_case_name_;
std::string test_name_;
std::string file_;
int line_;
bool disabled_;
bool pre_test_;
};
TestLauncher::TestInfo::TestInfo(const TestIdentifier& test_id)
: test_case_name_(test_id.test_case_name),
test_name_(test_id.test_name),
file_(test_id.file),
line_(test_id.line),
disabled_(false),
pre_test_(false) {
disabled_ = GetFullName().find(kDisabledTestPrefix) != std::string::npos;
pre_test_ = test_name_.find(kPreTestPrefix) != std::string::npos;
}
std::string TestLauncher::TestInfo::GetDisabledStrippedName() const {
std::string test_name = GetFullName();
ReplaceSubstringsAfterOffset(&test_name, 0, kDisabledTestPrefix,
std::string());
return test_name;
}
std::string TestLauncher::TestInfo::GetFullName() const {
return FormatFullTestName(test_case_name_, test_name_);
}
std::string TestLauncher::TestInfo::GetPreName() const {
std::string name = test_name_;
ReplaceSubstringsAfterOffset(&name, 0, kDisabledTestPrefix, std::string());
std::string case_name = test_case_name_;
ReplaceSubstringsAfterOffset(&case_name, 0, kDisabledTestPrefix,
std::string());
return FormatFullTestName(case_name, kPreTestPrefix + name);
}
std::string TestLauncher::TestInfo::GetPrefixStrippedName() const {
std::string test_name = GetDisabledStrippedName();
ReplaceSubstringsAfterOffset(&test_name, 0, kPreTestPrefix, std::string());
return test_name;
}
TestLauncherDelegate::~TestLauncherDelegate() = default;
bool TestLauncherDelegate::ShouldRunTest(const TestIdentifier& test) {
return true;
}
TestLauncher::LaunchOptions::LaunchOptions() = default;
TestLauncher::LaunchOptions::LaunchOptions(const LaunchOptions& other) =
default;
TestLauncher::LaunchOptions::~LaunchOptions() = default;
TestLauncher::TestLauncher(TestLauncherDelegate* launcher_delegate,
size_t parallel_jobs,
size_t retry_limit)
: launcher_delegate_(launcher_delegate),
total_shards_(1),
shard_index_(0),
cycles_(1),
broken_threshold_(0),
test_started_count_(0),
test_finished_count_(0),
test_success_count_(0),
test_broken_count_(0),
retries_left_(0),
retry_limit_(retry_limit),
output_bytes_limit_(kOutputSnippetBytesLimit),
force_run_broken_tests_(false),
watchdog_timer_(FROM_HERE,
kOutputTimeout,
this,
&TestLauncher::OnOutputTimeout),
parallel_jobs_(parallel_jobs),
print_test_stdio_(AUTO) {}
TestLauncher::~TestLauncher() {
if (base::ThreadPoolInstance::Get()) {
base::ThreadPoolInstance::Get()->Shutdown();
base::ThreadPoolInstance::Get()->JoinForTesting();
base::ThreadPoolInstance::Set(nullptr);
}
}
bool TestLauncher::Run(CommandLine* command_line) {
base::PlatformThread::SetName("TestLauncherMain");
if (!Init((command_line == nullptr) ? CommandLine::ForCurrentProcess()
: command_line)) {
return false;
}
#if BUILDFLAG(IS_POSIX)
CHECK_EQ(0, pipe(g_shutdown_pipe));
struct sigaction action;
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = &ShutdownPipeSignalHandler;
CHECK_EQ(0, sigaction(SIGINT, &action, nullptr));
CHECK_EQ(0, sigaction(SIGQUIT, &action, nullptr));
CHECK_EQ(0, sigaction(SIGTERM, &action, nullptr));
auto controller = base::FileDescriptorWatcher::WatchReadable(
g_shutdown_pipe[0],
base::BindRepeating(&TestLauncher::OnShutdownPipeReadable,
Unretained(this)));
#endif
watchdog_timer_.Reset();
bool test_failed = false;
int iterations = cycles_;
if (cycles_ > 1 && !stop_on_failure_) {
iterations = 1;
repeats_per_iteration_ = cycles_;
}
bool run_result = true;
while ((iterations > 0 || iterations == -1) &&
!(stop_on_failure_ && test_failed)) {
OnTestIterationStart();
RunTests();
bool retry_result = RunRetryTests();
run_result = run_result && retry_result;
if (retry_result) {
fprintf(stdout, "SUCCESS: all tests passed.\n");
fflush(stdout);
}
test_failed = test_success_count_ != test_finished_count_;
OnTestIterationFinished();
iterations = (iterations == -1) ? iterations : iterations - 1;
}
if (cycles_ != 1) {
results_tracker_.PrintSummaryOfAllIterations();
}
MaybeSaveSummaryAsJSON(std::vector<std::string>());
return run_result;
}
void TestLauncher::LaunchChildGTestProcess(
scoped_refptr<TaskRunner> task_runner,
const std::vector<std::string>& test_names,
const FilePath& task_temp_dir,
const FilePath& child_temp_dir) {
FilePath result_file;
CommandLine cmd_line = launcher_delegate_->GetCommandLine(
test_names, task_temp_dir, &result_file);
CommandLine new_command_line(PrepareCommandLineForGTest(
cmd_line, launcher_delegate_->GetWrapper(), retries_left_));
LaunchOptions options;
options.flags = launcher_delegate_->GetLaunchOptions();
if (BotModeEnabled(CommandLine::ForCurrentProcess())) {
LOG(INFO) << "Starting [" << base::JoinString(test_names, ", ") << "]";
}
ChildProcessResults process_results = DoLaunchChildTestProcess(
new_command_line, child_temp_dir, result_file,
launcher_delegate_->GetTimeout(), test_names.size(), options,
redirect_stdio_, launcher_delegate_);
task_runner->PostTask(
FROM_HERE,
BindOnce(&TestLauncher::ProcessTestResults, Unretained(this), test_names,
result_file, process_results.output_file_contents,
process_results.elapsed_time, process_results.exit_code,
process_results.was_timeout, process_results.thread_id,
process_results.process_num,
CountItemsInDirectory(child_temp_dir)));
}
TestResult::Status MissingResultStatus(size_t tests_to_run_count,
bool was_timeout,
bool exit_code) {
if (tests_to_run_count > 1u) {
return TestResult::TEST_SKIPPED;
}
if (was_timeout) {
return TestResult::TEST_TIMEOUT;
}
if (exit_code != 0) {
return TestResult::TEST_FAILURE;
}
return TestResult::TEST_UNKNOWN;
}
void TestLauncher::ProcessTestResults(
const std::vector<std::string>& test_names,
const FilePath& result_file,
const std::string& output,
TimeDelta elapsed_time,
int exit_code,
bool was_timeout,
PlatformThreadId thread_id,
int process_num,
int leaked_items) {
std::vector<TestResult> test_results;
bool crashed = false;
bool have_test_results =
ProcessGTestOutput(result_file, &test_results, &crashed);
if (!have_test_results) {
LOG(ERROR) << "Failed to get out-of-band test success data, "
"dumping full stdio below:\n"
<< output << "\n";
test_results.clear();
}
TestResult::Status missing_result_status =
MissingResultStatus(test_names.size(), was_timeout, exit_code);
std::map<std::string, TestResult> results_map;
for (const auto& i : test_results) {
results_map[i.full_name] = i;
}
std::vector<TestResult> final_results;
for (const auto& i : test_names) {
if (Contains(results_map, i)) {
TestResult test_result = results_map[i];
if ((was_timeout && test_result.status == TestResult::TEST_CRASH) ||
test_result.elapsed_time > launcher_delegate_->GetTimeout()) {
test_result.status = TestResult::TEST_TIMEOUT;
}
final_results.push_back(test_result);
} else {
LOG(ERROR) << "no test result for " << i;
TestResult test_result;
test_result.full_name = i;
test_result.status = missing_result_status;
final_results.push_back(test_result);
}
}
bool has_non_success_test = false;
for (const auto& i : final_results) {
if (i.status != TestResult::TEST_SUCCESS) {
has_non_success_test = true;
break;
}
}
if (!has_non_success_test && exit_code != 0) {
for (auto& i : final_results) {
i.status = TestResult::TEST_FAILURE_ON_EXIT;
}
}
for (auto& i : final_results) {
i.output_snippet = GetTestOutputSnippet(i, output);
i.thread_id = thread_id;
i.process_num = process_num;
}
if (leaked_items) {
results_tracker_.AddLeakedItems(leaked_items, test_names);
}
launcher_delegate_->ProcessTestResults(final_results, elapsed_time);
for (const auto& result : final_results) {
OnTestFinished(result);
}
}
void TestLauncher::OnTestFinished(const TestResult& original_result) {
++test_finished_count_;
TestResult result(original_result);
if (result.output_snippet.length() > output_bytes_limit_) {
if (result.status == TestResult::TEST_SUCCESS) {
result.status = TestResult::TEST_EXCESSIVE_OUTPUT;
}
result.output_snippet =
TruncateSnippetFocused(result.output_snippet, output_bytes_limit_);
}
bool print_snippet = false;
if (print_test_stdio_ == AUTO) {
print_snippet = (result.status != TestResult::TEST_SUCCESS);
} else if (print_test_stdio_ == ALWAYS) {
print_snippet = true;
} else if (print_test_stdio_ == NEVER) {
print_snippet = false;
}
if (print_snippet) {
std::vector<std::string_view> snippet_lines =
SplitStringPiece(result.output_snippet, "\n", base::KEEP_WHITESPACE,
base::SPLIT_WANT_ALL);
if (snippet_lines.size() > kOutputSnippetLinesLimit) {
size_t truncated_size = snippet_lines.size() - kOutputSnippetLinesLimit;
snippet_lines.erase(snippet_lines.begin(),
snippet_lines.begin() + truncated_size);
snippet_lines.insert(snippet_lines.begin(), "<truncated>");
}
fprintf(stdout, "%s", base::JoinString(snippet_lines, "\n").c_str());
fflush(stdout);
}
if (result.status == TestResult::TEST_SUCCESS) {
++test_success_count_;
} else {
std::string test_name(result.full_name);
ReplaceSubstringsAfterOffset(&test_name, 0, kPreTestPrefix, std::string());
ReplaceSubstringsAfterOffset(&test_name, 0, kDisabledTestPrefix,
std::string());
tests_to_retry_.insert(test_name);
}
if (result.status != TestResult::TEST_SKIPPED) {
results_tracker_.AddTestResult(result);
}
std::string status_line(StringPrintf("[%zu/%zu] %s ", test_finished_count_,
test_started_count_,
result.full_name.c_str()));
if (result.completed()) {
status_line.append(
StringPrintf("(%" PRId64 " ms)", result.elapsed_time.InMilliseconds()));
} else if (result.status == TestResult::TEST_TIMEOUT) {
status_line.append("(TIMED OUT)");
} else if (result.status == TestResult::TEST_CRASH) {
status_line.append("(CRASHED)");
} else if (result.status == TestResult::TEST_SKIPPED) {
status_line.append("(SKIPPED)");
} else if (result.status == TestResult::TEST_UNKNOWN) {
status_line.append("(UNKNOWN)");
} else {
NOTREACHED() << "Unhandled test result status: " << result.status;
}
fprintf(stdout, "%s\n", status_line.c_str());
fflush(stdout);
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kTestLauncherPrintTimestamps)) {
::logging::ScopedLoggingSettings scoped_logging_setting;
::logging::SetLogItems(true, true, true, true);
LOG(INFO) << "Test_finished_timestamp";
}
watchdog_timer_.Reset();
if (result.status == TestResult::TEST_TIMEOUT) {
test_broken_count_++;
}
if (!force_run_broken_tests_ && test_broken_count_ >= broken_threshold_) {
fprintf(stdout, "Too many badly broken tests (%zu), exiting now.\n",
test_broken_count_);
fflush(stdout);
#if BUILDFLAG(IS_POSIX)
KillSpawnedTestProcesses();
#endif
MaybeSaveSummaryAsJSON({"BROKEN_TEST_EARLY_EXIT"});
exit(1);
}
}
bool LoadFilterFile(const FilePath& file_path,
std::vector<std::string>* positive_filter,
std::vector<std::string>* negative_filter) {
std::string file_content;
if (!ReadFileToString(file_path, &file_content)) {
LOG(ERROR) << "Failed to read the filter file.";
return false;
}
std::vector<std::string> filter_lines = SplitString(
file_content, "\n", base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
int line_num = 0;
for (const std::string& filter_line : filter_lines) {
line_num++;
size_t hash_pos = filter_line.find('#');
if (hash_pos != std::string::npos && hash_pos > 0 &&
filter_line[hash_pos - 1] != ' ') {
LOG(WARNING) << "Content of line " << line_num << " in " << file_path
<< " after # is treated as a comment, " << filter_line;
}
std::string trimmed_line(
TrimWhitespaceASCII(filter_line.substr(0, hash_pos), TRIM_ALL));
if (trimmed_line.substr(0, 2) == "//") {
LOG(ERROR) << "Line " << line_num << " in " << file_path
<< " starts with //, use # for comments.";
return false;
}
if (trimmed_line.empty()) {
continue;
}
if (trimmed_line[0] == '-') {
negative_filter->push_back(trimmed_line.substr(1));
} else {
positive_filter->push_back(trimmed_line);
}
}
return true;
}
bool TestLauncher::IsOnlyExactPositiveFilterFromFile(
const CommandLine* command_line) const {
if (command_line->HasSwitch(kGTestFilterFlag)) {
LOG(ERROR) << "Found " << switches::kTestLauncherFilterFile;
return false;
}
if (!negative_test_filter_.empty()) {
LOG(ERROR) << "Found negative filters in the filter file.";
return false;
}
for (const auto& filter : positive_test_filter_) {
if (Contains(filter, '*')) {
LOG(ERROR) << "Found wildcard positive filters in the filter file.";
return false;
}
}
return true;
}
bool TestLauncher::Init(CommandLine* command_line) {
if (command_line->HasSwitch(switches::kTestLauncherTotalShards) &&
command_line->HasSwitch(switches::kTestLauncherShardIndex)) {
if (!StringToInt(command_line->GetSwitchValueASCII(
switches::kTestLauncherTotalShards),
&total_shards_)) {
LOG(ERROR) << "Invalid value for " << switches::kTestLauncherTotalShards;
return false;
}
if (!StringToInt(command_line->GetSwitchValueASCII(
switches::kTestLauncherShardIndex),
&shard_index_)) {
LOG(ERROR) << "Invalid value for " << switches::kTestLauncherShardIndex;
return false;
}
fprintf(stdout,
"Using sharding settings from command line. This is shard %d/%d\n",
shard_index_, total_shards_);
fflush(stdout);
} else {
if (!TakeInt32FromEnvironment(kTestTotalShards, &total_shards_)) {
return false;
}
if (!TakeInt32FromEnvironment(kTestShardIndex, &shard_index_)) {
return false;
}
fprintf(stdout,
"Using sharding settings from environment. This is shard %d/%d\n",
shard_index_, total_shards_);
fflush(stdout);
}
if (shard_index_ < 0 || total_shards_ < 0 || shard_index_ >= total_shards_) {
LOG(ERROR) << "Invalid sharding settings: we require 0 <= "
<< kTestShardIndex << " < " << kTestTotalShards
<< ", but you have " << kTestShardIndex << "=" << shard_index_
<< ", " << kTestTotalShards << "=" << total_shards_ << ".\n";
return false;
}
CHECK(UnsetEnvironmentVariableIfExists("GTEST_TOTAL_SHARDS"));
CHECK(UnsetEnvironmentVariableIfExists("GTEST_SHARD_INDEX"));
if (command_line->HasSwitch(kGTestRepeatFlag) &&
!StringToInt(command_line->GetSwitchValueASCII(kGTestRepeatFlag),
&cycles_)) {
LOG(ERROR) << "Invalid value for " << kGTestRepeatFlag;
return false;
}
if (command_line->HasSwitch(kIsolatedScriptTestRepeatFlag) &&
!StringToInt(
command_line->GetSwitchValueASCII(kIsolatedScriptTestRepeatFlag),
&cycles_)) {
LOG(ERROR) << "Invalid value for " << kIsolatedScriptTestRepeatFlag;
return false;
}
if (command_line->HasSwitch(switches::kTestLauncherRetryLimit)) {
int retry_limit = -1;
if (!StringToInt(command_line->GetSwitchValueASCII(
switches::kTestLauncherRetryLimit),
&retry_limit) ||
retry_limit < 0) {
LOG(ERROR) << "Invalid value for " << switches::kTestLauncherRetryLimit;
return false;
}
retry_limit_ = retry_limit;
} else if (command_line->HasSwitch(
switches::kIsolatedScriptTestLauncherRetryLimit)) {
int retry_limit = -1;
if (!StringToInt(command_line->GetSwitchValueASCII(
switches::kIsolatedScriptTestLauncherRetryLimit),
&retry_limit) ||
retry_limit < 0) {
LOG(ERROR) << "Invalid value for "
<< switches::kIsolatedScriptTestLauncherRetryLimit;
return false;
}
retry_limit_ = retry_limit;
} else if (command_line->HasSwitch(kGTestRepeatFlag) ||
command_line->HasSwitch(kGTestBreakOnFailure)) {
retry_limit_ = 0U;
} else if (!BotModeEnabled(command_line) &&
(command_line->HasSwitch(kGTestFilterFlag) ||
command_line->HasSwitch(kIsolatedScriptTestFilterFlag))) {
retry_limit_ = 0U;
}
retries_left_ = retry_limit_;
force_run_broken_tests_ =
command_line->HasSwitch(switches::kTestLauncherForceRunBrokenTests);
if (command_line->HasSwitch(switches::kTestLauncherOutputBytesLimit)) {
int output_bytes_limit = -1;
if (!StringToInt(command_line->GetSwitchValueASCII(
switches::kTestLauncherOutputBytesLimit),
&output_bytes_limit) ||
output_bytes_limit < 0) {
LOG(ERROR) << "Invalid value for "
<< switches::kTestLauncherOutputBytesLimit;
return false;
}
output_bytes_limit_ = output_bytes_limit;
}
fprintf(stdout, "Using %zu parallel jobs.\n", parallel_jobs_);
fflush(stdout);
CreateAndStartThreadPool(parallel_jobs_);
std::vector<std::string> positive_file_filter;
std::vector<std::string> positive_gtest_filter;
if (command_line->HasSwitch(switches::kTestLauncherFilterFile)) {
auto filter =
command_line->GetSwitchValueNative(switches::kTestLauncherFilterFile);
for (auto filter_file :
SplitStringPiece(filter, FILE_PATH_LITERAL(";"), base::TRIM_WHITESPACE,
base::SPLIT_WANT_ALL)) {
#if BUILDFLAG(IS_IOS)
base::FilePath data_dir;
PathService::Get(DIR_SRC_TEST_DATA_ROOT, &data_dir);
base::FilePath filter_file_path = data_dir.Append(FilePath(filter_file));
#else
base::FilePath filter_file_path =
base::MakeAbsoluteFilePath(FilePath(filter_file));
#endif
if (!LoadFilterFile(filter_file_path, &positive_file_filter,
&negative_test_filter_)) {
return false;
}
}
}
if (command_line->HasSwitch(kGTestRunDisabledTestsFlag)) {
negative_test_filter_.clear();
}
enforce_exact_postive_filter_ =
command_line->HasSwitch(switches::kEnforceExactPositiveFilter);
if (enforce_exact_postive_filter_ &&
!IsOnlyExactPositiveFilterFromFile(command_line)) {
LOG(ERROR) << "With " << switches::kEnforceExactPositiveFilter
<< ", only accept exact positive filters via "
<< switches::kTestLauncherFilterFile;
return false;
}
bool double_colon_supported = !command_line->HasSwitch(kGTestFilterFlag);
std::string filter = command_line->GetSwitchValueASCII(
double_colon_supported ? kIsolatedScriptTestFilterFlag
: kGTestFilterFlag);
size_t dash_pos = filter.find('-');
if (dash_pos == std::string::npos) {
positive_gtest_filter =
ExtractTestsFromFilter(filter, double_colon_supported);
} else {
positive_gtest_filter = ExtractTestsFromFilter(filter.substr(0, dash_pos),
double_colon_supported);
for (std::string pattern : ExtractTestsFromFilter(
filter.substr(dash_pos + 1), double_colon_supported)) {
negative_test_filter_.push_back(pattern);
}
}
skip_disabled_tests_ =
!command_line->HasSwitch(kGTestRunDisabledTestsFlag) &&
!command_line->HasSwitch(kIsolatedScriptRunDisabledTestsFlag);
if (!InitTests()) {
return false;
}
if (!ShuffleTests(command_line)) {
return false;
}
if (!ProcessAndValidateTests()) {
return false;
}
if (command_line->HasSwitch(switches::kTestLauncherPrintTestStdio)) {
std::string print_test_stdio = command_line->GetSwitchValueASCII(
switches::kTestLauncherPrintTestStdio);
if (print_test_stdio == "auto") {
print_test_stdio_ = AUTO;
} else if (print_test_stdio == "always") {
print_test_stdio_ = ALWAYS;
} else if (print_test_stdio == "never") {
print_test_stdio_ = NEVER;
} else {
LOG(WARNING) << "Invalid value of "
<< switches::kTestLauncherPrintTestStdio << ": "
<< print_test_stdio;
return false;
}
}
stop_on_failure_ = command_line->HasSwitch(kGTestBreakOnFailure);
if (command_line->HasSwitch(switches::kTestLauncherSummaryOutput)) {
summary_path_ = FilePath(
command_line->GetSwitchValuePath(switches::kTestLauncherSummaryOutput));
}
if (command_line->HasSwitch(switches::kTestLauncherTrace)) {
trace_path_ = FilePath(
command_line->GetSwitchValuePath(switches::kTestLauncherTrace));
}
redirect_stdio_ = (parallel_jobs_ > 1) || BotModeEnabled(command_line);
CombinePositiveTestFilters(std::move(positive_gtest_filter),
std::move(positive_file_filter));
if (!results_tracker_.Init(*command_line)) {
LOG(ERROR) << "Failed to initialize test results tracker.";
return true;
}
#if defined(NDEBUG)
results_tracker_.AddGlobalTag("MODE_RELEASE");
#else
results_tracker_.AddGlobalTag("MODE_DEBUG");
#endif
#if BUILDFLAG(IS_ANDROID)
results_tracker_.AddGlobalTag("OS_ANDROID");
#endif
#if BUILDFLAG(IS_APPLE)
results_tracker_.AddGlobalTag("OS_APPLE");
#endif
#if BUILDFLAG(IS_BSD)
results_tracker_.AddGlobalTag("OS_BSD");
#endif
#if BUILDFLAG(IS_FREEBSD)
results_tracker_.AddGlobalTag("OS_FREEBSD");
#endif
#if BUILDFLAG(IS_FUCHSIA)
results_tracker_.AddGlobalTag("OS_FUCHSIA");
#endif
#if BUILDFLAG(IS_IOS)
results_tracker_.AddGlobalTag("OS_IOS");
#endif
#if BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)
results_tracker_.AddGlobalTag("OS_LINUX");
#endif
#if BUILDFLAG(IS_CHROMEOS)
results_tracker_.AddGlobalTag("OS_CHROMEOS");
#endif
#if BUILDFLAG(IS_MAC)
results_tracker_.AddGlobalTag("OS_MAC");
#endif
#if BUILDFLAG(IS_OPENBSD)
results_tracker_.AddGlobalTag("OS_OPENBSD");
#endif
#if BUILDFLAG(IS_POSIX)
results_tracker_.AddGlobalTag("OS_POSIX");
#endif
#if BUILDFLAG(IS_SOLARIS)
results_tracker_.AddGlobalTag("OS_SOLARIS");
#endif
#if BUILDFLAG(IS_WIN)
results_tracker_.AddGlobalTag("OS_WIN");
#endif
#if defined(ARCH_CPU_32_BITS)
results_tracker_.AddGlobalTag("CPU_32_BITS");
#endif
#if defined(ARCH_CPU_64_BITS)
results_tracker_.AddGlobalTag("CPU_64_BITS");
#endif
return true;
}
bool TestLauncher::InitTests() {
std::vector<TestIdentifier> tests;
if (!launcher_delegate_->GetTests(&tests)) {
LOG(ERROR) << "Failed to get list of tests.";
return false;
}
std::unordered_set<std::string> full_test_names;
bool dups_found = false;
for (auto& test : tests) {
const std::string full_test_name =
test.test_case_name + "." + test.test_name;
auto [it, inserted] = full_test_names.insert(full_test_name);
if (!inserted) {
LOG(WARNING) << "Duplicate test name found: " << full_test_name;
dups_found = true;
}
}
CHECK(!dups_found);
std::vector<std::string> uninstantiated_tests;
for (const TestIdentifier& test_id : tests) {
TestInfo test_info(test_id);
if (test_id.test_case_name == "GoogleTestVerification") {
uninstantiated_tests.push_back(test_id.test_name);
continue;
}
if (launcher_delegate_->ShouldRunTest(test_id)) {
tests_.push_back(test_info);
}
}
if (!uninstantiated_tests.empty()) {
LOG(ERROR) << "Found uninstantiated parameterized tests. These test suites "
"will not run:";
for (const std::string& name : uninstantiated_tests) {
LOG(ERROR) << " " << name;
}
LOG(ERROR) << "Please use INSTANTIATE_TEST_SUITE_P to instantiate the "
"tests, or GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST if "
"the parameter list can be intentionally empty. See "
"//third_party/googletest/src/docs/advanced.md";
return false;
}
return true;
}
bool TestLauncher::ShuffleTests(CommandLine* command_line) {
if (command_line->HasSwitch(kGTestShuffleFlag)) {
uint32_t shuffle_seed;
if (command_line->HasSwitch(kGTestRandomSeedFlag)) {
const std::string custom_seed_str =
command_line->GetSwitchValueASCII(kGTestRandomSeedFlag);
uint32_t custom_seed = 0;
if (!StringToUint(custom_seed_str, &custom_seed)) {
LOG(ERROR) << "Unable to parse seed \"" << custom_seed_str << "\".";
return false;
}
if (custom_seed >= kRandomSeedUpperBound) {
LOG(ERROR) << "Seed " << custom_seed << " outside of expected range "
<< "[0, " << kRandomSeedUpperBound << ")";
return false;
}
shuffle_seed = custom_seed;
} else {
std::uniform_int_distribution<uint32_t> dist(0, kRandomSeedUpperBound);
std::random_device random_dev;
shuffle_seed = dist(random_dev);
}
std::mt19937 randomizer;
randomizer.seed(shuffle_seed);
std::ranges::shuffle(tests_, randomizer);
fprintf(stdout, "Randomizing with seed %u\n", shuffle_seed);
fflush(stdout);
} else if (command_line->HasSwitch(kGTestRandomSeedFlag)) {
LOG(ERROR) << kGTestRandomSeedFlag << " requires " << kGTestShuffleFlag;
return false;
}
return true;
}
bool TestLauncher::ProcessAndValidateTests() {
bool result = true;
std::unordered_set<std::string> disabled_tests;
std::unordered_map<std::string, TestInfo> pre_tests;
for (const TestInfo& test_info : tests_) {
std::string test_name = test_info.GetFullName();
results_tracker_.AddTest(test_name);
if (test_info.disabled()) {
disabled_tests.insert(test_info.GetDisabledStrippedName());
results_tracker_.AddDisabledTest(test_name);
}
if (test_info.pre_test()) {
pre_tests[test_info.GetDisabledStrippedName()] = test_info;
}
}
std::vector<TestInfo> tests_to_run;
for (const TestInfo& test_info : tests_) {
std::string test_name = test_info.GetFullName();
if (base::Contains(disabled_tests, test_name)) {
LOG(ERROR) << test_name << " duplicated by a DISABLED_ test";
result = false;
}
if (test_info.pre_test()) {
continue;
}
std::vector<TestInfo> test_sequence;
test_sequence.push_back(test_info);
while (base::Contains(pre_tests, test_sequence.back().GetPreName())) {
test_sequence.push_back(pre_tests[test_sequence.back().GetPreName()]);
pre_tests.erase(test_sequence.back().GetDisabledStrippedName());
}
if (!test_info.disabled() || !skip_disabled_tests_) {
tests_to_run.insert(tests_to_run.end(), test_sequence.rbegin(),
test_sequence.rend());
}
}
tests_ = std::move(tests_to_run);
for (const auto& i : pre_tests) {
LOG(ERROR) << i.first << " is an orphaned pre test";
result = false;
}
return result;
}
void TestLauncher::CreateAndStartThreadPool(size_t num_parallel_jobs) {
base::ThreadPoolInstance::Create("TestLauncher");
base::ThreadPoolInstance::Get()->Start({num_parallel_jobs});
}
void TestLauncher::CombinePositiveTestFilters(
std::vector<std::string> filter_a,
std::vector<std::string> filter_b) {
has_at_least_one_positive_filter_ = !filter_a.empty() || !filter_b.empty();
if (!has_at_least_one_positive_filter_) {
return;
}
if (!filter_a.empty() && !filter_b.empty()) {
for (const auto& i : tests_) {
std::string test_name = i.GetFullName();
bool found_a = false;
bool found_b = false;
for (const auto& k : filter_a) {
found_a = found_a || MatchPattern(test_name, k);
}
for (const auto& k : filter_b) {
found_b = found_b || MatchPattern(test_name, k);
}
if (found_a && found_b) {
positive_test_filter_.push_back(test_name);
}
}
} else if (!filter_a.empty()) {
positive_test_filter_ = std::move(filter_a);
} else {
positive_test_filter_ = std::move(filter_b);
}
}
bool TestLauncher::ShouldRunInCurrentShard(
std::string_view prefix_stripped_name) const {
CHECK(!StartsWith(prefix_stripped_name, kPreTestPrefix));
CHECK(!StartsWith(prefix_stripped_name, kDisabledTestPrefix));
return PersistentHash(prefix_stripped_name) % total_shards_ ==
static_cast<uint32_t>(shard_index_);
}
std::vector<std::string> TestLauncher::CollectTests() {
std::vector<std::string> test_names;
std::vector<std::string_view> positive_wildcards_filter;
std::unordered_set<std::string_view> positive_exact_filter;
positive_exact_filter.reserve(positive_test_filter_.size());
std::unordered_set<std::string> enforced_positive_tests;
for (const std::string& filter : positive_test_filter_) {
if (filter.find('*') != std::string::npos) {
positive_wildcards_filter.push_back(filter);
} else {
positive_exact_filter.insert(filter);
}
}
std::vector<std::string_view> negative_wildcards_filter;
std::unordered_set<std::string_view> negative_exact_filter;
negative_exact_filter.reserve(negative_test_filter_.size());
for (const std::string& filter : negative_test_filter_) {
if (filter.find('*') != std::string::npos) {
negative_wildcards_filter.push_back(filter);
} else {
negative_exact_filter.insert(filter);
}
}
for (const TestInfo& test_info : tests_) {
std::string test_name = test_info.GetFullName();
std::string prefix_stripped_name = test_info.GetPrefixStrippedName();
if (has_at_least_one_positive_filter_) {
bool found = positive_exact_filter.find(test_name) !=
positive_exact_filter.end() ||
positive_exact_filter.find(prefix_stripped_name) !=
positive_exact_filter.end();
if (found && enforce_exact_postive_filter_) {
enforced_positive_tests.insert(prefix_stripped_name);
}
if (!found) {
for (std::string_view filter : positive_wildcards_filter) {
if (MatchPattern(test_name, filter) ||
MatchPattern(prefix_stripped_name, filter)) {
found = true;
break;
}
}
}
if (!found) {
continue;
}
}
if (negative_exact_filter.find(test_name) != negative_exact_filter.end() ||
negative_exact_filter.find(prefix_stripped_name) !=
negative_exact_filter.end()) {
continue;
}
bool excluded = false;
for (std::string_view filter : negative_wildcards_filter) {
if (MatchPattern(test_name, filter) ||
MatchPattern(prefix_stripped_name, filter)) {
excluded = true;
break;
}
}
if (excluded) {
continue;
}
if (!ShouldRunInCurrentShard(prefix_stripped_name)) {
continue;
}
results_tracker_.AddTestLocation(test_name, test_info.file(),
test_info.line());
if (!test_info.pre_test()) {
results_tracker_.AddTestPlaceholder(test_name);
}
test_names.push_back(test_name);
}
if (enforce_exact_postive_filter_) {
bool found_exact_positive_filter_not_enforced = false;
for (const auto& filter : positive_exact_filter) {
if (!ShouldRunInCurrentShard(filter) ||
Contains(enforced_positive_tests, std::string(filter))) {
continue;
}
if (!found_exact_positive_filter_not_enforced) {
LOG(ERROR) << "Found exact positive filter not enforced:";
found_exact_positive_filter_not_enforced = true;
}
LOG(ERROR) << filter;
}
CHECK(!found_exact_positive_filter_not_enforced);
}
return test_names;
}
void TestLauncher::RunTests() {
std::vector<std::string> original_test_names = CollectTests();
std::vector<std::string> test_names;
for (int i = 0; i < repeats_per_iteration_; ++i) {
test_names.insert(test_names.end(), original_test_names.begin(),
original_test_names.end());
}
broken_threshold_ = std::max(static_cast<size_t>(20), tests_.size() / 10);
test_started_count_ = test_names.size();
if (test_started_count_ == 0) {
PrintFuzzyMatchingTestNames();
fprintf(stdout, "WARNING: No matching tests to run.\n");
fflush(stdout);
}
results_tracker_.GeneratePlaceholderIteration();
MaybeSaveSummaryAsJSON({"EARLY_SUMMARY"});
size_t batch_size =
repeats_per_iteration_ > 1 ? 1U : launcher_delegate_->GetBatchSize();
TestRunner test_runner(this, parallel_jobs_, batch_size);
test_runner.Run(test_names);
}
void TestLauncher::PrintFuzzyMatchingTestNames() {
for (const auto& filter : positive_test_filter_) {
if (filter.empty()) {
continue;
}
std::string almost_filter;
if (filter.front() != '*') {
almost_filter += '*';
}
almost_filter += filter;
if (filter.back() != '*') {
almost_filter += '*';
}
for (const TestInfo& test_info : tests_) {
std::string test_name = test_info.GetFullName();
std::string prefix_stripped_name = test_info.GetPrefixStrippedName();
if (MatchPattern(test_name, almost_filter) ||
MatchPattern(prefix_stripped_name, almost_filter)) {
fprintf(stdout, "Filter \"%s\" would have matched: %s\n",
almost_filter.c_str(), test_name.c_str());
fflush(stdout);
}
}
}
}
bool TestLauncher::RunRetryTests() {
while (!tests_to_retry_.empty() && retries_left_ > 0) {
std::vector<std::string> test_names;
for (const TestInfo& test_info : tests_) {
if (base::Contains(tests_to_retry_, test_info.GetPrefixStrippedName())) {
test_names.push_back(test_info.GetFullName());
}
}
tests_to_retry_.clear();
size_t retry_started_count = test_names.size();
test_started_count_ += retry_started_count;
if (retry_started_count == 0) {
return false;
}
fprintf(stdout, "Retrying %zu test%s (retry #%zu)\n", retry_started_count,
retry_started_count > 1 ? "s" : "", retry_limit_ - retries_left_);
fflush(stdout);
--retries_left_;
TestRunner test_runner(this);
test_runner.Run(test_names);
}
return tests_to_retry_.empty();
}
void TestLauncher::OnTestIterationStart() {
test_started_count_ = 0;
test_finished_count_ = 0;
test_success_count_ = 0;
test_broken_count_ = 0;
tests_to_retry_.clear();
results_tracker_.OnTestIterationStarting();
}
#if BUILDFLAG(IS_POSIX)
void TestLauncher::OnShutdownPipeReadable() {
fprintf(stdout, "\nCaught signal. Killing spawned test processes...\n");
fflush(stdout);
KillSpawnedTestProcesses();
MaybeSaveSummaryAsJSON({"CAUGHT_TERMINATION_SIGNAL"});
_exit(1);
}
#endif
void TestLauncher::MaybeSaveSummaryAsJSON(
const std::vector<std::string>& additional_tags) {
if (!summary_path_.empty()) {
if (!results_tracker_.SaveSummaryAsJSON(summary_path_, additional_tags)) {
LOG(ERROR) << "Failed to save test launcher output summary.";
}
}
if (!trace_path_.empty()) {
if (!GetTestLauncherTracer()->Dump(trace_path_)) {
LOG(ERROR) << "Failed to save test launcher trace.";
}
}
}
void TestLauncher::OnTestIterationFinished() {
TestResultsTracker::TestStatusMap tests_by_status(
results_tracker_.GetTestStatusMapForCurrentIteration());
if (!tests_by_status[TestResult::TEST_UNKNOWN].empty()) {
results_tracker_.AddGlobalTag(kUnreliableResultsTag);
}
results_tracker_.PrintSummaryOfCurrentIteration();
}
void TestLauncher::OnOutputTimeout() {
DCHECK(thread_checker_.CalledOnValidThread());
AutoLock lock(*GetLiveProcessesLock());
fprintf(stdout, "Still waiting for the following processes to finish:\n");
for (const auto& pair : *GetLiveProcesses()) {
#if BUILDFLAG(IS_WIN)
fwprintf(stdout, L"\t%s\n", pair.second.GetCommandLineString().c_str());
#else
fprintf(stdout, "\t%s\n", pair.second.GetCommandLineString().c_str());
#endif
}
fflush(stdout);
if (CommandLine::ForCurrentProcess()->HasSwitch(
switches::kTestLauncherPrintTimestamps)) {
::logging::ScopedLoggingSettings scoped_logging_setting;
::logging::SetLogItems(true, true, true, true);
LOG(INFO) << "Waiting_timestamp";
}
watchdog_timer_.Reset();
}
size_t NumParallelJobs(unsigned int cores_per_job) {
const CommandLine* command_line = CommandLine::ForCurrentProcess();
if (command_line->HasSwitch(switches::kTestLauncherJobs)) {
size_t jobs = 0U;
if (!StringToSizeT(
command_line->GetSwitchValueASCII(switches::kTestLauncherJobs),
&jobs) ||
!jobs) {
LOG(ERROR) << "Invalid value for " << switches::kTestLauncherJobs;
return 0U;
}
return jobs;
}
if (!BotModeEnabled(command_line) &&
(command_line->HasSwitch(kGTestFilterFlag) ||
command_line->HasSwitch(kIsolatedScriptTestFilterFlag))) {
return 1U;
}
#if BUILDFLAG(IS_WIN)
size_t cores = base::checked_cast<size_t>(
::GetActiveProcessorCount(ALL_PROCESSOR_GROUPS));
#else
size_t cores = base::checked_cast<size_t>(SysInfo::NumberOfProcessors());
#if BUILDFLAG(IS_MAC)
SysInfo::ResetCpuSecurityMitigationsEnabledForTesting();
#endif
#endif
#if BUILDFLAG(IS_IOS) && TARGET_OS_SIMULATOR
cores *= 2;
#endif
return std::max(size_t(1), cores / cores_per_job);
}
std::string GetTestOutputSnippet(const TestResult& result,
const std::string& full_output) {
size_t run_pos =
full_output.find(std::string("[ RUN ] ") + result.full_name);
if (run_pos == std::string::npos) {
return std::string();
}
size_t end_pos = full_output.find(
std::string("[ FAILED ] ") + result.full_name, run_pos);
if (end_pos == std::string::npos) {
if (result.status == TestResult::TEST_SUCCESS) {
end_pos = full_output.find(
std::string("[ OK ] ") + result.full_name, run_pos);
if (end_pos == std::string::npos) {
end_pos = full_output.find(
std::string("[ SKIPPED ] ") + result.full_name, run_pos);
}
} else {
end_pos = full_output.find(std::string("[ RUN ]"), run_pos + 1);
if (end_pos != std::string::npos) {
end_pos--;
}
}
}
if (end_pos != std::string::npos) {
size_t newline_pos = full_output.find("\n", end_pos);
if (newline_pos != std::string::npos) {
end_pos = newline_pos + 1;
}
}
std::string snippet(full_output.substr(run_pos));
if (end_pos != std::string::npos) {
snippet = full_output.substr(run_pos, end_pos - run_pos);
}
return snippet;
}
std::string TruncateSnippetFocused(std::string_view snippet,
size_t byte_limit) {
size_t fatal_message_pos =
std::min(snippet.find("FATAL:"), snippet.find("FATAL "));
size_t fatal_message_start = 0;
size_t fatal_message_end = 0;
if (fatal_message_pos != std::string::npos) {
size_t start_pos = snippet.rfind("\n", fatal_message_pos);
if (start_pos != std::string::npos) {
fatal_message_start = start_pos;
}
size_t end_pos = snippet.find("\n", fatal_message_pos);
if (end_pos != std::string::npos) {
fatal_message_end = end_pos + 1;
} else {
fatal_message_end = snippet.length();
}
}
fatal_message_end =
std::min(fatal_message_end, fatal_message_start + (byte_limit / 2));
size_t remaining_bytes =
byte_limit - (fatal_message_end - fatal_message_start);
size_t start_split_bytes;
size_t end_split_bytes;
if (fatal_message_start < remaining_bytes / 2) {
start_split_bytes = fatal_message_start;
end_split_bytes = remaining_bytes - fatal_message_start;
} else if ((snippet.length() - fatal_message_end) < remaining_bytes / 2) {
start_split_bytes =
remaining_bytes - (snippet.length() - fatal_message_end);
end_split_bytes = (snippet.length() - fatal_message_end);
} else {
start_split_bytes = remaining_bytes / 2;
end_split_bytes = remaining_bytes - start_split_bytes;
}
return base::StrCat(
{TruncateSnippet(snippet.substr(0, fatal_message_start),
start_split_bytes),
snippet.substr(fatal_message_start,
fatal_message_end - fatal_message_start),
TruncateSnippet(snippet.substr(fatal_message_end), end_split_bytes)});
}
}