Skip to content

Commit

Permalink
fix: sanding tests
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Peterson <[email protected]>
  • Loading branch information
mattp-swirldslabs committed Aug 1, 2024
1 parent 7594631 commit 1f62b7e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,17 @@ public class ConsumerBlockItemObserverTest {
@Mock private ObjectEvent<SubscribeStreamResponse> objectEvent;

@Mock private ServerCallStreamObserver<SubscribeStreamResponse> 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);

Expand All @@ -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);

Expand All @@ -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());
Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ public class LiveStreamMediatorImplTest {
@Mock private StreamObserver<SubscribeStreamResponse> streamObserver3;

@Mock private ServerCallStreamObserver<SubscribeStreamResponse> 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() {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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);

Expand All @@ -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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class ProducerBlockItemObserverTest {
@Mock private StreamObserver<SubscribeStreamResponse> streamObserver3;

@Mock private ServiceStatus serviceStatus;
@Mock private InstantSource testClock;

@Test
public void testProducerOnNext() throws IOException, NoSuchAlgorithmException {
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1f62b7e

Please sign in to comment.