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.80 by jsr166, Mon Jun 16 17:29:03 2014 UTC vs.
Revision 1.195 by jsr166, Sun Jul 22 20:09:31 2018 UTC

# Line 5 | Line 5
5   * http://creativecommons.org/publicdomain/zero/1.0/
6   */
7  
8 < import junit.framework.*;
8 > import static java.util.concurrent.TimeUnit.MILLISECONDS;
9 > import static java.util.concurrent.TimeUnit.SECONDS;
10 > import static java.util.concurrent.CompletableFuture.completedFuture;
11 > import static java.util.concurrent.CompletableFuture.failedFuture;
12 >
13 > import java.lang.reflect.Method;
14 > import java.lang.reflect.Modifier;
15 >
16 > import java.util.stream.Collectors;
17 > import java.util.stream.Stream;
18 >
19 > import java.util.ArrayList;
20 > import java.util.Arrays;
21 > import java.util.List;
22 > import java.util.Objects;
23 > import java.util.Set;
24   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
25   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
26   import java.util.concurrent.CompletableFuture;
27   import java.util.concurrent.CompletionException;
28   import java.util.concurrent.CompletionStage;
29 + import java.util.concurrent.ExecutionException;
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;
35   import java.util.concurrent.atomic.AtomicInteger;
36 < import static java.util.concurrent.TimeUnit.MILLISECONDS;
25 < import static java.util.concurrent.TimeUnit.SECONDS;
26 < import java.util.*;
27 < import java.util.function.Supplier;
28 < import java.util.function.Consumer;
36 > import java.util.concurrent.atomic.AtomicReference;
37   import java.util.function.BiConsumer;
30 import java.util.function.Function;
38   import java.util.function.BiFunction;
39 + import java.util.function.Consumer;
40 + import java.util.function.Function;
41 + import java.util.function.Predicate;
42 + import java.util.function.Supplier;
43 +
44 + import junit.framework.Test;
45 + import junit.framework.TestSuite;
46  
47   public class CompletableFutureTest extends JSR166TestCase {
48  
49      public static void main(String[] args) {
50 <        junit.textui.TestRunner.run(suite());
50 >        main(suite(), args);
51      }
52      public static Test suite() {
53          return new TestSuite(CompletableFutureTest.class);
# Line 44 | 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) {}
# Line 57 | Line 75 | public class CompletableFutureTest exten
75      }
76  
77      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
78 <        try {
79 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
78 >        checkTimedGet(f, value);
79 >
80          try {
81              assertEquals(value, f.join());
65        } catch (Throwable fail) { threadUnexpectedException(fail); }
66        try {
82              assertEquals(value, f.getNow(null));
68        } catch (Throwable fail) { threadUnexpectedException(fail); }
69        try {
83              assertEquals(value, f.get());
84          } catch (Throwable fail) { threadUnexpectedException(fail); }
85          assertTrue(f.isDone());
86          assertFalse(f.isCancelled());
87          assertFalse(f.isCompletedExceptionally());
88 <        assertTrue(f.toString().contains("[Completed normally]"));
88 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
89      }
90  
91 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
92 <        try {
93 <            f.get(LONG_DELAY_MS, MILLISECONDS);
94 <            shouldThrow();
95 <        } catch (ExecutionException success) {
96 <            assertTrue(success.getCause() instanceof CFException);
97 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
98 <        try {
86 <            f.join();
87 <            shouldThrow();
88 <        } catch (CompletionException success) {
89 <            assertTrue(success.getCause() instanceof CFException);
90 <        }
91 <        try {
92 <            f.getNow(null);
93 <            shouldThrow();
94 <        } catch (CompletionException success) {
95 <            assertTrue(success.getCause() instanceof CFException);
96 <        }
97 <        try {
98 <            f.get();
99 <            shouldThrow();
100 <        } catch (ExecutionException success) {
101 <            assertTrue(success.getCause() instanceof CFException);
102 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
103 <        assertTrue(f.isDone());
104 <        assertFalse(f.isCancelled());
105 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
91 >    /**
92 >     * Returns the "raw" internal exceptional completion of f,
93 >     * without any additional wrapping with CompletionException.
94 >     */
95 >    Throwable exceptionalCompletion(CompletableFuture<?> f) {
96 >        // handle (and whenComplete and exceptionally) can distinguish
97 >        // between "direct" and "wrapped" exceptional completion
98 >        return f.handle((u, t) -> t).join();
99      }
100  
101 <    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
102 <                                                      Throwable ex) {
101 >    void checkCompletedExceptionally(CompletableFuture<?> f,
102 >                                     boolean wrapped,
103 >                                     Consumer<Throwable> checker) {
104 >        Throwable cause = exceptionalCompletion(f);
105 >        if (wrapped) {
106 >            assertTrue(cause instanceof CompletionException);
107 >            cause = cause.getCause();
108 >        }
109 >        checker.accept(cause);
110 >
111 >        long startTime = System.nanoTime();
112          try {
113              f.get(LONG_DELAY_MS, MILLISECONDS);
114              shouldThrow();
115          } catch (ExecutionException success) {
116 <            assertSame(ex, success.getCause());
116 >            assertSame(cause, success.getCause());
117          } catch (Throwable fail) { threadUnexpectedException(fail); }
118 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
119 +
120          try {
121              f.join();
122              shouldThrow();
123          } catch (CompletionException success) {
124 <            assertSame(ex, success.getCause());
125 <        }
124 >            assertSame(cause, success.getCause());
125 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
126 >
127          try {
128              f.getNow(null);
129              shouldThrow();
130          } catch (CompletionException success) {
131 <            assertSame(ex, success.getCause());
132 <        }
131 >            assertSame(cause, success.getCause());
132 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
133 >
134          try {
135              f.get();
136              shouldThrow();
137          } catch (ExecutionException success) {
138 <            assertSame(ex, success.getCause());
138 >            assertSame(cause, success.getCause());
139          } catch (Throwable fail) { threadUnexpectedException(fail); }
140  
135        assertTrue(f.isDone());
141          assertFalse(f.isCancelled());
142 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
142 >        assertTrue(f.isDone());
143 >        assertTrue(f.isCompletedExceptionally());
144 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
145      }
146  
147 <    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
148 <                                                Throwable ex) {
149 <        checkCompletedExceptionallyWithRootCause(f, ex);
143 <        try {
144 <            CompletableFuture<Throwable> spy = f.handle
145 <                ((U u, Throwable t) -> t);
146 <            assertTrue(spy.join() instanceof CompletionException);
147 <            assertSame(ex, spy.join().getCause());
148 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
147 >    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
148 >        checkCompletedExceptionally(f, true,
149 >            t -> assertTrue(t instanceof CFException));
150      }
151  
152 <    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
153 <        checkCompletedExceptionallyWithRootCause(f, ex);
154 <        try {
155 <            CompletableFuture<Throwable> spy = f.handle
156 <                ((U u, Throwable t) -> t);
157 <            assertSame(ex, spy.join());
158 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
152 >    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
153 >        checkCompletedExceptionally(f, true,
154 >            t -> assertTrue(t instanceof CancellationException));
155 >    }
156 >
157 >    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
158 >        checkCompletedExceptionally(f, false,
159 >            t -> assertTrue(t instanceof TimeoutException));
160 >    }
161 >
162 >    void checkCompletedWithWrappedException(CompletableFuture<?> f,
163 >                                            Throwable ex) {
164 >        checkCompletedExceptionally(f, true, t -> assertSame(t, ex));
165 >    }
166 >
167 >    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
168 >        checkCompletedExceptionally(f, false, t -> assertSame(t, ex));
169      }
170  
171      void checkCancelled(CompletableFuture<?> f) {
172 +        long startTime = System.nanoTime();
173          try {
174              f.get(LONG_DELAY_MS, MILLISECONDS);
175              shouldThrow();
176          } catch (CancellationException success) {
177          } catch (Throwable fail) { threadUnexpectedException(fail); }
178 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
179 +
180          try {
181              f.join();
182              shouldThrow();
# Line 176 | Line 190 | public class CompletableFutureTest exten
190              shouldThrow();
191          } catch (CancellationException success) {
192          } catch (Throwable fail) { threadUnexpectedException(fail); }
179        assertTrue(f.isDone());
180        assertTrue(f.isCompletedExceptionally());
181        assertTrue(f.isCancelled());
182        assertTrue(f.toString().contains("[Completed exceptionally]"));
183    }
193  
194 <    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
195 <        try {
187 <            f.get(LONG_DELAY_MS, MILLISECONDS);
188 <            shouldThrow();
189 <        } catch (ExecutionException success) {
190 <            assertTrue(success.getCause() instanceof CancellationException);
191 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
192 <        try {
193 <            f.join();
194 <            shouldThrow();
195 <        } catch (CompletionException success) {
196 <            assertTrue(success.getCause() instanceof CancellationException);
197 <        }
198 <        try {
199 <            f.getNow(null);
200 <            shouldThrow();
201 <        } catch (CompletionException success) {
202 <            assertTrue(success.getCause() instanceof CancellationException);
203 <        }
204 <        try {
205 <            f.get();
206 <            shouldThrow();
207 <        } catch (ExecutionException success) {
208 <            assertTrue(success.getCause() instanceof CancellationException);
209 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
194 >        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
195 >
196          assertTrue(f.isDone());
211        assertFalse(f.isCancelled());
197          assertTrue(f.isCompletedExceptionally());
198 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
198 >        assertTrue(f.isCancelled());
199 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
200      }
201  
202      /**
# Line 257 | Line 243 | public class CompletableFutureTest exten
243      {
244          CompletableFuture<Integer> f = new CompletableFuture<>();
245          checkIncomplete(f);
246 <        assertTrue(f.cancel(true));
247 <        assertTrue(f.cancel(true));
246 >        assertTrue(f.cancel(mayInterruptIfRunning));
247 >        assertTrue(f.cancel(mayInterruptIfRunning));
248 >        assertTrue(f.cancel(!mayInterruptIfRunning));
249          checkCancelled(f);
250      }}
251  
# Line 308 | Line 295 | public class CompletableFutureTest exten
295          }
296  
297          f = new CompletableFuture<>();
298 <        f.completeExceptionally(ex = new CFException());
298 >        f.completeExceptionally(new CFException());
299          f.obtrudeValue(v1);
300          checkCompletedNormally(f, v1);
301          f.obtrudeException(ex = new CFException());
# Line 345 | Line 332 | public class CompletableFutureTest exten
332      /**
333       * toString indicates current completion state
334       */
335 <    public void testToString() {
336 <        CompletableFuture<String> f;
337 <
338 <        f = new CompletableFuture<String>();
339 <        assertTrue(f.toString().contains("[Not completed]"));
335 >    public void testToString_incomplete() {
336 >        CompletableFuture<String> f = new CompletableFuture<>();
337 >        assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]"));
338 >        if (testImplementationDetails)
339 >            assertEquals(identityString(f) + "[Not completed]",
340 >                         f.toString());
341 >    }
342  
343 +    public void testToString_normal() {
344 +        CompletableFuture<String> f = new CompletableFuture<>();
345          assertTrue(f.complete("foo"));
346 <        assertTrue(f.toString().contains("[Completed normally]"));
346 >        assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]"));
347 >        if (testImplementationDetails)
348 >            assertEquals(identityString(f) + "[Completed normally]",
349 >                         f.toString());
350 >    }
351  
352 <        f = new CompletableFuture<String>();
352 >    public void testToString_exception() {
353 >        CompletableFuture<String> f = new CompletableFuture<>();
354          assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
355 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
355 >        assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
356 >        if (testImplementationDetails)
357 >            assertTrue(f.toString().startsWith(
358 >                               identityString(f) + "[Completed exceptionally: "));
359 >    }
360  
361 +    public void testToString_cancelled() {
362          for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
363 <            f = new CompletableFuture<String>();
363 >            CompletableFuture<String> f = new CompletableFuture<>();
364              assertTrue(f.cancel(mayInterruptIfRunning));
365 <            assertTrue(f.toString().contains("[Completed exceptionally]"));
365 >            assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]"));
366 >            if (testImplementationDetails)
367 >                assertTrue(f.toString().startsWith(
368 >                                   identityString(f) + "[Completed exceptionally: "));
369          }
370      }
371  
# Line 373 | Line 377 | public class CompletableFutureTest exten
377          checkCompletedNormally(f, "test");
378      }
379  
380 <    abstract class CheckedAction {
380 >    abstract static class CheckedAction {
381          int invocationCount = 0;
382          final ExecutionMode m;
383          CheckedAction(ExecutionMode m) { this.m = m; }
# Line 385 | Line 389 | public class CompletableFutureTest exten
389          void assertInvoked() { assertEquals(1, invocationCount); }
390      }
391  
392 <    abstract class CheckedIntegerAction extends CheckedAction {
392 >    abstract static class CheckedIntegerAction extends CheckedAction {
393          Integer value;
394          CheckedIntegerAction(ExecutionMode m) { super(m); }
395          void assertValue(Integer expected) {
# Line 394 | Line 398 | public class CompletableFutureTest exten
398          }
399      }
400  
401 <    class IntegerSupplier extends CheckedAction
401 >    static class IntegerSupplier extends CheckedAction
402          implements Supplier<Integer>
403      {
404          final Integer value;
# Line 413 | Line 417 | public class CompletableFutureTest exten
417          return (x == null) ? null : x + 1;
418      }
419  
420 <    class NoopConsumer extends CheckedIntegerAction
420 >    static class NoopConsumer extends CheckedIntegerAction
421          implements Consumer<Integer>
422      {
423          NoopConsumer(ExecutionMode m) { super(m); }
# Line 423 | Line 427 | public class CompletableFutureTest exten
427          }
428      }
429  
430 <    class IncFunction extends CheckedIntegerAction
430 >    static class IncFunction extends CheckedIntegerAction
431          implements Function<Integer,Integer>
432      {
433          IncFunction(ExecutionMode m) { super(m); }
# Line 441 | Line 445 | public class CompletableFutureTest exten
445              - ((y == null) ? 99 : y.intValue());
446      }
447  
448 <    class SubtractAction extends CheckedIntegerAction
448 >    static class SubtractAction extends CheckedIntegerAction
449          implements BiConsumer<Integer, Integer>
450      {
451          SubtractAction(ExecutionMode m) { super(m); }
# Line 451 | Line 455 | public class CompletableFutureTest exten
455          }
456      }
457  
458 <    class SubtractFunction extends CheckedIntegerAction
458 >    static class SubtractFunction extends CheckedIntegerAction
459          implements BiFunction<Integer, Integer, Integer>
460      {
461          SubtractFunction(ExecutionMode m) { super(m); }
# Line 461 | Line 465 | public class CompletableFutureTest exten
465          }
466      }
467  
468 <    class Noop extends CheckedAction implements Runnable {
468 >    static class Noop extends CheckedAction implements Runnable {
469          Noop(ExecutionMode m) { super(m); }
470          public void run() {
471              invoked();
472          }
473      }
474  
475 <    class FailingSupplier extends CheckedAction
475 >    static class FailingSupplier extends CheckedAction
476          implements Supplier<Integer>
477      {
478 <        FailingSupplier(ExecutionMode m) { super(m); }
478 >        final CFException ex;
479 >        FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); }
480          public Integer get() {
481              invoked();
482 <            throw new CFException();
482 >            throw ex;
483          }
484      }
485  
486 <    class FailingConsumer extends CheckedIntegerAction
486 >    static class FailingConsumer extends CheckedIntegerAction
487          implements Consumer<Integer>
488      {
489 <        FailingConsumer(ExecutionMode m) { super(m); }
489 >        final CFException ex;
490 >        FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
491          public void accept(Integer x) {
492              invoked();
493              value = x;
494 <            throw new CFException();
494 >            throw ex;
495          }
496      }
497  
498 <    class FailingBiConsumer extends CheckedIntegerAction
498 >    static class FailingBiConsumer extends CheckedIntegerAction
499          implements BiConsumer<Integer, Integer>
500      {
501 <        FailingBiConsumer(ExecutionMode m) { super(m); }
501 >        final CFException ex;
502 >        FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); }
503          public void accept(Integer x, Integer y) {
504              invoked();
505              value = subtract(x, y);
506 <            throw new CFException();
506 >            throw ex;
507          }
508      }
509  
510 <    class FailingFunction extends CheckedIntegerAction
510 >    static class FailingFunction extends CheckedIntegerAction
511          implements Function<Integer, Integer>
512      {
513 <        FailingFunction(ExecutionMode m) { super(m); }
513 >        final CFException ex;
514 >        FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); }
515          public Integer apply(Integer x) {
516              invoked();
517              value = x;
518 <            throw new CFException();
518 >            throw ex;
519          }
520      }
521  
522 <    class FailingBiFunction extends CheckedIntegerAction
522 >    static class FailingBiFunction extends CheckedIntegerAction
523          implements BiFunction<Integer, Integer, Integer>
524      {
525 <        FailingBiFunction(ExecutionMode m) { super(m); }
525 >        final CFException ex;
526 >        FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); }
527          public Integer apply(Integer x, Integer y) {
528              invoked();
529              value = subtract(x, y);
530 <            throw new CFException();
530 >            throw ex;
531          }
532      }
533  
534 <    class FailingRunnable extends CheckedAction implements Runnable {
535 <        FailingRunnable(ExecutionMode m) { super(m); }
534 >    static class FailingRunnable extends CheckedAction implements Runnable {
535 >        final CFException ex;
536 >        FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); }
537          public void run() {
538              invoked();
539 <            throw new CFException();
539 >            throw ex;
540          }
541      }
542  
543 <
534 <    class CompletableFutureInc extends CheckedIntegerAction
543 >    static class CompletableFutureInc extends CheckedIntegerAction
544          implements Function<Integer, CompletableFuture<Integer>>
545      {
546          CompletableFutureInc(ExecutionMode m) { super(m); }
# Line 544 | Line 553 | public class CompletableFutureTest exten
553          }
554      }
555  
556 <    class FailingCompletableFutureFunction extends CheckedIntegerAction
556 >    static class FailingCompletableFutureFunction extends CheckedIntegerAction
557          implements Function<Integer, CompletableFuture<Integer>>
558      {
559 <        FailingCompletableFutureFunction(ExecutionMode m) { super(m); }
559 >        final CFException ex;
560 >        FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); }
561          public CompletableFuture<Integer> apply(Integer x) {
562              invoked();
563              value = x;
564 <            throw new CFException();
564 >            throw ex;
565 >        }
566 >    }
567 >
568 >    static class CountingRejectingExecutor implements Executor {
569 >        final RejectedExecutionException ex = new RejectedExecutionException();
570 >        final AtomicInteger count = new AtomicInteger(0);
571 >        public void execute(Runnable r) {
572 >            count.getAndIncrement();
573 >            throw ex;
574          }
575      }
576  
# Line 569 | Line 588 | public class CompletableFutureTest exten
588          }
589      }
590  
591 +    static final boolean defaultExecutorIsCommonPool
592 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
593 +
594      /**
595       * Permits the testing of parallel code for the 3 different
596       * execution modes without copy/pasting all the test methods.
597       */
598      enum ExecutionMode {
599 <        DEFAULT {
599 >        SYNC {
600              public void checkExecutionMode() {
601                  assertFalse(ThreadExecutor.startedCurrentThread());
602                  assertNull(ForkJoinTask.getPool());
# Line 650 | Line 672 | public class CompletableFutureTest exten
672  
673          ASYNC {
674              public void checkExecutionMode() {
675 <                assertSame(ForkJoinPool.commonPool(),
676 <                           ForkJoinTask.getPool());
675 >                assertEquals(defaultExecutorIsCommonPool,
676 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
677              }
678              public CompletableFuture<Void> runAsync(Runnable a) {
679                  return CompletableFuture.runAsync(a);
# Line 850 | Line 872 | public class CompletableFutureTest exten
872          if (!createIncomplete) assertTrue(f.complete(v1));
873          final CompletableFuture<Integer> g = f.exceptionally
874              ((Throwable t) -> {
853                // Should not be called
875                  a.getAndIncrement();
876 <                throw new AssertionError();
876 >                threadFail("should not be called");
877 >                return null;            // unreached
878              });
879          if (createIncomplete) assertTrue(f.complete(v1));
880  
# Line 875 | Line 897 | public class CompletableFutureTest exten
897          if (!createIncomplete) f.completeExceptionally(ex);
898          final CompletableFuture<Integer> g = f.exceptionally
899              ((Throwable t) -> {
900 <                ExecutionMode.DEFAULT.checkExecutionMode();
900 >                ExecutionMode.SYNC.checkExecutionMode();
901                  threadAssertSame(t, ex);
902                  a.getAndIncrement();
903                  return v1;
# Line 886 | Line 908 | public class CompletableFutureTest exten
908          assertEquals(1, a.get());
909      }}
910  
911 +    /**
912 +     * If an "exceptionally action" throws an exception, it completes
913 +     * exceptionally with that exception
914 +     */
915      public void testExceptionally_exceptionalCompletionActionFailed() {
916          for (boolean createIncomplete : new boolean[] { true, false })
891        for (Integer v1 : new Integer[] { 1, null })
917      {
918          final AtomicInteger a = new AtomicInteger(0);
919          final CFException ex1 = new CFException();
# Line 897 | Line 922 | public class CompletableFutureTest exten
922          if (!createIncomplete) f.completeExceptionally(ex1);
923          final CompletableFuture<Integer> g = f.exceptionally
924              ((Throwable t) -> {
925 <                ExecutionMode.DEFAULT.checkExecutionMode();
925 >                ExecutionMode.SYNC.checkExecutionMode();
926                  threadAssertSame(t, ex1);
927                  a.getAndIncrement();
928                  throw ex2;
# Line 905 | Line 930 | public class CompletableFutureTest exten
930          if (createIncomplete) f.completeExceptionally(ex1);
931  
932          checkCompletedWithWrappedException(g, ex2);
933 +        checkCompletedExceptionally(f, ex1);
934          assertEquals(1, a.get());
935      }}
936  
# Line 912 | Line 938 | public class CompletableFutureTest exten
938       * whenComplete action executes on normal completion, propagating
939       * source result.
940       */
941 <    public void testWhenComplete_normalCompletion1() {
941 >    public void testWhenComplete_normalCompletion() {
942          for (ExecutionMode m : ExecutionMode.values())
943          for (boolean createIncomplete : new boolean[] { true, false })
944          for (Integer v1 : new Integer[] { 1, null })
# Line 922 | Line 948 | public class CompletableFutureTest exten
948          if (!createIncomplete) assertTrue(f.complete(v1));
949          final CompletableFuture<Integer> g = m.whenComplete
950              (f,
951 <             (Integer x, Throwable t) -> {
951 >             (Integer result, Throwable t) -> {
952                  m.checkExecutionMode();
953 <                threadAssertSame(x, v1);
953 >                threadAssertSame(result, v1);
954                  threadAssertNull(t);
955                  a.getAndIncrement();
956              });
# Line 942 | Line 968 | public class CompletableFutureTest exten
968      public void testWhenComplete_exceptionalCompletion() {
969          for (ExecutionMode m : ExecutionMode.values())
970          for (boolean createIncomplete : new boolean[] { true, false })
945        for (Integer v1 : new Integer[] { 1, null })
971      {
972          final AtomicInteger a = new AtomicInteger(0);
973          final CFException ex = new CFException();
# Line 950 | Line 975 | public class CompletableFutureTest exten
975          if (!createIncomplete) f.completeExceptionally(ex);
976          final CompletableFuture<Integer> g = m.whenComplete
977              (f,
978 <             (Integer x, Throwable t) -> {
978 >             (Integer result, Throwable t) -> {
979                  m.checkExecutionMode();
980 <                threadAssertNull(x);
980 >                threadAssertNull(result);
981                  threadAssertSame(t, ex);
982                  a.getAndIncrement();
983              });
# Line 977 | Line 1002 | public class CompletableFutureTest exten
1002          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1003          final CompletableFuture<Integer> g = m.whenComplete
1004              (f,
1005 <             (Integer x, Throwable t) -> {
1005 >             (Integer result, Throwable t) -> {
1006                  m.checkExecutionMode();
1007 <                threadAssertNull(x);
1007 >                threadAssertNull(result);
1008                  threadAssertTrue(t instanceof CancellationException);
1009                  a.getAndIncrement();
1010              });
# Line 994 | Line 1019 | public class CompletableFutureTest exten
1019       * If a whenComplete action throws an exception when triggered by
1020       * a normal completion, it completes exceptionally
1021       */
1022 <    public void testWhenComplete_actionFailed() {
1022 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
1023          for (boolean createIncomplete : new boolean[] { true, false })
1024          for (ExecutionMode m : ExecutionMode.values())
1025          for (Integer v1 : new Integer[] { 1, null })
# Line 1005 | Line 1030 | public class CompletableFutureTest exten
1030          if (!createIncomplete) assertTrue(f.complete(v1));
1031          final CompletableFuture<Integer> g = m.whenComplete
1032              (f,
1033 <             (Integer x, Throwable t) -> {
1033 >             (Integer result, Throwable t) -> {
1034                  m.checkExecutionMode();
1035 <                threadAssertSame(x, v1);
1035 >                threadAssertSame(result, v1);
1036                  threadAssertNull(t);
1037                  a.getAndIncrement();
1038                  throw ex;
# Line 1022 | Line 1047 | public class CompletableFutureTest exten
1047      /**
1048       * If a whenComplete action throws an exception when triggered by
1049       * a source completion that also throws an exception, the source
1050 <     * exception takes precedence.
1050 >     * exception takes precedence (unlike handle)
1051       */
1052 <    public void testWhenComplete_actionFailedSourceFailed() {
1052 >    public void testWhenComplete_sourceFailedActionFailed() {
1053          for (boolean createIncomplete : new boolean[] { true, false })
1054          for (ExecutionMode m : ExecutionMode.values())
1030        for (Integer v1 : new Integer[] { 1, null })
1055      {
1056          final AtomicInteger a = new AtomicInteger(0);
1057          final CFException ex1 = new CFException();
# Line 1037 | Line 1061 | public class CompletableFutureTest exten
1061          if (!createIncomplete) f.completeExceptionally(ex1);
1062          final CompletableFuture<Integer> g = m.whenComplete
1063              (f,
1064 <             (Integer x, Throwable t) -> {
1064 >             (Integer result, Throwable t) -> {
1065                  m.checkExecutionMode();
1066                  threadAssertSame(t, ex1);
1067 <                threadAssertNull(x);
1067 >                threadAssertNull(result);
1068                  a.getAndIncrement();
1069                  throw ex2;
1070              });
# Line 1048 | Line 1072 | public class CompletableFutureTest exten
1072  
1073          checkCompletedWithWrappedException(g, ex1);
1074          checkCompletedExceptionally(f, ex1);
1075 +        if (testImplementationDetails) {
1076 +            assertEquals(1, ex1.getSuppressed().length);
1077 +            assertSame(ex2, ex1.getSuppressed()[0]);
1078 +        }
1079          assertEquals(1, a.get());
1080      }}
1081  
# Line 1065 | Line 1093 | public class CompletableFutureTest exten
1093          if (!createIncomplete) assertTrue(f.complete(v1));
1094          final CompletableFuture<Integer> g = m.handle
1095              (f,
1096 <             (Integer x, Throwable t) -> {
1096 >             (Integer result, Throwable t) -> {
1097                  m.checkExecutionMode();
1098 <                threadAssertSame(x, v1);
1098 >                threadAssertSame(result, v1);
1099                  threadAssertNull(t);
1100                  a.getAndIncrement();
1101                  return inc(v1);
# Line 1094 | Line 1122 | public class CompletableFutureTest exten
1122          if (!createIncomplete) f.completeExceptionally(ex);
1123          final CompletableFuture<Integer> g = m.handle
1124              (f,
1125 <             (Integer x, Throwable t) -> {
1125 >             (Integer result, Throwable t) -> {
1126                  m.checkExecutionMode();
1127 <                threadAssertNull(x);
1127 >                threadAssertNull(result);
1128                  threadAssertSame(t, ex);
1129                  a.getAndIncrement();
1130                  return v1;
# Line 1123 | Line 1151 | public class CompletableFutureTest exten
1151          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1152          final CompletableFuture<Integer> g = m.handle
1153              (f,
1154 <             (Integer x, Throwable t) -> {
1154 >             (Integer result, Throwable t) -> {
1155                  m.checkExecutionMode();
1156 <                threadAssertNull(x);
1156 >                threadAssertNull(result);
1157                  threadAssertTrue(t instanceof CancellationException);
1158                  a.getAndIncrement();
1159                  return v1;
# Line 1138 | Line 1166 | public class CompletableFutureTest exten
1166      }}
1167  
1168      /**
1169 <     * handle result completes exceptionally if action does
1169 >     * If a "handle action" throws an exception when triggered by
1170 >     * a normal completion, it completes exceptionally
1171       */
1172 <    public void testHandle_sourceFailedActionFailed() {
1172 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1173          for (ExecutionMode m : ExecutionMode.values())
1174          for (boolean createIncomplete : new boolean[] { true, false })
1175 +        for (Integer v1 : new Integer[] { 1, null })
1176      {
1177          final CompletableFuture<Integer> f = new CompletableFuture<>();
1178          final AtomicInteger a = new AtomicInteger(0);
1179 <        final CFException ex1 = new CFException();
1180 <        final CFException ex2 = new CFException();
1151 <        if (!createIncomplete) f.completeExceptionally(ex1);
1179 >        final CFException ex = new CFException();
1180 >        if (!createIncomplete) assertTrue(f.complete(v1));
1181          final CompletableFuture<Integer> g = m.handle
1182              (f,
1183 <             (Integer x, Throwable t) -> {
1183 >             (Integer result, Throwable t) -> {
1184                  m.checkExecutionMode();
1185 <                threadAssertNull(x);
1186 <                threadAssertSame(ex1, t);
1185 >                threadAssertSame(result, v1);
1186 >                threadAssertNull(t);
1187                  a.getAndIncrement();
1188 <                throw ex2;
1188 >                throw ex;
1189              });
1190 <        if (createIncomplete) f.completeExceptionally(ex1);
1190 >        if (createIncomplete) assertTrue(f.complete(v1));
1191  
1192 <        checkCompletedWithWrappedException(g, ex2);
1193 <        checkCompletedExceptionally(f, ex1);
1192 >        checkCompletedWithWrappedException(g, ex);
1193 >        checkCompletedNormally(f, v1);
1194          assertEquals(1, a.get());
1195      }}
1196  
1197 <    public void testHandle_sourceCompletedNormallyActionFailed() {
1198 <        for (ExecutionMode m : ExecutionMode.values())
1197 >    /**
1198 >     * If a "handle action" throws an exception when triggered by
1199 >     * a source completion that also throws an exception, the action
1200 >     * exception takes precedence (unlike whenComplete)
1201 >     */
1202 >    public void testHandle_sourceFailedActionFailed() {
1203          for (boolean createIncomplete : new boolean[] { true, false })
1204 <        for (Integer v1 : new Integer[] { 1, null })
1204 >        for (ExecutionMode m : ExecutionMode.values())
1205      {
1173        final CompletableFuture<Integer> f = new CompletableFuture<>();
1206          final AtomicInteger a = new AtomicInteger(0);
1207 <        final CFException ex = new CFException();
1208 <        if (!createIncomplete) assertTrue(f.complete(v1));
1207 >        final CFException ex1 = new CFException();
1208 >        final CFException ex2 = new CFException();
1209 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1210 >
1211 >        if (!createIncomplete) f.completeExceptionally(ex1);
1212          final CompletableFuture<Integer> g = m.handle
1213              (f,
1214 <             (Integer x, Throwable t) -> {
1214 >             (Integer result, Throwable t) -> {
1215                  m.checkExecutionMode();
1216 <                threadAssertSame(x, v1);
1217 <                threadAssertNull(t);
1216 >                threadAssertNull(result);
1217 >                threadAssertSame(ex1, t);
1218                  a.getAndIncrement();
1219 <                throw ex;
1219 >                throw ex2;
1220              });
1221 <        if (createIncomplete) assertTrue(f.complete(v1));
1221 >        if (createIncomplete) f.completeExceptionally(ex1);
1222  
1223 <        checkCompletedWithWrappedException(g, ex);
1224 <        checkCompletedNormally(f, v1);
1223 >        checkCompletedWithWrappedException(g, ex2);
1224 >        checkCompletedExceptionally(f, ex1);
1225          assertEquals(1, a.get());
1226      }}
1227  
# Line 1219 | Line 1254 | public class CompletableFutureTest exten
1254      {
1255          final FailingRunnable r = new FailingRunnable(m);
1256          final CompletableFuture<Void> f = m.runAsync(r);
1257 <        checkCompletedWithWrappedCFException(f);
1257 >        checkCompletedWithWrappedException(f, r.ex);
1258          r.assertInvoked();
1259      }}
1260  
1261 +    @SuppressWarnings("FutureReturnValueIgnored")
1262 +    public void testRunAsync_rejectingExecutor() {
1263 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1264 +        try {
1265 +            CompletableFuture.runAsync(() -> {}, e);
1266 +            shouldThrow();
1267 +        } catch (Throwable t) {
1268 +            assertSame(e.ex, t);
1269 +        }
1270 +
1271 +        assertEquals(1, e.count.get());
1272 +    }
1273 +
1274      /**
1275       * supplyAsync completes with result of supplier
1276       */
# Line 1253 | Line 1301 | public class CompletableFutureTest exten
1301      {
1302          FailingSupplier r = new FailingSupplier(m);
1303          CompletableFuture<Integer> f = m.supplyAsync(r);
1304 <        checkCompletedWithWrappedCFException(f);
1304 >        checkCompletedWithWrappedException(f, r.ex);
1305          r.assertInvoked();
1306      }}
1307  
1308 +    @SuppressWarnings("FutureReturnValueIgnored")
1309 +    public void testSupplyAsync_rejectingExecutor() {
1310 +        CountingRejectingExecutor e = new CountingRejectingExecutor();
1311 +        try {
1312 +            CompletableFuture.supplyAsync(() -> null, e);
1313 +            shouldThrow();
1314 +        } catch (Throwable t) {
1315 +            assertSame(e.ex, t);
1316 +        }
1317 +
1318 +        assertEquals(1, e.count.get());
1319 +    }
1320 +
1321      // seq completion methods
1322  
1323      /**
# Line 1264 | Line 1325 | public class CompletableFutureTest exten
1325       */
1326      public void testThenRun_normalCompletion() {
1327          for (ExecutionMode m : ExecutionMode.values())
1267        for (boolean createIncomplete : new boolean[] { true, false })
1328          for (Integer v1 : new Integer[] { 1, null })
1329      {
1330          final CompletableFuture<Integer> f = new CompletableFuture<>();
1331 <        final Noop r = new Noop(m);
1332 <        if (!createIncomplete) assertTrue(f.complete(v1));
1273 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1274 <        if (createIncomplete) {
1275 <            checkIncomplete(g);
1276 <            assertTrue(f.complete(v1));
1277 <        }
1331 >        final Noop[] rs = new Noop[6];
1332 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1333  
1334 <        checkCompletedNormally(g, null);
1334 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1335 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1336 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1337 >        checkIncomplete(h0);
1338 >        checkIncomplete(h1);
1339 >        checkIncomplete(h2);
1340 >        assertTrue(f.complete(v1));
1341 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1342 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1343 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1344 >
1345 >        checkCompletedNormally(h0, null);
1346 >        checkCompletedNormally(h1, null);
1347 >        checkCompletedNormally(h2, null);
1348 >        checkCompletedNormally(h3, null);
1349 >        checkCompletedNormally(h4, null);
1350 >        checkCompletedNormally(h5, null);
1351          checkCompletedNormally(f, v1);
1352 <        r.assertInvoked();
1352 >        for (Noop r : rs) r.assertInvoked();
1353      }}
1354  
1355      /**
# Line 1287 | Line 1358 | public class CompletableFutureTest exten
1358       */
1359      public void testThenRun_exceptionalCompletion() {
1360          for (ExecutionMode m : ExecutionMode.values())
1290        for (boolean createIncomplete : new boolean[] { true, false })
1361      {
1362          final CFException ex = new CFException();
1363          final CompletableFuture<Integer> f = new CompletableFuture<>();
1364 <        final Noop r = new Noop(m);
1365 <        if (!createIncomplete) f.completeExceptionally(ex);
1296 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1297 <        if (createIncomplete) {
1298 <            checkIncomplete(g);
1299 <            f.completeExceptionally(ex);
1300 <        }
1364 >        final Noop[] rs = new Noop[6];
1365 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1366  
1367 <        checkCompletedWithWrappedException(g, ex);
1367 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1368 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1369 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1370 >        checkIncomplete(h0);
1371 >        checkIncomplete(h1);
1372 >        checkIncomplete(h2);
1373 >        assertTrue(f.completeExceptionally(ex));
1374 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1375 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1376 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1377 >
1378 >        checkCompletedWithWrappedException(h0, ex);
1379 >        checkCompletedWithWrappedException(h1, ex);
1380 >        checkCompletedWithWrappedException(h2, ex);
1381 >        checkCompletedWithWrappedException(h3, ex);
1382 >        checkCompletedWithWrappedException(h4, ex);
1383 >        checkCompletedWithWrappedException(h5, ex);
1384          checkCompletedExceptionally(f, ex);
1385 <        r.assertNotInvoked();
1385 >        for (Noop r : rs) r.assertNotInvoked();
1386      }}
1387  
1388      /**
# Line 1309 | Line 1390 | public class CompletableFutureTest exten
1390       */
1391      public void testThenRun_sourceCancelled() {
1392          for (ExecutionMode m : ExecutionMode.values())
1312        for (boolean createIncomplete : new boolean[] { true, false })
1393          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1394      {
1395          final CompletableFuture<Integer> f = new CompletableFuture<>();
1396 <        final Noop r = new Noop(m);
1397 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1318 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1319 <        if (createIncomplete) {
1320 <            checkIncomplete(g);
1321 <            assertTrue(f.cancel(mayInterruptIfRunning));
1322 <        }
1396 >        final Noop[] rs = new Noop[6];
1397 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1398  
1399 <        checkCompletedWithWrappedCancellationException(g);
1399 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1400 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1401 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1402 >        checkIncomplete(h0);
1403 >        checkIncomplete(h1);
1404 >        checkIncomplete(h2);
1405 >        assertTrue(f.cancel(mayInterruptIfRunning));
1406 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1407 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1408 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1409 >
1410 >        checkCompletedWithWrappedCancellationException(h0);
1411 >        checkCompletedWithWrappedCancellationException(h1);
1412 >        checkCompletedWithWrappedCancellationException(h2);
1413 >        checkCompletedWithWrappedCancellationException(h3);
1414 >        checkCompletedWithWrappedCancellationException(h4);
1415 >        checkCompletedWithWrappedCancellationException(h5);
1416          checkCancelled(f);
1417 <        r.assertNotInvoked();
1417 >        for (Noop r : rs) r.assertNotInvoked();
1418      }}
1419  
1420      /**
# Line 1331 | Line 1422 | public class CompletableFutureTest exten
1422       */
1423      public void testThenRun_actionFailed() {
1424          for (ExecutionMode m : ExecutionMode.values())
1334        for (boolean createIncomplete : new boolean[] { true, false })
1425          for (Integer v1 : new Integer[] { 1, null })
1426      {
1427          final CompletableFuture<Integer> f = new CompletableFuture<>();
1428 <        final FailingRunnable r = new FailingRunnable(m);
1429 <        if (!createIncomplete) assertTrue(f.complete(v1));
1340 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1341 <        if (createIncomplete) {
1342 <            checkIncomplete(g);
1343 <            assertTrue(f.complete(v1));
1344 <        }
1428 >        final FailingRunnable[] rs = new FailingRunnable[6];
1429 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1430  
1431 <        checkCompletedWithWrappedCFException(g);
1431 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1432 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1433 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1434 >        assertTrue(f.complete(v1));
1435 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1436 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1437 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1438 >
1439 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1440 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1441 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1442 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1443 >        checkCompletedWithWrappedException(h4, rs[4].ex);
1444 >        checkCompletedWithWrappedException(h5, rs[5].ex);
1445          checkCompletedNormally(f, v1);
1446      }}
1447  
# Line 1352 | Line 1450 | public class CompletableFutureTest exten
1450       */
1451      public void testThenApply_normalCompletion() {
1452          for (ExecutionMode m : ExecutionMode.values())
1355        for (boolean createIncomplete : new boolean[] { true, false })
1453          for (Integer v1 : new Integer[] { 1, null })
1454      {
1455          final CompletableFuture<Integer> f = new CompletableFuture<>();
1456 <        final IncFunction r = new IncFunction(m);
1457 <        if (!createIncomplete) assertTrue(f.complete(v1));
1361 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1362 <        if (createIncomplete) {
1363 <            checkIncomplete(g);
1364 <            assertTrue(f.complete(v1));
1365 <        }
1456 >        final IncFunction[] rs = new IncFunction[4];
1457 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1458  
1459 <        checkCompletedNormally(g, inc(v1));
1459 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1460 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1461 >        checkIncomplete(h0);
1462 >        checkIncomplete(h1);
1463 >        assertTrue(f.complete(v1));
1464 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1465 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1466 >
1467 >        checkCompletedNormally(h0, inc(v1));
1468 >        checkCompletedNormally(h1, inc(v1));
1469 >        checkCompletedNormally(h2, inc(v1));
1470 >        checkCompletedNormally(h3, inc(v1));
1471          checkCompletedNormally(f, v1);
1472 <        r.assertValue(inc(v1));
1472 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1473      }}
1474  
1475      /**
# Line 1375 | Line 1478 | public class CompletableFutureTest exten
1478       */
1479      public void testThenApply_exceptionalCompletion() {
1480          for (ExecutionMode m : ExecutionMode.values())
1378        for (boolean createIncomplete : new boolean[] { true, false })
1481      {
1482          final CFException ex = new CFException();
1483          final CompletableFuture<Integer> f = new CompletableFuture<>();
1484 <        final IncFunction r = new IncFunction(m);
1485 <        if (!createIncomplete) f.completeExceptionally(ex);
1384 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1385 <        if (createIncomplete) {
1386 <            checkIncomplete(g);
1387 <            f.completeExceptionally(ex);
1388 <        }
1484 >        final IncFunction[] rs = new IncFunction[4];
1485 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1486  
1487 <        checkCompletedWithWrappedException(g, ex);
1487 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1488 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1489 >        assertTrue(f.completeExceptionally(ex));
1490 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1491 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1492 >
1493 >        checkCompletedWithWrappedException(h0, ex);
1494 >        checkCompletedWithWrappedException(h1, ex);
1495 >        checkCompletedWithWrappedException(h2, ex);
1496 >        checkCompletedWithWrappedException(h3, ex);
1497          checkCompletedExceptionally(f, ex);
1498 <        r.assertNotInvoked();
1498 >        for (IncFunction r : rs) r.assertNotInvoked();
1499      }}
1500  
1501      /**
# Line 1397 | Line 1503 | public class CompletableFutureTest exten
1503       */
1504      public void testThenApply_sourceCancelled() {
1505          for (ExecutionMode m : ExecutionMode.values())
1400        for (boolean createIncomplete : new boolean[] { true, false })
1506          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1507      {
1508          final CompletableFuture<Integer> f = new CompletableFuture<>();
1509 <        final IncFunction r = new IncFunction(m);
1510 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1406 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1407 <        if (createIncomplete) {
1408 <            checkIncomplete(g);
1409 <            assertTrue(f.cancel(mayInterruptIfRunning));
1410 <        }
1509 >        final IncFunction[] rs = new IncFunction[4];
1510 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1511  
1512 <        checkCompletedWithWrappedCancellationException(g);
1512 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1513 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1514 >        assertTrue(f.cancel(mayInterruptIfRunning));
1515 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1516 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1517 >
1518 >        checkCompletedWithWrappedCancellationException(h0);
1519 >        checkCompletedWithWrappedCancellationException(h1);
1520 >        checkCompletedWithWrappedCancellationException(h2);
1521 >        checkCompletedWithWrappedCancellationException(h3);
1522          checkCancelled(f);
1523 <        r.assertNotInvoked();
1523 >        for (IncFunction r : rs) r.assertNotInvoked();
1524      }}
1525  
1526      /**
# Line 1419 | Line 1528 | public class CompletableFutureTest exten
1528       */
1529      public void testThenApply_actionFailed() {
1530          for (ExecutionMode m : ExecutionMode.values())
1422        for (boolean createIncomplete : new boolean[] { true, false })
1531          for (Integer v1 : new Integer[] { 1, null })
1532      {
1533          final CompletableFuture<Integer> f = new CompletableFuture<>();
1534 <        final FailingFunction r = new FailingFunction(m);
1535 <        if (!createIncomplete) assertTrue(f.complete(v1));
1428 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1429 <        if (createIncomplete) {
1430 <            checkIncomplete(g);
1431 <            assertTrue(f.complete(v1));
1432 <        }
1534 >        final FailingFunction[] rs = new FailingFunction[4];
1535 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1536  
1537 <        checkCompletedWithWrappedCFException(g);
1537 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1538 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1539 >        assertTrue(f.complete(v1));
1540 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1541 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1542 >
1543 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1544 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1545 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1546 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1547          checkCompletedNormally(f, v1);
1548      }}
1549  
# Line 1440 | Line 1552 | public class CompletableFutureTest exten
1552       */
1553      public void testThenAccept_normalCompletion() {
1554          for (ExecutionMode m : ExecutionMode.values())
1443        for (boolean createIncomplete : new boolean[] { true, false })
1555          for (Integer v1 : new Integer[] { 1, null })
1556      {
1557          final CompletableFuture<Integer> f = new CompletableFuture<>();
1558 <        final NoopConsumer r = new NoopConsumer(m);
1559 <        if (!createIncomplete) assertTrue(f.complete(v1));
1449 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1450 <        if (createIncomplete) {
1451 <            checkIncomplete(g);
1452 <            assertTrue(f.complete(v1));
1453 <        }
1558 >        final NoopConsumer[] rs = new NoopConsumer[4];
1559 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1560  
1561 <        checkCompletedNormally(g, null);
1562 <        r.assertValue(v1);
1561 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1562 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1563 >        checkIncomplete(h0);
1564 >        checkIncomplete(h1);
1565 >        assertTrue(f.complete(v1));
1566 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1567 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1568 >
1569 >        checkCompletedNormally(h0, null);
1570 >        checkCompletedNormally(h1, null);
1571 >        checkCompletedNormally(h2, null);
1572 >        checkCompletedNormally(h3, null);
1573          checkCompletedNormally(f, v1);
1574 +        for (NoopConsumer r : rs) r.assertValue(v1);
1575      }}
1576  
1577      /**
# Line 1463 | Line 1580 | public class CompletableFutureTest exten
1580       */
1581      public void testThenAccept_exceptionalCompletion() {
1582          for (ExecutionMode m : ExecutionMode.values())
1466        for (boolean createIncomplete : new boolean[] { true, false })
1583      {
1584          final CFException ex = new CFException();
1585          final CompletableFuture<Integer> f = new CompletableFuture<>();
1586 <        final NoopConsumer r = new NoopConsumer(m);
1587 <        if (!createIncomplete) f.completeExceptionally(ex);
1472 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1473 <        if (createIncomplete) {
1474 <            checkIncomplete(g);
1475 <            f.completeExceptionally(ex);
1476 <        }
1586 >        final NoopConsumer[] rs = new NoopConsumer[4];
1587 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1588  
1589 <        checkCompletedWithWrappedException(g, ex);
1589 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1590 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1591 >        assertTrue(f.completeExceptionally(ex));
1592 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1593 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1594 >
1595 >        checkCompletedWithWrappedException(h0, ex);
1596 >        checkCompletedWithWrappedException(h1, ex);
1597 >        checkCompletedWithWrappedException(h2, ex);
1598 >        checkCompletedWithWrappedException(h3, ex);
1599          checkCompletedExceptionally(f, ex);
1600 <        r.assertNotInvoked();
1600 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1601      }}
1602  
1603      /**
# Line 1485 | Line 1605 | public class CompletableFutureTest exten
1605       */
1606      public void testThenAccept_sourceCancelled() {
1607          for (ExecutionMode m : ExecutionMode.values())
1488        for (boolean createIncomplete : new boolean[] { true, false })
1608          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1609      {
1610          final CompletableFuture<Integer> f = new CompletableFuture<>();
1611 <        final NoopConsumer r = new NoopConsumer(m);
1612 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1494 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1495 <        if (createIncomplete) {
1496 <            checkIncomplete(g);
1497 <            assertTrue(f.cancel(mayInterruptIfRunning));
1498 <        }
1611 >        final NoopConsumer[] rs = new NoopConsumer[4];
1612 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1613  
1614 <        checkCompletedWithWrappedCancellationException(g);
1614 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1615 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1616 >        assertTrue(f.cancel(mayInterruptIfRunning));
1617 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1618 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1619 >
1620 >        checkCompletedWithWrappedCancellationException(h0);
1621 >        checkCompletedWithWrappedCancellationException(h1);
1622 >        checkCompletedWithWrappedCancellationException(h2);
1623 >        checkCompletedWithWrappedCancellationException(h3);
1624          checkCancelled(f);
1625 <        r.assertNotInvoked();
1625 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1626      }}
1627  
1628      /**
# Line 1507 | Line 1630 | public class CompletableFutureTest exten
1630       */
1631      public void testThenAccept_actionFailed() {
1632          for (ExecutionMode m : ExecutionMode.values())
1510        for (boolean createIncomplete : new boolean[] { true, false })
1633          for (Integer v1 : new Integer[] { 1, null })
1634      {
1635          final CompletableFuture<Integer> f = new CompletableFuture<>();
1636 <        final FailingConsumer r = new FailingConsumer(m);
1637 <        if (!createIncomplete) f.complete(v1);
1516 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1517 <        if (createIncomplete) {
1518 <            checkIncomplete(g);
1519 <            f.complete(v1);
1520 <        }
1636 >        final FailingConsumer[] rs = new FailingConsumer[4];
1637 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1638  
1639 <        checkCompletedWithWrappedCFException(g);
1639 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1640 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1641 >        assertTrue(f.complete(v1));
1642 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1643 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1644 >
1645 >        checkCompletedWithWrappedException(h0, rs[0].ex);
1646 >        checkCompletedWithWrappedException(h1, rs[1].ex);
1647 >        checkCompletedWithWrappedException(h2, rs[2].ex);
1648 >        checkCompletedWithWrappedException(h3, rs[3].ex);
1649          checkCompletedNormally(f, v1);
1650      }}
1651  
# Line 1535 | Line 1661 | public class CompletableFutureTest exten
1661      {
1662          final CompletableFuture<Integer> f = new CompletableFuture<>();
1663          final CompletableFuture<Integer> g = new CompletableFuture<>();
1664 <        final SubtractFunction r1 = new SubtractFunction(m);
1665 <        final SubtractFunction r2 = new SubtractFunction(m);
1540 <        final SubtractFunction r3 = new SubtractFunction(m);
1664 >        final SubtractFunction[] rs = new SubtractFunction[6];
1665 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1666  
1667          final CompletableFuture<Integer> fst =  fFirst ? f : g;
1668          final CompletableFuture<Integer> snd = !fFirst ? f : g;
1669          final Integer w1 =  fFirst ? v1 : v2;
1670          final Integer w2 = !fFirst ? v1 : v2;
1671  
1672 <        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1672 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1673 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1674          assertTrue(fst.complete(w1));
1675 <        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1676 <        checkIncomplete(h1);
1677 <        checkIncomplete(h2);
1678 <        r1.assertNotInvoked();
1679 <        r2.assertNotInvoked();
1675 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1676 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1677 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1678 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1679 >        checkCompletedNormally(h1, subtract(w1, w1));
1680 >        checkCompletedNormally(h3, subtract(w1, w1));
1681 >        rs[1].assertValue(subtract(w1, w1));
1682 >        rs[3].assertValue(subtract(w1, w1));
1683          assertTrue(snd.complete(w2));
1684 <        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1684 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1685  
1686 <        checkCompletedNormally(h1, subtract(v1, v2));
1686 >        checkCompletedNormally(h0, subtract(v1, v2));
1687          checkCompletedNormally(h2, subtract(v1, v2));
1688 <        checkCompletedNormally(h3, subtract(v1, v2));
1689 <        r1.assertValue(subtract(v1, v2));
1690 <        r2.assertValue(subtract(v1, v2));
1691 <        r3.assertValue(subtract(v1, v2));
1688 >        checkCompletedNormally(h4, subtract(v1, v2));
1689 >        rs[0].assertValue(subtract(v1, v2));
1690 >        rs[2].assertValue(subtract(v1, v2));
1691 >        rs[4].assertValue(subtract(v1, v2));
1692 >
1693          checkCompletedNormally(f, v1);
1694          checkCompletedNormally(g, v2);
1695      }}
# Line 1677 | Line 1807 | public class CompletableFutureTest exten
1807          assertTrue(snd.complete(w2));
1808          final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1809  
1810 <        checkCompletedWithWrappedCFException(h1);
1811 <        checkCompletedWithWrappedCFException(h2);
1812 <        checkCompletedWithWrappedCFException(h3);
1810 >        checkCompletedWithWrappedException(h1, r1.ex);
1811 >        checkCompletedWithWrappedException(h2, r2.ex);
1812 >        checkCompletedWithWrappedException(h3, r3.ex);
1813 >        r1.assertInvoked();
1814 >        r2.assertInvoked();
1815 >        r3.assertInvoked();
1816          checkCompletedNormally(f, v1);
1817          checkCompletedNormally(g, v2);
1818      }}
# Line 1838 | Line 1971 | public class CompletableFutureTest exten
1971          assertTrue(snd.complete(w2));
1972          final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1973  
1974 <        checkCompletedWithWrappedCFException(h1);
1975 <        checkCompletedWithWrappedCFException(h2);
1976 <        checkCompletedWithWrappedCFException(h3);
1974 >        checkCompletedWithWrappedException(h1, r1.ex);
1975 >        checkCompletedWithWrappedException(h2, r2.ex);
1976 >        checkCompletedWithWrappedException(h3, r3.ex);
1977 >        r1.assertInvoked();
1978 >        r2.assertInvoked();
1979 >        r3.assertInvoked();
1980          checkCompletedNormally(f, v1);
1981          checkCompletedNormally(g, v2);
1982      }}
# Line 1851 | Line 1987 | public class CompletableFutureTest exten
1987       */
1988      public void testRunAfterBoth_normalCompletion() {
1989          for (ExecutionMode m : ExecutionMode.values())
1854        for (boolean createIncomplete : new boolean[] { true, false })
1990          for (boolean fFirst : new boolean[] { true, false })
1991          for (Integer v1 : new Integer[] { 1, null })
1992          for (Integer v2 : new Integer[] { 2, null })
1993      {
1994          final CompletableFuture<Integer> f = new CompletableFuture<>();
1995          final CompletableFuture<Integer> g = new CompletableFuture<>();
1996 <        final Noop r = new Noop(m);
1996 >        final Noop r1 = new Noop(m);
1997 >        final Noop r2 = new Noop(m);
1998 >        final Noop r3 = new Noop(m);
1999  
2000 <        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
2001 <        if (!createIncomplete)
2002 <            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
2003 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1867 <        if (createIncomplete) {
1868 <            checkIncomplete(h);
1869 <            r.assertNotInvoked();
1870 <            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1871 <        }
2000 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2001 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2002 >        final Integer w1 =  fFirst ? v1 : v2;
2003 >        final Integer w2 = !fFirst ? v1 : v2;
2004  
2005 <        checkCompletedNormally(h, null);
2006 <        r.assertInvoked();
2005 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2006 >        assertTrue(fst.complete(w1));
2007 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2008 >        checkIncomplete(h1);
2009 >        checkIncomplete(h2);
2010 >        r1.assertNotInvoked();
2011 >        r2.assertNotInvoked();
2012 >        assertTrue(snd.complete(w2));
2013 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2014 >
2015 >        checkCompletedNormally(h1, null);
2016 >        checkCompletedNormally(h2, null);
2017 >        checkCompletedNormally(h3, null);
2018 >        r1.assertInvoked();
2019 >        r2.assertInvoked();
2020 >        r3.assertInvoked();
2021          checkCompletedNormally(f, v1);
2022          checkCompletedNormally(g, v2);
2023      }}
# Line 1880 | Line 2026 | public class CompletableFutureTest exten
2026       * runAfterBoth result completes exceptionally after exceptional
2027       * completion of either source
2028       */
2029 <    public void testRunAfterBoth_exceptionalCompletion() {
2029 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
2030          for (ExecutionMode m : ExecutionMode.values())
1885        for (boolean createIncomplete : new boolean[] { true, false })
2031          for (boolean fFirst : new boolean[] { true, false })
2032 +        for (boolean failFirst : new boolean[] { true, false })
2033          for (Integer v1 : new Integer[] { 1, null })
2034      {
2035          final CompletableFuture<Integer> f = new CompletableFuture<>();
2036          final CompletableFuture<Integer> g = new CompletableFuture<>();
2037          final CFException ex = new CFException();
2038 <        final Noop r = new Noop(m);
2038 >        final Noop r1 = new Noop(m);
2039 >        final Noop r2 = new Noop(m);
2040 >        final Noop r3 = new Noop(m);
2041  
2042 <        assertTrue((fFirst ? f : g).complete(v1));
2043 <        if (!createIncomplete)
2044 <            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
2045 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2046 <        if (createIncomplete) {
2047 <            checkIncomplete(h);
2048 <            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
2049 <        }
2042 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2043 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2044 >        final Callable<Boolean> complete1 = failFirst ?
2045 >            () -> fst.completeExceptionally(ex) :
2046 >            () -> fst.complete(v1);
2047 >        final Callable<Boolean> complete2 = failFirst ?
2048 >            () -> snd.complete(v1) :
2049 >            () -> snd.completeExceptionally(ex);
2050  
2051 <        checkCompletedWithWrappedException(h, ex);
2052 <        r.assertNotInvoked();
2053 <        checkCompletedNormally(fFirst ? f : g, v1);
2054 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
2051 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2052 >        assertTrue(complete1.call());
2053 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2054 >        checkIncomplete(h1);
2055 >        checkIncomplete(h2);
2056 >        assertTrue(complete2.call());
2057 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2058 >
2059 >        checkCompletedWithWrappedException(h1, ex);
2060 >        checkCompletedWithWrappedException(h2, ex);
2061 >        checkCompletedWithWrappedException(h3, ex);
2062 >        r1.assertNotInvoked();
2063 >        r2.assertNotInvoked();
2064 >        r3.assertNotInvoked();
2065 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2066 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2067      }}
2068  
2069      /**
2070       * runAfterBoth result completes exceptionally if either source cancelled
2071       */
2072 <    public void testRunAfterBoth_sourceCancelled() {
2072 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2073          for (ExecutionMode m : ExecutionMode.values())
2074          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1915        for (boolean createIncomplete : new boolean[] { true, false })
2075          for (boolean fFirst : new boolean[] { true, false })
2076 +        for (boolean failFirst : new boolean[] { true, false })
2077          for (Integer v1 : new Integer[] { 1, null })
2078      {
2079          final CompletableFuture<Integer> f = new CompletableFuture<>();
2080          final CompletableFuture<Integer> g = new CompletableFuture<>();
2081 <        final Noop r = new Noop(m);
2081 >        final Noop r1 = new Noop(m);
2082 >        final Noop r2 = new Noop(m);
2083 >        final Noop r3 = new Noop(m);
2084  
2085 <        assertTrue((fFirst ? f : g).complete(v1));
2086 <        if (!createIncomplete)
2087 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2088 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2089 <        if (createIncomplete) {
2090 <            checkIncomplete(h);
2091 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2092 <        }
2085 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2086 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2087 >        final Callable<Boolean> complete1 = failFirst ?
2088 >            () -> fst.cancel(mayInterruptIfRunning) :
2089 >            () -> fst.complete(v1);
2090 >        final Callable<Boolean> complete2 = failFirst ?
2091 >            () -> snd.complete(v1) :
2092 >            () -> snd.cancel(mayInterruptIfRunning);
2093  
2094 <        checkCompletedWithWrappedCancellationException(h);
2095 <        checkCancelled(!fFirst ? f : g);
2096 <        r.assertNotInvoked();
2097 <        checkCompletedNormally(fFirst ? f : g, v1);
2094 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2095 >        assertTrue(complete1.call());
2096 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2097 >        checkIncomplete(h1);
2098 >        checkIncomplete(h2);
2099 >        assertTrue(complete2.call());
2100 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2101 >
2102 >        checkCompletedWithWrappedCancellationException(h1);
2103 >        checkCompletedWithWrappedCancellationException(h2);
2104 >        checkCompletedWithWrappedCancellationException(h3);
2105 >        r1.assertNotInvoked();
2106 >        r2.assertNotInvoked();
2107 >        r3.assertNotInvoked();
2108 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2109 >        checkCancelled(failFirst ? fst : snd);
2110      }}
2111  
2112      /**
# Line 1948 | Line 2122 | public class CompletableFutureTest exten
2122          final CompletableFuture<Integer> g = new CompletableFuture<>();
2123          final FailingRunnable r1 = new FailingRunnable(m);
2124          final FailingRunnable r2 = new FailingRunnable(m);
2125 +        final FailingRunnable r3 = new FailingRunnable(m);
2126  
2127 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2128 <        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
2129 <        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
2130 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2127 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2128 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2129 >        final Integer w1 =  fFirst ? v1 : v2;
2130 >        final Integer w2 = !fFirst ? v1 : v2;
2131 >
2132 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2133 >        assertTrue(fst.complete(w1));
2134 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2135 >        assertTrue(snd.complete(w2));
2136 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2137  
2138 <        checkCompletedWithWrappedCFException(h1);
2139 <        checkCompletedWithWrappedCFException(h2);
2138 >        checkCompletedWithWrappedException(h1, r1.ex);
2139 >        checkCompletedWithWrappedException(h2, r2.ex);
2140 >        checkCompletedWithWrappedException(h3, r3.ex);
2141 >        r1.assertInvoked();
2142 >        r2.assertInvoked();
2143 >        r3.assertInvoked();
2144          checkCompletedNormally(f, v1);
2145          checkCompletedNormally(g, v2);
2146      }}
# Line 2242 | Line 2427 | public class CompletableFutureTest exten
2427          f.complete(v1);
2428          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2429          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2430 <        checkCompletedWithWrappedCFException(h0);
2431 <        checkCompletedWithWrappedCFException(h1);
2432 <        checkCompletedWithWrappedCFException(h2);
2433 <        checkCompletedWithWrappedCFException(h3);
2430 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2431 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2432 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2433 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2434          for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2435  
2436          g.complete(v2);
# Line 2254 | Line 2439 | public class CompletableFutureTest exten
2439          final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]);
2440          final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]);
2441  
2442 <        checkCompletedWithWrappedCFException(h4);
2442 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2443          assertTrue(Objects.equals(v1, rs[4].value) ||
2444                     Objects.equals(v2, rs[4].value));
2445 <        checkCompletedWithWrappedCFException(h5);
2445 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2446          assertTrue(Objects.equals(v1, rs[5].value) ||
2447                     Objects.equals(v2, rs[5].value));
2448  
# Line 2395 | Line 2580 | public class CompletableFutureTest exten
2580  
2581          // unspecified behavior - both source completions available
2582          try {
2583 <            assertEquals(null, h0.join());
2583 >            assertNull(h0.join());
2584              rs[0].assertValue(v1);
2585          } catch (CompletionException ok) {
2586              checkCompletedWithWrappedException(h0, ex);
2587              rs[0].assertNotInvoked();
2588          }
2589          try {
2590 <            assertEquals(null, h1.join());
2590 >            assertNull(h1.join());
2591              rs[1].assertValue(v1);
2592          } catch (CompletionException ok) {
2593              checkCompletedWithWrappedException(h1, ex);
2594              rs[1].assertNotInvoked();
2595          }
2596          try {
2597 <            assertEquals(null, h2.join());
2597 >            assertNull(h2.join());
2598              rs[2].assertValue(v1);
2599          } catch (CompletionException ok) {
2600              checkCompletedWithWrappedException(h2, ex);
2601              rs[2].assertNotInvoked();
2602          }
2603          try {
2604 <            assertEquals(null, h3.join());
2604 >            assertNull(h3.join());
2605              rs[3].assertValue(v1);
2606          } catch (CompletionException ok) {
2607              checkCompletedWithWrappedException(h3, ex);
# Line 2501 | Line 2686 | public class CompletableFutureTest exten
2686          f.complete(v1);
2687          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2688          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2689 <        checkCompletedWithWrappedCFException(h0);
2690 <        checkCompletedWithWrappedCFException(h1);
2691 <        checkCompletedWithWrappedCFException(h2);
2692 <        checkCompletedWithWrappedCFException(h3);
2689 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2690 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2691 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2692 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2693          for (int i = 0; i < 4; i++) rs[i].assertValue(v1);
2694  
2695          g.complete(v2);
# Line 2513 | Line 2698 | public class CompletableFutureTest exten
2698          final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]);
2699          final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]);
2700  
2701 <        checkCompletedWithWrappedCFException(h4);
2701 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2702          assertTrue(Objects.equals(v1, rs[4].value) ||
2703                     Objects.equals(v2, rs[4].value));
2704 <        checkCompletedWithWrappedCFException(h5);
2704 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2705          assertTrue(Objects.equals(v1, rs[5].value) ||
2706                     Objects.equals(v2, rs[5].value));
2707  
# Line 2532 | Line 2717 | public class CompletableFutureTest exten
2717          for (ExecutionMode m : ExecutionMode.values())
2718          for (Integer v1 : new Integer[] { 1, null })
2719          for (Integer v2 : new Integer[] { 2, null })
2720 +        for (boolean pushNop : new boolean[] { true, false })
2721      {
2722          final CompletableFuture<Integer> f = new CompletableFuture<>();
2723          final CompletableFuture<Integer> g = new CompletableFuture<>();
# Line 2544 | Line 2730 | public class CompletableFutureTest exten
2730          checkIncomplete(h1);
2731          rs[0].assertNotInvoked();
2732          rs[1].assertNotInvoked();
2733 +        if (pushNop) {          // ad hoc test of intra-completion interference
2734 +            m.thenRun(f, () -> {});
2735 +            m.thenRun(g, () -> {});
2736 +        }
2737          f.complete(v1);
2738          checkCompletedNormally(h0, null);
2739          checkCompletedNormally(h1, null);
# Line 2650 | Line 2840 | public class CompletableFutureTest exten
2840  
2841          // unspecified behavior - both source completions available
2842          try {
2843 <            assertEquals(null, h0.join());
2843 >            assertNull(h0.join());
2844              rs[0].assertInvoked();
2845          } catch (CompletionException ok) {
2846              checkCompletedWithWrappedException(h0, ex);
2847              rs[0].assertNotInvoked();
2848          }
2849          try {
2850 <            assertEquals(null, h1.join());
2850 >            assertNull(h1.join());
2851              rs[1].assertInvoked();
2852          } catch (CompletionException ok) {
2853              checkCompletedWithWrappedException(h1, ex);
2854              rs[1].assertNotInvoked();
2855          }
2856          try {
2857 <            assertEquals(null, h2.join());
2857 >            assertNull(h2.join());
2858              rs[2].assertInvoked();
2859          } catch (CompletionException ok) {
2860              checkCompletedWithWrappedException(h2, ex);
2861              rs[2].assertNotInvoked();
2862          }
2863          try {
2864 <            assertEquals(null, h3.join());
2864 >            assertNull(h3.join());
2865              rs[3].assertInvoked();
2866          } catch (CompletionException ok) {
2867              checkCompletedWithWrappedException(h3, ex);
# Line 2756 | Line 2946 | public class CompletableFutureTest exten
2946          assertTrue(f.complete(v1));
2947          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2948          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2949 <        checkCompletedWithWrappedCFException(h0);
2950 <        checkCompletedWithWrappedCFException(h1);
2951 <        checkCompletedWithWrappedCFException(h2);
2952 <        checkCompletedWithWrappedCFException(h3);
2949 >        checkCompletedWithWrappedException(h0, rs[0].ex);
2950 >        checkCompletedWithWrappedException(h1, rs[1].ex);
2951 >        checkCompletedWithWrappedException(h2, rs[2].ex);
2952 >        checkCompletedWithWrappedException(h3, rs[3].ex);
2953          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2954          assertTrue(g.complete(v2));
2955          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2956          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2957 <        checkCompletedWithWrappedCFException(h4);
2958 <        checkCompletedWithWrappedCFException(h5);
2957 >        checkCompletedWithWrappedException(h4, rs[4].ex);
2958 >        checkCompletedWithWrappedException(h5, rs[5].ex);
2959  
2960          checkCompletedNormally(f, v1);
2961          checkCompletedNormally(g, v2);
# Line 2826 | Line 3016 | public class CompletableFutureTest exten
3016          final CompletableFuture<Integer> g = m.thenCompose(f, r);
3017          if (createIncomplete) assertTrue(f.complete(v1));
3018  
3019 <        checkCompletedWithWrappedCFException(g);
3019 >        checkCompletedWithWrappedException(g, r.ex);
3020          checkCompletedNormally(f, v1);
3021      }}
3022  
# Line 2851 | Line 3041 | public class CompletableFutureTest exten
3041          checkCancelled(f);
3042      }}
3043  
3044 +    /**
3045 +     * thenCompose result completes exceptionally if the result of the action does
3046 +     */
3047 +    public void testThenCompose_actionReturnsFailingFuture() {
3048 +        for (ExecutionMode m : ExecutionMode.values())
3049 +        for (int order = 0; order < 6; order++)
3050 +        for (Integer v1 : new Integer[] { 1, null })
3051 +    {
3052 +        final CFException ex = new CFException();
3053 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3054 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
3055 +        final CompletableFuture<Integer> h;
3056 +        // Test all permutations of orders
3057 +        switch (order) {
3058 +        case 0:
3059 +            assertTrue(f.complete(v1));
3060 +            assertTrue(g.completeExceptionally(ex));
3061 +            h = m.thenCompose(f, (x -> g));
3062 +            break;
3063 +        case 1:
3064 +            assertTrue(f.complete(v1));
3065 +            h = m.thenCompose(f, (x -> g));
3066 +            assertTrue(g.completeExceptionally(ex));
3067 +            break;
3068 +        case 2:
3069 +            assertTrue(g.completeExceptionally(ex));
3070 +            assertTrue(f.complete(v1));
3071 +            h = m.thenCompose(f, (x -> g));
3072 +            break;
3073 +        case 3:
3074 +            assertTrue(g.completeExceptionally(ex));
3075 +            h = m.thenCompose(f, (x -> g));
3076 +            assertTrue(f.complete(v1));
3077 +            break;
3078 +        case 4:
3079 +            h = m.thenCompose(f, (x -> g));
3080 +            assertTrue(f.complete(v1));
3081 +            assertTrue(g.completeExceptionally(ex));
3082 +            break;
3083 +        case 5:
3084 +            h = m.thenCompose(f, (x -> g));
3085 +            assertTrue(f.complete(v1));
3086 +            assertTrue(g.completeExceptionally(ex));
3087 +            break;
3088 +        default: throw new AssertionError();
3089 +        }
3090 +
3091 +        checkCompletedExceptionally(g, ex);
3092 +        checkCompletedWithWrappedException(h, ex);
3093 +        checkCompletedNormally(f, v1);
3094 +    }}
3095 +
3096      // other static methods
3097  
3098      /**
# Line 2867 | Line 3109 | public class CompletableFutureTest exten
3109       * when all components complete normally
3110       */
3111      public void testAllOf_normal() throws Exception {
3112 <        for (int k = 1; k < 20; ++k) {
3112 >        for (int k = 1; k < 10; k++) {
3113              CompletableFuture<Integer>[] fs
3114                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3115 <            for (int i = 0; i < k; ++i)
3115 >            for (int i = 0; i < k; i++)
3116                  fs[i] = new CompletableFuture<>();
3117              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3118 <            for (int i = 0; i < k; ++i) {
3118 >            for (int i = 0; i < k; i++) {
3119                  checkIncomplete(f);
3120                  checkIncomplete(CompletableFuture.allOf(fs));
3121                  fs[i].complete(one);
# Line 2883 | Line 3125 | public class CompletableFutureTest exten
3125          }
3126      }
3127  
3128 <    public void testAllOf_backwards() throws Exception {
3129 <        for (int k = 1; k < 20; ++k) {
3128 >    public void testAllOf_normal_backwards() throws Exception {
3129 >        for (int k = 1; k < 10; k++) {
3130              CompletableFuture<Integer>[] fs
3131                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3132 <            for (int i = 0; i < k; ++i)
3132 >            for (int i = 0; i < k; i++)
3133                  fs[i] = new CompletableFuture<>();
3134              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3135              for (int i = k - 1; i >= 0; i--) {
# Line 2900 | Line 3142 | public class CompletableFutureTest exten
3142          }
3143      }
3144  
3145 +    public void testAllOf_exceptional() throws Exception {
3146 +        for (int k = 1; k < 10; k++) {
3147 +            CompletableFuture<Integer>[] fs
3148 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3149 +            CFException ex = new CFException();
3150 +            for (int i = 0; i < k; i++)
3151 +                fs[i] = new CompletableFuture<>();
3152 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3153 +            for (int i = 0; i < k; i++) {
3154 +                checkIncomplete(f);
3155 +                checkIncomplete(CompletableFuture.allOf(fs));
3156 +                if (i != k / 2) {
3157 +                    fs[i].complete(i);
3158 +                    checkCompletedNormally(fs[i], i);
3159 +                } else {
3160 +                    fs[i].completeExceptionally(ex);
3161 +                    checkCompletedExceptionally(fs[i], ex);
3162 +                }
3163 +            }
3164 +            checkCompletedWithWrappedException(f, ex);
3165 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3166 +        }
3167 +    }
3168 +
3169      /**
3170       * anyOf(no component futures) returns an incomplete future
3171       */
3172      public void testAnyOf_empty() throws Exception {
3173 +        for (Integer v1 : new Integer[] { 1, null })
3174 +    {
3175          CompletableFuture<Object> f = CompletableFuture.anyOf();
3176          checkIncomplete(f);
3177 <    }
3177 >
3178 >        f.complete(v1);
3179 >        checkCompletedNormally(f, v1);
3180 >    }}
3181  
3182      /**
3183       * anyOf returns a future completed normally with a value when
3184       * a component future does
3185       */
3186      public void testAnyOf_normal() throws Exception {
3187 <        for (int k = 0; k < 10; ++k) {
3187 >        for (int k = 0; k < 10; k++) {
3188              CompletableFuture[] fs = new CompletableFuture[k];
3189 <            for (int i = 0; i < k; ++i)
3189 >            for (int i = 0; i < k; i++)
3190                  fs[i] = new CompletableFuture<>();
3191              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3192              checkIncomplete(f);
3193 <            for (int i = 0; i < k; ++i) {
3194 <                fs[i].complete(one);
3195 <                checkCompletedNormally(f, one);
3196 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3193 >            for (int i = 0; i < k; i++) {
3194 >                fs[i].complete(i);
3195 >                checkCompletedNormally(f, 0);
3196 >                int x = (int) CompletableFuture.anyOf(fs).join();
3197 >                assertTrue(0 <= x && x <= i);
3198 >            }
3199 >        }
3200 >    }
3201 >    public void testAnyOf_normal_backwards() throws Exception {
3202 >        for (int k = 0; k < 10; k++) {
3203 >            CompletableFuture[] fs = new CompletableFuture[k];
3204 >            for (int i = 0; i < k; i++)
3205 >                fs[i] = new CompletableFuture<>();
3206 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3207 >            checkIncomplete(f);
3208 >            for (int i = k - 1; i >= 0; i--) {
3209 >                fs[i].complete(i);
3210 >                checkCompletedNormally(f, k - 1);
3211 >                int x = (int) CompletableFuture.anyOf(fs).join();
3212 >                assertTrue(i <= x && x <= k - 1);
3213              }
3214          }
3215      }
# Line 2931 | Line 3218 | public class CompletableFutureTest exten
3218       * anyOf result completes exceptionally when any component does.
3219       */
3220      public void testAnyOf_exceptional() throws Exception {
3221 <        for (int k = 0; k < 10; ++k) {
3221 >        for (int k = 0; k < 10; k++) {
3222              CompletableFuture[] fs = new CompletableFuture[k];
3223 <            for (int i = 0; i < k; ++i)
3223 >            CFException[] exs = new CFException[k];
3224 >            for (int i = 0; i < k; i++) {
3225                  fs[i] = new CompletableFuture<>();
3226 +                exs[i] = new CFException();
3227 +            }
3228              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3229              checkIncomplete(f);
3230 <            for (int i = 0; i < k; ++i) {
3231 <                fs[i].completeExceptionally(new CFException());
3232 <                checkCompletedWithWrappedCFException(f);
3230 >            for (int i = 0; i < k; i++) {
3231 >                fs[i].completeExceptionally(exs[i]);
3232 >                checkCompletedWithWrappedException(f, exs[0]);
3233 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3234 >            }
3235 >        }
3236 >    }
3237 >
3238 >    public void testAnyOf_exceptional_backwards() throws Exception {
3239 >        for (int k = 0; k < 10; k++) {
3240 >            CompletableFuture[] fs = new CompletableFuture[k];
3241 >            CFException[] exs = new CFException[k];
3242 >            for (int i = 0; i < k; i++) {
3243 >                fs[i] = new CompletableFuture<>();
3244 >                exs[i] = new CFException();
3245 >            }
3246 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3247 >            checkIncomplete(f);
3248 >            for (int i = k - 1; i >= 0; i--) {
3249 >                fs[i].completeExceptionally(exs[i]);
3250 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3251                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3252              }
3253          }
# Line 2948 | Line 3256 | public class CompletableFutureTest exten
3256      /**
3257       * Completion methods throw NullPointerException with null arguments
3258       */
3259 +    @SuppressWarnings("FutureReturnValueIgnored")
3260      public void testNPE() {
3261          CompletableFuture<Integer> f = new CompletableFuture<>();
3262          CompletableFuture<Integer> g = new CompletableFuture<>();
3263          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2955        CompletableFuture<?> h;
3264          ThreadExecutor exec = new ThreadExecutor();
3265  
3266          Runnable[] throwingActions = {
3267              () -> CompletableFuture.supplyAsync(null),
3268              () -> CompletableFuture.supplyAsync(null, exec),
3269 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3269 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3270  
3271              () -> CompletableFuture.runAsync(null),
3272              () -> CompletableFuture.runAsync(null, exec),
# Line 2968 | Line 3276 | public class CompletableFutureTest exten
3276  
3277              () -> f.thenApply(null),
3278              () -> f.thenApplyAsync(null),
3279 <            () -> f.thenApplyAsync((x) -> x, null),
3279 >            () -> f.thenApplyAsync(x -> x, null),
3280              () -> f.thenApplyAsync(null, exec),
3281  
3282              () -> f.thenAccept(null),
3283              () -> f.thenAcceptAsync(null),
3284 <            () -> f.thenAcceptAsync((x) -> {} , null),
3284 >            () -> f.thenAcceptAsync(x -> {} , null),
3285              () -> f.thenAcceptAsync(null, exec),
3286  
3287              () -> f.thenRun(null),
# Line 3008 | Line 3316 | public class CompletableFutureTest exten
3316              () -> f.applyToEither(g, null),
3317              () -> f.applyToEitherAsync(g, null),
3318              () -> f.applyToEitherAsync(g, null, exec),
3319 <            () -> f.applyToEither(nullFuture, (x) -> x),
3320 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x),
3321 <            () -> f.applyToEitherAsync(nullFuture, (x) -> x, exec),
3322 <            () -> f.applyToEitherAsync(g, (x) -> x, null),
3319 >            () -> f.applyToEither(nullFuture, x -> x),
3320 >            () -> f.applyToEitherAsync(nullFuture, x -> x),
3321 >            () -> f.applyToEitherAsync(nullFuture, x -> x, exec),
3322 >            () -> f.applyToEitherAsync(g, x -> x, null),
3323  
3324              () -> f.acceptEither(g, null),
3325              () -> f.acceptEitherAsync(g, null),
3326              () -> f.acceptEitherAsync(g, null, exec),
3327 <            () -> f.acceptEither(nullFuture, (x) -> {}),
3328 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}),
3329 <            () -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec),
3330 <            () -> f.acceptEitherAsync(g, (x) -> {}, null),
3327 >            () -> f.acceptEither(nullFuture, x -> {}),
3328 >            () -> f.acceptEitherAsync(nullFuture, x -> {}),
3329 >            () -> f.acceptEitherAsync(nullFuture, x -> {}, exec),
3330 >            () -> f.acceptEitherAsync(g, x -> {}, null),
3331  
3332              () -> f.runAfterEither(g, null),
3333              () -> f.runAfterEitherAsync(g, null),
# Line 3049 | Line 3357 | public class CompletableFutureTest exten
3357              () -> CompletableFuture.anyOf(null, f),
3358  
3359              () -> f.obtrudeException(null),
3360 +
3361 +            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3362 +            () -> CompletableFuture.delayedExecutor(1L, null, exec),
3363 +            () -> CompletableFuture.delayedExecutor(1L, null),
3364 +
3365 +            () -> f.orTimeout(1L, null),
3366 +            () -> f.completeOnTimeout(42, 1L, null),
3367 +
3368 +            () -> CompletableFuture.failedFuture(null),
3369 +            () -> CompletableFuture.failedStage(null),
3370          };
3371  
3372          assertThrows(NullPointerException.class, throwingActions);
# Line 3056 | Line 3374 | public class CompletableFutureTest exten
3374      }
3375  
3376      /**
3377 +     * Test submissions to an executor that rejects all tasks.
3378 +     */
3379 +    public void testRejectingExecutor() {
3380 +        for (Integer v : new Integer[] { 1, null })
3381 +    {
3382 +        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3383 +
3384 +        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3385 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3386 +
3387 +        List<CompletableFuture<?>> futures = new ArrayList<>();
3388 +
3389 +        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3390 +        srcs.add(complete);
3391 +        srcs.add(incomplete);
3392 +
3393 +        for (CompletableFuture<Integer> src : srcs) {
3394 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3395 +            fs.add(src.thenRunAsync(() -> {}, e));
3396 +            fs.add(src.thenAcceptAsync(z -> {}, e));
3397 +            fs.add(src.thenApplyAsync(z -> z, e));
3398 +
3399 +            fs.add(src.thenCombineAsync(src, (x, y) -> x, e));
3400 +            fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e));
3401 +            fs.add(src.runAfterBothAsync(src, () -> {}, e));
3402 +
3403 +            fs.add(src.applyToEitherAsync(src, z -> z, e));
3404 +            fs.add(src.acceptEitherAsync(src, z -> {}, e));
3405 +            fs.add(src.runAfterEitherAsync(src, () -> {}, e));
3406 +
3407 +            fs.add(src.thenComposeAsync(z -> null, e));
3408 +            fs.add(src.whenCompleteAsync((z, t) -> {}, e));
3409 +            fs.add(src.handleAsync((z, t) -> null, e));
3410 +
3411 +            for (CompletableFuture<?> future : fs) {
3412 +                if (src.isDone())
3413 +                    checkCompletedWithWrappedException(future, e.ex);
3414 +                else
3415 +                    checkIncomplete(future);
3416 +            }
3417 +            futures.addAll(fs);
3418 +        }
3419 +
3420 +        {
3421 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3422 +
3423 +            fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3424 +            fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3425 +
3426 +            fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3427 +            fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3428 +
3429 +            fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3430 +            fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3431 +
3432 +            for (CompletableFuture<?> future : fs)
3433 +                checkIncomplete(future);
3434 +            futures.addAll(fs);
3435 +        }
3436 +
3437 +        {
3438 +            List<CompletableFuture<?>> fs = new ArrayList<>();
3439 +
3440 +            fs.add(complete.applyToEitherAsync(incomplete, z -> z, e));
3441 +            fs.add(incomplete.applyToEitherAsync(complete, z -> z, e));
3442 +
3443 +            fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e));
3444 +            fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e));
3445 +
3446 +            fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e));
3447 +            fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e));
3448 +
3449 +            for (CompletableFuture<?> future : fs)
3450 +                checkCompletedWithWrappedException(future, e.ex);
3451 +            futures.addAll(fs);
3452 +        }
3453 +
3454 +        incomplete.complete(v);
3455 +
3456 +        for (CompletableFuture<?> future : futures)
3457 +            checkCompletedWithWrappedException(future, e.ex);
3458 +
3459 +        assertEquals(futures.size(), e.count.get());
3460 +    }}
3461 +
3462 +    /**
3463 +     * Test submissions to an executor that rejects all tasks, but
3464 +     * should never be invoked because the dependent future is
3465 +     * explicitly completed.
3466 +     */
3467 +    public void testRejectingExecutorNeverInvoked() {
3468 +        for (Integer v : new Integer[] { 1, null })
3469 +    {
3470 +        final CountingRejectingExecutor e = new CountingRejectingExecutor();
3471 +
3472 +        final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v);
3473 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3474 +
3475 +        List<CompletableFuture<?>> futures = new ArrayList<>();
3476 +
3477 +        List<CompletableFuture<Integer>> srcs = new ArrayList<>();
3478 +        srcs.add(complete);
3479 +        srcs.add(incomplete);
3480 +
3481 +        List<CompletableFuture<?>> fs = new ArrayList<>();
3482 +        fs.add(incomplete.thenRunAsync(() -> {}, e));
3483 +        fs.add(incomplete.thenAcceptAsync(z -> {}, e));
3484 +        fs.add(incomplete.thenApplyAsync(z -> z, e));
3485 +
3486 +        fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e));
3487 +        fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3488 +        fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e));
3489 +
3490 +        fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e));
3491 +        fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e));
3492 +        fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e));
3493 +
3494 +        fs.add(incomplete.thenComposeAsync(z -> null, e));
3495 +        fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e));
3496 +        fs.add(incomplete.handleAsync((z, t) -> null, e));
3497 +
3498 +        fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e));
3499 +        fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e));
3500 +
3501 +        fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e));
3502 +        fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e));
3503 +
3504 +        fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e));
3505 +        fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e));
3506 +
3507 +        for (CompletableFuture<?> future : fs)
3508 +            checkIncomplete(future);
3509 +
3510 +        for (CompletableFuture<?> future : fs)
3511 +            future.complete(null);
3512 +
3513 +        incomplete.complete(v);
3514 +
3515 +        for (CompletableFuture<?> future : fs)
3516 +            checkCompletedNormally(future, null);
3517 +
3518 +        assertEquals(0, e.count.get());
3519 +    }}
3520 +
3521 +    /**
3522       * toCompletableFuture returns this CompletableFuture.
3523       */
3524      public void testToCompletableFuture() {
# Line 3063 | Line 3526 | public class CompletableFutureTest exten
3526          assertSame(f, f.toCompletableFuture());
3527      }
3528  
3529 < //     public void testRunAfterEither_resultDeterminedAtTimeOfCreation() {
3530 < //         for (ExecutionMode m : ExecutionMode.values())
3531 < //         for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3532 < //         for (Integer v1 : new Integer[] { 1, null })
3533 < //     {
3534 < //         final CompletableFuture<Integer> f = new CompletableFuture<>();
3535 < //         final CompletableFuture<Integer> g = new CompletableFuture<>();
3536 < //         final Noop[] rs = new Noop[2];
3537 < //         for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
3538 < //         f.complete(v1);
3539 < //         final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
3540 < //         final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
3541 < //         assertTrue(g.cancel(mayInterruptIfRunning));
3542 < //         checkCompletedNormally(h0, null);
3543 < //         checkCompletedNormally(h1, null);
3544 < //         for (Noop r : rs) r.assertInvoked();
3545 < //     }}
3529 >    // jdk9
3530 >
3531 >    /**
3532 >     * newIncompleteFuture returns an incomplete CompletableFuture
3533 >     */
3534 >    public void testNewIncompleteFuture() {
3535 >        for (Integer v1 : new Integer[] { 1, null })
3536 >    {
3537 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3538 >        CompletableFuture<Integer> g = f.newIncompleteFuture();
3539 >        checkIncomplete(f);
3540 >        checkIncomplete(g);
3541 >        f.complete(v1);
3542 >        checkCompletedNormally(f, v1);
3543 >        checkIncomplete(g);
3544 >        g.complete(v1);
3545 >        checkCompletedNormally(g, v1);
3546 >        assertSame(g.getClass(), CompletableFuture.class);
3547 >    }}
3548 >
3549 >    /**
3550 >     * completedStage returns a completed CompletionStage
3551 >     */
3552 >    public void testCompletedStage() {
3553 >        AtomicInteger x = new AtomicInteger(0);
3554 >        AtomicReference<Throwable> r = new AtomicReference<>();
3555 >        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3556 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3557 >        assertEquals(x.get(), 1);
3558 >        assertNull(r.get());
3559 >    }
3560 >
3561 >    /**
3562 >     * defaultExecutor by default returns the commonPool if
3563 >     * it supports more than one thread.
3564 >     */
3565 >    public void testDefaultExecutor() {
3566 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3567 >        Executor e = f.defaultExecutor();
3568 >        Executor c = ForkJoinPool.commonPool();
3569 >        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3570 >            assertSame(e, c);
3571 >        else
3572 >            assertNotSame(e, c);
3573 >    }
3574 >
3575 >    /**
3576 >     * failedFuture returns a CompletableFuture completed
3577 >     * exceptionally with the given Exception
3578 >     */
3579 >    public void testFailedFuture() {
3580 >        CFException ex = new CFException();
3581 >        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3582 >        checkCompletedExceptionally(f, ex);
3583 >    }
3584 >
3585 >    /**
3586 >     * failedFuture(null) throws NPE
3587 >     */
3588 >    public void testFailedFuture_null() {
3589 >        try {
3590 >            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3591 >            shouldThrow();
3592 >        } catch (NullPointerException success) {}
3593 >    }
3594 >
3595 >    /**
3596 >     * copy returns a CompletableFuture that is completed normally,
3597 >     * with the same value, when source is.
3598 >     */
3599 >    public void testCopy_normalCompletion() {
3600 >        for (boolean createIncomplete : new boolean[] { true, false })
3601 >        for (Integer v1 : new Integer[] { 1, null })
3602 >    {
3603 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3604 >        if (!createIncomplete) assertTrue(f.complete(v1));
3605 >        CompletableFuture<Integer> g = f.copy();
3606 >        if (createIncomplete) {
3607 >            checkIncomplete(f);
3608 >            checkIncomplete(g);
3609 >            assertTrue(f.complete(v1));
3610 >        }
3611 >        checkCompletedNormally(f, v1);
3612 >        checkCompletedNormally(g, v1);
3613 >    }}
3614 >
3615 >    /**
3616 >     * copy returns a CompletableFuture that is completed exceptionally
3617 >     * when source is.
3618 >     */
3619 >    public void testCopy_exceptionalCompletion() {
3620 >        for (boolean createIncomplete : new boolean[] { true, false })
3621 >    {
3622 >        CFException ex = new CFException();
3623 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3624 >        if (!createIncomplete) f.completeExceptionally(ex);
3625 >        CompletableFuture<Integer> g = f.copy();
3626 >        if (createIncomplete) {
3627 >            checkIncomplete(f);
3628 >            checkIncomplete(g);
3629 >            f.completeExceptionally(ex);
3630 >        }
3631 >        checkCompletedExceptionally(f, ex);
3632 >        checkCompletedWithWrappedException(g, ex);
3633 >    }}
3634 >
3635 >    /**
3636 >     * Completion of a copy does not complete its source.
3637 >     */
3638 >    public void testCopy_oneWayPropagation() {
3639 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3640 >        assertTrue(f.copy().complete(1));
3641 >        assertTrue(f.copy().complete(null));
3642 >        assertTrue(f.copy().cancel(true));
3643 >        assertTrue(f.copy().cancel(false));
3644 >        assertTrue(f.copy().completeExceptionally(new CFException()));
3645 >        checkIncomplete(f);
3646 >    }
3647 >
3648 >    /**
3649 >     * minimalCompletionStage returns a CompletableFuture that is
3650 >     * completed normally, with the same value, when source is.
3651 >     */
3652 >    public void testMinimalCompletionStage() {
3653 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3654 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3655 >        AtomicInteger x = new AtomicInteger(0);
3656 >        AtomicReference<Throwable> r = new AtomicReference<>();
3657 >        checkIncomplete(f);
3658 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3659 >        f.complete(1);
3660 >        checkCompletedNormally(f, 1);
3661 >        assertEquals(x.get(), 1);
3662 >        assertNull(r.get());
3663 >    }
3664 >
3665 >    /**
3666 >     * minimalCompletionStage returns a CompletableFuture that is
3667 >     * completed exceptionally when source is.
3668 >     */
3669 >    public void testMinimalCompletionStage2() {
3670 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3671 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3672 >        AtomicInteger x = new AtomicInteger(0);
3673 >        AtomicReference<Throwable> r = new AtomicReference<>();
3674 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3675 >        checkIncomplete(f);
3676 >        CFException ex = new CFException();
3677 >        f.completeExceptionally(ex);
3678 >        checkCompletedExceptionally(f, ex);
3679 >        assertEquals(x.get(), 0);
3680 >        assertEquals(r.get().getCause(), ex);
3681 >    }
3682 >
3683 >    /**
3684 >     * failedStage returns a CompletionStage completed
3685 >     * exceptionally with the given Exception
3686 >     */
3687 >    public void testFailedStage() {
3688 >        CFException ex = new CFException();
3689 >        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3690 >        AtomicInteger x = new AtomicInteger(0);
3691 >        AtomicReference<Throwable> r = new AtomicReference<>();
3692 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3693 >        assertEquals(x.get(), 0);
3694 >        assertEquals(r.get(), ex);
3695 >    }
3696 >
3697 >    /**
3698 >     * completeAsync completes with value of given supplier
3699 >     */
3700 >    public void testCompleteAsync() {
3701 >        for (Integer v1 : new Integer[] { 1, null })
3702 >    {
3703 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3704 >        f.completeAsync(() -> v1);
3705 >        f.join();
3706 >        checkCompletedNormally(f, v1);
3707 >    }}
3708 >
3709 >    /**
3710 >     * completeAsync completes exceptionally if given supplier throws
3711 >     */
3712 >    public void testCompleteAsync2() {
3713 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3714 >        CFException ex = new CFException();
3715 >        f.completeAsync(() -> { throw ex; });
3716 >        try {
3717 >            f.join();
3718 >            shouldThrow();
3719 >        } catch (CompletionException success) {}
3720 >        checkCompletedWithWrappedException(f, ex);
3721 >    }
3722 >
3723 >    /**
3724 >     * completeAsync with given executor completes with value of given supplier
3725 >     */
3726 >    public void testCompleteAsync3() {
3727 >        for (Integer v1 : new Integer[] { 1, null })
3728 >    {
3729 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3730 >        ThreadExecutor executor = new ThreadExecutor();
3731 >        f.completeAsync(() -> v1, executor);
3732 >        assertSame(v1, f.join());
3733 >        checkCompletedNormally(f, v1);
3734 >        assertEquals(1, executor.count.get());
3735 >    }}
3736 >
3737 >    /**
3738 >     * completeAsync with given executor completes exceptionally if
3739 >     * given supplier throws
3740 >     */
3741 >    public void testCompleteAsync4() {
3742 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3743 >        CFException ex = new CFException();
3744 >        ThreadExecutor executor = new ThreadExecutor();
3745 >        f.completeAsync(() -> { throw ex; }, executor);
3746 >        try {
3747 >            f.join();
3748 >            shouldThrow();
3749 >        } catch (CompletionException success) {}
3750 >        checkCompletedWithWrappedException(f, ex);
3751 >        assertEquals(1, executor.count.get());
3752 >    }
3753 >
3754 >    /**
3755 >     * orTimeout completes with TimeoutException if not complete
3756 >     */
3757 >    public void testOrTimeout_timesOut() {
3758 >        long timeoutMillis = timeoutMillis();
3759 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3760 >        long startTime = System.nanoTime();
3761 >        assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS));
3762 >        checkCompletedWithTimeoutException(f);
3763 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3764 >    }
3765 >
3766 >    /**
3767 >     * orTimeout completes normally if completed before timeout
3768 >     */
3769 >    public void testOrTimeout_completed() {
3770 >        for (Integer v1 : new Integer[] { 1, null })
3771 >    {
3772 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3773 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3774 >        long startTime = System.nanoTime();
3775 >        f.complete(v1);
3776 >        assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3777 >        assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3778 >        g.complete(v1);
3779 >        checkCompletedNormally(f, v1);
3780 >        checkCompletedNormally(g, v1);
3781 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3782 >    }}
3783 >
3784 >    /**
3785 >     * completeOnTimeout completes with given value if not complete
3786 >     */
3787 >    public void testCompleteOnTimeout_timesOut() {
3788 >        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3789 >                       () -> testCompleteOnTimeout_timesOut(null));
3790 >    }
3791 >
3792 >    /**
3793 >     * completeOnTimeout completes with given value if not complete
3794 >     */
3795 >    public void testCompleteOnTimeout_timesOut(Integer v) {
3796 >        long timeoutMillis = timeoutMillis();
3797 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3798 >        long startTime = System.nanoTime();
3799 >        assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS));
3800 >        assertSame(v, f.join());
3801 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3802 >        f.complete(99);         // should have no effect
3803 >        checkCompletedNormally(f, v);
3804 >    }
3805 >
3806 >    /**
3807 >     * completeOnTimeout has no effect if completed within timeout
3808 >     */
3809 >    public void testCompleteOnTimeout_completed() {
3810 >        for (Integer v1 : new Integer[] { 1, null })
3811 >    {
3812 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3813 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3814 >        long startTime = System.nanoTime();
3815 >        f.complete(v1);
3816 >        assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3817 >        assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3818 >        g.complete(v1);
3819 >        checkCompletedNormally(f, v1);
3820 >        checkCompletedNormally(g, v1);
3821 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3822 >    }}
3823 >
3824 >    /**
3825 >     * delayedExecutor returns an executor that delays submission
3826 >     */
3827 >    public void testDelayedExecutor() {
3828 >        testInParallel(() -> testDelayedExecutor(null, null),
3829 >                       () -> testDelayedExecutor(null, 1),
3830 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3831 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3832 >    }
3833 >
3834 >    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3835 >        long timeoutMillis = timeoutMillis();
3836 >        // Use an "unreasonably long" long timeout to catch lingering threads
3837 >        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3838 >        final Executor delayer, longDelayer;
3839 >        if (executor == null) {
3840 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3841 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3842 >        } else {
3843 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3844 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3845 >        }
3846 >        long startTime = System.nanoTime();
3847 >        CompletableFuture<Integer> f =
3848 >            CompletableFuture.supplyAsync(() -> v, delayer);
3849 >        CompletableFuture<Integer> g =
3850 >            CompletableFuture.supplyAsync(() -> v, longDelayer);
3851 >
3852 >        assertNull(g.getNow(null));
3853 >
3854 >        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3855 >        long millisElapsed = millisElapsedSince(startTime);
3856 >        assertTrue(millisElapsed >= timeoutMillis);
3857 >        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3858 >
3859 >        checkCompletedNormally(f, v);
3860 >
3861 >        checkIncomplete(g);
3862 >        assertTrue(g.cancel(true));
3863 >    }
3864 >
3865 >    //--- tests of implementation details; not part of official tck ---
3866 >
3867 >    Object resultOf(CompletableFuture<?> f) {
3868 >        SecurityManager sm = System.getSecurityManager();
3869 >        if (sm != null) {
3870 >            try {
3871 >                System.setSecurityManager(null);
3872 >            } catch (SecurityException giveUp) {
3873 >                return "Reflection not available";
3874 >            }
3875 >        }
3876 >
3877 >        try {
3878 >            java.lang.reflect.Field resultField
3879 >                = CompletableFuture.class.getDeclaredField("result");
3880 >            resultField.setAccessible(true);
3881 >            return resultField.get(f);
3882 >        } catch (Throwable t) {
3883 >            throw new AssertionError(t);
3884 >        } finally {
3885 >            if (sm != null) System.setSecurityManager(sm);
3886 >        }
3887 >    }
3888 >
3889 >    public void testExceptionPropagationReusesResultObject() {
3890 >        if (!testImplementationDetails) return;
3891 >        for (ExecutionMode m : ExecutionMode.values())
3892 >    {
3893 >        final CFException ex = new CFException();
3894 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3895 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3896 >
3897 >        final Runnable noopRunnable = new Noop(m);
3898 >        final Consumer<Integer> noopConsumer = new NoopConsumer(m);
3899 >        final Function<Integer, Integer> incFunction = new IncFunction(m);
3900 >
3901 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3902 >            = new ArrayList<>();
3903 >
3904 >        funs.add(y -> m.thenRun(y, noopRunnable));
3905 >        funs.add(y -> m.thenAccept(y, noopConsumer));
3906 >        funs.add(y -> m.thenApply(y, incFunction));
3907 >
3908 >        funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable));
3909 >        funs.add(y -> m.acceptEither(y, incomplete, noopConsumer));
3910 >        funs.add(y -> m.applyToEither(y, incomplete, incFunction));
3911 >
3912 >        funs.add(y -> m.runAfterBoth(y, v42, noopRunnable));
3913 >        funs.add(y -> m.runAfterBoth(v42, y, noopRunnable));
3914 >        funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3915 >        funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3916 >        funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m)));
3917 >        funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m)));
3918 >
3919 >        funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3920 >
3921 >        funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m)));
3922 >
3923 >        funs.add(y -> CompletableFuture.allOf(y));
3924 >        funs.add(y -> CompletableFuture.allOf(y, v42));
3925 >        funs.add(y -> CompletableFuture.allOf(v42, y));
3926 >        funs.add(y -> CompletableFuture.anyOf(y));
3927 >        funs.add(y -> CompletableFuture.anyOf(y, incomplete));
3928 >        funs.add(y -> CompletableFuture.anyOf(incomplete, y));
3929 >
3930 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3931 >                 fun : funs) {
3932 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3933 >            f.completeExceptionally(ex);
3934 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3935 >            checkCompletedWithWrappedException(src, ex);
3936 >            CompletableFuture<?> dep = fun.apply(src);
3937 >            checkCompletedWithWrappedException(dep, ex);
3938 >            assertSame(resultOf(src), resultOf(dep));
3939 >        }
3940 >
3941 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3942 >                 fun : funs) {
3943 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3944 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3945 >            CompletableFuture<?> dep = fun.apply(src);
3946 >            f.completeExceptionally(ex);
3947 >            checkCompletedWithWrappedException(src, ex);
3948 >            checkCompletedWithWrappedException(dep, ex);
3949 >            assertSame(resultOf(src), resultOf(dep));
3950 >        }
3951 >
3952 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3953 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3954 >                 fun : funs) {
3955 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3956 >            f.cancel(mayInterruptIfRunning);
3957 >            checkCancelled(f);
3958 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3959 >            checkCompletedWithWrappedCancellationException(src);
3960 >            CompletableFuture<?> dep = fun.apply(src);
3961 >            checkCompletedWithWrappedCancellationException(dep);
3962 >            assertSame(resultOf(src), resultOf(dep));
3963 >        }
3964 >
3965 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3966 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3967 >                 fun : funs) {
3968 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3969 >            CompletableFuture<Integer> src = m.thenApply(f, incFunction);
3970 >            CompletableFuture<?> dep = fun.apply(src);
3971 >            f.cancel(mayInterruptIfRunning);
3972 >            checkCancelled(f);
3973 >            checkCompletedWithWrappedCancellationException(src);
3974 >            checkCompletedWithWrappedCancellationException(dep);
3975 >            assertSame(resultOf(src), resultOf(dep));
3976 >        }
3977 >    }}
3978 >
3979 >    /**
3980 >     * Minimal completion stages throw UOE for most non-CompletionStage methods
3981 >     */
3982 >    public void testMinimalCompletionStage_minimality() {
3983 >        if (!testImplementationDetails) return;
3984 >        Function<Method, String> toSignature =
3985 >            method -> method.getName() + Arrays.toString(method.getParameterTypes());
3986 >        Predicate<Method> isNotStatic =
3987 >            method -> (method.getModifiers() & Modifier.STATIC) == 0;
3988 >        List<Method> minimalMethods =
3989 >            Stream.of(Object.class, CompletionStage.class)
3990 >            .flatMap(klazz -> Stream.of(klazz.getMethods()))
3991 >            .filter(isNotStatic)
3992 >            .collect(Collectors.toList());
3993 >        // Methods from CompletableFuture permitted NOT to throw UOE
3994 >        String[] signatureWhitelist = {
3995 >            "newIncompleteFuture[]",
3996 >            "defaultExecutor[]",
3997 >            "minimalCompletionStage[]",
3998 >            "copy[]",
3999 >        };
4000 >        Set<String> permittedMethodSignatures =
4001 >            Stream.concat(minimalMethods.stream().map(toSignature),
4002 >                          Stream.of(signatureWhitelist))
4003 >            .collect(Collectors.toSet());
4004 >        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
4005 >            .filter(isNotStatic)
4006 >            .filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method)))
4007 >            .collect(Collectors.toList());
4008 >
4009 >        List<CompletionStage<Integer>> stages = new ArrayList<>();
4010 >        CompletionStage<Integer> min =
4011 >            new CompletableFuture<Integer>().minimalCompletionStage();
4012 >        stages.add(min);
4013 >        stages.add(min.thenApply(x -> x));
4014 >        stages.add(CompletableFuture.completedStage(1));
4015 >        stages.add(CompletableFuture.failedStage(new CFException()));
4016 >
4017 >        List<Method> bugs = new ArrayList<>();
4018 >        for (Method method : allMethods) {
4019 >            Class<?>[] parameterTypes = method.getParameterTypes();
4020 >            Object[] args = new Object[parameterTypes.length];
4021 >            // Manufacture boxed primitives for primitive params
4022 >            for (int i = 0; i < args.length; i++) {
4023 >                Class<?> type = parameterTypes[i];
4024 >                if (parameterTypes[i] == boolean.class)
4025 >                    args[i] = false;
4026 >                else if (parameterTypes[i] == int.class)
4027 >                    args[i] = 0;
4028 >                else if (parameterTypes[i] == long.class)
4029 >                    args[i] = 0L;
4030 >            }
4031 >            for (CompletionStage<Integer> stage : stages) {
4032 >                try {
4033 >                    method.invoke(stage, args);
4034 >                    bugs.add(method);
4035 >                }
4036 >                catch (java.lang.reflect.InvocationTargetException expected) {
4037 >                    if (! (expected.getCause() instanceof UnsupportedOperationException)) {
4038 >                        bugs.add(method);
4039 >                        // expected.getCause().printStackTrace();
4040 >                    }
4041 >                }
4042 >                catch (ReflectiveOperationException bad) { throw new Error(bad); }
4043 >            }
4044 >        }
4045 >        if (!bugs.isEmpty())
4046 >            throw new Error("Methods did not throw UOE: " + bugs);
4047 >    }
4048 >
4049 >    /**
4050 >     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4051 >     * is completed normally, with the same value, when source is.
4052 >     */
4053 >    public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() {
4054 >        for (boolean createIncomplete : new boolean[] { true, false })
4055 >        for (Integer v1 : new Integer[] { 1, null })
4056 >    {
4057 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4058 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4059 >        if (!createIncomplete) assertTrue(f.complete(v1));
4060 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4061 >        if (createIncomplete) {
4062 >            checkIncomplete(f);
4063 >            checkIncomplete(g);
4064 >            assertTrue(f.complete(v1));
4065 >        }
4066 >        checkCompletedNormally(f, v1);
4067 >        checkCompletedNormally(g, v1);
4068 >    }}
4069 >
4070 >    /**
4071 >     * minimalStage.toCompletableFuture() returns a CompletableFuture that
4072 >     * is completed exceptionally when source is.
4073 >     */
4074 >    public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() {
4075 >        for (boolean createIncomplete : new boolean[] { true, false })
4076 >    {
4077 >        CFException ex = new CFException();
4078 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4079 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4080 >        if (!createIncomplete) f.completeExceptionally(ex);
4081 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4082 >        if (createIncomplete) {
4083 >            checkIncomplete(f);
4084 >            checkIncomplete(g);
4085 >            f.completeExceptionally(ex);
4086 >        }
4087 >        checkCompletedExceptionally(f, ex);
4088 >        checkCompletedWithWrappedException(g, ex);
4089 >    }}
4090 >
4091 >    /**
4092 >     * minimalStage.toCompletableFuture() gives mutable CompletableFuture
4093 >     */
4094 >    public void testMinimalCompletionStage_toCompletableFuture_mutable() {
4095 >        for (Integer v1 : new Integer[] { 1, null })
4096 >    {
4097 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4098 >        CompletionStage minimal = f.minimalCompletionStage();
4099 >        CompletableFuture<Integer> g = minimal.toCompletableFuture();
4100 >        assertTrue(g.complete(v1));
4101 >        checkCompletedNormally(g, v1);
4102 >        checkIncomplete(f);
4103 >        checkIncomplete(minimal.toCompletableFuture());
4104 >    }}
4105 >
4106 >    /**
4107 >     * minimalStage.toCompletableFuture().join() awaits completion
4108 >     */
4109 >    public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception {
4110 >        for (boolean createIncomplete : new boolean[] { true, false })
4111 >        for (Integer v1 : new Integer[] { 1, null })
4112 >    {
4113 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4114 >        if (!createIncomplete) assertTrue(f.complete(v1));
4115 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4116 >        if (createIncomplete) assertTrue(f.complete(v1));
4117 >        assertEquals(v1, minimal.toCompletableFuture().join());
4118 >        assertEquals(v1, minimal.toCompletableFuture().get());
4119 >        checkCompletedNormally(minimal.toCompletableFuture(), v1);
4120 >    }}
4121 >
4122 >    /**
4123 >     * Completion of a toCompletableFuture copy of a minimal stage
4124 >     * does not complete its source.
4125 >     */
4126 >    public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() {
4127 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4128 >        CompletionStage<Integer> g = f.minimalCompletionStage();
4129 >        assertTrue(g.toCompletableFuture().complete(1));
4130 >        assertTrue(g.toCompletableFuture().complete(null));
4131 >        assertTrue(g.toCompletableFuture().cancel(true));
4132 >        assertTrue(g.toCompletableFuture().cancel(false));
4133 >        assertTrue(g.toCompletableFuture().completeExceptionally(new CFException()));
4134 >        checkIncomplete(g.toCompletableFuture());
4135 >        f.complete(1);
4136 >        checkCompletedNormally(g.toCompletableFuture(), 1);
4137 >    }
4138 >
4139 >    /** Demo utility method for external reliable toCompletableFuture */
4140 >    static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) {
4141 >        CompletableFuture<T> f = new CompletableFuture<>();
4142 >        stage.handle((T t, Throwable ex) -> {
4143 >                         if (ex != null) f.completeExceptionally(ex);
4144 >                         else f.complete(t);
4145 >                         return null;
4146 >                     });
4147 >        return f;
4148 >    }
4149 >
4150 >    /** Demo utility method to join a CompletionStage */
4151 >    static <T> T join(CompletionStage<T> stage) {
4152 >        return toCompletableFuture(stage).join();
4153 >    }
4154 >
4155 >    /**
4156 >     * Joining a minimal stage "by hand" works
4157 >     */
4158 >    public void testMinimalCompletionStage_join_by_hand() {
4159 >        for (boolean createIncomplete : new boolean[] { true, false })
4160 >        for (Integer v1 : new Integer[] { 1, null })
4161 >    {
4162 >        CompletableFuture<Integer> f = new CompletableFuture<>();
4163 >        CompletionStage<Integer> minimal = f.minimalCompletionStage();
4164 >        CompletableFuture<Integer> g = new CompletableFuture<>();
4165 >        if (!createIncomplete) assertTrue(f.complete(v1));
4166 >        minimal.thenAccept(x -> g.complete(x));
4167 >        if (createIncomplete) assertTrue(f.complete(v1));
4168 >        g.join();
4169 >        checkCompletedNormally(g, v1);
4170 >        checkCompletedNormally(f, v1);
4171 >        assertEquals(v1, join(minimal));
4172 >    }}
4173 >
4174 >    static class Monad {
4175 >        static class ZeroException extends RuntimeException {
4176 >            public ZeroException() { super("monadic zero"); }
4177 >        }
4178 >        // "return", "unit"
4179 >        static <T> CompletableFuture<T> unit(T value) {
4180 >            return completedFuture(value);
4181 >        }
4182 >        // monadic zero ?
4183 >        static <T> CompletableFuture<T> zero() {
4184 >            return failedFuture(new ZeroException());
4185 >        }
4186 >        // >=>
4187 >        static <T,U,V> Function<T, CompletableFuture<V>> compose
4188 >            (Function<T, CompletableFuture<U>> f,
4189 >             Function<U, CompletableFuture<V>> g) {
4190 >            return x -> f.apply(x).thenCompose(g);
4191 >        }
4192 >
4193 >        static void assertZero(CompletableFuture<?> f) {
4194 >            try {
4195 >                f.getNow(null);
4196 >                throw new AssertionError("should throw");
4197 >            } catch (CompletionException success) {
4198 >                assertTrue(success.getCause() instanceof ZeroException);
4199 >            }
4200 >        }
4201 >
4202 >        static <T> void assertFutureEquals(CompletableFuture<T> f,
4203 >                                           CompletableFuture<T> g) {
4204 >            T fval = null, gval = null;
4205 >            Throwable fex = null, gex = null;
4206 >
4207 >            try { fval = f.get(); }
4208 >            catch (ExecutionException ex) { fex = ex.getCause(); }
4209 >            catch (Throwable ex) { fex = ex; }
4210 >
4211 >            try { gval = g.get(); }
4212 >            catch (ExecutionException ex) { gex = ex.getCause(); }
4213 >            catch (Throwable ex) { gex = ex; }
4214 >
4215 >            if (fex != null || gex != null)
4216 >                assertSame(fex.getClass(), gex.getClass());
4217 >            else
4218 >                assertEquals(fval, gval);
4219 >        }
4220 >
4221 >        static class PlusFuture<T> extends CompletableFuture<T> {
4222 >            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
4223 >        }
4224 >
4225 >        /** Implements "monadic plus". */
4226 >        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
4227 >                                             CompletableFuture<? extends T> g) {
4228 >            PlusFuture<T> plus = new PlusFuture<T>();
4229 >            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
4230 >                try {
4231 >                    if (ex == null) {
4232 >                        if (plus.complete(result))
4233 >                            if (plus.firstFailure.get() != null)
4234 >                                plus.firstFailure.set(null);
4235 >                    }
4236 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
4237 >                        if (plus.isDone())
4238 >                            plus.firstFailure.set(null);
4239 >                    }
4240 >                    else {
4241 >                        // first failure has precedence
4242 >                        Throwable first = plus.firstFailure.getAndSet(null);
4243 >
4244 >                        // may fail with "Self-suppression not permitted"
4245 >                        try { first.addSuppressed(ex); }
4246 >                        catch (Exception ignored) {}
4247 >
4248 >                        plus.completeExceptionally(first);
4249 >                    }
4250 >                } catch (Throwable unexpected) {
4251 >                    plus.completeExceptionally(unexpected);
4252 >                }
4253 >            };
4254 >            f.whenComplete(action);
4255 >            g.whenComplete(action);
4256 >            return plus;
4257 >        }
4258 >    }
4259 >
4260 >    /**
4261 >     * CompletableFuture is an additive monad - sort of.
4262 >     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
4263 >     */
4264 >    public void testAdditiveMonad() throws Throwable {
4265 >        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
4266 >        CompletableFuture<Long> zero = Monad.zero();
4267 >
4268 >        // Some mutually non-commutative functions
4269 >        Function<Long, CompletableFuture<Long>> triple
4270 >            = x -> Monad.unit(3 * x);
4271 >        Function<Long, CompletableFuture<Long>> inc
4272 >            = x -> Monad.unit(x + 1);
4273 >
4274 >        // unit is a right identity: m >>= unit === m
4275 >        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
4276 >                                 inc.apply(5L));
4277 >        // unit is a left identity: (unit x) >>= f === f x
4278 >        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
4279 >                                 inc.apply(5L));
4280 >
4281 >        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
4282 >        Monad.assertFutureEquals(
4283 >            unit.apply(5L).thenCompose(inc).thenCompose(triple),
4284 >            unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple)));
4285 >
4286 >        // The case for CompletableFuture as an additive monad is weaker...
4287 >
4288 >        // zero is a monadic zero
4289 >        Monad.assertZero(zero);
4290 >
4291 >        // left zero: zero >>= f === zero
4292 >        Monad.assertZero(zero.thenCompose(inc));
4293 >        // right zero: f >>= (\x -> zero) === zero
4294 >        Monad.assertZero(inc.apply(5L).thenCompose(x -> zero));
4295 >
4296 >        // f plus zero === f
4297 >        Monad.assertFutureEquals(Monad.unit(5L),
4298 >                                 Monad.plus(Monad.unit(5L), zero));
4299 >        // zero plus f === f
4300 >        Monad.assertFutureEquals(Monad.unit(5L),
4301 >                                 Monad.plus(zero, Monad.unit(5L)));
4302 >        // zero plus zero === zero
4303 >        Monad.assertZero(Monad.plus(zero, zero));
4304 >        {
4305 >            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
4306 >                                                   Monad.unit(8L));
4307 >            // non-determinism
4308 >            assertTrue(f.get() == 5L || f.get() == 8L);
4309 >        }
4310 >
4311 >        CompletableFuture<Long> godot = new CompletableFuture<>();
4312 >        // f plus godot === f (doesn't wait for godot)
4313 >        Monad.assertFutureEquals(Monad.unit(5L),
4314 >                                 Monad.plus(Monad.unit(5L), godot));
4315 >        // godot plus f === f (doesn't wait for godot)
4316 >        Monad.assertFutureEquals(Monad.unit(5L),
4317 >                                 Monad.plus(godot, Monad.unit(5L)));
4318 >    }
4319 >
4320 >    /** Test long recursive chains of CompletableFutures with cascading completions */
4321 >    @SuppressWarnings("FutureReturnValueIgnored")
4322 >    public void testRecursiveChains() throws Throwable {
4323 >        for (ExecutionMode m : ExecutionMode.values())
4324 >        for (boolean addDeadEnds : new boolean[] { true, false })
4325 >    {
4326 >        final int val = 42;
4327 >        final int n = expensiveTests ? 1_000 : 2;
4328 >        CompletableFuture<Integer> head = new CompletableFuture<>();
4329 >        CompletableFuture<Integer> tail = head;
4330 >        for (int i = 0; i < n; i++) {
4331 >            if (addDeadEnds) m.thenApply(tail, v -> v + 1);
4332 >            tail = m.thenApply(tail, v -> v + 1);
4333 >            if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1);
4334 >            tail = m.applyToEither(tail, tail, v -> v + 1);
4335 >            if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1);
4336 >            tail = m.thenCombine(tail, tail, (v, w) -> v + 1);
4337 >        }
4338 >        head.complete(val);
4339 >        assertEquals(val + 3 * n, (int) tail.join());
4340 >    }}
4341 >
4342 >    /**
4343 >     * A single CompletableFuture with many dependents.
4344 >     * A demo of scalability - runtime is O(n).
4345 >     */
4346 >    @SuppressWarnings("FutureReturnValueIgnored")
4347 >    public void testManyDependents() throws Throwable {
4348 >        final int n = expensiveTests ? 1_000_000 : 10;
4349 >        final CompletableFuture<Void> head = new CompletableFuture<>();
4350 >        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
4351 >        final AtomicInteger count = new AtomicInteger(0);
4352 >        for (int i = 0; i < n; i++) {
4353 >            head.thenRun(() -> count.getAndIncrement());
4354 >            head.thenAccept(x -> count.getAndIncrement());
4355 >            head.thenApply(x -> count.getAndIncrement());
4356 >
4357 >            head.runAfterBoth(complete, () -> count.getAndIncrement());
4358 >            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
4359 >            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
4360 >            complete.runAfterBoth(head, () -> count.getAndIncrement());
4361 >            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
4362 >            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
4363 >
4364 >            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
4365 >            head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4366 >            head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement());
4367 >            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
4368 >            new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement());
4369 >            new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement());
4370 >        }
4371 >        head.complete(null);
4372 >        assertEquals(5 * 3 * n, count.get());
4373 >    }
4374 >
4375 >    /** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */
4376 >    @SuppressWarnings("FutureReturnValueIgnored")
4377 >    public void testCoCompletionGarbageRetention() throws Throwable {
4378 >        final int n = expensiveTests ? 1_000_000 : 10;
4379 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
4380 >        CompletableFuture<Integer> f;
4381 >        for (int i = 0; i < n; i++) {
4382 >            f = new CompletableFuture<>();
4383 >            f.runAfterEither(incomplete, () -> {});
4384 >            f.complete(null);
4385 >
4386 >            f = new CompletableFuture<>();
4387 >            f.acceptEither(incomplete, x -> {});
4388 >            f.complete(null);
4389 >
4390 >            f = new CompletableFuture<>();
4391 >            f.applyToEither(incomplete, x -> x);
4392 >            f.complete(null);
4393 >
4394 >            f = new CompletableFuture<>();
4395 >            CompletableFuture.anyOf(f, incomplete);
4396 >            f.complete(null);
4397 >        }
4398 >
4399 >        for (int i = 0; i < n; i++) {
4400 >            f = new CompletableFuture<>();
4401 >            incomplete.runAfterEither(f, () -> {});
4402 >            f.complete(null);
4403 >
4404 >            f = new CompletableFuture<>();
4405 >            incomplete.acceptEither(f, x -> {});
4406 >            f.complete(null);
4407 >
4408 >            f = new CompletableFuture<>();
4409 >            incomplete.applyToEither(f, x -> x);
4410 >            f.complete(null);
4411 >
4412 >            f = new CompletableFuture<>();
4413 >            CompletableFuture.anyOf(incomplete, f);
4414 >            f.complete(null);
4415 >        }
4416 >    }
4417 >
4418 >    /**
4419 >     * Reproduction recipe for:
4420 >     * 8160402: Garbage retention with CompletableFuture.anyOf
4421 >     * 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
4422 >     */
4423 >    public void testAnyOfGarbageRetention() throws Throwable {
4424 >        for (Integer v : new Integer[] { 1, null })
4425 >    {
4426 >        final int n = expensiveTests ? 100_000 : 10;
4427 >        CompletableFuture<Integer>[] fs
4428 >            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4429 >        for (int i = 0; i < fs.length; i++)
4430 >            fs[i] = new CompletableFuture<>();
4431 >        fs[fs.length - 1].complete(v);
4432 >        for (int i = 0; i < n; i++)
4433 >            checkCompletedNormally(CompletableFuture.anyOf(fs), v);
4434 >    }}
4435 >
4436 >    /**
4437 >     * Checks for garbage retention with allOf.
4438 >     *
4439 >     * As of 2016-07, fails with OOME:
4440 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck
4441 >     */
4442 >    public void testCancelledAllOfGarbageRetention() throws Throwable {
4443 >        final int n = expensiveTests ? 100_000 : 10;
4444 >        CompletableFuture<Integer>[] fs
4445 >            = (CompletableFuture<Integer>[]) new CompletableFuture<?>[100];
4446 >        for (int i = 0; i < fs.length; i++)
4447 >            fs[i] = new CompletableFuture<>();
4448 >        for (int i = 0; i < n; i++)
4449 >            assertTrue(CompletableFuture.allOf(fs).cancel(false));
4450 >    }
4451 >
4452 >    /**
4453 >     * Checks for garbage retention when a dependent future is
4454 >     * cancelled and garbage-collected.
4455 >     * 8161600: Garbage retention when source CompletableFutures are never completed
4456 >     *
4457 >     * As of 2016-07, fails with OOME:
4458 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck
4459 >     */
4460 >    public void testCancelledGarbageRetention() throws Throwable {
4461 >        final int n = expensiveTests ? 100_000 : 10;
4462 >        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4463 >        for (int i = 0; i < n; i++)
4464 >            assertTrue(neverCompleted.thenRun(() -> {}).cancel(true));
4465 >    }
4466 >
4467 >    /**
4468 >     * Checks for garbage retention when MinimalStage.toCompletableFuture()
4469 >     * is invoked many times.
4470 >     * 8161600: Garbage retention when source CompletableFutures are never completed
4471 >     *
4472 >     * As of 2016-07, fails with OOME:
4473 >     * ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck
4474 >     */
4475 >    public void testToCompletableFutureGarbageRetention() throws Throwable {
4476 >        final int n = expensiveTests ? 900_000 : 10;
4477 >        CompletableFuture<Integer> neverCompleted = new CompletableFuture<>();
4478 >        CompletionStage minimal = neverCompleted.minimalCompletionStage();
4479 >        for (int i = 0; i < n; i++)
4480 >            assertTrue(minimal.toCompletableFuture().cancel(true));
4481 >    }
4482 >
4483 > //     static <U> U join(CompletionStage<U> stage) {
4484 > //         CompletableFuture<U> f = new CompletableFuture<>();
4485 > //         stage.whenComplete((v, ex) -> {
4486 > //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
4487 > //         });
4488 > //         return f.join();
4489 > //     }
4490 >
4491 > //     static <U> boolean isDone(CompletionStage<U> stage) {
4492 > //         CompletableFuture<U> f = new CompletableFuture<>();
4493 > //         stage.whenComplete((v, ex) -> {
4494 > //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
4495 > //         });
4496 > //         return f.isDone();
4497 > //     }
4498 >
4499 > //     static <U> U join2(CompletionStage<U> stage) {
4500 > //         return stage.toCompletableFuture().copy().join();
4501 > //     }
4502 >
4503 > //     static <U> boolean isDone2(CompletionStage<U> stage) {
4504 > //         return stage.toCompletableFuture().copy().isDone();
4505 > //     }
4506  
4507   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines