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.167 by jsr166, Sun Jul 17 17:49:23 2016 UTC vs.
Revision 1.187 by jsr166, Mon Jul 3 21:18:37 2017 UTC

# Line 32 | Line 32 | 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;
35   import java.util.concurrent.atomic.AtomicInteger;
36   import java.util.concurrent.atomic.AtomicReference;
37   import java.util.function.BiConsumer;
# Line 65 | Line 64 | public class CompletableFutureTest exten
64              assertNull(f.getNow(null));
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
66          try {
67 <            f.get(0L, SECONDS);
67 >            f.get(randomExpiredTimeout(), randomTimeUnit());
68              shouldThrow();
69          }
70          catch (TimeoutException success) {}
# Line 77 | Line 76 | public class CompletableFutureTest exten
76  
77          try {
78              assertEquals(value, f.join());
80        } catch (Throwable fail) { threadUnexpectedException(fail); }
81        try {
79              assertEquals(value, f.getNow(null));
83        } catch (Throwable fail) { threadUnexpectedException(fail); }
84        try {
80              assertEquals(value, f.get());
81          } catch (Throwable fail) { threadUnexpectedException(fail); }
82          assertTrue(f.isDone());
# Line 94 | Line 89 | public class CompletableFutureTest exten
89       * Returns the "raw" internal exceptional completion of f,
90       * without any additional wrapping with CompletionException.
91       */
92 <    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
93 <        // handle (and whenComplete) can distinguish between "direct"
94 <        // and "wrapped" exceptional completion
95 <        return f.handle((U u, Throwable t) -> t).join();
92 >    Throwable exceptionalCompletion(CompletableFuture<?> f) {
93 >        // handle (and whenComplete and exceptionally) can distinguish
94 >        // between "direct" and "wrapped" exceptional completion
95 >        return f.handle((u, t) -> t).join();
96      }
97  
98      void checkCompletedExceptionally(CompletableFuture<?> f,
# Line 148 | Line 143 | public class CompletableFutureTest exten
143  
144      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
145          checkCompletedExceptionally(f, true,
146 <            (t) -> assertTrue(t instanceof CFException));
146 >            t -> assertTrue(t instanceof CFException));
147      }
148  
149      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
150          checkCompletedExceptionally(f, true,
151 <            (t) -> assertTrue(t instanceof CancellationException));
151 >            t -> assertTrue(t instanceof CancellationException));
152      }
153  
154      void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
155          checkCompletedExceptionally(f, false,
156 <            (t) -> assertTrue(t instanceof TimeoutException));
156 >            t -> assertTrue(t instanceof TimeoutException));
157      }
158  
159      void checkCompletedWithWrappedException(CompletableFuture<?> f,
160                                              Throwable ex) {
161 <        checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
161 >        checkCompletedExceptionally(f, true, t -> assertSame(t, ex));
162      }
163  
164      void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
165 <        checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
165 >        checkCompletedExceptionally(f, false, t -> assertSame(t, ex));
166      }
167  
168      void checkCancelled(CompletableFuture<?> f) {
# Line 362 | Line 357 | public class CompletableFutureTest exten
357          checkCompletedNormally(f, "test");
358      }
359  
360 <    abstract class CheckedAction {
360 >    abstract static class CheckedAction {
361          int invocationCount = 0;
362          final ExecutionMode m;
363          CheckedAction(ExecutionMode m) { this.m = m; }
# Line 374 | Line 369 | public class CompletableFutureTest exten
369          void assertInvoked() { assertEquals(1, invocationCount); }
370      }
371  
372 <    abstract class CheckedIntegerAction extends CheckedAction {
372 >    abstract static class CheckedIntegerAction extends CheckedAction {
373          Integer value;
374          CheckedIntegerAction(ExecutionMode m) { super(m); }
375          void assertValue(Integer expected) {
# Line 383 | Line 378 | public class CompletableFutureTest exten
378          }
379      }
380  
381 <    class IntegerSupplier extends CheckedAction
381 >    static class IntegerSupplier extends CheckedAction
382          implements Supplier<Integer>
383      {
384          final Integer value;
# Line 402 | Line 397 | public class CompletableFutureTest exten
397          return (x == null) ? null : x + 1;
398      }
399  
400 <    class NoopConsumer extends CheckedIntegerAction
400 >    static class NoopConsumer extends CheckedIntegerAction
401          implements Consumer<Integer>
402      {
403          NoopConsumer(ExecutionMode m) { super(m); }
# Line 412 | Line 407 | public class CompletableFutureTest exten
407          }
408      }
409  
410 <    class IncFunction extends CheckedIntegerAction
410 >    static class IncFunction extends CheckedIntegerAction
411          implements Function<Integer,Integer>
412      {
413          IncFunction(ExecutionMode m) { super(m); }
# Line 430 | Line 425 | public class CompletableFutureTest exten
425              - ((y == null) ? 99 : y.intValue());
426      }
427  
428 <    class SubtractAction extends CheckedIntegerAction
428 >    static class SubtractAction extends CheckedIntegerAction
429          implements BiConsumer<Integer, Integer>
430      {
431          SubtractAction(ExecutionMode m) { super(m); }
# Line 440 | Line 435 | public class CompletableFutureTest exten
435          }
436      }
437  
438 <    class SubtractFunction extends CheckedIntegerAction
438 >    static class SubtractFunction extends CheckedIntegerAction
439          implements BiFunction<Integer, Integer, Integer>
440      {
441          SubtractFunction(ExecutionMode m) { super(m); }
# Line 450 | Line 445 | public class CompletableFutureTest exten
445          }
446      }
447  
448 <    class Noop extends CheckedAction implements Runnable {
448 >    static class Noop extends CheckedAction implements Runnable {
449          Noop(ExecutionMode m) { super(m); }
450          public void run() {
451              invoked();
452          }
453      }
454  
455 <    class FailingSupplier extends CheckedAction
455 >    static class FailingSupplier extends CheckedAction
456          implements Supplier<Integer>
457      {
458          final CFException ex;
# Line 468 | Line 463 | public class CompletableFutureTest exten
463          }
464      }
465  
466 <    class FailingConsumer extends CheckedIntegerAction
466 >    static class FailingConsumer extends CheckedIntegerAction
467          implements Consumer<Integer>
468      {
469          final CFException ex;
# Line 480 | Line 475 | public class CompletableFutureTest exten
475          }
476      }
477  
478 <    class FailingBiConsumer extends CheckedIntegerAction
478 >    static class FailingBiConsumer extends CheckedIntegerAction
479          implements BiConsumer<Integer, Integer>
480      {
481          final CFException ex;
# Line 492 | Line 487 | public class CompletableFutureTest exten
487          }
488      }
489  
490 <    class FailingFunction extends CheckedIntegerAction
490 >    static class FailingFunction extends CheckedIntegerAction
491          implements Function<Integer, Integer>
492      {
493          final CFException ex;
# Line 504 | Line 499 | public class CompletableFutureTest exten
499          }
500      }
501  
502 <    class FailingBiFunction extends CheckedIntegerAction
502 >    static class FailingBiFunction extends CheckedIntegerAction
503          implements BiFunction<Integer, Integer, Integer>
504      {
505          final CFException ex;
# Line 516 | Line 511 | public class CompletableFutureTest exten
511          }
512      }
513  
514 <    class FailingRunnable extends CheckedAction implements Runnable {
514 >    static class FailingRunnable extends CheckedAction implements Runnable {
515          final CFException ex;
516          FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
517          public void run() {
# Line 525 | Line 520 | public class CompletableFutureTest exten
520          }
521      }
522  
523 <    class CompletableFutureInc extends CheckedIntegerAction
523 >    static class CompletableFutureInc extends CheckedIntegerAction
524          implements Function<Integer, CompletableFuture<Integer>>
525      {
526          CompletableFutureInc(ExecutionMode m) { super(m); }
# Line 538 | Line 533 | public class CompletableFutureTest exten
533          }
534      }
535  
536 <    class FailingCompletableFutureFunction extends CheckedIntegerAction
536 >    static class FailingCompletableFutureFunction extends CheckedIntegerAction
537          implements Function<Integer, CompletableFuture<Integer>>
538      {
539          final CFException ex;
# Line 1243 | Line 1238 | public class CompletableFutureTest exten
1238          r.assertInvoked();
1239      }}
1240  
1241 +    @SuppressWarnings("FutureReturnValueIgnored")
1242      public void testRunAsync_rejectingExecutor() {
1243          CountingRejectingExecutor e = new CountingRejectingExecutor();
1244          try {
# Line 1289 | Line 1285 | public class CompletableFutureTest exten
1285          r.assertInvoked();
1286      }}
1287  
1288 +    @SuppressWarnings("FutureReturnValueIgnored")
1289      public void testSupplyAsync_rejectingExecutor() {
1290          CountingRejectingExecutor e = new CountingRejectingExecutor();
1291          try {
# Line 2563 | Line 2560 | public class CompletableFutureTest exten
2560  
2561          // unspecified behavior - both source completions available
2562          try {
2563 <            assertEquals(null, h0.join());
2563 >            assertNull(h0.join());
2564              rs[0].assertValue(v1);
2565          } catch (CompletionException ok) {
2566              checkCompletedWithWrappedException(h0, ex);
2567              rs[0].assertNotInvoked();
2568          }
2569          try {
2570 <            assertEquals(null, h1.join());
2570 >            assertNull(h1.join());
2571              rs[1].assertValue(v1);
2572          } catch (CompletionException ok) {
2573              checkCompletedWithWrappedException(h1, ex);
2574              rs[1].assertNotInvoked();
2575          }
2576          try {
2577 <            assertEquals(null, h2.join());
2577 >            assertNull(h2.join());
2578              rs[2].assertValue(v1);
2579          } catch (CompletionException ok) {
2580              checkCompletedWithWrappedException(h2, ex);
2581              rs[2].assertNotInvoked();
2582          }
2583          try {
2584 <            assertEquals(null, h3.join());
2584 >            assertNull(h3.join());
2585              rs[3].assertValue(v1);
2586          } catch (CompletionException ok) {
2587              checkCompletedWithWrappedException(h3, ex);
# Line 2823 | Line 2820 | public class CompletableFutureTest exten
2820  
2821          // unspecified behavior - both source completions available
2822          try {
2823 <            assertEquals(null, h0.join());
2823 >            assertNull(h0.join());
2824              rs[0].assertInvoked();
2825          } catch (CompletionException ok) {
2826              checkCompletedWithWrappedException(h0, ex);
2827              rs[0].assertNotInvoked();
2828          }
2829          try {
2830 <            assertEquals(null, h1.join());
2830 >            assertNull(h1.join());
2831              rs[1].assertInvoked();
2832          } catch (CompletionException ok) {
2833              checkCompletedWithWrappedException(h1, ex);
2834              rs[1].assertNotInvoked();
2835          }
2836          try {
2837 <            assertEquals(null, h2.join());
2837 >            assertNull(h2.join());
2838              rs[2].assertInvoked();
2839          } catch (CompletionException ok) {
2840              checkCompletedWithWrappedException(h2, ex);
2841              rs[2].assertNotInvoked();
2842          }
2843          try {
2844 <            assertEquals(null, h3.join());
2844 >            assertNull(h3.join());
2845              rs[3].assertInvoked();
2846          } catch (CompletionException ok) {
2847              checkCompletedWithWrappedException(h3, ex);
# Line 3239 | Line 3236 | public class CompletableFutureTest exten
3236      /**
3237       * Completion methods throw NullPointerException with null arguments
3238       */
3239 +    @SuppressWarnings("FutureReturnValueIgnored")
3240      public void testNPE() {
3241          CompletableFuture<Integer> f = new CompletableFuture<>();
3242          CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 3258 | Line 3256 | public class CompletableFutureTest exten
3256  
3257              () -> f.thenApply(null),
3258              () -> f.thenApplyAsync(null),
3259 <            () -> f.thenApplyAsync((x) -> x, null),
3259 >            () -> f.thenApplyAsync(x -> x, null),
3260              () -> f.thenApplyAsync(null, exec),
3261  
3262              () -> f.thenAccept(null),
3263              () -> f.thenAcceptAsync(null),
3264 <            () -> f.thenAcceptAsync((x) -> {} , null),
3264 >            () -> f.thenAcceptAsync(x -> {} , null),
3265              () -> f.thenAcceptAsync(null, exec),
3266  
3267              () -> f.thenRun(null),
# Line 3298 | Line 3296 | public class CompletableFutureTest exten
3296              () -> f.applyToEither(g, null),
3297              () -> f.applyToEitherAsync(g, null),
3298              () -> f.applyToEitherAsync(g, null, exec),
3299 <            () -> f.applyToEither(nullFuture, (x) -> x),
3300 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3301 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3302 <            () -> f.applyToEitherAsync(g, (x) -> x, null),
3299 >            () -> f.applyToEither(nullFuture, x -> x),
3300 >            () -> f.applyToEitherAsync(nullFuture, x -> x),
3301 >            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
3302 >            () -> f.applyToEitherAsync(g, x -> x, null),
3303  
3304              () -> f.acceptEither(g, null),
3305              () -> f.acceptEitherAsync(g, null),
3306              () -> f.acceptEitherAsync(g, null, exec),
3307 <            () -> f.acceptEither(nullFuture, (x) -> {}),
3308 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3309 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3310 <            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3307 >            () -> f.acceptEither(nullFuture, x -> {}),
3308 >            () -> f.acceptEitherAsync(nullFuture, x -> {}),
3309 >            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
3310 >            () -> f.acceptEitherAsync(g, x -> {}, null),
3311  
3312              () -> f.runAfterEither(g, null),
3313              () -> f.runAfterEitherAsync(g, null),
# Line 3375 | Line 3373 | public class CompletableFutureTest exten
3373          for (CompletableFuture<Integer> src : srcs) {
3374              List<CompletableFuture<?>> fs = new ArrayList<>();
3375              fs.add(src.thenRunAsync(() -> {}, e));
3376 <            fs.add(src.thenAcceptAsync((z) -> {}, e));
3377 <            fs.add(src.thenApplyAsync((z) -> z, e));
3376 >            fs.add(src.thenAcceptAsync(z -> {}, e));
3377 >            fs.add(src.thenApplyAsync(z -> z, e));
3378  
3379              fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3380              fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3381              fs.add(src.runAfterBothAsync(src, () -> {}, e));
3382  
3383 <            fs.add(src.applyToEitherAsync(src, (z) -> z, e));
3384 <            fs.add(src.acceptEitherAsync(src, (z) -> {}, e));
3383 >            fs.add(src.applyToEitherAsync(src, z -> z, e));
3384 >            fs.add(src.acceptEitherAsync(src, z -> {}, e));
3385              fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3386  
3387 <            fs.add(src.thenComposeAsync((z) -> null, e));
3387 >            fs.add(src.thenComposeAsync(z -> null, e));
3388              fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3389              fs.add(src.handleAsync((z, t) -> null, e));
3390  
# Line 3419 | Line 3417 | public class CompletableFutureTest exten
3417          {
3418              List<CompletableFuture<?>> fs = new ArrayList<>();
3419  
3420 <            fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e));
3421 <            fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e));
3420 >            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
3421 >            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
3422  
3423 <            fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e));
3424 <            fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e));
3423 >            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
3424 >            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
3425  
3426              fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3427              fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
# Line 3462 | Line 3460 | public class CompletableFutureTest exten
3460  
3461          List<CompletableFuture<?>> fs = new ArrayList<>();
3462          fs.add(incomplete.thenRunAsync(() -> {}, e));
3463 <        fs.add(incomplete.thenAcceptAsync((z) -> {}, e));
3464 <        fs.add(incomplete.thenApplyAsync((z) -> z, e));
3463 >        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
3464 >        fs.add(incomplete.thenApplyAsync(z -> z, e));
3465  
3466          fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3467          fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3468          fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3469  
3470 <        fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e));
3471 <        fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e));
3470 >        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
3471 >        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
3472          fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3473  
3474 <        fs.add(incomplete.thenComposeAsync((z) -> null, e));
3474 >        fs.add(incomplete.thenComposeAsync(z -> null, e));
3475          fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3476          fs.add(incomplete.handleAsync((z, t) -> null, e));
3477  
# Line 3533 | Line 3531 | public class CompletableFutureTest exten
3531       */
3532      public void testCompletedStage() {
3533          AtomicInteger x = new AtomicInteger(0);
3534 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3534 >        AtomicReference<Throwable> r = new AtomicReference<>();
3535          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3536          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3537          assertEquals(x.get(), 1);
# Line 3578 | Line 3576 | public class CompletableFutureTest exten
3576       * copy returns a CompletableFuture that is completed normally,
3577       * with the same value, when source is.
3578       */
3579 <    public void testCopy() {
3579 >    public void testCopy_normalCompletion() {
3580 >        for (boolean createIncomplete : new boolean[] { true, false })
3581 >        for (Integer v1 : new Integer[] { 1, null })
3582 >    {
3583          CompletableFuture<Integer> f = new CompletableFuture<>();
3584 +        if (!createIncomplete) assertTrue(f.complete(v1));
3585          CompletableFuture<Integer> g = f.copy();
3586 <        checkIncomplete(f);
3587 <        checkIncomplete(g);
3588 <        f.complete(1);
3589 <        checkCompletedNormally(f, 1);
3590 <        checkCompletedNormally(g, 1);
3591 <    }
3586 >        if (createIncomplete) {
3587 >            checkIncomplete(f);
3588 >            checkIncomplete(g);
3589 >            assertTrue(f.complete(v1));
3590 >        }
3591 >        checkCompletedNormally(f, v1);
3592 >        checkCompletedNormally(g, v1);
3593 >    }}
3594  
3595      /**
3596       * copy returns a CompletableFuture that is completed exceptionally
3597       * when source is.
3598       */
3599 <    public void testCopy2() {
3599 >    public void testCopy_exceptionalCompletion() {
3600 >        for (boolean createIncomplete : new boolean[] { true, false })
3601 >    {
3602 >        CFException ex = new CFException();
3603          CompletableFuture<Integer> f = new CompletableFuture<>();
3604 +        if (!createIncomplete) f.completeExceptionally(ex);
3605          CompletableFuture<Integer> g = f.copy();
3606 <        checkIncomplete(f);
3607 <        checkIncomplete(g);
3608 <        CFException ex = new CFException();
3609 <        f.completeExceptionally(ex);
3606 >        if (createIncomplete) {
3607 >            checkIncomplete(f);
3608 >            checkIncomplete(g);
3609 >            f.completeExceptionally(ex);
3610 >        }
3611          checkCompletedExceptionally(f, ex);
3612          checkCompletedWithWrappedException(g, ex);
3613 +    }}
3614 +
3615 +    /**
3616 +     * Completion of a copy does not complete its source.
3617 +     */
3618 +    public void testCopy_oneWayPropagation() {
3619 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3620 +        assertTrue(f.copy().complete(1));
3621 +        assertTrue(f.copy().complete(null));
3622 +        assertTrue(f.copy().cancel(true));
3623 +        assertTrue(f.copy().cancel(false));
3624 +        assertTrue(f.copy().completeExceptionally(new CFException()));
3625 +        checkIncomplete(f);
3626      }
3627  
3628      /**
# Line 3611 | Line 3633 | public class CompletableFutureTest exten
3633          CompletableFuture<Integer> f = new CompletableFuture<>();
3634          CompletionStage<Integer> g = f.minimalCompletionStage();
3635          AtomicInteger x = new AtomicInteger(0);
3636 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3636 >        AtomicReference<Throwable> r = new AtomicReference<>();
3637          checkIncomplete(f);
3638          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3639          f.complete(1);
# Line 3628 | Line 3650 | public class CompletableFutureTest exten
3650          CompletableFuture<Integer> f = new CompletableFuture<>();
3651          CompletionStage<Integer> g = f.minimalCompletionStage();
3652          AtomicInteger x = new AtomicInteger(0);
3653 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3653 >        AtomicReference<Throwable> r = new AtomicReference<>();
3654          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3655          checkIncomplete(f);
3656          CFException ex = new CFException();
# Line 3646 | Line 3668 | public class CompletableFutureTest exten
3668          CFException ex = new CFException();
3669          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3670          AtomicInteger x = new AtomicInteger(0);
3671 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3671 >        AtomicReference<Throwable> r = new AtomicReference<>();
3672          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3673          assertEquals(x.get(), 0);
3674          assertEquals(r.get(), ex);
# Line 3670 | Line 3692 | public class CompletableFutureTest exten
3692      public void testCompleteAsync2() {
3693          CompletableFuture<Integer> f = new CompletableFuture<>();
3694          CFException ex = new CFException();
3695 <        f.completeAsync(() -> {if (true) throw ex; return 1;});
3695 >        f.completeAsync(() -> { throw ex; });
3696          try {
3697              f.join();
3698              shouldThrow();
# Line 3700 | Line 3722 | public class CompletableFutureTest exten
3722          CompletableFuture<Integer> f = new CompletableFuture<>();
3723          CFException ex = new CFException();
3724          ThreadExecutor executor = new ThreadExecutor();
3725 <        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3725 >        f.completeAsync(() -> { throw ex; }, executor);
3726          try {
3727              f.join();
3728              shouldThrow();
# Line 3859 | Line 3881 | public class CompletableFutureTest exten
3881          List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3882              = new ArrayList<>();
3883  
3884 <        funs.add((y) -> m.thenRun(y, noopRunnable));
3885 <        funs.add((y) -> m.thenAccept(y, noopConsumer));
3886 <        funs.add((y) -> m.thenApply(y, incFunction));
3887 <
3888 <        funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable));
3889 <        funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer));
3890 <        funs.add((y) -> m.applyToEither(y, incomplete, incFunction));
3891 <
3892 <        funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable));
3893 <        funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable));
3894 <        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3895 <        funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3896 <        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3897 <        funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m)));
3898 <
3899 <        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3900 <
3901 <        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3902 <
3903 <        funs.add((y) -> CompletableFuture.allOf(y));
3904 <        funs.add((y) -> CompletableFuture.allOf(y, v42));
3905 <        funs.add((y) -> CompletableFuture.allOf(v42, y));
3906 <        funs.add((y) -> CompletableFuture.anyOf(y));
3907 <        funs.add((y) -> CompletableFuture.anyOf(y, incomplete));
3908 <        funs.add((y) -> CompletableFuture.anyOf(incomplete, y));
3884 >        funs.add(y -> m.thenRun(y, noopRunnable));
3885 >        funs.add(y -> m.thenAccept(y, noopConsumer));
3886 >        funs.add(y -> m.thenApply(y, incFunction));
3887 >
3888 >        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
3889 >        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
3890 >        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
3891 >
3892 >        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
3893 >        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
3894 >        funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3895 >        funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3896 >        funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m)));
3897 >        funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m)));
3898 >
3899 >        funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3900 >
3901 >        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
3902 >
3903 >        funs.add(y -> CompletableFuture.allOf(y));
3904 >        funs.add(y -> CompletableFuture.allOf(y, v42));
3905 >        funs.add(y -> CompletableFuture.allOf(v42, y));
3906 >        funs.add(y -> CompletableFuture.anyOf(y));
3907 >        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
3908 >        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
3909  
3910          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3911                   fun : funs) {
# Line 3940 | Line 3962 | public class CompletableFutureTest exten
3962      public void testMinimalCompletionStage_minimality() {
3963          if (!testImplementationDetails) return;
3964          Function<Method, String> toSignature =
3965 <            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3965 >            method -> method.getName() + Arrays.toString(method.getParameterTypes());
3966          Predicate<Method> isNotStatic =
3967 <            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3967 >            method -> (method.getModifiers() & Modifier.STATIC) == 0;
3968          List<Method> minimalMethods =
3969              Stream.of(Object.class, CompletionStage.class)
3970 <            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3970 >            .flatMap(klazz -> Stream.of(klazz.getMethods()))
3971              .filter(isNotStatic)
3972              .collect(Collectors.toList());
3973          // Methods from CompletableFuture permitted NOT to throw UOE
# Line 3961 | Line 3983 | public class CompletableFutureTest exten
3983              .collect(Collectors.toSet());
3984          List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3985              .filter(isNotStatic)
3986 <            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3986 >            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3987              .collect(Collectors.toList());
3988  
3989          List<CompletionStage<Integer>> stages = new ArrayList<>();
3990 <        stages.add(new CompletableFuture<Integer>().minimalCompletionStage());
3990 >        CompletionStage<Integer> min =
3991 >            new CompletableFuture<Integer>().minimalCompletionStage();
3992 >        stages.add(min);
3993 >        stages.add(min.thenApply(x -> x));
3994          stages.add(CompletableFuture.completedStage(1));
3995          stages.add(CompletableFuture.failedStage(new CFException()));
3996  
# Line 4001 | Line 4026 | public class CompletableFutureTest exten
4026              throw new Error("Methods did not throw UOE: " + bugs);
4027      }
4028  
4029 +    /**
4030 +     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4031 +     * is completed normally, with the same value, when source is.
4032 +     */
4033 +    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
4034 +        for (boolean createIncomplete : new boolean[] { true, false })
4035 +        for (Integer v1 : new Integer[] { 1, null })
4036 +    {
4037 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4038 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4039 +        if (!createIncomplete) assertTrue(f.complete(v1));
4040 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4041 +        if (createIncomplete) {
4042 +            checkIncomplete(f);
4043 +            checkIncomplete(g);
4044 +            assertTrue(f.complete(v1));
4045 +        }
4046 +        checkCompletedNormally(f, v1);
4047 +        checkCompletedNormally(g, v1);
4048 +    }}
4049 +
4050 +    /**
4051 +     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4052 +     * is completed exceptionally when source is.
4053 +     */
4054 +    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
4055 +        for (boolean createIncomplete : new boolean[] { true, false })
4056 +    {
4057 +        CFException ex = new CFException();
4058 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4059 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4060 +        if (!createIncomplete) f.completeExceptionally(ex);
4061 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4062 +        if (createIncomplete) {
4063 +            checkIncomplete(f);
4064 +            checkIncomplete(g);
4065 +            f.completeExceptionally(ex);
4066 +        }
4067 +        checkCompletedExceptionally(f, ex);
4068 +        checkCompletedWithWrappedException(g, ex);
4069 +    }}
4070 +
4071 +    /**
4072 +     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
4073 +     */
4074 +    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
4075 +        for (Integer v1 : new Integer[] { 1, null })
4076 +    {
4077 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4078 +        CompletionStage minimal = f.minimalCompletionStage();
4079 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4080 +        assertTrue(g.complete(v1));
4081 +        checkCompletedNormally(g, v1);
4082 +        checkIncomplete(f);
4083 +        checkIncomplete(minimal.toCompletableFuture());
4084 +    }}
4085 +
4086 +    /**
4087 +     * minimalStage.toCompletableFuture().join() awaits completion
4088 +     */
4089 +    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
4090 +        for (boolean createIncomplete : new boolean[] { true, false })
4091 +        for (Integer v1 : new Integer[] { 1, null })
4092 +    {
4093 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4094 +        if (!createIncomplete) assertTrue(f.complete(v1));
4095 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4096 +        if (createIncomplete) assertTrue(f.complete(v1));
4097 +        assertEquals(v1, minimal.toCompletableFuture().join());
4098 +        assertEquals(v1, minimal.toCompletableFuture().get());
4099 +        checkCompletedNormally(minimal.toCompletableFuture(), v1);
4100 +    }}
4101 +
4102 +    /**
4103 +     * Completion of a toCompletableFuture copy of a minimal stage
4104 +     * does not complete its source.
4105 +     */
4106 +    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
4107 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4108 +        CompletionStage<Integer> g = f.minimalCompletionStage();
4109 +        assertTrue(g.toCompletableFuture().complete(1));
4110 +        assertTrue(g.toCompletableFuture().complete(null));
4111 +        assertTrue(g.toCompletableFuture().cancel(true));
4112 +        assertTrue(g.toCompletableFuture().cancel(false));
4113 +        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
4114 +        checkIncomplete(g.toCompletableFuture());
4115 +        f.complete(1);
4116 +        checkCompletedNormally(g.toCompletableFuture(), 1);
4117 +    }
4118 +
4119 +    /** Demo utility method for external reliable toCompletableFuture */
4120 +    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
4121 +        CompletableFuture<T> f = new CompletableFuture<>();
4122 +        stage.handle((T t, Throwable ex) -> {
4123 +                         if (ex != null) f.completeExceptionally(ex);
4124 +                         else f.complete(t);
4125 +                         return null;
4126 +                     });
4127 +        return f;
4128 +    }
4129 +
4130 +    /** Demo utility method to join a CompletionStage */
4131 +    static <T> T join(CompletionStage<T> stage) {
4132 +        return toCompletableFuture(stage).join();
4133 +    }
4134 +
4135 +    /**
4136 +     * Joining a minimal stage "by hand" works
4137 +     */
4138 +    public void testMinimalCompletionStage_join_by_hand() {
4139 +        for (boolean createIncomplete : new boolean[] { true, false })
4140 +        for (Integer v1 : new Integer[] { 1, null })
4141 +    {
4142 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4143 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4144 +        CompletableFuture<Integer> g = new CompletableFuture<>();
4145 +        if (!createIncomplete) assertTrue(f.complete(v1));
4146 +        minimal.thenAccept(x -> g.complete(x));
4147 +        if (createIncomplete) assertTrue(f.complete(v1));
4148 +        g.join();
4149 +        checkCompletedNormally(g, v1);
4150 +        checkCompletedNormally(f, v1);
4151 +        assertEquals(v1, join(minimal));
4152 +    }}
4153 +
4154      static class Monad {
4155          static class ZeroException extends RuntimeException {
4156              public ZeroException() { super("monadic zero"); }
# Line 4017 | Line 4167 | public class CompletableFutureTest exten
4167          static <T,U,V> Function<T, CompletableFuture<V>> compose
4168              (Function<T, CompletableFuture<U>> f,
4169               Function<U, CompletableFuture<V>> g) {
4170 <            return (x) -> f.apply(x).thenCompose(g);
4170 >            return x -> f.apply(x).thenCompose(g);
4171          }
4172  
4173          static void assertZero(CompletableFuture<?> f) {
# Line 4097 | Line 4247 | public class CompletableFutureTest exten
4247  
4248          // Some mutually non-commutative functions
4249          Function<Long, CompletableFuture<Long>> triple
4250 <            = (x) -> Monad.unit(3 * x);
4250 >            = x -> Monad.unit(3 * x);
4251          Function<Long, CompletableFuture<Long>> inc
4252 <            = (x) -> Monad.unit(x + 1);
4252 >            = x -> Monad.unit(x + 1);
4253  
4254          // unit is a right identity: m >>= unit === m
4255          Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
# Line 4111 | Line 4261 | public class CompletableFutureTest exten
4261          // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
4262          Monad.assertFutureEquals(
4263              unit.apply(5L).thenCompose(inc).thenCompose(triple),
4264 <            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
4264 >            unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple)));
4265  
4266          // The case for CompletableFuture as an additive monad is weaker...
4267  
# Line 4121 | Line 4271 | public class CompletableFutureTest exten
4271          // left zero: zero >>= f === zero
4272          Monad.assertZero(zero.thenCompose(inc));
4273          // right zero: f >>= (\x -> zero) === zero
4274 <        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
4274 >        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
4275  
4276          // f plus zero === f
4277          Monad.assertFutureEquals(Monad.unit(5L),
# Line 4148 | Line 4298 | public class CompletableFutureTest exten
4298      }
4299  
4300      /** Test long recursive chains of CompletableFutures with cascading completions */
4301 +    @SuppressWarnings("FutureReturnValueIgnored")
4302      public void testRecursiveChains() throws Throwable {
4303          for (ExecutionMode m : ExecutionMode.values())
4304          for (boolean addDeadEnds : new boolean[] { true, false })
# Line 4172 | Line 4323 | public class CompletableFutureTest exten
4323       * A single CompletableFuture with many dependents.
4324       * A demo of scalability - runtime is O(n).
4325       */
4326 +    @SuppressWarnings("FutureReturnValueIgnored")
4327      public void testManyDependents() throws Throwable {
4328          final int n = expensiveTests ? 1_000_000 : 10;
4329          final CompletableFuture<Void> head = new CompletableFuture<>();
# Line 4179 | Line 4331 | public class CompletableFutureTest exten
4331          final AtomicInteger count = new AtomicInteger(0);
4332          for (int i = 0; i < n; i++) {
4333              head.thenRun(() -> count.getAndIncrement());
4334 <            head.thenAccept((x) -> count.getAndIncrement());
4335 <            head.thenApply((x) -> count.getAndIncrement());
4334 >            head.thenAccept(x -> count.getAndIncrement());
4335 >            head.thenApply(x -> count.getAndIncrement());
4336  
4337              head.runAfterBoth(complete, () -> count.getAndIncrement());
4338              head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
# Line 4190 | Line 4342 | public class CompletableFutureTest exten
4342              complete.thenCombine(head, (x, y) -> count.getAndIncrement());
4343  
4344              head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
4345 <            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4346 <            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4345 >            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4346 >            head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4347              new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
4348 <            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
4349 <            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
4348 >            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
4349 >            new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement());
4350          }
4351          head.complete(null);
4352          assertEquals(5 * 3 * n, count.get());
4353      }
4354  
4355      /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4356 +    @SuppressWarnings("FutureReturnValueIgnored")
4357      public void testCoCompletionGarbageRetention() throws Throwable {
4358          final int n = expensiveTests ? 1_000_000 : 10;
4359          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
# Line 4211 | Line 4364 | public class CompletableFutureTest exten
4364              f.complete(null);
4365  
4366              f = new CompletableFuture<>();
4367 <            f.acceptEither(incomplete, (x) -> {});
4367 >            f.acceptEither(incomplete, x -> {});
4368              f.complete(null);
4369  
4370              f = new CompletableFuture<>();
4371 <            f.applyToEither(incomplete, (x) -> x);
4371 >            f.applyToEither(incomplete, x -> x);
4372              f.complete(null);
4373  
4374              f = new CompletableFuture<>();
# Line 4229 | Line 4382 | public class CompletableFutureTest exten
4382              f.complete(null);
4383  
4384              f = new CompletableFuture<>();
4385 <            incomplete.acceptEither(f, (x) -> {});
4385 >            incomplete.acceptEither(f, x -> {});
4386              f.complete(null);
4387  
4388              f = new CompletableFuture<>();
4389 <            incomplete.applyToEither(f, (x) -> x);
4389 >            incomplete.applyToEither(f, x -> x);
4390              f.complete(null);
4391  
4392              f = new CompletableFuture<>();
# Line 4243 | Line 4396 | public class CompletableFutureTest exten
4396      }
4397  
4398      /**
4399 <     * Checks for garbage retention with anyOf.
4400 <     * Following used to fail with OOME:
4401 <     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck
4399 >     * Reproduction recipe for:
4400 >     * 8160402: Garbage retention with CompletableFuture.anyOf
4401 >     * cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A
4402       */
4403      public void testAnyOfGarbageRetention() throws Throwable {
4404          for (Integer v : new Integer[] { 1, null })
# Line 4276 | Line 4429 | public class CompletableFutureTest exten
4429              assertTrue(CompletableFuture.allOf(fs).cancel(false));
4430      }
4431  
4432 +    /**
4433 +     * Checks for garbage retention when a dependent future is
4434 +     * cancelled and garbage-collected.
4435 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4436 +     *
4437 +     * As of 2016-07, fails with OOME:
4438 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck
4439 +     */
4440 +    public void testCancelledGarbageRetention() throws Throwable {
4441 +        final int n = expensiveTests ? 100_000 : 10;
4442 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4443 +        for (int i = 0; i < n; i++)
4444 +            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
4445 +    }
4446 +
4447 +    /**
4448 +     * Checks for garbage retention when MinimalStage.toCompletableFuture()
4449 +     * is invoked many times.
4450 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4451 +     *
4452 +     * As of 2016-07, fails with OOME:
4453 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
4454 +     */
4455 +    public void testToCompletableFutureGarbageRetention() throws Throwable {
4456 +        final int n = expensiveTests ? 900_000 : 10;
4457 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4458 +        CompletionStage minimal = neverCompleted.minimalCompletionStage();
4459 +        for (int i = 0; i < n; i++)
4460 +            assertTrue(minimal.toCompletableFuture().cancel(true));
4461 +    }
4462 +
4463   //     static <U> U join(CompletionStage<U> stage) {
4464   //         CompletableFuture<U> f = new CompletableFuture<>();
4465   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines