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.81 by jsr166, Mon Jun 16 17:41:26 2014 UTC vs.
Revision 1.123 by jsr166, Tue Sep 8 19:45:35 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.lang.reflect.Method;
12 > import java.lang.reflect.Modifier;
13 >
14 > import java.util.stream.Collectors;
15 > import java.util.stream.Stream;
16 >
17 > import java.util.ArrayList;
18 > import java.util.Arrays;
19 > import java.util.List;
20 > import java.util.Objects;
21 > import java.util.Set;
22   import java.util.concurrent.Callable;
10 import java.util.concurrent.Executor;
11 import java.util.concurrent.ExecutorService;
12 import java.util.concurrent.Executors;
23   import java.util.concurrent.CancellationException;
14 import java.util.concurrent.CountDownLatch;
15 import java.util.concurrent.ExecutionException;
16 import java.util.concurrent.Future;
24   import java.util.concurrent.CompletableFuture;
25   import java.util.concurrent.CompletionException;
26   import java.util.concurrent.CompletionStage;
27 + import java.util.concurrent.ExecutionException;
28 + import java.util.concurrent.Executor;
29   import java.util.concurrent.ForkJoinPool;
30   import java.util.concurrent.ForkJoinTask;
31   import java.util.concurrent.TimeoutException;
32 + import java.util.concurrent.TimeUnit;
33   import java.util.concurrent.atomic.AtomicInteger;
34 < 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;
34 > import java.util.concurrent.atomic.AtomicReference;
35   import java.util.function.BiConsumer;
30 import java.util.function.Function;
36   import java.util.function.BiFunction;
37 + import java.util.function.Consumer;
38 + import java.util.function.Function;
39 + import java.util.function.Supplier;
40 +
41 + import junit.framework.Test;
42 + import junit.framework.TestSuite;
43  
44   public class CompletableFutureTest extends JSR166TestCase {
45  
46      public static void main(String[] args) {
47 <        junit.textui.TestRunner.run(suite());
47 >        main(suite(), args);
48      }
49      public static Test suite() {
50          return new TestSuite(CompletableFutureTest.class);
# Line 44 | Line 55 | public class CompletableFutureTest exten
55      void checkIncomplete(CompletableFuture<?> f) {
56          assertFalse(f.isDone());
57          assertFalse(f.isCancelled());
58 <        assertTrue(f.toString().contains("[Not completed]"));
58 >        assertTrue(f.toString().contains("Not completed"));
59          try {
60              assertNull(f.getNow(null));
61          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 57 | Line 68 | public class CompletableFutureTest exten
68      }
69  
70      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
71 <        try {
72 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
71 >        checkTimedGet(f, value);
72 >
73          try {
74              assertEquals(value, f.join());
75          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 75 | Line 85 | public class CompletableFutureTest exten
85          assertTrue(f.toString().contains("[Completed normally]"));
86      }
87  
88 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
89 <        try {
90 <            f.get(LONG_DELAY_MS, MILLISECONDS);
91 <            shouldThrow();
92 <        } catch (ExecutionException success) {
93 <            assertTrue(success.getCause() instanceof CFException);
94 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
95 <        try {
96 <            f.join();
97 <            shouldThrow();
98 <        } catch (CompletionException success) {
99 <            assertTrue(success.getCause() instanceof CFException);
100 <        }
101 <        try {
102 <            f.getNow(null);
103 <            shouldThrow();
104 <        } catch (CompletionException success) {
95 <            assertTrue(success.getCause() instanceof CFException);
88 >    /**
89 >     * Returns the "raw" internal exceptional completion of f,
90 >     * without any additional wrapping with CompletionException.
91 >     */
92 >    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
93 >        // handle (and whenComplete) can distinguish between "direct"
94 >        // and "wrapped" exceptional completion
95 >        return f.handle((U u, Throwable t) -> t).join();
96 >    }
97 >
98 >    void checkCompletedExceptionally(CompletableFuture<?> f,
99 >                                     boolean wrapped,
100 >                                     Consumer<Throwable> checker) {
101 >        Throwable cause = exceptionalCompletion(f);
102 >        if (wrapped) {
103 >            assertTrue(cause instanceof CompletionException);
104 >            cause = cause.getCause();
105          }
106 <        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 <    }
106 >        checker.accept(cause);
107  
108 <    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
109 <                                                      Throwable ex) {
108 >        long startTime = System.nanoTime();
109          try {
110              f.get(LONG_DELAY_MS, MILLISECONDS);
111              shouldThrow();
112          } catch (ExecutionException success) {
113 <            assertSame(ex, success.getCause());
113 >            assertSame(cause, success.getCause());
114          } catch (Throwable fail) { threadUnexpectedException(fail); }
115 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
116 +
117          try {
118              f.join();
119              shouldThrow();
120          } catch (CompletionException success) {
121 <            assertSame(ex, success.getCause());
122 <        }
121 >            assertSame(cause, success.getCause());
122 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
123 >
124          try {
125              f.getNow(null);
126              shouldThrow();
127          } catch (CompletionException success) {
128 <            assertSame(ex, success.getCause());
129 <        }
128 >            assertSame(cause, success.getCause());
129 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
130 >
131          try {
132              f.get();
133              shouldThrow();
134          } catch (ExecutionException success) {
135 <            assertSame(ex, success.getCause());
135 >            assertSame(cause, success.getCause());
136          } catch (Throwable fail) { threadUnexpectedException(fail); }
137  
135        assertTrue(f.isDone());
138          assertFalse(f.isCancelled());
139 +        assertTrue(f.isDone());
140 +        assertTrue(f.isCompletedExceptionally());
141          assertTrue(f.toString().contains("[Completed exceptionally]"));
142      }
143  
144 <    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
145 <                                                Throwable ex) {
146 <        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); }
144 >    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
145 >        checkCompletedExceptionally(f, true,
146 >            (t) -> assertTrue(t instanceof CFException));
147      }
148  
149 <    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
150 <        checkCompletedExceptionallyWithRootCause(f, ex);
151 <        try {
152 <            CompletableFuture<Throwable> spy = f.handle
153 <                ((U u, Throwable t) -> t);
154 <            assertSame(ex, spy.join());
155 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
149 >    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
150 >        checkCompletedExceptionally(f, true,
151 >            (t) -> assertTrue(t instanceof CancellationException));
152 >    }
153 >
154 >    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
155 >        checkCompletedExceptionally(f, false,
156 >            (t) -> assertTrue(t instanceof TimeoutException));
157 >    }
158 >
159 >    void checkCompletedWithWrappedException(CompletableFuture<?> f,
160 >                                            Throwable ex) {
161 >        checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
162 >    }
163 >
164 >    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
165 >        checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
166      }
167  
168      void checkCancelled(CompletableFuture<?> f) {
169 +        long startTime = System.nanoTime();
170          try {
171              f.get(LONG_DELAY_MS, MILLISECONDS);
172              shouldThrow();
173          } catch (CancellationException success) {
174          } catch (Throwable fail) { threadUnexpectedException(fail); }
175 +        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
176 +
177          try {
178              f.join();
179              shouldThrow();
# Line 176 | Line 187 | public class CompletableFutureTest exten
187              shouldThrow();
188          } catch (CancellationException success) {
189          } 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    }
190  
191 <    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
192 <        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); }
191 >        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
192 >
193          assertTrue(f.isDone());
211        assertFalse(f.isCancelled());
194          assertTrue(f.isCompletedExceptionally());
195 +        assertTrue(f.isCancelled());
196          assertTrue(f.toString().contains("[Completed exceptionally]"));
197      }
198  
# Line 257 | Line 240 | public class CompletableFutureTest exten
240      {
241          CompletableFuture<Integer> f = new CompletableFuture<>();
242          checkIncomplete(f);
243 <        assertTrue(f.cancel(true));
244 <        assertTrue(f.cancel(true));
243 >        assertTrue(f.cancel(mayInterruptIfRunning));
244 >        assertTrue(f.cancel(mayInterruptIfRunning));
245 >        assertTrue(f.cancel(!mayInterruptIfRunning));
246          checkCancelled(f);
247      }}
248  
# Line 530 | Line 514 | public class CompletableFutureTest exten
514          }
515      }
516  
533
517      class CompletableFutureInc extends CheckedIntegerAction
518          implements Function<Integer, CompletableFuture<Integer>>
519      {
# Line 569 | Line 552 | public class CompletableFutureTest exten
552          }
553      }
554  
555 +    static final boolean defaultExecutorIsCommonPool
556 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
557 +
558      /**
559       * Permits the testing of parallel code for the 3 different
560       * execution modes without copy/pasting all the test methods.
561       */
562      enum ExecutionMode {
563 <        DEFAULT {
563 >        SYNC {
564              public void checkExecutionMode() {
565                  assertFalse(ThreadExecutor.startedCurrentThread());
566                  assertNull(ForkJoinTask.getPool());
# Line 650 | Line 636 | public class CompletableFutureTest exten
636  
637          ASYNC {
638              public void checkExecutionMode() {
639 <                assertSame(ForkJoinPool.commonPool(),
640 <                           ForkJoinTask.getPool());
639 >                assertEquals(defaultExecutorIsCommonPool,
640 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
641              }
642              public CompletableFuture<Void> runAsync(Runnable a) {
643                  return CompletableFuture.runAsync(a);
# Line 875 | Line 861 | public class CompletableFutureTest exten
861          if (!createIncomplete) f.completeExceptionally(ex);
862          final CompletableFuture<Integer> g = f.exceptionally
863              ((Throwable t) -> {
864 <                ExecutionMode.DEFAULT.checkExecutionMode();
864 >                ExecutionMode.SYNC.checkExecutionMode();
865                  threadAssertSame(t, ex);
866                  a.getAndIncrement();
867                  return v1;
# Line 888 | Line 874 | public class CompletableFutureTest exten
874  
875      public void testExceptionally_exceptionalCompletionActionFailed() {
876          for (boolean createIncomplete : new boolean[] { true, false })
891        for (Integer v1 : new Integer[] { 1, null })
877      {
878          final AtomicInteger a = new AtomicInteger(0);
879          final CFException ex1 = new CFException();
# Line 897 | Line 882 | public class CompletableFutureTest exten
882          if (!createIncomplete) f.completeExceptionally(ex1);
883          final CompletableFuture<Integer> g = f.exceptionally
884              ((Throwable t) -> {
885 <                ExecutionMode.DEFAULT.checkExecutionMode();
885 >                ExecutionMode.SYNC.checkExecutionMode();
886                  threadAssertSame(t, ex1);
887                  a.getAndIncrement();
888                  throw ex2;
# Line 942 | Line 927 | public class CompletableFutureTest exten
927      public void testWhenComplete_exceptionalCompletion() {
928          for (ExecutionMode m : ExecutionMode.values())
929          for (boolean createIncomplete : new boolean[] { true, false })
945        for (Integer v1 : new Integer[] { 1, null })
930      {
931          final AtomicInteger a = new AtomicInteger(0);
932          final CFException ex = new CFException();
# Line 1027 | Line 1011 | public class CompletableFutureTest exten
1011      public void testWhenComplete_actionFailedSourceFailed() {
1012          for (boolean createIncomplete : new boolean[] { true, false })
1013          for (ExecutionMode m : ExecutionMode.values())
1030        for (Integer v1 : new Integer[] { 1, null })
1014      {
1015          final AtomicInteger a = new AtomicInteger(0);
1016          final CFException ex1 = new CFException();
# Line 1264 | Line 1247 | public class CompletableFutureTest exten
1247       */
1248      public void testThenRun_normalCompletion() {
1249          for (ExecutionMode m : ExecutionMode.values())
1267        for (boolean createIncomplete : new boolean[] { true, false })
1250          for (Integer v1 : new Integer[] { 1, null })
1251      {
1252          final CompletableFuture<Integer> f = new CompletableFuture<>();
1253 <        final Noop r = new Noop(m);
1254 <        if (!createIncomplete) assertTrue(f.complete(v1));
1273 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1274 <        if (createIncomplete) {
1275 <            checkIncomplete(g);
1276 <            assertTrue(f.complete(v1));
1277 <        }
1253 >        final Noop[] rs = new Noop[6];
1254 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1255  
1256 <        checkCompletedNormally(g, null);
1256 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1257 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1258 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1259 >        checkIncomplete(h0);
1260 >        checkIncomplete(h1);
1261 >        checkIncomplete(h2);
1262 >        assertTrue(f.complete(v1));
1263 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1264 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1265 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1266 >
1267 >        checkCompletedNormally(h0, null);
1268 >        checkCompletedNormally(h1, null);
1269 >        checkCompletedNormally(h2, null);
1270 >        checkCompletedNormally(h3, null);
1271 >        checkCompletedNormally(h4, null);
1272 >        checkCompletedNormally(h5, null);
1273          checkCompletedNormally(f, v1);
1274 <        r.assertInvoked();
1274 >        for (Noop r : rs) r.assertInvoked();
1275      }}
1276  
1277      /**
# Line 1287 | Line 1280 | public class CompletableFutureTest exten
1280       */
1281      public void testThenRun_exceptionalCompletion() {
1282          for (ExecutionMode m : ExecutionMode.values())
1290        for (boolean createIncomplete : new boolean[] { true, false })
1283      {
1284          final CFException ex = new CFException();
1285          final CompletableFuture<Integer> f = new CompletableFuture<>();
1286 <        final Noop r = new Noop(m);
1287 <        if (!createIncomplete) f.completeExceptionally(ex);
1296 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1297 <        if (createIncomplete) {
1298 <            checkIncomplete(g);
1299 <            f.completeExceptionally(ex);
1300 <        }
1286 >        final Noop[] rs = new Noop[6];
1287 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1288  
1289 <        checkCompletedWithWrappedException(g, ex);
1289 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1290 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1291 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1292 >        checkIncomplete(h0);
1293 >        checkIncomplete(h1);
1294 >        checkIncomplete(h2);
1295 >        assertTrue(f.completeExceptionally(ex));
1296 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1297 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1298 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1299 >
1300 >        checkCompletedWithWrappedException(h0, ex);
1301 >        checkCompletedWithWrappedException(h1, ex);
1302 >        checkCompletedWithWrappedException(h2, ex);
1303 >        checkCompletedWithWrappedException(h3, ex);
1304 >        checkCompletedWithWrappedException(h4, ex);
1305 >        checkCompletedWithWrappedException(h5, ex);
1306          checkCompletedExceptionally(f, ex);
1307 <        r.assertNotInvoked();
1307 >        for (Noop r : rs) r.assertNotInvoked();
1308      }}
1309  
1310      /**
# Line 1309 | Line 1312 | public class CompletableFutureTest exten
1312       */
1313      public void testThenRun_sourceCancelled() {
1314          for (ExecutionMode m : ExecutionMode.values())
1312        for (boolean createIncomplete : new boolean[] { true, false })
1315          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1316      {
1317          final CompletableFuture<Integer> f = new CompletableFuture<>();
1318 <        final Noop r = new Noop(m);
1319 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1318 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1319 <        if (createIncomplete) {
1320 <            checkIncomplete(g);
1321 <            assertTrue(f.cancel(mayInterruptIfRunning));
1322 <        }
1318 >        final Noop[] rs = new Noop[6];
1319 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1320  
1321 <        checkCompletedWithWrappedCancellationException(g);
1321 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1322 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1323 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1324 >        checkIncomplete(h0);
1325 >        checkIncomplete(h1);
1326 >        checkIncomplete(h2);
1327 >        assertTrue(f.cancel(mayInterruptIfRunning));
1328 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1329 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1330 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1331 >
1332 >        checkCompletedWithWrappedCancellationException(h0);
1333 >        checkCompletedWithWrappedCancellationException(h1);
1334 >        checkCompletedWithWrappedCancellationException(h2);
1335 >        checkCompletedWithWrappedCancellationException(h3);
1336 >        checkCompletedWithWrappedCancellationException(h4);
1337 >        checkCompletedWithWrappedCancellationException(h5);
1338          checkCancelled(f);
1339 <        r.assertNotInvoked();
1339 >        for (Noop r : rs) r.assertNotInvoked();
1340      }}
1341  
1342      /**
# Line 1331 | Line 1344 | public class CompletableFutureTest exten
1344       */
1345      public void testThenRun_actionFailed() {
1346          for (ExecutionMode m : ExecutionMode.values())
1334        for (boolean createIncomplete : new boolean[] { true, false })
1347          for (Integer v1 : new Integer[] { 1, null })
1348      {
1349          final CompletableFuture<Integer> f = new CompletableFuture<>();
1350 <        final FailingRunnable r = new FailingRunnable(m);
1351 <        if (!createIncomplete) assertTrue(f.complete(v1));
1340 <        final CompletableFuture<Void> g = m.thenRun(f, r);
1341 <        if (createIncomplete) {
1342 <            checkIncomplete(g);
1343 <            assertTrue(f.complete(v1));
1344 <        }
1350 >        final FailingRunnable[] rs = new FailingRunnable[6];
1351 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1352  
1353 <        checkCompletedWithWrappedCFException(g);
1353 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1354 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1355 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1356 >        assertTrue(f.complete(v1));
1357 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1358 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1359 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1360 >
1361 >        checkCompletedWithWrappedCFException(h0);
1362 >        checkCompletedWithWrappedCFException(h1);
1363 >        checkCompletedWithWrappedCFException(h2);
1364 >        checkCompletedWithWrappedCFException(h3);
1365 >        checkCompletedWithWrappedCFException(h4);
1366 >        checkCompletedWithWrappedCFException(h5);
1367          checkCompletedNormally(f, v1);
1368      }}
1369  
# Line 1352 | Line 1372 | public class CompletableFutureTest exten
1372       */
1373      public void testThenApply_normalCompletion() {
1374          for (ExecutionMode m : ExecutionMode.values())
1355        for (boolean createIncomplete : new boolean[] { true, false })
1375          for (Integer v1 : new Integer[] { 1, null })
1376      {
1377          final CompletableFuture<Integer> f = new CompletableFuture<>();
1378 <        final IncFunction r = new IncFunction(m);
1379 <        if (!createIncomplete) assertTrue(f.complete(v1));
1361 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1362 <        if (createIncomplete) {
1363 <            checkIncomplete(g);
1364 <            assertTrue(f.complete(v1));
1365 <        }
1378 >        final IncFunction[] rs = new IncFunction[4];
1379 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1380  
1381 <        checkCompletedNormally(g, inc(v1));
1381 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1382 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1383 >        checkIncomplete(h0);
1384 >        checkIncomplete(h1);
1385 >        assertTrue(f.complete(v1));
1386 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1387 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1388 >
1389 >        checkCompletedNormally(h0, inc(v1));
1390 >        checkCompletedNormally(h1, inc(v1));
1391 >        checkCompletedNormally(h2, inc(v1));
1392 >        checkCompletedNormally(h3, inc(v1));
1393          checkCompletedNormally(f, v1);
1394 <        r.assertValue(inc(v1));
1394 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1395      }}
1396  
1397      /**
# Line 1375 | Line 1400 | public class CompletableFutureTest exten
1400       */
1401      public void testThenApply_exceptionalCompletion() {
1402          for (ExecutionMode m : ExecutionMode.values())
1378        for (boolean createIncomplete : new boolean[] { true, false })
1403      {
1404          final CFException ex = new CFException();
1405          final CompletableFuture<Integer> f = new CompletableFuture<>();
1406 <        final IncFunction r = new IncFunction(m);
1407 <        if (!createIncomplete) f.completeExceptionally(ex);
1384 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1385 <        if (createIncomplete) {
1386 <            checkIncomplete(g);
1387 <            f.completeExceptionally(ex);
1388 <        }
1406 >        final IncFunction[] rs = new IncFunction[4];
1407 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1408  
1409 <        checkCompletedWithWrappedException(g, ex);
1409 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1410 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1411 >        assertTrue(f.completeExceptionally(ex));
1412 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1413 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1414 >
1415 >        checkCompletedWithWrappedException(h0, ex);
1416 >        checkCompletedWithWrappedException(h1, ex);
1417 >        checkCompletedWithWrappedException(h2, ex);
1418 >        checkCompletedWithWrappedException(h3, ex);
1419          checkCompletedExceptionally(f, ex);
1420 <        r.assertNotInvoked();
1420 >        for (IncFunction r : rs) r.assertNotInvoked();
1421      }}
1422  
1423      /**
# Line 1397 | Line 1425 | public class CompletableFutureTest exten
1425       */
1426      public void testThenApply_sourceCancelled() {
1427          for (ExecutionMode m : ExecutionMode.values())
1400        for (boolean createIncomplete : new boolean[] { true, false })
1428          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1429      {
1430          final CompletableFuture<Integer> f = new CompletableFuture<>();
1431 <        final IncFunction r = new IncFunction(m);
1432 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1406 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1407 <        if (createIncomplete) {
1408 <            checkIncomplete(g);
1409 <            assertTrue(f.cancel(mayInterruptIfRunning));
1410 <        }
1431 >        final IncFunction[] rs = new IncFunction[4];
1432 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1433  
1434 <        checkCompletedWithWrappedCancellationException(g);
1434 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1435 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1436 >        assertTrue(f.cancel(mayInterruptIfRunning));
1437 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1438 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1439 >
1440 >        checkCompletedWithWrappedCancellationException(h0);
1441 >        checkCompletedWithWrappedCancellationException(h1);
1442 >        checkCompletedWithWrappedCancellationException(h2);
1443 >        checkCompletedWithWrappedCancellationException(h3);
1444          checkCancelled(f);
1445 <        r.assertNotInvoked();
1445 >        for (IncFunction r : rs) r.assertNotInvoked();
1446      }}
1447  
1448      /**
# Line 1419 | Line 1450 | public class CompletableFutureTest exten
1450       */
1451      public void testThenApply_actionFailed() {
1452          for (ExecutionMode m : ExecutionMode.values())
1422        for (boolean createIncomplete : new boolean[] { true, false })
1453          for (Integer v1 : new Integer[] { 1, null })
1454      {
1455          final CompletableFuture<Integer> f = new CompletableFuture<>();
1456 <        final FailingFunction r = new FailingFunction(m);
1457 <        if (!createIncomplete) assertTrue(f.complete(v1));
1428 <        final CompletableFuture<Integer> g = m.thenApply(f, r);
1429 <        if (createIncomplete) {
1430 <            checkIncomplete(g);
1431 <            assertTrue(f.complete(v1));
1432 <        }
1456 >        final FailingFunction[] rs = new FailingFunction[4];
1457 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1458  
1459 <        checkCompletedWithWrappedCFException(g);
1459 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1460 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1461 >        assertTrue(f.complete(v1));
1462 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1463 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1464 >
1465 >        checkCompletedWithWrappedCFException(h0);
1466 >        checkCompletedWithWrappedCFException(h1);
1467 >        checkCompletedWithWrappedCFException(h2);
1468 >        checkCompletedWithWrappedCFException(h3);
1469          checkCompletedNormally(f, v1);
1470      }}
1471  
# Line 1440 | Line 1474 | public class CompletableFutureTest exten
1474       */
1475      public void testThenAccept_normalCompletion() {
1476          for (ExecutionMode m : ExecutionMode.values())
1443        for (boolean createIncomplete : new boolean[] { true, false })
1477          for (Integer v1 : new Integer[] { 1, null })
1478      {
1479          final CompletableFuture<Integer> f = new CompletableFuture<>();
1480 <        final NoopConsumer r = new NoopConsumer(m);
1481 <        if (!createIncomplete) assertTrue(f.complete(v1));
1449 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1450 <        if (createIncomplete) {
1451 <            checkIncomplete(g);
1452 <            assertTrue(f.complete(v1));
1453 <        }
1480 >        final NoopConsumer[] rs = new NoopConsumer[4];
1481 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1482  
1483 <        checkCompletedNormally(g, null);
1484 <        r.assertValue(v1);
1483 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1484 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1485 >        checkIncomplete(h0);
1486 >        checkIncomplete(h1);
1487 >        assertTrue(f.complete(v1));
1488 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1489 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1490 >
1491 >        checkCompletedNormally(h0, null);
1492 >        checkCompletedNormally(h1, null);
1493 >        checkCompletedNormally(h2, null);
1494 >        checkCompletedNormally(h3, null);
1495          checkCompletedNormally(f, v1);
1496 +        for (NoopConsumer r : rs) r.assertValue(v1);
1497      }}
1498  
1499      /**
# Line 1463 | Line 1502 | public class CompletableFutureTest exten
1502       */
1503      public void testThenAccept_exceptionalCompletion() {
1504          for (ExecutionMode m : ExecutionMode.values())
1466        for (boolean createIncomplete : new boolean[] { true, false })
1505      {
1506          final CFException ex = new CFException();
1507          final CompletableFuture<Integer> f = new CompletableFuture<>();
1508 <        final NoopConsumer r = new NoopConsumer(m);
1509 <        if (!createIncomplete) f.completeExceptionally(ex);
1472 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1473 <        if (createIncomplete) {
1474 <            checkIncomplete(g);
1475 <            f.completeExceptionally(ex);
1476 <        }
1508 >        final NoopConsumer[] rs = new NoopConsumer[4];
1509 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1510  
1511 <        checkCompletedWithWrappedException(g, ex);
1511 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1512 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1513 >        assertTrue(f.completeExceptionally(ex));
1514 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1515 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1516 >
1517 >        checkCompletedWithWrappedException(h0, ex);
1518 >        checkCompletedWithWrappedException(h1, ex);
1519 >        checkCompletedWithWrappedException(h2, ex);
1520 >        checkCompletedWithWrappedException(h3, ex);
1521          checkCompletedExceptionally(f, ex);
1522 <        r.assertNotInvoked();
1522 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1523      }}
1524  
1525      /**
# Line 1485 | Line 1527 | public class CompletableFutureTest exten
1527       */
1528      public void testThenAccept_sourceCancelled() {
1529          for (ExecutionMode m : ExecutionMode.values())
1488        for (boolean createIncomplete : new boolean[] { true, false })
1530          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1531      {
1532          final CompletableFuture<Integer> f = new CompletableFuture<>();
1533 <        final NoopConsumer r = new NoopConsumer(m);
1534 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1494 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1495 <        if (createIncomplete) {
1496 <            checkIncomplete(g);
1497 <            assertTrue(f.cancel(mayInterruptIfRunning));
1498 <        }
1533 >        final NoopConsumer[] rs = new NoopConsumer[4];
1534 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1535  
1536 <        checkCompletedWithWrappedCancellationException(g);
1536 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1537 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1538 >        assertTrue(f.cancel(mayInterruptIfRunning));
1539 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1540 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1541 >
1542 >        checkCompletedWithWrappedCancellationException(h0);
1543 >        checkCompletedWithWrappedCancellationException(h1);
1544 >        checkCompletedWithWrappedCancellationException(h2);
1545 >        checkCompletedWithWrappedCancellationException(h3);
1546          checkCancelled(f);
1547 <        r.assertNotInvoked();
1547 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1548      }}
1549  
1550      /**
# Line 1507 | Line 1552 | public class CompletableFutureTest exten
1552       */
1553      public void testThenAccept_actionFailed() {
1554          for (ExecutionMode m : ExecutionMode.values())
1510        for (boolean createIncomplete : new boolean[] { true, false })
1555          for (Integer v1 : new Integer[] { 1, null })
1556      {
1557          final CompletableFuture<Integer> f = new CompletableFuture<>();
1558 <        final FailingConsumer r = new FailingConsumer(m);
1559 <        if (!createIncomplete) f.complete(v1);
1516 <        final CompletableFuture<Void> g = m.thenAccept(f, r);
1517 <        if (createIncomplete) {
1518 <            checkIncomplete(g);
1519 <            f.complete(v1);
1520 <        }
1558 >        final FailingConsumer[] rs = new FailingConsumer[4];
1559 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1560  
1561 <        checkCompletedWithWrappedCFException(g);
1561 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1562 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1563 >        assertTrue(f.complete(v1));
1564 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1565 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1566 >
1567 >        checkCompletedWithWrappedCFException(h0);
1568 >        checkCompletedWithWrappedCFException(h1);
1569 >        checkCompletedWithWrappedCFException(h2);
1570 >        checkCompletedWithWrappedCFException(h3);
1571          checkCompletedNormally(f, v1);
1572      }}
1573  
# Line 1535 | Line 1583 | public class CompletableFutureTest exten
1583      {
1584          final CompletableFuture<Integer> f = new CompletableFuture<>();
1585          final CompletableFuture<Integer> g = new CompletableFuture<>();
1586 <        final SubtractFunction r1 = new SubtractFunction(m);
1587 <        final SubtractFunction r2 = new SubtractFunction(m);
1540 <        final SubtractFunction r3 = new SubtractFunction(m);
1586 >        final SubtractFunction[] rs = new SubtractFunction[6];
1587 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1588  
1589          final CompletableFuture<Integer> fst =  fFirst ? f : g;
1590          final CompletableFuture<Integer> snd = !fFirst ? f : g;
1591          final Integer w1 =  fFirst ? v1 : v2;
1592          final Integer w2 = !fFirst ? v1 : v2;
1593  
1594 <        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1594 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1595 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1596          assertTrue(fst.complete(w1));
1597 <        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1598 <        checkIncomplete(h1);
1599 <        checkIncomplete(h2);
1600 <        r1.assertNotInvoked();
1601 <        r2.assertNotInvoked();
1597 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1598 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1599 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1600 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1601 >        checkCompletedNormally(h1, subtract(w1, w1));
1602 >        checkCompletedNormally(h3, subtract(w1, w1));
1603 >        rs[1].assertValue(subtract(w1, w1));
1604 >        rs[3].assertValue(subtract(w1, w1));
1605          assertTrue(snd.complete(w2));
1606 <        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1606 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1607  
1608 <        checkCompletedNormally(h1, subtract(v1, v2));
1608 >        checkCompletedNormally(h0, subtract(v1, v2));
1609          checkCompletedNormally(h2, subtract(v1, v2));
1610 <        checkCompletedNormally(h3, subtract(v1, v2));
1611 <        r1.assertValue(subtract(v1, v2));
1612 <        r2.assertValue(subtract(v1, v2));
1613 <        r3.assertValue(subtract(v1, v2));
1610 >        checkCompletedNormally(h4, subtract(v1, v2));
1611 >        rs[0].assertValue(subtract(v1, v2));
1612 >        rs[2].assertValue(subtract(v1, v2));
1613 >        rs[4].assertValue(subtract(v1, v2));
1614 >
1615          checkCompletedNormally(f, v1);
1616          checkCompletedNormally(g, v2);
1617      }}
# Line 2906 | Line 2958 | public class CompletableFutureTest exten
2958          checkCancelled(f);
2959      }}
2960  
2961 +    /**
2962 +     * thenCompose result completes exceptionally if the result of the action does
2963 +     */
2964 +    public void testThenCompose_actionReturnsFailingFuture() {
2965 +        for (ExecutionMode m : ExecutionMode.values())
2966 +        for (int order = 0; order < 6; order++)
2967 +        for (Integer v1 : new Integer[] { 1, null })
2968 +    {
2969 +        final CFException ex = new CFException();
2970 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
2971 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
2972 +        final CompletableFuture<Integer> h;
2973 +        // Test all permutations of orders
2974 +        switch (order) {
2975 +        case 0:
2976 +            assertTrue(f.complete(v1));
2977 +            assertTrue(g.completeExceptionally(ex));
2978 +            h = m.thenCompose(f, (x -> g));
2979 +            break;
2980 +        case 1:
2981 +            assertTrue(f.complete(v1));
2982 +            h = m.thenCompose(f, (x -> g));
2983 +            assertTrue(g.completeExceptionally(ex));
2984 +            break;
2985 +        case 2:
2986 +            assertTrue(g.completeExceptionally(ex));
2987 +            assertTrue(f.complete(v1));
2988 +            h = m.thenCompose(f, (x -> g));
2989 +            break;
2990 +        case 3:
2991 +            assertTrue(g.completeExceptionally(ex));
2992 +            h = m.thenCompose(f, (x -> g));
2993 +            assertTrue(f.complete(v1));
2994 +            break;
2995 +        case 4:
2996 +            h = m.thenCompose(f, (x -> g));
2997 +            assertTrue(f.complete(v1));
2998 +            assertTrue(g.completeExceptionally(ex));
2999 +            break;
3000 +        case 5:
3001 +            h = m.thenCompose(f, (x -> g));
3002 +            assertTrue(f.complete(v1));
3003 +            assertTrue(g.completeExceptionally(ex));
3004 +            break;
3005 +        default: throw new AssertionError();
3006 +        }
3007 +
3008 +        checkCompletedExceptionally(g, ex);
3009 +        checkCompletedWithWrappedException(h, ex);
3010 +        checkCompletedNormally(f, v1);
3011 +    }}
3012 +
3013      // other static methods
3014  
3015      /**
# Line 2922 | Line 3026 | public class CompletableFutureTest exten
3026       * when all components complete normally
3027       */
3028      public void testAllOf_normal() throws Exception {
3029 <        for (int k = 1; k < 20; ++k) {
3029 >        for (int k = 1; k < 10; k++) {
3030              CompletableFuture<Integer>[] fs
3031                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3032 <            for (int i = 0; i < k; ++i)
3032 >            for (int i = 0; i < k; i++)
3033                  fs[i] = new CompletableFuture<>();
3034              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3035 <            for (int i = 0; i < k; ++i) {
3035 >            for (int i = 0; i < k; i++) {
3036                  checkIncomplete(f);
3037                  checkIncomplete(CompletableFuture.allOf(fs));
3038                  fs[i].complete(one);
# Line 2939 | Line 3043 | public class CompletableFutureTest exten
3043      }
3044  
3045      public void testAllOf_backwards() throws Exception {
3046 <        for (int k = 1; k < 20; ++k) {
3046 >        for (int k = 1; k < 10; k++) {
3047              CompletableFuture<Integer>[] fs
3048                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3049 <            for (int i = 0; i < k; ++i)
3049 >            for (int i = 0; i < k; i++)
3050                  fs[i] = new CompletableFuture<>();
3051              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3052              for (int i = k - 1; i >= 0; i--) {
# Line 2955 | Line 3059 | public class CompletableFutureTest exten
3059          }
3060      }
3061  
3062 +    public void testAllOf_exceptional() throws Exception {
3063 +        for (int k = 1; k < 10; k++) {
3064 +            CompletableFuture<Integer>[] fs
3065 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3066 +            CFException ex = new CFException();
3067 +            for (int i = 0; i < k; i++)
3068 +                fs[i] = new CompletableFuture<>();
3069 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3070 +            for (int i = 0; i < k; i++) {
3071 +                checkIncomplete(f);
3072 +                checkIncomplete(CompletableFuture.allOf(fs));
3073 +                if (i != k / 2) {
3074 +                    fs[i].complete(i);
3075 +                    checkCompletedNormally(fs[i], i);
3076 +                } else {
3077 +                    fs[i].completeExceptionally(ex);
3078 +                    checkCompletedExceptionally(fs[i], ex);
3079 +                }
3080 +            }
3081 +            checkCompletedWithWrappedException(f, ex);
3082 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3083 +        }
3084 +    }
3085 +
3086      /**
3087       * anyOf(no component futures) returns an incomplete future
3088       */
3089      public void testAnyOf_empty() throws Exception {
3090 +        for (Integer v1 : new Integer[] { 1, null })
3091 +    {
3092          CompletableFuture<Object> f = CompletableFuture.anyOf();
3093          checkIncomplete(f);
3094 <    }
3094 >
3095 >        f.complete(v1);
3096 >        checkCompletedNormally(f, v1);
3097 >    }}
3098  
3099      /**
3100       * anyOf returns a future completed normally with a value when
3101       * a component future does
3102       */
3103      public void testAnyOf_normal() throws Exception {
3104 <        for (int k = 0; k < 10; ++k) {
3104 >        for (int k = 0; k < 10; k++) {
3105              CompletableFuture[] fs = new CompletableFuture[k];
3106 <            for (int i = 0; i < k; ++i)
3106 >            for (int i = 0; i < k; i++)
3107                  fs[i] = new CompletableFuture<>();
3108              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3109              checkIncomplete(f);
3110 <            for (int i = 0; i < k; ++i) {
3111 <                fs[i].complete(one);
3112 <                checkCompletedNormally(f, one);
3113 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3110 >            for (int i = 0; i < k; i++) {
3111 >                fs[i].complete(i);
3112 >                checkCompletedNormally(f, 0);
3113 >                int x = (int) CompletableFuture.anyOf(fs).join();
3114 >                assertTrue(0 <= x && x <= i);
3115 >            }
3116 >        }
3117 >    }
3118 >    public void testAnyOf_normal_backwards() throws Exception {
3119 >        for (int k = 0; k < 10; k++) {
3120 >            CompletableFuture[] fs = new CompletableFuture[k];
3121 >            for (int i = 0; i < k; i++)
3122 >                fs[i] = new CompletableFuture<>();
3123 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3124 >            checkIncomplete(f);
3125 >            for (int i = k - 1; i >= 0; i--) {
3126 >                fs[i].complete(i);
3127 >                checkCompletedNormally(f, k - 1);
3128 >                int x = (int) CompletableFuture.anyOf(fs).join();
3129 >                assertTrue(i <= x && x <= k - 1);
3130              }
3131          }
3132      }
# Line 2986 | Line 3135 | public class CompletableFutureTest exten
3135       * anyOf result completes exceptionally when any component does.
3136       */
3137      public void testAnyOf_exceptional() throws Exception {
3138 <        for (int k = 0; k < 10; ++k) {
3138 >        for (int k = 0; k < 10; k++) {
3139              CompletableFuture[] fs = new CompletableFuture[k];
3140 <            for (int i = 0; i < k; ++i)
3140 >            CFException[] exs = new CFException[k];
3141 >            for (int i = 0; i < k; i++) {
3142                  fs[i] = new CompletableFuture<>();
3143 +                exs[i] = new CFException();
3144 +            }
3145              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3146              checkIncomplete(f);
3147 <            for (int i = 0; i < k; ++i) {
3148 <                fs[i].completeExceptionally(new CFException());
3149 <                checkCompletedWithWrappedCFException(f);
3147 >            for (int i = 0; i < k; i++) {
3148 >                fs[i].completeExceptionally(exs[i]);
3149 >                checkCompletedWithWrappedException(f, exs[0]);
3150 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3151 >            }
3152 >        }
3153 >    }
3154 >
3155 >    public void testAnyOf_exceptional_backwards() throws Exception {
3156 >        for (int k = 0; k < 10; k++) {
3157 >            CompletableFuture[] fs = new CompletableFuture[k];
3158 >            CFException[] exs = new CFException[k];
3159 >            for (int i = 0; i < k; i++) {
3160 >                fs[i] = new CompletableFuture<>();
3161 >                exs[i] = new CFException();
3162 >            }
3163 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3164 >            checkIncomplete(f);
3165 >            for (int i = k - 1; i >= 0; i--) {
3166 >                fs[i].completeExceptionally(exs[i]);
3167 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3168                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3169              }
3170          }
# Line 3007 | Line 3177 | public class CompletableFutureTest exten
3177          CompletableFuture<Integer> f = new CompletableFuture<>();
3178          CompletableFuture<Integer> g = new CompletableFuture<>();
3179          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3010        CompletableFuture<?> h;
3180          ThreadExecutor exec = new ThreadExecutor();
3181  
3182          Runnable[] throwingActions = {
3183              () -> CompletableFuture.supplyAsync(null),
3184              () -> CompletableFuture.supplyAsync(null, exec),
3185 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3185 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3186  
3187              () -> CompletableFuture.runAsync(null),
3188              () -> CompletableFuture.runAsync(null, exec),
# Line 3104 | Line 3273 | public class CompletableFutureTest exten
3273              () -> CompletableFuture.anyOf(null, f),
3274  
3275              () -> f.obtrudeException(null),
3276 +
3277 +            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3278 +            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3279 +            () -> CompletableFuture.delayedExecutor(1L, null),
3280 +
3281 +            () -> f.orTimeout(1L, null),
3282 +            () -> f.completeOnTimeout(42, 1L, null),
3283 +
3284 +            () -> CompletableFuture.failedFuture(null),
3285 +            () -> CompletableFuture.failedStage(null),
3286          };
3287  
3288          assertThrows(NullPointerException.class, throwingActions);
# Line 3118 | Line 3297 | public class CompletableFutureTest exten
3297          assertSame(f, f.toCompletableFuture());
3298      }
3299  
3300 < //     public void testRunAfterEither_resultDeterminedAtTimeOfCreation() {
3301 < //         for (ExecutionMode m : ExecutionMode.values())
3302 < //         for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3303 < //         for (Integer v1 : new Integer[] { 1, null })
3304 < //     {
3305 < //         final CompletableFuture<Integer> f = new CompletableFuture<>();
3306 < //         final CompletableFuture<Integer> g = new CompletableFuture<>();
3307 < //         final Noop[] rs = new Noop[2];
3308 < //         for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
3309 < //         f.complete(v1);
3310 < //         final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
3311 < //         final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
3312 < //         assertTrue(g.cancel(mayInterruptIfRunning));
3313 < //         checkCompletedNormally(h0, null);
3314 < //         checkCompletedNormally(h1, null);
3315 < //         for (Noop r : rs) r.assertInvoked();
3316 < //     }}
3300 >    // jdk9
3301 >
3302 >    /**
3303 >     * newIncompleteFuture returns an incomplete CompletableFuture
3304 >     */
3305 >    public void testNewIncompleteFuture() {
3306 >        for (Integer v1 : new Integer[] { 1, null })
3307 >    {
3308 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3309 >        CompletableFuture<Integer> g = f.newIncompleteFuture();
3310 >        checkIncomplete(f);
3311 >        checkIncomplete(g);
3312 >        f.complete(v1);
3313 >        checkCompletedNormally(f, v1);
3314 >        checkIncomplete(g);
3315 >        g.complete(v1);
3316 >        checkCompletedNormally(g, v1);
3317 >        assertSame(g.getClass(), CompletableFuture.class);
3318 >    }}
3319 >
3320 >    /**
3321 >     * completedStage returns a completed CompletionStage
3322 >     */
3323 >    public void testCompletedStage() {
3324 >        AtomicInteger x = new AtomicInteger(0);
3325 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3326 >        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3327 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3328 >        assertEquals(x.get(), 1);
3329 >        assertNull(r.get());
3330 >    }
3331 >
3332 >    /**
3333 >     * defaultExecutor by default returns the commonPool if
3334 >     * it supports more than one thread.
3335 >     */
3336 >    public void testDefaultExecutor() {
3337 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3338 >        Executor e = f.defaultExecutor();
3339 >        Executor c = ForkJoinPool.commonPool();
3340 >        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3341 >            assertSame(e, c);
3342 >        else
3343 >            assertNotSame(e, c);
3344 >    }
3345 >
3346 >    /**
3347 >     * failedFuture returns a CompletableFuture completed
3348 >     * exceptionally with the given Exception
3349 >     */
3350 >    public void testFailedFuture() {
3351 >        CFException ex = new CFException();
3352 >        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3353 >        checkCompletedExceptionally(f, ex);
3354 >    }
3355 >
3356 >    /**
3357 >     * failedFuture(null) throws NPE
3358 >     */
3359 >    public void testFailedFuture_null() {
3360 >        try {
3361 >            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3362 >            shouldThrow();
3363 >        } catch (NullPointerException success) {}
3364 >    }
3365 >
3366 >    /**
3367 >     * copy returns a CompletableFuture that is completed normally,
3368 >     * with the same value, when source is.
3369 >     */
3370 >    public void testCopy() {
3371 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3372 >        CompletableFuture<Integer> g = f.copy();
3373 >        checkIncomplete(f);
3374 >        checkIncomplete(g);
3375 >        f.complete(1);
3376 >        checkCompletedNormally(f, 1);
3377 >        checkCompletedNormally(g, 1);
3378 >    }
3379 >
3380 >    /**
3381 >     * copy returns a CompletableFuture that is completed exceptionally
3382 >     * when source is.
3383 >     */
3384 >    public void testCopy2() {
3385 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3386 >        CompletableFuture<Integer> g = f.copy();
3387 >        checkIncomplete(f);
3388 >        checkIncomplete(g);
3389 >        CFException ex = new CFException();
3390 >        f.completeExceptionally(ex);
3391 >        checkCompletedExceptionally(f, ex);
3392 >        checkCompletedWithWrappedException(g, ex);
3393 >    }
3394 >
3395 >    /**
3396 >     * minimalCompletionStage returns a CompletableFuture that is
3397 >     * completed normally, with the same value, when source is.
3398 >     */
3399 >    public void testMinimalCompletionStage() {
3400 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3401 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3402 >        AtomicInteger x = new AtomicInteger(0);
3403 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3404 >        checkIncomplete(f);
3405 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3406 >        f.complete(1);
3407 >        checkCompletedNormally(f, 1);
3408 >        assertEquals(x.get(), 1);
3409 >        assertNull(r.get());
3410 >    }
3411 >
3412 >    /**
3413 >     * minimalCompletionStage returns a CompletableFuture that is
3414 >     * completed exceptionally when source is.
3415 >     */
3416 >    public void testMinimalCompletionStage2() {
3417 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3418 >        CompletionStage<Integer> g = f.minimalCompletionStage();
3419 >        AtomicInteger x = new AtomicInteger(0);
3420 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3421 >        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3422 >        checkIncomplete(f);
3423 >        CFException ex = new CFException();
3424 >        f.completeExceptionally(ex);
3425 >        checkCompletedExceptionally(f, ex);
3426 >        assertEquals(x.get(), 0);
3427 >        assertEquals(r.get().getCause(), ex);
3428 >    }
3429 >
3430 >    /**
3431 >     * failedStage returns a CompletionStage completed
3432 >     * exceptionally with the given Exception
3433 >     */
3434 >    public void testFailedStage() {
3435 >        CFException ex = new CFException();
3436 >        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3437 >        AtomicInteger x = new AtomicInteger(0);
3438 >        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3439 >        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3440 >        assertEquals(x.get(), 0);
3441 >        assertEquals(r.get(), ex);
3442 >    }
3443 >
3444 >    /**
3445 >     * completeAsync completes with value of given supplier
3446 >     */
3447 >    public void testCompleteAsync() {
3448 >        for (Integer v1 : new Integer[] { 1, null })
3449 >    {
3450 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3451 >        f.completeAsync(() -> v1);
3452 >        f.join();
3453 >        checkCompletedNormally(f, v1);
3454 >    }}
3455 >
3456 >    /**
3457 >     * completeAsync completes exceptionally if given supplier throws
3458 >     */
3459 >    public void testCompleteAsync2() {
3460 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3461 >        CFException ex = new CFException();
3462 >        f.completeAsync(() -> {if (true) throw ex; return 1;});
3463 >        try {
3464 >            f.join();
3465 >            shouldThrow();
3466 >        } catch (CompletionException success) {}
3467 >        checkCompletedWithWrappedException(f, ex);
3468 >    }
3469 >
3470 >    /**
3471 >     * completeAsync with given executor completes with value of given supplier
3472 >     */
3473 >    public void testCompleteAsync3() {
3474 >        for (Integer v1 : new Integer[] { 1, null })
3475 >    {
3476 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3477 >        ThreadExecutor executor = new ThreadExecutor();
3478 >        f.completeAsync(() -> v1, executor);
3479 >        assertSame(v1, f.join());
3480 >        checkCompletedNormally(f, v1);
3481 >        assertEquals(1, executor.count.get());
3482 >    }}
3483 >
3484 >    /**
3485 >     * completeAsync with given executor completes exceptionally if
3486 >     * given supplier throws
3487 >     */
3488 >    public void testCompleteAsync4() {
3489 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3490 >        CFException ex = new CFException();
3491 >        ThreadExecutor executor = new ThreadExecutor();
3492 >        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3493 >        try {
3494 >            f.join();
3495 >            shouldThrow();
3496 >        } catch (CompletionException success) {}
3497 >        checkCompletedWithWrappedException(f, ex);
3498 >        assertEquals(1, executor.count.get());
3499 >    }
3500 >
3501 >    /**
3502 >     * orTimeout completes with TimeoutException if not complete
3503 >     */
3504 >    public void testOrTimeout_timesOut() {
3505 >        long timeoutMillis = timeoutMillis();
3506 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3507 >        long startTime = System.nanoTime();
3508 >        f.orTimeout(timeoutMillis, MILLISECONDS);
3509 >        checkCompletedWithTimeoutException(f);
3510 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3511 >    }
3512 >
3513 >    /**
3514 >     * orTimeout completes normally if completed before timeout
3515 >     */
3516 >    public void testOrTimeout_completed() {
3517 >        for (Integer v1 : new Integer[] { 1, null })
3518 >    {
3519 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3520 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3521 >        long startTime = System.nanoTime();
3522 >        f.complete(v1);
3523 >        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3524 >        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3525 >        g.complete(v1);
3526 >        checkCompletedNormally(f, v1);
3527 >        checkCompletedNormally(g, v1);
3528 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3529 >    }}
3530 >
3531 >    /**
3532 >     * completeOnTimeout completes with given value if not complete
3533 >     */
3534 >    public void testCompleteOnTimeout_timesOut() {
3535 >        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3536 >                       () -> testCompleteOnTimeout_timesOut(null));
3537 >    }
3538 >
3539 >    public void testCompleteOnTimeout_timesOut(Integer v) {
3540 >        long timeoutMillis = timeoutMillis();
3541 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3542 >        long startTime = System.nanoTime();
3543 >        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3544 >        assertSame(v, f.join());
3545 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3546 >        f.complete(99);         // should have no effect
3547 >        checkCompletedNormally(f, v);
3548 >    }
3549 >
3550 >    /**
3551 >     * completeOnTimeout has no effect if completed within timeout
3552 >     */
3553 >    public void testCompleteOnTimeout_completed() {
3554 >        for (Integer v1 : new Integer[] { 1, null })
3555 >    {
3556 >        CompletableFuture<Integer> f = new CompletableFuture<>();
3557 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3558 >        long startTime = System.nanoTime();
3559 >        f.complete(v1);
3560 >        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3561 >        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3562 >        g.complete(v1);
3563 >        checkCompletedNormally(f, v1);
3564 >        checkCompletedNormally(g, v1);
3565 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3566 >    }}
3567 >
3568 >    /**
3569 >     * delayedExecutor returns an executor that delays submission
3570 >     */
3571 >    public void testDelayedExecutor() {
3572 >        testInParallel(() -> testDelayedExecutor(null, null),
3573 >                       () -> testDelayedExecutor(null, 1),
3574 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3575 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3576 >    }
3577 >
3578 >    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3579 >        long timeoutMillis = timeoutMillis();
3580 >        // Use an "unreasonably long" long timeout to catch lingering threads
3581 >        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3582 >        final Executor delayer, longDelayer;
3583 >        if (executor == null) {
3584 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3585 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3586 >        } else {
3587 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3588 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3589 >        }
3590 >        long startTime = System.nanoTime();
3591 >        CompletableFuture<Integer> f =
3592 >            CompletableFuture.supplyAsync(() -> v, delayer);
3593 >        CompletableFuture<Integer> g =
3594 >            CompletableFuture.supplyAsync(() -> v, longDelayer);
3595 >
3596 >        assertNull(g.getNow(null));
3597 >
3598 >        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3599 >        long millisElapsed = millisElapsedSince(startTime);
3600 >        assertTrue(millisElapsed >= timeoutMillis);
3601 >        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3602 >
3603 >        checkCompletedNormally(f, v);
3604 >
3605 >        checkIncomplete(g);
3606 >        assertTrue(g.cancel(true));
3607 >    }
3608 >
3609 >    //--- tests of implementation details; not part of official tck ---
3610 >
3611 >    Object resultOf(CompletableFuture<?> f) {
3612 >        try {
3613 >            java.lang.reflect.Field resultField
3614 >                = CompletableFuture.class.getDeclaredField("result");
3615 >            resultField.setAccessible(true);
3616 >            return resultField.get(f);
3617 >        } catch (Throwable t) { throw new AssertionError(t); }
3618 >    }
3619 >
3620 >    public void testExceptionPropagationReusesResultObject() {
3621 >        if (!testImplementationDetails) return;
3622 >        for (ExecutionMode m : ExecutionMode.values())
3623 >    {
3624 >        final CFException ex = new CFException();
3625 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3626 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3627 >
3628 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3629 >            = new ArrayList<>();
3630 >
3631 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3632 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3633 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3634 >
3635 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3636 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3637 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3638 >
3639 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3640 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3641 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3642 >
3643 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3644 >
3645 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3646 >
3647 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3648 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3649 >
3650 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3651 >                 fun : funs) {
3652 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3653 >            f.completeExceptionally(ex);
3654 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3655 >            checkCompletedWithWrappedException(src, ex);
3656 >            CompletableFuture<?> dep = fun.apply(src);
3657 >            checkCompletedWithWrappedException(dep, ex);
3658 >            assertSame(resultOf(src), resultOf(dep));
3659 >        }
3660 >
3661 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3662 >                 fun : funs) {
3663 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3664 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3665 >            CompletableFuture<?> dep = fun.apply(src);
3666 >            f.completeExceptionally(ex);
3667 >            checkCompletedWithWrappedException(src, ex);
3668 >            checkCompletedWithWrappedException(dep, ex);
3669 >            assertSame(resultOf(src), resultOf(dep));
3670 >        }
3671 >
3672 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3673 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3674 >                 fun : funs) {
3675 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3676 >            f.cancel(mayInterruptIfRunning);
3677 >            checkCancelled(f);
3678 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3679 >            checkCompletedWithWrappedCancellationException(src);
3680 >            CompletableFuture<?> dep = fun.apply(src);
3681 >            checkCompletedWithWrappedCancellationException(dep);
3682 >            assertSame(resultOf(src), resultOf(dep));
3683 >        }
3684 >
3685 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3686 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3687 >                 fun : funs) {
3688 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3689 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3690 >            CompletableFuture<?> dep = fun.apply(src);
3691 >            f.cancel(mayInterruptIfRunning);
3692 >            checkCancelled(f);
3693 >            checkCompletedWithWrappedCancellationException(src);
3694 >            checkCompletedWithWrappedCancellationException(dep);
3695 >            assertSame(resultOf(src), resultOf(dep));
3696 >        }
3697 >    }}
3698 >
3699 >    /**
3700 >     * Minimal completion stages throw UOE for all non-CompletionStage methods
3701 >     */
3702 >    public void testMinimalCompletionStage_minimality() {
3703 >        if (!testImplementationDetails) return;
3704 >        Function<Method, String> toSignature =
3705 >            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3706 >        List<Method> minimalMethods =
3707 >            Stream.of(Object.class, CompletionStage.class)
3708 >            .map((klazz) -> Stream.of(klazz.getMethods()))
3709 >            .reduce(Stream::concat)
3710 >            .orElseGet(Stream::empty)
3711 >            .filter((method) -> (method.getModifiers() & Modifier.STATIC) == 0)
3712 >            .collect(Collectors.toList());
3713 >        // Methods from CompletableFuture permitted NOT to throw UOE
3714 >        String[] signatureWhitelist = {
3715 >            "newIncompleteFuture[]",
3716 >            "defaultExecutor[]",
3717 >            "minimalCompletionStage[]",
3718 >            "copy[]",
3719 >        };
3720 >        Set<String> permittedMethodSignatures =
3721 >            Stream.concat(minimalMethods.stream().map(toSignature),
3722 >                          Stream.of(signatureWhitelist))
3723 >            .collect(Collectors.toSet());
3724 >        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3725 >            .filter((method) -> (method.getModifiers() & Modifier.STATIC) == 0)
3726 >            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3727 >            .collect(Collectors.toList());
3728 >
3729 >        CompletionStage<Integer> minimalStage =
3730 >            new CompletableFuture<Integer>().minimalCompletionStage();
3731 >
3732 >        List<Method> bugs = new ArrayList<>();
3733 >        for (Method method : allMethods) {
3734 >            Class<?>[] parameterTypes = method.getParameterTypes();
3735 >            Object[] args = new Object[parameterTypes.length];
3736 >            // Manufacture boxed primitives for primitive params
3737 >            for (int i = 0; i < args.length; i++) {
3738 >                Class<?> type = parameterTypes[i];
3739 >                if (parameterTypes[i] == boolean.class)
3740 >                    args[i] = false;
3741 >                else if (parameterTypes[i] == int.class)
3742 >                    args[i] = 0;
3743 >                else if (parameterTypes[i] == long.class)
3744 >                    args[i] = 0L;
3745 >            }
3746 >            try {
3747 >                method.invoke(minimalStage, args);
3748 >                bugs.add(method);
3749 >            }
3750 >            catch (java.lang.reflect.InvocationTargetException expected) {
3751 >                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3752 >                    bugs.add(method);
3753 >                    // expected.getCause().printStackTrace();
3754 >                }
3755 >            }
3756 >            catch (ReflectiveOperationException bad) { throw new Error(bad); }
3757 >        }
3758 >        if (!bugs.isEmpty())
3759 >            throw new Error("Methods did not throw UOE: " + bugs.toString());
3760 >    }
3761 >
3762 > //     static <U> U join(CompletionStage<U> stage) {
3763 > //         CompletableFuture<U> f = new CompletableFuture<>();
3764 > //         stage.whenComplete((v, ex) -> {
3765 > //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3766 > //         });
3767 > //         return f.join();
3768 > //     }
3769 >
3770 > //     static <U> boolean isDone(CompletionStage<U> stage) {
3771 > //         CompletableFuture<U> f = new CompletableFuture<>();
3772 > //         stage.whenComplete((v, ex) -> {
3773 > //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3774 > //         });
3775 > //         return f.isDone();
3776 > //     }
3777 >
3778 > //     static <U> U join2(CompletionStage<U> stage) {
3779 > //         return stage.toCompletableFuture().copy().join();
3780 > //     }
3781 >
3782 > //     static <U> boolean isDone2(CompletionStage<U> stage) {
3783 > //         return stage.toCompletableFuture().copy().isDone();
3784 > //     }
3785  
3786   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines