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.166 by jsr166, Sun Jul 3 19:38:19 2016 UTC vs.
Revision 1.208 by jsr166, Sun Sep 23 15:29:31 2018 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 42 | Line 41 | import java.util.function.Function;
41   import java.util.function.Predicate;
42   import java.util.function.Supplier;
43  
45 import junit.framework.AssertionFailedError;
44   import junit.framework.Test;
45   import junit.framework.TestSuite;
46  
# Line 60 | Line 58 | public class CompletableFutureTest exten
58      void checkIncomplete(CompletableFuture<?> f) {
59          assertFalse(f.isDone());
60          assertFalse(f.isCancelled());
61 <        assertTrue(f.toString().contains("Not completed"));
61 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
62 >
63 >        Object result = null;
64          try {
65 <            assertNull(f.getNow(null));
65 >            result = f.getNow(null);
66          } catch (Throwable fail) { threadUnexpectedException(fail); }
67 +        assertNull(result);
68 +
69          try {
70 <            f.get(0L, SECONDS);
70 >            f.get(randomExpiredTimeout(), randomTimeUnit());
71              shouldThrow();
72          }
73          catch (TimeoutException success) {}
74          catch (Throwable fail) { threadUnexpectedException(fail); }
75      }
76  
77 <    <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
78 <        checkTimedGet(f, value);
77 >    <T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) {
78 >        checkTimedGet(f, expectedValue);
79  
80 +        assertEquals(expectedValue, f.join());
81 +        assertEquals(expectedValue, f.getNow(null));
82 +
83 +        T result = null;
84          try {
85 <            assertEquals(value, f.join());
80 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
81 <        try {
82 <            assertEquals(value, f.getNow(null));
83 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
84 <        try {
85 <            assertEquals(value, f.get());
85 >            result = f.get();
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
87 +        assertEquals(expectedValue, result);
88 +
89          assertTrue(f.isDone());
90          assertFalse(f.isCancelled());
91          assertFalse(f.isCompletedExceptionally());
92 <        assertTrue(f.toString().contains("[Completed normally]"));
92 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
93      }
94  
95      /**
96       * Returns the "raw" internal exceptional completion of f,
97       * without any additional wrapping with CompletionException.
98       */
99 <    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
100 <        // handle (and whenComplete) can distinguish between "direct"
101 <        // and "wrapped" exceptional completion
102 <        return f.handle((U u, Throwable t) -> t).join();
99 >    Throwable exceptionalCompletion(CompletableFuture<?> f) {
100 >        // handle (and whenComplete and exceptionally) can distinguish
101 >        // between "direct" and "wrapped" exceptional completion
102 >        return f.handle((u, t) -> t).join();
103      }
104  
105      void checkCompletedExceptionally(CompletableFuture<?> f,
# Line 143 | Line 145 | public class CompletableFutureTest exten
145          assertFalse(f.isCancelled());
146          assertTrue(f.isDone());
147          assertTrue(f.isCompletedExceptionally());
148 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
148 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
149      }
150  
151      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
152          checkCompletedExceptionally(f, true,
153 <            (t) -> assertTrue(t instanceof CFException));
153 >            t -> assertTrue(t instanceof CFException));
154      }
155  
156      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
157          checkCompletedExceptionally(f, true,
158 <            (t) -> assertTrue(t instanceof CancellationException));
158 >            t -> assertTrue(t instanceof CancellationException));
159      }
160  
161      void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
162          checkCompletedExceptionally(f, false,
163 <            (t) -> assertTrue(t instanceof TimeoutException));
163 >            t -> assertTrue(t instanceof TimeoutException));
164      }
165  
166      void checkCompletedWithWrappedException(CompletableFuture<?> f,
167                                              Throwable ex) {
168 <        checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
168 >        checkCompletedExceptionally(f, true, t -> assertSame(t, ex));
169      }
170  
171      void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
172 <        checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
172 >        checkCompletedExceptionally(f, false, t -> assertSame(t, ex));
173      }
174  
175      void checkCancelled(CompletableFuture<?> f) {
# Line 198 | Line 200 | public class CompletableFutureTest exten
200          assertTrue(f.isDone());
201          assertTrue(f.isCompletedExceptionally());
202          assertTrue(f.isCancelled());
203 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
203 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
204      }
205  
206      /**
# Line 297 | Line 299 | public class CompletableFutureTest exten
299          }
300  
301          f = new CompletableFuture<>();
302 <        f.completeExceptionally(ex = new CFException());
302 >        f.completeExceptionally(new CFException());
303          f.obtrudeValue(v1);
304          checkCompletedNormally(f, v1);
305          f.obtrudeException(ex = new CFException());
# Line 334 | Line 336 | public class CompletableFutureTest exten
336      /**
337       * toString indicates current completion state
338       */
339 <    public void testToString() {
340 <        CompletableFuture<String> f;
341 <
342 <        f = new CompletableFuture<String>();
343 <        assertTrue(f.toString().contains("[Not completed]"));
339 >    public void testToString_incomplete() {
340 >        CompletableFuture<String> f = new CompletableFuture<>();
341 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
342 >        if (testImplementationDetails)
343 >            assertEquals(identityString(f) + "[Not completed]",
344 >                         f.toString());
345 >    }
346  
347 +    public void testToString_normal() {
348 +        CompletableFuture<String> f = new CompletableFuture<>();
349          assertTrue(f.complete("foo"));
350 <        assertTrue(f.toString().contains("[Completed normally]"));
350 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
351 >        if (testImplementationDetails)
352 >            assertEquals(identityString(f) + "[Completed normally]",
353 >                         f.toString());
354 >    }
355  
356 <        f = new CompletableFuture<String>();
356 >    public void testToString_exception() {
357 >        CompletableFuture<String> f = new CompletableFuture<>();
358          assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
359 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
359 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
360 >        if (testImplementationDetails)
361 >            assertTrue(f.toString().startsWith(
362 >                               identityString(f) + "[Completed exceptionally: "));
363 >    }
364  
365 +    public void testToString_cancelled() {
366          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
367 <            f = new CompletableFuture<String>();
367 >            CompletableFuture<String> f = new CompletableFuture<>();
368              assertTrue(f.cancel(mayInterruptIfRunning));
369 <            assertTrue(f.toString().contains("[Completed exceptionally]"));
369 >            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
370 >            if (testImplementationDetails)
371 >                assertTrue(f.toString().startsWith(
372 >                                   identityString(f) + "[Completed exceptionally: "));
373          }
374      }
375  
# Line 362 | Line 381 | public class CompletableFutureTest exten
381          checkCompletedNormally(f, "test");
382      }
383  
384 <    abstract class CheckedAction {
384 >    abstract static class CheckedAction {
385          int invocationCount = 0;
386          final ExecutionMode m;
387          CheckedAction(ExecutionMode m) { this.m = m; }
# Line 374 | Line 393 | public class CompletableFutureTest exten
393          void assertInvoked() { assertEquals(1, invocationCount); }
394      }
395  
396 <    abstract class CheckedIntegerAction extends CheckedAction {
396 >    abstract static class CheckedIntegerAction extends CheckedAction {
397          Integer value;
398          CheckedIntegerAction(ExecutionMode m) { super(m); }
399          void assertValue(Integer expected) {
# Line 383 | Line 402 | public class CompletableFutureTest exten
402          }
403      }
404  
405 <    class IntegerSupplier extends CheckedAction
405 >    static class IntegerSupplier extends CheckedAction
406          implements Supplier<Integer>
407      {
408          final Integer value;
# Line 402 | Line 421 | public class CompletableFutureTest exten
421          return (x == null) ? null : x + 1;
422      }
423  
424 <    class NoopConsumer extends CheckedIntegerAction
424 >    static class NoopConsumer extends CheckedIntegerAction
425          implements Consumer<Integer>
426      {
427          NoopConsumer(ExecutionMode m) { super(m); }
# Line 412 | Line 431 | public class CompletableFutureTest exten
431          }
432      }
433  
434 <    class IncFunction extends CheckedIntegerAction
434 >    static class IncFunction extends CheckedIntegerAction
435          implements Function<Integer,Integer>
436      {
437          IncFunction(ExecutionMode m) { super(m); }
# Line 430 | Line 449 | public class CompletableFutureTest exten
449              - ((y == null) ? 99 : y.intValue());
450      }
451  
452 <    class SubtractAction extends CheckedIntegerAction
452 >    static class SubtractAction extends CheckedIntegerAction
453          implements BiConsumer<Integer, Integer>
454      {
455          SubtractAction(ExecutionMode m) { super(m); }
# Line 440 | Line 459 | public class CompletableFutureTest exten
459          }
460      }
461  
462 <    class SubtractFunction extends CheckedIntegerAction
462 >    static class SubtractFunction extends CheckedIntegerAction
463          implements BiFunction<Integer, Integer, Integer>
464      {
465          SubtractFunction(ExecutionMode m) { super(m); }
# Line 450 | Line 469 | public class CompletableFutureTest exten
469          }
470      }
471  
472 <    class Noop extends CheckedAction implements Runnable {
472 >    static class Noop extends CheckedAction implements Runnable {
473          Noop(ExecutionMode m) { super(m); }
474          public void run() {
475              invoked();
476          }
477      }
478  
479 <    class FailingSupplier extends CheckedAction
479 >    static class FailingSupplier extends CheckedAction
480          implements Supplier<Integer>
481      {
482          final CFException ex;
# Line 468 | Line 487 | public class CompletableFutureTest exten
487          }
488      }
489  
490 <    class FailingConsumer extends CheckedIntegerAction
490 >    static class FailingConsumer extends CheckedIntegerAction
491          implements Consumer<Integer>
492      {
493          final CFException ex;
# Line 480 | Line 499 | public class CompletableFutureTest exten
499          }
500      }
501  
502 <    class FailingBiConsumer extends CheckedIntegerAction
502 >    static class FailingBiConsumer extends CheckedIntegerAction
503          implements BiConsumer<Integer, Integer>
504      {
505          final CFException ex;
# Line 492 | Line 511 | public class CompletableFutureTest exten
511          }
512      }
513  
514 <    class FailingFunction extends CheckedIntegerAction
514 >    static class FailingFunction extends CheckedIntegerAction
515          implements Function<Integer, Integer>
516      {
517          final CFException ex;
# Line 504 | Line 523 | public class CompletableFutureTest exten
523          }
524      }
525  
526 <    class FailingBiFunction extends CheckedIntegerAction
526 >    static class FailingBiFunction extends CheckedIntegerAction
527          implements BiFunction<Integer, Integer, Integer>
528      {
529          final CFException ex;
# Line 516 | Line 535 | public class CompletableFutureTest exten
535          }
536      }
537  
538 <    class FailingRunnable extends CheckedAction implements Runnable {
538 >    static class FailingRunnable extends CheckedAction implements Runnable {
539          final CFException ex;
540          FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
541          public void run() {
# Line 525 | Line 544 | public class CompletableFutureTest exten
544          }
545      }
546  
547 <    class CompletableFutureInc extends CheckedIntegerAction
547 >    static class CompletableFutureInc extends CheckedIntegerAction
548          implements Function<Integer, CompletableFuture<Integer>>
549      {
550          CompletableFutureInc(ExecutionMode m) { super(m); }
551          public CompletableFuture<Integer> apply(Integer x) {
552              invoked();
553              value = x;
554 <            CompletableFuture<Integer> f = new CompletableFuture<>();
536 <            assertTrue(f.complete(inc(x)));
537 <            return f;
554 >            return CompletableFuture.completedFuture(inc(x));
555          }
556      }
557  
558 <    class FailingCompletableFutureFunction extends CheckedIntegerAction
558 >    static class FailingExceptionalCompletableFutureFunction extends CheckedAction
559 >        implements Function<Throwable, CompletableFuture<Integer>>
560 >    {
561 >        final CFException ex;
562 >        FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
563 >        public CompletableFuture<Integer> apply(Throwable x) {
564 >            invoked();
565 >            throw ex;
566 >        }
567 >    }
568 >
569 >    static class ExceptionalCompletableFutureFunction extends CheckedAction
570 >        implements Function<Throwable, CompletionStage<Integer>> {
571 >        final Integer value = 3;
572 >        ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); }
573 >        public CompletionStage<Integer> apply(Throwable x) {
574 >            invoked();
575 >            return CompletableFuture.completedFuture(value);
576 >        }
577 >    }
578 >
579 >    static class FailingCompletableFutureFunction extends CheckedIntegerAction
580          implements Function<Integer, CompletableFuture<Integer>>
581      {
582          final CFException ex;
# Line 653 | Line 691 | public class CompletableFutureTest exten
691                   Function<? super T,U> a) {
692                  return f.applyToEither(g, a);
693              }
694 +            public <T> CompletableFuture<T> exceptionally
695 +                (CompletableFuture<T> f,
696 +                 Function<Throwable, ? extends T> fn) {
697 +                return f.exceptionally(fn);
698 +            }
699 +            public <T> CompletableFuture<T> exceptionallyCompose
700 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
701 +                return f.exceptionallyCompose(fn);
702 +            }
703          },
657
704          ASYNC {
705              public void checkExecutionMode() {
706                  assertEquals(defaultExecutorIsCommonPool,
# Line 727 | Line 773 | public class CompletableFutureTest exten
773                   Function<? super T,U> a) {
774                  return f.applyToEitherAsync(g, a);
775              }
776 +            public <T> CompletableFuture<T> exceptionally
777 +                (CompletableFuture<T> f,
778 +                 Function<Throwable, ? extends T> fn) {
779 +                return f.exceptionallyAsync(fn);
780 +            }
781 +
782 +            public <T> CompletableFuture<T> exceptionallyCompose
783 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
784 +                return f.exceptionallyComposeAsync(fn);
785 +            }
786 +
787          },
788  
789          EXECUTOR {
# Line 800 | Line 857 | public class CompletableFutureTest exten
857                   Function<? super T,U> a) {
858                  return f.applyToEitherAsync(g, a, new ThreadExecutor());
859              }
860 +            public <T> CompletableFuture<T> exceptionally
861 +                (CompletableFuture<T> f,
862 +                 Function<Throwable, ? extends T> fn) {
863 +                return f.exceptionallyAsync(fn, new ThreadExecutor());
864 +            }
865 +            public <T> CompletableFuture<T> exceptionallyCompose
866 +                (CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) {
867 +                return f.exceptionallyComposeAsync(fn, new ThreadExecutor());
868 +            }
869 +
870          };
871  
872          public abstract void checkExecutionMode();
# Line 842 | Line 909 | public class CompletableFutureTest exten
909              (CompletableFuture<T> f,
910               CompletionStage<? extends T> g,
911               Function<? super T,U> a);
912 +        public abstract <T> CompletableFuture<T> exceptionally
913 +            (CompletableFuture<T> f,
914 +             Function<Throwable, ? extends T> fn);
915 +        public abstract <T> CompletableFuture<T> exceptionallyCompose
916 +            (CompletableFuture<T> f,
917 +             Function<Throwable, ? extends CompletionStage<T>> fn);
918      }
919  
920      /**
# Line 849 | Line 922 | public class CompletableFutureTest exten
922       * normally, and source result is propagated
923       */
924      public void testExceptionally_normalCompletion() {
925 +        for (ExecutionMode m : ExecutionMode.values())
926          for (boolean createIncomplete : new boolean[] { true, false })
927          for (Integer v1 : new Integer[] { 1, null })
928      {
855        final AtomicInteger a = new AtomicInteger(0);
929          final CompletableFuture<Integer> f = new CompletableFuture<>();
930          if (!createIncomplete) assertTrue(f.complete(v1));
931 <        final CompletableFuture<Integer> g = f.exceptionally
932 <            ((Throwable t) -> {
860 <                a.getAndIncrement();
931 >        final CompletableFuture<Integer> g = m.exceptionally
932 >            (f, (Throwable t) -> {
933                  threadFail("should not be called");
934                  return null;            // unreached
935              });
# Line 865 | Line 937 | public class CompletableFutureTest exten
937  
938          checkCompletedNormally(g, v1);
939          checkCompletedNormally(f, v1);
868        assertEquals(0, a.get());
940      }}
941  
942      /**
# Line 873 | Line 944 | public class CompletableFutureTest exten
944       * exception
945       */
946      public void testExceptionally_exceptionalCompletion() {
947 +        for (ExecutionMode m : ExecutionMode.values())
948          for (boolean createIncomplete : new boolean[] { true, false })
949          for (Integer v1 : new Integer[] { 1, null })
950      {
# Line 880 | Line 952 | public class CompletableFutureTest exten
952          final CFException ex = new CFException();
953          final CompletableFuture<Integer> f = new CompletableFuture<>();
954          if (!createIncomplete) f.completeExceptionally(ex);
955 <        final CompletableFuture<Integer> g = f.exceptionally
956 <            ((Throwable t) -> {
957 <                ExecutionMode.SYNC.checkExecutionMode();
955 >        final CompletableFuture<Integer> g = m.exceptionally
956 >            (f, (Throwable t) -> {
957 >                m.checkExecutionMode();
958                  threadAssertSame(t, ex);
959                  a.getAndIncrement();
960                  return v1;
# Line 898 | Line 970 | public class CompletableFutureTest exten
970       * exceptionally with that exception
971       */
972      public void testExceptionally_exceptionalCompletionActionFailed() {
973 +        for (ExecutionMode m : ExecutionMode.values())
974          for (boolean createIncomplete : new boolean[] { true, false })
975      {
976          final AtomicInteger a = new AtomicInteger(0);
# Line 905 | Line 978 | public class CompletableFutureTest exten
978          final CFException ex2 = new CFException();
979          final CompletableFuture<Integer> f = new CompletableFuture<>();
980          if (!createIncomplete) f.completeExceptionally(ex1);
981 <        final CompletableFuture<Integer> g = f.exceptionally
982 <            ((Throwable t) -> {
983 <                ExecutionMode.SYNC.checkExecutionMode();
981 >        final CompletableFuture<Integer> g = m.exceptionally
982 >            (f, (Throwable t) -> {
983 >                m.checkExecutionMode();
984                  threadAssertSame(t, ex1);
985                  a.getAndIncrement();
986                  throw ex2;
# Line 1243 | Line 1316 | public class CompletableFutureTest exten
1316          r.assertInvoked();
1317      }}
1318  
1319 +    @SuppressWarnings("FutureReturnValueIgnored")
1320      public void testRunAsync_rejectingExecutor() {
1321          CountingRejectingExecutor e = new CountingRejectingExecutor();
1322          try {
# Line 1289 | Line 1363 | public class CompletableFutureTest exten
1363          r.assertInvoked();
1364      }}
1365  
1366 +    @SuppressWarnings("FutureReturnValueIgnored")
1367      public void testSupplyAsync_rejectingExecutor() {
1368          CountingRejectingExecutor e = new CountingRejectingExecutor();
1369          try {
# Line 2563 | Line 2638 | public class CompletableFutureTest exten
2638  
2639          // unspecified behavior - both source completions available
2640          try {
2641 <            assertEquals(null, h0.join());
2641 >            assertNull(h0.join());
2642              rs[0].assertValue(v1);
2643          } catch (CompletionException ok) {
2644              checkCompletedWithWrappedException(h0, ex);
2645              rs[0].assertNotInvoked();
2646          }
2647          try {
2648 <            assertEquals(null, h1.join());
2648 >            assertNull(h1.join());
2649              rs[1].assertValue(v1);
2650          } catch (CompletionException ok) {
2651              checkCompletedWithWrappedException(h1, ex);
2652              rs[1].assertNotInvoked();
2653          }
2654          try {
2655 <            assertEquals(null, h2.join());
2655 >            assertNull(h2.join());
2656              rs[2].assertValue(v1);
2657          } catch (CompletionException ok) {
2658              checkCompletedWithWrappedException(h2, ex);
2659              rs[2].assertNotInvoked();
2660          }
2661          try {
2662 <            assertEquals(null, h3.join());
2662 >            assertNull(h3.join());
2663              rs[3].assertValue(v1);
2664          } catch (CompletionException ok) {
2665              checkCompletedWithWrappedException(h3, ex);
# Line 2823 | Line 2898 | public class CompletableFutureTest exten
2898  
2899          // unspecified behavior - both source completions available
2900          try {
2901 <            assertEquals(null, h0.join());
2901 >            assertNull(h0.join());
2902              rs[0].assertInvoked();
2903          } catch (CompletionException ok) {
2904              checkCompletedWithWrappedException(h0, ex);
2905              rs[0].assertNotInvoked();
2906          }
2907          try {
2908 <            assertEquals(null, h1.join());
2908 >            assertNull(h1.join());
2909              rs[1].assertInvoked();
2910          } catch (CompletionException ok) {
2911              checkCompletedWithWrappedException(h1, ex);
2912              rs[1].assertNotInvoked();
2913          }
2914          try {
2915 <            assertEquals(null, h2.join());
2915 >            assertNull(h2.join());
2916              rs[2].assertInvoked();
2917          } catch (CompletionException ok) {
2918              checkCompletedWithWrappedException(h2, ex);
2919              rs[2].assertNotInvoked();
2920          }
2921          try {
2922 <            assertEquals(null, h3.join());
2922 >            assertNull(h3.join());
2923              rs[3].assertInvoked();
2924          } catch (CompletionException ok) {
2925              checkCompletedWithWrappedException(h3, ex);
# Line 3076 | Line 3151 | public class CompletableFutureTest exten
3151          checkCompletedNormally(f, v1);
3152      }}
3153  
3154 +    /**
3155 +     * exceptionallyCompose result completes normally after normal
3156 +     * completion of source
3157 +     */
3158 +    public void testExceptionallyCompose_normalCompletion() {
3159 +        for (ExecutionMode m : ExecutionMode.values())
3160 +        for (boolean createIncomplete : new boolean[] { true, false })
3161 +        for (Integer v1 : new Integer[] { 1, null })
3162 +    {
3163 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3164 +        final ExceptionalCompletableFutureFunction r =
3165 +            new ExceptionalCompletableFutureFunction(m);
3166 +        if (!createIncomplete) assertTrue(f.complete(v1));
3167 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3168 +        if (createIncomplete) assertTrue(f.complete(v1));
3169 +
3170 +        if (!createIncomplete && testImplementationDetails)
3171 +            assertSame(f, g);
3172 +        checkCompletedNormally(f, v1);
3173 +        checkCompletedNormally(g, v1);
3174 +        r.assertNotInvoked();
3175 +    }}
3176 +
3177 +    /**
3178 +     * exceptionallyCompose result completes normally after exceptional
3179 +     * completion of source
3180 +     */
3181 +    public void testExceptionallyCompose_exceptionalCompletion() {
3182 +        for (ExecutionMode m : ExecutionMode.values())
3183 +        for (boolean createIncomplete : new boolean[] { true, false })
3184 +    {
3185 +        final CFException ex = new CFException();
3186 +        final ExceptionalCompletableFutureFunction r =
3187 +            new ExceptionalCompletableFutureFunction(m);
3188 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3189 +        if (!createIncomplete) f.completeExceptionally(ex);
3190 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3191 +        if (createIncomplete) f.completeExceptionally(ex);
3192 +
3193 +        checkCompletedExceptionally(f, ex);
3194 +        checkCompletedNormally(g, r.value);
3195 +        r.assertInvoked();
3196 +    }}
3197 +
3198 +    /**
3199 +     * exceptionallyCompose completes exceptionally on exception if action does
3200 +     */
3201 +    public void testExceptionallyCompose_actionFailed() {
3202 +        for (ExecutionMode m : ExecutionMode.values())
3203 +        for (boolean createIncomplete : new boolean[] { true, false })
3204 +        for (Integer v1 : new Integer[] { 1, null })
3205 +    {
3206 +        final CFException ex = new CFException();
3207 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3208 +        final FailingExceptionalCompletableFutureFunction r
3209 +            = new FailingExceptionalCompletableFutureFunction(m);
3210 +        if (!createIncomplete) f.completeExceptionally(ex);
3211 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3212 +        if (createIncomplete) f.completeExceptionally(ex);
3213 +
3214 +        checkCompletedExceptionally(f, ex);
3215 +        checkCompletedWithWrappedException(g, r.ex);
3216 +        r.assertInvoked();
3217 +    }}
3218 +
3219 +
3220      // other static methods
3221  
3222      /**
# Line 3239 | Line 3380 | public class CompletableFutureTest exten
3380      /**
3381       * Completion methods throw NullPointerException with null arguments
3382       */
3383 +    @SuppressWarnings("FutureReturnValueIgnored")
3384      public void testNPE() {
3385          CompletableFuture<Integer> f = new CompletableFuture<>();
3386          CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 3258 | Line 3400 | public class CompletableFutureTest exten
3400  
3401              () -> f.thenApply(null),
3402              () -> f.thenApplyAsync(null),
3403 <            () -> f.thenApplyAsync((x) -> x, null),
3403 >            () -> f.thenApplyAsync(x -> x, null),
3404              () -> f.thenApplyAsync(null, exec),
3405  
3406              () -> f.thenAccept(null),
3407              () -> f.thenAcceptAsync(null),
3408 <            () -> f.thenAcceptAsync((x) -> {} , null),
3408 >            () -> f.thenAcceptAsync(x -> {} , null),
3409              () -> f.thenAcceptAsync(null, exec),
3410  
3411              () -> f.thenRun(null),
# Line 3298 | Line 3440 | public class CompletableFutureTest exten
3440              () -> f.applyToEither(g, null),
3441              () -> f.applyToEitherAsync(g, null),
3442              () -> f.applyToEitherAsync(g, null, exec),
3443 <            () -> f.applyToEither(nullFuture, (x) -> x),
3444 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3445 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3446 <            () -> f.applyToEitherAsync(g, (x) -> x, null),
3443 >            () -> f.applyToEither(nullFuture, x -> x),
3444 >            () -> f.applyToEitherAsync(nullFuture, x -> x),
3445 >            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
3446 >            () -> f.applyToEitherAsync(g, x -> x, null),
3447  
3448              () -> f.acceptEither(g, null),
3449              () -> f.acceptEitherAsync(g, null),
3450              () -> f.acceptEitherAsync(g, null, exec),
3451 <            () -> f.acceptEither(nullFuture, (x) -> {}),
3452 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3453 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3454 <            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3451 >            () -> f.acceptEither(nullFuture, x -> {}),
3452 >            () -> f.acceptEitherAsync(nullFuture, x -> {}),
3453 >            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
3454 >            () -> f.acceptEitherAsync(g, x -> {}, null),
3455  
3456              () -> f.runAfterEither(g, null),
3457              () -> f.runAfterEitherAsync(g, null),
# Line 3375 | Line 3517 | public class CompletableFutureTest exten
3517          for (CompletableFuture<Integer> src : srcs) {
3518              List<CompletableFuture<?>> fs = new ArrayList<>();
3519              fs.add(src.thenRunAsync(() -> {}, e));
3520 <            fs.add(src.thenAcceptAsync((z) -> {}, e));
3521 <            fs.add(src.thenApplyAsync((z) -> z, e));
3520 >            fs.add(src.thenAcceptAsync(z -> {}, e));
3521 >            fs.add(src.thenApplyAsync(z -> z, e));
3522  
3523              fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3524              fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3525              fs.add(src.runAfterBothAsync(src, () -> {}, e));
3526  
3527 <            fs.add(src.applyToEitherAsync(src, (z) -> z, e));
3528 <            fs.add(src.acceptEitherAsync(src, (z) -> {}, e));
3527 >            fs.add(src.applyToEitherAsync(src, z -> z, e));
3528 >            fs.add(src.acceptEitherAsync(src, z -> {}, e));
3529              fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3530  
3531 <            fs.add(src.thenComposeAsync((z) -> null, e));
3531 >            fs.add(src.thenComposeAsync(z -> null, e));
3532              fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3533              fs.add(src.handleAsync((z, t) -> null, e));
3534  
# Line 3419 | Line 3561 | public class CompletableFutureTest exten
3561          {
3562              List<CompletableFuture<?>> fs = new ArrayList<>();
3563  
3564 <            fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e));
3565 <            fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e));
3564 >            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
3565 >            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
3566  
3567 <            fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e));
3568 <            fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e));
3567 >            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
3568 >            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
3569  
3570              fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3571              fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
# Line 3462 | Line 3604 | public class CompletableFutureTest exten
3604  
3605          List<CompletableFuture<?>> fs = new ArrayList<>();
3606          fs.add(incomplete.thenRunAsync(() -> {}, e));
3607 <        fs.add(incomplete.thenAcceptAsync((z) -> {}, e));
3608 <        fs.add(incomplete.thenApplyAsync((z) -> z, e));
3607 >        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
3608 >        fs.add(incomplete.thenApplyAsync(z -> z, e));
3609  
3610          fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3611          fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3612          fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3613  
3614 <        fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e));
3615 <        fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e));
3614 >        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
3615 >        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
3616          fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3617  
3618 <        fs.add(incomplete.thenComposeAsync((z) -> null, e));
3618 >        fs.add(incomplete.thenComposeAsync(z -> null, e));
3619          fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3620          fs.add(incomplete.handleAsync((z, t) -> null, e));
3621  
# Line 3533 | Line 3675 | public class CompletableFutureTest exten
3675       */
3676      public void testCompletedStage() {
3677          AtomicInteger x = new AtomicInteger(0);
3678 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3678 >        AtomicReference<Throwable> r = new AtomicReference<>();
3679          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3680          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3681          assertEquals(x.get(), 1);
# Line 3578 | Line 3720 | public class CompletableFutureTest exten
3720       * copy returns a CompletableFuture that is completed normally,
3721       * with the same value, when source is.
3722       */
3723 <    public void testCopy() {
3723 >    public void testCopy_normalCompletion() {
3724 >        for (boolean createIncomplete : new boolean[] { true, false })
3725 >        for (Integer v1 : new Integer[] { 1, null })
3726 >    {
3727          CompletableFuture<Integer> f = new CompletableFuture<>();
3728 +        if (!createIncomplete) assertTrue(f.complete(v1));
3729          CompletableFuture<Integer> g = f.copy();
3730 <        checkIncomplete(f);
3731 <        checkIncomplete(g);
3732 <        f.complete(1);
3733 <        checkCompletedNormally(f, 1);
3734 <        checkCompletedNormally(g, 1);
3735 <    }
3730 >        if (createIncomplete) {
3731 >            checkIncomplete(f);
3732 >            checkIncomplete(g);
3733 >            assertTrue(f.complete(v1));
3734 >        }
3735 >        checkCompletedNormally(f, v1);
3736 >        checkCompletedNormally(g, v1);
3737 >    }}
3738  
3739      /**
3740       * copy returns a CompletableFuture that is completed exceptionally
3741       * when source is.
3742       */
3743 <    public void testCopy2() {
3743 >    public void testCopy_exceptionalCompletion() {
3744 >        for (boolean createIncomplete : new boolean[] { true, false })
3745 >    {
3746 >        CFException ex = new CFException();
3747          CompletableFuture<Integer> f = new CompletableFuture<>();
3748 +        if (!createIncomplete) f.completeExceptionally(ex);
3749          CompletableFuture<Integer> g = f.copy();
3750 <        checkIncomplete(f);
3751 <        checkIncomplete(g);
3752 <        CFException ex = new CFException();
3753 <        f.completeExceptionally(ex);
3750 >        if (createIncomplete) {
3751 >            checkIncomplete(f);
3752 >            checkIncomplete(g);
3753 >            f.completeExceptionally(ex);
3754 >        }
3755          checkCompletedExceptionally(f, ex);
3756          checkCompletedWithWrappedException(g, ex);
3757 +    }}
3758 +
3759 +    /**
3760 +     * Completion of a copy does not complete its source.
3761 +     */
3762 +    public void testCopy_oneWayPropagation() {
3763 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3764 +        assertTrue(f.copy().complete(1));
3765 +        assertTrue(f.copy().complete(null));
3766 +        assertTrue(f.copy().cancel(true));
3767 +        assertTrue(f.copy().cancel(false));
3768 +        assertTrue(f.copy().completeExceptionally(new CFException()));
3769 +        checkIncomplete(f);
3770      }
3771  
3772      /**
# Line 3611 | Line 3777 | public class CompletableFutureTest exten
3777          CompletableFuture<Integer> f = new CompletableFuture<>();
3778          CompletionStage<Integer> g = f.minimalCompletionStage();
3779          AtomicInteger x = new AtomicInteger(0);
3780 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3780 >        AtomicReference<Throwable> r = new AtomicReference<>();
3781          checkIncomplete(f);
3782          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3783          f.complete(1);
# Line 3628 | Line 3794 | public class CompletableFutureTest exten
3794          CompletableFuture<Integer> f = new CompletableFuture<>();
3795          CompletionStage<Integer> g = f.minimalCompletionStage();
3796          AtomicInteger x = new AtomicInteger(0);
3797 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3797 >        AtomicReference<Throwable> r = new AtomicReference<>();
3798          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3799          checkIncomplete(f);
3800          CFException ex = new CFException();
# Line 3646 | Line 3812 | public class CompletableFutureTest exten
3812          CFException ex = new CFException();
3813          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3814          AtomicInteger x = new AtomicInteger(0);
3815 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3815 >        AtomicReference<Throwable> r = new AtomicReference<>();
3816          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3817          assertEquals(x.get(), 0);
3818          assertEquals(r.get(), ex);
# Line 3670 | Line 3836 | public class CompletableFutureTest exten
3836      public void testCompleteAsync2() {
3837          CompletableFuture<Integer> f = new CompletableFuture<>();
3838          CFException ex = new CFException();
3839 <        f.completeAsync(() -> {if (true) throw ex; return 1;});
3839 >        f.completeAsync(() -> { throw ex; });
3840          try {
3841              f.join();
3842              shouldThrow();
# Line 3700 | Line 3866 | public class CompletableFutureTest exten
3866          CompletableFuture<Integer> f = new CompletableFuture<>();
3867          CFException ex = new CFException();
3868          ThreadExecutor executor = new ThreadExecutor();
3869 <        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3869 >        f.completeAsync(() -> { throw ex; }, executor);
3870          try {
3871              f.join();
3872              shouldThrow();
# Line 3859 | Line 4025 | public class CompletableFutureTest exten
4025          List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
4026              = new ArrayList<>();
4027  
4028 <        funs.add((y) -> m.thenRun(y, noopRunnable));
4029 <        funs.add((y) -> m.thenAccept(y, noopConsumer));
4030 <        funs.add((y) -> m.thenApply(y, incFunction));
4031 <
4032 <        funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable));
4033 <        funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer));
4034 <        funs.add((y) -> m.applyToEither(y, incomplete, incFunction));
4035 <
4036 <        funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable));
4037 <        funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable));
4038 <        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
4039 <        funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
4040 <        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
4041 <        funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m)));
4042 <
4043 <        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
4044 <
4045 <        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
4046 <
4047 <        funs.add((y) -> CompletableFuture.allOf(y));
4048 <        funs.add((y) -> CompletableFuture.allOf(y, v42));
4049 <        funs.add((y) -> CompletableFuture.allOf(v42, y));
4050 <        funs.add((y) -> CompletableFuture.anyOf(y));
4051 <        funs.add((y) -> CompletableFuture.anyOf(y, incomplete));
4052 <        funs.add((y) -> CompletableFuture.anyOf(incomplete, y));
4028 >        funs.add(y -> m.thenRun(y, noopRunnable));
4029 >        funs.add(y -> m.thenAccept(y, noopConsumer));
4030 >        funs.add(y -> m.thenApply(y, incFunction));
4031 >
4032 >        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
4033 >        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
4034 >        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
4035 >
4036 >        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
4037 >        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
4038 >        funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
4039 >        funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
4040 >        funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m)));
4041 >        funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m)));
4042 >
4043 >        funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
4044 >
4045 >        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
4046 >
4047 >        funs.add(y -> CompletableFuture.allOf(y));
4048 >        funs.add(y -> CompletableFuture.allOf(y, v42));
4049 >        funs.add(y -> CompletableFuture.allOf(v42, y));
4050 >        funs.add(y -> CompletableFuture.anyOf(y));
4051 >        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
4052 >        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
4053  
4054          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
4055                   fun : funs) {
# Line 3940 | Line 4106 | public class CompletableFutureTest exten
4106      public void testMinimalCompletionStage_minimality() {
4107          if (!testImplementationDetails) return;
4108          Function<Method, String> toSignature =
4109 <            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
4109 >            method -> method.getName() + Arrays.toString(method.getParameterTypes());
4110          Predicate<Method> isNotStatic =
4111 <            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
4111 >            method -> (method.getModifiers() & Modifier.STATIC) == 0;
4112          List<Method> minimalMethods =
4113              Stream.of(Object.class, CompletionStage.class)
4114 <            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
4114 >            .flatMap(klazz -> Stream.of(klazz.getMethods()))
4115              .filter(isNotStatic)
4116              .collect(Collectors.toList());
4117          // Methods from CompletableFuture permitted NOT to throw UOE
# Line 3961 | Line 4127 | public class CompletableFutureTest exten
4127              .collect(Collectors.toSet());
4128          List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
4129              .filter(isNotStatic)
4130 <            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4130 >            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4131              .collect(Collectors.toList());
4132  
4133          List<CompletionStage<Integer>> stages = new ArrayList<>();
4134 <        stages.add(new CompletableFuture<Integer>().minimalCompletionStage());
4134 >        CompletionStage<Integer> min =
4135 >            new CompletableFuture<Integer>().minimalCompletionStage();
4136 >        stages.add(min);
4137 >        stages.add(min.thenApply(x -> x));
4138          stages.add(CompletableFuture.completedStage(1));
4139          stages.add(CompletableFuture.failedStage(new CFException()));
4140  
# Line 4001 | Line 4170 | public class CompletableFutureTest exten
4170              throw new Error("Methods did not throw UOE: " + bugs);
4171      }
4172  
4173 +    /**
4174 +     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4175 +     * is completed normally, with the same value, when source is.
4176 +     */
4177 +    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
4178 +        for (boolean createIncomplete : new boolean[] { true, false })
4179 +        for (Integer v1 : new Integer[] { 1, null })
4180 +    {
4181 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4182 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4183 +        if (!createIncomplete) assertTrue(f.complete(v1));
4184 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4185 +        if (createIncomplete) {
4186 +            checkIncomplete(f);
4187 +            checkIncomplete(g);
4188 +            assertTrue(f.complete(v1));
4189 +        }
4190 +        checkCompletedNormally(f, v1);
4191 +        checkCompletedNormally(g, v1);
4192 +    }}
4193 +
4194 +    /**
4195 +     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4196 +     * is completed exceptionally when source is.
4197 +     */
4198 +    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
4199 +        for (boolean createIncomplete : new boolean[] { true, false })
4200 +    {
4201 +        CFException ex = new CFException();
4202 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4203 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4204 +        if (!createIncomplete) f.completeExceptionally(ex);
4205 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4206 +        if (createIncomplete) {
4207 +            checkIncomplete(f);
4208 +            checkIncomplete(g);
4209 +            f.completeExceptionally(ex);
4210 +        }
4211 +        checkCompletedExceptionally(f, ex);
4212 +        checkCompletedWithWrappedException(g, ex);
4213 +    }}
4214 +
4215 +    /**
4216 +     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
4217 +     */
4218 +    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
4219 +        for (Integer v1 : new Integer[] { 1, null })
4220 +    {
4221 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4222 +        CompletionStage minimal = f.minimalCompletionStage();
4223 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4224 +        assertTrue(g.complete(v1));
4225 +        checkCompletedNormally(g, v1);
4226 +        checkIncomplete(f);
4227 +        checkIncomplete(minimal.toCompletableFuture());
4228 +    }}
4229 +
4230 +    /**
4231 +     * minimalStage.toCompletableFuture().join() awaits completion
4232 +     */
4233 +    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
4234 +        for (boolean createIncomplete : new boolean[] { true, false })
4235 +        for (Integer v1 : new Integer[] { 1, null })
4236 +    {
4237 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4238 +        if (!createIncomplete) assertTrue(f.complete(v1));
4239 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4240 +        if (createIncomplete) assertTrue(f.complete(v1));
4241 +        assertEquals(v1, minimal.toCompletableFuture().join());
4242 +        assertEquals(v1, minimal.toCompletableFuture().get());
4243 +        checkCompletedNormally(minimal.toCompletableFuture(), v1);
4244 +    }}
4245 +
4246 +    /**
4247 +     * Completion of a toCompletableFuture copy of a minimal stage
4248 +     * does not complete its source.
4249 +     */
4250 +    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
4251 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4252 +        CompletionStage<Integer> g = f.minimalCompletionStage();
4253 +        assertTrue(g.toCompletableFuture().complete(1));
4254 +        assertTrue(g.toCompletableFuture().complete(null));
4255 +        assertTrue(g.toCompletableFuture().cancel(true));
4256 +        assertTrue(g.toCompletableFuture().cancel(false));
4257 +        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
4258 +        checkIncomplete(g.toCompletableFuture());
4259 +        f.complete(1);
4260 +        checkCompletedNormally(g.toCompletableFuture(), 1);
4261 +    }
4262 +
4263 +    /** Demo utility method for external reliable toCompletableFuture */
4264 +    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
4265 +        CompletableFuture<T> f = new CompletableFuture<>();
4266 +        stage.handle((T t, Throwable ex) -> {
4267 +                         if (ex != null) f.completeExceptionally(ex);
4268 +                         else f.complete(t);
4269 +                         return null;
4270 +                     });
4271 +        return f;
4272 +    }
4273 +
4274 +    /** Demo utility method to join a CompletionStage */
4275 +    static <T> T join(CompletionStage<T> stage) {
4276 +        return toCompletableFuture(stage).join();
4277 +    }
4278 +
4279 +    /**
4280 +     * Joining a minimal stage "by hand" works
4281 +     */
4282 +    public void testMinimalCompletionStage_join_by_hand() {
4283 +        for (boolean createIncomplete : new boolean[] { true, false })
4284 +        for (Integer v1 : new Integer[] { 1, null })
4285 +    {
4286 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4287 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4288 +        CompletableFuture<Integer> g = new CompletableFuture<>();
4289 +        if (!createIncomplete) assertTrue(f.complete(v1));
4290 +        minimal.thenAccept(x -> g.complete(x));
4291 +        if (createIncomplete) assertTrue(f.complete(v1));
4292 +        g.join();
4293 +        checkCompletedNormally(g, v1);
4294 +        checkCompletedNormally(f, v1);
4295 +        assertEquals(v1, join(minimal));
4296 +    }}
4297 +
4298      static class Monad {
4299          static class ZeroException extends RuntimeException {
4300              public ZeroException() { super("monadic zero"); }
# Line 4017 | Line 4311 | public class CompletableFutureTest exten
4311          static <T,U,V> Function<T, CompletableFuture<V>> compose
4312              (Function<T, CompletableFuture<U>> f,
4313               Function<U, CompletableFuture<V>> g) {
4314 <            return (x) -> f.apply(x).thenCompose(g);
4314 >            return x -> f.apply(x).thenCompose(g);
4315          }
4316  
4317          static void assertZero(CompletableFuture<?> f) {
4318              try {
4319                  f.getNow(null);
4320 <                throw new AssertionFailedError("should throw");
4320 >                throw new AssertionError("should throw");
4321              } catch (CompletionException success) {
4322                  assertTrue(success.getCause() instanceof ZeroException);
4323              }
# Line 4097 | Line 4391 | public class CompletableFutureTest exten
4391  
4392          // Some mutually non-commutative functions
4393          Function<Long, CompletableFuture<Long>> triple
4394 <            = (x) -> Monad.unit(3 * x);
4394 >            = x -> Monad.unit(3 * x);
4395          Function<Long, CompletableFuture<Long>> inc
4396 <            = (x) -> Monad.unit(x + 1);
4396 >            = x -> Monad.unit(x + 1);
4397  
4398          // unit is a right identity: m >>= unit === m
4399          Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
# Line 4111 | Line 4405 | public class CompletableFutureTest exten
4405          // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
4406          Monad.assertFutureEquals(
4407              unit.apply(5L).thenCompose(inc).thenCompose(triple),
4408 <            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
4408 >            unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple)));
4409  
4410          // The case for CompletableFuture as an additive monad is weaker...
4411  
# Line 4121 | Line 4415 | public class CompletableFutureTest exten
4415          // left zero: zero >>= f === zero
4416          Monad.assertZero(zero.thenCompose(inc));
4417          // right zero: f >>= (\x -> zero) === zero
4418 <        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
4418 >        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
4419  
4420          // f plus zero === f
4421          Monad.assertFutureEquals(Monad.unit(5L),
# Line 4148 | Line 4442 | public class CompletableFutureTest exten
4442      }
4443  
4444      /** Test long recursive chains of CompletableFutures with cascading completions */
4445 +    @SuppressWarnings("FutureReturnValueIgnored")
4446      public void testRecursiveChains() throws Throwable {
4447          for (ExecutionMode m : ExecutionMode.values())
4448          for (boolean addDeadEnds : new boolean[] { true, false })
# Line 4172 | Line 4467 | public class CompletableFutureTest exten
4467       * A single CompletableFuture with many dependents.
4468       * A demo of scalability - runtime is O(n).
4469       */
4470 +    @SuppressWarnings("FutureReturnValueIgnored")
4471      public void testManyDependents() throws Throwable {
4472          final int n = expensiveTests ? 1_000_000 : 10;
4473          final CompletableFuture<Void> head = new CompletableFuture<>();
# Line 4179 | Line 4475 | public class CompletableFutureTest exten
4475          final AtomicInteger count = new AtomicInteger(0);
4476          for (int i = 0; i < n; i++) {
4477              head.thenRun(() -> count.getAndIncrement());
4478 <            head.thenAccept((x) -> count.getAndIncrement());
4479 <            head.thenApply((x) -> count.getAndIncrement());
4478 >            head.thenAccept(x -> count.getAndIncrement());
4479 >            head.thenApply(x -> count.getAndIncrement());
4480  
4481              head.runAfterBoth(complete, () -> count.getAndIncrement());
4482              head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
# Line 4190 | Line 4486 | public class CompletableFutureTest exten
4486              complete.thenCombine(head, (x, y) -> count.getAndIncrement());
4487  
4488              head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
4489 <            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4490 <            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4489 >            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4490 >            head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4491              new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
4492 <            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
4493 <            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
4492 >            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
4493 >            new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement());
4494          }
4495          head.complete(null);
4496          assertEquals(5 * 3 * n, count.get());
4497      }
4498  
4499      /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4500 +    @SuppressWarnings("FutureReturnValueIgnored")
4501      public void testCoCompletionGarbageRetention() throws Throwable {
4502          final int n = expensiveTests ? 1_000_000 : 10;
4503          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
# Line 4211 | Line 4508 | public class CompletableFutureTest exten
4508              f.complete(null);
4509  
4510              f = new CompletableFuture<>();
4511 <            f.acceptEither(incomplete, (x) -> {});
4511 >            f.acceptEither(incomplete, x -> {});
4512              f.complete(null);
4513  
4514              f = new CompletableFuture<>();
4515 <            f.applyToEither(incomplete, (x) -> x);
4515 >            f.applyToEither(incomplete, x -> x);
4516              f.complete(null);
4517  
4518              f = new CompletableFuture<>();
4519 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete });
4519 >            CompletableFuture.anyOf(f, incomplete);
4520              f.complete(null);
4521          }
4522  
# Line 4229 | Line 4526 | public class CompletableFutureTest exten
4526              f.complete(null);
4527  
4528              f = new CompletableFuture<>();
4529 <            incomplete.acceptEither(f, (x) -> {});
4529 >            incomplete.acceptEither(f, x -> {});
4530              f.complete(null);
4531  
4532              f = new CompletableFuture<>();
4533 <            incomplete.applyToEither(f, (x) -> x);
4533 >            incomplete.applyToEither(f, x -> x);
4534              f.complete(null);
4535  
4536              f = new CompletableFuture<>();
4537 <            CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f });
4537 >            CompletableFuture.anyOf(incomplete, f);
4538              f.complete(null);
4539          }
4540      }
4541  
4542 <    /*
4543 <     * Tests below currently fail in stress mode due to memory retention.
4544 <     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck
4542 >    /**
4543 >     * Reproduction recipe for:
4544 >     * 8160402: Garbage retention with CompletableFuture.anyOf
4545 >     * 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
4546       */
4249
4250    /** Checks for garbage retention with anyOf. */
4547      public void testAnyOfGarbageRetention() throws Throwable {
4548          for (Integer v : new Integer[] { 1, null })
4549      {
# Line 4261 | Line 4557 | public class CompletableFutureTest exten
4557              checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4558      }}
4559  
4560 <    /** Checks for garbage retention with allOf. */
4560 >    /**
4561 >     * Checks for garbage retention with allOf.
4562 >     *
4563 >     * As of 2016-07, fails with OOME:
4564 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck
4565 >     */
4566      public void testCancelledAllOfGarbageRetention() throws Throwable {
4567          final int n = expensiveTests ? 100_000 : 10;
4568          CompletableFuture<Integer>[] fs
# Line 4272 | Line 4573 | public class CompletableFutureTest exten
4573              assertTrue(CompletableFuture.allOf(fs).cancel(false));
4574      }
4575  
4576 +    /**
4577 +     * Checks for garbage retention when a dependent future is
4578 +     * cancelled and garbage-collected.
4579 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4580 +     *
4581 +     * As of 2016-07, fails with OOME:
4582 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck
4583 +     */
4584 +    public void testCancelledGarbageRetention() throws Throwable {
4585 +        final int n = expensiveTests ? 100_000 : 10;
4586 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4587 +        for (int i = 0; i < n; i++)
4588 +            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
4589 +    }
4590 +
4591 +    /**
4592 +     * Checks for garbage retention when MinimalStage.toCompletableFuture()
4593 +     * is invoked many times.
4594 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4595 +     *
4596 +     * As of 2016-07, fails with OOME:
4597 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
4598 +     */
4599 +    public void testToCompletableFutureGarbageRetention() throws Throwable {
4600 +        final int n = expensiveTests ? 900_000 : 10;
4601 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4602 +        CompletionStage minimal = neverCompleted.minimalCompletionStage();
4603 +        for (int i = 0; i < n; i++)
4604 +            assertTrue(minimal.toCompletableFuture().cancel(true));
4605 +    }
4606 +
4607   //     static <U> U join(CompletionStage<U> stage) {
4608   //         CompletableFuture<U> f = new CompletableFuture<>();
4609   //         stage.whenComplete((v, ex) -> {
# Line 4296 | Line 4628 | public class CompletableFutureTest exten
4628   //         return stage.toCompletableFuture().copy().isDone();
4629   //     }
4630  
4631 +    // For testing default implementations
4632 +    // Only non-default interface methods defined.
4633 +    static final class DelegatedCompletionStage<T> implements CompletionStage<T> {
4634 +        final CompletableFuture<T> cf;
4635 +        DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; }
4636 +        public CompletableFuture<T> toCompletableFuture() {
4637 +            return cf; }
4638 +        public CompletionStage<Void> thenRun
4639 +            (Runnable action) {
4640 +            return cf.thenRun(action); }
4641 +        public CompletionStage<Void> thenRunAsync
4642 +            (Runnable action) {
4643 +            return cf.thenRunAsync(action); }
4644 +        public CompletionStage<Void> thenRunAsync
4645 +            (Runnable action,
4646 +             Executor executor) {
4647 +            return cf.thenRunAsync(action, executor); }
4648 +        public CompletionStage<Void> thenAccept
4649 +            (Consumer<? super T> action) {
4650 +            return cf.thenAccept(action); }
4651 +        public CompletionStage<Void> thenAcceptAsync
4652 +            (Consumer<? super T> action) {
4653 +            return cf.thenAcceptAsync(action); }
4654 +        public CompletionStage<Void> thenAcceptAsync
4655 +            (Consumer<? super T> action,
4656 +             Executor executor) {
4657 +            return cf.thenAcceptAsync(action, executor); }
4658 +        public <U> CompletionStage<U> thenApply
4659 +            (Function<? super T,? extends U> a) {
4660 +            return cf.thenApply(a); }
4661 +        public <U> CompletionStage<U> thenApplyAsync
4662 +            (Function<? super T,? extends U> fn) {
4663 +            return cf.thenApplyAsync(fn); }
4664 +        public <U> CompletionStage<U> thenApplyAsync
4665 +            (Function<? super T,? extends U> fn,
4666 +             Executor executor) {
4667 +            return cf.thenApplyAsync(fn, executor); }
4668 +        public <U,V> CompletionStage<V> thenCombine
4669 +            (CompletionStage<? extends U> other,
4670 +             BiFunction<? super T,? super U,? extends V> fn) {
4671 +            return cf.thenCombine(other, fn); }
4672 +        public <U,V> CompletionStage<V> thenCombineAsync
4673 +            (CompletionStage<? extends U> other,
4674 +             BiFunction<? super T,? super U,? extends V> fn) {
4675 +            return cf.thenCombineAsync(other, fn); }
4676 +        public <U,V> CompletionStage<V> thenCombineAsync
4677 +            (CompletionStage<? extends U> other,
4678 +             BiFunction<? super T,? super U,? extends V> fn,
4679 +             Executor executor) {
4680 +            return cf.thenCombineAsync(other, fn, executor); }
4681 +        public <U> CompletionStage<Void> thenAcceptBoth
4682 +            (CompletionStage<? extends U> other,
4683 +             BiConsumer<? super T, ? super U> action) {
4684 +            return cf.thenAcceptBoth(other, action); }
4685 +        public <U> CompletionStage<Void> thenAcceptBothAsync
4686 +            (CompletionStage<? extends U> other,
4687 +             BiConsumer<? super T, ? super U> action) {
4688 +            return cf.thenAcceptBothAsync(other, action); }
4689 +        public <U> CompletionStage<Void> thenAcceptBothAsync
4690 +            (CompletionStage<? extends U> other,
4691 +             BiConsumer<? super T, ? super U> action,
4692 +             Executor executor) {
4693 +            return cf.thenAcceptBothAsync(other, action, executor); }
4694 +        public CompletionStage<Void> runAfterBoth
4695 +            (CompletionStage<?> other,
4696 +             Runnable action) {
4697 +            return cf.runAfterBoth(other, action); }
4698 +        public CompletionStage<Void> runAfterBothAsync
4699 +            (CompletionStage<?> other,
4700 +             Runnable action) {
4701 +            return cf.runAfterBothAsync(other, action); }
4702 +        public CompletionStage<Void> runAfterBothAsync
4703 +            (CompletionStage<?> other,
4704 +             Runnable action,
4705 +             Executor executor) {
4706 +            return cf.runAfterBothAsync(other, action, executor); }
4707 +        public <U> CompletionStage<U> applyToEither
4708 +            (CompletionStage<? extends T> other,
4709 +             Function<? super T, U> fn) {
4710 +            return cf.applyToEither(other, fn); }
4711 +        public <U> CompletionStage<U> applyToEitherAsync
4712 +            (CompletionStage<? extends T> other,
4713 +             Function<? super T, U> fn) {
4714 +            return cf.applyToEitherAsync(other, fn); }
4715 +        public <U> CompletionStage<U> applyToEitherAsync
4716 +            (CompletionStage<? extends T> other,
4717 +             Function<? super T, U> fn,
4718 +             Executor executor) {
4719 +            return cf.applyToEitherAsync(other, fn, executor); }
4720 +        public CompletionStage<Void> acceptEither
4721 +            (CompletionStage<? extends T> other,
4722 +             Consumer<? super T> action) {
4723 +            return cf.acceptEither(other, action); }
4724 +        public CompletionStage<Void> acceptEitherAsync
4725 +            (CompletionStage<? extends T> other,
4726 +             Consumer<? super T> action) {
4727 +            return cf.acceptEitherAsync(other, action); }
4728 +        public CompletionStage<Void> acceptEitherAsync
4729 +            (CompletionStage<? extends T> other,
4730 +             Consumer<? super T> action,
4731 +             Executor executor) {
4732 +            return cf.acceptEitherAsync(other, action, executor); }
4733 +        public CompletionStage<Void> runAfterEither
4734 +            (CompletionStage<?> other,
4735 +             Runnable action) {
4736 +            return cf.runAfterEither(other, action); }
4737 +        public CompletionStage<Void> runAfterEitherAsync
4738 +            (CompletionStage<?> other,
4739 +             Runnable action) {
4740 +            return cf.runAfterEitherAsync(other, action); }
4741 +        public CompletionStage<Void> runAfterEitherAsync
4742 +            (CompletionStage<?> other,
4743 +             Runnable action,
4744 +             Executor executor) {
4745 +            return cf.runAfterEitherAsync(other, action, executor); }
4746 +        public <U> CompletionStage<U> thenCompose
4747 +            (Function<? super T, ? extends CompletionStage<U>> fn) {
4748 +            return cf.thenCompose(fn); }
4749 +        public <U> CompletionStage<U> thenComposeAsync
4750 +            (Function<? super T, ? extends CompletionStage<U>> fn) {
4751 +            return cf.thenComposeAsync(fn); }
4752 +        public <U> CompletionStage<U> thenComposeAsync
4753 +            (Function<? super T, ? extends CompletionStage<U>> fn,
4754 +             Executor executor) {
4755 +            return cf.thenComposeAsync(fn, executor); }
4756 +        public <U> CompletionStage<U> handle
4757 +            (BiFunction<? super T, Throwable, ? extends U> fn) {
4758 +            return cf.handle(fn); }
4759 +        public <U> CompletionStage<U> handleAsync
4760 +            (BiFunction<? super T, Throwable, ? extends U> fn) {
4761 +            return cf.handleAsync(fn); }
4762 +        public <U> CompletionStage<U> handleAsync
4763 +            (BiFunction<? super T, Throwable, ? extends U> fn,
4764 +             Executor executor) {
4765 +            return cf.handleAsync(fn, executor); }
4766 +        public CompletionStage<T> whenComplete
4767 +            (BiConsumer<? super T, ? super Throwable> action) {
4768 +            return cf.whenComplete(action); }
4769 +        public CompletionStage<T> whenCompleteAsync
4770 +            (BiConsumer<? super T, ? super Throwable> action) {
4771 +            return cf.whenCompleteAsync(action); }
4772 +        public CompletionStage<T> whenCompleteAsync
4773 +            (BiConsumer<? super T, ? super Throwable> action,
4774 +             Executor executor) {
4775 +            return cf.whenCompleteAsync(action, executor); }
4776 +        public CompletionStage<T> exceptionally
4777 +            (Function<Throwable, ? extends T> fn) {
4778 +            return cf.exceptionally(fn); }
4779 +    }
4780 +
4781 +    /**
4782 +     * default-implemented exceptionallyAsync action is not invoked when
4783 +     * source completes normally, and source result is propagated
4784 +     */
4785 +    public void testDefaultExceptionallyAsync_normalCompletion() {
4786 +        for (boolean createIncomplete : new boolean[] { true, false })
4787 +        for (Integer v1 : new Integer[] { 1, null })
4788 +    {
4789 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4790 +        final DelegatedCompletionStage<Integer> d =
4791 +            new DelegatedCompletionStage<Integer>(f);
4792 +        if (!createIncomplete) assertTrue(f.complete(v1));
4793 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4794 +            ((Throwable t) -> {
4795 +                threadFail("should not be called");
4796 +                return null;            // unreached
4797 +            });
4798 +        if (createIncomplete) assertTrue(f.complete(v1));
4799 +
4800 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4801 +    }}
4802 +
4803 +    /**
4804 +     * default-implemented exceptionallyAsync action completes with
4805 +     * function value on source exception
4806 +     */
4807 +    public void testDefaultExceptionallyAsync_exceptionalCompletion() {
4808 +        for (boolean createIncomplete : new boolean[] { true, false })
4809 +        for (Integer v1 : new Integer[] { 1, null })
4810 +    {
4811 +        final AtomicInteger a = new AtomicInteger(0);
4812 +        final CFException ex = new CFException();
4813 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4814 +        final DelegatedCompletionStage<Integer> d =
4815 +            new DelegatedCompletionStage<Integer>(f);
4816 +        if (!createIncomplete) f.completeExceptionally(ex);
4817 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4818 +            ((Throwable t) -> {
4819 +                threadAssertSame(t, ex);
4820 +                a.getAndIncrement();
4821 +                return v1;
4822 +            });
4823 +        if (createIncomplete) f.completeExceptionally(ex);
4824 +
4825 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4826 +        assertEquals(1, a.get());
4827 +    }}
4828 +
4829 +    /**
4830 +     * Under default implementation, if an "exceptionally action"
4831 +     * throws an exception, it completes exceptionally with that
4832 +     * exception
4833 +     */
4834 +    public void testDefaultExceptionallyAsync_exceptionalCompletionActionFailed() {
4835 +        for (boolean createIncomplete : new boolean[] { true, false })
4836 +    {
4837 +        final AtomicInteger a = new AtomicInteger(0);
4838 +        final CFException ex1 = new CFException();
4839 +        final CFException ex2 = new CFException();
4840 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4841 +        final DelegatedCompletionStage<Integer> d =
4842 +            new DelegatedCompletionStage<Integer>(f);
4843 +        if (!createIncomplete) f.completeExceptionally(ex1);
4844 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4845 +            ((Throwable t) -> {
4846 +                threadAssertSame(t, ex1);
4847 +                a.getAndIncrement();
4848 +                throw ex2;
4849 +            });
4850 +        if (createIncomplete) f.completeExceptionally(ex1);
4851 +
4852 +        checkCompletedWithWrappedException(g.toCompletableFuture(), ex2);
4853 +        checkCompletedExceptionally(f, ex1);
4854 +        checkCompletedExceptionally(d.toCompletableFuture(), ex1);
4855 +        assertEquals(1, a.get());
4856 +    }}
4857 +
4858 +    /**
4859 +     * default exceptionallyCompose result completes normally after normal
4860 +     * completion of source
4861 +     */
4862 +    public void testDefaultExceptionallyCompose_normalCompletion() {
4863 +        for (boolean createIncomplete : new boolean[] { true, false })
4864 +        for (Integer v1 : new Integer[] { 1, null })
4865 +    {
4866 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4867 +        final ExceptionalCompletableFutureFunction r =
4868 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4869 +        final DelegatedCompletionStage<Integer> d =
4870 +            new DelegatedCompletionStage<Integer>(f);
4871 +        if (!createIncomplete) assertTrue(f.complete(v1));
4872 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4873 +        if (createIncomplete) assertTrue(f.complete(v1));
4874 +
4875 +        checkCompletedNormally(f, v1);
4876 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4877 +        r.assertNotInvoked();
4878 +    }}
4879 +
4880 +    /**
4881 +     * default-implemented exceptionallyCompose result completes
4882 +     * normally after exceptional completion of source
4883 +     */
4884 +    public void testDefaultExceptionallyCompose_exceptionalCompletion() {
4885 +        for (boolean createIncomplete : new boolean[] { true, false })
4886 +    {
4887 +        final CFException ex = new CFException();
4888 +        final ExceptionalCompletableFutureFunction r =
4889 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4890 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4891 +        final DelegatedCompletionStage<Integer> d =
4892 +            new DelegatedCompletionStage<Integer>(f);
4893 +        if (!createIncomplete) f.completeExceptionally(ex);
4894 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4895 +        if (createIncomplete) f.completeExceptionally(ex);
4896 +
4897 +        checkCompletedExceptionally(f, ex);
4898 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4899 +        r.assertInvoked();
4900 +    }}
4901 +
4902 +    /**
4903 +     * default-implemented exceptionallyCompose completes
4904 +     * exceptionally on exception if action does
4905 +     */
4906 +    public void testDefaultExceptionallyCompose_actionFailed() {
4907 +        for (boolean createIncomplete : new boolean[] { true, false })
4908 +        for (Integer v1 : new Integer[] { 1, null })
4909 +    {
4910 +        final CFException ex = new CFException();
4911 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4912 +        final FailingExceptionalCompletableFutureFunction r
4913 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4914 +        final DelegatedCompletionStage<Integer> d =
4915 +            new DelegatedCompletionStage<Integer>(f);
4916 +        if (!createIncomplete) f.completeExceptionally(ex);
4917 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4918 +        if (createIncomplete) f.completeExceptionally(ex);
4919 +
4920 +        checkCompletedExceptionally(f, ex);
4921 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4922 +        r.assertInvoked();
4923 +    }}
4924 +
4925 +    /**
4926 +     * default exceptionallyComposeAsync result completes normally after normal
4927 +     * completion of source
4928 +     */
4929 +    public void testDefaultExceptionallyComposeAsync_normalCompletion() {
4930 +        for (boolean createIncomplete : new boolean[] { true, false })
4931 +        for (Integer v1 : new Integer[] { 1, null })
4932 +    {
4933 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4934 +        final ExceptionalCompletableFutureFunction r =
4935 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4936 +        final DelegatedCompletionStage<Integer> d =
4937 +            new DelegatedCompletionStage<Integer>(f);
4938 +        if (!createIncomplete) assertTrue(f.complete(v1));
4939 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4940 +        if (createIncomplete) assertTrue(f.complete(v1));
4941 +
4942 +        checkCompletedNormally(f, v1);
4943 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4944 +        r.assertNotInvoked();
4945 +    }}
4946 +
4947 +    /**
4948 +     * default-implemented exceptionallyComposeAsync result completes
4949 +     * normally after exceptional completion of source
4950 +     */
4951 +    public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() {
4952 +        for (boolean createIncomplete : new boolean[] { true, false })
4953 +    {
4954 +        final CFException ex = new CFException();
4955 +        final ExceptionalCompletableFutureFunction r =
4956 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4957 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4958 +        final DelegatedCompletionStage<Integer> d =
4959 +            new DelegatedCompletionStage<Integer>(f);
4960 +        if (!createIncomplete) f.completeExceptionally(ex);
4961 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4962 +        if (createIncomplete) f.completeExceptionally(ex);
4963 +
4964 +        checkCompletedExceptionally(f, ex);
4965 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4966 +        r.assertInvoked();
4967 +    }}
4968 +
4969 +    /**
4970 +     * default-implemented exceptionallyComposeAsync completes
4971 +     * exceptionally on exception if action does
4972 +     */
4973 +    public void testDefaultExceptionallyComposeAsync_actionFailed() {
4974 +        for (boolean createIncomplete : new boolean[] { true, false })
4975 +        for (Integer v1 : new Integer[] { 1, null })
4976 +    {
4977 +        final CFException ex = new CFException();
4978 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4979 +        final FailingExceptionalCompletableFutureFunction r
4980 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4981 +        final DelegatedCompletionStage<Integer> d =
4982 +            new DelegatedCompletionStage<Integer>(f);
4983 +        if (!createIncomplete) f.completeExceptionally(ex);
4984 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4985 +        if (createIncomplete) f.completeExceptionally(ex);
4986 +
4987 +        checkCompletedExceptionally(f, ex);
4988 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4989 +        r.assertInvoked();
4990 +    }}
4991 +
4992 +
4993 +    /**
4994 +     * default exceptionallyComposeAsync result completes normally after normal
4995 +     * completion of source
4996 +     */
4997 +    public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() {
4998 +        for (boolean createIncomplete : new boolean[] { true, false })
4999 +        for (Integer v1 : new Integer[] { 1, null })
5000 +    {
5001 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5002 +        final ExceptionalCompletableFutureFunction r =
5003 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5004 +        final DelegatedCompletionStage<Integer> d =
5005 +            new DelegatedCompletionStage<Integer>(f);
5006 +        if (!createIncomplete) assertTrue(f.complete(v1));
5007 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
5008 +        if (createIncomplete) assertTrue(f.complete(v1));
5009 +
5010 +        checkCompletedNormally(f, v1);
5011 +        checkCompletedNormally(g.toCompletableFuture(), v1);
5012 +        r.assertNotInvoked();
5013 +    }}
5014 +
5015 +    /**
5016 +     * default-implemented exceptionallyComposeAsync result completes
5017 +     * normally after exceptional completion of source
5018 +     */
5019 +    public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() {
5020 +        for (boolean createIncomplete : new boolean[] { true, false })
5021 +    {
5022 +        final CFException ex = new CFException();
5023 +        final ExceptionalCompletableFutureFunction r =
5024 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5025 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5026 +        final DelegatedCompletionStage<Integer> d =
5027 +            new DelegatedCompletionStage<Integer>(f);
5028 +        if (!createIncomplete) f.completeExceptionally(ex);
5029 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
5030 +        if (createIncomplete) f.completeExceptionally(ex);
5031 +
5032 +        checkCompletedExceptionally(f, ex);
5033 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
5034 +        r.assertInvoked();
5035 +    }}
5036 +
5037 +    /**
5038 +     * default-implemented exceptionallyComposeAsync completes
5039 +     * exceptionally on exception if action does
5040 +     */
5041 +    public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
5042 +        for (boolean createIncomplete : new boolean[] { true, false })
5043 +        for (Integer v1 : new Integer[] { 1, null })
5044 +    {
5045 +        final CFException ex = new CFException();
5046 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5047 +        final FailingExceptionalCompletableFutureFunction r
5048 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5049 +        final DelegatedCompletionStage<Integer> d =
5050 +            new DelegatedCompletionStage<Integer>(f);
5051 +        if (!createIncomplete) f.completeExceptionally(ex);
5052 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor());
5053 +        if (createIncomplete) f.completeExceptionally(ex);
5054 +
5055 +        checkCompletedExceptionally(f, ex);
5056 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
5057 +        r.assertInvoked();
5058 +    }}
5059 +
5060   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines