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.149 by jsr166, Sun Jun 26 16:01:17 2016 UTC vs.
Revision 1.152 by jsr166, Sun Jun 26 19:03:27 2016 UTC

# Line 30 | Line 30 | import java.util.concurrent.ExecutionExc
30   import java.util.concurrent.Executor;
31   import java.util.concurrent.ForkJoinPool;
32   import java.util.concurrent.ForkJoinTask;
33 + import java.util.concurrent.RejectedExecutionException;
34   import java.util.concurrent.TimeoutException;
35   import java.util.concurrent.TimeUnit;
36   import java.util.concurrent.atomic.AtomicInteger;
# Line 459 | Line 460 | public class CompletableFutureTest exten
460      class FailingSupplier extends CheckedAction
461          implements Supplier<Integer>
462      {
463 <        FailingSupplier(ExecutionMode m) { super(m); }
463 >        final CFException ex;
464 >        FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); }
465          public Integer get() {
466              invoked();
467 <            throw new CFException();
467 >            throw ex;
468          }
469      }
470  
471      class FailingConsumer extends CheckedIntegerAction
472          implements Consumer<Integer>
473      {
474 <        FailingConsumer(ExecutionMode m) { super(m); }
474 >        final CFException ex;
475 >        FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
476          public void accept(Integer x) {
477              invoked();
478              value = x;
479 <            throw new CFException();
479 >            throw ex;
480          }
481      }
482  
483      class FailingBiConsumer extends CheckedIntegerAction
484          implements BiConsumer<Integer, Integer>
485      {
486 <        FailingBiConsumer(ExecutionMode m) { super(m); }
486 >        final CFException ex;
487 >        FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
488          public void accept(Integer x, Integer y) {
489              invoked();
490              value = subtract(x, y);
491 <            throw new CFException();
491 >            throw ex;
492          }
493      }
494  
495      class FailingFunction extends CheckedIntegerAction
496          implements Function<Integer, Integer>
497      {
498 <        FailingFunction(ExecutionMode m) { super(m); }
498 >        final CFException ex;
499 >        FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); }
500          public Integer apply(Integer x) {
501              invoked();
502              value = x;
503 <            throw new CFException();
503 >            throw ex;
504          }
505      }
506  
507      class FailingBiFunction extends CheckedIntegerAction
508          implements BiFunction<Integer, Integer, Integer>
509      {
510 <        FailingBiFunction(ExecutionMode m) { super(m); }
510 >        final CFException ex;
511 >        FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); }
512          public Integer apply(Integer x, Integer y) {
513              invoked();
514              value = subtract(x, y);
515 <            throw new CFException();
515 >            throw ex;
516          }
517      }
518  
519      class FailingRunnable extends CheckedAction implements Runnable {
520 <        FailingRunnable(ExecutionMode m) { super(m); }
520 >        final CFException ex;
521 >        FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
522          public void run() {
523              invoked();
524 <            throw new CFException();
524 >            throw ex;
525          }
526      }
527  
# Line 534 | Line 541 | public class CompletableFutureTest exten
541      class FailingCompletableFutureFunction extends CheckedIntegerAction
542          implements Function<Integer, CompletableFuture<Integer>>
543      {
544 <        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
544 >        final CFException ex;
545 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
546          public CompletableFuture<Integer> apply(Integer x) {
547              invoked();
548              value = x;
549 <            throw new CFException();
549 >            throw ex;
550          }
551      }
552  
# Line 1222 | Line 1230 | public class CompletableFutureTest exten
1230      {
1231          final FailingRunnable r = new FailingRunnable(m);
1232          final CompletableFuture<Void> f = m.runAsync(r);
1233 <        checkCompletedWithWrappedCFException(f);
1233 >        checkCompletedWithWrappedException(f, r.ex);
1234          r.assertInvoked();
1235      }}
1236  
# Line 1256 | Line 1264 | public class CompletableFutureTest exten
1264      {
1265          FailingSupplier r = new FailingSupplier(m);
1266          CompletableFuture<Integer> f = m.supplyAsync(r);
1267 <        checkCompletedWithWrappedCFException(f);
1267 >        checkCompletedWithWrappedException(f, r.ex);
1268          r.assertInvoked();
1269      }}
1270  
# Line 1378 | Line 1386 | public class CompletableFutureTest exten
1386          final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1387          final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1388  
1389 <        checkCompletedWithWrappedCFException(h0);
1390 <        checkCompletedWithWrappedCFException(h1);
1391 <        checkCompletedWithWrappedCFException(h2);
1392 <        checkCompletedWithWrappedCFException(h3);
1393 <        checkCompletedWithWrappedCFException(h4);
1394 <        checkCompletedWithWrappedCFException(h5);
1389 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1390 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1391 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1392 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1393 >        checkCompletedWithWrappedException(h4, rs[4].ex);
1394 >        checkCompletedWithWrappedException(h5, rs[5].ex);
1395          checkCompletedNormally(f, v1);
1396      }}
1397  
# Line 1482 | Line 1490 | public class CompletableFutureTest exten
1490          final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1491          final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1492  
1493 <        checkCompletedWithWrappedCFException(h0);
1494 <        checkCompletedWithWrappedCFException(h1);
1495 <        checkCompletedWithWrappedCFException(h2);
1496 <        checkCompletedWithWrappedCFException(h3);
1493 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1494 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1495 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1496 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1497          checkCompletedNormally(f, v1);
1498      }}
1499  
# Line 1584 | Line 1592 | public class CompletableFutureTest exten
1592          final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1593          final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1594  
1595 <        checkCompletedWithWrappedCFException(h0);
1596 <        checkCompletedWithWrappedCFException(h1);
1597 <        checkCompletedWithWrappedCFException(h2);
1598 <        checkCompletedWithWrappedCFException(h3);
1595 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1596 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1597 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1598 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1599          checkCompletedNormally(f, v1);
1600      }}
1601  
# Line 1749 | Line 1757 | public class CompletableFutureTest exten
1757          assertTrue(snd.complete(w2));
1758          final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1759  
1760 <        checkCompletedWithWrappedCFException(h1);
1761 <        checkCompletedWithWrappedCFException(h2);
1762 <        checkCompletedWithWrappedCFException(h3);
1760 >        checkCompletedWithWrappedException(h1, r1.ex);
1761 >        checkCompletedWithWrappedException(h2, r2.ex);
1762 >        checkCompletedWithWrappedException(h3, r3.ex);
1763          r1.assertInvoked();
1764          r2.assertInvoked();
1765          r3.assertInvoked();
# Line 1913 | Line 1921 | public class CompletableFutureTest exten
1921          assertTrue(snd.complete(w2));
1922          final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1923  
1924 <        checkCompletedWithWrappedCFException(h1);
1925 <        checkCompletedWithWrappedCFException(h2);
1926 <        checkCompletedWithWrappedCFException(h3);
1924 >        checkCompletedWithWrappedException(h1, r1.ex);
1925 >        checkCompletedWithWrappedException(h2, r2.ex);
1926 >        checkCompletedWithWrappedException(h3, r3.ex);
1927          r1.assertInvoked();
1928          r2.assertInvoked();
1929          r3.assertInvoked();
# Line 2077 | Line 2085 | public class CompletableFutureTest exten
2085          assertTrue(snd.complete(w2));
2086          final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2087  
2088 <        checkCompletedWithWrappedCFException(h1);
2089 <        checkCompletedWithWrappedCFException(h2);
2090 <        checkCompletedWithWrappedCFException(h3);
2088 >        checkCompletedWithWrappedException(h1, r1.ex);
2089 >        checkCompletedWithWrappedException(h2, r2.ex);
2090 >        checkCompletedWithWrappedException(h3, r3.ex);
2091          r1.assertInvoked();
2092          r2.assertInvoked();
2093          r3.assertInvoked();
# Line 2369 | Line 2377 | public class CompletableFutureTest exten
2377          f.complete(v1);
2378          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2379          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2380 <        checkCompletedWithWrappedCFException(h0);
2381 <        checkCompletedWithWrappedCFException(h1);
2382 <        checkCompletedWithWrappedCFException(h2);
2383 <        checkCompletedWithWrappedCFException(h3);
2380 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2381 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2382 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2383 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2384          for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2385  
2386          g.complete(v2);
# Line 2381 | Line 2389 | public class CompletableFutureTest exten
2389          final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2390          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2391  
2392 <        checkCompletedWithWrappedCFException(h4);
2392 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2393          assertTrue(Objects.equals(v1, rs[4].value) ||
2394                     Objects.equals(v2, rs[4].value));
2395 <        checkCompletedWithWrappedCFException(h5);
2395 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2396          assertTrue(Objects.equals(v1, rs[5].value) ||
2397                     Objects.equals(v2, rs[5].value));
2398  
# Line 2628 | Line 2636 | public class CompletableFutureTest exten
2636          f.complete(v1);
2637          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2638          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2639 <        checkCompletedWithWrappedCFException(h0);
2640 <        checkCompletedWithWrappedCFException(h1);
2641 <        checkCompletedWithWrappedCFException(h2);
2642 <        checkCompletedWithWrappedCFException(h3);
2639 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2640 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2641 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2642 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2643          for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2644  
2645          g.complete(v2);
# Line 2640 | Line 2648 | public class CompletableFutureTest exten
2648          final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2649          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2650  
2651 <        checkCompletedWithWrappedCFException(h4);
2651 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2652          assertTrue(Objects.equals(v1, rs[4].value) ||
2653                     Objects.equals(v2, rs[4].value));
2654 <        checkCompletedWithWrappedCFException(h5);
2654 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2655          assertTrue(Objects.equals(v1, rs[5].value) ||
2656                     Objects.equals(v2, rs[5].value));
2657  
# Line 2883 | Line 2891 | public class CompletableFutureTest exten
2891          assertTrue(f.complete(v1));
2892          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2893          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2894 <        checkCompletedWithWrappedCFException(h0);
2895 <        checkCompletedWithWrappedCFException(h1);
2896 <        checkCompletedWithWrappedCFException(h2);
2897 <        checkCompletedWithWrappedCFException(h3);
2894 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2895 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2896 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2897 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2898          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2899          assertTrue(g.complete(v2));
2900          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2901          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2902 <        checkCompletedWithWrappedCFException(h4);
2903 <        checkCompletedWithWrappedCFException(h5);
2902 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2903 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2904  
2905          checkCompletedNormally(f, v1);
2906          checkCompletedNormally(g, v2);
# Line 2953 | Line 2961 | public class CompletableFutureTest exten
2961          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2962          if (createIncomplete) assertTrue(f.complete(v1));
2963  
2964 <        checkCompletedWithWrappedCFException(g);
2964 >        checkCompletedWithWrappedException(g, r.ex);
2965          checkCompletedNormally(f, v1);
2966      }}
2967  
# Line 3062 | Line 3070 | public class CompletableFutureTest exten
3070          }
3071      }
3072  
3073 <    public void testAllOf_backwards() throws Exception {
3073 >    public void testAllOf_normal_backwards() throws Exception {
3074          for (int k = 1; k < 10; k++) {
3075              CompletableFuture<Integer>[] fs
3076                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
# Line 3310 | Line 3318 | public class CompletableFutureTest exten
3318      }
3319  
3320      /**
3321 +     * Test submissions to an executor that rejects all tasks.
3322 +     */
3323 +    public void testRejectingExecutor() {
3324 +        final RejectedExecutionException ex = new RejectedExecutionException();
3325 +        final Executor e = (Runnable r) -> { throw ex; };
3326 +
3327 +        for (Integer v : new Integer[] { 1, null }) {
3328 +
3329 +        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3330 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3331 +
3332 +        List<CompletableFuture<?>> futures = new ArrayList<>();
3333 +
3334 +        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3335 +        srcs.add(complete);
3336 +        srcs.add(incomplete);
3337 +
3338 +        for (CompletableFuture<Integer> src : srcs) {
3339 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3340 +            fs.add(src.thenRunAsync(() -> {}, e));
3341 +            fs.add(src.thenAcceptAsync((z) -> {}, e));
3342 +            fs.add(src.thenApplyAsync((z) -> z, e));
3343 +
3344 +            fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3345 +            fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3346 +            fs.add(src.runAfterBothAsync(src, () -> {}, e));
3347 +
3348 +            fs.add(src.applyToEitherAsync(src, (z) -> z, e));
3349 +            fs.add(src.acceptEitherAsync(src, (z) -> {}, e));
3350 +            fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3351 +
3352 +            fs.add(src.thenComposeAsync((z) -> null, e));
3353 +            fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3354 +            fs.add(src.handleAsync((z, t) -> null, e));
3355 +
3356 +            for (CompletableFuture<?> future : fs) {
3357 +                if (src.isDone())
3358 +                    checkCompletedWithWrappedException(future, ex);
3359 +                else
3360 +                    checkIncomplete(future);
3361 +            }
3362 +            futures.addAll(fs);
3363 +        }
3364 +
3365 +        {
3366 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3367 +
3368 +            fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3369 +            fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3370 +
3371 +            fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3372 +            fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3373 +
3374 +            fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3375 +            fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3376 +
3377 +            for (CompletableFuture<?> future : fs)
3378 +                checkIncomplete(future);
3379 +            futures.addAll(fs);
3380 +        }
3381 +
3382 +        {
3383 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3384 +
3385 +            fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e));
3386 +            fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e));
3387 +
3388 +            fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e));
3389 +            fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e));
3390 +
3391 +            fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3392 +            fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
3393 +
3394 +            for (CompletableFuture<?> future : fs)
3395 +                checkCompletedWithWrappedException(future, ex);
3396 +            futures.addAll(fs);
3397 +        }
3398 +
3399 +        incomplete.complete(v);
3400 +
3401 +        for (CompletableFuture<?> future : futures)
3402 +            checkCompletedWithWrappedException(future, ex);
3403 +        }
3404 +    }
3405 +
3406 +    /**
3407       * toCompletableFuture returns this CompletableFuture.
3408       */
3409      public void testToCompletableFuture() {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines