--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 20:10:04 1.52 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 22:48:50 1.57 @@ -284,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); @@ -349,76 +349,106 @@ public class CompletableFutureTest exten } } static final class IncFunction implements Function { + final ExecutionMode m; int invocationCount = 0; Integer value; + IncFunction(ExecutionMode m) { this.m = m; } public Integer apply(Integer x) { + m.checkExecutionMode(); invocationCount++; return value = inc(x); } } static final class SubtractAction implements BiConsumer { + final ExecutionMode m; int invocationCount = 0; Integer value; // Check this action was invoked exactly once when result is computed. + SubtractAction(ExecutionMode m) { this.m = m; } public void accept(Integer x, Integer y) { + m.checkExecutionMode(); invocationCount++; value = subtract(x, y); } } static final class SubtractFunction implements BiFunction { + final ExecutionMode m; int invocationCount = 0; Integer value; // Check this action was invoked exactly once when result is computed. + SubtractFunction(ExecutionMode m) { this.m = m; } public Integer apply(Integer x, Integer y) { + m.checkExecutionMode(); invocationCount++; return value = subtract(x, y); } } static final class Noop implements Runnable { + final ExecutionMode m; int invocationCount = 0; + Noop(ExecutionMode m) { this.m = m; } public void run() { + m.checkExecutionMode(); invocationCount++; } } static final class FailingSupplier implements Supplier { + final ExecutionMode m; int invocationCount = 0; + FailingSupplier(ExecutionMode m) { this.m = m; } public Integer get() { + m.checkExecutionMode(); invocationCount++; throw new CFException(); } } static final class FailingConsumer implements Consumer { + final ExecutionMode m; int invocationCount = 0; + FailingConsumer(ExecutionMode m) { this.m = m; } public void accept(Integer x) { + m.checkExecutionMode(); invocationCount++; throw new CFException(); } } static final class FailingBiConsumer implements BiConsumer { + final ExecutionMode m; int invocationCount = 0; + FailingBiConsumer(ExecutionMode m) { this.m = m; } public void accept(Integer x, Integer y) { + m.checkExecutionMode(); invocationCount++; throw new CFException(); } } static final class FailingFunction implements Function { + final ExecutionMode m; int invocationCount = 0; + FailingFunction(ExecutionMode m) { this.m = m; } public Integer apply(Integer x) { + m.checkExecutionMode(); invocationCount++; throw new CFException(); } } static final class FailingBiFunction implements BiFunction { + final ExecutionMode m; int invocationCount = 0; + FailingBiFunction(ExecutionMode m) { this.m = m; } public Integer apply(Integer x, Integer y) { + m.checkExecutionMode(); invocationCount++; throw new CFException(); } } static final class FailingRunnable implements Runnable { + final ExecutionMode m; int invocationCount = 0; + FailingRunnable(ExecutionMode m) { this.m = m; } public void run() { + m.checkExecutionMode(); invocationCount++; throw new CFException(); } @@ -426,8 +456,11 @@ public class CompletableFutureTest exten static final class CompletableFutureInc implements Function> { + final ExecutionMode m; int invocationCount = 0; + CompletableFutureInc(ExecutionMode m) { this.m = m; } public CompletableFuture apply(Integer x) { + m.checkExecutionMode(); invocationCount++; CompletableFuture f = new CompletableFuture<>(); f.complete(inc(x)); @@ -437,8 +470,11 @@ public class CompletableFutureTest exten static final class FailingCompletableFutureFunction implements Function> { + final ExecutionMode m; int invocationCount = 0; + FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; } public CompletableFuture apply(Integer x) { + m.checkExecutionMode(); invocationCount++; throw new CFException(); } @@ -446,11 +482,15 @@ public class CompletableFutureTest exten // Used for explicit executor tests static final class ThreadExecutor implements Executor { - AtomicInteger count = new AtomicInteger(0); + 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(r).start(); + new Thread(tg, r).start(); } } @@ -463,6 +503,9 @@ public class CompletableFutureTest exten public void checkExecutionMode() { assertNull(ForkJoinTask.getPool()); } + public CompletableFuture runAsync(Runnable a) { + throw new UnsupportedOperationException(); + } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { return f.thenRun(a); @@ -531,6 +574,9 @@ public class CompletableFutureTest exten assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool()); } + public CompletableFuture runAsync(Runnable a) { + return CompletableFuture.runAsync(a); + } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { return f.thenRunAsync(a); @@ -596,7 +642,10 @@ public class CompletableFutureTest exten EXECUTOR { public void checkExecutionMode() { - //TODO + assertTrue(ThreadExecutor.startedCurrentThread()); + } + public CompletableFuture runAsync(Runnable a) { + return CompletableFuture.runAsync(a, new ThreadExecutor()); } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { @@ -662,6 +711,7 @@ public class CompletableFutureTest exten }; public abstract void checkExecutionMode(); + public abstract CompletableFuture runAsync(Runnable a); public abstract CompletableFuture thenRun (CompletableFuture f, Runnable a); public abstract CompletableFuture thenAccept @@ -740,6 +790,7 @@ public class CompletableFutureTest exten if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture g = f.exceptionally ((Throwable t) -> { + ExecutionMode.DEFAULT.checkExecutionMode(); threadAssertSame(t, ex); a.getAndIncrement(); return v1; @@ -761,6 +812,7 @@ public class CompletableFutureTest exten if (!createIncomplete) f.completeExceptionally(ex1); final CompletableFuture g = f.exceptionally ((Throwable t) -> { + ExecutionMode.DEFAULT.checkExecutionMode(); threadAssertSame(t, ex1); a.getAndIncrement(); throw ex2; @@ -786,6 +838,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertSame(x, v1); threadAssertNull(t); a.getAndIncrement(); @@ -814,6 +867,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertNull(x); threadAssertSame(t, ex); a.getAndIncrement(); @@ -842,6 +896,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertNull(x); threadAssertTrue(t instanceof CancellationException); a.getAndIncrement(); @@ -869,6 +924,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertNull(x); threadAssertSame(ex1, t); a.getAndIncrement(); @@ -893,6 +949,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertSame(x, v1); threadAssertNull(t); a.getAndIncrement(); @@ -908,36 +965,35 @@ public class CompletableFutureTest exten /** * runAsync completes after running Runnable */ - public void testRunAsync() { - Noop r = new Noop(); - CompletableFuture f = CompletableFuture.runAsync(r); + 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()); - assertEquals(1, r.invocationCount); checkCompletedNormally(f, null); - } - - /** - * runAsync with executor completes after running Runnable - */ - public void testRunAsync2() { - Noop r = new Noop(); - ThreadExecutor exec = new ThreadExecutor(); - CompletableFuture f = CompletableFuture.runAsync(r, exec); - assertNull(f.join()); assertEquals(1, r.invocationCount); - checkCompletedNormally(f, null); - assertEquals(1, exec.count.get()); - } + }} /** * failing runAsync completes exceptionally after running Runnable */ - public void testRunAsync3() { - FailingRunnable r = new FailingRunnable(); - CompletableFuture f = CompletableFuture.runAsync(r); + 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); assertEquals(1, r.invocationCount); - } + }} /** * supplyAsync completes with result of supplier @@ -963,7 +1019,7 @@ public class CompletableFutureTest exten * Failing supplyAsync completes exceptionally */ public void testSupplyAsync3() { - FailingSupplier r = new FailingSupplier(); + FailingSupplier r = new FailingSupplier(ExecutionMode.ASYNC); CompletableFuture f = CompletableFuture.supplyAsync(r); checkCompletedWithWrappedCFException(f); assertEquals(1, r.invocationCount); @@ -980,7 +1036,7 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); if (!createIncomplete) f.complete(v1); final CompletableFuture g = m.thenRun(f, r); if (createIncomplete) { @@ -1003,7 +1059,7 @@ public class CompletableFutureTest exten { final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture g = m.thenRun(f, r); if (createIncomplete) { @@ -1025,9 +1081,9 @@ public class CompletableFutureTest exten for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { final CompletableFuture f = new CompletableFuture<>(); - final Noop r = new Noop(); + final Noop r = new Noop(m); if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); - final CompletableFuture g = f.thenRun(r); + final CompletableFuture g = m.thenRun(f, r); if (createIncomplete) { checkIncomplete(g); assertTrue(f.cancel(mayInterruptIfRunning)); @@ -1047,9 +1103,9 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final FailingRunnable r = new FailingRunnable(); + final FailingRunnable r = new FailingRunnable(m); if (!createIncomplete) f.complete(v1); - final CompletableFuture g = f.thenRun(r); + final CompletableFuture g = m.thenRun(f, r); if (createIncomplete) { checkIncomplete(g); f.complete(v1); @@ -1068,7 +1124,7 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); + final IncFunction r = new IncFunction(m); if (!createIncomplete) f.complete(v1); final CompletableFuture g = m.thenApply(f, r); if (createIncomplete) { @@ -1091,7 +1147,7 @@ public class CompletableFutureTest exten { final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); + final IncFunction r = new IncFunction(m); if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture g = m.thenApply(f, r); if (createIncomplete) { @@ -1113,9 +1169,9 @@ public class CompletableFutureTest exten for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { final CompletableFuture f = new CompletableFuture<>(); - final IncFunction r = new IncFunction(); + final IncFunction r = new IncFunction(m); if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); - final CompletableFuture g = f.thenApply(r); + final CompletableFuture g = m.thenApply(f, r); if (createIncomplete) { checkIncomplete(g); assertTrue(f.cancel(mayInterruptIfRunning)); @@ -1135,9 +1191,9 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final FailingFunction r = new FailingFunction(); + final FailingFunction r = new FailingFunction(m); if (!createIncomplete) f.complete(v1); - final CompletableFuture g = f.thenApply(r); + final CompletableFuture g = m.thenApply(f, r); if (createIncomplete) { checkIncomplete(g); f.complete(v1); @@ -1202,9 +1258,9 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final FailingConsumer r = new FailingConsumer(); + final FailingConsumer r = new FailingConsumer(m); if (!createIncomplete) f.complete(v1); - final CompletableFuture g = f.thenAccept(r); + final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { checkIncomplete(g); f.complete(v1); @@ -1225,7 +1281,7 @@ public class CompletableFutureTest exten final CompletableFuture f = new CompletableFuture<>(); final IncAction r = new IncAction(); if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); - final CompletableFuture g = f.thenAccept(r); + final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { checkIncomplete(g); assertTrue(f.cancel(mayInterruptIfRunning)); @@ -1249,7 +1305,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); + final SubtractFunction r = new SubtractFunction(m); if (fFirst) f.complete(v1); else g.complete(v2); if (!createIncomplete) @@ -1280,7 +1336,7 @@ public class CompletableFutureTest exten final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final CFException ex = new CFException(); - final SubtractFunction r = new SubtractFunction(); + final SubtractFunction r = new SubtractFunction(m); (fFirst ? f : g).complete(v1); if (!createIncomplete) @@ -1308,7 +1364,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingBiFunction r = new FailingBiFunction(); + final FailingBiFunction r = new FailingBiFunction(m); final CompletableFuture h = m.thenCombine(f, g, r); if (fFirst) { @@ -1336,7 +1392,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractFunction r = new SubtractFunction(); + final SubtractFunction r = new SubtractFunction(m); (fFirst ? f : g).complete(v1); if (!createIncomplete) @@ -1357,79 +1413,26 @@ public class CompletableFutureTest exten * thenAcceptBoth result completes normally after normal * completion of sources */ - 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() { - 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); - - g.complete(v2); - checkIncomplete(h); - assertEquals(0, r.invocationCount); - f.complete(v1); - - checkCompletedNormally(h, null); - assertEquals(subtract(v1, v2), r.value); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testThenAcceptBoth_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 SubtractAction r = new SubtractAction(); - - g.complete(v2); - f.complete(v1); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - - checkCompletedNormally(h, null); - assertEquals(subtract(v1, v2), r.value); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testThenAcceptBoth_normalCompletion4() { + 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 }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); + final SubtractAction r = new SubtractAction(m); - 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); @@ -1441,119 +1444,53 @@ 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() { - 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(); - - 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() { + 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(); + final SubtractAction r = new SubtractAction(m); - 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 }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingBiConsumer r = new FailingBiConsumer(); + final FailingBiConsumer r = new FailingBiConsumer(m); 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); @@ -1563,161 +1500,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(); + final SubtractAction r = new SubtractAction(m); - 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() { - 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(); - - 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() { + 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); - 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); @@ -1729,121 +1561,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 FailingRunnable r = new FailingRunnable(); - 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 FailingRunnable r = new FailingRunnable(); - final CompletableFuture h = m.runAfterBoth(f, g, r); + final FailingRunnable r = new FailingRunnable(m); - 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); }} @@ -1851,137 +1619,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(); - - 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); - }} + final Noop r = new Noop(m); - 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); + final IncFunction r = new IncFunction(m); - 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)); - }} + final IncFunction r = new IncFunction(m); - 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); @@ -1999,96 +1712,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(); + final IncFunction r = new IncFunction(m); - 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(m); + final IncFunction r2 = new IncFunction(m); 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); }} /** @@ -2101,7 +1792,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingFunction r = new FailingFunction(); + final FailingFunction r = new FailingFunction(m); final CompletableFuture h = m.applyToEither(f, g, r); f.complete(v1); @@ -2118,7 +1809,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingFunction r = new FailingFunction(); + final FailingFunction r = new FailingFunction(m); final CompletableFuture h = m.applyToEither(f, g, r); g.complete(v2); @@ -2134,95 +1825,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); + final IncFunction r = new IncFunction(m); - 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(m); + final IncFunction r2 = new IncFunction(m); + 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); }} /** @@ -2401,7 +2072,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingConsumer r = new FailingConsumer(); + final FailingConsumer r = new FailingConsumer(m); final CompletableFuture h = m.acceptEither(f, g, r); f.complete(v1); @@ -2418,7 +2089,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingConsumer r = new FailingConsumer(); + final FailingConsumer r = new FailingConsumer(m); final CompletableFuture h = m.acceptEither(f, g, r); g.complete(v2); @@ -2538,7 +2209,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); @@ -2559,7 +2230,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); @@ -2580,7 +2251,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); @@ -2602,7 +2273,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(); @@ -2622,7 +2293,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(); @@ -2642,7 +2313,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); @@ -2669,7 +2340,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); @@ -2700,7 +2371,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingRunnable r = new FailingRunnable(); + final FailingRunnable r = new FailingRunnable(m); final CompletableFuture h = m.runAfterEither(f, g, r); f.complete(v1); @@ -2717,7 +2388,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final FailingRunnable r = new FailingRunnable(); + final FailingRunnable r = new FailingRunnable(m); final CompletableFuture h = m.runAfterEither(f, g, r); g.complete(v2); @@ -2737,7 +2408,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)); @@ -2757,7 +2428,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)); @@ -2777,7 +2448,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); @@ -2804,7 +2475,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); @@ -2833,9 +2504,9 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final CompletableFutureInc r = new CompletableFutureInc(); + final CompletableFutureInc r = new CompletableFutureInc(m); if (!createIncomplete) f.complete(v1); - final CompletableFuture g = f.thenCompose(r); + final CompletableFuture g = m.thenCompose(f, r); if (createIncomplete) f.complete(v1); checkCompletedNormally(g, inc(v1)); @@ -2852,10 +2523,10 @@ public class CompletableFutureTest exten for (boolean createIncomplete : new boolean[] { true, false }) { final CFException ex = new CFException(); - final CompletableFutureInc r = new CompletableFutureInc(); + final CompletableFutureInc r = new CompletableFutureInc(m); final CompletableFuture f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex); - final CompletableFuture g = f.thenCompose(r); + final CompletableFuture g = m.thenCompose(f, r); if (createIncomplete) f.completeExceptionally(ex); checkCompletedWithWrappedCFException(g, ex); @@ -2873,9 +2544,9 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final FailingCompletableFutureFunction r - = new FailingCompletableFutureFunction(); + = new FailingCompletableFutureFunction(m); if (!createIncomplete) f.complete(v1); - final CompletableFuture g = f.thenCompose(r); + final CompletableFuture g = m.thenCompose(f, r); if (createIncomplete) f.complete(v1); checkCompletedWithWrappedCFException(g); @@ -2891,9 +2562,9 @@ public class CompletableFutureTest exten for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { final CompletableFuture f = new CompletableFuture<>(); - final CompletableFutureInc r = new CompletableFutureInc(); + final CompletableFutureInc r = new CompletableFutureInc(m); if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); - final CompletableFuture g = f.thenCompose(r); + final CompletableFuture g = m.thenCompose(f, r); if (createIncomplete) { checkIncomplete(g); assertTrue(f.cancel(mayInterruptIfRunning)); @@ -3065,7 +2736,7 @@ public class CompletableFutureTest exten () -> f.thenCompose(null), () -> f.thenComposeAsync(null), - () -> f.thenComposeAsync(new CompletableFutureInc(), null), + () -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), () -> f.thenComposeAsync(null, exec), () -> f.exceptionally(null),