aboutsummaryrefslogtreecommitdiff
path: root/guava-tests/test/com/google/common/util/concurrent/JdkFutureAdaptersTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'guava-tests/test/com/google/common/util/concurrent/JdkFutureAdaptersTest.java')
-rw-r--r--guava-tests/test/com/google/common/util/concurrent/JdkFutureAdaptersTest.java109
1 files changed, 47 insertions, 62 deletions
diff --git a/guava-tests/test/com/google/common/util/concurrent/JdkFutureAdaptersTest.java b/guava-tests/test/com/google/common/util/concurrent/JdkFutureAdaptersTest.java
index 1f1111b2e..1f41cf474 100644
--- a/guava-tests/test/com/google/common/util/concurrent/JdkFutureAdaptersTest.java
+++ b/guava-tests/test/com/google/common/util/concurrent/JdkFutureAdaptersTest.java
@@ -25,14 +25,17 @@ import static java.util.concurrent.TimeUnit.SECONDS;
import com.google.common.testing.ClassSanityTester;
import com.google.common.util.concurrent.FuturesTest.ExecutorSpy;
+import com.google.common.util.concurrent.FuturesTest.SingleCallListener;
+
+import junit.framework.AssertionFailedError;
+import junit.framework.TestCase;
+
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Future;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
-import junit.framework.AssertionFailedError;
-import junit.framework.TestCase;
/**
* Unit tests for {@link JdkFutureAdapters}.
@@ -48,38 +51,14 @@ public class JdkFutureAdaptersTest extends TestCase {
assertSame(listenableFuture, listenInPoolThread(listenableFuture));
}
- private static class SingleCallListener implements Runnable {
-
- private boolean expectCall = false;
- private final CountDownLatch calledCountDown = new CountDownLatch(1);
-
- @Override
- public void run() {
- assertTrue("Listener called before it was expected", expectCall);
- assertFalse("Listener called more than once", wasCalled());
- calledCountDown.countDown();
- }
-
- public void expectCall() {
- assertFalse("expectCall is already true", expectCall);
- expectCall = true;
- }
-
- public boolean wasCalled() {
- return calledCountDown.getCount() == 0;
- }
-
- public void waitForCall() throws InterruptedException {
- assertTrue("expectCall is false", expectCall);
- calledCountDown.await();
- }
- }
-
- public void testListenInPoolThreadIgnoresExecutorWhenDelegateIsDone() throws Exception {
- NonListenableSettableFuture<String> abstractFuture = NonListenableSettableFuture.create();
+ public void testListenInPoolThreadIgnoresExecutorWhenDelegateIsDone()
+ throws Exception {
+ NonListenableSettableFuture<String> abstractFuture =
+ NonListenableSettableFuture.create();
abstractFuture.set(DATA1);
ExecutorSpy spy = new ExecutorSpy(directExecutor());
- ListenableFuture<String> listenableFuture = listenInPoolThread(abstractFuture, spy);
+ ListenableFuture<String> listenableFuture =
+ listenInPoolThread(abstractFuture, spy);
SingleCallListener singleCallListener = new SingleCallListener();
singleCallListener.expectCall();
@@ -101,11 +80,13 @@ public class JdkFutureAdaptersTest extends TestCase {
}
public void testListenInPoolThreadUsesGivenExecutor() throws Exception {
- ExecutorService executorService =
- newCachedThreadPool(new ThreadFactoryBuilder().setDaemon(true).build());
- NonListenableSettableFuture<String> abstractFuture = NonListenableSettableFuture.create();
+ ExecutorService executorService = newCachedThreadPool(
+ new ThreadFactoryBuilder().setDaemon(true).build());
+ NonListenableSettableFuture<String> abstractFuture =
+ NonListenableSettableFuture.create();
ExecutorSpy spy = new ExecutorSpy(executorService);
- ListenableFuture<String> listenableFuture = listenInPoolThread(abstractFuture, spy);
+ ListenableFuture<String> listenableFuture =
+ listenInPoolThread(abstractFuture, spy);
SingleCallListener singleCallListener = new SingleCallListener();
singleCallListener.expectCall();
@@ -124,23 +105,22 @@ public class JdkFutureAdaptersTest extends TestCase {
assertTrue(listenableFuture.isDone());
}
- public void testListenInPoolThreadCustomExecutorInterrupted() throws Exception {
+ public void testListenInPoolThreadCustomExecutorInterrupted()
+ throws Exception {
final CountDownLatch submitSuccessful = new CountDownLatch(1);
- ExecutorService executorService =
- new ThreadPoolExecutor(
- 0,
- Integer.MAX_VALUE,
- 60L,
- TimeUnit.SECONDS,
- new SynchronousQueue<Runnable>(),
- new ThreadFactoryBuilder().setDaemon(true).build()) {
- @Override
- protected void beforeExecute(Thread t, Runnable r) {
- submitSuccessful.countDown();
- }
- };
- NonListenableSettableFuture<String> abstractFuture = NonListenableSettableFuture.create();
- ListenableFuture<String> listenableFuture = listenInPoolThread(abstractFuture, executorService);
+ ExecutorService executorService = new ThreadPoolExecutor(
+ 0, Integer.MAX_VALUE, 60L, TimeUnit.SECONDS,
+ new SynchronousQueue<Runnable>(),
+ new ThreadFactoryBuilder().setDaemon(true).build()) {
+ @Override
+ protected void beforeExecute(Thread t, Runnable r) {
+ submitSuccessful.countDown();
+ }
+ };
+ NonListenableSettableFuture<String> abstractFuture =
+ NonListenableSettableFuture.create();
+ ListenableFuture<String> listenableFuture =
+ listenInPoolThread(abstractFuture, executorService);
SingleCallListener singleCallListener = new SingleCallListener();
singleCallListener.expectCall();
@@ -164,16 +144,19 @@ public class JdkFutureAdaptersTest extends TestCase {
assertTrue(listenableFuture.isDone());
}
- /** A Future that doesn't implement ListenableFuture, useful for testing listenInPoolThread. */
- private static final class NonListenableSettableFuture<V> extends ForwardingFuture<V> {
+ /**
+ * A Future that doesn't implement ListenableFuture, useful for testing
+ * listenInPoolThread.
+ */
+ private static final class NonListenableSettableFuture<V>
+ extends ForwardingFuture<V> {
static <V> NonListenableSettableFuture<V> create() {
return new NonListenableSettableFuture<V>();
}
final SettableFuture<V> delegate = SettableFuture.create();
- @Override
- protected Future<V> delegate() {
+ @Override protected Future<V> delegate() {
return delegate;
}
@@ -182,7 +165,8 @@ public class JdkFutureAdaptersTest extends TestCase {
}
}
- private static final class RuntimeExceptionThrowingFuture<V> implements Future<V> {
+ private static final class RuntimeExceptionThrowingFuture<V>
+ implements Future<V> {
final CountDownLatch allowGetToComplete = new CountDownLatch(1);
@Override
@@ -233,16 +217,17 @@ public class JdkFutureAdaptersTest extends TestCase {
}
}
- public void testListenInPoolThreadRunsListenerAfterRuntimeException() throws Exception {
- RuntimeExceptionThrowingFuture<String> input = new RuntimeExceptionThrowingFuture<>();
+ public void testListenInPoolThreadRunsListenerAfterRuntimeException()
+ throws Exception {
+ RuntimeExceptionThrowingFuture<String> input =
+ new RuntimeExceptionThrowingFuture<String>();
/*
* The compiler recognizes that "input instanceof ListenableFuture" is
* impossible. We want the test, though, in case that changes in the future,
* so we use isInstance instead.
*/
- assertFalse(
- "Can't test the main listenInPoolThread path "
- + "if the input is already a ListenableFuture",
+ assertFalse("Can't test the main listenInPoolThread path "
+ + "if the input is already a ListenableFuture",
ListenableFuture.class.isInstance(input));
ListenableFuture<String> listenable = listenInPoolThread(input);
/*