Skip to content

Commit 424327e

Browse files
committed
Merge #15324: test: Make bloom tests deterministic
fae169c test: Make bloom tests deterministic (MarcoFalke) Pull request description: non-deterministic tests are useless, since a failing test could not be reproduced unless the seed is known. Tree-SHA512: 4f634ff0c6adf663444f1ac504f6dbceaa46b78d697b840531977ba30006453ac559d5c21cc3eaef6d92b87d46008a34b0db6331ea3318001987fcfaec634acf
2 parents 64127b3 + fae169c commit 424327e

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

src/random.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,9 +514,11 @@ void GetRandBytes(unsigned char* buf, int num) noexcept { ProcRand(buf, num, RNG
514514
void GetStrongRandBytes(unsigned char* buf, int num) noexcept { ProcRand(buf, num, RNGLevel::SLOW); }
515515
void RandAddSeedSleep() { ProcRand(nullptr, 0, RNGLevel::SLEEP); }
516516

517+
bool g_mock_deterministic_tests{false};
518+
517519
uint64_t GetRand(uint64_t nMax) noexcept
518520
{
519-
return FastRandomContext().randrange(nMax);
521+
return FastRandomContext(g_mock_deterministic_tests).randrange(nMax);
520522
}
521523

522524
int GetRandInt(int nMax) noexcept

src/test/bloom_tests.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,9 @@ static std::vector<unsigned char> RandomData()
461461

462462
BOOST_AUTO_TEST_CASE(rolling_bloom)
463463
{
464+
SeedInsecureRand(/* deterministic */ true);
465+
g_mock_deterministic_tests = true;
466+
464467
// last-100-entry, 1% false positive:
465468
CRollingBloomFilter rb1(100, 0.01);
466469

@@ -485,12 +488,8 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
485488
if (rb1.contains(RandomData()))
486489
++nHits;
487490
}
488-
// Run test_bitcoin with --log_level=message to see BOOST_TEST_MESSAGEs:
489-
BOOST_TEST_MESSAGE("RollingBloomFilter got " << nHits << " false positives (~100 expected)");
490-
491-
// Insanely unlikely to get a fp count outside this range:
492-
BOOST_CHECK(nHits > 25);
493-
BOOST_CHECK(nHits < 175);
491+
// Expect about 100 hits
492+
BOOST_CHECK_EQUAL(nHits, 75);
494493

495494
BOOST_CHECK(rb1.contains(data[DATASIZE-1]));
496495
rb1.reset();
@@ -517,10 +516,8 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
517516
if (rb1.contains(data[i]))
518517
++nHits;
519518
}
520-
// Expect about 5 false positives, more than 100 means
521-
// something is definitely broken.
522-
BOOST_TEST_MESSAGE("RollingBloomFilter got " << nHits << " false positives (~5 expected)");
523-
BOOST_CHECK(nHits < 100);
519+
// Expect about 5 false positives
520+
BOOST_CHECK_EQUAL(nHits, 6);
524521

525522
// last-1000-entry, 0.01% false positive:
526523
CRollingBloomFilter rb2(1000, 0.001);
@@ -531,6 +528,7 @@ BOOST_AUTO_TEST_CASE(rolling_bloom)
531528
for (int i = 0; i < DATASIZE; i++) {
532529
BOOST_CHECK(rb2.contains(data[i]));
533530
}
531+
g_mock_deterministic_tests = false;
534532
}
535533

536534
BOOST_AUTO_TEST_SUITE_END()

src/test/random_tests.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,14 @@ BOOST_AUTO_TEST_CASE(osrandom_tests)
2121
BOOST_AUTO_TEST_CASE(fastrandom_tests)
2222
{
2323
// Check that deterministic FastRandomContexts are deterministic
24+
g_mock_deterministic_tests = true;
2425
FastRandomContext ctx1(true);
2526
FastRandomContext ctx2(true);
2627

28+
for (int i = 10; i > 0; --i) {
29+
BOOST_CHECK_EQUAL(GetRand(std::numeric_limits<uint64_t>::max()), uint64_t{10393729187455219830U});
30+
BOOST_CHECK_EQUAL(GetRandInt(std::numeric_limits<int>::max()), int{769702006});
31+
}
2732
BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
2833
BOOST_CHECK_EQUAL(ctx1.rand32(), ctx2.rand32());
2934
BOOST_CHECK_EQUAL(ctx1.rand64(), ctx2.rand64());
@@ -38,6 +43,11 @@ BOOST_AUTO_TEST_CASE(fastrandom_tests)
3843
BOOST_CHECK(ctx1.randbytes(50) == ctx2.randbytes(50));
3944

4045
// Check that a nondeterministic ones are not
46+
g_mock_deterministic_tests = false;
47+
for (int i = 10; i > 0; --i) {
48+
BOOST_CHECK(GetRand(std::numeric_limits<uint64_t>::max()) != uint64_t{10393729187455219830U});
49+
BOOST_CHECK(GetRandInt(std::numeric_limits<int>::max()) != int{769702006});
50+
}
4151
{
4252
FastRandomContext ctx3, ctx4;
4353
BOOST_CHECK(ctx3.rand64() != ctx4.rand64()); // extremely unlikely to be equal

src/test/test_bitcoin.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,11 @@ std::ostream& operator<<(typename std::enable_if<std::is_enum<T>::value, std::os
3535
*/
3636
extern FastRandomContext g_insecure_rand_ctx;
3737

38+
/**
39+
* Flag to make GetRand in random.h return the same number
40+
*/
41+
extern bool g_mock_deterministic_tests;
42+
3843
static inline void SeedInsecureRand(bool deterministic = false)
3944
{
4045
g_insecure_rand_ctx = FastRandomContext(deterministic);

0 commit comments

Comments
 (0)