Changeset View
Changeset View
Standalone View
Standalone View
src/test/avalanche_tests.cpp
Show First 20 Lines • Show All 591 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(poll_and_response) { | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(p.registerVotes(avanodeid, resp, updates)); | BOOST_CHECK(p.registerVotes(avanodeid, resp, updates)); | ||||
BOOST_CHECK_EQUAL(updates.size(), 0); | BOOST_CHECK_EQUAL(updates.size(), 0); | ||||
BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), avanodeid); | BOOST_CHECK_EQUAL(AvalancheTest::getSuitableNodeToQuery(p), avanodeid); | ||||
CConnmanTest::ClearNodes(); | CConnmanTest::ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(poll_inflight_timeout) { | BOOST_AUTO_TEST_CASE(poll_inflight_timeout, *boost::unit_test::timeout(60)) { | ||||
AvalancheProcessor p(g_connman.get()); | AvalancheProcessor p(g_connman.get()); | ||||
std::vector<AvalancheBlockUpdate> updates; | std::vector<AvalancheBlockUpdate> updates; | ||||
CBlock block = CreateAndProcessBlock({}, CScript()); | CBlock block = CreateAndProcessBlock({}, CScript()); | ||||
const uint256 blockHash = block.GetHash(); | const uint256 blockHash = block.GetHash(); | ||||
const CBlockIndex *pindex = mapBlockIndex[blockHash]; | const CBlockIndex *pindex = mapBlockIndex[blockHash]; | ||||
// Add the block | // Add the block | ||||
BOOST_CHECK(p.addBlockToReconcile(pindex)); | BOOST_CHECK(p.addBlockToReconcile(pindex)); | ||||
// Create a node that supports avalanche. | // Create a node that supports avalanche. | ||||
const Config &config = GetConfig(); | const Config &config = GetConfig(); | ||||
auto avanode = ConnectNode(config, NODE_AVALANCHE, *peerLogic); | auto avanode = ConnectNode(config, NODE_AVALANCHE, *peerLogic); | ||||
NodeId avanodeid = avanode->GetId(); | NodeId avanodeid = avanode->GetId(); | ||||
BOOST_CHECK(p.addPeer(avanodeid, 0)); | BOOST_CHECK(p.addPeer(avanodeid, 0)); | ||||
// Expire requests after some time. | // Expire requests after some time. | ||||
p.setQueryTimeoutDuration(std::chrono::milliseconds(10)); | auto queryTimeDuration = std::chrono::milliseconds(10); | ||||
p.setQueryTimeoutDuration(queryTimeDuration); | |||||
for (int i = 0; i < 10; i++) { | for (int i = 0; i < 10; i++) { | ||||
AvalancheResponse resp = { | AvalancheResponse resp = { | ||||
AvalancheTest::getRound(p), 0, {AvalancheVote(0, blockHash)}}; | AvalancheTest::getRound(p), 0, {AvalancheVote(0, blockHash)}}; | ||||
auto start = std::chrono::steady_clock::now(); | |||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
// NB: This could wait longer than 1ms in some cases and make the | // We cannot guarantee that we'll wait for just 1ms, so we have to bail | ||||
// test flacky. We'll have to come up with a better solution to test | // if we aren't within the proper time range. | ||||
// this if that were to be the case. I never was able to trigger this | std::this_thread::sleep_for(std::chrono::milliseconds(1)); | ||||
// myself, so it's probably good enough. | |||||
boost::this_thread::sleep_for(boost::chrono::milliseconds(1)); | |||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(p.registerVotes(avanodeid, next(resp), updates)); | |||||
// Now try again but wait. | bool ret = p.registerVotes(avanodeid, next(resp), updates); | ||||
if (std::chrono::steady_clock::now() > start + queryTimeDuration) { | |||||
// We waited for too long, bail. Because we can't know for sure when | |||||
// previous steps ran, ret is not deterministic and we do not check | |||||
// it. | |||||
i--; | |||||
continue; | |||||
} | |||||
// We are within time bounds, so the vote should have worked. | |||||
BOOST_CHECK(ret); | |||||
// Now try again but wait for expiration. | |||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
boost::this_thread::sleep_for(boost::chrono::milliseconds(10)); | std::this_thread::sleep_for(queryTimeDuration); | ||||
AvalancheTest::runEventLoop(p); | AvalancheTest::runEventLoop(p); | ||||
BOOST_CHECK(!p.registerVotes(avanodeid, next(resp), updates)); | BOOST_CHECK(!p.registerVotes(avanodeid, next(resp), updates)); | ||||
} | } | ||||
CConnmanTest::ClearNodes(); | CConnmanTest::ClearNodes(); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(poll_inflight_count) { | BOOST_AUTO_TEST_CASE(poll_inflight_count) { | ||||
▲ Show 20 Lines • Show All 226 Lines • Show Last 20 Lines |