HELLO·Android
系统源代码
IT资讯
技术文章
我的收藏
注册
登录
-
我收藏的文章
创建代码块
我的代码块
我的账号
Nougat 7.1
|
7.1.1_r28
下载
查看原文件
收藏
根目录
libcore
jsr166-tests
src
test
java
jsr166
CompletableFutureTest.java
/* * Written by Doug Lea and Martin Buchholz with assistance from * members of JCP JSR-166 Expert Group and released to the public * domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package jsr166; import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.SECONDS; import static java.util.concurrent.CompletableFuture.completedFuture; import static java.util.concurrent.CompletableFuture.failedFuture; import java.lang.reflect.Method; import java.lang.reflect.Modifier; import java.util.stream.Collectors; import java.util.stream.Stream; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Objects; import java.util.Set; import java.util.concurrent.Callable; import java.util.concurrent.CancellationException; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionStage; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executor; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicReference; import java.util.function.BiConsumer; import java.util.function.BiFunction; import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; import java.util.function.Supplier; import junit.framework.AssertionFailedError; import junit.framework.Test; import junit.framework.TestSuite; public class CompletableFutureTest extends JSR166TestCase { // android-note: Removed because the CTS runner does a bad job of // retrying tests that have suite() declarations. // // public static void main(String[] args) { // main(suite(), args); // } // public static Test suite() { // return new TestSuite(CompletableFutureTest.class); // } static class CFException extends RuntimeException {} void checkIncomplete(CompletableFuture> f) { assertFalse(f.isDone()); assertFalse(f.isCancelled()); assertTrue(f.toString().contains("Not completed")); try { assertNull(f.getNow(null)); } catch (Throwable fail) { threadUnexpectedException(fail); } try { f.get(0L, SECONDS); shouldThrow(); } catch (TimeoutException success) {} catch (Throwable fail) { threadUnexpectedException(fail); } }
void checkCompletedNormally(CompletableFuture
f, T value) { checkTimedGet(f, value); try { assertEquals(value, f.join()); } catch (Throwable fail) { threadUnexpectedException(fail); } try { assertEquals(value, f.getNow(null)); } catch (Throwable fail) { threadUnexpectedException(fail); } try { assertEquals(value, f.get()); } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(f.isDone()); assertFalse(f.isCancelled()); assertFalse(f.isCompletedExceptionally()); assertTrue(f.toString().contains("[Completed normally]")); } /** * Returns the "raw" internal exceptional completion of f, * without any additional wrapping with CompletionException. */
Throwable exceptionalCompletion(CompletableFuture
f) { // handle (and whenComplete) can distinguish between "direct" // and "wrapped" exceptional completion return f.handle((U u, Throwable t) -> t).join(); } void checkCompletedExceptionally(CompletableFuture> f, boolean wrapped, Consumer
checker) { Throwable cause = exceptionalCompletion(f); if (wrapped) { assertTrue(cause instanceof CompletionException); cause = cause.getCause(); } checker.accept(cause); long startTime = System.nanoTime(); try { f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (ExecutionException success) { assertSame(cause, success.getCause()); } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); try { f.join(); shouldThrow(); } catch (CompletionException success) { assertSame(cause, success.getCause()); } catch (Throwable fail) { threadUnexpectedException(fail); } try { f.getNow(null); shouldThrow(); } catch (CompletionException success) { assertSame(cause, success.getCause()); } catch (Throwable fail) { threadUnexpectedException(fail); } try { f.get(); shouldThrow(); } catch (ExecutionException success) { assertSame(cause, success.getCause()); } catch (Throwable fail) { threadUnexpectedException(fail); } assertFalse(f.isCancelled()); assertTrue(f.isDone()); assertTrue(f.isCompletedExceptionally()); assertTrue(f.toString().contains("[Completed exceptionally]")); } void checkCompletedWithWrappedCFException(CompletableFuture> f) { checkCompletedExceptionally(f, true, (t) -> assertTrue(t instanceof CFException)); } void checkCompletedWithWrappedCancellationException(CompletableFuture> f) { checkCompletedExceptionally(f, true, (t) -> assertTrue(t instanceof CancellationException)); } void checkCompletedWithTimeoutException(CompletableFuture> f) { checkCompletedExceptionally(f, false, (t) -> assertTrue(t instanceof TimeoutException)); } void checkCompletedWithWrappedException(CompletableFuture> f, Throwable ex) { checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); } void checkCompletedExceptionally(CompletableFuture> f, Throwable ex) { checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); } void checkCancelled(CompletableFuture> f) { long startTime = System.nanoTime(); try { f.get(LONG_DELAY_MS, MILLISECONDS); shouldThrow(); } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); try { f.join(); shouldThrow(); } catch (CancellationException success) {} try { f.getNow(null); shouldThrow(); } catch (CancellationException success) {} try { f.get(); shouldThrow(); } catch (CancellationException success) { } catch (Throwable fail) { threadUnexpectedException(fail); } assertTrue(exceptionalCompletion(f) instanceof CancellationException); assertTrue(f.isDone()); assertTrue(f.isCompletedExceptionally()); assertTrue(f.isCancelled()); assertTrue(f.toString().contains("[Completed exceptionally]")); } /** * A newly constructed CompletableFuture is incomplete, as indicated * by methods isDone, isCancelled, and getNow */ public void testConstructor() { CompletableFuture
f = new CompletableFuture<>(); checkIncomplete(f); } /** * complete completes normally, as indicated by methods isDone, * isCancelled, join, get, and getNow */ public void testComplete() { for (Integer v1 : new Integer[] { 1, null }) { CompletableFuture
f = new CompletableFuture<>(); checkIncomplete(f); assertTrue(f.complete(v1)); assertFalse(f.complete(v1)); checkCompletedNormally(f, v1); }} /** * completeExceptionally completes exceptionally, as indicated by * methods isDone, isCancelled, join, get, and getNow */ public void testCompleteExceptionally() { CompletableFuture
f = new CompletableFuture<>(); CFException ex = new CFException(); checkIncomplete(f); f.completeExceptionally(ex); checkCompletedExceptionally(f, ex); } /** * cancel completes exceptionally and reports cancelled, as indicated by * methods isDone, isCancelled, join, get, and getNow */ public void testCancel() { for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { CompletableFuture
f = new CompletableFuture<>(); checkIncomplete(f); assertTrue(f.cancel(mayInterruptIfRunning)); assertTrue(f.cancel(mayInterruptIfRunning)); assertTrue(f.cancel(!mayInterruptIfRunning)); checkCancelled(f); }} /** * obtrudeValue forces completion with given value */ public void testObtrudeValue() { CompletableFuture
f = new CompletableFuture<>(); checkIncomplete(f); assertTrue(f.complete(one)); checkCompletedNormally(f, one); f.obtrudeValue(three); checkCompletedNormally(f, three); f.obtrudeValue(two); checkCompletedNormally(f, two); f = new CompletableFuture<>(); f.obtrudeValue(three); checkCompletedNormally(f, three); f.obtrudeValue(null); checkCompletedNormally(f, null); f = new CompletableFuture<>(); f.completeExceptionally(new CFException()); f.obtrudeValue(four); checkCompletedNormally(f, four); } /** * obtrudeException forces completion with given exception */ public void testObtrudeException() { for (Integer v1 : new Integer[] { 1, null }) { CFException ex; CompletableFuture
f; f = new CompletableFuture<>(); assertTrue(f.complete(v1)); for (int i = 0; i < 2; i++) { f.obtrudeException(ex = new CFException()); checkCompletedExceptionally(f, ex); } f = new CompletableFuture<>(); for (int i = 0; i < 2; i++) { f.obtrudeException(ex = new CFException()); checkCompletedExceptionally(f, ex); } f = new CompletableFuture<>(); f.completeExceptionally(ex = new CFException()); f.obtrudeValue(v1); checkCompletedNormally(f, v1); f.obtrudeException(ex = new CFException()); checkCompletedExceptionally(f, ex); f.completeExceptionally(new CFException()); checkCompletedExceptionally(f, ex); assertFalse(f.complete(v1)); checkCompletedExceptionally(f, ex); }} /** * getNumberOfDependents returns number of dependent tasks */ public void testGetNumberOfDependents() { for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { CompletableFuture
f = new CompletableFuture<>(); assertEquals(0, f.getNumberOfDependents()); final CompletableFuture
g = m.thenRun(f, new Noop(m)); assertEquals(1, f.getNumberOfDependents()); assertEquals(0, g.getNumberOfDependents()); final CompletableFuture
h = m.thenRun(f, new Noop(m)); assertEquals(2, f.getNumberOfDependents()); assertEquals(0, h.getNumberOfDependents()); assertTrue(f.complete(v1)); checkCompletedNormally(g, null); checkCompletedNormally(h, null); assertEquals(0, f.getNumberOfDependents()); assertEquals(0, g.getNumberOfDependents()); assertEquals(0, h.getNumberOfDependents()); }} /** * toString indicates current completion state */ public void testToString() { CompletableFuture
f; f = new CompletableFuture
(); assertTrue(f.toString().contains("[Not completed]")); assertTrue(f.complete("foo")); assertTrue(f.toString().contains("[Completed normally]")); f = new CompletableFuture
(); assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); assertTrue(f.toString().contains("[Completed exceptionally]")); for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { f = new CompletableFuture
(); assertTrue(f.cancel(mayInterruptIfRunning)); assertTrue(f.toString().contains("[Completed exceptionally]")); } } /** * completedFuture returns a completed CompletableFuture with given value */ public void testCompletedFuture() { CompletableFuture
f = CompletableFuture.completedFuture("test"); checkCompletedNormally(f, "test"); } abstract class CheckedAction { int invocationCount = 0; final ExecutionMode m; CheckedAction(ExecutionMode m) { this.m = m; } void invoked() { m.checkExecutionMode(); assertEquals(0, invocationCount++); } void assertNotInvoked() { assertEquals(0, invocationCount); } void assertInvoked() { assertEquals(1, invocationCount); } } abstract class CheckedIntegerAction extends CheckedAction { Integer value; CheckedIntegerAction(ExecutionMode m) { super(m); } void assertValue(Integer expected) { assertInvoked(); assertEquals(expected, value); } } class IntegerSupplier extends CheckedAction implements Supplier
{ final Integer value; IntegerSupplier(ExecutionMode m, Integer value) { super(m); this.value = value; } public Integer get() { invoked(); return value; } } // A function that handles and produces null values as well. static Integer inc(Integer x) { return (x == null) ? null : x + 1; } class NoopConsumer extends CheckedIntegerAction implements Consumer
{ NoopConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { invoked(); value = x; } } class IncFunction extends CheckedIntegerAction implements Function
{ IncFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x) { invoked(); return value = inc(x); } } // Choose non-commutative actions for better coverage // A non-commutative function that handles and produces null values as well. static Integer subtract(Integer x, Integer y) { return (x == null && y == null) ? null : ((x == null) ? 42 : x.intValue()) - ((y == null) ? 99 : y.intValue()); } class SubtractAction extends CheckedIntegerAction implements BiConsumer
{ SubtractAction(ExecutionMode m) { super(m); } public void accept(Integer x, Integer y) { invoked(); value = subtract(x, y); } } class SubtractFunction extends CheckedIntegerAction implements BiFunction
{ SubtractFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x, Integer y) { invoked(); return value = subtract(x, y); } } class Noop extends CheckedAction implements Runnable { Noop(ExecutionMode m) { super(m); } public void run() { invoked(); } } class FailingSupplier extends CheckedAction implements Supplier
{ FailingSupplier(ExecutionMode m) { super(m); } public Integer get() { invoked(); throw new CFException(); } } class FailingConsumer extends CheckedIntegerAction implements Consumer
{ FailingConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { invoked(); value = x; throw new CFException(); } } class FailingBiConsumer extends CheckedIntegerAction implements BiConsumer
{ FailingBiConsumer(ExecutionMode m) { super(m); } public void accept(Integer x, Integer y) { invoked(); value = subtract(x, y); throw new CFException(); } } class FailingFunction extends CheckedIntegerAction implements Function
{ FailingFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x) { invoked(); value = x; throw new CFException(); } } class FailingBiFunction extends CheckedIntegerAction implements BiFunction
{ FailingBiFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x, Integer y) { invoked(); value = subtract(x, y); throw new CFException(); } } class FailingRunnable extends CheckedAction implements Runnable { FailingRunnable(ExecutionMode m) { super(m); } public void run() { invoked(); throw new CFException(); } } class CompletableFutureInc extends CheckedIntegerAction implements Function
> { CompletableFutureInc(ExecutionMode m) { super(m); } public CompletableFuture
apply(Integer x) { invoked(); value = x; CompletableFuture
f = new CompletableFuture<>(); assertTrue(f.complete(inc(x))); return f; } } class FailingCompletableFutureFunction extends CheckedIntegerAction implements Function
> { FailingCompletableFutureFunction(ExecutionMode m) { super(m); } public CompletableFuture
apply(Integer x) { invoked(); value = x; throw new CFException(); } } // Used for explicit executor tests static final class ThreadExecutor implements Executor { final AtomicInteger count = new AtomicInteger(0); static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); static boolean startedCurrentThread() { return Thread.currentThread().getThreadGroup() == tg; } public void execute(Runnable r) { count.getAndIncrement(); new Thread(tg, r).start(); } } static final boolean defaultExecutorIsCommonPool = ForkJoinPool.getCommonPoolParallelism() > 1; /** * Permits the testing of parallel code for the 3 different * execution modes without copy/pasting all the test methods. */ enum ExecutionMode { SYNC { public void checkExecutionMode() { assertFalse(ThreadExecutor.startedCurrentThread()); assertNull(ForkJoinTask.getPool()); } public CompletableFuture
runAsync(Runnable a) { throw new UnsupportedOperationException(); } public
CompletableFuture
supplyAsync(Supplier
a) { throw new UnsupportedOperationException(); } public
CompletableFuture
thenRun (CompletableFuture
f, Runnable a) { return f.thenRun(a); } public
CompletableFuture
thenAccept (CompletableFuture
f, Consumer super T> a) { return f.thenAccept(a); } public
CompletableFuture
thenApply (CompletableFuture
f, Function super T,U> a) { return f.thenApply(a); } public
CompletableFuture
thenCompose (CompletableFuture
f, Function super T,? extends CompletionStage
> a) { return f.thenCompose(a); } public
CompletableFuture
handle (CompletableFuture
f, BiFunction super T,Throwable,? extends U> a) { return f.handle(a); } public
CompletableFuture
whenComplete (CompletableFuture
f, BiConsumer super T,? super Throwable> a) { return f.whenComplete(a); } public
CompletableFuture
runAfterBoth (CompletableFuture
f, CompletableFuture
g, Runnable a) { return f.runAfterBoth(g, a); } public
CompletableFuture
thenAcceptBoth (CompletableFuture
f, CompletionStage extends U> g, BiConsumer super T,? super U> a) { return f.thenAcceptBoth(g, a); } public
CompletableFuture
thenCombine (CompletableFuture
f, CompletionStage extends U> g, BiFunction super T,? super U,? extends V> a) { return f.thenCombine(g, a); } public
CompletableFuture
runAfterEither (CompletableFuture
f, CompletionStage> g, java.lang.Runnable a) { return f.runAfterEither(g, a); } public
CompletableFuture
acceptEither (CompletableFuture
f, CompletionStage extends T> g, Consumer super T> a) { return f.acceptEither(g, a); } public
CompletableFuture
applyToEither (CompletableFuture
f, CompletionStage extends T> g, Function super T,U> a) { return f.applyToEither(g, a); } }, ASYNC { public void checkExecutionMode() { assertEquals(defaultExecutorIsCommonPool, (ForkJoinPool.commonPool() == ForkJoinTask.getPool())); } public CompletableFuture
runAsync(Runnable a) { return CompletableFuture.runAsync(a); } public
CompletableFuture
supplyAsync(Supplier
a) { return CompletableFuture.supplyAsync(a); } public
CompletableFuture
thenRun (CompletableFuture
f, Runnable a) { return f.thenRunAsync(a); } public
CompletableFuture
thenAccept (CompletableFuture
f, Consumer super T> a) { return f.thenAcceptAsync(a); } public
CompletableFuture
thenApply (CompletableFuture
f, Function super T,U> a) { return f.thenApplyAsync(a); } public
CompletableFuture
thenCompose (CompletableFuture
f, Function super T,? extends CompletionStage
> a) { return f.thenComposeAsync(a); } public
CompletableFuture
handle (CompletableFuture
f, BiFunction super T,Throwable,? extends U> a) { return f.handleAsync(a); } public
CompletableFuture
whenComplete (CompletableFuture
f, BiConsumer super T,? super Throwable> a) { return f.whenCompleteAsync(a); } public
CompletableFuture
runAfterBoth (CompletableFuture
f, CompletableFuture
g, Runnable a) { return f.runAfterBothAsync(g, a); } public
CompletableFuture
thenAcceptBoth (CompletableFuture
f, CompletionStage extends U> g, BiConsumer super T,? super U> a) { return f.thenAcceptBothAsync(g, a); } public
CompletableFuture
thenCombine (CompletableFuture
f, CompletionStage extends U> g, BiFunction super T,? super U,? extends V> a) { return f.thenCombineAsync(g, a); } public
CompletableFuture
runAfterEither (CompletableFuture
f, CompletionStage> g, java.lang.Runnable a) { return f.runAfterEitherAsync(g, a); } public
CompletableFuture
acceptEither (CompletableFuture
f, CompletionStage extends T> g, Consumer super T> a) { return f.acceptEitherAsync(g, a); } public
CompletableFuture
applyToEither (CompletableFuture
f, CompletionStage extends T> g, Function super T,U> a) { return f.applyToEitherAsync(g, a); } }, EXECUTOR { public void checkExecutionMode() { assertTrue(ThreadExecutor.startedCurrentThread()); } public CompletableFuture
runAsync(Runnable a) { return CompletableFuture.runAsync(a, new ThreadExecutor()); } public
CompletableFuture
supplyAsync(Supplier
a) { return CompletableFuture.supplyAsync(a, new ThreadExecutor()); } public
CompletableFuture
thenRun (CompletableFuture
f, Runnable a) { return f.thenRunAsync(a, new ThreadExecutor()); } public
CompletableFuture
thenAccept (CompletableFuture
f, Consumer super T> a) { return f.thenAcceptAsync(a, new ThreadExecutor()); } public
CompletableFuture
thenApply (CompletableFuture
f, Function super T,U> a) { return f.thenApplyAsync(a, new ThreadExecutor()); } public
CompletableFuture
thenCompose (CompletableFuture
f, Function super T,? extends CompletionStage
> a) { return f.thenComposeAsync(a, new ThreadExecutor()); } public
CompletableFuture
handle (CompletableFuture
f, BiFunction super T,Throwable,? extends U> a) { return f.handleAsync(a, new ThreadExecutor()); } public
CompletableFuture
whenComplete (CompletableFuture
f, BiConsumer super T,? super Throwable> a) { return f.whenCompleteAsync(a, new ThreadExecutor()); } public
CompletableFuture
runAfterBoth (CompletableFuture
f, CompletableFuture
g, Runnable a) { return f.runAfterBothAsync(g, a, new ThreadExecutor()); } public
CompletableFuture
thenAcceptBoth (CompletableFuture
f, CompletionStage extends U> g, BiConsumer super T,? super U> a) { return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); } public
CompletableFuture
thenCombine (CompletableFuture
f, CompletionStage extends U> g, BiFunction super T,? super U,? extends V> a) { return f.thenCombineAsync(g, a, new ThreadExecutor()); } public
CompletableFuture
runAfterEither (CompletableFuture
f, CompletionStage> g, java.lang.Runnable a) { return f.runAfterEitherAsync(g, a, new ThreadExecutor()); } public
CompletableFuture
acceptEither (CompletableFuture
f, CompletionStage extends T> g, Consumer super T> a) { return f.acceptEitherAsync(g, a, new ThreadExecutor()); } public
CompletableFuture
applyToEither (CompletableFuture
f, CompletionStage extends T> g, Function super T,U> a) { return f.applyToEitherAsync(g, a, new ThreadExecutor()); } }; public abstract void checkExecutionMode(); public abstract CompletableFuture
runAsync(Runnable a); public abstract
CompletableFuture
supplyAsync(Supplier
a); public abstract
CompletableFuture
thenRun (CompletableFuture
f, Runnable a); public abstract
CompletableFuture
thenAccept (CompletableFuture
f, Consumer super T> a); public abstract
CompletableFuture
thenApply (CompletableFuture
f, Function super T,U> a); public abstract
CompletableFuture
thenCompose (CompletableFuture
f, Function super T,? extends CompletionStage
> a); public abstract
CompletableFuture
handle (CompletableFuture
f, BiFunction super T,Throwable,? extends U> a); public abstract
CompletableFuture
whenComplete (CompletableFuture
f, BiConsumer super T,? super Throwable> a); public abstract
CompletableFuture
runAfterBoth (CompletableFuture
f, CompletableFuture
g, Runnable a); public abstract
CompletableFuture
thenAcceptBoth (CompletableFuture
f, CompletionStage extends U> g, BiConsumer super T,? super U> a); public abstract
CompletableFuture
thenCombine (CompletableFuture
f, CompletionStage extends U> g, BiFunction super T,? super U,? extends V> a); public abstract
CompletableFuture
runAfterEither (CompletableFuture
f, CompletionStage> g, java.lang.Runnable a); public abstract
CompletableFuture
acceptEither (CompletableFuture
f, CompletionStage extends T> g, Consumer super T> a); public abstract
CompletableFuture
applyToEither (CompletableFuture
f, CompletionStage extends T> g, Function super T,U> a); } /** * exceptionally action is not invoked when source completes * normally, and source result is propagated */ public void testExceptionally_normalCompletion() { for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final AtomicInteger a = new AtomicInteger(0); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) assertTrue(f.complete(v1)); final CompletableFuture
g = f.exceptionally ((Throwable t) -> { a.getAndIncrement(); threadFail("should not be called"); return null; // unreached }); if (createIncomplete) assertTrue(f.complete(v1)); checkCompletedNormally(g, v1); checkCompletedNormally(f, v1); assertEquals(0, a.get()); }} /** * exceptionally action completes with function value on source * exception */ public void testExceptionally_exceptionalCompletion() { for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final AtomicInteger a = new AtomicInteger(0); final CFException ex = new CFException(); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture
g = f.exceptionally ((Throwable t) -> { ExecutionMode.SYNC.checkExecutionMode(); threadAssertSame(t, ex); a.getAndIncrement(); return v1; }); if (createIncomplete) f.completeExceptionally(ex); checkCompletedNormally(g, v1); assertEquals(1, a.get()); }} /** * If an "exceptionally action" throws an exception, it completes * exceptionally with that exception */ public void testExceptionally_exceptionalCompletionActionFailed() { for (boolean createIncomplete : new boolean[] { true, false }) { final AtomicInteger a = new AtomicInteger(0); final CFException ex1 = new CFException(); final CFException ex2 = new CFException(); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex1); final CompletableFuture
g = f.exceptionally ((Throwable t) -> { ExecutionMode.SYNC.checkExecutionMode(); threadAssertSame(t, ex1); a.getAndIncrement(); throw ex2; }); if (createIncomplete) f.completeExceptionally(ex1); checkCompletedWithWrappedException(g, ex2); checkCompletedExceptionally(f, ex1); assertEquals(1, a.get()); }} /** * whenComplete action executes on normal completion, propagating * source result. */ public void testWhenComplete_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final AtomicInteger a = new AtomicInteger(0); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) assertTrue(f.complete(v1)); final CompletableFuture
g = m.whenComplete (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertSame(result, v1); threadAssertNull(t); a.getAndIncrement(); }); if (createIncomplete) assertTrue(f.complete(v1)); checkCompletedNormally(g, v1); checkCompletedNormally(f, v1); assertEquals(1, a.get()); }} /** * whenComplete action executes on exceptional completion, propagating * source result. */ public void testWhenComplete_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) { final AtomicInteger a = new AtomicInteger(0); final CFException ex = new CFException(); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture
g = m.whenComplete (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertNull(result); threadAssertSame(t, ex); a.getAndIncrement(); }); if (createIncomplete) f.completeExceptionally(ex); checkCompletedWithWrappedException(g, ex); checkCompletedExceptionally(f, ex); assertEquals(1, a.get()); }} /** * whenComplete action executes on cancelled source, propagating * CancellationException. */ public void testWhenComplete_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) for (boolean createIncomplete : new boolean[] { true, false }) { final AtomicInteger a = new AtomicInteger(0); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); final CompletableFuture
g = m.whenComplete (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertNull(result); threadAssertTrue(t instanceof CancellationException); a.getAndIncrement(); }); if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); checkCompletedWithWrappedCancellationException(g); checkCancelled(f); assertEquals(1, a.get()); }} /** * If a whenComplete action throws an exception when triggered by * a normal completion, it completes exceptionally */ public void testWhenComplete_sourceCompletedNormallyActionFailed() { for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { final AtomicInteger a = new AtomicInteger(0); final CFException ex = new CFException(); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) assertTrue(f.complete(v1)); final CompletableFuture
g = m.whenComplete (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertSame(result, v1); threadAssertNull(t); a.getAndIncrement(); throw ex; }); if (createIncomplete) assertTrue(f.complete(v1)); checkCompletedWithWrappedException(g, ex); checkCompletedNormally(f, v1); assertEquals(1, a.get()); }} /** * If a whenComplete action throws an exception when triggered by * a source completion that also throws an exception, the source * exception takes precedence (unlike handle) */ public void testWhenComplete_sourceFailedActionFailed() { for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) { final AtomicInteger a = new AtomicInteger(0); final CFException ex1 = new CFException(); final CFException ex2 = new CFException(); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex1); final CompletableFuture
g = m.whenComplete (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertSame(t, ex1); threadAssertNull(result); a.getAndIncrement(); throw ex2; }); if (createIncomplete) f.completeExceptionally(ex1); checkCompletedWithWrappedException(g, ex1); checkCompletedExceptionally(f, ex1); if (testImplementationDetails) { assertEquals(1, ex1.getSuppressed().length); assertSame(ex2, ex1.getSuppressed()[0]); } assertEquals(1, a.get()); }} /** * handle action completes normally with function value on normal * completion of source */ public void testHandle_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture
f = new CompletableFuture<>(); final AtomicInteger a = new AtomicInteger(0); if (!createIncomplete) assertTrue(f.complete(v1)); final CompletableFuture
g = m.handle (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertSame(result, v1); threadAssertNull(t); a.getAndIncrement(); return inc(v1); }); if (createIncomplete) assertTrue(f.complete(v1)); checkCompletedNormally(g, inc(v1)); checkCompletedNormally(f, v1); assertEquals(1, a.get()); }} /** * handle action completes normally with function value on * exceptional completion of source */ public void testHandle_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture
f = new CompletableFuture<>(); final AtomicInteger a = new AtomicInteger(0); final CFException ex = new CFException(); if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture
g = m.handle (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertNull(result); threadAssertSame(t, ex); a.getAndIncrement(); return v1; }); if (createIncomplete) f.completeExceptionally(ex); checkCompletedNormally(g, v1); checkCompletedExceptionally(f, ex); assertEquals(1, a.get()); }} /** * handle action completes normally with function value on * cancelled source */ public void testHandle_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture
f = new CompletableFuture<>(); final AtomicInteger a = new AtomicInteger(0); if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); final CompletableFuture
g = m.handle (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertNull(result); threadAssertTrue(t instanceof CancellationException); a.getAndIncrement(); return v1; }); if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); checkCompletedNormally(g, v1); checkCancelled(f); assertEquals(1, a.get()); }} /** * If a "handle action" throws an exception when triggered by * a normal completion, it completes exceptionally */ public void testHandle_sourceCompletedNormallyActionFailed() { for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture
f = new CompletableFuture<>(); final AtomicInteger a = new AtomicInteger(0); final CFException ex = new CFException(); if (!createIncomplete) assertTrue(f.complete(v1)); final CompletableFuture
g = m.handle (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertSame(result, v1); threadAssertNull(t); a.getAndIncrement(); throw ex; }); if (createIncomplete) assertTrue(f.complete(v1)); checkCompletedWithWrappedException(g, ex); checkCompletedNormally(f, v1); assertEquals(1, a.get()); }} /** * If a "handle action" throws an exception when triggered by * a source completion that also throws an exception, the action * exception takes precedence (unlike whenComplete) */ public void testHandle_sourceFailedActionFailed() { for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) { final AtomicInteger a = new AtomicInteger(0); final CFException ex1 = new CFException(); final CFException ex2 = new CFException(); final CompletableFuture
f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex1); final CompletableFuture
g = m.handle (f, (Integer result, Throwable t) -> { m.checkExecutionMode(); threadAssertNull(result); threadAssertSame(ex1, t); a.getAndIncrement(); throw ex2; }); if (createIncomplete) f.completeExceptionally(ex1); checkCompletedWithWrappedException(g, ex2); checkCompletedExceptionally(f, ex1); assertEquals(1, a.get()); }} /** * runAsync completes after running Runnable */ public void testRunAsync_normalCompletion() { ExecutionMode[] executionModes = { ExecutionMode.ASYNC, ExecutionMode.EXECUTOR, }; for (ExecutionMode m : executionModes) { final Noop r = new Noop(m); final CompletableFuture
f = m.runAsync(r); assertNull(f.join()); checkCompletedNormally(f, null); r.assertInvoked(); }} /** * failing runAsync completes exceptionally after running Runnable */ public void testRunAsync_exceptionalCompletion() { ExecutionMode[] executionModes = { ExecutionMode.ASYNC, ExecutionMode.EXECUTOR, }; for (ExecutionMode m : executionModes) { final FailingRunnable r = new FailingRunnable(m); final CompletableFuture
f = m.runAsync(r); checkCompletedWithWrappedCFException(f); r.assertInvoked(); }} /** * supplyAsync completes with result of supplier */ public void testSupplyAsync_normalCompletion() { ExecutionMode[] executionModes = { ExecutionMode.ASYNC, ExecutionMode.EXECUTOR, }; for (ExecutionMode m : executionModes) for (Integer v1 : new Integer[] { 1, null }) { final IntegerSupplier r = new IntegerSupplier(m, v1); final CompletableFuture
f = m.supplyAsync(r); assertSame(v1, f.join()); checkCompletedNormally(f, v1); r.assertInvoked(); }} /** * Failing supplyAsync completes exceptionally */ public void testSupplyAsync_exceptionalCompletion() { ExecutionMode[] executionModes = { ExecutionMode.ASYNC, ExecutionMode.EXECUTOR, }; for (ExecutionMode m : executionModes) { FailingSupplier r = new FailingSupplier(m); CompletableFuture
f = m.supplyAsync(r); checkCompletedWithWrappedCFException(f); r.assertInvoked(); }} // seq completion methods /** * thenRun result completes normally after normal completion of source */ public void testThenRun_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture
f = new CompletableFuture<>(); final Noop[] rs = new Noop[6]; for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); final CompletableFuture
h0 = m.thenRun(f, rs[0]); final CompletableFuture
h1 = m.runAfterBoth(f, f, rs[1]); final CompletableFuture
h2 = m.runAfterEither(f, f, rs[2]); checkIncomplete(h0); checkIncomplete(h1); checkIncomplete(h2); assertTrue(f.complete(v1)); final CompletableFuture
h3 = m.thenRun(f, rs[3]); final CompletableFuture
h4 = m.runAfterBoth(f, f, rs[4]); final CompletableFuture
h5 = m.runAfterEither(f, f, rs[5]); checkCompletedNormally(h0, null); checkCompletedNormally(h1, null); checkCompletedNormally(h2, null); checkCompletedNormally(h3, null); checkCompletedNormally(h4, null); checkCompletedNormally(h5, null); checkCompletedNormally(f, v1); for (Noop r : rs) r.assertInvoked(); }} /** * thenRun result completes exceptionally after exceptional * completion of source */ public void testThenRun_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) { final CFException ex = new CFException(); final CompletableFuture
f = new CompletableFuture<>(); final Noop[] rs = new Noop[6]; for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); final CompletableFuture
h0 = m.thenRun(f, rs[0]); final CompletableFuture
h1 = m.runAfterBoth(f, f, rs[1]); final CompletableFuture
h2 = m.runAfterEither(f, f, rs[2]); checkIncomplete(h0); checkIncomplete(h1); checkIncomplete(h2); assertTrue(f.completeExceptionally(ex)); final CompletableFuture
h3 = m.thenRun(f, rs[3]); final CompletableFuture
h4 = m.runAfterBoth(f, f, rs[4]); final CompletableFuture
h5 = m.runAfterEither(f, f, rs[5]); checkCompletedWithWrappedException(h0, ex); checkCompletedWithWrappedException(h1, ex); checkCompletedWithWrappedException(h2, ex); checkCompletedWithWrappedException(h3, ex); checkCompletedWithWrappedException(h4, ex); checkCompletedWithWrappedException(h5, ex); checkCompletedExceptionally(f, ex); for (Noop r : rs) r.assertNotInvoked(); }} /** * thenRun result completes exceptionally if source cancelled */ public void testThenRun_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { final CompletableFuture
f = new CompletableFuture<>(); final Noop[] rs = new Noop[6]; for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); final CompletableFuture
h0 = m.thenRun(f, rs[0]); final CompletableFuture
h1 = m.runAfterBoth(f, f, rs[1]); final CompletableFuture
h2 = m.runAfterEither(f, f, rs[2]); checkIncomplete(h0); checkIncomplete(h1); checkIncomplete(h2); assertTrue(f.cancel(mayInterruptIfRunning)); final CompletableFuture
h3 = m.thenRun(f, rs[3]); final CompletableFuture
h4 = m.runAfterBoth(f, f, rs[4]); final CompletableFuture
h5 = m.runAfterEither(f, f, rs[5]); checkCompletedWithWrappedCancellationException(h0); checkCompletedWithWrappedCancellationException(h1); checkCompletedWithWrappedCancellationException(h2); checkCompletedWithWrappedCancellationException(h3); checkCompletedWithWrappedCancellationException(h4); checkCompletedWithWrappedCancellationException(h5); checkCancelled(f); for (Noop r : rs) r.assertNotInvoked(); }} /** * thenRun result completes exceptionally if action does */ public void testThenRun_actionFailed() { for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture