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.75 by jsr166, Sat Jun 7 21:14:42 2014 UTC vs.
Revision 1.80 by jsr166, Mon Jun 16 17:29:03 2014 UTC

# Line 227 | Line 227 | public class CompletableFutureTest exten
227       * isCancelled, join, get, and getNow
228       */
229      public void testComplete() {
230 +        for (Integer v1 : new Integer[] { 1, null })
231 +    {
232          CompletableFuture<Integer> f = new CompletableFuture<>();
233          checkIncomplete(f);
234 <        f.complete(one);
235 <        checkCompletedNormally(f, one);
236 <    }
234 >        assertTrue(f.complete(v1));
235 >        assertFalse(f.complete(v1));
236 >        checkCompletedNormally(f, v1);
237 >    }}
238  
239      /**
240       * completeExceptionally completes exceptionally, as indicated by
# Line 250 | Line 253 | public class CompletableFutureTest exten
253       * methods isDone, isCancelled, join, get, and getNow
254       */
255      public void testCancel() {
256 +        for (boolean mayInterruptIfRunning : new boolean[] { true, false })
257 +    {
258          CompletableFuture<Integer> f = new CompletableFuture<>();
259          checkIncomplete(f);
260          assertTrue(f.cancel(true));
261 +        assertTrue(f.cancel(true));
262          checkCancelled(f);
263 <    }
263 >    }}
264  
265      /**
266       * obtrudeValue forces completion with given value
# Line 262 | Line 268 | public class CompletableFutureTest exten
268      public void testObtrudeValue() {
269          CompletableFuture<Integer> f = new CompletableFuture<>();
270          checkIncomplete(f);
271 <        f.complete(one);
271 >        assertTrue(f.complete(one));
272          checkCompletedNormally(f, one);
273          f.obtrudeValue(three);
274          checkCompletedNormally(f, three);
# Line 289 | Line 295 | public class CompletableFutureTest exten
295          CompletableFuture<Integer> f;
296  
297          f = new CompletableFuture<>();
298 <        f.complete(v1);
298 >        assertTrue(f.complete(v1));
299          for (int i = 0; i < 2; i++) {
300              f.obtrudeException(ex = new CFException());
301              checkCompletedExceptionally(f, ex);
# Line 309 | Line 315 | public class CompletableFutureTest exten
315          checkCompletedExceptionally(f, ex);
316          f.completeExceptionally(new CFException());
317          checkCompletedExceptionally(f, ex);
318 <        f.complete(v1);
318 >        assertFalse(f.complete(v1));
319          checkCompletedExceptionally(f, ex);
320      }}
321  
# Line 317 | Line 323 | public class CompletableFutureTest exten
323       * getNumberOfDependents returns number of dependent tasks
324       */
325      public void testGetNumberOfDependents() {
326 +        for (ExecutionMode m : ExecutionMode.values())
327 +        for (Integer v1 : new Integer[] { 1, null })
328 +    {
329          CompletableFuture<Integer> f = new CompletableFuture<>();
330          assertEquals(0, f.getNumberOfDependents());
331 <        CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT));
331 >        final CompletableFuture<Void> g = m.thenRun(f, new Noop(m));
332          assertEquals(1, f.getNumberOfDependents());
333          assertEquals(0, g.getNumberOfDependents());
334 <        CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT));
334 >        final CompletableFuture<Void> h = m.thenRun(f, new Noop(m));
335          assertEquals(2, f.getNumberOfDependents());
336 <        f.complete(1);
336 >        assertEquals(0, h.getNumberOfDependents());
337 >        assertTrue(f.complete(v1));
338          checkCompletedNormally(g, null);
339 +        checkCompletedNormally(h, null);
340          assertEquals(0, f.getNumberOfDependents());
341          assertEquals(0, g.getNumberOfDependents());
342 <    }
342 >        assertEquals(0, h.getNumberOfDependents());
343 >    }}
344  
345      /**
346       * toString indicates current completion state
# Line 339 | Line 351 | public class CompletableFutureTest exten
351          f = new CompletableFuture<String>();
352          assertTrue(f.toString().contains("[Not completed]"));
353  
354 <        f.complete("foo");
354 >        assertTrue(f.complete("foo"));
355          assertTrue(f.toString().contains("[Completed normally]"));
356  
357          f = new CompletableFuture<String>();
358 <        f.completeExceptionally(new IndexOutOfBoundsException());
347 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
348 <
349 <        f = new CompletableFuture<String>();
350 <        f.cancel(true);
358 >        assertTrue(f.completeExceptionally(new IndexOutOfBoundsException()));
359          assertTrue(f.toString().contains("[Completed exceptionally]"));
360  
361 <        f = new CompletableFuture<String>();
362 <        f.cancel(false);
363 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
361 >        for (boolean mayInterruptIfRunning : new boolean[] { true, false }) {
362 >            f = new CompletableFuture<String>();
363 >            assertTrue(f.cancel(mayInterruptIfRunning));
364 >            assertTrue(f.toString().contains("[Completed exceptionally]"));
365 >        }
366      }
367  
368      /**
# Line 529 | Line 539 | public class CompletableFutureTest exten
539              invoked();
540              value = x;
541              CompletableFuture<Integer> f = new CompletableFuture<>();
542 <            f.complete(inc(x));
542 >            assertTrue(f.complete(inc(x)));
543              return f;
544          }
545      }
# Line 837 | Line 847 | public class CompletableFutureTest exten
847      {
848          final AtomicInteger a = new AtomicInteger(0);
849          final CompletableFuture<Integer> f = new CompletableFuture<>();
850 <        if (!createIncomplete) f.complete(v1);
850 >        if (!createIncomplete) assertTrue(f.complete(v1));
851          final CompletableFuture<Integer> g = f.exceptionally
852              ((Throwable t) -> {
853                  // Should not be called
854                  a.getAndIncrement();
855                  throw new AssertionError();
856              });
857 <        if (createIncomplete) f.complete(v1);
857 >        if (createIncomplete) assertTrue(f.complete(v1));
858  
859          checkCompletedNormally(g, v1);
860          checkCompletedNormally(f, v1);
# Line 909 | Line 919 | public class CompletableFutureTest exten
919      {
920          final AtomicInteger a = new AtomicInteger(0);
921          final CompletableFuture<Integer> f = new CompletableFuture<>();
922 <        if (!createIncomplete) f.complete(v1);
922 >        if (!createIncomplete) assertTrue(f.complete(v1));
923          final CompletableFuture<Integer> g = m.whenComplete
924              (f,
925               (Integer x, Throwable t) -> {
# Line 918 | Line 928 | public class CompletableFutureTest exten
928                  threadAssertNull(t);
929                  a.getAndIncrement();
930              });
931 <        if (createIncomplete) f.complete(v1);
931 >        if (createIncomplete) assertTrue(f.complete(v1));
932  
933          checkCompletedNormally(g, v1);
934          checkCompletedNormally(f, v1);
# Line 992 | Line 1002 | public class CompletableFutureTest exten
1002          final AtomicInteger a = new AtomicInteger(0);
1003          final CFException ex = new CFException();
1004          final CompletableFuture<Integer> f = new CompletableFuture<>();
1005 <        if (!createIncomplete) f.complete(v1);
1005 >        if (!createIncomplete) assertTrue(f.complete(v1));
1006          final CompletableFuture<Integer> g = m.whenComplete
1007              (f,
1008               (Integer x, Throwable t) -> {
# Line 1002 | Line 1012 | public class CompletableFutureTest exten
1012                  a.getAndIncrement();
1013                  throw ex;
1014              });
1015 <        if (createIncomplete) f.complete(v1);
1015 >        if (createIncomplete) assertTrue(f.complete(v1));
1016  
1017          checkCompletedWithWrappedException(g, ex);
1018          checkCompletedNormally(f, v1);
# Line 1052 | Line 1062 | public class CompletableFutureTest exten
1062      {
1063          final CompletableFuture<Integer> f = new CompletableFuture<>();
1064          final AtomicInteger a = new AtomicInteger(0);
1065 <        if (!createIncomplete) f.complete(v1);
1065 >        if (!createIncomplete) assertTrue(f.complete(v1));
1066          final CompletableFuture<Integer> g = m.handle
1067              (f,
1068               (Integer x, Throwable t) -> {
# Line 1062 | Line 1072 | public class CompletableFutureTest exten
1072                  a.getAndIncrement();
1073                  return inc(v1);
1074              });
1075 <        if (createIncomplete) f.complete(v1);
1075 >        if (createIncomplete) assertTrue(f.complete(v1));
1076  
1077          checkCompletedNormally(g, inc(v1));
1078          checkCompletedNormally(f, v1);
# Line 1163 | Line 1173 | public class CompletableFutureTest exten
1173          final CompletableFuture<Integer> f = new CompletableFuture<>();
1174          final AtomicInteger a = new AtomicInteger(0);
1175          final CFException ex = new CFException();
1176 <        if (!createIncomplete) f.complete(v1);
1176 >        if (!createIncomplete) assertTrue(f.complete(v1));
1177          final CompletableFuture<Integer> g = m.handle
1178              (f,
1179               (Integer x, Throwable t) -> {
# Line 1173 | Line 1183 | public class CompletableFutureTest exten
1183                  a.getAndIncrement();
1184                  throw ex;
1185              });
1186 <        if (createIncomplete) f.complete(v1);
1186 >        if (createIncomplete) assertTrue(f.complete(v1));
1187  
1188          checkCompletedWithWrappedException(g, ex);
1189          checkCompletedNormally(f, v1);
# Line 1259 | Line 1269 | public class CompletableFutureTest exten
1269      {
1270          final CompletableFuture<Integer> f = new CompletableFuture<>();
1271          final Noop r = new Noop(m);
1272 <        if (!createIncomplete) f.complete(v1);
1272 >        if (!createIncomplete) assertTrue(f.complete(v1));
1273          final CompletableFuture<Void> g = m.thenRun(f, r);
1274          if (createIncomplete) {
1275              checkIncomplete(g);
1276 <            f.complete(v1);
1276 >            assertTrue(f.complete(v1));
1277          }
1278  
1279          checkCompletedNormally(g, null);
# Line 1326 | Line 1336 | public class CompletableFutureTest exten
1336      {
1337          final CompletableFuture<Integer> f = new CompletableFuture<>();
1338          final FailingRunnable r = new FailingRunnable(m);
1339 <        if (!createIncomplete) f.complete(v1);
1339 >        if (!createIncomplete) assertTrue(f.complete(v1));
1340          final CompletableFuture<Void> g = m.thenRun(f, r);
1341          if (createIncomplete) {
1342              checkIncomplete(g);
1343 <            f.complete(v1);
1343 >            assertTrue(f.complete(v1));
1344          }
1345  
1346          checkCompletedWithWrappedCFException(g);
# Line 1347 | Line 1357 | public class CompletableFutureTest exten
1357      {
1358          final CompletableFuture<Integer> f = new CompletableFuture<>();
1359          final IncFunction r = new IncFunction(m);
1360 <        if (!createIncomplete) f.complete(v1);
1360 >        if (!createIncomplete) assertTrue(f.complete(v1));
1361          final CompletableFuture<Integer> g = m.thenApply(f, r);
1362          if (createIncomplete) {
1363              checkIncomplete(g);
1364 <            f.complete(v1);
1364 >            assertTrue(f.complete(v1));
1365          }
1366  
1367          checkCompletedNormally(g, inc(v1));
# Line 1414 | Line 1424 | public class CompletableFutureTest exten
1424      {
1425          final CompletableFuture<Integer> f = new CompletableFuture<>();
1426          final FailingFunction r = new FailingFunction(m);
1427 <        if (!createIncomplete) f.complete(v1);
1427 >        if (!createIncomplete) assertTrue(f.complete(v1));
1428          final CompletableFuture<Integer> g = m.thenApply(f, r);
1429          if (createIncomplete) {
1430              checkIncomplete(g);
1431 <            f.complete(v1);
1431 >            assertTrue(f.complete(v1));
1432          }
1433  
1434          checkCompletedWithWrappedCFException(g);
# Line 1435 | Line 1445 | public class CompletableFutureTest exten
1445      {
1446          final CompletableFuture<Integer> f = new CompletableFuture<>();
1447          final NoopConsumer r = new NoopConsumer(m);
1448 <        if (!createIncomplete) f.complete(v1);
1448 >        if (!createIncomplete) assertTrue(f.complete(v1));
1449          final CompletableFuture<Void> g = m.thenAccept(f, r);
1450          if (createIncomplete) {
1451              checkIncomplete(g);
1452 <            f.complete(v1);
1452 >            assertTrue(f.complete(v1));
1453          }
1454  
1455          checkCompletedNormally(g, null);
# Line 1519 | Line 1529 | public class CompletableFutureTest exten
1529       */
1530      public void testThenCombine_normalCompletion() {
1531          for (ExecutionMode m : ExecutionMode.values())
1522        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 <        if (fFirst) f.complete(v1); else g.complete(v2);
1541 <        if (!createIncomplete)
1542 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1543 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1544 <        if (createIncomplete) {
1545 <            checkIncomplete(h);
1546 <            r.assertNotInvoked();
1547 <            if (!fFirst) f.complete(v1); else 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);
1544        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())
1553        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 <        (fFirst ? f : g).complete(v1);
1583 <        if (!createIncomplete)
1584 <            (!fFirst ? f : g).completeExceptionally(ex);
1585 <        final CompletableFuture<Integer> h = m.thenCombine(f, g, r);
1586 <        if (createIncomplete) {
1587 <            checkIncomplete(h);
1588 <            (!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 })
1583        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 <        (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      /**
# Line 1614 | Line 1662 | public class CompletableFutureTest exten
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);
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 <        if (fFirst) {
1681 <            f.complete(v1);
1682 <            g.complete(v2);
1623 <        } else {
1624 <            g.complete(v2);
1625 <            f.complete(v1);
1626 <        }
1627 <
1628 <        checkCompletedWithWrappedCFException(h);
1680 >        checkCompletedWithWrappedCFException(h1);
1681 >        checkCompletedWithWrappedCFException(h2);
1682 >        checkCompletedWithWrappedCFException(h3);
1683          checkCompletedNormally(f, v1);
1684          checkCompletedNormally(g, v2);
1685      }}
# Line 1636 | Line 1690 | public class CompletableFutureTest exten
1690       */
1691      public void testThenAcceptBoth_normalCompletion() {
1692          for (ExecutionMode m : ExecutionMode.values())
1639        for (boolean createIncomplete : new boolean[] { true, false })
1693          for (boolean fFirst : new boolean[] { true, false })
1694          for (Integer v1 : new Integer[] { 1, null })
1695          for (Integer v2 : new Integer[] { 2, null })
1696      {
1697          final CompletableFuture<Integer> f = new CompletableFuture<>();
1698          final CompletableFuture<Integer> g = new CompletableFuture<>();
1699 <        final SubtractAction r = new SubtractAction(m);
1700 <
1701 <        if (fFirst) f.complete(v1); else g.complete(v2);
1702 <        if (!createIncomplete)
1703 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1704 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1705 <        if (createIncomplete) {
1706 <            checkIncomplete(h);
1707 <            r.assertNotInvoked();
1708 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1709 <        }
1699 >        final SubtractAction r1 = new SubtractAction(m);
1700 >        final SubtractAction r2 = new SubtractAction(m);
1701 >        final SubtractAction r3 = new SubtractAction(m);
1702 >
1703 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1704 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1705 >        final Integer w1 =  fFirst ? v1 : v2;
1706 >        final Integer w2 = !fFirst ? v1 : v2;
1707 >
1708 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1709 >        assertTrue(fst.complete(w1));
1710 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1711 >        checkIncomplete(h1);
1712 >        checkIncomplete(h2);
1713 >        r1.assertNotInvoked();
1714 >        r2.assertNotInvoked();
1715 >        assertTrue(snd.complete(w2));
1716 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1717  
1718 <        checkCompletedNormally(h, null);
1719 <        r.assertValue(subtract(v1, v2));
1718 >        checkCompletedNormally(h1, null);
1719 >        checkCompletedNormally(h2, null);
1720 >        checkCompletedNormally(h3, null);
1721 >        r1.assertValue(subtract(v1, v2));
1722 >        r2.assertValue(subtract(v1, v2));
1723 >        r3.assertValue(subtract(v1, v2));
1724          checkCompletedNormally(f, v1);
1725          checkCompletedNormally(g, v2);
1726      }}
# Line 1665 | Line 1729 | public class CompletableFutureTest exten
1729       * thenAcceptBoth result completes exceptionally after exceptional
1730       * completion of either source
1731       */
1732 <    public void testThenAcceptBoth_exceptionalCompletion() {
1732 >    public void testThenAcceptBoth_exceptionalCompletion() throws Throwable {
1733          for (ExecutionMode m : ExecutionMode.values())
1670        for (boolean createIncomplete : new boolean[] { true, false })
1734          for (boolean fFirst : new boolean[] { true, false })
1735 +        for (boolean failFirst : new boolean[] { true, false })
1736          for (Integer v1 : new Integer[] { 1, null })
1737      {
1738          final CompletableFuture<Integer> f = new CompletableFuture<>();
1739          final CompletableFuture<Integer> g = new CompletableFuture<>();
1740          final CFException ex = new CFException();
1741 <        final SubtractAction r = new SubtractAction(m);
1742 <
1743 <        (fFirst ? f : g).complete(v1);
1744 <        if (!createIncomplete)
1745 <            (!fFirst ? f : g).completeExceptionally(ex);
1746 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1747 <        if (createIncomplete) {
1748 <            checkIncomplete(h);
1749 <            (!fFirst ? f : g).completeExceptionally(ex);
1750 <        }
1741 >        final SubtractAction r1 = new SubtractAction(m);
1742 >        final SubtractAction r2 = new SubtractAction(m);
1743 >        final SubtractAction r3 = new SubtractAction(m);
1744 >
1745 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1746 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1747 >        final Callable<Boolean> complete1 = failFirst ?
1748 >            () -> fst.completeExceptionally(ex) :
1749 >            () -> fst.complete(v1);
1750 >        final Callable<Boolean> complete2 = failFirst ?
1751 >            () -> snd.complete(v1) :
1752 >            () -> snd.completeExceptionally(ex);
1753 >
1754 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1755 >        assertTrue(complete1.call());
1756 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1757 >        checkIncomplete(h1);
1758 >        checkIncomplete(h2);
1759 >        assertTrue(complete2.call());
1760 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1761  
1762 <        checkCompletedWithWrappedException(h, ex);
1763 <        r.assertNotInvoked();
1764 <        checkCompletedNormally(fFirst ? f : g, v1);
1765 <        checkCompletedExceptionally(!fFirst ? f : g, ex);
1762 >        checkCompletedWithWrappedException(h1, ex);
1763 >        checkCompletedWithWrappedException(h2, ex);
1764 >        checkCompletedWithWrappedException(h3, ex);
1765 >        r1.assertNotInvoked();
1766 >        r2.assertNotInvoked();
1767 >        r3.assertNotInvoked();
1768 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1769 >        checkCompletedExceptionally(failFirst ? fst : snd, ex);
1770      }}
1771  
1772      /**
1773       * thenAcceptBoth result completes exceptionally if either source cancelled
1774       */
1775 <    public void testThenAcceptBoth_sourceCancelled() {
1775 >    public void testThenAcceptBoth_sourceCancelled() throws Throwable {
1776          for (ExecutionMode m : ExecutionMode.values())
1777          for (boolean mayInterruptIfRunning : new boolean[] { true, false })
1700        for (boolean createIncomplete : new boolean[] { true, false })
1778          for (boolean fFirst : new boolean[] { true, false })
1779 +        for (boolean failFirst : new boolean[] { true, false })
1780          for (Integer v1 : new Integer[] { 1, null })
1781      {
1782          final CompletableFuture<Integer> f = new CompletableFuture<>();
1783          final CompletableFuture<Integer> g = new CompletableFuture<>();
1784 <        final SubtractAction r = new SubtractAction(m);
1784 >        final SubtractAction r1 = new SubtractAction(m);
1785 >        final SubtractAction r2 = new SubtractAction(m);
1786 >        final SubtractAction r3 = new SubtractAction(m);
1787  
1788 <        (fFirst ? f : g).complete(v1);
1789 <        if (!createIncomplete)
1790 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1791 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1792 <        if (createIncomplete) {
1793 <            checkIncomplete(h);
1794 <            assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1795 <        }
1788 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1789 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1790 >        final Callable<Boolean> complete1 = failFirst ?
1791 >            () -> fst.cancel(mayInterruptIfRunning) :
1792 >            () -> fst.complete(v1);
1793 >        final Callable<Boolean> complete2 = failFirst ?
1794 >            () -> snd.complete(v1) :
1795 >            () -> snd.cancel(mayInterruptIfRunning);
1796  
1797 <        checkCompletedWithWrappedCancellationException(h);
1798 <        checkCancelled(!fFirst ? f : g);
1799 <        r.assertNotInvoked();
1800 <        checkCompletedNormally(fFirst ? f : g, v1);
1797 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1798 >        assertTrue(complete1.call());
1799 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1800 >        checkIncomplete(h1);
1801 >        checkIncomplete(h2);
1802 >        assertTrue(complete2.call());
1803 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1804 >
1805 >        checkCompletedWithWrappedCancellationException(h1);
1806 >        checkCompletedWithWrappedCancellationException(h2);
1807 >        checkCompletedWithWrappedCancellationException(h3);
1808 >        r1.assertNotInvoked();
1809 >        r2.assertNotInvoked();
1810 >        r3.assertNotInvoked();
1811 >        checkCompletedNormally(failFirst ? snd : fst, v1);
1812 >        checkCancelled(failFirst ? fst : snd);
1813      }}
1814  
1815      /**
# Line 1731 | Line 1823 | public class CompletableFutureTest exten
1823      {
1824          final CompletableFuture<Integer> f = new CompletableFuture<>();
1825          final CompletableFuture<Integer> g = new CompletableFuture<>();
1826 <        final FailingBiConsumer r = new FailingBiConsumer(m);
1827 <        final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r);
1828 <
1829 <        if (fFirst) {
1830 <            f.complete(v1);
1831 <            g.complete(v2);
1832 <        } else {
1833 <            g.complete(v2);
1834 <            f.complete(v1);
1835 <        }
1826 >        final FailingBiConsumer r1 = new FailingBiConsumer(m);
1827 >        final FailingBiConsumer r2 = new FailingBiConsumer(m);
1828 >        final FailingBiConsumer r3 = new FailingBiConsumer(m);
1829 >
1830 >        final CompletableFuture<Integer> fst =  fFirst ? f : g;
1831 >        final CompletableFuture<Integer> snd = !fFirst ? f : g;
1832 >        final Integer w1 =  fFirst ? v1 : v2;
1833 >        final Integer w2 = !fFirst ? v1 : v2;
1834 >
1835 >        final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1);
1836 >        assertTrue(fst.complete(w1));
1837 >        final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2);
1838 >        assertTrue(snd.complete(w2));
1839 >        final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3);
1840  
1841 <        checkCompletedWithWrappedCFException(h);
1841 >        checkCompletedWithWrappedCFException(h1);
1842 >        checkCompletedWithWrappedCFException(h2);
1843 >        checkCompletedWithWrappedCFException(h3);
1844          checkCompletedNormally(f, v1);
1845          checkCompletedNormally(g, v2);
1846      }}
# Line 1762 | Line 1860 | public class CompletableFutureTest exten
1860          final CompletableFuture<Integer> g = new CompletableFuture<>();
1861          final Noop r = new Noop(m);
1862  
1863 <        if (fFirst) f.complete(v1); else g.complete(v2);
1863 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1864          if (!createIncomplete)
1865 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1865 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1866          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1867          if (createIncomplete) {
1868              checkIncomplete(h);
1869              r.assertNotInvoked();
1870 <            if (!fFirst) f.complete(v1); else g.complete(v2);
1870 >            assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1871          }
1872  
1873          checkCompletedNormally(h, null);
# Line 1793 | Line 1891 | public class CompletableFutureTest exten
1891          final CFException ex = new CFException();
1892          final Noop r = new Noop(m);
1893  
1894 <        (fFirst ? f : g).complete(v1);
1894 >        assertTrue((fFirst ? f : g).complete(v1));
1895          if (!createIncomplete)
1896 <            (!fFirst ? f : g).completeExceptionally(ex);
1896 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1897          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
1898          if (createIncomplete) {
1899              checkIncomplete(h);
1900 <            (!fFirst ? f : g).completeExceptionally(ex);
1900 >            assertTrue((!fFirst ? f : g).completeExceptionally(ex));
1901          }
1902  
1903          checkCompletedWithWrappedException(h, ex);
# Line 1822 | Line 1920 | public class CompletableFutureTest exten
1920          final CompletableFuture<Integer> g = new CompletableFuture<>();
1921          final Noop r = new Noop(m);
1922  
1923 <        (fFirst ? f : g).complete(v1);
1923 >        assertTrue((fFirst ? f : g).complete(v1));
1924          if (!createIncomplete)
1925              assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning));
1926          final CompletableFuture<Void> h = m.runAfterBoth(f, g, r);
# Line 1852 | Line 1950 | public class CompletableFutureTest exten
1950          final FailingRunnable r2 = new FailingRunnable(m);
1951  
1952          CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1);
1953 <        if (fFirst) {
1954 <            f.complete(v1);
1857 <            g.complete(v2);
1858 <        } else {
1859 <            g.complete(v2);
1860 <            f.complete(v1);
1861 <        }
1953 >        assertTrue(fFirst ? f.complete(v1) : g.complete(v2));
1954 >        assertTrue(!fFirst ? f.complete(v1) : g.complete(v2));
1955          CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2);
1956  
1957          checkCompletedWithWrappedCFException(h1);
# Line 1985 | Line 2078 | public class CompletableFutureTest exten
2078  
2079          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2080          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2081 <        if (fFirst) {
2082 <            f.complete(v1);
1990 <            g.completeExceptionally(ex);
1991 <        } else {
1992 <            g.completeExceptionally(ex);
1993 <            f.complete(v1);
1994 <        }
2081 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2082 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2083          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2084          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2085  
# Line 2097 | Line 2185 | public class CompletableFutureTest exten
2185  
2186          final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]);
2187          final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]);
2188 <        if (fFirst) {
2189 <            f.complete(v1);
2102 <            g.cancel(mayInterruptIfRunning);
2103 <        } else {
2104 <            g.cancel(mayInterruptIfRunning);
2105 <            f.complete(v1);
2106 <        }
2188 >        assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2189 >        assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning));
2190          final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]);
2191          final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]);
2192  
# Line 2305 | Line 2388 | public class CompletableFutureTest exten
2388  
2389          final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]);
2390          final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]);
2391 <        if (fFirst) {
2392 <            f.complete(v1);
2310 <            g.completeExceptionally(ex);
2311 <        } else {
2312 <            g.completeExceptionally(ex);
2313 <            f.complete(v1);
2314 <        }
2391 >        assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2392 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2393          final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]);
2394          final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]);
2395  
# Line 2514 | Line 2592 | public class CompletableFutureTest exten
2592          checkIncomplete(h1);
2593          rs[0].assertNotInvoked();
2594          rs[1].assertNotInvoked();
2595 <        f.completeExceptionally(ex);
2595 >        assertTrue(f.completeExceptionally(ex));
2596          checkCompletedWithWrappedException(h0, ex);
2597          checkCompletedWithWrappedException(h1, ex);
2598          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
# Line 2522 | Line 2600 | public class CompletableFutureTest exten
2600          checkCompletedWithWrappedException(h2, ex);
2601          checkCompletedWithWrappedException(h3, ex);
2602  
2603 <        g.complete(v1);
2603 >        assertTrue(g.complete(v1));
2604  
2605          // unspecified behavior - both source completions available
2606          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2565 | Line 2643 | public class CompletableFutureTest exten
2643  
2644          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2645          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2646 <        if (fFirst) {
2647 <            f.complete(v1);
2570 <            g.completeExceptionally(ex);
2571 <        } else {
2572 <            g.completeExceptionally(ex);
2573 <            f.complete(v1);
2574 <        }
2646 >        assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2647 >        assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex));
2648          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2649          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2650  
# Line 2636 | Line 2709 | public class CompletableFutureTest exten
2709          checkCompletedWithWrappedCancellationException(h2);
2710          checkCompletedWithWrappedCancellationException(h3);
2711  
2712 <        g.complete(v1);
2712 >        assertTrue(g.complete(v1));
2713  
2714          // unspecified behavior - both source completions available
2715          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
# Line 2680 | Line 2753 | public class CompletableFutureTest exten
2753  
2754          final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
2755          final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
2756 <        f.complete(v1);
2756 >        assertTrue(f.complete(v1));
2757          final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]);
2758          final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]);
2759          checkCompletedWithWrappedCFException(h0);
# Line 2688 | Line 2761 | public class CompletableFutureTest exten
2761          checkCompletedWithWrappedCFException(h2);
2762          checkCompletedWithWrappedCFException(h3);
2763          for (int i = 0; i < 4; i++) rs[i].assertInvoked();
2764 <        g.complete(v2);
2764 >        assertTrue(g.complete(v2));
2765          final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]);
2766          final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]);
2767          checkCompletedWithWrappedCFException(h4);
# Line 2709 | Line 2782 | public class CompletableFutureTest exten
2782      {
2783          final CompletableFuture<Integer> f = new CompletableFuture<>();
2784          final CompletableFutureInc r = new CompletableFutureInc(m);
2785 <        if (!createIncomplete) f.complete(v1);
2785 >        if (!createIncomplete) assertTrue(f.complete(v1));
2786          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2787 <        if (createIncomplete) f.complete(v1);
2787 >        if (createIncomplete) assertTrue(f.complete(v1));
2788  
2789          checkCompletedNormally(g, inc(v1));
2790          checkCompletedNormally(f, v1);
# Line 2749 | Line 2822 | public class CompletableFutureTest exten
2822          final CompletableFuture<Integer> f = new CompletableFuture<>();
2823          final FailingCompletableFutureFunction r
2824              = new FailingCompletableFutureFunction(m);
2825 <        if (!createIncomplete) f.complete(v1);
2825 >        if (!createIncomplete) assertTrue(f.complete(v1));
2826          final CompletableFuture<Integer> g = m.thenCompose(f, r);
2827 <        if (createIncomplete) f.complete(v1);
2827 >        if (createIncomplete) assertTrue(f.complete(v1));
2828  
2829          checkCompletedWithWrappedCFException(g);
2830          checkCompletedNormally(f, v1);
# Line 2990 | Line 3063 | public class CompletableFutureTest exten
3063          assertSame(f, f.toCompletableFuture());
3064      }
3065  
3066 + //     public void testRunAfterEither_resultDeterminedAtTimeOfCreation() {
3067 + //         for (ExecutionMode m : ExecutionMode.values())
3068 + //         for (boolean mayInterruptIfRunning : new boolean[] { true, false })
3069 + //         for (Integer v1 : new Integer[] { 1, null })
3070 + //     {
3071 + //         final CompletableFuture<Integer> f = new CompletableFuture<>();
3072 + //         final CompletableFuture<Integer> g = new CompletableFuture<>();
3073 + //         final Noop[] rs = new Noop[2];
3074 + //         for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m);
3075 + //         f.complete(v1);
3076 + //         final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]);
3077 + //         final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]);
3078 + //         assertTrue(g.cancel(mayInterruptIfRunning));
3079 + //         checkCompletedNormally(h0, null);
3080 + //         checkCompletedNormally(h1, null);
3081 + //         for (Noop r : rs) r.assertInvoked();
3082 + //     }}
3083 +
3084   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines