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.80 by jsr166, Mon Jun 16 17:29:03 2014 UTC vs.
Revision 1.96 by jsr166, Sat Nov 1 14:50:26 2014 UTC

# Line 57 | Line 57 | public class CompletableFutureTest exten
57      }
58  
59      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
60 <        try {
61 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
60 >        checkTimedGet(f, value);
61 >
62          try {
63              assertEquals(value, f.join());
64          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 76 | Line 75 | public class CompletableFutureTest exten
75      }
76  
77      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
78 +        long startTime = System.nanoTime();
79 +        long timeoutMillis = LONG_DELAY_MS;
80          try {
81 <            f.get(LONG_DELAY_MS, MILLISECONDS);
81 >            f.get(timeoutMillis, MILLISECONDS);
82              shouldThrow();
83          } catch (ExecutionException success) {
84              assertTrue(success.getCause() instanceof CFException);
85          } catch (Throwable fail) { threadUnexpectedException(fail); }
86 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
87 +
88          try {
89              f.join();
90              shouldThrow();
# Line 107 | Line 110 | public class CompletableFutureTest exten
110  
111      <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
112                                                        Throwable ex) {
113 +        long startTime = System.nanoTime();
114 +        long timeoutMillis = LONG_DELAY_MS;
115          try {
116 <            f.get(LONG_DELAY_MS, MILLISECONDS);
116 >            f.get(timeoutMillis, MILLISECONDS);
117              shouldThrow();
118          } catch (ExecutionException success) {
119              assertSame(ex, success.getCause());
120          } catch (Throwable fail) { threadUnexpectedException(fail); }
121 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
122 +
123          try {
124              f.join();
125              shouldThrow();
# Line 158 | Line 165 | public class CompletableFutureTest exten
165      }
166  
167      void checkCancelled(CompletableFuture<?> f) {
168 +        long startTime = System.nanoTime();
169 +        long timeoutMillis = LONG_DELAY_MS;
170          try {
171 <            f.get(LONG_DELAY_MS, MILLISECONDS);
171 >            f.get(timeoutMillis, MILLISECONDS);
172              shouldThrow();
173          } catch (CancellationException success) {
174          } catch (Throwable fail) { threadUnexpectedException(fail); }
175 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
176 +
177          try {
178              f.join();
179              shouldThrow();
# Line 183 | Line 194 | public class CompletableFutureTest exten
194      }
195  
196      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
197 +        long startTime = System.nanoTime();
198 +        long timeoutMillis = LONG_DELAY_MS;
199          try {
200 <            f.get(LONG_DELAY_MS, MILLISECONDS);
200 >            f.get(timeoutMillis, MILLISECONDS);
201              shouldThrow();
202          } catch (ExecutionException success) {
203              assertTrue(success.getCause() instanceof CancellationException);
204          } catch (Throwable fail) { threadUnexpectedException(fail); }
205 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
206 +
207          try {
208              f.join();
209              shouldThrow();
# Line 569 | Line 584 | public class CompletableFutureTest exten
584          }
585      }
586  
587 +    static final boolean defaultExecutorIsCommonPool
588 +        = ForkJoinPool.getCommonPoolParallelism() > 1;
589 +
590      /**
591       * Permits the testing of parallel code for the 3 different
592       * execution modes without copy/pasting all the test methods.
593       */
594      enum ExecutionMode {
595 <        DEFAULT {
595 >        SYNC {
596              public void checkExecutionMode() {
597                  assertFalse(ThreadExecutor.startedCurrentThread());
598                  assertNull(ForkJoinTask.getPool());
# Line 650 | Line 668 | public class CompletableFutureTest exten
668  
669          ASYNC {
670              public void checkExecutionMode() {
671 <                assertSame(ForkJoinPool.commonPool(),
672 <                           ForkJoinTask.getPool());
671 >                assertEquals(defaultExecutorIsCommonPool,
672 >                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
673              }
674              public CompletableFuture<Void> runAsync(Runnable a) {
675                  return CompletableFuture.runAsync(a);
# Line 875 | Line 893 | public class CompletableFutureTest exten
893          if (!createIncomplete) f.completeExceptionally(ex);
894          final CompletableFuture<Integer> g = f.exceptionally
895              ((Throwable t) -> {
896 <                ExecutionMode.DEFAULT.checkExecutionMode();
896 >                ExecutionMode.SYNC.checkExecutionMode();
897                  threadAssertSame(t, ex);
898                  a.getAndIncrement();
899                  return v1;
# Line 897 | Line 915 | public class CompletableFutureTest exten
915          if (!createIncomplete) f.completeExceptionally(ex1);
916          final CompletableFuture<Integer> g = f.exceptionally
917              ((Throwable t) -> {
918 <                ExecutionMode.DEFAULT.checkExecutionMode();
918 >                ExecutionMode.SYNC.checkExecutionMode();
919                  threadAssertSame(t, ex1);
920                  a.getAndIncrement();
921                  throw ex2;
# Line 1264 | Line 1282 | public class CompletableFutureTest exten
1282       */
1283      public void testThenRun_normalCompletion() {
1284          for (ExecutionMode m : ExecutionMode.values())
1267        for (boolean createIncomplete : new boolean[] { true, false })
1285          for (Integer v1 : new Integer[] { 1, null })
1286      {
1287          final CompletableFuture<Integer> f = new CompletableFuture<>();
1288 <        final Noop r = new Noop(m);
1289 <        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 <        }
1288 >        final Noop[] rs = new Noop[6];
1289 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1290  
1291 <        checkCompletedNormally(g, null);
1291 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1292 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1293 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1294 >        checkIncomplete(h0);
1295 >        checkIncomplete(h1);
1296 >        checkIncomplete(h2);
1297 >        assertTrue(f.complete(v1));
1298 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1299 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1300 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1301 >
1302 >        checkCompletedNormally(h0, null);
1303 >        checkCompletedNormally(h1, null);
1304 >        checkCompletedNormally(h2, null);
1305 >        checkCompletedNormally(h3, null);
1306 >        checkCompletedNormally(h4, null);
1307 >        checkCompletedNormally(h5, null);
1308          checkCompletedNormally(f, v1);
1309 <        r.assertInvoked();
1309 >        for (Noop r : rs) r.assertInvoked();
1310      }}
1311  
1312      /**
# Line 1287 | Line 1315 | public class CompletableFutureTest exten
1315       */
1316      public void testThenRun_exceptionalCompletion() {
1317          for (ExecutionMode m : ExecutionMode.values())
1290        for (boolean createIncomplete : new boolean[] { true, false })
1318      {
1319          final CFException ex = new CFException();
1320          final CompletableFuture<Integer> f = new CompletableFuture<>();
1321 <        final Noop r = new Noop(m);
1322 <        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 <        }
1321 >        final Noop[] rs = new Noop[6];
1322 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1323  
1324 <        checkCompletedWithWrappedException(g, ex);
1324 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1325 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1326 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1327 >        checkIncomplete(h0);
1328 >        checkIncomplete(h1);
1329 >        checkIncomplete(h2);
1330 >        assertTrue(f.completeExceptionally(ex));
1331 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1332 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1333 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1334 >
1335 >        checkCompletedWithWrappedException(h0, ex);
1336 >        checkCompletedWithWrappedException(h1, ex);
1337 >        checkCompletedWithWrappedException(h2, ex);
1338 >        checkCompletedWithWrappedException(h3, ex);
1339 >        checkCompletedWithWrappedException(h4, ex);
1340 >        checkCompletedWithWrappedException(h5, ex);
1341          checkCompletedExceptionally(f, ex);
1342 <        r.assertNotInvoked();
1342 >        for (Noop r : rs) r.assertNotInvoked();
1343      }}
1344  
1345      /**
# Line 1309 | Line 1347 | public class CompletableFutureTest exten
1347       */
1348      public void testThenRun_sourceCancelled() {
1349          for (ExecutionMode m : ExecutionMode.values())
1312        for (boolean createIncomplete : new boolean[] { true, false })
1350          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1351      {
1352          final CompletableFuture<Integer> f = new CompletableFuture<>();
1353 <        final Noop r = new Noop(m);
1354 <        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 <        }
1353 >        final Noop[] rs = new Noop[6];
1354 >        for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
1355  
1356 <        checkCompletedWithWrappedCancellationException(g);
1356 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1357 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1358 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1359 >        checkIncomplete(h0);
1360 >        checkIncomplete(h1);
1361 >        checkIncomplete(h2);
1362 >        assertTrue(f.cancel(mayInterruptIfRunning));
1363 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1364 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1365 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1366 >
1367 >        checkCompletedWithWrappedCancellationException(h0);
1368 >        checkCompletedWithWrappedCancellationException(h1);
1369 >        checkCompletedWithWrappedCancellationException(h2);
1370 >        checkCompletedWithWrappedCancellationException(h3);
1371 >        checkCompletedWithWrappedCancellationException(h4);
1372 >        checkCompletedWithWrappedCancellationException(h5);
1373          checkCancelled(f);
1374 <        r.assertNotInvoked();
1374 >        for (Noop r : rs) r.assertNotInvoked();
1375      }}
1376  
1377      /**
# Line 1331 | Line 1379 | public class CompletableFutureTest exten
1379       */
1380      public void testThenRun_actionFailed() {
1381          for (ExecutionMode m : ExecutionMode.values())
1334        for (boolean createIncomplete : new boolean[] { true, false })
1382          for (Integer v1 : new Integer[] { 1, null })
1383      {
1384          final CompletableFuture<Integer> f = new CompletableFuture<>();
1385 <        final FailingRunnable r = new FailingRunnable(m);
1386 <        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 <        }
1385 >        final FailingRunnable[] rs = new FailingRunnable[6];
1386 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m);
1387  
1388 <        checkCompletedWithWrappedCFException(g);
1388 >        final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]);
1389 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]);
1390 >        final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]);
1391 >        assertTrue(f.complete(v1));
1392 >        final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]);
1393 >        final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]);
1394 >        final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]);
1395 >
1396 >        checkCompletedWithWrappedCFException(h0);
1397 >        checkCompletedWithWrappedCFException(h1);
1398 >        checkCompletedWithWrappedCFException(h2);
1399 >        checkCompletedWithWrappedCFException(h3);
1400 >        checkCompletedWithWrappedCFException(h4);
1401 >        checkCompletedWithWrappedCFException(h5);
1402          checkCompletedNormally(f, v1);
1403      }}
1404  
# Line 1352 | Line 1407 | public class CompletableFutureTest exten
1407       */
1408      public void testThenApply_normalCompletion() {
1409          for (ExecutionMode m : ExecutionMode.values())
1355        for (boolean createIncomplete : new boolean[] { true, false })
1410          for (Integer v1 : new Integer[] { 1, null })
1411      {
1412          final CompletableFuture<Integer> f = new CompletableFuture<>();
1413 <        final IncFunction r = new IncFunction(m);
1414 <        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 <        }
1413 >        final IncFunction[] rs = new IncFunction[4];
1414 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1415  
1416 <        checkCompletedNormally(g, inc(v1));
1416 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1417 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1418 >        checkIncomplete(h0);
1419 >        checkIncomplete(h1);
1420 >        assertTrue(f.complete(v1));
1421 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1422 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1423 >
1424 >        checkCompletedNormally(h0, inc(v1));
1425 >        checkCompletedNormally(h1, inc(v1));
1426 >        checkCompletedNormally(h2, inc(v1));
1427 >        checkCompletedNormally(h3, inc(v1));
1428          checkCompletedNormally(f, v1);
1429 <        r.assertValue(inc(v1));
1429 >        for (IncFunction r : rs) r.assertValue(inc(v1));
1430      }}
1431  
1432      /**
# Line 1375 | Line 1435 | public class CompletableFutureTest exten
1435       */
1436      public void testThenApply_exceptionalCompletion() {
1437          for (ExecutionMode m : ExecutionMode.values())
1378        for (boolean createIncomplete : new boolean[] { true, false })
1438      {
1439          final CFException ex = new CFException();
1440          final CompletableFuture<Integer> f = new CompletableFuture<>();
1441 <        final IncFunction r = new IncFunction(m);
1442 <        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 <        }
1441 >        final IncFunction[] rs = new IncFunction[4];
1442 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1443  
1444 <        checkCompletedWithWrappedException(g, ex);
1444 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1445 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1446 >        assertTrue(f.completeExceptionally(ex));
1447 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1448 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1449 >
1450 >        checkCompletedWithWrappedException(h0, ex);
1451 >        checkCompletedWithWrappedException(h1, ex);
1452 >        checkCompletedWithWrappedException(h2, ex);
1453 >        checkCompletedWithWrappedException(h3, ex);
1454          checkCompletedExceptionally(f, ex);
1455 <        r.assertNotInvoked();
1455 >        for (IncFunction r : rs) r.assertNotInvoked();
1456      }}
1457  
1458      /**
# Line 1397 | Line 1460 | public class CompletableFutureTest exten
1460       */
1461      public void testThenApply_sourceCancelled() {
1462          for (ExecutionMode m : ExecutionMode.values())
1400        for (boolean createIncomplete : new boolean[] { true, false })
1463          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1464      {
1465          final CompletableFuture<Integer> f = new CompletableFuture<>();
1466 <        final IncFunction r = new IncFunction(m);
1467 <        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 <        }
1466 >        final IncFunction[] rs = new IncFunction[4];
1467 >        for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m);
1468  
1469 <        checkCompletedWithWrappedCancellationException(g);
1469 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1470 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1471 >        assertTrue(f.cancel(mayInterruptIfRunning));
1472 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1473 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1474 >
1475 >        checkCompletedWithWrappedCancellationException(h0);
1476 >        checkCompletedWithWrappedCancellationException(h1);
1477 >        checkCompletedWithWrappedCancellationException(h2);
1478 >        checkCompletedWithWrappedCancellationException(h3);
1479          checkCancelled(f);
1480 <        r.assertNotInvoked();
1480 >        for (IncFunction r : rs) r.assertNotInvoked();
1481      }}
1482  
1483      /**
# Line 1419 | Line 1485 | public class CompletableFutureTest exten
1485       */
1486      public void testThenApply_actionFailed() {
1487          for (ExecutionMode m : ExecutionMode.values())
1422        for (boolean createIncomplete : new boolean[] { true, false })
1488          for (Integer v1 : new Integer[] { 1, null })
1489      {
1490          final CompletableFuture<Integer> f = new CompletableFuture<>();
1491 <        final FailingFunction r = new FailingFunction(m);
1492 <        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 <        }
1491 >        final FailingFunction[] rs = new FailingFunction[4];
1492 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m);
1493  
1494 <        checkCompletedWithWrappedCFException(g);
1494 >        final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]);
1495 >        final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]);
1496 >        assertTrue(f.complete(v1));
1497 >        final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]);
1498 >        final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]);
1499 >
1500 >        checkCompletedWithWrappedCFException(h0);
1501 >        checkCompletedWithWrappedCFException(h1);
1502 >        checkCompletedWithWrappedCFException(h2);
1503 >        checkCompletedWithWrappedCFException(h3);
1504          checkCompletedNormally(f, v1);
1505      }}
1506  
# Line 1440 | Line 1509 | public class CompletableFutureTest exten
1509       */
1510      public void testThenAccept_normalCompletion() {
1511          for (ExecutionMode m : ExecutionMode.values())
1443        for (boolean createIncomplete : new boolean[] { true, false })
1512          for (Integer v1 : new Integer[] { 1, null })
1513      {
1514          final CompletableFuture<Integer> f = new CompletableFuture<>();
1515 <        final NoopConsumer r = new NoopConsumer(m);
1516 <        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 <        }
1515 >        final NoopConsumer[] rs = new NoopConsumer[4];
1516 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1517  
1518 <        checkCompletedNormally(g, null);
1519 <        r.assertValue(v1);
1518 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1519 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1520 >        checkIncomplete(h0);
1521 >        checkIncomplete(h1);
1522 >        assertTrue(f.complete(v1));
1523 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1524 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1525 >
1526 >        checkCompletedNormally(h0, null);
1527 >        checkCompletedNormally(h1, null);
1528 >        checkCompletedNormally(h2, null);
1529 >        checkCompletedNormally(h3, null);
1530          checkCompletedNormally(f, v1);
1531 +        for (NoopConsumer r : rs) r.assertValue(v1);
1532      }}
1533  
1534      /**
# Line 1463 | Line 1537 | public class CompletableFutureTest exten
1537       */
1538      public void testThenAccept_exceptionalCompletion() {
1539          for (ExecutionMode m : ExecutionMode.values())
1466        for (boolean createIncomplete : new boolean[] { true, false })
1540      {
1541          final CFException ex = new CFException();
1542          final CompletableFuture<Integer> f = new CompletableFuture<>();
1543 <        final NoopConsumer r = new NoopConsumer(m);
1544 <        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 <        }
1543 >        final NoopConsumer[] rs = new NoopConsumer[4];
1544 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1545  
1546 <        checkCompletedWithWrappedException(g, ex);
1546 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1547 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1548 >        assertTrue(f.completeExceptionally(ex));
1549 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1550 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1551 >
1552 >        checkCompletedWithWrappedException(h0, ex);
1553 >        checkCompletedWithWrappedException(h1, ex);
1554 >        checkCompletedWithWrappedException(h2, ex);
1555 >        checkCompletedWithWrappedException(h3, ex);
1556          checkCompletedExceptionally(f, ex);
1557 <        r.assertNotInvoked();
1557 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1558      }}
1559  
1560      /**
# Line 1485 | Line 1562 | public class CompletableFutureTest exten
1562       */
1563      public void testThenAccept_sourceCancelled() {
1564          for (ExecutionMode m : ExecutionMode.values())
1488        for (boolean createIncomplete : new boolean[] { true, false })
1565          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1566      {
1567          final CompletableFuture<Integer> f = new CompletableFuture<>();
1568 <        final NoopConsumer r = new NoopConsumer(m);
1569 <        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 <        }
1568 >        final NoopConsumer[] rs = new NoopConsumer[4];
1569 >        for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m);
1570  
1571 <        checkCompletedWithWrappedCancellationException(g);
1571 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1572 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1573 >        assertTrue(f.cancel(mayInterruptIfRunning));
1574 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1575 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1576 >
1577 >        checkCompletedWithWrappedCancellationException(h0);
1578 >        checkCompletedWithWrappedCancellationException(h1);
1579 >        checkCompletedWithWrappedCancellationException(h2);
1580 >        checkCompletedWithWrappedCancellationException(h3);
1581          checkCancelled(f);
1582 <        r.assertNotInvoked();
1582 >        for (NoopConsumer r : rs) r.assertNotInvoked();
1583      }}
1584  
1585      /**
# Line 1507 | Line 1587 | public class CompletableFutureTest exten
1587       */
1588      public void testThenAccept_actionFailed() {
1589          for (ExecutionMode m : ExecutionMode.values())
1510        for (boolean createIncomplete : new boolean[] { true, false })
1590          for (Integer v1 : new Integer[] { 1, null })
1591      {
1592          final CompletableFuture<Integer> f = new CompletableFuture<>();
1593 <        final FailingConsumer r = new FailingConsumer(m);
1594 <        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 <        }
1593 >        final FailingConsumer[] rs = new FailingConsumer[4];
1594 >        for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m);
1595  
1596 <        checkCompletedWithWrappedCFException(g);
1596 >        final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]);
1597 >        final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]);
1598 >        assertTrue(f.complete(v1));
1599 >        final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]);
1600 >        final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]);
1601 >
1602 >        checkCompletedWithWrappedCFException(h0);
1603 >        checkCompletedWithWrappedCFException(h1);
1604 >        checkCompletedWithWrappedCFException(h2);
1605 >        checkCompletedWithWrappedCFException(h3);
1606          checkCompletedNormally(f, v1);
1607      }}
1608  
# Line 1535 | Line 1618 | public class CompletableFutureTest exten
1618      {
1619          final CompletableFuture<Integer> f = new CompletableFuture<>();
1620          final CompletableFuture<Integer> g = new CompletableFuture<>();
1621 <        final SubtractFunction r1 = new SubtractFunction(m);
1622 <        final SubtractFunction r2 = new SubtractFunction(m);
1540 <        final SubtractFunction r3 = new SubtractFunction(m);
1621 >        final SubtractFunction[] rs = new SubtractFunction[6];
1622 >        for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m);
1623  
1624          final CompletableFuture<Integer> fst =  fFirst ? f : g;
1625          final CompletableFuture<Integer> snd = !fFirst ? f : g;
1626          final Integer w1 =  fFirst ? v1 : v2;
1627          final Integer w2 = !fFirst ? v1 : v2;
1628  
1629 <        final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1);
1629 >        final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]);
1630 >        final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]);
1631          assertTrue(fst.complete(w1));
1632 <        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2);
1633 <        checkIncomplete(h1);
1634 <        checkIncomplete(h2);
1635 <        r1.assertNotInvoked();
1636 <        r2.assertNotInvoked();
1632 >        final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]);
1633 >        final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]);
1634 >        checkIncomplete(h0); rs[0].assertNotInvoked();
1635 >        checkIncomplete(h2); rs[2].assertNotInvoked();
1636 >        checkCompletedNormally(h1, subtract(w1, w1));
1637 >        checkCompletedNormally(h3, subtract(w1, w1));
1638 >        rs[1].assertValue(subtract(w1, w1));
1639 >        rs[3].assertValue(subtract(w1, w1));
1640          assertTrue(snd.complete(w2));
1641 <        final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3);
1641 >        final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]);
1642  
1643 <        checkCompletedNormally(h1, subtract(v1, v2));
1643 >        checkCompletedNormally(h0, subtract(v1, v2));
1644          checkCompletedNormally(h2, subtract(v1, v2));
1645 <        checkCompletedNormally(h3, subtract(v1, v2));
1646 <        r1.assertValue(subtract(v1, v2));
1647 <        r2.assertValue(subtract(v1, v2));
1648 <        r3.assertValue(subtract(v1, v2));
1645 >        checkCompletedNormally(h4, subtract(v1, v2));
1646 >        rs[0].assertValue(subtract(v1, v2));
1647 >        rs[2].assertValue(subtract(v1, v2));
1648 >        rs[4].assertValue(subtract(v1, v2));
1649 >
1650          checkCompletedNormally(f, v1);
1651          checkCompletedNormally(g, v2);
1652      }}
# Line 1680 | Line 1767 | public class CompletableFutureTest exten
1767          checkCompletedWithWrappedCFException(h1);
1768          checkCompletedWithWrappedCFException(h2);
1769          checkCompletedWithWrappedCFException(h3);
1770 +        r1.assertInvoked();
1771 +        r2.assertInvoked();
1772 +        r3.assertInvoked();
1773          checkCompletedNormally(f, v1);
1774          checkCompletedNormally(g, v2);
1775      }}
# Line 1841 | Line 1931 | public class CompletableFutureTest exten
1931          checkCompletedWithWrappedCFException(h1);
1932          checkCompletedWithWrappedCFException(h2);
1933          checkCompletedWithWrappedCFException(h3);
1934 +        r1.assertInvoked();
1935 +        r2.assertInvoked();
1936 +        r3.assertInvoked();
1937          checkCompletedNormally(f, v1);
1938          checkCompletedNormally(g, v2);
1939      }}
# Line 1851 | Line 1944 | public class CompletableFutureTest exten
1944       */
1945      public void testRunAfterBoth_normalCompletion() {
1946          for (ExecutionMode m : ExecutionMode.values())
1854        for (boolean createIncomplete : new boolean[] { true, false })
1947          for (boolean fFirst : new boolean[] { true, false })
1948          for (Integer v1 : new Integer[] { 1, null })
1949          for (Integer v2 : new Integer[] { 2, null })
1950      {
1951          final CompletableFuture<Integer> f = new CompletableFuture<>();
1952          final CompletableFuture<Integer> g = new CompletableFuture<>();
1953 <        final Noop r = new Noop(m);
1953 >        final Noop r1 = new Noop(m);
1954 >        final Noop r2 = new Noop(m);
1955 >        final Noop r3 = new Noop(m);
1956  
1957 <        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1958 <        if (!createIncomplete)
1959 <            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1960 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1867 <        if (createIncomplete) {
1868 <            checkIncomplete(h);
1869 <            r.assertNotInvoked();
1870 <            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1871 <        }
1957 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1958 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1959 >        final Integer w1 =  fFirst ? v1 : v2;
1960 >        final Integer w2 = !fFirst ? v1 : v2;
1961  
1962 <        checkCompletedNormally(h, null);
1963 <        r.assertInvoked();
1962 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1963 >        assertTrue(fst.complete(w1));
1964 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1965 >        checkIncomplete(h1);
1966 >        checkIncomplete(h2);
1967 >        r1.assertNotInvoked();
1968 >        r2.assertNotInvoked();
1969 >        assertTrue(snd.complete(w2));
1970 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
1971 >
1972 >        checkCompletedNormally(h1, null);
1973 >        checkCompletedNormally(h2, null);
1974 >        checkCompletedNormally(h3, null);
1975 >        r1.assertInvoked();
1976 >        r2.assertInvoked();
1977 >        r3.assertInvoked();
1978          checkCompletedNormally(f, v1);
1979          checkCompletedNormally(g, v2);
1980      }}
# Line 1880 | Line 1983 | public class CompletableFutureTest exten
1983       * runAfterBoth result completes exceptionally after exceptional
1984       * completion of either source
1985       */
1986 <    public void testRunAfterBoth_exceptionalCompletion() {
1986 >    public void testRunAfterBoth_exceptionalCompletion() throws Throwable {
1987          for (ExecutionMode m : ExecutionMode.values())
1885        for (boolean createIncomplete : new boolean[] { true, false })
1988          for (boolean fFirst : new boolean[] { true, false })
1989 +        for (boolean failFirst : new boolean[] { true, false })
1990          for (Integer v1 : new Integer[] { 1, null })
1991      {
1992          final CompletableFuture<Integer> f = new CompletableFuture<>();
1993          final CompletableFuture<Integer> g = new CompletableFuture<>();
1994          final CFException ex = new CFException();
1995 <        final Noop r = new Noop(m);
1995 >        final Noop r1 = new Noop(m);
1996 >        final Noop r2 = new Noop(m);
1997 >        final Noop r3 = new Noop(m);
1998  
1999 <        assertTrue((fFirst ? f : g).complete(v1));
2000 <        if (!createIncomplete)
2001 <            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
2002 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2003 <        if (createIncomplete) {
2004 <            checkIncomplete(h);
2005 <            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
2006 <        }
1999 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2000 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2001 >        final Callable<Boolean> complete1 = failFirst ?
2002 >            () -> fst.completeExceptionally(ex) :
2003 >            () -> fst.complete(v1);
2004 >        final Callable<Boolean> complete2 = failFirst ?
2005 >            () -> snd.complete(v1) :
2006 >            () -> snd.completeExceptionally(ex);
2007  
2008 <        checkCompletedWithWrappedException(h, ex);
2009 <        r.assertNotInvoked();
2010 <        checkCompletedNormally(fFirst ? f : g, v1);
2011 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
2008 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2009 >        assertTrue(complete1.call());
2010 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2011 >        checkIncomplete(h1);
2012 >        checkIncomplete(h2);
2013 >        assertTrue(complete2.call());
2014 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2015 >
2016 >        checkCompletedWithWrappedException(h1, ex);
2017 >        checkCompletedWithWrappedException(h2, ex);
2018 >        checkCompletedWithWrappedException(h3, ex);
2019 >        r1.assertNotInvoked();
2020 >        r2.assertNotInvoked();
2021 >        r3.assertNotInvoked();
2022 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2023 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
2024      }}
2025  
2026      /**
2027       * runAfterBoth result completes exceptionally if either source cancelled
2028       */
2029 <    public void testRunAfterBoth_sourceCancelled() {
2029 >    public void testRunAfterBoth_sourceCancelled() throws Throwable {
2030          for (ExecutionMode m : ExecutionMode.values())
2031          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1915        for (boolean createIncomplete : new boolean[] { true, false })
2032          for (boolean fFirst : new boolean[] { true, false })
2033 +        for (boolean failFirst : new boolean[] { true, false })
2034          for (Integer v1 : new Integer[] { 1, null })
2035      {
2036          final CompletableFuture<Integer> f = new CompletableFuture<>();
2037          final CompletableFuture<Integer> g = new CompletableFuture<>();
2038 <        final Noop r = new Noop(m);
2038 >        final Noop r1 = new Noop(m);
2039 >        final Noop r2 = new Noop(m);
2040 >        final Noop r3 = new Noop(m);
2041  
2042 <        assertTrue((fFirst ? f : g).complete(v1));
2043 <        if (!createIncomplete)
2044 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2045 <        final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
2046 <        if (createIncomplete) {
2047 <            checkIncomplete(h);
2048 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
2049 <        }
2042 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2043 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2044 >        final Callable<Boolean> complete1 = failFirst ?
2045 >            () -> fst.cancel(mayInterruptIfRunning) :
2046 >            () -> fst.complete(v1);
2047 >        final Callable<Boolean> complete2 = failFirst ?
2048 >            () -> snd.complete(v1) :
2049 >            () -> snd.cancel(mayInterruptIfRunning);
2050  
2051 <        checkCompletedWithWrappedCancellationException(h);
2052 <        checkCancelled(!fFirst ? f : g);
2053 <        r.assertNotInvoked();
2054 <        checkCompletedNormally(fFirst ? f : g, v1);
2051 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2052 >        assertTrue(complete1.call());
2053 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2054 >        checkIncomplete(h1);
2055 >        checkIncomplete(h2);
2056 >        assertTrue(complete2.call());
2057 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2058 >
2059 >        checkCompletedWithWrappedCancellationException(h1);
2060 >        checkCompletedWithWrappedCancellationException(h2);
2061 >        checkCompletedWithWrappedCancellationException(h3);
2062 >        r1.assertNotInvoked();
2063 >        r2.assertNotInvoked();
2064 >        r3.assertNotInvoked();
2065 >        checkCompletedNormally(failFirst ? snd : fst, v1);
2066 >        checkCancelled(failFirst ? fst : snd);
2067      }}
2068  
2069      /**
# Line 1948 | Line 2079 | public class CompletableFutureTest exten
2079          final CompletableFuture<Integer> g = new CompletableFuture<>();
2080          final FailingRunnable r1 = new FailingRunnable(m);
2081          final FailingRunnable r2 = new FailingRunnable(m);
2082 +        final FailingRunnable r3 = new FailingRunnable(m);
2083  
2084 <        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2085 <        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
2086 <        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
2087 <        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2084 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
2085 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
2086 >        final Integer w1 =  fFirst ? v1 : v2;
2087 >        final Integer w2 = !fFirst ? v1 : v2;
2088 >
2089 >        final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
2090 >        assertTrue(fst.complete(w1));
2091 >        final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
2092 >        assertTrue(snd.complete(w2));
2093 >        final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3);
2094  
2095          checkCompletedWithWrappedCFException(h1);
2096          checkCompletedWithWrappedCFException(h2);
2097 +        checkCompletedWithWrappedCFException(h3);
2098 +        r1.assertInvoked();
2099 +        r2.assertInvoked();
2100 +        r3.assertInvoked();
2101          checkCompletedNormally(f, v1);
2102          checkCompletedNormally(g, v2);
2103      }}
# Line 2867 | Line 3009 | public class CompletableFutureTest exten
3009       * when all components complete normally
3010       */
3011      public void testAllOf_normal() throws Exception {
3012 <        for (int k = 1; k < 20; ++k) {
3012 >        for (int k = 1; k < 10; k++) {
3013              CompletableFuture<Integer>[] fs
3014                  = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3015 <            for (int i = 0; i < k; ++i)
3015 >            for (int i = 0; i < k; i++)
3016                  fs[i] = new CompletableFuture<>();
3017              CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3018 <            for (int i = 0; i < k; ++i) {
3018 >            for (int i = 0; i < k; i++) {
3019                  checkIncomplete(f);
3020                  checkIncomplete(CompletableFuture.allOf(fs));
3021                  fs[i].complete(one);
# Line 2884 | Line 3026 | public class CompletableFutureTest exten
3026      }
3027  
3028      public void testAllOf_backwards() 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 = k - 1; i >= 0; i--) {
# Line 2900 | Line 3042 | public class CompletableFutureTest exten
3042          }
3043      }
3044  
3045 +    public void testAllOf_exceptional() throws Exception {
3046 +        for (int k = 1; k < 10; k++) {
3047 +            CompletableFuture<Integer>[] fs
3048 +                = (CompletableFuture<Integer>[]) new CompletableFuture[k];
3049 +            CFException ex = new CFException();
3050 +            for (int i = 0; i < k; i++)
3051 +                fs[i] = new CompletableFuture<>();
3052 +            CompletableFuture<Void> f = CompletableFuture.allOf(fs);
3053 +            for (int i = 0; i < k; i++) {
3054 +                checkIncomplete(f);
3055 +                checkIncomplete(CompletableFuture.allOf(fs));
3056 +                if (i != k/2) {
3057 +                    fs[i].complete(i);
3058 +                    checkCompletedNormally(fs[i], i);
3059 +                } else {
3060 +                    fs[i].completeExceptionally(ex);
3061 +                    checkCompletedExceptionally(fs[i], ex);
3062 +                }
3063 +            }
3064 +            checkCompletedWithWrappedException(f, ex);
3065 +            checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex);
3066 +        }
3067 +    }
3068 +
3069      /**
3070       * anyOf(no component futures) returns an incomplete future
3071       */
3072      public void testAnyOf_empty() throws Exception {
3073 +        for (Integer v1 : new Integer[] { 1, null })
3074 +    {
3075          CompletableFuture<Object> f = CompletableFuture.anyOf();
3076          checkIncomplete(f);
3077 <    }
3077 >
3078 >        f.complete(v1);
3079 >        checkCompletedNormally(f, v1);
3080 >    }}
3081  
3082      /**
3083       * anyOf returns a future completed normally with a value when
3084       * a component future does
3085       */
3086      public void testAnyOf_normal() throws Exception {
3087 <        for (int k = 0; k < 10; ++k) {
3087 >        for (int k = 0; k < 10; k++) {
3088              CompletableFuture[] fs = new CompletableFuture[k];
3089 <            for (int i = 0; i < k; ++i)
3089 >            for (int i = 0; i < k; i++)
3090                  fs[i] = new CompletableFuture<>();
3091              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3092              checkIncomplete(f);
3093 <            for (int i = 0; i < k; ++i) {
3094 <                fs[i].complete(one);
3095 <                checkCompletedNormally(f, one);
3096 <                checkCompletedNormally(CompletableFuture.anyOf(fs), one);
3093 >            for (int i = 0; i < k; i++) {
3094 >                fs[i].complete(i);
3095 >                checkCompletedNormally(f, 0);
3096 >                int x = (int) CompletableFuture.anyOf(fs).join();
3097 >                assertTrue(0 <= x && x <= i);
3098 >            }
3099 >        }
3100 >    }
3101 >    public void testAnyOf_normal_backwards() throws Exception {
3102 >        for (int k = 0; k < 10; k++) {
3103 >            CompletableFuture[] fs = new CompletableFuture[k];
3104 >            for (int i = 0; i < k; i++)
3105 >                fs[i] = new CompletableFuture<>();
3106 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3107 >            checkIncomplete(f);
3108 >            for (int i = k - 1; i >= 0; i--) {
3109 >                fs[i].complete(i);
3110 >                checkCompletedNormally(f, k - 1);
3111 >                int x = (int) CompletableFuture.anyOf(fs).join();
3112 >                assertTrue(i <= x && x <= k - 1);
3113              }
3114          }
3115      }
# Line 2931 | Line 3118 | public class CompletableFutureTest exten
3118       * anyOf result completes exceptionally when any component does.
3119       */
3120      public void testAnyOf_exceptional() throws Exception {
3121 <        for (int k = 0; k < 10; ++k) {
3121 >        for (int k = 0; k < 10; k++) {
3122              CompletableFuture[] fs = new CompletableFuture[k];
3123 <            for (int i = 0; i < k; ++i)
3123 >            CFException[] exs = new CFException[k];
3124 >            for (int i = 0; i < k; i++) {
3125                  fs[i] = new CompletableFuture<>();
3126 +                exs[i] = new CFException();
3127 +            }
3128              CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3129              checkIncomplete(f);
3130 <            for (int i = 0; i < k; ++i) {
3131 <                fs[i].completeExceptionally(new CFException());
3132 <                checkCompletedWithWrappedCFException(f);
3130 >            for (int i = 0; i < k; i++) {
3131 >                fs[i].completeExceptionally(exs[i]);
3132 >                checkCompletedWithWrappedException(f, exs[0]);
3133 >                checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3134 >            }
3135 >        }
3136 >    }
3137 >
3138 >    public void testAnyOf_exceptional_backwards() throws Exception {
3139 >        for (int k = 0; k < 10; k++) {
3140 >            CompletableFuture[] fs = new CompletableFuture[k];
3141 >            CFException[] exs = new CFException[k];
3142 >            for (int i = 0; i < k; i++) {
3143 >                fs[i] = new CompletableFuture<>();
3144 >                exs[i] = new CFException();
3145 >            }
3146 >            CompletableFuture<Object> f = CompletableFuture.anyOf(fs);
3147 >            checkIncomplete(f);
3148 >            for (int i = k - 1; i >= 0; i--) {
3149 >                fs[i].completeExceptionally(exs[i]);
3150 >                checkCompletedWithWrappedException(f, exs[k - 1]);
3151                  checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs));
3152              }
3153          }
# Line 2958 | Line 3166 | public class CompletableFutureTest exten
3166          Runnable[] throwingActions = {
3167              () -> CompletableFuture.supplyAsync(null),
3168              () -> CompletableFuture.supplyAsync(null, exec),
3169 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3169 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3170  
3171              () -> CompletableFuture.runAsync(null),
3172              () -> CompletableFuture.runAsync(null, exec),
# Line 3063 | Line 3271 | public class CompletableFutureTest exten
3271          assertSame(f, f.toCompletableFuture());
3272      }
3273  
3274 < //     public void testRunAfterEither_resultDeterminedAtTimeOfCreation() {
3275 < //         for (ExecutionMode m : ExecutionMode.values())
3276 < //         for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3277 < //         for (Integer v1 : new Integer[] { 1, null })
3278 < //     {
3279 < //         final CompletableFuture<Integer> f = new CompletableFuture<>();
3280 < //         final CompletableFuture<Integer> g = new CompletableFuture<>();
3281 < //         final Noop[] rs = new Noop[2];
3282 < //         for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
3283 < //         f.complete(v1);
3284 < //         final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
3285 < //         final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
3286 < //         assertTrue(g.cancel(mayInterruptIfRunning));
3287 < //         checkCompletedNormally(h0, null);
3288 < //         checkCompletedNormally(h1, null);
3289 < //         for (Noop r : rs) r.assertInvoked();
3290 < //     }}
3274 >    //--- tests of implementation details; not part of official tck ---
3275 >
3276 >    Object resultOf(CompletableFuture<?> f) {
3277 >        try {
3278 >            java.lang.reflect.Field resultField
3279 >                = CompletableFuture.class.getDeclaredField("result");
3280 >            resultField.setAccessible(true);
3281 >            return resultField.get(f);
3282 >        } catch (Throwable t) { throw new AssertionError(t); }
3283 >    }
3284 >
3285 >    public void testExceptionPropagationReusesResultObject() {
3286 >        if (!testImplementationDetails) return;
3287 >        for (ExecutionMode m : ExecutionMode.values())
3288 >    {
3289 >        final CFException ex = new CFException();
3290 >        final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42);
3291 >        final CompletableFuture<Integer> incomplete = new CompletableFuture<>();
3292 >
3293 >        List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs
3294 >            = new ArrayList<>();
3295 >
3296 >        funs.add((y) -> m.thenRun(y, new Noop(m)));
3297 >        funs.add((y) -> m.thenAccept(y, new NoopConsumer(m)));
3298 >        funs.add((y) -> m.thenApply(y, new IncFunction(m)));
3299 >
3300 >        funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m)));
3301 >        funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m)));
3302 >        funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3303 >
3304 >        funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3305 >        funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3306 >        funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3307 >
3308 >        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3309 >
3310 >        funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3311 >
3312 >        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3313 >        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3314 >
3315 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3316 >                 fun : funs) {
3317 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3318 >            f.completeExceptionally(ex);
3319 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3320 >            checkCompletedWithWrappedException(src, ex);
3321 >            CompletableFuture<?> dep = fun.apply(src);
3322 >            checkCompletedWithWrappedException(dep, ex);
3323 >            assertSame(resultOf(src), resultOf(dep));
3324 >        }
3325 >
3326 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3327 >                 fun : funs) {
3328 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3329 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3330 >            CompletableFuture<?> dep = fun.apply(src);
3331 >            f.completeExceptionally(ex);
3332 >            checkCompletedWithWrappedException(src, ex);
3333 >            checkCompletedWithWrappedException(dep, ex);
3334 >            assertSame(resultOf(src), resultOf(dep));
3335 >        }
3336 >
3337 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3338 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3339 >                 fun : funs) {
3340 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3341 >            f.cancel(mayInterruptIfRunning);
3342 >            checkCancelled(f);
3343 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3344 >            checkCompletedWithWrappedCancellationException(src);
3345 >            CompletableFuture<?> dep = fun.apply(src);
3346 >            checkCompletedWithWrappedCancellationException(dep);
3347 >            assertSame(resultOf(src), resultOf(dep));
3348 >        }
3349 >
3350 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3351 >        for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3352 >                 fun : funs) {
3353 >            CompletableFuture<Integer> f = new CompletableFuture<>();
3354 >            CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m));
3355 >            CompletableFuture<?> dep = fun.apply(src);
3356 >            f.cancel(mayInterruptIfRunning);
3357 >            checkCancelled(f);
3358 >            checkCompletedWithWrappedCancellationException(src);
3359 >            checkCompletedWithWrappedCancellationException(dep);
3360 >            assertSame(resultOf(src), resultOf(dep));
3361 >        }
3362 >    }}
3363  
3364   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines