1529 |
|
*/ |
1530 |
|
public void testThenCombine_normalCompletion() { |
1531 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1532 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
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 SubtractFunction r = new SubtractFunction(m); |
1539 |
< |
|
1540 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1541 |
< |
if (!createIncomplete) |
1542 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1543 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1544 |
< |
if (createIncomplete) { |
1545 |
< |
checkIncomplete(h); |
1546 |
< |
r.assertNotInvoked(); |
1547 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1548 |
< |
} |
1549 |
< |
|
1550 |
< |
checkCompletedNormally(h, subtract(v1, v2)); |
1538 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1539 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1540 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1541 |
> |
|
1542 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1543 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1544 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1545 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1546 |
> |
|
1547 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1548 |
> |
assertTrue(fst.complete(w1)); |
1549 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1550 |
> |
checkIncomplete(h1); |
1551 |
> |
checkIncomplete(h2); |
1552 |
> |
r1.assertNotInvoked(); |
1553 |
> |
r2.assertNotInvoked(); |
1554 |
> |
assertTrue(snd.complete(w2)); |
1555 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1556 |
> |
|
1557 |
> |
checkCompletedNormally(h1, subtract(v1, v2)); |
1558 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1559 |
> |
checkCompletedNormally(h3, subtract(v1, v2)); |
1560 |
> |
r1.assertValue(subtract(v1, v2)); |
1561 |
> |
r2.assertValue(subtract(v1, v2)); |
1562 |
> |
r3.assertValue(subtract(v1, v2)); |
1563 |
|
checkCompletedNormally(f, v1); |
1564 |
|
checkCompletedNormally(g, v2); |
1554 |
– |
r.assertValue(subtract(v1, v2)); |
1565 |
|
}} |
1566 |
|
|
1567 |
|
/** |
1568 |
|
* thenCombine result completes exceptionally after exceptional |
1569 |
|
* completion of either source |
1570 |
|
*/ |
1571 |
< |
public void testThenCombine_exceptionalCompletion() { |
1571 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1572 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1563 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1573 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1574 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1575 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1576 |
|
{ |
1577 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1578 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1579 |
|
final CFException ex = new CFException(); |
1580 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1581 |
< |
|
1582 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1583 |
< |
if (!createIncomplete) |
1584 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1585 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1586 |
< |
if (createIncomplete) { |
1587 |
< |
checkIncomplete(h); |
1588 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1589 |
< |
} |
1580 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1581 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1582 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1583 |
> |
|
1584 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1585 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1586 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1587 |
> |
() -> fst.completeExceptionally(ex) : |
1588 |
> |
() -> fst.complete(v1); |
1589 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1590 |
> |
() -> snd.complete(v1) : |
1591 |
> |
() -> snd.completeExceptionally(ex); |
1592 |
> |
|
1593 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1594 |
> |
assertTrue(complete1.call()); |
1595 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1596 |
> |
checkIncomplete(h1); |
1597 |
> |
checkIncomplete(h2); |
1598 |
> |
assertTrue(complete2.call()); |
1599 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1600 |
|
|
1601 |
< |
checkCompletedWithWrappedException(h, ex); |
1602 |
< |
r.assertNotInvoked(); |
1603 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1604 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1601 |
> |
checkCompletedWithWrappedException(h1, ex); |
1602 |
> |
checkCompletedWithWrappedException(h2, ex); |
1603 |
> |
checkCompletedWithWrappedException(h3, ex); |
1604 |
> |
r1.assertNotInvoked(); |
1605 |
> |
r2.assertNotInvoked(); |
1606 |
> |
r3.assertNotInvoked(); |
1607 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1608 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1609 |
|
}} |
1610 |
|
|
1611 |
|
/** |
1612 |
|
* thenCombine result completes exceptionally if either source cancelled |
1613 |
|
*/ |
1614 |
< |
public void testThenCombine_sourceCancelled() { |
1614 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1615 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1616 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1593 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1617 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1618 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1619 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1620 |
|
{ |
1621 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1622 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1623 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1623 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1624 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1625 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1626 |
|
|
1627 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1628 |
< |
if (!createIncomplete) |
1629 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1630 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1631 |
< |
if (createIncomplete) { |
1632 |
< |
checkIncomplete(h); |
1633 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1634 |
< |
} |
1627 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1628 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1629 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1630 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1631 |
> |
() -> fst.complete(v1); |
1632 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1633 |
> |
() -> snd.complete(v1) : |
1634 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1635 |
|
|
1636 |
< |
checkCompletedWithWrappedCancellationException(h); |
1637 |
< |
checkCancelled(!fFirst ? f : g); |
1638 |
< |
r.assertNotInvoked(); |
1639 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1636 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1637 |
> |
assertTrue(complete1.call()); |
1638 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1639 |
> |
checkIncomplete(h1); |
1640 |
> |
checkIncomplete(h2); |
1641 |
> |
assertTrue(complete2.call()); |
1642 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1643 |
> |
|
1644 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1645 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1646 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1647 |
> |
r1.assertNotInvoked(); |
1648 |
> |
r2.assertNotInvoked(); |
1649 |
> |
r3.assertNotInvoked(); |
1650 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1651 |
> |
checkCancelled(failFirst ? fst : snd); |
1652 |
|
}} |
1653 |
|
|
1654 |
|
/** |
1662 |
|
{ |
1663 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1664 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1665 |
< |
final FailingBiFunction r = new FailingBiFunction(m); |
1666 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1667 |
< |
|
1668 |
< |
assertTrue( fFirst ? f.complete(v1) : g.complete(v2)); |
1669 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1665 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1666 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1667 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1668 |
> |
|
1669 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1670 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1671 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1672 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1673 |
> |
|
1674 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1675 |
> |
assertTrue(fst.complete(w1)); |
1676 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1677 |
> |
assertTrue(snd.complete(w2)); |
1678 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1679 |
|
|
1680 |
< |
checkCompletedWithWrappedCFException(h); |
1680 |
> |
checkCompletedWithWrappedCFException(h1); |
1681 |
> |
checkCompletedWithWrappedCFException(h2); |
1682 |
> |
checkCompletedWithWrappedCFException(h3); |
1683 |
> |
r1.assertInvoked(); |
1684 |
> |
r2.assertInvoked(); |
1685 |
> |
r3.assertInvoked(); |
1686 |
|
checkCompletedNormally(f, v1); |
1687 |
|
checkCompletedNormally(g, v2); |
1688 |
|
}} |
1693 |
|
*/ |
1694 |
|
public void testThenAcceptBoth_normalCompletion() { |
1695 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1644 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1696 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1697 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1698 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1699 |
|
{ |
1700 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1701 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1702 |
< |
final SubtractAction r = new SubtractAction(m); |
1703 |
< |
|
1704 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1705 |
< |
if (!createIncomplete) |
1706 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1707 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1708 |
< |
if (createIncomplete) { |
1709 |
< |
checkIncomplete(h); |
1710 |
< |
r.assertNotInvoked(); |
1711 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1712 |
< |
} |
1702 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1703 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1704 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1705 |
> |
|
1706 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1707 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1708 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1709 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1710 |
> |
|
1711 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1712 |
> |
assertTrue(fst.complete(w1)); |
1713 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1714 |
> |
checkIncomplete(h1); |
1715 |
> |
checkIncomplete(h2); |
1716 |
> |
r1.assertNotInvoked(); |
1717 |
> |
r2.assertNotInvoked(); |
1718 |
> |
assertTrue(snd.complete(w2)); |
1719 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1720 |
|
|
1721 |
< |
checkCompletedNormally(h, null); |
1722 |
< |
r.assertValue(subtract(v1, v2)); |
1721 |
> |
checkCompletedNormally(h1, null); |
1722 |
> |
checkCompletedNormally(h2, null); |
1723 |
> |
checkCompletedNormally(h3, null); |
1724 |
> |
r1.assertValue(subtract(v1, v2)); |
1725 |
> |
r2.assertValue(subtract(v1, v2)); |
1726 |
> |
r3.assertValue(subtract(v1, v2)); |
1727 |
|
checkCompletedNormally(f, v1); |
1728 |
|
checkCompletedNormally(g, v2); |
1729 |
|
}} |
1732 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1733 |
|
* completion of either source |
1734 |
|
*/ |
1735 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1735 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1736 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1675 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1737 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1738 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1739 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1740 |
|
{ |
1741 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1742 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1743 |
|
final CFException ex = new CFException(); |
1744 |
< |
final SubtractAction r = new SubtractAction(m); |
1745 |
< |
|
1746 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1747 |
< |
if (!createIncomplete) |
1748 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1749 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1750 |
< |
if (createIncomplete) { |
1751 |
< |
checkIncomplete(h); |
1752 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1753 |
< |
} |
1744 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1745 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1746 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1747 |
> |
|
1748 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1749 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1750 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1751 |
> |
() -> fst.completeExceptionally(ex) : |
1752 |
> |
() -> fst.complete(v1); |
1753 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1754 |
> |
() -> snd.complete(v1) : |
1755 |
> |
() -> snd.completeExceptionally(ex); |
1756 |
> |
|
1757 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1758 |
> |
assertTrue(complete1.call()); |
1759 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1760 |
> |
checkIncomplete(h1); |
1761 |
> |
checkIncomplete(h2); |
1762 |
> |
assertTrue(complete2.call()); |
1763 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1764 |
|
|
1765 |
< |
checkCompletedWithWrappedException(h, ex); |
1766 |
< |
r.assertNotInvoked(); |
1767 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1768 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1765 |
> |
checkCompletedWithWrappedException(h1, ex); |
1766 |
> |
checkCompletedWithWrappedException(h2, ex); |
1767 |
> |
checkCompletedWithWrappedException(h3, ex); |
1768 |
> |
r1.assertNotInvoked(); |
1769 |
> |
r2.assertNotInvoked(); |
1770 |
> |
r3.assertNotInvoked(); |
1771 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1772 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1773 |
|
}} |
1774 |
|
|
1775 |
|
/** |
1776 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1777 |
|
*/ |
1778 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1778 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1779 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1780 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1705 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1781 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1782 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1783 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1784 |
|
{ |
1785 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1786 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1787 |
< |
final SubtractAction r = new SubtractAction(m); |
1787 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1788 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1789 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1790 |
|
|
1791 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1792 |
< |
if (!createIncomplete) |
1793 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1794 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1795 |
< |
if (createIncomplete) { |
1796 |
< |
checkIncomplete(h); |
1797 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1798 |
< |
} |
1791 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1792 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1793 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1794 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1795 |
> |
() -> fst.complete(v1); |
1796 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1797 |
> |
() -> snd.complete(v1) : |
1798 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1799 |
|
|
1800 |
< |
checkCompletedWithWrappedCancellationException(h); |
1801 |
< |
checkCancelled(!fFirst ? f : g); |
1802 |
< |
r.assertNotInvoked(); |
1803 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1800 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1801 |
> |
assertTrue(complete1.call()); |
1802 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1803 |
> |
checkIncomplete(h1); |
1804 |
> |
checkIncomplete(h2); |
1805 |
> |
assertTrue(complete2.call()); |
1806 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1807 |
> |
|
1808 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1809 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1810 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1811 |
> |
r1.assertNotInvoked(); |
1812 |
> |
r2.assertNotInvoked(); |
1813 |
> |
r3.assertNotInvoked(); |
1814 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1815 |
> |
checkCancelled(failFirst ? fst : snd); |
1816 |
|
}} |
1817 |
|
|
1818 |
|
/** |
1826 |
|
{ |
1827 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1828 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1829 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1830 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1831 |
< |
|
1832 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1833 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1829 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1830 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1831 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1832 |
> |
|
1833 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1834 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1835 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1836 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1837 |
> |
|
1838 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1839 |
> |
assertTrue(fst.complete(w1)); |
1840 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1841 |
> |
assertTrue(snd.complete(w2)); |
1842 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1843 |
|
|
1844 |
< |
checkCompletedWithWrappedCFException(h); |
1844 |
> |
checkCompletedWithWrappedCFException(h1); |
1845 |
> |
checkCompletedWithWrappedCFException(h2); |
1846 |
> |
checkCompletedWithWrappedCFException(h3); |
1847 |
> |
r1.assertInvoked(); |
1848 |
> |
r2.assertInvoked(); |
1849 |
> |
r3.assertInvoked(); |
1850 |
|
checkCompletedNormally(f, v1); |
1851 |
|
checkCompletedNormally(g, v2); |
1852 |
|
}} |
1857 |
|
*/ |
1858 |
|
public void testRunAfterBoth_normalCompletion() { |
1859 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1756 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1860 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1861 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1862 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1863 |
|
{ |
1864 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1865 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1866 |
< |
final Noop r = new Noop(m); |
1867 |
< |
|
1868 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1869 |
< |
if (!createIncomplete) |
1870 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1871 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1872 |
< |
if (createIncomplete) { |
1873 |
< |
checkIncomplete(h); |
1874 |
< |
r.assertNotInvoked(); |
1875 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1876 |
< |
} |
1866 |
> |
final Noop r1 = new Noop(m); |
1867 |
> |
final Noop r2 = new Noop(m); |
1868 |
> |
final Noop r3 = new Noop(m); |
1869 |
> |
|
1870 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1871 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1872 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1873 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1874 |
> |
|
1875 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1876 |
> |
assertTrue(fst.complete(w1)); |
1877 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1878 |
> |
checkIncomplete(h1); |
1879 |
> |
checkIncomplete(h2); |
1880 |
> |
r1.assertNotInvoked(); |
1881 |
> |
r2.assertNotInvoked(); |
1882 |
> |
assertTrue(snd.complete(w2)); |
1883 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1884 |
|
|
1885 |
< |
checkCompletedNormally(h, null); |
1886 |
< |
r.assertInvoked(); |
1885 |
> |
checkCompletedNormally(h1, null); |
1886 |
> |
checkCompletedNormally(h2, null); |
1887 |
> |
checkCompletedNormally(h3, null); |
1888 |
> |
r1.assertInvoked(); |
1889 |
> |
r2.assertInvoked(); |
1890 |
> |
r3.assertInvoked(); |
1891 |
|
checkCompletedNormally(f, v1); |
1892 |
|
checkCompletedNormally(g, v2); |
1893 |
|
}} |
1896 |
|
* runAfterBoth result completes exceptionally after exceptional |
1897 |
|
* completion of either source |
1898 |
|
*/ |
1899 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
1899 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1900 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1787 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1901 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1902 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1903 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1904 |
|
{ |
1905 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1906 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1907 |
|
final CFException ex = new CFException(); |
1908 |
< |
final Noop r = new Noop(m); |
1909 |
< |
|
1910 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1911 |
< |
if (!createIncomplete) |
1912 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1913 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1914 |
< |
if (createIncomplete) { |
1915 |
< |
checkIncomplete(h); |
1916 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1917 |
< |
} |
1908 |
> |
final Noop r1 = new Noop(m); |
1909 |
> |
final Noop r2 = new Noop(m); |
1910 |
> |
final Noop r3 = new Noop(m); |
1911 |
> |
|
1912 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1913 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1914 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1915 |
> |
() -> fst.completeExceptionally(ex) : |
1916 |
> |
() -> fst.complete(v1); |
1917 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1918 |
> |
() -> snd.complete(v1) : |
1919 |
> |
() -> snd.completeExceptionally(ex); |
1920 |
> |
|
1921 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1922 |
> |
assertTrue(complete1.call()); |
1923 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1924 |
> |
checkIncomplete(h1); |
1925 |
> |
checkIncomplete(h2); |
1926 |
> |
assertTrue(complete2.call()); |
1927 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1928 |
|
|
1929 |
< |
checkCompletedWithWrappedException(h, ex); |
1930 |
< |
r.assertNotInvoked(); |
1931 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1932 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1929 |
> |
checkCompletedWithWrappedException(h1, ex); |
1930 |
> |
checkCompletedWithWrappedException(h2, ex); |
1931 |
> |
checkCompletedWithWrappedException(h3, ex); |
1932 |
> |
r1.assertNotInvoked(); |
1933 |
> |
r2.assertNotInvoked(); |
1934 |
> |
r3.assertNotInvoked(); |
1935 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1936 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1937 |
|
}} |
1938 |
|
|
1939 |
|
/** |
1940 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
1941 |
|
*/ |
1942 |
< |
public void testRunAfterBoth_sourceCancelled() { |
1942 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
1943 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1944 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1817 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1945 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1946 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1947 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1948 |
|
{ |
1949 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1950 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1951 |
< |
final Noop r = new Noop(m); |
1951 |
> |
final Noop r1 = new Noop(m); |
1952 |
> |
final Noop r2 = new Noop(m); |
1953 |
> |
final Noop r3 = new Noop(m); |
1954 |
|
|
1955 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1956 |
< |
if (!createIncomplete) |
1957 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1958 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1959 |
< |
if (createIncomplete) { |
1960 |
< |
checkIncomplete(h); |
1961 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1962 |
< |
} |
1955 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1956 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1957 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1958 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1959 |
> |
() -> fst.complete(v1); |
1960 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1961 |
> |
() -> snd.complete(v1) : |
1962 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1963 |
|
|
1964 |
< |
checkCompletedWithWrappedCancellationException(h); |
1965 |
< |
checkCancelled(!fFirst ? f : g); |
1966 |
< |
r.assertNotInvoked(); |
1967 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1964 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1965 |
> |
assertTrue(complete1.call()); |
1966 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1967 |
> |
checkIncomplete(h1); |
1968 |
> |
checkIncomplete(h2); |
1969 |
> |
assertTrue(complete2.call()); |
1970 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1971 |
> |
|
1972 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1973 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1974 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1975 |
> |
r1.assertNotInvoked(); |
1976 |
> |
r2.assertNotInvoked(); |
1977 |
> |
r3.assertNotInvoked(); |
1978 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1979 |
> |
checkCancelled(failFirst ? fst : snd); |
1980 |
|
}} |
1981 |
|
|
1982 |
|
/** |
1992 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1993 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
1994 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
1995 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
1996 |
|
|
1997 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1998 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1999 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
2000 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1997 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1998 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1999 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2000 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2001 |
> |
|
2002 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2003 |
> |
assertTrue(fst.complete(w1)); |
2004 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2005 |
> |
assertTrue(snd.complete(w2)); |
2006 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2007 |
|
|
2008 |
|
checkCompletedWithWrappedCFException(h1); |
2009 |
|
checkCompletedWithWrappedCFException(h2); |
2010 |
+ |
checkCompletedWithWrappedCFException(h3); |
2011 |
+ |
r1.assertInvoked(); |
2012 |
+ |
r2.assertInvoked(); |
2013 |
+ |
r3.assertInvoked(); |
2014 |
|
checkCompletedNormally(f, v1); |
2015 |
|
checkCompletedNormally(g, v2); |
2016 |
|
}} |
2922 |
|
* when all components complete normally |
2923 |
|
*/ |
2924 |
|
public void testAllOf_normal() throws Exception { |
2925 |
< |
for (int k = 1; k < 20; ++k) { |
2925 |
> |
for (int k = 1; k < 10; k++) { |
2926 |
|
CompletableFuture<Integer>[] fs |
2927 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2928 |
< |
for (int i = 0; i < k; ++i) |
2928 |
> |
for (int i = 0; i < k; i++) |
2929 |
|
fs[i] = new CompletableFuture<>(); |
2930 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2931 |
< |
for (int i = 0; i < k; ++i) { |
2931 |
> |
for (int i = 0; i < k; i++) { |
2932 |
|
checkIncomplete(f); |
2933 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
2934 |
|
fs[i].complete(one); |
2939 |
|
} |
2940 |
|
|
2941 |
|
public void testAllOf_backwards() throws Exception { |
2942 |
< |
for (int k = 1; k < 20; ++k) { |
2942 |
> |
for (int k = 1; k < 10; k++) { |
2943 |
|
CompletableFuture<Integer>[] fs |
2944 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2945 |
< |
for (int i = 0; i < k; ++i) |
2945 |
> |
for (int i = 0; i < k; i++) |
2946 |
|
fs[i] = new CompletableFuture<>(); |
2947 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2948 |
|
for (int i = k - 1; i >= 0; i--) { |
2955 |
|
} |
2956 |
|
} |
2957 |
|
|
2958 |
+ |
public void testAllOf_exceptional() throws Exception { |
2959 |
+ |
for (int k = 1; k < 10; k++) { |
2960 |
+ |
CompletableFuture<Integer>[] fs |
2961 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2962 |
+ |
CFException ex = new CFException(); |
2963 |
+ |
for (int i = 0; i < k; i++) |
2964 |
+ |
fs[i] = new CompletableFuture<>(); |
2965 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2966 |
+ |
for (int i = 0; i < k; i++) { |
2967 |
+ |
checkIncomplete(f); |
2968 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2969 |
+ |
if (i != k/2) { |
2970 |
+ |
fs[i].complete(i); |
2971 |
+ |
checkCompletedNormally(fs[i], i); |
2972 |
+ |
} else { |
2973 |
+ |
fs[i].completeExceptionally(ex); |
2974 |
+ |
checkCompletedExceptionally(fs[i], ex); |
2975 |
+ |
} |
2976 |
+ |
} |
2977 |
+ |
checkCompletedWithWrappedException(f, ex); |
2978 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
2979 |
+ |
} |
2980 |
+ |
} |
2981 |
+ |
|
2982 |
|
/** |
2983 |
|
* anyOf(no component futures) returns an incomplete future |
2984 |
|
*/ |
2985 |
|
public void testAnyOf_empty() throws Exception { |
2986 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2987 |
+ |
{ |
2988 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
2989 |
|
checkIncomplete(f); |
2990 |
< |
} |
2990 |
> |
|
2991 |
> |
f.complete(v1); |
2992 |
> |
checkCompletedNormally(f, v1); |
2993 |
> |
}} |
2994 |
|
|
2995 |
|
/** |
2996 |
|
* anyOf returns a future completed normally with a value when |
2997 |
|
* a component future does |
2998 |
|
*/ |
2999 |
|
public void testAnyOf_normal() throws Exception { |
3000 |
< |
for (int k = 0; k < 10; ++k) { |
3000 |
> |
for (int k = 0; k < 10; k++) { |
3001 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3002 |
< |
for (int i = 0; i < k; ++i) |
3002 |
> |
for (int i = 0; i < k; i++) |
3003 |
|
fs[i] = new CompletableFuture<>(); |
3004 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3005 |
|
checkIncomplete(f); |
3006 |
< |
for (int i = 0; i < k; ++i) { |
3007 |
< |
fs[i].complete(one); |
3008 |
< |
checkCompletedNormally(f, one); |
3009 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3006 |
> |
for (int i = 0; i < k; i++) { |
3007 |
> |
fs[i].complete(i); |
3008 |
> |
checkCompletedNormally(f, 0); |
3009 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3010 |
> |
assertTrue(0 <= x && x <= i); |
3011 |
> |
} |
3012 |
> |
} |
3013 |
> |
} |
3014 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3015 |
> |
for (int k = 0; k < 10; k++) { |
3016 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3017 |
> |
for (int i = 0; i < k; i++) |
3018 |
> |
fs[i] = new CompletableFuture<>(); |
3019 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3020 |
> |
checkIncomplete(f); |
3021 |
> |
for (int i = k - 1; i >= 0; i--) { |
3022 |
> |
fs[i].complete(i); |
3023 |
> |
checkCompletedNormally(f, k - 1); |
3024 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3025 |
> |
assertTrue(i <= x && x <= k - 1); |
3026 |
|
} |
3027 |
|
} |
3028 |
|
} |
3031 |
|
* anyOf result completes exceptionally when any component does. |
3032 |
|
*/ |
3033 |
|
public void testAnyOf_exceptional() throws Exception { |
3034 |
< |
for (int k = 0; k < 10; ++k) { |
3034 |
> |
for (int k = 0; k < 10; k++) { |
3035 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3036 |
> |
CFException[] exs = new CFException[k]; |
3037 |
> |
for (int i = 0; i < k; i++) { |
3038 |
> |
fs[i] = new CompletableFuture<>(); |
3039 |
> |
exs[i] = new CFException(); |
3040 |
> |
} |
3041 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3042 |
> |
checkIncomplete(f); |
3043 |
> |
for (int i = 0; i < k; i++) { |
3044 |
> |
fs[i].completeExceptionally(exs[i]); |
3045 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3046 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3047 |
> |
} |
3048 |
> |
} |
3049 |
> |
} |
3050 |
> |
|
3051 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3052 |
> |
for (int k = 0; k < 10; k++) { |
3053 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3054 |
< |
for (int i = 0; i < k; ++i) |
3054 |
> |
CFException[] exs = new CFException[k]; |
3055 |
> |
for (int i = 0; i < k; i++) { |
3056 |
|
fs[i] = new CompletableFuture<>(); |
3057 |
+ |
exs[i] = new CFException(); |
3058 |
+ |
} |
3059 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3060 |
|
checkIncomplete(f); |
3061 |
< |
for (int i = 0; i < k; ++i) { |
3062 |
< |
fs[i].completeExceptionally(new CFException()); |
3063 |
< |
checkCompletedWithWrappedCFException(f); |
3061 |
> |
for (int i = k - 1; i >= 0; i--) { |
3062 |
> |
fs[i].completeExceptionally(exs[i]); |
3063 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3064 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3065 |
|
} |
3066 |
|
} |
3184 |
|
assertSame(f, f.toCompletableFuture()); |
3185 |
|
} |
3186 |
|
|
3187 |
< |
// public void testRunAfterEither_resultDeterminedAtTimeOfCreation() { |
3188 |
< |
// for (ExecutionMode m : ExecutionMode.values()) |
3189 |
< |
// for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3190 |
< |
// for (Integer v1 : new Integer[] { 1, null }) |
3191 |
< |
// { |
3192 |
< |
// final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3193 |
< |
// final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3194 |
< |
// final Noop[] rs = new Noop[2]; |
3195 |
< |
// for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
3196 |
< |
// f.complete(v1); |
3197 |
< |
// final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
3198 |
< |
// final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
3199 |
< |
// assertTrue(g.cancel(mayInterruptIfRunning)); |
3200 |
< |
// checkCompletedNormally(h0, null); |
3201 |
< |
// checkCompletedNormally(h1, null); |
3202 |
< |
// for (Noop r : rs) r.assertInvoked(); |
3203 |
< |
// }} |
3187 |
> |
//--- tests of implementation details; not part of official tck --- |
3188 |
> |
|
3189 |
> |
Object resultOf(CompletableFuture<?> f) { |
3190 |
> |
try { |
3191 |
> |
java.lang.reflect.Field resultField |
3192 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3193 |
> |
resultField.setAccessible(true); |
3194 |
> |
return resultField.get(f); |
3195 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3196 |
> |
} |
3197 |
> |
|
3198 |
> |
public void testExceptionPropagationReusesResultObject() { |
3199 |
> |
if (!testImplementationDetails) return; |
3200 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3201 |
> |
{ |
3202 |
> |
final CFException ex = new CFException(); |
3203 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3204 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3205 |
> |
|
3206 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> dependentFactories |
3207 |
> |
= new ArrayList<>(); |
3208 |
> |
|
3209 |
> |
dependentFactories.add((y) -> m.thenRun(y, new Noop(m))); |
3210 |
> |
dependentFactories.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3211 |
> |
dependentFactories.add((y) -> m.thenApply(y, new IncFunction(m))); |
3212 |
> |
|
3213 |
> |
dependentFactories.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3214 |
> |
dependentFactories.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3215 |
> |
dependentFactories.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3216 |
> |
|
3217 |
> |
dependentFactories.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3218 |
> |
dependentFactories.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3219 |
> |
dependentFactories.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3220 |
> |
|
3221 |
> |
dependentFactories.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3222 |
> |
|
3223 |
> |
dependentFactories.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3224 |
> |
|
3225 |
> |
dependentFactories.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3226 |
> |
dependentFactories.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3227 |
> |
|
3228 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3229 |
> |
dependentFactory : dependentFactories) { |
3230 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3231 |
> |
f.completeExceptionally(ex); |
3232 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3233 |
> |
checkCompletedWithWrappedException(src, ex); |
3234 |
> |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3235 |
> |
checkCompletedWithWrappedException(dep, ex); |
3236 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3237 |
> |
} |
3238 |
> |
|
3239 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3240 |
> |
dependentFactory : dependentFactories) { |
3241 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3242 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3243 |
> |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3244 |
> |
f.completeExceptionally(ex); |
3245 |
> |
checkCompletedWithWrappedException(src, ex); |
3246 |
> |
checkCompletedWithWrappedException(dep, ex); |
3247 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3248 |
> |
} |
3249 |
> |
|
3250 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3251 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3252 |
> |
dependentFactory : dependentFactories) { |
3253 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3254 |
> |
f.cancel(mayInterruptIfRunning); |
3255 |
> |
checkCancelled(f); |
3256 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3257 |
> |
checkCompletedWithWrappedCancellationException(src); |
3258 |
> |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3259 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3260 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3261 |
> |
} |
3262 |
> |
|
3263 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3264 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3265 |
> |
dependentFactory : dependentFactories) { |
3266 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3267 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3268 |
> |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3269 |
> |
f.cancel(mayInterruptIfRunning); |
3270 |
> |
checkCancelled(f); |
3271 |
> |
checkCompletedWithWrappedCancellationException(src); |
3272 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3273 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3274 |
> |
} |
3275 |
> |
}} |
3276 |
|
|
3277 |
|
} |