Skip to content

Commit

Permalink
Fixing integration test and adding some basic unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
asoto-iov committed Aug 7, 2024
1 parent 2904f13 commit 4bdb8b1
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 16 deletions.
3 changes: 2 additions & 1 deletion rskj-core/src/main/java/co/rsk/net/SnapshotProcessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@ public void processStateChunkResponse(SnapSyncState state, Peer peer, SnapStateC
}
}

private void onStateChunkResponseError(Peer peer, SnapStateChunkResponseMessage responseMessage) {
@VisibleForTesting
void onStateChunkResponseError(Peer peer, SnapStateChunkResponseMessage responseMessage) {
logger.error("Error while processing chunk response from {} of peer {}. Asking for chunk again.", responseMessage.getFrom(), peer.getPeerNodeID());
Peer alternativePeer = peersInformation.getBestSnapPeerCandidates().stream()
.filter(listedPeer -> !listedPeer.getPeerNodeID().equals(peer.getPeerNodeID()))
Expand Down
36 changes: 33 additions & 3 deletions rskj-core/src/test/java/co/rsk/net/SnapshotProcessorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import org.ethereum.core.Blockchain;
import org.ethereum.core.TransactionPool;
import org.ethereum.db.BlockStore;
import org.ethereum.util.RLP;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -104,7 +105,7 @@ void givenSnapStatusResponseCalled_thenSnapChunkRequestsAreMade() {
TEST_CHUNK_SIZE,
false);

for (long blockNumber = 0; blockNumber < blockchain.getSize(); blockNumber ++){
for (long blockNumber = 0; blockNumber < blockchain.getSize(); blockNumber++) {
Block currentBlock = blockchain.getBlockByNumber(blockNumber);
blocks.add(currentBlock);
difficulties.add(blockStore.getTotalDifficultyForHash(currentBlock.getHash().getBytes()));
Expand Down Expand Up @@ -181,7 +182,7 @@ void givenSnapBlocksResponseReceived_thenSnapBlocksRequestMessageIsSent() {
200,
false);

for (long blockNumber = 0; blockNumber < blockchain.getSize(); blockNumber ++){
for (long blockNumber = 0; blockNumber < blockchain.getSize(); blockNumber++) {
Block currentBlock = blockchain.getBlockByNumber(blockNumber);
blocks.add(currentBlock);
difficulties.add(blockStore.getTotalDifficultyForHash(currentBlock.getHash().getBytes()));
Expand Down Expand Up @@ -216,7 +217,7 @@ void givenSnapStateChunkRequest_thenSnapStateChunkResponseMessageIsSent() {
TEST_CHUNK_SIZE,
false);

SnapStateChunkRequestMessage snapStateChunkRequestMessage = new SnapStateChunkRequestMessage(1L, 1L,1, TEST_CHUNK_SIZE);
SnapStateChunkRequestMessage snapStateChunkRequestMessage = new SnapStateChunkRequestMessage(1L, 1L, 1, TEST_CHUNK_SIZE);

//when
underTest.processStateChunkRequestInternal(peer, snapStateChunkRequestMessage);
Expand Down Expand Up @@ -333,6 +334,35 @@ void processStateChunkRequestInternal(Peer sender, SnapStateChunkRequestMessage
assertEquals(msg, jobArg.getValue().getMsg());
}

@Test
void givenErrorRLPData_thenOnStateChunkErrorIsCalled() {
underTest = new SnapshotProcessor(
blockchain,
trieStore,
peersInformation,
blockStore,
transactionPool,
TEST_CHUNK_SIZE,
false);

PriorityQueue<SnapStateChunkResponseMessage> queue = new PriorityQueue<>(
Comparator.comparingLong(SnapStateChunkResponseMessage::getFrom));
when(snapSyncState.getSnapStateChunkQueue()).thenReturn(queue);
when(snapSyncState.getChunkTaskQueue()).thenReturn(new LinkedList<>());
SnapStateChunkResponseMessage responseMessage = mock(SnapStateChunkResponseMessage.class);
when(snapSyncState.getNextExpectedFrom()).thenReturn(1L);
when(responseMessage.getFrom()).thenReturn(1L);
when(responseMessage.getChunkOfTrieKeyValue()).thenReturn(RLP.encodedEmptyList());
underTest = spy(underTest);

underTest.processStateChunkResponse(snapSyncState, peer, responseMessage);

verify(snapSyncState, times(1)).onNewChunk();
verify(underTest, times(1)).onStateChunkResponseError(peer, responseMessage);
verify(peer, times(1)).sendMessage(any(SnapStateChunkRequestMessage.class));

}

private void initializeBlockchainWithAmountOfBlocks(int numberOfBlocks) {
BlockChainBuilder blockChainBuilder = new BlockChainBuilder();
blockchain = blockChainBuilder.ofSize(numberOfBlocks);
Expand Down
106 changes: 94 additions & 12 deletions rskj-core/src/test/java/co/rsk/net/sync/SnapSyncStateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,35 @@
*/
package co.rsk.net.sync;

import co.rsk.core.BlockDifficulty;
import co.rsk.net.NodeID;
import co.rsk.net.Peer;
import co.rsk.net.SnapshotProcessor;
import co.rsk.net.messages.SnapBlocksResponseMessage;
import co.rsk.net.messages.SnapStateChunkResponseMessage;
import co.rsk.net.messages.SnapStatusResponseMessage;
import org.apache.commons.lang3.tuple.Pair;
import org.ethereum.core.Block;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import java.math.BigInteger;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.Duration;
import java.util.List;
import java.util.Optional;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.greaterThan;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class SnapSyncStateTest {
Expand All @@ -56,8 +62,8 @@ class SnapSyncStateTest {
private final SnapSyncState underTest = new SnapSyncState(syncEventsHandler, snapshotProcessor, syncConfiguration, listener);

@BeforeEach
void setUp(){
reset(syncEventsHandler,peersInformation, snapshotProcessor);
void setUp() {
reset(syncEventsHandler, peersInformation, snapshotProcessor);
}

@AfterEach
Expand All @@ -66,15 +72,15 @@ void tearDown() {
}

@Test
void givenOnEnterWasCalledAndNotRunningYet_thenSyncingStartsWithTestObjectAsParameter(){
void givenOnEnterWasCalledAndNotRunningYet_thenSyncingStartsWithTestObjectAsParameter() {
//given-when
underTest.onEnter();
//then
verify(snapshotProcessor, times(1)).startSyncing();
}

@Test
void givenFinishWasCalledTwice_thenStopSyncingOnlyOnce(){
void givenFinishWasCalledTwice_thenStopSyncingOnlyOnce() {
//given-when
underTest.setRunning();
underTest.finish();
Expand All @@ -84,7 +90,7 @@ void givenFinishWasCalledTwice_thenStopSyncingOnlyOnce(){
}

@Test
void givenOnEnterWasCalledTwice_thenSyncingStartsOnlyOnce(){
void givenOnEnterWasCalledTwice_thenSyncingStartsOnlyOnce() {
//given-when
underTest.onEnter();
underTest.onEnter();
Expand All @@ -93,7 +99,7 @@ void givenOnEnterWasCalledTwice_thenSyncingStartsOnlyOnce(){
}

@Test
void givenOnMessageTimeOutCalled_thenSyncingStops(){
void givenOnMessageTimeOutCalled_thenSyncingStops() {
//given-when
underTest.setRunning();
underTest.onMessageTimeOut();
Expand All @@ -102,7 +108,7 @@ void givenOnMessageTimeOutCalled_thenSyncingStops(){
}

@Test
void givenNewChunk_thenTimerIsReset(){
void givenNewChunk_thenTimerIsReset() {
//given
underTest.timeElapsed = Duration.ofMinutes(1);
assertThat(underTest.timeElapsed, greaterThan(Duration.ZERO));
Expand All @@ -114,7 +120,7 @@ void givenNewChunk_thenTimerIsReset(){
}

@Test
void givenTickIsCalledBeforeTimeout_thenTimerIsUpdated_andNoTimeoutHappens(){
void givenTickIsCalledBeforeTimeout_thenTimerIsUpdated_andNoTimeoutHappens() {
//given
Duration elapsedTime = Duration.ofMillis(10);
underTest.timeElapsed = Duration.ZERO;
Expand All @@ -123,7 +129,7 @@ void givenTickIsCalledBeforeTimeout_thenTimerIsUpdated_andNoTimeoutHappens(){
//then
assertThat(underTest.timeElapsed, equalTo(elapsedTime));
verify(syncEventsHandler, never()).stopSyncing();
verify(syncEventsHandler, never()).onErrorSyncing(any(),any(),any(),any());
verify(syncEventsHandler, never()).onErrorSyncing(any(), any(), any(), any());
}

@Test
Expand All @@ -145,7 +151,7 @@ void givenTickIsCalledAfterTimeout_thenTimerIsUpdated_andTimeoutHappens() throws
}

@Test
void givenFinishIsCalled_thenSyncEventHandlerStopsSync(){
void givenFinishIsCalled_thenSyncEventHandlerStopsSync() {
//given-when
underTest.setRunning();
underTest.finish();
Expand Down Expand Up @@ -219,10 +225,86 @@ void givenOnSnapStateChunkIsCalled_thenJobIsAddedAndRun() throws InterruptedExce
assertEquals(msg, jobArg.getValue().getMsg());
}

@Test
void testSetAndGetLastBlock() {
Block mockBlock = mock(Block.class);
underTest.setLastBlock(mockBlock);
assertEquals(mockBlock, underTest.getLastBlock());
}

@Test
void testSetAndGetStateChunkSize() {
BigInteger expectedSize = BigInteger.valueOf(100L);
underTest.setStateChunkSize(expectedSize);
assertEquals(expectedSize, underTest.getStateChunkSize());
}

@Test
void testSetAndGetStateSize() {
BigInteger expectedSize = BigInteger.valueOf(1000L);
underTest.setStateSize(expectedSize);
assertEquals(expectedSize, underTest.getStateSize());
}

@Test
void testGetChunkTaskQueue() {
Queue<ChunkTask> queue = underTest.getChunkTaskQueue();
assertNotNull(queue);
}

@Test
void testSetAndGetNextExpectedFrom() {
long expectedValue = 100L;
underTest.setNextExpectedFrom(expectedValue);
assertEquals(expectedValue, underTest.getNextExpectedFrom());
}

private static void doCountDownOnQueueEmpty(SyncMessageHandler.Listener listener, CountDownLatch latch) {
doAnswer(invocation -> {
latch.countDown();
return null;
}).when(listener).onQueueEmpty();
}

@Test
void testGetSnapStateChunkQueue() {
PriorityQueue<SnapStateChunkResponseMessage> queue = underTest.getSnapStateChunkQueue();
assertNotNull(queue);
}

@Test
void testSetAndGetLastBlockDifficulty() {
BlockDifficulty mockBlockDifficulty = mock(BlockDifficulty.class);
underTest.setLastBlockDifficulty(mockBlockDifficulty);
assertEquals(mockBlockDifficulty, underTest.getLastBlockDifficulty());
}

@Test
void testSetAndGetRemoteRootHash() {
byte[] mockRootHash = new byte[]{1, 2, 3};
underTest.setRemoteRootHash(mockRootHash);
assertArrayEquals(mockRootHash, underTest.getRemoteRootHash());
}

@Test
void testSetAndGetRemoteTrieSize() {
long expectedSize = 12345L;
underTest.setRemoteTrieSize(expectedSize);
assertEquals(expectedSize, underTest.getRemoteTrieSize());
}

@Test
void testConnectBlocks() {
BlockConnectorHelper blockConnectorHelper = mock(BlockConnectorHelper.class);
Pair<Block, BlockDifficulty> mockBlockPair = mock(Pair.class);
underTest.addBlock(mockBlockPair);
ArgumentCaptor<List<Pair<Block, BlockDifficulty>>> captor = ArgumentCaptor.forClass(List.class);

underTest.connectBlocks(blockConnectorHelper);

verify(blockConnectorHelper, times(1)).startConnecting(captor.capture());
assertTrue(captor.getValue().contains(mockBlockPair));
}


}

0 comments on commit 4bdb8b1

Please sign in to comment.