Changeset View
Changeset View
Standalone View
Standalone View
src/test/avalanche_tests.cpp
Show All 14 Lines | struct AvalancheTest { | ||||
static std::vector<CInv> getInvsForNextPoll(const AvalancheProcessor &p) { | static std::vector<CInv> getInvsForNextPoll(const AvalancheProcessor &p) { | ||||
return p.getInvsForNextPoll(); | return p.getInvsForNextPoll(); | ||||
} | } | ||||
static NodeId getSuitableNodeToQuery(AvalancheProcessor &p) { | static NodeId getSuitableNodeToQuery(AvalancheProcessor &p) { | ||||
return p.getSuitableNodeToQuery(); | return p.getSuitableNodeToQuery(); | ||||
} | } | ||||
static uint32_t getRound(const AvalancheProcessor &p) { return p.round; } | |||||
}; | }; | ||||
BOOST_FIXTURE_TEST_SUITE(avalanche_tests, TestChain100Setup) | BOOST_FIXTURE_TEST_SUITE(avalanche_tests, TestChain100Setup) | ||||
#define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | #define REGISTER_VOTE_AND_CHECK(vr, vote, state, finalized, confidence) \ | ||||
vr.registerVote(vote); \ | vr.registerVote(vote); \ | ||||
BOOST_CHECK_EQUAL(vr.isAccepted(), state); \ | BOOST_CHECK_EQUAL(vr.isAccepted(), state); \ | ||||
BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | BOOST_CHECK_EQUAL(vr.hasFinalized(), finalized); \ | ||||
▲ Show 20 Lines • Show All 439 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(event_loop) { | ||||
NodeId nodeid = avanode->GetId(); | NodeId nodeid = avanode->GetId(); | ||||
// There is no query in flight at the moment. | // There is no query in flight at the moment. | ||||
BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), nodeid); | BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), nodeid); | ||||
// Add a new block. Check it is added to the polls. | // Add a new block. Check it is added to the polls. | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
bool hasQueried = false; | uint32_t round = AvalancheTest::getRound(p); | ||||
for (int i = 0; i < 1000; i++) { | for (int i = 0; i < 1000; i++) { | ||||
// Technically, this is a race condition, but this should do just fine | // Technically, this is a race condition, but this should do just fine | ||||
// as we wait up to 1s for an event that should take 10ms. | // as we wait up to 1s for an event that should take 10ms. | ||||
boost::this_thread::sleep_for(boost::chrono::milliseconds(1)); | boost::this_thread::sleep_for(boost::chrono::milliseconds(1)); | ||||
if (AvalancheTest::getSuitableNodeToQuery(p) == -1) { | if (AvalancheTest::getRound(p) != round) { | ||||
hasQueried = true; | break; | ||||
} | |||||
} | |||||
// Check that we effectively got a request and not timed out. | |||||
BOOST_CHECK(AvalancheTest::getRound(p) > round); | |||||
// Respond and check the cooldown time is respected. | |||||
round = AvalancheTest::getRound(p); | |||||
auto queryTime = | |||||
std::chrono::steady_clock::now() + std::chrono::milliseconds(100); | |||||
std::vector<AvalancheBlockUpdate> updates; | |||||
p.registerVotes(nodeid, {100, {AvalancheVote(0, blockHash)}}, updates); | |||||
for (int i = 0; i < 1000; i++) { | |||||
// We make sure that we do not get a request before queryTime. | |||||
boost::this_thread::sleep_for(boost::chrono::milliseconds(1)); | |||||
if (AvalancheTest::getRound(p) != round) { | |||||
BOOST_CHECK(std::chrono::steady_clock::now() > queryTime); | |||||
break; | break; | ||||
} | } | ||||
} | } | ||||
BOOST_CHECK(hasQueried); | // But we eventually get one. | ||||
BOOST_CHECK(AvalancheTest::getRound(p) > round); | |||||
// Stop event loop. | // Stop event loop. | ||||
BOOST_CHECK(p.stopEventLoop()); | BOOST_CHECK(p.stopEventLoop()); | ||||
// We don't have any task scheduled anymore. | // We don't have any task scheduled anymore. | ||||
BOOST_CHECK_EQUAL(s.getQueueInfo(start, stop), 0); | BOOST_CHECK_EQUAL(s.getQueueInfo(start, stop), 0); | ||||
// Can't stop the event loop twice. | // Can't stop the event loop twice. | ||||
Show All 31 Lines |