--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 21:41:37 1.55 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/06 19:25:41 1.67 @@ -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,138 +360,178 @@ 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 NoopConsumer extends CheckedIntegerAction + implements Consumer + { + NoopConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { - invocationCount++; - value = inc(x); + invoked(); + value = 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 { - final ExecutionMode m; - int invocationCount = 0; - Noop(ExecutionMode m) { this.m = m; } + + class Noop extends CheckedAction implements Runnable { + Noop(ExecutionMode m) { super(m); } public void run() { - m.checkExecutionMode(); - 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 CheckedIntegerAction + implements Consumer + { + FailingConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { - invocationCount++; + invoked(); + value = x; throw new CFException(); } } - static final class FailingBiConsumer implements BiConsumer { - int invocationCount = 0; + + class FailingBiConsumer extends CheckedIntegerAction + implements BiConsumer + { + FailingBiConsumer(ExecutionMode m) { super(m); } public void accept(Integer x, Integer y) { - invocationCount++; + invoked(); + value = subtract(x, y); throw new CFException(); } } - static final class FailingFunction implements Function { - int invocationCount = 0; + + class FailingFunction extends CheckedIntegerAction + implements Function + { + FailingFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x) { - invocationCount++; + invoked(); + value = x; throw new CFException(); } } - static final class FailingBiFunction implements BiFunction { - int invocationCount = 0; + + class FailingBiFunction extends CheckedIntegerAction + implements BiFunction + { + FailingBiFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x, Integer y) { - invocationCount++; + invoked(); + value = subtract(x, y); 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 CheckedIntegerAction + implements Function> + { + CompletableFutureInc(ExecutionMode m) { super(m); } public CompletableFuture apply(Integer x) { - invocationCount++; + invoked(); + value = x; CompletableFuture f = new CompletableFuture<>(); f.complete(inc(x)); return f; } } - static final class FailingCompletableFutureFunction - implements Function> { - int invocationCount = 0; + class FailingCompletableFutureFunction extends CheckedIntegerAction + implements Function> + { + FailingCompletableFutureFunction(ExecutionMode m) { super(m); } public CompletableFuture apply(Integer x) { - invocationCount++; + invoked(); + value = x; 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); @@ -534,6 +600,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); @@ -599,7 +671,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) { @@ -665,6 +743,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 @@ -743,6 +823,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; @@ -764,6 +845,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; @@ -789,6 +871,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertSame(x, v1); threadAssertNull(t); a.getAndIncrement(); @@ -817,6 +900,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertNull(x); threadAssertSame(t, ex); a.getAndIncrement(); @@ -845,6 +929,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(); @@ -872,6 +957,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertNull(x); threadAssertSame(ex1, t); a.getAndIncrement(); @@ -896,6 +982,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.handle (f, (Integer x, Throwable t) -> { + m.checkExecutionMode(); threadAssertSame(x, v1); threadAssertNull(t); a.getAndIncrement(); @@ -911,66 +998,69 @@ public class CompletableFutureTest exten /** * runAsync completes after running Runnable */ - public void testRunAsync() { - Noop r = new Noop(ExecutionMode.ASYNC); - 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(ExecutionMode.EXECUTOR); - 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 @@ -993,7 +1083,7 @@ public class CompletableFutureTest exten checkCompletedNormally(g, null); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1016,7 +1106,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1030,7 +1120,7 @@ public class CompletableFutureTest exten final CompletableFuture f = new CompletableFuture<>(); 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)); @@ -1038,7 +1128,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); checkCancelled(f); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1050,9 +1140,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); @@ -1071,7 +1161,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) { @@ -1081,7 +1171,7 @@ public class CompletableFutureTest exten checkCompletedNormally(g, inc(v1)); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1094,7 +1184,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) { @@ -1104,7 +1194,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1116,9 +1206,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)); @@ -1126,7 +1216,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); checkCancelled(f); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1138,9 +1228,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); @@ -1159,7 +1249,7 @@ public class CompletableFutureTest exten for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final NoopConsumer r = new NoopConsumer(m); if (!createIncomplete) f.complete(v1); final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { @@ -1168,9 +1258,8 @@ public class CompletableFutureTest exten } checkCompletedNormally(g, null); + r.assertValue(v1); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); - assertEquals(inc(v1), r.value); }} /** @@ -1183,7 +1272,7 @@ public class CompletableFutureTest exten { final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); - final IncAction r = new IncAction(); + final NoopConsumer r = new NoopConsumer(m); if (!createIncomplete) f.completeExceptionally(ex); final CompletableFuture g = m.thenAccept(f, r); if (createIncomplete) { @@ -1193,50 +1282,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 NoopConsumer r = new NoopConsumer(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); }} /** @@ -1252,7 +1341,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) @@ -1260,14 +1349,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(); }} /** @@ -1283,7 +1372,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) @@ -1295,39 +1384,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() { @@ -1339,7 +1401,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) @@ -1352,11 +1414,38 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(!fFirst ? f : g); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(fFirst ? f : g, v1); }} /** + * 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(m); + 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); + }} + + /** * thenAcceptBoth result completes normally after normal * completion of sources */ @@ -1369,7 +1458,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); + final SubtractAction r = new SubtractAction(m); if (fFirst) f.complete(v1); else g.complete(v2); if (!createIncomplete) @@ -1377,12 +1466,12 @@ public class CompletableFutureTest exten final CompletableFuture h = m.thenAcceptBoth(f, g, r); if (createIncomplete) { checkIncomplete(h); - assertEquals(0, r.invocationCount); + 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); }} @@ -1400,7 +1489,7 @@ public class CompletableFutureTest exten final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final CFException ex = new CFException(); - final SubtractAction r = new SubtractAction(); + final SubtractAction r = new SubtractAction(m); (fFirst ? f : g).complete(v1); if (!createIncomplete) @@ -1412,39 +1501,12 @@ public class CompletableFutureTest exten } checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(fFirst ? f : g, v1); checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); }} /** - * thenAcceptBoth result completes exceptionally if action does - */ - 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 CompletableFuture h = m.thenAcceptBoth(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); - }} - - /** * thenAcceptBoth result completes exceptionally if either source cancelled */ public void testThenAcceptBoth_sourceCancelled() { @@ -1456,7 +1518,7 @@ public class CompletableFutureTest exten { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final SubtractAction r = new SubtractAction(); + final SubtractAction r = new SubtractAction(m); (fFirst ? f : g).complete(v1); if (!createIncomplete) @@ -1469,11 +1531,38 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(!fFirst ? f : g); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(fFirst ? f : g, v1); }} /** + * thenAcceptBoth result completes exceptionally if action does + */ + 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(m); + final CompletableFuture h = m.thenAcceptBoth(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); + }} + + /** * runAfterBoth result completes normally after normal * completion of sources */ @@ -1494,12 +1583,12 @@ public class CompletableFutureTest exten final CompletableFuture h = m.runAfterBoth(f, g, r); if (createIncomplete) { checkIncomplete(h); - assertEquals(0, r.invocationCount); + 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); }} @@ -1529,41 +1618,12 @@ public class CompletableFutureTest exten } checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(fFirst ? f : g, v1); checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); }} /** - * runAfterBoth result completes exceptionally if action does - */ - 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(); - - 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(h1); - checkCompletedWithWrappedCFException(h2); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - /** * runAfterBoth result completes exceptionally if either source cancelled */ public void testRunAfterBoth_sourceCancelled() { @@ -1589,116 +1649,158 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(h); checkCancelled(!fFirst ? f : g); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); checkCompletedNormally(fFirst ? f : g, v1); }} /** - * applyToEither result completes normally after normal completion - * of either source + * runAfterBoth result completes exceptionally if action does */ - public void testApplyToEither_normalCompletion() { + public void testRunAfterBoth_actionFailed() { 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 FailingRunnable r1 = new FailingRunnable(m); + final FailingRunnable r2 = new FailingRunnable(m); - 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); + CompletableFuture h1 = m.runAfterBoth(f, g, r1); + if (fFirst) { + f.complete(v1); + g.complete(v2); + } else { + g.complete(v2); + f.complete(v1); } - checkCompletedNormally(h, inc(fFirst ? v1 : v2)); - if (!fFirst) f.complete(v1); else g.complete(v2); + CompletableFuture h2 = m.runAfterBoth(f, g, r2); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - checkCompletedNormally(h, inc(fFirst ? v1 : v2)); }} - public void testApplyToEither_normalCompletionBothAvailable() { + /** + * applyToEither result completes normally after normal completion + * of either source + */ + public void testApplyToEither_normalCompletion() { 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 IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); - if (fFirst) { - f.complete(v1); - g.complete(v2); - } else { - g.complete(v2); - f.complete(v1); - } + 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(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); - 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 (boolean createIncomplete : new boolean[] { true, false }) - for (boolean fFirst : new boolean[] { true, false }) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); final CFException ex = new CFException(); - final IncFunction r = new IncFunction(); + final IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); - if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex); - final CompletableFuture h = m.applyToEither(f, g, r); - if (createIncomplete) { - checkIncomplete(h); - assertEquals(0, r.invocationCount); - (fFirst ? f : g).completeExceptionally(ex); - } + 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(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); - checkCompletedWithWrappedCFException(h, ex); - (!fFirst ? f : g).complete(v1); + // 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), h4.join()); + rs[4].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h4, ex); + rs[4].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h5.join()); + rs[5].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h5, ex); + rs[5].assertNotInvoked(); + } - assertEquals(0, r.invocationCount); - checkCompletedNormally(!fFirst ? f : g, v1); - checkCompletedWithWrappedCFException(fFirst ? f : g, ex); - checkCompletedWithWrappedCFException(h, ex); + checkCompletedWithWrappedCFException(f, ex); + checkCompletedNormally(g, v1); + 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(); }} 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 r1 = new IncFunction(); - final IncFunction r2 = new IncFunction(); final CFException ex = new CFException(); - final CompletableFuture j = (reverseArgs ? g : f); - final CompletableFuture k = (reverseArgs ? f : g); - final CompletableFuture h1 = m.applyToEither(j, k, r1); + 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]); if (fFirst) { f.complete(v1); g.completeExceptionally(ex); @@ -1706,740 +1808,597 @@ public class CompletableFutureTest exten g.completeExceptionally(ex); f.complete(v1); } - final CompletableFuture h2 = m.applyToEither(j, k, r2); + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); - // unspecified behavior + // unspecified behavior - both source completions available + try { + assertEquals(inc(v1), h0.join()); + rs[0].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h0, ex); + rs[0].assertNotInvoked(); + } try { assertEquals(inc(v1), h1.join()); - assertEquals(1, r1.invocationCount); + rs[1].assertValue(inc(v1)); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h1, ex); - assertEquals(0, r1.invocationCount); + rs[1].assertNotInvoked(); } - try { assertEquals(inc(v1), h2.join()); - assertEquals(1, r2.invocationCount); + rs[2].assertValue(inc(v1)); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h2, ex); - assertEquals(0, r2.invocationCount); + rs[2].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h3.join()); + rs[3].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCFException(h3, ex); + rs[3].assertNotInvoked(); } - checkCompletedWithWrappedCFException(g, ex); - checkCompletedNormally(f, 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(g, ex); }} /** * 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 (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 IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); - 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)); - } + 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); - checkCompletedWithWrappedCancellationException(h); - (!fFirst ? f : g).complete(v1); + // 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), h4.join()); + rs[4].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h4); + rs[4].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h5.join()); + rs[5].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h5); + rs[5].assertNotInvoked(); + } - assertEquals(0, r.invocationCount); - checkCompletedNormally(!fFirst ? f : g, v1); - checkCancelled(fFirst ? f : g); - checkCompletedWithWrappedCancellationException(h); + 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_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 r1 = new IncFunction(); - final IncFunction r2 = new IncFunction(); - final CFException ex = new CFException(); - final CompletableFuture j = (reverseArgs ? g : f); - final CompletableFuture k = (reverseArgs ? f : g); + final IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); - final CompletableFuture h1 = m.applyToEither(j, k, r1); + final CompletableFuture h0 = m.applyToEither(f, g, rs[0]); + final CompletableFuture h1 = m.applyToEither(g, f, rs[1]); if (fFirst) { f.complete(v1); - assertTrue(g.cancel(mayInterruptIfRunning)); + g.cancel(mayInterruptIfRunning); } else { - assertTrue(g.cancel(mayInterruptIfRunning)); + g.cancel(mayInterruptIfRunning); f.complete(v1); } - final CompletableFuture h2 = m.applyToEither(j, k, r2); + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); - // unspecified behavior + // unspecified behavior - both source completions available + try { + assertEquals(inc(v1), h0.join()); + rs[0].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h0); + rs[0].assertNotInvoked(); + } try { assertEquals(inc(v1), h1.join()); - assertEquals(1, r1.invocationCount); + rs[1].assertValue(inc(v1)); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h1); - assertEquals(0, r1.invocationCount); + rs[1].assertNotInvoked(); } - try { assertEquals(inc(v1), h2.join()); - assertEquals(1, r2.invocationCount); + rs[2].assertValue(inc(v1)); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h2); - assertEquals(0, r2.invocationCount); + rs[2].assertNotInvoked(); + } + try { + assertEquals(inc(v1), h3.join()); + rs[3].assertValue(inc(v1)); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h3); + rs[3].assertNotInvoked(); } - checkCancelled(g); checkCompletedNormally(f, v1); + checkCancelled(g); }} /** - * acceptEither result completes normally after normal completion - * of either source + * applyToEither result completes exceptionally if action does */ - public void testAcceptEither_normalCompletion1() { + public void testApplyToEither_actionFailed() { 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 IncAction r = new IncAction(); - final CompletableFuture h = m.acceptEither(f, g, r); + final FailingFunction[] rs = new FailingFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); + final CompletableFuture h0 = m.applyToEither(f, g, rs[0]); + final CompletableFuture h1 = m.applyToEither(g, f, rs[1]); f.complete(v1); - checkCompletedNormally(h, null); - assertEquals(inc(v1), r.value); - g.complete(v2); - - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - checkCompletedNormally(h, null); - }} - - public void testAcceptEither_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 IncAction r = new IncAction(); - final CompletableFuture h = m.acceptEither(f, g, r); + final CompletableFuture h2 = m.applyToEither(f, g, rs[2]); + final CompletableFuture h3 = m.applyToEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h0); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); + checkCompletedWithWrappedCFException(h3); + for (int i = 0; i < 4; i++) rs[i].assertValue(v1); g.complete(v2); - checkCompletedNormally(h, null); - assertEquals(inc(v2), r.value); - f.complete(v1); + + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.applyToEither(f, g, rs[4]); + final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); + + checkCompletedWithWrappedCFException(h4); + assertTrue(Objects.equals(v1, rs[4].value) || + Objects.equals(v2, rs[4].value)); + checkCompletedWithWrappedCFException(h5); + assertTrue(Objects.equals(v1, rs[5].value) || + Objects.equals(v2, rs[5].value)); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - checkCompletedNormally(h, null); }} - public void testAcceptEither_normalCompletion3() { + /** + * acceptEither result completes normally after normal completion + * of either source + */ + public void testAcceptEither_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 IncAction r = new IncAction(); + final NoopConsumer[] rs = new NoopConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); + final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.complete(v1); + checkCompletedNormally(h0, null); + checkCompletedNormally(h1, null); + rs[0].assertValue(v1); + rs[1].assertValue(v1); + final CompletableFuture h2 = m.acceptEither(f, g, rs[2]); + final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); + checkCompletedNormally(h2, null); + checkCompletedNormally(h3, null); + rs[2].assertValue(v1); + rs[3].assertValue(v1); g.complete(v2); - final CompletableFuture h = m.acceptEither(f, g, r); - checkCompletedNormally(h, null); + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); + final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); + checkCompletedNormally(h4, null); + checkCompletedNormally(h5, null); + assertTrue(Objects.equals(v1, rs[4].value) || + Objects.equals(v2, rs[4].value)); + assertTrue(Objects.equals(v1, rs[5].value) || + Objects.equals(v2, rs[5].value)); + checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - - // unspecified behavior - assertTrue(Objects.equals(r.value, inc(v1)) || - Objects.equals(r.value, inc(v2))); + checkCompletedNormally(h0, null); + checkCompletedNormally(h1, null); + checkCompletedNormally(h2, null); + checkCompletedNormally(h3, null); + for (int i = 0; i < 4; i++) rs[i].assertValue(v1); }} /** * acceptEither result completes exceptionally after exceptional * completion of either source */ - public void testAcceptEither_exceptionalCompletion1() { + public void testAcceptEither_exceptionalCompletion() { for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { final CompletableFuture f = new CompletableFuture<>(); final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); - final CompletableFuture h = m.acceptEither(f, g, r); final CFException ex = new CFException(); + final NoopConsumer[] rs = new NoopConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); + final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); - g.complete(v1); + checkCompletedWithWrappedCFException(h0, ex); + checkCompletedWithWrappedCFException(h1, ex); + final CompletableFuture h2 = m.acceptEither(f, g, rs[2]); + final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h2, ex); + checkCompletedWithWrappedCFException(h3, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - checkCompletedWithWrappedCFException(f, ex); - checkCompletedWithWrappedCFException(h, ex); - }} - - public void testAcceptEither_exceptionalCompletion2() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); - final CompletableFuture h = m.acceptEither(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 testAcceptEither_exceptionalCompletion3() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.acceptEither(f, g, r); + g.complete(v1); - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); + final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); - assertEquals(inc(v1), r.value); + assertNull(h4.join()); + rs[4].assertValue(v1); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(h4, ex); + rs[4].assertNotInvoked(); } - - checkCompletedWithWrappedCFException(g, ex); - checkCompletedNormally(f, v1); - }} - - public void testAcceptEither_exceptionalCompletion4() { - for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) - { - final CompletableFuture f = new CompletableFuture<>(); - final CompletableFuture g = new CompletableFuture<>(); - final IncAction r = new IncAction(); - final CFException ex = new CFException(); - - f.completeExceptionally(ex); - g.complete(v1); - final CompletableFuture h = m.acceptEither(f, g, r); - - // unspecified behavior - Integer v; try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); - assertEquals(inc(v1), r.value); + assertNull(h5.join()); + rs[5].assertValue(v1); } catch (CompletionException ok) { - checkCompletedWithWrappedCFException(h, ex); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCFException(h5, ex); + rs[5].assertNotInvoked(); } checkCompletedWithWrappedCFException(f, ex); checkCompletedNormally(g, v1); - }} - - /** - * acceptEither result completes exceptionally if action does - */ - public void testAcceptEither_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 FailingConsumer r = new FailingConsumer(); - final CompletableFuture h = m.acceptEither(f, g, r); - - f.complete(v1); - checkCompletedWithWrappedCFException(h); - g.complete(v2); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testAcceptEither_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 FailingConsumer r = new FailingConsumer(); - final CompletableFuture h = m.acceptEither(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(); }} /** * acceptEither result completes exceptionally if either source cancelled */ - public void testAcceptEither_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 IncAction r = new IncAction(); - final CompletableFuture h = m.acceptEither(f, g, r); - - assertTrue(f.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); - g.complete(v1); - - checkCancelled(f); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - checkCompletedWithWrappedCancellationException(h); - }} - - public void testAcceptEither_sourceCancelled2() { + public void testAcceptEither_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 IncAction r = new IncAction(); - final CompletableFuture h = m.acceptEither(f, g, r); + final NoopConsumer[] rs = new NoopConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); - assertTrue(g.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); - f.complete(v1); + final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(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.acceptEither(f, g, rs[2]); + final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); - checkCancelled(g); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - checkCompletedWithWrappedCancellationException(h); - }} - - public void testAcceptEither_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 IncAction r = new IncAction(); - - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.acceptEither(f, g, r); + g.complete(v1); - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); + final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); - assertEquals(inc(v1), r.value); + assertNull(h4.join()); + rs[4].assertValue(v1); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCancellationException(h4); + rs[4].assertNotInvoked(); } - - checkCancelled(g); - checkCompletedNormally(f, v1); - }} - - public void testAcceptEither_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 IncAction r = new IncAction(); - - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); - final CompletableFuture h = m.acceptEither(f, g, r); - - // unspecified behavior - Integer v; try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); - assertEquals(inc(v1), r.value); + assertNull(h5.join()); + rs[5].assertValue(v1); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCancellationException(h5); + rs[5].assertNotInvoked(); } checkCancelled(f); checkCompletedNormally(g, v1); + checkCompletedWithWrappedCancellationException(h0); + checkCompletedWithWrappedCancellationException(h1); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); + for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); }} /** - * runAfterEither result completes normally after normal completion - * of either source + * acceptEither result completes exceptionally if action does */ - public void testRunAfterEither_normalCompletion1() { + public void testAcceptEither_actionFailed() { 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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); + final FailingConsumer[] rs = new FailingConsumer[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); + final CompletableFuture h0 = m.acceptEither(f, g, rs[0]); + final CompletableFuture h1 = m.acceptEither(g, f, rs[1]); f.complete(v1); - checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); + final CompletableFuture h2 = m.acceptEither(f, g, rs[2]); + final CompletableFuture h3 = m.acceptEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h0); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); + checkCompletedWithWrappedCFException(h3); + for (int i = 0; i < 4; i++) rs[i].assertValue(v1); + g.complete(v2); + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); + final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); + + checkCompletedWithWrappedCFException(h4); + assertTrue(Objects.equals(v1, rs[4].value) || + Objects.equals(v2, rs[4].value)); + checkCompletedWithWrappedCFException(h5); + assertTrue(Objects.equals(v1, rs[5].value) || + Objects.equals(v2, rs[5].value)); + checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); }} - public void testRunAfterEither_normalCompletion2() { + /** + * runAfterEither result completes normally after normal completion + * of either source + */ + public void testRunAfterEither_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 Noop r = new Noop(m); - final CompletableFuture h = m.runAfterEither(f, g, r); + final Noop[] rs = new Noop[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); - g.complete(v2); - checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.complete(v1); + checkCompletedNormally(h0, null); + checkCompletedNormally(h1, null); + rs[0].assertInvoked(); + rs[1].assertInvoked(); + final CompletableFuture h2 = m.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); + checkCompletedNormally(h2, null); + checkCompletedNormally(h3, null); + rs[2].assertInvoked(); + rs[3].assertInvoked(); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - checkCompletedNormally(h, null); - assertEquals(1, r.invocationCount); - }} - - public void testRunAfterEither_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(m); - - f.complete(v1); g.complete(v2); - final CompletableFuture h = m.runAfterEither(f, g, r); - checkCompletedNormally(h, null); + final CompletableFuture h4 = m.runAfterEither(f, g, rs[4]); + final CompletableFuture h5 = m.runAfterEither(g, f, rs[5]); + checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); - assertEquals(1, r.invocationCount); + checkCompletedNormally(h0, null); + checkCompletedNormally(h1, null); + checkCompletedNormally(h2, null); + checkCompletedNormally(h3, null); + checkCompletedNormally(h4, null); + checkCompletedNormally(h5, null); + for (int i = 0; i < 6; i++) rs[i].assertInvoked(); }} /** * runAfterEither result completes exceptionally after exceptional * completion of either source */ - public void testRunAfterEither_exceptionalCompletion1() { + public void testRunAfterEither_exceptionalCompletion() { 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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); final CFException ex = new CFException(); + final Noop[] rs = new Noop[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(g, f, rs[1]); + checkIncomplete(h0); + checkIncomplete(h1); + rs[0].assertNotInvoked(); + rs[1].assertNotInvoked(); f.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); - g.complete(v1); - - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - checkCompletedWithWrappedCFException(f, ex); - checkCompletedWithWrappedCFException(h, ex); - }} - - public void testRunAfterEither_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(m); - final CompletableFuture h = m.runAfterEither(f, g, r); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - checkCompletedWithWrappedCFException(h, ex); - f.complete(v1); + checkCompletedWithWrappedCFException(h0, ex); + checkCompletedWithWrappedCFException(h1, ex); + final CompletableFuture h2 = m.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h2, ex); + checkCompletedWithWrappedCFException(h3, ex); - assertEquals(0, r.invocationCount); - checkCompletedNormally(f, v1); - checkCompletedWithWrappedCFException(g, ex); - checkCompletedWithWrappedCFException(h, ex); - }} - - public void testRunAfterEither_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(m); - final CFException ex = new CFException(); - - g.completeExceptionally(ex); - f.complete(v1); - final CompletableFuture h = m.runAfterEither(f, g, r); + g.complete(v1); - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.runAfterEither(f, g, rs[4]); + final CompletableFuture h5 = m.runAfterEither(g, f, rs[5]); try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); + assertNull(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 testRunAfterEither_exceptionalCompletion4() { - 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(m); - final CFException ex = new CFException(); - - f.completeExceptionally(ex); - g.complete(v1); - final CompletableFuture h = m.runAfterEither(f, g, r); - - // unspecified behavior - Integer v; try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); + assertNull(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); - }} - - /** - * runAfterEither result completes exceptionally if action does - */ - public void testRunAfterEither_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.runAfterEither(f, g, r); - - f.complete(v1); - checkCompletedWithWrappedCFException(h); - g.complete(v2); - checkCompletedNormally(f, v1); - checkCompletedNormally(g, v2); - }} - - public void testRunAfterEither_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.runAfterEither(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(); }} /** * runAfterEither result completes exceptionally if either source cancelled */ - public void testRunAfterEither_sourceCancelled1() { + public void testRunAfterEither_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 Noop r = new Noop(m); - final CompletableFuture h = m.runAfterEither(f, g, r); + final Noop[] rs = new Noop[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); - assertTrue(f.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); - g.complete(v1); - - checkCancelled(f); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - checkCompletedWithWrappedCancellationException(h); - }} - - public void testRunAfterEither_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(m); - final CompletableFuture h = m.runAfterEither(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 testRunAfterEither_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 Noop r = new Noop(m); + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(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.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); + checkCompletedWithWrappedCancellationException(h2); + checkCompletedWithWrappedCancellationException(h3); - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - final CompletableFuture h = m.runAfterEither(f, g, r); + g.complete(v1); - // unspecified behavior - Integer v; + // unspecified behavior - both source completions available + final CompletableFuture h4 = m.runAfterEither(f, g, rs[4]); + final CompletableFuture h5 = m.runAfterEither(g, f, rs[5]); try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); + assertNull(h4.join()); + rs[4].assertInvoked(); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + checkCompletedWithWrappedCancellationException(h4); + rs[4].assertNotInvoked(); + } + try { + assertNull(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 testRunAfterEither_sourceCancelled4() { + /** + * runAfterEither result completes exceptionally if action does + */ + public void testRunAfterEither_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 Noop r = new Noop(m); - - assertTrue(f.cancel(mayInterruptIfRunning)); - g.complete(v1); - final CompletableFuture h = m.runAfterEither(f, g, r); + final FailingRunnable[] rs = new FailingRunnable[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); - // unspecified behavior - Integer v; - try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); - } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); - } + final CompletableFuture h0 = m.runAfterEither(f, g, rs[0]); + final CompletableFuture h1 = m.runAfterEither(g, f, rs[1]); + f.complete(v1); + final CompletableFuture h2 = m.runAfterEither(f, g, rs[2]); + final CompletableFuture h3 = m.runAfterEither(g, f, rs[3]); + checkCompletedWithWrappedCFException(h0); + checkCompletedWithWrappedCFException(h1); + checkCompletedWithWrappedCFException(h2); + checkCompletedWithWrappedCFException(h3); + for (int i = 0; i < 4; i++) rs[i].assertInvoked(); + g.complete(v2); + final CompletableFuture h4 = m.runAfterEither(f, g, rs[4]); + final CompletableFuture h5 = m.runAfterEither(g, f, rs[5]); + checkCompletedWithWrappedCFException(h4); + checkCompletedWithWrappedCFException(h5); - checkCancelled(f); - checkCompletedNormally(g, v1); + checkCompletedNormally(f, v1); + checkCompletedNormally(g, v2); + for (int i = 0; i < 6; i++) rs[i].assertInvoked(); }} /** @@ -2451,14 +2410,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(); }} /** @@ -2470,15 +2429,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(); }} /** @@ -2491,9 +2450,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); @@ -2509,9 +2468,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)); @@ -2538,7 +2497,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); @@ -2552,6 +2512,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 */ @@ -2610,7 +2587,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), @@ -2683,7 +2660,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),