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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines