diff options
Diffstat (limited to 'android/guava-testlib/src/com/google/common/util/concurrent/testing/AbstractListenableFutureTest.java')
-rw-r--r-- | android/guava-testlib/src/com/google/common/util/concurrent/testing/AbstractListenableFutureTest.java | 47 |
1 files changed, 13 insertions, 34 deletions
diff --git a/android/guava-testlib/src/com/google/common/util/concurrent/testing/AbstractListenableFutureTest.java b/android/guava-testlib/src/com/google/common/util/concurrent/testing/AbstractListenableFutureTest.java index abe419e05..325388342 100644 --- a/android/guava-testlib/src/com/google/common/util/concurrent/testing/AbstractListenableFutureTest.java +++ b/android/guava-testlib/src/com/google/common/util/concurrent/testing/AbstractListenableFutureTest.java @@ -18,6 +18,7 @@ package com.google.common.util.concurrent.testing; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; +import static org.junit.Assert.assertThrows; import com.google.common.annotations.GwtIncompatible; import com.google.common.util.concurrent.ListenableFuture; @@ -70,29 +71,17 @@ public abstract class AbstractListenableFutureTest extends TestCase { assertFalse(future.isDone()); assertFalse(future.isCancelled()); - CountDownLatch successLatch = new CountDownLatch(1); - Throwable[] badness = new Throwable[1]; - - // Wait on the future in a separate thread. - new Thread( - () -> { - try { - assertSame(Boolean.TRUE, future.get()); - successLatch.countDown(); - } catch (Throwable t) { - t.printStackTrace(); - badness[0] = t; - } - }) - .start(); + ExecutorService executor = Executors.newSingleThreadExecutor(); - // Release the future value. - latch.countDown(); + try { + Future<Boolean> getResult = executor.submit(() -> future.get()); - assertTrue(successLatch.await(10, SECONDS)); + // Release the future value. + latch.countDown(); - if (badness[0] != null) { - throw badness[0]; + assertTrue(getResult.get(10, SECONDS)); + } finally { + executor.shutdownNow(); } assertTrue(future.isDone()); @@ -127,13 +116,8 @@ public abstract class AbstractListenableFutureTest extends TestCase { // Run cancellation in a separate thread as an extra thread-safety test. new Thread( () -> { - try { - future.get(); - } catch (CancellationException expected) { - successLatch.countDown(); - } catch (Exception ignored) { - // All other errors are ignored, we expect a cancellation. - } + assertThrows(CancellationException.class, future::get); + successLatch.countDown(); }) .start(); @@ -160,13 +144,8 @@ public abstract class AbstractListenableFutureTest extends TestCase { new Thread( () -> { - try { - future.get(); - } catch (CancellationException expected) { - successLatch.countDown(); - } catch (Exception ignored) { - // No success latch count down. - } + assertThrows(CancellationException.class, future::get); + successLatch.countDown(); }) .start(); |