Changeset View
Changeset View
Standalone View
Standalone View
src/avalanche/test/peermanager_tests.cpp
Show First 20 Lines • Show All 143 Lines • ▼ Show 20 Lines | for (int c = 0; c < 1000; c++) { | ||||
auto i = selectPeerImpl(slots, s, max); | auto i = selectPeerImpl(slots, s, max); | ||||
// /!\ Because of the way we construct the vector, the peer id is | // /!\ Because of the way we construct the vector, the peer id is | ||||
// always the index. This might not be the case in practice. | // always the index. This might not be the case in practice. | ||||
BOOST_CHECK(i == NO_PEER || slots[i].contains(s)); | BOOST_CHECK(i == NO_PEER || slots[i].contains(s)); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(add_peer) { | BOOST_AUTO_TEST_CASE(peer_probabilities) { | ||||
// No peers. | // No peers. | ||||
PeerManager pm; | PeerManager pm; | ||||
BOOST_CHECK_EQUAL(pm.selectPeer(), NO_PEER); | BOOST_CHECK_EQUAL(pm.selectNode(), NO_NODE); | ||||
const NodeId node0 = 42, node1 = 69, node2 = 37; | |||||
// One peer, we always return it. | // One peer, we always return it. | ||||
PeerId peer0 = pm.addPeer(100); | Proof proof0(100); | ||||
BOOST_CHECK_EQUAL(pm.selectPeer(), peer0); | pm.addNode(Proof(100), node0, CPubKey()); | ||||
BOOST_CHECK_EQUAL(pm.selectNode(), node0); | |||||
// Two peers, verify ratio. | // Two peers, verify ratio. | ||||
PeerId peer1 = pm.addPeer(200); | pm.addNode(Proof(200), node1, CPubKey()); | ||||
std::unordered_map<PeerId, int> results = {}; | std::unordered_map<PeerId, int> results = {}; | ||||
for (int i = 0; i < 10000; i++) { | for (int i = 0; i < 10000; i++) { | ||||
size_t p = pm.selectPeer(); | size_t n = pm.selectNode(); | ||||
BOOST_CHECK(p == peer0 || p == peer1); | BOOST_CHECK(n == node0 || n == node1); | ||||
results[p]++; | results[n]++; | ||||
} | } | ||||
BOOST_CHECK(abs(2 * results[0] - results[1]) < 500); | BOOST_CHECK(abs(2 * results[0] - results[1]) < 500); | ||||
// Three peers, verify ratio. | // Three peers, verify ratio. | ||||
PeerId peer2 = pm.addPeer(100); | pm.addNode(Proof(100), node2, CPubKey()); | ||||
results.clear(); | results.clear(); | ||||
for (int i = 0; i < 10000; i++) { | for (int i = 0; i < 10000; i++) { | ||||
size_t p = pm.selectPeer(); | size_t n = pm.selectNode(); | ||||
BOOST_CHECK(p == peer0 || p == peer1 || p == peer2); | BOOST_CHECK(n == node0 || n == node1 || n == node2); | ||||
results[p]++; | results[n]++; | ||||
} | } | ||||
BOOST_CHECK(abs(results[0] - results[1] + results[2]) < 500); | BOOST_CHECK(abs(results[0] - results[1] + results[2]) < 500); | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(remove_peer) { | BOOST_AUTO_TEST_CASE(remove_peer) { | ||||
// 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. | ||||
std::array<PeerId, 8> peerids; | std::array<PeerId, 8> peerids; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
peerids[i] = pm.addPeer(100); | peerids[i] = pm.getPeer(Proof(100)); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 400); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 400); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
PeerId p = pm.selectPeer(); | PeerId p = pm.selectPeer(); | ||||
BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] || | BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] || | ||||
Show All 13 Lines | BOOST_AUTO_TEST_CASE(remove_peer) { | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
PeerId p = pm.selectPeer(); | PeerId p = pm.selectPeer(); | ||||
BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[3]); | BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[3]); | ||||
} | } | ||||
// Add 4 more peers. | // Add 4 more peers. | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
peerids[i + 4] = pm.addPeer(100); | peerids[i + 4] = pm.getPeer(Proof(100)); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | ||||
BOOST_CHECK(pm.removePeer(peerids[0])); | BOOST_CHECK(pm.removePeer(peerids[0])); | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 700); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 100); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 100); | ||||
Show All 25 Lines | |||||
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. | ||||
std::array<PeerId, 4> peerids; | std::array<PeerId, 4> peerids; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
peerids[i] = pm.addPeer(100); | peerids[i] = pm.getPeer(Proof(100)); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 400); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 400); | ||||
BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | BOOST_CHECK_EQUAL(pm.getFragmentation(), 0); | ||||
for (int i = 0; i < 100; i++) { | for (int i = 0; i < 100; i++) { | ||||
PeerId p = pm.selectPeer(); | PeerId p = pm.selectPeer(); | ||||
BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] || | BOOST_CHECK(p == peerids[0] || p == peerids[1] || p == peerids[2] || | ||||
▲ Show 20 Lines • Show All 51 Lines • ▼ Show 20 Lines | |||||
} | } | ||||
BOOST_AUTO_TEST_CASE(compact_slots) { | BOOST_AUTO_TEST_CASE(compact_slots) { | ||||
PeerManager pm; | PeerManager pm; | ||||
// Add 4 peers. | // Add 4 peers. | ||||
std::array<PeerId, 4> peerids; | std::array<PeerId, 4> peerids; | ||||
for (int i = 0; i < 4; i++) { | for (int i = 0; i < 4; i++) { | ||||
peerids[i] = pm.addPeer(100); | peerids[i] = pm.getPeer(Proof(100)); | ||||
} | } | ||||
// Remove all peers. | // Remove all peers. | ||||
for (auto p : peerids) { | for (auto p : peerids) { | ||||
pm.removePeer(p); | pm.removePeer(p); | ||||
} | } | ||||
BOOST_CHECK_EQUAL(pm.getSlotCount(), 300); | BOOST_CHECK_EQUAL(pm.getSlotCount(), 300); | ||||
▲ Show 20 Lines • Show All 79 Lines • Show Last 20 Lines |