Search in sources :

Example 16 with StubPeer

use of tech.pegasys.teku.network.p2p.peer.StubPeer in project teku by ConsenSys.

the class Eth2PeerSelectionStrategyTest method selectPeersToDisconnect_shouldDisconnectLowestScoringPeersWhenPeerCountExceedsUpperBound.

@Test
void selectPeersToDisconnect_shouldDisconnectLowestScoringPeersWhenPeerCountExceedsUpperBound() {
    final Eth2PeerSelectionStrategy strategy = createStrategy(0, 1, 0);
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    final StubPeer peer3 = new StubPeer(new MockNodeId(3));
    peerScorer.setScore(peer1.getId(), 100);
    peerScorer.setScore(peer2.getId(), 200);
    peerScorer.setScore(peer3.getId(), 150);
    when(network.getPeerCount()).thenReturn(3);
    when(network.streamPeers()).thenReturn(Stream.of(peer1, peer2, peer3));
    assertThat(strategy.selectPeersToDisconnect(network, peerPools)).containsExactlyInAnyOrder(peer1, peer3);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) Test(org.junit.jupiter.api.Test)

Example 17 with StubPeer

use of tech.pegasys.teku.network.p2p.peer.StubPeer in project teku by ConsenSys.

the class Eth2PeerSelectionStrategyTest method selectPeersToDisconnect_shouldNotDisconnectFromStaticPeers.

@Test
void selectPeersToDisconnect_shouldNotDisconnectFromStaticPeers() {
    final Eth2PeerSelectionStrategy strategy = createStrategy(0, 0, 0);
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    final StubPeer peer3 = new StubPeer(new MockNodeId(3));
    when(network.getPeerCount()).thenReturn(3);
    when(network.streamPeers()).thenReturn(Stream.of(peer1, peer2, peer3));
    peerPools.addPeerToPool(peer2.getId(), PeerPool.STATIC);
    assertThat(strategy.selectPeersToDisconnect(network, peerPools)).containsExactlyInAnyOrder(peer1, peer3);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) Test(org.junit.jupiter.api.Test)

Example 18 with StubPeer

use of tech.pegasys.teku.network.p2p.peer.StubPeer in project teku by ConsenSys.

the class Eth2PeerSelectionStrategyTest method selectPeersToDisconnect_shouldNotDisconnectRandomlySelectedPeersBasedOnScore.

@Test
void selectPeersToDisconnect_shouldNotDisconnectRandomlySelectedPeersBasedOnScore() {
    final Eth2PeerSelectionStrategy strategy = createStrategy(2, 2, 1);
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    final StubPeer peer3 = new StubPeer(new MockNodeId(3));
    when(network.getPeerCount()).thenReturn(3);
    when(network.streamPeers()).thenReturn(Stream.of(peer1, peer2, peer3));
    peerPools.addPeerToPool(peer2.getId(), PeerPool.RANDOMLY_SELECTED);
    peerScorer.setScore(peer1.getId(), 100);
    peerScorer.setScore(peer2.getId(), 0);
    peerScorer.setScore(peer3.getId(), 50);
    // peer2 has the lowest score but is safe because it's in the randomly selected pool
    assertThat(strategy.selectPeersToDisconnect(network, peerPools)).containsExactlyInAnyOrder(peer3);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)18 StubPeer (tech.pegasys.teku.network.p2p.peer.StubPeer)18 MockNodeId (tech.pegasys.teku.networking.p2p.mock.MockNodeId)14 ConnectionManager (tech.pegasys.teku.networking.p2p.connection.ConnectionManager)9 Peer (tech.pegasys.teku.networking.p2p.peer.Peer)3 SafeFuture (tech.pegasys.teku.infrastructure.async.SafeFuture)2 PeerAddress (tech.pegasys.teku.networking.p2p.network.PeerAddress)1