--- jsr166/src/test/tck/CompletableFutureTest.java 2016/03/28 19:18:16 1.140 +++ jsr166/src/test/tck/CompletableFutureTest.java 2016/06/26 23:45:46 1.156 @@ -30,6 +30,7 @@ import java.util.concurrent.ExecutionExc import java.util.concurrent.Executor; import java.util.concurrent.ForkJoinPool; import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.RejectedExecutionException; import java.util.concurrent.TimeoutException; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; @@ -459,62 +460,68 @@ public class CompletableFutureTest exten class FailingSupplier extends CheckedAction implements Supplier { - FailingSupplier(ExecutionMode m) { super(m); } + final CFException ex; + FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } public Integer get() { invoked(); - throw new CFException(); + throw ex; } } class FailingConsumer extends CheckedIntegerAction implements Consumer { - FailingConsumer(ExecutionMode m) { super(m); } + final CFException ex; + FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } public void accept(Integer x) { invoked(); value = x; - throw new CFException(); + throw ex; } } class FailingBiConsumer extends CheckedIntegerAction implements BiConsumer { - FailingBiConsumer(ExecutionMode m) { super(m); } + final CFException ex; + FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } public void accept(Integer x, Integer y) { invoked(); value = subtract(x, y); - throw new CFException(); + throw ex; } } class FailingFunction extends CheckedIntegerAction implements Function { - FailingFunction(ExecutionMode m) { super(m); } + final CFException ex; + FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } public Integer apply(Integer x) { invoked(); value = x; - throw new CFException(); + throw ex; } } class FailingBiFunction extends CheckedIntegerAction implements BiFunction { - FailingBiFunction(ExecutionMode m) { super(m); } + final CFException ex; + FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } public Integer apply(Integer x, Integer y) { invoked(); value = subtract(x, y); - throw new CFException(); + throw ex; } } class FailingRunnable extends CheckedAction implements Runnable { - FailingRunnable(ExecutionMode m) { super(m); } + final CFException ex; + FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } public void run() { invoked(); - throw new CFException(); + throw ex; } } @@ -534,11 +541,21 @@ public class CompletableFutureTest exten class FailingCompletableFutureFunction extends CheckedIntegerAction implements Function> { - FailingCompletableFutureFunction(ExecutionMode m) { super(m); } + final CFException ex; + FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } public CompletableFuture apply(Integer x) { invoked(); value = x; - throw new CFException(); + throw ex; + } + } + + static class CountingRejectingExecutor implements Executor { + final RejectedExecutionException ex = new RejectedExecutionException(); + final AtomicInteger count = new AtomicInteger(0); + public void execute(Runnable r) { + count.getAndIncrement(); + throw ex; } } @@ -1222,10 +1239,22 @@ public class CompletableFutureTest exten { final FailingRunnable r = new FailingRunnable(m); final CompletableFuture f = m.runAsync(r); - checkCompletedWithWrappedCFException(f); + checkCompletedWithWrappedException(f, r.ex); r.assertInvoked(); }} + public void testRunAsync_rejectingExecutor() { + CountingRejectingExecutor e = new CountingRejectingExecutor(); + try { + CompletableFuture.runAsync(() -> {}, e); + shouldThrow(); + } catch (Throwable t) { + assertSame(e.ex, t); + } + + assertEquals(1, e.count.get()); + } + /** * supplyAsync completes with result of supplier */ @@ -1256,10 +1285,22 @@ public class CompletableFutureTest exten { FailingSupplier r = new FailingSupplier(m); CompletableFuture f = m.supplyAsync(r); - checkCompletedWithWrappedCFException(f); + checkCompletedWithWrappedException(f, r.ex); r.assertInvoked(); }} + public void testSupplyAsync_rejectingExecutor() { + CountingRejectingExecutor e = new CountingRejectingExecutor(); + try { + CompletableFuture.supplyAsync(() -> null, e); + shouldThrow(); + } catch (Throwable t) { + assertSame(e.ex, t); + } + + assertEquals(1, e.count.get()); + } + // seq completion methods /** @@ -1378,12 +1419,12 @@ public class CompletableFutureTest exten final CompletableFuture h4 = m.runAfterBoth(f, f, rs[4]); final CompletableFuture h5 = m.runAfterEither(f, f, rs[5]); - checkCompletedWithWrappedCFException(h0); - checkCompletedWithWrappedCFException(h1); - checkCompletedWithWrappedCFException(h2); - checkCompletedWithWrappedCFException(h3); - checkCompletedWithWrappedCFException(h4); - checkCompletedWithWrappedCFException(h5); + checkCompletedWithWrappedException(h0, rs[0].ex); + checkCompletedWithWrappedException(h1, rs[1].ex); + checkCompletedWithWrappedException(h2, rs[2].ex); + checkCompletedWithWrappedException(h3, rs[3].ex); + checkCompletedWithWrappedException(h4, rs[4].ex); + checkCompletedWithWrappedException(h5, rs[5].ex); checkCompletedNormally(f, v1); }} @@ -1482,10 +1523,10 @@ public class CompletableFutureTest exten final CompletableFuture h2 = m.thenApply(f, rs[2]); final CompletableFuture h3 = m.applyToEither(f, f, rs[3]); - checkCompletedWithWrappedCFException(h0); - checkCompletedWithWrappedCFException(h1); - checkCompletedWithWrappedCFException(h2); - checkCompletedWithWrappedCFException(h3); + checkCompletedWithWrappedException(h0, rs[0].ex); + checkCompletedWithWrappedException(h1, rs[1].ex); + checkCompletedWithWrappedException(h2, rs[2].ex); + checkCompletedWithWrappedException(h3, rs[3].ex); checkCompletedNormally(f, v1); }} @@ -1584,10 +1625,10 @@ public class CompletableFutureTest exten final CompletableFuture h2 = m.thenAccept(f, rs[2]); final CompletableFuture h3 = m.acceptEither(f, f, rs[3]); - checkCompletedWithWrappedCFException(h0); - checkCompletedWithWrappedCFException(h1); - checkCompletedWithWrappedCFException(h2); - checkCompletedWithWrappedCFException(h3); + checkCompletedWithWrappedException(h0, rs[0].ex); + checkCompletedWithWrappedException(h1, rs[1].ex); + checkCompletedWithWrappedException(h2, rs[2].ex); + checkCompletedWithWrappedException(h3, rs[3].ex); checkCompletedNormally(f, v1); }} @@ -1749,9 +1790,9 @@ public class CompletableFutureTest exten assertTrue(snd.complete(w2)); final CompletableFuture h3 = m.thenCombine(f, g, r3); - checkCompletedWithWrappedCFException(h1); - checkCompletedWithWrappedCFException(h2); - checkCompletedWithWrappedCFException(h3); + checkCompletedWithWrappedException(h1, r1.ex); + checkCompletedWithWrappedException(h2, r2.ex); + checkCompletedWithWrappedException(h3, r3.ex); r1.assertInvoked(); r2.assertInvoked(); r3.assertInvoked(); @@ -1913,9 +1954,9 @@ public class CompletableFutureTest exten assertTrue(snd.complete(w2)); final CompletableFuture h3 = m.thenAcceptBoth(f, g, r3); - checkCompletedWithWrappedCFException(h1); - checkCompletedWithWrappedCFException(h2); - checkCompletedWithWrappedCFException(h3); + checkCompletedWithWrappedException(h1, r1.ex); + checkCompletedWithWrappedException(h2, r2.ex); + checkCompletedWithWrappedException(h3, r3.ex); r1.assertInvoked(); r2.assertInvoked(); r3.assertInvoked(); @@ -2077,9 +2118,9 @@ public class CompletableFutureTest exten assertTrue(snd.complete(w2)); final CompletableFuture h3 = m.runAfterBoth(f, g, r3); - checkCompletedWithWrappedCFException(h1); - checkCompletedWithWrappedCFException(h2); - checkCompletedWithWrappedCFException(h3); + checkCompletedWithWrappedException(h1, r1.ex); + checkCompletedWithWrappedException(h2, r2.ex); + checkCompletedWithWrappedException(h3, r3.ex); r1.assertInvoked(); r2.assertInvoked(); r3.assertInvoked(); @@ -2369,10 +2410,10 @@ public class CompletableFutureTest exten f.complete(v1); 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); + checkCompletedWithWrappedException(h0, rs[0].ex); + checkCompletedWithWrappedException(h1, rs[1].ex); + checkCompletedWithWrappedException(h2, rs[2].ex); + checkCompletedWithWrappedException(h3, rs[3].ex); for (int i = 0; i < 4; i++) rs[i].assertValue(v1); g.complete(v2); @@ -2381,10 +2422,10 @@ public class CompletableFutureTest exten final CompletableFuture h4 = m.applyToEither(f, g, rs[4]); final CompletableFuture h5 = m.applyToEither(g, f, rs[5]); - checkCompletedWithWrappedCFException(h4); + checkCompletedWithWrappedException(h4, rs[4].ex); assertTrue(Objects.equals(v1, rs[4].value) || Objects.equals(v2, rs[4].value)); - checkCompletedWithWrappedCFException(h5); + checkCompletedWithWrappedException(h5, rs[5].ex); assertTrue(Objects.equals(v1, rs[5].value) || Objects.equals(v2, rs[5].value)); @@ -2628,10 +2669,10 @@ public class CompletableFutureTest exten f.complete(v1); 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); + checkCompletedWithWrappedException(h0, rs[0].ex); + checkCompletedWithWrappedException(h1, rs[1].ex); + checkCompletedWithWrappedException(h2, rs[2].ex); + checkCompletedWithWrappedException(h3, rs[3].ex); for (int i = 0; i < 4; i++) rs[i].assertValue(v1); g.complete(v2); @@ -2640,10 +2681,10 @@ public class CompletableFutureTest exten final CompletableFuture h4 = m.acceptEither(f, g, rs[4]); final CompletableFuture h5 = m.acceptEither(g, f, rs[5]); - checkCompletedWithWrappedCFException(h4); + checkCompletedWithWrappedException(h4, rs[4].ex); assertTrue(Objects.equals(v1, rs[4].value) || Objects.equals(v2, rs[4].value)); - checkCompletedWithWrappedCFException(h5); + checkCompletedWithWrappedException(h5, rs[5].ex); assertTrue(Objects.equals(v1, rs[5].value) || Objects.equals(v2, rs[5].value)); @@ -2883,16 +2924,16 @@ public class CompletableFutureTest exten assertTrue(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); + checkCompletedWithWrappedException(h0, rs[0].ex); + checkCompletedWithWrappedException(h1, rs[1].ex); + checkCompletedWithWrappedException(h2, rs[2].ex); + checkCompletedWithWrappedException(h3, rs[3].ex); for (int i = 0; i < 4; i++) rs[i].assertInvoked(); assertTrue(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); + checkCompletedWithWrappedException(h4, rs[4].ex); + checkCompletedWithWrappedException(h5, rs[5].ex); checkCompletedNormally(f, v1); checkCompletedNormally(g, v2); @@ -2953,7 +2994,7 @@ public class CompletableFutureTest exten final CompletableFuture g = m.thenCompose(f, r); if (createIncomplete) assertTrue(f.complete(v1)); - checkCompletedWithWrappedCFException(g); + checkCompletedWithWrappedException(g, r.ex); checkCompletedNormally(f, v1); }} @@ -3062,7 +3103,7 @@ public class CompletableFutureTest exten } } - public void testAllOf_backwards() throws Exception { + public void testAllOf_normal_backwards() throws Exception { for (int k = 1; k < 10; k++) { CompletableFuture[] fs = (CompletableFuture[]) new CompletableFuture[k]; @@ -3295,7 +3336,7 @@ public class CompletableFutureTest exten () -> f.obtrudeException(null), () -> CompletableFuture.delayedExecutor(1L, SECONDS, null), - () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), + () -> CompletableFuture.delayedExecutor(1L, null, exec), () -> CompletableFuture.delayedExecutor(1L, null), () -> f.orTimeout(1L, null), @@ -3310,6 +3351,155 @@ public class CompletableFutureTest exten } /** + * Test submissions to an executor that rejects all tasks. + */ + public void testRejectingExecutor() { + for (Integer v : new Integer[] { 1, null }) { + + final CountingRejectingExecutor e = new CountingRejectingExecutor(); + + final CompletableFuture complete = CompletableFuture.completedFuture(v); + final CompletableFuture incomplete = new CompletableFuture<>(); + + List> futures = new ArrayList<>(); + + List> srcs = new ArrayList<>(); + srcs.add(complete); + srcs.add(incomplete); + + for (CompletableFuture src : srcs) { + List> fs = new ArrayList<>(); + fs.add(src.thenRunAsync(() -> {}, e)); + fs.add(src.thenAcceptAsync((z) -> {}, e)); + fs.add(src.thenApplyAsync((z) -> z, e)); + + fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); + fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); + fs.add(src.runAfterBothAsync(src, () -> {}, e)); + + fs.add(src.applyToEitherAsync(src, (z) -> z, e)); + fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); + fs.add(src.runAfterEitherAsync(src, () -> {}, e)); + + fs.add(src.thenComposeAsync((z) -> null, e)); + fs.add(src.whenCompleteAsync((z, t) -> {}, e)); + fs.add(src.handleAsync((z, t) -> null, e)); + + for (CompletableFuture future : fs) { + if (src.isDone()) + checkCompletedWithWrappedException(future, e.ex); + else + checkIncomplete(future); + } + futures.addAll(fs); + } + + { + List> fs = new ArrayList<>(); + + fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); + fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); + + fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); + fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); + + fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); + fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); + + for (CompletableFuture future : fs) + checkIncomplete(future); + futures.addAll(fs); + } + + { + List> fs = new ArrayList<>(); + + fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); + fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); + + fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); + fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); + + fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); + fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); + + for (CompletableFuture future : fs) + checkCompletedWithWrappedException(future, e.ex); + futures.addAll(fs); + } + + incomplete.complete(v); + + for (CompletableFuture future : futures) + checkCompletedWithWrappedException(future, e.ex); + + assertEquals(futures.size(), e.count.get()); + + } + } + + /** + * Test submissions to an executor that rejects all tasks, but + * should never be invoked because the dependent future is + * explicitly completed. + */ + public void testRejectingExecutorNeverInvoked() { + final CountingRejectingExecutor e = new CountingRejectingExecutor(); + + for (Integer v : new Integer[] { 1, null }) { + + final CompletableFuture complete = CompletableFuture.completedFuture(v); + final CompletableFuture incomplete = new CompletableFuture<>(); + + List> futures = new ArrayList<>(); + + List> srcs = new ArrayList<>(); + srcs.add(complete); + srcs.add(incomplete); + + List> fs = new ArrayList<>(); + fs.add(incomplete.thenRunAsync(() -> {}, e)); + fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); + fs.add(incomplete.thenApplyAsync((z) -> z, e)); + + fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); + fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); + fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); + + fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); + fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); + fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); + + fs.add(incomplete.thenComposeAsync((z) -> null, e)); + fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); + fs.add(incomplete.handleAsync((z, t) -> null, e)); + + fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); + fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); + + fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); + fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); + + fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); + fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); + + for (CompletableFuture future : fs) + checkIncomplete(future); + + for (CompletableFuture future : fs) + future.complete(null); + + incomplete.complete(v); + + for (CompletableFuture future : fs) + checkCompletedNormally(future, null); + + assertEquals(0, e.count.get()); + + } + } + + /** * toCompletableFuture returns this CompletableFuture. */ public void testToCompletableFuture() { @@ -3525,7 +3715,7 @@ public class CompletableFutureTest exten long timeoutMillis = timeoutMillis(); CompletableFuture f = new CompletableFuture<>(); long startTime = System.nanoTime(); - f.orTimeout(timeoutMillis, MILLISECONDS); + assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); checkCompletedWithTimeoutException(f); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); } @@ -3540,8 +3730,8 @@ public class CompletableFutureTest exten CompletableFuture g = new CompletableFuture<>(); long startTime = System.nanoTime(); f.complete(v1); - f.orTimeout(LONG_DELAY_MS, MILLISECONDS); - g.orTimeout(LONG_DELAY_MS, MILLISECONDS); + assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); + assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); g.complete(v1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v1); @@ -3556,11 +3746,14 @@ public class CompletableFutureTest exten () -> testCompleteOnTimeout_timesOut(null)); } + /** + * completeOnTimeout completes with given value if not complete + */ public void testCompleteOnTimeout_timesOut(Integer v) { long timeoutMillis = timeoutMillis(); CompletableFuture f = new CompletableFuture<>(); long startTime = System.nanoTime(); - f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); + assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); assertSame(v, f.join()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis); f.complete(99); // should have no effect @@ -3577,8 +3770,8 @@ public class CompletableFutureTest exten CompletableFuture g = new CompletableFuture<>(); long startTime = System.nanoTime(); f.complete(v1); - f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); - g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); + assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); + assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); g.complete(v1); checkCompletedNormally(f, v1); checkCompletedNormally(g, v1); @@ -3629,12 +3822,25 @@ public class CompletableFutureTest exten //--- tests of implementation details; not part of official tck --- Object resultOf(CompletableFuture f) { + SecurityManager sm = System.getSecurityManager(); + if (sm != null) { + try { + System.setSecurityManager(null); + } catch (SecurityException giveUp) { + return "Reflection not available"; + } + } + try { java.lang.reflect.Field resultField = CompletableFuture.class.getDeclaredField("result"); resultField.setAccessible(true); return resultField.get(f); - } catch (Throwable t) { throw new AssertionError(t); } + } catch (Throwable t) { + throw new AssertionError(t); + } finally { + if (sm != null) System.setSecurityManager(sm); + } } public void testExceptionPropagationReusesResultObject() { @@ -3657,15 +3863,22 @@ public class CompletableFutureTest exten funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); + funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); + funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); + funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); + funs.add((y) -> CompletableFuture.allOf(new CompletableFuture[] {y})); funs.add((y) -> CompletableFuture.allOf(new CompletableFuture[] {y, v42})); + funs.add((y) -> CompletableFuture.allOf(new CompletableFuture[] {v42, y})); + funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture[] {y})); funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture[] {y, incomplete})); + funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture[] {incomplete, y})); for (Function, CompletableFuture> fun : funs) { @@ -3927,26 +4140,77 @@ public class CompletableFutureTest exten /** * A single CompletableFuture with many dependents. + * A demo of scalability - runtime is O(n). */ public void testManyDependents() throws Throwable { - final int n = 10_000; + final int n = 1_000; final CompletableFuture head = new CompletableFuture<>(); - final CompletableFuture incomplete = new CompletableFuture<>(); final CompletableFuture complete = CompletableFuture.completedFuture((Void)null); final AtomicInteger count = new AtomicInteger(0); for (int i = 0; i < n; i++) { head.thenRun(() -> count.getAndIncrement()); head.thenAccept((x) -> count.getAndIncrement()); head.thenApply((x) -> count.getAndIncrement()); + head.runAfterBoth(complete, () -> count.getAndIncrement()); head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); head.thenCombine(complete, (x, y) -> count.getAndIncrement()); - head.runAfterEither(incomplete, () -> count.getAndIncrement()); - head.acceptEither(incomplete, (x) -> count.getAndIncrement()); - head.applyToEither(incomplete, (x) -> count.getAndIncrement()); + complete.runAfterBoth(head, () -> count.getAndIncrement()); + complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); + complete.thenCombine(head, (x, y) -> count.getAndIncrement()); + + head.runAfterEither(new CompletableFuture(), () -> count.getAndIncrement()); + head.acceptEither(new CompletableFuture(), (x) -> count.getAndIncrement()); + head.applyToEither(new CompletableFuture(), (x) -> count.getAndIncrement()); + new CompletableFuture().runAfterEither(head, () -> count.getAndIncrement()); + new CompletableFuture().acceptEither(head, (x) -> count.getAndIncrement()); + new CompletableFuture().applyToEither(head, (x) -> count.getAndIncrement()); } head.complete(null); - assertEquals(9 * n, count.get()); + assertEquals(5 * 3 * n, count.get()); + } + + /** a66 -Dvmoptions=-Xmx8m -Djsr166.tckTestClass=CompletableFutureTest tck */ + public void testCoCompletionGarbage() throws Throwable { + // final int n = 3_000_000; + final int n = 100; + final CompletableFuture incomplete = new CompletableFuture<>(); + CompletableFuture f; + for (int i = 0; i < n; i++) { + f = new CompletableFuture<>(); + f.runAfterEither(incomplete, () -> {}); + f.complete(null); + + f = new CompletableFuture<>(); + f.acceptEither(incomplete, (x) -> {}); + f.complete(null); + + f = new CompletableFuture<>(); + f.applyToEither(incomplete, (x) -> x); + f.complete(null); + + f = new CompletableFuture<>(); + CompletableFuture.anyOf(new CompletableFuture[] { f, incomplete }); + f.complete(null); + } + + for (int i = 0; i < n; i++) { + f = new CompletableFuture<>(); + incomplete.runAfterEither(f, () -> {}); + f.complete(null); + + f = new CompletableFuture<>(); + incomplete.acceptEither(f, (x) -> {}); + f.complete(null); + + f = new CompletableFuture<>(); + incomplete.applyToEither(f, (x) -> x); + f.complete(null); + + f = new CompletableFuture<>(); + CompletableFuture.anyOf(new CompletableFuture[] { incomplete, f }); + f.complete(null); + } } // static U join(CompletionStage stage) {