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.159 by jsr166, Mon Jun 27 21:39:37 2016 UTC vs.
Revision 1.186 by jsr166, Mon Jul 3 20:55:45 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 2563 | Line 2558 | public class CompletableFutureTest exten
2558  
2559          // unspecified behavior - both source completions available
2560          try {
2561 <            assertEquals(null, h0.join());
2561 >            assertNull(h0.join());
2562              rs[0].assertValue(v1);
2563          } catch (CompletionException ok) {
2564              checkCompletedWithWrappedException(h0, ex);
2565              rs[0].assertNotInvoked();
2566          }
2567          try {
2568 <            assertEquals(null, h1.join());
2568 >            assertNull(h1.join());
2569              rs[1].assertValue(v1);
2570          } catch (CompletionException ok) {
2571              checkCompletedWithWrappedException(h1, ex);
2572              rs[1].assertNotInvoked();
2573          }
2574          try {
2575 <            assertEquals(null, h2.join());
2575 >            assertNull(h2.join());
2576              rs[2].assertValue(v1);
2577          } catch (CompletionException ok) {
2578              checkCompletedWithWrappedException(h2, ex);
2579              rs[2].assertNotInvoked();
2580          }
2581          try {
2582 <            assertEquals(null, h3.join());
2582 >            assertNull(h3.join());
2583              rs[3].assertValue(v1);
2584          } catch (CompletionException ok) {
2585              checkCompletedWithWrappedException(h3, ex);
# Line 2700 | Line 2695 | public class CompletableFutureTest exten
2695          for (ExecutionMode m : ExecutionMode.values())
2696          for (Integer v1 : new Integer[] { 1, null })
2697          for (Integer v2 : new Integer[] { 2, null })
2698 +        for (boolean pushNop : new boolean[] { true, false })
2699      {
2700          final CompletableFuture<Integer> f = new CompletableFuture<>();
2701          final CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 2712 | Line 2708 | public class CompletableFutureTest exten
2708          checkIncomplete(h1);
2709          rs[0].assertNotInvoked();
2710          rs[1].assertNotInvoked();
2711 +        if (pushNop) {          // ad hoc test of intra-completion interference
2712 +            m.thenRun(f, () -> {});
2713 +            m.thenRun(g, () -> {});
2714 +        }
2715          f.complete(v1);
2716          checkCompletedNormally(h0, null);
2717          checkCompletedNormally(h1, null);
# Line 2818 | Line 2818 | public class CompletableFutureTest exten
2818  
2819          // unspecified behavior - both source completions available
2820          try {
2821 <            assertEquals(null, h0.join());
2821 >            assertNull(h0.join());
2822              rs[0].assertInvoked();
2823          } catch (CompletionException ok) {
2824              checkCompletedWithWrappedException(h0, ex);
2825              rs[0].assertNotInvoked();
2826          }
2827          try {
2828 <            assertEquals(null, h1.join());
2828 >            assertNull(h1.join());
2829              rs[1].assertInvoked();
2830          } catch (CompletionException ok) {
2831              checkCompletedWithWrappedException(h1, ex);
2832              rs[1].assertNotInvoked();
2833          }
2834          try {
2835 <            assertEquals(null, h2.join());
2835 >            assertNull(h2.join());
2836              rs[2].assertInvoked();
2837          } catch (CompletionException ok) {
2838              checkCompletedWithWrappedException(h2, ex);
2839              rs[2].assertNotInvoked();
2840          }
2841          try {
2842 <            assertEquals(null, h3.join());
2842 >            assertNull(h3.join());
2843              rs[3].assertInvoked();
2844          } catch (CompletionException ok) {
2845              checkCompletedWithWrappedException(h3, ex);
# Line 3234 | Line 3234 | public class CompletableFutureTest exten
3234      /**
3235       * Completion methods throw NullPointerException with null arguments
3236       */
3237 +    @SuppressWarnings("FutureReturnValueIgnored")
3238      public void testNPE() {
3239          CompletableFuture<Integer> f = new CompletableFuture<>();
3240          CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 3253 | Line 3254 | public class CompletableFutureTest exten
3254  
3255              () -> f.thenApply(null),
3256              () -> f.thenApplyAsync(null),
3257 <            () -> f.thenApplyAsync((x) -> x, null),
3257 >            () -> f.thenApplyAsync(x -> x, null),
3258              () -> f.thenApplyAsync(null, exec),
3259  
3260              () -> f.thenAccept(null),
3261              () -> f.thenAcceptAsync(null),
3262 <            () -> f.thenAcceptAsync((x) -> {} , null),
3262 >            () -> f.thenAcceptAsync(x -> {} , null),
3263              () -> f.thenAcceptAsync(null, exec),
3264  
3265              () -> f.thenRun(null),
# Line 3293 | Line 3294 | public class CompletableFutureTest exten
3294              () -> f.applyToEither(g, null),
3295              () -> f.applyToEitherAsync(g, null),
3296              () -> f.applyToEitherAsync(g, null, exec),
3297 <            () -> f.applyToEither(nullFuture, (x) -> x),
3298 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3299 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3300 <            () -> f.applyToEitherAsync(g, (x) -> x, null),
3297 >            () -> f.applyToEither(nullFuture, x -> x),
3298 >            () -> f.applyToEitherAsync(nullFuture, x -> x),
3299 >            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
3300 >            () -> f.applyToEitherAsync(g, x -> x, null),
3301  
3302              () -> f.acceptEither(g, null),
3303              () -> f.acceptEitherAsync(g, null),
3304              () -> f.acceptEitherAsync(g, null, exec),
3305 <            () -> f.acceptEither(nullFuture, (x) -> {}),
3306 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3307 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3308 <            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3305 >            () -> f.acceptEither(nullFuture, x -> {}),
3306 >            () -> f.acceptEitherAsync(nullFuture, x -> {}),
3307 >            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
3308 >            () -> f.acceptEitherAsync(g, x -> {}, null),
3309  
3310              () -> f.runAfterEither(g, null),
3311              () -> f.runAfterEitherAsync(g, null),
# Line 3354 | Line 3355 | public class CompletableFutureTest exten
3355       * Test submissions to an executor that rejects all tasks.
3356       */
3357      public void testRejectingExecutor() {
3358 <        for (Integer v : new Integer[] { 1, null }) {
3359 <
3358 >        for (Integer v : new Integer[] { 1, null })
3359 >    {
3360          final CountingRejectingExecutor e = new CountingRejectingExecutor();
3361  
3362          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
# Line 3370 | Line 3371 | public class CompletableFutureTest exten
3371          for (CompletableFuture<Integer> src : srcs) {
3372              List<CompletableFuture<?>> fs = new ArrayList<>();
3373              fs.add(src.thenRunAsync(() -> {}, e));
3374 <            fs.add(src.thenAcceptAsync((z) -> {}, e));
3375 <            fs.add(src.thenApplyAsync((z) -> z, e));
3374 >            fs.add(src.thenAcceptAsync(z -> {}, e));
3375 >            fs.add(src.thenApplyAsync(z -> z, e));
3376  
3377              fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3378              fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3379              fs.add(src.runAfterBothAsync(src, () -> {}, e));
3380  
3381 <            fs.add(src.applyToEitherAsync(src, (z) -> z, e));
3382 <            fs.add(src.acceptEitherAsync(src, (z) -> {}, e));
3381 >            fs.add(src.applyToEitherAsync(src, z -> z, e));
3382 >            fs.add(src.acceptEitherAsync(src, z -> {}, e));
3383              fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3384  
3385 <            fs.add(src.thenComposeAsync((z) -> null, e));
3385 >            fs.add(src.thenComposeAsync(z -> null, e));
3386              fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3387              fs.add(src.handleAsync((z, t) -> null, e));
3388  
# Line 3414 | Line 3415 | public class CompletableFutureTest exten
3415          {
3416              List<CompletableFuture<?>> fs = new ArrayList<>();
3417  
3418 <            fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e));
3419 <            fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e));
3418 >            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
3419 >            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
3420  
3421 <            fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e));
3422 <            fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e));
3421 >            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
3422 >            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
3423  
3424              fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3425              fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
# Line 3434 | Line 3435 | public class CompletableFutureTest exten
3435              checkCompletedWithWrappedException(future, e.ex);
3436  
3437          assertEquals(futures.size(), e.count.get());
3438 <
3438 <        }
3439 <    }
3438 >    }}
3439  
3440      /**
3441       * Test submissions to an executor that rejects all tasks, but
# Line 3444 | Line 3443 | public class CompletableFutureTest exten
3443       * explicitly completed.
3444       */
3445      public void testRejectingExecutorNeverInvoked() {
3446 +        for (Integer v : new Integer[] { 1, null })
3447 +    {
3448          final CountingRejectingExecutor e = new CountingRejectingExecutor();
3449  
3449        for (Integer v : new Integer[] { 1, null }) {
3450
3450          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3451          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3452  
# Line 3459 | Line 3458 | public class CompletableFutureTest exten
3458  
3459          List<CompletableFuture<?>> fs = new ArrayList<>();
3460          fs.add(incomplete.thenRunAsync(() -> {}, e));
3461 <        fs.add(incomplete.thenAcceptAsync((z) -> {}, e));
3462 <        fs.add(incomplete.thenApplyAsync((z) -> z, e));
3461 >        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
3462 >        fs.add(incomplete.thenApplyAsync(z -> z, e));
3463  
3464          fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3465          fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3466          fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3467  
3468 <        fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e));
3469 <        fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e));
3468 >        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
3469 >        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
3470          fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3471  
3472 <        fs.add(incomplete.thenComposeAsync((z) -> null, e));
3472 >        fs.add(incomplete.thenComposeAsync(z -> null, e));
3473          fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3474          fs.add(incomplete.handleAsync((z, t) -> null, e));
3475  
# Line 3495 | Line 3494 | public class CompletableFutureTest exten
3494              checkCompletedNormally(future, null);
3495  
3496          assertEquals(0, e.count.get());
3497 <
3499 <        }
3500 <    }
3497 >    }}
3498  
3499      /**
3500       * toCompletableFuture returns this CompletableFuture.
# Line 3532 | Line 3529 | public class CompletableFutureTest exten
3529       */
3530      public void testCompletedStage() {
3531          AtomicInteger x = new AtomicInteger(0);
3532 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3532 >        AtomicReference<Throwable> r = new AtomicReference<>();
3533          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3534          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3535          assertEquals(x.get(), 1);
# Line 3577 | Line 3574 | public class CompletableFutureTest exten
3574       * copy returns a CompletableFuture that is completed normally,
3575       * with the same value, when source is.
3576       */
3577 <    public void testCopy() {
3577 >    public void testCopy_normalCompletion() {
3578 >        for (boolean createIncomplete : new boolean[] { true, false })
3579 >        for (Integer v1 : new Integer[] { 1, null })
3580 >    {
3581          CompletableFuture<Integer> f = new CompletableFuture<>();
3582 +        if (!createIncomplete) assertTrue(f.complete(v1));
3583          CompletableFuture<Integer> g = f.copy();
3584 <        checkIncomplete(f);
3585 <        checkIncomplete(g);
3586 <        f.complete(1);
3587 <        checkCompletedNormally(f, 1);
3588 <        checkCompletedNormally(g, 1);
3589 <    }
3584 >        if (createIncomplete) {
3585 >            checkIncomplete(f);
3586 >            checkIncomplete(g);
3587 >            assertTrue(f.complete(v1));
3588 >        }
3589 >        checkCompletedNormally(f, v1);
3590 >        checkCompletedNormally(g, v1);
3591 >    }}
3592  
3593      /**
3594       * copy returns a CompletableFuture that is completed exceptionally
3595       * when source is.
3596       */
3597 <    public void testCopy2() {
3597 >    public void testCopy_exceptionalCompletion() {
3598 >        for (boolean createIncomplete : new boolean[] { true, false })
3599 >    {
3600 >        CFException ex = new CFException();
3601          CompletableFuture<Integer> f = new CompletableFuture<>();
3602 +        if (!createIncomplete) f.completeExceptionally(ex);
3603          CompletableFuture<Integer> g = f.copy();
3604 <        checkIncomplete(f);
3605 <        checkIncomplete(g);
3606 <        CFException ex = new CFException();
3607 <        f.completeExceptionally(ex);
3604 >        if (createIncomplete) {
3605 >            checkIncomplete(f);
3606 >            checkIncomplete(g);
3607 >            f.completeExceptionally(ex);
3608 >        }
3609          checkCompletedExceptionally(f, ex);
3610          checkCompletedWithWrappedException(g, ex);
3611 +    }}
3612 +
3613 +    /**
3614 +     * Completion of a copy does not complete its source.
3615 +     */
3616 +    public void testCopy_oneWayPropagation() {
3617 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3618 +        assertTrue(f.copy().complete(1));
3619 +        assertTrue(f.copy().complete(null));
3620 +        assertTrue(f.copy().cancel(true));
3621 +        assertTrue(f.copy().cancel(false));
3622 +        assertTrue(f.copy().completeExceptionally(new CFException()));
3623 +        checkIncomplete(f);
3624      }
3625  
3626      /**
# Line 3610 | Line 3631 | public class CompletableFutureTest exten
3631          CompletableFuture<Integer> f = new CompletableFuture<>();
3632          CompletionStage<Integer> g = f.minimalCompletionStage();
3633          AtomicInteger x = new AtomicInteger(0);
3634 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3634 >        AtomicReference<Throwable> r = new AtomicReference<>();
3635          checkIncomplete(f);
3636          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3637          f.complete(1);
# Line 3627 | Line 3648 | public class CompletableFutureTest exten
3648          CompletableFuture<Integer> f = new CompletableFuture<>();
3649          CompletionStage<Integer> g = f.minimalCompletionStage();
3650          AtomicInteger x = new AtomicInteger(0);
3651 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3651 >        AtomicReference<Throwable> r = new AtomicReference<>();
3652          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3653          checkIncomplete(f);
3654          CFException ex = new CFException();
# Line 3645 | Line 3666 | public class CompletableFutureTest exten
3666          CFException ex = new CFException();
3667          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3668          AtomicInteger x = new AtomicInteger(0);
3669 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3669 >        AtomicReference<Throwable> r = new AtomicReference<>();
3670          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3671          assertEquals(x.get(), 0);
3672          assertEquals(r.get(), ex);
# Line 3669 | Line 3690 | public class CompletableFutureTest exten
3690      public void testCompleteAsync2() {
3691          CompletableFuture<Integer> f = new CompletableFuture<>();
3692          CFException ex = new CFException();
3693 <        f.completeAsync(() -> {if (true) throw ex; return 1;});
3693 >        f.completeAsync(() -> { throw ex; });
3694          try {
3695              f.join();
3696              shouldThrow();
# Line 3699 | Line 3720 | public class CompletableFutureTest exten
3720          CompletableFuture<Integer> f = new CompletableFuture<>();
3721          CFException ex = new CFException();
3722          ThreadExecutor executor = new ThreadExecutor();
3723 <        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3723 >        f.completeAsync(() -> { throw ex; }, executor);
3724          try {
3725              f.join();
3726              shouldThrow();
# Line 3851 | Line 3872 | public class CompletableFutureTest exten
3872          final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3873          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3874  
3875 +        final Runnable noopRunnable = new Noop(m);
3876 +        final Consumer<Integer> noopConsumer = new NoopConsumer(m);
3877 +        final Function<Integer, Integer> incFunction = new IncFunction(m);
3878 +
3879          List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3880              = new ArrayList<>();
3881  
3882 <        funs.add((y) -> m.thenRun(y, new Noop(m)));
3883 <        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3884 <        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3885 <
3886 <        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3887 <        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3888 <        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3889 <
3890 <        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3891 <        funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m)));
3892 <        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3893 <        funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3894 <        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3895 <        funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m)));
3896 <
3897 <        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3898 <
3899 <        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3900 <
3901 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y}));
3902 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3903 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y}));
3904 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y}));
3905 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3906 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y}));
3882 >        funs.add(y -> m.thenRun(y, noopRunnable));
3883 >        funs.add(y -> m.thenAccept(y, noopConsumer));
3884 >        funs.add(y -> m.thenApply(y, incFunction));
3885 >
3886 >        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
3887 >        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
3888 >        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
3889 >
3890 >        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
3891 >        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
3892 >        funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3893 >        funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3894 >        funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m)));
3895 >        funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m)));
3896 >
3897 >        funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3898 >
3899 >        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
3900 >
3901 >        funs.add(y -> CompletableFuture.allOf(y));
3902 >        funs.add(y -> CompletableFuture.allOf(y, v42));
3903 >        funs.add(y -> CompletableFuture.allOf(v42, y));
3904 >        funs.add(y -> CompletableFuture.anyOf(y));
3905 >        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
3906 >        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
3907  
3908          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3909                   fun : funs) {
3910              CompletableFuture<Integer> f = new CompletableFuture<>();
3911              f.completeExceptionally(ex);
3912 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3912 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3913              checkCompletedWithWrappedException(src, ex);
3914              CompletableFuture<?> dep = fun.apply(src);
3915              checkCompletedWithWrappedException(dep, ex);
# Line 3894 | Line 3919 | public class CompletableFutureTest exten
3919          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3920                   fun : funs) {
3921              CompletableFuture<Integer> f = new CompletableFuture<>();
3922 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3922 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3923              CompletableFuture<?> dep = fun.apply(src);
3924              f.completeExceptionally(ex);
3925              checkCompletedWithWrappedException(src, ex);
# Line 3908 | Line 3933 | public class CompletableFutureTest exten
3933              CompletableFuture<Integer> f = new CompletableFuture<>();
3934              f.cancel(mayInterruptIfRunning);
3935              checkCancelled(f);
3936 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3936 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3937              checkCompletedWithWrappedCancellationException(src);
3938              CompletableFuture<?> dep = fun.apply(src);
3939              checkCompletedWithWrappedCancellationException(dep);
# Line 3919 | Line 3944 | public class CompletableFutureTest exten
3944          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3945                   fun : funs) {
3946              CompletableFuture<Integer> f = new CompletableFuture<>();
3947 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3947 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3948              CompletableFuture<?> dep = fun.apply(src);
3949              f.cancel(mayInterruptIfRunning);
3950              checkCancelled(f);
# Line 3930 | Line 3955 | public class CompletableFutureTest exten
3955      }}
3956  
3957      /**
3958 <     * Minimal completion stages throw UOE for all non-CompletionStage methods
3958 >     * Minimal completion stages throw UOE for most non-CompletionStage methods
3959       */
3960      public void testMinimalCompletionStage_minimality() {
3961          if (!testImplementationDetails) return;
3962          Function<Method, String> toSignature =
3963 <            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3963 >            method -> method.getName() + Arrays.toString(method.getParameterTypes());
3964          Predicate<Method> isNotStatic =
3965 <            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3965 >            method -> (method.getModifiers() & Modifier.STATIC) == 0;
3966          List<Method> minimalMethods =
3967              Stream.of(Object.class, CompletionStage.class)
3968 <            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3968 >            .flatMap(klazz -> Stream.of(klazz.getMethods()))
3969              .filter(isNotStatic)
3970              .collect(Collectors.toList());
3971          // Methods from CompletableFuture permitted NOT to throw UOE
# Line 3956 | Line 3981 | public class CompletableFutureTest exten
3981              .collect(Collectors.toSet());
3982          List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3983              .filter(isNotStatic)
3984 <            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3984 >            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3985              .collect(Collectors.toList());
3986  
3987 <        CompletionStage<Integer> minimalStage =
3987 >        List<CompletionStage<Integer>> stages = new ArrayList<>();
3988 >        CompletionStage<Integer> min =
3989              new CompletableFuture<Integer>().minimalCompletionStage();
3990 +        stages.add(min);
3991 +        stages.add(min.thenApply(x -> x));
3992 +        stages.add(CompletableFuture.completedStage(1));
3993 +        stages.add(CompletableFuture.failedStage(new CFException()));
3994  
3995          List<Method> bugs = new ArrayList<>();
3996          for (Method method : allMethods) {
# Line 3976 | Line 4006 | public class CompletableFutureTest exten
4006                  else if (parameterTypes[i] == long.class)
4007                      args[i] = 0L;
4008              }
4009 <            try {
4010 <                method.invoke(minimalStage, args);
4011 <                bugs.add(method);
3982 <            }
3983 <            catch (java.lang.reflect.InvocationTargetException expected) {
3984 <                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4009 >            for (CompletionStage<Integer> stage : stages) {
4010 >                try {
4011 >                    method.invoke(stage, args);
4012                      bugs.add(method);
3986                    // expected.getCause().printStackTrace();
4013                  }
4014 +                catch (java.lang.reflect.InvocationTargetException expected) {
4015 +                    if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4016 +                        bugs.add(method);
4017 +                        // expected.getCause().printStackTrace();
4018 +                    }
4019 +                }
4020 +                catch (ReflectiveOperationException bad) { throw new Error(bad); }
4021              }
3989            catch (ReflectiveOperationException bad) { throw new Error(bad); }
4022          }
4023          if (!bugs.isEmpty())
4024 <            throw new Error("Methods did not throw UOE: " + bugs.toString());
4024 >            throw new Error("Methods did not throw UOE: " + bugs);
4025 >    }
4026 >
4027 >    /**
4028 >     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4029 >     * is completed normally, with the same value, when source is.
4030 >     */
4031 >    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
4032 >        for (boolean createIncomplete : new boolean[] { true, false })
4033 >        for (Integer v1 : new Integer[] { 1, null })
4034 >    {
4035 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4036 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4037 >        if (!createIncomplete) assertTrue(f.complete(v1));
4038 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4039 >        if (createIncomplete) {
4040 >            checkIncomplete(f);
4041 >            checkIncomplete(g);
4042 >            assertTrue(f.complete(v1));
4043 >        }
4044 >        checkCompletedNormally(f, v1);
4045 >        checkCompletedNormally(g, v1);
4046 >    }}
4047 >
4048 >    /**
4049 >     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4050 >     * is completed exceptionally when source is.
4051 >     */
4052 >    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
4053 >        for (boolean createIncomplete : new boolean[] { true, false })
4054 >    {
4055 >        CFException ex = new CFException();
4056 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4057 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4058 >        if (!createIncomplete) f.completeExceptionally(ex);
4059 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4060 >        if (createIncomplete) {
4061 >            checkIncomplete(f);
4062 >            checkIncomplete(g);
4063 >            f.completeExceptionally(ex);
4064 >        }
4065 >        checkCompletedExceptionally(f, ex);
4066 >        checkCompletedWithWrappedException(g, ex);
4067 >    }}
4068 >
4069 >    /**
4070 >     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
4071 >     */
4072 >    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
4073 >        for (Integer v1 : new Integer[] { 1, null })
4074 >    {
4075 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4076 >        CompletionStage minimal = f.minimalCompletionStage();
4077 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4078 >        assertTrue(g.complete(v1));
4079 >        checkCompletedNormally(g, v1);
4080 >        checkIncomplete(f);
4081 >        checkIncomplete(minimal.toCompletableFuture());
4082 >    }}
4083 >
4084 >    /**
4085 >     * minimalStage.toCompletableFuture().join() awaits completion
4086 >     */
4087 >    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
4088 >        for (boolean createIncomplete : new boolean[] { true, false })
4089 >        for (Integer v1 : new Integer[] { 1, null })
4090 >    {
4091 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4092 >        if (!createIncomplete) assertTrue(f.complete(v1));
4093 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4094 >        if (createIncomplete) assertTrue(f.complete(v1));
4095 >        assertEquals(v1, minimal.toCompletableFuture().join());
4096 >        assertEquals(v1, minimal.toCompletableFuture().get());
4097 >        checkCompletedNormally(minimal.toCompletableFuture(), v1);
4098 >    }}
4099 >
4100 >    /**
4101 >     * Completion of a toCompletableFuture copy of a minimal stage
4102 >     * does not complete its source.
4103 >     */
4104 >    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
4105 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4106 >        CompletionStage<Integer> g = f.minimalCompletionStage();
4107 >        assertTrue(g.toCompletableFuture().complete(1));
4108 >        assertTrue(g.toCompletableFuture().complete(null));
4109 >        assertTrue(g.toCompletableFuture().cancel(true));
4110 >        assertTrue(g.toCompletableFuture().cancel(false));
4111 >        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
4112 >        checkIncomplete(g.toCompletableFuture());
4113 >        f.complete(1);
4114 >        checkCompletedNormally(g.toCompletableFuture(), 1);
4115 >    }
4116 >
4117 >    /** Demo utility method for external reliable toCompletableFuture */
4118 >    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
4119 >        CompletableFuture<T> f = new CompletableFuture<>();
4120 >        stage.handle((T t, Throwable ex) -> {
4121 >                         if (ex != null) f.completeExceptionally(ex);
4122 >                         else f.complete(t);
4123 >                         return null;
4124 >                     });
4125 >        return f;
4126 >    }
4127 >
4128 >    /** Demo utility method to join a CompletionStage */
4129 >    static <T> T join(CompletionStage<T> stage) {
4130 >        return toCompletableFuture(stage).join();
4131      }
4132  
4133 +    /**
4134 +     * Joining a minimal stage "by hand" works
4135 +     */
4136 +    public void testMinimalCompletionStage_join_by_hand() {
4137 +        for (boolean createIncomplete : new boolean[] { true, false })
4138 +        for (Integer v1 : new Integer[] { 1, null })
4139 +    {
4140 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4141 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4142 +        CompletableFuture<Integer> g = new CompletableFuture<>();
4143 +        if (!createIncomplete) assertTrue(f.complete(v1));
4144 +        minimal.thenAccept(x -> g.complete(x));
4145 +        if (createIncomplete) assertTrue(f.complete(v1));
4146 +        g.join();
4147 +        checkCompletedNormally(g, v1);
4148 +        checkCompletedNormally(f, v1);
4149 +        assertEquals(v1, join(minimal));
4150 +    }}
4151 +
4152      static class Monad {
4153          static class ZeroException extends RuntimeException {
4154              public ZeroException() { super("monadic zero"); }
# Line 4008 | Line 4165 | public class CompletableFutureTest exten
4165          static <T,U,V> Function<T, CompletableFuture<V>> compose
4166              (Function<T, CompletableFuture<U>> f,
4167               Function<U, CompletableFuture<V>> g) {
4168 <            return (x) -> f.apply(x).thenCompose(g);
4168 >            return x -> f.apply(x).thenCompose(g);
4169          }
4170  
4171          static void assertZero(CompletableFuture<?> f) {
# Line 4088 | Line 4245 | public class CompletableFutureTest exten
4245  
4246          // Some mutually non-commutative functions
4247          Function<Long, CompletableFuture<Long>> triple
4248 <            = (x) -> Monad.unit(3 * x);
4248 >            = x -> Monad.unit(3 * x);
4249          Function<Long, CompletableFuture<Long>> inc
4250 <            = (x) -> Monad.unit(x + 1);
4250 >            = x -> Monad.unit(x + 1);
4251  
4252          // unit is a right identity: m >>= unit === m
4253          Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
# Line 4102 | Line 4259 | public class CompletableFutureTest exten
4259          // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
4260          Monad.assertFutureEquals(
4261              unit.apply(5L).thenCompose(inc).thenCompose(triple),
4262 <            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
4262 >            unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple)));
4263  
4264          // The case for CompletableFuture as an additive monad is weaker...
4265  
# Line 4112 | Line 4269 | public class CompletableFutureTest exten
4269          // left zero: zero >>= f === zero
4270          Monad.assertZero(zero.thenCompose(inc));
4271          // right zero: f >>= (\x -> zero) === zero
4272 <        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
4272 >        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
4273  
4274          // f plus zero === f
4275          Monad.assertFutureEquals(Monad.unit(5L),
# Line 4138 | Line 4295 | public class CompletableFutureTest exten
4295                                   Monad.plus(godot, Monad.unit(5L)));
4296      }
4297  
4298 +    /** Test long recursive chains of CompletableFutures with cascading completions */
4299 +    public void testRecursiveChains() throws Throwable {
4300 +        for (ExecutionMode m : ExecutionMode.values())
4301 +        for (boolean addDeadEnds : new boolean[] { true, false })
4302 +    {
4303 +        final int val = 42;
4304 +        final int n = expensiveTests ? 1_000 : 2;
4305 +        CompletableFuture<Integer> head = new CompletableFuture<>();
4306 +        CompletableFuture<Integer> tail = head;
4307 +        for (int i = 0; i < n; i++) {
4308 +            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
4309 +            tail = m.thenApply(tail, v -> v + 1);
4310 +            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
4311 +            tail = m.applyToEither(tail, tail, v -> v + 1);
4312 +            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
4313 +            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
4314 +        }
4315 +        head.complete(val);
4316 +        assertEquals(val + 3 * n, (int) tail.join());
4317 +    }}
4318 +
4319      /**
4320       * A single CompletableFuture with many dependents.
4321       * A demo of scalability - runtime is O(n).
# Line 4149 | Line 4327 | public class CompletableFutureTest exten
4327          final AtomicInteger count = new AtomicInteger(0);
4328          for (int i = 0; i < n; i++) {
4329              head.thenRun(() -> count.getAndIncrement());
4330 <            head.thenAccept((x) -> count.getAndIncrement());
4331 <            head.thenApply((x) -> count.getAndIncrement());
4330 >            head.thenAccept(x -> count.getAndIncrement());
4331 >            head.thenApply(x -> count.getAndIncrement());
4332  
4333              head.runAfterBoth(complete, () -> count.getAndIncrement());
4334              head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
# Line 4160 | Line 4338 | public class CompletableFutureTest exten
4338              complete.thenCombine(head, (x, y) -> count.getAndIncrement());
4339  
4340              head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
4341 <            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4342 <            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4341 >            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4342 >            head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4343              new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
4344 <            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
4345 <            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
4344 >            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
4345 >            new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement());
4346          }
4347          head.complete(null);
4348          assertEquals(5 * 3 * n, count.get());
4349      }
4350  
4351 <    /** ant -Dvmoptions=-Xmx8m -Djsr166.tckTestClass=CompletableFutureTest tck */
4352 <    public void testCoCompletionGarbage() throws Throwable {
4351 >    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4352 >    public void testCoCompletionGarbageRetention() throws Throwable {
4353          final int n = expensiveTests ? 1_000_000 : 10;
4354          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4355          CompletableFuture<Integer> f;
# Line 4181 | Line 4359 | public class CompletableFutureTest exten
4359              f.complete(null);
4360  
4361              f = new CompletableFuture<>();
4362 <            f.acceptEither(incomplete, (x) -> {});
4362 >            f.acceptEither(incomplete, x -> {});
4363              f.complete(null);
4364  
4365              f = new CompletableFuture<>();
4366 <            f.applyToEither(incomplete, (x) -> x);
4366 >            f.applyToEither(incomplete, x -> x);
4367              f.complete(null);
4368  
4369              f = new CompletableFuture<>();
# Line 4199 | Line 4377 | public class CompletableFutureTest exten
4377              f.complete(null);
4378  
4379              f = new CompletableFuture<>();
4380 <            incomplete.acceptEither(f, (x) -> {});
4380 >            incomplete.acceptEither(f, x -> {});
4381              f.complete(null);
4382  
4383              f = new CompletableFuture<>();
4384 <            incomplete.applyToEither(f, (x) -> x);
4384 >            incomplete.applyToEither(f, x -> x);
4385              f.complete(null);
4386  
4387              f = new CompletableFuture<>();
# Line 4212 | Line 4390 | public class CompletableFutureTest exten
4390          }
4391      }
4392  
4393 <    /*
4394 <     * Tests below currently fail in stress mode due to memory retention.
4395 <     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck
4393 >    /**
4394 >     * Reproduction recipe for:
4395 >     * 8160402: Garbage retention with CompletableFuture.anyOf
4396 >     * 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
4397       */
4219
4220    /** Checks for garbage retention with anyOf. */
4398      public void testAnyOfGarbageRetention() throws Throwable {
4399          for (Integer v : new Integer[] { 1, null })
4400      {
# Line 4231 | Line 4408 | public class CompletableFutureTest exten
4408              checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4409      }}
4410  
4411 <    /** Checks for garbage retention with allOf. */
4411 >    /**
4412 >     * Checks for garbage retention with allOf.
4413 >     *
4414 >     * As of 2016-07, fails with OOME:
4415 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck
4416 >     */
4417      public void testCancelledAllOfGarbageRetention() throws Throwable {
4418          final int n = expensiveTests ? 100_000 : 10;
4419          CompletableFuture<Integer>[] fs
# Line 4242 | Line 4424 | public class CompletableFutureTest exten
4424              assertTrue(CompletableFuture.allOf(fs).cancel(false));
4425      }
4426  
4427 +    /**
4428 +     * Checks for garbage retention when a dependent future is
4429 +     * cancelled and garbage-collected.
4430 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4431 +     *
4432 +     * As of 2016-07, fails with OOME:
4433 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck
4434 +     */
4435 +    public void testCancelledGarbageRetention() throws Throwable {
4436 +        final int n = expensiveTests ? 100_000 : 10;
4437 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4438 +        for (int i = 0; i < n; i++)
4439 +            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
4440 +    }
4441 +
4442 +    /**
4443 +     * Checks for garbage retention when MinimalStage.toCompletableFuture()
4444 +     * is invoked many times.
4445 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4446 +     *
4447 +     * As of 2016-07, fails with OOME:
4448 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
4449 +     */
4450 +    public void testToCompletableFutureGarbageRetention() throws Throwable {
4451 +        final int n = expensiveTests ? 900_000 : 10;
4452 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4453 +        CompletionStage minimal = neverCompleted.minimalCompletionStage();
4454 +        for (int i = 0; i < n; i++)
4455 +            assertTrue(minimal.toCompletableFuture().cancel(true));
4456 +    }
4457 +
4458   //     static <U> U join(CompletionStage<U> stage) {
4459   //         CompletableFuture<U> f = new CompletableFuture<>();
4460   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines