Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | BOOST_AUTO_TEST_CASE(select_peer_dichotomic) { | ||||
BOOST_CHECK_EQUAL(selectPeerImpl(slots, 200, max), 99); | BOOST_CHECK_EQUAL(selectPeerImpl(slots, 200, max), 99); | ||||
BOOST_CHECK_EQUAL(selectPeerImpl(slots, 256, max), 99); | BOOST_CHECK_EQUAL(selectPeerImpl(slots, 256, max), 99); | ||||
BOOST_CHECK_EQUAL(selectPeerImpl(slots, 299, max), 99); | BOOST_CHECK_EQUAL(selectPeerImpl(slots, 299, max), 99); | ||||
BOOST_CHECK_EQUAL(selectPeerImpl(slots, 300, max), NO_PEER); | BOOST_CHECK_EQUAL(selectPeerImpl(slots, 300, max), NO_PEER); | ||||
// Update the slots to be heavily skewed toward the first element. | // Update the slots to be heavily skewed toward the first element. | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
slots[i] = Slot(slots[i].getStart() + 100, slots[i].getScore(), | slots[i] = slots[i].withStart(slots[i].getStart() + 100); | ||||
slots[i].getPeerId()); | |||||
} | } | ||||
slots[0] = Slot(1, slots[0].getStop() - 1, slots[0].getPeerId()); | slots[0] = Slot(1, slots[0].getStop() - 1, slots[0].getPeerId()); | ||||
slots[99] = slots[99].withScore(1); | slots[99] = slots[99].withScore(1); | ||||
max = slots[99].getStop(); | max = slots[99].getStop(); | ||||
BOOST_CHECK_EQUAL(max, 300); | BOOST_CHECK_EQUAL(max, 300); | ||||
BOOST_CHECK_EQUAL(selectPeerImpl(slots, 0, max), NO_PEER); | BOOST_CHECK_EQUAL(selectPeerImpl(slots, 0, max), NO_PEER); | ||||
▲ Show 20 Lines • Show All 123 Lines • ▼ Show 20 Lines | for (int i = 0; i < 100; i++) { | ||||
p == peerids[5] || p == peerids[6]); | p == peerids[5] || p == peerids[6]); | ||||
} | } | ||||
// Removing non existent peers fails. | // Removing non existent peers fails. | ||||
BOOST_CHECK(!pm.removePeer(peerids[0])); | BOOST_CHECK(!pm.removePeer(peerids[0])); | ||||
BOOST_CHECK(!pm.removePeer(peerids[2])); | BOOST_CHECK(!pm.removePeer(peerids[2])); | ||||
BOOST_CHECK(!pm.removePeer(peerids[7])); | BOOST_CHECK(!pm.removePeer(peerids[7])); | ||||
BOOST_CHECK(!pm.removePeer(NO_PEER)); | BOOST_CHECK(!pm.removePeer(NO_PEER)); | ||||
// Compact the peer manager. | |||||
BOOST_CHECK_EQUAL(pm.compact(), 200); | |||||
BOOST_CHECK(pm.verify()); | |||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 500); | |||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(rescore_peer, *boost::unit_test::timeout(5)) { | BOOST_AUTO_TEST_CASE(rescore_peer, *boost::unit_test::timeout(5)) { | ||||
// No peers. | // No peers. | ||||
PeerManager pm; | PeerManager pm; | ||||
BOOST_CHECK_EQUAL(pm.selectPeer(), NO_PEER); | BOOST_CHECK_EQUAL(pm.selectPeer(), NO_PEER); | ||||
// Add 4 peers. | // Add 4 peers. | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | for (int i = 0; i < 25; i++) { | ||||
BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] || | BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] || | ||||
p == peerids[3]); | p == peerids[3]); | ||||
// Make sure peer 1 reappeared. | // Make sure peer 1 reappeared. | ||||
if (p == peerids[1]) { | if (p == peerids[1]) { | ||||
break; | break; | ||||
} | } | ||||
} | } | ||||
} | } | ||||
// Compact the peer manager. | |||||
BOOST_CHECK_EQUAL(pm.compact(), 100); | |||||
BOOST_CHECK(pm.verify()); | |||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 500); | |||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | |||||
} | |||||
BOOST_AUTO_TEST_CASE(compact_slots) { | |||||
PeerManager pm; | |||||
// Add 4 peers. | |||||
std::array<PeerId, 4> peerids; | |||||
for (int i = 0; i < 4; i++) { | |||||
peerids[i] = pm.addPeer(100); | |||||
} | |||||
// Remove all peers. | |||||
for (auto p : peerids) { | |||||
pm.removePeer(p); | |||||
} | |||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 300); | |||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 300); | |||||
BOOST_CHECK_EQUAL(pm.compact(), 300); | |||||
BOOST_CHECK(pm.verify()); | |||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 0); | |||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | |||||
} | } | ||||
BOOST_AUTO_TEST_SUITE_END() | BOOST_AUTO_TEST_SUITE_END() |