--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/03 03:54:14 1.59 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/06 19:19:04 1.66 @@ -320,171 +320,184 @@ public class CompletableFutureTest exten checkCompletedNormally(f, "test"); } - // 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()); + 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 function that handles and produces null values as well. - static Integer inc(Integer x) { - return (x == null) ? null : x + 1; + abstract class CheckedIntegerAction extends CheckedAction { + Integer value; + CheckedIntegerAction(ExecutionMode m) { super(m); } + void assertValue(Integer expected) { + assertInvoked(); + assertEquals(expected, value); + } } - static final class IntegerSupplier implements Supplier { - final ExecutionMode m; - int invocationCount = 0; + class IntegerSupplier extends CheckedAction + implements Supplier + { final Integer value; IntegerSupplier(ExecutionMode m, Integer value) { - this.m = m; + super(m); this.value = value; } public Integer get() { - m.checkExecutionMode(); - invocationCount++; + invoked(); return value; } } - - static final class IncAction implements Consumer { - int invocationCount = 0; - Integer value; + + // A function that handles and produces null values as well. + static Integer inc(Integer x) { + return (x == null) ? null : x + 1; + } + + class NoopConsumer extends CheckedIntegerAction + implements Consumer + { + NoopConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { - invocationCount++; - value = inc(x); + invoked(); + value = x; } } - static final class IncFunction implements Function { - final ExecutionMode m; - int invocationCount = 0; - Integer value; - IncFunction(ExecutionMode m) { this.m = m; } + + class IncFunction extends CheckedIntegerAction + implements Function + { + IncFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x) { - m.checkExecutionMode(); - invocationCount++; + invoked(); return value = inc(x); } } - static final class SubtractAction implements BiConsumer { - final ExecutionMode m; - int invocationCount = 0; - Integer value; - // Check this action was invoked exactly once when result is computed. - SubtractAction(ExecutionMode m) { this.m = m; } + + // 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) { - m.checkExecutionMode(); - invocationCount++; + invoked(); value = subtract(x, y); } } - static final class SubtractFunction implements BiFunction { - final ExecutionMode m; - int invocationCount = 0; - Integer value; - // Check this action was invoked exactly once when result is computed. - SubtractFunction(ExecutionMode m) { this.m = m; } + + class SubtractFunction extends CheckedIntegerAction + implements BiFunction + { + SubtractFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x, Integer y) { - m.checkExecutionMode(); - 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 { - final ExecutionMode m; - int invocationCount = 0; - FailingSupplier(ExecutionMode m) { this.m = m; } + class FailingSupplier extends CheckedAction + implements Supplier + { + FailingSupplier(ExecutionMode m) { super(m); } public Integer get() { - m.checkExecutionMode(); - invocationCount++; + invoked(); throw new CFException(); } } - static final class FailingConsumer implements Consumer { - final ExecutionMode m; - int invocationCount = 0; - FailingConsumer(ExecutionMode m) { this.m = m; } + + class FailingConsumer extends CheckedIntegerAction + implements Consumer + { + FailingConsumer(ExecutionMode m) { super(m); } public void accept(Integer x) { - m.checkExecutionMode(); - invocationCount++; + invoked(); + value = x; throw new CFException(); } } - static final class FailingBiConsumer implements BiConsumer { - final ExecutionMode m; - int invocationCount = 0; - FailingBiConsumer(ExecutionMode m) { this.m = m; } + + class FailingBiConsumer extends CheckedIntegerAction + implements BiConsumer + { + FailingBiConsumer(ExecutionMode m) { super(m); } public void accept(Integer x, Integer y) { - m.checkExecutionMode(); - invocationCount++; + invoked(); + value = subtract(x, y); throw new CFException(); } } - static final class FailingFunction implements Function { - final ExecutionMode m; - int invocationCount = 0; - FailingFunction(ExecutionMode m) { this.m = m; } + + class FailingFunction extends CheckedIntegerAction + implements Function + { + FailingFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x) { - m.checkExecutionMode(); - invocationCount++; + invoked(); + value = x; throw new CFException(); } } - static final class FailingBiFunction implements BiFunction { - final ExecutionMode m; - int invocationCount = 0; - FailingBiFunction(ExecutionMode m) { this.m = m; } + + class FailingBiFunction extends CheckedIntegerAction + implements BiFunction + { + FailingBiFunction(ExecutionMode m) { super(m); } public Integer apply(Integer x, Integer y) { - m.checkExecutionMode(); - invocationCount++; + invoked(); + value = subtract(x, y); throw new CFException(); } } - static final class FailingRunnable implements Runnable { - final ExecutionMode m; - int invocationCount = 0; - FailingRunnable(ExecutionMode m) { this.m = m; } + + class FailingRunnable extends CheckedAction implements Runnable { + FailingRunnable(ExecutionMode m) { super(m); } public void run() { - m.checkExecutionMode(); - invocationCount++; + invoked(); throw new CFException(); } } - static final class CompletableFutureInc - implements Function> { - final ExecutionMode m; - int invocationCount = 0; - CompletableFutureInc(ExecutionMode m) { this.m = m; } + + class CompletableFutureInc extends CheckedIntegerAction + implements Function> + { + CompletableFutureInc(ExecutionMode m) { super(m); } public CompletableFuture apply(Integer x) { - m.checkExecutionMode(); - invocationCount++; + invoked(); + value = x; CompletableFuture f = new CompletableFuture<>(); f.complete(inc(x)); return f; } } - static final class FailingCompletableFutureFunction - implements Function> { - final ExecutionMode m; - int invocationCount = 0; - FailingCompletableFutureFunction(ExecutionMode m) { this.m = m; } + class FailingCompletableFutureFunction extends CheckedIntegerAction + implements Function> + { + FailingCompletableFutureFunction(ExecutionMode m) { super(m); } public CompletableFuture apply(Integer x) { - m.checkExecutionMode(); - invocationCount++; + invoked(); + value = x; throw new CFException(); } } @@ -505,11 +518,12 @@ public class CompletableFutureTest exten /** * 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) { @@ -995,7 +1009,7 @@ public class CompletableFutureTest exten final CompletableFuture f = m.runAsync(r); assertNull(f.join()); checkCompletedNormally(f, null); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1011,7 +1025,7 @@ public class CompletableFutureTest exten final FailingRunnable r = new FailingRunnable(m); final CompletableFuture f = m.runAsync(r); checkCompletedWithWrappedCFException(f); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1029,7 +1043,7 @@ public class CompletableFutureTest exten final CompletableFuture f = m.supplyAsync(r); assertSame(v1, f.join()); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1045,7 +1059,7 @@ public class CompletableFutureTest exten FailingSupplier r = new FailingSupplier(m); CompletableFuture f = m.supplyAsync(r); checkCompletedWithWrappedCFException(f); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} // seq completion methods @@ -1069,7 +1083,7 @@ public class CompletableFutureTest exten checkCompletedNormally(g, null); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1092,7 +1106,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1114,7 +1128,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); checkCancelled(f); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1157,7 +1171,7 @@ public class CompletableFutureTest exten checkCompletedNormally(g, inc(v1)); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -1180,7 +1194,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1202,7 +1216,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCancellationException(g); checkCancelled(f); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /** @@ -1235,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) { @@ -1244,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); }} /** @@ -1259,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) { @@ -1269,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(m); - if (!createIncomplete) f.complete(v1); + 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 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); }} /** @@ -1336,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(); }} /** @@ -1371,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(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); - }} - - /** * thenCombine result completes exceptionally if either source cancelled */ public void testThenCombine_sourceCancelled() { @@ -1428,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 */ @@ -1453,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); }} @@ -1488,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(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); - }} - - /** * thenAcceptBoth result completes exceptionally if either source cancelled */ public void testThenAcceptBoth_sourceCancelled() { @@ -1545,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 */ @@ -1570,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); }} @@ -1605,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(m); - - 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() { @@ -1665,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(m); + 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(m); + 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(m); + 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(m); - final IncFunction r2 = new IncFunction(m); 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); @@ -1782,740 +1808,540 @@ 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(m); - 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(m); - 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(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)); - } - - checkCompletedWithWrappedCancellationException(h); - (!fFirst ? f : g).complete(v1); - - assertEquals(0, r.invocationCount); - checkCompletedNormally(!fFirst ? f : g, v1); - checkCancelled(fFirst ? f : g); - checkCompletedWithWrappedCancellationException(h); - }} + final IncFunction[] rs = new IncFunction[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); - 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(m); - final IncFunction r2 = new IncFunction(m); - 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); - if (fFirst) { - f.complete(v1); - assertTrue(g.cancel(mayInterruptIfRunning)); - } else { - assertTrue(g.cancel(mayInterruptIfRunning)); - f.complete(v1); - } - final CompletableFuture h2 = m.applyToEither(j, k, r2); + 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); - // unspecified behavior + // 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), h1.join()); - assertEquals(1, r1.invocationCount); + assertEquals(inc(v1), h4.join()); + rs[4].assertValue(inc(v1)); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h1); - assertEquals(0, r1.invocationCount); + checkCompletedWithWrappedCancellationException(h4); + rs[4].assertNotInvoked(); } - try { - assertEquals(inc(v1), h2.join()); - assertEquals(1, r2.invocationCount); + assertEquals(inc(v1), h5.join()); + rs[5].assertValue(inc(v1)); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h2); - assertEquals(0, r2.invocationCount); + 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(); }} /** - * 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(m); - 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(m); - 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); - - 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(); + 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); - 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(m); - 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(m); - 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); - - assertTrue(f.cancel(mayInterruptIfRunning)); - checkCompletedWithWrappedCancellationException(h); - g.complete(v1); + final Noop[] rs = new Noop[6]; + for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); - checkCancelled(f); - assertEquals(0, r.invocationCount); - checkCompletedNormally(g, v1); - checkCompletedWithWrappedCancellationException(h); - }} + 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); - 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); - - 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(h4.join()); + rs[4].assertInvoked(); + } catch (CompletionException ok) { + checkCompletedWithWrappedCancellationException(h4); + rs[4].assertNotInvoked(); + } try { - assertNull(h.join()); - assertEquals(1, r.invocationCount); + assertNull(h5.join()); + rs[5].assertInvoked(); } catch (CompletionException ok) { - checkCompletedWithWrappedCancellationException(h); - assertEquals(0, r.invocationCount); + 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(); }} /** @@ -2534,7 +2360,7 @@ public class CompletableFutureTest exten checkCompletedNormally(g, inc(v1)); checkCompletedNormally(f, v1); - assertEquals(1, r.invocationCount); + r.assertInvoked(); }} /** @@ -2554,7 +2380,7 @@ public class CompletableFutureTest exten checkCompletedWithWrappedCFException(g, ex); checkCompletedWithWrappedCFException(f, ex); - assertEquals(0, r.invocationCount); + r.assertNotInvoked(); }} /**