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.190 by jsr166, Wed Nov 8 02:21:43 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 60 | Line 59 | public class CompletableFutureTest exten
59      void checkIncomplete(CompletableFuture<?> f) {
60          assertFalse(f.isDone());
61          assertFalse(f.isCancelled());
62 <        assertTrue(f.toString().contains("Not completed"));
62 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
63          try {
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());
83          assertFalse(f.isCancelled());
84          assertFalse(f.isCompletedExceptionally());
85 <        assertTrue(f.toString().contains("[Completed normally]"));
85 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
86      }
87  
88      /**
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 143 | Line 138 | public class CompletableFutureTest exten
138          assertFalse(f.isCancelled());
139          assertTrue(f.isDone());
140          assertTrue(f.isCompletedExceptionally());
141 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
141 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
142      }
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 198 | Line 193 | public class CompletableFutureTest exten
193          assertTrue(f.isDone());
194          assertTrue(f.isCompletedExceptionally());
195          assertTrue(f.isCancelled());
196 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
196 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
197      }
198  
199      /**
# Line 334 | Line 329 | public class CompletableFutureTest exten
329      /**
330       * toString indicates current completion state
331       */
332 <    public void testToString() {
333 <        CompletableFuture<String> f;
334 <
335 <        f = new CompletableFuture<String>();
336 <        assertTrue(f.toString().contains("[Not completed]"));
332 >    public void testToString_incomplete() {
333 >        CompletableFuture<String> f = new CompletableFuture<String>();
334 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
335 >        if (testImplementationDetails)
336 >            assertEquals(identityString(f) + "[Not completed]",
337 >                         f.toString());
338 >    }
339  
340 +    public void testToString_normal() {
341 +        CompletableFuture<String> f = new CompletableFuture<String>();
342          assertTrue(f.complete("foo"));
343 <        assertTrue(f.toString().contains("[Completed normally]"));
343 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
344 >        if (testImplementationDetails)
345 >            assertEquals(identityString(f) + "[Completed normally]",
346 >                         f.toString());
347 >    }
348  
349 <        f = new CompletableFuture<String>();
349 >    public void testToString_exception() {
350 >        CompletableFuture<String> f = new CompletableFuture<String>();
351          assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
352 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
352 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
353 >        if (testImplementationDetails)
354 >            assertTrue(f.toString().startsWith(
355 >                               identityString(f) + "[Completed exceptionally: "));
356 >    }
357  
358 +    public void testToString_cancelled() {
359          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
360 <            f = new CompletableFuture<String>();
360 >            CompletableFuture<String> f = new CompletableFuture<String>();
361              assertTrue(f.cancel(mayInterruptIfRunning));
362 <            assertTrue(f.toString().contains("[Completed exceptionally]"));
362 >            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
363 >            if (testImplementationDetails)
364 >                assertTrue(f.toString().startsWith(
365 >                                   identityString(f) + "[Completed exceptionally: "));
366          }
367      }
368  
# Line 362 | Line 374 | public class CompletableFutureTest exten
374          checkCompletedNormally(f, "test");
375      }
376  
377 <    abstract class CheckedAction {
377 >    abstract static class CheckedAction {
378          int invocationCount = 0;
379          final ExecutionMode m;
380          CheckedAction(ExecutionMode m) { this.m = m; }
# Line 374 | Line 386 | public class CompletableFutureTest exten
386          void assertInvoked() { assertEquals(1, invocationCount); }
387      }
388  
389 <    abstract class CheckedIntegerAction extends CheckedAction {
389 >    abstract static class CheckedIntegerAction extends CheckedAction {
390          Integer value;
391          CheckedIntegerAction(ExecutionMode m) { super(m); }
392          void assertValue(Integer expected) {
# Line 383 | Line 395 | public class CompletableFutureTest exten
395          }
396      }
397  
398 <    class IntegerSupplier extends CheckedAction
398 >    static class IntegerSupplier extends CheckedAction
399          implements Supplier<Integer>
400      {
401          final Integer value;
# Line 402 | Line 414 | public class CompletableFutureTest exten
414          return (x == null) ? null : x + 1;
415      }
416  
417 <    class NoopConsumer extends CheckedIntegerAction
417 >    static class NoopConsumer extends CheckedIntegerAction
418          implements Consumer<Integer>
419      {
420          NoopConsumer(ExecutionMode m) { super(m); }
# Line 412 | Line 424 | public class CompletableFutureTest exten
424          }
425      }
426  
427 <    class IncFunction extends CheckedIntegerAction
427 >    static class IncFunction extends CheckedIntegerAction
428          implements Function<Integer,Integer>
429      {
430          IncFunction(ExecutionMode m) { super(m); }
# Line 430 | Line 442 | public class CompletableFutureTest exten
442              - ((y == null) ? 99 : y.intValue());
443      }
444  
445 <    class SubtractAction extends CheckedIntegerAction
445 >    static class SubtractAction extends CheckedIntegerAction
446          implements BiConsumer<Integer, Integer>
447      {
448          SubtractAction(ExecutionMode m) { super(m); }
# Line 440 | Line 452 | public class CompletableFutureTest exten
452          }
453      }
454  
455 <    class SubtractFunction extends CheckedIntegerAction
455 >    static class SubtractFunction extends CheckedIntegerAction
456          implements BiFunction<Integer, Integer, Integer>
457      {
458          SubtractFunction(ExecutionMode m) { super(m); }
# Line 450 | Line 462 | public class CompletableFutureTest exten
462          }
463      }
464  
465 <    class Noop extends CheckedAction implements Runnable {
465 >    static class Noop extends CheckedAction implements Runnable {
466          Noop(ExecutionMode m) { super(m); }
467          public void run() {
468              invoked();
469          }
470      }
471  
472 <    class FailingSupplier extends CheckedAction
472 >    static class FailingSupplier extends CheckedAction
473          implements Supplier<Integer>
474      {
475          final CFException ex;
# Line 468 | Line 480 | public class CompletableFutureTest exten
480          }
481      }
482  
483 <    class FailingConsumer extends CheckedIntegerAction
483 >    static class FailingConsumer extends CheckedIntegerAction
484          implements Consumer<Integer>
485      {
486          final CFException ex;
# Line 480 | Line 492 | public class CompletableFutureTest exten
492          }
493      }
494  
495 <    class FailingBiConsumer extends CheckedIntegerAction
495 >    static class FailingBiConsumer extends CheckedIntegerAction
496          implements BiConsumer<Integer, Integer>
497      {
498          final CFException ex;
# Line 492 | Line 504 | public class CompletableFutureTest exten
504          }
505      }
506  
507 <    class FailingFunction extends CheckedIntegerAction
507 >    static class FailingFunction extends CheckedIntegerAction
508          implements Function<Integer, Integer>
509      {
510          final CFException ex;
# Line 504 | Line 516 | public class CompletableFutureTest exten
516          }
517      }
518  
519 <    class FailingBiFunction extends CheckedIntegerAction
519 >    static class FailingBiFunction extends CheckedIntegerAction
520          implements BiFunction<Integer, Integer, Integer>
521      {
522          final CFException ex;
# Line 516 | Line 528 | public class CompletableFutureTest exten
528          }
529      }
530  
531 <    class FailingRunnable extends CheckedAction implements Runnable {
531 >    static class FailingRunnable extends CheckedAction implements Runnable {
532          final CFException ex;
533          FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
534          public void run() {
# Line 525 | Line 537 | public class CompletableFutureTest exten
537          }
538      }
539  
540 <    class CompletableFutureInc extends CheckedIntegerAction
540 >    static class CompletableFutureInc extends CheckedIntegerAction
541          implements Function<Integer, CompletableFuture<Integer>>
542      {
543          CompletableFutureInc(ExecutionMode m) { super(m); }
# Line 538 | Line 550 | public class CompletableFutureTest exten
550          }
551      }
552  
553 <    class FailingCompletableFutureFunction extends CheckedIntegerAction
553 >    static class FailingCompletableFutureFunction extends CheckedIntegerAction
554          implements Function<Integer, CompletableFuture<Integer>>
555      {
556          final CFException ex;
# Line 1243 | Line 1255 | public class CompletableFutureTest exten
1255          r.assertInvoked();
1256      }}
1257  
1258 +    @SuppressWarnings("FutureReturnValueIgnored")
1259      public void testRunAsync_rejectingExecutor() {
1260          CountingRejectingExecutor e = new CountingRejectingExecutor();
1261          try {
# Line 1289 | Line 1302 | public class CompletableFutureTest exten
1302          r.assertInvoked();
1303      }}
1304  
1305 +    @SuppressWarnings("FutureReturnValueIgnored")
1306      public void testSupplyAsync_rejectingExecutor() {
1307          CountingRejectingExecutor e = new CountingRejectingExecutor();
1308          try {
# Line 2563 | Line 2577 | public class CompletableFutureTest exten
2577  
2578          // unspecified behavior - both source completions available
2579          try {
2580 <            assertEquals(null, h0.join());
2580 >            assertNull(h0.join());
2581              rs[0].assertValue(v1);
2582          } catch (CompletionException ok) {
2583              checkCompletedWithWrappedException(h0, ex);
2584              rs[0].assertNotInvoked();
2585          }
2586          try {
2587 <            assertEquals(null, h1.join());
2587 >            assertNull(h1.join());
2588              rs[1].assertValue(v1);
2589          } catch (CompletionException ok) {
2590              checkCompletedWithWrappedException(h1, ex);
2591              rs[1].assertNotInvoked();
2592          }
2593          try {
2594 <            assertEquals(null, h2.join());
2594 >            assertNull(h2.join());
2595              rs[2].assertValue(v1);
2596          } catch (CompletionException ok) {
2597              checkCompletedWithWrappedException(h2, ex);
2598              rs[2].assertNotInvoked();
2599          }
2600          try {
2601 <            assertEquals(null, h3.join());
2601 >            assertNull(h3.join());
2602              rs[3].assertValue(v1);
2603          } catch (CompletionException ok) {
2604              checkCompletedWithWrappedException(h3, ex);
# Line 2700 | Line 2714 | public class CompletableFutureTest exten
2714          for (ExecutionMode m : ExecutionMode.values())
2715          for (Integer v1 : new Integer[] { 1, null })
2716          for (Integer v2 : new Integer[] { 2, null })
2717 +        for (boolean pushNop : new boolean[] { true, false })
2718      {
2719          final CompletableFuture<Integer> f = new CompletableFuture<>();
2720          final CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 2712 | Line 2727 | public class CompletableFutureTest exten
2727          checkIncomplete(h1);
2728          rs[0].assertNotInvoked();
2729          rs[1].assertNotInvoked();
2730 +        if (pushNop) {          // ad hoc test of intra-completion interference
2731 +            m.thenRun(f, () -> {});
2732 +            m.thenRun(g, () -> {});
2733 +        }
2734          f.complete(v1);
2735          checkCompletedNormally(h0, null);
2736          checkCompletedNormally(h1, null);
# Line 2818 | Line 2837 | public class CompletableFutureTest exten
2837  
2838          // unspecified behavior - both source completions available
2839          try {
2840 <            assertEquals(null, h0.join());
2840 >            assertNull(h0.join());
2841              rs[0].assertInvoked();
2842          } catch (CompletionException ok) {
2843              checkCompletedWithWrappedException(h0, ex);
2844              rs[0].assertNotInvoked();
2845          }
2846          try {
2847 <            assertEquals(null, h1.join());
2847 >            assertNull(h1.join());
2848              rs[1].assertInvoked();
2849          } catch (CompletionException ok) {
2850              checkCompletedWithWrappedException(h1, ex);
2851              rs[1].assertNotInvoked();
2852          }
2853          try {
2854 <            assertEquals(null, h2.join());
2854 >            assertNull(h2.join());
2855              rs[2].assertInvoked();
2856          } catch (CompletionException ok) {
2857              checkCompletedWithWrappedException(h2, ex);
2858              rs[2].assertNotInvoked();
2859          }
2860          try {
2861 <            assertEquals(null, h3.join());
2861 >            assertNull(h3.join());
2862              rs[3].assertInvoked();
2863          } catch (CompletionException ok) {
2864              checkCompletedWithWrappedException(h3, ex);
# Line 3234 | Line 3253 | public class CompletableFutureTest exten
3253      /**
3254       * Completion methods throw NullPointerException with null arguments
3255       */
3256 +    @SuppressWarnings("FutureReturnValueIgnored")
3257      public void testNPE() {
3258          CompletableFuture<Integer> f = new CompletableFuture<>();
3259          CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 3253 | Line 3273 | public class CompletableFutureTest exten
3273  
3274              () -> f.thenApply(null),
3275              () -> f.thenApplyAsync(null),
3276 <            () -> f.thenApplyAsync((x) -> x, null),
3276 >            () -> f.thenApplyAsync(x -> x, null),
3277              () -> f.thenApplyAsync(null, exec),
3278  
3279              () -> f.thenAccept(null),
3280              () -> f.thenAcceptAsync(null),
3281 <            () -> f.thenAcceptAsync((x) -> {} , null),
3281 >            () -> f.thenAcceptAsync(x -> {} , null),
3282              () -> f.thenAcceptAsync(null, exec),
3283  
3284              () -> f.thenRun(null),
# Line 3293 | Line 3313 | public class CompletableFutureTest exten
3313              () -> f.applyToEither(g, null),
3314              () -> f.applyToEitherAsync(g, null),
3315              () -> f.applyToEitherAsync(g, null, exec),
3316 <            () -> f.applyToEither(nullFuture, (x) -> x),
3317 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3318 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3319 <            () -> f.applyToEitherAsync(g, (x) -> x, null),
3316 >            () -> f.applyToEither(nullFuture, x -> x),
3317 >            () -> f.applyToEitherAsync(nullFuture, x -> x),
3318 >            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
3319 >            () -> f.applyToEitherAsync(g, x -> x, null),
3320  
3321              () -> f.acceptEither(g, null),
3322              () -> f.acceptEitherAsync(g, null),
3323              () -> f.acceptEitherAsync(g, null, exec),
3324 <            () -> f.acceptEither(nullFuture, (x) -> {}),
3325 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3326 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3327 <            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3324 >            () -> f.acceptEither(nullFuture, x -> {}),
3325 >            () -> f.acceptEitherAsync(nullFuture, x -> {}),
3326 >            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
3327 >            () -> f.acceptEitherAsync(g, x -> {}, null),
3328  
3329              () -> f.runAfterEither(g, null),
3330              () -> f.runAfterEitherAsync(g, null),
# Line 3354 | Line 3374 | public class CompletableFutureTest exten
3374       * Test submissions to an executor that rejects all tasks.
3375       */
3376      public void testRejectingExecutor() {
3377 <        for (Integer v : new Integer[] { 1, null }) {
3378 <
3377 >        for (Integer v : new Integer[] { 1, null })
3378 >    {
3379          final CountingRejectingExecutor e = new CountingRejectingExecutor();
3380  
3381          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
# Line 3370 | Line 3390 | public class CompletableFutureTest exten
3390          for (CompletableFuture<Integer> src : srcs) {
3391              List<CompletableFuture<?>> fs = new ArrayList<>();
3392              fs.add(src.thenRunAsync(() -> {}, e));
3393 <            fs.add(src.thenAcceptAsync((z) -> {}, e));
3394 <            fs.add(src.thenApplyAsync((z) -> z, e));
3393 >            fs.add(src.thenAcceptAsync(z -> {}, e));
3394 >            fs.add(src.thenApplyAsync(z -> z, e));
3395  
3396              fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3397              fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3398              fs.add(src.runAfterBothAsync(src, () -> {}, e));
3399  
3400 <            fs.add(src.applyToEitherAsync(src, (z) -> z, e));
3401 <            fs.add(src.acceptEitherAsync(src, (z) -> {}, e));
3400 >            fs.add(src.applyToEitherAsync(src, z -> z, e));
3401 >            fs.add(src.acceptEitherAsync(src, z -> {}, e));
3402              fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3403  
3404 <            fs.add(src.thenComposeAsync((z) -> null, e));
3404 >            fs.add(src.thenComposeAsync(z -> null, e));
3405              fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3406              fs.add(src.handleAsync((z, t) -> null, e));
3407  
# Line 3414 | Line 3434 | public class CompletableFutureTest exten
3434          {
3435              List<CompletableFuture<?>> fs = new ArrayList<>();
3436  
3437 <            fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e));
3438 <            fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e));
3437 >            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
3438 >            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
3439  
3440 <            fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e));
3441 <            fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e));
3440 >            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
3441 >            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
3442  
3443              fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3444              fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
# Line 3434 | Line 3454 | public class CompletableFutureTest exten
3454              checkCompletedWithWrappedException(future, e.ex);
3455  
3456          assertEquals(futures.size(), e.count.get());
3457 <
3438 <        }
3439 <    }
3457 >    }}
3458  
3459      /**
3460       * Test submissions to an executor that rejects all tasks, but
# Line 3444 | Line 3462 | public class CompletableFutureTest exten
3462       * explicitly completed.
3463       */
3464      public void testRejectingExecutorNeverInvoked() {
3465 +        for (Integer v : new Integer[] { 1, null })
3466 +    {
3467          final CountingRejectingExecutor e = new CountingRejectingExecutor();
3468  
3449        for (Integer v : new Integer[] { 1, null }) {
3450
3469          final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3470          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3471  
# Line 3459 | Line 3477 | public class CompletableFutureTest exten
3477  
3478          List<CompletableFuture<?>> fs = new ArrayList<>();
3479          fs.add(incomplete.thenRunAsync(() -> {}, e));
3480 <        fs.add(incomplete.thenAcceptAsync((z) -> {}, e));
3481 <        fs.add(incomplete.thenApplyAsync((z) -> z, e));
3480 >        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
3481 >        fs.add(incomplete.thenApplyAsync(z -> z, e));
3482  
3483          fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3484          fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3485          fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3486  
3487 <        fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e));
3488 <        fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e));
3487 >        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
3488 >        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
3489          fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3490  
3491 <        fs.add(incomplete.thenComposeAsync((z) -> null, e));
3491 >        fs.add(incomplete.thenComposeAsync(z -> null, e));
3492          fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3493          fs.add(incomplete.handleAsync((z, t) -> null, e));
3494  
# Line 3495 | Line 3513 | public class CompletableFutureTest exten
3513              checkCompletedNormally(future, null);
3514  
3515          assertEquals(0, e.count.get());
3516 <
3499 <        }
3500 <    }
3516 >    }}
3517  
3518      /**
3519       * toCompletableFuture returns this CompletableFuture.
# Line 3532 | Line 3548 | public class CompletableFutureTest exten
3548       */
3549      public void testCompletedStage() {
3550          AtomicInteger x = new AtomicInteger(0);
3551 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3551 >        AtomicReference<Throwable> r = new AtomicReference<>();
3552          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3553          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3554          assertEquals(x.get(), 1);
# Line 3577 | Line 3593 | public class CompletableFutureTest exten
3593       * copy returns a CompletableFuture that is completed normally,
3594       * with the same value, when source is.
3595       */
3596 <    public void testCopy() {
3596 >    public void testCopy_normalCompletion() {
3597 >        for (boolean createIncomplete : new boolean[] { true, false })
3598 >        for (Integer v1 : new Integer[] { 1, null })
3599 >    {
3600          CompletableFuture<Integer> f = new CompletableFuture<>();
3601 +        if (!createIncomplete) assertTrue(f.complete(v1));
3602          CompletableFuture<Integer> g = f.copy();
3603 <        checkIncomplete(f);
3604 <        checkIncomplete(g);
3605 <        f.complete(1);
3606 <        checkCompletedNormally(f, 1);
3607 <        checkCompletedNormally(g, 1);
3608 <    }
3603 >        if (createIncomplete) {
3604 >            checkIncomplete(f);
3605 >            checkIncomplete(g);
3606 >            assertTrue(f.complete(v1));
3607 >        }
3608 >        checkCompletedNormally(f, v1);
3609 >        checkCompletedNormally(g, v1);
3610 >    }}
3611  
3612      /**
3613       * copy returns a CompletableFuture that is completed exceptionally
3614       * when source is.
3615       */
3616 <    public void testCopy2() {
3616 >    public void testCopy_exceptionalCompletion() {
3617 >        for (boolean createIncomplete : new boolean[] { true, false })
3618 >    {
3619 >        CFException ex = new CFException();
3620          CompletableFuture<Integer> f = new CompletableFuture<>();
3621 +        if (!createIncomplete) f.completeExceptionally(ex);
3622          CompletableFuture<Integer> g = f.copy();
3623 <        checkIncomplete(f);
3624 <        checkIncomplete(g);
3625 <        CFException ex = new CFException();
3626 <        f.completeExceptionally(ex);
3623 >        if (createIncomplete) {
3624 >            checkIncomplete(f);
3625 >            checkIncomplete(g);
3626 >            f.completeExceptionally(ex);
3627 >        }
3628          checkCompletedExceptionally(f, ex);
3629          checkCompletedWithWrappedException(g, ex);
3630 +    }}
3631 +
3632 +    /**
3633 +     * Completion of a copy does not complete its source.
3634 +     */
3635 +    public void testCopy_oneWayPropagation() {
3636 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3637 +        assertTrue(f.copy().complete(1));
3638 +        assertTrue(f.copy().complete(null));
3639 +        assertTrue(f.copy().cancel(true));
3640 +        assertTrue(f.copy().cancel(false));
3641 +        assertTrue(f.copy().completeExceptionally(new CFException()));
3642 +        checkIncomplete(f);
3643      }
3644  
3645      /**
# Line 3610 | 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          checkIncomplete(f);
3655          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3656          f.complete(1);
# Line 3627 | Line 3667 | public class CompletableFutureTest exten
3667          CompletableFuture<Integer> f = new CompletableFuture<>();
3668          CompletionStage<Integer> g = f.minimalCompletionStage();
3669          AtomicInteger x = new AtomicInteger(0);
3670 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3670 >        AtomicReference<Throwable> r = new AtomicReference<>();
3671          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3672          checkIncomplete(f);
3673          CFException ex = new CFException();
# Line 3645 | Line 3685 | public class CompletableFutureTest exten
3685          CFException ex = new CFException();
3686          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3687          AtomicInteger x = new AtomicInteger(0);
3688 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3688 >        AtomicReference<Throwable> r = new AtomicReference<>();
3689          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3690          assertEquals(x.get(), 0);
3691          assertEquals(r.get(), ex);
# Line 3669 | Line 3709 | public class CompletableFutureTest exten
3709      public void testCompleteAsync2() {
3710          CompletableFuture<Integer> f = new CompletableFuture<>();
3711          CFException ex = new CFException();
3712 <        f.completeAsync(() -> {if (true) throw ex; return 1;});
3712 >        f.completeAsync(() -> { throw ex; });
3713          try {
3714              f.join();
3715              shouldThrow();
# Line 3699 | Line 3739 | public class CompletableFutureTest exten
3739          CompletableFuture<Integer> f = new CompletableFuture<>();
3740          CFException ex = new CFException();
3741          ThreadExecutor executor = new ThreadExecutor();
3742 <        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3742 >        f.completeAsync(() -> { throw ex; }, executor);
3743          try {
3744              f.join();
3745              shouldThrow();
# Line 3851 | Line 3891 | public class CompletableFutureTest exten
3891          final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3892          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3893  
3894 +        final Runnable noopRunnable = new Noop(m);
3895 +        final Consumer<Integer> noopConsumer = new NoopConsumer(m);
3896 +        final Function<Integer, Integer> incFunction = new IncFunction(m);
3897 +
3898          List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3899              = new ArrayList<>();
3900  
3901 <        funs.add((y) -> m.thenRun(y, new Noop(m)));
3902 <        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3903 <        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3904 <
3905 <        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3906 <        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3907 <        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3908 <
3909 <        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3910 <        funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m)));
3911 <        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3912 <        funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3913 <        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3914 <        funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m)));
3915 <
3916 <        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3917 <
3918 <        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3919 <
3920 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y}));
3921 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3922 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y}));
3923 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y}));
3924 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3925 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y}));
3901 >        funs.add(y -> m.thenRun(y, noopRunnable));
3902 >        funs.add(y -> m.thenAccept(y, noopConsumer));
3903 >        funs.add(y -> m.thenApply(y, incFunction));
3904 >
3905 >        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
3906 >        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
3907 >        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
3908 >
3909 >        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
3910 >        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
3911 >        funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3912 >        funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3913 >        funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m)));
3914 >        funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m)));
3915 >
3916 >        funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3917 >
3918 >        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
3919 >
3920 >        funs.add(y -> CompletableFuture.allOf(y));
3921 >        funs.add(y -> CompletableFuture.allOf(y, v42));
3922 >        funs.add(y -> CompletableFuture.allOf(v42, y));
3923 >        funs.add(y -> CompletableFuture.anyOf(y));
3924 >        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
3925 >        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
3926  
3927          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3928                   fun : funs) {
3929              CompletableFuture<Integer> f = new CompletableFuture<>();
3930              f.completeExceptionally(ex);
3931 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3931 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3932              checkCompletedWithWrappedException(src, ex);
3933              CompletableFuture<?> dep = fun.apply(src);
3934              checkCompletedWithWrappedException(dep, ex);
# Line 3894 | Line 3938 | public class CompletableFutureTest exten
3938          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3939                   fun : funs) {
3940              CompletableFuture<Integer> f = new CompletableFuture<>();
3941 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3941 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3942              CompletableFuture<?> dep = fun.apply(src);
3943              f.completeExceptionally(ex);
3944              checkCompletedWithWrappedException(src, ex);
# Line 3908 | Line 3952 | public class CompletableFutureTest exten
3952              CompletableFuture<Integer> f = new CompletableFuture<>();
3953              f.cancel(mayInterruptIfRunning);
3954              checkCancelled(f);
3955 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3955 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3956              checkCompletedWithWrappedCancellationException(src);
3957              CompletableFuture<?> dep = fun.apply(src);
3958              checkCompletedWithWrappedCancellationException(dep);
# Line 3919 | Line 3963 | public class CompletableFutureTest exten
3963          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3964                   fun : funs) {
3965              CompletableFuture<Integer> f = new CompletableFuture<>();
3966 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3966 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3967              CompletableFuture<?> dep = fun.apply(src);
3968              f.cancel(mayInterruptIfRunning);
3969              checkCancelled(f);
# Line 3930 | Line 3974 | public class CompletableFutureTest exten
3974      }}
3975  
3976      /**
3977 <     * Minimal completion stages throw UOE for all non-CompletionStage methods
3977 >     * Minimal completion stages throw UOE for most non-CompletionStage methods
3978       */
3979      public void testMinimalCompletionStage_minimality() {
3980          if (!testImplementationDetails) return;
3981          Function<Method, String> toSignature =
3982 <            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3982 >            method -> method.getName() + Arrays.toString(method.getParameterTypes());
3983          Predicate<Method> isNotStatic =
3984 <            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3984 >            method -> (method.getModifiers() & Modifier.STATIC) == 0;
3985          List<Method> minimalMethods =
3986              Stream.of(Object.class, CompletionStage.class)
3987 <            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3987 >            .flatMap(klazz -> Stream.of(klazz.getMethods()))
3988              .filter(isNotStatic)
3989              .collect(Collectors.toList());
3990          // Methods from CompletableFuture permitted NOT to throw UOE
# Line 3956 | Line 4000 | public class CompletableFutureTest exten
4000              .collect(Collectors.toSet());
4001          List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
4002              .filter(isNotStatic)
4003 <            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4003 >            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4004              .collect(Collectors.toList());
4005  
4006 <        CompletionStage<Integer> minimalStage =
4006 >        List<CompletionStage<Integer>> stages = new ArrayList<>();
4007 >        CompletionStage<Integer> min =
4008              new CompletableFuture<Integer>().minimalCompletionStage();
4009 +        stages.add(min);
4010 +        stages.add(min.thenApply(x -> x));
4011 +        stages.add(CompletableFuture.completedStage(1));
4012 +        stages.add(CompletableFuture.failedStage(new CFException()));
4013  
4014          List<Method> bugs = new ArrayList<>();
4015          for (Method method : allMethods) {
# Line 3976 | Line 4025 | public class CompletableFutureTest exten
4025                  else if (parameterTypes[i] == long.class)
4026                      args[i] = 0L;
4027              }
4028 <            try {
4029 <                method.invoke(minimalStage, args);
4030 <                bugs.add(method);
3982 <            }
3983 <            catch (java.lang.reflect.InvocationTargetException expected) {
3984 <                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4028 >            for (CompletionStage<Integer> stage : stages) {
4029 >                try {
4030 >                    method.invoke(stage, args);
4031                      bugs.add(method);
3986                    // expected.getCause().printStackTrace();
4032                  }
4033 +                catch (java.lang.reflect.InvocationTargetException expected) {
4034 +                    if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4035 +                        bugs.add(method);
4036 +                        // expected.getCause().printStackTrace();
4037 +                    }
4038 +                }
4039 +                catch (ReflectiveOperationException bad) { throw new Error(bad); }
4040              }
3989            catch (ReflectiveOperationException bad) { throw new Error(bad); }
4041          }
4042          if (!bugs.isEmpty())
4043 <            throw new Error("Methods did not throw UOE: " + bugs.toString());
4043 >            throw new Error("Methods did not throw UOE: " + bugs);
4044 >    }
4045 >
4046 >    /**
4047 >     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4048 >     * is completed normally, with the same value, when source is.
4049 >     */
4050 >    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
4051 >        for (boolean createIncomplete : new boolean[] { true, false })
4052 >        for (Integer v1 : new Integer[] { 1, null })
4053 >    {
4054 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4055 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4056 >        if (!createIncomplete) assertTrue(f.complete(v1));
4057 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4058 >        if (createIncomplete) {
4059 >            checkIncomplete(f);
4060 >            checkIncomplete(g);
4061 >            assertTrue(f.complete(v1));
4062 >        }
4063 >        checkCompletedNormally(f, v1);
4064 >        checkCompletedNormally(g, v1);
4065 >    }}
4066 >
4067 >    /**
4068 >     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4069 >     * is completed exceptionally when source is.
4070 >     */
4071 >    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
4072 >        for (boolean createIncomplete : new boolean[] { true, false })
4073 >    {
4074 >        CFException ex = new CFException();
4075 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4076 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4077 >        if (!createIncomplete) f.completeExceptionally(ex);
4078 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4079 >        if (createIncomplete) {
4080 >            checkIncomplete(f);
4081 >            checkIncomplete(g);
4082 >            f.completeExceptionally(ex);
4083 >        }
4084 >        checkCompletedExceptionally(f, ex);
4085 >        checkCompletedWithWrappedException(g, ex);
4086 >    }}
4087 >
4088 >    /**
4089 >     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
4090 >     */
4091 >    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
4092 >        for (Integer v1 : new Integer[] { 1, null })
4093 >    {
4094 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4095 >        CompletionStage minimal = f.minimalCompletionStage();
4096 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4097 >        assertTrue(g.complete(v1));
4098 >        checkCompletedNormally(g, v1);
4099 >        checkIncomplete(f);
4100 >        checkIncomplete(minimal.toCompletableFuture());
4101 >    }}
4102 >
4103 >    /**
4104 >     * minimalStage.toCompletableFuture().join() awaits completion
4105 >     */
4106 >    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
4107 >        for (boolean createIncomplete : new boolean[] { true, false })
4108 >        for (Integer v1 : new Integer[] { 1, null })
4109 >    {
4110 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4111 >        if (!createIncomplete) assertTrue(f.complete(v1));
4112 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4113 >        if (createIncomplete) assertTrue(f.complete(v1));
4114 >        assertEquals(v1, minimal.toCompletableFuture().join());
4115 >        assertEquals(v1, minimal.toCompletableFuture().get());
4116 >        checkCompletedNormally(minimal.toCompletableFuture(), v1);
4117 >    }}
4118 >
4119 >    /**
4120 >     * Completion of a toCompletableFuture copy of a minimal stage
4121 >     * does not complete its source.
4122 >     */
4123 >    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
4124 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4125 >        CompletionStage<Integer> g = f.minimalCompletionStage();
4126 >        assertTrue(g.toCompletableFuture().complete(1));
4127 >        assertTrue(g.toCompletableFuture().complete(null));
4128 >        assertTrue(g.toCompletableFuture().cancel(true));
4129 >        assertTrue(g.toCompletableFuture().cancel(false));
4130 >        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
4131 >        checkIncomplete(g.toCompletableFuture());
4132 >        f.complete(1);
4133 >        checkCompletedNormally(g.toCompletableFuture(), 1);
4134 >    }
4135 >
4136 >    /** Demo utility method for external reliable toCompletableFuture */
4137 >    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
4138 >        CompletableFuture<T> f = new CompletableFuture<>();
4139 >        stage.handle((T t, Throwable ex) -> {
4140 >                         if (ex != null) f.completeExceptionally(ex);
4141 >                         else f.complete(t);
4142 >                         return null;
4143 >                     });
4144 >        return f;
4145 >    }
4146 >
4147 >    /** Demo utility method to join a CompletionStage */
4148 >    static <T> T join(CompletionStage<T> stage) {
4149 >        return toCompletableFuture(stage).join();
4150      }
4151  
4152 +    /**
4153 +     * Joining a minimal stage "by hand" works
4154 +     */
4155 +    public void testMinimalCompletionStage_join_by_hand() {
4156 +        for (boolean createIncomplete : new boolean[] { true, false })
4157 +        for (Integer v1 : new Integer[] { 1, null })
4158 +    {
4159 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4160 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4161 +        CompletableFuture<Integer> g = new CompletableFuture<>();
4162 +        if (!createIncomplete) assertTrue(f.complete(v1));
4163 +        minimal.thenAccept(x -> g.complete(x));
4164 +        if (createIncomplete) assertTrue(f.complete(v1));
4165 +        g.join();
4166 +        checkCompletedNormally(g, v1);
4167 +        checkCompletedNormally(f, v1);
4168 +        assertEquals(v1, join(minimal));
4169 +    }}
4170 +
4171      static class Monad {
4172          static class ZeroException extends RuntimeException {
4173              public ZeroException() { super("monadic zero"); }
# Line 4008 | Line 4184 | public class CompletableFutureTest exten
4184          static <T,U,V> Function<T, CompletableFuture<V>> compose
4185              (Function<T, CompletableFuture<U>> f,
4186               Function<U, CompletableFuture<V>> g) {
4187 <            return (x) -> f.apply(x).thenCompose(g);
4187 >            return x -> f.apply(x).thenCompose(g);
4188          }
4189  
4190          static void assertZero(CompletableFuture<?> f) {
# Line 4088 | Line 4264 | public class CompletableFutureTest exten
4264  
4265          // Some mutually non-commutative functions
4266          Function<Long, CompletableFuture<Long>> triple
4267 <            = (x) -> Monad.unit(3 * x);
4267 >            = x -> Monad.unit(3 * x);
4268          Function<Long, CompletableFuture<Long>> inc
4269 <            = (x) -> Monad.unit(x + 1);
4269 >            = x -> Monad.unit(x + 1);
4270  
4271          // unit is a right identity: m >>= unit === m
4272          Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
# Line 4102 | Line 4278 | public class CompletableFutureTest exten
4278          // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
4279          Monad.assertFutureEquals(
4280              unit.apply(5L).thenCompose(inc).thenCompose(triple),
4281 <            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
4281 >            unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple)));
4282  
4283          // The case for CompletableFuture as an additive monad is weaker...
4284  
# Line 4112 | Line 4288 | public class CompletableFutureTest exten
4288          // left zero: zero >>= f === zero
4289          Monad.assertZero(zero.thenCompose(inc));
4290          // right zero: f >>= (\x -> zero) === zero
4291 <        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
4291 >        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
4292  
4293          // f plus zero === f
4294          Monad.assertFutureEquals(Monad.unit(5L),
# Line 4138 | Line 4314 | public class CompletableFutureTest exten
4314                                   Monad.plus(godot, Monad.unit(5L)));
4315      }
4316  
4317 +    /** Test long recursive chains of CompletableFutures with cascading completions */
4318 +    @SuppressWarnings("FutureReturnValueIgnored")
4319 +    public void testRecursiveChains() throws Throwable {
4320 +        for (ExecutionMode m : ExecutionMode.values())
4321 +        for (boolean addDeadEnds : new boolean[] { true, false })
4322 +    {
4323 +        final int val = 42;
4324 +        final int n = expensiveTests ? 1_000 : 2;
4325 +        CompletableFuture<Integer> head = new CompletableFuture<>();
4326 +        CompletableFuture<Integer> tail = head;
4327 +        for (int i = 0; i < n; i++) {
4328 +            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
4329 +            tail = m.thenApply(tail, v -> v + 1);
4330 +            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
4331 +            tail = m.applyToEither(tail, tail, v -> v + 1);
4332 +            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
4333 +            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
4334 +        }
4335 +        head.complete(val);
4336 +        assertEquals(val + 3 * n, (int) tail.join());
4337 +    }}
4338 +
4339      /**
4340       * A single CompletableFuture with many dependents.
4341       * A demo of scalability - runtime is O(n).
4342       */
4343 +    @SuppressWarnings("FutureReturnValueIgnored")
4344      public void testManyDependents() throws Throwable {
4345          final int n = expensiveTests ? 1_000_000 : 10;
4346          final CompletableFuture<Void> head = new CompletableFuture<>();
# Line 4149 | Line 4348 | public class CompletableFutureTest exten
4348          final AtomicInteger count = new AtomicInteger(0);
4349          for (int i = 0; i < n; i++) {
4350              head.thenRun(() -> count.getAndIncrement());
4351 <            head.thenAccept((x) -> count.getAndIncrement());
4352 <            head.thenApply((x) -> count.getAndIncrement());
4351 >            head.thenAccept(x -> count.getAndIncrement());
4352 >            head.thenApply(x -> count.getAndIncrement());
4353  
4354              head.runAfterBoth(complete, () -> count.getAndIncrement());
4355              head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
# Line 4160 | Line 4359 | public class CompletableFutureTest exten
4359              complete.thenCombine(head, (x, y) -> count.getAndIncrement());
4360  
4361              head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
4362 <            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4363 <            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4362 >            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4363 >            head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4364              new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
4365 <            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
4366 <            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
4365 >            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
4366 >            new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement());
4367          }
4368          head.complete(null);
4369          assertEquals(5 * 3 * n, count.get());
4370      }
4371  
4372 <    /** ant -Dvmoptions=-Xmx8m -Djsr166.tckTestClass=CompletableFutureTest tck */
4373 <    public void testCoCompletionGarbage() throws Throwable {
4372 >    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4373 >    @SuppressWarnings("FutureReturnValueIgnored")
4374 >    public void testCoCompletionGarbageRetention() throws Throwable {
4375          final int n = expensiveTests ? 1_000_000 : 10;
4376          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4377          CompletableFuture<Integer> f;
# Line 4181 | Line 4381 | public class CompletableFutureTest exten
4381              f.complete(null);
4382  
4383              f = new CompletableFuture<>();
4384 <            f.acceptEither(incomplete, (x) -> {});
4384 >            f.acceptEither(incomplete, x -> {});
4385              f.complete(null);
4386  
4387              f = new CompletableFuture<>();
4388 <            f.applyToEither(incomplete, (x) -> x);
4388 >            f.applyToEither(incomplete, x -> x);
4389              f.complete(null);
4390  
4391              f = new CompletableFuture<>();
# Line 4199 | Line 4399 | public class CompletableFutureTest exten
4399              f.complete(null);
4400  
4401              f = new CompletableFuture<>();
4402 <            incomplete.acceptEither(f, (x) -> {});
4402 >            incomplete.acceptEither(f, x -> {});
4403              f.complete(null);
4404  
4405              f = new CompletableFuture<>();
4406 <            incomplete.applyToEither(f, (x) -> x);
4406 >            incomplete.applyToEither(f, x -> x);
4407              f.complete(null);
4408  
4409              f = new CompletableFuture<>();
# Line 4212 | Line 4412 | public class CompletableFutureTest exten
4412          }
4413      }
4414  
4415 <    /*
4416 <     * Tests below currently fail in stress mode due to memory retention.
4417 <     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck
4415 >    /**
4416 >     * Reproduction recipe for:
4417 >     * 8160402: Garbage retention with CompletableFuture.anyOf
4418 >     * 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
4419       */
4219
4220    /** Checks for garbage retention with anyOf. */
4420      public void testAnyOfGarbageRetention() throws Throwable {
4421          for (Integer v : new Integer[] { 1, null })
4422      {
# Line 4231 | Line 4430 | public class CompletableFutureTest exten
4430              checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4431      }}
4432  
4433 <    /** Checks for garbage retention with allOf. */
4433 >    /**
4434 >     * Checks for garbage retention with allOf.
4435 >     *
4436 >     * As of 2016-07, fails with OOME:
4437 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck
4438 >     */
4439      public void testCancelledAllOfGarbageRetention() throws Throwable {
4440          final int n = expensiveTests ? 100_000 : 10;
4441          CompletableFuture<Integer>[] fs
# Line 4242 | Line 4446 | public class CompletableFutureTest exten
4446              assertTrue(CompletableFuture.allOf(fs).cancel(false));
4447      }
4448  
4449 +    /**
4450 +     * Checks for garbage retention when a dependent future is
4451 +     * cancelled and garbage-collected.
4452 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4453 +     *
4454 +     * As of 2016-07, fails with OOME:
4455 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck
4456 +     */
4457 +    public void testCancelledGarbageRetention() throws Throwable {
4458 +        final int n = expensiveTests ? 100_000 : 10;
4459 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4460 +        for (int i = 0; i < n; i++)
4461 +            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
4462 +    }
4463 +
4464 +    /**
4465 +     * Checks for garbage retention when MinimalStage.toCompletableFuture()
4466 +     * is invoked many times.
4467 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4468 +     *
4469 +     * As of 2016-07, fails with OOME:
4470 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
4471 +     */
4472 +    public void testToCompletableFutureGarbageRetention() throws Throwable {
4473 +        final int n = expensiveTests ? 900_000 : 10;
4474 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4475 +        CompletionStage minimal = neverCompleted.minimalCompletionStage();
4476 +        for (int i = 0; i < n; i++)
4477 +            assertTrue(minimal.toCompletableFuture().cancel(true));
4478 +    }
4479 +
4480   //     static <U> U join(CompletionStage<U> stage) {
4481   //         CompletableFuture<U> f = new CompletableFuture<>();
4482   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines