--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/03 03:54:14 1.59 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/04 04:34:49 1.61 @@ -320,20 +320,6 @@ 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()); - } - - // A function that handles and produces null values as well. - static Integer inc(Integer x) { - return (x == null) ? null : x + 1; - } - static final class IntegerSupplier implements Supplier { final ExecutionMode m; int invocationCount = 0; @@ -348,7 +334,12 @@ public class CompletableFutureTest exten return value; } } - + + // A function that handles and produces null values as well. + static Integer inc(Integer x) { + return (x == null) ? null : x + 1; + } + static final class IncAction implements Consumer { int invocationCount = 0; Integer value; @@ -368,6 +359,15 @@ public class CompletableFutureTest exten return value = inc(x); } } + + // 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()); + } + static final class SubtractAction implements BiConsumer { final ExecutionMode m; int invocationCount = 0; @@ -392,6 +392,7 @@ public class CompletableFutureTest exten return value = subtract(x, y); } } + static final class Noop implements Runnable { final ExecutionMode m; int invocationCount = 0; @@ -505,11 +506,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) { @@ -1273,46 +1275,46 @@ public class CompletableFutureTest exten }} /** - * 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 IncAction r = new IncAction(); + 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); + assertEquals(0, r.invocationCount); }} /** - * 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); }} /** @@ -1377,33 +1379,6 @@ public class CompletableFutureTest exten }} /** - * 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() { @@ -1433,6 +1408,33 @@ public class CompletableFutureTest exten }} /** + * 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 */ @@ -1494,33 +1496,6 @@ public class CompletableFutureTest exten }} /** - * 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() { @@ -1550,6 +1525,33 @@ public class CompletableFutureTest exten }} /** + * 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 */ @@ -1611,35 +1613,6 @@ public class CompletableFutureTest exten }} /** - * 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() { @@ -1670,6 +1643,35 @@ public class CompletableFutureTest exten }} /** + * 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); + }} + + /** * applyToEither result completes normally after normal completion * of either source */