53 |
|
catch (Throwable fail) { threadUnexpectedException(fail); } |
54 |
|
} |
55 |
|
|
56 |
< |
void checkCompletedNormally(CompletableFuture<?> f, Object value) { |
56 |
> |
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
57 |
|
try { |
58 |
|
assertEquals(value, f.join()); |
59 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
75 |
|
try { |
76 |
|
f.join(); |
77 |
|
shouldThrow(); |
78 |
< |
} catch (Throwable ex) { |
79 |
< |
assertTrue(ex instanceof CompletionException && |
80 |
< |
((CompletionException)ex).getCause() instanceof CFException); |
78 |
> |
} catch (CompletionException success) { |
79 |
> |
assertTrue(success.getCause() instanceof CFException); |
80 |
|
} |
81 |
|
try { |
82 |
|
f.getNow(null); |
83 |
|
shouldThrow(); |
84 |
< |
} catch (Throwable ex) { |
85 |
< |
assertTrue(ex instanceof CompletionException && |
87 |
< |
((CompletionException)ex).getCause() instanceof CFException); |
84 |
> |
} catch (CompletionException success) { |
85 |
> |
assertTrue(success.getCause() instanceof CFException); |
86 |
|
} |
87 |
|
try { |
88 |
|
f.get(); |
89 |
|
shouldThrow(); |
90 |
< |
} catch (Throwable ex) { |
91 |
< |
assertTrue(ex instanceof ExecutionException && |
92 |
< |
((ExecutionException)ex).getCause() instanceof CFException); |
95 |
< |
} |
90 |
> |
} catch (ExecutionException success) { |
91 |
> |
assertTrue(success.getCause() instanceof CFException); |
92 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
93 |
|
try { |
94 |
|
f.get(0L, SECONDS); |
95 |
|
shouldThrow(); |
96 |
< |
} catch (Throwable ex) { |
97 |
< |
assertTrue(ex instanceof ExecutionException && |
98 |
< |
((ExecutionException)ex).getCause() instanceof CFException); |
102 |
< |
} |
96 |
> |
} catch (ExecutionException success) { |
97 |
> |
assertTrue(success.getCause() instanceof CFException); |
98 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
|
assertTrue(f.isDone()); |
100 |
|
assertFalse(f.isCancelled()); |
101 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
102 |
|
} |
103 |
|
|
104 |
|
void checkCancelled(CompletableFuture<?> f) { |
105 |
|
try { |
106 |
|
f.join(); |
107 |
|
shouldThrow(); |
108 |
< |
} catch (Throwable ex) { |
112 |
< |
assertTrue(ex instanceof CancellationException); |
113 |
< |
} |
108 |
> |
} catch (CancellationException success) {} |
109 |
|
try { |
110 |
|
f.getNow(null); |
111 |
|
shouldThrow(); |
112 |
< |
} catch (Throwable ex) { |
118 |
< |
assertTrue(ex instanceof CancellationException); |
119 |
< |
} |
112 |
> |
} catch (CancellationException success) {} |
113 |
|
try { |
114 |
|
f.get(); |
115 |
|
shouldThrow(); |
116 |
< |
} catch (Throwable ex) { |
117 |
< |
assertTrue(ex instanceof CancellationException); |
125 |
< |
} |
116 |
> |
} catch (CancellationException success) { |
117 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
|
try { |
119 |
|
f.get(0L, SECONDS); |
120 |
|
shouldThrow(); |
121 |
< |
} catch (Throwable ex) { |
122 |
< |
assertTrue(ex instanceof CancellationException); |
131 |
< |
} |
121 |
> |
} catch (CancellationException success) { |
122 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
|
assertTrue(f.isDone()); |
124 |
|
assertTrue(f.isCancelled()); |
125 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
126 |
|
} |
127 |
|
|
128 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
129 |
|
try { |
130 |
|
f.join(); |
131 |
|
shouldThrow(); |
132 |
< |
} catch (Throwable ex) { |
133 |
< |
assertTrue(ex instanceof CompletionException && |
142 |
< |
((CompletionException)ex).getCause() instanceof CancellationException); |
132 |
> |
} catch (CompletionException success) { |
133 |
> |
assertTrue(success.getCause() instanceof CancellationException); |
134 |
|
} |
135 |
|
try { |
136 |
|
f.getNow(null); |
137 |
|
shouldThrow(); |
138 |
< |
} catch (Throwable ex) { |
139 |
< |
assertTrue(ex instanceof CompletionException && |
149 |
< |
((CompletionException)ex).getCause() instanceof CancellationException); |
138 |
> |
} catch (CompletionException success) { |
139 |
> |
assertTrue(success.getCause() instanceof CancellationException); |
140 |
|
} |
141 |
|
try { |
142 |
|
f.get(); |
143 |
|
shouldThrow(); |
144 |
< |
} catch (Throwable ex) { |
145 |
< |
assertTrue(ex instanceof ExecutionException && |
146 |
< |
((ExecutionException)ex).getCause() instanceof CancellationException); |
157 |
< |
} |
144 |
> |
} catch (ExecutionException success) { |
145 |
> |
assertTrue(success.getCause() instanceof CancellationException); |
146 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
147 |
|
try { |
148 |
|
f.get(0L, SECONDS); |
149 |
|
shouldThrow(); |
150 |
< |
} catch (Throwable ex) { |
151 |
< |
assertTrue(ex instanceof ExecutionException && |
152 |
< |
((ExecutionException)ex).getCause() instanceof CancellationException); |
164 |
< |
} |
150 |
> |
} catch (ExecutionException success) { |
151 |
> |
assertTrue(success.getCause() instanceof CancellationException); |
152 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
|
assertTrue(f.isDone()); |
154 |
|
assertFalse(f.isCancelled()); |
155 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
238 |
|
f.obtrudeException(new CFException()); |
239 |
|
checkCompletedWithWrappedCFException(f); |
240 |
|
} |
241 |
< |
|
241 |
> |
|
242 |
|
/** |
243 |
|
* getNumberOfDependents returns number of dependent tasks |
244 |
|
*/ |
274 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
275 |
|
} |
276 |
|
|
277 |
< |
static final Supplier<Integer> supplyOne = |
277 |
> |
/** |
278 |
> |
* completedFuture returns a completed CompletableFuture with given value |
279 |
> |
*/ |
280 |
> |
public void testCompletedFuture() { |
281 |
> |
CompletableFuture<String> f = CompletableFuture.completedFuture("test"); |
282 |
> |
checkCompletedNormally(f, "test"); |
283 |
> |
} |
284 |
> |
|
285 |
> |
static final Supplier<Integer> supplyOne = |
286 |
|
() -> Integer.valueOf(1); |
287 |
< |
static final Function<Integer, Integer> inc = |
287 |
> |
static final Function<Integer, Integer> inc = |
288 |
|
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
289 |
< |
static final BiFunction<Integer, Integer, Integer> add = |
289 |
> |
static final BiFunction<Integer, Integer, Integer> add = |
290 |
|
(Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue()); |
291 |
|
static final class IncAction implements Consumer<Integer> { |
292 |
|
int value; |
294 |
|
} |
295 |
|
static final class AddAction implements BiConsumer<Integer, Integer> { |
296 |
|
int value; |
297 |
< |
public void accept(Integer x, Integer y) { |
298 |
< |
value = x.intValue() + y.intValue(); |
297 |
> |
public void accept(Integer x, Integer y) { |
298 |
> |
value = x.intValue() + y.intValue(); |
299 |
|
} |
300 |
|
} |
301 |
|
static final class Noop implements Runnable { |
342 |
|
ran = true; throw new CFException(); |
343 |
|
} |
344 |
|
} |
345 |
< |
|
345 |
> |
|
346 |
|
// Used for explicit executor tests |
347 |
|
static final class ThreadExecutor implements Executor { |
348 |
|
public void execute(Runnable r) { |
355 |
|
} |
356 |
|
|
357 |
|
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
358 |
< |
public Integer apply(Integer x, Throwable t) { |
358 |
> |
boolean ran; |
359 |
> |
public Integer apply(Integer x, Throwable t) { |
360 |
> |
ran = true; |
361 |
|
return (t == null) ? two : three; |
362 |
|
} |
363 |
|
} |
386 |
|
* normal or exceptional completion of source |
387 |
|
*/ |
388 |
|
public void testHandle() { |
389 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
390 |
< |
IntegerHandler r = new IntegerHandler(); |
391 |
< |
CompletableFuture<Integer> g = f.handle(r); |
389 |
> |
CompletableFuture<Integer> f, g; |
390 |
> |
IntegerHandler r; |
391 |
> |
|
392 |
> |
f = new CompletableFuture<Integer>(); |
393 |
|
f.completeExceptionally(new CFException()); |
394 |
+ |
g = f.handle(r = new IntegerHandler()); |
395 |
+ |
assertTrue(r.ran); |
396 |
|
checkCompletedNormally(g, three); |
397 |
|
|
398 |
|
f = new CompletableFuture<Integer>(); |
399 |
< |
r = new IntegerHandler(); |
400 |
< |
g = f.handle(r); |
399 |
> |
g = f.handle(r = new IntegerHandler()); |
400 |
> |
assertFalse(r.ran); |
401 |
> |
f.completeExceptionally(new CFException()); |
402 |
> |
checkCompletedNormally(g, three); |
403 |
> |
assertTrue(r.ran); |
404 |
> |
|
405 |
> |
f = new CompletableFuture<Integer>(); |
406 |
|
f.complete(one); |
407 |
+ |
g = f.handle(r = new IntegerHandler()); |
408 |
+ |
assertTrue(r.ran); |
409 |
+ |
checkCompletedNormally(g, two); |
410 |
+ |
|
411 |
+ |
f = new CompletableFuture<Integer>(); |
412 |
+ |
g = f.handle(r = new IntegerHandler()); |
413 |
+ |
assertFalse(r.ran); |
414 |
+ |
f.complete(one); |
415 |
+ |
assertTrue(r.ran); |
416 |
|
checkCompletedNormally(g, two); |
417 |
|
} |
418 |
|
|
424 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
425 |
|
assertNull(f.join()); |
426 |
|
assertTrue(r.ran); |
427 |
+ |
checkCompletedNormally(f, null); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
435 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor()); |
436 |
|
assertNull(f.join()); |
437 |
|
assertTrue(r.ran); |
438 |
+ |
checkCompletedNormally(f, null); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
474 |
|
assertTrue(r.ran); |
475 |
|
} |
476 |
|
|
477 |
< |
// seq conmpletion methods |
478 |
< |
|
477 |
> |
// seq completion methods |
478 |
> |
|
479 |
|
/** |
480 |
|
* thenRun result completes normally after normal completion of source |
481 |
|
*/ |
1144 |
|
try { |
1145 |
|
g.join(); |
1146 |
|
shouldThrow(); |
1147 |
< |
} catch(Exception ok) { |
1147 |
> |
} catch (Exception ok) { |
1148 |
|
} |
1149 |
|
checkCompletedWithWrappedCFException(g); |
1150 |
|
} |
1159 |
|
f.complete(null); |
1160 |
|
checkCompletedWithWrappedCFException(g); |
1161 |
|
} |
1162 |
< |
|
1162 |
> |
|
1163 |
|
/** |
1164 |
|
* thenRunAsync result completes exceptionally if source cancelled |
1165 |
|
*/ |
1202 |
|
f.complete(null); |
1203 |
|
checkCompletedWithWrappedCFException(g); |
1204 |
|
} |
1205 |
< |
|
1205 |
> |
|
1206 |
|
/** |
1207 |
|
* thenApplyAsync result completes exceptionally if source cancelled |
1208 |
|
*/ |
1248 |
|
f.complete(null); |
1249 |
|
checkCompletedWithWrappedCFException(g); |
1250 |
|
} |
1251 |
< |
|
1251 |
> |
|
1252 |
|
/** |
1253 |
|
* thenAcceptAsync result completes exceptionally if source cancelled |
1254 |
|
*/ |
1306 |
|
f2.complete(two); |
1307 |
|
checkCompletedWithWrappedCFException(g); |
1308 |
|
} |
1309 |
< |
|
1309 |
> |
|
1310 |
|
/** |
1311 |
|
* thenCombineAsync result completes exceptionally if either source cancelled |
1312 |
|
*/ |
1317 |
|
assertTrue(f.cancel(true)); |
1318 |
|
f2.complete(two); |
1319 |
|
checkCompletedWithWrappedCancellationException(g); |
1320 |
< |
|
1320 |
> |
|
1321 |
|
f = new CompletableFuture<Integer>(); |
1322 |
|
f2 = new CompletableFuture<Integer>(); |
1323 |
|
g = f.thenCombineAsync(f2, add); |
1371 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1372 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1373 |
|
FailingBiConsumer r = new FailingBiConsumer(); |
1374 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1374 |
> |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1375 |
|
f.complete(one); |
1376 |
|
checkIncomplete(g); |
1377 |
|
f2.complete(two); |
1378 |
|
checkCompletedWithWrappedCFException(g); |
1379 |
|
} |
1380 |
< |
|
1380 |
> |
|
1381 |
|
/** |
1382 |
|
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
1383 |
|
*/ |
1389 |
|
assertTrue(f.cancel(true)); |
1390 |
|
f2.complete(two); |
1391 |
|
checkCompletedWithWrappedCancellationException(g); |
1392 |
< |
|
1392 |
> |
|
1393 |
|
r = new AddAction(); |
1394 |
|
f = new CompletableFuture<Integer>(); |
1395 |
|
f2 = new CompletableFuture<Integer>(); |
1444 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1445 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1446 |
|
FailingNoop r = new FailingNoop(); |
1447 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1447 |
> |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1448 |
|
f.complete(one); |
1449 |
|
checkIncomplete(g); |
1450 |
|
f2.complete(two); |
1451 |
|
checkCompletedWithWrappedCFException(g); |
1452 |
|
} |
1453 |
< |
|
1453 |
> |
|
1454 |
|
/** |
1455 |
|
* runAfterBothAsync result completes exceptionally if either source cancelled |
1456 |
|
*/ |
1462 |
|
assertTrue(f.cancel(true)); |
1463 |
|
f2.complete(two); |
1464 |
|
checkCompletedWithWrappedCancellationException(g); |
1465 |
< |
|
1465 |
> |
|
1466 |
|
r = new Noop(); |
1467 |
|
f = new CompletableFuture<Integer>(); |
1468 |
|
f2 = new CompletableFuture<Integer>(); |
1516 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1517 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1518 |
|
FailingFunction r = new FailingFunction(); |
1519 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1519 |
> |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1520 |
|
f.complete(one); |
1521 |
|
checkCompletedWithWrappedCFException(g); |
1522 |
|
} |
1523 |
< |
|
1523 |
> |
|
1524 |
|
/** |
1525 |
|
* applyToEitherAsync result completes exceptionally if either source cancelled |
1526 |
|
*/ |
1530 |
|
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1531 |
|
assertTrue(f.cancel(true)); |
1532 |
|
checkCompletedWithWrappedCancellationException(g); |
1533 |
< |
|
1533 |
> |
|
1534 |
|
f = new CompletableFuture<Integer>(); |
1535 |
|
f2 = new CompletableFuture<Integer>(); |
1536 |
|
assertTrue(f2.cancel(true)); |
1588 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1589 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1590 |
|
FailingConsumer r = new FailingConsumer(); |
1591 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1591 |
> |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1592 |
|
f.complete(one); |
1593 |
|
checkCompletedWithWrappedCFException(g); |
1594 |
|
} |
1595 |
< |
|
1595 |
> |
|
1596 |
|
/** |
1597 |
|
* acceptEitherAsync result completes exceptionally if either |
1598 |
|
* source cancelled |
1604 |
|
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1605 |
|
assertTrue(f.cancel(true)); |
1606 |
|
checkCompletedWithWrappedCancellationException(g); |
1607 |
< |
|
1607 |
> |
|
1608 |
|
r = new IncAction(); |
1609 |
|
f = new CompletableFuture<Integer>(); |
1610 |
|
f2 = new CompletableFuture<Integer>(); |
1663 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1664 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1665 |
|
FailingNoop r = new FailingNoop(); |
1666 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1666 |
> |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1667 |
|
f.complete(one); |
1668 |
|
checkCompletedWithWrappedCFException(g); |
1669 |
|
} |
1670 |
< |
|
1670 |
> |
|
1671 |
|
/** |
1672 |
|
* runAfterEitherAsync result completes exceptionally if either |
1673 |
|
* source cancelled |
1679 |
|
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1680 |
|
assertTrue(f.cancel(true)); |
1681 |
|
checkCompletedWithWrappedCancellationException(g); |
1682 |
< |
|
1682 |
> |
|
1683 |
|
r = new Noop(); |
1684 |
|
f = new CompletableFuture<Integer>(); |
1685 |
|
f2 = new CompletableFuture<Integer>(); |
1689 |
|
} |
1690 |
|
|
1691 |
|
/** |
1692 |
< |
* thenCompse result completes normally after normal completion of source |
1692 |
> |
* thenComposeAsync result completes normally after normal |
1693 |
> |
* completion of source |
1694 |
|
*/ |
1695 |
|
public void testThenComposeAsync() { |
1696 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1701 |
|
} |
1702 |
|
|
1703 |
|
/** |
1704 |
< |
* thenComposeAsync result completes exceptionally after exceptional |
1705 |
< |
* completion of source |
1704 |
> |
* thenComposeAsync result completes exceptionally after |
1705 |
> |
* exceptional completion of source |
1706 |
|
*/ |
1707 |
|
public void testThenComposeAsync2() { |
1708 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1735 |
|
} |
1736 |
|
|
1737 |
|
|
1738 |
< |
// aaync with explicit executors |
1738 |
> |
// async with explicit executors |
1739 |
|
|
1740 |
|
/** |
1741 |
|
* thenRunAsync result completes normally after normal completion of source |
1767 |
|
try { |
1768 |
|
g.join(); |
1769 |
|
shouldThrow(); |
1770 |
< |
} catch(Exception ok) { |
1770 |
> |
} catch (Exception ok) { |
1771 |
|
} |
1772 |
|
checkCompletedWithWrappedCFException(g); |
1773 |
|
} |
1782 |
|
f.complete(null); |
1783 |
|
checkCompletedWithWrappedCFException(g); |
1784 |
|
} |
1785 |
< |
|
1785 |
> |
|
1786 |
|
/** |
1787 |
|
* thenRunAsync result completes exceptionally if source cancelled |
1788 |
|
*/ |
1825 |
|
f.complete(null); |
1826 |
|
checkCompletedWithWrappedCFException(g); |
1827 |
|
} |
1828 |
< |
|
1828 |
> |
|
1829 |
|
/** |
1830 |
|
* thenApplyAsync result completes exceptionally if source cancelled |
1831 |
|
*/ |
1871 |
|
f.complete(null); |
1872 |
|
checkCompletedWithWrappedCFException(g); |
1873 |
|
} |
1874 |
< |
|
1874 |
> |
|
1875 |
|
/** |
1876 |
|
* thenAcceptAsync result completes exceptionally if source cancelled |
1877 |
|
*/ |
1929 |
|
f2.complete(two); |
1930 |
|
checkCompletedWithWrappedCFException(g); |
1931 |
|
} |
1932 |
< |
|
1932 |
> |
|
1933 |
|
/** |
1934 |
|
* thenCombineAsync result completes exceptionally if either source cancelled |
1935 |
|
*/ |
1940 |
|
assertTrue(f.cancel(true)); |
1941 |
|
f2.complete(two); |
1942 |
|
checkCompletedWithWrappedCancellationException(g); |
1943 |
< |
|
1943 |
> |
|
1944 |
|
f = new CompletableFuture<Integer>(); |
1945 |
|
f2 = new CompletableFuture<Integer>(); |
1946 |
|
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1994 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1995 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1996 |
|
FailingBiConsumer r = new FailingBiConsumer(); |
1997 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1997 |
> |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1998 |
|
f.complete(one); |
1999 |
|
checkIncomplete(g); |
2000 |
|
f2.complete(two); |
2001 |
|
checkCompletedWithWrappedCFException(g); |
2002 |
|
} |
2003 |
< |
|
2003 |
> |
|
2004 |
|
/** |
2005 |
|
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
2006 |
|
*/ |
2012 |
|
assertTrue(f.cancel(true)); |
2013 |
|
f2.complete(two); |
2014 |
|
checkCompletedWithWrappedCancellationException(g); |
2015 |
< |
|
2015 |
> |
|
2016 |
|
r = new AddAction(); |
2017 |
|
f = new CompletableFuture<Integer>(); |
2018 |
|
f2 = new CompletableFuture<Integer>(); |
2067 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2068 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2069 |
|
FailingNoop r = new FailingNoop(); |
2070 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2070 |
> |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2071 |
|
f.complete(one); |
2072 |
|
checkIncomplete(g); |
2073 |
|
f2.complete(two); |
2074 |
|
checkCompletedWithWrappedCFException(g); |
2075 |
|
} |
2076 |
< |
|
2076 |
> |
|
2077 |
|
/** |
2078 |
|
* runAfterBothAsync result completes exceptionally if either source cancelled |
2079 |
|
*/ |
2085 |
|
assertTrue(f.cancel(true)); |
2086 |
|
f2.complete(two); |
2087 |
|
checkCompletedWithWrappedCancellationException(g); |
2088 |
< |
|
2088 |
> |
|
2089 |
|
r = new Noop(); |
2090 |
|
f = new CompletableFuture<Integer>(); |
2091 |
|
f2 = new CompletableFuture<Integer>(); |
2139 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2140 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2141 |
|
FailingFunction r = new FailingFunction(); |
2142 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2142 |
> |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2143 |
|
f.complete(one); |
2144 |
|
checkCompletedWithWrappedCFException(g); |
2145 |
|
} |
2146 |
< |
|
2146 |
> |
|
2147 |
|
/** |
2148 |
|
* applyToEitherAsync result completes exceptionally if either source cancelled |
2149 |
|
*/ |
2153 |
|
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2154 |
|
assertTrue(f.cancel(true)); |
2155 |
|
checkCompletedWithWrappedCancellationException(g); |
2156 |
< |
|
2156 |
> |
|
2157 |
|
f = new CompletableFuture<Integer>(); |
2158 |
|
f2 = new CompletableFuture<Integer>(); |
2159 |
|
assertTrue(f2.cancel(true)); |
2211 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2212 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2213 |
|
FailingConsumer r = new FailingConsumer(); |
2214 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2214 |
> |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2215 |
|
f.complete(one); |
2216 |
|
checkCompletedWithWrappedCFException(g); |
2217 |
|
} |
2218 |
< |
|
2218 |
> |
|
2219 |
|
/** |
2220 |
|
* acceptEitherAsync result completes exceptionally if either |
2221 |
|
* source cancelled |
2227 |
|
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2228 |
|
assertTrue(f.cancel(true)); |
2229 |
|
checkCompletedWithWrappedCancellationException(g); |
2230 |
< |
|
2230 |
> |
|
2231 |
|
r = new IncAction(); |
2232 |
|
f = new CompletableFuture<Integer>(); |
2233 |
|
f2 = new CompletableFuture<Integer>(); |
2286 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2287 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2288 |
|
FailingNoop r = new FailingNoop(); |
2289 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2289 |
> |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2290 |
|
f.complete(one); |
2291 |
|
checkCompletedWithWrappedCFException(g); |
2292 |
|
} |
2293 |
< |
|
2293 |
> |
|
2294 |
|
/** |
2295 |
|
* runAfterEitherAsync result completes exceptionally if either |
2296 |
|
* source cancelled |
2302 |
|
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2303 |
|
assertTrue(f.cancel(true)); |
2304 |
|
checkCompletedWithWrappedCancellationException(g); |
2305 |
< |
|
2305 |
> |
|
2306 |
|
r = new Noop(); |
2307 |
|
f = new CompletableFuture<Integer>(); |
2308 |
|
f2 = new CompletableFuture<Integer>(); |
2312 |
|
} |
2313 |
|
|
2314 |
|
/** |
2315 |
< |
* thenCompse result completes normally after normal completion of source |
2315 |
> |
* thenComposeAsync result completes normally after normal |
2316 |
> |
* completion of source |
2317 |
|
*/ |
2318 |
|
public void testThenComposeAsyncE() { |
2319 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2324 |
|
} |
2325 |
|
|
2326 |
|
/** |
2327 |
< |
* thenComposeAsync result completes exceptionally after exceptional |
2328 |
< |
* completion of source |
2327 |
> |
* thenComposeAsync result completes exceptionally after |
2328 |
> |
* exceptional completion of source |
2329 |
|
*/ |
2330 |
|
public void testThenComposeAsync2E() { |
2331 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2357 |
|
checkCompletedWithWrappedCancellationException(g); |
2358 |
|
} |
2359 |
|
|
2360 |
< |
// other static methods |
2360 |
> |
// other static methods |
2361 |
|
|
2362 |
|
/** |
2363 |
|
* allOf(no component futures) returns a future completed normally |
2374 |
|
public void testAllOf() throws Exception { |
2375 |
|
for (int k = 1; k < 20; ++k) { |
2376 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2377 |
< |
for (int i = 0; i < k; ++i) |
2377 |
> |
for (int i = 0; i < k; ++i) |
2378 |
|
fs[i] = new CompletableFuture<Integer>(); |
2379 |
< |
CompletableFuture<?> f = CompletableFuture.allOf(fs); |
2379 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2380 |
|
for (int i = 0; i < k; ++i) { |
2381 |
|
checkIncomplete(f); |
2382 |
|
fs[i].complete(one); |
2383 |
|
} |
2384 |
< |
assertTrue(f.isDone()); |
2365 |
< |
assertFalse(f.isCancelled()); |
2384 |
> |
checkCompletedNormally(f, null); |
2385 |
|
} |
2386 |
|
} |
2387 |
|
|
2394 |
|
} |
2395 |
|
|
2396 |
|
/** |
2397 |
< |
* allOf returns a future completed when any components complete |
2397 |
> |
* anyOf returns a future completed when any components complete |
2398 |
|
*/ |
2399 |
|
public void testAnyOf() throws Exception { |
2400 |
|
for (int k = 1; k < 20; ++k) { |
2401 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2402 |
< |
for (int i = 0; i < k; ++i) |
2402 |
> |
for (int i = 0; i < k; ++i) |
2403 |
|
fs[i] = new CompletableFuture<Integer>(); |
2404 |
< |
CompletableFuture<?> f = CompletableFuture.anyOf(fs); |
2404 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2405 |
|
checkIncomplete(f); |
2406 |
|
for (int i = 0; i < k; ++i) { |
2407 |
|
fs[i].complete(one); |
2408 |
< |
assertTrue(f.isDone()); |
2408 |
> |
checkCompletedNormally(f, one); |
2409 |
|
} |
2410 |
|
} |
2411 |
|
} |
2416 |
|
public void testNPE() { |
2417 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2418 |
|
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
2419 |
< |
CompletableFuture h; |
2420 |
< |
try { h = f.thenApply(null); } catch (NullPointerException ok) {} |
2421 |
< |
try { h = f.thenAccept(null); } catch (NullPointerException ok) {} |
2422 |
< |
try { h = f.thenRun(null); } catch (NullPointerException ok) {} |
2423 |
< |
try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {} |
2424 |
< |
try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {} |
2425 |
< |
try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {} |
2426 |
< |
try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {} |
2427 |
< |
try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {} |
2428 |
< |
try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {} |
2429 |
< |
try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {} |
2430 |
< |
try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {} |
2431 |
< |
try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {} |
2432 |
< |
try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {} |
2433 |
< |
try { h = f.exceptionally(null); } catch (NullPointerException ok) {} |
2434 |
< |
try { h = f.handle(null); } catch (NullPointerException ok) {} |
2435 |
< |
try { h = f.thenCompose(null); } catch (NullPointerException ok) {} |
2436 |
< |
|
2437 |
< |
try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {} |
2438 |
< |
try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {} |
2439 |
< |
try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {} |
2440 |
< |
try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {} |
2441 |
< |
try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {} |
2442 |
< |
try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {} |
2443 |
< |
try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {} |
2444 |
< |
try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {} |
2445 |
< |
try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {} |
2446 |
< |
try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {} |
2447 |
< |
try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {} |
2448 |
< |
try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {} |
2449 |
< |
try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {} |
2419 |
> |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2420 |
> |
CompletableFuture<?> h; |
2421 |
> |
Executor exec = new ThreadExecutor(); |
2422 |
> |
|
2423 |
> |
Runnable[] throwingActions = { |
2424 |
> |
() -> { CompletableFuture.supplyAsync(null); }, |
2425 |
> |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
2426 |
> |
() -> { CompletableFuture.supplyAsync(() -> one, null); }, |
2427 |
> |
|
2428 |
> |
() -> { CompletableFuture.runAsync(null); }, |
2429 |
> |
() -> { CompletableFuture.runAsync(null, exec); }, |
2430 |
> |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
2431 |
> |
|
2432 |
> |
() -> { f.completeExceptionally(null); }, |
2433 |
> |
|
2434 |
> |
() -> { f.thenApply(null); }, |
2435 |
> |
() -> { f.thenApplyAsync(null); }, |
2436 |
> |
() -> { f.thenApplyAsync((x) -> x, null); }, |
2437 |
> |
() -> { f.thenApplyAsync(null, exec); }, |
2438 |
> |
|
2439 |
> |
() -> { f.thenAccept(null); }, |
2440 |
> |
() -> { f.thenAcceptAsync(null); }, |
2441 |
> |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
2442 |
> |
() -> { f.thenAcceptAsync(null, exec); }, |
2443 |
> |
|
2444 |
> |
() -> { f.thenRun(null); }, |
2445 |
> |
() -> { f.thenRunAsync(null); }, |
2446 |
> |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
2447 |
> |
() -> { f.thenRunAsync(null, exec); }, |
2448 |
> |
|
2449 |
> |
() -> { f.thenCombine(g, null); }, |
2450 |
> |
() -> { f.thenCombineAsync(g, null); }, |
2451 |
> |
() -> { f.thenCombineAsync(g, null, exec); }, |
2452 |
> |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
2453 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
2454 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
2455 |
> |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
2456 |
> |
|
2457 |
> |
() -> { f.thenAcceptBoth(g, null); }, |
2458 |
> |
() -> { f.thenAcceptBothAsync(g, null); }, |
2459 |
> |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
2460 |
> |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
2461 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
2462 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
2463 |
> |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
2464 |
> |
|
2465 |
> |
() -> { f.runAfterBoth(g, null); }, |
2466 |
> |
() -> { f.runAfterBothAsync(g, null); }, |
2467 |
> |
() -> { f.runAfterBothAsync(g, null, exec); }, |
2468 |
> |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
2469 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
2470 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
2471 |
> |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
2472 |
> |
|
2473 |
> |
() -> { f.applyToEither(g, null); }, |
2474 |
> |
() -> { f.applyToEitherAsync(g, null); }, |
2475 |
> |
() -> { f.applyToEitherAsync(g, null, exec); }, |
2476 |
> |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
2477 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
2478 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
2479 |
> |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
2480 |
> |
|
2481 |
> |
() -> { f.acceptEither(g, null); }, |
2482 |
> |
() -> { f.acceptEitherAsync(g, null); }, |
2483 |
> |
() -> { f.acceptEitherAsync(g, null, exec); }, |
2484 |
> |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
2485 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
2486 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
2487 |
> |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
2488 |
> |
|
2489 |
> |
() -> { f.runAfterEither(g, null); }, |
2490 |
> |
() -> { f.runAfterEitherAsync(g, null); }, |
2491 |
> |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
2492 |
> |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
2493 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
2494 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
2495 |
> |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
2496 |
> |
|
2497 |
> |
() -> { f.thenCompose(null); }, |
2498 |
> |
() -> { f.thenComposeAsync(null); }, |
2499 |
> |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
2500 |
> |
() -> { f.thenComposeAsync(null, exec); }, |
2501 |
> |
|
2502 |
> |
() -> { f.exceptionally(null); }, |
2503 |
> |
|
2504 |
> |
() -> { f.handle(null); }, |
2505 |
> |
|
2506 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
2507 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
2508 |
> |
() -> { CompletableFuture.allOf(f, null); }, |
2509 |
> |
() -> { CompletableFuture.allOf(null, f); }, |
2510 |
> |
|
2511 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
2512 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
2513 |
> |
() -> { CompletableFuture.anyOf(f, null); }, |
2514 |
> |
() -> { CompletableFuture.anyOf(null, f); }, |
2515 |
> |
|
2516 |
> |
// TODO: Crashes javac with lambda-8-2013-03-31... |
2517 |
> |
//() -> { CompletableFuture<?> x = f.thenAccept(null); }, |
2518 |
> |
//() -> { CompletableFuture<Void> x = f.thenRun(null); }, |
2519 |
> |
//() -> { CompletableFuture<Integer> x = f.thenApply(() -> { ; }); }, |
2520 |
> |
}; |
2521 |
|
|
2522 |
+ |
assertThrows(NullPointerException.class, throwingActions); |
2523 |
|
} |
2524 |
|
|
2434 |
– |
|
2525 |
|
} |