ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.152 by jsr166, Sun Jun 26 19:03:27 2016 UTC vs.
Revision 1.163 by jsr166, Sun Jul 3 18:33:17 2016 UTC

# Line 550 | Line 550 | public class CompletableFutureTest exten
550          }
551      }
552  
553 +    static class CountingRejectingExecutor implements Executor {
554 +        final RejectedExecutionException ex = new RejectedExecutionException();
555 +        final AtomicInteger count = new AtomicInteger(0);
556 +        public void execute(Runnable r) {
557 +            count.getAndIncrement();
558 +            throw ex;
559 +        }
560 +    }
561 +
562      // Used for explicit executor tests
563      static final class ThreadExecutor implements Executor {
564          final AtomicInteger count = new AtomicInteger(0);
# Line 1234 | Line 1243 | public class CompletableFutureTest exten
1243          r.assertInvoked();
1244      }}
1245  
1246 +    public void testRunAsync_rejectingExecutor() {
1247 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1248 +        try {
1249 +            CompletableFuture.runAsync(() -> {}, e);
1250 +            shouldThrow();
1251 +        } catch (Throwable t) {
1252 +            assertSame(e.ex, t);
1253 +        }
1254 +
1255 +        assertEquals(1, e.count.get());
1256 +    }
1257 +
1258      /**
1259       * supplyAsync completes with result of supplier
1260       */
# Line 1268 | Line 1289 | public class CompletableFutureTest exten
1289          r.assertInvoked();
1290      }}
1291  
1292 +    public void testSupplyAsync_rejectingExecutor() {
1293 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1294 +        try {
1295 +            CompletableFuture.supplyAsync(() -> null, e);
1296 +            shouldThrow();
1297 +        } catch (Throwable t) {
1298 +            assertSame(e.ex, t);
1299 +        }
1300 +
1301 +        assertEquals(1, e.count.get());
1302 +    }
1303 +
1304      // seq completion methods
1305  
1306      /**
# Line 2667 | Line 2700 | public class CompletableFutureTest exten
2700          for (ExecutionMode m : ExecutionMode.values())
2701          for (Integer v1 : new Integer[] { 1, null })
2702          for (Integer v2 : new Integer[] { 2, null })
2703 +        for (boolean pushNop : new boolean[] { true, false })
2704      {
2705          final CompletableFuture<Integer> f = new CompletableFuture<>();
2706          final CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 2679 | Line 2713 | public class CompletableFutureTest exten
2713          checkIncomplete(h1);
2714          rs[0].assertNotInvoked();
2715          rs[1].assertNotInvoked();
2716 +        if (pushNop) {          // ad hoc test of intra-completion interference
2717 +            m.thenRun(f, () -> {});
2718 +            m.thenRun(g, () -> {});
2719 +        }
2720          f.complete(v1);
2721          checkCompletedNormally(h0, null);
2722          checkCompletedNormally(h1, null);
# Line 3321 | Line 3359 | public class CompletableFutureTest exten
3359       * Test submissions to an executor that rejects all tasks.
3360       */
3361      public void testRejectingExecutor() {
3362 <        final RejectedExecutionException ex = new RejectedExecutionException();
3363 <        final Executor e = (Runnable r) -> { throw ex; };
3364 <
3327 <        for (Integer v : new Integer[] { 1, null }) {
3362 >        for (Integer v : new Integer[] { 1, null })
3363 >    {
3364 >        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3365  
3366          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3367          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
# Line 3355 | Line 3392 | public class CompletableFutureTest exten
3392  
3393              for (CompletableFuture<?> future : fs) {
3394                  if (src.isDone())
3395 <                    checkCompletedWithWrappedException(future, ex);
3395 >                    checkCompletedWithWrappedException(future, e.ex);
3396                  else
3397                      checkIncomplete(future);
3398              }
# Line 3392 | Line 3429 | public class CompletableFutureTest exten
3429              fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
3430  
3431              for (CompletableFuture<?> future : fs)
3432 <                checkCompletedWithWrappedException(future, ex);
3432 >                checkCompletedWithWrappedException(future, e.ex);
3433              futures.addAll(fs);
3434          }
3435  
3436          incomplete.complete(v);
3437  
3438          for (CompletableFuture<?> future : futures)
3439 <            checkCompletedWithWrappedException(future, ex);
3440 <        }
3441 <    }
3439 >            checkCompletedWithWrappedException(future, e.ex);
3440 >
3441 >        assertEquals(futures.size(), e.count.get());
3442 >    }}
3443 >
3444 >    /**
3445 >     * Test submissions to an executor that rejects all tasks, but
3446 >     * should never be invoked because the dependent future is
3447 >     * explicitly completed.
3448 >     */
3449 >    public void testRejectingExecutorNeverInvoked() {
3450 >        for (Integer v : new Integer[] { 1, null })
3451 >    {
3452 >        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3453 >
3454 >        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3455 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3456 >
3457 >        List<CompletableFuture<?>> futures = new ArrayList<>();
3458 >
3459 >        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3460 >        srcs.add(complete);
3461 >        srcs.add(incomplete);
3462 >
3463 >        List<CompletableFuture<?>> fs = new ArrayList<>();
3464 >        fs.add(incomplete.thenRunAsync(() -> {}, e));
3465 >        fs.add(incomplete.thenAcceptAsync((z) -> {}, e));
3466 >        fs.add(incomplete.thenApplyAsync((z) -> z, e));
3467 >
3468 >        fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3469 >        fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3470 >        fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3471 >
3472 >        fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e));
3473 >        fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e));
3474 >        fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3475 >
3476 >        fs.add(incomplete.thenComposeAsync((z) -> null, e));
3477 >        fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3478 >        fs.add(incomplete.handleAsync((z, t) -> null, e));
3479 >
3480 >        fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3481 >        fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3482 >
3483 >        fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3484 >        fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3485 >
3486 >        fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3487 >        fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3488 >
3489 >        for (CompletableFuture<?> future : fs)
3490 >            checkIncomplete(future);
3491 >
3492 >        for (CompletableFuture<?> future : fs)
3493 >            future.complete(null);
3494 >
3495 >        incomplete.complete(v);
3496 >
3497 >        for (CompletableFuture<?> future : fs)
3498 >            checkCompletedNormally(future, null);
3499 >
3500 >        assertEquals(0, e.count.get());
3501 >    }}
3502  
3503      /**
3504       * toCompletableFuture returns this CompletableFuture.
# Line 4042 | Line 4139 | public class CompletableFutureTest exten
4139                                   Monad.plus(godot, Monad.unit(5L)));
4140      }
4141  
4142 +    /** Test long recursive chains of CompletableFutures with cascading completions */
4143 +    public void testRecursiveChains() throws Throwable {
4144 +        for (ExecutionMode m : ExecutionMode.values())
4145 +        for (boolean addDeadEnds : new boolean[] { true, false })
4146 +    {
4147 +        final int val = 42;
4148 +        final int n = expensiveTests ? 1_000 : 2;
4149 +        CompletableFuture<Integer> head = new CompletableFuture<>();
4150 +        CompletableFuture<Integer> tail = head;
4151 +        for (int i = 0; i < n; i++) {
4152 +            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
4153 +            tail = m.thenApply(tail, v -> v + 1);
4154 +            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
4155 +            tail = m.applyToEither(tail, tail, v -> v + 1);
4156 +            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
4157 +            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
4158 +        }
4159 +        head.complete(val);
4160 +        assertEquals(val + 3 * n, (int) tail.join());
4161 +    }}
4162 +
4163      /**
4164       * A single CompletableFuture with many dependents.
4165       * A demo of scalability - runtime is O(n).
4166       */
4167      public void testManyDependents() throws Throwable {
4168 <        final int n = 1_000;
4168 >        final int n = expensiveTests ? 1_000_000 : 10;
4169          final CompletableFuture<Void> head = new CompletableFuture<>();
4170          final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
4171          final AtomicInteger count = new AtomicInteger(0);
# Line 4074 | Line 4192 | public class CompletableFutureTest exten
4192          assertEquals(5 * 3 * n, count.get());
4193      }
4194  
4195 +    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4196 +    public void testCoCompletionGarbageRetention() throws Throwable {
4197 +        final int n = expensiveTests ? 1_000_000 : 10;
4198 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4199 +        CompletableFuture<Integer> f;
4200 +        for (int i = 0; i < n; i++) {
4201 +            f = new CompletableFuture<>();
4202 +            f.runAfterEither(incomplete, () -> {});
4203 +            f.complete(null);
4204 +
4205 +            f = new CompletableFuture<>();
4206 +            f.acceptEither(incomplete, (x) -> {});
4207 +            f.complete(null);
4208 +
4209 +            f = new CompletableFuture<>();
4210 +            f.applyToEither(incomplete, (x) -> x);
4211 +            f.complete(null);
4212 +
4213 +            f = new CompletableFuture<>();
4214 +            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4215 +            f.complete(null);
4216 +        }
4217 +
4218 +        for (int i = 0; i < n; i++) {
4219 +            f = new CompletableFuture<>();
4220 +            incomplete.runAfterEither(f, () -> {});
4221 +            f.complete(null);
4222 +
4223 +            f = new CompletableFuture<>();
4224 +            incomplete.acceptEither(f, (x) -> {});
4225 +            f.complete(null);
4226 +
4227 +            f = new CompletableFuture<>();
4228 +            incomplete.applyToEither(f, (x) -> x);
4229 +            f.complete(null);
4230 +
4231 +            f = new CompletableFuture<>();
4232 +            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4233 +            f.complete(null);
4234 +        }
4235 +    }
4236 +
4237 +    /*
4238 +     * Tests below currently fail in stress mode due to memory retention.
4239 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck
4240 +     */
4241 +
4242 +    /** Checks for garbage retention with anyOf. */
4243 +    public void testAnyOfGarbageRetention() throws Throwable {
4244 +        for (Integer v : new Integer[] { 1, null })
4245 +    {
4246 +        final int n = expensiveTests ? 100_000 : 10;
4247 +        CompletableFuture<Integer>[] fs
4248 +            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4249 +        for (int i = 0; i < fs.length; i++)
4250 +            fs[i] = new CompletableFuture<>();
4251 +        fs[fs.length - 1].complete(v);
4252 +        for (int i = 0; i < n; i++)
4253 +            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4254 +    }}
4255 +
4256 +    /** Checks for garbage retention with allOf. */
4257 +    public void testCancelledAllOfGarbageRetention() throws Throwable {
4258 +        final int n = expensiveTests ? 100_000 : 10;
4259 +        CompletableFuture<Integer>[] fs
4260 +            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4261 +        for (int i = 0; i < fs.length; i++)
4262 +            fs[i] = new CompletableFuture<>();
4263 +        for (int i = 0; i < n; i++)
4264 +            assertTrue(CompletableFuture.allOf(fs).cancel(false));
4265 +    }
4266 +
4267   //     static <U> U join(CompletionStage<U> stage) {
4268   //         CompletableFuture<U> f = new CompletableFuture<>();
4269   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines