Merge #16978: test: Seed test RNG context for each test case, print seed
fae43a97ca
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 ACKfae43a97ca
Tree-SHA512: 33d848dd1f4180d3664ecf60e9810c2a93590c05276b2c46b1e4fe6e376b45916a46b90c803bb602750ab666da3a05ce499e550024685a90b8cc38fab6667cb8
This commit is contained in:
commit
772673dfbe
9 changed files with 46 additions and 15 deletions
|
@ -461,7 +461,7 @@ static std::vector<unsigned char> RandomData()
|
|||
|
||||
BOOST_AUTO_TEST_CASE(rolling_bloom)
|
||||
{
|
||||
SeedInsecureRand(/* deterministic */ true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
g_mock_deterministic_tests = true;
|
||||
|
||||
// last-100-entry, 1% false positive:
|
||||
|
|
|
@ -279,7 +279,7 @@ UtxoData::iterator FindRandomFrom(const std::set<COutPoint> &utxoSet) {
|
|||
// has the expected effect (the other duplicate is overwritten at all cache levels)
|
||||
BOOST_AUTO_TEST_CASE(updatecoins_simulation_test)
|
||||
{
|
||||
SeedInsecureRand(/* deterministic */ true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
g_mock_deterministic_tests = true;
|
||||
|
||||
bool spent_a_duplicate_coinbase = false;
|
||||
|
|
|
@ -29,7 +29,7 @@ BOOST_AUTO_TEST_SUITE(cuckoocache_tests);
|
|||
*/
|
||||
BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
||||
{
|
||||
SeedInsecureRand(true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
CuckooCache::cache<uint256, SignatureCacheHasher> cc{};
|
||||
size_t megabytes = 4;
|
||||
cc.setup_bytes(megabytes << 20);
|
||||
|
@ -47,7 +47,7 @@ BOOST_AUTO_TEST_CASE(test_cuckoocache_no_fakes)
|
|||
template <typename Cache>
|
||||
static double test_cache(size_t megabytes, double load)
|
||||
{
|
||||
SeedInsecureRand(true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
std::vector<uint256> hashes;
|
||||
Cache set{};
|
||||
size_t bytes = megabytes * (1 << 20);
|
||||
|
@ -118,7 +118,7 @@ template <typename Cache>
|
|||
static void test_cache_erase(size_t megabytes)
|
||||
{
|
||||
double load = 1;
|
||||
SeedInsecureRand(true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
std::vector<uint256> hashes;
|
||||
Cache set{};
|
||||
size_t bytes = megabytes * (1 << 20);
|
||||
|
@ -181,7 +181,7 @@ template <typename Cache>
|
|||
static void test_cache_erase_parallel(size_t megabytes)
|
||||
{
|
||||
double load = 1;
|
||||
SeedInsecureRand(true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
std::vector<uint256> hashes;
|
||||
Cache set{};
|
||||
size_t bytes = megabytes * (1 << 20);
|
||||
|
@ -285,7 +285,7 @@ static void test_cache_generations()
|
|||
// iterations with non-deterministic values, so it isn't "overfit" to the
|
||||
// specific entropy in FastRandomContext(true) and implementation of the
|
||||
// cache.
|
||||
SeedInsecureRand(true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
|
||||
// block_activity models a chunk of network activity. n_insert elements are
|
||||
// added to the cache. The first and last n/4 are stored for removal later
|
||||
|
|
|
@ -30,7 +30,6 @@ BOOST_FIXTURE_TEST_SUITE(pmt_tests, BasicTestingSetup)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(pmt_test1)
|
||||
{
|
||||
SeedInsecureRand(false);
|
||||
static const unsigned int nTxCounts[] = {1, 4, 7, 17, 56, 100, 127, 256, 312, 513, 1000, 4095};
|
||||
|
||||
for (int i = 0; i < 12; i++) {
|
||||
|
|
|
@ -119,8 +119,6 @@ BOOST_FIXTURE_TEST_SUITE(sighash_tests, BasicTestingSetup)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(sighash_test)
|
||||
{
|
||||
SeedInsecureRand(false);
|
||||
|
||||
#if defined(PRINT_SIGHASH_JSON)
|
||||
std::cout << "[\n";
|
||||
std::cout << "\t[\"raw_transaction, script, input_index, hashType, signature_hash (result)\"],\n";
|
||||
|
|
|
@ -338,7 +338,7 @@ BOOST_AUTO_TEST_CASE(streams_buffered_file)
|
|||
BOOST_AUTO_TEST_CASE(streams_buffered_file_rand)
|
||||
{
|
||||
// Make this test deterministic.
|
||||
SeedInsecureRand(true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
|
||||
for (int rep = 0; rep < 50; ++rep) {
|
||||
FILE* file = fsbridge::fopen("streams_test_tmp", "w+b");
|
||||
|
|
|
@ -34,6 +34,27 @@
|
|||
const std::function<std::string(const char*)> G_TRANSLATION_FUN = nullptr;
|
||||
|
||||
FastRandomContext g_insecure_rand_ctx;
|
||||
/** Random context to get unique temp data dirs. Separate from g_insecure_rand_ctx, which can be seeded from a const env var */
|
||||
static FastRandomContext g_insecure_rand_ctx_temp_path;
|
||||
|
||||
/** Return the unsigned from the environment var if available, otherwise 0 */
|
||||
static uint256 GetUintFromEnv(const std::string& env_name)
|
||||
{
|
||||
const char* num = std::getenv(env_name.c_str());
|
||||
if (!num) return {};
|
||||
return uint256S(num);
|
||||
}
|
||||
|
||||
void Seed(FastRandomContext& ctx)
|
||||
{
|
||||
// Should be enough to get the seed once for the process
|
||||
static uint256 seed{};
|
||||
static const std::string RANDOM_CTX_SEED{"RANDOM_CTX_SEED"};
|
||||
if (seed.IsNull()) seed = GetUintFromEnv(RANDOM_CTX_SEED);
|
||||
if (seed.IsNull()) seed = GetRandHash();
|
||||
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
|
||||
ctx = FastRandomContext(seed);
|
||||
}
|
||||
|
||||
std::ostream& operator<<(std::ostream& os, const uint256& num)
|
||||
{
|
||||
|
@ -42,12 +63,13 @@ std::ostream& operator<<(std::ostream& os, const uint256& num)
|
|||
}
|
||||
|
||||
BasicTestingSetup::BasicTestingSetup(const std::string& chainName)
|
||||
: m_path_root(fs::temp_directory_path() / "test_common_" PACKAGE_NAME / strprintf("%lu_%i", (unsigned long)GetTime(), (int)(InsecureRandRange(1 << 30))))
|
||||
: m_path_root{fs::temp_directory_path() / "test_common_" PACKAGE_NAME / std::to_string(g_insecure_rand_ctx_temp_path.rand32())}
|
||||
{
|
||||
fs::create_directories(m_path_root);
|
||||
gArgs.ForceSetArg("-datadir", m_path_root.string());
|
||||
ClearDatadirCache();
|
||||
SelectParams(chainName);
|
||||
SeedInsecureRand();
|
||||
gArgs.ForceSetArg("-printtoconsole", "0");
|
||||
InitLogging();
|
||||
LogInstance().StartLogging();
|
||||
|
|
|
@ -39,9 +39,21 @@ extern FastRandomContext g_insecure_rand_ctx;
|
|||
*/
|
||||
extern bool g_mock_deterministic_tests;
|
||||
|
||||
static inline void SeedInsecureRand(bool deterministic = false)
|
||||
enum class SeedRand {
|
||||
ZEROS, //!< Seed with a compile time constant of zeros
|
||||
SEED, //!< Call the Seed() helper
|
||||
};
|
||||
|
||||
/** Seed the given random ctx or use the seed passed in via an environment var */
|
||||
void Seed(FastRandomContext& ctx);
|
||||
|
||||
static inline void SeedInsecureRand(SeedRand seed = SeedRand::SEED)
|
||||
{
|
||||
g_insecure_rand_ctx = FastRandomContext(deterministic);
|
||||
if (seed == SeedRand::ZEROS) {
|
||||
g_insecure_rand_ctx = FastRandomContext(/* deterministic */ true);
|
||||
} else {
|
||||
Seed(g_insecure_rand_ctx);
|
||||
}
|
||||
}
|
||||
|
||||
static inline uint32_t InsecureRand32() { return g_insecure_rand_ctx.rand32(); }
|
||||
|
|
|
@ -1106,7 +1106,7 @@ BOOST_AUTO_TEST_CASE(util_IsHexNumber)
|
|||
|
||||
BOOST_AUTO_TEST_CASE(util_seed_insecure_rand)
|
||||
{
|
||||
SeedInsecureRand(true);
|
||||
SeedInsecureRand(SeedRand::ZEROS);
|
||||
for (int mod=2;mod<11;mod++)
|
||||
{
|
||||
int mask = 1;
|
||||
|
|
Loading…
Reference in a new issue