From 25294886930c585af731112f4e40283fc5926151 Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Thu, 1 Aug 2024 10:06:45 -0600 Subject: [PATCH] fix: replaced lock Signed-off-by: Matt Peterson --- .../ProducerBlockItemObserverTest.java | 56 +++++++------------ 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/server/src/test/java/com/hedera/block/server/producer/ProducerBlockItemObserverTest.java b/server/src/test/java/com/hedera/block/server/producer/ProducerBlockItemObserverTest.java index d439e23b3..2bd4cc1ea 100644 --- a/server/src/test/java/com/hedera/block/server/producer/ProducerBlockItemObserverTest.java +++ b/server/src/test/java/com/hedera/block/server/producer/ProducerBlockItemObserverTest.java @@ -47,8 +47,6 @@ @ExtendWith(MockitoExtension.class) public class ProducerBlockItemObserverTest { - private final Object lock = new Object(); - @Mock private ItemAckBuilder itemAckBuilder; @Mock private StreamMediator> streamMediator; @Mock private StreamObserver publishStreamResponseObserver; @@ -63,11 +61,10 @@ public class ProducerBlockItemObserverTest { @Mock private ServiceStatus serviceStatus; @Test - public void testProducerOnNext() - throws InterruptedException, IOException, NoSuchAlgorithmException { + public void testProducerOnNext() throws IOException, NoSuchAlgorithmException { - List blockItems = generateBlockItems(1); - ProducerBlockItemObserver producerBlockItemObserver = + final List blockItems = generateBlockItems(1); + final ProducerBlockItemObserver producerBlockItemObserver = new ProducerBlockItemObserver( streamMediator, publishStreamResponseObserver, @@ -76,16 +73,12 @@ public void testProducerOnNext() when(serviceStatus.isRunning()).thenReturn(true); - BlockItem blockHeader = blockItems.getFirst(); - PublishStreamRequest publishStreamRequest = + final BlockItem blockHeader = blockItems.getFirst(); + final PublishStreamRequest publishStreamRequest = PublishStreamRequest.newBuilder().setBlockItem(blockHeader).build(); producerBlockItemObserver.onNext(publishStreamRequest); - synchronized (lock) { - lock.wait(50); - } - - verify(streamMediator, times(1)).publishEvent(blockHeader); + verify(streamMediator, timeout(50).times(1)).publishEvent(blockHeader); final ItemAcknowledgement itemAck = ItemAcknowledgement.newBuilder() @@ -95,12 +88,12 @@ public void testProducerOnNext() BlockStreamService.PublishStreamResponse.newBuilder() .setAcknowledgement(itemAck) .build(); - verify(publishStreamResponseObserver, times(1)).onNext(publishStreamResponse); + verify(publishStreamResponseObserver, timeout(50).times(1)).onNext(publishStreamResponse); // Helidon will call onCompleted after onNext producerBlockItemObserver.onCompleted(); - verify(publishStreamResponseObserver, times(1)).onCompleted(); + verify(publishStreamResponseObserver, timeout(50).times(1)).onCompleted(); } @Test @@ -160,18 +153,14 @@ public void testProducerToManyConsumers() throws IOException, InterruptedExcepti new ItemAckBuilder(), serviceStatus); - PublishStreamRequest publishStreamRequest = + final PublishStreamRequest publishStreamRequest = PublishStreamRequest.newBuilder().setBlockItem(blockItem).build(); producerBlockItemObserver.onNext(publishStreamRequest); - synchronized (lock) { - lock.wait(50); - } - // Confirm each subscriber was notified of the new block - verify(streamObserver1, times(1)).onNext(subscribeStreamResponse); - verify(streamObserver2, times(1)).onNext(subscribeStreamResponse); - verify(streamObserver3, times(1)).onNext(subscribeStreamResponse); + verify(streamObserver1, timeout(50).times(1)).onNext(subscribeStreamResponse); + verify(streamObserver2, timeout(50).times(1)).onNext(subscribeStreamResponse); + verify(streamObserver3, timeout(50).times(1)).onNext(subscribeStreamResponse); // Confirm the BlockStorage write method was // called despite the absence of subscribers @@ -180,23 +169,22 @@ public void testProducerToManyConsumers() throws IOException, InterruptedExcepti @Test public void testOnError() { - ProducerBlockItemObserver producerBlockItemObserver = + final ProducerBlockItemObserver producerBlockItemObserver = new ProducerBlockItemObserver( streamMediator, publishStreamResponseObserver, new ItemAckBuilder(), serviceStatus); - Throwable t = new Throwable("Test error"); + final Throwable t = new Throwable("Test error"); producerBlockItemObserver.onError(t); verify(publishStreamResponseObserver).onError(t); } @Test - public void testItemAckBuilderExceptionTest() - throws InterruptedException, IOException, NoSuchAlgorithmException { + public void testItemAckBuilderExceptionTest() throws IOException, NoSuchAlgorithmException { - ProducerBlockItemObserver producerBlockItemObserver = + final ProducerBlockItemObserver producerBlockItemObserver = new ProducerBlockItemObserver( streamMediator, publishStreamResponseObserver, @@ -207,16 +195,12 @@ public void testItemAckBuilderExceptionTest() when(itemAckBuilder.buildAck(any())) .thenThrow(new NoSuchAlgorithmException("Test exception")); - List blockItems = generateBlockItems(1); - BlockItem blockHeader = blockItems.getFirst(); - PublishStreamRequest publishStreamRequest = + final List blockItems = generateBlockItems(1); + final BlockItem blockHeader = blockItems.getFirst(); + final PublishStreamRequest publishStreamRequest = PublishStreamRequest.newBuilder().setBlockItem(blockHeader).build(); producerBlockItemObserver.onNext(publishStreamRequest); - synchronized (lock) { - lock.wait(50); - } - final PublishStreamResponse.EndOfStream endOfStream = PublishStreamResponse.EndOfStream.newBuilder() .setStatus( @@ -225,6 +209,6 @@ public void testItemAckBuilderExceptionTest() .build(); final PublishStreamResponse errorResponse = PublishStreamResponse.newBuilder().setStatus(endOfStream).build(); - verify(publishStreamResponseObserver, times(1)).onNext(errorResponse); + verify(publishStreamResponseObserver, timeout(50).times(1)).onNext(errorResponse); } }