284 |
|
public void testGetNumberOfDependents() { |
285 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
286 |
|
assertEquals(0, f.getNumberOfDependents()); |
287 |
< |
CompletableFuture g = f.thenRun(new Noop()); |
287 |
> |
CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
288 |
|
assertEquals(1, f.getNumberOfDependents()); |
289 |
|
assertEquals(0, g.getNumberOfDependents()); |
290 |
< |
CompletableFuture h = f.thenRun(new Noop()); |
290 |
> |
CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
291 |
|
assertEquals(2, f.getNumberOfDependents()); |
292 |
|
f.complete(1); |
293 |
|
checkCompletedNormally(g, null); |
375 |
|
} |
376 |
|
} |
377 |
|
static final class Noop implements Runnable { |
378 |
+ |
final ExecutionMode m; |
379 |
|
int invocationCount = 0; |
380 |
+ |
Noop(ExecutionMode m) { this.m = m; } |
381 |
|
public void run() { |
382 |
+ |
m.checkExecutionMode(); |
383 |
|
invocationCount++; |
384 |
|
} |
385 |
|
} |
912 |
|
* runAsync completes after running Runnable |
913 |
|
*/ |
914 |
|
public void testRunAsync() { |
915 |
< |
Noop r = new Noop(); |
915 |
> |
Noop r = new Noop(ExecutionMode.ASYNC); |
916 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
917 |
|
assertNull(f.join()); |
918 |
|
assertEquals(1, r.invocationCount); |
923 |
|
* runAsync with executor completes after running Runnable |
924 |
|
*/ |
925 |
|
public void testRunAsync2() { |
926 |
< |
Noop r = new Noop(); |
926 |
> |
Noop r = new Noop(ExecutionMode.EXECUTOR); |
927 |
|
ThreadExecutor exec = new ThreadExecutor(); |
928 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
929 |
|
assertNull(f.join()); |
983 |
|
for (Integer v1 : new Integer[] { 1, null }) |
984 |
|
{ |
985 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
986 |
< |
final Noop r = new Noop(); |
986 |
> |
final Noop r = new Noop(m); |
987 |
|
if (!createIncomplete) f.complete(v1); |
988 |
|
final CompletableFuture<Void> g = m.thenRun(f, r); |
989 |
|
if (createIncomplete) { |
1006 |
|
{ |
1007 |
|
final CFException ex = new CFException(); |
1008 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1009 |
< |
final Noop r = new Noop(); |
1009 |
> |
final Noop r = new Noop(m); |
1010 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1011 |
|
final CompletableFuture<Void> g = m.thenRun(f, r); |
1012 |
|
if (createIncomplete) { |
1028 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1029 |
|
{ |
1030 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1031 |
< |
final Noop r = new Noop(); |
1031 |
> |
final Noop r = new Noop(m); |
1032 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1033 |
|
final CompletableFuture<Void> g = f.thenRun(r); |
1034 |
|
if (createIncomplete) { |
1477 |
|
* runAfterBoth result completes normally after normal |
1478 |
|
* completion of sources |
1479 |
|
*/ |
1480 |
< |
public void testRunAfterBoth_normalCompletion1() { |
1478 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1479 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1480 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1481 |
< |
{ |
1482 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1483 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1484 |
< |
final Noop r = new Noop(); |
1485 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1486 |
< |
|
1487 |
< |
f.complete(v1); |
1488 |
< |
checkIncomplete(h); |
1489 |
< |
assertEquals(0, r.invocationCount); |
1490 |
< |
g.complete(v2); |
1491 |
< |
|
1492 |
< |
checkCompletedNormally(h, null); |
1493 |
< |
assertEquals(1, r.invocationCount); |
1494 |
< |
checkCompletedNormally(f, v1); |
1495 |
< |
checkCompletedNormally(g, v2); |
1496 |
< |
}} |
1497 |
< |
|
1498 |
< |
public void testRunAfterBoth_normalCompletion2() { |
1499 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1500 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1501 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1502 |
< |
{ |
1503 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1504 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1505 |
< |
final Noop r = new Noop(); |
1506 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1507 |
< |
|
1508 |
< |
g.complete(v2); |
1509 |
< |
checkIncomplete(h); |
1510 |
< |
assertEquals(0, r.invocationCount); |
1511 |
< |
f.complete(v1); |
1512 |
< |
|
1513 |
< |
checkCompletedNormally(h, null); |
1514 |
< |
assertEquals(1, r.invocationCount); |
1515 |
< |
checkCompletedNormally(f, v1); |
1516 |
< |
checkCompletedNormally(g, v2); |
1517 |
< |
}} |
1518 |
< |
|
1519 |
< |
public void testRunAfterBoth_normalCompletion3() { |
1520 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1521 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1522 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1523 |
< |
{ |
1524 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1525 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1526 |
< |
final Noop r = new Noop(); |
1527 |
< |
|
1528 |
< |
g.complete(v2); |
1529 |
< |
f.complete(v1); |
1530 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1531 |
< |
|
1532 |
< |
checkCompletedNormally(h, null); |
1533 |
< |
assertEquals(1, r.invocationCount); |
1534 |
< |
checkCompletedNormally(f, v1); |
1535 |
< |
checkCompletedNormally(g, v2); |
1536 |
< |
}} |
1537 |
< |
|
1538 |
< |
public void testRunAfterBoth_normalCompletion4() { |
1480 |
> |
public void testRunAfterBoth_normalCompletion() { |
1481 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1482 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1483 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1484 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1485 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1486 |
|
{ |
1487 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1488 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1489 |
< |
final Noop r = new Noop(); |
1489 |
> |
final Noop r = new Noop(m); |
1490 |
|
|
1491 |
< |
f.complete(v1); |
1492 |
< |
g.complete(v2); |
1491 |
> |
if (fFirst) f.complete(v1); else g.complete(v2); |
1492 |
> |
if (!createIncomplete) |
1493 |
> |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1494 |
|
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1495 |
+ |
if (createIncomplete) { |
1496 |
+ |
checkIncomplete(h); |
1497 |
+ |
assertEquals(0, r.invocationCount); |
1498 |
+ |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1499 |
+ |
} |
1500 |
|
|
1501 |
|
checkCompletedNormally(h, null); |
1502 |
|
assertEquals(1, r.invocationCount); |
1508 |
|
* runAfterBoth result completes exceptionally after exceptional |
1509 |
|
* completion of either source |
1510 |
|
*/ |
1511 |
< |
public void testRunAfterBoth_exceptionalCompletion1() { |
1562 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1563 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1564 |
< |
{ |
1565 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1566 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1567 |
< |
final Noop r = new Noop(); |
1568 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1569 |
< |
final CFException ex = new CFException(); |
1570 |
< |
|
1571 |
< |
f.completeExceptionally(ex); |
1572 |
< |
checkIncomplete(h); |
1573 |
< |
g.complete(v1); |
1574 |
< |
|
1575 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1576 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1577 |
< |
assertEquals(0, r.invocationCount); |
1578 |
< |
checkCompletedNormally(g, v1); |
1579 |
< |
}} |
1580 |
< |
|
1581 |
< |
public void testRunAfterBoth_exceptionalCompletion2() { |
1582 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1583 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1584 |
< |
{ |
1585 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1586 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1587 |
< |
final Noop r = new Noop(); |
1588 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1589 |
< |
final CFException ex = new CFException(); |
1590 |
< |
|
1591 |
< |
g.completeExceptionally(ex); |
1592 |
< |
checkIncomplete(h); |
1593 |
< |
f.complete(v1); |
1594 |
< |
|
1595 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1596 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1597 |
< |
assertEquals(0, r.invocationCount); |
1598 |
< |
checkCompletedNormally(f, v1); |
1599 |
< |
}} |
1600 |
< |
|
1601 |
< |
public void testRunAfterBoth_exceptionalCompletion3() { |
1602 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1603 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1604 |
< |
{ |
1605 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1606 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1607 |
< |
final Noop r = new Noop(); |
1608 |
< |
final CFException ex = new CFException(); |
1609 |
< |
|
1610 |
< |
g.completeExceptionally(ex); |
1611 |
< |
f.complete(v1); |
1612 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1613 |
< |
|
1614 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1615 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1616 |
< |
assertEquals(0, r.invocationCount); |
1617 |
< |
checkCompletedNormally(f, v1); |
1618 |
< |
}} |
1619 |
< |
|
1620 |
< |
public void testRunAfterBoth_exceptionalCompletion4() { |
1511 |
> |
public void testRunAfterBoth_exceptionalCompletion() { |
1512 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1513 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1514 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1515 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1516 |
|
{ |
1517 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1518 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1626 |
– |
final Noop r = new Noop(); |
1519 |
|
final CFException ex = new CFException(); |
1520 |
+ |
final Noop r = new Noop(m); |
1521 |
|
|
1522 |
< |
f.completeExceptionally(ex); |
1523 |
< |
g.complete(v1); |
1522 |
> |
(fFirst ? f : g).complete(v1); |
1523 |
> |
if (!createIncomplete) |
1524 |
> |
(!fFirst ? f : g).completeExceptionally(ex); |
1525 |
|
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1526 |
+ |
if (createIncomplete) { |
1527 |
+ |
checkIncomplete(h); |
1528 |
+ |
(!fFirst ? f : g).completeExceptionally(ex); |
1529 |
+ |
} |
1530 |
|
|
1531 |
|
checkCompletedWithWrappedCFException(h, ex); |
1634 |
– |
checkCompletedWithWrappedCFException(f, ex); |
1532 |
|
assertEquals(0, r.invocationCount); |
1533 |
< |
checkCompletedNormally(g, v1); |
1533 |
> |
checkCompletedNormally(fFirst ? f : g, v1); |
1534 |
> |
checkCompletedWithWrappedCFException(!fFirst ? f : g, ex); |
1535 |
|
}} |
1536 |
|
|
1537 |
|
/** |
1538 |
|
* runAfterBoth result completes exceptionally if action does |
1539 |
|
*/ |
1540 |
< |
public void testRunAfterBoth_actionFailed1() { |
1643 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1644 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1645 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1646 |
< |
{ |
1647 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1648 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1649 |
< |
final FailingRunnable r = new FailingRunnable(); |
1650 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1651 |
< |
|
1652 |
< |
f.complete(v1); |
1653 |
< |
checkIncomplete(h); |
1654 |
< |
g.complete(v2); |
1655 |
< |
|
1656 |
< |
checkCompletedWithWrappedCFException(h); |
1657 |
< |
checkCompletedNormally(f, v1); |
1658 |
< |
checkCompletedNormally(g, v2); |
1659 |
< |
}} |
1660 |
< |
|
1661 |
< |
public void testRunAfterBoth_actionFailed2() { |
1540 |
> |
public void testRunAfterBoth_actionFailed() { |
1541 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1542 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1543 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1544 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1545 |
|
{ |
1546 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1547 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1548 |
|
final FailingRunnable r = new FailingRunnable(); |
1669 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1549 |
|
|
1550 |
< |
g.complete(v2); |
1551 |
< |
checkIncomplete(h); |
1552 |
< |
f.complete(v1); |
1550 |
> |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r); |
1551 |
> |
if (fFirst) { |
1552 |
> |
f.complete(v1); |
1553 |
> |
g.complete(v2); |
1554 |
> |
} else { |
1555 |
> |
g.complete(v2); |
1556 |
> |
f.complete(v1); |
1557 |
> |
} |
1558 |
> |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r); |
1559 |
|
|
1560 |
< |
checkCompletedWithWrappedCFException(h); |
1560 |
> |
checkCompletedWithWrappedCFException(h1); |
1561 |
> |
checkCompletedWithWrappedCFException(h2); |
1562 |
|
checkCompletedNormally(f, v1); |
1563 |
|
checkCompletedNormally(g, v2); |
1564 |
|
}} |
1566 |
|
/** |
1567 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
1568 |
|
*/ |
1569 |
< |
public void testRunAfterBoth_sourceCancelled1() { |
1684 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1685 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1686 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1687 |
< |
{ |
1688 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1689 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1690 |
< |
final Noop r = new Noop(); |
1691 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1692 |
< |
|
1693 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1694 |
< |
checkIncomplete(h); |
1695 |
< |
g.complete(v1); |
1696 |
< |
|
1697 |
< |
checkCompletedWithWrappedCancellationException(h); |
1698 |
< |
checkCancelled(f); |
1699 |
< |
assertEquals(0, r.invocationCount); |
1700 |
< |
checkCompletedNormally(g, v1); |
1701 |
< |
}} |
1702 |
< |
|
1703 |
< |
public void testRunAfterBoth_sourceCancelled2() { |
1704 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1705 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1706 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1707 |
< |
{ |
1708 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1709 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1710 |
< |
final Noop r = new Noop(); |
1711 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1712 |
< |
|
1713 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1714 |
< |
checkIncomplete(h); |
1715 |
< |
f.complete(v1); |
1716 |
< |
|
1717 |
< |
checkCompletedWithWrappedCancellationException(h); |
1718 |
< |
checkCancelled(g); |
1719 |
< |
assertEquals(0, r.invocationCount); |
1720 |
< |
checkCompletedNormally(f, v1); |
1721 |
< |
}} |
1722 |
< |
|
1723 |
< |
public void testRunAfterBoth_sourceCancelled3() { |
1569 |
> |
public void testRunAfterBoth_sourceCancelled() { |
1570 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1571 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1572 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1573 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1574 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1575 |
|
{ |
1576 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1577 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1578 |
< |
final Noop r = new Noop(); |
1578 |
> |
final Noop r = new Noop(m); |
1579 |
|
|
1732 |
– |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1733 |
– |
f.complete(v1); |
1734 |
– |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1580 |
|
|
1581 |
< |
checkCompletedWithWrappedCancellationException(h); |
1582 |
< |
checkCancelled(g); |
1583 |
< |
assertEquals(0, r.invocationCount); |
1739 |
< |
checkCompletedNormally(f, v1); |
1740 |
< |
}} |
1741 |
< |
|
1742 |
< |
public void testRunAfterBoth_sourceCancelled4() { |
1743 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1744 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1745 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1746 |
< |
{ |
1747 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1748 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1749 |
< |
final Noop r = new Noop(); |
1750 |
< |
|
1751 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1752 |
< |
g.complete(v1); |
1581 |
> |
(fFirst ? f : g).complete(v1); |
1582 |
> |
if (!createIncomplete) |
1583 |
> |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1584 |
|
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1585 |
+ |
if (createIncomplete) { |
1586 |
+ |
checkIncomplete(h); |
1587 |
+ |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1588 |
+ |
} |
1589 |
|
|
1590 |
|
checkCompletedWithWrappedCancellationException(h); |
1591 |
< |
checkCancelled(f); |
1591 |
> |
checkCancelled(!fFirst ? f : g); |
1592 |
|
assertEquals(0, r.invocationCount); |
1593 |
< |
checkCompletedNormally(g, v1); |
1593 |
> |
checkCompletedNormally(fFirst ? f : g, v1); |
1594 |
|
}} |
1595 |
|
|
1596 |
|
/** |
1597 |
|
* applyToEither result completes normally after normal completion |
1598 |
|
* of either source |
1599 |
|
*/ |
1600 |
< |
public void testApplyToEither_normalCompletion1() { |
1600 |
> |
public void testApplyToEither_normalCompletion() { |
1601 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1602 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1603 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1604 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1605 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1606 |
|
{ |
1607 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1608 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1609 |
|
final IncFunction r = new IncFunction(); |
1773 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1610 |
|
|
1611 |
< |
f.complete(v1); |
1612 |
< |
checkCompletedNormally(h, inc(v1)); |
1613 |
< |
g.complete(v2); |
1611 |
> |
if (!createIncomplete) |
1612 |
> |
if (fFirst) f.complete(v1); else g.complete(v2); |
1613 |
> |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1614 |
> |
if (createIncomplete) { |
1615 |
> |
checkIncomplete(h); |
1616 |
> |
assertEquals(0, r.invocationCount); |
1617 |
> |
if (fFirst) f.complete(v1); else g.complete(v2); |
1618 |
> |
} |
1619 |
> |
checkCompletedNormally(h, inc(fFirst ? v1 : v2)); |
1620 |
> |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1621 |
|
|
1622 |
|
checkCompletedNormally(f, v1); |
1623 |
|
checkCompletedNormally(g, v2); |
1624 |
< |
checkCompletedNormally(h, inc(v1)); |
1624 |
> |
checkCompletedNormally(h, inc(fFirst ? v1 : v2)); |
1625 |
|
}} |
1626 |
|
|
1627 |
< |
public void testApplyToEither_normalCompletion2() { |
1627 |
> |
public void testApplyToEither_normalCompletionBothAvailable() { |
1628 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1629 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1630 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1631 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1632 |
|
{ |
1633 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1634 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1635 |
|
final IncFunction r = new IncFunction(); |
1792 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1793 |
– |
|
1794 |
– |
g.complete(v2); |
1795 |
– |
checkCompletedNormally(h, inc(v2)); |
1796 |
– |
f.complete(v1); |
1636 |
|
|
1637 |
< |
checkCompletedNormally(f, v1); |
1638 |
< |
checkCompletedNormally(g, v2); |
1639 |
< |
checkCompletedNormally(h, inc(v2)); |
1640 |
< |
}} |
1641 |
< |
|
1642 |
< |
public void testApplyToEither_normalCompletion3() { |
1643 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1805 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1806 |
< |
for (Integer v2 : new Integer[] { 2, null }) |
1807 |
< |
{ |
1808 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1809 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1810 |
< |
final IncFunction r = new IncFunction(); |
1637 |
> |
if (fFirst) { |
1638 |
> |
f.complete(v1); |
1639 |
> |
g.complete(v2); |
1640 |
> |
} else { |
1641 |
> |
g.complete(v2); |
1642 |
> |
f.complete(v1); |
1643 |
> |
} |
1644 |
|
|
1812 |
– |
f.complete(v1); |
1813 |
– |
g.complete(v2); |
1645 |
|
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1646 |
|
|
1647 |
|
checkCompletedNormally(f, v1); |
1659 |
|
*/ |
1660 |
|
public void testApplyToEither_exceptionalCompletion1() { |
1661 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1662 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1663 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1664 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1665 |
|
{ |
1666 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1667 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1835 |
– |
final IncFunction r = new IncFunction(); |
1836 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1668 |
|
final CFException ex = new CFException(); |
1838 |
– |
|
1839 |
– |
f.completeExceptionally(ex); |
1840 |
– |
checkCompletedWithWrappedCFException(h, ex); |
1841 |
– |
g.complete(v1); |
1842 |
– |
|
1843 |
– |
assertEquals(0, r.invocationCount); |
1844 |
– |
checkCompletedNormally(g, v1); |
1845 |
– |
checkCompletedWithWrappedCFException(f, ex); |
1846 |
– |
checkCompletedWithWrappedCFException(h, ex); |
1847 |
– |
}} |
1848 |
– |
|
1849 |
– |
public void testApplyToEither_exceptionalCompletion2() { |
1850 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1851 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1852 |
– |
{ |
1853 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1854 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1669 |
|
final IncFunction r = new IncFunction(); |
1670 |
+ |
|
1671 |
+ |
if (!createIncomplete) (fFirst ? f : g).completeExceptionally(ex); |
1672 |
|
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1673 |
< |
final CFException ex = new CFException(); |
1673 |
> |
if (createIncomplete) { |
1674 |
> |
checkIncomplete(h); |
1675 |
> |
assertEquals(0, r.invocationCount); |
1676 |
> |
(fFirst ? f : g).completeExceptionally(ex); |
1677 |
> |
} |
1678 |
|
|
1859 |
– |
g.completeExceptionally(ex); |
1679 |
|
checkCompletedWithWrappedCFException(h, ex); |
1680 |
< |
f.complete(v1); |
1680 |
> |
(!fFirst ? f : g).complete(v1); |
1681 |
|
|
1682 |
|
assertEquals(0, r.invocationCount); |
1683 |
< |
checkCompletedNormally(f, v1); |
1684 |
< |
checkCompletedWithWrappedCFException(g, ex); |
1683 |
> |
checkCompletedNormally(!fFirst ? f : g, v1); |
1684 |
> |
checkCompletedWithWrappedCFException(fFirst ? f : g, ex); |
1685 |
|
checkCompletedWithWrappedCFException(h, ex); |
1686 |
|
}} |
1687 |
|
|
1688 |
< |
public void testApplyToEither_exceptionalCompletion3() { |
1688 |
> |
public void testApplyToEither_exceptionalCompletion2() { |
1689 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1690 |
+ |
for (boolean reverseArgs : new boolean[] { true, false }) |
1691 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1692 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1693 |
|
{ |
1694 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1695 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1696 |
< |
final IncFunction r = new IncFunction(); |
1696 |
> |
final IncFunction r1 = new IncFunction(); |
1697 |
> |
final IncFunction r2 = new IncFunction(); |
1698 |
|
final CFException ex = new CFException(); |
1699 |
< |
|
1700 |
< |
g.completeExceptionally(ex); |
1701 |
< |
f.complete(v1); |
1702 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1699 |
> |
final CompletableFuture<Integer> j = (reverseArgs ? g : f); |
1700 |
> |
final CompletableFuture<Integer> k = (reverseArgs ? f : g); |
1701 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1); |
1702 |
> |
if (fFirst) { |
1703 |
> |
f.complete(v1); |
1704 |
> |
g.completeExceptionally(ex); |
1705 |
> |
} else { |
1706 |
> |
g.completeExceptionally(ex); |
1707 |
> |
f.complete(v1); |
1708 |
> |
} |
1709 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2); |
1710 |
|
|
1711 |
|
// unspecified behavior |
1883 |
– |
Integer v; |
1712 |
|
try { |
1713 |
< |
assertEquals(inc(v1), h.join()); |
1714 |
< |
assertEquals(1, r.invocationCount); |
1713 |
> |
assertEquals(inc(v1), h1.join()); |
1714 |
> |
assertEquals(1, r1.invocationCount); |
1715 |
|
} catch (CompletionException ok) { |
1716 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1717 |
< |
assertEquals(0, r.invocationCount); |
1716 |
> |
checkCompletedWithWrappedCFException(h1, ex); |
1717 |
> |
assertEquals(0, r1.invocationCount); |
1718 |
|
} |
1719 |
|
|
1892 |
– |
checkCompletedWithWrappedCFException(g, ex); |
1893 |
– |
checkCompletedNormally(f, v1); |
1894 |
– |
}} |
1895 |
– |
|
1896 |
– |
public void testApplyToEither_exceptionalCompletion4() { |
1897 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
1898 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1899 |
– |
{ |
1900 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1901 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1902 |
– |
final IncFunction r = new IncFunction(); |
1903 |
– |
final CFException ex = new CFException(); |
1904 |
– |
|
1905 |
– |
f.completeExceptionally(ex); |
1906 |
– |
g.complete(v1); |
1907 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1908 |
– |
|
1909 |
– |
// unspecified behavior |
1910 |
– |
Integer v; |
1720 |
|
try { |
1721 |
< |
assertEquals(inc(v1), h.join()); |
1722 |
< |
assertEquals(1, r.invocationCount); |
1721 |
> |
assertEquals(inc(v1), h2.join()); |
1722 |
> |
assertEquals(1, r2.invocationCount); |
1723 |
|
} catch (CompletionException ok) { |
1724 |
< |
checkCompletedWithWrappedCFException(h, ex); |
1725 |
< |
assertEquals(0, r.invocationCount); |
1724 |
> |
checkCompletedWithWrappedCFException(h2, ex); |
1725 |
> |
assertEquals(0, r2.invocationCount); |
1726 |
|
} |
1727 |
|
|
1728 |
< |
checkCompletedWithWrappedCFException(f, ex); |
1729 |
< |
checkCompletedNormally(g, v1); |
1728 |
> |
checkCompletedWithWrappedCFException(g, ex); |
1729 |
> |
checkCompletedNormally(f, v1); |
1730 |
|
}} |
1731 |
|
|
1732 |
|
/** |
1772 |
|
public void testApplyToEither_sourceCancelled1() { |
1773 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1774 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1775 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1776 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1777 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1778 |
|
{ |
1779 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1780 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1781 |
|
final IncFunction r = new IncFunction(); |
1971 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1972 |
– |
|
1973 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1974 |
– |
checkCompletedWithWrappedCancellationException(h); |
1975 |
– |
g.complete(v1); |
1782 |
|
|
1783 |
< |
checkCancelled(f); |
1978 |
< |
assertEquals(0, r.invocationCount); |
1979 |
< |
checkCompletedNormally(g, v1); |
1980 |
< |
checkCompletedWithWrappedCancellationException(h); |
1981 |
< |
}} |
1982 |
< |
|
1983 |
< |
public void testApplyToEither_sourceCancelled2() { |
1984 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1985 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1986 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1987 |
< |
{ |
1988 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1989 |
< |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1990 |
< |
final IncFunction r = new IncFunction(); |
1783 |
> |
if (!createIncomplete) assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1784 |
|
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1785 |
+ |
if (createIncomplete) { |
1786 |
+ |
checkIncomplete(h); |
1787 |
+ |
assertEquals(0, r.invocationCount); |
1788 |
+ |
assertTrue((fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1789 |
+ |
} |
1790 |
|
|
1993 |
– |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1791 |
|
checkCompletedWithWrappedCancellationException(h); |
1792 |
< |
f.complete(v1); |
1792 |
> |
(!fFirst ? f : g).complete(v1); |
1793 |
|
|
1997 |
– |
checkCancelled(g); |
1794 |
|
assertEquals(0, r.invocationCount); |
1795 |
< |
checkCompletedNormally(f, v1); |
1795 |
> |
checkCompletedNormally(!fFirst ? f : g, v1); |
1796 |
> |
checkCancelled(fFirst ? f : g); |
1797 |
|
checkCompletedWithWrappedCancellationException(h); |
1798 |
|
}} |
1799 |
|
|
1800 |
< |
public void testApplyToEither_sourceCancelled3() { |
1800 |
> |
public void testApplyToEither_sourceCancelled2() { |
1801 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1802 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1803 |
+ |
for (boolean reverseArgs : new boolean[] { true, false }) |
1804 |
+ |
for (boolean fFirst : new boolean[] { true, false }) |
1805 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1806 |
|
{ |
1807 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1808 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1809 |
< |
final IncFunction r = new IncFunction(); |
1809 |
> |
final IncFunction r1 = new IncFunction(); |
1810 |
> |
final IncFunction r2 = new IncFunction(); |
1811 |
> |
final CFException ex = new CFException(); |
1812 |
> |
final CompletableFuture<Integer> j = (reverseArgs ? g : f); |
1813 |
> |
final CompletableFuture<Integer> k = (reverseArgs ? f : g); |
1814 |
|
|
1815 |
< |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1816 |
< |
f.complete(v1); |
1817 |
< |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
1815 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(j, k, r1); |
1816 |
> |
if (fFirst) { |
1817 |
> |
f.complete(v1); |
1818 |
> |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1819 |
> |
} else { |
1820 |
> |
assertTrue(g.cancel(mayInterruptIfRunning)); |
1821 |
> |
f.complete(v1); |
1822 |
> |
} |
1823 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(j, k, r2); |
1824 |
|
|
1825 |
|
// unspecified behavior |
2017 |
– |
Integer v; |
1826 |
|
try { |
1827 |
< |
assertEquals(inc(v1), h.join()); |
1828 |
< |
assertEquals(1, r.invocationCount); |
1827 |
> |
assertEquals(inc(v1), h1.join()); |
1828 |
> |
assertEquals(1, r1.invocationCount); |
1829 |
|
} catch (CompletionException ok) { |
1830 |
< |
checkCompletedWithWrappedCancellationException(h); |
1831 |
< |
assertEquals(0, r.invocationCount); |
1830 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1831 |
> |
assertEquals(0, r1.invocationCount); |
1832 |
|
} |
1833 |
|
|
2026 |
– |
checkCancelled(g); |
2027 |
– |
checkCompletedNormally(f, v1); |
2028 |
– |
}} |
2029 |
– |
|
2030 |
– |
public void testApplyToEither_sourceCancelled4() { |
2031 |
– |
for (ExecutionMode m : ExecutionMode.values()) |
2032 |
– |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2033 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
2034 |
– |
{ |
2035 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2036 |
– |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2037 |
– |
final IncFunction r = new IncFunction(); |
2038 |
– |
|
2039 |
– |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2040 |
– |
g.complete(v1); |
2041 |
– |
final CompletableFuture<Integer> h = m.applyToEither(f, g, r); |
2042 |
– |
|
2043 |
– |
// unspecified behavior |
2044 |
– |
Integer v; |
1834 |
|
try { |
1835 |
< |
assertEquals(inc(v1), h.join()); |
1836 |
< |
assertEquals(1, r.invocationCount); |
1835 |
> |
assertEquals(inc(v1), h2.join()); |
1836 |
> |
assertEquals(1, r2.invocationCount); |
1837 |
|
} catch (CompletionException ok) { |
1838 |
< |
checkCompletedWithWrappedCancellationException(h); |
1839 |
< |
assertEquals(0, r.invocationCount); |
1838 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1839 |
> |
assertEquals(0, r2.invocationCount); |
1840 |
|
} |
1841 |
|
|
1842 |
< |
checkCancelled(f); |
1843 |
< |
checkCompletedNormally(g, v1); |
1842 |
> |
checkCancelled(g); |
1843 |
> |
checkCompletedNormally(f, v1); |
1844 |
|
}} |
1845 |
|
|
1846 |
|
/** |
2156 |
|
{ |
2157 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2158 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2159 |
< |
final Noop r = new Noop(); |
2159 |
> |
final Noop r = new Noop(m); |
2160 |
|
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2161 |
|
|
2162 |
|
f.complete(v1); |
2177 |
|
{ |
2178 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2179 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2180 |
< |
final Noop r = new Noop(); |
2180 |
> |
final Noop r = new Noop(m); |
2181 |
|
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2182 |
|
|
2183 |
|
g.complete(v2); |
2198 |
|
{ |
2199 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2200 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2201 |
< |
final Noop r = new Noop(); |
2201 |
> |
final Noop r = new Noop(m); |
2202 |
|
|
2203 |
|
f.complete(v1); |
2204 |
|
g.complete(v2); |
2220 |
|
{ |
2221 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2222 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2223 |
< |
final Noop r = new Noop(); |
2223 |
> |
final Noop r = new Noop(m); |
2224 |
|
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2225 |
|
final CFException ex = new CFException(); |
2226 |
|
|
2240 |
|
{ |
2241 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2242 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2243 |
< |
final Noop r = new Noop(); |
2243 |
> |
final Noop r = new Noop(m); |
2244 |
|
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2245 |
|
final CFException ex = new CFException(); |
2246 |
|
|
2260 |
|
{ |
2261 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2262 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2263 |
< |
final Noop r = new Noop(); |
2263 |
> |
final Noop r = new Noop(m); |
2264 |
|
final CFException ex = new CFException(); |
2265 |
|
|
2266 |
|
g.completeExceptionally(ex); |
2287 |
|
{ |
2288 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2289 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2290 |
< |
final Noop r = new Noop(); |
2290 |
> |
final Noop r = new Noop(m); |
2291 |
|
final CFException ex = new CFException(); |
2292 |
|
|
2293 |
|
f.completeExceptionally(ex); |
2355 |
|
{ |
2356 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2357 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2358 |
< |
final Noop r = new Noop(); |
2358 |
> |
final Noop r = new Noop(m); |
2359 |
|
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2360 |
|
|
2361 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
2375 |
|
{ |
2376 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2377 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2378 |
< |
final Noop r = new Noop(); |
2378 |
> |
final Noop r = new Noop(m); |
2379 |
|
final CompletableFuture<Void> h = m.runAfterEither(f, g, r); |
2380 |
|
|
2381 |
|
assertTrue(g.cancel(mayInterruptIfRunning)); |
2395 |
|
{ |
2396 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2397 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2398 |
< |
final Noop r = new Noop(); |
2398 |
> |
final Noop r = new Noop(m); |
2399 |
|
|
2400 |
|
assertTrue(g.cancel(mayInterruptIfRunning)); |
2401 |
|
f.complete(v1); |
2422 |
|
{ |
2423 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2424 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2425 |
< |
final Noop r = new Noop(); |
2425 |
> |
final Noop r = new Noop(m); |
2426 |
|
|
2427 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
2428 |
|
g.complete(v1); |