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 |
+ |
AtomicInteger count = new AtomicInteger(0); |
349 |
+ |
|
350 |
|
public void execute(Runnable r) { |
351 |
+ |
count.getAndIncrement(); |
352 |
|
new Thread(r).start(); |
353 |
|
} |
354 |
|
} |
358 |
|
} |
359 |
|
|
360 |
|
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
361 |
< |
public Integer apply(Integer x, Throwable t) { |
361 |
> |
boolean ran; |
362 |
> |
public Integer apply(Integer x, Throwable t) { |
363 |
> |
ran = true; |
364 |
|
return (t == null) ? two : three; |
365 |
|
} |
366 |
|
} |
389 |
|
* normal or exceptional completion of source |
390 |
|
*/ |
391 |
|
public void testHandle() { |
392 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
393 |
< |
IntegerHandler r = new IntegerHandler(); |
394 |
< |
CompletableFuture<Integer> g = f.handle(r); |
392 |
> |
CompletableFuture<Integer> f, g; |
393 |
> |
IntegerHandler r; |
394 |
> |
|
395 |
> |
f = new CompletableFuture<Integer>(); |
396 |
> |
f.completeExceptionally(new CFException()); |
397 |
> |
g = f.handle(r = new IntegerHandler()); |
398 |
> |
assertTrue(r.ran); |
399 |
> |
checkCompletedNormally(g, three); |
400 |
> |
|
401 |
> |
f = new CompletableFuture<Integer>(); |
402 |
> |
g = f.handle(r = new IntegerHandler()); |
403 |
> |
assertFalse(r.ran); |
404 |
|
f.completeExceptionally(new CFException()); |
405 |
|
checkCompletedNormally(g, three); |
406 |
+ |
assertTrue(r.ran); |
407 |
|
|
408 |
|
f = new CompletableFuture<Integer>(); |
397 |
– |
r = new IntegerHandler(); |
398 |
– |
g = f.handle(r); |
409 |
|
f.complete(one); |
410 |
+ |
g = f.handle(r = new IntegerHandler()); |
411 |
+ |
assertTrue(r.ran); |
412 |
+ |
checkCompletedNormally(g, two); |
413 |
+ |
|
414 |
+ |
f = new CompletableFuture<Integer>(); |
415 |
+ |
g = f.handle(r = new IntegerHandler()); |
416 |
+ |
assertFalse(r.ran); |
417 |
+ |
f.complete(one); |
418 |
+ |
assertTrue(r.ran); |
419 |
|
checkCompletedNormally(g, two); |
420 |
|
} |
421 |
|
|
427 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
428 |
|
assertNull(f.join()); |
429 |
|
assertTrue(r.ran); |
430 |
+ |
checkCompletedNormally(f, null); |
431 |
|
} |
432 |
|
|
433 |
|
/** |
435 |
|
*/ |
436 |
|
public void testRunAsync2() { |
437 |
|
Noop r = new Noop(); |
438 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor()); |
438 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
439 |
> |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
440 |
|
assertNull(f.join()); |
441 |
|
assertTrue(r.ran); |
442 |
+ |
checkCompletedNormally(f, null); |
443 |
+ |
assertEquals(1, exec.count.get()); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
457 |
|
* supplyAsync completes with result of supplier |
458 |
|
*/ |
459 |
|
public void testSupplyAsync() { |
460 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne); |
460 |
> |
CompletableFuture<Integer> f; |
461 |
> |
f = CompletableFuture.supplyAsync(supplyOne); |
462 |
|
assertEquals(f.join(), one); |
463 |
+ |
checkCompletedNormally(f, one); |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
|
* supplyAsync with executor completes with result of supplier |
468 |
|
*/ |
469 |
|
public void testSupplyAsync2() { |
470 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
470 |
> |
CompletableFuture<Integer> f; |
471 |
> |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
472 |
|
assertEquals(f.join(), one); |
473 |
+ |
checkCompletedNormally(f, one); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
483 |
|
assertTrue(r.ran); |
484 |
|
} |
485 |
|
|
486 |
< |
// seq conmpletion methods |
487 |
< |
|
486 |
> |
// seq completion methods |
487 |
> |
|
488 |
|
/** |
489 |
|
* thenRun result completes normally after normal completion of source |
490 |
|
*/ |
1153 |
|
try { |
1154 |
|
g.join(); |
1155 |
|
shouldThrow(); |
1156 |
< |
} catch(Exception ok) { |
1156 |
> |
} catch (Exception ok) { |
1157 |
|
} |
1158 |
|
checkCompletedWithWrappedCFException(g); |
1159 |
|
} |
1168 |
|
f.complete(null); |
1169 |
|
checkCompletedWithWrappedCFException(g); |
1170 |
|
} |
1171 |
< |
|
1171 |
> |
|
1172 |
|
/** |
1173 |
|
* thenRunAsync result completes exceptionally if source cancelled |
1174 |
|
*/ |
1211 |
|
f.complete(null); |
1212 |
|
checkCompletedWithWrappedCFException(g); |
1213 |
|
} |
1214 |
< |
|
1214 |
> |
|
1215 |
|
/** |
1216 |
|
* thenApplyAsync result completes exceptionally if source cancelled |
1217 |
|
*/ |
1257 |
|
f.complete(null); |
1258 |
|
checkCompletedWithWrappedCFException(g); |
1259 |
|
} |
1260 |
< |
|
1260 |
> |
|
1261 |
|
/** |
1262 |
|
* thenAcceptAsync result completes exceptionally if source cancelled |
1263 |
|
*/ |
1315 |
|
f2.complete(two); |
1316 |
|
checkCompletedWithWrappedCFException(g); |
1317 |
|
} |
1318 |
< |
|
1318 |
> |
|
1319 |
|
/** |
1320 |
|
* thenCombineAsync result completes exceptionally if either source cancelled |
1321 |
|
*/ |
1326 |
|
assertTrue(f.cancel(true)); |
1327 |
|
f2.complete(two); |
1328 |
|
checkCompletedWithWrappedCancellationException(g); |
1329 |
< |
|
1329 |
> |
|
1330 |
|
f = new CompletableFuture<Integer>(); |
1331 |
|
f2 = new CompletableFuture<Integer>(); |
1332 |
|
g = f.thenCombineAsync(f2, add); |
1380 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1381 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1382 |
|
FailingBiConsumer r = new FailingBiConsumer(); |
1383 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1383 |
> |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1384 |
|
f.complete(one); |
1385 |
|
checkIncomplete(g); |
1386 |
|
f2.complete(two); |
1387 |
|
checkCompletedWithWrappedCFException(g); |
1388 |
|
} |
1389 |
< |
|
1389 |
> |
|
1390 |
|
/** |
1391 |
|
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
1392 |
|
*/ |
1398 |
|
assertTrue(f.cancel(true)); |
1399 |
|
f2.complete(two); |
1400 |
|
checkCompletedWithWrappedCancellationException(g); |
1401 |
< |
|
1401 |
> |
|
1402 |
|
r = new AddAction(); |
1403 |
|
f = new CompletableFuture<Integer>(); |
1404 |
|
f2 = new CompletableFuture<Integer>(); |
1453 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1454 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1455 |
|
FailingNoop r = new FailingNoop(); |
1456 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1456 |
> |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1457 |
|
f.complete(one); |
1458 |
|
checkIncomplete(g); |
1459 |
|
f2.complete(two); |
1460 |
|
checkCompletedWithWrappedCFException(g); |
1461 |
|
} |
1462 |
< |
|
1462 |
> |
|
1463 |
|
/** |
1464 |
|
* runAfterBothAsync result completes exceptionally if either source cancelled |
1465 |
|
*/ |
1471 |
|
assertTrue(f.cancel(true)); |
1472 |
|
f2.complete(two); |
1473 |
|
checkCompletedWithWrappedCancellationException(g); |
1474 |
< |
|
1474 |
> |
|
1475 |
|
r = new Noop(); |
1476 |
|
f = new CompletableFuture<Integer>(); |
1477 |
|
f2 = new CompletableFuture<Integer>(); |
1525 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1526 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1527 |
|
FailingFunction r = new FailingFunction(); |
1528 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1528 |
> |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1529 |
|
f.complete(one); |
1530 |
|
checkCompletedWithWrappedCFException(g); |
1531 |
|
} |
1532 |
< |
|
1532 |
> |
|
1533 |
|
/** |
1534 |
|
* applyToEitherAsync result completes exceptionally if either source cancelled |
1535 |
|
*/ |
1539 |
|
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1540 |
|
assertTrue(f.cancel(true)); |
1541 |
|
checkCompletedWithWrappedCancellationException(g); |
1542 |
< |
|
1542 |
> |
|
1543 |
|
f = new CompletableFuture<Integer>(); |
1544 |
|
f2 = new CompletableFuture<Integer>(); |
1545 |
|
assertTrue(f2.cancel(true)); |
1597 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1598 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1599 |
|
FailingConsumer r = new FailingConsumer(); |
1600 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1600 |
> |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1601 |
|
f.complete(one); |
1602 |
|
checkCompletedWithWrappedCFException(g); |
1603 |
|
} |
1604 |
< |
|
1604 |
> |
|
1605 |
|
/** |
1606 |
|
* acceptEitherAsync result completes exceptionally if either |
1607 |
|
* source cancelled |
1613 |
|
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1614 |
|
assertTrue(f.cancel(true)); |
1615 |
|
checkCompletedWithWrappedCancellationException(g); |
1616 |
< |
|
1616 |
> |
|
1617 |
|
r = new IncAction(); |
1618 |
|
f = new CompletableFuture<Integer>(); |
1619 |
|
f2 = new CompletableFuture<Integer>(); |
1672 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1673 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1674 |
|
FailingNoop r = new FailingNoop(); |
1675 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1675 |
> |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1676 |
|
f.complete(one); |
1677 |
|
checkCompletedWithWrappedCFException(g); |
1678 |
|
} |
1679 |
< |
|
1679 |
> |
|
1680 |
|
/** |
1681 |
|
* runAfterEitherAsync result completes exceptionally if either |
1682 |
|
* source cancelled |
1688 |
|
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1689 |
|
assertTrue(f.cancel(true)); |
1690 |
|
checkCompletedWithWrappedCancellationException(g); |
1691 |
< |
|
1691 |
> |
|
1692 |
|
r = new Noop(); |
1693 |
|
f = new CompletableFuture<Integer>(); |
1694 |
|
f2 = new CompletableFuture<Integer>(); |
1698 |
|
} |
1699 |
|
|
1700 |
|
/** |
1701 |
< |
* thenCompse result completes normally after normal completion of source |
1701 |
> |
* thenComposeAsync result completes normally after normal |
1702 |
> |
* completion of source |
1703 |
|
*/ |
1704 |
|
public void testThenComposeAsync() { |
1705 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1710 |
|
} |
1711 |
|
|
1712 |
|
/** |
1713 |
< |
* thenComposeAsync result completes exceptionally after exceptional |
1714 |
< |
* completion of source |
1713 |
> |
* thenComposeAsync result completes exceptionally after |
1714 |
> |
* exceptional completion of source |
1715 |
|
*/ |
1716 |
|
public void testThenComposeAsync2() { |
1717 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1744 |
|
} |
1745 |
|
|
1746 |
|
|
1747 |
< |
// aaync with explicit executors |
1747 |
> |
// async with explicit executors |
1748 |
|
|
1749 |
|
/** |
1750 |
|
* thenRunAsync result completes normally after normal completion of source |
1776 |
|
try { |
1777 |
|
g.join(); |
1778 |
|
shouldThrow(); |
1779 |
< |
} catch(Exception ok) { |
1779 |
> |
} catch (Exception ok) { |
1780 |
|
} |
1781 |
|
checkCompletedWithWrappedCFException(g); |
1782 |
|
} |
1791 |
|
f.complete(null); |
1792 |
|
checkCompletedWithWrappedCFException(g); |
1793 |
|
} |
1794 |
< |
|
1794 |
> |
|
1795 |
|
/** |
1796 |
|
* thenRunAsync result completes exceptionally if source cancelled |
1797 |
|
*/ |
1834 |
|
f.complete(null); |
1835 |
|
checkCompletedWithWrappedCFException(g); |
1836 |
|
} |
1837 |
< |
|
1837 |
> |
|
1838 |
|
/** |
1839 |
|
* thenApplyAsync result completes exceptionally if source cancelled |
1840 |
|
*/ |
1880 |
|
f.complete(null); |
1881 |
|
checkCompletedWithWrappedCFException(g); |
1882 |
|
} |
1883 |
< |
|
1883 |
> |
|
1884 |
|
/** |
1885 |
|
* thenAcceptAsync result completes exceptionally if source cancelled |
1886 |
|
*/ |
1938 |
|
f2.complete(two); |
1939 |
|
checkCompletedWithWrappedCFException(g); |
1940 |
|
} |
1941 |
< |
|
1941 |
> |
|
1942 |
|
/** |
1943 |
|
* thenCombineAsync result completes exceptionally if either source cancelled |
1944 |
|
*/ |
1949 |
|
assertTrue(f.cancel(true)); |
1950 |
|
f2.complete(two); |
1951 |
|
checkCompletedWithWrappedCancellationException(g); |
1952 |
< |
|
1952 |
> |
|
1953 |
|
f = new CompletableFuture<Integer>(); |
1954 |
|
f2 = new CompletableFuture<Integer>(); |
1955 |
|
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
2003 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2004 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2005 |
|
FailingBiConsumer r = new FailingBiConsumer(); |
2006 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2006 |
> |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2007 |
|
f.complete(one); |
2008 |
|
checkIncomplete(g); |
2009 |
|
f2.complete(two); |
2010 |
|
checkCompletedWithWrappedCFException(g); |
2011 |
|
} |
2012 |
< |
|
2012 |
> |
|
2013 |
|
/** |
2014 |
|
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
2015 |
|
*/ |
2021 |
|
assertTrue(f.cancel(true)); |
2022 |
|
f2.complete(two); |
2023 |
|
checkCompletedWithWrappedCancellationException(g); |
2024 |
< |
|
2024 |
> |
|
2025 |
|
r = new AddAction(); |
2026 |
|
f = new CompletableFuture<Integer>(); |
2027 |
|
f2 = new CompletableFuture<Integer>(); |
2076 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2077 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2078 |
|
FailingNoop r = new FailingNoop(); |
2079 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2079 |
> |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2080 |
|
f.complete(one); |
2081 |
|
checkIncomplete(g); |
2082 |
|
f2.complete(two); |
2083 |
|
checkCompletedWithWrappedCFException(g); |
2084 |
|
} |
2085 |
< |
|
2085 |
> |
|
2086 |
|
/** |
2087 |
|
* runAfterBothAsync result completes exceptionally if either source cancelled |
2088 |
|
*/ |
2094 |
|
assertTrue(f.cancel(true)); |
2095 |
|
f2.complete(two); |
2096 |
|
checkCompletedWithWrappedCancellationException(g); |
2097 |
< |
|
2097 |
> |
|
2098 |
|
r = new Noop(); |
2099 |
|
f = new CompletableFuture<Integer>(); |
2100 |
|
f2 = new CompletableFuture<Integer>(); |
2148 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2149 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2150 |
|
FailingFunction r = new FailingFunction(); |
2151 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2151 |
> |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2152 |
|
f.complete(one); |
2153 |
|
checkCompletedWithWrappedCFException(g); |
2154 |
|
} |
2155 |
< |
|
2155 |
> |
|
2156 |
|
/** |
2157 |
|
* applyToEitherAsync result completes exceptionally if either source cancelled |
2158 |
|
*/ |
2162 |
|
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2163 |
|
assertTrue(f.cancel(true)); |
2164 |
|
checkCompletedWithWrappedCancellationException(g); |
2165 |
< |
|
2165 |
> |
|
2166 |
|
f = new CompletableFuture<Integer>(); |
2167 |
|
f2 = new CompletableFuture<Integer>(); |
2168 |
|
assertTrue(f2.cancel(true)); |
2220 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2221 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2222 |
|
FailingConsumer r = new FailingConsumer(); |
2223 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2223 |
> |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2224 |
|
f.complete(one); |
2225 |
|
checkCompletedWithWrappedCFException(g); |
2226 |
|
} |
2227 |
< |
|
2227 |
> |
|
2228 |
|
/** |
2229 |
|
* acceptEitherAsync result completes exceptionally if either |
2230 |
|
* source cancelled |
2236 |
|
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2237 |
|
assertTrue(f.cancel(true)); |
2238 |
|
checkCompletedWithWrappedCancellationException(g); |
2239 |
< |
|
2239 |
> |
|
2240 |
|
r = new IncAction(); |
2241 |
|
f = new CompletableFuture<Integer>(); |
2242 |
|
f2 = new CompletableFuture<Integer>(); |
2295 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2296 |
|
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2297 |
|
FailingNoop r = new FailingNoop(); |
2298 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2298 |
> |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2299 |
|
f.complete(one); |
2300 |
|
checkCompletedWithWrappedCFException(g); |
2301 |
|
} |
2302 |
< |
|
2302 |
> |
|
2303 |
|
/** |
2304 |
|
* runAfterEitherAsync result completes exceptionally if either |
2305 |
|
* source cancelled |
2311 |
|
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2312 |
|
assertTrue(f.cancel(true)); |
2313 |
|
checkCompletedWithWrappedCancellationException(g); |
2314 |
< |
|
2314 |
> |
|
2315 |
|
r = new Noop(); |
2316 |
|
f = new CompletableFuture<Integer>(); |
2317 |
|
f2 = new CompletableFuture<Integer>(); |
2321 |
|
} |
2322 |
|
|
2323 |
|
/** |
2324 |
< |
* thenCompse result completes normally after normal completion of source |
2324 |
> |
* thenComposeAsync result completes normally after normal |
2325 |
> |
* completion of source |
2326 |
|
*/ |
2327 |
|
public void testThenComposeAsyncE() { |
2328 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2333 |
|
} |
2334 |
|
|
2335 |
|
/** |
2336 |
< |
* thenComposeAsync result completes exceptionally after exceptional |
2337 |
< |
* completion of source |
2336 |
> |
* thenComposeAsync result completes exceptionally after |
2337 |
> |
* exceptional completion of source |
2338 |
|
*/ |
2339 |
|
public void testThenComposeAsync2E() { |
2340 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2366 |
|
checkCompletedWithWrappedCancellationException(g); |
2367 |
|
} |
2368 |
|
|
2369 |
< |
// other static methods |
2369 |
> |
// other static methods |
2370 |
|
|
2371 |
|
/** |
2372 |
|
* allOf(no component futures) returns a future completed normally |
2383 |
|
public void testAllOf() throws Exception { |
2384 |
|
for (int k = 1; k < 20; ++k) { |
2385 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2386 |
< |
for (int i = 0; i < k; ++i) |
2386 |
> |
for (int i = 0; i < k; ++i) |
2387 |
|
fs[i] = new CompletableFuture<Integer>(); |
2388 |
< |
CompletableFuture<?> f = CompletableFuture.allOf(fs); |
2388 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2389 |
|
for (int i = 0; i < k; ++i) { |
2390 |
|
checkIncomplete(f); |
2391 |
|
fs[i].complete(one); |
2392 |
|
} |
2393 |
< |
assertTrue(f.isDone()); |
2365 |
< |
assertFalse(f.isCancelled()); |
2393 |
> |
checkCompletedNormally(f, null); |
2394 |
|
} |
2395 |
|
} |
2396 |
|
|
2403 |
|
} |
2404 |
|
|
2405 |
|
/** |
2406 |
< |
* allOf returns a future completed when any components complete |
2406 |
> |
* anyOf returns a future completed when any components complete |
2407 |
|
*/ |
2408 |
|
public void testAnyOf() throws Exception { |
2409 |
|
for (int k = 1; k < 20; ++k) { |
2410 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2411 |
< |
for (int i = 0; i < k; ++i) |
2411 |
> |
for (int i = 0; i < k; ++i) |
2412 |
|
fs[i] = new CompletableFuture<Integer>(); |
2413 |
< |
CompletableFuture<?> f = CompletableFuture.anyOf(fs); |
2413 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2414 |
|
checkIncomplete(f); |
2415 |
|
for (int i = 0; i < k; ++i) { |
2416 |
|
fs[i].complete(one); |
2417 |
< |
assertTrue(f.isDone()); |
2417 |
> |
checkCompletedNormally(f, one); |
2418 |
|
} |
2419 |
|
} |
2420 |
|
} |
2425 |
|
public void testNPE() { |
2426 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2427 |
|
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
2428 |
< |
CompletableFuture h; |
2429 |
< |
try { h = f.thenApply(null); } catch (NullPointerException ok) {} |
2430 |
< |
try { h = f.thenAccept(null); } catch (NullPointerException ok) {} |
2431 |
< |
try { h = f.thenRun(null); } catch (NullPointerException ok) {} |
2432 |
< |
try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {} |
2433 |
< |
try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {} |
2434 |
< |
try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {} |
2435 |
< |
try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {} |
2436 |
< |
try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {} |
2437 |
< |
try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {} |
2438 |
< |
try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {} |
2439 |
< |
try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {} |
2440 |
< |
try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {} |
2441 |
< |
try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {} |
2442 |
< |
try { h = f.exceptionally(null); } catch (NullPointerException ok) {} |
2443 |
< |
try { h = f.handle(null); } catch (NullPointerException ok) {} |
2444 |
< |
try { h = f.thenCompose(null); } catch (NullPointerException ok) {} |
2445 |
< |
|
2446 |
< |
try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {} |
2447 |
< |
try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {} |
2448 |
< |
try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {} |
2449 |
< |
try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {} |
2450 |
< |
try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {} |
2451 |
< |
try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {} |
2452 |
< |
try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {} |
2453 |
< |
try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {} |
2454 |
< |
try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {} |
2455 |
< |
try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {} |
2456 |
< |
try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {} |
2457 |
< |
try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {} |
2458 |
< |
try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {} |
2428 |
> |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2429 |
> |
CompletableFuture<?> h; |
2430 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
2431 |
> |
|
2432 |
> |
Runnable[] throwingActions = { |
2433 |
> |
() -> { CompletableFuture.supplyAsync(null); }, |
2434 |
> |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
2435 |
> |
() -> { CompletableFuture.supplyAsync(supplyOne, null); }, |
2436 |
> |
|
2437 |
> |
() -> { CompletableFuture.runAsync(null); }, |
2438 |
> |
() -> { CompletableFuture.runAsync(null, exec); }, |
2439 |
> |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
2440 |
> |
|
2441 |
> |
() -> { f.completeExceptionally(null); }, |
2442 |
> |
|
2443 |
> |
() -> { f.thenApply(null); }, |
2444 |
> |
() -> { f.thenApplyAsync(null); }, |
2445 |
> |
() -> { f.thenApplyAsync((x) -> x, null); }, |
2446 |
> |
() -> { f.thenApplyAsync(null, exec); }, |
2447 |
> |
|
2448 |
> |
() -> { f.thenAccept(null); }, |
2449 |
> |
() -> { f.thenAcceptAsync(null); }, |
2450 |
> |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
2451 |
> |
() -> { f.thenAcceptAsync(null, exec); }, |
2452 |
> |
|
2453 |
> |
() -> { f.thenRun(null); }, |
2454 |
> |
() -> { f.thenRunAsync(null); }, |
2455 |
> |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
2456 |
> |
() -> { f.thenRunAsync(null, exec); }, |
2457 |
> |
|
2458 |
> |
() -> { f.thenCombine(g, null); }, |
2459 |
> |
() -> { f.thenCombineAsync(g, null); }, |
2460 |
> |
() -> { f.thenCombineAsync(g, null, exec); }, |
2461 |
> |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
2462 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
2463 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
2464 |
> |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
2465 |
> |
|
2466 |
> |
() -> { f.thenAcceptBoth(g, null); }, |
2467 |
> |
() -> { f.thenAcceptBothAsync(g, null); }, |
2468 |
> |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
2469 |
> |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
2470 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
2471 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
2472 |
> |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
2473 |
> |
|
2474 |
> |
() -> { f.runAfterBoth(g, null); }, |
2475 |
> |
() -> { f.runAfterBothAsync(g, null); }, |
2476 |
> |
() -> { f.runAfterBothAsync(g, null, exec); }, |
2477 |
> |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
2478 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
2479 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
2480 |
> |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
2481 |
> |
|
2482 |
> |
() -> { f.applyToEither(g, null); }, |
2483 |
> |
() -> { f.applyToEitherAsync(g, null); }, |
2484 |
> |
() -> { f.applyToEitherAsync(g, null, exec); }, |
2485 |
> |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
2486 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
2487 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
2488 |
> |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
2489 |
> |
|
2490 |
> |
() -> { f.acceptEither(g, null); }, |
2491 |
> |
() -> { f.acceptEitherAsync(g, null); }, |
2492 |
> |
() -> { f.acceptEitherAsync(g, null, exec); }, |
2493 |
> |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
2494 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
2495 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
2496 |
> |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
2497 |
> |
|
2498 |
> |
() -> { f.runAfterEither(g, null); }, |
2499 |
> |
() -> { f.runAfterEitherAsync(g, null); }, |
2500 |
> |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
2501 |
> |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
2502 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
2503 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
2504 |
> |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
2505 |
> |
|
2506 |
> |
() -> { f.thenCompose(null); }, |
2507 |
> |
() -> { f.thenComposeAsync(null); }, |
2508 |
> |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
2509 |
> |
() -> { f.thenComposeAsync(null, exec); }, |
2510 |
> |
|
2511 |
> |
() -> { f.exceptionally(null); }, |
2512 |
> |
|
2513 |
> |
() -> { f.handle(null); }, |
2514 |
> |
|
2515 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
2516 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
2517 |
> |
() -> { CompletableFuture.allOf(f, null); }, |
2518 |
> |
() -> { CompletableFuture.allOf(null, f); }, |
2519 |
> |
|
2520 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
2521 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
2522 |
> |
() -> { CompletableFuture.anyOf(f, null); }, |
2523 |
> |
() -> { CompletableFuture.anyOf(null, f); }, |
2524 |
> |
}; |
2525 |
|
|
2526 |
+ |
assertThrows(NullPointerException.class, throwingActions); |
2527 |
+ |
assertEquals(0, exec.count.get()); |
2528 |
|
} |
2529 |
|
|
2434 |
– |
|
2530 |
|
} |