Skip to content

Commit

Permalink
test: unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: georgi-l95 <[email protected]>
  • Loading branch information
georgi-l95 committed Nov 11, 2024
1 parent f34a034 commit 1226d52
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.hedera.block.simulator.config.data.BlockStreamConfig;
Expand Down Expand Up @@ -133,9 +135,10 @@ private String getAbsoluteFolder(String relativePath) {
}

@Test
void stop_doesNotThrowException() {
void stop_doesNotThrowException() throws InterruptedException {
assertDoesNotThrow(() -> blockStreamSimulator.stop());
assertFalse(blockStreamSimulator.isRunning());
verify(publishStreamGrpcClient, atLeast(1)).completeStreaming();
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.hedera.block.simulator.grpc;

import static com.hedera.block.simulator.TestUtils.getTestMetrics;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
Expand Down Expand Up @@ -91,11 +92,15 @@ void streamBlock() {
new PublishStreamGrpcClientImpl(grpcConfig, blockStreamConfig, metricsService, streamEnabled);

publishStreamGrpcClient.init();
assertTrue(publishStreamGrpcClient.getLastKnownStatuses().isEmpty());

boolean result = publishStreamGrpcClient.streamBlock(block);
assertTrue(result);

boolean result1 = publishStreamGrpcClient.streamBlock(block1);
assertTrue(result1);

assertEquals(2, publishStreamGrpcClient.getPublishedBlocks());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,51 @@ void testStartWithConstantRateStreaming_WithinMaxItems() throws Exception {
verify(blockStreamManager, times(3)).getNextBlock();
}

@Test
void testStartWithConstantRateStreaming_ExceedingMaxItems() throws Exception {
when(blockStreamConfig.streamingMode()).thenReturn(StreamingMode.CONSTANT_RATE);
when(blockStreamConfig.delayBetweenBlockItems()).thenReturn(0);
when(blockStreamConfig.maxBlockItemsToStream()).thenReturn(5);

publisherModeHandler = new PublisherModeHandler(
blockStreamConfig, publishStreamGrpcClient, blockStreamManager, metricsService);
when(publishStreamGrpcClient.streamBlock(any(Block.class))).thenReturn(true);

Block block1 = mock(Block.class);
Block block2 = mock(Block.class);
Block block3 = mock(Block.class);
Block block4 = mock(Block.class);

BlockItem blockItem1 = mock(BlockItem.class);
BlockItem blockItem2 = mock(BlockItem.class);
BlockItem blockItem3 = mock(BlockItem.class);
BlockItem blockItem4 = mock(BlockItem.class);

when(block1.getItemsList()).thenReturn(Arrays.asList(blockItem1, blockItem2));
when(block2.getItemsList()).thenReturn(Arrays.asList(blockItem3, blockItem4));
when(block3.getItemsList()).thenReturn(Arrays.asList(blockItem1, blockItem2));
when(block4.getItemsList()).thenReturn(Arrays.asList(blockItem3, blockItem4));

when(blockStreamManager.getNextBlock())
.thenReturn(block1)
.thenReturn(block2)
.thenReturn(block3)
.thenReturn(block4);

when(publishStreamGrpcClient.streamBlock(block1)).thenReturn(true);
when(publishStreamGrpcClient.streamBlock(block2)).thenReturn(true);
when(publishStreamGrpcClient.streamBlock(block3)).thenReturn(true);
when(publishStreamGrpcClient.streamBlock(block4)).thenReturn(true);

publisherModeHandler.start();

verify(publishStreamGrpcClient).streamBlock(block1);
verify(publishStreamGrpcClient).streamBlock(block2);
verify(publishStreamGrpcClient).streamBlock(block3);
verifyNoMoreInteractions(publishStreamGrpcClient);
verify(blockStreamManager, times(3)).getNextBlock();
}

@Test
void testStartWithConstantRateStreaming_NoBlocks() throws Exception {
when(blockStreamConfig.streamingMode()).thenReturn(StreamingMode.CONSTANT_RATE);
Expand Down

0 comments on commit 1226d52

Please sign in to comment.