--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 20:10:04 1.52 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/06 01:19:22 1.62 @@ -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); @@ -320,13 +320,39 @@ public class CompletableFutureTest exten checkCompletedNormally(f, "test"); } - // Choose non-commutative actions for better coverage + abstract class CheckedAction { + int invocationCount = 0; + final ExecutionMode m; + CheckedAction(ExecutionMode m) { this.m = m; } + void invoked() { + m.checkExecutionMode(); + assertEquals(0, invocationCount++); + } + void assertNotInvoked() { assertEquals(0, invocationCount); } + void assertInvoked() { assertEquals(1, invocationCount); } + } - // A non-commutative function that handles and produces null values as well. - static Integer subtract(Integer x, Integer y) { - return (x == null && y == null) ? null : - ((x == null) ? 42 : x.intValue()) - - ((y == null) ? 99 : y.intValue()); + abstract class CheckedIntegerAction extends CheckedAction { + Integer value; + CheckedIntegerAction(ExecutionMode m) { super(m); } + void assertValue(Integer expected) { + assertInvoked(); + assertEquals(expected, value); + } + } + + class IntegerSupplier extends CheckedAction + implements Supplier + { + final Integer value; + IntegerSupplier(ExecutionMode m, Integer value) { + super(m); + this.value = value; + } + public Integer get() { + invoked(); + return value; + } } // A function that handles and produces null values as well. @@ -334,135 +360,169 @@ public class CompletableFutureTest exten return (x == null) ? null : x + 1; } - static final Supplier supplyOne = - () -> Integer.valueOf(1); - static final Function inc = - (Integer x) -> Integer.valueOf(x.intValue() + 1); - static final BiFunction subtract = - (Integer x, Integer y) -> subtract(x, y); - static final class IncAction implements Consumer { - int invocationCount = 0; - Integer value; + class IncAction extends CheckedIntegerAction + implements Consumer + { + IncAction(ExecutionMode m) { super(m); } public void accept(Integer x) { - invocationCount++; + invoked(); value = inc(x); } } - static final class IncFunction implements Function { - int invocationCount = 0; - Integer value; + + class IncFunction extends CheckedIntegerAction + implements Function + { + IncFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x) { - invocationCount++; + invoked(); return value = inc(x); } } - static final class SubtractAction implements BiConsumer { - int invocationCount = 0; - Integer value; - // Check this action was invoked exactly once when result is computed. + + // Choose non-commutative actions for better coverage + // A non-commutative function that handles and produces null values as well. + static Integer subtract(Integer x, Integer y) { + return (x == null && y == null) ? null : + ((x == null) ? 42 : x.intValue()) + - ((y == null) ? 99 : y.intValue()); + } + + class SubtractAction extends CheckedIntegerAction + implements BiConsumer + { + SubtractAction(ExecutionMode m) { super(m); } public void accept(Integer x, Integer y) { - invocationCount++; + invoked(); value = subtract(x, y); } } - static final class SubtractFunction implements BiFunction { - int invocationCount = 0; - Integer value; - // Check this action was invoked exactly once when result is computed. + + class SubtractFunction extends CheckedIntegerAction + implements BiFunction + { + SubtractFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x, Integer y) { - invocationCount++; + invoked(); return value = subtract(x, y); } } - static final class Noop implements Runnable { - int invocationCount = 0; + + class Noop extends CheckedAction implements Runnable { + Noop(ExecutionMode m) { super(m); } public void run() { - invocationCount++; + invoked(); } } - static final class FailingSupplier implements Supplier { - int invocationCount = 0; + class FailingSupplier extends CheckedAction implements Supplier + { + FailingSupplier(ExecutionMode m) { super(m); } public Integer get() { - invocationCount++; + invoked(); throw new CFException(); } } - static final class FailingConsumer implements Consumer { - int invocationCount = 0; + + class FailingConsumer extends CheckedAction implements Consumer { + FailingConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { - invocationCount++; + invoked(); throw new CFException(); } } - static final class FailingBiConsumer implements BiConsumer { - int invocationCount = 0; + + class FailingBiConsumer extends CheckedAction + implements BiConsumer + { + FailingBiConsumer(ExecutionMode m) { super(m); } public void accept(Integer x, Integer y) { - invocationCount++; + invoked(); throw new CFException(); } } - static final class FailingFunction implements Function { - int invocationCount = 0; + + class FailingFunction extends CheckedAction + implements Function + { + FailingFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x) { - invocationCount++; + invoked(); throw new CFException(); } } - static final class FailingBiFunction implements BiFunction { - int invocationCount = 0; + + class FailingBiFunction extends CheckedAction + implements BiFunction + { + FailingBiFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x, Integer y) { - invocationCount++; + invoked(); throw new CFException(); } } - static final class FailingRunnable implements Runnable { - int invocationCount = 0; + + class FailingRunnable extends CheckedAction implements Runnable { + FailingRunnable(ExecutionMode m) { super(m); } public void run() { - invocationCount++; + invoked(); throw new CFException(); } } - static final class CompletableFutureInc - implements Function> { - int invocationCount = 0; + + class CompletableFutureInc extends CheckedAction + implements Function> + { + CompletableFutureInc(ExecutionMode m) { super(m); } public CompletableFuture apply(Integer x) { - invocationCount++; + invoked(); CompletableFuture f = new CompletableFuture<>(); f.complete(inc(x)); return f; } } - static final class FailingCompletableFutureFunction - implements Function> { - int invocationCount = 0; + class FailingCompletableFutureFunction extends CheckedAction + implements Function> + { + FailingCompletableFutureFunction(ExecutionMode m) { super(m); } public CompletableFuture apply(Integer x) { - invocationCount++; + invoked(); throw new CFException(); } } // 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(); } } /** * Permits the testing of parallel code for the 3 different - * execution modes without repeating all the testing code. + * execution modes without copy/pasting all the test methods. */ enum ExecutionMode { DEFAULT { public void checkExecutionMode() { + assertFalse(ThreadExecutor.startedCurrentThread()); assertNull(ForkJoinTask.getPool()); } + public CompletableFuture runAsync(Runnable a) { + throw new UnsupportedOperationException(); + } + public CompletableFuture supplyAsync(Supplier a) { + throw new UnsupportedOperationException(); + } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { return f.thenRun(a); @@ -531,6 +591,12 @@ public class CompletableFutureTest exten assertSame(ForkJoinPool.commonPool(), ForkJoinTask.getPool()); } + public CompletableFuture runAsync(Runnable a) { + return CompletableFuture.runAsync(a); + } + public CompletableFuture supplyAsync(Supplier a) { + return CompletableFuture.supplyAsync(a); + } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { return f.thenRunAsync(a); @@ -596,7 +662,13 @@ 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 supplyAsync(Supplier a) { + return CompletableFuture.supplyAsync(a, new ThreadExecutor()); } public CompletableFuture thenRun (CompletableFuture f, Runnable a) { @@ -662,6 +734,8 @@ public class CompletableFutureTest exten }; public abstract void checkExecutionMode(); + public abstract CompletableFuture runAsync(Runnable a); + public abstract CompletableFuture supplyAsync(Supplier a); public abstract CompletableFuture thenRun (CompletableFuture f, Runnable a); public abstract CompletableFuture thenAccept @@ -740,6 +814,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 +836,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 +862,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 +891,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 +920,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 +948,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 +973,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,66 +989,69 @@ public class CompletableFutureTest exten /** * runAsync completes after running Runnable */ - public void testRunAsync() { - Noop r = new Noop(); - CompletableFuture f = CompletableFuture.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); + 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); - assertEquals(1, exec.count.get()); - } + r.assertInvoked(); + }} /** * 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); - } + r.assertInvoked(); + }} /** * supplyAsync completes with result of supplier */ - public void testSupplyAsync() { - CompletableFuture f; - f = CompletableFuture.supplyAsync(supplyOne); - assertEquals(f.join(), one); - checkCompletedNormally(f, one); - } - - /** - * supplyAsync with executor completes with result of supplier - */ - public void testSupplyAsync2() { - CompletableFuture f; - f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); - assertEquals(f.join(), one); - checkCompletedNormally(f, one); - } + public void testSupplyAsync_normalCompletion() { + ExecutionMode[] executionModes = { + ExecutionMode.ASYNC, + ExecutionMode.EXECUTOR, + }; + for (ExecutionMode m : executionModes) + for (Integer v1 : new Integer[] { 1, null }) + { + final IntegerSupplier r = new IntegerSupplier(m, v1); + final CompletableFuture f = m.supplyAsync(r); + assertSame(v1, f.join()); + checkCompletedNormally(f, v1); + r.assertInvoked(); + }} /** * Failing supplyAsync completes exceptionally */ - public void testSupplyAsync3() { - FailingSupplier r = new FailingSupplier(); - CompletableFuture f = CompletableFuture.supplyAsync(r); + public void testSupplyAsync_exceptionalCompletion() { + ExecutionMode[] executionModes = { + ExecutionMode.ASYNC, + ExecutionMode.EXECUTOR, + }; + for (ExecutionMode m : executionModes) + { + FailingSupplier r = new FailingSupplier(m); + CompletableFuture f = m.supplyAsync(r); checkCompletedWithWrappedCFException(f); - assertEquals(1, r.invocationCount); - } + r.assertInvoked(); + }} // seq completion methods @@ -980,7 +1064,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) { @@ -990,7 +1074,7 @@ public class CompletableFutureTest exten checkCompletedNormally(g, null); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1003,7 +1087,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) { @@ -1013,7 +1097,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1025,9 +1109,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)); @@ -1035,7 +1119,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); checkCancelled(f); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1047,9 +1131,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 +1152,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) { @@ -1078,7 +1162,7 @@ public class CompletableFutureTest exten checkCompletedNormally(g, inc(v1)); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1091,7 +1175,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) { @@ -1101,7 +1185,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1113,9 +1197,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)); @@ -1123,7 +1207,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); checkCancelled(f); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1135,9 +1219,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); @@ -1156,7 +1240,7 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); if (!createIncomplete) f.complete(v1); final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { @@ -1166,8 +1250,8 @@ public class CompletableFutureTest exten checkCompletedNormally(g, null); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); - assertEquals(inc(v1), r.value); + r.assertInvoked(); + r.assertValue(inc(v1)); }} /** @@ -1180,7 +1264,7 @@ public class CompletableFutureTest exten { final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { @@ -1190,50 +1274,50 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** - * thenAccept result completes exceptionally if action does + * thenAccept result completes exceptionally if source cancelled */ - public void testThenAccept_actionFailed() { + public void testThenAccept_sourceCancelled() { for (ExecutionMode m : ExecutionMode.values()) for (boolean createIncomplete : new boolean[] { true, false }) - for (Integer v1 : new Integer[] { 1, null }) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { final CompletableFuture f = new CompletableFuture<>(); - final FailingConsumer r = new FailingConsumer(); - if (!createIncomplete) f.complete(v1); - final CompletableFuture g = f.thenAccept(r); + final IncAction r = new IncAction(m); + if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); + final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { checkIncomplete(g); - f.complete(v1); + assertTrue(f.cancel(mayInterruptIfRunning)); } - checkCompletedWithWrappedCFException(g); - checkCompletedNormally(f, v1); + checkCompletedWithWrappedCancellationException(g); + checkCancelled(f); + r.assertNotInvoked(); }} /** - * thenAccept result completes exceptionally if source cancelled + * thenAccept result completes exceptionally if action does */ - public void testThenAccept_sourceCancelled() { + public void testThenAccept_actionFailed() { 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 IncAction r = new IncAction(); - if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); - final CompletableFuture g = f.thenAccept(r); + final FailingConsumer r = new FailingConsumer(m); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { checkIncomplete(g); - assertTrue(f.cancel(mayInterruptIfRunning)); + f.complete(v1); } - checkCompletedWithWrappedCancellationException(g); - checkCancelled(f); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(g); + checkCompletedNormally(f, v1); }} /** @@ -1249,7 +1333,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) @@ -1257,14 +1341,14 @@ public class CompletableFutureTest exten final CompletableFuture h = m.thenCombine(f, g, r); if (createIncomplete) { checkIncomplete(h); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); if (!fFirst) f.complete(v1); else g.complete(v2); } checkCompletedNormally(h, subtract(v1, v2)); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1280,7 +1364,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) @@ -1292,39 +1376,12 @@ public class CompletableFutureTest exten } checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(fFirst ? f : g, v1); checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); }} /** - * thenCombine result completes exceptionally if action does - */ - 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 FailingBiFunction r = new FailingBiFunction(); - final CompletableFuture h = m.thenCombine(f, g, r); - - if (fFirst) { - f.complete(v1); - g.complete(v2); - } else { - g.complete(v2); - f.complete(v1); - } - - checkCompletedWithWrappedCFException(h); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - /** * thenCombine result completes exceptionally if either source cancelled */ public void testThenCombine_sourceCancelled() { @@ -1336,7 +1393,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) @@ -1349,90 +1406,64 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(!fFirst ? f : g); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(fFirst ? f : g, v1); }} /** - * 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() { - 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() { + 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 FailingBiFunction r = new FailingBiFunction(m); + final CompletableFuture h = m.thenCombine(f, g, r); - g.complete(v2); - f.complete(v1); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); + 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_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 }) { 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); + r.assertNotInvoked(); + if (!fFirst) f.complete(v1); else g.complete(v2); + } checkCompletedNormally(h, null); - assertEquals(subtract(v1, v2), r.value); + r.assertValue(subtract(v1, v2)); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); }} @@ -1441,119 +1472,82 @@ 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(); + final SubtractAction r = new SubtractAction(m); - g.completeExceptionally(ex); - f.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(g, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); + r.assertNotInvoked(); + checkCompletedNormally(fFirst ? f : g, v1); + checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); }} - public void testThenAcceptBoth_exceptionalCompletion4() { + /** + * thenAcceptBoth result completes exceptionally if either source cancelled + */ + 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 CFException ex = new CFException(); + final SubtractAction r = new SubtractAction(m); - f.completeExceptionally(ex); - 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)); + } - checkCompletedWithWrappedCFException(h, ex); - checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); + checkCompletedWithWrappedCancellationException(h); + checkCancelled(!fFirst ? f : g); + r.assertNotInvoked(); + checkCompletedNormally(fFirst ? f : g, v1); }} /** * 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); @@ -1561,166 +1555,32 @@ 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() { - 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(f.cancel(mayInterruptIfRunning)); - g.complete(v1); - final CompletableFuture h = m.thenAcceptBoth(f, g, r); - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(f); - assertEquals(0, r.invocationCount); - checkCompletedNormally(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); + r.assertNotInvoked(); + if (!fFirst) f.complete(v1); else g.complete(v2); + } checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); + r.assertInvoked(); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); }} @@ -1729,500 +1589,283 @@ 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); - }} - - /** - * 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() { - 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); - - g.complete(v2); - checkIncomplete(h); - f.complete(v1); - - checkCompletedWithWrappedCFException(h); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); + r.assertNotInvoked(); + checkCompletedNormally(fFirst ? f : g, v1); + checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); }} /** * runAfterBoth result completes exceptionally if either source cancelled */ - public void testRunAfterBoth_sourceCancelled1() { + 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 CompletableFuture h = m.runAfterBoth(f, g, r); - - assertTrue(f.cancel(mayInterruptIfRunning)); - checkIncomplete(h); - g.complete(v1); + final Noop r = new Noop(m); - 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(); + (fFirst ? f : g).complete(v1); + if (!createIncomplete) + assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); final CompletableFuture h = m.runAfterBoth(f, g, r); - - assertTrue(g.cancel(mayInterruptIfRunning)); - checkIncomplete(h); - f.complete(v1); + if (createIncomplete) { + checkIncomplete(h); + assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); + } checkCompletedWithWrappedCancellationException(h); - checkCancelled(g); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); + checkCancelled(!fFirst ? f : g); + r.assertNotInvoked(); + checkCompletedNormally(fFirst ? f : g, v1); }} - public void testRunAfterBoth_sourceCancelled3() { + /** + * runAfterBoth result completes exceptionally if action does + */ + public void testRunAfterBoth_actionFailed() { for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : 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 FailingRunnable r1 = new FailingRunnable(m); + final FailingRunnable r2 = new FailingRunnable(m); - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.runAfterBoth(f, g, r); + CompletableFuture h1 = m.runAfterBoth(f, g, r1); + if (fFirst) { + f.complete(v1); + g.complete(v2); + } else { + g.complete(v2); + f.complete(v1); + } + CompletableFuture h2 = m.runAfterBoth(f, g, r2); - checkCompletedWithWrappedCancellationException(h); - checkCancelled(g); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); 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); - final CompletableFuture h = m.runAfterBoth(f, g, r); - - checkCompletedWithWrappedCancellationException(h); - checkCancelled(f); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); + checkCompletedNormally(g, v2); }} /** * applyToEither result completes normally after normal completion * of either source */ - public void testApplyToEither_normalCompletion1() { + public void testApplyToEither_normalCompletion() { 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(); - final CompletableFuture h = m.applyToEither(f, g, r); + final IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); + final CompletableFuture h0 = m.applyToEither(f, g, rs[0]); + final CompletableFuture h1 = m.applyToEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.complete(v1); - checkCompletedNormally(h, inc(v1)); + checkCompletedNormally(h0, inc(v1)); + checkCompletedNormally(h1, inc(v1)); + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); + checkCompletedNormally(h2, inc(v1)); + checkCompletedNormally(h3, inc(v1)); g.complete(v2); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - checkCompletedNormally(h, inc(v1)); - }} - - public void testApplyToEither_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 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(); - - f.complete(v1); - g.complete(v2); - final CompletableFuture h = m.applyToEither(f, g, r); + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.applyToEither(f, g, rs[4]); + final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); + rs[4].assertValue(h4.join()); + rs[5].assertValue(h5.join()); + assertTrue(Objects.equals(inc(v1), h4.join()) || + Objects.equals(inc(v2), h4.join())); + assertTrue(Objects.equals(inc(v1), h5.join()) || + Objects.equals(inc(v2), h5.join())); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - - // unspecified behavior - assertTrue(Objects.equals(h.join(), inc(v1)) || - Objects.equals(h.join(), inc(v2))); - assertEquals(1, r.invocationCount); + checkCompletedNormally(h0, inc(v1)); + checkCompletedNormally(h1, inc(v1)); + checkCompletedNormally(h2, inc(v1)); + checkCompletedNormally(h3, inc(v1)); + for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); }} /** * applyToEither result completes exceptionally after exceptional * completion of either source */ - public void testApplyToEither_exceptionalCompletion1() { + public void testApplyToEither_exceptionalCompletion() { 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 CompletableFuture h = m.applyToEither(f, g, r); final CFException ex = new CFException(); + final IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); + final CompletableFuture h0 = m.applyToEither(f, g, rs[0]); + final CompletableFuture h1 = m.applyToEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); + checkCompletedWithWrappedCFException(h0, ex); + checkCompletedWithWrappedCFException(h1, ex); + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h2, ex); + checkCompletedWithWrappedCFException(h3, 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(); - final CompletableFuture h = m.applyToEither(f, g, r); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); - f.complete(v1); - - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - checkCompletedWithWrappedCFException(g, ex); - checkCompletedWithWrappedCFException(h, ex); - }} - - public void testApplyToEither_exceptionalCompletion3() { - 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(); - - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.applyToEither(f, g, r); - - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.applyToEither(f, g, rs[4]); + final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); try { - assertEquals(inc(v1), h.join()); - assertEquals(1, r.invocationCount); + assertEquals(inc(v1), h4.join()); + rs[4].assertInvoked(); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(h4, ex); + rs[4].assertNotInvoked(); } - - 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), h5.join()); + rs[5].assertInvoked(); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(h5, ex); + rs[5].assertNotInvoked(); } checkCompletedWithWrappedCFException(f, ex); checkCompletedNormally(g, v1); - }} - - /** - * applyToEither result completes exceptionally if action does - */ - public void testApplyToEither_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 FailingFunction r = new FailingFunction(); - final CompletableFuture h = m.applyToEither(f, g, r); - - f.complete(v1); - checkCompletedWithWrappedCFException(h); - g.complete(v2); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testApplyToEither_actionFailed2() { - 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 FailingFunction r = new FailingFunction(); - final CompletableFuture h = m.applyToEither(f, g, r); - - g.complete(v2); - checkCompletedWithWrappedCFException(h); - f.complete(v1); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); + checkCompletedWithWrappedCFException(h0, ex); + checkCompletedWithWrappedCFException(h1, ex); + checkCompletedWithWrappedCFException(h2, ex); + checkCompletedWithWrappedCFException(h3, ex); + checkCompletedWithWrappedCFException(h4, ex); + for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} /** * applyToEither result completes exceptionally if either source cancelled */ - public void testApplyToEither_sourceCancelled1() { + public void testApplyToEither_sourceCancelled() { 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(); - final CompletableFuture h = m.applyToEither(f, g, r); + final IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); - assertTrue(f.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); + final CompletableFuture h0 = m.applyToEither(f, g, rs[0]); + final CompletableFuture h1 = m.applyToEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); + f.cancel(mayInterruptIfRunning); + checkCompletedWithWrappedCancellationException(h0); + checkCompletedWithWrappedCancellationException(h1); + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); 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(); - final CompletableFuture h = m.applyToEither(f, g, r); - - assertTrue(g.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); - f.complete(v1); - - checkCancelled(g); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - checkCompletedWithWrappedCancellationException(h); - }} - - public void testApplyToEither_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 IncFunction r = new IncFunction(); - - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.applyToEither(f, g, r); - - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.applyToEither(f, g, rs[4]); + final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); try { - assertEquals(inc(v1), h.join()); - assertEquals(1, r.invocationCount); + assertEquals(inc(v1), h4.join()); + rs[4].assertInvoked(); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCancellationException(h4); + rs[4].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h5.join()); + rs[5].assertInvoked(); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h5); + rs[5].assertNotInvoked(); } - checkCancelled(g); - checkCompletedNormally(f, v1); + checkCancelled(f); + checkCompletedNormally(g, v1); + checkCompletedWithWrappedCancellationException(h0); + checkCompletedWithWrappedCancellationException(h1); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); + for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} - public void testApplyToEither_sourceCancelled4() { + /** + * applyToEither result completes exceptionally if action does + */ + public void testApplyToEither_actionFailed() { for (ExecutionMode m : ExecutionMode.values()) - for (boolean mayInterruptIfRunning : 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(); - - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); - final CompletableFuture h = m.applyToEither(f, g, r); + final FailingFunction[] rs = new FailingFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); - // unspecified behavior - Integer v; - try { - assertEquals(inc(v1), h.join()); - assertEquals(1, r.invocationCount); - } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); - } - - checkCancelled(f); - checkCompletedNormally(g, v1); + final CompletableFuture h0 = m.applyToEither(f, g, rs[0]); + final CompletableFuture h1 = m.applyToEither(g, f, rs[1]); + f.complete(v1); + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); + g.complete(v2); + final CompletableFuture h4 = m.applyToEither(f, g, rs[4]); + final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); + + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + checkCompletedWithWrappedCFException(h0); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); + checkCompletedWithWrappedCFException(h3); + checkCompletedWithWrappedCFException(h4); + checkCompletedWithWrappedCFException(h5); + for (int i = 0; i < rs.length; i++) rs[i].assertInvoked(); }} /** @@ -2236,7 +1879,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); final CompletableFuture h = m.acceptEither(f, g, r); f.complete(v1); @@ -2256,7 +1899,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); final CompletableFuture h = m.acceptEither(f, g, r); g.complete(v2); @@ -2276,7 +1919,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); f.complete(v1); g.complete(v2); @@ -2301,7 +1944,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); final CompletableFuture h = m.acceptEither(f, g, r); final CFException ex = new CFException(); @@ -2309,7 +1952,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); g.complete(v1); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(g, v1); checkCompletedWithWrappedCFException(f, ex); checkCompletedWithWrappedCFException(h, ex); @@ -2321,7 +1964,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); final CompletableFuture h = m.acceptEither(f, g, r); final CFException ex = new CFException(); @@ -2329,7 +1972,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); f.complete(v1); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(f, v1); checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(h, ex); @@ -2341,7 +1984,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); final CFException ex = new CFException(); g.completeExceptionally(ex); @@ -2352,11 +1995,11 @@ public class CompletableFutureTest exten Integer v; try { assertNull(h.join()); - assertEquals(1, r.invocationCount); + r.assertInvoked(); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); } checkCompletedWithWrappedCFException(g, ex); @@ -2369,7 +2012,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); final CFException ex = new CFException(); f.completeExceptionally(ex); @@ -2380,11 +2023,11 @@ public class CompletableFutureTest exten Integer v; try { assertNull(h.join()); - assertEquals(1, r.invocationCount); + r.assertInvoked(); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); } checkCompletedWithWrappedCFException(f, ex); @@ -2401,7 +2044,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 +2061,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); @@ -2438,7 +2081,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); final CompletableFuture h = m.acceptEither(f, g, r); assertTrue(f.cancel(mayInterruptIfRunning)); @@ -2446,7 +2089,7 @@ public class CompletableFutureTest exten g.complete(v1); checkCancelled(f); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(g, v1); checkCompletedWithWrappedCancellationException(h); }} @@ -2458,7 +2101,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); final CompletableFuture h = m.acceptEither(f, g, r); assertTrue(g.cancel(mayInterruptIfRunning)); @@ -2466,7 +2109,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCancelled(g); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(f, v1); checkCompletedWithWrappedCancellationException(h); }} @@ -2478,7 +2121,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); assertTrue(g.cancel(mayInterruptIfRunning)); f.complete(v1); @@ -2488,11 +2131,11 @@ public class CompletableFutureTest exten Integer v; try { assertNull(h.join()); - assertEquals(1, r.invocationCount); + r.assertInvoked(); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); } checkCancelled(g); @@ -2506,7 +2149,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final IncAction r = new IncAction(m); assertTrue(f.cancel(mayInterruptIfRunning)); g.complete(v1); @@ -2516,11 +2159,11 @@ public class CompletableFutureTest exten Integer v; try { assertNull(h.join()); - assertEquals(1, r.invocationCount); + r.assertInvoked(); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); } checkCancelled(f); @@ -2538,18 +2181,18 @@ 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); checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); + r.assertInvoked(); g.complete(v2); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} public void testRunAfterEither_normalCompletion2() { @@ -2559,18 +2202,18 @@ 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); checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); + r.assertInvoked(); f.complete(v1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} public void testRunAfterEither_normalCompletion3() { @@ -2580,7 +2223,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); @@ -2589,7 +2232,7 @@ public class CompletableFutureTest exten checkCompletedNormally(h, null); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -2602,7 +2245,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(); @@ -2610,7 +2253,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); g.complete(v1); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(g, v1); checkCompletedWithWrappedCFException(f, ex); checkCompletedWithWrappedCFException(h, ex); @@ -2622,7 +2265,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(); @@ -2630,7 +2273,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(h, ex); f.complete(v1); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(f, v1); checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(h, ex); @@ -2642,7 +2285,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); @@ -2653,10 +2296,10 @@ public class CompletableFutureTest exten Integer v; try { assertNull(h.join()); - assertEquals(1, r.invocationCount); + r.assertInvoked(); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); } checkCompletedWithWrappedCFException(g, ex); @@ -2669,7 +2312,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); @@ -2680,10 +2323,10 @@ public class CompletableFutureTest exten Integer v; try { assertNull(h.join()); - assertEquals(1, r.invocationCount); + r.assertInvoked(); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); } checkCompletedWithWrappedCFException(f, ex); @@ -2700,7 +2343,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 +2360,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 +2380,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)); @@ -2745,7 +2388,7 @@ public class CompletableFutureTest exten g.complete(v1); checkCancelled(f); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(g, v1); checkCompletedWithWrappedCancellationException(h); }} @@ -2757,7 +2400,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)); @@ -2765,7 +2408,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCancelled(g); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(f, v1); checkCompletedWithWrappedCancellationException(h); }} @@ -2777,7 +2420,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); @@ -2787,10 +2430,10 @@ public class CompletableFutureTest exten Integer v; try { assertNull(h.join()); - assertEquals(1, r.invocationCount); + r.assertInvoked(); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); } checkCancelled(g); @@ -2804,7 +2447,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); @@ -2814,10 +2457,10 @@ public class CompletableFutureTest exten Integer v; try { assertNull(h.join()); - assertEquals(1, r.invocationCount); + r.assertInvoked(); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); } checkCancelled(f); @@ -2833,14 +2476,14 @@ 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)); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -2852,15 +2495,15 @@ 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); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -2873,9 +2516,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 +2534,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)); @@ -2920,7 +2563,8 @@ public class CompletableFutureTest exten */ public void testAllOf_normal() throws Exception { for (int k = 1; k < 20; ++k) { - CompletableFuture[] fs = (CompletableFuture[]) new CompletableFuture[k]; + CompletableFuture[] fs + = (CompletableFuture[]) new CompletableFuture[k]; for (int i = 0; i < k; ++i) fs[i] = new CompletableFuture<>(); CompletableFuture f = CompletableFuture.allOf(fs); @@ -2934,6 +2578,23 @@ public class CompletableFutureTest exten } } + public void testAllOf_backwards() throws Exception { + for (int k = 1; k < 20; ++k) { + CompletableFuture[] fs + = (CompletableFuture[]) new CompletableFuture[k]; + for (int i = 0; i < k; ++i) + fs[i] = new CompletableFuture<>(); + CompletableFuture f = CompletableFuture.allOf(fs); + for (int i = k - 1; i >= 0; i--) { + checkIncomplete(f); + checkIncomplete(CompletableFuture.allOf(fs)); + fs[i].complete(one); + } + checkCompletedNormally(f, null); + checkCompletedNormally(CompletableFuture.allOf(fs), null); + } + } + /** * anyOf(no component futures) returns an incomplete future */ @@ -2992,7 +2653,7 @@ public class CompletableFutureTest exten Runnable[] throwingActions = { () -> CompletableFuture.supplyAsync(null), () -> CompletableFuture.supplyAsync(null, exec), - () -> CompletableFuture.supplyAsync(supplyOne, null), + () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), () -> CompletableFuture.runAsync(null), () -> CompletableFuture.runAsync(null, exec), @@ -3065,7 +2726,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),