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

Commit

Permalink
fix: Watchdog controls lifecycle of the future, not executor (#1890) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
blakeli0 committed Dec 9, 2022
1 parent f7caf21 commit 89e55e4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 11 deletions.
16 changes: 13 additions & 3 deletions gax/src/main/java/com/google/api/gax/rpc/Watchdog.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,11 @@
import java.util.Map.Entry;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
Expand All @@ -61,6 +63,7 @@
* </ul>
*/
public final class Watchdog implements Runnable, BackgroundResource {

private static final Logger LOG = Logger.getLogger(Watchdog.class.getName());

// Dummy value to convert the ConcurrentHashMap into a Set
Expand Down Expand Up @@ -138,12 +141,12 @@ public void shutdown() {

@Override
public boolean isShutdown() {
return executor.isShutdown();
return future.isCancelled();
}

@Override
public boolean isTerminated() {
return executor.isTerminated();
return future.isDone();
}

@Override
Expand All @@ -153,7 +156,14 @@ public void shutdownNow() {

@Override
public boolean awaitTermination(long duration, TimeUnit unit) throws InterruptedException {
return executor.awaitTermination(duration, unit);
try {
future.get(duration, unit);
return true;
} catch (ExecutionException | CancellationException e) {
return true;
} catch (TimeoutException e) {
return false;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,6 @@ public interface WatchdogProvider {

Watchdog getWatchdog();

/** Return true if the watchdog should be automatically unscheduled. */
boolean shouldAutoClose();
}
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 89e55e4

Please sign in to comment.