diff --git a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java index a233de21b..c73c1b2c1 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/BlockStreamSimulatorTest.java @@ -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; @@ -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 diff --git a/simulator/src/test/java/com/hedera/block/simulator/grpc/PublishStreamGrpcClientImplTest.java b/simulator/src/test/java/com/hedera/block/simulator/grpc/PublishStreamGrpcClientImplTest.java index 2d1a196cc..4a7d3edb3 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/grpc/PublishStreamGrpcClientImplTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/grpc/PublishStreamGrpcClientImplTest.java @@ -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; @@ -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 diff --git a/simulator/src/test/java/com/hedera/block/simulator/mode/PublisherModeHandlerTest.java b/simulator/src/test/java/com/hedera/block/simulator/mode/PublisherModeHandlerTest.java index 78ee4b250..4be31d5d8 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/mode/PublisherModeHandlerTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/mode/PublisherModeHandlerTest.java @@ -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);