Skip to content
This repository has been archived by the owner on Sep 26, 2023. It is now read-only.

test: Add unit tests to Watchdog #1919

Merged
merged 2 commits into from
Dec 2, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 74 additions & 8 deletions gax/src/test/java/com/google/api/gax/rpc/WatchdogTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
Expand Down Expand Up @@ -195,14 +196,7 @@ public void testMultiple() throws Exception {
@SuppressWarnings("unchecked")
public void testWatchdogBeingClosed() {
ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
ScheduledExecutorService mockExecutor = Mockito.mock(ScheduledExecutorService.class);
Mockito.when(
mockExecutor.scheduleAtFixedRate(
Mockito.any(Watchdog.class),
Mockito.anyLong(),
Mockito.anyLong(),
Mockito.any(TimeUnit.class)))
.thenReturn(future);
ScheduledExecutorService mockExecutor = getMockExecutorService(future);
Watchdog underTest = Watchdog.create(clock, checkInterval, mockExecutor);
assertThat(underTest).isInstanceOf(BackgroundResource.class);

Expand All @@ -219,6 +213,78 @@ public void testWatchdogBeingClosed() {
Mockito.verifyNoMoreInteractions(mockExecutor);
}

@Test
public void awaitTermination_shouldReturnTrueIfFutureIsDone() throws Exception {
int duration = 1000;
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
// Mockito.doNothing().when(future).get(duration, timeUnit);
blakeli0 marked this conversation as resolved.
Show resolved Hide resolved
ScheduledExecutorService mockExecutor = getMockExecutorService(future);
Watchdog watchdog = Watchdog.create(clock, checkInterval, mockExecutor);
watchdog.shutdown();

boolean actual = watchdog.awaitTermination(duration, timeUnit);

assertThat(actual).isTrue();
}

@Test
public void awaitTermination_shouldReturnFalseIfGettingFutureTimedOut() throws Exception {
int duration = 1000;
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
Mockito.doThrow(new TimeoutException()).when(future).get(duration, timeUnit);
ScheduledExecutorService mockExecutor = getMockExecutorService(future);
Watchdog watchdog = Watchdog.create(clock, checkInterval, mockExecutor);

boolean actual = watchdog.awaitTermination(duration, timeUnit);

assertThat(actual).isFalse();
}

@Test
public void awaitTermination_shouldReturnTrueIfFutureIsAlreadyCancelled() throws Exception {
int duration = 1000;
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
Mockito.doThrow(new CancellationException()).when(future).get(duration, timeUnit);
ScheduledExecutorService mockExecutor = getMockExecutorService(future);
Watchdog watchdog = Watchdog.create(clock, checkInterval, mockExecutor);

boolean actual = watchdog.awaitTermination(duration, timeUnit);

assertThat(actual).isTrue();
}

@Test
public void awaitTermination_shouldReturnFalseIfGettingFutureThrowsExecutionException()
throws Exception {
int duration = 1000;
TimeUnit timeUnit = TimeUnit.MILLISECONDS;
ScheduledFuture future = Mockito.mock(ScheduledFuture.class);
Mockito.doThrow(new ExecutionException(new RuntimeException()))
.when(future)
.get(duration, timeUnit);
ScheduledExecutorService mockExecutor = getMockExecutorService(future);
Watchdog watchdog = Watchdog.create(clock, checkInterval, mockExecutor);

boolean actual = watchdog.awaitTermination(duration, timeUnit);

assertThat(actual).isTrue();
}

private ScheduledExecutorService getMockExecutorService(ScheduledFuture future) {
ScheduledExecutorService mockExecutor = Mockito.mock(ScheduledExecutorService.class);
Mockito.when(
mockExecutor.scheduleAtFixedRate(
Mockito.any(Watchdog.class),
Mockito.anyLong(),
Mockito.anyLong(),
Mockito.any(TimeUnit.class)))
.thenReturn(future);
return mockExecutor;
}

static class AccumulatingObserver<T> implements ResponseObserver<T> {
SettableApiFuture<StreamController> controller = SettableApiFuture.create();
Queue<T> responses = Queues.newLinkedBlockingDeque();
Expand Down