diff --git a/deps/googletest/include/gtest/gtest.h b/deps/googletest/include/gtest/gtest.h index 4218adebde20d8..f4a91ee44d0aeb 100644 --- a/deps/googletest/include/gtest/gtest.h +++ b/deps/googletest/include/gtest/gtest.h @@ -137,6 +137,10 @@ GTEST_DECLARE_int32_(repeat); // only torn down once, for the last. GTEST_DECLARE_bool_(recreate_environments_when_repeating); +// Together these flags determine which tests are run if the test is sharded. +GTEST_DECLARE_int32_(shard_index); +GTEST_DECLARE_int32_(total_shards); + // This flag controls whether Google Test includes Google Test internal // stack frames in failure stack traces. GTEST_DECLARE_bool_(show_internal_stack_frames); diff --git a/deps/googletest/src/gtest-internal-inl.h b/deps/googletest/src/gtest-internal-inl.h index 6a39b93be132e2..2ebfcd39ef5645 100644 --- a/deps/googletest/src/gtest-internal-inl.h +++ b/deps/googletest/src/gtest-internal-inl.h @@ -246,15 +246,12 @@ GTEST_API_ std::string WideStringToUtf8(const wchar_t* str, int num_chars); // be created, prints an error and exits. void WriteToShardStatusFileIfNeeded(); -// Checks whether sharding is enabled by examining the relevant -// environment variable values. If the variables are present, -// but inconsistent (e.g., shard_index >= total_shards), prints -// an error and exits. If in_subprocess_for_death_test, sharding is +// Checks whether sharding is enabled by examining the relevant flag values. +// If the flags are set, but inconsistent (e.g., shard_index >= total_shards), +// prints an error and exits. If in_subprocess_for_death_test, sharding is // disabled because it must only be applied to the original test // process. Otherwise, we could filter out death tests we intended to execute. -GTEST_API_ bool ShouldShard(const char* total_shards_str, - const char* shard_index_str, - bool in_subprocess_for_death_test); +GTEST_API_ bool ShouldShard(bool in_subprocess_for_death_test); // Parses the environment variable var as a 32-bit integer. If it is unset, // returns default_val. If it is not a 32-bit integer, prints an error and diff --git a/deps/googletest/src/gtest.cc b/deps/googletest/src/gtest.cc index 8a3801807e7dfa..ac90786a091b51 100644 --- a/deps/googletest/src/gtest.cc +++ b/deps/googletest/src/gtest.cc @@ -407,6 +407,18 @@ GTEST_DEFINE_bool_( "if exceptions are enabled or exit the program with a non-zero code " "otherwise. For use with an external test framework."); +GTEST_DEFINE_int32_( + shard_index, + testing::internal::Int32FromEnvOrDie(testing::kTestShardIndex, -1), + "The zero-based index of the shard to run. A value of -1 " + "(the default) indicates that sharding is disabled."); + +GTEST_DEFINE_int32_( + total_shards, + testing::internal::Int32FromEnvOrDie(testing::kTestTotalShards, -1), + "The total number of shards to use when running tests in parallel. " + "A value of -1 (the default) indicates that sharding is disabled."); + #if GTEST_USE_OWN_FLAGFILE_FLAG_ GTEST_DEFINE_string_( flagfile, testing::internal::StringFromGTestEnv("flagfile", ""), @@ -3475,11 +3487,11 @@ void PrettyUnitTestResultPrinter::OnTestIterationStart( filter); } - if (internal::ShouldShard(kTestTotalShards, kTestShardIndex, false)) { - const int32_t shard_index = Int32FromEnvOrDie(kTestShardIndex, -1); - ColoredPrintf(GTestColor::kYellow, "Note: This is test shard %d of %s.\n", + if (internal::ShouldShard(false)) { + const int32_t shard_index = GTEST_FLAG_GET(shard_index); + ColoredPrintf(GTestColor::kYellow, "Note: This is test shard %d of %d.\n", static_cast(shard_index) + 1, - internal::posix::GetEnv(kTestTotalShards)); + GTEST_FLAG_GET(total_shards)); } if (GTEST_FLAG_GET(shuffle)) { @@ -5983,8 +5995,7 @@ bool UnitTestImpl::RunAllTests() { #endif // defined(GTEST_EXTRA_DEATH_TEST_CHILD_SETUP_) #endif // GTEST_HAS_DEATH_TEST - const bool should_shard = ShouldShard(kTestTotalShards, kTestShardIndex, - in_subprocess_for_death_test); + const bool should_shard = ShouldShard(in_subprocess_for_death_test); // Compares the full test names with the filter to decide which // tests to run. @@ -6196,45 +6207,44 @@ void WriteToShardStatusFileIfNeeded() { } #endif // GTEST_HAS_FILE_SYSTEM -// Checks whether sharding is enabled by examining the relevant -// environment variable values. If the variables are present, -// but inconsistent (i.e., shard_index >= total_shards), prints -// an error and exits. If in_subprocess_for_death_test, sharding is -// disabled because it must only be applied to the original test -// process. Otherwise, we could filter out death tests we intended to execute. -bool ShouldShard(const char* total_shards_env, const char* shard_index_env, - bool in_subprocess_for_death_test) { +// Checks whether sharding is enabled by examining the relevant command line +// arguments. If the arguments are present, but inconsistent +// (i.e., shard_index >= total_shards), prints an error and exits. +// If in_subprocess_for_death_test, sharding is disabled because it must only +// be applied to the original test process. Otherwise, we could filter out death +// tests we intended to execute. +bool ShouldShard(bool in_subprocess_for_death_test) { if (in_subprocess_for_death_test) { return false; } - const int32_t total_shards = Int32FromEnvOrDie(total_shards_env, -1); - const int32_t shard_index = Int32FromEnvOrDie(shard_index_env, -1); + const int32_t total_shards = GTEST_FLAG_GET(total_shards); + const int32_t shard_index = GTEST_FLAG_GET(shard_index); if (total_shards == -1 && shard_index == -1) { return false; } else if (total_shards == -1 && shard_index != -1) { - const Message msg = Message() << "Invalid environment variables: you have " - << kTestShardIndex << " = " << shard_index - << ", but have left " << kTestTotalShards - << " unset.\n"; + const Message msg = Message() + << "Invalid sharding: you have " << kTestShardIndex + << " = " << shard_index << ", but have left " + << kTestTotalShards << " unset.\n"; ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); fflush(stdout); exit(EXIT_FAILURE); } else if (total_shards != -1 && shard_index == -1) { const Message msg = Message() - << "Invalid environment variables: you have " - << kTestTotalShards << " = " << total_shards - << ", but have left " << kTestShardIndex << " unset.\n"; + << "Invalid sharding: you have " << kTestTotalShards + << " = " << total_shards << ", but have left " + << kTestShardIndex << " unset.\n"; ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); fflush(stdout); exit(EXIT_FAILURE); } else if (shard_index < 0 || shard_index >= total_shards) { const Message msg = - Message() << "Invalid environment variables: we require 0 <= " - << kTestShardIndex << " < " << kTestTotalShards - << ", but you have " << kTestShardIndex << "=" << shard_index - << ", " << kTestTotalShards << "=" << total_shards << ".\n"; + Message() << "Invalid sharding: we require 0 <= " << kTestShardIndex + << " < " << kTestTotalShards << ", but you have " + << kTestShardIndex << "=" << shard_index << ", " + << kTestTotalShards << "=" << total_shards << ".\n"; ColoredPrintf(GTestColor::kRed, "%s", msg.GetString().c_str()); fflush(stdout); exit(EXIT_FAILURE); @@ -6277,11 +6287,10 @@ bool ShouldRunTestOnShard(int total_shards, int shard_index, int test_id) { // . Returns the number of tests that should run. int UnitTestImpl::FilterTests(ReactionToSharding shard_tests) { const int32_t total_shards = shard_tests == HONOR_SHARDING_PROTOCOL - ? Int32FromEnvOrDie(kTestTotalShards, -1) + ? GTEST_FLAG_GET(total_shards) : -1; - const int32_t shard_index = shard_tests == HONOR_SHARDING_PROTOCOL - ? Int32FromEnvOrDie(kTestShardIndex, -1) - : -1; + const int32_t shard_index = + shard_tests == HONOR_SHARDING_PROTOCOL ? GTEST_FLAG_GET(shard_index) : -1; const PositiveAndNegativeUnitTestFilter gtest_flag_filter( GTEST_FLAG_GET(filter)); @@ -6810,6 +6819,8 @@ static bool ParseGoogleTestFlag(const char* const arg) { GTEST_INTERNAL_PARSE_FLAG(print_utf8); GTEST_INTERNAL_PARSE_FLAG(random_seed); GTEST_INTERNAL_PARSE_FLAG(repeat); + GTEST_INTERNAL_PARSE_FLAG(shard_index); + GTEST_INTERNAL_PARSE_FLAG(total_shards); GTEST_INTERNAL_PARSE_FLAG(recreate_environments_when_repeating); GTEST_INTERNAL_PARSE_FLAG(shuffle); GTEST_INTERNAL_PARSE_FLAG(stack_trace_depth);