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.132 by jsr166, Sun Nov 15 19:37:48 2015 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());
347 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
348 <
349 <        f = new CompletableFuture<String>();
350 <        f.cancel(true);
346 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
347          assertTrue(f.toString().contains("[Completed exceptionally]"));
348  
349 <        f = new CompletableFuture<String>();
350 <        f.cancel(false);
351 <        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 >                assertEquals(defaultExecutorIsCommonPool,
644 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
645              }
646              public CompletableFuture<Void> runAsync(Runnable a) {
647                  return CompletableFuture.runAsync(a);
# Line 837 | Line 837 | public class CompletableFutureTest exten
837      {
838          final AtomicInteger a = new AtomicInteger(0);
839          final CompletableFuture<Integer> f = new CompletableFuture<>();
840 <        if (!createIncomplete) f.complete(v1);
840 >        if (!createIncomplete) assertTrue(f.complete(v1));
841          final CompletableFuture<Integer> g = f.exceptionally
842              ((Throwable t) -> {
843                // Should not be called
843                  a.getAndIncrement();
844 <                throw new AssertionError();
844 >                threadFail("should not be called");
845 >                return null;            // unreached
846              });
847 <        if (createIncomplete) f.complete(v1);
847 >        if (createIncomplete) assertTrue(f.complete(v1));
848  
849          checkCompletedNormally(g, v1);
850          checkCompletedNormally(f, v1);
# Line 865 | Line 865 | public class CompletableFutureTest exten
865          if (!createIncomplete) f.completeExceptionally(ex);
866          final CompletableFuture<Integer> g = f.exceptionally
867              ((Throwable t) -> {
868 <                ExecutionMode.DEFAULT.checkExecutionMode();
868 >                ExecutionMode.SYNC.checkExecutionMode();
869                  threadAssertSame(t, ex);
870                  a.getAndIncrement();
871                  return v1;
# Line 878 | Line 878 | public class CompletableFutureTest exten
878  
879      public void testExceptionally_exceptionalCompletionActionFailed() {
880          for (boolean createIncomplete : new boolean[] { true, false })
881        for (Integer v1 : new Integer[] { 1, null })
881      {
882          final AtomicInteger a = new AtomicInteger(0);
883          final CFException ex1 = new CFException();
# Line 887 | Line 886 | public class CompletableFutureTest exten
886          if (!createIncomplete) f.completeExceptionally(ex1);
887          final CompletableFuture<Integer> g = f.exceptionally
888              ((Throwable t) -> {
889 <                ExecutionMode.DEFAULT.checkExecutionMode();
889 >                ExecutionMode.SYNC.checkExecutionMode();
890                  threadAssertSame(t, ex1);
891                  a.getAndIncrement();
892                  throw ex2;
# Line 902 | Line 901 | public class CompletableFutureTest exten
901       * whenComplete action executes on normal completion, propagating
902       * source result.
903       */
904 <    public void testWhenComplete_normalCompletion1() {
904 >    public void testWhenComplete_normalCompletion() {
905          for (ExecutionMode m : ExecutionMode.values())
906          for (boolean createIncomplete : new boolean[] { true, false })
907          for (Integer v1 : new Integer[] { 1, null })
908      {
909          final AtomicInteger a = new AtomicInteger(0);
910          final CompletableFuture<Integer> f = new CompletableFuture<>();
911 <        if (!createIncomplete) f.complete(v1);
911 >        if (!createIncomplete) assertTrue(f.complete(v1));
912          final CompletableFuture<Integer> g = m.whenComplete
913              (f,
914               (Integer x, Throwable t) -> {
# Line 918 | Line 917 | public class CompletableFutureTest exten
917                  threadAssertNull(t);
918                  a.getAndIncrement();
919              });
920 <        if (createIncomplete) f.complete(v1);
920 >        if (createIncomplete) assertTrue(f.complete(v1));
921  
922          checkCompletedNormally(g, v1);
923          checkCompletedNormally(f, v1);
# Line 932 | Line 931 | public class CompletableFutureTest exten
931      public void testWhenComplete_exceptionalCompletion() {
932          for (ExecutionMode m : ExecutionMode.values())
933          for (boolean createIncomplete : new boolean[] { true, false })
935        for (Integer v1 : new Integer[] { 1, null })
934      {
935          final AtomicInteger a = new AtomicInteger(0);
936          final CFException ex = new CFException();
# Line 984 | Line 982 | public class CompletableFutureTest exten
982       * If a whenComplete action throws an exception when triggered by
983       * a normal completion, it completes exceptionally
984       */
985 <    public void testWhenComplete_actionFailed() {
985 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
986          for (boolean createIncomplete : new boolean[] { true, false })
987          for (ExecutionMode m : ExecutionMode.values())
988          for (Integer v1 : new Integer[] { 1, null })
# Line 992 | Line 990 | public class CompletableFutureTest exten
990          final AtomicInteger a = new AtomicInteger(0);
991          final CFException ex = new CFException();
992          final CompletableFuture<Integer> f = new CompletableFuture<>();
993 <        if (!createIncomplete) f.complete(v1);
993 >        if (!createIncomplete) assertTrue(f.complete(v1));
994          final CompletableFuture<Integer> g = m.whenComplete
995              (f,
996               (Integer x, Throwable t) -> {
# Line 1002 | Line 1000 | public class CompletableFutureTest exten
1000                  a.getAndIncrement();
1001                  throw ex;
1002              });
1003 <        if (createIncomplete) f.complete(v1);
1003 >        if (createIncomplete) assertTrue(f.complete(v1));
1004  
1005          checkCompletedWithWrappedException(g, ex);
1006          checkCompletedNormally(f, v1);
# Line 1017 | Line 1015 | public class CompletableFutureTest exten
1015      public void testWhenComplete_actionFailedSourceFailed() {
1016          for (boolean createIncomplete : new boolean[] { true, false })
1017          for (ExecutionMode m : ExecutionMode.values())
1020        for (Integer v1 : new Integer[] { 1, null })
1018      {
1019          final AtomicInteger a = new AtomicInteger(0);
1020          final CFException ex1 = new CFException();
# Line 1052 | Line 1049 | public class CompletableFutureTest exten
1049      {
1050          final CompletableFuture<Integer> f = new CompletableFuture<>();
1051          final AtomicInteger a = new AtomicInteger(0);
1052 <        if (!createIncomplete) f.complete(v1);
1052 >        if (!createIncomplete) assertTrue(f.complete(v1));
1053          final CompletableFuture<Integer> g = m.handle
1054              (f,
1055               (Integer x, Throwable t) -> {
# Line 1062 | Line 1059 | public class CompletableFutureTest exten
1059                  a.getAndIncrement();
1060                  return inc(v1);
1061              });
1062 <        if (createIncomplete) f.complete(v1);
1062 >        if (createIncomplete) assertTrue(f.complete(v1));
1063  
1064          checkCompletedNormally(g, inc(v1));
1065          checkCompletedNormally(f, v1);
# Line 1163 | Line 1160 | public class CompletableFutureTest exten
1160          final CompletableFuture<Integer> f = new CompletableFuture<>();
1161          final AtomicInteger a = new AtomicInteger(0);
1162          final CFException ex = new CFException();
1163 <        if (!createIncomplete) f.complete(v1);
1163 >        if (!createIncomplete) assertTrue(f.complete(v1));
1164          final CompletableFuture<Integer> g = m.handle
1165              (f,
1166               (Integer x, Throwable t) -> {
# Line 1173 | Line 1170 | public class CompletableFutureTest exten
1170                  a.getAndIncrement();
1171                  throw ex;
1172              });
1173 <        if (createIncomplete) f.complete(v1);
1173 >        if (createIncomplete) assertTrue(f.complete(v1));
1174  
1175          checkCompletedWithWrappedException(g, ex);
1176          checkCompletedNormally(f, v1);
# Line 1254 | Line 1251 | public class CompletableFutureTest exten
1251       */
1252      public void testThenRun_normalCompletion() {
1253          for (ExecutionMode m : ExecutionMode.values())
1257        for (boolean createIncomplete : new boolean[] { true, false })
1254          for (Integer v1 : new Integer[] { 1, null })
1255      {
1256          final CompletableFuture<Integer> f = new CompletableFuture<>();
1257 <        final Noop r = new Noop(m);
1258 <        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 <        }
1257 >        final Noop[] rs = new Noop[6];
1258 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1259  
1260 <        checkCompletedNormally(g, null);
1260 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1261 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1262 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1263 >        checkIncomplete(h0);
1264 >        checkIncomplete(h1);
1265 >        checkIncomplete(h2);
1266 >        assertTrue(f.complete(v1));
1267 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1268 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1269 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1270 >
1271 >        checkCompletedNormally(h0, null);
1272 >        checkCompletedNormally(h1, null);
1273 >        checkCompletedNormally(h2, null);
1274 >        checkCompletedNormally(h3, null);
1275 >        checkCompletedNormally(h4, null);
1276 >        checkCompletedNormally(h5, null);
1277          checkCompletedNormally(f, v1);
1278 <        r.assertInvoked();
1278 >        for (Noop r : rs) r.assertInvoked();
1279      }}
1280  
1281      /**
# Line 1277 | Line 1284 | public class CompletableFutureTest exten
1284       */
1285      public void testThenRun_exceptionalCompletion() {
1286          for (ExecutionMode m : ExecutionMode.values())
1280        for (boolean createIncomplete : new boolean[] { true, false })
1287      {
1288          final CFException ex = new CFException();
1289          final CompletableFuture<Integer> f = new CompletableFuture<>();
1290 <        final Noop r = new Noop(m);
1291 <        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 <        }
1290 >        final Noop[] rs = new Noop[6];
1291 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1292  
1293 <        checkCompletedWithWrappedException(g, ex);
1293 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1294 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1295 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1296 >        checkIncomplete(h0);
1297 >        checkIncomplete(h1);
1298 >        checkIncomplete(h2);
1299 >        assertTrue(f.completeExceptionally(ex));
1300 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1301 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1302 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1303 >
1304 >        checkCompletedWithWrappedException(h0, ex);
1305 >        checkCompletedWithWrappedException(h1, ex);
1306 >        checkCompletedWithWrappedException(h2, ex);
1307 >        checkCompletedWithWrappedException(h3, ex);
1308 >        checkCompletedWithWrappedException(h4, ex);
1309 >        checkCompletedWithWrappedException(h5, ex);
1310          checkCompletedExceptionally(f, ex);
1311 <        r.assertNotInvoked();
1311 >        for (Noop r : rs) r.assertNotInvoked();
1312      }}
1313  
1314      /**
# Line 1299 | Line 1316 | public class CompletableFutureTest exten
1316       */
1317      public void testThenRun_sourceCancelled() {
1318          for (ExecutionMode m : ExecutionMode.values())
1302        for (boolean createIncomplete : new boolean[] { true, false })
1319          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1320      {
1321          final CompletableFuture<Integer> f = new CompletableFuture<>();
1322 <        final Noop r = new Noop(m);
1323 <        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 <        }
1322 >        final Noop[] rs = new Noop[6];
1323 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1324  
1325 <        checkCompletedWithWrappedCancellationException(g);
1325 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1326 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1327 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1328 >        checkIncomplete(h0);
1329 >        checkIncomplete(h1);
1330 >        checkIncomplete(h2);
1331 >        assertTrue(f.cancel(mayInterruptIfRunning));
1332 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1333 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1334 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1335 >
1336 >        checkCompletedWithWrappedCancellationException(h0);
1337 >        checkCompletedWithWrappedCancellationException(h1);
1338 >        checkCompletedWithWrappedCancellationException(h2);
1339 >        checkCompletedWithWrappedCancellationException(h3);
1340 >        checkCompletedWithWrappedCancellationException(h4);
1341 >        checkCompletedWithWrappedCancellationException(h5);
1342          checkCancelled(f);
1343 <        r.assertNotInvoked();
1343 >        for (Noop r : rs) r.assertNotInvoked();
1344      }}
1345  
1346      /**
# Line 1321 | Line 1348 | public class CompletableFutureTest exten
1348       */
1349      public void testThenRun_actionFailed() {
1350          for (ExecutionMode m : ExecutionMode.values())
1324        for (boolean createIncomplete : new boolean[] { true, false })
1351          for (Integer v1 : new Integer[] { 1, null })
1352      {
1353          final CompletableFuture<Integer> f = new CompletableFuture<>();
1354 <        final FailingRunnable r = new FailingRunnable(m);
1355 <        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 <        }
1354 >        final FailingRunnable[] rs = new FailingRunnable[6];
1355 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1356  
1357 <        checkCompletedWithWrappedCFException(g);
1357 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1358 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1359 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1360 >        assertTrue(f.complete(v1));
1361 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1362 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1363 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1364 >
1365 >        checkCompletedWithWrappedCFException(h0);
1366 >        checkCompletedWithWrappedCFException(h1);
1367 >        checkCompletedWithWrappedCFException(h2);
1368 >        checkCompletedWithWrappedCFException(h3);
1369 >        checkCompletedWithWrappedCFException(h4);
1370 >        checkCompletedWithWrappedCFException(h5);
1371          checkCompletedNormally(f, v1);
1372      }}
1373  
# Line 1342 | Line 1376 | public class CompletableFutureTest exten
1376       */
1377      public void testThenApply_normalCompletion() {
1378          for (ExecutionMode m : ExecutionMode.values())
1345        for (boolean createIncomplete : new boolean[] { true, false })
1379          for (Integer v1 : new Integer[] { 1, null })
1380      {
1381          final CompletableFuture<Integer> f = new CompletableFuture<>();
1382 <        final IncFunction r = new IncFunction(m);
1383 <        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 <        }
1382 >        final IncFunction[] rs = new IncFunction[4];
1383 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1384  
1385 <        checkCompletedNormally(g, inc(v1));
1385 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1386 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1387 >        checkIncomplete(h0);
1388 >        checkIncomplete(h1);
1389 >        assertTrue(f.complete(v1));
1390 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1391 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1392 >
1393 >        checkCompletedNormally(h0, inc(v1));
1394 >        checkCompletedNormally(h1, inc(v1));
1395 >        checkCompletedNormally(h2, inc(v1));
1396 >        checkCompletedNormally(h3, inc(v1));
1397          checkCompletedNormally(f, v1);
1398 <        r.assertValue(inc(v1));
1398 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1399      }}
1400  
1401      /**
# Line 1365 | Line 1404 | public class CompletableFutureTest exten
1404       */
1405      public void testThenApply_exceptionalCompletion() {
1406          for (ExecutionMode m : ExecutionMode.values())
1368        for (boolean createIncomplete : new boolean[] { true, false })
1407      {
1408          final CFException ex = new CFException();
1409          final CompletableFuture<Integer> f = new CompletableFuture<>();
1410 <        final IncFunction r = new IncFunction(m);
1411 <        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 <        }
1410 >        final IncFunction[] rs = new IncFunction[4];
1411 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1412  
1413 <        checkCompletedWithWrappedException(g, ex);
1413 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1414 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1415 >        assertTrue(f.completeExceptionally(ex));
1416 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1417 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1418 >
1419 >        checkCompletedWithWrappedException(h0, ex);
1420 >        checkCompletedWithWrappedException(h1, ex);
1421 >        checkCompletedWithWrappedException(h2, ex);
1422 >        checkCompletedWithWrappedException(h3, ex);
1423          checkCompletedExceptionally(f, ex);
1424 <        r.assertNotInvoked();
1424 >        for (IncFunction r : rs) r.assertNotInvoked();
1425      }}
1426  
1427      /**
# Line 1387 | Line 1429 | public class CompletableFutureTest exten
1429       */
1430      public void testThenApply_sourceCancelled() {
1431          for (ExecutionMode m : ExecutionMode.values())
1390        for (boolean createIncomplete : new boolean[] { true, false })
1432          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1433      {
1434          final CompletableFuture<Integer> f = new CompletableFuture<>();
1435 <        final IncFunction r = new IncFunction(m);
1436 <        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 <        }
1435 >        final IncFunction[] rs = new IncFunction[4];
1436 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1437  
1438 <        checkCompletedWithWrappedCancellationException(g);
1438 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1439 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1440 >        assertTrue(f.cancel(mayInterruptIfRunning));
1441 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1442 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1443 >
1444 >        checkCompletedWithWrappedCancellationException(h0);
1445 >        checkCompletedWithWrappedCancellationException(h1);
1446 >        checkCompletedWithWrappedCancellationException(h2);
1447 >        checkCompletedWithWrappedCancellationException(h3);
1448          checkCancelled(f);
1449 <        r.assertNotInvoked();
1449 >        for (IncFunction r : rs) r.assertNotInvoked();
1450      }}
1451  
1452      /**
# Line 1409 | Line 1454 | public class CompletableFutureTest exten
1454       */
1455      public void testThenApply_actionFailed() {
1456          for (ExecutionMode m : ExecutionMode.values())
1412        for (boolean createIncomplete : new boolean[] { true, false })
1457          for (Integer v1 : new Integer[] { 1, null })
1458      {
1459          final CompletableFuture<Integer> f = new CompletableFuture<>();
1460 <        final FailingFunction r = new FailingFunction(m);
1461 <        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 <        }
1460 >        final FailingFunction[] rs = new FailingFunction[4];
1461 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1462  
1463 <        checkCompletedWithWrappedCFException(g);
1463 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1464 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1465 >        assertTrue(f.complete(v1));
1466 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1467 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1468 >
1469 >        checkCompletedWithWrappedCFException(h0);
1470 >        checkCompletedWithWrappedCFException(h1);
1471 >        checkCompletedWithWrappedCFException(h2);
1472 >        checkCompletedWithWrappedCFException(h3);
1473          checkCompletedNormally(f, v1);
1474      }}
1475  
# Line 1430 | Line 1478 | public class CompletableFutureTest exten
1478       */
1479      public void testThenAccept_normalCompletion() {
1480          for (ExecutionMode m : ExecutionMode.values())
1433        for (boolean createIncomplete : new boolean[] { true, false })
1481          for (Integer v1 : new Integer[] { 1, null })
1482      {
1483          final CompletableFuture<Integer> f = new CompletableFuture<>();
1484 <        final NoopConsumer r = new NoopConsumer(m);
1485 <        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 <        }
1484 >        final NoopConsumer[] rs = new NoopConsumer[4];
1485 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1486  
1487 <        checkCompletedNormally(g, null);
1488 <        r.assertValue(v1);
1487 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1488 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1489 >        checkIncomplete(h0);
1490 >        checkIncomplete(h1);
1491 >        assertTrue(f.complete(v1));
1492 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1493 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1494 >
1495 >        checkCompletedNormally(h0, null);
1496 >        checkCompletedNormally(h1, null);
1497 >        checkCompletedNormally(h2, null);
1498 >        checkCompletedNormally(h3, null);
1499          checkCompletedNormally(f, v1);
1500 +        for (NoopConsumer r : rs) r.assertValue(v1);
1501      }}
1502  
1503      /**
# Line 1453 | Line 1506 | public class CompletableFutureTest exten
1506       */
1507      public void testThenAccept_exceptionalCompletion() {
1508          for (ExecutionMode m : ExecutionMode.values())
1456        for (boolean createIncomplete : new boolean[] { true, false })
1509      {
1510          final CFException ex = new CFException();
1511          final CompletableFuture<Integer> f = new CompletableFuture<>();
1512 <        final NoopConsumer r = new NoopConsumer(m);
1513 <        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 <        }
1512 >        final NoopConsumer[] rs = new NoopConsumer[4];
1513 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1514  
1515 <        checkCompletedWithWrappedException(g, ex);
1515 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1516 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1517 >        assertTrue(f.completeExceptionally(ex));
1518 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1519 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1520 >
1521 >        checkCompletedWithWrappedException(h0, ex);
1522 >        checkCompletedWithWrappedException(h1, ex);
1523 >        checkCompletedWithWrappedException(h2, ex);
1524 >        checkCompletedWithWrappedException(h3, ex);
1525          checkCompletedExceptionally(f, ex);
1526 <        r.assertNotInvoked();
1526 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1527      }}
1528  
1529      /**
# Line 1475 | Line 1531 | public class CompletableFutureTest exten
1531       */
1532      public void testThenAccept_sourceCancelled() {
1533          for (ExecutionMode m : ExecutionMode.values())
1478        for (boolean createIncomplete : new boolean[] { true, false })
1534          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1535      {
1536          final CompletableFuture<Integer> f = new CompletableFuture<>();
1537 <        final NoopConsumer r = new NoopConsumer(m);
1538 <        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 <        }
1537 >        final NoopConsumer[] rs = new NoopConsumer[4];
1538 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1539  
1540 <        checkCompletedWithWrappedCancellationException(g);
1540 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1541 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1542 >        assertTrue(f.cancel(mayInterruptIfRunning));
1543 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1544 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1545 >
1546 >        checkCompletedWithWrappedCancellationException(h0);
1547 >        checkCompletedWithWrappedCancellationException(h1);
1548 >        checkCompletedWithWrappedCancellationException(h2);
1549 >        checkCompletedWithWrappedCancellationException(h3);
1550          checkCancelled(f);
1551 <        r.assertNotInvoked();
1551 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1552      }}
1553  
1554      /**
# Line 1497 | Line 1556 | public class CompletableFutureTest exten
1556       */
1557      public void testThenAccept_actionFailed() {
1558          for (ExecutionMode m : ExecutionMode.values())
1500        for (boolean createIncomplete : new boolean[] { true, false })
1559          for (Integer v1 : new Integer[] { 1, null })
1560      {
1561          final CompletableFuture<Integer> f = new CompletableFuture<>();
1562 <        final FailingConsumer r = new FailingConsumer(m);
1563 <        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 <        }
1562 >        final FailingConsumer[] rs = new FailingConsumer[4];
1563 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1564  
1565 <        checkCompletedWithWrappedCFException(g);
1565 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1566 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1567 >        assertTrue(f.complete(v1));
1568 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1569 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1570 >
1571 >        checkCompletedWithWrappedCFException(h0);
1572 >        checkCompletedWithWrappedCFException(h1);
1573 >        checkCompletedWithWrappedCFException(h2);
1574 >        checkCompletedWithWrappedCFException(h3);
1575          checkCompletedNormally(f, v1);
1576      }}
1577  
# Line 1519 | Line 1581 | public class CompletableFutureTest exten
1581       */
1582      public void testThenCombine_normalCompletion() {
1583          for (ExecutionMode m : ExecutionMode.values())
1522        for (boolean createIncomplete : new boolean[] { true, false })
1584          for (boolean fFirst : new boolean[] { true, false })
1585          for (Integer v1 : new Integer[] { 1, null })
1586          for (Integer v2 : new Integer[] { 2, null })
1587      {
1588          final CompletableFuture<Integer> f = new CompletableFuture<>();
1589          final CompletableFuture<Integer> g = new CompletableFuture<>();
1590 <        final SubtractFunction r = new SubtractFunction(m);
1590 >        final SubtractFunction[] rs = new SubtractFunction[6];
1591 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1592  
1593 <        if (fFirst) f.complete(v1); else g.complete(v2);
1594 <        if (!createIncomplete)
1595 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1596 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1597 <        if (createIncomplete) {
1598 <            checkIncomplete(h);
1599 <            r.assertNotInvoked();
1600 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1601 <        }
1593 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1594 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1595 >        final Integer w1 =  fFirst ? v1 : v2;
1596 >        final Integer w2 = !fFirst ? v1 : v2;
1597 >
1598 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1599 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1600 >        assertTrue(fst.complete(w1));
1601 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1602 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1603 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1604 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1605 >        checkCompletedNormally(h1, subtract(w1, w1));
1606 >        checkCompletedNormally(h3, subtract(w1, w1));
1607 >        rs[1].assertValue(subtract(w1, w1));
1608 >        rs[3].assertValue(subtract(w1, w1));
1609 >        assertTrue(snd.complete(w2));
1610 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1611 >
1612 >        checkCompletedNormally(h0, subtract(v1, v2));
1613 >        checkCompletedNormally(h2, subtract(v1, v2));
1614 >        checkCompletedNormally(h4, subtract(v1, v2));
1615 >        rs[0].assertValue(subtract(v1, v2));
1616 >        rs[2].assertValue(subtract(v1, v2));
1617 >        rs[4].assertValue(subtract(v1, v2));
1618  
1541        checkCompletedNormally(h, subtract(v1, v2));
1619          checkCompletedNormally(f, v1);
1620          checkCompletedNormally(g, v2);
1544        r.assertValue(subtract(v1, v2));
1621      }}
1622  
1623      /**
1624       * thenCombine result completes exceptionally after exceptional
1625       * completion of either source
1626       */
1627 <    public void testThenCombine_exceptionalCompletion() {
1627 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1628          for (ExecutionMode m : ExecutionMode.values())
1553        for (boolean createIncomplete : new boolean[] { true, false })
1629          for (boolean fFirst : new boolean[] { true, false })
1630 +        for (boolean failFirst : new boolean[] { true, false })
1631          for (Integer v1 : new Integer[] { 1, null })
1632      {
1633          final CompletableFuture<Integer> f = new CompletableFuture<>();
1634          final CompletableFuture<Integer> g = new CompletableFuture<>();
1635          final CFException ex = new CFException();
1636 <        final SubtractFunction r = new SubtractFunction(m);
1637 <
1638 <        (fFirst ? f : g).complete(v1);
1639 <        if (!createIncomplete)
1640 <            (!fFirst ? f : g).completeExceptionally(ex);
1641 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1642 <        if (createIncomplete) {
1643 <            checkIncomplete(h);
1644 <            (!fFirst ? f : g).completeExceptionally(ex);
1645 <        }
1636 >        final SubtractFunction r1 = new SubtractFunction(m);
1637 >        final SubtractFunction r2 = new SubtractFunction(m);
1638 >        final SubtractFunction r3 = new SubtractFunction(m);
1639 >
1640 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1641 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1642 >        final Callable<Boolean> complete1 = failFirst ?
1643 >            () -> fst.completeExceptionally(ex) :
1644 >            () -> fst.complete(v1);
1645 >        final Callable<Boolean> complete2 = failFirst ?
1646 >            () -> snd.complete(v1) :
1647 >            () -> snd.completeExceptionally(ex);
1648 >
1649 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1650 >        assertTrue(complete1.call());
1651 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1652 >        checkIncomplete(h1);
1653 >        checkIncomplete(h2);
1654 >        assertTrue(complete2.call());
1655 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1656  
1657 <        checkCompletedWithWrappedException(h, ex);
1658 <        r.assertNotInvoked();
1659 <        checkCompletedNormally(fFirst ? f : g, v1);
1660 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1657 >        checkCompletedWithWrappedException(h1, ex);
1658 >        checkCompletedWithWrappedException(h2, ex);
1659 >        checkCompletedWithWrappedException(h3, ex);
1660 >        r1.assertNotInvoked();
1661 >        r2.assertNotInvoked();
1662 >        r3.assertNotInvoked();
1663 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1664 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1665      }}
1666  
1667      /**
1668       * thenCombine result completes exceptionally if either source cancelled
1669       */
1670 <    public void testThenCombine_sourceCancelled() {
1670 >    public void testThenCombine_sourceCancelled() throws Throwable {
1671          for (ExecutionMode m : ExecutionMode.values())
1672          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1583        for (boolean createIncomplete : new boolean[] { true, false })
1673          for (boolean fFirst : new boolean[] { true, false })
1674 +        for (boolean failFirst : new boolean[] { true, false })
1675          for (Integer v1 : new Integer[] { 1, null })
1676      {
1677          final CompletableFuture<Integer> f = new CompletableFuture<>();
1678          final CompletableFuture<Integer> g = new CompletableFuture<>();
1679 <        final SubtractFunction r = new SubtractFunction(m);
1679 >        final SubtractFunction r1 = new SubtractFunction(m);
1680 >        final SubtractFunction r2 = new SubtractFunction(m);
1681 >        final SubtractFunction r3 = new SubtractFunction(m);
1682  
1683 <        (fFirst ? f : g).complete(v1);
1684 <        if (!createIncomplete)
1685 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1686 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1687 <        if (createIncomplete) {
1688 <            checkIncomplete(h);
1689 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1690 <        }
1683 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1684 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1685 >        final Callable<Boolean> complete1 = failFirst ?
1686 >            () -> fst.cancel(mayInterruptIfRunning) :
1687 >            () -> fst.complete(v1);
1688 >        final Callable<Boolean> complete2 = failFirst ?
1689 >            () -> snd.complete(v1) :
1690 >            () -> snd.cancel(mayInterruptIfRunning);
1691  
1692 <        checkCompletedWithWrappedCancellationException(h);
1693 <        checkCancelled(!fFirst ? f : g);
1694 <        r.assertNotInvoked();
1695 <        checkCompletedNormally(fFirst ? f : g, v1);
1692 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1693 >        assertTrue(complete1.call());
1694 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1695 >        checkIncomplete(h1);
1696 >        checkIncomplete(h2);
1697 >        assertTrue(complete2.call());
1698 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1699 >
1700 >        checkCompletedWithWrappedCancellationException(h1);
1701 >        checkCompletedWithWrappedCancellationException(h2);
1702 >        checkCompletedWithWrappedCancellationException(h3);
1703 >        r1.assertNotInvoked();
1704 >        r2.assertNotInvoked();
1705 >        r3.assertNotInvoked();
1706 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1707 >        checkCancelled(failFirst ? fst : snd);
1708      }}
1709  
1710      /**
# Line 1614 | Line 1718 | public class CompletableFutureTest exten
1718      {
1719          final CompletableFuture<Integer> f = new CompletableFuture<>();
1720          final CompletableFuture<Integer> g = new CompletableFuture<>();
1721 <        final FailingBiFunction r = new FailingBiFunction(m);
1722 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1721 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1722 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1723 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1724 >
1725 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1726 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1727 >        final Integer w1 =  fFirst ? v1 : v2;
1728 >        final Integer w2 = !fFirst ? v1 : v2;
1729 >
1730 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1731 >        assertTrue(fst.complete(w1));
1732 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1733 >        assertTrue(snd.complete(w2));
1734 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1735  
1736 <        if (fFirst) {
1737 <            f.complete(v1);
1738 <            g.complete(v2);
1739 <        } else {
1740 <            g.complete(v2);
1741 <            f.complete(v1);
1626 <        }
1627 <
1628 <        checkCompletedWithWrappedCFException(h);
1736 >        checkCompletedWithWrappedCFException(h1);
1737 >        checkCompletedWithWrappedCFException(h2);
1738 >        checkCompletedWithWrappedCFException(h3);
1739 >        r1.assertInvoked();
1740 >        r2.assertInvoked();
1741 >        r3.assertInvoked();
1742          checkCompletedNormally(f, v1);
1743          checkCompletedNormally(g, v2);
1744      }}
# Line 1636 | Line 1749 | public class CompletableFutureTest exten
1749       */
1750      public void testThenAcceptBoth_normalCompletion() {
1751          for (ExecutionMode m : ExecutionMode.values())
1639        for (boolean createIncomplete : new boolean[] { true, false })
1752          for (boolean fFirst : new boolean[] { true, false })
1753          for (Integer v1 : new Integer[] { 1, null })
1754          for (Integer v2 : new Integer[] { 2, null })
1755      {
1756          final CompletableFuture<Integer> f = new CompletableFuture<>();
1757          final CompletableFuture<Integer> g = new CompletableFuture<>();
1758 <        final SubtractAction r = new SubtractAction(m);
1759 <
1760 <        if (fFirst) f.complete(v1); else g.complete(v2);
1761 <        if (!createIncomplete)
1762 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1763 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1764 <        if (createIncomplete) {
1765 <            checkIncomplete(h);
1766 <            r.assertNotInvoked();
1767 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1768 <        }
1758 >        final SubtractAction r1 = new SubtractAction(m);
1759 >        final SubtractAction r2 = new SubtractAction(m);
1760 >        final SubtractAction r3 = new SubtractAction(m);
1761 >
1762 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1763 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1764 >        final Integer w1 =  fFirst ? v1 : v2;
1765 >        final Integer w2 = !fFirst ? v1 : v2;
1766 >
1767 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1768 >        assertTrue(fst.complete(w1));
1769 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1770 >        checkIncomplete(h1);
1771 >        checkIncomplete(h2);
1772 >        r1.assertNotInvoked();
1773 >        r2.assertNotInvoked();
1774 >        assertTrue(snd.complete(w2));
1775 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1776  
1777 <        checkCompletedNormally(h, null);
1778 <        r.assertValue(subtract(v1, v2));
1777 >        checkCompletedNormally(h1, null);
1778 >        checkCompletedNormally(h2, null);
1779 >        checkCompletedNormally(h3, null);
1780 >        r1.assertValue(subtract(v1, v2));
1781 >        r2.assertValue(subtract(v1, v2));
1782 >        r3.assertValue(subtract(v1, v2));
1783          checkCompletedNormally(f, v1);
1784          checkCompletedNormally(g, v2);
1785      }}
# Line 1665 | Line 1788 | public class CompletableFutureTest exten
1788       * thenAcceptBoth result completes exceptionally after exceptional
1789       * completion of either source
1790       */
1791 <    public void testThenAcceptBoth_exceptionalCompletion() {
1791 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1792          for (ExecutionMode m : ExecutionMode.values())
1670        for (boolean createIncomplete : new boolean[] { true, false })
1793          for (boolean fFirst : new boolean[] { true, false })
1794 +        for (boolean failFirst : new boolean[] { true, false })
1795          for (Integer v1 : new Integer[] { 1, null })
1796      {
1797          final CompletableFuture<Integer> f = new CompletableFuture<>();
1798          final CompletableFuture<Integer> g = new CompletableFuture<>();
1799          final CFException ex = new CFException();
1800 <        final SubtractAction r = new SubtractAction(m);
1801 <
1802 <        (fFirst ? f : g).complete(v1);
1803 <        if (!createIncomplete)
1804 <            (!fFirst ? f : g).completeExceptionally(ex);
1805 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1806 <        if (createIncomplete) {
1807 <            checkIncomplete(h);
1808 <            (!fFirst ? f : g).completeExceptionally(ex);
1809 <        }
1800 >        final SubtractAction r1 = new SubtractAction(m);
1801 >        final SubtractAction r2 = new SubtractAction(m);
1802 >        final SubtractAction r3 = new SubtractAction(m);
1803 >
1804 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1805 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1806 >        final Callable<Boolean> complete1 = failFirst ?
1807 >            () -> fst.completeExceptionally(ex) :
1808 >            () -> fst.complete(v1);
1809 >        final Callable<Boolean> complete2 = failFirst ?
1810 >            () -> snd.complete(v1) :
1811 >            () -> snd.completeExceptionally(ex);
1812 >
1813 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1814 >        assertTrue(complete1.call());
1815 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1816 >        checkIncomplete(h1);
1817 >        checkIncomplete(h2);
1818 >        assertTrue(complete2.call());
1819 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1820  
1821 <        checkCompletedWithWrappedException(h, ex);
1822 <        r.assertNotInvoked();
1823 <        checkCompletedNormally(fFirst ? f : g, v1);
1824 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1821 >        checkCompletedWithWrappedException(h1, ex);
1822 >        checkCompletedWithWrappedException(h2, ex);
1823 >        checkCompletedWithWrappedException(h3, ex);
1824 >        r1.assertNotInvoked();
1825 >        r2.assertNotInvoked();
1826 >        r3.assertNotInvoked();
1827 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1828 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1829      }}
1830  
1831      /**
1832       * thenAcceptBoth result completes exceptionally if either source cancelled
1833       */
1834 <    public void testThenAcceptBoth_sourceCancelled() {
1834 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1835          for (ExecutionMode m : ExecutionMode.values())
1836          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1700        for (boolean createIncomplete : new boolean[] { true, false })
1837          for (boolean fFirst : new boolean[] { true, false })
1838 +        for (boolean failFirst : new boolean[] { true, false })
1839          for (Integer v1 : new Integer[] { 1, null })
1840      {
1841          final CompletableFuture<Integer> f = new CompletableFuture<>();
1842          final CompletableFuture<Integer> g = new CompletableFuture<>();
1843 <        final SubtractAction r = new SubtractAction(m);
1843 >        final SubtractAction r1 = new SubtractAction(m);
1844 >        final SubtractAction r2 = new SubtractAction(m);
1845 >        final SubtractAction r3 = new SubtractAction(m);
1846  
1847 <        (fFirst ? f : g).complete(v1);
1848 <        if (!createIncomplete)
1849 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1850 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1851 <        if (createIncomplete) {
1852 <            checkIncomplete(h);
1853 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1854 <        }
1847 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1848 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1849 >        final Callable<Boolean> complete1 = failFirst ?
1850 >            () -> fst.cancel(mayInterruptIfRunning) :
1851 >            () -> fst.complete(v1);
1852 >        final Callable<Boolean> complete2 = failFirst ?
1853 >            () -> snd.complete(v1) :
1854 >            () -> snd.cancel(mayInterruptIfRunning);
1855  
1856 <        checkCompletedWithWrappedCancellationException(h);
1857 <        checkCancelled(!fFirst ? f : g);
1858 <        r.assertNotInvoked();
1859 <        checkCompletedNormally(fFirst ? f : g, v1);
1856 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1857 >        assertTrue(complete1.call());
1858 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1859 >        checkIncomplete(h1);
1860 >        checkIncomplete(h2);
1861 >        assertTrue(complete2.call());
1862 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1863 >
1864 >        checkCompletedWithWrappedCancellationException(h1);
1865 >        checkCompletedWithWrappedCancellationException(h2);
1866 >        checkCompletedWithWrappedCancellationException(h3);
1867 >        r1.assertNotInvoked();
1868 >        r2.assertNotInvoked();
1869 >        r3.assertNotInvoked();
1870 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1871 >        checkCancelled(failFirst ? fst : snd);
1872      }}
1873  
1874      /**
# Line 1731 | Line 1882 | public class CompletableFutureTest exten
1882      {
1883          final CompletableFuture<Integer> f = new CompletableFuture<>();
1884          final CompletableFuture<Integer> g = new CompletableFuture<>();
1885 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1886 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1885 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1886 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1887 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1888 >
1889 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1890 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1891 >        final Integer w1 =  fFirst ? v1 : v2;
1892 >        final Integer w2 = !fFirst ? v1 : v2;
1893 >
1894 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1895 >        assertTrue(fst.complete(w1));
1896 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1897 >        assertTrue(snd.complete(w2));
1898 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1899  
1900 <        if (fFirst) {
1901 <            f.complete(v1);
1902 <            g.complete(v2);
1903 <        } else {
1904 <            g.complete(v2);
1905 <            f.complete(v1);
1743 <        }
1744 <
1745 <        checkCompletedWithWrappedCFException(h);
1900 >        checkCompletedWithWrappedCFException(h1);
1901 >        checkCompletedWithWrappedCFException(h2);
1902 >        checkCompletedWithWrappedCFException(h3);
1903 >        r1.assertInvoked();
1904 >        r2.assertInvoked();
1905 >        r3.assertInvoked();
1906          checkCompletedNormally(f, v1);
1907          checkCompletedNormally(g, v2);
1908      }}
# Line 1753 | Line 1913 | public class CompletableFutureTest exten
1913       */
1914      public void testRunAfterBoth_normalCompletion() {
1915          for (ExecutionMode m : ExecutionMode.values())
1756        for (boolean createIncomplete : new boolean[] { true, false })
1916          for (boolean fFirst : new boolean[] { true, false })
1917          for (Integer v1 : new Integer[] { 1, null })
1918          for (Integer v2 : new Integer[] { 2, null })
1919      {
1920          final CompletableFuture<Integer> f = new CompletableFuture<>();
1921          final CompletableFuture<Integer> g = new CompletableFuture<>();
1922 <        final Noop r = new Noop(m);
1923 <
1924 <        if (fFirst) f.complete(v1); else g.complete(v2);
1925 <        if (!createIncomplete)
1926 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1927 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1928 <        if (createIncomplete) {
1929 <            checkIncomplete(h);
1930 <            r.assertNotInvoked();
1931 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1932 <        }
1922 >        final Noop r1 = new Noop(m);
1923 >        final Noop r2 = new Noop(m);
1924 >        final Noop r3 = new Noop(m);
1925 >
1926 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1927 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1928 >        final Integer w1 =  fFirst ? v1 : v2;
1929 >        final Integer w2 = !fFirst ? v1 : v2;
1930 >
1931 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1932 >        assertTrue(fst.complete(w1));
1933 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1934 >        checkIncomplete(h1);
1935 >        checkIncomplete(h2);
1936 >        r1.assertNotInvoked();
1937 >        r2.assertNotInvoked();
1938 >        assertTrue(snd.complete(w2));
1939 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1940  
1941 <        checkCompletedNormally(h, null);
1942 <        r.assertInvoked();
1941 >        checkCompletedNormally(h1, null);
1942 >        checkCompletedNormally(h2, null);
1943 >        checkCompletedNormally(h3, null);
1944 >        r1.assertInvoked();
1945 >        r2.assertInvoked();
1946 >        r3.assertInvoked();
1947          checkCompletedNormally(f, v1);
1948          checkCompletedNormally(g, v2);
1949      }}
# Line 1782 | Line 1952 | public class CompletableFutureTest exten
1952       * runAfterBoth result completes exceptionally after exceptional
1953       * completion of either source
1954       */
1955 <    public void testRunAfterBoth_exceptionalCompletion() {
1955 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1956          for (ExecutionMode m : ExecutionMode.values())
1787        for (boolean createIncomplete : new boolean[] { true, false })
1957          for (boolean fFirst : new boolean[] { true, false })
1958 +        for (boolean failFirst : new boolean[] { true, false })
1959          for (Integer v1 : new Integer[] { 1, null })
1960      {
1961          final CompletableFuture<Integer> f = new CompletableFuture<>();
1962          final CompletableFuture<Integer> g = new CompletableFuture<>();
1963          final CFException ex = new CFException();
1964 <        final Noop r = new Noop(m);
1965 <
1966 <        (fFirst ? f : g).complete(v1);
1967 <        if (!createIncomplete)
1968 <            (!fFirst ? f : g).completeExceptionally(ex);
1969 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1970 <        if (createIncomplete) {
1971 <            checkIncomplete(h);
1972 <            (!fFirst ? f : g).completeExceptionally(ex);
1973 <        }
1964 >        final Noop r1 = new Noop(m);
1965 >        final Noop r2 = new Noop(m);
1966 >        final Noop r3 = new Noop(m);
1967 >
1968 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1969 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1970 >        final Callable<Boolean> complete1 = failFirst ?
1971 >            () -> fst.completeExceptionally(ex) :
1972 >            () -> fst.complete(v1);
1973 >        final Callable<Boolean> complete2 = failFirst ?
1974 >            () -> snd.complete(v1) :
1975 >            () -> snd.completeExceptionally(ex);
1976 >
1977 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1978 >        assertTrue(complete1.call());
1979 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1980 >        checkIncomplete(h1);
1981 >        checkIncomplete(h2);
1982 >        assertTrue(complete2.call());
1983 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1984  
1985 <        checkCompletedWithWrappedException(h, ex);
1986 <        r.assertNotInvoked();
1987 <        checkCompletedNormally(fFirst ? f : g, v1);
1988 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1985 >        checkCompletedWithWrappedException(h1, ex);
1986 >        checkCompletedWithWrappedException(h2, ex);
1987 >        checkCompletedWithWrappedException(h3, ex);
1988 >        r1.assertNotInvoked();
1989 >        r2.assertNotInvoked();
1990 >        r3.assertNotInvoked();
1991 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1992 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1993      }}
1994  
1995      /**
1996       * runAfterBoth result completes exceptionally if either source cancelled
1997       */
1998 <    public void testRunAfterBoth_sourceCancelled() {
1998 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
1999          for (ExecutionMode m : ExecutionMode.values())
2000          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1817        for (boolean createIncomplete : new boolean[] { true, false })
2001          for (boolean fFirst : new boolean[] { true, false })
2002 +        for (boolean failFirst : new boolean[] { true, false })
2003          for (Integer v1 : new Integer[] { 1, null })
2004      {
2005          final CompletableFuture<Integer> f = new CompletableFuture<>();
2006          final CompletableFuture<Integer> g = new CompletableFuture<>();
2007 <        final Noop r = new Noop(m);
2007 >        final Noop r1 = new Noop(m);
2008 >        final Noop r2 = new Noop(m);
2009 >        final Noop r3 = new Noop(m);
2010  
2011 <        (fFirst ? f : g).complete(v1);
2012 <        if (!createIncomplete)
2013 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2014 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2015 <        if (createIncomplete) {
2016 <            checkIncomplete(h);
2017 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2018 <        }
2011 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2012 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2013 >        final Callable<Boolean> complete1 = failFirst ?
2014 >            () -> fst.cancel(mayInterruptIfRunning) :
2015 >            () -> fst.complete(v1);
2016 >        final Callable<Boolean> complete2 = failFirst ?
2017 >            () -> snd.complete(v1) :
2018 >            () -> snd.cancel(mayInterruptIfRunning);
2019  
2020 <        checkCompletedWithWrappedCancellationException(h);
2021 <        checkCancelled(!fFirst ? f : g);
2022 <        r.assertNotInvoked();
2023 <        checkCompletedNormally(fFirst ? f : g, v1);
2020 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2021 >        assertTrue(complete1.call());
2022 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2023 >        checkIncomplete(h1);
2024 >        checkIncomplete(h2);
2025 >        assertTrue(complete2.call());
2026 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2027 >
2028 >        checkCompletedWithWrappedCancellationException(h1);
2029 >        checkCompletedWithWrappedCancellationException(h2);
2030 >        checkCompletedWithWrappedCancellationException(h3);
2031 >        r1.assertNotInvoked();
2032 >        r2.assertNotInvoked();
2033 >        r3.assertNotInvoked();
2034 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2035 >        checkCancelled(failFirst ? fst : snd);
2036      }}
2037  
2038      /**
# Line 1850 | Line 2048 | public class CompletableFutureTest exten
2048          final CompletableFuture<Integer> g = new CompletableFuture<>();
2049          final FailingRunnable r1 = new FailingRunnable(m);
2050          final FailingRunnable r2 = new FailingRunnable(m);
2051 +        final FailingRunnable r3 = new FailingRunnable(m);
2052  
2053 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2054 <        if (fFirst) {
2055 <            f.complete(v1);
2056 <            g.complete(v2);
2057 <        } else {
2058 <            g.complete(v2);
2059 <            f.complete(v1);
2060 <        }
2061 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2053 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2054 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2055 >        final Integer w1 =  fFirst ? v1 : v2;
2056 >        final Integer w2 = !fFirst ? v1 : v2;
2057 >
2058 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2059 >        assertTrue(fst.complete(w1));
2060 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2061 >        assertTrue(snd.complete(w2));
2062 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2063  
2064          checkCompletedWithWrappedCFException(h1);
2065          checkCompletedWithWrappedCFException(h2);
2066 +        checkCompletedWithWrappedCFException(h3);
2067 +        r1.assertInvoked();
2068 +        r2.assertInvoked();
2069 +        r3.assertInvoked();
2070          checkCompletedNormally(f, v1);
2071          checkCompletedNormally(g, v2);
2072      }}
# Line 1985 | Line 2189 | public class CompletableFutureTest exten
2189  
2190          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2191          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2192 <        if (fFirst) {
2193 <            f.complete(v1);
1990 <            g.completeExceptionally(ex);
1991 <        } else {
1992 <            g.completeExceptionally(ex);
1993 <            f.complete(v1);
1994 <        }
2192 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2193 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2194          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2195          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2196  
# Line 2097 | Line 2296 | public class CompletableFutureTest exten
2296  
2297          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2298          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2299 <        if (fFirst) {
2300 <            f.complete(v1);
2102 <            g.cancel(mayInterruptIfRunning);
2103 <        } else {
2104 <            g.cancel(mayInterruptIfRunning);
2105 <            f.complete(v1);
2106 <        }
2299 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2300 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2301          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2302          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2303  
# Line 2305 | Line 2499 | public class CompletableFutureTest exten
2499  
2500          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2501          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2502 <        if (fFirst) {
2503 <            f.complete(v1);
2310 <            g.completeExceptionally(ex);
2311 <        } else {
2312 <            g.completeExceptionally(ex);
2313 <            f.complete(v1);
2314 <        }
2502 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2503 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2504          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2505          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2506  
# Line 2514 | Line 2703 | public class CompletableFutureTest exten
2703          checkIncomplete(h1);
2704          rs[0].assertNotInvoked();
2705          rs[1].assertNotInvoked();
2706 <        f.completeExceptionally(ex);
2706 >        assertTrue(f.completeExceptionally(ex));
2707          checkCompletedWithWrappedException(h0, ex);
2708          checkCompletedWithWrappedException(h1, ex);
2709          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
# Line 2522 | Line 2711 | public class CompletableFutureTest exten
2711          checkCompletedWithWrappedException(h2, ex);
2712          checkCompletedWithWrappedException(h3, ex);
2713  
2714 <        g.complete(v1);
2714 >        assertTrue(g.complete(v1));
2715  
2716          // unspecified behavior - both source completions available
2717          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2565 | Line 2754 | public class CompletableFutureTest exten
2754  
2755          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2756          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2757 <        if (fFirst) {
2758 <            f.complete(v1);
2570 <            g.completeExceptionally(ex);
2571 <        } else {
2572 <            g.completeExceptionally(ex);
2573 <            f.complete(v1);
2574 <        }
2757 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2758 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2759          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2760          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2761  
# Line 2636 | Line 2820 | public class CompletableFutureTest exten
2820          checkCompletedWithWrappedCancellationException(h2);
2821          checkCompletedWithWrappedCancellationException(h3);
2822  
2823 <        g.complete(v1);
2823 >        assertTrue(g.complete(v1));
2824  
2825          // unspecified behavior - both source completions available
2826          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2680 | Line 2864 | public class CompletableFutureTest exten
2864  
2865          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2866          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2867 <        f.complete(v1);
2867 >        assertTrue(f.complete(v1));
2868          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2869          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2870          checkCompletedWithWrappedCFException(h0);
# Line 2688 | Line 2872 | public class CompletableFutureTest exten
2872          checkCompletedWithWrappedCFException(h2);
2873          checkCompletedWithWrappedCFException(h3);
2874          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2875 <        g.complete(v2);
2875 >        assertTrue(g.complete(v2));
2876          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2877          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2878          checkCompletedWithWrappedCFException(h4);
# Line 2709 | Line 2893 | public class CompletableFutureTest exten
2893      {
2894          final CompletableFuture<Integer> f = new CompletableFuture<>();
2895          final CompletableFutureInc r = new CompletableFutureInc(m);
2896 <        if (!createIncomplete) f.complete(v1);
2896 >        if (!createIncomplete) assertTrue(f.complete(v1));
2897          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2898 <        if (createIncomplete) f.complete(v1);
2898 >        if (createIncomplete) assertTrue(f.complete(v1));
2899  
2900          checkCompletedNormally(g, inc(v1));
2901          checkCompletedNormally(f, v1);
# Line 2749 | Line 2933 | public class CompletableFutureTest exten
2933          final CompletableFuture<Integer> f = new CompletableFuture<>();
2934          final FailingCompletableFutureFunction r
2935              = new FailingCompletableFutureFunction(m);
2936 <        if (!createIncomplete) f.complete(v1);
2936 >        if (!createIncomplete) assertTrue(f.complete(v1));
2937          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2938 <        if (createIncomplete) f.complete(v1);
2938 >        if (createIncomplete) assertTrue(f.complete(v1));
2939  
2940          checkCompletedWithWrappedCFException(g);
2941          checkCompletedNormally(f, v1);
# Line 2778 | Line 2962 | public class CompletableFutureTest exten
2962          checkCancelled(f);
2963      }}
2964  
2965 +    /**
2966 +     * thenCompose result completes exceptionally if the result of the action does
2967 +     */
2968 +    public void testThenCompose_actionReturnsFailingFuture() {
2969 +        for (ExecutionMode m : ExecutionMode.values())
2970 +        for (int order = 0; order < 6; order++)
2971 +        for (Integer v1 : new Integer[] { 1, null })
2972 +    {
2973 +        final CFException ex = new CFException();
2974 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2975 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2976 +        final CompletableFuture<Integer> h;
2977 +        // Test all permutations of orders
2978 +        switch (order) {
2979 +        case 0:
2980 +            assertTrue(f.complete(v1));
2981 +            assertTrue(g.completeExceptionally(ex));
2982 +            h = m.thenCompose(f, (x -> g));
2983 +            break;
2984 +        case 1:
2985 +            assertTrue(f.complete(v1));
2986 +            h = m.thenCompose(f, (x -> g));
2987 +            assertTrue(g.completeExceptionally(ex));
2988 +            break;
2989 +        case 2:
2990 +            assertTrue(g.completeExceptionally(ex));
2991 +            assertTrue(f.complete(v1));
2992 +            h = m.thenCompose(f, (x -> g));
2993 +            break;
2994 +        case 3:
2995 +            assertTrue(g.completeExceptionally(ex));
2996 +            h = m.thenCompose(f, (x -> g));
2997 +            assertTrue(f.complete(v1));
2998 +            break;
2999 +        case 4:
3000 +            h = m.thenCompose(f, (x -> g));
3001 +            assertTrue(f.complete(v1));
3002 +            assertTrue(g.completeExceptionally(ex));
3003 +            break;
3004 +        case 5:
3005 +            h = m.thenCompose(f, (x -> g));
3006 +            assertTrue(f.complete(v1));
3007 +            assertTrue(g.completeExceptionally(ex));
3008 +            break;
3009 +        default: throw new AssertionError();
3010 +        }
3011 +
3012 +        checkCompletedExceptionally(g, ex);
3013 +        checkCompletedWithWrappedException(h, ex);
3014 +        checkCompletedNormally(f, v1);
3015 +    }}
3016 +
3017      // other static methods
3018  
3019      /**
# Line 2794 | Line 3030 | public class CompletableFutureTest exten
3030       * when all components complete normally
3031       */
3032      public void testAllOf_normal() throws Exception {
3033 <        for (int k = 1; k < 20; ++k) {
3033 >        for (int k = 1; k < 10; k++) {
3034              CompletableFuture<Integer>[] fs
3035                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3036 <            for (int i = 0; i < k; ++i)
3036 >            for (int i = 0; i < k; i++)
3037                  fs[i] = new CompletableFuture<>();
3038              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3039 <            for (int i = 0; i < k; ++i) {
3039 >            for (int i = 0; i < k; i++) {
3040                  checkIncomplete(f);
3041                  checkIncomplete(CompletableFuture.allOf(fs));
3042                  fs[i].complete(one);
# Line 2811 | Line 3047 | public class CompletableFutureTest exten
3047      }
3048  
3049      public void testAllOf_backwards() throws Exception {
3050 <        for (int k = 1; k < 20; ++k) {
3050 >        for (int k = 1; k < 10; k++) {
3051              CompletableFuture<Integer>[] fs
3052                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3053 <            for (int i = 0; i < k; ++i)
3053 >            for (int i = 0; i < k; i++)
3054                  fs[i] = new CompletableFuture<>();
3055              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3056              for (int i = k - 1; i >= 0; i--) {
# Line 2827 | Line 3063 | public class CompletableFutureTest exten
3063          }
3064      }
3065  
3066 +    public void testAllOf_exceptional() throws Exception {
3067 +        for (int k = 1; k < 10; k++) {
3068 +            CompletableFuture<Integer>[] fs
3069 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3070 +            CFException ex = new CFException();
3071 +            for (int i = 0; i < k; i++)
3072 +                fs[i] = new CompletableFuture<>();
3073 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3074 +            for (int i = 0; i < k; i++) {
3075 +                checkIncomplete(f);
3076 +                checkIncomplete(CompletableFuture.allOf(fs));
3077 +                if (i != k / 2) {
3078 +                    fs[i].complete(i);
3079 +                    checkCompletedNormally(fs[i], i);
3080 +                } else {
3081 +                    fs[i].completeExceptionally(ex);
3082 +                    checkCompletedExceptionally(fs[i], ex);
3083 +                }
3084 +            }
3085 +            checkCompletedWithWrappedException(f, ex);
3086 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3087 +        }
3088 +    }
3089 +
3090      /**
3091       * anyOf(no component futures) returns an incomplete future
3092       */
3093      public void testAnyOf_empty() throws Exception {
3094 +        for (Integer v1 : new Integer[] { 1, null })
3095 +    {
3096          CompletableFuture<Object> f = CompletableFuture.anyOf();
3097          checkIncomplete(f);
3098 <    }
3098 >
3099 >        f.complete(v1);
3100 >        checkCompletedNormally(f, v1);
3101 >    }}
3102  
3103      /**
3104       * anyOf returns a future completed normally with a value when
3105       * a component future does
3106       */
3107      public void testAnyOf_normal() throws Exception {
3108 <        for (int k = 0; k < 10; ++k) {
3108 >        for (int k = 0; k < 10; k++) {
3109              CompletableFuture[] fs = new CompletableFuture[k];
3110 <            for (int i = 0; i < k; ++i)
3110 >            for (int i = 0; i < k; i++)
3111                  fs[i] = new CompletableFuture<>();
3112              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3113              checkIncomplete(f);
3114 <            for (int i = 0; i < k; ++i) {
3115 <                fs[i].complete(one);
3116 <                checkCompletedNormally(f, one);
3117 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3114 >            for (int i = 0; i < k; i++) {
3115 >                fs[i].complete(i);
3116 >                checkCompletedNormally(f, 0);
3117 >                int x = (int) CompletableFuture.anyOf(fs).join();
3118 >                assertTrue(0 <= x && x <= i);
3119 >            }
3120 >        }
3121 >    }
3122 >    public void testAnyOf_normal_backwards() throws Exception {
3123 >        for (int k = 0; k < 10; k++) {
3124 >            CompletableFuture[] fs = new CompletableFuture[k];
3125 >            for (int i = 0; i < k; i++)
3126 >                fs[i] = new CompletableFuture<>();
3127 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3128 >            checkIncomplete(f);
3129 >            for (int i = k - 1; i >= 0; i--) {
3130 >                fs[i].complete(i);
3131 >                checkCompletedNormally(f, k - 1);
3132 >                int x = (int) CompletableFuture.anyOf(fs).join();
3133 >                assertTrue(i <= x && x <= k - 1);
3134              }
3135          }
3136      }
# Line 2858 | Line 3139 | public class CompletableFutureTest exten
3139       * anyOf result completes exceptionally when any component does.
3140       */
3141      public void testAnyOf_exceptional() throws Exception {
3142 <        for (int k = 0; k < 10; ++k) {
3142 >        for (int k = 0; k < 10; k++) {
3143              CompletableFuture[] fs = new CompletableFuture[k];
3144 <            for (int i = 0; i < k; ++i)
3144 >            CFException[] exs = new CFException[k];
3145 >            for (int i = 0; i < k; i++) {
3146                  fs[i] = new CompletableFuture<>();
3147 +                exs[i] = new CFException();
3148 +            }
3149              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3150              checkIncomplete(f);
3151 <            for (int i = 0; i < k; ++i) {
3152 <                fs[i].completeExceptionally(new CFException());
3153 <                checkCompletedWithWrappedCFException(f);
3151 >            for (int i = 0; i < k; i++) {
3152 >                fs[i].completeExceptionally(exs[i]);
3153 >                checkCompletedWithWrappedException(f, exs[0]);
3154 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3155 >            }
3156 >        }
3157 >    }
3158 >
3159 >    public void testAnyOf_exceptional_backwards() throws Exception {
3160 >        for (int k = 0; k < 10; k++) {
3161 >            CompletableFuture[] fs = new CompletableFuture[k];
3162 >            CFException[] exs = new CFException[k];
3163 >            for (int i = 0; i < k; i++) {
3164 >                fs[i] = new CompletableFuture<>();
3165 >                exs[i] = new CFException();
3166 >            }
3167 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3168 >            checkIncomplete(f);
3169 >            for (int i = k - 1; i >= 0; i--) {
3170 >                fs[i].completeExceptionally(exs[i]);
3171 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3172                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3173              }
3174          }
# Line 2879 | Line 3181 | public class CompletableFutureTest exten
3181          CompletableFuture<Integer> f = new CompletableFuture<>();
3182          CompletableFuture<Integer> g = new CompletableFuture<>();
3183          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2882        CompletableFuture<?> h;
3184          ThreadExecutor exec = new ThreadExecutor();
3185  
3186          Runnable[] throwingActions = {
3187              () -> CompletableFuture.supplyAsync(null),
3188              () -> CompletableFuture.supplyAsync(null, exec),
3189 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3189 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3190  
3191              () -> CompletableFuture.runAsync(null),
3192              () -> CompletableFuture.runAsync(null, exec),
# Line 2976 | Line 3277 | public class CompletableFutureTest exten
3277              () -> CompletableFuture.anyOf(null, f),
3278  
3279              () -> f.obtrudeException(null),
3280 +
3281 +            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3282 +            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3283 +            () -> CompletableFuture.delayedExecutor(1L, null),
3284 +
3285 +            () -> f.orTimeout(1L, null),
3286 +            () -> f.completeOnTimeout(42, 1L, null),
3287 +
3288 +            () -> CompletableFuture.failedFuture(null),
3289 +            () -> CompletableFuture.failedStage(null),
3290          };
3291  
3292          assertThrows(NullPointerException.class, throwingActions);
# Line 2990 | Line 3301 | public class CompletableFutureTest exten
3301          assertSame(f, f.toCompletableFuture());
3302      }
3303  
3304 +    // jdk9
3305 +
3306 +    /**
3307 +     * newIncompleteFuture returns an incomplete CompletableFuture
3308 +     */
3309 +    public void testNewIncompleteFuture() {
3310 +        for (Integer v1 : new Integer[] { 1, null })
3311 +    {
3312 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3313 +        CompletableFuture<Integer> g = f.newIncompleteFuture();
3314 +        checkIncomplete(f);
3315 +        checkIncomplete(g);
3316 +        f.complete(v1);
3317 +        checkCompletedNormally(f, v1);
3318 +        checkIncomplete(g);
3319 +        g.complete(v1);
3320 +        checkCompletedNormally(g, v1);
3321 +        assertSame(g.getClass(), CompletableFuture.class);
3322 +    }}
3323 +
3324 +    /**
3325 +     * completedStage returns a completed CompletionStage
3326 +     */
3327 +    public void testCompletedStage() {
3328 +        AtomicInteger x = new AtomicInteger(0);
3329 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3330 +        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3331 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3332 +        assertEquals(x.get(), 1);
3333 +        assertNull(r.get());
3334 +    }
3335 +
3336 +    /**
3337 +     * defaultExecutor by default returns the commonPool if
3338 +     * it supports more than one thread.
3339 +     */
3340 +    public void testDefaultExecutor() {
3341 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3342 +        Executor e = f.defaultExecutor();
3343 +        Executor c = ForkJoinPool.commonPool();
3344 +        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3345 +            assertSame(e, c);
3346 +        else
3347 +            assertNotSame(e, c);
3348 +    }
3349 +
3350 +    /**
3351 +     * failedFuture returns a CompletableFuture completed
3352 +     * exceptionally with the given Exception
3353 +     */
3354 +    public void testFailedFuture() {
3355 +        CFException ex = new CFException();
3356 +        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3357 +        checkCompletedExceptionally(f, ex);
3358 +    }
3359 +
3360 +    /**
3361 +     * failedFuture(null) throws NPE
3362 +     */
3363 +    public void testFailedFuture_null() {
3364 +        try {
3365 +            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3366 +            shouldThrow();
3367 +        } catch (NullPointerException success) {}
3368 +    }
3369 +
3370 +    /**
3371 +     * copy returns a CompletableFuture that is completed normally,
3372 +     * with the same value, when source is.
3373 +     */
3374 +    public void testCopy() {
3375 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3376 +        CompletableFuture<Integer> g = f.copy();
3377 +        checkIncomplete(f);
3378 +        checkIncomplete(g);
3379 +        f.complete(1);
3380 +        checkCompletedNormally(f, 1);
3381 +        checkCompletedNormally(g, 1);
3382 +    }
3383 +
3384 +    /**
3385 +     * copy returns a CompletableFuture that is completed exceptionally
3386 +     * when source is.
3387 +     */
3388 +    public void testCopy2() {
3389 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3390 +        CompletableFuture<Integer> g = f.copy();
3391 +        checkIncomplete(f);
3392 +        checkIncomplete(g);
3393 +        CFException ex = new CFException();
3394 +        f.completeExceptionally(ex);
3395 +        checkCompletedExceptionally(f, ex);
3396 +        checkCompletedWithWrappedException(g, ex);
3397 +    }
3398 +
3399 +    /**
3400 +     * minimalCompletionStage returns a CompletableFuture that is
3401 +     * completed normally, with the same value, when source is.
3402 +     */
3403 +    public void testMinimalCompletionStage() {
3404 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3405 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3406 +        AtomicInteger x = new AtomicInteger(0);
3407 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3408 +        checkIncomplete(f);
3409 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3410 +        f.complete(1);
3411 +        checkCompletedNormally(f, 1);
3412 +        assertEquals(x.get(), 1);
3413 +        assertNull(r.get());
3414 +    }
3415 +
3416 +    /**
3417 +     * minimalCompletionStage returns a CompletableFuture that is
3418 +     * completed exceptionally when source is.
3419 +     */
3420 +    public void testMinimalCompletionStage2() {
3421 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3422 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3423 +        AtomicInteger x = new AtomicInteger(0);
3424 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3425 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3426 +        checkIncomplete(f);
3427 +        CFException ex = new CFException();
3428 +        f.completeExceptionally(ex);
3429 +        checkCompletedExceptionally(f, ex);
3430 +        assertEquals(x.get(), 0);
3431 +        assertEquals(r.get().getCause(), ex);
3432 +    }
3433 +
3434 +    /**
3435 +     * failedStage returns a CompletionStage completed
3436 +     * exceptionally with the given Exception
3437 +     */
3438 +    public void testFailedStage() {
3439 +        CFException ex = new CFException();
3440 +        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3441 +        AtomicInteger x = new AtomicInteger(0);
3442 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3443 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3444 +        assertEquals(x.get(), 0);
3445 +        assertEquals(r.get(), ex);
3446 +    }
3447 +
3448 +    /**
3449 +     * completeAsync completes with value of given supplier
3450 +     */
3451 +    public void testCompleteAsync() {
3452 +        for (Integer v1 : new Integer[] { 1, null })
3453 +    {
3454 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3455 +        f.completeAsync(() -> v1);
3456 +        f.join();
3457 +        checkCompletedNormally(f, v1);
3458 +    }}
3459 +
3460 +    /**
3461 +     * completeAsync completes exceptionally if given supplier throws
3462 +     */
3463 +    public void testCompleteAsync2() {
3464 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3465 +        CFException ex = new CFException();
3466 +        f.completeAsync(() -> {if (true) throw ex; return 1;});
3467 +        try {
3468 +            f.join();
3469 +            shouldThrow();
3470 +        } catch (CompletionException success) {}
3471 +        checkCompletedWithWrappedException(f, ex);
3472 +    }
3473 +
3474 +    /**
3475 +     * completeAsync with given executor completes with value of given supplier
3476 +     */
3477 +    public void testCompleteAsync3() {
3478 +        for (Integer v1 : new Integer[] { 1, null })
3479 +    {
3480 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3481 +        ThreadExecutor executor = new ThreadExecutor();
3482 +        f.completeAsync(() -> v1, executor);
3483 +        assertSame(v1, f.join());
3484 +        checkCompletedNormally(f, v1);
3485 +        assertEquals(1, executor.count.get());
3486 +    }}
3487 +
3488 +    /**
3489 +     * completeAsync with given executor completes exceptionally if
3490 +     * given supplier throws
3491 +     */
3492 +    public void testCompleteAsync4() {
3493 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3494 +        CFException ex = new CFException();
3495 +        ThreadExecutor executor = new ThreadExecutor();
3496 +        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3497 +        try {
3498 +            f.join();
3499 +            shouldThrow();
3500 +        } catch (CompletionException success) {}
3501 +        checkCompletedWithWrappedException(f, ex);
3502 +        assertEquals(1, executor.count.get());
3503 +    }
3504 +
3505 +    /**
3506 +     * orTimeout completes with TimeoutException if not complete
3507 +     */
3508 +    public void testOrTimeout_timesOut() {
3509 +        long timeoutMillis = timeoutMillis();
3510 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3511 +        long startTime = System.nanoTime();
3512 +        f.orTimeout(timeoutMillis, MILLISECONDS);
3513 +        checkCompletedWithTimeoutException(f);
3514 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3515 +    }
3516 +
3517 +    /**
3518 +     * orTimeout completes normally if completed before timeout
3519 +     */
3520 +    public void testOrTimeout_completed() {
3521 +        for (Integer v1 : new Integer[] { 1, null })
3522 +    {
3523 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3524 +        CompletableFuture<Integer> g = new CompletableFuture<>();
3525 +        long startTime = System.nanoTime();
3526 +        f.complete(v1);
3527 +        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3528 +        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3529 +        g.complete(v1);
3530 +        checkCompletedNormally(f, v1);
3531 +        checkCompletedNormally(g, v1);
3532 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3533 +    }}
3534 +
3535 +    /**
3536 +     * completeOnTimeout completes with given value if not complete
3537 +     */
3538 +    public void testCompleteOnTimeout_timesOut() {
3539 +        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3540 +                       () -> testCompleteOnTimeout_timesOut(null));
3541 +    }
3542 +
3543 +    public void testCompleteOnTimeout_timesOut(Integer v) {
3544 +        long timeoutMillis = timeoutMillis();
3545 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3546 +        long startTime = System.nanoTime();
3547 +        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3548 +        assertSame(v, f.join());
3549 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3550 +        f.complete(99);         // should have no effect
3551 +        checkCompletedNormally(f, v);
3552 +    }
3553 +
3554 +    /**
3555 +     * completeOnTimeout has no effect if completed within timeout
3556 +     */
3557 +    public void testCompleteOnTimeout_completed() {
3558 +        for (Integer v1 : new Integer[] { 1, null })
3559 +    {
3560 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3561 +        CompletableFuture<Integer> g = new CompletableFuture<>();
3562 +        long startTime = System.nanoTime();
3563 +        f.complete(v1);
3564 +        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3565 +        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3566 +        g.complete(v1);
3567 +        checkCompletedNormally(f, v1);
3568 +        checkCompletedNormally(g, v1);
3569 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3570 +    }}
3571 +
3572 +    /**
3573 +     * delayedExecutor returns an executor that delays submission
3574 +     */
3575 +    public void testDelayedExecutor() {
3576 +        testInParallel(() -> testDelayedExecutor(null, null),
3577 +                       () -> testDelayedExecutor(null, 1),
3578 +                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3579 +                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3580 +    }
3581 +
3582 +    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3583 +        long timeoutMillis = timeoutMillis();
3584 +        // Use an "unreasonably long" long timeout to catch lingering threads
3585 +        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3586 +        final Executor delayer, longDelayer;
3587 +        if (executor == null) {
3588 +            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3589 +            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3590 +        } else {
3591 +            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3592 +            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3593 +        }
3594 +        long startTime = System.nanoTime();
3595 +        CompletableFuture<Integer> f =
3596 +            CompletableFuture.supplyAsync(() -> v, delayer);
3597 +        CompletableFuture<Integer> g =
3598 +            CompletableFuture.supplyAsync(() -> v, longDelayer);
3599 +
3600 +        assertNull(g.getNow(null));
3601 +
3602 +        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3603 +        long millisElapsed = millisElapsedSince(startTime);
3604 +        assertTrue(millisElapsed >= timeoutMillis);
3605 +        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3606 +
3607 +        checkCompletedNormally(f, v);
3608 +
3609 +        checkIncomplete(g);
3610 +        assertTrue(g.cancel(true));
3611 +    }
3612 +
3613 +    //--- tests of implementation details; not part of official tck ---
3614 +
3615 +    Object resultOf(CompletableFuture<?> f) {
3616 +        try {
3617 +            java.lang.reflect.Field resultField
3618 +                = CompletableFuture.class.getDeclaredField("result");
3619 +            resultField.setAccessible(true);
3620 +            return resultField.get(f);
3621 +        } catch (Throwable t) { throw new AssertionError(t); }
3622 +    }
3623 +
3624 +    public void testExceptionPropagationReusesResultObject() {
3625 +        if (!testImplementationDetails) return;
3626 +        for (ExecutionMode m : ExecutionMode.values())
3627 +    {
3628 +        final CFException ex = new CFException();
3629 +        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3630 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3631 +
3632 +        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3633 +            = new ArrayList<>();
3634 +
3635 +        funs.add((y) -> m.thenRun(y, new Noop(m)));
3636 +        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3637 +        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3638 +
3639 +        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3640 +        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3641 +        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3642 +
3643 +        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3644 +        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3645 +        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3646 +
3647 +        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3648 +
3649 +        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3650 +
3651 +        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3652 +        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3653 +
3654 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3655 +                 fun : funs) {
3656 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3657 +            f.completeExceptionally(ex);
3658 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3659 +            checkCompletedWithWrappedException(src, ex);
3660 +            CompletableFuture<?> dep = fun.apply(src);
3661 +            checkCompletedWithWrappedException(dep, ex);
3662 +            assertSame(resultOf(src), resultOf(dep));
3663 +        }
3664 +
3665 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3666 +                 fun : funs) {
3667 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3668 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3669 +            CompletableFuture<?> dep = fun.apply(src);
3670 +            f.completeExceptionally(ex);
3671 +            checkCompletedWithWrappedException(src, ex);
3672 +            checkCompletedWithWrappedException(dep, ex);
3673 +            assertSame(resultOf(src), resultOf(dep));
3674 +        }
3675 +
3676 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3677 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3678 +                 fun : funs) {
3679 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3680 +            f.cancel(mayInterruptIfRunning);
3681 +            checkCancelled(f);
3682 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3683 +            checkCompletedWithWrappedCancellationException(src);
3684 +            CompletableFuture<?> dep = fun.apply(src);
3685 +            checkCompletedWithWrappedCancellationException(dep);
3686 +            assertSame(resultOf(src), resultOf(dep));
3687 +        }
3688 +
3689 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3690 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3691 +                 fun : funs) {
3692 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3693 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3694 +            CompletableFuture<?> dep = fun.apply(src);
3695 +            f.cancel(mayInterruptIfRunning);
3696 +            checkCancelled(f);
3697 +            checkCompletedWithWrappedCancellationException(src);
3698 +            checkCompletedWithWrappedCancellationException(dep);
3699 +            assertSame(resultOf(src), resultOf(dep));
3700 +        }
3701 +    }}
3702 +
3703 +    /**
3704 +     * Minimal completion stages throw UOE for all non-CompletionStage methods
3705 +     */
3706 +    public void testMinimalCompletionStage_minimality() {
3707 +        if (!testImplementationDetails) return;
3708 +        Function<Method, String> toSignature =
3709 +            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3710 +        Predicate<Method> isNotStatic =
3711 +            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3712 +        List<Method> minimalMethods =
3713 +            Stream.of(Object.class, CompletionStage.class)
3714 +            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3715 +            .filter(isNotStatic)
3716 +            .collect(Collectors.toList());
3717 +        // Methods from CompletableFuture permitted NOT to throw UOE
3718 +        String[] signatureWhitelist = {
3719 +            "newIncompleteFuture[]",
3720 +            "defaultExecutor[]",
3721 +            "minimalCompletionStage[]",
3722 +            "copy[]",
3723 +        };
3724 +        Set<String> permittedMethodSignatures =
3725 +            Stream.concat(minimalMethods.stream().map(toSignature),
3726 +                          Stream.of(signatureWhitelist))
3727 +            .collect(Collectors.toSet());
3728 +        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3729 +            .filter(isNotStatic)
3730 +            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3731 +            .collect(Collectors.toList());
3732 +
3733 +        CompletionStage<Integer> minimalStage =
3734 +            new CompletableFuture<Integer>().minimalCompletionStage();
3735 +
3736 +        List<Method> bugs = new ArrayList<>();
3737 +        for (Method method : allMethods) {
3738 +            Class<?>[] parameterTypes = method.getParameterTypes();
3739 +            Object[] args = new Object[parameterTypes.length];
3740 +            // Manufacture boxed primitives for primitive params
3741 +            for (int i = 0; i < args.length; i++) {
3742 +                Class<?> type = parameterTypes[i];
3743 +                if (parameterTypes[i] == boolean.class)
3744 +                    args[i] = false;
3745 +                else if (parameterTypes[i] == int.class)
3746 +                    args[i] = 0;
3747 +                else if (parameterTypes[i] == long.class)
3748 +                    args[i] = 0L;
3749 +            }
3750 +            try {
3751 +                method.invoke(minimalStage, args);
3752 +                bugs.add(method);
3753 +            }
3754 +            catch (java.lang.reflect.InvocationTargetException expected) {
3755 +                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3756 +                    bugs.add(method);
3757 +                    // expected.getCause().printStackTrace();
3758 +                }
3759 +            }
3760 +            catch (ReflectiveOperationException bad) { throw new Error(bad); }
3761 +        }
3762 +        if (!bugs.isEmpty())
3763 +            throw new Error("Methods did not throw UOE: " + bugs.toString());
3764 +    }
3765 +
3766 +    static class Monad {
3767 +        static class ZeroException extends RuntimeException {
3768 +            public ZeroException() { super("monadic zero"); }
3769 +        }
3770 +        // "return", "unit"
3771 +        static <T> CompletableFuture<T> unit(T value) {
3772 +            return completedFuture(value);
3773 +        }
3774 +        // monadic zero ?
3775 +        static <T> CompletableFuture<T> zero() {
3776 +            return failedFuture(new ZeroException());
3777 +        }
3778 +        // >=>
3779 +        static <T,U,V> Function<T, CompletableFuture<V>> compose
3780 +            (Function<T, CompletableFuture<U>> f,
3781 +             Function<U, CompletableFuture<V>> g) {
3782 +            return (x) -> f.apply(x).thenCompose(g);
3783 +        }
3784 +
3785 +        static void assertZero(CompletableFuture<?> f) {
3786 +            try {
3787 +                f.getNow(null);
3788 +                throw new AssertionFailedError("should throw");
3789 +            } catch (CompletionException success) {
3790 +                assertTrue(success.getCause() instanceof ZeroException);
3791 +            }
3792 +        }
3793 +
3794 +        static <T> void assertFutureEquals(CompletableFuture<T> f,
3795 +                                           CompletableFuture<T> g) {
3796 +            T fval = null, gval = null;
3797 +            Throwable fex = null, gex = null;
3798 +
3799 +            try { fval = f.get(); }
3800 +            catch (ExecutionException ex) { fex = ex.getCause(); }
3801 +            catch (Throwable ex) { fex = ex; }
3802 +
3803 +            try { gval = g.get(); }
3804 +            catch (ExecutionException ex) { gex = ex.getCause(); }
3805 +            catch (Throwable ex) { gex = ex; }
3806 +
3807 +            if (fex != null || gex != null)
3808 +                assertSame(fex.getClass(), gex.getClass());
3809 +            else
3810 +                assertEquals(fval, gval);
3811 +        }
3812 +
3813 +        static class PlusFuture<T> extends CompletableFuture<T> {
3814 +            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3815 +        }
3816 +
3817 +        // Monadic "plus"
3818 +        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3819 +                                             CompletableFuture<? extends T> g) {
3820 +            PlusFuture<T> plus = new PlusFuture<T>();
3821 +            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3822 +                if (ex == null) {
3823 +                    if (plus.complete(result))
3824 +                        if (plus.firstFailure.get() != null)
3825 +                            plus.firstFailure.set(null);
3826 +                }
3827 +                else if (plus.firstFailure.compareAndSet(null, ex)) {
3828 +                    if (plus.isDone())
3829 +                        plus.firstFailure.set(null);
3830 +                }
3831 +                else {
3832 +                    // first failure has precedence
3833 +                    Throwable first = plus.firstFailure.getAndSet(null);
3834 +
3835 +                    // may fail with "Self-suppression not permitted"
3836 +                    try { first.addSuppressed(ex); }
3837 +                    catch (Exception ignored) {}
3838 +
3839 +                    plus.completeExceptionally(first);
3840 +                }
3841 +            };
3842 +            f.whenComplete(action);
3843 +            g.whenComplete(action);
3844 +            return plus;
3845 +        }
3846 +    }
3847 +
3848 +    /**
3849 +     * CompletableFuture is an additive monad - sort of.
3850 +     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3851 +     */
3852 +    public void testAdditiveMonad() throws Throwable {
3853 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3854 +        CompletableFuture<Long> zero = Monad.zero();
3855 +
3856 +        // Some mutually non-commutative functions
3857 +        Function<Long, CompletableFuture<Long>> triple
3858 +            = (x) -> Monad.unit(3 * x);
3859 +        Function<Long, CompletableFuture<Long>> inc
3860 +            = (x) -> Monad.unit(x + 1);
3861 +
3862 +        // unit is a right identity: m >>= unit === m
3863 +        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3864 +                                 inc.apply(5L));
3865 +        // unit is a left identity: (unit x) >>= f === f x
3866 +        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3867 +                                 inc.apply(5L));
3868 +
3869 +        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3870 +        Monad.assertFutureEquals(
3871 +            unit.apply(5L).thenCompose(inc).thenCompose(triple),
3872 +            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3873 +
3874 +        // The case for CompletableFuture as an additive monad is weaker...
3875 +
3876 +        // zero is a monadic zero
3877 +        Monad.assertZero(zero);
3878 +
3879 +        // left zero: zero >>= f === zero
3880 +        Monad.assertZero(zero.thenCompose(inc));
3881 +        // right zero: f >>= (\x -> zero) === zero
3882 +        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3883 +
3884 +        // f plus zero === f
3885 +        Monad.assertFutureEquals(Monad.unit(5L),
3886 +                                 Monad.plus(Monad.unit(5L), zero));
3887 +        // zero plus f === f
3888 +        Monad.assertFutureEquals(Monad.unit(5L),
3889 +                                 Monad.plus(zero, Monad.unit(5L)));
3890 +        // zero plus zero === zero
3891 +        Monad.assertZero(Monad.plus(zero, zero));
3892 +        {
3893 +            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3894 +                                                   Monad.unit(8L));
3895 +            // non-determinism
3896 +            assertTrue(f.get() == 5L || f.get() == 8L);
3897 +        }
3898 +
3899 +        CompletableFuture<Long> godot = new CompletableFuture<>();
3900 +        // f plus godot === f (doesn't wait for godot)
3901 +        Monad.assertFutureEquals(Monad.unit(5L),
3902 +                                 Monad.plus(Monad.unit(5L), godot));
3903 +        // godot plus f === f (doesn't wait for godot)
3904 +        Monad.assertFutureEquals(Monad.unit(5L),
3905 +                                 Monad.plus(godot, Monad.unit(5L)));
3906 +    }
3907 +
3908 + //     static <U> U join(CompletionStage<U> stage) {
3909 + //         CompletableFuture<U> f = new CompletableFuture<>();
3910 + //         stage.whenComplete((v, ex) -> {
3911 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3912 + //         });
3913 + //         return f.join();
3914 + //     }
3915 +
3916 + //     static <U> boolean isDone(CompletionStage<U> stage) {
3917 + //         CompletableFuture<U> f = new CompletableFuture<>();
3918 + //         stage.whenComplete((v, ex) -> {
3919 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3920 + //         });
3921 + //         return f.isDone();
3922 + //     }
3923 +
3924 + //     static <U> U join2(CompletionStage<U> stage) {
3925 + //         return stage.toCompletableFuture().copy().join();
3926 + //     }
3927 +
3928 + //     static <U> boolean isDone2(CompletionStage<U> stage) {
3929 + //         return stage.toCompletableFuture().copy().isDone();
3930 + //     }
3931 +
3932   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines