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

Commit

Permalink
test: Add unit tests for Watchdog (#1919)
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeli0 committed Dec 2, 2022
1 parent bd1714e commit fb4a180
Showing 1 changed file with 73 additions and 8 deletions.
81 changes: 73 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,77 @@ 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);
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

0 comments on commit fb4a180

Please sign in to comment.