diff options
Diffstat (limited to 'src/test/validation_flush_tests.cpp')
-rw-r--r-- | src/test/validation_flush_tests.cpp | 33 |
1 files changed, 20 insertions, 13 deletions
diff --git a/src/test/validation_flush_tests.cpp b/src/test/validation_flush_tests.cpp index 26c48eb0e0..7398091215 100644 --- a/src/test/validation_flush_tests.cpp +++ b/src/test/validation_flush_tests.cpp @@ -36,12 +36,12 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate) BOOST_TEST_MESSAGE("CCoinsViewCache memory usage: " << view.DynamicMemoryUsage()); }; - constexpr size_t MAX_COINS_CACHE_BYTES = 1024; + // PoolResource defaults to 256 KiB that will be allocated, so we'll take that and make it a bit larger. + constexpr size_t MAX_COINS_CACHE_BYTES = 262144 + 512; // Without any coins in the cache, we shouldn't need to flush. - BOOST_CHECK_EQUAL( - chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0), - CoinsCacheSizeState::OK); + BOOST_TEST( + chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/ 0) != CoinsCacheSizeState::CRITICAL); // If the initial memory allocations of cacheCoins don't match these common // cases, we can't really continue to make assertions about memory usage. @@ -71,13 +71,21 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate) // cacheCoins (unordered_map) preallocates. constexpr int COINS_UNTIL_CRITICAL{3}; + // no coin added, so we have plenty of space left. + BOOST_CHECK_EQUAL( + chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 0), + CoinsCacheSizeState::OK); + for (int i{0}; i < COINS_UNTIL_CRITICAL; ++i) { const COutPoint res = AddTestCoin(view); print_view_mem_usage(view); BOOST_CHECK_EQUAL(view.AccessCoin(res).DynamicMemoryUsage(), COIN_SIZE); + + // adding first coin causes the MemoryResource to allocate one 256 KiB chunk of memory, + // pushing us immediately over to LARGE BOOST_CHECK_EQUAL( - chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0), - CoinsCacheSizeState::OK); + chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/ 0), + CoinsCacheSizeState::LARGE); } // Adding some additional coins will push us over the edge to CRITICAL. @@ -94,16 +102,16 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate) chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/0), CoinsCacheSizeState::CRITICAL); - // Passing non-zero max mempool usage should allow us more headroom. + // Passing non-zero max mempool usage (512 KiB) should allow us more headroom. BOOST_CHECK_EQUAL( - chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10), + chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/ 1 << 19), CoinsCacheSizeState::OK); for (int i{0}; i < 3; ++i) { AddTestCoin(view); print_view_mem_usage(view); BOOST_CHECK_EQUAL( - chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/1 << 10), + chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes=*/ 1 << 19), CoinsCacheSizeState::OK); } @@ -119,7 +127,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate) BOOST_CHECK(usage_percentage >= 0.9); BOOST_CHECK(usage_percentage < 1); BOOST_CHECK_EQUAL( - chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 1 << 10), + chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, /*max_mempool_size_bytes*/ 1 << 10), // 1024 CoinsCacheSizeState::LARGE); } @@ -131,8 +139,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate) CoinsCacheSizeState::OK); } - // Flushing the view doesn't take us back to OK because cacheCoins has - // preallocated memory that doesn't get reclaimed even after flush. + // Flushing the view does take us back to OK because ReallocateCache() is called BOOST_CHECK_EQUAL( chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0), @@ -144,7 +151,7 @@ BOOST_AUTO_TEST_CASE(getcoinscachesizestate) BOOST_CHECK_EQUAL( chainstate.GetCoinsCacheSizeState(MAX_COINS_CACHE_BYTES, 0), - CoinsCacheSizeState::CRITICAL); + CoinsCacheSizeState::OK); } BOOST_AUTO_TEST_SUITE_END() |