From 4d37a6524990697128bc1787262b56d1fb879ac7 Mon Sep 17 00:00:00 2001 From: georgi-l95 Date: Mon, 25 Nov 2024 20:25:23 +0200 Subject: [PATCH] test: add more tests Signed-off-by: georgi-l95 --- .../grpc/impl/ConsumerStreamObserver.java | 1 + .../grpc/impl/ConsumerStreamObserverTest.java | 34 ++++++++++------ .../impl/PublishStreamGrpcClientImplTest.java | 40 +++++++++---------- 3 files changed, 40 insertions(+), 35 deletions(-) diff --git a/simulator/src/main/java/com/hedera/block/simulator/grpc/impl/ConsumerStreamObserver.java b/simulator/src/main/java/com/hedera/block/simulator/grpc/impl/ConsumerStreamObserver.java index 2c2daff8..7ef05736 100644 --- a/simulator/src/main/java/com/hedera/block/simulator/grpc/impl/ConsumerStreamObserver.java +++ b/simulator/src/main/java/com/hedera/block/simulator/grpc/impl/ConsumerStreamObserver.java @@ -51,6 +51,7 @@ public class ConsumerStreamObserver implements StreamObserver blockItems = Arrays.asList(blockItemWithProof, blockItemWithoutProof, blockItemWithProof); - BlockItemSet blockItemSet = - BlockItemSet.newBuilder().addAllBlockItems(blockItems).build(); + BlockItemSet blockItemsSet = BlockItemSet.newBuilder() + .addBlockItems(blockItemHeader) + .addBlockItems(blockItemProof) + .addBlockItems(blockItemProof1) + .build(); - SubscribeStreamResponse response = - SubscribeStreamResponse.newBuilder().setBlockItems(blockItemSet).build(); - assertEquals(metricsService.get(Counter.LiveBlocksConsumed).get(), 0); + SubscribeStreamResponse response = SubscribeStreamResponse.newBuilder() + .setBlockItems(blockItemsSet) + .build(); + assertEquals(0, metricsService.get(Counter.LiveBlocksConsumed).get()); observer.onNext(response); - assertEquals(metricsService.get(Counter.LiveBlocksConsumed).get(), 2); + assertEquals(2, metricsService.get(Counter.LiveBlocksConsumed).get()); verifyNoInteractions(streamLatch); } diff --git a/simulator/src/test/java/com/hedera/block/simulator/grpc/impl/PublishStreamGrpcClientImplTest.java b/simulator/src/test/java/com/hedera/block/simulator/grpc/impl/PublishStreamGrpcClientImplTest.java index afb8dafa..85e02c38 100644 --- a/simulator/src/test/java/com/hedera/block/simulator/grpc/impl/PublishStreamGrpcClientImplTest.java +++ b/simulator/src/test/java/com/hedera/block/simulator/grpc/impl/PublishStreamGrpcClientImplTest.java @@ -27,6 +27,7 @@ import com.hedera.block.simulator.TestUtils; import com.hedera.block.simulator.config.data.BlockStreamConfig; import com.hedera.block.simulator.config.data.GrpcConfig; +import com.hedera.block.simulator.grpc.PublishStreamGrpcClient; import com.hedera.block.simulator.metrics.MetricsService; import com.hedera.block.simulator.metrics.MetricsServiceImpl; import com.hedera.hapi.block.stream.protoc.Block; @@ -38,7 +39,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.atomic.AtomicBoolean; -import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -48,10 +48,10 @@ class PublishStreamGrpcClientImplTest { private MetricsService metricsService; - - GrpcConfig grpcConfig; - BlockStreamConfig blockStreamConfig; - AtomicBoolean streamEnabled; + private PublishStreamGrpcClient publishStreamGrpcClient; + private GrpcConfig grpcConfig; + private BlockStreamConfig blockStreamConfig; + private AtomicBoolean streamEnabled; @BeforeEach void setUp() throws IOException { @@ -63,23 +63,23 @@ void setUp() throws IOException { Configuration config = TestUtils.getTestConfiguration(); metricsService = new MetricsServiceImpl(getTestMetrics(config)); streamEnabled = new AtomicBoolean(true); + publishStreamGrpcClient = + new PublishStreamGrpcClientImpl(grpcConfig, blockStreamConfig, metricsService, streamEnabled); } - @AfterEach - void tearDown() {} - @Test - void streamBlockItem() { + void streamBlockItem() throws InterruptedException { BlockItem blockItem = BlockItem.newBuilder().build(); - PublishStreamGrpcClientImpl publishStreamGrpcClient = - new PublishStreamGrpcClientImpl(grpcConfig, blockStreamConfig, metricsService, streamEnabled); publishStreamGrpcClient.init(); boolean result = publishStreamGrpcClient.streamBlockItem(List.of(blockItem)); assertTrue(result); + + publishStreamGrpcClient.completeStreaming(); + publishStreamGrpcClient.shutdown(); } @Test - void streamBlock() { + void streamBlock() throws InterruptedException { BlockItem blockItem = BlockItem.newBuilder().build(); Block block = Block.newBuilder().addItems(blockItem).build(); @@ -89,9 +89,6 @@ void streamBlock() { .addItems(blockItem) .build(); - PublishStreamGrpcClientImpl publishStreamGrpcClient = - new PublishStreamGrpcClientImpl(grpcConfig, blockStreamConfig, metricsService, streamEnabled); - publishStreamGrpcClient.init(); assertTrue(publishStreamGrpcClient.getLastKnownStatuses().isEmpty()); @@ -102,6 +99,8 @@ void streamBlock() { assertTrue(result1); assertEquals(2, publishStreamGrpcClient.getPublishedBlocks()); + publishStreamGrpcClient.completeStreaming(); + publishStreamGrpcClient.shutdown(); } @Test @@ -109,28 +108,25 @@ void streamBlockFailsBecauseOfCompletedStreaming() throws InterruptedException { BlockItem blockItem = BlockItem.newBuilder().build(); Block block = Block.newBuilder().addItems(blockItem).build(); - PublishStreamGrpcClientImpl publishStreamGrpcClient = - new PublishStreamGrpcClientImpl(grpcConfig, blockStreamConfig, metricsService, streamEnabled); - publishStreamGrpcClient.init(); assertTrue(publishStreamGrpcClient.getLastKnownStatuses().isEmpty()); publishStreamGrpcClient.completeStreaming(); - assertThrows(IllegalStateException.class, () -> publishStreamGrpcClient.streamBlock(block)); + publishStreamGrpcClient.shutdown(); } @Test - void streamBlockReturnsFalse() { + void streamBlockReturnsFalse() throws InterruptedException { BlockItem blockItem = BlockItem.newBuilder().build(); Block block = Block.newBuilder().addItems(blockItem).build(); streamEnabled.set(false); - PublishStreamGrpcClientImpl publishStreamGrpcClient = - new PublishStreamGrpcClientImpl(grpcConfig, blockStreamConfig, metricsService, streamEnabled); publishStreamGrpcClient.init(); boolean result = publishStreamGrpcClient.streamBlock(block); assertFalse(result); + publishStreamGrpcClient.completeStreaming(); + publishStreamGrpcClient.shutdown(); } @Test