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.59 by jsr166, Tue Jun 3 03:54:14 2014 UTC vs.
Revision 1.61 by jsr166, Wed Jun 4 04:34:49 2014 UTC

# Line 320 | Line 320 | public class CompletableFutureTest exten
320          checkCompletedNormally(f, "test");
321      }
322  
323    // Choose non-commutative actions for better coverage
324
325    // A non-commutative function that handles and produces null values as well.
326    static Integer subtract(Integer x, Integer y) {
327        return (x == null && y == null) ? null :
328            ((x == null) ? 42 : x.intValue())
329            - ((y == null) ? 99 : y.intValue());
330    }
331
332    // A function that handles and produces null values as well.
333    static Integer inc(Integer x) {
334        return (x == null) ? null : x + 1;
335    }
336
323      static final class IntegerSupplier implements Supplier<Integer> {
324          final ExecutionMode m;
325          int invocationCount = 0;
# Line 348 | Line 334 | public class CompletableFutureTest exten
334              return value;
335          }
336      }
337 <        
337 >
338 >    // A function that handles and produces null values as well.
339 >    static Integer inc(Integer x) {
340 >        return (x == null) ? null : x + 1;
341 >    }
342 >
343      static final class IncAction implements Consumer<Integer> {
344          int invocationCount = 0;
345          Integer value;
# Line 368 | Line 359 | public class CompletableFutureTest exten
359              return value = inc(x);
360          }
361      }
362 +
363 +    // Choose non-commutative actions for better coverage
364 +    // A non-commutative function that handles and produces null values as well.
365 +    static Integer subtract(Integer x, Integer y) {
366 +        return (x == null && y == null) ? null :
367 +            ((x == null) ? 42 : x.intValue())
368 +            - ((y == null) ? 99 : y.intValue());
369 +    }
370 +
371      static final class SubtractAction implements BiConsumer<Integer, Integer> {
372          final ExecutionMode m;
373          int invocationCount = 0;
# Line 392 | Line 392 | public class CompletableFutureTest exten
392              return value = subtract(x, y);
393          }
394      }
395 +
396      static final class Noop implements Runnable {
397          final ExecutionMode m;
398          int invocationCount = 0;
# Line 505 | Line 506 | public class CompletableFutureTest exten
506  
507      /**
508       * Permits the testing of parallel code for the 3 different
509 <     * execution modes without repeating all the testing code.
509 >     * execution modes without copy/pasting all the test methods.
510       */
511      enum ExecutionMode {
512          DEFAULT {
513              public void checkExecutionMode() {
514 +                assertFalse(ThreadExecutor.startedCurrentThread());
515                  assertNull(ForkJoinTask.getPool());
516              }
517              public CompletableFuture<Void> runAsync(Runnable a) {
# Line 1273 | Line 1275 | public class CompletableFutureTest exten
1275      }}
1276  
1277      /**
1278 <     * thenAccept result completes exceptionally if action does
1278 >     * thenAccept result completes exceptionally if source cancelled
1279       */
1280 <    public void testThenAccept_actionFailed() {
1280 >    public void testThenAccept_sourceCancelled() {
1281          for (ExecutionMode m : ExecutionMode.values())
1282          for (boolean createIncomplete : new boolean[] { true, false })
1283 <        for (Integer v1 : new Integer[] { 1, null })
1283 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1284      {
1285          final CompletableFuture<Integer> f = new CompletableFuture<>();
1286 <        final FailingConsumer r = new FailingConsumer(m);
1287 <        if (!createIncomplete) f.complete(v1);
1286 >        final IncAction r = new IncAction();
1287 >        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1288          final CompletableFuture<Void> g = m.thenAccept(f, r);
1289          if (createIncomplete) {
1290              checkIncomplete(g);
1291 <            f.complete(v1);
1291 >            assertTrue(f.cancel(mayInterruptIfRunning));
1292          }
1293  
1294 <        checkCompletedWithWrappedCFException(g);
1295 <        checkCompletedNormally(f, v1);
1294 >        checkCompletedWithWrappedCancellationException(g);
1295 >        checkCancelled(f);
1296 >        assertEquals(0, r.invocationCount);
1297      }}
1298  
1299      /**
1300 <     * thenAccept result completes exceptionally if source cancelled
1300 >     * thenAccept result completes exceptionally if action does
1301       */
1302 <    public void testThenAccept_sourceCancelled() {
1302 >    public void testThenAccept_actionFailed() {
1303          for (ExecutionMode m : ExecutionMode.values())
1304          for (boolean createIncomplete : new boolean[] { true, false })
1305 <        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1305 >        for (Integer v1 : new Integer[] { 1, null })
1306      {
1307          final CompletableFuture<Integer> f = new CompletableFuture<>();
1308 <        final IncAction r = new IncAction();
1309 <        if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1308 >        final FailingConsumer r = new FailingConsumer(m);
1309 >        if (!createIncomplete) f.complete(v1);
1310          final CompletableFuture<Void> g = m.thenAccept(f, r);
1311          if (createIncomplete) {
1312              checkIncomplete(g);
1313 <            assertTrue(f.cancel(mayInterruptIfRunning));
1313 >            f.complete(v1);
1314          }
1315  
1316 <        checkCompletedWithWrappedCancellationException(g);
1317 <        checkCancelled(f);
1315 <        assertEquals(0, r.invocationCount);
1316 >        checkCompletedWithWrappedCFException(g);
1317 >        checkCompletedNormally(f, v1);
1318      }}
1319  
1320      /**
# Line 1377 | Line 1379 | public class CompletableFutureTest exten
1379      }}
1380  
1381      /**
1380     * thenCombine result completes exceptionally if action does
1381     */
1382    public void testThenCombine_actionFailed() {
1383        for (ExecutionMode m : ExecutionMode.values())
1384        for (boolean fFirst : new boolean[] { true, false })
1385        for (Integer v1 : new Integer[] { 1, null })
1386        for (Integer v2 : new Integer[] { 2, null })
1387    {
1388        final CompletableFuture<Integer> f = new CompletableFuture<>();
1389        final CompletableFuture<Integer> g = new CompletableFuture<>();
1390        final FailingBiFunction r = new FailingBiFunction(m);
1391        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1392
1393        if (fFirst) {
1394            f.complete(v1);
1395            g.complete(v2);
1396        } else {
1397            g.complete(v2);
1398            f.complete(v1);
1399        }
1400
1401        checkCompletedWithWrappedCFException(h);
1402        checkCompletedNormally(f, v1);
1403        checkCompletedNormally(g, v2);
1404    }}
1405
1406    /**
1382       * thenCombine result completes exceptionally if either source cancelled
1383       */
1384      public void testThenCombine_sourceCancelled() {
# Line 1433 | Line 1408 | public class CompletableFutureTest exten
1408      }}
1409  
1410      /**
1411 +     * thenCombine result completes exceptionally if action does
1412 +     */
1413 +    public void testThenCombine_actionFailed() {
1414 +        for (ExecutionMode m : ExecutionMode.values())
1415 +        for (boolean fFirst : new boolean[] { true, false })
1416 +        for (Integer v1 : new Integer[] { 1, null })
1417 +        for (Integer v2 : new Integer[] { 2, null })
1418 +    {
1419 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1420 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1421 +        final FailingBiFunction r = new FailingBiFunction(m);
1422 +        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1423 +
1424 +        if (fFirst) {
1425 +            f.complete(v1);
1426 +            g.complete(v2);
1427 +        } else {
1428 +            g.complete(v2);
1429 +            f.complete(v1);
1430 +        }
1431 +
1432 +        checkCompletedWithWrappedCFException(h);
1433 +        checkCompletedNormally(f, v1);
1434 +        checkCompletedNormally(g, v2);
1435 +    }}
1436 +
1437 +    /**
1438       * thenAcceptBoth result completes normally after normal
1439       * completion of sources
1440       */
# Line 1494 | Line 1496 | public class CompletableFutureTest exten
1496      }}
1497  
1498      /**
1497     * thenAcceptBoth result completes exceptionally if action does
1498     */
1499    public void testThenAcceptBoth_actionFailed() {
1500        for (ExecutionMode m : ExecutionMode.values())
1501        for (boolean fFirst : new boolean[] { true, false })
1502        for (Integer v1 : new Integer[] { 1, null })
1503        for (Integer v2 : new Integer[] { 2, null })
1504    {
1505        final CompletableFuture<Integer> f = new CompletableFuture<>();
1506        final CompletableFuture<Integer> g = new CompletableFuture<>();
1507        final FailingBiConsumer r = new FailingBiConsumer(m);
1508        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1509
1510        if (fFirst) {
1511            f.complete(v1);
1512            g.complete(v2);
1513        } else {
1514            g.complete(v2);
1515            f.complete(v1);
1516        }
1517
1518        checkCompletedWithWrappedCFException(h);
1519        checkCompletedNormally(f, v1);
1520        checkCompletedNormally(g, v2);
1521    }}
1522
1523    /**
1499       * thenAcceptBoth result completes exceptionally if either source cancelled
1500       */
1501      public void testThenAcceptBoth_sourceCancelled() {
# Line 1550 | Line 1525 | public class CompletableFutureTest exten
1525      }}
1526  
1527      /**
1528 +     * thenAcceptBoth result completes exceptionally if action does
1529 +     */
1530 +    public void testThenAcceptBoth_actionFailed() {
1531 +        for (ExecutionMode m : ExecutionMode.values())
1532 +        for (boolean fFirst : new boolean[] { true, false })
1533 +        for (Integer v1 : new Integer[] { 1, null })
1534 +        for (Integer v2 : new Integer[] { 2, null })
1535 +    {
1536 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1537 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1538 +        final FailingBiConsumer r = new FailingBiConsumer(m);
1539 +        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1540 +
1541 +        if (fFirst) {
1542 +            f.complete(v1);
1543 +            g.complete(v2);
1544 +        } else {
1545 +            g.complete(v2);
1546 +            f.complete(v1);
1547 +        }
1548 +
1549 +        checkCompletedWithWrappedCFException(h);
1550 +        checkCompletedNormally(f, v1);
1551 +        checkCompletedNormally(g, v2);
1552 +    }}
1553 +
1554 +    /**
1555       * runAfterBoth result completes normally after normal
1556       * completion of sources
1557       */
# Line 1611 | Line 1613 | public class CompletableFutureTest exten
1613      }}
1614  
1615      /**
1614     * runAfterBoth result completes exceptionally if action does
1615     */
1616    public void testRunAfterBoth_actionFailed() {
1617        for (ExecutionMode m : ExecutionMode.values())
1618        for (boolean fFirst : new boolean[] { true, false })
1619        for (Integer v1 : new Integer[] { 1, null })
1620        for (Integer v2 : new Integer[] { 2, null })
1621    {
1622        final CompletableFuture<Integer> f = new CompletableFuture<>();
1623        final CompletableFuture<Integer> g = new CompletableFuture<>();
1624        final FailingRunnable r = new FailingRunnable(m);
1625
1626        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1627        if (fFirst) {
1628            f.complete(v1);
1629            g.complete(v2);
1630        } else {
1631            g.complete(v2);
1632            f.complete(v1);
1633        }
1634        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1635
1636        checkCompletedWithWrappedCFException(h1);
1637        checkCompletedWithWrappedCFException(h2);
1638        checkCompletedNormally(f, v1);
1639        checkCompletedNormally(g, v2);
1640    }}
1641
1642    /**
1616       * runAfterBoth result completes exceptionally if either source cancelled
1617       */
1618      public void testRunAfterBoth_sourceCancelled() {
# Line 1670 | Line 1643 | public class CompletableFutureTest exten
1643      }}
1644  
1645      /**
1646 +     * runAfterBoth result completes exceptionally if action does
1647 +     */
1648 +    public void testRunAfterBoth_actionFailed() {
1649 +        for (ExecutionMode m : ExecutionMode.values())
1650 +        for (boolean fFirst : new boolean[] { true, false })
1651 +        for (Integer v1 : new Integer[] { 1, null })
1652 +        for (Integer v2 : new Integer[] { 2, null })
1653 +    {
1654 +        final CompletableFuture<Integer> f = new CompletableFuture<>();
1655 +        final CompletableFuture<Integer> g = new CompletableFuture<>();
1656 +        final FailingRunnable r = new FailingRunnable(m);
1657 +
1658 +        CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r);
1659 +        if (fFirst) {
1660 +            f.complete(v1);
1661 +            g.complete(v2);
1662 +        } else {
1663 +            g.complete(v2);
1664 +            f.complete(v1);
1665 +        }
1666 +        CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r);
1667 +
1668 +        checkCompletedWithWrappedCFException(h1);
1669 +        checkCompletedWithWrappedCFException(h2);
1670 +        checkCompletedNormally(f, v1);
1671 +        checkCompletedNormally(g, v2);
1672 +    }}
1673 +
1674 +    /**
1675       * applyToEither result completes normally after normal completion
1676       * of either source
1677       */

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines