ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.75 by jsr166, Sat Jun 7 21:14:42 2014 UTC vs.
Revision 1.121 by jsr166, Sun Sep 6 21:14:12 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 >
11 > import java.util.ArrayList;
12 > import java.util.List;
13 > import java.util.Objects;
14   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
15   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
16   import java.util.concurrent.CompletableFuture;
17   import java.util.concurrent.CompletionException;
18   import java.util.concurrent.CompletionStage;
19 + import java.util.concurrent.ExecutionException;
20 + import java.util.concurrent.Executor;
21   import java.util.concurrent.ForkJoinPool;
22   import java.util.concurrent.ForkJoinTask;
23   import java.util.concurrent.TimeoutException;
24 + import java.util.concurrent.TimeUnit;
25   import java.util.concurrent.atomic.AtomicInteger;
26 < 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;
26 > import java.util.concurrent.atomic.AtomicReference;
27   import java.util.function.BiConsumer;
30 import java.util.function.Function;
28   import java.util.function.BiFunction;
29 + import java.util.function.Consumer;
30 + import java.util.function.Function;
31 + import java.util.function.Supplier;
32 +
33 + import junit.framework.Test;
34 + import junit.framework.TestSuite;
35  
36   public class CompletableFutureTest extends JSR166TestCase {
37  
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41      public static Test suite() {
42          return new TestSuite(CompletableFutureTest.class);
# Line 44 | Line 47 | public class CompletableFutureTest exten
47      void checkIncomplete(CompletableFuture<?> f) {
48          assertFalse(f.isDone());
49          assertFalse(f.isCancelled());
50 <        assertTrue(f.toString().contains("[Not completed]"));
50 >        assertTrue(f.toString().contains("Not completed"));
51          try {
52              assertNull(f.getNow(null));
53          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 57 | Line 60 | public class CompletableFutureTest exten
60      }
61  
62      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
63 <        try {
64 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
63 >        checkTimedGet(f, value);
64 >
65          try {
66              assertEquals(value, f.join());
67          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 75 | Line 77 | public class CompletableFutureTest exten
77          assertTrue(f.toString().contains("[Completed normally]"));
78      }
79  
80 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
81 <        try {
82 <            f.get(LONG_DELAY_MS, MILLISECONDS);
83 <            shouldThrow();
84 <        } catch (ExecutionException success) {
85 <            assertTrue(success.getCause() instanceof CFException);
86 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
87 <        try {
88 <            f.join();
89 <            shouldThrow();
90 <        } catch (CompletionException success) {
91 <            assertTrue(success.getCause() instanceof CFException);
92 <        }
93 <        try {
94 <            f.getNow(null);
95 <            shouldThrow();
96 <        } catch (CompletionException success) {
95 <            assertTrue(success.getCause() instanceof CFException);
80 >    /**
81 >     * Returns the "raw" internal exceptional completion of f,
82 >     * without any additional wrapping with CompletionException.
83 >     */
84 >    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
85 >        // handle (and whenComplete) can distinguish between "direct"
86 >        // and "wrapped" exceptional completion
87 >        return f.handle((U u, Throwable t) -> t).join();
88 >    }
89 >
90 >    void checkCompletedExceptionally(CompletableFuture<?> f,
91 >                                     boolean wrapped,
92 >                                     Consumer<Throwable> checker) {
93 >        Throwable cause = exceptionalCompletion(f);
94 >        if (wrapped) {
95 >            assertTrue(cause instanceof CompletionException);
96 >            cause = cause.getCause();
97          }
98 <        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 <    }
98 >        checker.accept(cause);
99  
100 <    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 <                                                      Throwable ex) {
100 >        long startTime = System.nanoTime();
101          try {
102              f.get(LONG_DELAY_MS, MILLISECONDS);
103              shouldThrow();
104          } catch (ExecutionException success) {
105 <            assertSame(ex, success.getCause());
105 >            assertSame(cause, success.getCause());
106          } catch (Throwable fail) { threadUnexpectedException(fail); }
107 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
108 +
109          try {
110              f.join();
111              shouldThrow();
112          } catch (CompletionException success) {
113 <            assertSame(ex, success.getCause());
114 <        }
113 >            assertSame(cause, success.getCause());
114 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
115 >
116          try {
117              f.getNow(null);
118              shouldThrow();
119          } catch (CompletionException success) {
120 <            assertSame(ex, success.getCause());
121 <        }
120 >            assertSame(cause, success.getCause());
121 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
122 >
123          try {
124              f.get();
125              shouldThrow();
126          } catch (ExecutionException success) {
127 <            assertSame(ex, success.getCause());
127 >            assertSame(cause, success.getCause());
128          } catch (Throwable fail) { threadUnexpectedException(fail); }
129  
135        assertTrue(f.isDone());
130          assertFalse(f.isCancelled());
131 +        assertTrue(f.isDone());
132 +        assertTrue(f.isCompletedExceptionally());
133          assertTrue(f.toString().contains("[Completed exceptionally]"));
134      }
135  
136 <    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
137 <                                                Throwable ex) {
138 <        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); }
136 >    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
137 >        checkCompletedExceptionally(f, true,
138 >            (t) -> assertTrue(t instanceof CFException));
139      }
140  
141 <    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
142 <        checkCompletedExceptionallyWithRootCause(f, ex);
143 <        try {
144 <            CompletableFuture<Throwable> spy = f.handle
145 <                ((U u, Throwable t) -> t);
146 <            assertSame(ex, spy.join());
147 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
141 >    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
142 >        checkCompletedExceptionally(f, true,
143 >            (t) -> assertTrue(t instanceof CancellationException));
144 >    }
145 >
146 >    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
147 >        checkCompletedExceptionally(f, false,
148 >            (t) -> assertTrue(t instanceof TimeoutException));
149 >    }
150 >
151 >    void checkCompletedWithWrappedException(CompletableFuture<?> f,
152 >                                            Throwable ex) {
153 >        checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
154 >    }
155 >
156 >    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
157 >        checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
158      }
159  
160      void checkCancelled(CompletableFuture<?> f) {
161 +        long startTime = System.nanoTime();
162          try {
163              f.get(LONG_DELAY_MS, MILLISECONDS);
164              shouldThrow();
165          } catch (CancellationException success) {
166          } catch (Throwable fail) { threadUnexpectedException(fail); }
167 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
168 +
169          try {
170              f.join();
171              shouldThrow();
# Line 176 | Line 179 | public class CompletableFutureTest exten
179              shouldThrow();
180          } catch (CancellationException success) {
181          } 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    }
182  
183 <    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
184 <        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); }
183 >        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
184 >
185          assertTrue(f.isDone());
211        assertFalse(f.isCancelled());
186          assertTrue(f.isCompletedExceptionally());
187 +        assertTrue(f.isCancelled());
188          assertTrue(f.toString().contains("[Completed exceptionally]"));
189      }
190  
# Line 227 | Line 202 | public class CompletableFutureTest exten
202       * isCancelled, join, get, and getNow
203       */
204      public void testComplete() {
205 +        for (Integer v1 : new Integer[] { 1, null })
206 +    {
207          CompletableFuture<Integer> f = new CompletableFuture<>();
208          checkIncomplete(f);
209 <        f.complete(one);
210 <        checkCompletedNormally(f, one);
211 <    }
209 >        assertTrue(f.complete(v1));
210 >        assertFalse(f.complete(v1));
211 >        checkCompletedNormally(f, v1);
212 >    }}
213  
214      /**
215       * completeExceptionally completes exceptionally, as indicated by
# Line 250 | Line 228 | public class CompletableFutureTest exten
228       * methods isDone, isCancelled, join, get, and getNow
229       */
230      public void testCancel() {
231 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
232 +    {
233          CompletableFuture<Integer> f = new CompletableFuture<>();
234          checkIncomplete(f);
235 <        assertTrue(f.cancel(true));
235 >        assertTrue(f.cancel(mayInterruptIfRunning));
236 >        assertTrue(f.cancel(mayInterruptIfRunning));
237 >        assertTrue(f.cancel(!mayInterruptIfRunning));
238          checkCancelled(f);
239 <    }
239 >    }}
240  
241      /**
242       * obtrudeValue forces completion with given value
# Line 262 | Line 244 | public class CompletableFutureTest exten
244      public void testObtrudeValue() {
245          CompletableFuture<Integer> f = new CompletableFuture<>();
246          checkIncomplete(f);
247 <        f.complete(one);
247 >        assertTrue(f.complete(one));
248          checkCompletedNormally(f, one);
249          f.obtrudeValue(three);
250          checkCompletedNormally(f, three);
# Line 289 | Line 271 | public class CompletableFutureTest exten
271          CompletableFuture<Integer> f;
272  
273          f = new CompletableFuture<>();
274 <        f.complete(v1);
274 >        assertTrue(f.complete(v1));
275          for (int i = 0; i < 2; i++) {
276              f.obtrudeException(ex = new CFException());
277              checkCompletedExceptionally(f, ex);
# Line 309 | Line 291 | public class CompletableFutureTest exten
291          checkCompletedExceptionally(f, ex);
292          f.completeExceptionally(new CFException());
293          checkCompletedExceptionally(f, ex);
294 <        f.complete(v1);
294 >        assertFalse(f.complete(v1));
295          checkCompletedExceptionally(f, ex);
296      }}
297  
# Line 317 | Line 299 | public class CompletableFutureTest exten
299       * getNumberOfDependents returns number of dependent tasks
300       */
301      public void testGetNumberOfDependents() {
302 +        for (ExecutionMode m : ExecutionMode.values())
303 +        for (Integer v1 : new Integer[] { 1, null })
304 +    {
305          CompletableFuture<Integer> f = new CompletableFuture<>();
306          assertEquals(0, f.getNumberOfDependents());
307 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
307 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
308          assertEquals(1, f.getNumberOfDependents());
309          assertEquals(0, g.getNumberOfDependents());
310 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
310 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
311          assertEquals(2, f.getNumberOfDependents());
312 <        f.complete(1);
312 >        assertEquals(0, h.getNumberOfDependents());
313 >        assertTrue(f.complete(v1));
314          checkCompletedNormally(g, null);
315 +        checkCompletedNormally(h, null);
316          assertEquals(0, f.getNumberOfDependents());
317          assertEquals(0, g.getNumberOfDependents());
318 <    }
318 >        assertEquals(0, h.getNumberOfDependents());
319 >    }}
320  
321      /**
322       * toString indicates current completion state
# Line 339 | Line 327 | public class CompletableFutureTest exten
327          f = new CompletableFuture<String>();
328          assertTrue(f.toString().contains("[Not completed]"));
329  
330 <        f.complete("foo");
330 >        assertTrue(f.complete("foo"));
331          assertTrue(f.toString().contains("[Completed normally]"));
332  
333          f = new CompletableFuture<String>();
334 <        f.completeExceptionally(new IndexOutOfBoundsException());
347 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
348 <
349 <        f = new CompletableFuture<String>();
350 <        f.cancel(true);
334 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
335          assertTrue(f.toString().contains("[Completed exceptionally]"));
336  
337 <        f = new CompletableFuture<String>();
338 <        f.cancel(false);
339 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
337 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
338 >            f = new CompletableFuture<String>();
339 >            assertTrue(f.cancel(mayInterruptIfRunning));
340 >            assertTrue(f.toString().contains("[Completed exceptionally]"));
341 >        }
342      }
343  
344      /**
# Line 520 | Line 506 | public class CompletableFutureTest exten
506          }
507      }
508  
523
509      class CompletableFutureInc extends CheckedIntegerAction
510          implements Function<Integer, CompletableFuture<Integer>>
511      {
# Line 529 | Line 514 | public class CompletableFutureTest exten
514              invoked();
515              value = x;
516              CompletableFuture<Integer> f = new CompletableFuture<>();
517 <            f.complete(inc(x));
517 >            assertTrue(f.complete(inc(x)));
518              return f;
519          }
520      }
# Line 559 | Line 544 | public class CompletableFutureTest exten
544          }
545      }
546  
547 +    static final boolean defaultExecutorIsCommonPool
548 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
549 +
550      /**
551       * Permits the testing of parallel code for the 3 different
552       * execution modes without copy/pasting all the test methods.
553       */
554      enum ExecutionMode {
555 <        DEFAULT {
555 >        SYNC {
556              public void checkExecutionMode() {
557                  assertFalse(ThreadExecutor.startedCurrentThread());
558                  assertNull(ForkJoinTask.getPool());
# Line 640 | Line 628 | public class CompletableFutureTest exten
628  
629          ASYNC {
630              public void checkExecutionMode() {
631 <                assertSame(ForkJoinPool.commonPool(),
632 <                           ForkJoinTask.getPool());
631 >                assertEquals(defaultExecutorIsCommonPool,
632 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
633              }
634              public CompletableFuture<Void> runAsync(Runnable a) {
635                  return CompletableFuture.runAsync(a);
# Line 837 | Line 825 | public class CompletableFutureTest exten
825      {
826          final AtomicInteger a = new AtomicInteger(0);
827          final CompletableFuture<Integer> f = new CompletableFuture<>();
828 <        if (!createIncomplete) f.complete(v1);
828 >        if (!createIncomplete) assertTrue(f.complete(v1));
829          final CompletableFuture<Integer> g = f.exceptionally
830              ((Throwable t) -> {
831                  // Should not be called
832                  a.getAndIncrement();
833                  throw new AssertionError();
834              });
835 <        if (createIncomplete) f.complete(v1);
835 >        if (createIncomplete) assertTrue(f.complete(v1));
836  
837          checkCompletedNormally(g, v1);
838          checkCompletedNormally(f, v1);
# Line 865 | Line 853 | public class CompletableFutureTest exten
853          if (!createIncomplete) f.completeExceptionally(ex);
854          final CompletableFuture<Integer> g = f.exceptionally
855              ((Throwable t) -> {
856 <                ExecutionMode.DEFAULT.checkExecutionMode();
856 >                ExecutionMode.SYNC.checkExecutionMode();
857                  threadAssertSame(t, ex);
858                  a.getAndIncrement();
859                  return v1;
# Line 878 | Line 866 | public class CompletableFutureTest exten
866  
867      public void testExceptionally_exceptionalCompletionActionFailed() {
868          for (boolean createIncomplete : new boolean[] { true, false })
881        for (Integer v1 : new Integer[] { 1, null })
869      {
870          final AtomicInteger a = new AtomicInteger(0);
871          final CFException ex1 = new CFException();
# Line 887 | Line 874 | public class CompletableFutureTest exten
874          if (!createIncomplete) f.completeExceptionally(ex1);
875          final CompletableFuture<Integer> g = f.exceptionally
876              ((Throwable t) -> {
877 <                ExecutionMode.DEFAULT.checkExecutionMode();
877 >                ExecutionMode.SYNC.checkExecutionMode();
878                  threadAssertSame(t, ex1);
879                  a.getAndIncrement();
880                  throw ex2;
# Line 909 | Line 896 | public class CompletableFutureTest exten
896      {
897          final AtomicInteger a = new AtomicInteger(0);
898          final CompletableFuture<Integer> f = new CompletableFuture<>();
899 <        if (!createIncomplete) f.complete(v1);
899 >        if (!createIncomplete) assertTrue(f.complete(v1));
900          final CompletableFuture<Integer> g = m.whenComplete
901              (f,
902               (Integer x, Throwable t) -> {
# Line 918 | Line 905 | public class CompletableFutureTest exten
905                  threadAssertNull(t);
906                  a.getAndIncrement();
907              });
908 <        if (createIncomplete) f.complete(v1);
908 >        if (createIncomplete) assertTrue(f.complete(v1));
909  
910          checkCompletedNormally(g, v1);
911          checkCompletedNormally(f, v1);
# Line 932 | Line 919 | public class CompletableFutureTest exten
919      public void testWhenComplete_exceptionalCompletion() {
920          for (ExecutionMode m : ExecutionMode.values())
921          for (boolean createIncomplete : new boolean[] { true, false })
935        for (Integer v1 : new Integer[] { 1, null })
922      {
923          final AtomicInteger a = new AtomicInteger(0);
924          final CFException ex = new CFException();
# Line 992 | Line 978 | public class CompletableFutureTest exten
978          final AtomicInteger a = new AtomicInteger(0);
979          final CFException ex = new CFException();
980          final CompletableFuture<Integer> f = new CompletableFuture<>();
981 <        if (!createIncomplete) f.complete(v1);
981 >        if (!createIncomplete) assertTrue(f.complete(v1));
982          final CompletableFuture<Integer> g = m.whenComplete
983              (f,
984               (Integer x, Throwable t) -> {
# Line 1002 | Line 988 | public class CompletableFutureTest exten
988                  a.getAndIncrement();
989                  throw ex;
990              });
991 <        if (createIncomplete) f.complete(v1);
991 >        if (createIncomplete) assertTrue(f.complete(v1));
992  
993          checkCompletedWithWrappedException(g, ex);
994          checkCompletedNormally(f, v1);
# Line 1017 | Line 1003 | public class CompletableFutureTest exten
1003      public void testWhenComplete_actionFailedSourceFailed() {
1004          for (boolean createIncomplete : new boolean[] { true, false })
1005          for (ExecutionMode m : ExecutionMode.values())
1020        for (Integer v1 : new Integer[] { 1, null })
1006      {
1007          final AtomicInteger a = new AtomicInteger(0);
1008          final CFException ex1 = new CFException();
# Line 1052 | Line 1037 | public class CompletableFutureTest exten
1037      {
1038          final CompletableFuture<Integer> f = new CompletableFuture<>();
1039          final AtomicInteger a = new AtomicInteger(0);
1040 <        if (!createIncomplete) f.complete(v1);
1040 >        if (!createIncomplete) assertTrue(f.complete(v1));
1041          final CompletableFuture<Integer> g = m.handle
1042              (f,
1043               (Integer x, Throwable t) -> {
# Line 1062 | Line 1047 | public class CompletableFutureTest exten
1047                  a.getAndIncrement();
1048                  return inc(v1);
1049              });
1050 <        if (createIncomplete) f.complete(v1);
1050 >        if (createIncomplete) assertTrue(f.complete(v1));
1051  
1052          checkCompletedNormally(g, inc(v1));
1053          checkCompletedNormally(f, v1);
# Line 1163 | Line 1148 | public class CompletableFutureTest exten
1148          final CompletableFuture<Integer> f = new CompletableFuture<>();
1149          final AtomicInteger a = new AtomicInteger(0);
1150          final CFException ex = new CFException();
1151 <        if (!createIncomplete) f.complete(v1);
1151 >        if (!createIncomplete) assertTrue(f.complete(v1));
1152          final CompletableFuture<Integer> g = m.handle
1153              (f,
1154               (Integer x, Throwable t) -> {
# Line 1173 | Line 1158 | public class CompletableFutureTest exten
1158                  a.getAndIncrement();
1159                  throw ex;
1160              });
1161 <        if (createIncomplete) f.complete(v1);
1161 >        if (createIncomplete) assertTrue(f.complete(v1));
1162  
1163          checkCompletedWithWrappedException(g, ex);
1164          checkCompletedNormally(f, v1);
# Line 1254 | Line 1239 | public class CompletableFutureTest exten
1239       */
1240      public void testThenRun_normalCompletion() {
1241          for (ExecutionMode m : ExecutionMode.values())
1257        for (boolean createIncomplete : new boolean[] { true, false })
1242          for (Integer v1 : new Integer[] { 1, null })
1243      {
1244          final CompletableFuture<Integer> f = new CompletableFuture<>();
1245 <        final Noop r = new Noop(m);
1246 <        if (!createIncomplete) f.complete(v1);
1263 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1264 <        if (createIncomplete) {
1265 <            checkIncomplete(g);
1266 <            f.complete(v1);
1267 <        }
1245 >        final Noop[] rs = new Noop[6];
1246 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1247  
1248 <        checkCompletedNormally(g, null);
1248 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1249 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1250 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1251 >        checkIncomplete(h0);
1252 >        checkIncomplete(h1);
1253 >        checkIncomplete(h2);
1254 >        assertTrue(f.complete(v1));
1255 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1256 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1257 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1258 >
1259 >        checkCompletedNormally(h0, null);
1260 >        checkCompletedNormally(h1, null);
1261 >        checkCompletedNormally(h2, null);
1262 >        checkCompletedNormally(h3, null);
1263 >        checkCompletedNormally(h4, null);
1264 >        checkCompletedNormally(h5, null);
1265          checkCompletedNormally(f, v1);
1266 <        r.assertInvoked();
1266 >        for (Noop r : rs) r.assertInvoked();
1267      }}
1268  
1269      /**
# Line 1277 | Line 1272 | public class CompletableFutureTest exten
1272       */
1273      public void testThenRun_exceptionalCompletion() {
1274          for (ExecutionMode m : ExecutionMode.values())
1280        for (boolean createIncomplete : new boolean[] { true, false })
1275      {
1276          final CFException ex = new CFException();
1277          final CompletableFuture<Integer> f = new CompletableFuture<>();
1278 <        final Noop r = new Noop(m);
1279 <        if (!createIncomplete) f.completeExceptionally(ex);
1286 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1287 <        if (createIncomplete) {
1288 <            checkIncomplete(g);
1289 <            f.completeExceptionally(ex);
1290 <        }
1278 >        final Noop[] rs = new Noop[6];
1279 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1280  
1281 <        checkCompletedWithWrappedException(g, ex);
1281 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1282 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1283 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1284 >        checkIncomplete(h0);
1285 >        checkIncomplete(h1);
1286 >        checkIncomplete(h2);
1287 >        assertTrue(f.completeExceptionally(ex));
1288 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1289 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1290 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1291 >
1292 >        checkCompletedWithWrappedException(h0, ex);
1293 >        checkCompletedWithWrappedException(h1, ex);
1294 >        checkCompletedWithWrappedException(h2, ex);
1295 >        checkCompletedWithWrappedException(h3, ex);
1296 >        checkCompletedWithWrappedException(h4, ex);
1297 >        checkCompletedWithWrappedException(h5, ex);
1298          checkCompletedExceptionally(f, ex);
1299 <        r.assertNotInvoked();
1299 >        for (Noop r : rs) r.assertNotInvoked();
1300      }}
1301  
1302      /**
# Line 1299 | Line 1304 | public class CompletableFutureTest exten
1304       */
1305      public void testThenRun_sourceCancelled() {
1306          for (ExecutionMode m : ExecutionMode.values())
1302        for (boolean createIncomplete : new boolean[] { true, false })
1307          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1308      {
1309          final CompletableFuture<Integer> f = new CompletableFuture<>();
1310 <        final Noop r = new Noop(m);
1311 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1308 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1309 <        if (createIncomplete) {
1310 <            checkIncomplete(g);
1311 <            assertTrue(f.cancel(mayInterruptIfRunning));
1312 <        }
1310 >        final Noop[] rs = new Noop[6];
1311 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1312  
1313 <        checkCompletedWithWrappedCancellationException(g);
1313 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1314 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1315 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1316 >        checkIncomplete(h0);
1317 >        checkIncomplete(h1);
1318 >        checkIncomplete(h2);
1319 >        assertTrue(f.cancel(mayInterruptIfRunning));
1320 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1321 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1322 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1323 >
1324 >        checkCompletedWithWrappedCancellationException(h0);
1325 >        checkCompletedWithWrappedCancellationException(h1);
1326 >        checkCompletedWithWrappedCancellationException(h2);
1327 >        checkCompletedWithWrappedCancellationException(h3);
1328 >        checkCompletedWithWrappedCancellationException(h4);
1329 >        checkCompletedWithWrappedCancellationException(h5);
1330          checkCancelled(f);
1331 <        r.assertNotInvoked();
1331 >        for (Noop r : rs) r.assertNotInvoked();
1332      }}
1333  
1334      /**
# Line 1321 | Line 1336 | public class CompletableFutureTest exten
1336       */
1337      public void testThenRun_actionFailed() {
1338          for (ExecutionMode m : ExecutionMode.values())
1324        for (boolean createIncomplete : new boolean[] { true, false })
1339          for (Integer v1 : new Integer[] { 1, null })
1340      {
1341          final CompletableFuture<Integer> f = new CompletableFuture<>();
1342 <        final FailingRunnable r = new FailingRunnable(m);
1343 <        if (!createIncomplete) f.complete(v1);
1330 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1331 <        if (createIncomplete) {
1332 <            checkIncomplete(g);
1333 <            f.complete(v1);
1334 <        }
1342 >        final FailingRunnable[] rs = new FailingRunnable[6];
1343 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1344  
1345 <        checkCompletedWithWrappedCFException(g);
1345 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1346 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1347 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1348 >        assertTrue(f.complete(v1));
1349 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1350 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1351 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1352 >
1353 >        checkCompletedWithWrappedCFException(h0);
1354 >        checkCompletedWithWrappedCFException(h1);
1355 >        checkCompletedWithWrappedCFException(h2);
1356 >        checkCompletedWithWrappedCFException(h3);
1357 >        checkCompletedWithWrappedCFException(h4);
1358 >        checkCompletedWithWrappedCFException(h5);
1359          checkCompletedNormally(f, v1);
1360      }}
1361  
# Line 1342 | Line 1364 | public class CompletableFutureTest exten
1364       */
1365      public void testThenApply_normalCompletion() {
1366          for (ExecutionMode m : ExecutionMode.values())
1345        for (boolean createIncomplete : new boolean[] { true, false })
1367          for (Integer v1 : new Integer[] { 1, null })
1368      {
1369          final CompletableFuture<Integer> f = new CompletableFuture<>();
1370 <        final IncFunction r = new IncFunction(m);
1371 <        if (!createIncomplete) f.complete(v1);
1351 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1352 <        if (createIncomplete) {
1353 <            checkIncomplete(g);
1354 <            f.complete(v1);
1355 <        }
1370 >        final IncFunction[] rs = new IncFunction[4];
1371 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1372  
1373 <        checkCompletedNormally(g, inc(v1));
1373 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1374 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1375 >        checkIncomplete(h0);
1376 >        checkIncomplete(h1);
1377 >        assertTrue(f.complete(v1));
1378 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1379 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1380 >
1381 >        checkCompletedNormally(h0, inc(v1));
1382 >        checkCompletedNormally(h1, inc(v1));
1383 >        checkCompletedNormally(h2, inc(v1));
1384 >        checkCompletedNormally(h3, inc(v1));
1385          checkCompletedNormally(f, v1);
1386 <        r.assertValue(inc(v1));
1386 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1387      }}
1388  
1389      /**
# Line 1365 | Line 1392 | public class CompletableFutureTest exten
1392       */
1393      public void testThenApply_exceptionalCompletion() {
1394          for (ExecutionMode m : ExecutionMode.values())
1368        for (boolean createIncomplete : new boolean[] { true, false })
1395      {
1396          final CFException ex = new CFException();
1397          final CompletableFuture<Integer> f = new CompletableFuture<>();
1398 <        final IncFunction r = new IncFunction(m);
1399 <        if (!createIncomplete) f.completeExceptionally(ex);
1374 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1375 <        if (createIncomplete) {
1376 <            checkIncomplete(g);
1377 <            f.completeExceptionally(ex);
1378 <        }
1398 >        final IncFunction[] rs = new IncFunction[4];
1399 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1400  
1401 <        checkCompletedWithWrappedException(g, ex);
1401 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1402 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1403 >        assertTrue(f.completeExceptionally(ex));
1404 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1405 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1406 >
1407 >        checkCompletedWithWrappedException(h0, ex);
1408 >        checkCompletedWithWrappedException(h1, ex);
1409 >        checkCompletedWithWrappedException(h2, ex);
1410 >        checkCompletedWithWrappedException(h3, ex);
1411          checkCompletedExceptionally(f, ex);
1412 <        r.assertNotInvoked();
1412 >        for (IncFunction r : rs) r.assertNotInvoked();
1413      }}
1414  
1415      /**
# Line 1387 | Line 1417 | public class CompletableFutureTest exten
1417       */
1418      public void testThenApply_sourceCancelled() {
1419          for (ExecutionMode m : ExecutionMode.values())
1390        for (boolean createIncomplete : new boolean[] { true, false })
1420          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1421      {
1422          final CompletableFuture<Integer> f = new CompletableFuture<>();
1423 <        final IncFunction r = new IncFunction(m);
1424 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1396 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1397 <        if (createIncomplete) {
1398 <            checkIncomplete(g);
1399 <            assertTrue(f.cancel(mayInterruptIfRunning));
1400 <        }
1423 >        final IncFunction[] rs = new IncFunction[4];
1424 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1425  
1426 <        checkCompletedWithWrappedCancellationException(g);
1426 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1427 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1428 >        assertTrue(f.cancel(mayInterruptIfRunning));
1429 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1430 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1431 >
1432 >        checkCompletedWithWrappedCancellationException(h0);
1433 >        checkCompletedWithWrappedCancellationException(h1);
1434 >        checkCompletedWithWrappedCancellationException(h2);
1435 >        checkCompletedWithWrappedCancellationException(h3);
1436          checkCancelled(f);
1437 <        r.assertNotInvoked();
1437 >        for (IncFunction r : rs) r.assertNotInvoked();
1438      }}
1439  
1440      /**
# Line 1409 | Line 1442 | public class CompletableFutureTest exten
1442       */
1443      public void testThenApply_actionFailed() {
1444          for (ExecutionMode m : ExecutionMode.values())
1412        for (boolean createIncomplete : new boolean[] { true, false })
1445          for (Integer v1 : new Integer[] { 1, null })
1446      {
1447          final CompletableFuture<Integer> f = new CompletableFuture<>();
1448 <        final FailingFunction r = new FailingFunction(m);
1449 <        if (!createIncomplete) f.complete(v1);
1418 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1419 <        if (createIncomplete) {
1420 <            checkIncomplete(g);
1421 <            f.complete(v1);
1422 <        }
1448 >        final FailingFunction[] rs = new FailingFunction[4];
1449 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1450  
1451 <        checkCompletedWithWrappedCFException(g);
1451 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1452 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1453 >        assertTrue(f.complete(v1));
1454 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1455 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1456 >
1457 >        checkCompletedWithWrappedCFException(h0);
1458 >        checkCompletedWithWrappedCFException(h1);
1459 >        checkCompletedWithWrappedCFException(h2);
1460 >        checkCompletedWithWrappedCFException(h3);
1461          checkCompletedNormally(f, v1);
1462      }}
1463  
# Line 1430 | Line 1466 | public class CompletableFutureTest exten
1466       */
1467      public void testThenAccept_normalCompletion() {
1468          for (ExecutionMode m : ExecutionMode.values())
1433        for (boolean createIncomplete : new boolean[] { true, false })
1469          for (Integer v1 : new Integer[] { 1, null })
1470      {
1471          final CompletableFuture<Integer> f = new CompletableFuture<>();
1472 <        final NoopConsumer r = new NoopConsumer(m);
1473 <        if (!createIncomplete) f.complete(v1);
1439 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1440 <        if (createIncomplete) {
1441 <            checkIncomplete(g);
1442 <            f.complete(v1);
1443 <        }
1472 >        final NoopConsumer[] rs = new NoopConsumer[4];
1473 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1474  
1475 <        checkCompletedNormally(g, null);
1476 <        r.assertValue(v1);
1475 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1476 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1477 >        checkIncomplete(h0);
1478 >        checkIncomplete(h1);
1479 >        assertTrue(f.complete(v1));
1480 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1481 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1482 >
1483 >        checkCompletedNormally(h0, null);
1484 >        checkCompletedNormally(h1, null);
1485 >        checkCompletedNormally(h2, null);
1486 >        checkCompletedNormally(h3, null);
1487          checkCompletedNormally(f, v1);
1488 +        for (NoopConsumer r : rs) r.assertValue(v1);
1489      }}
1490  
1491      /**
# Line 1453 | Line 1494 | public class CompletableFutureTest exten
1494       */
1495      public void testThenAccept_exceptionalCompletion() {
1496          for (ExecutionMode m : ExecutionMode.values())
1456        for (boolean createIncomplete : new boolean[] { true, false })
1497      {
1498          final CFException ex = new CFException();
1499          final CompletableFuture<Integer> f = new CompletableFuture<>();
1500 <        final NoopConsumer r = new NoopConsumer(m);
1501 <        if (!createIncomplete) f.completeExceptionally(ex);
1462 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1463 <        if (createIncomplete) {
1464 <            checkIncomplete(g);
1465 <            f.completeExceptionally(ex);
1466 <        }
1500 >        final NoopConsumer[] rs = new NoopConsumer[4];
1501 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1502  
1503 <        checkCompletedWithWrappedException(g, ex);
1503 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1504 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1505 >        assertTrue(f.completeExceptionally(ex));
1506 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1507 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1508 >
1509 >        checkCompletedWithWrappedException(h0, ex);
1510 >        checkCompletedWithWrappedException(h1, ex);
1511 >        checkCompletedWithWrappedException(h2, ex);
1512 >        checkCompletedWithWrappedException(h3, ex);
1513          checkCompletedExceptionally(f, ex);
1514 <        r.assertNotInvoked();
1514 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1515      }}
1516  
1517      /**
# Line 1475 | Line 1519 | public class CompletableFutureTest exten
1519       */
1520      public void testThenAccept_sourceCancelled() {
1521          for (ExecutionMode m : ExecutionMode.values())
1478        for (boolean createIncomplete : new boolean[] { true, false })
1522          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1523      {
1524          final CompletableFuture<Integer> f = new CompletableFuture<>();
1525 <        final NoopConsumer r = new NoopConsumer(m);
1526 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1484 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1485 <        if (createIncomplete) {
1486 <            checkIncomplete(g);
1487 <            assertTrue(f.cancel(mayInterruptIfRunning));
1488 <        }
1525 >        final NoopConsumer[] rs = new NoopConsumer[4];
1526 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1527  
1528 <        checkCompletedWithWrappedCancellationException(g);
1528 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1529 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1530 >        assertTrue(f.cancel(mayInterruptIfRunning));
1531 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1532 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1533 >
1534 >        checkCompletedWithWrappedCancellationException(h0);
1535 >        checkCompletedWithWrappedCancellationException(h1);
1536 >        checkCompletedWithWrappedCancellationException(h2);
1537 >        checkCompletedWithWrappedCancellationException(h3);
1538          checkCancelled(f);
1539 <        r.assertNotInvoked();
1539 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1540      }}
1541  
1542      /**
# Line 1497 | Line 1544 | public class CompletableFutureTest exten
1544       */
1545      public void testThenAccept_actionFailed() {
1546          for (ExecutionMode m : ExecutionMode.values())
1500        for (boolean createIncomplete : new boolean[] { true, false })
1547          for (Integer v1 : new Integer[] { 1, null })
1548      {
1549          final CompletableFuture<Integer> f = new CompletableFuture<>();
1550 <        final FailingConsumer r = new FailingConsumer(m);
1551 <        if (!createIncomplete) f.complete(v1);
1506 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1507 <        if (createIncomplete) {
1508 <            checkIncomplete(g);
1509 <            f.complete(v1);
1510 <        }
1550 >        final FailingConsumer[] rs = new FailingConsumer[4];
1551 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1552  
1553 <        checkCompletedWithWrappedCFException(g);
1553 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1554 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1555 >        assertTrue(f.complete(v1));
1556 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1557 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1558 >
1559 >        checkCompletedWithWrappedCFException(h0);
1560 >        checkCompletedWithWrappedCFException(h1);
1561 >        checkCompletedWithWrappedCFException(h2);
1562 >        checkCompletedWithWrappedCFException(h3);
1563          checkCompletedNormally(f, v1);
1564      }}
1565  
# Line 1519 | Line 1569 | public class CompletableFutureTest exten
1569       */
1570      public void testThenCombine_normalCompletion() {
1571          for (ExecutionMode m : ExecutionMode.values())
1522        for (boolean createIncomplete : new boolean[] { true, false })
1572          for (boolean fFirst : new boolean[] { true, false })
1573          for (Integer v1 : new Integer[] { 1, null })
1574          for (Integer v2 : new Integer[] { 2, null })
1575      {
1576          final CompletableFuture<Integer> f = new CompletableFuture<>();
1577          final CompletableFuture<Integer> g = new CompletableFuture<>();
1578 <        final SubtractFunction r = new SubtractFunction(m);
1578 >        final SubtractFunction[] rs = new SubtractFunction[6];
1579 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1580  
1581 <        if (fFirst) f.complete(v1); else g.complete(v2);
1582 <        if (!createIncomplete)
1583 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1584 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1585 <        if (createIncomplete) {
1586 <            checkIncomplete(h);
1587 <            r.assertNotInvoked();
1588 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1589 <        }
1581 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1582 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1583 >        final Integer w1 =  fFirst ? v1 : v2;
1584 >        final Integer w2 = !fFirst ? v1 : v2;
1585 >
1586 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1587 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1588 >        assertTrue(fst.complete(w1));
1589 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1590 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1591 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1592 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1593 >        checkCompletedNormally(h1, subtract(w1, w1));
1594 >        checkCompletedNormally(h3, subtract(w1, w1));
1595 >        rs[1].assertValue(subtract(w1, w1));
1596 >        rs[3].assertValue(subtract(w1, w1));
1597 >        assertTrue(snd.complete(w2));
1598 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1599 >
1600 >        checkCompletedNormally(h0, subtract(v1, v2));
1601 >        checkCompletedNormally(h2, subtract(v1, v2));
1602 >        checkCompletedNormally(h4, subtract(v1, v2));
1603 >        rs[0].assertValue(subtract(v1, v2));
1604 >        rs[2].assertValue(subtract(v1, v2));
1605 >        rs[4].assertValue(subtract(v1, v2));
1606  
1541        checkCompletedNormally(h, subtract(v1, v2));
1607          checkCompletedNormally(f, v1);
1608          checkCompletedNormally(g, v2);
1544        r.assertValue(subtract(v1, v2));
1609      }}
1610  
1611      /**
1612       * thenCombine result completes exceptionally after exceptional
1613       * completion of either source
1614       */
1615 <    public void testThenCombine_exceptionalCompletion() {
1615 >    public void testThenCombine_exceptionalCompletion() throws Throwable {
1616          for (ExecutionMode m : ExecutionMode.values())
1553        for (boolean createIncomplete : new boolean[] { true, false })
1617          for (boolean fFirst : new boolean[] { true, false })
1618 +        for (boolean failFirst : new boolean[] { true, false })
1619          for (Integer v1 : new Integer[] { 1, null })
1620      {
1621          final CompletableFuture<Integer> f = new CompletableFuture<>();
1622          final CompletableFuture<Integer> g = new CompletableFuture<>();
1623          final CFException ex = new CFException();
1624 <        final SubtractFunction r = new SubtractFunction(m);
1625 <
1626 <        (fFirst ? f : g).complete(v1);
1627 <        if (!createIncomplete)
1628 <            (!fFirst ? f : g).completeExceptionally(ex);
1629 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1630 <        if (createIncomplete) {
1631 <            checkIncomplete(h);
1632 <            (!fFirst ? f : g).completeExceptionally(ex);
1633 <        }
1624 >        final SubtractFunction r1 = new SubtractFunction(m);
1625 >        final SubtractFunction r2 = new SubtractFunction(m);
1626 >        final SubtractFunction r3 = new SubtractFunction(m);
1627 >
1628 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1629 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1630 >        final Callable<Boolean> complete1 = failFirst ?
1631 >            () -> fst.completeExceptionally(ex) :
1632 >            () -> fst.complete(v1);
1633 >        final Callable<Boolean> complete2 = failFirst ?
1634 >            () -> snd.complete(v1) :
1635 >            () -> snd.completeExceptionally(ex);
1636 >
1637 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1638 >        assertTrue(complete1.call());
1639 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1640 >        checkIncomplete(h1);
1641 >        checkIncomplete(h2);
1642 >        assertTrue(complete2.call());
1643 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1644  
1645 <        checkCompletedWithWrappedException(h, ex);
1646 <        r.assertNotInvoked();
1647 <        checkCompletedNormally(fFirst ? f : g, v1);
1648 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1645 >        checkCompletedWithWrappedException(h1, ex);
1646 >        checkCompletedWithWrappedException(h2, ex);
1647 >        checkCompletedWithWrappedException(h3, ex);
1648 >        r1.assertNotInvoked();
1649 >        r2.assertNotInvoked();
1650 >        r3.assertNotInvoked();
1651 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1652 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1653      }}
1654  
1655      /**
1656       * thenCombine result completes exceptionally if either source cancelled
1657       */
1658 <    public void testThenCombine_sourceCancelled() {
1658 >    public void testThenCombine_sourceCancelled() throws Throwable {
1659          for (ExecutionMode m : ExecutionMode.values())
1660          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1583        for (boolean createIncomplete : new boolean[] { true, false })
1661          for (boolean fFirst : new boolean[] { true, false })
1662 +        for (boolean failFirst : new boolean[] { true, false })
1663          for (Integer v1 : new Integer[] { 1, null })
1664      {
1665          final CompletableFuture<Integer> f = new CompletableFuture<>();
1666          final CompletableFuture<Integer> g = new CompletableFuture<>();
1667 <        final SubtractFunction r = new SubtractFunction(m);
1667 >        final SubtractFunction r1 = new SubtractFunction(m);
1668 >        final SubtractFunction r2 = new SubtractFunction(m);
1669 >        final SubtractFunction r3 = new SubtractFunction(m);
1670  
1671 <        (fFirst ? f : g).complete(v1);
1672 <        if (!createIncomplete)
1673 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1674 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1675 <        if (createIncomplete) {
1676 <            checkIncomplete(h);
1677 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1678 <        }
1671 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1672 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1673 >        final Callable<Boolean> complete1 = failFirst ?
1674 >            () -> fst.cancel(mayInterruptIfRunning) :
1675 >            () -> fst.complete(v1);
1676 >        final Callable<Boolean> complete2 = failFirst ?
1677 >            () -> snd.complete(v1) :
1678 >            () -> snd.cancel(mayInterruptIfRunning);
1679  
1680 <        checkCompletedWithWrappedCancellationException(h);
1681 <        checkCancelled(!fFirst ? f : g);
1682 <        r.assertNotInvoked();
1683 <        checkCompletedNormally(fFirst ? f : g, v1);
1680 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1681 >        assertTrue(complete1.call());
1682 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1683 >        checkIncomplete(h1);
1684 >        checkIncomplete(h2);
1685 >        assertTrue(complete2.call());
1686 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1687 >
1688 >        checkCompletedWithWrappedCancellationException(h1);
1689 >        checkCompletedWithWrappedCancellationException(h2);
1690 >        checkCompletedWithWrappedCancellationException(h3);
1691 >        r1.assertNotInvoked();
1692 >        r2.assertNotInvoked();
1693 >        r3.assertNotInvoked();
1694 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1695 >        checkCancelled(failFirst ? fst : snd);
1696      }}
1697  
1698      /**
# Line 1614 | Line 1706 | public class CompletableFutureTest exten
1706      {
1707          final CompletableFuture<Integer> f = new CompletableFuture<>();
1708          final CompletableFuture<Integer> g = new CompletableFuture<>();
1709 <        final FailingBiFunction r = new FailingBiFunction(m);
1710 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1709 >        final FailingBiFunction r1 = new FailingBiFunction(m);
1710 >        final FailingBiFunction r2 = new FailingBiFunction(m);
1711 >        final FailingBiFunction r3 = new FailingBiFunction(m);
1712 >
1713 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1714 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1715 >        final Integer w1 =  fFirst ? v1 : v2;
1716 >        final Integer w2 = !fFirst ? v1 : v2;
1717 >
1718 >        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1719 >        assertTrue(fst.complete(w1));
1720 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1721 >        assertTrue(snd.complete(w2));
1722 >        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1723  
1724 <        if (fFirst) {
1725 <            f.complete(v1);
1726 <            g.complete(v2);
1727 <        } else {
1728 <            g.complete(v2);
1729 <            f.complete(v1);
1626 <        }
1627 <
1628 <        checkCompletedWithWrappedCFException(h);
1724 >        checkCompletedWithWrappedCFException(h1);
1725 >        checkCompletedWithWrappedCFException(h2);
1726 >        checkCompletedWithWrappedCFException(h3);
1727 >        r1.assertInvoked();
1728 >        r2.assertInvoked();
1729 >        r3.assertInvoked();
1730          checkCompletedNormally(f, v1);
1731          checkCompletedNormally(g, v2);
1732      }}
# Line 1636 | Line 1737 | public class CompletableFutureTest exten
1737       */
1738      public void testThenAcceptBoth_normalCompletion() {
1739          for (ExecutionMode m : ExecutionMode.values())
1639        for (boolean createIncomplete : new boolean[] { true, false })
1740          for (boolean fFirst : new boolean[] { true, false })
1741          for (Integer v1 : new Integer[] { 1, null })
1742          for (Integer v2 : new Integer[] { 2, null })
1743      {
1744          final CompletableFuture<Integer> f = new CompletableFuture<>();
1745          final CompletableFuture<Integer> g = new CompletableFuture<>();
1746 <        final SubtractAction r = new SubtractAction(m);
1747 <
1748 <        if (fFirst) f.complete(v1); else g.complete(v2);
1749 <        if (!createIncomplete)
1750 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1751 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1752 <        if (createIncomplete) {
1753 <            checkIncomplete(h);
1754 <            r.assertNotInvoked();
1755 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1756 <        }
1746 >        final SubtractAction r1 = new SubtractAction(m);
1747 >        final SubtractAction r2 = new SubtractAction(m);
1748 >        final SubtractAction r3 = new SubtractAction(m);
1749 >
1750 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1751 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1752 >        final Integer w1 =  fFirst ? v1 : v2;
1753 >        final Integer w2 = !fFirst ? v1 : v2;
1754 >
1755 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1756 >        assertTrue(fst.complete(w1));
1757 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1758 >        checkIncomplete(h1);
1759 >        checkIncomplete(h2);
1760 >        r1.assertNotInvoked();
1761 >        r2.assertNotInvoked();
1762 >        assertTrue(snd.complete(w2));
1763 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1764  
1765 <        checkCompletedNormally(h, null);
1766 <        r.assertValue(subtract(v1, v2));
1765 >        checkCompletedNormally(h1, null);
1766 >        checkCompletedNormally(h2, null);
1767 >        checkCompletedNormally(h3, null);
1768 >        r1.assertValue(subtract(v1, v2));
1769 >        r2.assertValue(subtract(v1, v2));
1770 >        r3.assertValue(subtract(v1, v2));
1771          checkCompletedNormally(f, v1);
1772          checkCompletedNormally(g, v2);
1773      }}
# Line 1665 | Line 1776 | public class CompletableFutureTest exten
1776       * thenAcceptBoth result completes exceptionally after exceptional
1777       * completion of either source
1778       */
1779 <    public void testThenAcceptBoth_exceptionalCompletion() {
1779 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1780          for (ExecutionMode m : ExecutionMode.values())
1670        for (boolean createIncomplete : new boolean[] { true, false })
1781          for (boolean fFirst : new boolean[] { true, false })
1782 +        for (boolean failFirst : new boolean[] { true, false })
1783          for (Integer v1 : new Integer[] { 1, null })
1784      {
1785          final CompletableFuture<Integer> f = new CompletableFuture<>();
1786          final CompletableFuture<Integer> g = new CompletableFuture<>();
1787          final CFException ex = new CFException();
1788 <        final SubtractAction r = new SubtractAction(m);
1789 <
1790 <        (fFirst ? f : g).complete(v1);
1791 <        if (!createIncomplete)
1792 <            (!fFirst ? f : g).completeExceptionally(ex);
1793 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1794 <        if (createIncomplete) {
1795 <            checkIncomplete(h);
1796 <            (!fFirst ? f : g).completeExceptionally(ex);
1797 <        }
1788 >        final SubtractAction r1 = new SubtractAction(m);
1789 >        final SubtractAction r2 = new SubtractAction(m);
1790 >        final SubtractAction r3 = new SubtractAction(m);
1791 >
1792 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1793 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1794 >        final Callable<Boolean> complete1 = failFirst ?
1795 >            () -> fst.completeExceptionally(ex) :
1796 >            () -> fst.complete(v1);
1797 >        final Callable<Boolean> complete2 = failFirst ?
1798 >            () -> snd.complete(v1) :
1799 >            () -> snd.completeExceptionally(ex);
1800 >
1801 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1802 >        assertTrue(complete1.call());
1803 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1804 >        checkIncomplete(h1);
1805 >        checkIncomplete(h2);
1806 >        assertTrue(complete2.call());
1807 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1808  
1809 <        checkCompletedWithWrappedException(h, ex);
1810 <        r.assertNotInvoked();
1811 <        checkCompletedNormally(fFirst ? f : g, v1);
1812 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1809 >        checkCompletedWithWrappedException(h1, ex);
1810 >        checkCompletedWithWrappedException(h2, ex);
1811 >        checkCompletedWithWrappedException(h3, ex);
1812 >        r1.assertNotInvoked();
1813 >        r2.assertNotInvoked();
1814 >        r3.assertNotInvoked();
1815 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1816 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1817      }}
1818  
1819      /**
1820       * thenAcceptBoth result completes exceptionally if either source cancelled
1821       */
1822 <    public void testThenAcceptBoth_sourceCancelled() {
1822 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1823          for (ExecutionMode m : ExecutionMode.values())
1824          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1700        for (boolean createIncomplete : new boolean[] { true, false })
1825          for (boolean fFirst : new boolean[] { true, false })
1826 +        for (boolean failFirst : new boolean[] { true, false })
1827          for (Integer v1 : new Integer[] { 1, null })
1828      {
1829          final CompletableFuture<Integer> f = new CompletableFuture<>();
1830          final CompletableFuture<Integer> g = new CompletableFuture<>();
1831 <        final SubtractAction r = new SubtractAction(m);
1831 >        final SubtractAction r1 = new SubtractAction(m);
1832 >        final SubtractAction r2 = new SubtractAction(m);
1833 >        final SubtractAction r3 = new SubtractAction(m);
1834  
1835 <        (fFirst ? f : g).complete(v1);
1836 <        if (!createIncomplete)
1837 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1838 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1839 <        if (createIncomplete) {
1840 <            checkIncomplete(h);
1841 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1842 <        }
1835 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1836 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1837 >        final Callable<Boolean> complete1 = failFirst ?
1838 >            () -> fst.cancel(mayInterruptIfRunning) :
1839 >            () -> fst.complete(v1);
1840 >        final Callable<Boolean> complete2 = failFirst ?
1841 >            () -> snd.complete(v1) :
1842 >            () -> snd.cancel(mayInterruptIfRunning);
1843  
1844 <        checkCompletedWithWrappedCancellationException(h);
1845 <        checkCancelled(!fFirst ? f : g);
1846 <        r.assertNotInvoked();
1847 <        checkCompletedNormally(fFirst ? f : g, v1);
1844 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1845 >        assertTrue(complete1.call());
1846 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1847 >        checkIncomplete(h1);
1848 >        checkIncomplete(h2);
1849 >        assertTrue(complete2.call());
1850 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1851 >
1852 >        checkCompletedWithWrappedCancellationException(h1);
1853 >        checkCompletedWithWrappedCancellationException(h2);
1854 >        checkCompletedWithWrappedCancellationException(h3);
1855 >        r1.assertNotInvoked();
1856 >        r2.assertNotInvoked();
1857 >        r3.assertNotInvoked();
1858 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1859 >        checkCancelled(failFirst ? fst : snd);
1860      }}
1861  
1862      /**
# Line 1731 | Line 1870 | public class CompletableFutureTest exten
1870      {
1871          final CompletableFuture<Integer> f = new CompletableFuture<>();
1872          final CompletableFuture<Integer> g = new CompletableFuture<>();
1873 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1874 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1873 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1874 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1875 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1876 >
1877 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1878 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1879 >        final Integer w1 =  fFirst ? v1 : v2;
1880 >        final Integer w2 = !fFirst ? v1 : v2;
1881 >
1882 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1883 >        assertTrue(fst.complete(w1));
1884 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1885 >        assertTrue(snd.complete(w2));
1886 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1887  
1888 <        if (fFirst) {
1889 <            f.complete(v1);
1890 <            g.complete(v2);
1891 <        } else {
1892 <            g.complete(v2);
1893 <            f.complete(v1);
1743 <        }
1744 <
1745 <        checkCompletedWithWrappedCFException(h);
1888 >        checkCompletedWithWrappedCFException(h1);
1889 >        checkCompletedWithWrappedCFException(h2);
1890 >        checkCompletedWithWrappedCFException(h3);
1891 >        r1.assertInvoked();
1892 >        r2.assertInvoked();
1893 >        r3.assertInvoked();
1894          checkCompletedNormally(f, v1);
1895          checkCompletedNormally(g, v2);
1896      }}
# Line 1753 | Line 1901 | public class CompletableFutureTest exten
1901       */
1902      public void testRunAfterBoth_normalCompletion() {
1903          for (ExecutionMode m : ExecutionMode.values())
1756        for (boolean createIncomplete : new boolean[] { true, false })
1904          for (boolean fFirst : new boolean[] { true, false })
1905          for (Integer v1 : new Integer[] { 1, null })
1906          for (Integer v2 : new Integer[] { 2, null })
1907      {
1908          final CompletableFuture<Integer> f = new CompletableFuture<>();
1909          final CompletableFuture<Integer> g = new CompletableFuture<>();
1910 <        final Noop r = new Noop(m);
1911 <
1912 <        if (fFirst) f.complete(v1); else g.complete(v2);
1913 <        if (!createIncomplete)
1914 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1915 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1916 <        if (createIncomplete) {
1917 <            checkIncomplete(h);
1918 <            r.assertNotInvoked();
1919 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1920 <        }
1910 >        final Noop r1 = new Noop(m);
1911 >        final Noop r2 = new Noop(m);
1912 >        final Noop r3 = new Noop(m);
1913 >
1914 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1915 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1916 >        final Integer w1 =  fFirst ? v1 : v2;
1917 >        final Integer w2 = !fFirst ? v1 : v2;
1918 >
1919 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1920 >        assertTrue(fst.complete(w1));
1921 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1922 >        checkIncomplete(h1);
1923 >        checkIncomplete(h2);
1924 >        r1.assertNotInvoked();
1925 >        r2.assertNotInvoked();
1926 >        assertTrue(snd.complete(w2));
1927 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1928  
1929 <        checkCompletedNormally(h, null);
1930 <        r.assertInvoked();
1929 >        checkCompletedNormally(h1, null);
1930 >        checkCompletedNormally(h2, null);
1931 >        checkCompletedNormally(h3, null);
1932 >        r1.assertInvoked();
1933 >        r2.assertInvoked();
1934 >        r3.assertInvoked();
1935          checkCompletedNormally(f, v1);
1936          checkCompletedNormally(g, v2);
1937      }}
# Line 1782 | Line 1940 | public class CompletableFutureTest exten
1940       * runAfterBoth result completes exceptionally after exceptional
1941       * completion of either source
1942       */
1943 <    public void testRunAfterBoth_exceptionalCompletion() {
1943 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1944          for (ExecutionMode m : ExecutionMode.values())
1787        for (boolean createIncomplete : new boolean[] { true, false })
1945          for (boolean fFirst : new boolean[] { true, false })
1946 +        for (boolean failFirst : new boolean[] { true, false })
1947          for (Integer v1 : new Integer[] { 1, null })
1948      {
1949          final CompletableFuture<Integer> f = new CompletableFuture<>();
1950          final CompletableFuture<Integer> g = new CompletableFuture<>();
1951          final CFException ex = new CFException();
1952 <        final Noop r = new Noop(m);
1953 <
1954 <        (fFirst ? f : g).complete(v1);
1955 <        if (!createIncomplete)
1956 <            (!fFirst ? f : g).completeExceptionally(ex);
1957 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1958 <        if (createIncomplete) {
1959 <            checkIncomplete(h);
1960 <            (!fFirst ? f : g).completeExceptionally(ex);
1961 <        }
1952 >        final Noop r1 = new Noop(m);
1953 >        final Noop r2 = new Noop(m);
1954 >        final Noop r3 = new Noop(m);
1955 >
1956 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1957 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1958 >        final Callable<Boolean> complete1 = failFirst ?
1959 >            () -> fst.completeExceptionally(ex) :
1960 >            () -> fst.complete(v1);
1961 >        final Callable<Boolean> complete2 = failFirst ?
1962 >            () -> snd.complete(v1) :
1963 >            () -> snd.completeExceptionally(ex);
1964 >
1965 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1966 >        assertTrue(complete1.call());
1967 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1968 >        checkIncomplete(h1);
1969 >        checkIncomplete(h2);
1970 >        assertTrue(complete2.call());
1971 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1972  
1973 <        checkCompletedWithWrappedException(h, ex);
1974 <        r.assertNotInvoked();
1975 <        checkCompletedNormally(fFirst ? f : g, v1);
1976 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1973 >        checkCompletedWithWrappedException(h1, ex);
1974 >        checkCompletedWithWrappedException(h2, ex);
1975 >        checkCompletedWithWrappedException(h3, ex);
1976 >        r1.assertNotInvoked();
1977 >        r2.assertNotInvoked();
1978 >        r3.assertNotInvoked();
1979 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1980 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1981      }}
1982  
1983      /**
1984       * runAfterBoth result completes exceptionally if either source cancelled
1985       */
1986 <    public void testRunAfterBoth_sourceCancelled() {
1986 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
1987          for (ExecutionMode m : ExecutionMode.values())
1988          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1817        for (boolean createIncomplete : new boolean[] { true, false })
1989          for (boolean fFirst : new boolean[] { true, false })
1990 +        for (boolean failFirst : new boolean[] { true, false })
1991          for (Integer v1 : new Integer[] { 1, null })
1992      {
1993          final CompletableFuture<Integer> f = new CompletableFuture<>();
1994          final CompletableFuture<Integer> g = new CompletableFuture<>();
1995 <        final Noop r = new Noop(m);
1995 >        final Noop r1 = new Noop(m);
1996 >        final Noop r2 = new Noop(m);
1997 >        final Noop r3 = new Noop(m);
1998  
1999 <        (fFirst ? f : g).complete(v1);
2000 <        if (!createIncomplete)
2001 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2002 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2003 <        if (createIncomplete) {
2004 <            checkIncomplete(h);
2005 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2006 <        }
1999 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2000 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2001 >        final Callable<Boolean> complete1 = failFirst ?
2002 >            () -> fst.cancel(mayInterruptIfRunning) :
2003 >            () -> fst.complete(v1);
2004 >        final Callable<Boolean> complete2 = failFirst ?
2005 >            () -> snd.complete(v1) :
2006 >            () -> snd.cancel(mayInterruptIfRunning);
2007  
2008 <        checkCompletedWithWrappedCancellationException(h);
2009 <        checkCancelled(!fFirst ? f : g);
2010 <        r.assertNotInvoked();
2011 <        checkCompletedNormally(fFirst ? f : g, v1);
2008 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2009 >        assertTrue(complete1.call());
2010 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2011 >        checkIncomplete(h1);
2012 >        checkIncomplete(h2);
2013 >        assertTrue(complete2.call());
2014 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2015 >
2016 >        checkCompletedWithWrappedCancellationException(h1);
2017 >        checkCompletedWithWrappedCancellationException(h2);
2018 >        checkCompletedWithWrappedCancellationException(h3);
2019 >        r1.assertNotInvoked();
2020 >        r2.assertNotInvoked();
2021 >        r3.assertNotInvoked();
2022 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2023 >        checkCancelled(failFirst ? fst : snd);
2024      }}
2025  
2026      /**
# Line 1850 | Line 2036 | public class CompletableFutureTest exten
2036          final CompletableFuture<Integer> g = new CompletableFuture<>();
2037          final FailingRunnable r1 = new FailingRunnable(m);
2038          final FailingRunnable r2 = new FailingRunnable(m);
2039 +        final FailingRunnable r3 = new FailingRunnable(m);
2040  
2041 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2042 <        if (fFirst) {
2043 <            f.complete(v1);
2044 <            g.complete(v2);
2045 <        } else {
2046 <            g.complete(v2);
2047 <            f.complete(v1);
2048 <        }
2049 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2041 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2042 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2043 >        final Integer w1 =  fFirst ? v1 : v2;
2044 >        final Integer w2 = !fFirst ? v1 : v2;
2045 >
2046 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2047 >        assertTrue(fst.complete(w1));
2048 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2049 >        assertTrue(snd.complete(w2));
2050 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2051  
2052          checkCompletedWithWrappedCFException(h1);
2053          checkCompletedWithWrappedCFException(h2);
2054 +        checkCompletedWithWrappedCFException(h3);
2055 +        r1.assertInvoked();
2056 +        r2.assertInvoked();
2057 +        r3.assertInvoked();
2058          checkCompletedNormally(f, v1);
2059          checkCompletedNormally(g, v2);
2060      }}
# Line 1985 | Line 2177 | public class CompletableFutureTest exten
2177  
2178          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2179          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2180 <        if (fFirst) {
2181 <            f.complete(v1);
1990 <            g.completeExceptionally(ex);
1991 <        } else {
1992 <            g.completeExceptionally(ex);
1993 <            f.complete(v1);
1994 <        }
2180 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2181 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2182          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2183          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2184  
# Line 2097 | Line 2284 | public class CompletableFutureTest exten
2284  
2285          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2286          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2287 <        if (fFirst) {
2288 <            f.complete(v1);
2102 <            g.cancel(mayInterruptIfRunning);
2103 <        } else {
2104 <            g.cancel(mayInterruptIfRunning);
2105 <            f.complete(v1);
2106 <        }
2287 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2288 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2289          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2290          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2291  
# Line 2305 | Line 2487 | public class CompletableFutureTest exten
2487  
2488          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2489          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2490 <        if (fFirst) {
2491 <            f.complete(v1);
2310 <            g.completeExceptionally(ex);
2311 <        } else {
2312 <            g.completeExceptionally(ex);
2313 <            f.complete(v1);
2314 <        }
2490 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2491 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2492          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2493          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2494  
# Line 2514 | Line 2691 | public class CompletableFutureTest exten
2691          checkIncomplete(h1);
2692          rs[0].assertNotInvoked();
2693          rs[1].assertNotInvoked();
2694 <        f.completeExceptionally(ex);
2694 >        assertTrue(f.completeExceptionally(ex));
2695          checkCompletedWithWrappedException(h0, ex);
2696          checkCompletedWithWrappedException(h1, ex);
2697          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
# Line 2522 | Line 2699 | public class CompletableFutureTest exten
2699          checkCompletedWithWrappedException(h2, ex);
2700          checkCompletedWithWrappedException(h3, ex);
2701  
2702 <        g.complete(v1);
2702 >        assertTrue(g.complete(v1));
2703  
2704          // unspecified behavior - both source completions available
2705          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2565 | Line 2742 | public class CompletableFutureTest exten
2742  
2743          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2744          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2745 <        if (fFirst) {
2746 <            f.complete(v1);
2570 <            g.completeExceptionally(ex);
2571 <        } else {
2572 <            g.completeExceptionally(ex);
2573 <            f.complete(v1);
2574 <        }
2745 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2746 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2747          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2748          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2749  
# Line 2636 | Line 2808 | public class CompletableFutureTest exten
2808          checkCompletedWithWrappedCancellationException(h2);
2809          checkCompletedWithWrappedCancellationException(h3);
2810  
2811 <        g.complete(v1);
2811 >        assertTrue(g.complete(v1));
2812  
2813          // unspecified behavior - both source completions available
2814          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2680 | Line 2852 | public class CompletableFutureTest exten
2852  
2853          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2854          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2855 <        f.complete(v1);
2855 >        assertTrue(f.complete(v1));
2856          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2857          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2858          checkCompletedWithWrappedCFException(h0);
# Line 2688 | Line 2860 | public class CompletableFutureTest exten
2860          checkCompletedWithWrappedCFException(h2);
2861          checkCompletedWithWrappedCFException(h3);
2862          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2863 <        g.complete(v2);
2863 >        assertTrue(g.complete(v2));
2864          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2865          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2866          checkCompletedWithWrappedCFException(h4);
# Line 2709 | Line 2881 | public class CompletableFutureTest exten
2881      {
2882          final CompletableFuture<Integer> f = new CompletableFuture<>();
2883          final CompletableFutureInc r = new CompletableFutureInc(m);
2884 <        if (!createIncomplete) f.complete(v1);
2884 >        if (!createIncomplete) assertTrue(f.complete(v1));
2885          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2886 <        if (createIncomplete) f.complete(v1);
2886 >        if (createIncomplete) assertTrue(f.complete(v1));
2887  
2888          checkCompletedNormally(g, inc(v1));
2889          checkCompletedNormally(f, v1);
# Line 2749 | Line 2921 | public class CompletableFutureTest exten
2921          final CompletableFuture<Integer> f = new CompletableFuture<>();
2922          final FailingCompletableFutureFunction r
2923              = new FailingCompletableFutureFunction(m);
2924 <        if (!createIncomplete) f.complete(v1);
2924 >        if (!createIncomplete) assertTrue(f.complete(v1));
2925          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2926 <        if (createIncomplete) f.complete(v1);
2926 >        if (createIncomplete) assertTrue(f.complete(v1));
2927  
2928          checkCompletedWithWrappedCFException(g);
2929          checkCompletedNormally(f, v1);
# Line 2778 | Line 2950 | public class CompletableFutureTest exten
2950          checkCancelled(f);
2951      }}
2952  
2953 +    /**
2954 +     * thenCompose result completes exceptionally if the result of the action does
2955 +     */
2956 +    public void testThenCompose_actionReturnsFailingFuture() {
2957 +        for (ExecutionMode m : ExecutionMode.values())
2958 +        for (int order = 0; order < 6; order++)
2959 +        for (Integer v1 : new Integer[] { 1, null })
2960 +    {
2961 +        final CFException ex = new CFException();
2962 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2963 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2964 +        final CompletableFuture<Integer> h;
2965 +        // Test all permutations of orders
2966 +        switch (order) {
2967 +        case 0:
2968 +            assertTrue(f.complete(v1));
2969 +            assertTrue(g.completeExceptionally(ex));
2970 +            h = m.thenCompose(f, (x -> g));
2971 +            break;
2972 +        case 1:
2973 +            assertTrue(f.complete(v1));
2974 +            h = m.thenCompose(f, (x -> g));
2975 +            assertTrue(g.completeExceptionally(ex));
2976 +            break;
2977 +        case 2:
2978 +            assertTrue(g.completeExceptionally(ex));
2979 +            assertTrue(f.complete(v1));
2980 +            h = m.thenCompose(f, (x -> g));
2981 +            break;
2982 +        case 3:
2983 +            assertTrue(g.completeExceptionally(ex));
2984 +            h = m.thenCompose(f, (x -> g));
2985 +            assertTrue(f.complete(v1));
2986 +            break;
2987 +        case 4:
2988 +            h = m.thenCompose(f, (x -> g));
2989 +            assertTrue(f.complete(v1));
2990 +            assertTrue(g.completeExceptionally(ex));
2991 +            break;
2992 +        case 5:
2993 +            h = m.thenCompose(f, (x -> g));
2994 +            assertTrue(f.complete(v1));
2995 +            assertTrue(g.completeExceptionally(ex));
2996 +            break;
2997 +        default: throw new AssertionError();
2998 +        }
2999 +
3000 +        checkCompletedExceptionally(g, ex);
3001 +        checkCompletedWithWrappedException(h, ex);
3002 +        checkCompletedNormally(f, v1);
3003 +    }}
3004 +
3005      // other static methods
3006  
3007      /**
# Line 2794 | Line 3018 | public class CompletableFutureTest exten
3018       * when all components complete normally
3019       */
3020      public void testAllOf_normal() throws Exception {
3021 <        for (int k = 1; k < 20; ++k) {
3021 >        for (int k = 1; k < 10; k++) {
3022              CompletableFuture<Integer>[] fs
3023                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3024 <            for (int i = 0; i < k; ++i)
3024 >            for (int i = 0; i < k; i++)
3025                  fs[i] = new CompletableFuture<>();
3026              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3027 <            for (int i = 0; i < k; ++i) {
3027 >            for (int i = 0; i < k; i++) {
3028                  checkIncomplete(f);
3029                  checkIncomplete(CompletableFuture.allOf(fs));
3030                  fs[i].complete(one);
# Line 2811 | Line 3035 | public class CompletableFutureTest exten
3035      }
3036  
3037      public void testAllOf_backwards() throws Exception {
3038 <        for (int k = 1; k < 20; ++k) {
3038 >        for (int k = 1; k < 10; k++) {
3039              CompletableFuture<Integer>[] fs
3040                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3041 <            for (int i = 0; i < k; ++i)
3041 >            for (int i = 0; i < k; i++)
3042                  fs[i] = new CompletableFuture<>();
3043              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3044              for (int i = k - 1; i >= 0; i--) {
# Line 2827 | Line 3051 | public class CompletableFutureTest exten
3051          }
3052      }
3053  
3054 +    public void testAllOf_exceptional() throws Exception {
3055 +        for (int k = 1; k < 10; k++) {
3056 +            CompletableFuture<Integer>[] fs
3057 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3058 +            CFException ex = new CFException();
3059 +            for (int i = 0; i < k; i++)
3060 +                fs[i] = new CompletableFuture<>();
3061 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3062 +            for (int i = 0; i < k; i++) {
3063 +                checkIncomplete(f);
3064 +                checkIncomplete(CompletableFuture.allOf(fs));
3065 +                if (i != k / 2) {
3066 +                    fs[i].complete(i);
3067 +                    checkCompletedNormally(fs[i], i);
3068 +                } else {
3069 +                    fs[i].completeExceptionally(ex);
3070 +                    checkCompletedExceptionally(fs[i], ex);
3071 +                }
3072 +            }
3073 +            checkCompletedWithWrappedException(f, ex);
3074 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3075 +        }
3076 +    }
3077 +
3078      /**
3079       * anyOf(no component futures) returns an incomplete future
3080       */
3081      public void testAnyOf_empty() throws Exception {
3082 +        for (Integer v1 : new Integer[] { 1, null })
3083 +    {
3084          CompletableFuture<Object> f = CompletableFuture.anyOf();
3085          checkIncomplete(f);
3086 <    }
3086 >
3087 >        f.complete(v1);
3088 >        checkCompletedNormally(f, v1);
3089 >    }}
3090  
3091      /**
3092       * anyOf returns a future completed normally with a value when
3093       * a component future does
3094       */
3095      public void testAnyOf_normal() throws Exception {
3096 <        for (int k = 0; k < 10; ++k) {
3096 >        for (int k = 0; k < 10; k++) {
3097              CompletableFuture[] fs = new CompletableFuture[k];
3098 <            for (int i = 0; i < k; ++i)
3098 >            for (int i = 0; i < k; i++)
3099                  fs[i] = new CompletableFuture<>();
3100              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3101              checkIncomplete(f);
3102 <            for (int i = 0; i < k; ++i) {
3103 <                fs[i].complete(one);
3104 <                checkCompletedNormally(f, one);
3105 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3102 >            for (int i = 0; i < k; i++) {
3103 >                fs[i].complete(i);
3104 >                checkCompletedNormally(f, 0);
3105 >                int x = (int) CompletableFuture.anyOf(fs).join();
3106 >                assertTrue(0 <= x && x <= i);
3107 >            }
3108 >        }
3109 >    }
3110 >    public void testAnyOf_normal_backwards() throws Exception {
3111 >        for (int k = 0; k < 10; k++) {
3112 >            CompletableFuture[] fs = new CompletableFuture[k];
3113 >            for (int i = 0; i < k; i++)
3114 >                fs[i] = new CompletableFuture<>();
3115 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3116 >            checkIncomplete(f);
3117 >            for (int i = k - 1; i >= 0; i--) {
3118 >                fs[i].complete(i);
3119 >                checkCompletedNormally(f, k - 1);
3120 >                int x = (int) CompletableFuture.anyOf(fs).join();
3121 >                assertTrue(i <= x && x <= k - 1);
3122              }
3123          }
3124      }
# Line 2858 | Line 3127 | public class CompletableFutureTest exten
3127       * anyOf result completes exceptionally when any component does.
3128       */
3129      public void testAnyOf_exceptional() throws Exception {
3130 <        for (int k = 0; k < 10; ++k) {
3130 >        for (int k = 0; k < 10; k++) {
3131              CompletableFuture[] fs = new CompletableFuture[k];
3132 <            for (int i = 0; i < k; ++i)
3132 >            CFException[] exs = new CFException[k];
3133 >            for (int i = 0; i < k; i++) {
3134                  fs[i] = new CompletableFuture<>();
3135 +                exs[i] = new CFException();
3136 +            }
3137              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3138              checkIncomplete(f);
3139 <            for (int i = 0; i < k; ++i) {
3140 <                fs[i].completeExceptionally(new CFException());
3141 <                checkCompletedWithWrappedCFException(f);
3139 >            for (int i = 0; i < k; i++) {
3140 >                fs[i].completeExceptionally(exs[i]);
3141 >                checkCompletedWithWrappedException(f, exs[0]);
3142 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3143 >            }
3144 >        }
3145 >    }
3146 >
3147 >    public void testAnyOf_exceptional_backwards() throws Exception {
3148 >        for (int k = 0; k < 10; k++) {
3149 >            CompletableFuture[] fs = new CompletableFuture[k];
3150 >            CFException[] exs = new CFException[k];
3151 >            for (int i = 0; i < k; i++) {
3152 >                fs[i] = new CompletableFuture<>();
3153 >                exs[i] = new CFException();
3154 >            }
3155 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3156 >            checkIncomplete(f);
3157 >            for (int i = k - 1; i >= 0; i--) {
3158 >                fs[i].completeExceptionally(exs[i]);
3159 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3160                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3161              }
3162          }
# Line 2879 | Line 3169 | public class CompletableFutureTest exten
3169          CompletableFuture<Integer> f = new CompletableFuture<>();
3170          CompletableFuture<Integer> g = new CompletableFuture<>();
3171          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
2882        CompletableFuture<?> h;
3172          ThreadExecutor exec = new ThreadExecutor();
3173  
3174          Runnable[] throwingActions = {
3175              () -> CompletableFuture.supplyAsync(null),
3176              () -> CompletableFuture.supplyAsync(null, exec),
3177 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3177 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3178  
3179              () -> CompletableFuture.runAsync(null),
3180              () -> CompletableFuture.runAsync(null, exec),
# Line 2976 | Line 3265 | public class CompletableFutureTest exten
3265              () -> CompletableFuture.anyOf(null, f),
3266  
3267              () -> f.obtrudeException(null),
3268 +
3269 +            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3270 +            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3271 +            () -> CompletableFuture.delayedExecutor(1L, null),
3272 +
3273 +            () -> f.orTimeout(1L, null),
3274 +            () -> f.completeOnTimeout(42, 1L, null),
3275 +
3276 +            () -> CompletableFuture.failedFuture(null),
3277 +            () -> CompletableFuture.failedStage(null),
3278          };
3279  
3280          assertThrows(NullPointerException.class, throwingActions);
# Line 2990 | Line 3289 | public class CompletableFutureTest exten
3289          assertSame(f, f.toCompletableFuture());
3290      }
3291  
3292 +    // jdk9
3293 +
3294 +    /**
3295 +     * newIncompleteFuture returns an incomplete CompletableFuture
3296 +     */
3297 +    public void testNewIncompleteFuture() {
3298 +        for (Integer v1 : new Integer[] { 1, null })
3299 +    {
3300 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3301 +        CompletableFuture<Integer> g = f.newIncompleteFuture();
3302 +        checkIncomplete(f);
3303 +        checkIncomplete(g);
3304 +        f.complete(v1);
3305 +        checkCompletedNormally(f, v1);
3306 +        checkIncomplete(g);
3307 +        g.complete(v1);
3308 +        checkCompletedNormally(g, v1);
3309 +        assertSame(g.getClass(), CompletableFuture.class);
3310 +    }}
3311 +
3312 +    /**
3313 +     * completedStage returns a completed CompletionStage
3314 +     */
3315 +    public void testCompletedStage() {
3316 +        AtomicInteger x = new AtomicInteger(0);
3317 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3318 +        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3319 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3320 +        assertEquals(x.get(), 1);
3321 +        assertNull(r.get());
3322 +    }
3323 +
3324 +    /**
3325 +     * defaultExecutor by default returns the commonPool if
3326 +     * it supports more than one thread.
3327 +     */
3328 +    public void testDefaultExecutor() {
3329 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3330 +        Executor e = f.defaultExecutor();
3331 +        Executor c = ForkJoinPool.commonPool();
3332 +        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3333 +            assertSame(e, c);
3334 +        else
3335 +            assertNotSame(e, c);
3336 +    }
3337 +
3338 +    /**
3339 +     * failedFuture returns a CompletableFuture completed
3340 +     * exceptionally with the given Exception
3341 +     */
3342 +    public void testFailedFuture() {
3343 +        CFException ex = new CFException();
3344 +        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3345 +        checkCompletedExceptionally(f, ex);
3346 +    }
3347 +
3348 +    /**
3349 +     * failedFuture(null) throws NPE
3350 +     */
3351 +    public void testFailedFuture_null() {
3352 +        try {
3353 +            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3354 +            shouldThrow();
3355 +        } catch (NullPointerException success) {}
3356 +    }
3357 +
3358 +    /**
3359 +     * copy returns a CompletableFuture that is completed normally,
3360 +     * with the same value, when source is.
3361 +     */
3362 +    public void testCopy() {
3363 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3364 +        CompletableFuture<Integer> g = f.copy();
3365 +        checkIncomplete(f);
3366 +        checkIncomplete(g);
3367 +        f.complete(1);
3368 +        checkCompletedNormally(f, 1);
3369 +        checkCompletedNormally(g, 1);
3370 +    }
3371 +
3372 +    /**
3373 +     * copy returns a CompletableFuture that is completed exceptionally
3374 +     * when source is.
3375 +     */
3376 +    public void testCopy2() {
3377 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3378 +        CompletableFuture<Integer> g = f.copy();
3379 +        checkIncomplete(f);
3380 +        checkIncomplete(g);
3381 +        CFException ex = new CFException();
3382 +        f.completeExceptionally(ex);
3383 +        checkCompletedExceptionally(f, ex);
3384 +        checkCompletedWithWrappedException(g, ex);
3385 +    }
3386 +
3387 +    /**
3388 +     * minimalCompletionStage returns a CompletableFuture that is
3389 +     * completed normally, with the same value, when source is.
3390 +     */
3391 +    public void testMinimalCompletionStage() {
3392 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3393 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3394 +        AtomicInteger x = new AtomicInteger(0);
3395 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3396 +        checkIncomplete(f);
3397 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3398 +        f.complete(1);
3399 +        checkCompletedNormally(f, 1);
3400 +        assertEquals(x.get(), 1);
3401 +        assertNull(r.get());
3402 +    }
3403 +
3404 +    /**
3405 +     * minimalCompletionStage returns a CompletableFuture that is
3406 +     * completed exceptionally when source is.
3407 +     */
3408 +    public void testMinimalCompletionStage2() {
3409 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3410 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3411 +        AtomicInteger x = new AtomicInteger(0);
3412 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3413 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3414 +        checkIncomplete(f);
3415 +        CFException ex = new CFException();
3416 +        f.completeExceptionally(ex);
3417 +        checkCompletedExceptionally(f, ex);
3418 +        assertEquals(x.get(), 0);
3419 +        assertEquals(r.get().getCause(), ex);
3420 +    }
3421 +
3422 +    /**
3423 +     * failedStage returns a CompletionStage completed
3424 +     * exceptionally with the given Exception
3425 +     */
3426 +    public void testFailedStage() {
3427 +        CFException ex = new CFException();
3428 +        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3429 +        AtomicInteger x = new AtomicInteger(0);
3430 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3431 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3432 +        assertEquals(x.get(), 0);
3433 +        assertEquals(r.get(), ex);
3434 +    }
3435 +
3436 +    /**
3437 +     * completeAsync completes with value of given supplier
3438 +     */
3439 +    public void testCompleteAsync() {
3440 +        for (Integer v1 : new Integer[] { 1, null })
3441 +    {
3442 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3443 +        f.completeAsync(() -> v1);
3444 +        f.join();
3445 +        checkCompletedNormally(f, v1);
3446 +    }}
3447 +
3448 +    /**
3449 +     * completeAsync completes exceptionally if given supplier throws
3450 +     */
3451 +    public void testCompleteAsync2() {
3452 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3453 +        CFException ex = new CFException();
3454 +        f.completeAsync(() -> {if (true) throw ex; return 1;});
3455 +        try {
3456 +            f.join();
3457 +            shouldThrow();
3458 +        } catch (CompletionException success) {}
3459 +        checkCompletedWithWrappedException(f, ex);
3460 +    }
3461 +
3462 +    /**
3463 +     * completeAsync with given executor completes with value of given supplier
3464 +     */
3465 +    public void testCompleteAsync3() {
3466 +        for (Integer v1 : new Integer[] { 1, null })
3467 +    {
3468 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3469 +        ThreadExecutor executor = new ThreadExecutor();
3470 +        f.completeAsync(() -> v1, executor);
3471 +        assertSame(v1, f.join());
3472 +        checkCompletedNormally(f, v1);
3473 +        assertEquals(1, executor.count.get());
3474 +    }}
3475 +
3476 +    /**
3477 +     * completeAsync with given executor completes exceptionally if
3478 +     * given supplier throws
3479 +     */
3480 +    public void testCompleteAsync4() {
3481 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3482 +        CFException ex = new CFException();
3483 +        ThreadExecutor executor = new ThreadExecutor();
3484 +        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3485 +        try {
3486 +            f.join();
3487 +            shouldThrow();
3488 +        } catch (CompletionException success) {}
3489 +        checkCompletedWithWrappedException(f, ex);
3490 +        assertEquals(1, executor.count.get());
3491 +    }
3492 +
3493 +    /**
3494 +     * orTimeout completes with TimeoutException if not complete
3495 +     */
3496 +    public void testOrTimeout_timesOut() {
3497 +        long timeoutMillis = timeoutMillis();
3498 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3499 +        long startTime = System.nanoTime();
3500 +        f.orTimeout(timeoutMillis, MILLISECONDS);
3501 +        checkCompletedWithTimeoutException(f);
3502 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3503 +    }
3504 +
3505 +    /**
3506 +     * orTimeout completes normally if completed before timeout
3507 +     */
3508 +    public void testOrTimeout_completed() {
3509 +        for (Integer v1 : new Integer[] { 1, null })
3510 +    {
3511 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3512 +        CompletableFuture<Integer> g = new CompletableFuture<>();
3513 +        long startTime = System.nanoTime();
3514 +        f.complete(v1);
3515 +        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3516 +        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3517 +        g.complete(v1);
3518 +        checkCompletedNormally(f, v1);
3519 +        checkCompletedNormally(g, v1);
3520 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3521 +    }}
3522 +
3523 +    /**
3524 +     * completeOnTimeout completes with given value if not complete
3525 +     */
3526 +    public void testCompleteOnTimeout_timesOut() {
3527 +        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3528 +                       () -> testCompleteOnTimeout_timesOut(null));
3529 +    }
3530 +
3531 +    public void testCompleteOnTimeout_timesOut(Integer v) {
3532 +        long timeoutMillis = timeoutMillis();
3533 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3534 +        long startTime = System.nanoTime();
3535 +        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3536 +        assertSame(v, f.join());
3537 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3538 +        f.complete(99);         // should have no effect
3539 +        checkCompletedNormally(f, v);
3540 +    }
3541 +
3542 +    /**
3543 +     * completeOnTimeout has no effect if completed within timeout
3544 +     */
3545 +    public void testCompleteOnTimeout_completed() {
3546 +        for (Integer v1 : new Integer[] { 1, null })
3547 +    {
3548 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3549 +        CompletableFuture<Integer> g = new CompletableFuture<>();
3550 +        long startTime = System.nanoTime();
3551 +        f.complete(v1);
3552 +        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3553 +        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3554 +        g.complete(v1);
3555 +        checkCompletedNormally(f, v1);
3556 +        checkCompletedNormally(g, v1);
3557 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3558 +    }}
3559 +
3560 +    /**
3561 +     * delayedExecutor returns an executor that delays submission
3562 +     */
3563 +    public void testDelayedExecutor() {
3564 +        testInParallel(() -> testDelayedExecutor(null, null),
3565 +                       () -> testDelayedExecutor(null, 1),
3566 +                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3567 +                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3568 +    }
3569 +
3570 +    /**
3571 +     * delayedExecutor returns an executor that delays submission
3572 +     */
3573 +    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3574 +        long timeoutMillis = timeoutMillis();
3575 +        // Use an "unreasonably long" long timeout to catch lingering threads
3576 +        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3577 +        final Executor delayer, longDelayer;
3578 +        if (executor == null) {
3579 +            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3580 +            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3581 +        } else {
3582 +            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3583 +            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3584 +        }
3585 +        long startTime = System.nanoTime();
3586 +        CompletableFuture<Integer> f =
3587 +            CompletableFuture.supplyAsync(() -> v, delayer);
3588 +        CompletableFuture<Integer> g =
3589 +            CompletableFuture.supplyAsync(() -> v, longDelayer);
3590 +
3591 +        assertNull(g.getNow(null));
3592 +
3593 +        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3594 +        long millisElapsed = millisElapsedSince(startTime);
3595 +        assertTrue(millisElapsed >= timeoutMillis);
3596 +        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3597 +
3598 +        checkCompletedNormally(f, v);
3599 +
3600 +        checkIncomplete(g);
3601 +        assertTrue(g.cancel(true));
3602 +    }
3603 +
3604 +    //--- tests of implementation details; not part of official tck ---
3605 +
3606 +    Object resultOf(CompletableFuture<?> f) {
3607 +        try {
3608 +            java.lang.reflect.Field resultField
3609 +                = CompletableFuture.class.getDeclaredField("result");
3610 +            resultField.setAccessible(true);
3611 +            return resultField.get(f);
3612 +        } catch (Throwable t) { throw new AssertionError(t); }
3613 +    }
3614 +
3615 +    public void testExceptionPropagationReusesResultObject() {
3616 +        if (!testImplementationDetails) return;
3617 +        for (ExecutionMode m : ExecutionMode.values())
3618 +    {
3619 +        final CFException ex = new CFException();
3620 +        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3621 +        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3622 +
3623 +        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3624 +            = new ArrayList<>();
3625 +
3626 +        funs.add((y) -> m.thenRun(y, new Noop(m)));
3627 +        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3628 +        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3629 +
3630 +        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3631 +        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3632 +        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3633 +
3634 +        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3635 +        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3636 +        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3637 +
3638 +        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3639 +
3640 +        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3641 +
3642 +        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3643 +        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3644 +
3645 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3646 +                 fun : funs) {
3647 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3648 +            f.completeExceptionally(ex);
3649 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3650 +            checkCompletedWithWrappedException(src, ex);
3651 +            CompletableFuture<?> dep = fun.apply(src);
3652 +            checkCompletedWithWrappedException(dep, ex);
3653 +            assertSame(resultOf(src), resultOf(dep));
3654 +        }
3655 +
3656 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3657 +                 fun : funs) {
3658 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3659 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3660 +            CompletableFuture<?> dep = fun.apply(src);
3661 +            f.completeExceptionally(ex);
3662 +            checkCompletedWithWrappedException(src, ex);
3663 +            checkCompletedWithWrappedException(dep, ex);
3664 +            assertSame(resultOf(src), resultOf(dep));
3665 +        }
3666 +
3667 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3668 +        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3669 +                 fun : funs) {
3670 +            CompletableFuture<Integer> f = new CompletableFuture<>();
3671 +            f.cancel(mayInterruptIfRunning);
3672 +            checkCancelled(f);
3673 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3674 +            checkCompletedWithWrappedCancellationException(src);
3675 +            CompletableFuture<?> dep = fun.apply(src);
3676 +            checkCompletedWithWrappedCancellationException(dep);
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 +            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3685 +            CompletableFuture<?> dep = fun.apply(src);
3686 +            f.cancel(mayInterruptIfRunning);
3687 +            checkCancelled(f);
3688 +            checkCompletedWithWrappedCancellationException(src);
3689 +            checkCompletedWithWrappedCancellationException(dep);
3690 +            assertSame(resultOf(src), resultOf(dep));
3691 +        }
3692 +    }}
3693 +
3694   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines