Skip to content

Commit 2c3cbd6

Browse files
committed
test: add use of Sync() to coins tests
1 parent 6d8affc commit 2c3cbd6

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/test/coins_tests.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class CCoinsViewTest : public CCoinsView
5555

5656
bool BatchWrite(CCoinsMap& mapCoins, const uint256& hashBlock, bool erase = true) override
5757
{
58-
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); ) {
58+
for (CCoinsMap::iterator it = mapCoins.begin(); it != mapCoins.end(); it = erase ? mapCoins.erase(it) : ++it) {
5959
if (it->second.flags & CCoinsCacheEntry::DIRTY) {
6060
// Same optimization used in CCoinsViewDB is to only write dirty entries.
6161
map_[it->first] = it->second.coin;
@@ -64,7 +64,6 @@ class CCoinsViewTest : public CCoinsView
6464
map_.erase(it->first);
6565
}
6666
}
67-
mapCoins.erase(it++);
6867
}
6968
if (!hashBlock.IsNull())
7069
hashBestBlock_ = hashBlock;
@@ -126,6 +125,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
126125
bool found_an_entry = false;
127126
bool missed_an_entry = false;
128127
bool uncached_an_entry = false;
128+
bool flushed_without_erase = false;
129129

130130
// A simple map to track what we expect the cache stack to represent.
131131
std::map<COutPoint, Coin> result;
@@ -228,15 +228,19 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
228228
if (stack.size() > 1 && InsecureRandBool() == 0) {
229229
unsigned int flushIndex = InsecureRandRange(stack.size() - 1);
230230
if (fake_best_block) stack[flushIndex]->SetBestBlock(InsecureRand256());
231-
BOOST_CHECK(stack[flushIndex]->Flush());
231+
bool should_erase = InsecureRandRange(4) < 3;
232+
BOOST_CHECK(should_erase ? stack[flushIndex]->Flush() : stack[flushIndex]->Sync());
233+
flushed_without_erase |= !should_erase;
232234
}
233235
}
234236
if (InsecureRandRange(100) == 0) {
235237
// Every 100 iterations, change the cache stack.
236238
if (stack.size() > 0 && InsecureRandBool() == 0) {
237239
//Remove the top cache
238240
if (fake_best_block) stack.back()->SetBestBlock(InsecureRand256());
239-
BOOST_CHECK(stack.back()->Flush());
241+
bool should_erase = InsecureRandRange(4) < 3;
242+
BOOST_CHECK(should_erase ? stack.back()->Flush() : stack.back()->Sync());
243+
flushed_without_erase |= !should_erase;
240244
delete stack.back();
241245
stack.pop_back();
242246
}
@@ -272,6 +276,7 @@ void SimulationTest(CCoinsView* base, bool fake_best_block)
272276
BOOST_CHECK(found_an_entry);
273277
BOOST_CHECK(missed_an_entry);
274278
BOOST_CHECK(uncached_an_entry);
279+
BOOST_CHECK(flushed_without_erase);
275280
}
276281

277282
// Run the above simulation for multiple base types.

0 commit comments

Comments
 (0)