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.89 by jsr166, Mon Jun 16 21:34:49 2014 UTC vs.
Revision 1.112 by jsr166, Fri Sep 4 19:46:22 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 76 | Line 78 | public class CompletableFutureTest exten
78      }
79  
80      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
81 +        long startTime = System.nanoTime();
82 +        long timeoutMillis = LONG_DELAY_MS;
83          try {
84 <            f.get(LONG_DELAY_MS, MILLISECONDS);
84 >            f.get(timeoutMillis, MILLISECONDS);
85              shouldThrow();
86          } catch (ExecutionException success) {
87              assertTrue(success.getCause() instanceof CFException);
88          } catch (Throwable fail) { threadUnexpectedException(fail); }
89 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
90 +
91          try {
92              f.join();
93              shouldThrow();
# Line 107 | Line 113 | public class CompletableFutureTest exten
113  
114      <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
115                                                        Throwable ex) {
116 +        long startTime = System.nanoTime();
117 +        long timeoutMillis = LONG_DELAY_MS;
118          try {
119 <            f.get(LONG_DELAY_MS, MILLISECONDS);
119 >            f.get(timeoutMillis, MILLISECONDS);
120              shouldThrow();
121          } catch (ExecutionException success) {
122              assertSame(ex, success.getCause());
123          } catch (Throwable fail) { threadUnexpectedException(fail); }
124 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
125 +
126          try {
127              f.join();
128              shouldThrow();
# Line 137 | Line 147 | public class CompletableFutureTest exten
147          assertTrue(f.toString().contains("[Completed exceptionally]"));
148      }
149  
150 +    <U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) {
151 +        long startTime = System.nanoTime();
152 +        long timeoutMillis = LONG_DELAY_MS;
153 +        try {
154 +            f.get(timeoutMillis, MILLISECONDS);
155 +            shouldThrow();
156 +        } catch (ExecutionException ex) {
157 +            assertTrue(ex.getCause() instanceof TimeoutException);
158 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
159 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
160 +
161 +        try {
162 +            f.join();
163 +            shouldThrow();
164 +        } catch (Throwable ex) {
165 +            assertTrue(ex.getCause() instanceof TimeoutException);
166 +        }
167 +
168 +        try {
169 +            f.getNow(null);
170 +            shouldThrow();
171 +        } catch (Throwable ex) {
172 +            assertTrue(ex.getCause() instanceof TimeoutException);
173 +        }
174 +
175 +        try {
176 +            f.get();
177 +            shouldThrow();
178 +        } catch (ExecutionException ex) {
179 +            assertTrue(ex.getCause() instanceof TimeoutException);
180 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
181 +
182 +        assertTrue(f.isDone());
183 +        assertFalse(f.isCancelled());
184 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
185 +    }
186 +
187      <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
188                                                  Throwable ex) {
189          checkCompletedExceptionallyWithRootCause(f, ex);
# Line 158 | Line 205 | public class CompletableFutureTest exten
205      }
206  
207      void checkCancelled(CompletableFuture<?> f) {
208 +        long startTime = System.nanoTime();
209 +        long timeoutMillis = LONG_DELAY_MS;
210          try {
211 <            f.get(LONG_DELAY_MS, MILLISECONDS);
211 >            f.get(timeoutMillis, MILLISECONDS);
212              shouldThrow();
213          } catch (CancellationException success) {
214          } catch (Throwable fail) { threadUnexpectedException(fail); }
215 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
216 +
217          try {
218              f.join();
219              shouldThrow();
# Line 183 | Line 234 | public class CompletableFutureTest exten
234      }
235  
236      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
237 +        long startTime = System.nanoTime();
238 +        long timeoutMillis = LONG_DELAY_MS;
239          try {
240 <            f.get(LONG_DELAY_MS, MILLISECONDS);
240 >            f.get(timeoutMillis, MILLISECONDS);
241              shouldThrow();
242          } catch (ExecutionException success) {
243              assertTrue(success.getCause() instanceof CancellationException);
244          } catch (Throwable fail) { threadUnexpectedException(fail); }
245 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
246 +
247          try {
248              f.join();
249              shouldThrow();
# Line 257 | Line 312 | public class CompletableFutureTest exten
312      {
313          CompletableFuture<Integer> f = new CompletableFuture<>();
314          checkIncomplete(f);
315 <        assertTrue(f.cancel(true));
316 <        assertTrue(f.cancel(true));
315 >        assertTrue(f.cancel(mayInterruptIfRunning));
316 >        assertTrue(f.cancel(mayInterruptIfRunning));
317 >        assertTrue(f.cancel(!mayInterruptIfRunning));
318          checkCancelled(f);
319      }}
320  
# Line 530 | Line 586 | public class CompletableFutureTest exten
586          }
587      }
588  
533
589      class CompletableFutureInc extends CheckedIntegerAction
590          implements Function<Integer, CompletableFuture<Integer>>
591      {
# Line 569 | Line 624 | public class CompletableFutureTest exten
624          }
625      }
626  
627 +    static final boolean defaultExecutorIsCommonPool
628 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
629 +
630      /**
631       * Permits the testing of parallel code for the 3 different
632       * execution modes without copy/pasting all the test methods.
633       */
634      enum ExecutionMode {
635 <        DEFAULT {
635 >        SYNC {
636              public void checkExecutionMode() {
637                  assertFalse(ThreadExecutor.startedCurrentThread());
638                  assertNull(ForkJoinTask.getPool());
# Line 650 | Line 708 | public class CompletableFutureTest exten
708  
709          ASYNC {
710              public void checkExecutionMode() {
711 <                assertSame(ForkJoinPool.commonPool(),
712 <                           ForkJoinTask.getPool());
711 >                assertEquals(defaultExecutorIsCommonPool,
712 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
713              }
714              public CompletableFuture<Void> runAsync(Runnable a) {
715                  return CompletableFuture.runAsync(a);
# Line 875 | Line 933 | public class CompletableFutureTest exten
933          if (!createIncomplete) f.completeExceptionally(ex);
934          final CompletableFuture<Integer> g = f.exceptionally
935              ((Throwable t) -> {
936 <                ExecutionMode.DEFAULT.checkExecutionMode();
936 >                ExecutionMode.SYNC.checkExecutionMode();
937                  threadAssertSame(t, ex);
938                  a.getAndIncrement();
939                  return v1;
# Line 888 | Line 946 | public class CompletableFutureTest exten
946  
947      public void testExceptionally_exceptionalCompletionActionFailed() {
948          for (boolean createIncomplete : new boolean[] { true, false })
891        for (Integer v1 : new Integer[] { 1, null })
949      {
950          final AtomicInteger a = new AtomicInteger(0);
951          final CFException ex1 = new CFException();
# Line 897 | Line 954 | public class CompletableFutureTest exten
954          if (!createIncomplete) f.completeExceptionally(ex1);
955          final CompletableFuture<Integer> g = f.exceptionally
956              ((Throwable t) -> {
957 <                ExecutionMode.DEFAULT.checkExecutionMode();
957 >                ExecutionMode.SYNC.checkExecutionMode();
958                  threadAssertSame(t, ex1);
959                  a.getAndIncrement();
960                  throw ex2;
# Line 942 | Line 999 | public class CompletableFutureTest exten
999      public void testWhenComplete_exceptionalCompletion() {
1000          for (ExecutionMode m : ExecutionMode.values())
1001          for (boolean createIncomplete : new boolean[] { true, false })
945        for (Integer v1 : new Integer[] { 1, null })
1002      {
1003          final AtomicInteger a = new AtomicInteger(0);
1004          final CFException ex = new CFException();
# Line 1027 | Line 1083 | public class CompletableFutureTest exten
1083      public void testWhenComplete_actionFailedSourceFailed() {
1084          for (boolean createIncomplete : new boolean[] { true, false })
1085          for (ExecutionMode m : ExecutionMode.values())
1030        for (Integer v1 : new Integer[] { 1, null })
1086      {
1087          final AtomicInteger a = new AtomicInteger(0);
1088          final CFException ex1 = new CFException();
# Line 1264 | Line 1319 | public class CompletableFutureTest exten
1319       */
1320      public void testThenRun_normalCompletion() {
1321          for (ExecutionMode m : ExecutionMode.values())
1267        for (boolean createIncomplete : new boolean[] { true, false })
1322          for (Integer v1 : new Integer[] { 1, null })
1323      {
1324          final CompletableFuture<Integer> f = new CompletableFuture<>();
1325 <        final Noop r = new Noop(m);
1326 <        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 <        }
1325 >        final Noop[] rs = new Noop[6];
1326 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1327  
1328 <        checkCompletedNormally(g, null);
1328 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1329 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1330 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1331 >        checkIncomplete(h0);
1332 >        checkIncomplete(h1);
1333 >        checkIncomplete(h2);
1334 >        assertTrue(f.complete(v1));
1335 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1336 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1337 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1338 >
1339 >        checkCompletedNormally(h0, null);
1340 >        checkCompletedNormally(h1, null);
1341 >        checkCompletedNormally(h2, null);
1342 >        checkCompletedNormally(h3, null);
1343 >        checkCompletedNormally(h4, null);
1344 >        checkCompletedNormally(h5, null);
1345          checkCompletedNormally(f, v1);
1346 <        r.assertInvoked();
1346 >        for (Noop r : rs) r.assertInvoked();
1347      }}
1348  
1349      /**
# Line 1287 | Line 1352 | public class CompletableFutureTest exten
1352       */
1353      public void testThenRun_exceptionalCompletion() {
1354          for (ExecutionMode m : ExecutionMode.values())
1290        for (boolean createIncomplete : new boolean[] { true, false })
1355      {
1356          final CFException ex = new CFException();
1357          final CompletableFuture<Integer> f = new CompletableFuture<>();
1358 <        final Noop r = new Noop(m);
1359 <        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 <        }
1358 >        final Noop[] rs = new Noop[6];
1359 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1360  
1361 <        checkCompletedWithWrappedException(g, ex);
1361 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1362 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1363 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1364 >        checkIncomplete(h0);
1365 >        checkIncomplete(h1);
1366 >        checkIncomplete(h2);
1367 >        assertTrue(f.completeExceptionally(ex));
1368 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1369 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1370 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1371 >
1372 >        checkCompletedWithWrappedException(h0, ex);
1373 >        checkCompletedWithWrappedException(h1, ex);
1374 >        checkCompletedWithWrappedException(h2, ex);
1375 >        checkCompletedWithWrappedException(h3, ex);
1376 >        checkCompletedWithWrappedException(h4, ex);
1377 >        checkCompletedWithWrappedException(h5, ex);
1378          checkCompletedExceptionally(f, ex);
1379 <        r.assertNotInvoked();
1379 >        for (Noop r : rs) r.assertNotInvoked();
1380      }}
1381  
1382      /**
# Line 1309 | Line 1384 | public class CompletableFutureTest exten
1384       */
1385      public void testThenRun_sourceCancelled() {
1386          for (ExecutionMode m : ExecutionMode.values())
1312        for (boolean createIncomplete : new boolean[] { true, false })
1387          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1388      {
1389          final CompletableFuture<Integer> f = new CompletableFuture<>();
1390 <        final Noop r = new Noop(m);
1391 <        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 <        }
1390 >        final Noop[] rs = new Noop[6];
1391 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1392  
1393 <        checkCompletedWithWrappedCancellationException(g);
1393 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1394 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1395 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1396 >        checkIncomplete(h0);
1397 >        checkIncomplete(h1);
1398 >        checkIncomplete(h2);
1399 >        assertTrue(f.cancel(mayInterruptIfRunning));
1400 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1401 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1402 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1403 >
1404 >        checkCompletedWithWrappedCancellationException(h0);
1405 >        checkCompletedWithWrappedCancellationException(h1);
1406 >        checkCompletedWithWrappedCancellationException(h2);
1407 >        checkCompletedWithWrappedCancellationException(h3);
1408 >        checkCompletedWithWrappedCancellationException(h4);
1409 >        checkCompletedWithWrappedCancellationException(h5);
1410          checkCancelled(f);
1411 <        r.assertNotInvoked();
1411 >        for (Noop r : rs) r.assertNotInvoked();
1412      }}
1413  
1414      /**
# Line 1331 | Line 1416 | public class CompletableFutureTest exten
1416       */
1417      public void testThenRun_actionFailed() {
1418          for (ExecutionMode m : ExecutionMode.values())
1334        for (boolean createIncomplete : new boolean[] { true, false })
1419          for (Integer v1 : new Integer[] { 1, null })
1420      {
1421          final CompletableFuture<Integer> f = new CompletableFuture<>();
1422 <        final FailingRunnable r = new FailingRunnable(m);
1423 <        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 <        }
1422 >        final FailingRunnable[] rs = new FailingRunnable[6];
1423 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1424  
1425 <        checkCompletedWithWrappedCFException(g);
1425 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1426 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1427 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1428 >        assertTrue(f.complete(v1));
1429 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1430 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1431 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1432 >
1433 >        checkCompletedWithWrappedCFException(h0);
1434 >        checkCompletedWithWrappedCFException(h1);
1435 >        checkCompletedWithWrappedCFException(h2);
1436 >        checkCompletedWithWrappedCFException(h3);
1437 >        checkCompletedWithWrappedCFException(h4);
1438 >        checkCompletedWithWrappedCFException(h5);
1439          checkCompletedNormally(f, v1);
1440      }}
1441  
# Line 1352 | Line 1444 | public class CompletableFutureTest exten
1444       */
1445      public void testThenApply_normalCompletion() {
1446          for (ExecutionMode m : ExecutionMode.values())
1355        for (boolean createIncomplete : new boolean[] { true, false })
1447          for (Integer v1 : new Integer[] { 1, null })
1448      {
1449          final CompletableFuture<Integer> f = new CompletableFuture<>();
1450 <        final IncFunction r = new IncFunction(m);
1451 <        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 <        }
1450 >        final IncFunction[] rs = new IncFunction[4];
1451 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1452  
1453 <        checkCompletedNormally(g, inc(v1));
1453 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1454 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1455 >        checkIncomplete(h0);
1456 >        checkIncomplete(h1);
1457 >        assertTrue(f.complete(v1));
1458 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1459 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1460 >
1461 >        checkCompletedNormally(h0, inc(v1));
1462 >        checkCompletedNormally(h1, inc(v1));
1463 >        checkCompletedNormally(h2, inc(v1));
1464 >        checkCompletedNormally(h3, inc(v1));
1465          checkCompletedNormally(f, v1);
1466 <        r.assertValue(inc(v1));
1466 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1467      }}
1468  
1469      /**
# Line 1375 | Line 1472 | public class CompletableFutureTest exten
1472       */
1473      public void testThenApply_exceptionalCompletion() {
1474          for (ExecutionMode m : ExecutionMode.values())
1378        for (boolean createIncomplete : new boolean[] { true, false })
1475      {
1476          final CFException ex = new CFException();
1477          final CompletableFuture<Integer> f = new CompletableFuture<>();
1478 <        final IncFunction r = new IncFunction(m);
1479 <        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 <        }
1478 >        final IncFunction[] rs = new IncFunction[4];
1479 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1480  
1481 <        checkCompletedWithWrappedException(g, ex);
1481 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1482 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1483 >        assertTrue(f.completeExceptionally(ex));
1484 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1485 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1486 >
1487 >        checkCompletedWithWrappedException(h0, ex);
1488 >        checkCompletedWithWrappedException(h1, ex);
1489 >        checkCompletedWithWrappedException(h2, ex);
1490 >        checkCompletedWithWrappedException(h3, ex);
1491          checkCompletedExceptionally(f, ex);
1492 <        r.assertNotInvoked();
1492 >        for (IncFunction r : rs) r.assertNotInvoked();
1493      }}
1494  
1495      /**
# Line 1397 | Line 1497 | public class CompletableFutureTest exten
1497       */
1498      public void testThenApply_sourceCancelled() {
1499          for (ExecutionMode m : ExecutionMode.values())
1400        for (boolean createIncomplete : new boolean[] { true, false })
1500          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1501      {
1502          final CompletableFuture<Integer> f = new CompletableFuture<>();
1503 <        final IncFunction r = new IncFunction(m);
1504 <        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 <        }
1503 >        final IncFunction[] rs = new IncFunction[4];
1504 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1505  
1506 <        checkCompletedWithWrappedCancellationException(g);
1506 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1507 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1508 >        assertTrue(f.cancel(mayInterruptIfRunning));
1509 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1510 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1511 >
1512 >        checkCompletedWithWrappedCancellationException(h0);
1513 >        checkCompletedWithWrappedCancellationException(h1);
1514 >        checkCompletedWithWrappedCancellationException(h2);
1515 >        checkCompletedWithWrappedCancellationException(h3);
1516          checkCancelled(f);
1517 <        r.assertNotInvoked();
1517 >        for (IncFunction r : rs) r.assertNotInvoked();
1518      }}
1519  
1520      /**
# Line 1419 | Line 1522 | public class CompletableFutureTest exten
1522       */
1523      public void testThenApply_actionFailed() {
1524          for (ExecutionMode m : ExecutionMode.values())
1422        for (boolean createIncomplete : new boolean[] { true, false })
1525          for (Integer v1 : new Integer[] { 1, null })
1526      {
1527          final CompletableFuture<Integer> f = new CompletableFuture<>();
1528 <        final FailingFunction r = new FailingFunction(m);
1529 <        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 <        }
1528 >        final FailingFunction[] rs = new FailingFunction[4];
1529 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1530  
1531 <        checkCompletedWithWrappedCFException(g);
1531 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1532 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1533 >        assertTrue(f.complete(v1));
1534 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1535 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1536 >
1537 >        checkCompletedWithWrappedCFException(h0);
1538 >        checkCompletedWithWrappedCFException(h1);
1539 >        checkCompletedWithWrappedCFException(h2);
1540 >        checkCompletedWithWrappedCFException(h3);
1541          checkCompletedNormally(f, v1);
1542      }}
1543  
# Line 1440 | Line 1546 | public class CompletableFutureTest exten
1546       */
1547      public void testThenAccept_normalCompletion() {
1548          for (ExecutionMode m : ExecutionMode.values())
1443        for (boolean createIncomplete : new boolean[] { true, false })
1549          for (Integer v1 : new Integer[] { 1, null })
1550      {
1551          final CompletableFuture<Integer> f = new CompletableFuture<>();
1552 <        final NoopConsumer r = new NoopConsumer(m);
1553 <        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 <        }
1552 >        final NoopConsumer[] rs = new NoopConsumer[4];
1553 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1554  
1555 <        checkCompletedNormally(g, null);
1556 <        r.assertValue(v1);
1555 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1556 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1557 >        checkIncomplete(h0);
1558 >        checkIncomplete(h1);
1559 >        assertTrue(f.complete(v1));
1560 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1561 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1562 >
1563 >        checkCompletedNormally(h0, null);
1564 >        checkCompletedNormally(h1, null);
1565 >        checkCompletedNormally(h2, null);
1566 >        checkCompletedNormally(h3, null);
1567          checkCompletedNormally(f, v1);
1568 +        for (NoopConsumer r : rs) r.assertValue(v1);
1569      }}
1570  
1571      /**
# Line 1463 | Line 1574 | public class CompletableFutureTest exten
1574       */
1575      public void testThenAccept_exceptionalCompletion() {
1576          for (ExecutionMode m : ExecutionMode.values())
1466        for (boolean createIncomplete : new boolean[] { true, false })
1577      {
1578          final CFException ex = new CFException();
1579          final CompletableFuture<Integer> f = new CompletableFuture<>();
1580 <        final NoopConsumer r = new NoopConsumer(m);
1581 <        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 <        }
1580 >        final NoopConsumer[] rs = new NoopConsumer[4];
1581 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1582  
1583 <        checkCompletedWithWrappedException(g, ex);
1583 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1584 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1585 >        assertTrue(f.completeExceptionally(ex));
1586 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1587 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1588 >
1589 >        checkCompletedWithWrappedException(h0, ex);
1590 >        checkCompletedWithWrappedException(h1, ex);
1591 >        checkCompletedWithWrappedException(h2, ex);
1592 >        checkCompletedWithWrappedException(h3, ex);
1593          checkCompletedExceptionally(f, ex);
1594 <        r.assertNotInvoked();
1594 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1595      }}
1596  
1597      /**
# Line 1485 | Line 1599 | public class CompletableFutureTest exten
1599       */
1600      public void testThenAccept_sourceCancelled() {
1601          for (ExecutionMode m : ExecutionMode.values())
1488        for (boolean createIncomplete : new boolean[] { true, false })
1602          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1603      {
1604          final CompletableFuture<Integer> f = new CompletableFuture<>();
1605 <        final NoopConsumer r = new NoopConsumer(m);
1606 <        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 <        }
1605 >        final NoopConsumer[] rs = new NoopConsumer[4];
1606 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1607  
1608 <        checkCompletedWithWrappedCancellationException(g);
1608 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1609 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1610 >        assertTrue(f.cancel(mayInterruptIfRunning));
1611 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1612 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1613 >
1614 >        checkCompletedWithWrappedCancellationException(h0);
1615 >        checkCompletedWithWrappedCancellationException(h1);
1616 >        checkCompletedWithWrappedCancellationException(h2);
1617 >        checkCompletedWithWrappedCancellationException(h3);
1618          checkCancelled(f);
1619 <        r.assertNotInvoked();
1619 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1620      }}
1621  
1622      /**
# Line 1507 | Line 1624 | public class CompletableFutureTest exten
1624       */
1625      public void testThenAccept_actionFailed() {
1626          for (ExecutionMode m : ExecutionMode.values())
1510        for (boolean createIncomplete : new boolean[] { true, false })
1627          for (Integer v1 : new Integer[] { 1, null })
1628      {
1629          final CompletableFuture<Integer> f = new CompletableFuture<>();
1630 <        final FailingConsumer r = new FailingConsumer(m);
1631 <        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 <        }
1630 >        final FailingConsumer[] rs = new FailingConsumer[4];
1631 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1632  
1633 <        checkCompletedWithWrappedCFException(g);
1633 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1634 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1635 >        assertTrue(f.complete(v1));
1636 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1637 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1638 >
1639 >        checkCompletedWithWrappedCFException(h0);
1640 >        checkCompletedWithWrappedCFException(h1);
1641 >        checkCompletedWithWrappedCFException(h2);
1642 >        checkCompletedWithWrappedCFException(h3);
1643          checkCompletedNormally(f, v1);
1644      }}
1645  
# Line 1535 | Line 1655 | public class CompletableFutureTest exten
1655      {
1656          final CompletableFuture<Integer> f = new CompletableFuture<>();
1657          final CompletableFuture<Integer> g = new CompletableFuture<>();
1658 <        final SubtractFunction r1 = new SubtractFunction(m);
1659 <        final SubtractFunction r2 = new SubtractFunction(m);
1540 <        final SubtractFunction r3 = new SubtractFunction(m);
1658 >        final SubtractFunction[] rs = new SubtractFunction[6];
1659 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1660  
1661          final CompletableFuture<Integer> fst =  fFirst ? f : g;
1662          final CompletableFuture<Integer> snd = !fFirst ? f : g;
1663          final Integer w1 =  fFirst ? v1 : v2;
1664          final Integer w2 = !fFirst ? v1 : v2;
1665  
1666 <        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1666 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1667 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1668          assertTrue(fst.complete(w1));
1669 <        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1670 <        checkIncomplete(h1);
1671 <        checkIncomplete(h2);
1672 <        r1.assertNotInvoked();
1673 <        r2.assertNotInvoked();
1669 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1670 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1671 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1672 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1673 >        checkCompletedNormally(h1, subtract(w1, w1));
1674 >        checkCompletedNormally(h3, subtract(w1, w1));
1675 >        rs[1].assertValue(subtract(w1, w1));
1676 >        rs[3].assertValue(subtract(w1, w1));
1677          assertTrue(snd.complete(w2));
1678 <        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1678 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1679  
1680 <        checkCompletedNormally(h1, subtract(v1, v2));
1680 >        checkCompletedNormally(h0, subtract(v1, v2));
1681          checkCompletedNormally(h2, subtract(v1, v2));
1682 <        checkCompletedNormally(h3, subtract(v1, v2));
1683 <        r1.assertValue(subtract(v1, v2));
1684 <        r2.assertValue(subtract(v1, v2));
1685 <        r3.assertValue(subtract(v1, v2));
1682 >        checkCompletedNormally(h4, subtract(v1, v2));
1683 >        rs[0].assertValue(subtract(v1, v2));
1684 >        rs[2].assertValue(subtract(v1, v2));
1685 >        rs[4].assertValue(subtract(v1, v2));
1686 >
1687          checkCompletedNormally(f, v1);
1688          checkCompletedNormally(g, v2);
1689      }}
# Line 2906 | Line 3030 | public class CompletableFutureTest exten
3030          checkCancelled(f);
3031      }}
3032  
3033 +    /**
3034 +     * thenCompose result completes exceptionally if the result of the action does
3035 +     */
3036 +    public void testThenCompose_actionReturnsFailingFuture() {
3037 +        for (ExecutionMode m : ExecutionMode.values())
3038 +        for (int order = 0; order < 6; order++)
3039 +        for (Integer v1 : new Integer[] { 1, null })
3040 +    {
3041 +        final CFException ex = new CFException();
3042 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3043 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
3044 +        final CompletableFuture<Integer> h;
3045 +        // Test all permutations of orders
3046 +        switch (order) {
3047 +        case 0:
3048 +            assertTrue(f.complete(v1));
3049 +            assertTrue(g.completeExceptionally(ex));
3050 +            h = m.thenCompose(f, (x -> g));
3051 +            break;
3052 +        case 1:
3053 +            assertTrue(f.complete(v1));
3054 +            h = m.thenCompose(f, (x -> g));
3055 +            assertTrue(g.completeExceptionally(ex));
3056 +            break;
3057 +        case 2:
3058 +            assertTrue(g.completeExceptionally(ex));
3059 +            assertTrue(f.complete(v1));
3060 +            h = m.thenCompose(f, (x -> g));
3061 +            break;
3062 +        case 3:
3063 +            assertTrue(g.completeExceptionally(ex));
3064 +            h = m.thenCompose(f, (x -> g));
3065 +            assertTrue(f.complete(v1));
3066 +            break;
3067 +        case 4:
3068 +            h = m.thenCompose(f, (x -> g));
3069 +            assertTrue(f.complete(v1));
3070 +            assertTrue(g.completeExceptionally(ex));
3071 +            break;
3072 +        case 5:
3073 +            h = m.thenCompose(f, (x -> g));
3074 +            assertTrue(f.complete(v1));
3075 +            assertTrue(g.completeExceptionally(ex));
3076 +            break;
3077 +        default: throw new AssertionError();
3078 +        }
3079 +
3080 +        checkCompletedExceptionally(g, ex);
3081 +        checkCompletedWithWrappedException(h, ex);
3082 +        checkCompletedNormally(f, v1);
3083 +    }}
3084 +
3085      // other static methods
3086  
3087      /**
# Line 3073 | Line 3249 | public class CompletableFutureTest exten
3249          CompletableFuture<Integer> f = new CompletableFuture<>();
3250          CompletableFuture<Integer> g = new CompletableFuture<>();
3251          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3076        CompletableFuture<?> h;
3252          ThreadExecutor exec = new ThreadExecutor();
3253  
3254          Runnable[] throwingActions = {
3255              () -> CompletableFuture.supplyAsync(null),
3256              () -> CompletableFuture.supplyAsync(null, exec),
3257 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3257 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3258  
3259              () -> CompletableFuture.runAsync(null),
3260              () -> CompletableFuture.runAsync(null, exec),
# Line 3184 | Line 3359 | public class CompletableFutureTest exten
3359          assertSame(f, f.toCompletableFuture());
3360      }
3361  
3362 +    // jdk9
3363 +
3364 +    /**
3365 +     * newIncompleteFuture returns an incomplete CompletableFuture
3366 +     */
3367 +    public void testNewIncompleteFuture() {
3368 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3369 +        CompletableFuture<Integer> g = f.newIncompleteFuture();
3370 +        checkIncomplete(f);
3371 +        checkIncomplete(g);
3372 +    }
3373 +
3374 +    /**
3375 +     * completedStage returns a completed CompletionStage
3376 +     */
3377 +    public void testCompletedStage() {
3378 +        AtomicInteger x = new AtomicInteger();
3379 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3380 +        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3381 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3382 +        assertEquals(x.get(), 1);
3383 +        assertNull(r.get());
3384 +    }
3385 +
3386 +    /**
3387 +     * defaultExecutor by default returns the commonPool if
3388 +     * it supports more than one thread.
3389 +     */
3390 +    public void testDefaultExecutor() {
3391 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3392 +        Executor e = f.defaultExecutor();
3393 +        Executor c = ForkJoinPool.commonPool();
3394 +        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3395 +            assertSame(e, c);
3396 +        else
3397 +            assertNotSame(e, c);
3398 +    }
3399 +
3400 +    /**
3401 +     * failedFuture returns a CompletableFuture completed
3402 +     * exceptionally with the given Exception
3403 +     */
3404 +    public void testFailedFuture() {
3405 +        CFException ex = new CFException();
3406 +        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3407 +        checkCompletedExceptionallyWithRootCause(f, ex);
3408 +    }
3409 +
3410 +    /**
3411 +     * failedFuture(null) throws NPE
3412 +     */
3413 +    public void testFailedFuture2() {
3414 +        try {
3415 +            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3416 +            shouldThrow();
3417 +        } catch (NullPointerException success) {}
3418 +    }
3419 +
3420 +    /**
3421 +     * copy returns a CompletableFuture that is completed normally,
3422 +     * with the same value, when source is.
3423 +     */
3424 +    public void testCopy() {
3425 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3426 +        CompletableFuture<Integer> g = f.copy();
3427 +        checkIncomplete(f);
3428 +        checkIncomplete(g);
3429 +        f.complete(1);
3430 +        checkCompletedNormally(f, 1);
3431 +        checkCompletedNormally(g, 1);
3432 +    }
3433 +
3434 +    /**
3435 +     * copy returns a CompletableFuture that is completed exceptionally
3436 +     * when source is.
3437 +     */
3438 +    public void testCopy2() {
3439 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3440 +        CompletableFuture<Integer> g = f.copy();
3441 +        checkIncomplete(f);
3442 +        checkIncomplete(g);
3443 +        CFException ex = new CFException();
3444 +        f.completeExceptionally(ex);
3445 +        checkCompletedExceptionally(f, ex);
3446 +        checkCompletedWithWrappedCFException(g);
3447 +    }
3448 +
3449 +    /**
3450 +     * minimalCompletionStage returns a CompletableFuture that is
3451 +     * completed normally, with the same value, when source is.
3452 +     */
3453 +    public void testMinimalCompletionStage() {
3454 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3455 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3456 +        AtomicInteger x = new AtomicInteger();
3457 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3458 +        checkIncomplete(f);
3459 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3460 +        f.complete(1);
3461 +        checkCompletedNormally(f, 1);
3462 +        assertEquals(x.get(), 1);
3463 +        assertNull(r.get());
3464 +    }
3465 +
3466 +    /**
3467 +     * minimalCompletionStage returns a CompletableFuture that is
3468 +     * completed exceptionally when source is.
3469 +     */
3470 +    public void testMinimalCompletionStage2() {
3471 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3472 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3473 +        AtomicInteger x = new AtomicInteger();
3474 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3475 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3476 +        checkIncomplete(f);
3477 +        CFException ex = new CFException();
3478 +        f.completeExceptionally(ex);
3479 +        checkCompletedExceptionally(f, ex);
3480 +        assertEquals(x.get(), 0);
3481 +        assertEquals(r.get().getCause(), ex);
3482 +    }
3483 +
3484 +    /**
3485 +     * failedStage returns a CompletionStage completed
3486 +     * exceptionally with the given Exception
3487 +     */
3488 +    public void testFailedStage() {
3489 +        CFException ex = new CFException();
3490 +        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3491 +        AtomicInteger x = new AtomicInteger();
3492 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3493 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3494 +        assertEquals(x.get(), 0);
3495 +        assertEquals(r.get().getCause(), ex);
3496 +    }
3497 +
3498 +    /**
3499 +     * completeAsync completes with value of given supplier
3500 +     */
3501 +    public void testCompleteAsync() {
3502 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3503 +        f.completeAsync(() -> 1);
3504 +        f.join();
3505 +        checkCompletedNormally(f, 1);
3506 +    }
3507 +
3508 +    /**
3509 +     * completeAsync completes exceptionally if given supplier throws
3510 +     */
3511 +    public void testCompleteAsync2() {
3512 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3513 +        CFException ex = new CFException();
3514 +        f.completeAsync(() -> {if (true) throw ex; return 1;});
3515 +        try {
3516 +            f.join();
3517 +            shouldThrow();
3518 +        } catch (Exception success) {}
3519 +        checkCompletedWithWrappedCFException(f);
3520 +    }
3521 +
3522 +    /**
3523 +     * completeAsync with given executor completes with value of given supplier
3524 +     */
3525 +    public void testCompleteAsync3() {
3526 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3527 +        f.completeAsync(() -> 1, new ThreadExecutor());
3528 +        f.join();
3529 +        checkCompletedNormally(f, 1);
3530 +    }
3531 +
3532 +    /**
3533 +     * completeAsync with given executor completes exceptionally if
3534 +     * given supplier throws
3535 +     */
3536 +    public void testCompleteAsync4() {
3537 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3538 +        CFException ex = new CFException();
3539 +        f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor());
3540 +        try {
3541 +            f.join();
3542 +            shouldThrow();
3543 +        } catch (Exception success) {}
3544 +        checkCompletedWithWrappedCFException(f);
3545 +    }
3546 +
3547 +    /**
3548 +     * orTimeout completes with TimeoutException if not complete
3549 +     */
3550 +    public void testOrTimeout() {
3551 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3552 +        f.orTimeout(SHORT_DELAY_MS, MILLISECONDS);
3553 +        checkCompletedExceptionallyWithTimeout(f);
3554 +    }
3555 +
3556 +    /**
3557 +     * orTimeout completes normally if completed before timeout
3558 +     */
3559 +    public void testOrTimeout2() {
3560 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3561 +        f.complete(1);
3562 +        f.orTimeout(SHORT_DELAY_MS, MILLISECONDS);
3563 +        checkCompletedNormally(f, 1);
3564 +    }
3565 +
3566 +    /**
3567 +     * completeOnTimeout completes with given value if not complete
3568 +     */
3569 +    public void testCompleteOnTimeout() {
3570 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3571 +        f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS);
3572 +        f.join();
3573 +        checkCompletedNormally(f, -1);
3574 +    }
3575 +
3576 +    /**
3577 +     * completeOnTimeout has no effect if completed within timeout
3578 +     */
3579 +    public void testCompleteOnTimeout2() {
3580 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3581 +        f.complete(1);
3582 +        f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS);
3583 +        checkCompletedNormally(f, 1);
3584 +    }
3585 +
3586 +    /**
3587 +     * delayedExecutor returns an executor that delays submission
3588 +     */
3589 +    public void testDelayedExecutor() {
3590 +        long timeoutMillis = SMALL_DELAY_MS;
3591 +        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3592 +                                                       MILLISECONDS);
3593 +        long startTime = System.nanoTime();
3594 +        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3595 +        assertNull(f.getNow(null));
3596 +        try {
3597 +            f.get(LONG_DELAY_MS, MILLISECONDS);
3598 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
3599 +        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3600 +        checkCompletedNormally(f, 1);
3601 +    }
3602 +
3603 +    /**
3604 +     * delayedExecutor for a given executor returns an executor that
3605 +     * delays submission
3606 +     */
3607 +    public void testDelayedExecutor2() {
3608 +        long timeoutMillis = SMALL_DELAY_MS;
3609 +        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3610 +                                                       MILLISECONDS,
3611 +                                                       new ThreadExecutor());
3612 +        long startTime = System.nanoTime();
3613 +        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3614 +        assertNull(f.getNow(null));
3615 +        try {
3616 +            f.get(LONG_DELAY_MS, MILLISECONDS);
3617 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
3618 +        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3619 +        checkCompletedNormally(f, 1);
3620 +    }
3621 +
3622      //--- tests of implementation details; not part of official tck ---
3623  
3624      Object resultOf(CompletableFuture<?> f) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines