--- jsr166/src/test/tck/CompletableFutureTest.java 2016/06/26 19:03:27 1.152 +++ jsr166/src/test/tck/CompletableFutureTest.java 2016/06/27 21:17:49 1.158 @@ -550,6 +550,15 @@ public class CompletableFutureTest exten } } + 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; + } + } + // Used for explicit executor tests static final class ThreadExecutor implements Executor { final AtomicInteger count = new AtomicInteger(0); @@ -1234,6 +1243,18 @@ public class CompletableFutureTest exten 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 */ @@ -1268,6 +1289,18 @@ public class CompletableFutureTest exten 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 /** @@ -3321,11 +3354,10 @@ public class CompletableFutureTest exten * Test submissions to an executor that rejects all tasks. */ public void testRejectingExecutor() { - final RejectedExecutionException ex = new RejectedExecutionException(); - final Executor e = (Runnable r) -> { throw ex; }; - for (Integer v : new Integer[] { 1, null }) { + final CountingRejectingExecutor e = new CountingRejectingExecutor(); + final CompletableFuture complete = CompletableFuture.completedFuture(v); final CompletableFuture incomplete = new CompletableFuture<>(); @@ -3355,7 +3387,7 @@ public class CompletableFutureTest exten for (CompletableFuture future : fs) { if (src.isDone()) - checkCompletedWithWrappedException(future, ex); + checkCompletedWithWrappedException(future, e.ex); else checkIncomplete(future); } @@ -3392,14 +3424,78 @@ public class CompletableFutureTest exten fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); for (CompletableFuture future : fs) - checkCompletedWithWrappedException(future, ex); + checkCompletedWithWrappedException(future, e.ex); futures.addAll(fs); } incomplete.complete(v); for (CompletableFuture future : futures) - checkCompletedWithWrappedException(future, ex); + 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()); + } } @@ -4074,6 +4170,79 @@ public class CompletableFutureTest exten assertEquals(5 * 3 * n, count.get()); } + /** ant -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); + } + } + + /* + * Tests below currently fail in stress mode due to memory retention. + * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck + */ + + /** Checks for garbage retention with anyOf. */ + public void testAnyOfGarbageRetention() throws Throwable { + for (Integer v : new Integer[] { 1, null }) + { + final int n = expensiveTests ? 100_000 : 10; + CompletableFuture[] fs + = (CompletableFuture[]) new CompletableFuture[100]; + for (int i = 0; i < fs.length; i++) + fs[i] = new CompletableFuture<>(); + fs[fs.length - 1].complete(v); + for (int i = 0; i < n; i++) + checkCompletedNormally(CompletableFuture.anyOf(fs), v); + }} + + /** Checks for garbage retention with allOf. */ + public void testCancelledAllOfGarbageRetention() throws Throwable { + final int n = expensiveTests ? 100_000 : 10; + CompletableFuture[] fs + = (CompletableFuture[]) new CompletableFuture[100]; + for (int i = 0; i < fs.length; i++) + fs[i] = new CompletableFuture<>(); + for (int i = 0; i < n; i++) + assertTrue(CompletableFuture.allOf(fs).cancel(false)); + } + // static U join(CompletionStage stage) { // CompletableFuture f = new CompletableFuture<>(); // stage.whenComplete((v, ex) -> {