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.75 by jsr166, Sat Jun 7 21:14:42 2014 UTC vs.
Revision 1.142 by dl, Fri Apr 1 23:18:00 2016 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.TimeoutException;
34 + import java.util.concurrent.TimeUnit;
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.AssertionFailedError;
45 + import junit.framework.Test;
46 + import junit.framework.TestSuite;
47  
48   public class CompletableFutureTest extends JSR166TestCase {
49  
50      public static void main(String[] args) {
51 <        junit.textui.TestRunner.run(suite());
51 >        main(suite(), args);
52      }
53      public static Test suite() {
54          return new TestSuite(CompletableFutureTest.class);
# Line 44 | Line 59 | public class CompletableFutureTest exten
59      void checkIncomplete(CompletableFuture<?> f) {
60          assertFalse(f.isDone());
61          assertFalse(f.isCancelled());
62 <        assertTrue(f.toString().contains("[Not completed]"));
62 >        assertTrue(f.toString().contains("Not completed"));
63          try {
64              assertNull(f.getNow(null));
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 57 | Line 72 | public class CompletableFutureTest exten
72      }
73  
74      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
75 <        try {
76 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
75 >        checkTimedGet(f, value);
76 >
77          try {
78              assertEquals(value, f.join());
79          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 75 | Line 89 | public class CompletableFutureTest exten
89          assertTrue(f.toString().contains("[Completed normally]"));
90      }
91  
92 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
93 <        try {
94 <            f.get(LONG_DELAY_MS, MILLISECONDS);
95 <            shouldThrow();
96 <        } catch (ExecutionException success) {
97 <            assertTrue(success.getCause() instanceof CFException);
98 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
99 <        try {
100 <            f.join();
101 <            shouldThrow();
102 <        } catch (CompletionException success) {
103 <            assertTrue(success.getCause() instanceof CFException);
104 <        }
105 <        try {
106 <            f.getNow(null);
107 <            shouldThrow();
108 <        } catch (CompletionException success) {
95 <            assertTrue(success.getCause() instanceof CFException);
92 >    /**
93 >     * Returns the "raw" internal exceptional completion of f,
94 >     * without any additional wrapping with CompletionException.
95 >     */
96 >    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
97 >        // handle (and whenComplete) can distinguish between "direct"
98 >        // and "wrapped" exceptional completion
99 >        return f.handle((U u, Throwable t) -> t).join();
100 >    }
101 >
102 >    void checkCompletedExceptionally(CompletableFuture<?> f,
103 >                                     boolean wrapped,
104 >                                     Consumer<Throwable> checker) {
105 >        Throwable cause = exceptionalCompletion(f);
106 >        if (wrapped) {
107 >            assertTrue(cause instanceof CompletionException);
108 >            cause = cause.getCause();
109          }
110 <        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]"));
106 <    }
110 >        checker.accept(cause);
111  
112 <    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 <                                                      Throwable ex) {
112 >        long startTime = System.nanoTime();
113          try {
114              f.get(LONG_DELAY_MS, MILLISECONDS);
115              shouldThrow();
116          } catch (ExecutionException success) {
117 <            assertSame(ex, success.getCause());
117 >            assertSame(cause, success.getCause());
118          } catch (Throwable fail) { threadUnexpectedException(fail); }
119 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
120 +
121          try {
122              f.join();
123              shouldThrow();
124          } catch (CompletionException success) {
125 <            assertSame(ex, success.getCause());
126 <        }
125 >            assertSame(cause, success.getCause());
126 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
127 >
128          try {
129              f.getNow(null);
130              shouldThrow();
131          } catch (CompletionException success) {
132 <            assertSame(ex, success.getCause());
133 <        }
132 >            assertSame(cause, success.getCause());
133 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
134 >
135          try {
136              f.get();
137              shouldThrow();
138          } catch (ExecutionException success) {
139 <            assertSame(ex, success.getCause());
139 >            assertSame(cause, success.getCause());
140          } catch (Throwable fail) { threadUnexpectedException(fail); }
141  
135        assertTrue(f.isDone());
142          assertFalse(f.isCancelled());
143 +        assertTrue(f.isDone());
144 +        assertTrue(f.isCompletedExceptionally());
145          assertTrue(f.toString().contains("[Completed exceptionally]"));
146      }
147  
148 <    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
149 <                                                Throwable ex) {
150 <        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); }
148 >    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
149 >        checkCompletedExceptionally(f, true,
150 >            (t) -> assertTrue(t instanceof CFException));
151      }
152  
153 <    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
154 <        checkCompletedExceptionallyWithRootCause(f, ex);
155 <        try {
156 <            CompletableFuture<Throwable> spy = f.handle
157 <                ((U u, Throwable t) -> t);
158 <            assertSame(ex, spy.join());
159 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
153 >    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
154 >        checkCompletedExceptionally(f, true,
155 >            (t) -> assertTrue(t instanceof CancellationException));
156 >    }
157 >
158 >    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
159 >        checkCompletedExceptionally(f, false,
160 >            (t) -> assertTrue(t instanceof TimeoutException));
161 >    }
162 >
163 >    void checkCompletedWithWrappedException(CompletableFuture<?> f,
164 >                                            Throwable ex) {
165 >        checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
166 >    }
167 >
168 >    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
169 >        checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
170      }
171  
172      void checkCancelled(CompletableFuture<?> f) {
173 +        long startTime = System.nanoTime();
174          try {
175              f.get(LONG_DELAY_MS, MILLISECONDS);
176              shouldThrow();
177          } catch (CancellationException success) {
178          } catch (Throwable fail) { threadUnexpectedException(fail); }
179 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
180 +
181          try {
182              f.join();
183              shouldThrow();
# Line 176 | Line 191 | public class CompletableFutureTest exten
191              shouldThrow();
192          } catch (CancellationException success) {
193          } 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    }
194  
195 <    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
196 <        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); }
195 >        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
196 >
197          assertTrue(f.isDone());
211        assertFalse(f.isCancelled());
198          assertTrue(f.isCompletedExceptionally());
199 +        assertTrue(f.isCancelled());
200          assertTrue(f.toString().contains("[Completed exceptionally]"));
201      }
202  
# Line 227 | Line 214 | public class CompletableFutureTest exten
214       * isCancelled, join, get, and getNow
215       */
216      public void testComplete() {
217 +        for (Integer v1 : new Integer[] { 1, null })
218 +    {
219          CompletableFuture<Integer> f = new CompletableFuture<>();
220          checkIncomplete(f);
221 <        f.complete(one);
222 <        checkCompletedNormally(f, one);
223 <    }
221 >        assertTrue(f.complete(v1));
222 >        assertFalse(f.complete(v1));
223 >        checkCompletedNormally(f, v1);
224 >    }}
225  
226      /**
227       * completeExceptionally completes exceptionally, as indicated by
# Line 250 | Line 240 | public class CompletableFutureTest exten
240       * methods isDone, isCancelled, join, get, and getNow
241       */
242      public void testCancel() {
243 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
244 +    {
245          CompletableFuture<Integer> f = new CompletableFuture<>();
246          checkIncomplete(f);
247 <        assertTrue(f.cancel(true));
247 >        assertTrue(f.cancel(mayInterruptIfRunning));
248 >        assertTrue(f.cancel(mayInterruptIfRunning));
249 >        assertTrue(f.cancel(!mayInterruptIfRunning));
250          checkCancelled(f);
251 <    }
251 >    }}
252  
253      /**
254       * obtrudeValue forces completion with given value
# Line 262 | Line 256 | public class CompletableFutureTest exten
256      public void testObtrudeValue() {
257          CompletableFuture<Integer> f = new CompletableFuture<>();
258          checkIncomplete(f);
259 <        f.complete(one);
259 >        assertTrue(f.complete(one));
260          checkCompletedNormally(f, one);
261          f.obtrudeValue(three);
262          checkCompletedNormally(f, three);
# Line 289 | Line 283 | public class CompletableFutureTest exten
283          CompletableFuture<Integer> f;
284  
285          f = new CompletableFuture<>();
286 <        f.complete(v1);
286 >        assertTrue(f.complete(v1));
287          for (int i = 0; i < 2; i++) {
288              f.obtrudeException(ex = new CFException());
289              checkCompletedExceptionally(f, ex);
# Line 309 | Line 303 | public class CompletableFutureTest exten
303          checkCompletedExceptionally(f, ex);
304          f.completeExceptionally(new CFException());
305          checkCompletedExceptionally(f, ex);
306 <        f.complete(v1);
306 >        assertFalse(f.complete(v1));
307          checkCompletedExceptionally(f, ex);
308      }}
309  
# Line 317 | Line 311 | public class CompletableFutureTest exten
311       * getNumberOfDependents returns number of dependent tasks
312       */
313      public void testGetNumberOfDependents() {
314 +        for (ExecutionMode m : ExecutionMode.values())
315 +        for (Integer v1 : new Integer[] { 1, null })
316 +    {
317          CompletableFuture<Integer> f = new CompletableFuture<>();
318          assertEquals(0, f.getNumberOfDependents());
319 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
319 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
320          assertEquals(1, f.getNumberOfDependents());
321          assertEquals(0, g.getNumberOfDependents());
322 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
322 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
323          assertEquals(2, f.getNumberOfDependents());
324 <        f.complete(1);
324 >        assertEquals(0, h.getNumberOfDependents());
325 >        assertTrue(f.complete(v1));
326          checkCompletedNormally(g, null);
327 +        checkCompletedNormally(h, null);
328          assertEquals(0, f.getNumberOfDependents());
329          assertEquals(0, g.getNumberOfDependents());
330 <    }
330 >        assertEquals(0, h.getNumberOfDependents());
331 >    }}
332  
333      /**
334       * toString indicates current completion state
# Line 339 | Line 339 | public class CompletableFutureTest exten
339          f = new CompletableFuture<String>();
340          assertTrue(f.toString().contains("[Not completed]"));
341  
342 <        f.complete("foo");
342 >        assertTrue(f.complete("foo"));
343          assertTrue(f.toString().contains("[Completed normally]"));
344  
345          f = new CompletableFuture<String>();
346 <        f.completeExceptionally(new IndexOutOfBoundsException());
346 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
347          assertTrue(f.toString().contains("[Completed exceptionally]"));
348  
349 <        f = new CompletableFuture<String>();
350 <        f.cancel(true);
351 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
352 <
353 <        f = new CompletableFuture<String>();
354 <        f.cancel(false);
355 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
349 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
350 >            f = new CompletableFuture<String>();
351 >            assertTrue(f.cancel(mayInterruptIfRunning));
352 >            assertTrue(f.toString().contains("[Completed exceptionally]"));
353 >        }
354      }
355  
356      /**
# Line 520 | Line 518 | public class CompletableFutureTest exten
518          }
519      }
520  
523
521      class CompletableFutureInc extends CheckedIntegerAction
522          implements Function<Integer, CompletableFuture<Integer>>
523      {
# Line 529 | Line 526 | public class CompletableFutureTest exten
526              invoked();
527              value = x;
528              CompletableFuture<Integer> f = new CompletableFuture<>();
529 <            f.complete(inc(x));
529 >            assertTrue(f.complete(inc(x)));
530              return f;
531          }
532      }
# Line 559 | Line 556 | public class CompletableFutureTest exten
556          }
557      }
558  
559 +    static final boolean defaultExecutorIsCommonPool
560 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
561 +
562      /**
563       * Permits the testing of parallel code for the 3 different
564       * execution modes without copy/pasting all the test methods.
565       */
566      enum ExecutionMode {
567 <        DEFAULT {
567 >        SYNC {
568              public void checkExecutionMode() {
569                  assertFalse(ThreadExecutor.startedCurrentThread());
570                  assertNull(ForkJoinTask.getPool());
# Line 640 | Line 640 | public class CompletableFutureTest exten
640  
641          ASYNC {
642              public void checkExecutionMode() {
643 <                assertSame(ForkJoinPool.commonPool(),
644 <                           ForkJoinTask.getPool());
643 >                // If tests are added that may run across different
644 >                // pools, this needs to be weakened to no-op.
645 >                ForkJoinPool p = ForkJoinTask.getPool();
646 >                assertTrue(p == null ||
647 >                           (defaultExecutorIsCommonPool &&
648 >                            p == ForkJoinPool.commonPool()));
649              }
650              public CompletableFuture<Void> runAsync(Runnable a) {
651                  return CompletableFuture.runAsync(a);
# Line 837 | Line 841 | public class CompletableFutureTest exten
841      {
842          final AtomicInteger a = new AtomicInteger(0);
843          final CompletableFuture<Integer> f = new CompletableFuture<>();
844 <        if (!createIncomplete) f.complete(v1);
844 >        if (!createIncomplete) assertTrue(f.complete(v1));
845          final CompletableFuture<Integer> g = f.exceptionally
846              ((Throwable t) -> {
843                // Should not be called
847                  a.getAndIncrement();
848 <                throw new AssertionError();
848 >                threadFail("should not be called");
849 >                return null;            // unreached
850              });
851 <        if (createIncomplete) f.complete(v1);
851 >        if (createIncomplete) assertTrue(f.complete(v1));
852  
853          checkCompletedNormally(g, v1);
854          checkCompletedNormally(f, v1);
# Line 865 | Line 869 | public class CompletableFutureTest exten
869          if (!createIncomplete) f.completeExceptionally(ex);
870          final CompletableFuture<Integer> g = f.exceptionally
871              ((Throwable t) -> {
872 <                ExecutionMode.DEFAULT.checkExecutionMode();
872 >                ExecutionMode.SYNC.checkExecutionMode();
873                  threadAssertSame(t, ex);
874                  a.getAndIncrement();
875                  return v1;
# Line 876 | Line 880 | public class CompletableFutureTest exten
880          assertEquals(1, a.get());
881      }}
882  
883 +    /**
884 +     * If an "exceptionally action" throws an exception, it completes
885 +     * exceptionally with that exception
886 +     */
887      public void testExceptionally_exceptionalCompletionActionFailed() {
888          for (boolean createIncomplete : new boolean[] { true, false })
881        for (Integer v1 : new Integer[] { 1, null })
889      {
890          final AtomicInteger a = new AtomicInteger(0);
891          final CFException ex1 = new CFException();
# Line 887 | Line 894 | public class CompletableFutureTest exten
894          if (!createIncomplete) f.completeExceptionally(ex1);
895          final CompletableFuture<Integer> g = f.exceptionally
896              ((Throwable t) -> {
897 <                ExecutionMode.DEFAULT.checkExecutionMode();
897 >                ExecutionMode.SYNC.checkExecutionMode();
898                  threadAssertSame(t, ex1);
899                  a.getAndIncrement();
900                  throw ex2;
# Line 895 | Line 902 | public class CompletableFutureTest exten
902          if (createIncomplete) f.completeExceptionally(ex1);
903  
904          checkCompletedWithWrappedException(g, ex2);
905 +        checkCompletedExceptionally(f, ex1);
906          assertEquals(1, a.get());
907      }}
908  
# Line 902 | Line 910 | public class CompletableFutureTest exten
910       * whenComplete action executes on normal completion, propagating
911       * source result.
912       */
913 <    public void testWhenComplete_normalCompletion1() {
913 >    public void testWhenComplete_normalCompletion() {
914          for (ExecutionMode m : ExecutionMode.values())
915          for (boolean createIncomplete : new boolean[] { true, false })
916          for (Integer v1 : new Integer[] { 1, null })
917      {
918          final AtomicInteger a = new AtomicInteger(0);
919          final CompletableFuture<Integer> f = new CompletableFuture<>();
920 <        if (!createIncomplete) f.complete(v1);
920 >        if (!createIncomplete) assertTrue(f.complete(v1));
921          final CompletableFuture<Integer> g = m.whenComplete
922              (f,
923 <             (Integer x, Throwable t) -> {
923 >             (Integer result, Throwable t) -> {
924                  m.checkExecutionMode();
925 <                threadAssertSame(x, v1);
925 >                threadAssertSame(result, v1);
926                  threadAssertNull(t);
927                  a.getAndIncrement();
928              });
929 <        if (createIncomplete) f.complete(v1);
929 >        if (createIncomplete) assertTrue(f.complete(v1));
930  
931          checkCompletedNormally(g, v1);
932          checkCompletedNormally(f, v1);
# Line 932 | Line 940 | public class CompletableFutureTest exten
940      public void testWhenComplete_exceptionalCompletion() {
941          for (ExecutionMode m : ExecutionMode.values())
942          for (boolean createIncomplete : new boolean[] { true, false })
935        for (Integer v1 : new Integer[] { 1, null })
943      {
944          final AtomicInteger a = new AtomicInteger(0);
945          final CFException ex = new CFException();
# Line 940 | Line 947 | public class CompletableFutureTest exten
947          if (!createIncomplete) f.completeExceptionally(ex);
948          final CompletableFuture<Integer> g = m.whenComplete
949              (f,
950 <             (Integer x, Throwable t) -> {
950 >             (Integer result, Throwable t) -> {
951                  m.checkExecutionMode();
952 <                threadAssertNull(x);
952 >                threadAssertNull(result);
953                  threadAssertSame(t, ex);
954                  a.getAndIncrement();
955              });
# Line 967 | Line 974 | public class CompletableFutureTest exten
974          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
975          final CompletableFuture<Integer> g = m.whenComplete
976              (f,
977 <             (Integer x, Throwable t) -> {
977 >             (Integer result, Throwable t) -> {
978                  m.checkExecutionMode();
979 <                threadAssertNull(x);
979 >                threadAssertNull(result);
980                  threadAssertTrue(t instanceof CancellationException);
981                  a.getAndIncrement();
982              });
# Line 984 | Line 991 | public class CompletableFutureTest exten
991       * If a whenComplete action throws an exception when triggered by
992       * a normal completion, it completes exceptionally
993       */
994 <    public void testWhenComplete_actionFailed() {
994 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
995          for (boolean createIncomplete : new boolean[] { true, false })
996          for (ExecutionMode m : ExecutionMode.values())
997          for (Integer v1 : new Integer[] { 1, null })
# Line 992 | Line 999 | public class CompletableFutureTest exten
999          final AtomicInteger a = new AtomicInteger(0);
1000          final CFException ex = new CFException();
1001          final CompletableFuture<Integer> f = new CompletableFuture<>();
1002 <        if (!createIncomplete) f.complete(v1);
1002 >        if (!createIncomplete) assertTrue(f.complete(v1));
1003          final CompletableFuture<Integer> g = m.whenComplete
1004              (f,
1005 <             (Integer x, Throwable t) -> {
1005 >             (Integer result, Throwable t) -> {
1006                  m.checkExecutionMode();
1007 <                threadAssertSame(x, v1);
1007 >                threadAssertSame(result, v1);
1008                  threadAssertNull(t);
1009                  a.getAndIncrement();
1010                  throw ex;
1011              });
1012 <        if (createIncomplete) f.complete(v1);
1012 >        if (createIncomplete) assertTrue(f.complete(v1));
1013  
1014          checkCompletedWithWrappedException(g, ex);
1015          checkCompletedNormally(f, v1);
# Line 1012 | Line 1019 | public class CompletableFutureTest exten
1019      /**
1020       * If a whenComplete action throws an exception when triggered by
1021       * a source completion that also throws an exception, the source
1022 <     * exception takes precedence.
1022 >     * exception takes precedence (unlike handle)
1023       */
1024 <    public void testWhenComplete_actionFailedSourceFailed() {
1024 >    public void testWhenComplete_sourceFailedActionFailed() {
1025          for (boolean createIncomplete : new boolean[] { true, false })
1026          for (ExecutionMode m : ExecutionMode.values())
1020        for (Integer v1 : new Integer[] { 1, null })
1027      {
1028          final AtomicInteger a = new AtomicInteger(0);
1029          final CFException ex1 = new CFException();
# Line 1027 | Line 1033 | public class CompletableFutureTest exten
1033          if (!createIncomplete) f.completeExceptionally(ex1);
1034          final CompletableFuture<Integer> g = m.whenComplete
1035              (f,
1036 <             (Integer x, Throwable t) -> {
1036 >             (Integer result, Throwable t) -> {
1037                  m.checkExecutionMode();
1038                  threadAssertSame(t, ex1);
1039 <                threadAssertNull(x);
1039 >                threadAssertNull(result);
1040                  a.getAndIncrement();
1041                  throw ex2;
1042              });
# Line 1038 | Line 1044 | public class CompletableFutureTest exten
1044  
1045          checkCompletedWithWrappedException(g, ex1);
1046          checkCompletedExceptionally(f, ex1);
1047 +        if (testImplementationDetails) {
1048 +            assertEquals(1, ex1.getSuppressed().length);
1049 +            assertSame(ex2, ex1.getSuppressed()[0]);
1050 +        }
1051          assertEquals(1, a.get());
1052      }}
1053  
# Line 1052 | Line 1062 | public class CompletableFutureTest exten
1062      {
1063          final CompletableFuture<Integer> f = new CompletableFuture<>();
1064          final AtomicInteger a = new AtomicInteger(0);
1065 <        if (!createIncomplete) f.complete(v1);
1065 >        if (!createIncomplete) assertTrue(f.complete(v1));
1066          final CompletableFuture<Integer> g = m.handle
1067              (f,
1068 <             (Integer x, Throwable t) -> {
1068 >             (Integer result, Throwable t) -> {
1069                  m.checkExecutionMode();
1070 <                threadAssertSame(x, v1);
1070 >                threadAssertSame(result, v1);
1071                  threadAssertNull(t);
1072                  a.getAndIncrement();
1073                  return inc(v1);
1074              });
1075 <        if (createIncomplete) f.complete(v1);
1075 >        if (createIncomplete) assertTrue(f.complete(v1));
1076  
1077          checkCompletedNormally(g, inc(v1));
1078          checkCompletedNormally(f, v1);
# Line 1084 | Line 1094 | public class CompletableFutureTest exten
1094          if (!createIncomplete) f.completeExceptionally(ex);
1095          final CompletableFuture<Integer> g = m.handle
1096              (f,
1097 <             (Integer x, Throwable t) -> {
1097 >             (Integer result, Throwable t) -> {
1098                  m.checkExecutionMode();
1099 <                threadAssertNull(x);
1099 >                threadAssertNull(result);
1100                  threadAssertSame(t, ex);
1101                  a.getAndIncrement();
1102                  return v1;
# Line 1113 | Line 1123 | public class CompletableFutureTest exten
1123          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1124          final CompletableFuture<Integer> g = m.handle
1125              (f,
1126 <             (Integer x, Throwable t) -> {
1126 >             (Integer result, Throwable t) -> {
1127                  m.checkExecutionMode();
1128 <                threadAssertNull(x);
1128 >                threadAssertNull(result);
1129                  threadAssertTrue(t instanceof CancellationException);
1130                  a.getAndIncrement();
1131                  return v1;
# Line 1128 | Line 1138 | public class CompletableFutureTest exten
1138      }}
1139  
1140      /**
1141 <     * handle result completes exceptionally if action does
1141 >     * If a "handle action" throws an exception when triggered by
1142 >     * a normal completion, it completes exceptionally
1143       */
1144 <    public void testHandle_sourceFailedActionFailed() {
1144 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1145          for (ExecutionMode m : ExecutionMode.values())
1146          for (boolean createIncomplete : new boolean[] { true, false })
1147 +        for (Integer v1 : new Integer[] { 1, null })
1148      {
1149          final CompletableFuture<Integer> f = new CompletableFuture<>();
1150          final AtomicInteger a = new AtomicInteger(0);
1151 <        final CFException ex1 = new CFException();
1152 <        final CFException ex2 = new CFException();
1141 <        if (!createIncomplete) f.completeExceptionally(ex1);
1151 >        final CFException ex = new CFException();
1152 >        if (!createIncomplete) assertTrue(f.complete(v1));
1153          final CompletableFuture<Integer> g = m.handle
1154              (f,
1155 <             (Integer x, Throwable t) -> {
1155 >             (Integer result, Throwable t) -> {
1156                  m.checkExecutionMode();
1157 <                threadAssertNull(x);
1158 <                threadAssertSame(ex1, t);
1157 >                threadAssertSame(result, v1);
1158 >                threadAssertNull(t);
1159                  a.getAndIncrement();
1160 <                throw ex2;
1160 >                throw ex;
1161              });
1162 <        if (createIncomplete) f.completeExceptionally(ex1);
1162 >        if (createIncomplete) assertTrue(f.complete(v1));
1163  
1164 <        checkCompletedWithWrappedException(g, ex2);
1165 <        checkCompletedExceptionally(f, ex1);
1164 >        checkCompletedWithWrappedException(g, ex);
1165 >        checkCompletedNormally(f, v1);
1166          assertEquals(1, a.get());
1167      }}
1168  
1169 <    public void testHandle_sourceCompletedNormallyActionFailed() {
1170 <        for (ExecutionMode m : ExecutionMode.values())
1169 >    /**
1170 >     * If a "handle action" throws an exception when triggered by
1171 >     * a source completion that also throws an exception, the action
1172 >     * exception takes precedence (unlike whenComplete)
1173 >     */
1174 >    public void testHandle_sourceFailedActionFailed() {
1175          for (boolean createIncomplete : new boolean[] { true, false })
1176 <        for (Integer v1 : new Integer[] { 1, null })
1176 >        for (ExecutionMode m : ExecutionMode.values())
1177      {
1163        final CompletableFuture<Integer> f = new CompletableFuture<>();
1178          final AtomicInteger a = new AtomicInteger(0);
1179 <        final CFException ex = new CFException();
1180 <        if (!createIncomplete) f.complete(v1);
1179 >        final CFException ex1 = new CFException();
1180 >        final CFException ex2 = new CFException();
1181 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1182 >
1183 >        if (!createIncomplete) f.completeExceptionally(ex1);
1184          final CompletableFuture<Integer> g = m.handle
1185              (f,
1186 <             (Integer x, Throwable t) -> {
1186 >             (Integer result, Throwable t) -> {
1187                  m.checkExecutionMode();
1188 <                threadAssertSame(x, v1);
1189 <                threadAssertNull(t);
1188 >                threadAssertNull(result);
1189 >                threadAssertSame(ex1, t);
1190                  a.getAndIncrement();
1191 <                throw ex;
1191 >                throw ex2;
1192              });
1193 <        if (createIncomplete) f.complete(v1);
1193 >        if (createIncomplete) f.completeExceptionally(ex1);
1194  
1195 <        checkCompletedWithWrappedException(g, ex);
1196 <        checkCompletedNormally(f, v1);
1195 >        checkCompletedWithWrappedException(g, ex2);
1196 >        checkCompletedExceptionally(f, ex1);
1197          assertEquals(1, a.get());
1198      }}
1199  
# Line 1254 | Line 1271 | public class CompletableFutureTest exten
1271       */
1272      public void testThenRun_normalCompletion() {
1273          for (ExecutionMode m : ExecutionMode.values())
1257        for (boolean createIncomplete : new boolean[] { true, false })
1274          for (Integer v1 : new Integer[] { 1, null })
1275      {
1276          final CompletableFuture<Integer> f = new CompletableFuture<>();
1277 <        final Noop r = new Noop(m);
1278 <        if (!createIncomplete) f.complete(v1);
1263 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1264 <        if (createIncomplete) {
1265 <            checkIncomplete(g);
1266 <            f.complete(v1);
1267 <        }
1277 >        final Noop[] rs = new Noop[6];
1278 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1279  
1280 <        checkCompletedNormally(g, null);
1280 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1281 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1282 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1283 >        checkIncomplete(h0);
1284 >        checkIncomplete(h1);
1285 >        checkIncomplete(h2);
1286 >        assertTrue(f.complete(v1));
1287 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1288 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1289 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1290 >
1291 >        checkCompletedNormally(h0, null);
1292 >        checkCompletedNormally(h1, null);
1293 >        checkCompletedNormally(h2, null);
1294 >        checkCompletedNormally(h3, null);
1295 >        checkCompletedNormally(h4, null);
1296 >        checkCompletedNormally(h5, null);
1297          checkCompletedNormally(f, v1);
1298 <        r.assertInvoked();
1298 >        for (Noop r : rs) r.assertInvoked();
1299      }}
1300  
1301      /**
# Line 1277 | Line 1304 | public class CompletableFutureTest exten
1304       */
1305      public void testThenRun_exceptionalCompletion() {
1306          for (ExecutionMode m : ExecutionMode.values())
1280        for (boolean createIncomplete : new boolean[] { true, false })
1307      {
1308          final CFException ex = new CFException();
1309          final CompletableFuture<Integer> f = new CompletableFuture<>();
1310 <        final Noop r = new Noop(m);
1311 <        if (!createIncomplete) f.completeExceptionally(ex);
1286 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1287 <        if (createIncomplete) {
1288 <            checkIncomplete(g);
1289 <            f.completeExceptionally(ex);
1290 <        }
1310 >        final Noop[] rs = new Noop[6];
1311 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1312  
1313 <        checkCompletedWithWrappedException(g, ex);
1313 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1314 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1315 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1316 >        checkIncomplete(h0);
1317 >        checkIncomplete(h1);
1318 >        checkIncomplete(h2);
1319 >        assertTrue(f.completeExceptionally(ex));
1320 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1321 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1322 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1323 >
1324 >        checkCompletedWithWrappedException(h0, ex);
1325 >        checkCompletedWithWrappedException(h1, ex);
1326 >        checkCompletedWithWrappedException(h2, ex);
1327 >        checkCompletedWithWrappedException(h3, ex);
1328 >        checkCompletedWithWrappedException(h4, ex);
1329 >        checkCompletedWithWrappedException(h5, ex);
1330          checkCompletedExceptionally(f, ex);
1331 <        r.assertNotInvoked();
1331 >        for (Noop r : rs) r.assertNotInvoked();
1332      }}
1333  
1334      /**
# Line 1299 | Line 1336 | public class CompletableFutureTest exten
1336       */
1337      public void testThenRun_sourceCancelled() {
1338          for (ExecutionMode m : ExecutionMode.values())
1302        for (boolean createIncomplete : new boolean[] { true, false })
1339          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1340      {
1341          final CompletableFuture<Integer> f = new CompletableFuture<>();
1342 <        final Noop r = new Noop(m);
1343 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1308 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1309 <        if (createIncomplete) {
1310 <            checkIncomplete(g);
1311 <            assertTrue(f.cancel(mayInterruptIfRunning));
1312 <        }
1342 >        final Noop[] rs = new Noop[6];
1343 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1344  
1345 <        checkCompletedWithWrappedCancellationException(g);
1345 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1346 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1347 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1348 >        checkIncomplete(h0);
1349 >        checkIncomplete(h1);
1350 >        checkIncomplete(h2);
1351 >        assertTrue(f.cancel(mayInterruptIfRunning));
1352 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1353 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1354 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1355 >
1356 >        checkCompletedWithWrappedCancellationException(h0);
1357 >        checkCompletedWithWrappedCancellationException(h1);
1358 >        checkCompletedWithWrappedCancellationException(h2);
1359 >        checkCompletedWithWrappedCancellationException(h3);
1360 >        checkCompletedWithWrappedCancellationException(h4);
1361 >        checkCompletedWithWrappedCancellationException(h5);
1362          checkCancelled(f);
1363 <        r.assertNotInvoked();
1363 >        for (Noop r : rs) r.assertNotInvoked();
1364      }}
1365  
1366      /**
# Line 1321 | Line 1368 | public class CompletableFutureTest exten
1368       */
1369      public void testThenRun_actionFailed() {
1370          for (ExecutionMode m : ExecutionMode.values())
1324        for (boolean createIncomplete : new boolean[] { true, false })
1371          for (Integer v1 : new Integer[] { 1, null })
1372      {
1373          final CompletableFuture<Integer> f = new CompletableFuture<>();
1374 <        final FailingRunnable r = new FailingRunnable(m);
1375 <        if (!createIncomplete) f.complete(v1);
1330 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1331 <        if (createIncomplete) {
1332 <            checkIncomplete(g);
1333 <            f.complete(v1);
1334 <        }
1374 >        final FailingRunnable[] rs = new FailingRunnable[6];
1375 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1376  
1377 <        checkCompletedWithWrappedCFException(g);
1377 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1378 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1379 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1380 >        assertTrue(f.complete(v1));
1381 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1382 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1383 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1384 >
1385 >        checkCompletedWithWrappedCFException(h0);
1386 >        checkCompletedWithWrappedCFException(h1);
1387 >        checkCompletedWithWrappedCFException(h2);
1388 >        checkCompletedWithWrappedCFException(h3);
1389 >        checkCompletedWithWrappedCFException(h4);
1390 >        checkCompletedWithWrappedCFException(h5);
1391          checkCompletedNormally(f, v1);
1392      }}
1393  
# Line 1342 | Line 1396 | public class CompletableFutureTest exten
1396       */
1397      public void testThenApply_normalCompletion() {
1398          for (ExecutionMode m : ExecutionMode.values())
1345        for (boolean createIncomplete : new boolean[] { true, false })
1399          for (Integer v1 : new Integer[] { 1, null })
1400      {
1401          final CompletableFuture<Integer> f = new CompletableFuture<>();
1402 <        final IncFunction r = new IncFunction(m);
1403 <        if (!createIncomplete) f.complete(v1);
1351 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1352 <        if (createIncomplete) {
1353 <            checkIncomplete(g);
1354 <            f.complete(v1);
1355 <        }
1402 >        final IncFunction[] rs = new IncFunction[4];
1403 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1404  
1405 <        checkCompletedNormally(g, inc(v1));
1405 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1406 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1407 >        checkIncomplete(h0);
1408 >        checkIncomplete(h1);
1409 >        assertTrue(f.complete(v1));
1410 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1411 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1412 >
1413 >        checkCompletedNormally(h0, inc(v1));
1414 >        checkCompletedNormally(h1, inc(v1));
1415 >        checkCompletedNormally(h2, inc(v1));
1416 >        checkCompletedNormally(h3, inc(v1));
1417          checkCompletedNormally(f, v1);
1418 <        r.assertValue(inc(v1));
1418 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1419      }}
1420  
1421      /**
# Line 1365 | Line 1424 | public class CompletableFutureTest exten
1424       */
1425      public void testThenApply_exceptionalCompletion() {
1426          for (ExecutionMode m : ExecutionMode.values())
1368        for (boolean createIncomplete : new boolean[] { true, false })
1427      {
1428          final CFException ex = new CFException();
1429          final CompletableFuture<Integer> f = new CompletableFuture<>();
1430 <        final IncFunction r = new IncFunction(m);
1431 <        if (!createIncomplete) f.completeExceptionally(ex);
1374 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1375 <        if (createIncomplete) {
1376 <            checkIncomplete(g);
1377 <            f.completeExceptionally(ex);
1378 <        }
1430 >        final IncFunction[] rs = new IncFunction[4];
1431 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1432  
1433 <        checkCompletedWithWrappedException(g, ex);
1433 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1434 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1435 >        assertTrue(f.completeExceptionally(ex));
1436 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1437 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1438 >
1439 >        checkCompletedWithWrappedException(h0, ex);
1440 >        checkCompletedWithWrappedException(h1, ex);
1441 >        checkCompletedWithWrappedException(h2, ex);
1442 >        checkCompletedWithWrappedException(h3, ex);
1443          checkCompletedExceptionally(f, ex);
1444 <        r.assertNotInvoked();
1444 >        for (IncFunction r : rs) r.assertNotInvoked();
1445      }}
1446  
1447      /**
# Line 1387 | Line 1449 | public class CompletableFutureTest exten
1449       */
1450      public void testThenApply_sourceCancelled() {
1451          for (ExecutionMode m : ExecutionMode.values())
1390        for (boolean createIncomplete : new boolean[] { true, false })
1452          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1453      {
1454          final CompletableFuture<Integer> f = new CompletableFuture<>();
1455 <        final IncFunction r = new IncFunction(m);
1456 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1396 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1397 <        if (createIncomplete) {
1398 <            checkIncomplete(g);
1399 <            assertTrue(f.cancel(mayInterruptIfRunning));
1400 <        }
1455 >        final IncFunction[] rs = new IncFunction[4];
1456 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1457  
1458 <        checkCompletedWithWrappedCancellationException(g);
1458 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1459 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1460 >        assertTrue(f.cancel(mayInterruptIfRunning));
1461 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1462 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1463 >
1464 >        checkCompletedWithWrappedCancellationException(h0);
1465 >        checkCompletedWithWrappedCancellationException(h1);
1466 >        checkCompletedWithWrappedCancellationException(h2);
1467 >        checkCompletedWithWrappedCancellationException(h3);
1468          checkCancelled(f);
1469 <        r.assertNotInvoked();
1469 >        for (IncFunction r : rs) r.assertNotInvoked();
1470      }}
1471  
1472      /**
# Line 1409 | Line 1474 | public class CompletableFutureTest exten
1474       */
1475      public void testThenApply_actionFailed() {
1476          for (ExecutionMode m : ExecutionMode.values())
1412        for (boolean createIncomplete : new boolean[] { true, false })
1477          for (Integer v1 : new Integer[] { 1, null })
1478      {
1479          final CompletableFuture<Integer> f = new CompletableFuture<>();
1480 <        final FailingFunction r = new FailingFunction(m);
1481 <        if (!createIncomplete) f.complete(v1);
1418 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1419 <        if (createIncomplete) {
1420 <            checkIncomplete(g);
1421 <            f.complete(v1);
1422 <        }
1480 >        final FailingFunction[] rs = new FailingFunction[4];
1481 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1482  
1483 <        checkCompletedWithWrappedCFException(g);
1483 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1484 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1485 >        assertTrue(f.complete(v1));
1486 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1487 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1488 >
1489 >        checkCompletedWithWrappedCFException(h0);
1490 >        checkCompletedWithWrappedCFException(h1);
1491 >        checkCompletedWithWrappedCFException(h2);
1492 >        checkCompletedWithWrappedCFException(h3);
1493          checkCompletedNormally(f, v1);
1494      }}
1495  
# Line 1430 | Line 1498 | public class CompletableFutureTest exten
1498       */
1499      public void testThenAccept_normalCompletion() {
1500          for (ExecutionMode m : ExecutionMode.values())
1433        for (boolean createIncomplete : new boolean[] { true, false })
1501          for (Integer v1 : new Integer[] { 1, null })
1502      {
1503          final CompletableFuture<Integer> f = new CompletableFuture<>();
1504 <        final NoopConsumer r = new NoopConsumer(m);
1505 <        if (!createIncomplete) f.complete(v1);
1439 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1440 <        if (createIncomplete) {
1441 <            checkIncomplete(g);
1442 <            f.complete(v1);
1443 <        }
1504 >        final NoopConsumer[] rs = new NoopConsumer[4];
1505 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1506  
1507 <        checkCompletedNormally(g, null);
1508 <        r.assertValue(v1);
1507 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1508 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1509 >        checkIncomplete(h0);
1510 >        checkIncomplete(h1);
1511 >        assertTrue(f.complete(v1));
1512 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1513 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1514 >
1515 >        checkCompletedNormally(h0, null);
1516 >        checkCompletedNormally(h1, null);
1517 >        checkCompletedNormally(h2, null);
1518 >        checkCompletedNormally(h3, null);
1519          checkCompletedNormally(f, v1);
1520 +        for (NoopConsumer r : rs) r.assertValue(v1);
1521      }}
1522  
1523      /**
# Line 1453 | Line 1526 | public class CompletableFutureTest exten
1526       */
1527      public void testThenAccept_exceptionalCompletion() {
1528          for (ExecutionMode m : ExecutionMode.values())
1456        for (boolean createIncomplete : new boolean[] { true, false })
1529      {
1530          final CFException ex = new CFException();
1531          final CompletableFuture<Integer> f = new CompletableFuture<>();
1532 <        final NoopConsumer r = new NoopConsumer(m);
1533 <        if (!createIncomplete) f.completeExceptionally(ex);
1462 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1463 <        if (createIncomplete) {
1464 <            checkIncomplete(g);
1465 <            f.completeExceptionally(ex);
1466 <        }
1532 >        final NoopConsumer[] rs = new NoopConsumer[4];
1533 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1534  
1535 <        checkCompletedWithWrappedException(g, ex);
1535 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1536 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1537 >        assertTrue(f.completeExceptionally(ex));
1538 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1539 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1540 >
1541 >        checkCompletedWithWrappedException(h0, ex);
1542 >        checkCompletedWithWrappedException(h1, ex);
1543 >        checkCompletedWithWrappedException(h2, ex);
1544 >        checkCompletedWithWrappedException(h3, ex);
1545          checkCompletedExceptionally(f, ex);
1546 <        r.assertNotInvoked();
1546 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1547      }}
1548  
1549      /**
# Line 1475 | Line 1551 | public class CompletableFutureTest exten
1551       */
1552      public void testThenAccept_sourceCancelled() {
1553          for (ExecutionMode m : ExecutionMode.values())
1478        for (boolean createIncomplete : new boolean[] { true, false })
1554          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1555      {
1556          final CompletableFuture<Integer> f = new CompletableFuture<>();
1557 <        final NoopConsumer r = new NoopConsumer(m);
1558 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1484 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1485 <        if (createIncomplete) {
1486 <            checkIncomplete(g);
1487 <            assertTrue(f.cancel(mayInterruptIfRunning));
1488 <        }
1557 >        final NoopConsumer[] rs = new NoopConsumer[4];
1558 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1559  
1560 <        checkCompletedWithWrappedCancellationException(g);
1560 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1561 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1562 >        assertTrue(f.cancel(mayInterruptIfRunning));
1563 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1564 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1565 >
1566 >        checkCompletedWithWrappedCancellationException(h0);
1567 >        checkCompletedWithWrappedCancellationException(h1);
1568 >        checkCompletedWithWrappedCancellationException(h2);
1569 >        checkCompletedWithWrappedCancellationException(h3);
1570          checkCancelled(f);
1571 <        r.assertNotInvoked();
1571 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1572      }}
1573  
1574      /**
# Line 1497 | Line 1576 | public class CompletableFutureTest exten
1576       */
1577      public void testThenAccept_actionFailed() {
1578          for (ExecutionMode m : ExecutionMode.values())
1500        for (boolean createIncomplete : new boolean[] { true, false })
1579          for (Integer v1 : new Integer[] { 1, null })
1580      {
1581          final CompletableFuture<Integer> f = new CompletableFuture<>();
1582 <        final FailingConsumer r = new FailingConsumer(m);
1583 <        if (!createIncomplete) f.complete(v1);
1506 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1507 <        if (createIncomplete) {
1508 <            checkIncomplete(g);
1509 <            f.complete(v1);
1510 <        }
1582 >        final FailingConsumer[] rs = new FailingConsumer[4];
1583 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1584  
1585 <        checkCompletedWithWrappedCFException(g);
1585 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1586 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1587 >        assertTrue(f.complete(v1));
1588 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1589 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1590 >
1591 >        checkCompletedWithWrappedCFException(h0);
1592 >        checkCompletedWithWrappedCFException(h1);
1593 >        checkCompletedWithWrappedCFException(h2);
1594 >        checkCompletedWithWrappedCFException(h3);
1595          checkCompletedNormally(f, v1);
1596      }}
1597  
# Line 1519 | Line 1601 | public class CompletableFutureTest exten
1601       */
1602      public void testThenCombine_normalCompletion() {
1603          for (ExecutionMode m : ExecutionMode.values())
1522        for (boolean createIncomplete : new boolean[] { true, false })
1604          for (boolean fFirst : new boolean[] { true, false })
1605          for (Integer v1 : new Integer[] { 1, null })
1606          for (Integer v2 : new Integer[] { 2, null })
1607      {
1608          final CompletableFuture<Integer> f = new CompletableFuture<>();
1609          final CompletableFuture<Integer> g = new CompletableFuture<>();
1610 <        final SubtractFunction r = new SubtractFunction(m);
1610 >        final SubtractFunction[] rs = new SubtractFunction[6];
1611 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1612  
1613 <        if (fFirst) f.complete(v1); else g.complete(v2);
1614 <        if (!createIncomplete)
1615 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1616 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1617 <        if (createIncomplete) {
1618 <            checkIncomplete(h);
1619 <            r.assertNotInvoked();
1620 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1621 <        }
1613 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1614 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1615 >        final Integer w1 =  fFirst ? v1 : v2;
1616 >        final Integer w2 = !fFirst ? v1 : v2;
1617 >
1618 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1619 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1620 >        assertTrue(fst.complete(w1));
1621 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1622 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1623 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1624 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1625 >        checkCompletedNormally(h1, subtract(w1, w1));
1626 >        checkCompletedNormally(h3, subtract(w1, w1));
1627 >        rs[1].assertValue(subtract(w1, w1));
1628 >        rs[3].assertValue(subtract(w1, w1));
1629 >        assertTrue(snd.complete(w2));
1630 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1631 >
1632 >        checkCompletedNormally(h0, subtract(v1, v2));
1633 >        checkCompletedNormally(h2, subtract(v1, v2));
1634 >        checkCompletedNormally(h4, subtract(v1, v2));
1635 >        rs[0].assertValue(subtract(v1, v2));
1636 >        rs[2].assertValue(subtract(v1, v2));
1637 >        rs[4].assertValue(subtract(v1, v2));
1638  
1541        checkCompletedNormally(h, subtract(v1, v2));
1639          checkCompletedNormally(f, v1);
1640          checkCompletedNormally(g, v2);
1544        r.assertValue(subtract(v1, v2));
1641      }}
1642  
1643      /**
1644       * thenCombine result completes exceptionally after exceptional
1645       * completion of either source
1646       */
1647 <    public void testThenCombine_exceptionalCompletion() {
1647 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1648          for (ExecutionMode m : ExecutionMode.values())
1553        for (boolean createIncomplete : new boolean[] { true, false })
1649          for (boolean fFirst : new boolean[] { true, false })
1650 +        for (boolean failFirst : new boolean[] { true, false })
1651          for (Integer v1 : new Integer[] { 1, null })
1652      {
1653          final CompletableFuture<Integer> f = new CompletableFuture<>();
1654          final CompletableFuture<Integer> g = new CompletableFuture<>();
1655          final CFException ex = new CFException();
1656 <        final SubtractFunction r = new SubtractFunction(m);
1657 <
1658 <        (fFirst ? f : g).complete(v1);
1659 <        if (!createIncomplete)
1660 <            (!fFirst ? f : g).completeExceptionally(ex);
1661 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1662 <        if (createIncomplete) {
1663 <            checkIncomplete(h);
1664 <            (!fFirst ? f : g).completeExceptionally(ex);
1665 <        }
1656 >        final SubtractFunction r1 = new SubtractFunction(m);
1657 >        final SubtractFunction r2 = new SubtractFunction(m);
1658 >        final SubtractFunction r3 = new SubtractFunction(m);
1659 >
1660 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1661 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1662 >        final Callable<Boolean> complete1 = failFirst ?
1663 >            () -> fst.completeExceptionally(ex) :
1664 >            () -> fst.complete(v1);
1665 >        final Callable<Boolean> complete2 = failFirst ?
1666 >            () -> snd.complete(v1) :
1667 >            () -> snd.completeExceptionally(ex);
1668 >
1669 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1670 >        assertTrue(complete1.call());
1671 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1672 >        checkIncomplete(h1);
1673 >        checkIncomplete(h2);
1674 >        assertTrue(complete2.call());
1675 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1676  
1677 <        checkCompletedWithWrappedException(h, ex);
1678 <        r.assertNotInvoked();
1679 <        checkCompletedNormally(fFirst ? f : g, v1);
1680 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1677 >        checkCompletedWithWrappedException(h1, ex);
1678 >        checkCompletedWithWrappedException(h2, ex);
1679 >        checkCompletedWithWrappedException(h3, ex);
1680 >        r1.assertNotInvoked();
1681 >        r2.assertNotInvoked();
1682 >        r3.assertNotInvoked();
1683 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1684 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1685      }}
1686  
1687      /**
1688       * thenCombine result completes exceptionally if either source cancelled
1689       */
1690 <    public void testThenCombine_sourceCancelled() {
1690 >    public void testThenCombine_sourceCancelled() throws Throwable {
1691          for (ExecutionMode m : ExecutionMode.values())
1692          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1583        for (boolean createIncomplete : new boolean[] { true, false })
1693          for (boolean fFirst : new boolean[] { true, false })
1694 +        for (boolean failFirst : new boolean[] { true, false })
1695          for (Integer v1 : new Integer[] { 1, null })
1696      {
1697          final CompletableFuture<Integer> f = new CompletableFuture<>();
1698          final CompletableFuture<Integer> g = new CompletableFuture<>();
1699 <        final SubtractFunction r = new SubtractFunction(m);
1699 >        final SubtractFunction r1 = new SubtractFunction(m);
1700 >        final SubtractFunction r2 = new SubtractFunction(m);
1701 >        final SubtractFunction r3 = new SubtractFunction(m);
1702  
1703 <        (fFirst ? f : g).complete(v1);
1704 <        if (!createIncomplete)
1705 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1706 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1707 <        if (createIncomplete) {
1708 <            checkIncomplete(h);
1709 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1710 <        }
1703 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1704 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1705 >        final Callable<Boolean> complete1 = failFirst ?
1706 >            () -> fst.cancel(mayInterruptIfRunning) :
1707 >            () -> fst.complete(v1);
1708 >        final Callable<Boolean> complete2 = failFirst ?
1709 >            () -> snd.complete(v1) :
1710 >            () -> snd.cancel(mayInterruptIfRunning);
1711  
1712 <        checkCompletedWithWrappedCancellationException(h);
1713 <        checkCancelled(!fFirst ? f : g);
1714 <        r.assertNotInvoked();
1715 <        checkCompletedNormally(fFirst ? f : g, v1);
1712 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1713 >        assertTrue(complete1.call());
1714 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1715 >        checkIncomplete(h1);
1716 >        checkIncomplete(h2);
1717 >        assertTrue(complete2.call());
1718 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1719 >
1720 >        checkCompletedWithWrappedCancellationException(h1);
1721 >        checkCompletedWithWrappedCancellationException(h2);
1722 >        checkCompletedWithWrappedCancellationException(h3);
1723 >        r1.assertNotInvoked();
1724 >        r2.assertNotInvoked();
1725 >        r3.assertNotInvoked();
1726 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1727 >        checkCancelled(failFirst ? fst : snd);
1728      }}
1729  
1730      /**
# Line 1614 | Line 1738 | public class CompletableFutureTest exten
1738      {
1739          final CompletableFuture<Integer> f = new CompletableFuture<>();
1740          final CompletableFuture<Integer> g = new CompletableFuture<>();
1741 <        final FailingBiFunction r = new FailingBiFunction(m);
1742 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1741 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1742 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1743 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1744 >
1745 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1746 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1747 >        final Integer w1 =  fFirst ? v1 : v2;
1748 >        final Integer w2 = !fFirst ? v1 : v2;
1749 >
1750 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1751 >        assertTrue(fst.complete(w1));
1752 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1753 >        assertTrue(snd.complete(w2));
1754 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1755  
1756 <        if (fFirst) {
1757 <            f.complete(v1);
1758 <            g.complete(v2);
1759 <        } else {
1760 <            g.complete(v2);
1761 <            f.complete(v1);
1626 <        }
1627 <
1628 <        checkCompletedWithWrappedCFException(h);
1756 >        checkCompletedWithWrappedCFException(h1);
1757 >        checkCompletedWithWrappedCFException(h2);
1758 >        checkCompletedWithWrappedCFException(h3);
1759 >        r1.assertInvoked();
1760 >        r2.assertInvoked();
1761 >        r3.assertInvoked();
1762          checkCompletedNormally(f, v1);
1763          checkCompletedNormally(g, v2);
1764      }}
# Line 1636 | Line 1769 | public class CompletableFutureTest exten
1769       */
1770      public void testThenAcceptBoth_normalCompletion() {
1771          for (ExecutionMode m : ExecutionMode.values())
1639        for (boolean createIncomplete : new boolean[] { true, false })
1772          for (boolean fFirst : new boolean[] { true, false })
1773          for (Integer v1 : new Integer[] { 1, null })
1774          for (Integer v2 : new Integer[] { 2, null })
1775      {
1776          final CompletableFuture<Integer> f = new CompletableFuture<>();
1777          final CompletableFuture<Integer> g = new CompletableFuture<>();
1778 <        final SubtractAction r = new SubtractAction(m);
1779 <
1780 <        if (fFirst) f.complete(v1); else g.complete(v2);
1781 <        if (!createIncomplete)
1782 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1783 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1784 <        if (createIncomplete) {
1785 <            checkIncomplete(h);
1786 <            r.assertNotInvoked();
1787 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1788 <        }
1778 >        final SubtractAction r1 = new SubtractAction(m);
1779 >        final SubtractAction r2 = new SubtractAction(m);
1780 >        final SubtractAction r3 = new SubtractAction(m);
1781 >
1782 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1783 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1784 >        final Integer w1 =  fFirst ? v1 : v2;
1785 >        final Integer w2 = !fFirst ? v1 : v2;
1786 >
1787 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1788 >        assertTrue(fst.complete(w1));
1789 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1790 >        checkIncomplete(h1);
1791 >        checkIncomplete(h2);
1792 >        r1.assertNotInvoked();
1793 >        r2.assertNotInvoked();
1794 >        assertTrue(snd.complete(w2));
1795 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1796  
1797 <        checkCompletedNormally(h, null);
1798 <        r.assertValue(subtract(v1, v2));
1797 >        checkCompletedNormally(h1, null);
1798 >        checkCompletedNormally(h2, null);
1799 >        checkCompletedNormally(h3, null);
1800 >        r1.assertValue(subtract(v1, v2));
1801 >        r2.assertValue(subtract(v1, v2));
1802 >        r3.assertValue(subtract(v1, v2));
1803          checkCompletedNormally(f, v1);
1804          checkCompletedNormally(g, v2);
1805      }}
# Line 1665 | Line 1808 | public class CompletableFutureTest exten
1808       * thenAcceptBoth result completes exceptionally after exceptional
1809       * completion of either source
1810       */
1811 <    public void testThenAcceptBoth_exceptionalCompletion() {
1811 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1812          for (ExecutionMode m : ExecutionMode.values())
1670        for (boolean createIncomplete : new boolean[] { true, false })
1813          for (boolean fFirst : new boolean[] { true, false })
1814 +        for (boolean failFirst : new boolean[] { true, false })
1815          for (Integer v1 : new Integer[] { 1, null })
1816      {
1817          final CompletableFuture<Integer> f = new CompletableFuture<>();
1818          final CompletableFuture<Integer> g = new CompletableFuture<>();
1819          final CFException ex = new CFException();
1820 <        final SubtractAction r = new SubtractAction(m);
1821 <
1822 <        (fFirst ? f : g).complete(v1);
1823 <        if (!createIncomplete)
1824 <            (!fFirst ? f : g).completeExceptionally(ex);
1825 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1826 <        if (createIncomplete) {
1827 <            checkIncomplete(h);
1828 <            (!fFirst ? f : g).completeExceptionally(ex);
1829 <        }
1820 >        final SubtractAction r1 = new SubtractAction(m);
1821 >        final SubtractAction r2 = new SubtractAction(m);
1822 >        final SubtractAction r3 = new SubtractAction(m);
1823 >
1824 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1825 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1826 >        final Callable<Boolean> complete1 = failFirst ?
1827 >            () -> fst.completeExceptionally(ex) :
1828 >            () -> fst.complete(v1);
1829 >        final Callable<Boolean> complete2 = failFirst ?
1830 >            () -> snd.complete(v1) :
1831 >            () -> snd.completeExceptionally(ex);
1832 >
1833 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1834 >        assertTrue(complete1.call());
1835 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1836 >        checkIncomplete(h1);
1837 >        checkIncomplete(h2);
1838 >        assertTrue(complete2.call());
1839 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1840  
1841 <        checkCompletedWithWrappedException(h, ex);
1842 <        r.assertNotInvoked();
1843 <        checkCompletedNormally(fFirst ? f : g, v1);
1844 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1841 >        checkCompletedWithWrappedException(h1, ex);
1842 >        checkCompletedWithWrappedException(h2, ex);
1843 >        checkCompletedWithWrappedException(h3, ex);
1844 >        r1.assertNotInvoked();
1845 >        r2.assertNotInvoked();
1846 >        r3.assertNotInvoked();
1847 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1848 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1849      }}
1850  
1851      /**
1852       * thenAcceptBoth result completes exceptionally if either source cancelled
1853       */
1854 <    public void testThenAcceptBoth_sourceCancelled() {
1854 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1855          for (ExecutionMode m : ExecutionMode.values())
1856          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1700        for (boolean createIncomplete : new boolean[] { true, false })
1857          for (boolean fFirst : new boolean[] { true, false })
1858 +        for (boolean failFirst : new boolean[] { true, false })
1859          for (Integer v1 : new Integer[] { 1, null })
1860      {
1861          final CompletableFuture<Integer> f = new CompletableFuture<>();
1862          final CompletableFuture<Integer> g = new CompletableFuture<>();
1863 <        final SubtractAction r = new SubtractAction(m);
1863 >        final SubtractAction r1 = new SubtractAction(m);
1864 >        final SubtractAction r2 = new SubtractAction(m);
1865 >        final SubtractAction r3 = new SubtractAction(m);
1866  
1867 <        (fFirst ? f : g).complete(v1);
1868 <        if (!createIncomplete)
1869 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1870 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1871 <        if (createIncomplete) {
1872 <            checkIncomplete(h);
1873 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1874 <        }
1867 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1868 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1869 >        final Callable<Boolean> complete1 = failFirst ?
1870 >            () -> fst.cancel(mayInterruptIfRunning) :
1871 >            () -> fst.complete(v1);
1872 >        final Callable<Boolean> complete2 = failFirst ?
1873 >            () -> snd.complete(v1) :
1874 >            () -> snd.cancel(mayInterruptIfRunning);
1875  
1876 <        checkCompletedWithWrappedCancellationException(h);
1877 <        checkCancelled(!fFirst ? f : g);
1878 <        r.assertNotInvoked();
1879 <        checkCompletedNormally(fFirst ? f : g, v1);
1876 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1877 >        assertTrue(complete1.call());
1878 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1879 >        checkIncomplete(h1);
1880 >        checkIncomplete(h2);
1881 >        assertTrue(complete2.call());
1882 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1883 >
1884 >        checkCompletedWithWrappedCancellationException(h1);
1885 >        checkCompletedWithWrappedCancellationException(h2);
1886 >        checkCompletedWithWrappedCancellationException(h3);
1887 >        r1.assertNotInvoked();
1888 >        r2.assertNotInvoked();
1889 >        r3.assertNotInvoked();
1890 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1891 >        checkCancelled(failFirst ? fst : snd);
1892      }}
1893  
1894      /**
# Line 1731 | Line 1902 | public class CompletableFutureTest exten
1902      {
1903          final CompletableFuture<Integer> f = new CompletableFuture<>();
1904          final CompletableFuture<Integer> g = new CompletableFuture<>();
1905 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1906 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1905 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1906 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1907 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1908 >
1909 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1910 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1911 >        final Integer w1 =  fFirst ? v1 : v2;
1912 >        final Integer w2 = !fFirst ? v1 : v2;
1913 >
1914 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1915 >        assertTrue(fst.complete(w1));
1916 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1917 >        assertTrue(snd.complete(w2));
1918 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1919  
1920 <        if (fFirst) {
1921 <            f.complete(v1);
1922 <            g.complete(v2);
1923 <        } else {
1924 <            g.complete(v2);
1925 <            f.complete(v1);
1743 <        }
1744 <
1745 <        checkCompletedWithWrappedCFException(h);
1920 >        checkCompletedWithWrappedCFException(h1);
1921 >        checkCompletedWithWrappedCFException(h2);
1922 >        checkCompletedWithWrappedCFException(h3);
1923 >        r1.assertInvoked();
1924 >        r2.assertInvoked();
1925 >        r3.assertInvoked();
1926          checkCompletedNormally(f, v1);
1927          checkCompletedNormally(g, v2);
1928      }}
# Line 1753 | Line 1933 | public class CompletableFutureTest exten
1933       */
1934      public void testRunAfterBoth_normalCompletion() {
1935          for (ExecutionMode m : ExecutionMode.values())
1756        for (boolean createIncomplete : new boolean[] { true, false })
1936          for (boolean fFirst : new boolean[] { true, false })
1937          for (Integer v1 : new Integer[] { 1, null })
1938          for (Integer v2 : new Integer[] { 2, null })
1939      {
1940          final CompletableFuture<Integer> f = new CompletableFuture<>();
1941          final CompletableFuture<Integer> g = new CompletableFuture<>();
1942 <        final Noop r = new Noop(m);
1943 <
1944 <        if (fFirst) f.complete(v1); else g.complete(v2);
1945 <        if (!createIncomplete)
1946 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1947 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1948 <        if (createIncomplete) {
1949 <            checkIncomplete(h);
1950 <            r.assertNotInvoked();
1951 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1952 <        }
1942 >        final Noop r1 = new Noop(m);
1943 >        final Noop r2 = new Noop(m);
1944 >        final Noop r3 = new Noop(m);
1945 >
1946 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1947 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1948 >        final Integer w1 =  fFirst ? v1 : v2;
1949 >        final Integer w2 = !fFirst ? v1 : v2;
1950 >
1951 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1952 >        assertTrue(fst.complete(w1));
1953 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1954 >        checkIncomplete(h1);
1955 >        checkIncomplete(h2);
1956 >        r1.assertNotInvoked();
1957 >        r2.assertNotInvoked();
1958 >        assertTrue(snd.complete(w2));
1959 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1960  
1961 <        checkCompletedNormally(h, null);
1962 <        r.assertInvoked();
1961 >        checkCompletedNormally(h1, null);
1962 >        checkCompletedNormally(h2, null);
1963 >        checkCompletedNormally(h3, null);
1964 >        r1.assertInvoked();
1965 >        r2.assertInvoked();
1966 >        r3.assertInvoked();
1967          checkCompletedNormally(f, v1);
1968          checkCompletedNormally(g, v2);
1969      }}
# Line 1782 | Line 1972 | public class CompletableFutureTest exten
1972       * runAfterBoth result completes exceptionally after exceptional
1973       * completion of either source
1974       */
1975 <    public void testRunAfterBoth_exceptionalCompletion() {
1975 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1976          for (ExecutionMode m : ExecutionMode.values())
1787        for (boolean createIncomplete : new boolean[] { true, false })
1977          for (boolean fFirst : new boolean[] { true, false })
1978 +        for (boolean failFirst : new boolean[] { true, false })
1979          for (Integer v1 : new Integer[] { 1, null })
1980      {
1981          final CompletableFuture<Integer> f = new CompletableFuture<>();
1982          final CompletableFuture<Integer> g = new CompletableFuture<>();
1983          final CFException ex = new CFException();
1984 <        final Noop r = new Noop(m);
1985 <
1986 <        (fFirst ? f : g).complete(v1);
1987 <        if (!createIncomplete)
1988 <            (!fFirst ? f : g).completeExceptionally(ex);
1989 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1990 <        if (createIncomplete) {
1991 <            checkIncomplete(h);
1992 <            (!fFirst ? f : g).completeExceptionally(ex);
1993 <        }
1984 >        final Noop r1 = new Noop(m);
1985 >        final Noop r2 = new Noop(m);
1986 >        final Noop r3 = new Noop(m);
1987 >
1988 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1989 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1990 >        final Callable<Boolean> complete1 = failFirst ?
1991 >            () -> fst.completeExceptionally(ex) :
1992 >            () -> fst.complete(v1);
1993 >        final Callable<Boolean> complete2 = failFirst ?
1994 >            () -> snd.complete(v1) :
1995 >            () -> snd.completeExceptionally(ex);
1996 >
1997 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1998 >        assertTrue(complete1.call());
1999 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2000 >        checkIncomplete(h1);
2001 >        checkIncomplete(h2);
2002 >        assertTrue(complete2.call());
2003 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2004  
2005 <        checkCompletedWithWrappedException(h, ex);
2006 <        r.assertNotInvoked();
2007 <        checkCompletedNormally(fFirst ? f : g, v1);
2008 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
2005 >        checkCompletedWithWrappedException(h1, ex);
2006 >        checkCompletedWithWrappedException(h2, ex);
2007 >        checkCompletedWithWrappedException(h3, ex);
2008 >        r1.assertNotInvoked();
2009 >        r2.assertNotInvoked();
2010 >        r3.assertNotInvoked();
2011 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2012 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2013      }}
2014  
2015      /**
2016       * runAfterBoth result completes exceptionally if either source cancelled
2017       */
2018 <    public void testRunAfterBoth_sourceCancelled() {
2018 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2019          for (ExecutionMode m : ExecutionMode.values())
2020          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1817        for (boolean createIncomplete : new boolean[] { true, false })
2021          for (boolean fFirst : new boolean[] { true, false })
2022 +        for (boolean failFirst : new boolean[] { true, false })
2023          for (Integer v1 : new Integer[] { 1, null })
2024      {
2025          final CompletableFuture<Integer> f = new CompletableFuture<>();
2026          final CompletableFuture<Integer> g = new CompletableFuture<>();
2027 <        final Noop r = new Noop(m);
2027 >        final Noop r1 = new Noop(m);
2028 >        final Noop r2 = new Noop(m);
2029 >        final Noop r3 = new Noop(m);
2030  
2031 <        (fFirst ? f : g).complete(v1);
2032 <        if (!createIncomplete)
2033 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2034 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2035 <        if (createIncomplete) {
2036 <            checkIncomplete(h);
2037 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2038 <        }
2031 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2032 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2033 >        final Callable<Boolean> complete1 = failFirst ?
2034 >            () -> fst.cancel(mayInterruptIfRunning) :
2035 >            () -> fst.complete(v1);
2036 >        final Callable<Boolean> complete2 = failFirst ?
2037 >            () -> snd.complete(v1) :
2038 >            () -> snd.cancel(mayInterruptIfRunning);
2039  
2040 <        checkCompletedWithWrappedCancellationException(h);
2041 <        checkCancelled(!fFirst ? f : g);
2042 <        r.assertNotInvoked();
2043 <        checkCompletedNormally(fFirst ? f : g, v1);
2040 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2041 >        assertTrue(complete1.call());
2042 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2043 >        checkIncomplete(h1);
2044 >        checkIncomplete(h2);
2045 >        assertTrue(complete2.call());
2046 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2047 >
2048 >        checkCompletedWithWrappedCancellationException(h1);
2049 >        checkCompletedWithWrappedCancellationException(h2);
2050 >        checkCompletedWithWrappedCancellationException(h3);
2051 >        r1.assertNotInvoked();
2052 >        r2.assertNotInvoked();
2053 >        r3.assertNotInvoked();
2054 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2055 >        checkCancelled(failFirst ? fst : snd);
2056      }}
2057  
2058      /**
# Line 1850 | Line 2068 | public class CompletableFutureTest exten
2068          final CompletableFuture<Integer> g = new CompletableFuture<>();
2069          final FailingRunnable r1 = new FailingRunnable(m);
2070          final FailingRunnable r2 = new FailingRunnable(m);
2071 +        final FailingRunnable r3 = new FailingRunnable(m);
2072  
2073 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2074 <        if (fFirst) {
2075 <            f.complete(v1);
2076 <            g.complete(v2);
2077 <        } else {
2078 <            g.complete(v2);
2079 <            f.complete(v1);
2080 <        }
2081 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2073 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2074 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2075 >        final Integer w1 =  fFirst ? v1 : v2;
2076 >        final Integer w2 = !fFirst ? v1 : v2;
2077 >
2078 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2079 >        assertTrue(fst.complete(w1));
2080 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2081 >        assertTrue(snd.complete(w2));
2082 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2083  
2084          checkCompletedWithWrappedCFException(h1);
2085          checkCompletedWithWrappedCFException(h2);
2086 +        checkCompletedWithWrappedCFException(h3);
2087 +        r1.assertInvoked();
2088 +        r2.assertInvoked();
2089 +        r3.assertInvoked();
2090          checkCompletedNormally(f, v1);
2091          checkCompletedNormally(g, v2);
2092      }}
# Line 1985 | Line 2209 | public class CompletableFutureTest exten
2209  
2210          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2211          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2212 <        if (fFirst) {
2213 <            f.complete(v1);
1990 <            g.completeExceptionally(ex);
1991 <        } else {
1992 <            g.completeExceptionally(ex);
1993 <            f.complete(v1);
1994 <        }
2212 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2213 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2214          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2215          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2216  
# Line 2097 | Line 2316 | public class CompletableFutureTest exten
2316  
2317          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2318          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2319 <        if (fFirst) {
2320 <            f.complete(v1);
2102 <            g.cancel(mayInterruptIfRunning);
2103 <        } else {
2104 <            g.cancel(mayInterruptIfRunning);
2105 <            f.complete(v1);
2106 <        }
2319 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2320 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2321          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2322          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2323  
# Line 2305 | Line 2519 | public class CompletableFutureTest exten
2519  
2520          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2521          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2522 <        if (fFirst) {
2523 <            f.complete(v1);
2310 <            g.completeExceptionally(ex);
2311 <        } else {
2312 <            g.completeExceptionally(ex);
2313 <            f.complete(v1);
2314 <        }
2522 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2523 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2524          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2525          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2526  
# Line 2514 | Line 2723 | public class CompletableFutureTest exten
2723          checkIncomplete(h1);
2724          rs[0].assertNotInvoked();
2725          rs[1].assertNotInvoked();
2726 <        f.completeExceptionally(ex);
2726 >        assertTrue(f.completeExceptionally(ex));
2727          checkCompletedWithWrappedException(h0, ex);
2728          checkCompletedWithWrappedException(h1, ex);
2729          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
# Line 2522 | Line 2731 | public class CompletableFutureTest exten
2731          checkCompletedWithWrappedException(h2, ex);
2732          checkCompletedWithWrappedException(h3, ex);
2733  
2734 <        g.complete(v1);
2734 >        assertTrue(g.complete(v1));
2735  
2736          // unspecified behavior - both source completions available
2737          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2565 | Line 2774 | public class CompletableFutureTest exten
2774  
2775          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2776          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2777 <        if (fFirst) {
2778 <            f.complete(v1);
2570 <            g.completeExceptionally(ex);
2571 <        } else {
2572 <            g.completeExceptionally(ex);
2573 <            f.complete(v1);
2574 <        }
2777 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2778 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2779          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2780          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2781  
# Line 2636 | Line 2840 | public class CompletableFutureTest exten
2840          checkCompletedWithWrappedCancellationException(h2);
2841          checkCompletedWithWrappedCancellationException(h3);
2842  
2843 <        g.complete(v1);
2843 >        assertTrue(g.complete(v1));
2844  
2845          // unspecified behavior - both source completions available
2846          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2680 | Line 2884 | public class CompletableFutureTest exten
2884  
2885          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2886          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2887 <        f.complete(v1);
2887 >        assertTrue(f.complete(v1));
2888          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2889          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2890          checkCompletedWithWrappedCFException(h0);
# Line 2688 | Line 2892 | public class CompletableFutureTest exten
2892          checkCompletedWithWrappedCFException(h2);
2893          checkCompletedWithWrappedCFException(h3);
2894          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2895 <        g.complete(v2);
2895 >        assertTrue(g.complete(v2));
2896          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2897          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2898          checkCompletedWithWrappedCFException(h4);
# Line 2709 | Line 2913 | public class CompletableFutureTest exten
2913      {
2914          final CompletableFuture<Integer> f = new CompletableFuture<>();
2915          final CompletableFutureInc r = new CompletableFutureInc(m);
2916 <        if (!createIncomplete) f.complete(v1);
2916 >        if (!createIncomplete) assertTrue(f.complete(v1));
2917          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2918 <        if (createIncomplete) f.complete(v1);
2918 >        if (createIncomplete) assertTrue(f.complete(v1));
2919  
2920          checkCompletedNormally(g, inc(v1));
2921          checkCompletedNormally(f, v1);
# Line 2749 | Line 2953 | public class CompletableFutureTest exten
2953          final CompletableFuture<Integer> f = new CompletableFuture<>();
2954          final FailingCompletableFutureFunction r
2955              = new FailingCompletableFutureFunction(m);
2956 <        if (!createIncomplete) f.complete(v1);
2956 >        if (!createIncomplete) assertTrue(f.complete(v1));
2957          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2958 <        if (createIncomplete) f.complete(v1);
2958 >        if (createIncomplete) assertTrue(f.complete(v1));
2959  
2960          checkCompletedWithWrappedCFException(g);
2961          checkCompletedNormally(f, v1);
# Line 2778 | Line 2982 | public class CompletableFutureTest exten
2982          checkCancelled(f);
2983      }}
2984  
2985 +    /**
2986 +     * thenCompose result completes exceptionally if the result of the action does
2987 +     */
2988 +    public void testThenCompose_actionReturnsFailingFuture() {
2989 +        for (ExecutionMode m : ExecutionMode.values())
2990 +        for (int order = 0; order < 6; order++)
2991 +        for (Integer v1 : new Integer[] { 1, null })
2992 +    {
2993 +        final CFException ex = new CFException();
2994 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2995 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2996 +        final CompletableFuture<Integer> h;
2997 +        // Test all permutations of orders
2998 +        switch (order) {
2999 +        case 0:
3000 +            assertTrue(f.complete(v1));
3001 +            assertTrue(g.completeExceptionally(ex));
3002 +            h = m.thenCompose(f, (x -> g));
3003 +            break;
3004 +        case 1:
3005 +            assertTrue(f.complete(v1));
3006 +            h = m.thenCompose(f, (x -> g));
3007 +            assertTrue(g.completeExceptionally(ex));
3008 +            break;
3009 +        case 2:
3010 +            assertTrue(g.completeExceptionally(ex));
3011 +            assertTrue(f.complete(v1));
3012 +            h = m.thenCompose(f, (x -> g));
3013 +            break;
3014 +        case 3:
3015 +            assertTrue(g.completeExceptionally(ex));
3016 +            h = m.thenCompose(f, (x -> g));
3017 +            assertTrue(f.complete(v1));
3018 +            break;
3019 +        case 4:
3020 +            h = m.thenCompose(f, (x -> g));
3021 +            assertTrue(f.complete(v1));
3022 +            assertTrue(g.completeExceptionally(ex));
3023 +            break;
3024 +        case 5:
3025 +            h = m.thenCompose(f, (x -> g));
3026 +            assertTrue(f.complete(v1));
3027 +            assertTrue(g.completeExceptionally(ex));
3028 +            break;
3029 +        default: throw new AssertionError();
3030 +        }
3031 +
3032 +        checkCompletedExceptionally(g, ex);
3033 +        checkCompletedWithWrappedException(h, ex);
3034 +        checkCompletedNormally(f, v1);
3035 +    }}
3036 +
3037      // other static methods
3038  
3039      /**
# Line 2794 | Line 3050 | public class CompletableFutureTest exten
3050       * when all components complete normally
3051       */
3052      public void testAllOf_normal() throws Exception {
3053 <        for (int k = 1; k < 20; ++k) {
3053 >        for (int k = 1; k < 10; k++) {
3054              CompletableFuture<Integer>[] fs
3055                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3056 <            for (int i = 0; i < k; ++i)
3056 >            for (int i = 0; i < k; i++)
3057                  fs[i] = new CompletableFuture<>();
3058              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3059 <            for (int i = 0; i < k; ++i) {
3059 >            for (int i = 0; i < k; i++) {
3060                  checkIncomplete(f);
3061                  checkIncomplete(CompletableFuture.allOf(fs));
3062                  fs[i].complete(one);
# Line 2811 | Line 3067 | public class CompletableFutureTest exten
3067      }
3068  
3069      public void testAllOf_backwards() throws Exception {
3070 <        for (int k = 1; k < 20; ++k) {
3070 >        for (int k = 1; k < 10; k++) {
3071              CompletableFuture<Integer>[] fs
3072                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3073 <            for (int i = 0; i < k; ++i)
3073 >            for (int i = 0; i < k; i++)
3074                  fs[i] = new CompletableFuture<>();
3075              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3076              for (int i = k - 1; i >= 0; i--) {
# Line 2827 | Line 3083 | public class CompletableFutureTest exten
3083          }
3084      }
3085  
3086 +    public void testAllOf_exceptional() throws Exception {
3087 +        for (int k = 1; k < 10; k++) {
3088 +            CompletableFuture<Integer>[] fs
3089 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3090 +            CFException ex = new CFException();
3091 +            for (int i = 0; i < k; i++)
3092 +                fs[i] = new CompletableFuture<>();
3093 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3094 +            for (int i = 0; i < k; i++) {
3095 +                checkIncomplete(f);
3096 +                checkIncomplete(CompletableFuture.allOf(fs));
3097 +                if (i != k / 2) {
3098 +                    fs[i].complete(i);
3099 +                    checkCompletedNormally(fs[i], i);
3100 +                } else {
3101 +                    fs[i].completeExceptionally(ex);
3102 +                    checkCompletedExceptionally(fs[i], ex);
3103 +                }
3104 +            }
3105 +            checkCompletedWithWrappedException(f, ex);
3106 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3107 +        }
3108 +    }
3109 +
3110      /**
3111       * anyOf(no component futures) returns an incomplete future
3112       */
3113      public void testAnyOf_empty() throws Exception {
3114 +        for (Integer v1 : new Integer[] { 1, null })
3115 +    {
3116          CompletableFuture<Object> f = CompletableFuture.anyOf();
3117          checkIncomplete(f);
3118 <    }
3118 >
3119 >        f.complete(v1);
3120 >        checkCompletedNormally(f, v1);
3121 >    }}
3122  
3123      /**
3124       * anyOf returns a future completed normally with a value when
3125       * a component future does
3126       */
3127      public void testAnyOf_normal() throws Exception {
3128 <        for (int k = 0; k < 10; ++k) {
3128 >        for (int k = 0; k < 10; k++) {
3129              CompletableFuture[] fs = new CompletableFuture[k];
3130 <            for (int i = 0; i < k; ++i)
3130 >            for (int i = 0; i < k; i++)
3131                  fs[i] = new CompletableFuture<>();
3132              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3133              checkIncomplete(f);
3134 <            for (int i = 0; i < k; ++i) {
3135 <                fs[i].complete(one);
3136 <                checkCompletedNormally(f, one);
3137 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3134 >            for (int i = 0; i < k; i++) {
3135 >                fs[i].complete(i);
3136 >                checkCompletedNormally(f, 0);
3137 >                int x = (int) CompletableFuture.anyOf(fs).join();
3138 >                assertTrue(0 <= x && x <= i);
3139 >            }
3140 >        }
3141 >    }
3142 >    public void testAnyOf_normal_backwards() throws Exception {
3143 >        for (int k = 0; k < 10; k++) {
3144 >            CompletableFuture[] fs = new CompletableFuture[k];
3145 >            for (int i = 0; i < k; i++)
3146 >                fs[i] = new CompletableFuture<>();
3147 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3148 >            checkIncomplete(f);
3149 >            for (int i = k - 1; i >= 0; i--) {
3150 >                fs[i].complete(i);
3151 >                checkCompletedNormally(f, k - 1);
3152 >                int x = (int) CompletableFuture.anyOf(fs).join();
3153 >                assertTrue(i <= x && x <= k - 1);
3154              }
3155          }
3156      }
# Line 2858 | Line 3159 | public class CompletableFutureTest exten
3159       * anyOf result completes exceptionally when any component does.
3160       */
3161      public void testAnyOf_exceptional() throws Exception {
3162 <        for (int k = 0; k < 10; ++k) {
3162 >        for (int k = 0; k < 10; k++) {
3163 >            CompletableFuture[] fs = new CompletableFuture[k];
3164 >            CFException[] exs = new CFException[k];
3165 >            for (int i = 0; i < k; i++) {
3166 >                fs[i] = new CompletableFuture<>();
3167 >                exs[i] = new CFException();
3168 >            }
3169 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3170 >            checkIncomplete(f);
3171 >            for (int i = 0; i < k; i++) {
3172 >                fs[i].completeExceptionally(exs[i]);
3173 >                checkCompletedWithWrappedException(f, exs[0]);
3174 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3175 >            }
3176 >        }
3177 >    }
3178 >
3179 >    public void testAnyOf_exceptional_backwards() throws Exception {
3180 >        for (int k = 0; k < 10; k++) {
3181              CompletableFuture[] fs = new CompletableFuture[k];
3182 <            for (int i = 0; i < k; ++i)
3182 >            CFException[] exs = new CFException[k];
3183 >            for (int i = 0; i < k; i++) {
3184                  fs[i] = new CompletableFuture<>();
3185 +                exs[i] = new CFException();
3186 +            }
3187              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3188              checkIncomplete(f);
3189 <            for (int i = 0; i < k; ++i) {
3190 <                fs[i].completeExceptionally(new CFException());
3191 <                checkCompletedWithWrappedCFException(f);
3189 >            for (int i = k - 1; i >= 0; i--) {
3190 >                fs[i].completeExceptionally(exs[i]);
3191 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3192                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3193              }
3194          }
# Line 2879 | Line 3201 | public class CompletableFutureTest exten
3201          CompletableFuture<Integer> f = new CompletableFuture<>();
3202          CompletableFuture<Integer> g = new CompletableFuture<>();
3203          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2882        CompletableFuture<?> h;
3204          ThreadExecutor exec = new ThreadExecutor();
3205  
3206          Runnable[] throwingActions = {
3207              () -> CompletableFuture.supplyAsync(null),
3208              () -> CompletableFuture.supplyAsync(null, exec),
3209 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3209 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3210  
3211              () -> CompletableFuture.runAsync(null),
3212              () -> CompletableFuture.runAsync(null, exec),
# Line 2976 | Line 3297 | public class CompletableFutureTest exten
3297              () -> CompletableFuture.anyOf(null, f),
3298  
3299              () -> f.obtrudeException(null),
3300 +
3301 +            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3302 +            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3303 +            () -> CompletableFuture.delayedExecutor(1L, null),
3304 +
3305 +            () -> f.orTimeout(1L, null),
3306 +            () -> f.completeOnTimeout(42, 1L, null),
3307 +
3308 +            () -> CompletableFuture.failedFuture(null),
3309 +            () -> CompletableFuture.failedStage(null),
3310          };
3311  
3312          assertThrows(NullPointerException.class, throwingActions);
# Line 2990 | Line 3321 | public class CompletableFutureTest exten
3321          assertSame(f, f.toCompletableFuture());
3322      }
3323  
3324 +    // jdk9
3325 +
3326 +    /**
3327 +     * newIncompleteFuture returns an incomplete CompletableFuture
3328 +     */
3329 +    public void testNewIncompleteFuture() {
3330 +        for (Integer v1 : new Integer[] { 1, null })
3331 +    {
3332 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3333 +        CompletableFuture<Integer> g = f.newIncompleteFuture();
3334 +        checkIncomplete(f);
3335 +        checkIncomplete(g);
3336 +        f.complete(v1);
3337 +        checkCompletedNormally(f, v1);
3338 +        checkIncomplete(g);
3339 +        g.complete(v1);
3340 +        checkCompletedNormally(g, v1);
3341 +        assertSame(g.getClass(), CompletableFuture.class);
3342 +    }}
3343 +
3344 +    /**
3345 +     * completedStage returns a completed CompletionStage
3346 +     */
3347 +    public void testCompletedStage() {
3348 +        AtomicInteger x = new AtomicInteger(0);
3349 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3350 +        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3351 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3352 +        assertEquals(x.get(), 1);
3353 +        assertNull(r.get());
3354 +    }
3355 +
3356 +    /**
3357 +     * defaultExecutor by default returns the commonPool if
3358 +     * it supports more than one thread.
3359 +     */
3360 +    public void testDefaultExecutor() {
3361 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3362 +        Executor e = f.defaultExecutor();
3363 +        Executor c = ForkJoinPool.commonPool();
3364 +        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3365 +            assertSame(e, c);
3366 +        else
3367 +            assertNotSame(e, c);
3368 +    }
3369 +
3370 +    /**
3371 +     * failedFuture returns a CompletableFuture completed
3372 +     * exceptionally with the given Exception
3373 +     */
3374 +    public void testFailedFuture() {
3375 +        CFException ex = new CFException();
3376 +        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3377 +        checkCompletedExceptionally(f, ex);
3378 +    }
3379 +
3380 +    /**
3381 +     * failedFuture(null) throws NPE
3382 +     */
3383 +    public void testFailedFuture_null() {
3384 +        try {
3385 +            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3386 +            shouldThrow();
3387 +        } catch (NullPointerException success) {}
3388 +    }
3389 +
3390 +    /**
3391 +     * copy returns a CompletableFuture that is completed normally,
3392 +     * with the same value, when source is.
3393 +     */
3394 +    public void testCopy() {
3395 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3396 +        CompletableFuture<Integer> g = f.copy();
3397 +        checkIncomplete(f);
3398 +        checkIncomplete(g);
3399 +        f.complete(1);
3400 +        checkCompletedNormally(f, 1);
3401 +        checkCompletedNormally(g, 1);
3402 +    }
3403 +
3404 +    /**
3405 +     * copy returns a CompletableFuture that is completed exceptionally
3406 +     * when source is.
3407 +     */
3408 +    public void testCopy2() {
3409 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3410 +        CompletableFuture<Integer> g = f.copy();
3411 +        checkIncomplete(f);
3412 +        checkIncomplete(g);
3413 +        CFException ex = new CFException();
3414 +        f.completeExceptionally(ex);
3415 +        checkCompletedExceptionally(f, ex);
3416 +        checkCompletedWithWrappedException(g, ex);
3417 +    }
3418 +
3419 +    /**
3420 +     * minimalCompletionStage returns a CompletableFuture that is
3421 +     * completed normally, with the same value, when source is.
3422 +     */
3423 +    public void testMinimalCompletionStage() {
3424 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3425 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3426 +        AtomicInteger x = new AtomicInteger(0);
3427 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3428 +        checkIncomplete(f);
3429 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3430 +        f.complete(1);
3431 +        checkCompletedNormally(f, 1);
3432 +        assertEquals(x.get(), 1);
3433 +        assertNull(r.get());
3434 +    }
3435 +
3436 +    /**
3437 +     * minimalCompletionStage returns a CompletableFuture that is
3438 +     * completed exceptionally when source is.
3439 +     */
3440 +    public void testMinimalCompletionStage2() {
3441 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3442 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3443 +        AtomicInteger x = new AtomicInteger(0);
3444 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3445 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3446 +        checkIncomplete(f);
3447 +        CFException ex = new CFException();
3448 +        f.completeExceptionally(ex);
3449 +        checkCompletedExceptionally(f, ex);
3450 +        assertEquals(x.get(), 0);
3451 +        assertEquals(r.get().getCause(), ex);
3452 +    }
3453 +
3454 +    /**
3455 +     * failedStage returns a CompletionStage completed
3456 +     * exceptionally with the given Exception
3457 +     */
3458 +    public void testFailedStage() {
3459 +        CFException ex = new CFException();
3460 +        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3461 +        AtomicInteger x = new AtomicInteger(0);
3462 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3463 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3464 +        assertEquals(x.get(), 0);
3465 +        assertEquals(r.get(), ex);
3466 +    }
3467 +
3468 +    /**
3469 +     * completeAsync completes with value of given supplier
3470 +     */
3471 +    public void testCompleteAsync() {
3472 +        for (Integer v1 : new Integer[] { 1, null })
3473 +    {
3474 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3475 +        f.completeAsync(() -> v1);
3476 +        f.join();
3477 +        checkCompletedNormally(f, v1);
3478 +    }}
3479 +
3480 +    /**
3481 +     * completeAsync completes exceptionally if given supplier throws
3482 +     */
3483 +    public void testCompleteAsync2() {
3484 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3485 +        CFException ex = new CFException();
3486 +        f.completeAsync(() -> {if (true) throw ex; return 1;});
3487 +        try {
3488 +            f.join();
3489 +            shouldThrow();
3490 +        } catch (CompletionException success) {}
3491 +        checkCompletedWithWrappedException(f, ex);
3492 +    }
3493 +
3494 +    /**
3495 +     * completeAsync with given executor completes with value of given supplier
3496 +     */
3497 +    public void testCompleteAsync3() {
3498 +        for (Integer v1 : new Integer[] { 1, null })
3499 +    {
3500 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3501 +        ThreadExecutor executor = new ThreadExecutor();
3502 +        f.completeAsync(() -> v1, executor);
3503 +        assertSame(v1, f.join());
3504 +        checkCompletedNormally(f, v1);
3505 +        assertEquals(1, executor.count.get());
3506 +    }}
3507 +
3508 +    /**
3509 +     * completeAsync with given executor completes exceptionally if
3510 +     * given supplier throws
3511 +     */
3512 +    public void testCompleteAsync4() {
3513 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3514 +        CFException ex = new CFException();
3515 +        ThreadExecutor executor = new ThreadExecutor();
3516 +        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3517 +        try {
3518 +            f.join();
3519 +            shouldThrow();
3520 +        } catch (CompletionException success) {}
3521 +        checkCompletedWithWrappedException(f, ex);
3522 +        assertEquals(1, executor.count.get());
3523 +    }
3524 +
3525 +    /**
3526 +     * orTimeout completes with TimeoutException if not complete
3527 +     */
3528 +    public void testOrTimeout_timesOut() {
3529 +        long timeoutMillis = timeoutMillis();
3530 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3531 +        long startTime = System.nanoTime();
3532 +        f.orTimeout(timeoutMillis, MILLISECONDS);
3533 +        checkCompletedWithTimeoutException(f);
3534 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3535 +    }
3536 +
3537 +    /**
3538 +     * orTimeout completes normally if completed before timeout
3539 +     */
3540 +    public void testOrTimeout_completed() {
3541 +        for (Integer v1 : new Integer[] { 1, null })
3542 +    {
3543 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3544 +        CompletableFuture<Integer> g = new CompletableFuture<>();
3545 +        long startTime = System.nanoTime();
3546 +        f.complete(v1);
3547 +        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3548 +        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3549 +        g.complete(v1);
3550 +        checkCompletedNormally(f, v1);
3551 +        checkCompletedNormally(g, v1);
3552 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3553 +    }}
3554 +
3555 +    /**
3556 +     * completeOnTimeout completes with given value if not complete
3557 +     */
3558 +    public void testCompleteOnTimeout_timesOut() {
3559 +        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3560 +                       () -> testCompleteOnTimeout_timesOut(null));
3561 +    }
3562 +
3563 +    public void testCompleteOnTimeout_timesOut(Integer v) {
3564 +        long timeoutMillis = timeoutMillis();
3565 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3566 +        long startTime = System.nanoTime();
3567 +        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3568 +        assertSame(v, f.join());
3569 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3570 +        f.complete(99);         // should have no effect
3571 +        checkCompletedNormally(f, v);
3572 +    }
3573 +
3574 +    /**
3575 +     * completeOnTimeout has no effect if completed within timeout
3576 +     */
3577 +    public void testCompleteOnTimeout_completed() {
3578 +        for (Integer v1 : new Integer[] { 1, null })
3579 +    {
3580 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3581 +        CompletableFuture<Integer> g = new CompletableFuture<>();
3582 +        long startTime = System.nanoTime();
3583 +        f.complete(v1);
3584 +        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3585 +        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3586 +        g.complete(v1);
3587 +        checkCompletedNormally(f, v1);
3588 +        checkCompletedNormally(g, v1);
3589 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3590 +    }}
3591 +
3592 +    /**
3593 +     * delayedExecutor returns an executor that delays submission
3594 +     */
3595 +    public void testDelayedExecutor() {
3596 +        testInParallel(() -> testDelayedExecutor(null, null),
3597 +                       () -> testDelayedExecutor(null, 1),
3598 +                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3599 +                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3600 +    }
3601 +
3602 +    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3603 +        long timeoutMillis = timeoutMillis();
3604 +        // Use an "unreasonably long" long timeout to catch lingering threads
3605 +        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3606 +        final Executor delayer, longDelayer;
3607 +        if (executor == null) {
3608 +            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3609 +            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3610 +        } else {
3611 +            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3612 +            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3613 +        }
3614 +        long startTime = System.nanoTime();
3615 +        CompletableFuture<Integer> f =
3616 +            CompletableFuture.supplyAsync(() -> v, delayer);
3617 +        CompletableFuture<Integer> g =
3618 +            CompletableFuture.supplyAsync(() -> v, longDelayer);
3619 +
3620 +        assertNull(g.getNow(null));
3621 +
3622 +        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3623 +        long millisElapsed = millisElapsedSince(startTime);
3624 +        assertTrue(millisElapsed >= timeoutMillis);
3625 +        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3626 +
3627 +        checkCompletedNormally(f, v);
3628 +
3629 +        checkIncomplete(g);
3630 +        assertTrue(g.cancel(true));
3631 +    }
3632 +
3633 +    //--- tests of implementation details; not part of official tck ---
3634 +
3635 +    Object resultOf(CompletableFuture<?> f) {
3636 +        try {
3637 +            java.lang.reflect.Field resultField
3638 +                = CompletableFuture.class.getDeclaredField("result");
3639 +            resultField.setAccessible(true);
3640 +            return resultField.get(f);
3641 +        } catch (Throwable t) { throw new AssertionError(t); }
3642 +    }
3643 +
3644 +    public void testExceptionPropagationReusesResultObject() {
3645 +        if (!testImplementationDetails) return;
3646 +        for (ExecutionMode m : ExecutionMode.values())
3647 +    {
3648 +        final CFException ex = new CFException();
3649 +        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3650 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3651 +
3652 +        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3653 +            = new ArrayList<>();
3654 +
3655 +        funs.add((y) -> m.thenRun(y, new Noop(m)));
3656 +        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3657 +        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3658 +
3659 +        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3660 +        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3661 +        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3662 +
3663 +        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3664 +        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3665 +        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3666 +
3667 +        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3668 +
3669 +        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3670 +
3671 +        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3672 +        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3673 +
3674 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3675 +                 fun : funs) {
3676 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3677 +            f.completeExceptionally(ex);
3678 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3679 +            checkCompletedWithWrappedException(src, ex);
3680 +            CompletableFuture<?> dep = fun.apply(src);
3681 +            checkCompletedWithWrappedException(dep, ex);
3682 +            assertSame(resultOf(src), resultOf(dep));
3683 +        }
3684 +
3685 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3686 +                 fun : funs) {
3687 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3688 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3689 +            CompletableFuture<?> dep = fun.apply(src);
3690 +            f.completeExceptionally(ex);
3691 +            checkCompletedWithWrappedException(src, ex);
3692 +            checkCompletedWithWrappedException(dep, ex);
3693 +            assertSame(resultOf(src), resultOf(dep));
3694 +        }
3695 +
3696 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3697 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3698 +                 fun : funs) {
3699 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3700 +            f.cancel(mayInterruptIfRunning);
3701 +            checkCancelled(f);
3702 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3703 +            checkCompletedWithWrappedCancellationException(src);
3704 +            CompletableFuture<?> dep = fun.apply(src);
3705 +            checkCompletedWithWrappedCancellationException(dep);
3706 +            assertSame(resultOf(src), resultOf(dep));
3707 +        }
3708 +
3709 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3710 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3711 +                 fun : funs) {
3712 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3713 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3714 +            CompletableFuture<?> dep = fun.apply(src);
3715 +            f.cancel(mayInterruptIfRunning);
3716 +            checkCancelled(f);
3717 +            checkCompletedWithWrappedCancellationException(src);
3718 +            checkCompletedWithWrappedCancellationException(dep);
3719 +            assertSame(resultOf(src), resultOf(dep));
3720 +        }
3721 +    }}
3722 +
3723 +    /**
3724 +     * Minimal completion stages throw UOE for all non-CompletionStage methods
3725 +     */
3726 +    public void testMinimalCompletionStage_minimality() {
3727 +        if (!testImplementationDetails) return;
3728 +        Function<Method, String> toSignature =
3729 +            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3730 +        Predicate<Method> isNotStatic =
3731 +            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3732 +        List<Method> minimalMethods =
3733 +            Stream.of(Object.class, CompletionStage.class)
3734 +            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3735 +            .filter(isNotStatic)
3736 +            .collect(Collectors.toList());
3737 +        // Methods from CompletableFuture permitted NOT to throw UOE
3738 +        String[] signatureWhitelist = {
3739 +            "newIncompleteFuture[]",
3740 +            "defaultExecutor[]",
3741 +            "minimalCompletionStage[]",
3742 +            "copy[]",
3743 +        };
3744 +        Set<String> permittedMethodSignatures =
3745 +            Stream.concat(minimalMethods.stream().map(toSignature),
3746 +                          Stream.of(signatureWhitelist))
3747 +            .collect(Collectors.toSet());
3748 +        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3749 +            .filter(isNotStatic)
3750 +            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3751 +            .collect(Collectors.toList());
3752 +
3753 +        CompletionStage<Integer> minimalStage =
3754 +            new CompletableFuture<Integer>().minimalCompletionStage();
3755 +
3756 +        List<Method> bugs = new ArrayList<>();
3757 +        for (Method method : allMethods) {
3758 +            Class<?>[] parameterTypes = method.getParameterTypes();
3759 +            Object[] args = new Object[parameterTypes.length];
3760 +            // Manufacture boxed primitives for primitive params
3761 +            for (int i = 0; i < args.length; i++) {
3762 +                Class<?> type = parameterTypes[i];
3763 +                if (parameterTypes[i] == boolean.class)
3764 +                    args[i] = false;
3765 +                else if (parameterTypes[i] == int.class)
3766 +                    args[i] = 0;
3767 +                else if (parameterTypes[i] == long.class)
3768 +                    args[i] = 0L;
3769 +            }
3770 +            try {
3771 +                method.invoke(minimalStage, args);
3772 +                bugs.add(method);
3773 +            }
3774 +            catch (java.lang.reflect.InvocationTargetException expected) {
3775 +                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3776 +                    bugs.add(method);
3777 +                    // expected.getCause().printStackTrace();
3778 +                }
3779 +            }
3780 +            catch (ReflectiveOperationException bad) { throw new Error(bad); }
3781 +        }
3782 +        if (!bugs.isEmpty())
3783 +            throw new Error("Methods did not throw UOE: " + bugs.toString());
3784 +    }
3785 +
3786 +    static class Monad {
3787 +        static class ZeroException extends RuntimeException {
3788 +            public ZeroException() { super("monadic zero"); }
3789 +        }
3790 +        // "return", "unit"
3791 +        static <T> CompletableFuture<T> unit(T value) {
3792 +            return completedFuture(value);
3793 +        }
3794 +        // monadic zero ?
3795 +        static <T> CompletableFuture<T> zero() {
3796 +            return failedFuture(new ZeroException());
3797 +        }
3798 +        // >=>
3799 +        static <T,U,V> Function<T, CompletableFuture<V>> compose
3800 +            (Function<T, CompletableFuture<U>> f,
3801 +             Function<U, CompletableFuture<V>> g) {
3802 +            return (x) -> f.apply(x).thenCompose(g);
3803 +        }
3804 +
3805 +        static void assertZero(CompletableFuture<?> f) {
3806 +            try {
3807 +                f.getNow(null);
3808 +                throw new AssertionFailedError("should throw");
3809 +            } catch (CompletionException success) {
3810 +                assertTrue(success.getCause() instanceof ZeroException);
3811 +            }
3812 +        }
3813 +
3814 +        static <T> void assertFutureEquals(CompletableFuture<T> f,
3815 +                                           CompletableFuture<T> g) {
3816 +            T fval = null, gval = null;
3817 +            Throwable fex = null, gex = null;
3818 +
3819 +            try { fval = f.get(); }
3820 +            catch (ExecutionException ex) { fex = ex.getCause(); }
3821 +            catch (Throwable ex) { fex = ex; }
3822 +
3823 +            try { gval = g.get(); }
3824 +            catch (ExecutionException ex) { gex = ex.getCause(); }
3825 +            catch (Throwable ex) { gex = ex; }
3826 +
3827 +            if (fex != null || gex != null)
3828 +                assertSame(fex.getClass(), gex.getClass());
3829 +            else
3830 +                assertEquals(fval, gval);
3831 +        }
3832 +
3833 +        static class PlusFuture<T> extends CompletableFuture<T> {
3834 +            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3835 +        }
3836 +
3837 +        /** Implements "monadic plus". */
3838 +        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3839 +                                             CompletableFuture<? extends T> g) {
3840 +            PlusFuture<T> plus = new PlusFuture<T>();
3841 +            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3842 +                try {
3843 +                    if (ex == null) {
3844 +                        if (plus.complete(result))
3845 +                            if (plus.firstFailure.get() != null)
3846 +                                plus.firstFailure.set(null);
3847 +                    }
3848 +                    else if (plus.firstFailure.compareAndSet(null, ex)) {
3849 +                        if (plus.isDone())
3850 +                            plus.firstFailure.set(null);
3851 +                    }
3852 +                    else {
3853 +                        // first failure has precedence
3854 +                        Throwable first = plus.firstFailure.getAndSet(null);
3855 +
3856 +                        // may fail with "Self-suppression not permitted"
3857 +                        try { first.addSuppressed(ex); }
3858 +                        catch (Exception ignored) {}
3859 +
3860 +                        plus.completeExceptionally(first);
3861 +                    }
3862 +                } catch (Throwable unexpected) {
3863 +                    plus.completeExceptionally(unexpected);
3864 +                }
3865 +            };
3866 +            f.whenComplete(action);
3867 +            g.whenComplete(action);
3868 +            return plus;
3869 +        }
3870 +    }
3871 +
3872 +    /**
3873 +     * CompletableFuture is an additive monad - sort of.
3874 +     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3875 +     */
3876 +    public void testAdditiveMonad() throws Throwable {
3877 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3878 +        CompletableFuture<Long> zero = Monad.zero();
3879 +
3880 +        // Some mutually non-commutative functions
3881 +        Function<Long, CompletableFuture<Long>> triple
3882 +            = (x) -> Monad.unit(3 * x);
3883 +        Function<Long, CompletableFuture<Long>> inc
3884 +            = (x) -> Monad.unit(x + 1);
3885 +
3886 +        // unit is a right identity: m >>= unit === m
3887 +        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3888 +                                 inc.apply(5L));
3889 +        // unit is a left identity: (unit x) >>= f === f x
3890 +        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3891 +                                 inc.apply(5L));
3892 +
3893 +        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3894 +        Monad.assertFutureEquals(
3895 +            unit.apply(5L).thenCompose(inc).thenCompose(triple),
3896 +            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3897 +
3898 +        // The case for CompletableFuture as an additive monad is weaker...
3899 +
3900 +        // zero is a monadic zero
3901 +        Monad.assertZero(zero);
3902 +
3903 +        // left zero: zero >>= f === zero
3904 +        Monad.assertZero(zero.thenCompose(inc));
3905 +        // right zero: f >>= (\x -> zero) === zero
3906 +        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3907 +
3908 +        // f plus zero === f
3909 +        Monad.assertFutureEquals(Monad.unit(5L),
3910 +                                 Monad.plus(Monad.unit(5L), zero));
3911 +        // zero plus f === f
3912 +        Monad.assertFutureEquals(Monad.unit(5L),
3913 +                                 Monad.plus(zero, Monad.unit(5L)));
3914 +        // zero plus zero === zero
3915 +        Monad.assertZero(Monad.plus(zero, zero));
3916 +        {
3917 +            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3918 +                                                   Monad.unit(8L));
3919 +            // non-determinism
3920 +            assertTrue(f.get() == 5L || f.get() == 8L);
3921 +        }
3922 +
3923 +        CompletableFuture<Long> godot = new CompletableFuture<>();
3924 +        // f plus godot === f (doesn't wait for godot)
3925 +        Monad.assertFutureEquals(Monad.unit(5L),
3926 +                                 Monad.plus(Monad.unit(5L), godot));
3927 +        // godot plus f === f (doesn't wait for godot)
3928 +        Monad.assertFutureEquals(Monad.unit(5L),
3929 +                                 Monad.plus(godot, Monad.unit(5L)));
3930 +    }
3931 +
3932 +    /**
3933 +     * A single CompletableFuture with many dependents.
3934 +     * A demo of scalability - runtime is O(n).
3935 +     */
3936 +    public void testManyDependents() throws Throwable {
3937 +        final int n = 1_000;
3938 +        final CompletableFuture<Void> head = new CompletableFuture<>();
3939 +        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
3940 +        final AtomicInteger count = new AtomicInteger(0);
3941 +        for (int i = 0; i < n; i++) {
3942 +            head.thenRun(() -> count.getAndIncrement());
3943 +            head.thenAccept((x) -> count.getAndIncrement());
3944 +            head.thenApply((x) -> count.getAndIncrement());
3945 +
3946 +            head.runAfterBoth(complete, () -> count.getAndIncrement());
3947 +            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
3948 +            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
3949 +            complete.runAfterBoth(head, () -> count.getAndIncrement());
3950 +            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
3951 +            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
3952 +
3953 +            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
3954 +            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3955 +            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3956 +            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
3957 +            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
3958 +            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
3959 +        }
3960 +        head.complete(null);
3961 +        assertEquals(5 * 3 * n, count.get());
3962 +    }
3963 +
3964 + //     static <U> U join(CompletionStage<U> stage) {
3965 + //         CompletableFuture<U> f = new CompletableFuture<>();
3966 + //         stage.whenComplete((v, ex) -> {
3967 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3968 + //         });
3969 + //         return f.join();
3970 + //     }
3971 +
3972 + //     static <U> boolean isDone(CompletionStage<U> stage) {
3973 + //         CompletableFuture<U> f = new CompletableFuture<>();
3974 + //         stage.whenComplete((v, ex) -> {
3975 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3976 + //         });
3977 + //         return f.isDone();
3978 + //     }
3979 +
3980 + //     static <U> U join2(CompletionStage<U> stage) {
3981 + //         return stage.toCompletableFuture().copy().join();
3982 + //     }
3983 +
3984 + //     static <U> boolean isDone2(CompletionStage<U> stage) {
3985 + //         return stage.toCompletableFuture().copy().isDone();
3986 + //     }
3987 +
3988   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines