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.100 by jsr166, Thu Jan 15 18:34:19 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.atomic.AtomicInteger;
24 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;
25   import java.util.function.BiConsumer;
30 import java.util.function.Function;
26   import java.util.function.BiFunction;
27 + import java.util.function.Consumer;
28 + import java.util.function.Function;
29 + import java.util.function.Supplier;
30 +
31 + import junit.framework.Test;
32 + import junit.framework.TestSuite;
33  
34   public class CompletableFutureTest extends JSR166TestCase {
35  
# Line 57 | Line 58 | public class CompletableFutureTest exten
58      }
59  
60      <T> void checkCompletedNormally(CompletableFuture<T> f, T value) {
61 <        try {
62 <            assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS));
62 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
61 >        checkTimedGet(f, value);
62 >
63          try {
64              assertEquals(value, f.join());
65          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 76 | Line 76 | public class CompletableFutureTest exten
76      }
77  
78      void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
79 +        long startTime = System.nanoTime();
80 +        long timeoutMillis = LONG_DELAY_MS;
81          try {
82 <            f.get(LONG_DELAY_MS, MILLISECONDS);
82 >            f.get(timeoutMillis, MILLISECONDS);
83              shouldThrow();
84          } catch (ExecutionException success) {
85              assertTrue(success.getCause() instanceof CFException);
86          } catch (Throwable fail) { threadUnexpectedException(fail); }
87 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
88 +
89          try {
90              f.join();
91              shouldThrow();
# Line 107 | Line 111 | public class CompletableFutureTest exten
111  
112      <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
113                                                        Throwable ex) {
114 +        long startTime = System.nanoTime();
115 +        long timeoutMillis = LONG_DELAY_MS;
116          try {
117 <            f.get(LONG_DELAY_MS, MILLISECONDS);
117 >            f.get(timeoutMillis, MILLISECONDS);
118              shouldThrow();
119          } catch (ExecutionException success) {
120              assertSame(ex, success.getCause());
121          } catch (Throwable fail) { threadUnexpectedException(fail); }
122 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
123 +
124          try {
125              f.join();
126              shouldThrow();
# Line 158 | Line 166 | public class CompletableFutureTest exten
166      }
167  
168      void checkCancelled(CompletableFuture<?> f) {
169 +        long startTime = System.nanoTime();
170 +        long timeoutMillis = LONG_DELAY_MS;
171          try {
172 <            f.get(LONG_DELAY_MS, MILLISECONDS);
172 >            f.get(timeoutMillis, MILLISECONDS);
173              shouldThrow();
174          } catch (CancellationException success) {
175          } catch (Throwable fail) { threadUnexpectedException(fail); }
176 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
177 +
178          try {
179              f.join();
180              shouldThrow();
# Line 183 | Line 195 | public class CompletableFutureTest exten
195      }
196  
197      void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
198 +        long startTime = System.nanoTime();
199 +        long timeoutMillis = LONG_DELAY_MS;
200          try {
201 <            f.get(LONG_DELAY_MS, MILLISECONDS);
201 >            f.get(timeoutMillis, MILLISECONDS);
202              shouldThrow();
203          } catch (ExecutionException success) {
204              assertTrue(success.getCause() instanceof CancellationException);
205          } catch (Throwable fail) { threadUnexpectedException(fail); }
206 +        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
207 +
208          try {
209              f.join();
210              shouldThrow();
# Line 530 | Line 546 | public class CompletableFutureTest exten
546          }
547      }
548  
533
549      class CompletableFutureInc extends CheckedIntegerAction
550          implements Function<Integer, CompletableFuture<Integer>>
551      {
# 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 2906 | Line 2993 | public class CompletableFutureTest exten
2993          checkCancelled(f);
2994      }}
2995  
2996 +    /**
2997 +     * thenCompose result completes exceptionally if the result of the action does
2998 +     */
2999 +    public void testThenCompose_actionReturnsFailingFuture() {
3000 +        for (ExecutionMode m : ExecutionMode.values())
3001 +        for (int order = 0; order < 6; order++)
3002 +        for (Integer v1 : new Integer[] { 1, null })
3003 +    {
3004 +        final CFException ex = new CFException();
3005 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
3006 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
3007 +        final CompletableFuture<Integer> h;
3008 +        // Test all permutations of orders
3009 +        switch (order) {
3010 +        case 0:
3011 +            assertTrue(f.complete(v1));
3012 +            assertTrue(g.completeExceptionally(ex));
3013 +            h = m.thenCompose(f, (x -> g));
3014 +            break;
3015 +        case 1:
3016 +            assertTrue(f.complete(v1));
3017 +            h = m.thenCompose(f, (x -> g));
3018 +            assertTrue(g.completeExceptionally(ex));
3019 +            break;
3020 +        case 2:
3021 +            assertTrue(g.completeExceptionally(ex));
3022 +            assertTrue(f.complete(v1));
3023 +            h = m.thenCompose(f, (x -> g));
3024 +            break;
3025 +        case 3:
3026 +            assertTrue(g.completeExceptionally(ex));
3027 +            h = m.thenCompose(f, (x -> g));
3028 +            assertTrue(f.complete(v1));
3029 +            break;
3030 +        case 4:
3031 +            h = m.thenCompose(f, (x -> g));
3032 +            assertTrue(f.complete(v1));
3033 +            assertTrue(g.completeExceptionally(ex));
3034 +            break;
3035 +        case 5:
3036 +            h = m.thenCompose(f, (x -> g));
3037 +            assertTrue(f.complete(v1));
3038 +            assertTrue(g.completeExceptionally(ex));
3039 +            break;
3040 +        default: throw new AssertionError();
3041 +        }
3042 +
3043 +        checkCompletedExceptionally(g, ex);
3044 +        checkCompletedWithWrappedException(h, ex);
3045 +        checkCompletedNormally(f, v1);
3046 +    }}
3047 +
3048      // other static methods
3049  
3050      /**
# Line 3079 | Line 3218 | public class CompletableFutureTest exten
3218          Runnable[] throwingActions = {
3219              () -> CompletableFuture.supplyAsync(null),
3220              () -> CompletableFuture.supplyAsync(null, exec),
3221 <            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null),
3221 >            () -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null),
3222  
3223              () -> CompletableFuture.runAsync(null),
3224              () -> CompletableFuture.runAsync(null, exec),

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines