use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.
the class FetchBlockTaskTest method run_failAndRetryWithNoNewPeers.
@Test
public void run_failAndRetryWithNoNewPeers() {
final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(10);
final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
FetchBlockTask task = FetchBlockTask.create(eth2P2PNetwork, blockRoot);
final Eth2Peer peer = registerNewPeer(1);
when(peer.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.failedFuture(new RuntimeException("whoops")));
final SafeFuture<FetchBlockResult> result = task.run();
assertThat(result).isDone();
final FetchBlockResult fetchBlockResult = result.getNow(null);
assertThat(fetchBlockResult.isSuccessful()).isFalse();
assertThat(fetchBlockResult.getStatus()).isEqualTo(Status.FETCH_FAILED);
assertThat(task.getNumberOfRetries()).isEqualTo(0);
// Retry
final SafeFuture<FetchBlockResult> result2 = task.run();
assertThat(result).isDone();
final FetchBlockResult fetchBlockResult2 = result2.getNow(null);
assertThat(fetchBlockResult2.isSuccessful()).isFalse();
assertThat(fetchBlockResult2.getStatus()).isEqualTo(Status.NO_AVAILABLE_PEERS);
assertThat(task.getNumberOfRetries()).isEqualTo(0);
}
use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.
the class FetchBlockTaskTest method registerNewPeer.
private Eth2Peer registerNewPeer(final int id) {
final Eth2Peer peer = mock(Eth2Peer.class);
when(peer.getOutstandingRequests()).thenReturn(0);
when(peer.getId()).thenReturn(new MockNodeId(id));
peers.add(peer);
return peer;
}
use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.
the class FetchBlockTaskTest method run_successful.
@Test
public void run_successful() {
final SignedBeaconBlock block = dataStructureUtil.randomSignedBeaconBlock(10);
final Bytes32 blockRoot = block.getMessage().hashTreeRoot();
FetchBlockTask task = FetchBlockTask.create(eth2P2PNetwork, blockRoot);
assertThat(task.getBlockRoot()).isEqualTo(blockRoot);
final Eth2Peer peer = registerNewPeer(1);
when(peer.requestBlockByRoot(blockRoot)).thenReturn(SafeFuture.completedFuture(Optional.of(block)));
final SafeFuture<FetchBlockResult> result = task.run();
assertThat(result).isDone();
final FetchBlockResult fetchBlockResult = result.getNow(null);
assertThat(fetchBlockResult.isSuccessful()).isTrue();
assertThat(fetchBlockResult.getBlock()).isEqualTo(block);
}
use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.
the class ReadinessTest method shouldReturnOkWhenInSyncAndReadyAndTargetPeerCountReached.
@Test
public void shouldReturnOkWhenInSyncAndReadyAndTargetPeerCountReached() throws Exception {
final Eth2Peer peer1 = mock(Eth2Peer.class);
final Readiness handler = new Readiness(syncDataProvider, chainDataProvider, network, jsonProvider);
when(chainDataProvider.isStoreAvailable()).thenReturn(true);
when(syncService.getCurrentSyncState()).thenReturn(SyncState.IN_SYNC);
when(eth2P2PNetwork.streamPeers()).thenReturn(Stream.of(peer1, peer1)).thenReturn(Stream.of(peer1, peer1));
when(context.queryParamMap()).thenReturn(Map.of(TARGET_PEER_COUNT, List.of("1")));
handler.handle(context);
verifyCacheStatus(CACHE_NONE);
verifyStatusCode(SC_OK);
}
use of tech.pegasys.teku.networking.eth2.peers.Eth2Peer in project teku by ConsenSys.
the class ReadinessTest method shouldReturnUnavailableWhenTargetPeerCountNotReached.
@Test
public void shouldReturnUnavailableWhenTargetPeerCountNotReached() throws Exception {
final Eth2Peer peer1 = mock(Eth2Peer.class);
final Readiness handler = new Readiness(syncDataProvider, chainDataProvider, network, jsonProvider);
when(chainDataProvider.isStoreAvailable()).thenReturn(true);
when(syncService.getCurrentSyncState()).thenReturn(SyncState.IN_SYNC);
when(eth2P2PNetwork.streamPeers()).thenReturn(Stream.of(peer1)).thenReturn(Stream.of(peer1));
when(context.queryParamMap()).thenReturn(Map.of(TARGET_PEER_COUNT, List.of("10")));
handler.handle(context);
verifyCacheStatus(CACHE_NONE);
verifyStatusCode(SC_SERVICE_UNAVAILABLE);
}
Aggregations