Skip to content

Commit 44dc3cc

Browse files
committed
Emit warning when using --shard-count (> 1) with --order rand
1 parent 8b08d4d commit 44dc3cc

6 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/catch2/catch_config.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@ namespace Catch {
207207
double Config::minDuration() const { return m_data.minDuration; }
208208
TestRunOrder Config::runOrder() const { return m_data.runOrder; }
209209
uint32_t Config::rngSeed() const { return m_data.rngSeed; }
210+
bool Config::rngSeedWasFixed() const { return m_data.rngSeedWasFixed; }
210211
unsigned int Config::shardCount() const { return m_data.shardCount; }
211212
unsigned int Config::shardIndex() const { return m_data.shardIndex; }
212213
ColourMode Config::defaultColourMode() const { return m_data.defaultColourMode; }
@@ -271,6 +272,7 @@ namespace Catch {
271272
<< bazelRandomSeed << "') as proper seed.\n";
272273
} else {
273274
m_data.rngSeed = *parsedSeed;
275+
m_data.rngSeedWasFixed = true;
274276
}
275277
}
276278
}

src/catch2/catch_config.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ namespace Catch {
6262

6363
int abortAfter = -1;
6464
uint32_t rngSeed = generateRandomSeed(GenerateFrom::Default);
65+
bool rngSeedWasFixed = false;
6566

6667
unsigned int shardCount = 1;
6768
unsigned int shardIndex = 0;
@@ -133,6 +134,7 @@ namespace Catch {
133134
double minDuration() const override;
134135
TestRunOrder runOrder() const override;
135136
uint32_t rngSeed() const override;
137+
bool rngSeedWasFixed() const;
136138
unsigned int shardCount() const override;
137139
unsigned int shardIndex() const override;
138140
ColourMode defaultColourMode() const override;

src/catch2/catch_session.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,19 @@ namespace Catch {
346346
CATCH_TRY {
347347
config(); // Force config to be constructed
348348

349+
if ( m_config->shardCount() > 1 &&
350+
m_config->runOrder() == TestRunOrder::Randomized &&
351+
!m_config->rngSeedWasFixed() ) {
352+
Catch::cerr()
353+
<< "Warning: using sharding (--shard-count) with random "
354+
"order (--order rand, the default) and without a fixed "
355+
"numeric --rng-seed does not guarantee disjoint coverage "
356+
"between shard invocations. Pass the same numeric "
357+
"--rng-seed to every shard, or use --order decl or "
358+
"--order lex instead.\n"
359+
<< std::flush;
360+
}
361+
349362
// We need to retrieve potential Bazel config with the full Config
350363
// constructor, so we have to create the guard file after it is created.
351364
setUpGuardFile( m_config->getExitGuardFilePath() );

src/catch2/internal/catch_commandline.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,11 @@ namespace Catch {
7575
auto const setRngSeed = [&]( std::string const& seed ) {
7676
if( seed == "time" ) {
7777
config.rngSeed = generateRandomSeed(GenerateFrom::Time);
78+
config.rngSeedWasFixed = false;
7879
return ParserResult::ok(ParseResultType::Matched);
7980
} else if (seed == "random-device") {
8081
config.rngSeed = generateRandomSeed(GenerateFrom::RandomDevice);
82+
config.rngSeedWasFixed = false;
8183
return ParserResult::ok(ParseResultType::Matched);
8284
}
8385

@@ -88,6 +90,7 @@ namespace Catch {
8890
return ParserResult::runtimeError( "Could not parse '" + seed + "' as seed" );
8991
}
9092
config.rngSeed = *parsedSeed;
93+
config.rngSeedWasFixed = true;
9194
return ParserResult::ok( ParseResultType::Matched );
9295
};
9396
auto const setDefaultColourMode = [&]( std::string const& colourMode ) {

tests/ExtraTests/CMakeLists.txt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,24 @@ set_tests_properties(TestSharding::OverlyLargeShardIndex
2121
PASS_REGULAR_EXPRESSION "The shard count \\(5\\) must be greater than the shard index \\(5\\)"
2222
)
2323

24+
add_test(
25+
NAME TestSharding::WarningOnRandomOrderWithoutFixedSeed
26+
COMMAND $<TARGET_FILE:SelfTest> --shard-index 0 --shard-count 2 --list-tests
27+
)
28+
set_tests_properties(TestSharding::WarningOnRandomOrderWithoutFixedSeed
29+
PROPERTIES
30+
PASS_REGULAR_EXPRESSION "Warning: using sharding \\(--shard-count\\) with random order"
31+
)
32+
33+
add_test(
34+
NAME TestSharding::NoWarningOnRandomOrderWithFixedSeed
35+
COMMAND $<TARGET_FILE:SelfTest> --shard-index 0 --shard-count 2 --rng-seed 12345 --list-tests
36+
)
37+
set_tests_properties(TestSharding::NoWarningOnRandomOrderWithFixedSeed
38+
PROPERTIES
39+
FAIL_REGULAR_EXPRESSION "Warning: using sharding \\(--shard-count\\) with random order"
40+
)
41+
2442
# The MinDuration reporting tests do not need separate compilation, but
2543
# they have non-trivial execution time, so they are categorized as
2644
# extra tests, so that they are run less.

tests/SelfTest/IntrospectiveTests/CmdLine.tests.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -475,12 +475,24 @@ TEST_CASE( "Parse rng seed in different formats", "[approvals][cli][rng-seed]" )
475475

476476
REQUIRE( result );
477477
REQUIRE( config.rngSeed == seed_value );
478+
REQUIRE( config.rngSeedWasFixed );
479+
}
480+
SECTION( "time seed is not considered fixed" ) {
481+
auto result = cli.parse( { "tests", "--rng-seed", "time" } );
482+
REQUIRE( result );
483+
REQUIRE_FALSE( config.rngSeedWasFixed );
484+
}
485+
SECTION( "random-device seed is not considered fixed" ) {
486+
auto result = cli.parse( { "tests", "--rng-seed", "random-device" } );
487+
REQUIRE( result );
488+
REQUIRE_FALSE( config.rngSeedWasFixed );
478489
}
479490
SECTION( "Error cases" ) {
480491
auto seed_string =
481492
GENERATE( "0xSEED", "999999999999", "08888", "BEEF", "123 456" );
482493
CAPTURE( seed_string );
483494
REQUIRE_FALSE( cli.parse( { "tests", "--rng-seed", seed_string } ) );
495+
REQUIRE_FALSE( config.rngSeedWasFixed );
484496
}
485497
}
486498

0 commit comments

Comments
 (0)