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 |
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 |
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); |
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); |
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 |
|
|
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()); |
334 |
|
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
335 |
|
assertEquals(2, f.getNumberOfDependents()); |
336 |
|
assertEquals(0, h.getNumberOfDependents()); |
337 |
< |
f.complete(1); |
337 |
> |
assertTrue(f.complete(v1)); |
338 |
|
checkCompletedNormally(g, null); |
339 |
|
checkCompletedNormally(h, null); |
340 |
|
assertEquals(0, f.getNumberOfDependents()); |
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()); |
352 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
353 |
< |
|
354 |
< |
f = new CompletableFuture<String>(); |
355 |
< |
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 |
|
/** |
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 |
|
} |
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); |
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) -> { |
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); |
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) -> { |
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); |
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) -> { |
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); |
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) -> { |
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); |
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); |
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); |
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)); |
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); |
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); |
1529 |
|
*/ |
1530 |
|
public void testThenCombine_normalCompletion() { |
1531 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1527 |
– |
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); |
1549 |
– |
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()) |
1558 |
– |
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 }) |
1588 |
– |
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 |
|
/** |
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 |
< |
if (fFirst) { |
1669 |
< |
f.complete(v1); |
1670 |
< |
g.complete(v2); |
1671 |
< |
} else { |
1672 |
< |
g.complete(v2); |
1673 |
< |
f.complete(v1); |
1674 |
< |
} |
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 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1705 |
< |
if (!createIncomplete) |
1706 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1707 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1708 |
< |
if (createIncomplete) { |
1709 |
< |
checkIncomplete(h); |
1710 |
< |
r.assertNotInvoked(); |
1711 |
< |
if (!fFirst) f.complete(v1); else 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 |
< |
(fFirst ? f : g).complete(v1); |
1747 |
< |
if (!createIncomplete) |
1748 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1749 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1750 |
< |
if (createIncomplete) { |
1751 |
< |
checkIncomplete(h); |
1752 |
< |
(!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 |
< |
(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); |
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 |
< |
if (fFirst) { |
1845 |
< |
f.complete(v1); |
1846 |
< |
g.complete(v2); |
1847 |
< |
} else { |
1848 |
< |
g.complete(v2); |
1849 |
< |
f.complete(v1); |
1748 |
< |
} |
1749 |
< |
|
1750 |
< |
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()) |
1761 |
– |
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 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1869 |
< |
if (!createIncomplete) |
1870 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1871 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1872 |
< |
if (createIncomplete) { |
1873 |
< |
checkIncomplete(h); |
1874 |
< |
r.assertNotInvoked(); |
1875 |
< |
if (!fFirst) f.complete(v1); else 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()) |
1792 |
– |
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 |
< |
(fFirst ? f : g).complete(v1); |
1911 |
< |
if (!createIncomplete) |
1912 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1913 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1914 |
< |
if (createIncomplete) { |
1915 |
< |
checkIncomplete(h); |
1916 |
< |
(!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 }) |
1822 |
– |
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 |
< |
(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 |
< |
if (fFirst) { |
1999 |
< |
f.complete(v1); |
2000 |
< |
g.complete(v2); |
2001 |
< |
} else { |
2002 |
< |
g.complete(v2); |
2003 |
< |
f.complete(v1); |
2004 |
< |
} |
2005 |
< |
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 |
|
}} |
2133 |
|
|
2134 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2135 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2136 |
< |
if (fFirst) { |
2137 |
< |
f.complete(v1); |
1995 |
< |
g.completeExceptionally(ex); |
1996 |
< |
} else { |
1997 |
< |
g.completeExceptionally(ex); |
1998 |
< |
f.complete(v1); |
1999 |
< |
} |
2136 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2137 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2138 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2139 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2140 |
|
|
2240 |
|
|
2241 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2242 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2243 |
< |
if (fFirst) { |
2244 |
< |
f.complete(v1); |
2107 |
< |
g.cancel(mayInterruptIfRunning); |
2108 |
< |
} else { |
2109 |
< |
g.cancel(mayInterruptIfRunning); |
2110 |
< |
f.complete(v1); |
2111 |
< |
} |
2243 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2244 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2245 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2246 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2247 |
|
|
2443 |
|
|
2444 |
|
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2445 |
|
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2446 |
< |
if (fFirst) { |
2447 |
< |
f.complete(v1); |
2315 |
< |
g.completeExceptionally(ex); |
2316 |
< |
} else { |
2317 |
< |
g.completeExceptionally(ex); |
2318 |
< |
f.complete(v1); |
2319 |
< |
} |
2446 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2447 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2448 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2449 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2450 |
|
|
2647 |
|
checkIncomplete(h1); |
2648 |
|
rs[0].assertNotInvoked(); |
2649 |
|
rs[1].assertNotInvoked(); |
2650 |
< |
f.completeExceptionally(ex); |
2650 |
> |
assertTrue(f.completeExceptionally(ex)); |
2651 |
|
checkCompletedWithWrappedException(h0, ex); |
2652 |
|
checkCompletedWithWrappedException(h1, ex); |
2653 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2655 |
|
checkCompletedWithWrappedException(h2, ex); |
2656 |
|
checkCompletedWithWrappedException(h3, ex); |
2657 |
|
|
2658 |
< |
g.complete(v1); |
2658 |
> |
assertTrue(g.complete(v1)); |
2659 |
|
|
2660 |
|
// unspecified behavior - both source completions available |
2661 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2698 |
|
|
2699 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2700 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2701 |
< |
if (fFirst) { |
2702 |
< |
f.complete(v1); |
2575 |
< |
g.completeExceptionally(ex); |
2576 |
< |
} else { |
2577 |
< |
g.completeExceptionally(ex); |
2578 |
< |
f.complete(v1); |
2579 |
< |
} |
2701 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2702 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2703 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2704 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2705 |
|
|
2764 |
|
checkCompletedWithWrappedCancellationException(h2); |
2765 |
|
checkCompletedWithWrappedCancellationException(h3); |
2766 |
|
|
2767 |
< |
g.complete(v1); |
2767 |
> |
assertTrue(g.complete(v1)); |
2768 |
|
|
2769 |
|
// unspecified behavior - both source completions available |
2770 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2808 |
|
|
2809 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2810 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2811 |
< |
f.complete(v1); |
2811 |
> |
assertTrue(f.complete(v1)); |
2812 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2813 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2814 |
|
checkCompletedWithWrappedCFException(h0); |
2816 |
|
checkCompletedWithWrappedCFException(h2); |
2817 |
|
checkCompletedWithWrappedCFException(h3); |
2818 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2819 |
< |
g.complete(v2); |
2819 |
> |
assertTrue(g.complete(v2)); |
2820 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2821 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2822 |
|
checkCompletedWithWrappedCFException(h4); |
2837 |
|
{ |
2838 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2839 |
|
final CompletableFutureInc r = new CompletableFutureInc(m); |
2840 |
< |
if (!createIncomplete) f.complete(v1); |
2840 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2841 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2842 |
< |
if (createIncomplete) f.complete(v1); |
2842 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2843 |
|
|
2844 |
|
checkCompletedNormally(g, inc(v1)); |
2845 |
|
checkCompletedNormally(f, v1); |
2877 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2878 |
|
final FailingCompletableFutureFunction r |
2879 |
|
= new FailingCompletableFutureFunction(m); |
2880 |
< |
if (!createIncomplete) f.complete(v1); |
2880 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2881 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2882 |
< |
if (createIncomplete) f.complete(v1); |
2882 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2883 |
|
|
2884 |
|
checkCompletedWithWrappedCFException(g); |
2885 |
|
checkCompletedNormally(f, v1); |
3118 |
|
assertSame(f, f.toCompletableFuture()); |
3119 |
|
} |
3120 |
|
|
3121 |
+ |
//--- tests of implementation details; not part of official tck --- |
3122 |
+ |
|
3123 |
+ |
Object resultOf(CompletableFuture<?> f) { |
3124 |
+ |
try { |
3125 |
+ |
java.lang.reflect.Field resultField |
3126 |
+ |
= CompletableFuture.class.getDeclaredField("result"); |
3127 |
+ |
resultField.setAccessible(true); |
3128 |
+ |
return resultField.get(f); |
3129 |
+ |
} catch (Throwable t) { throw new AssertionError(t); } |
3130 |
+ |
} |
3131 |
+ |
|
3132 |
+ |
public void testExceptionPropagationReusesResultObject() { |
3133 |
+ |
if (!testImplementationDetails) return; |
3134 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3135 |
+ |
{ |
3136 |
+ |
final CFException ex = new CFException(); |
3137 |
+ |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3138 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3139 |
+ |
|
3140 |
+ |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> dependentFactories |
3141 |
+ |
= new ArrayList<>(); |
3142 |
+ |
|
3143 |
+ |
dependentFactories.add((y) -> m.thenRun(y, new Noop(m))); |
3144 |
+ |
dependentFactories.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3145 |
+ |
dependentFactories.add((y) -> m.thenApply(y, new IncFunction(m))); |
3146 |
+ |
|
3147 |
+ |
dependentFactories.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3148 |
+ |
dependentFactories.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3149 |
+ |
dependentFactories.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3150 |
+ |
|
3151 |
+ |
dependentFactories.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3152 |
+ |
dependentFactories.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3153 |
+ |
dependentFactories.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3154 |
+ |
|
3155 |
+ |
dependentFactories.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3156 |
+ |
|
3157 |
+ |
dependentFactories.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3158 |
+ |
|
3159 |
+ |
dependentFactories.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3160 |
+ |
dependentFactories.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3161 |
+ |
|
3162 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3163 |
+ |
dependentFactory : dependentFactories) { |
3164 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3165 |
+ |
f.completeExceptionally(ex); |
3166 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3167 |
+ |
checkCompletedWithWrappedException(src, ex); |
3168 |
+ |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3169 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3170 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3171 |
+ |
} |
3172 |
+ |
|
3173 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3174 |
+ |
dependentFactory : dependentFactories) { |
3175 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3176 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3177 |
+ |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3178 |
+ |
f.completeExceptionally(ex); |
3179 |
+ |
checkCompletedWithWrappedException(src, ex); |
3180 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3181 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3182 |
+ |
} |
3183 |
+ |
|
3184 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3185 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3186 |
+ |
dependentFactory : dependentFactories) { |
3187 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3188 |
+ |
f.cancel(mayInterruptIfRunning); |
3189 |
+ |
checkCancelled(f); |
3190 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3191 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3192 |
+ |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3193 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3194 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3195 |
+ |
} |
3196 |
+ |
|
3197 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3198 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3199 |
+ |
dependentFactory : dependentFactories) { |
3200 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3201 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3202 |
+ |
CompletableFuture<?> dep = dependentFactory.apply(src); |
3203 |
+ |
f.cancel(mayInterruptIfRunning); |
3204 |
+ |
checkCancelled(f); |
3205 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3206 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3207 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3208 |
+ |
} |
3209 |
+ |
}} |
3210 |
+ |
|
3211 |
|
} |