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.147 by jsr166, Sat Jun 4 23:49:29 2016 UTC vs.
Revision 1.203 by jsr166, Sat Sep 22 22:25:12 2018 UTC

# Line 30 | Line 30 | import java.util.concurrent.ExecutionExc
30   import java.util.concurrent.Executor;
31   import java.util.concurrent.ForkJoinPool;
32   import java.util.concurrent.ForkJoinTask;
33 + import java.util.concurrent.RejectedExecutionException;
34   import java.util.concurrent.TimeoutException;
34 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 41 | Line 41 | import java.util.function.Function;
41   import java.util.function.Predicate;
42   import java.util.function.Supplier;
43  
44 import junit.framework.AssertionFailedError;
44   import junit.framework.Test;
45   import junit.framework.TestSuite;
46  
# Line 59 | 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());
79 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
80 <        try {
81 <            assertEquals(value, f.getNow(null));
82 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
83 <        try {
84 <            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 142 | 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 197 | 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 296 | 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 333 | 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 361 | 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 373 | 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 382 | 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 401 | 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 411 | 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 429 | 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 439 | 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 449 | 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 <        FailingSupplier(ExecutionMode m) { super(m); }
482 >        final CFException ex;
483 >        FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); }
484          public Integer get() {
485              invoked();
486 <            throw new CFException();
486 >            throw ex;
487          }
488      }
489  
490 <    class FailingConsumer extends CheckedIntegerAction
490 >    static class FailingConsumer extends CheckedIntegerAction
491          implements Consumer<Integer>
492      {
493 <        FailingConsumer(ExecutionMode m) { super(m); }
493 >        final CFException ex;
494 >        FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
495          public void accept(Integer x) {
496              invoked();
497              value = x;
498 <            throw new CFException();
498 >            throw ex;
499          }
500      }
501  
502 <    class FailingBiConsumer extends CheckedIntegerAction
502 >    static class FailingBiConsumer extends CheckedIntegerAction
503          implements BiConsumer<Integer, Integer>
504      {
505 <        FailingBiConsumer(ExecutionMode m) { super(m); }
505 >        final CFException ex;
506 >        FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
507          public void accept(Integer x, Integer y) {
508              invoked();
509              value = subtract(x, y);
510 <            throw new CFException();
510 >            throw ex;
511          }
512      }
513  
514 <    class FailingFunction extends CheckedIntegerAction
514 >    static class FailingFunction extends CheckedIntegerAction
515          implements Function<Integer, Integer>
516      {
517 <        FailingFunction(ExecutionMode m) { super(m); }
517 >        final CFException ex;
518 >        FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); }
519          public Integer apply(Integer x) {
520              invoked();
521              value = x;
522 <            throw new CFException();
522 >            throw ex;
523          }
524      }
525  
526 <    class FailingBiFunction extends CheckedIntegerAction
526 >    static class FailingBiFunction extends CheckedIntegerAction
527          implements BiFunction<Integer, Integer, Integer>
528      {
529 <        FailingBiFunction(ExecutionMode m) { super(m); }
529 >        final CFException ex;
530 >        FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); }
531          public Integer apply(Integer x, Integer y) {
532              invoked();
533              value = subtract(x, y);
534 <            throw new CFException();
534 >            throw ex;
535          }
536      }
537  
538 <    class FailingRunnable extends CheckedAction implements Runnable {
539 <        FailingRunnable(ExecutionMode m) { super(m); }
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() {
542              invoked();
543 <            throw new CFException();
543 >            throw ex;
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<>();
529 <            assertTrue(f.complete(inc(x)));
530 <            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 <        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
582 >        final CFException ex;
583 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
584          public CompletableFuture<Integer> apply(Integer x) {
585              invoked();
586              value = x;
587 <            throw new CFException();
587 >            throw ex;
588 >        }
589 >    }
590 >
591 >    static class CountingRejectingExecutor implements Executor {
592 >        final RejectedExecutionException ex = new RejectedExecutionException();
593 >        final AtomicInteger count = new AtomicInteger(0);
594 >        public void execute(Runnable r) {
595 >            count.getAndIncrement();
596 >            throw ex;
597          }
598      }
599  
# Line 636 | 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          },
640
704          ASYNC {
705              public void checkExecutionMode() {
706                  assertEquals(defaultExecutorIsCommonPool,
# Line 710 | 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 783 | 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 825 | 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 832 | 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      {
929          final AtomicInteger a = new AtomicInteger(0);
930          final CompletableFuture<Integer> f = new CompletableFuture<>();
931          if (!createIncomplete) assertTrue(f.complete(v1));
932 <        final CompletableFuture<Integer> g = f.exceptionally
933 <            ((Throwable t) -> {
932 >        final CompletableFuture<Integer> g = m.exceptionally
933 >            (f, (Throwable t) -> {
934                  a.getAndIncrement();
935                  threadFail("should not be called");
936                  return null;            // unreached
# Line 856 | Line 947 | public class CompletableFutureTest exten
947       * exception
948       */
949      public void testExceptionally_exceptionalCompletion() {
950 +        for (ExecutionMode m : ExecutionMode.values())
951          for (boolean createIncomplete : new boolean[] { true, false })
952          for (Integer v1 : new Integer[] { 1, null })
953      {
# Line 863 | Line 955 | public class CompletableFutureTest exten
955          final CFException ex = new CFException();
956          final CompletableFuture<Integer> f = new CompletableFuture<>();
957          if (!createIncomplete) f.completeExceptionally(ex);
958 <        final CompletableFuture<Integer> g = f.exceptionally
959 <            ((Throwable t) -> {
960 <                ExecutionMode.SYNC.checkExecutionMode();
958 >        final CompletableFuture<Integer> g = m.exceptionally
959 >            (f, (Throwable t) -> {
960 >                m.checkExecutionMode();
961                  threadAssertSame(t, ex);
962                  a.getAndIncrement();
963                  return v1;
# Line 881 | Line 973 | public class CompletableFutureTest exten
973       * exceptionally with that exception
974       */
975      public void testExceptionally_exceptionalCompletionActionFailed() {
976 +        for (ExecutionMode m : ExecutionMode.values())
977          for (boolean createIncomplete : new boolean[] { true, false })
978      {
979          final AtomicInteger a = new AtomicInteger(0);
# Line 888 | Line 981 | public class CompletableFutureTest exten
981          final CFException ex2 = new CFException();
982          final CompletableFuture<Integer> f = new CompletableFuture<>();
983          if (!createIncomplete) f.completeExceptionally(ex1);
984 <        final CompletableFuture<Integer> g = f.exceptionally
985 <            ((Throwable t) -> {
986 <                ExecutionMode.SYNC.checkExecutionMode();
984 >        final CompletableFuture<Integer> g = m.exceptionally
985 >            (f, (Throwable t) -> {
986 >                m.checkExecutionMode();
987                  threadAssertSame(t, ex1);
988                  a.getAndIncrement();
989                  throw ex2;
# Line 1222 | Line 1315 | public class CompletableFutureTest exten
1315      {
1316          final FailingRunnable r = new FailingRunnable(m);
1317          final CompletableFuture<Void> f = m.runAsync(r);
1318 <        checkCompletedWithWrappedCFException(f);
1318 >        checkCompletedWithWrappedException(f, r.ex);
1319          r.assertInvoked();
1320      }}
1321  
1322 +    @SuppressWarnings("FutureReturnValueIgnored")
1323 +    public void testRunAsync_rejectingExecutor() {
1324 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1325 +        try {
1326 +            CompletableFuture.runAsync(() -> {}, e);
1327 +            shouldThrow();
1328 +        } catch (Throwable t) {
1329 +            assertSame(e.ex, t);
1330 +        }
1331 +
1332 +        assertEquals(1, e.count.get());
1333 +    }
1334 +
1335      /**
1336       * supplyAsync completes with result of supplier
1337       */
# Line 1256 | Line 1362 | public class CompletableFutureTest exten
1362      {
1363          FailingSupplier r = new FailingSupplier(m);
1364          CompletableFuture<Integer> f = m.supplyAsync(r);
1365 <        checkCompletedWithWrappedCFException(f);
1365 >        checkCompletedWithWrappedException(f, r.ex);
1366          r.assertInvoked();
1367      }}
1368  
1369 +    @SuppressWarnings("FutureReturnValueIgnored")
1370 +    public void testSupplyAsync_rejectingExecutor() {
1371 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1372 +        try {
1373 +            CompletableFuture.supplyAsync(() -> null, e);
1374 +            shouldThrow();
1375 +        } catch (Throwable t) {
1376 +            assertSame(e.ex, t);
1377 +        }
1378 +
1379 +        assertEquals(1, e.count.get());
1380 +    }
1381 +
1382      // seq completion methods
1383  
1384      /**
# Line 1378 | Line 1497 | public class CompletableFutureTest exten
1497          final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1498          final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1499  
1500 <        checkCompletedWithWrappedCFException(h0);
1501 <        checkCompletedWithWrappedCFException(h1);
1502 <        checkCompletedWithWrappedCFException(h2);
1503 <        checkCompletedWithWrappedCFException(h3);
1504 <        checkCompletedWithWrappedCFException(h4);
1505 <        checkCompletedWithWrappedCFException(h5);
1500 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1501 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1502 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1503 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1504 >        checkCompletedWithWrappedException(h4, rs[4].ex);
1505 >        checkCompletedWithWrappedException(h5, rs[5].ex);
1506          checkCompletedNormally(f, v1);
1507      }}
1508  
# Line 1482 | Line 1601 | public class CompletableFutureTest exten
1601          final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1602          final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1603  
1604 <        checkCompletedWithWrappedCFException(h0);
1605 <        checkCompletedWithWrappedCFException(h1);
1606 <        checkCompletedWithWrappedCFException(h2);
1607 <        checkCompletedWithWrappedCFException(h3);
1604 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1605 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1606 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1607 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1608          checkCompletedNormally(f, v1);
1609      }}
1610  
# Line 1584 | Line 1703 | public class CompletableFutureTest exten
1703          final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1704          final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1705  
1706 <        checkCompletedWithWrappedCFException(h0);
1707 <        checkCompletedWithWrappedCFException(h1);
1708 <        checkCompletedWithWrappedCFException(h2);
1709 <        checkCompletedWithWrappedCFException(h3);
1706 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1707 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1708 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1709 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1710          checkCompletedNormally(f, v1);
1711      }}
1712  
# Line 1749 | Line 1868 | public class CompletableFutureTest exten
1868          assertTrue(snd.complete(w2));
1869          final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1870  
1871 <        checkCompletedWithWrappedCFException(h1);
1872 <        checkCompletedWithWrappedCFException(h2);
1873 <        checkCompletedWithWrappedCFException(h3);
1871 >        checkCompletedWithWrappedException(h1, r1.ex);
1872 >        checkCompletedWithWrappedException(h2, r2.ex);
1873 >        checkCompletedWithWrappedException(h3, r3.ex);
1874          r1.assertInvoked();
1875          r2.assertInvoked();
1876          r3.assertInvoked();
# Line 1913 | Line 2032 | public class CompletableFutureTest exten
2032          assertTrue(snd.complete(w2));
2033          final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
2034  
2035 <        checkCompletedWithWrappedCFException(h1);
2036 <        checkCompletedWithWrappedCFException(h2);
2037 <        checkCompletedWithWrappedCFException(h3);
2035 >        checkCompletedWithWrappedException(h1, r1.ex);
2036 >        checkCompletedWithWrappedException(h2, r2.ex);
2037 >        checkCompletedWithWrappedException(h3, r3.ex);
2038          r1.assertInvoked();
2039          r2.assertInvoked();
2040          r3.assertInvoked();
# Line 2077 | Line 2196 | public class CompletableFutureTest exten
2196          assertTrue(snd.complete(w2));
2197          final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2198  
2199 <        checkCompletedWithWrappedCFException(h1);
2200 <        checkCompletedWithWrappedCFException(h2);
2201 <        checkCompletedWithWrappedCFException(h3);
2199 >        checkCompletedWithWrappedException(h1, r1.ex);
2200 >        checkCompletedWithWrappedException(h2, r2.ex);
2201 >        checkCompletedWithWrappedException(h3, r3.ex);
2202          r1.assertInvoked();
2203          r2.assertInvoked();
2204          r3.assertInvoked();
# Line 2369 | Line 2488 | public class CompletableFutureTest exten
2488          f.complete(v1);
2489          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2490          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2491 <        checkCompletedWithWrappedCFException(h0);
2492 <        checkCompletedWithWrappedCFException(h1);
2493 <        checkCompletedWithWrappedCFException(h2);
2494 <        checkCompletedWithWrappedCFException(h3);
2491 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2492 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2493 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2494 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2495          for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2496  
2497          g.complete(v2);
# Line 2381 | Line 2500 | public class CompletableFutureTest exten
2500          final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2501          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2502  
2503 <        checkCompletedWithWrappedCFException(h4);
2503 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2504          assertTrue(Objects.equals(v1, rs[4].value) ||
2505                     Objects.equals(v2, rs[4].value));
2506 <        checkCompletedWithWrappedCFException(h5);
2506 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2507          assertTrue(Objects.equals(v1, rs[5].value) ||
2508                     Objects.equals(v2, rs[5].value));
2509  
# Line 2522 | Line 2641 | public class CompletableFutureTest exten
2641  
2642          // unspecified behavior - both source completions available
2643          try {
2644 <            assertEquals(null, h0.join());
2644 >            assertNull(h0.join());
2645              rs[0].assertValue(v1);
2646          } catch (CompletionException ok) {
2647              checkCompletedWithWrappedException(h0, ex);
2648              rs[0].assertNotInvoked();
2649          }
2650          try {
2651 <            assertEquals(null, h1.join());
2651 >            assertNull(h1.join());
2652              rs[1].assertValue(v1);
2653          } catch (CompletionException ok) {
2654              checkCompletedWithWrappedException(h1, ex);
2655              rs[1].assertNotInvoked();
2656          }
2657          try {
2658 <            assertEquals(null, h2.join());
2658 >            assertNull(h2.join());
2659              rs[2].assertValue(v1);
2660          } catch (CompletionException ok) {
2661              checkCompletedWithWrappedException(h2, ex);
2662              rs[2].assertNotInvoked();
2663          }
2664          try {
2665 <            assertEquals(null, h3.join());
2665 >            assertNull(h3.join());
2666              rs[3].assertValue(v1);
2667          } catch (CompletionException ok) {
2668              checkCompletedWithWrappedException(h3, ex);
# Line 2628 | Line 2747 | public class CompletableFutureTest exten
2747          f.complete(v1);
2748          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2749          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2750 <        checkCompletedWithWrappedCFException(h0);
2751 <        checkCompletedWithWrappedCFException(h1);
2752 <        checkCompletedWithWrappedCFException(h2);
2753 <        checkCompletedWithWrappedCFException(h3);
2750 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2751 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2752 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2753 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2754          for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2755  
2756          g.complete(v2);
# Line 2640 | Line 2759 | public class CompletableFutureTest exten
2759          final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2760          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2761  
2762 <        checkCompletedWithWrappedCFException(h4);
2762 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2763          assertTrue(Objects.equals(v1, rs[4].value) ||
2764                     Objects.equals(v2, rs[4].value));
2765 <        checkCompletedWithWrappedCFException(h5);
2765 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2766          assertTrue(Objects.equals(v1, rs[5].value) ||
2767                     Objects.equals(v2, rs[5].value));
2768  
# Line 2659 | Line 2778 | public class CompletableFutureTest exten
2778          for (ExecutionMode m : ExecutionMode.values())
2779          for (Integer v1 : new Integer[] { 1, null })
2780          for (Integer v2 : new Integer[] { 2, null })
2781 +        for (boolean pushNop : new boolean[] { true, false })
2782      {
2783          final CompletableFuture<Integer> f = new CompletableFuture<>();
2784          final CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 2671 | Line 2791 | public class CompletableFutureTest exten
2791          checkIncomplete(h1);
2792          rs[0].assertNotInvoked();
2793          rs[1].assertNotInvoked();
2794 +        if (pushNop) {          // ad hoc test of intra-completion interference
2795 +            m.thenRun(f, () -> {});
2796 +            m.thenRun(g, () -> {});
2797 +        }
2798          f.complete(v1);
2799          checkCompletedNormally(h0, null);
2800          checkCompletedNormally(h1, null);
# Line 2777 | Line 2901 | public class CompletableFutureTest exten
2901  
2902          // unspecified behavior - both source completions available
2903          try {
2904 <            assertEquals(null, h0.join());
2904 >            assertNull(h0.join());
2905              rs[0].assertInvoked();
2906          } catch (CompletionException ok) {
2907              checkCompletedWithWrappedException(h0, ex);
2908              rs[0].assertNotInvoked();
2909          }
2910          try {
2911 <            assertEquals(null, h1.join());
2911 >            assertNull(h1.join());
2912              rs[1].assertInvoked();
2913          } catch (CompletionException ok) {
2914              checkCompletedWithWrappedException(h1, ex);
2915              rs[1].assertNotInvoked();
2916          }
2917          try {
2918 <            assertEquals(null, h2.join());
2918 >            assertNull(h2.join());
2919              rs[2].assertInvoked();
2920          } catch (CompletionException ok) {
2921              checkCompletedWithWrappedException(h2, ex);
2922              rs[2].assertNotInvoked();
2923          }
2924          try {
2925 <            assertEquals(null, h3.join());
2925 >            assertNull(h3.join());
2926              rs[3].assertInvoked();
2927          } catch (CompletionException ok) {
2928              checkCompletedWithWrappedException(h3, ex);
# Line 2883 | Line 3007 | public class CompletableFutureTest exten
3007          assertTrue(f.complete(v1));
3008          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
3009          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
3010 <        checkCompletedWithWrappedCFException(h0);
3011 <        checkCompletedWithWrappedCFException(h1);
3012 <        checkCompletedWithWrappedCFException(h2);
3013 <        checkCompletedWithWrappedCFException(h3);
3010 >        checkCompletedWithWrappedException(h0, rs[0].ex);
3011 >        checkCompletedWithWrappedException(h1, rs[1].ex);
3012 >        checkCompletedWithWrappedException(h2, rs[2].ex);
3013 >        checkCompletedWithWrappedException(h3, rs[3].ex);
3014          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
3015          assertTrue(g.complete(v2));
3016          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
3017          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
3018 <        checkCompletedWithWrappedCFException(h4);
3019 <        checkCompletedWithWrappedCFException(h5);
3018 >        checkCompletedWithWrappedException(h4, rs[4].ex);
3019 >        checkCompletedWithWrappedException(h5, rs[5].ex);
3020  
3021          checkCompletedNormally(f, v1);
3022          checkCompletedNormally(g, v2);
# Line 2953 | Line 3077 | public class CompletableFutureTest exten
3077          final CompletableFuture<Integer> g = m.thenCompose(f, r);
3078          if (createIncomplete) assertTrue(f.complete(v1));
3079  
3080 <        checkCompletedWithWrappedCFException(g);
3080 >        checkCompletedWithWrappedException(g, r.ex);
3081          checkCompletedNormally(f, v1);
3082      }}
3083  
# Line 3030 | Line 3154 | public class CompletableFutureTest exten
3154          checkCompletedNormally(f, v1);
3155      }}
3156  
3157 +    /**
3158 +     * exceptionallyCompose result completes normally after normal
3159 +     * completion of source
3160 +     */
3161 +    public void testExceptionallyCompose_normalCompletion() {
3162 +        for (ExecutionMode m : ExecutionMode.values())
3163 +        for (boolean createIncomplete : new boolean[] { true, false })
3164 +        for (Integer v1 : new Integer[] { 1, null })
3165 +    {
3166 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3167 +        final ExceptionalCompletableFutureFunction r =
3168 +            new ExceptionalCompletableFutureFunction(m);
3169 +        if (!createIncomplete) assertTrue(f.complete(v1));
3170 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3171 +        if (createIncomplete) assertTrue(f.complete(v1));
3172 +
3173 +        checkCompletedNormally(f, v1);
3174 +        checkCompletedNormally(g, v1);
3175 +        r.assertNotInvoked();
3176 +    }}
3177 +
3178 +    /**
3179 +     * exceptionallyCompose result completes normally after exceptional
3180 +     * completion of source
3181 +     */
3182 +    public void testExceptionallyCompose_exceptionalCompletion() {
3183 +        for (ExecutionMode m : ExecutionMode.values())
3184 +        for (boolean createIncomplete : new boolean[] { true, false })
3185 +    {
3186 +        final CFException ex = new CFException();
3187 +        final ExceptionalCompletableFutureFunction r =
3188 +            new ExceptionalCompletableFutureFunction(m);
3189 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3190 +        if (!createIncomplete) f.completeExceptionally(ex);
3191 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3192 +        if (createIncomplete) f.completeExceptionally(ex);
3193 +
3194 +        checkCompletedExceptionally(f, ex);
3195 +        checkCompletedNormally(g, r.value);
3196 +        r.assertInvoked();
3197 +    }}
3198 +
3199 +    /**
3200 +     * exceptionallyCompose completes exceptionally on exception if action does
3201 +     */
3202 +    public void testExceptionallyCompose_actionFailed() {
3203 +        for (ExecutionMode m : ExecutionMode.values())
3204 +        for (boolean createIncomplete : new boolean[] { true, false })
3205 +        for (Integer v1 : new Integer[] { 1, null })
3206 +    {
3207 +        final CFException ex = new CFException();
3208 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3209 +        final FailingExceptionalCompletableFutureFunction r
3210 +            = new FailingExceptionalCompletableFutureFunction(m);
3211 +        if (!createIncomplete) f.completeExceptionally(ex);
3212 +        final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r);
3213 +        if (createIncomplete) f.completeExceptionally(ex);
3214 +
3215 +        checkCompletedExceptionally(f, ex);
3216 +        checkCompletedWithWrappedException(g, r.ex);
3217 +        r.assertInvoked();
3218 +    }}
3219 +
3220 +
3221      // other static methods
3222  
3223      /**
# Line 3062 | Line 3250 | public class CompletableFutureTest exten
3250          }
3251      }
3252  
3253 <    public void testAllOf_backwards() throws Exception {
3253 >    public void testAllOf_normal_backwards() throws Exception {
3254          for (int k = 1; k < 10; k++) {
3255              CompletableFuture<Integer>[] fs
3256                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
# Line 3193 | Line 3381 | public class CompletableFutureTest exten
3381      /**
3382       * Completion methods throw NullPointerException with null arguments
3383       */
3384 +    @SuppressWarnings("FutureReturnValueIgnored")
3385      public void testNPE() {
3386          CompletableFuture<Integer> f = new CompletableFuture<>();
3387          CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 3212 | Line 3401 | public class CompletableFutureTest exten
3401  
3402              () -> f.thenApply(null),
3403              () -> f.thenApplyAsync(null),
3404 <            () -> f.thenApplyAsync((x) -> x, null),
3404 >            () -> f.thenApplyAsync(x -> x, null),
3405              () -> f.thenApplyAsync(null, exec),
3406  
3407              () -> f.thenAccept(null),
3408              () -> f.thenAcceptAsync(null),
3409 <            () -> f.thenAcceptAsync((x) -> {} , null),
3409 >            () -> f.thenAcceptAsync(x -> {} , null),
3410              () -> f.thenAcceptAsync(null, exec),
3411  
3412              () -> f.thenRun(null),
# Line 3252 | Line 3441 | public class CompletableFutureTest exten
3441              () -> f.applyToEither(g, null),
3442              () -> f.applyToEitherAsync(g, null),
3443              () -> f.applyToEitherAsync(g, null, exec),
3444 <            () -> f.applyToEither(nullFuture, (x) -> x),
3445 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3446 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3447 <            () -> f.applyToEitherAsync(g, (x) -> x, null),
3444 >            () -> f.applyToEither(nullFuture, x -> x),
3445 >            () -> f.applyToEitherAsync(nullFuture, x -> x),
3446 >            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
3447 >            () -> f.applyToEitherAsync(g, x -> x, null),
3448  
3449              () -> f.acceptEither(g, null),
3450              () -> f.acceptEitherAsync(g, null),
3451              () -> f.acceptEitherAsync(g, null, exec),
3452 <            () -> f.acceptEither(nullFuture, (x) -> {}),
3453 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3454 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3455 <            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3452 >            () -> f.acceptEither(nullFuture, x -> {}),
3453 >            () -> f.acceptEitherAsync(nullFuture, x -> {}),
3454 >            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
3455 >            () -> f.acceptEitherAsync(g, x -> {}, null),
3456  
3457              () -> f.runAfterEither(g, null),
3458              () -> f.runAfterEitherAsync(g, null),
# Line 3310 | Line 3499 | public class CompletableFutureTest exten
3499      }
3500  
3501      /**
3502 +     * Test submissions to an executor that rejects all tasks.
3503 +     */
3504 +    public void testRejectingExecutor() {
3505 +        for (Integer v : new Integer[] { 1, null })
3506 +    {
3507 +        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3508 +
3509 +        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3510 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3511 +
3512 +        List<CompletableFuture<?>> futures = new ArrayList<>();
3513 +
3514 +        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3515 +        srcs.add(complete);
3516 +        srcs.add(incomplete);
3517 +
3518 +        for (CompletableFuture<Integer> src : srcs) {
3519 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3520 +            fs.add(src.thenRunAsync(() -> {}, e));
3521 +            fs.add(src.thenAcceptAsync(z -> {}, e));
3522 +            fs.add(src.thenApplyAsync(z -> z, e));
3523 +
3524 +            fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3525 +            fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3526 +            fs.add(src.runAfterBothAsync(src, () -> {}, e));
3527 +
3528 +            fs.add(src.applyToEitherAsync(src, z -> z, e));
3529 +            fs.add(src.acceptEitherAsync(src, z -> {}, e));
3530 +            fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3531 +
3532 +            fs.add(src.thenComposeAsync(z -> null, e));
3533 +            fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3534 +            fs.add(src.handleAsync((z, t) -> null, e));
3535 +
3536 +            for (CompletableFuture<?> future : fs) {
3537 +                if (src.isDone())
3538 +                    checkCompletedWithWrappedException(future, e.ex);
3539 +                else
3540 +                    checkIncomplete(future);
3541 +            }
3542 +            futures.addAll(fs);
3543 +        }
3544 +
3545 +        {
3546 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3547 +
3548 +            fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3549 +            fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3550 +
3551 +            fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3552 +            fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3553 +
3554 +            fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3555 +            fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3556 +
3557 +            for (CompletableFuture<?> future : fs)
3558 +                checkIncomplete(future);
3559 +            futures.addAll(fs);
3560 +        }
3561 +
3562 +        {
3563 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3564 +
3565 +            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
3566 +            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
3567 +
3568 +            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
3569 +            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
3570 +
3571 +            fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3572 +            fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
3573 +
3574 +            for (CompletableFuture<?> future : fs)
3575 +                checkCompletedWithWrappedException(future, e.ex);
3576 +            futures.addAll(fs);
3577 +        }
3578 +
3579 +        incomplete.complete(v);
3580 +
3581 +        for (CompletableFuture<?> future : futures)
3582 +            checkCompletedWithWrappedException(future, e.ex);
3583 +
3584 +        assertEquals(futures.size(), e.count.get());
3585 +    }}
3586 +
3587 +    /**
3588 +     * Test submissions to an executor that rejects all tasks, but
3589 +     * should never be invoked because the dependent future is
3590 +     * explicitly completed.
3591 +     */
3592 +    public void testRejectingExecutorNeverInvoked() {
3593 +        for (Integer v : new Integer[] { 1, null })
3594 +    {
3595 +        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3596 +
3597 +        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3598 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3599 +
3600 +        List<CompletableFuture<?>> futures = new ArrayList<>();
3601 +
3602 +        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3603 +        srcs.add(complete);
3604 +        srcs.add(incomplete);
3605 +
3606 +        List<CompletableFuture<?>> fs = new ArrayList<>();
3607 +        fs.add(incomplete.thenRunAsync(() -> {}, e));
3608 +        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
3609 +        fs.add(incomplete.thenApplyAsync(z -> z, e));
3610 +
3611 +        fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3612 +        fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3613 +        fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3614 +
3615 +        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
3616 +        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
3617 +        fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3618 +
3619 +        fs.add(incomplete.thenComposeAsync(z -> null, e));
3620 +        fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3621 +        fs.add(incomplete.handleAsync((z, t) -> null, e));
3622 +
3623 +        fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3624 +        fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3625 +
3626 +        fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3627 +        fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3628 +
3629 +        fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3630 +        fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3631 +
3632 +        for (CompletableFuture<?> future : fs)
3633 +            checkIncomplete(future);
3634 +
3635 +        for (CompletableFuture<?> future : fs)
3636 +            future.complete(null);
3637 +
3638 +        incomplete.complete(v);
3639 +
3640 +        for (CompletableFuture<?> future : fs)
3641 +            checkCompletedNormally(future, null);
3642 +
3643 +        assertEquals(0, e.count.get());
3644 +    }}
3645 +
3646 +    /**
3647       * toCompletableFuture returns this CompletableFuture.
3648       */
3649      public void testToCompletableFuture() {
# Line 3342 | Line 3676 | public class CompletableFutureTest exten
3676       */
3677      public void testCompletedStage() {
3678          AtomicInteger x = new AtomicInteger(0);
3679 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3679 >        AtomicReference<Throwable> r = new AtomicReference<>();
3680          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3681          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3682          assertEquals(x.get(), 1);
# Line 3387 | Line 3721 | public class CompletableFutureTest exten
3721       * copy returns a CompletableFuture that is completed normally,
3722       * with the same value, when source is.
3723       */
3724 <    public void testCopy() {
3724 >    public void testCopy_normalCompletion() {
3725 >        for (boolean createIncomplete : new boolean[] { true, false })
3726 >        for (Integer v1 : new Integer[] { 1, null })
3727 >    {
3728          CompletableFuture<Integer> f = new CompletableFuture<>();
3729 +        if (!createIncomplete) assertTrue(f.complete(v1));
3730          CompletableFuture<Integer> g = f.copy();
3731 <        checkIncomplete(f);
3732 <        checkIncomplete(g);
3733 <        f.complete(1);
3734 <        checkCompletedNormally(f, 1);
3735 <        checkCompletedNormally(g, 1);
3736 <    }
3731 >        if (createIncomplete) {
3732 >            checkIncomplete(f);
3733 >            checkIncomplete(g);
3734 >            assertTrue(f.complete(v1));
3735 >        }
3736 >        checkCompletedNormally(f, v1);
3737 >        checkCompletedNormally(g, v1);
3738 >    }}
3739  
3740      /**
3741       * copy returns a CompletableFuture that is completed exceptionally
3742       * when source is.
3743       */
3744 <    public void testCopy2() {
3744 >    public void testCopy_exceptionalCompletion() {
3745 >        for (boolean createIncomplete : new boolean[] { true, false })
3746 >    {
3747 >        CFException ex = new CFException();
3748          CompletableFuture<Integer> f = new CompletableFuture<>();
3749 +        if (!createIncomplete) f.completeExceptionally(ex);
3750          CompletableFuture<Integer> g = f.copy();
3751 <        checkIncomplete(f);
3752 <        checkIncomplete(g);
3753 <        CFException ex = new CFException();
3754 <        f.completeExceptionally(ex);
3751 >        if (createIncomplete) {
3752 >            checkIncomplete(f);
3753 >            checkIncomplete(g);
3754 >            f.completeExceptionally(ex);
3755 >        }
3756          checkCompletedExceptionally(f, ex);
3757          checkCompletedWithWrappedException(g, ex);
3758 +    }}
3759 +
3760 +    /**
3761 +     * Completion of a copy does not complete its source.
3762 +     */
3763 +    public void testCopy_oneWayPropagation() {
3764 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3765 +        assertTrue(f.copy().complete(1));
3766 +        assertTrue(f.copy().complete(null));
3767 +        assertTrue(f.copy().cancel(true));
3768 +        assertTrue(f.copy().cancel(false));
3769 +        assertTrue(f.copy().completeExceptionally(new CFException()));
3770 +        checkIncomplete(f);
3771      }
3772  
3773      /**
# Line 3420 | Line 3778 | public class CompletableFutureTest exten
3778          CompletableFuture<Integer> f = new CompletableFuture<>();
3779          CompletionStage<Integer> g = f.minimalCompletionStage();
3780          AtomicInteger x = new AtomicInteger(0);
3781 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3781 >        AtomicReference<Throwable> r = new AtomicReference<>();
3782          checkIncomplete(f);
3783          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3784          f.complete(1);
# Line 3437 | Line 3795 | public class CompletableFutureTest exten
3795          CompletableFuture<Integer> f = new CompletableFuture<>();
3796          CompletionStage<Integer> g = f.minimalCompletionStage();
3797          AtomicInteger x = new AtomicInteger(0);
3798 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3798 >        AtomicReference<Throwable> r = new AtomicReference<>();
3799          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3800          checkIncomplete(f);
3801          CFException ex = new CFException();
# Line 3455 | Line 3813 | public class CompletableFutureTest exten
3813          CFException ex = new CFException();
3814          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3815          AtomicInteger x = new AtomicInteger(0);
3816 <        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3816 >        AtomicReference<Throwable> r = new AtomicReference<>();
3817          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3818          assertEquals(x.get(), 0);
3819          assertEquals(r.get(), ex);
# Line 3479 | Line 3837 | public class CompletableFutureTest exten
3837      public void testCompleteAsync2() {
3838          CompletableFuture<Integer> f = new CompletableFuture<>();
3839          CFException ex = new CFException();
3840 <        f.completeAsync(() -> {if (true) throw ex; return 1;});
3840 >        f.completeAsync(() -> { throw ex; });
3841          try {
3842              f.join();
3843              shouldThrow();
# Line 3509 | Line 3867 | public class CompletableFutureTest exten
3867          CompletableFuture<Integer> f = new CompletableFuture<>();
3868          CFException ex = new CFException();
3869          ThreadExecutor executor = new ThreadExecutor();
3870 <        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3870 >        f.completeAsync(() -> { throw ex; }, executor);
3871          try {
3872              f.join();
3873              shouldThrow();
# Line 3661 | Line 4019 | public class CompletableFutureTest exten
4019          final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
4020          final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4021  
4022 +        final Runnable noopRunnable = new Noop(m);
4023 +        final Consumer<Integer> noopConsumer = new NoopConsumer(m);
4024 +        final Function<Integer, Integer> incFunction = new IncFunction(m);
4025 +
4026          List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
4027              = new ArrayList<>();
4028  
4029 <        funs.add((y) -> m.thenRun(y, new Noop(m)));
4030 <        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
4031 <        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
4032 <
4033 <        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
4034 <        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
4035 <        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
4036 <
4037 <        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
4038 <        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
4039 <        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
4040 <
4041 <        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
4042 <
4043 <        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
4044 <
4045 <        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
4046 <        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
4029 >        funs.add(y -> m.thenRun(y, noopRunnable));
4030 >        funs.add(y -> m.thenAccept(y, noopConsumer));
4031 >        funs.add(y -> m.thenApply(y, incFunction));
4032 >
4033 >        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
4034 >        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
4035 >        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
4036 >
4037 >        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
4038 >        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
4039 >        funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
4040 >        funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
4041 >        funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m)));
4042 >        funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m)));
4043 >
4044 >        funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
4045 >
4046 >        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
4047 >
4048 >        funs.add(y -> CompletableFuture.allOf(y));
4049 >        funs.add(y -> CompletableFuture.allOf(y, v42));
4050 >        funs.add(y -> CompletableFuture.allOf(v42, y));
4051 >        funs.add(y -> CompletableFuture.anyOf(y));
4052 >        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
4053 >        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
4054  
4055          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
4056                   fun : funs) {
4057              CompletableFuture<Integer> f = new CompletableFuture<>();
4058              f.completeExceptionally(ex);
4059 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
4059 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
4060              checkCompletedWithWrappedException(src, ex);
4061              CompletableFuture<?> dep = fun.apply(src);
4062              checkCompletedWithWrappedException(dep, ex);
# Line 3697 | Line 4066 | public class CompletableFutureTest exten
4066          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
4067                   fun : funs) {
4068              CompletableFuture<Integer> f = new CompletableFuture<>();
4069 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
4069 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
4070              CompletableFuture<?> dep = fun.apply(src);
4071              f.completeExceptionally(ex);
4072              checkCompletedWithWrappedException(src, ex);
# Line 3711 | Line 4080 | public class CompletableFutureTest exten
4080              CompletableFuture<Integer> f = new CompletableFuture<>();
4081              f.cancel(mayInterruptIfRunning);
4082              checkCancelled(f);
4083 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
4083 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
4084              checkCompletedWithWrappedCancellationException(src);
4085              CompletableFuture<?> dep = fun.apply(src);
4086              checkCompletedWithWrappedCancellationException(dep);
# Line 3722 | Line 4091 | public class CompletableFutureTest exten
4091          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
4092                   fun : funs) {
4093              CompletableFuture<Integer> f = new CompletableFuture<>();
4094 <            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
4094 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
4095              CompletableFuture<?> dep = fun.apply(src);
4096              f.cancel(mayInterruptIfRunning);
4097              checkCancelled(f);
# Line 3733 | Line 4102 | public class CompletableFutureTest exten
4102      }}
4103  
4104      /**
4105 <     * Minimal completion stages throw UOE for all non-CompletionStage methods
4105 >     * Minimal completion stages throw UOE for most non-CompletionStage methods
4106       */
4107      public void testMinimalCompletionStage_minimality() {
4108          if (!testImplementationDetails) return;
4109          Function<Method, String> toSignature =
4110 <            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
4110 >            method -> method.getName() + Arrays.toString(method.getParameterTypes());
4111          Predicate<Method> isNotStatic =
4112 <            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
4112 >            method -> (method.getModifiers() & Modifier.STATIC) == 0;
4113          List<Method> minimalMethods =
4114              Stream.of(Object.class, CompletionStage.class)
4115 <            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
4115 >            .flatMap(klazz -> Stream.of(klazz.getMethods()))
4116              .filter(isNotStatic)
4117              .collect(Collectors.toList());
4118          // Methods from CompletableFuture permitted NOT to throw UOE
# Line 3759 | Line 4128 | public class CompletableFutureTest exten
4128              .collect(Collectors.toSet());
4129          List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
4130              .filter(isNotStatic)
4131 <            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4131 >            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4132              .collect(Collectors.toList());
4133  
4134 <        CompletionStage<Integer> minimalStage =
4134 >        List<CompletionStage<Integer>> stages = new ArrayList<>();
4135 >        CompletionStage<Integer> min =
4136              new CompletableFuture<Integer>().minimalCompletionStage();
4137 +        stages.add(min);
4138 +        stages.add(min.thenApply(x -> x));
4139 +        stages.add(CompletableFuture.completedStage(1));
4140 +        stages.add(CompletableFuture.failedStage(new CFException()));
4141  
4142          List<Method> bugs = new ArrayList<>();
4143          for (Method method : allMethods) {
# Line 3779 | Line 4153 | public class CompletableFutureTest exten
4153                  else if (parameterTypes[i] == long.class)
4154                      args[i] = 0L;
4155              }
4156 <            try {
4157 <                method.invoke(minimalStage, args);
4158 <                bugs.add(method);
3785 <            }
3786 <            catch (java.lang.reflect.InvocationTargetException expected) {
3787 <                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4156 >            for (CompletionStage<Integer> stage : stages) {
4157 >                try {
4158 >                    method.invoke(stage, args);
4159                      bugs.add(method);
3789                    // expected.getCause().printStackTrace();
4160                  }
4161 +                catch (java.lang.reflect.InvocationTargetException expected) {
4162 +                    if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4163 +                        bugs.add(method);
4164 +                        // expected.getCause().printStackTrace();
4165 +                    }
4166 +                }
4167 +                catch (ReflectiveOperationException bad) { throw new Error(bad); }
4168              }
3792            catch (ReflectiveOperationException bad) { throw new Error(bad); }
4169          }
4170          if (!bugs.isEmpty())
4171 <            throw new Error("Methods did not throw UOE: " + bugs.toString());
4171 >            throw new Error("Methods did not throw UOE: " + bugs);
4172      }
4173  
4174 +    /**
4175 +     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4176 +     * is completed normally, with the same value, when source is.
4177 +     */
4178 +    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
4179 +        for (boolean createIncomplete : new boolean[] { true, false })
4180 +        for (Integer v1 : new Integer[] { 1, null })
4181 +    {
4182 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4183 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4184 +        if (!createIncomplete) assertTrue(f.complete(v1));
4185 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4186 +        if (createIncomplete) {
4187 +            checkIncomplete(f);
4188 +            checkIncomplete(g);
4189 +            assertTrue(f.complete(v1));
4190 +        }
4191 +        checkCompletedNormally(f, v1);
4192 +        checkCompletedNormally(g, v1);
4193 +    }}
4194 +
4195 +    /**
4196 +     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4197 +     * is completed exceptionally when source is.
4198 +     */
4199 +    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
4200 +        for (boolean createIncomplete : new boolean[] { true, false })
4201 +    {
4202 +        CFException ex = new CFException();
4203 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4204 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4205 +        if (!createIncomplete) f.completeExceptionally(ex);
4206 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4207 +        if (createIncomplete) {
4208 +            checkIncomplete(f);
4209 +            checkIncomplete(g);
4210 +            f.completeExceptionally(ex);
4211 +        }
4212 +        checkCompletedExceptionally(f, ex);
4213 +        checkCompletedWithWrappedException(g, ex);
4214 +    }}
4215 +
4216 +    /**
4217 +     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
4218 +     */
4219 +    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
4220 +        for (Integer v1 : new Integer[] { 1, null })
4221 +    {
4222 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4223 +        CompletionStage minimal = f.minimalCompletionStage();
4224 +        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4225 +        assertTrue(g.complete(v1));
4226 +        checkCompletedNormally(g, v1);
4227 +        checkIncomplete(f);
4228 +        checkIncomplete(minimal.toCompletableFuture());
4229 +    }}
4230 +
4231 +    /**
4232 +     * minimalStage.toCompletableFuture().join() awaits completion
4233 +     */
4234 +    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
4235 +        for (boolean createIncomplete : new boolean[] { true, false })
4236 +        for (Integer v1 : new Integer[] { 1, null })
4237 +    {
4238 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4239 +        if (!createIncomplete) assertTrue(f.complete(v1));
4240 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4241 +        if (createIncomplete) assertTrue(f.complete(v1));
4242 +        assertEquals(v1, minimal.toCompletableFuture().join());
4243 +        assertEquals(v1, minimal.toCompletableFuture().get());
4244 +        checkCompletedNormally(minimal.toCompletableFuture(), v1);
4245 +    }}
4246 +
4247 +    /**
4248 +     * Completion of a toCompletableFuture copy of a minimal stage
4249 +     * does not complete its source.
4250 +     */
4251 +    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
4252 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4253 +        CompletionStage<Integer> g = f.minimalCompletionStage();
4254 +        assertTrue(g.toCompletableFuture().complete(1));
4255 +        assertTrue(g.toCompletableFuture().complete(null));
4256 +        assertTrue(g.toCompletableFuture().cancel(true));
4257 +        assertTrue(g.toCompletableFuture().cancel(false));
4258 +        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
4259 +        checkIncomplete(g.toCompletableFuture());
4260 +        f.complete(1);
4261 +        checkCompletedNormally(g.toCompletableFuture(), 1);
4262 +    }
4263 +
4264 +    /** Demo utility method for external reliable toCompletableFuture */
4265 +    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
4266 +        CompletableFuture<T> f = new CompletableFuture<>();
4267 +        stage.handle((T t, Throwable ex) -> {
4268 +                         if (ex != null) f.completeExceptionally(ex);
4269 +                         else f.complete(t);
4270 +                         return null;
4271 +                     });
4272 +        return f;
4273 +    }
4274 +
4275 +    /** Demo utility method to join a CompletionStage */
4276 +    static <T> T join(CompletionStage<T> stage) {
4277 +        return toCompletableFuture(stage).join();
4278 +    }
4279 +
4280 +    /**
4281 +     * Joining a minimal stage "by hand" works
4282 +     */
4283 +    public void testMinimalCompletionStage_join_by_hand() {
4284 +        for (boolean createIncomplete : new boolean[] { true, false })
4285 +        for (Integer v1 : new Integer[] { 1, null })
4286 +    {
4287 +        CompletableFuture<Integer> f = new CompletableFuture<>();
4288 +        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4289 +        CompletableFuture<Integer> g = new CompletableFuture<>();
4290 +        if (!createIncomplete) assertTrue(f.complete(v1));
4291 +        minimal.thenAccept(x -> g.complete(x));
4292 +        if (createIncomplete) assertTrue(f.complete(v1));
4293 +        g.join();
4294 +        checkCompletedNormally(g, v1);
4295 +        checkCompletedNormally(f, v1);
4296 +        assertEquals(v1, join(minimal));
4297 +    }}
4298 +
4299      static class Monad {
4300          static class ZeroException extends RuntimeException {
4301              public ZeroException() { super("monadic zero"); }
# Line 3811 | Line 4312 | public class CompletableFutureTest exten
4312          static <T,U,V> Function<T, CompletableFuture<V>> compose
4313              (Function<T, CompletableFuture<U>> f,
4314               Function<U, CompletableFuture<V>> g) {
4315 <            return (x) -> f.apply(x).thenCompose(g);
4315 >            return x -> f.apply(x).thenCompose(g);
4316          }
4317  
4318          static void assertZero(CompletableFuture<?> f) {
4319              try {
4320                  f.getNow(null);
4321 <                throw new AssertionFailedError("should throw");
4321 >                throw new AssertionError("should throw");
4322              } catch (CompletionException success) {
4323                  assertTrue(success.getCause() instanceof ZeroException);
4324              }
# Line 3891 | Line 4392 | public class CompletableFutureTest exten
4392  
4393          // Some mutually non-commutative functions
4394          Function<Long, CompletableFuture<Long>> triple
4395 <            = (x) -> Monad.unit(3 * x);
4395 >            = x -> Monad.unit(3 * x);
4396          Function<Long, CompletableFuture<Long>> inc
4397 <            = (x) -> Monad.unit(x + 1);
4397 >            = x -> Monad.unit(x + 1);
4398  
4399          // unit is a right identity: m >>= unit === m
4400          Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
# Line 3905 | Line 4406 | public class CompletableFutureTest exten
4406          // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
4407          Monad.assertFutureEquals(
4408              unit.apply(5L).thenCompose(inc).thenCompose(triple),
4409 <            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
4409 >            unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple)));
4410  
4411          // The case for CompletableFuture as an additive monad is weaker...
4412  
# Line 3915 | Line 4416 | public class CompletableFutureTest exten
4416          // left zero: zero >>= f === zero
4417          Monad.assertZero(zero.thenCompose(inc));
4418          // right zero: f >>= (\x -> zero) === zero
4419 <        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
4419 >        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
4420  
4421          // f plus zero === f
4422          Monad.assertFutureEquals(Monad.unit(5L),
# Line 3941 | Line 4442 | public class CompletableFutureTest exten
4442                                   Monad.plus(godot, Monad.unit(5L)));
4443      }
4444  
4445 +    /** Test long recursive chains of CompletableFutures with cascading completions */
4446 +    @SuppressWarnings("FutureReturnValueIgnored")
4447 +    public void testRecursiveChains() throws Throwable {
4448 +        for (ExecutionMode m : ExecutionMode.values())
4449 +        for (boolean addDeadEnds : new boolean[] { true, false })
4450 +    {
4451 +        final int val = 42;
4452 +        final int n = expensiveTests ? 1_000 : 2;
4453 +        CompletableFuture<Integer> head = new CompletableFuture<>();
4454 +        CompletableFuture<Integer> tail = head;
4455 +        for (int i = 0; i < n; i++) {
4456 +            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
4457 +            tail = m.thenApply(tail, v -> v + 1);
4458 +            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
4459 +            tail = m.applyToEither(tail, tail, v -> v + 1);
4460 +            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
4461 +            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
4462 +        }
4463 +        head.complete(val);
4464 +        assertEquals(val + 3 * n, (int) tail.join());
4465 +    }}
4466 +
4467      /**
4468       * A single CompletableFuture with many dependents.
4469       * A demo of scalability - runtime is O(n).
4470       */
4471 +    @SuppressWarnings("FutureReturnValueIgnored")
4472      public void testManyDependents() throws Throwable {
4473 <        final int n = 1_000;
4473 >        final int n = expensiveTests ? 1_000_000 : 10;
4474          final CompletableFuture<Void> head = new CompletableFuture<>();
4475          final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
4476          final AtomicInteger count = new AtomicInteger(0);
4477          for (int i = 0; i < n; i++) {
4478              head.thenRun(() -> count.getAndIncrement());
4479 <            head.thenAccept((x) -> count.getAndIncrement());
4480 <            head.thenApply((x) -> count.getAndIncrement());
4479 >            head.thenAccept(x -> count.getAndIncrement());
4480 >            head.thenApply(x -> count.getAndIncrement());
4481  
4482              head.runAfterBoth(complete, () -> count.getAndIncrement());
4483              head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
# Line 3963 | Line 4487 | public class CompletableFutureTest exten
4487              complete.thenCombine(head, (x, y) -> count.getAndIncrement());
4488  
4489              head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
4490 <            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4491 <            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
4490 >            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4491 >            head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4492              new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
4493 <            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
4494 <            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
4493 >            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
4494 >            new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement());
4495          }
4496          head.complete(null);
4497          assertEquals(5 * 3 * n, count.get());
4498      }
4499  
4500 +    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4501 +    @SuppressWarnings("FutureReturnValueIgnored")
4502 +    public void testCoCompletionGarbageRetention() throws Throwable {
4503 +        final int n = expensiveTests ? 1_000_000 : 10;
4504 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4505 +        CompletableFuture<Integer> f;
4506 +        for (int i = 0; i < n; i++) {
4507 +            f = new CompletableFuture<>();
4508 +            f.runAfterEither(incomplete, () -> {});
4509 +            f.complete(null);
4510 +
4511 +            f = new CompletableFuture<>();
4512 +            f.acceptEither(incomplete, x -> {});
4513 +            f.complete(null);
4514 +
4515 +            f = new CompletableFuture<>();
4516 +            f.applyToEither(incomplete, x -> x);
4517 +            f.complete(null);
4518 +
4519 +            f = new CompletableFuture<>();
4520 +            CompletableFuture.anyOf(f, incomplete);
4521 +            f.complete(null);
4522 +        }
4523 +
4524 +        for (int i = 0; i < n; i++) {
4525 +            f = new CompletableFuture<>();
4526 +            incomplete.runAfterEither(f, () -> {});
4527 +            f.complete(null);
4528 +
4529 +            f = new CompletableFuture<>();
4530 +            incomplete.acceptEither(f, x -> {});
4531 +            f.complete(null);
4532 +
4533 +            f = new CompletableFuture<>();
4534 +            incomplete.applyToEither(f, x -> x);
4535 +            f.complete(null);
4536 +
4537 +            f = new CompletableFuture<>();
4538 +            CompletableFuture.anyOf(incomplete, f);
4539 +            f.complete(null);
4540 +        }
4541 +    }
4542 +
4543 +    /**
4544 +     * Reproduction recipe for:
4545 +     * 8160402: Garbage retention with CompletableFuture.anyOf
4546 +     * 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
4547 +     */
4548 +    public void testAnyOfGarbageRetention() throws Throwable {
4549 +        for (Integer v : new Integer[] { 1, null })
4550 +    {
4551 +        final int n = expensiveTests ? 100_000 : 10;
4552 +        CompletableFuture<Integer>[] fs
4553 +            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4554 +        for (int i = 0; i < fs.length; i++)
4555 +            fs[i] = new CompletableFuture<>();
4556 +        fs[fs.length - 1].complete(v);
4557 +        for (int i = 0; i < n; i++)
4558 +            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4559 +    }}
4560 +
4561 +    /**
4562 +     * Checks for garbage retention with allOf.
4563 +     *
4564 +     * As of 2016-07, fails with OOME:
4565 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck
4566 +     */
4567 +    public void testCancelledAllOfGarbageRetention() throws Throwable {
4568 +        final int n = expensiveTests ? 100_000 : 10;
4569 +        CompletableFuture<Integer>[] fs
4570 +            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4571 +        for (int i = 0; i < fs.length; i++)
4572 +            fs[i] = new CompletableFuture<>();
4573 +        for (int i = 0; i < n; i++)
4574 +            assertTrue(CompletableFuture.allOf(fs).cancel(false));
4575 +    }
4576 +
4577 +    /**
4578 +     * Checks for garbage retention when a dependent future is
4579 +     * cancelled and garbage-collected.
4580 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4581 +     *
4582 +     * As of 2016-07, fails with OOME:
4583 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck
4584 +     */
4585 +    public void testCancelledGarbageRetention() throws Throwable {
4586 +        final int n = expensiveTests ? 100_000 : 10;
4587 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4588 +        for (int i = 0; i < n; i++)
4589 +            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
4590 +    }
4591 +
4592 +    /**
4593 +     * Checks for garbage retention when MinimalStage.toCompletableFuture()
4594 +     * is invoked many times.
4595 +     * 8161600: Garbage retention when source CompletableFutures are never completed
4596 +     *
4597 +     * As of 2016-07, fails with OOME:
4598 +     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
4599 +     */
4600 +    public void testToCompletableFutureGarbageRetention() throws Throwable {
4601 +        final int n = expensiveTests ? 900_000 : 10;
4602 +        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4603 +        CompletionStage minimal = neverCompleted.minimalCompletionStage();
4604 +        for (int i = 0; i < n; i++)
4605 +            assertTrue(minimal.toCompletableFuture().cancel(true));
4606 +    }
4607 +
4608   //     static <U> U join(CompletionStage<U> stage) {
4609   //         CompletableFuture<U> f = new CompletableFuture<>();
4610   //         stage.whenComplete((v, ex) -> {
# Line 3997 | Line 4629 | public class CompletableFutureTest exten
4629   //         return stage.toCompletableFuture().copy().isDone();
4630   //     }
4631  
4632 +    // For testing default implementations
4633 +    // Only non-default interface methods defined.
4634 +    static final class DelegatedCompletionStage<T> implements CompletionStage<T> {
4635 +        final CompletableFuture<T> cf;
4636 +        DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; }
4637 +        public CompletableFuture<T> toCompletableFuture() {
4638 +            return cf; }
4639 +        public CompletionStage<Void> thenRun
4640 +            (Runnable action) {
4641 +            return cf.thenRun(action); }
4642 +        public CompletionStage<Void> thenRunAsync
4643 +            (Runnable action) {
4644 +            return cf.thenRunAsync(action); }
4645 +        public CompletionStage<Void> thenRunAsync
4646 +            (Runnable action,
4647 +             Executor executor) {
4648 +            return cf.thenRunAsync(action, executor); }
4649 +        public CompletionStage<Void> thenAccept
4650 +            (Consumer<? super T> action) {
4651 +            return cf.thenAccept(action); }
4652 +        public CompletionStage<Void> thenAcceptAsync
4653 +            (Consumer<? super T> action) {
4654 +            return cf.thenAcceptAsync(action); }
4655 +        public CompletionStage<Void> thenAcceptAsync
4656 +            (Consumer<? super T> action,
4657 +             Executor executor) {
4658 +            return cf.thenAcceptAsync(action, executor); }
4659 +        public <U> CompletionStage<U> thenApply
4660 +            (Function<? super T,? extends U> a) {
4661 +            return cf.thenApply(a); }
4662 +        public <U> CompletionStage<U> thenApplyAsync
4663 +            (Function<? super T,? extends U> fn) {
4664 +            return cf.thenApplyAsync(fn); }
4665 +        public <U> CompletionStage<U> thenApplyAsync
4666 +            (Function<? super T,? extends U> fn,
4667 +             Executor executor) {
4668 +            return cf.thenApplyAsync(fn, executor); }
4669 +        public <U,V> CompletionStage<V> thenCombine
4670 +            (CompletionStage<? extends U> other,
4671 +             BiFunction<? super T,? super U,? extends V> fn) {
4672 +            return cf.thenCombine(other, fn); }
4673 +        public <U,V> CompletionStage<V> thenCombineAsync
4674 +            (CompletionStage<? extends U> other,
4675 +             BiFunction<? super T,? super U,? extends V> fn) {
4676 +            return cf.thenCombineAsync(other, fn); }
4677 +        public <U,V> CompletionStage<V> thenCombineAsync
4678 +            (CompletionStage<? extends U> other,
4679 +             BiFunction<? super T,? super U,? extends V> fn,
4680 +             Executor executor) {
4681 +            return cf.thenCombineAsync(other, fn, executor); }
4682 +        public <U> CompletionStage<Void> thenAcceptBoth
4683 +            (CompletionStage<? extends U> other,
4684 +             BiConsumer<? super T, ? super U> action) {
4685 +            return cf.thenAcceptBoth(other, action); }
4686 +        public <U> CompletionStage<Void> thenAcceptBothAsync
4687 +            (CompletionStage<? extends U> other,
4688 +             BiConsumer<? super T, ? super U> action) {
4689 +            return cf.thenAcceptBothAsync(other, action); }
4690 +        public <U> CompletionStage<Void> thenAcceptBothAsync
4691 +            (CompletionStage<? extends U> other,
4692 +             BiConsumer<? super T, ? super U> action,
4693 +             Executor executor) {
4694 +            return cf.thenAcceptBothAsync(other, action, executor); }
4695 +        public CompletionStage<Void> runAfterBoth
4696 +            (CompletionStage<?> other,
4697 +             Runnable action) {
4698 +            return cf.runAfterBoth(other, action); }
4699 +        public CompletionStage<Void> runAfterBothAsync
4700 +            (CompletionStage<?> other,
4701 +             Runnable action) {
4702 +            return cf.runAfterBothAsync(other, action); }
4703 +        public CompletionStage<Void> runAfterBothAsync
4704 +            (CompletionStage<?> other,
4705 +             Runnable action,
4706 +             Executor executor) {
4707 +            return cf.runAfterBothAsync(other, action, executor); }
4708 +        public <U> CompletionStage<U> applyToEither
4709 +            (CompletionStage<? extends T> other,
4710 +             Function<? super T, U> fn) {
4711 +            return cf.applyToEither(other, fn); }
4712 +        public <U> CompletionStage<U> applyToEitherAsync
4713 +            (CompletionStage<? extends T> other,
4714 +             Function<? super T, U> fn) {
4715 +            return cf.applyToEitherAsync(other, fn); }
4716 +        public <U> CompletionStage<U> applyToEitherAsync
4717 +            (CompletionStage<? extends T> other,
4718 +             Function<? super T, U> fn,
4719 +             Executor executor) {
4720 +            return cf.applyToEitherAsync(other, fn, executor); }
4721 +        public CompletionStage<Void> acceptEither
4722 +            (CompletionStage<? extends T> other,
4723 +             Consumer<? super T> action) {
4724 +            return cf.acceptEither(other, action); }
4725 +        public CompletionStage<Void> acceptEitherAsync
4726 +            (CompletionStage<? extends T> other,
4727 +             Consumer<? super T> action) {
4728 +            return cf.acceptEitherAsync(other, action); }
4729 +        public CompletionStage<Void> acceptEitherAsync
4730 +            (CompletionStage<? extends T> other,
4731 +             Consumer<? super T> action,
4732 +             Executor executor) {
4733 +            return cf.acceptEitherAsync(other, action, executor); }
4734 +        public CompletionStage<Void> runAfterEither
4735 +            (CompletionStage<?> other,
4736 +             Runnable action) {
4737 +            return cf.runAfterEither(other, action); }
4738 +        public CompletionStage<Void> runAfterEitherAsync
4739 +            (CompletionStage<?> other,
4740 +             Runnable action) {
4741 +            return cf.runAfterEitherAsync(other, action); }
4742 +        public CompletionStage<Void> runAfterEitherAsync
4743 +            (CompletionStage<?> other,
4744 +             Runnable action,
4745 +             Executor executor) {
4746 +            return cf.runAfterEitherAsync(other, action, executor); }
4747 +        public <U> CompletionStage<U> thenCompose
4748 +            (Function<? super T, ? extends CompletionStage<U>> fn) {
4749 +            return cf.thenCompose(fn); }
4750 +        public <U> CompletionStage<U> thenComposeAsync
4751 +            (Function<? super T, ? extends CompletionStage<U>> fn) {
4752 +            return cf.thenComposeAsync(fn); }
4753 +        public <U> CompletionStage<U> thenComposeAsync
4754 +            (Function<? super T, ? extends CompletionStage<U>> fn,
4755 +             Executor executor) {
4756 +            return cf.thenComposeAsync(fn, executor); }
4757 +        public <U> CompletionStage<U> handle
4758 +            (BiFunction<? super T, Throwable, ? extends U> fn) {
4759 +            return cf.handle(fn); }
4760 +        public <U> CompletionStage<U> handleAsync
4761 +            (BiFunction<? super T, Throwable, ? extends U> fn) {
4762 +            return cf.handleAsync(fn); }
4763 +        public <U> CompletionStage<U> handleAsync
4764 +            (BiFunction<? super T, Throwable, ? extends U> fn,
4765 +             Executor executor) {
4766 +            return cf.handleAsync(fn, executor); }
4767 +        public CompletionStage<T> whenComplete
4768 +            (BiConsumer<? super T, ? super Throwable> action) {
4769 +            return cf.whenComplete(action); }
4770 +        public CompletionStage<T> whenCompleteAsync
4771 +            (BiConsumer<? super T, ? super Throwable> action) {
4772 +            return cf.whenCompleteAsync(action); }
4773 +        public CompletionStage<T> whenCompleteAsync
4774 +            (BiConsumer<? super T, ? super Throwable> action,
4775 +             Executor executor) {
4776 +            return cf.whenCompleteAsync(action, executor); }
4777 +        public CompletionStage<T> exceptionally
4778 +            (Function<Throwable, ? extends T> fn) {
4779 +            return cf.exceptionally(fn); }
4780 +    }
4781 +
4782 +    /**
4783 +     * default-implemented exceptionallyAsync action completes with
4784 +     * function value on source exception
4785 +     */
4786 +    public void testDefaulExceptionallyAsync_exceptionalCompletion() {
4787 +        for (boolean createIncomplete : new boolean[] { true, false })
4788 +        for (Integer v1 : new Integer[] { 1, null })
4789 +    {
4790 +        final AtomicInteger a = new AtomicInteger(0);
4791 +        final CFException ex = new CFException();
4792 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4793 +        final DelegatedCompletionStage<Integer> d =
4794 +            new DelegatedCompletionStage<Integer>(f);
4795 +        if (!createIncomplete) f.completeExceptionally(ex);
4796 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4797 +            ((Throwable t) -> {
4798 +                threadAssertSame(t, ex);
4799 +                a.getAndIncrement();
4800 +                return v1;
4801 +            });
4802 +        if (createIncomplete) f.completeExceptionally(ex);
4803 +
4804 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4805 +        assertEquals(1, a.get());
4806 +    }}
4807 +
4808 +    /**
4809 +     * Under default implementation, if an "exceptionally action"
4810 +     * throws an exception, it completes exceptionally with that
4811 +     * exception
4812 +     */
4813 +    public void testDefaulExceptionallyAsync_exceptionalCompletionActionFailed() {
4814 +        for (boolean createIncomplete : new boolean[] { true, false })
4815 +    {
4816 +        final AtomicInteger a = new AtomicInteger(0);
4817 +        final CFException ex1 = new CFException();
4818 +        final CFException ex2 = new CFException();
4819 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4820 +        final DelegatedCompletionStage<Integer> d =
4821 +            new DelegatedCompletionStage<Integer>(f);
4822 +        if (!createIncomplete) f.completeExceptionally(ex1);
4823 +        final CompletionStage<Integer> g = d.exceptionallyAsync
4824 +            ((Throwable t) -> {
4825 +                threadAssertSame(t, ex1);
4826 +                a.getAndIncrement();
4827 +                throw ex2;
4828 +            });
4829 +        if (createIncomplete) f.completeExceptionally(ex1);
4830 +
4831 +        checkCompletedWithWrappedException(g.toCompletableFuture(), ex2);
4832 +        checkCompletedExceptionally(f, ex1);
4833 +        checkCompletedExceptionally(d.toCompletableFuture(), ex1);
4834 +        assertEquals(1, a.get());
4835 +    }}
4836 +
4837 +    /**
4838 +     * default exceptionallyCompose result completes normally after normal
4839 +     * completion of source
4840 +     */
4841 +    public void testDefaultExceptionallyCompose_normalCompletion() {
4842 +        for (boolean createIncomplete : new boolean[] { true, false })
4843 +        for (Integer v1 : new Integer[] { 1, null })
4844 +    {
4845 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4846 +        final ExceptionalCompletableFutureFunction r =
4847 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4848 +        final DelegatedCompletionStage<Integer> d =
4849 +            new DelegatedCompletionStage<Integer>(f);
4850 +        if (!createIncomplete) assertTrue(f.complete(v1));
4851 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4852 +        if (createIncomplete) assertTrue(f.complete(v1));
4853 +
4854 +        checkCompletedNormally(f, v1);
4855 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4856 +        r.assertNotInvoked();
4857 +    }}
4858 +
4859 +    /**
4860 +     * default-implemented exceptionallyCompose result completes
4861 +     * normally after exceptional completion of source
4862 +     */
4863 +    public void testDefaultExceptionallyCompose_exceptionalCompletion() {
4864 +        for (boolean createIncomplete : new boolean[] { true, false })
4865 +    {
4866 +        final CFException ex = new CFException();
4867 +        final ExceptionalCompletableFutureFunction r =
4868 +            new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4869 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4870 +        final DelegatedCompletionStage<Integer> d =
4871 +            new DelegatedCompletionStage<Integer>(f);
4872 +        if (!createIncomplete) f.completeExceptionally(ex);
4873 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4874 +        if (createIncomplete) f.completeExceptionally(ex);
4875 +
4876 +        checkCompletedExceptionally(f, ex);
4877 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4878 +        r.assertInvoked();
4879 +    }}
4880 +
4881 +    /**
4882 +     * default-implemented exceptionallyCompose completes
4883 +     * exceptionally on exception if action does
4884 +     */
4885 +    public void testDefaultExceptionallyCompose_actionFailed() {
4886 +        for (boolean createIncomplete : new boolean[] { true, false })
4887 +        for (Integer v1 : new Integer[] { 1, null })
4888 +    {
4889 +        final CFException ex = new CFException();
4890 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4891 +        final FailingExceptionalCompletableFutureFunction r
4892 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC);
4893 +        final DelegatedCompletionStage<Integer> d =
4894 +            new DelegatedCompletionStage<Integer>(f);
4895 +        if (!createIncomplete) f.completeExceptionally(ex);
4896 +        final CompletionStage<Integer> g = d.exceptionallyCompose(r);
4897 +        if (createIncomplete) f.completeExceptionally(ex);
4898 +
4899 +        checkCompletedExceptionally(f, ex);
4900 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4901 +        r.assertInvoked();
4902 +    }}
4903 +
4904 +    /**
4905 +     * default exceptionallyComposeAsync result completes normally after normal
4906 +     * completion of source
4907 +     */
4908 +    public void testDefaultExceptionallyComposeAsync_normalCompletion() {
4909 +        for (boolean createIncomplete : new boolean[] { true, false })
4910 +        for (Integer v1 : new Integer[] { 1, null })
4911 +    {
4912 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4913 +        final ExceptionalCompletableFutureFunction r =
4914 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4915 +        final DelegatedCompletionStage<Integer> d =
4916 +            new DelegatedCompletionStage<Integer>(f);
4917 +        if (!createIncomplete) assertTrue(f.complete(v1));
4918 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4919 +        if (createIncomplete) assertTrue(f.complete(v1));
4920 +
4921 +        checkCompletedNormally(f, v1);
4922 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4923 +        r.assertNotInvoked();
4924 +    }}
4925 +
4926 +    /**
4927 +     * default-implemented exceptionallyComposeAsync result completes
4928 +     * normally after exceptional completion of source
4929 +     */
4930 +    public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() {
4931 +        for (boolean createIncomplete : new boolean[] { true, false })
4932 +    {
4933 +        final CFException ex = new CFException();
4934 +        final ExceptionalCompletableFutureFunction r =
4935 +            new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4936 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4937 +        final DelegatedCompletionStage<Integer> d =
4938 +            new DelegatedCompletionStage<Integer>(f);
4939 +        if (!createIncomplete) f.completeExceptionally(ex);
4940 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4941 +        if (createIncomplete) f.completeExceptionally(ex);
4942 +
4943 +        checkCompletedExceptionally(f, ex);
4944 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
4945 +        r.assertInvoked();
4946 +    }}
4947 +
4948 +    /**
4949 +     * default-implemented exceptionallyComposeAsync completes
4950 +     * exceptionally on exception if action does
4951 +     */
4952 +    public void testDefaultExceptionallyComposeAsync_actionFailed() {
4953 +        for (boolean createIncomplete : new boolean[] { true, false })
4954 +        for (Integer v1 : new Integer[] { 1, null })
4955 +    {
4956 +        final CFException ex = new CFException();
4957 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4958 +        final FailingExceptionalCompletableFutureFunction r
4959 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC);
4960 +        final DelegatedCompletionStage<Integer> d =
4961 +            new DelegatedCompletionStage<Integer>(f);
4962 +        if (!createIncomplete) f.completeExceptionally(ex);
4963 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r);
4964 +        if (createIncomplete) f.completeExceptionally(ex);
4965 +
4966 +        checkCompletedExceptionally(f, ex);
4967 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
4968 +        r.assertInvoked();
4969 +    }}
4970 +
4971 +
4972 +    /**
4973 +     * default exceptionallyComposeAsync result completes normally after normal
4974 +     * completion of source
4975 +     */
4976 +    public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() {
4977 +        for (boolean createIncomplete : new boolean[] { true, false })
4978 +        for (Integer v1 : new Integer[] { 1, null })
4979 +    {
4980 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
4981 +        final ExceptionalCompletableFutureFunction r =
4982 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
4983 +        final DelegatedCompletionStage<Integer> d =
4984 +            new DelegatedCompletionStage<Integer>(f);
4985 +        if (!createIncomplete) assertTrue(f.complete(v1));
4986 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
4987 +        if (createIncomplete) assertTrue(f.complete(v1));
4988 +
4989 +        checkCompletedNormally(f, v1);
4990 +        checkCompletedNormally(g.toCompletableFuture(), v1);
4991 +        r.assertNotInvoked();
4992 +    }}
4993 +
4994 +    /**
4995 +     * default-implemented exceptionallyComposeAsync result completes
4996 +     * normally after exceptional completion of source
4997 +     */
4998 +    public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() {
4999 +        for (boolean createIncomplete : new boolean[] { true, false })
5000 +    {
5001 +        final CFException ex = new CFException();
5002 +        final ExceptionalCompletableFutureFunction r =
5003 +            new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5004 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5005 +        final DelegatedCompletionStage<Integer> d =
5006 +            new DelegatedCompletionStage<Integer>(f);
5007 +        if (!createIncomplete) f.completeExceptionally(ex);
5008 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5009 +        if (createIncomplete) f.completeExceptionally(ex);
5010 +
5011 +        checkCompletedExceptionally(f, ex);
5012 +        checkCompletedNormally(g.toCompletableFuture(), r.value);
5013 +        r.assertInvoked();
5014 +    }}
5015 +
5016 +    /**
5017 +     * default-implemented exceptionallyComposeAsync completes
5018 +     * exceptionally on exception if action does
5019 +     */
5020 +    public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() {
5021 +        for (boolean createIncomplete : new boolean[] { true, false })
5022 +        for (Integer v1 : new Integer[] { 1, null })
5023 +    {
5024 +        final CFException ex = new CFException();
5025 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
5026 +        final FailingExceptionalCompletableFutureFunction r
5027 +            = new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR);
5028 +        final DelegatedCompletionStage<Integer> d =
5029 +            new DelegatedCompletionStage<Integer>(f);
5030 +        if (!createIncomplete) f.completeExceptionally(ex);
5031 +        final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r,  new ThreadExecutor());
5032 +        if (createIncomplete) f.completeExceptionally(ex);
5033 +
5034 +        checkCompletedExceptionally(f, ex);
5035 +        checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex);
5036 +        r.assertInvoked();
5037 +    }}
5038 +
5039   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines