Search in sources :

Example 6 with StubPeer

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

the class Eth2PeerSelectionStrategyTest method selectPeersToDisconnect_shouldMoveExcessRandomlySelectedPeersBackToScoreBasedPool.

@Test
void selectPeersToDisconnect_shouldMoveExcessRandomlySelectedPeersBackToScoreBasedPool() {
    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(peer1.getId(), PeerPool.RANDOMLY_SELECTED);
    peerPools.addPeerToPool(peer2.getId(), PeerPool.RANDOMLY_SELECTED);
    peerScorer.setScore(peer1.getId(), 100);
    peerScorer.setScore(peer2.getId(), 200);
    peerScorer.setScore(peer3.getId(), 50);
    withShuffleOrder(peer2, peer1, peer3);
    // Peer2 was dropped from the random pool but had a better score than peer3 so was kept
    assertThat(strategy.selectPeersToDisconnect(network, peerPools)).containsExactlyInAnyOrder(peer3);
    assertThat(peerPools.getPool(peer2.getId())).isEqualTo(PeerPool.SCORE_BASED);
}
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 7 with StubPeer

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

the class ConnectionManagerTest method shouldConnectPeersThatPassPeerFilter.

@Test
public void shouldConnectPeersThatPassPeerFilter() {
    final ConnectionManager manager = createManager();
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    when(network.connect(PEER1)).thenReturn(SafeFuture.completedFuture(peer1));
    when(network.connect(PEER2)).thenReturn(SafeFuture.completedFuture(peer2));
    when(discoveryService.streamKnownPeers()).thenReturn(Stream.of(DISCOVERY_PEER1, DISCOVERY_PEER2));
    manager.start().join();
    verify(network).connect(PEER1);
    verify(network).connect(PEER2);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) ConnectionManager(tech.pegasys.teku.networking.p2p.connection.ConnectionManager) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) Test(org.junit.jupiter.api.Test)

Example 8 with StubPeer

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

the class ConnectionManagerTest method shouldNotAddDuplicatePeerToStaticList.

@Test
public void shouldNotAddDuplicatePeerToStaticList() {
    final ConnectionManager manager = createManager(PEER1);
    final MockNodeId peerId = new MockNodeId();
    final StubPeer peer = new StubPeer(peerId);
    when(network.connect(PEER1)).thenReturn(SafeFuture.completedFuture(peer)).thenReturn(new SafeFuture<>());
    manager.start().join();
    verify(network).connect(PEER1);
    manager.addStaticPeer(PEER1);
    // Doesn't attempt to connect a second time.
    verify(network, times(1)).connect(PEER1);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) ConnectionManager(tech.pegasys.teku.networking.p2p.connection.ConnectionManager) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) Test(org.junit.jupiter.api.Test)

Example 9 with StubPeer

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

the class ConnectionManagerTest method shouldUsePeerSelectionStrategyToSelectPeersToDisconnect.

@Test
public void shouldUsePeerSelectionStrategyToSelectPeersToDisconnect() {
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    final ConnectionManager manager = createManager();
    when(network.connect(PEER1)).thenReturn(SafeFuture.completedFuture(peer1));
    when(network.connect(PEER2)).thenReturn(SafeFuture.completedFuture(peer2));
    manager.start().join();
    final PeerConnectedSubscriber<Peer> peerConnectedSubscriber = getPeerConnectedSubscriber();
    when(peerSelectionStrategy.selectPeersToDisconnect(eq(network), any())).thenReturn(List.of(peer1));
    peerConnectedSubscriber.onConnected(peer1);
    assertThat(peer2.isConnected()).isTrue();
    assertThat(peer1.isConnected()).isFalse();
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) ConnectionManager(tech.pegasys.teku.networking.p2p.connection.ConnectionManager) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) StubPeer(tech.pegasys.teku.network.p2p.peer.StubPeer) Peer(tech.pegasys.teku.networking.p2p.peer.Peer) Test(org.junit.jupiter.api.Test)

Example 10 with StubPeer

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

the class ConnectionManagerTest method shouldNotConnectPeersThatDoNotPassPeerFilter.

@Test
public void shouldNotConnectPeersThatDoNotPassPeerFilter() {
    final ConnectionManager manager = createManager();
    manager.addPeerPredicate((peer) -> !peer.equals(DISCOVERY_PEER2));
    final StubPeer peer1 = new StubPeer(new MockNodeId(1));
    final StubPeer peer2 = new StubPeer(new MockNodeId(2));
    when(network.connect(PEER1)).thenReturn(SafeFuture.completedFuture(peer1));
    when(network.connect(PEER2)).thenReturn(SafeFuture.completedFuture(peer2));
    when(discoveryService.streamKnownPeers()).thenReturn(Stream.of(DISCOVERY_PEER1, DISCOVERY_PEER2));
    manager.start().join();
    verify(network).connect(PEER1);
    verify(network, never()).connect(PEER2);
}
Also used : MockNodeId(tech.pegasys.teku.networking.p2p.mock.MockNodeId) ConnectionManager(tech.pegasys.teku.networking.p2p.connection.ConnectionManager) 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