From 1f62b7eb38e97a354e8c162da34e66f7581a24bf Mon Sep 17 00:00:00 2001 From: Matt Peterson Date: Thu, 1 Aug 2024 14:00:22 -0600 Subject: [PATCH] fix: sanding tests Signed-off-by: Matt Peterson --- .../ConsumerBlockItemObserverTest.java | 34 +++++----- .../mediator/LiveStreamMediatorImplTest.java | 64 +++++-------------- .../storage/BlockAsDirWriterTest.java | 4 +- .../ProducerBlockItemObserverTest.java | 26 +++----- 4 files changed, 43 insertions(+), 85 deletions(-) diff --git a/server/src/test/java/com/hedera/block/server/consumer/ConsumerBlockItemObserverTest.java b/server/src/test/java/com/hedera/block/server/consumer/ConsumerBlockItemObserverTest.java index 5edf21333..36fa74d5e 100644 --- a/server/src/test/java/com/hedera/block/server/consumer/ConsumerBlockItemObserverTest.java +++ b/server/src/test/java/com/hedera/block/server/consumer/ConsumerBlockItemObserverTest.java @@ -40,18 +40,17 @@ public class ConsumerBlockItemObserverTest { @Mock private ObjectEvent objectEvent; @Mock private ServerCallStreamObserver serverCallStreamObserver; + @Mock private InstantSource testClock; @Test public void testProducerTimeoutWithinWindow() { - final InstantSource mockClockInsideWindow = mock(InstantSource.class); - when(mockClockInsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS - 1); + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS); final var consumerBlockItemObserver = new ConsumerBlockItemObserver( TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, + testClock, streamMediator, responseStreamObserver); @@ -74,14 +73,14 @@ public void testProducerTimeoutWithinWindow() { @Test public void testProducerTimeoutOutsideWindow() throws InterruptedException { - final InstantSource mockClockOutsideWindow = mock(InstantSource.class); - when(mockClockOutsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS + 1); + // Mock a clock with 2 different return values in response to anticipated + // millis() calls. Here the second call will always be outside the timeout window. + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS + 1); final var consumerBlockItemObserver = new ConsumerBlockItemObserver( TIMEOUT_THRESHOLD_MILLIS, - mockClockOutsideWindow, + testClock, streamMediator, responseStreamObserver); @@ -94,15 +93,12 @@ public void testProducerTimeoutOutsideWindow() throws InterruptedException { @Test public void testHandlersSetOnObserver() throws InterruptedException { - final InstantSource mockClockInsideWindow = mock(InstantSource.class); - when(mockClockInsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS - 1); + // Mock a clock with 2 different return values in response to anticipated + // millis() calls. Here the second call will always be inside the timeout window. + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS); new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - serverCallStreamObserver); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, serverCallStreamObserver); verify(serverCallStreamObserver, timeout(50)).setOnCloseHandler(any()); verify(serverCallStreamObserver, timeout(50)).setOnCancelHandler(any()); @@ -111,14 +107,14 @@ public void testHandlersSetOnObserver() throws InterruptedException { @Test public void testConsumerNotToSendBeforeBlockHeader() { - final InstantSource mockClockInsideWindow = mock(InstantSource.class); - when(mockClockInsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS - 1); + // Mock a clock with 2 different return values in response to anticipated + // millis() calls. Here the second call will always be inside the timeout window. + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS); final var consumerBlockItemObserver = new ConsumerBlockItemObserver( TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, + testClock, streamMediator, responseStreamObserver); diff --git a/server/src/test/java/com/hedera/block/server/mediator/LiveStreamMediatorImplTest.java b/server/src/test/java/com/hedera/block/server/mediator/LiveStreamMediatorImplTest.java index 20eef71bf..ffd19260c 100644 --- a/server/src/test/java/com/hedera/block/server/mediator/LiveStreamMediatorImplTest.java +++ b/server/src/test/java/com/hedera/block/server/mediator/LiveStreamMediatorImplTest.java @@ -54,6 +54,10 @@ public class LiveStreamMediatorImplTest { @Mock private StreamObserver streamObserver3; @Mock private ServerCallStreamObserver serverCallStreamObserver; + @Mock private InstantSource testClock; + + private final long TIMEOUT_THRESHOLD_MILLIS = 100L; + private final long TEST_TIME = 1_719_427_664_950L; @Test public void testUnsubscribeEach() { @@ -113,36 +117,23 @@ public void testMediatorPersistenceWithoutSubscribers() throws IOException { @Test public void testMediatorPublishEventToSubscribers() throws IOException, InterruptedException { - final long TIMEOUT_THRESHOLD_MILLIS = 100L; - final long TEST_TIME = 1_719_427_664_950L; final var streamMediator = new LiveStreamMediatorImpl( new FileSystemPersistenceHandler(blockReader, blockWriter)); - final InstantSource mockClockInsideWindow = mock(InstantSource.class); - when(mockClockInsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS - 1); + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS); final var concreteObserver1 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver1); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver1); final var concreteObserver2 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver2); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver2); final var concreteObserver3 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver3); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver3); // Set up the subscribers streamMediator.subscribe(concreteObserver1); @@ -178,36 +169,23 @@ public void testMediatorPublishEventToSubscribers() throws IOException, Interrup @Test public void testSubAndUnsubHandling() { - final long TIMEOUT_THRESHOLD_MILLIS = 100L; - final long TEST_TIME = 1_719_427_664_950L; final var streamMediator = new LiveStreamMediatorImpl( new FileSystemPersistenceHandler(blockReader, blockWriter)); - final InstantSource mockClockInsideWindow = mock(InstantSource.class); - when(mockClockInsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS - 1); + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS); final var concreteObserver1 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver1); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver1); final var concreteObserver2 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver2); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver2); final var concreteObserver3 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver3); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver3); // Set up the subscribers streamMediator.subscribe(concreteObserver1); @@ -222,21 +200,16 @@ public void testSubAndUnsubHandling() { @Test public void testOnCancelSubscriptionHandling() throws IOException { - final long TIMEOUT_THRESHOLD_MILLIS = 100L; - final long TEST_TIME = 1_719_427_664_950L; - final var streamMediator = new LiveStreamMediatorImpl( new FileSystemPersistenceHandler(blockReader, blockWriter)); - final InstantSource mockClockInsideWindow = mock(InstantSource.class); - when(mockClockInsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS - 1); + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS); final var testConsumerBlockItemObserver = new TestConsumerBlockItemObserver( TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, + testClock, streamMediator, serverCallStreamObserver); @@ -256,21 +229,18 @@ public void testOnCancelSubscriptionHandling() throws IOException { @Test public void testOnCloseSubscriptionHandling() throws IOException { - final long TIMEOUT_THRESHOLD_MILLIS = 100L; - final long TEST_TIME = 1_719_427_664_950L; final var streamMediator = new LiveStreamMediatorImpl( new FileSystemPersistenceHandler(blockReader, blockWriter)); - final InstantSource mockClockOutsideWindow = mock(InstantSource.class); - when(mockClockOutsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS + 1); + // testClock configured to be outside the timeout window + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS + 1); final var testConsumerBlockItemObserver = new TestConsumerBlockItemObserver( TIMEOUT_THRESHOLD_MILLIS, - mockClockOutsideWindow, + testClock, streamMediator, serverCallStreamObserver); diff --git a/server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirWriterTest.java b/server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirWriterTest.java index 448a63890..2492f7a3a 100644 --- a/server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirWriterTest.java +++ b/server/src/test/java/com/hedera/block/server/persistence/storage/BlockAsDirWriterTest.java @@ -211,11 +211,11 @@ public void testPartialBlockRemoval() throws IOException { for (int i = 0; i < 23; i++) { // Prepare the block writer to call the actual write method - // for 22 block items + // for 23 block items doCallRealMethod().when(blockWriter).write(same(blockItems.get(i))); } - // Simulate an IOException when writing the 23rd block item + // Simulate an IOException when writing the 24th block item // from an overridden write method in sub-class. doThrow(IOException.class).when(blockWriter).write(any(), same(blockItems.get(23))); 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 a9f23cf74..3dc0ac922 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 @@ -59,6 +59,7 @@ public class ProducerBlockItemObserverTest { @Mock private StreamObserver streamObserver3; @Mock private ServiceStatus serviceStatus; + @Mock private InstantSource testClock; @Test public void testProducerOnNext() throws IOException, NoSuchAlgorithmException { @@ -98,37 +99,28 @@ public void testProducerOnNext() throws IOException, NoSuchAlgorithmException { @Test public void testProducerToManyConsumers() throws IOException, InterruptedException { - final long TIMEOUT_THRESHOLD_MILLIS = 100L; - final long TEST_TIME = 1_719_427_664_950L; final ServiceStatus serviceStatus = new ServiceStatusImpl(); final var streamMediator = new LiveStreamMediatorImpl( new FileSystemPersistenceHandler(blockReader, blockWriter), serviceStatus); - final InstantSource mockClockInsideWindow = mock(InstantSource.class); - when(mockClockInsideWindow.millis()) - .thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS - 1); + // Mock a clock with 2 different return values in response to anticipated + // millis() calls. Here the second call will always be inside the timeout window. + long TIMEOUT_THRESHOLD_MILLIS = 100L; + long TEST_TIME = 1_719_427_664_950L; + when(testClock.millis()).thenReturn(TEST_TIME, TEST_TIME + TIMEOUT_THRESHOLD_MILLIS); final var concreteObserver1 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver1); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver1); final var concreteObserver2 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver2); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver2); final var concreteObserver3 = new ConsumerBlockItemObserver( - TIMEOUT_THRESHOLD_MILLIS, - mockClockInsideWindow, - streamMediator, - streamObserver3); + TIMEOUT_THRESHOLD_MILLIS, testClock, streamMediator, streamObserver3); // Set up the subscribers streamMediator.subscribe(concreteObserver1);