--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 18:21:34 1.47 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 21:41:37 1.55 @@ -17,6 +17,8 @@ import java.util.concurrent.Future; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.CompletionStage; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinTask; import java.util.concurrent.TimeoutException; import java.util.concurrent.atomic.AtomicInteger; import static java.util.concurrent.TimeUnit.MILLISECONDS; @@ -282,10 +284,10 @@ public class CompletableFutureTest exten public void testGetNumberOfDependents() { CompletableFuture f = new CompletableFuture<>(); assertEquals(0, f.getNumberOfDependents()); - CompletableFuture g = f.thenRun(new Noop()); + CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT)); assertEquals(1, f.getNumberOfDependents()); assertEquals(0, g.getNumberOfDependents()); - CompletableFuture h = f.thenRun(new Noop()); + CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT)); assertEquals(2, f.getNumberOfDependents()); f.complete(1); checkCompletedNormally(g, null); @@ -373,8 +375,11 @@ public class CompletableFutureTest exten } } static final class Noop implements Runnable { + final ExecutionMode m; int invocationCount = 0; + Noop(ExecutionMode m) { this.m = m; } public void run() { + m.checkExecutionMode(); invocationCount++; } } @@ -414,7 +419,7 @@ public class CompletableFutureTest exten throw new CFException(); } } - static final class FailingNoop implements Runnable { + static final class FailingRunnable implements Runnable { int invocationCount = 0; public void run() { invocationCount++; @@ -458,6 +463,9 @@ public class CompletableFutureTest exten */ enum ExecutionMode { DEFAULT { + public void checkExecutionMode() { + assertNull(ForkJoinTask.getPool()); + } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { return f.thenRun(a); @@ -521,7 +529,11 @@ public class CompletableFutureTest exten } }, - DEFAULT_ASYNC { + ASYNC { + public void checkExecutionMode() { + assertSame(ForkJoinPool.commonPool(), + ForkJoinTask.getPool()); + } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { return f.thenRunAsync(a); @@ -586,6 +598,9 @@ public class CompletableFutureTest exten }, EXECUTOR { + public void checkExecutionMode() { + //TODO + } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { return f.thenRunAsync(a, new ThreadExecutor()); @@ -649,6 +664,7 @@ public class CompletableFutureTest exten } }; + public abstract void checkExecutionMode(); public abstract CompletableFuture thenRun (CompletableFuture f, Runnable a); public abstract CompletableFuture thenAccept @@ -896,7 +912,7 @@ public class CompletableFutureTest exten * runAsync completes after running Runnable */ public void testRunAsync() { - Noop r = new Noop(); + Noop r = new Noop(ExecutionMode.ASYNC); CompletableFuture f = CompletableFuture.runAsync(r); assertNull(f.join()); assertEquals(1, r.invocationCount); @@ -907,7 +923,7 @@ public class CompletableFutureTest exten * runAsync with executor completes after running Runnable */ public void testRunAsync2() { - Noop r = new Noop(); + Noop r = new Noop(ExecutionMode.EXECUTOR); ThreadExecutor exec = new ThreadExecutor(); CompletableFuture f = CompletableFuture.runAsync(r, exec); assertNull(f.join()); @@ -920,7 +936,7 @@ public class CompletableFutureTest exten * failing runAsync completes exceptionally after running Runnable */ public void testRunAsync3() { - FailingNoop r = new FailingNoop(); + FailingRunnable r = new FailingRunnable(); CompletableFuture f = CompletableFuture.runAsync(r); checkCompletedWithWrappedCFException(f); assertEquals(1, r.invocationCount); @@ -961,478 +977,393 @@ public class CompletableFutureTest exten /** * thenRun result completes normally after normal completion of source */ - public void testThenRun() { - CompletableFuture f; - CompletableFuture g; - Noop r; - - f = new CompletableFuture<>(); - g = f.thenRun(r = new Noop()); - f.complete(null); - checkCompletedNormally(g, null); - assertEquals(1, r.invocationCount); + public void testThenRun_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 Noop r = new Noop(m); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = m.thenRun(f, r); + if (createIncomplete) { + checkIncomplete(g); + f.complete(v1); + } - f = new CompletableFuture<>(); - f.complete(null); - g = f.thenRun(r = new Noop()); checkCompletedNormally(g, null); + checkCompletedNormally(f, v1); assertEquals(1, r.invocationCount); - } + }} /** * thenRun result completes exceptionally after exceptional * completion of source */ - public void testThenRun2() { - CompletableFuture f; - CompletableFuture g; - Noop r; - - f = new CompletableFuture<>(); - g = f.thenRun(r = new Noop()); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - assertEquals(0, r.invocationCount); + public void testThenRun_exceptionalCompletion() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + { + final CFException ex = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + final Noop r = new Noop(m); + if (!createIncomplete) f.completeExceptionally(ex); + final CompletableFuture g = m.thenRun(f, r); + if (createIncomplete) { + checkIncomplete(g); + f.completeExceptionally(ex); + } - f = new CompletableFuture<>(); - f.completeExceptionally(new CFException()); - g = f.thenRun(r = new Noop()); - checkCompletedWithWrappedCFException(g); + checkCompletedWithWrappedCFException(g, ex); + checkCompletedWithWrappedCFException(f, ex); assertEquals(0, r.invocationCount); - } - - /** - * thenRun result completes exceptionally if action does - */ - public void testThenRun3() { - CompletableFuture f; - CompletableFuture g; - FailingNoop r; - - f = new CompletableFuture<>(); - g = f.thenRun(r = new FailingNoop()); - f.complete(null); - checkCompletedWithWrappedCFException(g); - - f = new CompletableFuture<>(); - f.complete(null); - g = f.thenRun(r = new FailingNoop()); - checkCompletedWithWrappedCFException(g); - } + }} /** * thenRun result completes exceptionally if source cancelled */ - public void testThenRun4() { - CompletableFuture f; - CompletableFuture g; - Noop r; - - f = new CompletableFuture<>(); - g = f.thenRun(r = new Noop()); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - - f = new CompletableFuture<>(); - assertTrue(f.cancel(true)); - g = f.thenRun(r = new Noop()); - checkCompletedWithWrappedCancellationException(g); - } - - /** - * thenApply result completes normally after normal completion of source - */ - public void testThenApply() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApply(inc); - f.complete(one); - checkCompletedNormally(g, two); - } - - /** - * thenApply result completes exceptionally after exceptional - * completion of source - */ - public void testThenApply2() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApply(inc); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenApply result completes exceptionally if action does - */ - public void testThenApply3() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApply(new FailingFunction()); - f.complete(one); - checkCompletedWithWrappedCFException(g); - } + public void testThenRun_sourceCancelled() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + { + final CompletableFuture f = new CompletableFuture<>(); + final Noop r = new Noop(m); + if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); + final CompletableFuture g = f.thenRun(r); + if (createIncomplete) { + checkIncomplete(g); + assertTrue(f.cancel(mayInterruptIfRunning)); + } - /** - * thenApply result completes exceptionally if source cancelled - */ - public void testThenApply4() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApply(inc); - assertTrue(f.cancel(true)); checkCompletedWithWrappedCancellationException(g); - } - - /** - * thenAccept result completes normally after normal completion of source - */ - public void testThenAccept() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAccept(r); - f.complete(one); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); - } + checkCancelled(f); + assertEquals(0, r.invocationCount); + }} /** - * thenAccept result completes exceptionally after exceptional - * completion of source + * thenRun result completes exceptionally if action does */ - public void testThenAccept2() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAccept(r); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - } + public void testThenRun_actionFailed() { + 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 FailingRunnable r = new FailingRunnable(); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = f.thenRun(r); + if (createIncomplete) { + checkIncomplete(g); + f.complete(v1); + } - /** - * thenAccept result completes exceptionally if action does - */ - public void testThenAccept3() { - CompletableFuture f = new CompletableFuture<>(); - FailingConsumer r = new FailingConsumer(); - CompletableFuture g = f.thenAccept(r); - f.complete(one); checkCompletedWithWrappedCFException(g); - assertEquals(1, r.invocationCount); - } - - /** - * thenAccept result completes exceptionally if source cancelled - */ - public void testThenAccept4() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAccept(r); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - } + checkCompletedNormally(f, v1); + }} /** - * thenCombine result completes normally after normal completion - * of sources + * thenApply result completes normally after normal completion of source */ - public void testThenCombine_normalCompletion1() { - for (boolean createIncomplete : new boolean[] { true, false }) - for (boolean fFirst : new boolean[] { true, false }) + public void testThenApply_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - CompletableFuture h = null; - if (createIncomplete) h = m.thenCombine(f, g, r); - - if (fFirst) - f.complete(v1); - else - g.complete(v2); - if (createIncomplete) checkIncomplete(h); - assertEquals(0, r.invocationCount); - if (!fFirst) + final IncFunction r = new IncFunction(); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = m.thenApply(f, r); + if (createIncomplete) { + checkIncomplete(g); f.complete(v1); - else - g.complete(v2); - if (!createIncomplete) h = m.thenCombine(f, g, r); + } - checkCompletedNormally(h, subtract(v1, v2)); + checkCompletedNormally(g, inc(v1)); checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); assertEquals(1, r.invocationCount); }} /** - * thenCombine result completes exceptionally after exceptional - * completion of either source + * thenApply result completes exceptionally after exceptional + * completion of source */ - public void testThenCombine_exceptionalCompletion1() { + public void testThenApply_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) + for (boolean createIncomplete : new boolean[] { true, false }) { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - final CompletableFuture h = m.thenCombine(f, g, r); final CFException ex = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + final IncFunction r = new IncFunction(); + if (!createIncomplete) f.completeExceptionally(ex); + final CompletableFuture g = m.thenApply(f, r); + if (createIncomplete) { + checkIncomplete(g); + f.completeExceptionally(ex); + } - f.completeExceptionally(ex); - checkIncomplete(h); - g.complete(v1); - - checkCompletedWithWrappedCFException(h, ex); + checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); }} - public void testThenCombine_exceptionalCompletion2() { + /** + * thenApply result completes exceptionally if source cancelled + */ + public void testThenApply_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - final CompletableFuture h = m.thenCombine(f, g, r); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - checkIncomplete(h); - f.complete(v1); + final IncFunction r = new IncFunction(); + if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); + final CompletableFuture g = f.thenApply(r); + if (createIncomplete) { + checkIncomplete(g); + assertTrue(f.cancel(mayInterruptIfRunning)); + } - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(g, ex); + checkCompletedWithWrappedCancellationException(g); + checkCancelled(f); assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); }} - public void testThenCombine_exceptionalCompletion3() { + /** + * thenApply result completes exceptionally if action does + */ + public void testThenApply_actionFailed() { 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 CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.thenCombine(f, g, r); + final FailingFunction r = new FailingFunction(); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = f.thenApply(r); + if (createIncomplete) { + checkIncomplete(g); + f.complete(v1); + } - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(g, ex); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(g); checkCompletedNormally(f, v1); }} - public void testThenCombine_exceptionalCompletion4() { + /** + * thenAccept result completes normally after normal completion of source + */ + public void testThenAccept_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 CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - final CFException ex = new CFException(); - - f.completeExceptionally(ex); - g.complete(v1); - final CompletableFuture h = m.thenCombine(f, g, r); + final IncAction r = new IncAction(); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = m.thenAccept(f, r); + if (createIncomplete) { + checkIncomplete(g); + f.complete(v1); + } - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); + checkCompletedNormally(g, null); + checkCompletedNormally(f, v1); + assertEquals(1, r.invocationCount); + assertEquals(inc(v1), r.value); }} /** - * thenCombine result completes exceptionally if action does + * thenAccept result completes exceptionally after exceptional + * completion of source */ - public void testThenCombine_actionFailed1() { + public void testThenAccept_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) + for (boolean createIncomplete : new boolean[] { true, false }) { + final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final FailingBiFunction r = new FailingBiFunction(); - final CompletableFuture h = m.thenCombine(f, g, r); - - f.complete(v1); - checkIncomplete(h); - g.complete(v2); + final IncAction r = new IncAction(); + if (!createIncomplete) f.completeExceptionally(ex); + final CompletableFuture g = m.thenAccept(f, r); + if (createIncomplete) { + checkIncomplete(g); + f.completeExceptionally(ex); + } - checkCompletedWithWrappedCFException(h); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); + checkCompletedWithWrappedCFException(g, ex); + checkCompletedWithWrappedCFException(f, ex); + assertEquals(0, r.invocationCount); }} - public void testThenCombine_actionFailed2() { + /** + * thenAccept result completes exceptionally if action does + */ + public void testThenAccept_actionFailed() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final FailingBiFunction r = new FailingBiFunction(); - final CompletableFuture h = m.thenCombine(f, g, r); - - g.complete(v2); - checkIncomplete(h); - f.complete(v1); + final FailingConsumer r = new FailingConsumer(); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = f.thenAccept(r); + if (createIncomplete) { + checkIncomplete(g); + f.complete(v1); + } - checkCompletedWithWrappedCFException(h); + checkCompletedWithWrappedCFException(g); checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); }} /** - * thenCombine result completes exceptionally if either source cancelled + * thenAccept result completes exceptionally if source cancelled */ - public void testThenCombine_sourceCancelled1() { + public void testThenAccept_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - final CompletableFuture h = m.thenCombine(f, g, r); - - assertTrue(f.cancel(mayInterruptIfRunning)); - checkIncomplete(h); - g.complete(v1); + final IncAction r = new IncAction(); + if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); + final CompletableFuture g = f.thenAccept(r); + if (createIncomplete) { + checkIncomplete(g); + assertTrue(f.cancel(mayInterruptIfRunning)); + } - checkCompletedWithWrappedCancellationException(h); + checkCompletedWithWrappedCancellationException(g); checkCancelled(f); assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); }} - public void testThenCombine_sourceCancelled2() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); - final CompletableFuture h = m.thenCombine(f, g, r); - - assertTrue(g.cancel(mayInterruptIfRunning)); - checkIncomplete(h); - f.complete(v1); - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(g); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testThenCombine_sourceCancelled3() { + /** + * thenCombine result completes normally after normal completion + * of sources + */ + public void testThenCombine_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) + for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final SubtractFunction r = new SubtractFunction(); - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); + if (fFirst) f.complete(v1); else g.complete(v2); + if (!createIncomplete) + if (!fFirst) f.complete(v1); else g.complete(v2); final CompletableFuture h = m.thenCombine(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + assertEquals(0, r.invocationCount); + if (!fFirst) f.complete(v1); else g.complete(v2); + } - checkCompletedWithWrappedCancellationException(h); - checkCancelled(g); - assertEquals(0, r.invocationCount); + checkCompletedNormally(h, subtract(v1, v2)); checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + assertEquals(1, r.invocationCount); }} - public void testThenCombine_sourceCancelled4() { + /** + * thenCombine result completes exceptionally after exceptional + * completion of either source + */ + public void testThenCombine_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); + final CFException ex = new CFException(); final SubtractFunction r = new SubtractFunction(); - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); + (fFirst ? f : g).complete(v1); + if (!createIncomplete) + (!fFirst ? f : g).completeExceptionally(ex); final CompletableFuture h = m.thenCombine(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + (!fFirst ? f : g).completeExceptionally(ex); + } - checkCompletedWithWrappedCancellationException(h); - checkCancelled(f); + checkCompletedWithWrappedCFException(h, ex); assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); + checkCompletedNormally(fFirst ? f : g, v1); + checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); }} /** - * thenAcceptBoth result completes normally after normal - * completion of sources + * thenCombine result completes exceptionally if action does */ - public void testThenAcceptBoth_normalCompletion1() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - - f.complete(v1); - checkIncomplete(h); - assertEquals(0, r.invocationCount); - g.complete(v2); - - checkCompletedNormally(h, null); - assertEquals(subtract(v1, v2), r.value); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testThenAcceptBoth_normalCompletion2() { + public void testThenCombine_actionFailed() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); + final FailingBiFunction r = new FailingBiFunction(); + final CompletableFuture h = m.thenCombine(f, g, r); - g.complete(v2); - checkIncomplete(h); - assertEquals(0, r.invocationCount); - f.complete(v1); + if (fFirst) { + f.complete(v1); + g.complete(v2); + } else { + g.complete(v2); + f.complete(v1); + } - checkCompletedNormally(h, null); - assertEquals(subtract(v1, v2), r.value); + checkCompletedWithWrappedCFException(h); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); }} - public void testThenAcceptBoth_normalCompletion3() { + /** + * thenCombine result completes exceptionally if either source cancelled + */ + public void testThenCombine_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); + final SubtractFunction r = new SubtractFunction(); - g.complete(v2); - f.complete(v1); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); + (fFirst ? f : g).complete(v1); + if (!createIncomplete) + assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); + final CompletableFuture h = m.thenCombine(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); + } - checkCompletedNormally(h, null); - assertEquals(subtract(v1, v2), r.value); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); + checkCompletedWithWrappedCancellationException(h); + checkCancelled(!fFirst ? f : g); + assertEquals(0, r.invocationCount); + checkCompletedNormally(fFirst ? f : g, v1); }} - public void testThenAcceptBoth_normalCompletion4() { + /** + * thenAcceptBoth result completes normally after normal + * completion of sources + */ + public void testThenAcceptBoth_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) { @@ -1440,9 +1371,15 @@ public class CompletableFutureTest exten final CompletableFuture g = new CompletableFuture<>(); final SubtractAction r = new SubtractAction(); - f.complete(v1); - g.complete(v2); + if (fFirst) f.complete(v1); else g.complete(v2); + if (!createIncomplete) + if (!fFirst) f.complete(v1); else g.complete(v2); final CompletableFuture h = m.thenAcceptBoth(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + assertEquals(0, r.invocationCount); + if (!fFirst) f.complete(v1); else g.complete(v2); + } checkCompletedNormally(h, null); assertEquals(subtract(v1, v2), r.value); @@ -1454,108 +1391,38 @@ public class CompletableFutureTest exten * thenAcceptBoth result completes exceptionally after exceptional * completion of either source */ - public void testThenAcceptBoth_exceptionalCompletion1() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - final CFException ex = new CFException(); - - f.completeExceptionally(ex); - checkIncomplete(h); - g.complete(v1); - - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - }} - - public void testThenAcceptBoth_exceptionalCompletion2() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - checkIncomplete(h); - f.complete(v1); - - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(g, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testThenAcceptBoth_exceptionalCompletion3() { + public void testThenAcceptBoth_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); final CFException ex = new CFException(); - - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(g, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testThenAcceptBoth_exceptionalCompletion4() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); final SubtractAction r = new SubtractAction(); - final CFException ex = new CFException(); - f.completeExceptionally(ex); - g.complete(v1); + (fFirst ? f : g).complete(v1); + if (!createIncomplete) + (!fFirst ? f : g).completeExceptionally(ex); final CompletableFuture h = m.thenAcceptBoth(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + (!fFirst ? f : g).completeExceptionally(ex); + } checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(f, ex); assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); + checkCompletedNormally(fFirst ? f : g, v1); + checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); }} /** * thenAcceptBoth result completes exceptionally if action does */ - public void testThenAcceptBoth_actionFailed1() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final FailingBiConsumer r = new FailingBiConsumer(); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - - f.complete(v1); - checkIncomplete(h); - g.complete(v2); - - checkCompletedWithWrappedCFException(h); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testThenAcceptBoth_actionFailed2() { + public void testThenAcceptBoth_actionFailed() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) { @@ -1564,9 +1431,13 @@ public class CompletableFutureTest exten final FailingBiConsumer r = new FailingBiConsumer(); final CompletableFuture h = m.thenAcceptBoth(f, g, r); - g.complete(v2); - checkIncomplete(h); - f.complete(v1); + if (fFirst) { + f.complete(v1); + g.complete(v2); + } else { + g.complete(v2); + f.complete(v1); + } checkCompletedWithWrappedCFException(h); checkCompletedNormally(f, v1); @@ -1576,161 +1447,56 @@ public class CompletableFutureTest exten /** * thenAcceptBoth result completes exceptionally if either source cancelled */ - public void testThenAcceptBoth_sourceCancelled1() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - - assertTrue(f.cancel(mayInterruptIfRunning)); - checkIncomplete(h); - g.complete(v1); - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(f); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - }} - - public void testThenAcceptBoth_sourceCancelled2() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - - assertTrue(g.cancel(mayInterruptIfRunning)); - checkIncomplete(h); - f.complete(v1); - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(g); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testThenAcceptBoth_sourceCancelled3() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); - - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(g); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testThenAcceptBoth_sourceCancelled4() { + public void testThenAcceptBoth_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final SubtractAction r = new SubtractAction(); - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); + (fFirst ? f : g).complete(v1); + if (!createIncomplete) + assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); final CompletableFuture h = m.thenAcceptBoth(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); + } checkCompletedWithWrappedCancellationException(h); - checkCancelled(f); + checkCancelled(!fFirst ? f : g); assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); + checkCompletedNormally(fFirst ? f : g, v1); }} /** * runAfterBoth result completes normally after normal * completion of sources */ - public void testRunAfterBoth_normalCompletion1() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - final CompletableFuture h = m.runAfterBoth(f, g, r); - - f.complete(v1); - checkIncomplete(h); - assertEquals(0, r.invocationCount); - g.complete(v2); - - checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testRunAfterBoth_normalCompletion2() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - final CompletableFuture h = m.runAfterBoth(f, g, r); - - g.complete(v2); - checkIncomplete(h); - assertEquals(0, r.invocationCount); - f.complete(v1); - - checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testRunAfterBoth_normalCompletion3() { + public void testRunAfterBoth_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); - g.complete(v2); - f.complete(v1); - final CompletableFuture h = m.runAfterBoth(f, g, r); - - checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testRunAfterBoth_normalCompletion4() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - - f.complete(v1); - g.complete(v2); + if (fFirst) f.complete(v1); else g.complete(v2); + if (!createIncomplete) + if (!fFirst) f.complete(v1); else g.complete(v2); final CompletableFuture h = m.runAfterBoth(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + assertEquals(0, r.invocationCount); + if (!fFirst) f.complete(v1); else g.complete(v2); + } checkCompletedNormally(h, null); assertEquals(1, r.invocationCount); @@ -1742,121 +1508,57 @@ public class CompletableFutureTest exten * runAfterBoth result completes exceptionally after exceptional * completion of either source */ - public void testRunAfterBoth_exceptionalCompletion1() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - final CompletableFuture h = m.runAfterBoth(f, g, r); - final CFException ex = new CFException(); - - f.completeExceptionally(ex); - checkIncomplete(h); - g.complete(v1); - - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - }} - - public void testRunAfterBoth_exceptionalCompletion2() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - final CompletableFuture h = m.runAfterBoth(f, g, r); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - checkIncomplete(h); - f.complete(v1); - - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(g, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testRunAfterBoth_exceptionalCompletion3() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.runAfterBoth(f, g, r); - - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(g, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testRunAfterBoth_exceptionalCompletion4() { + public void testRunAfterBoth_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); final CFException ex = new CFException(); + final Noop r = new Noop(m); - f.completeExceptionally(ex); - g.complete(v1); + (fFirst ? f : g).complete(v1); + if (!createIncomplete) + (!fFirst ? f : g).completeExceptionally(ex); final CompletableFuture h = m.runAfterBoth(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + (!fFirst ? f : g).completeExceptionally(ex); + } checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(f, ex); assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); + checkCompletedNormally(fFirst ? f : g, v1); + checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); }} /** * runAfterBoth result completes exceptionally if action does */ - public void testRunAfterBoth_actionFailed1() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final FailingNoop r = new FailingNoop(); - final CompletableFuture h = m.runAfterBoth(f, g, r); - - f.complete(v1); - checkIncomplete(h); - g.complete(v2); - - checkCompletedWithWrappedCFException(h); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testRunAfterBoth_actionFailed2() { + public void testRunAfterBoth_actionFailed() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingNoop r = new FailingNoop(); - final CompletableFuture h = m.runAfterBoth(f, g, r); + final FailingRunnable r = new FailingRunnable(); - g.complete(v2); - checkIncomplete(h); - f.complete(v1); + CompletableFuture h1 = m.runAfterBoth(f, g, r); + if (fFirst) { + f.complete(v1); + g.complete(v2); + } else { + g.complete(v2); + f.complete(v1); + } + CompletableFuture h2 = m.runAfterBoth(f, g, r); - checkCompletedWithWrappedCFException(h); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); }} @@ -1864,137 +1566,82 @@ public class CompletableFutureTest exten /** * runAfterBoth result completes exceptionally if either source cancelled */ - public void testRunAfterBoth_sourceCancelled1() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - final CompletableFuture h = m.runAfterBoth(f, g, r); - - assertTrue(f.cancel(mayInterruptIfRunning)); - checkIncomplete(h); - g.complete(v1); - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(f); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - }} - - public void testRunAfterBoth_sourceCancelled2() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - final CompletableFuture h = m.runAfterBoth(f, g, r); - - assertTrue(g.cancel(mayInterruptIfRunning)); - checkIncomplete(h); - f.complete(v1); - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(g); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testRunAfterBoth_sourceCancelled3() { + public void testRunAfterBoth_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.runAfterBoth(f, g, r); - checkCompletedWithWrappedCancellationException(h); - checkCancelled(g); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - }} - - public void testRunAfterBoth_sourceCancelled4() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); - - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); + (fFirst ? f : g).complete(v1); + if (!createIncomplete) + assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); final CompletableFuture h = m.runAfterBoth(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); + } checkCompletedWithWrappedCancellationException(h); - checkCancelled(f); + checkCancelled(!fFirst ? f : g); assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); + checkCompletedNormally(fFirst ? f : g, v1); }} /** * applyToEither result completes normally after normal completion * of either source */ - public void testApplyToEither_normalCompletion1() { + public void testApplyToEither_normalCompletion() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final IncFunction r = new IncFunction(); - final CompletableFuture h = m.applyToEither(f, g, r); - f.complete(v1); - checkCompletedNormally(h, inc(v1)); - g.complete(v2); + if (!createIncomplete) + if (fFirst) f.complete(v1); else g.complete(v2); + final CompletableFuture h = m.applyToEither(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + assertEquals(0, r.invocationCount); + if (fFirst) f.complete(v1); else g.complete(v2); + } + checkCompletedNormally(h, inc(fFirst ? v1 : v2)); + if (!fFirst) f.complete(v1); else g.complete(v2); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - checkCompletedNormally(h, inc(v1)); + checkCompletedNormally(h, inc(fFirst ? v1 : v2)); }} - public void testApplyToEither_normalCompletion2() { + public void testApplyToEither_normalCompletionBothAvailable() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) for (Integer v2 : new Integer[] { 2, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final IncFunction r = new IncFunction(); - final CompletableFuture h = m.applyToEither(f, g, r); - g.complete(v2); - checkCompletedNormally(h, inc(v2)); - f.complete(v1); - - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - checkCompletedNormally(h, inc(v2)); - }} - - public void testApplyToEither_normalCompletion3() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - for (Integer v2 : new Integer[] { 2, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); + if (fFirst) { + f.complete(v1); + g.complete(v2); + } else { + g.complete(v2); + f.complete(v1); + } - f.complete(v1); - g.complete(v2); final CompletableFuture h = m.applyToEither(f, g, r); checkCompletedNormally(f, v1); @@ -2012,96 +1659,74 @@ public class CompletableFutureTest exten */ public void testApplyToEither_exceptionalCompletion1() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); - final CompletableFuture h = m.applyToEither(f, g, r); final CFException ex = new CFException(); - - f.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); - g.complete(v1); - - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - checkCompletedWithWrappedCFException(f, ex); - checkCompletedWithWrappedCFException(h, ex); - }} - - public void testApplyToEither_exceptionalCompletion2() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); final IncFunction r = new IncFunction(); + + if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex); final CompletableFuture h = m.applyToEither(f, g, r); - final CFException ex = new CFException(); + if (createIncomplete) { + checkIncomplete(h); + assertEquals(0, r.invocationCount); + (fFirst ? f : g).completeExceptionally(ex); + } - g.completeExceptionally(ex); checkCompletedWithWrappedCFException(h, ex); - f.complete(v1); + (!fFirst ? f : g).complete(v1); assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - checkCompletedWithWrappedCFException(g, ex); + checkCompletedNormally(!fFirst ? f : g, v1); + checkCompletedWithWrappedCFException(fFirst ? f : g, ex); checkCompletedWithWrappedCFException(h, ex); }} - public void testApplyToEither_exceptionalCompletion3() { + public void testApplyToEither_exceptionalCompletion2() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean reverseArgs : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); + final IncFunction r1 = new IncFunction(); + final IncFunction r2 = new IncFunction(); final CFException ex = new CFException(); - - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.applyToEither(f, g, r); + final CompletableFuture j = (reverseArgs ? g : f); + final CompletableFuture k = (reverseArgs ? f : g); + final CompletableFuture h1 = m.applyToEither(j, k, r1); + if (fFirst) { + f.complete(v1); + g.completeExceptionally(ex); + } else { + g.completeExceptionally(ex); + f.complete(v1); + } + final CompletableFuture h2 = m.applyToEither(j, k, r2); // unspecified behavior - Integer v; try { - assertEquals(inc(v1), h.join()); - assertEquals(1, r.invocationCount); + assertEquals(inc(v1), h1.join()); + assertEquals(1, r1.invocationCount); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(h1, ex); + assertEquals(0, r1.invocationCount); } - checkCompletedWithWrappedCFException(g, ex); - checkCompletedNormally(f, v1); - }} - - public void testApplyToEither_exceptionalCompletion4() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); - final CFException ex = new CFException(); - - f.completeExceptionally(ex); - g.complete(v1); - final CompletableFuture h = m.applyToEither(f, g, r); - - // unspecified behavior - Integer v; try { - assertEquals(inc(v1), h.join()); - assertEquals(1, r.invocationCount); + assertEquals(inc(v1), h2.join()); + assertEquals(1, r2.invocationCount); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(h2, ex); + assertEquals(0, r2.invocationCount); } - checkCompletedWithWrappedCFException(f, ex); - checkCompletedNormally(g, v1); + checkCompletedWithWrappedCFException(g, ex); + checkCompletedNormally(f, v1); }} /** @@ -2147,95 +1772,75 @@ public class CompletableFutureTest exten public void testApplyToEither_sourceCancelled1() { for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean createIncomplete : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final IncFunction r = new IncFunction(); - final CompletableFuture h = m.applyToEither(f, g, r); - - assertTrue(f.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); - g.complete(v1); - - checkCancelled(f); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - checkCompletedWithWrappedCancellationException(h); - }} - public void testApplyToEither_sourceCancelled2() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); + if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); final CompletableFuture h = m.applyToEither(f, g, r); + if (createIncomplete) { + checkIncomplete(h); + assertEquals(0, r.invocationCount); + assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); + } - assertTrue(g.cancel(mayInterruptIfRunning)); checkCompletedWithWrappedCancellationException(h); - f.complete(v1); + (!fFirst ? f : g).complete(v1); - checkCancelled(g); assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); + checkCompletedNormally(!fFirst ? f : g, v1); + checkCancelled(fFirst ? f : g); checkCompletedWithWrappedCancellationException(h); }} - public void testApplyToEither_sourceCancelled3() { + public void testApplyToEither_sourceCancelled2() { for (ExecutionMode m : ExecutionMode.values()) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean reverseArgs : new boolean[] { true, false }) + for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); + final IncFunction r1 = new IncFunction(); + final IncFunction r2 = new IncFunction(); + final CFException ex = new CFException(); + final CompletableFuture j = (reverseArgs ? g : f); + final CompletableFuture k = (reverseArgs ? f : g); - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.applyToEither(f, g, r); + final CompletableFuture h1 = m.applyToEither(j, k, r1); + if (fFirst) { + f.complete(v1); + assertTrue(g.cancel(mayInterruptIfRunning)); + } else { + assertTrue(g.cancel(mayInterruptIfRunning)); + f.complete(v1); + } + final CompletableFuture h2 = m.applyToEither(j, k, r2); // unspecified behavior - Integer v; try { - assertEquals(inc(v1), h.join()); - assertEquals(1, r.invocationCount); + assertEquals(inc(v1), h1.join()); + assertEquals(1, r1.invocationCount); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCancellationException(h1); + assertEquals(0, r1.invocationCount); } - checkCancelled(g); - checkCompletedNormally(f, v1); - }} - - public void testApplyToEither_sourceCancelled4() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); - - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); - final CompletableFuture h = m.applyToEither(f, g, r); - - // unspecified behavior - Integer v; try { - assertEquals(inc(v1), h.join()); - assertEquals(1, r.invocationCount); + assertEquals(inc(v1), h2.join()); + assertEquals(1, r2.invocationCount); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCancellationException(h2); + assertEquals(0, r2.invocationCount); } - checkCancelled(f); - checkCompletedNormally(g, v1); + checkCancelled(g); + checkCompletedNormally(f, v1); }} /** @@ -2551,7 +2156,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); final CompletableFuture h = m.runAfterEither(f, g, r); f.complete(v1); @@ -2572,7 +2177,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); final CompletableFuture h = m.runAfterEither(f, g, r); g.complete(v2); @@ -2593,7 +2198,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); f.complete(v1); g.complete(v2); @@ -2615,7 +2220,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); final CompletableFuture h = m.runAfterEither(f, g, r); final CFException ex = new CFException(); @@ -2635,7 +2240,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); final CompletableFuture h = m.runAfterEither(f, g, r); final CFException ex = new CFException(); @@ -2655,7 +2260,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); final CFException ex = new CFException(); g.completeExceptionally(ex); @@ -2682,7 +2287,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); final CFException ex = new CFException(); f.completeExceptionally(ex); @@ -2713,7 +2318,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingNoop r = new FailingNoop(); + final FailingRunnable r = new FailingRunnable(); final CompletableFuture h = m.runAfterEither(f, g, r); f.complete(v1); @@ -2730,7 +2335,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingNoop r = new FailingNoop(); + final FailingRunnable r = new FailingRunnable(); final CompletableFuture h = m.runAfterEither(f, g, r); g.complete(v2); @@ -2750,7 +2355,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); final CompletableFuture h = m.runAfterEither(f, g, r); assertTrue(f.cancel(mayInterruptIfRunning)); @@ -2770,7 +2375,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); final CompletableFuture h = m.runAfterEither(f, g, r); assertTrue(g.cancel(mayInterruptIfRunning)); @@ -2790,7 +2395,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); assertTrue(g.cancel(mayInterruptIfRunning)); f.complete(v1); @@ -2817,7 +2422,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); assertTrue(f.cancel(mayInterruptIfRunning)); g.complete(v1); @@ -2840,27 +2445,17 @@ public class CompletableFutureTest exten /** * thenCompose result completes normally after normal completion of source */ - public void testThenCompose_normalCompletion1() { + public void testThenCompose_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 CompletableFutureInc r = new CompletableFutureInc(); + if (!createIncomplete) f.complete(v1); final CompletableFuture g = f.thenCompose(r); - f.complete(v1); - checkCompletedNormally(g, inc(v1)); - checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); - }} + if (createIncomplete) f.complete(v1); - public void testThenCompose_normalCompletion2() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFutureInc r = new CompletableFutureInc(); - f.complete(v1); - final CompletableFuture g = f.thenCompose(r); checkCompletedNormally(g, inc(v1)); checkCompletedNormally(f, v1); assertEquals(1, r.invocationCount); @@ -2870,55 +2465,37 @@ public class CompletableFutureTest exten * thenCompose result completes exceptionally after exceptional * completion of source */ - public void testThenCompose_exceptionalCompletion1() { + public void testThenCompose_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) { final CFException ex = new CFException(); final CompletableFutureInc r = new CompletableFutureInc(); final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture g = f.thenCompose(r); - f.completeExceptionally(ex); - checkCompletedWithWrappedCFException(g, ex); - checkCompletedWithWrappedCFException(f, ex); - }} + if (createIncomplete) f.completeExceptionally(ex); - public void testThenCompose_exceptionalCompletion2() { - for (ExecutionMode m : ExecutionMode.values()) - { - final CFException ex = new CFException(); - final CompletableFuture f = new CompletableFuture<>(); - f.completeExceptionally(ex); - final CompletableFutureInc r = new CompletableFutureInc(); - final CompletableFuture g = f.thenCompose(r); checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); + assertEquals(0, r.invocationCount); }} /** * thenCompose result completes exceptionally if action does */ - public void testThenCompose_actionFailed1() { + public void testThenCompose_actionFailed() { 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 FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); + if (!createIncomplete) f.complete(v1); final CompletableFuture g = f.thenCompose(r); - f.complete(v1); - checkCompletedWithWrappedCFException(g); - checkCompletedNormally(f, v1); - }} + if (createIncomplete) f.complete(v1); - public void testThenCompose_actionFailed2() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - f.complete(v1); - final FailingCompletableFutureFunction r - = new FailingCompletableFutureFunction(); - final CompletableFuture g = f.thenCompose(r); checkCompletedWithWrappedCFException(g); checkCompletedNormally(f, v1); }} @@ -2926,324 +2503,24 @@ public class CompletableFutureTest exten /** * thenCompose result completes exceptionally if source cancelled */ - public void testThenCompose_sourceCancelled1() { + public void testThenCompose_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFutureInc r = new CompletableFutureInc(); + if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); final CompletableFuture g = f.thenCompose(r); - assertTrue(f.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(g); - checkCancelled(f); - }} + if (createIncomplete) { + checkIncomplete(g); + assertTrue(f.cancel(mayInterruptIfRunning)); + } - public void testThenCompose_sourceCancelled2() { - for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : new boolean[] { true, false }) - { - final CompletableFuture f = new CompletableFuture<>(); - assertTrue(f.cancel(mayInterruptIfRunning)); - final CompletableFutureInc r = new CompletableFutureInc(); - final CompletableFuture g = f.thenCompose(r); checkCompletedWithWrappedCancellationException(g); checkCancelled(f); }} - // asyncs - - /** - * thenRunAsync result completes normally after normal completion of source - */ - public void testThenRunAsync() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRunAsync(r); - f.complete(null); - checkCompletedNormally(g, null); - - // reordered version - f = new CompletableFuture<>(); - f.complete(null); - r = new Noop(); - g = f.thenRunAsync(r); - checkCompletedNormally(g, null); - } - - /** - * thenRunAsync result completes exceptionally after exceptional - * completion of source - */ - public void testThenRunAsync2() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRunAsync(r); - f.completeExceptionally(new CFException()); - try { - g.join(); - shouldThrow(); - } catch (CompletionException success) {} - checkCompletedWithWrappedCFException(g); - } - - /** - * thenRunAsync result completes exceptionally if action does - */ - public void testThenRunAsync3() { - CompletableFuture f = new CompletableFuture<>(); - FailingNoop r = new FailingNoop(); - CompletableFuture g = f.thenRunAsync(r); - f.complete(null); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenRunAsync result completes exceptionally if source cancelled - */ - public void testThenRunAsync4() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRunAsync(r); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - } - - /** - * thenApplyAsync result completes normally after normal completion of source - */ - public void testThenApplyAsync() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApplyAsync(inc); - f.complete(one); - checkCompletedNormally(g, two); - } - - /** - * thenApplyAsync result completes exceptionally after exceptional - * completion of source - */ - public void testThenApplyAsync2() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApplyAsync(inc); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenApplyAsync result completes exceptionally if action does - */ - public void testThenApplyAsync3() { - CompletableFuture f = new CompletableFuture<>(); - FailingFunction r = new FailingFunction(); - CompletableFuture g = f.thenApplyAsync(r); - f.complete(null); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenApplyAsync result completes exceptionally if source cancelled - */ - public void testThenApplyAsync4() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApplyAsync(inc); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - } - - /** - * thenAcceptAsync result completes normally after normal - * completion of source - */ - public void testThenAcceptAsync() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAcceptAsync(r); - f.complete(one); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); - } - - /** - * thenAcceptAsync result completes exceptionally after exceptional - * completion of source - */ - public void testThenAcceptAsync2() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAcceptAsync(r); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenAcceptAsync result completes exceptionally if action does - */ - public void testThenAcceptAsync3() { - CompletableFuture f = new CompletableFuture<>(); - FailingConsumer r = new FailingConsumer(); - CompletableFuture g = f.thenAcceptAsync(r); - f.complete(null); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenAcceptAsync result completes exceptionally if source cancelled - */ - public void testThenAcceptAsync4() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAcceptAsync(r); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - } - - // async with explicit executors - - /** - * thenRunAsync result completes normally after normal completion of source - */ - public void testThenRunAsyncE() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRunAsync(r, new ThreadExecutor()); - f.complete(null); - checkCompletedNormally(g, null); - - // reordered version - f = new CompletableFuture<>(); - f.complete(null); - r = new Noop(); - g = f.thenRunAsync(r, new ThreadExecutor()); - checkCompletedNormally(g, null); - } - - /** - * thenRunAsync result completes exceptionally after exceptional - * completion of source - */ - public void testThenRunAsync2E() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRunAsync(r, new ThreadExecutor()); - f.completeExceptionally(new CFException()); - try { - g.join(); - shouldThrow(); - } catch (CompletionException success) {} - checkCompletedWithWrappedCFException(g); - } - - /** - * thenRunAsync result completes exceptionally if action does - */ - public void testThenRunAsync3E() { - CompletableFuture f = new CompletableFuture<>(); - FailingNoop r = new FailingNoop(); - CompletableFuture g = f.thenRunAsync(r, new ThreadExecutor()); - f.complete(null); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenRunAsync result completes exceptionally if source cancelled - */ - public void testThenRunAsync4E() { - CompletableFuture f = new CompletableFuture<>(); - Noop r = new Noop(); - CompletableFuture g = f.thenRunAsync(r, new ThreadExecutor()); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - } - - /** - * thenApplyAsync result completes normally after normal completion of source - */ - public void testThenApplyAsyncE() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApplyAsync(inc, new ThreadExecutor()); - f.complete(one); - checkCompletedNormally(g, two); - } - - /** - * thenApplyAsync result completes exceptionally after exceptional - * completion of source - */ - public void testThenApplyAsync2E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApplyAsync(inc, new ThreadExecutor()); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenApplyAsync result completes exceptionally if action does - */ - public void testThenApplyAsync3E() { - CompletableFuture f = new CompletableFuture<>(); - FailingFunction r = new FailingFunction(); - CompletableFuture g = f.thenApplyAsync(r, new ThreadExecutor()); - f.complete(null); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenApplyAsync result completes exceptionally if source cancelled - */ - public void testThenApplyAsync4E() { - CompletableFuture f = new CompletableFuture<>(); - CompletableFuture g = f.thenApplyAsync(inc, new ThreadExecutor()); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - } - - /** - * thenAcceptAsync result completes normally after normal - * completion of source - */ - public void testThenAcceptAsyncE() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAcceptAsync(r, new ThreadExecutor()); - f.complete(one); - checkCompletedNormally(g, null); - assertEquals(r.value, (Integer) 2); - } - - /** - * thenAcceptAsync result completes exceptionally after exceptional - * completion of source - */ - public void testThenAcceptAsync2E() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAcceptAsync(r, new ThreadExecutor()); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenAcceptAsync result completes exceptionally if action does - */ - public void testThenAcceptAsync3E() { - CompletableFuture f = new CompletableFuture<>(); - FailingConsumer r = new FailingConsumer(); - CompletableFuture g = f.thenAcceptAsync(r, new ThreadExecutor()); - f.complete(null); - checkCompletedWithWrappedCFException(g); - } - - /** - * thenAcceptAsync result completes exceptionally if source cancelled - */ - public void testThenAcceptAsync4E() { - CompletableFuture f = new CompletableFuture<>(); - IncAction r = new IncAction(); - CompletableFuture g = f.thenAcceptAsync(r, new ThreadExecutor()); - assertTrue(f.cancel(true)); - checkCompletedWithWrappedCancellationException(g); - } - // other static methods /**