Skip to content

Commit 772673d

Browse files
author
MarcoFalke
committed
Merge #16978: test: Seed test RNG context for each test case, print seed
fae43a9 test: Seed test RNG context for each test case, print seed (MarcoFalke) Pull request description: Debugging failing unit tests is hard if the failure is non-deterministic and the seed is not known. Fix that by printing the seed and making it possible to set the seed from outside. ACKs for top commit: davereikher: Tested ACK fae43a9 Tree-SHA512: 33d848dd1f4180d3664ecf60e9810c2a93590c05276b2c46b1e4fe6e376b45916a46b90c803bb602750ab666da3a05ce499e550024685a90b8cc38fab6667cb8
2 parents 7d14e35 + fae43a9 commit 772673d

File tree

9 files changed

+46
-15
lines changed

9 files changed

+46
-15
lines changed

src/test/bloom_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -461,7 +461,7 @@ static std::vector<unsigned char> RandomData()
461461

462462
BOOST_AUTO_TEST_CASE(rolling_bloom)
463463
{
464-
SeedInsecureRand(/* deterministic */ true);
464+
SeedInsecureRand(SeedRand::ZEROS);
465465
g_mock_deterministic_tests = true;
466466

467467
// last-100-entry, 1% false positive:

src/test/coins_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
279279
// has the expected effect (the other duplicate is overwritten at all cache levels)
280280
BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
281281
{
282-
SeedInsecureRand(/* deterministic */ true);
282+
SeedInsecureRand(SeedRand::ZEROS);
283283
g_mock_deterministic_tests = true;
284284

285285
bool spent_a_duplicate_coinbase = false;

src/test/cuckoocache_tests.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
2929
*/
3030
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
3131
{
32-
SeedInsecureRand(true);
32+
SeedInsecureRand(SeedRand::ZEROS);
3333
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
3434
size_t megabytes = 4;
3535
cc.setup_bytes(megabytes << 20);
@@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
4747
template <typename Cache>
4848
static double test_cache(size_t megabytes, double load)
4949
{
50-
SeedInsecureRand(true);
50+
SeedInsecureRand(SeedRand::ZEROS);
5151
std::vector<uint256> hashes;
5252
Cache set{};
5353
size_t bytes = megabytes * (1 << 20);
@@ -118,7 +118,7 @@ template <typename Cache>
118118
static void test_cache_erase(size_t megabytes)
119119
{
120120
double load = 1;
121-
SeedInsecureRand(true);
121+
SeedInsecureRand(SeedRand::ZEROS);
122122
std::vector<uint256> hashes;
123123
Cache set{};
124124
size_t bytes = megabytes * (1 << 20);
@@ -181,7 +181,7 @@ template <typename Cache>
181181
static void test_cache_erase_parallel(size_t megabytes)
182182
{
183183
double load = 1;
184-
SeedInsecureRand(true);
184+
SeedInsecureRand(SeedRand::ZEROS);
185185
std::vector<uint256> hashes;
186186
Cache set{};
187187
size_t bytes = megabytes * (1 << 20);
@@ -285,7 +285,7 @@ static void test_cache_generations()
285285
// iterations with non-deterministic values, so it isn't "overfit" to the
286286
// specific entropy in FastRandomContext(true) and implementation of the
287287
// cache.
288-
SeedInsecureRand(true);
288+
SeedInsecureRand(SeedRand::ZEROS);
289289

290290
// block_activity models a chunk of network activity. n_insert elements are
291291
// added to the cache. The first and last n/4 are stored for removal later

src/test/pmt_tests.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
3030

3131
BOOST_AUTO_TEST_CASE(pmt_test1)
3232
{
33-
SeedInsecureRand(false);
3433
static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
3534

3635
for (int i = 0; i < 12; i++) {

src/test/sighash_tests.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,6 @@ BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
119119

120120
BOOST_AUTO_TEST_CASE(sighash_test)
121121
{
122-
SeedInsecureRand(false);
123-
124122
#if defined(PRINT_SIGHASH_JSON)
125123
std::cout << "[\n";
126124
std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";

src/test/streams_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
338338
BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
339339
{
340340
// Make this test deterministic.
341-
SeedInsecureRand(true);
341+
SeedInsecureRand(SeedRand::ZEROS);
342342

343343
for (int rep = 0; rep < 50; ++rep) {
344344
FILE* file = fsbridge::fopen("streams_test_tmp", "w+b");

src/test/util/setup_common.cpp

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@
3434
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
3535

3636
FastRandomContext g_insecure_rand_ctx;
37+
/** Random context to get unique temp data dirs. Separate from g_insecure_rand_ctx, which can be seeded from a const env var */
38+
static FastRandomContext g_insecure_rand_ctx_temp_path;
39+
40+
/** Return the unsigned from the environment var if available, otherwise 0 */
41+
static uint256 GetUintFromEnv(const std::string& env_name)
42+
{
43+
const char* num = std::getenv(env_name.c_str());
44+
if (!num) return {};
45+
return uint256S(num);
46+
}
47+
48+
void Seed(FastRandomContext& ctx)
49+
{
50+
// Should be enough to get the seed once for the process
51+
static uint256 seed{};
52+
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
53+
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
54+
if (seed.IsNull()) seed = GetRandHash();
55+
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
56+
ctx = FastRandomContext(seed);
57+
}
3758

3859
std::ostream& operator<<(std::ostream& os, const uint256& num)
3960
{
@@ -42,12 +63,13 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
4263
}
4364

4465
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
45-
: m_path_root(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
66+
: m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / std::to_string(g_insecure_rand_ctx_temp_path.rand32())}
4667
{
4768
fs::create_directories(m_path_root);
4869
gArgs.ForceSetArg("-datadir", m_path_root.string());
4970
ClearDatadirCache();
5071
SelectParams(chainName);
72+
SeedInsecureRand();
5173
gArgs.ForceSetArg("-printtoconsole", "0");
5274
InitLogging();
5375
LogInstance().StartLogging();

src/test/util/setup_common.h

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,21 @@ extern FastRandomContext g_insecure_rand_ctx;
3939
*/
4040
extern bool g_mock_deterministic_tests;
4141

42-
static inline void SeedInsecureRand(bool deterministic = false)
42+
enum class SeedRand {
43+
ZEROS, //!< Seed with a compile time constant of zeros
44+
SEED, //!< Call the Seed() helper
45+
};
46+
47+
/** Seed the given random ctx or use the seed passed in via an environment var */
48+
void Seed(FastRandomContext& ctx);
49+
50+
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
4351
{
44-
g_insecure_rand_ctx = FastRandomContext(deterministic);
52+
if (seed == SeedRand::ZEROS) {
53+
g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
54+
} else {
55+
Seed(g_insecure_rand_ctx);
56+
}
4557
}
4658

4759
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }

src/test/util_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1106,7 +1106,7 @@ BOOST_AUTO_TEST_CASE(util_IsHexNumber)
11061106

11071107
BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
11081108
{
1109-
SeedInsecureRand(true);
1109+
SeedInsecureRand(SeedRand::ZEROS);
11101110
for (int mod=2;mod<11;mod++)
11111111
{
11121112
int mask = 1;

0 commit comments

Comments
 (0)