--- jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 06:07:34 1.45 +++ jsr166/src/test/tck/CompletableFutureTest.java 2014/06/02 17:41:22 1.46 @@ -247,6 +247,8 @@ public class CompletableFutureTest exten f = new CompletableFuture<>(); f.obtrudeValue(three); checkCompletedNormally(f, three); + f.obtrudeValue(null); + checkCompletedNormally(f, null); f = new CompletableFuture<>(); f.completeExceptionally(new CFException()); f.obtrudeValue(four); @@ -279,16 +281,16 @@ public class CompletableFutureTest exten */ public void testGetNumberOfDependents() { CompletableFuture f = new CompletableFuture<>(); - assertEquals(f.getNumberOfDependents(), 0); + assertEquals(0, f.getNumberOfDependents()); CompletableFuture g = f.thenRun(new Noop()); - assertEquals(f.getNumberOfDependents(), 1); - assertEquals(g.getNumberOfDependents(), 0); + assertEquals(1, f.getNumberOfDependents()); + assertEquals(0, g.getNumberOfDependents()); CompletableFuture h = f.thenRun(new Noop()); - assertEquals(f.getNumberOfDependents(), 2); + assertEquals(2, f.getNumberOfDependents()); f.complete(1); checkCompletedNormally(g, null); - assertEquals(f.getNumberOfDependents(), 0); - assertEquals(g.getNumberOfDependents(), 0); + assertEquals(0, f.getNumberOfDependents()); + assertEquals(0, g.getNumberOfDependents()); } /** @@ -450,24 +452,39 @@ public class CompletableFutureTest exten } } - static final class ExceptionToInteger implements Function { - public Integer apply(Throwable x) { return Integer.valueOf(3); } - } - - static final class IntegerHandler implements BiFunction { - int invocationCount = 0; - public Integer apply(Integer x, Throwable t) { - invocationCount++; - return (t == null) ? two : three; - } - } - /** * Permits the testing of parallel code for the 3 different * execution modes without repeating all the testing code. */ enum ExecutionMode { DEFAULT { + public CompletableFuture thenRun + (CompletableFuture f, Runnable a) { + return f.thenRun(a); + } + public CompletableFuture thenAccept + (CompletableFuture f, Consumer a) { + return f.thenAccept(a); + } + public CompletableFuture thenApply + (CompletableFuture f, Function a) { + return f.thenApply(a); + } + public CompletableFuture thenCompose + (CompletableFuture f, + Function> a) { + return f.thenCompose(a); + } + public CompletableFuture handle + (CompletableFuture f, + BiFunction a) { + return f.handle(a); + } + public CompletableFuture whenComplete + (CompletableFuture f, + BiConsumer a) { + return f.whenComplete(a); + } public CompletableFuture runAfterBoth (CompletableFuture f, CompletableFuture g, Runnable a) { return f.runAfterBoth(g, a); @@ -484,11 +501,11 @@ public class CompletableFutureTest exten BiFunction a) { return f.thenCombine(g, a); } - public CompletableFuture applyToEither + public CompletableFuture runAfterEither (CompletableFuture f, - CompletionStage g, - Function a) { - return f.applyToEither(g, a); + CompletionStage g, + java.lang.Runnable a) { + return f.runAfterEither(g, a); } public CompletableFuture acceptEither (CompletableFuture f, @@ -496,25 +513,42 @@ public class CompletableFutureTest exten Consumer a) { return f.acceptEither(g, a); } - public CompletableFuture runAfterEither + public CompletableFuture applyToEither (CompletableFuture f, - CompletionStage g, - java.lang.Runnable a) { - return f.runAfterEither(g, a); + CompletionStage g, + Function a) { + return f.applyToEither(g, a); + } + }, + + DEFAULT_ASYNC { + public CompletableFuture thenRun + (CompletableFuture f, Runnable a) { + return f.thenRunAsync(a); + } + public CompletableFuture thenAccept + (CompletableFuture f, Consumer a) { + return f.thenAcceptAsync(a); + } + public CompletableFuture thenApply + (CompletableFuture f, Function a) { + return f.thenApplyAsync(a); } public CompletableFuture thenCompose (CompletableFuture f, Function> a) { - return f.thenCompose(a); + return f.thenComposeAsync(a); + } + public CompletableFuture handle + (CompletableFuture f, + BiFunction a) { + return f.handleAsync(a); } public CompletableFuture whenComplete (CompletableFuture f, BiConsumer a) { - return f.whenComplete(a); + return f.whenCompleteAsync(a); } - }, - - DEFAULT_ASYNC { public CompletableFuture runAfterBoth (CompletableFuture f, CompletableFuture g, Runnable a) { return f.runAfterBothAsync(g, a); @@ -531,11 +565,11 @@ public class CompletableFutureTest exten BiFunction a) { return f.thenCombineAsync(g, a); } - public CompletableFuture applyToEither + public CompletableFuture runAfterEither (CompletableFuture f, - CompletionStage g, - Function a) { - return f.applyToEitherAsync(g, a); + CompletionStage g, + java.lang.Runnable a) { + return f.runAfterEitherAsync(g, a); } public CompletableFuture acceptEither (CompletableFuture f, @@ -543,25 +577,42 @@ public class CompletableFutureTest exten Consumer a) { return f.acceptEitherAsync(g, a); } - public CompletableFuture runAfterEither + public CompletableFuture applyToEither (CompletableFuture f, - CompletionStage g, - java.lang.Runnable a) { - return f.runAfterEitherAsync(g, a); + CompletionStage g, + Function a) { + return f.applyToEitherAsync(g, a); + } + }, + + EXECUTOR { + public CompletableFuture thenRun + (CompletableFuture f, Runnable a) { + return f.thenRunAsync(a, new ThreadExecutor()); + } + public CompletableFuture thenAccept + (CompletableFuture f, Consumer a) { + return f.thenAcceptAsync(a, new ThreadExecutor()); + } + public CompletableFuture thenApply + (CompletableFuture f, Function a) { + return f.thenApplyAsync(a, new ThreadExecutor()); } public CompletableFuture thenCompose (CompletableFuture f, Function> a) { - return f.thenComposeAsync(a); + return f.thenComposeAsync(a, new ThreadExecutor()); + } + public CompletableFuture handle + (CompletableFuture f, + BiFunction a) { + return f.handleAsync(a, new ThreadExecutor()); } public CompletableFuture whenComplete (CompletableFuture f, BiConsumer a) { - return f.whenCompleteAsync(a); + return f.whenCompleteAsync(a, new ThreadExecutor()); } - }, - - EXECUTOR { public CompletableFuture runAfterBoth (CompletableFuture f, CompletableFuture g, Runnable a) { return f.runAfterBothAsync(g, a, new ThreadExecutor()); @@ -578,36 +629,41 @@ public class CompletableFutureTest exten BiFunction a) { return f.thenCombineAsync(g, a, new ThreadExecutor()); } - public CompletableFuture applyToEither - (CompletableFuture f, - CompletionStage g, - Function a) { - return f.applyToEitherAsync(g, a, new ThreadExecutor()); - } - public CompletableFuture acceptEither - (CompletableFuture f, - CompletionStage g, - Consumer a) { - return f.acceptEitherAsync(g, a, new ThreadExecutor()); - } public CompletableFuture runAfterEither (CompletableFuture f, CompletionStage g, java.lang.Runnable a) { return f.runAfterEitherAsync(g, a, new ThreadExecutor()); } - public CompletableFuture thenCompose + public CompletableFuture acceptEither (CompletableFuture f, - Function> a) { - return f.thenComposeAsync(a, new ThreadExecutor()); + CompletionStage g, + Consumer a) { + return f.acceptEitherAsync(g, a, new ThreadExecutor()); } - public CompletableFuture whenComplete + public CompletableFuture applyToEither (CompletableFuture f, - BiConsumer a) { - return f.whenCompleteAsync(a, new ThreadExecutor()); + CompletionStage g, + Function a) { + return f.applyToEitherAsync(g, a, new ThreadExecutor()); } }; + public abstract CompletableFuture thenRun + (CompletableFuture f, Runnable a); + public abstract CompletableFuture thenAccept + (CompletableFuture f, Consumer a); + public abstract CompletableFuture thenApply + (CompletableFuture f, Function a); + public abstract CompletableFuture thenCompose + (CompletableFuture f, + Function> a); + public abstract CompletableFuture handle + (CompletableFuture f, + BiFunction a); + public abstract CompletableFuture whenComplete + (CompletableFuture f, + BiConsumer a); public abstract CompletableFuture runAfterBoth (CompletableFuture f, CompletableFuture g, Runnable a); public abstract CompletableFuture thenAcceptBoth @@ -618,81 +674,223 @@ public class CompletableFutureTest exten (CompletableFuture f, CompletionStage g, BiFunction a); - public abstract CompletableFuture applyToEither - (CompletableFuture f, - CompletionStage g, - Function a); - public abstract CompletableFuture acceptEither - (CompletableFuture f, - CompletionStage g, - Consumer a); public abstract CompletableFuture runAfterEither (CompletableFuture f, CompletionStage g, java.lang.Runnable a); - public abstract CompletableFuture thenCompose + public abstract CompletableFuture acceptEither (CompletableFuture f, - Function> a); - public abstract CompletableFuture whenComplete + CompletionStage g, + Consumer a); + public abstract CompletableFuture applyToEither (CompletableFuture f, - BiConsumer a); + CompletionStage g, + Function a); + } + /** + * exceptionally action is not invoked when source completes + * normally, and source result is propagated + */ + public void testExceptionally_normalCompletion() { + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final AtomicInteger a = new AtomicInteger(0); + final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = f.exceptionally + ((Throwable t) -> { + // Should not be called + a.getAndIncrement(); + throw new AssertionError(); + }); + if (createIncomplete) f.complete(v1); + + checkCompletedNormally(g, v1); + checkCompletedNormally(f, v1); + assertEquals(0, a.get()); + }} - } /** * exceptionally action completes with function value on source - * exception; otherwise with source value + * exception */ - public void testExceptionally() { - CompletableFuture f = new CompletableFuture<>(); - ExceptionToInteger r = new ExceptionToInteger(); - CompletableFuture g = f.exceptionally(r); - f.completeExceptionally(new CFException()); - checkCompletedNormally(g, three); + public void testExceptionally_exceptionalCompletion() { + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final AtomicInteger a = new AtomicInteger(0); + final CFException ex = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) f.completeExceptionally(ex); + final CompletableFuture g = f.exceptionally + ((Throwable t) -> { + threadAssertSame(t, ex); + a.getAndIncrement(); + return v1; + }); + if (createIncomplete) f.completeExceptionally(ex); - f = new CompletableFuture<>(); - r = new ExceptionToInteger(); - g = f.exceptionally(r); - f.complete(one); - checkCompletedNormally(g, one); - } + checkCompletedNormally(g, v1); + assertEquals(1, a.get()); + }} + + public void testExceptionally_exceptionalCompletionActionFailed() { + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final AtomicInteger a = new AtomicInteger(0); + final CFException ex1 = new CFException(); + final CFException ex2 = new CFException(); + final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) f.completeExceptionally(ex1); + final CompletableFuture g = f.exceptionally + ((Throwable t) -> { + threadAssertSame(t, ex1); + a.getAndIncrement(); + throw ex2; + }); + if (createIncomplete) f.completeExceptionally(ex1); + + checkCompletedWithWrappedCFException(g, ex2); + assertEquals(1, a.get()); + }} /** - * handle action completes normally with function value on either - * normal or exceptional completion of source + * handle action completes normally with function value on normal + * completion of source */ - public void testHandle() { - CompletableFuture f, g; - IntegerHandler r; + public void testHandle_normalCompletion() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final AtomicInteger a = new AtomicInteger(0); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = m.handle + (f, + (Integer x, Throwable t) -> { + threadAssertSame(x, v1); + threadAssertNull(t); + a.getAndIncrement(); + return inc(v1); + }); + if (createIncomplete) f.complete(v1); - f = new CompletableFuture<>(); - f.completeExceptionally(new CFException()); - g = f.handle(r = new IntegerHandler()); - assertEquals(1, r.invocationCount); - assertEquals(1, r.invocationCount); - checkCompletedNormally(g, three); + checkCompletedNormally(g, inc(v1)); + checkCompletedNormally(f, v1); + assertEquals(1, a.get()); + }} - f = new CompletableFuture<>(); - g = f.handle(r = new IntegerHandler()); - assertEquals(0, r.invocationCount); - f.completeExceptionally(new CFException()); - checkCompletedNormally(g, three); - assertEquals(1, r.invocationCount); + /** + * handle action completes normally with function value on + * exceptional completion of source + */ + public void testHandle_exceptionalCompletion() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final AtomicInteger a = new AtomicInteger(0); + final CFException ex = new CFException(); + if (!createIncomplete) f.completeExceptionally(ex); + final CompletableFuture g = m.handle + (f, + (Integer x, Throwable t) -> { + threadAssertNull(x); + threadAssertSame(t, ex); + a.getAndIncrement(); + return v1; + }); + if (createIncomplete) f.completeExceptionally(ex); - f = new CompletableFuture<>(); - f.complete(one); - g = f.handle(r = new IntegerHandler()); - assertEquals(1, r.invocationCount); - checkCompletedNormally(g, two); + checkCompletedNormally(g, v1); + checkCompletedWithWrappedCFException(f, ex); + assertEquals(1, a.get()); + }} - f = new CompletableFuture<>(); - g = f.handle(r = new IntegerHandler()); - assertEquals(0, r.invocationCount); - f.complete(one); - assertEquals(1, r.invocationCount); - checkCompletedNormally(g, two); - } + /** + * handle action completes normally with function value on + * cancelled source + */ + public void testHandle_sourceCancelled() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final AtomicInteger a = new AtomicInteger(0); + if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); + final CompletableFuture g = m.handle + (f, + (Integer x, Throwable t) -> { + threadAssertNull(x); + threadAssertTrue(t instanceof CancellationException); + a.getAndIncrement(); + return v1; + }); + if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); + + checkCompletedNormally(g, v1); + checkCancelled(f); + assertEquals(1, a.get()); + }} + + /** + * handle result completes exceptionally if action does + */ + public void testHandle_sourceFailedActionFailed() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + { + final CompletableFuture f = new CompletableFuture<>(); + final AtomicInteger a = new AtomicInteger(0); + final CFException ex1 = new CFException(); + final CFException ex2 = new CFException(); + if (!createIncomplete) f.completeExceptionally(ex1); + final CompletableFuture g = m.handle + (f, + (Integer x, Throwable t) -> { + threadAssertNull(x); + threadAssertSame(ex1, t); + a.getAndIncrement(); + throw ex2; + }); + if (createIncomplete) f.completeExceptionally(ex1); + + checkCompletedWithWrappedCFException(g, ex2); + checkCompletedWithWrappedCFException(f, ex1); + assertEquals(1, a.get()); + }} + + public void testHandle_sourceCompletedNormallyActionFailed() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final CompletableFuture f = new CompletableFuture<>(); + final AtomicInteger a = new AtomicInteger(0); + final CFException ex = new CFException(); + if (!createIncomplete) f.complete(v1); + final CompletableFuture g = m.handle + (f, + (Integer x, Throwable t) -> { + threadAssertSame(x, v1); + threadAssertNull(t); + a.getAndIncrement(); + throw ex; + }); + if (createIncomplete) f.complete(v1); + + checkCompletedWithWrappedCFException(g, ex); + checkCompletedNormally(f, v1); + assertEquals(1, a.get()); + }} /** * runAsync completes after running Runnable @@ -1199,7 +1397,7 @@ public class CompletableFutureTest exten g.complete(v2); checkCompletedNormally(h, null); - assertEquals(r.value, subtract(v1, v2)); + assertEquals(subtract(v1, v2), r.value); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1221,7 +1419,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCompletedNormally(h, null); - assertEquals(r.value, subtract(v1, v2)); + assertEquals(subtract(v1, v2), r.value); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1241,7 +1439,7 @@ public class CompletableFutureTest exten final CompletableFuture h = m.thenAcceptBoth(f, g, r); checkCompletedNormally(h, null); - assertEquals(r.value, subtract(v1, v2)); + assertEquals(subtract(v1, v2), r.value); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1261,7 +1459,7 @@ public class CompletableFutureTest exten final CompletableFuture h = m.thenAcceptBoth(f, g, r); checkCompletedNormally(h, null); - assertEquals(r.value, subtract(v1, v2)); + assertEquals(subtract(v1, v2), r.value); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); } @@ -1911,7 +2109,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), inc(v1)); + assertEquals(inc(v1), h.join()); assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); @@ -1939,7 +2137,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), inc(v1)); + assertEquals(inc(v1), h.join()); assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); @@ -2051,7 +2249,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), inc(v1)); + assertEquals(inc(v1), h.join()); assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); @@ -2079,7 +2277,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), inc(v1)); + assertEquals(inc(v1), h.join()); assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); @@ -2107,7 +2305,7 @@ public class CompletableFutureTest exten f.complete(v1); checkCompletedNormally(h, null); - assertEquals(r.value, inc(v1)); + assertEquals(inc(v1), r.value); g.complete(v2); checkCompletedNormally(f, v1); @@ -2128,7 +2326,7 @@ public class CompletableFutureTest exten g.complete(v2); checkCompletedNormally(h, null); - assertEquals(r.value, inc(v2)); + assertEquals(inc(v2), r.value); f.complete(v1); checkCompletedNormally(f, v1); @@ -2221,7 +2419,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), null); + assertNull(h.join()); assertEquals(1, r.invocationCount); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { @@ -2250,7 +2448,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), null); + assertNull(h.join()); assertEquals(1, r.invocationCount); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { @@ -2363,7 +2561,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), null); + assertNull(h.join()); assertEquals(1, r.invocationCount); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { @@ -2392,7 +2590,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), null); + assertNull(h.join()); assertEquals(1, r.invocationCount); assertEquals(inc(v1), r.value); } catch (CompletionException ok) { @@ -2534,7 +2732,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), null); + assertNull(h.join()); assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); @@ -2562,7 +2760,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), null); + assertNull(h.join()); assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCFException(h, ex); @@ -2674,7 +2872,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), null); + assertNull(h.join()); assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); @@ -2702,7 +2900,7 @@ public class CompletableFutureTest exten // unspecified behavior Integer v; try { - assertEquals(h.join(), null); + assertNull(h.join()); assertEquals(1, r.invocationCount); } catch (CompletionException ok) { checkCompletedWithWrappedCancellationException(h); @@ -3328,37 +3526,37 @@ public class CompletableFutureTest exten * source result. */ public void testWhenComplete_normalCompletion1() { - for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) { - - final AtomicInteger a = new AtomicInteger(); + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final AtomicInteger a = new AtomicInteger(0); final CompletableFuture f = new CompletableFuture<>(); if (!createIncomplete) f.complete(v1); - final CompletableFuture g = - m.whenComplete(f, - (Integer x, Throwable t) -> { - threadAssertSame(x, v1); - threadAssertNull(t); - a.getAndIncrement(); - }); + final CompletableFuture g = m.whenComplete + (f, + (Integer x, Throwable t) -> { + threadAssertSame(x, v1); + threadAssertNull(t); + a.getAndIncrement(); + }); if (createIncomplete) f.complete(v1); - checkCompletedNormally(f, v1); + checkCompletedNormally(g, v1); - assertEquals(a.get(), 1); - } - } + checkCompletedNormally(f, v1); + assertEquals(1, a.get()); + }} /** * whenComplete action executes on exceptional completion, propagating * source result. */ public void testWhenComplete_exceptionalCompletion() { - for (boolean createIncomplete : new boolean[] { true, false }) for (ExecutionMode m : ExecutionMode.values()) - for (Integer v1 : new Integer[] { 1, null }) { - - final AtomicInteger a = new AtomicInteger(); + for (boolean createIncomplete : new boolean[] { true, false }) + for (Integer v1 : new Integer[] { 1, null }) + { + final AtomicInteger a = new AtomicInteger(0); final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); if (!createIncomplete) f.completeExceptionally(ex); @@ -3372,9 +3570,35 @@ public class CompletableFutureTest exten if (createIncomplete) f.completeExceptionally(ex); checkCompletedWithWrappedCFException(f, ex); checkCompletedWithWrappedCFException(g, ex); - assertEquals(a.get(), 1); - } - } + assertEquals(1, a.get()); + }} + + /** + * whenComplete action executes on cancelled source, propagating + * CancellationException. + */ + public void testWhenComplete_sourceCancelled() { + for (ExecutionMode m : ExecutionMode.values()) + for (boolean mayInterruptIfRunning : new boolean[] { true, false }) + for (boolean createIncomplete : new boolean[] { true, false }) + { + final AtomicInteger a = new AtomicInteger(0); + final CompletableFuture f = new CompletableFuture<>(); + if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); + final CompletableFuture g = m.whenComplete + (f, + (Integer x, Throwable t) -> { + threadAssertNull(x); + threadAssertTrue(t instanceof CancellationException); + a.getAndIncrement(); + }); + if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); + + //try { g.join(); } catch (Throwable t) { throw new Error(t); } + checkCompletedWithWrappedCancellationException(g); + checkCancelled(f); + assertEquals(1, a.get()); + }} /** * If a whenComplete action throws an exception when triggered by @@ -3385,6 +3609,7 @@ public class CompletableFutureTest exten for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { + final AtomicInteger a = new AtomicInteger(0); final CFException ex = new CFException(); final CompletableFuture f = new CompletableFuture<>(); if (!createIncomplete) f.complete(v1); @@ -3393,11 +3618,13 @@ public class CompletableFutureTest exten (Integer x, Throwable t) -> { threadAssertSame(x, v1); threadAssertNull(t); + a.getAndIncrement(); throw ex; }); if (createIncomplete) f.complete(v1); checkCompletedNormally(f, v1); checkCompletedWithWrappedCFException(g, ex); + assertEquals(1, a.get()); } } @@ -3411,6 +3638,7 @@ public class CompletableFutureTest exten for (ExecutionMode m : ExecutionMode.values()) for (Integer v1 : new Integer[] { 1, null }) { + final AtomicInteger a = new AtomicInteger(0); final CFException ex1 = new CFException(); final CFException ex2 = new CFException(); final CompletableFuture f = new CompletableFuture<>(); @@ -3421,97 +3649,15 @@ public class CompletableFutureTest exten (Integer x, Throwable t) -> { threadAssertSame(t, ex1); threadAssertNull(x); + a.getAndIncrement(); throw ex2; }); if (createIncomplete) f.completeExceptionally(ex1); checkCompletedWithWrappedCFException(f, ex1); checkCompletedWithWrappedCFException(g, ex1); + assertEquals(1, a.get()); } } - /** - * handleAsync action completes normally with function value on - * either normal or exceptional completion of source - */ - public void testHandleAsync() { - CompletableFuture f, g; - IntegerHandler r; - - f = new CompletableFuture<>(); - g = f.handleAsync(r = new IntegerHandler()); - assertEquals(0, r.invocationCount); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(f); - checkCompletedNormally(g, three); - assertEquals(1, r.invocationCount); - - f = new CompletableFuture<>(); - g = f.handleAsync(r = new IntegerHandler()); - assertEquals(0, r.invocationCount); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(f); - checkCompletedNormally(g, three); - assertEquals(1, r.invocationCount); - - f = new CompletableFuture<>(); - g = f.handleAsync(r = new IntegerHandler()); - assertEquals(0, r.invocationCount); - f.complete(one); - checkCompletedNormally(f, one); - checkCompletedNormally(g, two); - assertEquals(1, r.invocationCount); - - f = new CompletableFuture<>(); - g = f.handleAsync(r = new IntegerHandler()); - assertEquals(0, r.invocationCount); - f.complete(one); - checkCompletedNormally(f, one); - checkCompletedNormally(g, two); - assertEquals(1, r.invocationCount); - } - - /** - * handleAsync action with Executor completes normally with - * function value on either normal or exceptional completion of - * source - */ - public void testHandleAsync2() { - CompletableFuture f, g; - ThreadExecutor exec = new ThreadExecutor(); - IntegerHandler r; - - f = new CompletableFuture<>(); - g = f.handleAsync(r = new IntegerHandler(), exec); - assertEquals(0, r.invocationCount); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(f); - checkCompletedNormally(g, three); - assertEquals(1, r.invocationCount); - - f = new CompletableFuture<>(); - g = f.handleAsync(r = new IntegerHandler(), exec); - assertEquals(0, r.invocationCount); - f.completeExceptionally(new CFException()); - checkCompletedWithWrappedCFException(f); - checkCompletedNormally(g, three); - assertEquals(1, r.invocationCount); - - f = new CompletableFuture<>(); - g = f.handleAsync(r = new IntegerHandler(), exec); - assertEquals(0, r.invocationCount); - f.complete(one); - checkCompletedNormally(f, one); - checkCompletedNormally(g, two); - assertEquals(1, r.invocationCount); - - f = new CompletableFuture<>(); - g = f.handleAsync(r = new IntegerHandler(), exec); - assertEquals(0, r.invocationCount); - f.complete(one); - checkCompletedNormally(f, one); - checkCompletedNormally(g, two); - assertEquals(1, r.invocationCount); - } - }