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); } |
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) { |
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) { |
152 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
|
assertTrue(f.isDone()); |
154 |
|
assertFalse(f.isCancelled()); |
155 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
282 |
|
checkCompletedNormally(f, "test"); |
283 |
|
} |
284 |
|
|
285 |
+ |
// Choose non-commutative actions for better coverage |
286 |
+ |
|
287 |
|
static final Supplier<Integer> supplyOne = |
288 |
|
() -> Integer.valueOf(1); |
289 |
|
static final Function<Integer, Integer> inc = |
290 |
|
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
291 |
< |
static final BiFunction<Integer, Integer, Integer> add = |
292 |
< |
(Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue()); |
291 |
> |
static final BiFunction<Integer, Integer, Integer> subtract = |
292 |
> |
(Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue()); |
293 |
|
static final class IncAction implements Consumer<Integer> { |
294 |
|
int value; |
295 |
|
public void accept(Integer x) { value = x.intValue() + 1; } |
330 |
|
public void run() { ran = true; throw new CFException(); } |
331 |
|
} |
332 |
|
|
333 |
< |
static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> { |
333 |
> |
static final class CompletableFutureInc |
334 |
> |
implements Function<Integer, CompletableFuture<Integer>> { |
335 |
|
public CompletableFuture<Integer> apply(Integer x) { |
336 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
337 |
|
f.complete(Integer.valueOf(x.intValue() + 1)); |
339 |
|
} |
340 |
|
} |
341 |
|
|
342 |
< |
static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> { |
342 |
> |
static final class FailingCompletableFutureFunction |
343 |
> |
implements Function<Integer, CompletableFuture<Integer>> { |
344 |
|
boolean ran; |
345 |
|
public CompletableFuture<Integer> apply(Integer x) { |
346 |
|
ran = true; throw new CFException(); |
349 |
|
|
350 |
|
// Used for explicit executor tests |
351 |
|
static final class ThreadExecutor implements Executor { |
352 |
+ |
AtomicInteger count = new AtomicInteger(0); |
353 |
+ |
|
354 |
|
public void execute(Runnable r) { |
355 |
+ |
count.getAndIncrement(); |
356 |
|
new Thread(r).start(); |
357 |
|
} |
358 |
|
} |
362 |
|
} |
363 |
|
|
364 |
|
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
365 |
+ |
boolean ran; |
366 |
|
public Integer apply(Integer x, Throwable t) { |
367 |
+ |
ran = true; |
368 |
|
return (t == null) ? two : three; |
369 |
|
} |
370 |
|
} |
393 |
|
* normal or exceptional completion of source |
394 |
|
*/ |
395 |
|
public void testHandle() { |
396 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
397 |
< |
IntegerHandler r = new IntegerHandler(); |
398 |
< |
CompletableFuture<Integer> g = f.handle(r); |
396 |
> |
CompletableFuture<Integer> f, g; |
397 |
> |
IntegerHandler r; |
398 |
> |
|
399 |
> |
f = new CompletableFuture<Integer>(); |
400 |
|
f.completeExceptionally(new CFException()); |
401 |
+ |
g = f.handle(r = new IntegerHandler()); |
402 |
+ |
assertTrue(r.ran); |
403 |
|
checkCompletedNormally(g, three); |
404 |
|
|
405 |
|
f = new CompletableFuture<Integer>(); |
406 |
< |
r = new IntegerHandler(); |
407 |
< |
g = f.handle(r); |
406 |
> |
g = f.handle(r = new IntegerHandler()); |
407 |
> |
assertFalse(r.ran); |
408 |
> |
f.completeExceptionally(new CFException()); |
409 |
> |
checkCompletedNormally(g, three); |
410 |
> |
assertTrue(r.ran); |
411 |
> |
|
412 |
> |
f = new CompletableFuture<Integer>(); |
413 |
|
f.complete(one); |
414 |
+ |
g = f.handle(r = new IntegerHandler()); |
415 |
+ |
assertTrue(r.ran); |
416 |
+ |
checkCompletedNormally(g, two); |
417 |
+ |
|
418 |
+ |
f = new CompletableFuture<Integer>(); |
419 |
+ |
g = f.handle(r = new IntegerHandler()); |
420 |
+ |
assertFalse(r.ran); |
421 |
+ |
f.complete(one); |
422 |
+ |
assertTrue(r.ran); |
423 |
|
checkCompletedNormally(g, two); |
424 |
|
} |
425 |
|
|
431 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
432 |
|
assertNull(f.join()); |
433 |
|
assertTrue(r.ran); |
434 |
+ |
checkCompletedNormally(f, null); |
435 |
|
} |
436 |
|
|
437 |
|
/** |
439 |
|
*/ |
440 |
|
public void testRunAsync2() { |
441 |
|
Noop r = new Noop(); |
442 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor()); |
442 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
443 |
> |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
444 |
|
assertNull(f.join()); |
445 |
|
assertTrue(r.ran); |
446 |
+ |
checkCompletedNormally(f, null); |
447 |
+ |
assertEquals(1, exec.count.get()); |
448 |
|
} |
449 |
|
|
450 |
|
/** |
461 |
|
* supplyAsync completes with result of supplier |
462 |
|
*/ |
463 |
|
public void testSupplyAsync() { |
464 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne); |
464 |
> |
CompletableFuture<Integer> f; |
465 |
> |
f = CompletableFuture.supplyAsync(supplyOne); |
466 |
|
assertEquals(f.join(), one); |
467 |
+ |
checkCompletedNormally(f, one); |
468 |
|
} |
469 |
|
|
470 |
|
/** |
471 |
|
* supplyAsync with executor completes with result of supplier |
472 |
|
*/ |
473 |
|
public void testSupplyAsync2() { |
474 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
474 |
> |
CompletableFuture<Integer> f; |
475 |
> |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
476 |
|
assertEquals(f.join(), one); |
477 |
+ |
checkCompletedNormally(f, one); |
478 |
|
} |
479 |
|
|
480 |
|
/** |
630 |
|
|
631 |
|
|
632 |
|
/** |
633 |
< |
* thenCombine result completes normally after normal completion of sources |
633 |
> |
* thenCombine result completes normally after normal completion |
634 |
> |
* of sources |
635 |
|
*/ |
636 |
|
public void testThenCombine() { |
637 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
600 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
601 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
602 |
< |
f.complete(one); |
603 |
< |
checkIncomplete(g); |
604 |
< |
f2.complete(two); |
605 |
< |
checkCompletedNormally(g, three); |
637 |
> |
CompletableFuture<Integer> f, g, h; |
638 |
|
|
639 |
|
f = new CompletableFuture<Integer>(); |
640 |
< |
f.complete(one); |
641 |
< |
f2 = new CompletableFuture<Integer>(); |
642 |
< |
g = f.thenCombine(f2, add); |
643 |
< |
checkIncomplete(g); |
644 |
< |
f2.complete(two); |
645 |
< |
checkCompletedNormally(g, three); |
640 |
> |
g = new CompletableFuture<Integer>(); |
641 |
> |
h = f.thenCombine(g, subtract); |
642 |
> |
f.complete(3); |
643 |
> |
checkIncomplete(h); |
644 |
> |
g.complete(1); |
645 |
> |
checkCompletedNormally(h, 2); |
646 |
> |
|
647 |
> |
f = new CompletableFuture<Integer>(); |
648 |
> |
g = new CompletableFuture<Integer>(); |
649 |
> |
h = f.thenCombine(g, subtract); |
650 |
> |
g.complete(1); |
651 |
> |
checkIncomplete(h); |
652 |
> |
f.complete(3); |
653 |
> |
checkCompletedNormally(h, 2); |
654 |
> |
|
655 |
> |
f = new CompletableFuture<Integer>(); |
656 |
> |
g = new CompletableFuture<Integer>(); |
657 |
> |
g.complete(1); |
658 |
> |
f.complete(3); |
659 |
> |
h = f.thenCombine(g, subtract); |
660 |
> |
checkCompletedNormally(h, 2); |
661 |
|
} |
662 |
|
|
663 |
|
/** |
665 |
|
* completion of either source |
666 |
|
*/ |
667 |
|
public void testThenCombine2() { |
668 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
669 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
670 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
668 |
> |
CompletableFuture<Integer> f, g, h; |
669 |
> |
|
670 |
> |
f = new CompletableFuture<Integer>(); |
671 |
> |
g = new CompletableFuture<Integer>(); |
672 |
> |
h = f.thenCombine(g, subtract); |
673 |
|
f.completeExceptionally(new CFException()); |
674 |
< |
f2.complete(two); |
675 |
< |
checkCompletedWithWrappedCFException(g); |
674 |
> |
checkIncomplete(h); |
675 |
> |
g.complete(1); |
676 |
> |
checkCompletedWithWrappedCFException(h); |
677 |
|
|
678 |
|
f = new CompletableFuture<Integer>(); |
679 |
< |
f.complete(one); |
680 |
< |
f2 = new CompletableFuture<Integer>(); |
681 |
< |
g = f.thenCombine(f2, add); |
682 |
< |
f2.completeExceptionally(new CFException()); |
683 |
< |
checkCompletedWithWrappedCFException(g); |
679 |
> |
g = new CompletableFuture<Integer>(); |
680 |
> |
h = f.thenCombine(g, subtract); |
681 |
> |
g.completeExceptionally(new CFException()); |
682 |
> |
checkIncomplete(h); |
683 |
> |
f.complete(3); |
684 |
> |
checkCompletedWithWrappedCFException(h); |
685 |
> |
|
686 |
> |
f = new CompletableFuture<Integer>(); |
687 |
> |
g = new CompletableFuture<Integer>(); |
688 |
> |
f.complete(3); |
689 |
> |
g.completeExceptionally(new CFException()); |
690 |
> |
h = f.thenCombine(g, subtract); |
691 |
> |
checkCompletedWithWrappedCFException(h); |
692 |
> |
|
693 |
> |
f = new CompletableFuture<Integer>(); |
694 |
> |
g = new CompletableFuture<Integer>(); |
695 |
> |
f.completeExceptionally(new CFException()); |
696 |
> |
g.complete(3); |
697 |
> |
h = f.thenCombine(g, subtract); |
698 |
> |
checkCompletedWithWrappedCFException(h); |
699 |
|
} |
700 |
|
|
701 |
|
/** |
708 |
|
CompletableFuture<Integer> g = f.thenCombine(f2, r); |
709 |
|
f.complete(one); |
710 |
|
checkIncomplete(g); |
711 |
+ |
assertFalse(r.ran); |
712 |
|
f2.complete(two); |
713 |
|
checkCompletedWithWrappedCFException(g); |
714 |
+ |
assertTrue(r.ran); |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* thenCombine result completes exceptionally if either source cancelled |
719 |
|
*/ |
720 |
|
public void testThenCombine4() { |
721 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
722 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
723 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
721 |
> |
CompletableFuture<Integer> f, g, h; |
722 |
> |
|
723 |
> |
f = new CompletableFuture<Integer>(); |
724 |
> |
g = new CompletableFuture<Integer>(); |
725 |
> |
h = f.thenCombine(g, subtract); |
726 |
|
assertTrue(f.cancel(true)); |
727 |
< |
f2.complete(two); |
728 |
< |
checkCompletedWithWrappedCancellationException(g); |
727 |
> |
checkIncomplete(h); |
728 |
> |
g.complete(1); |
729 |
> |
checkCompletedWithWrappedCancellationException(h); |
730 |
> |
|
731 |
|
f = new CompletableFuture<Integer>(); |
732 |
< |
f2 = new CompletableFuture<Integer>(); |
733 |
< |
g = f.thenCombine(f2, add); |
734 |
< |
f.complete(one); |
735 |
< |
assertTrue(f2.cancel(true)); |
736 |
< |
checkCompletedWithWrappedCancellationException(g); |
732 |
> |
g = new CompletableFuture<Integer>(); |
733 |
> |
h = f.thenCombine(g, subtract); |
734 |
> |
assertTrue(g.cancel(true)); |
735 |
> |
checkIncomplete(h); |
736 |
> |
f.complete(3); |
737 |
> |
checkCompletedWithWrappedCancellationException(h); |
738 |
> |
|
739 |
> |
f = new CompletableFuture<Integer>(); |
740 |
> |
g = new CompletableFuture<Integer>(); |
741 |
> |
assertTrue(f.cancel(true)); |
742 |
> |
assertTrue(g.cancel(true)); |
743 |
> |
h = f.thenCombine(g, subtract); |
744 |
> |
checkCompletedWithWrappedCancellationException(h); |
745 |
|
} |
746 |
|
|
747 |
|
/** |
1314 |
|
assertTrue(f.cancel(true)); |
1315 |
|
checkCompletedWithWrappedCancellationException(g); |
1316 |
|
} |
1317 |
+ |
|
1318 |
|
/** |
1319 |
|
* thenCombineAsync result completes normally after normal |
1320 |
|
* completion of sources |
1321 |
|
*/ |
1322 |
|
public void testThenCombineAsync() { |
1323 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1324 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1325 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1326 |
< |
f.complete(one); |
1327 |
< |
checkIncomplete(g); |
1328 |
< |
f2.complete(two); |
1329 |
< |
checkCompletedNormally(g, three); |
1323 |
> |
CompletableFuture<Integer> f, g, h; |
1324 |
> |
|
1325 |
> |
f = new CompletableFuture<Integer>(); |
1326 |
> |
g = new CompletableFuture<Integer>(); |
1327 |
> |
h = f.thenCombineAsync(g, subtract); |
1328 |
> |
f.complete(3); |
1329 |
> |
checkIncomplete(h); |
1330 |
> |
g.complete(1); |
1331 |
> |
checkCompletedNormally(h, 2); |
1332 |
> |
|
1333 |
> |
f = new CompletableFuture<Integer>(); |
1334 |
> |
g = new CompletableFuture<Integer>(); |
1335 |
> |
h = f.thenCombineAsync(g, subtract); |
1336 |
> |
g.complete(1); |
1337 |
> |
checkIncomplete(h); |
1338 |
> |
f.complete(3); |
1339 |
> |
checkCompletedNormally(h, 2); |
1340 |
> |
|
1341 |
> |
f = new CompletableFuture<Integer>(); |
1342 |
> |
g = new CompletableFuture<Integer>(); |
1343 |
> |
g.complete(1); |
1344 |
> |
f.complete(3); |
1345 |
> |
h = f.thenCombineAsync(g, subtract); |
1346 |
> |
checkCompletedNormally(h, 2); |
1347 |
|
} |
1348 |
|
|
1349 |
|
/** |
1350 |
|
* thenCombineAsync result completes exceptionally after exceptional |
1351 |
< |
* completion of source |
1351 |
> |
* completion of either source |
1352 |
|
*/ |
1353 |
|
public void testThenCombineAsync2() { |
1354 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1355 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1356 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1354 |
> |
CompletableFuture<Integer> f, g, h; |
1355 |
> |
|
1356 |
> |
f = new CompletableFuture<Integer>(); |
1357 |
> |
g = new CompletableFuture<Integer>(); |
1358 |
> |
h = f.thenCombineAsync(g, subtract); |
1359 |
|
f.completeExceptionally(new CFException()); |
1360 |
< |
f2.complete(two); |
1361 |
< |
checkCompletedWithWrappedCFException(g); |
1360 |
> |
checkIncomplete(h); |
1361 |
> |
g.complete(1); |
1362 |
> |
checkCompletedWithWrappedCFException(h); |
1363 |
|
|
1364 |
|
f = new CompletableFuture<Integer>(); |
1365 |
< |
f2 = new CompletableFuture<Integer>(); |
1366 |
< |
g = f.thenCombineAsync(f2, add); |
1367 |
< |
f.complete(one); |
1368 |
< |
f2.completeExceptionally(new CFException()); |
1369 |
< |
checkCompletedWithWrappedCFException(g); |
1365 |
> |
g = new CompletableFuture<Integer>(); |
1366 |
> |
h = f.thenCombineAsync(g, subtract); |
1367 |
> |
g.completeExceptionally(new CFException()); |
1368 |
> |
checkIncomplete(h); |
1369 |
> |
f.complete(3); |
1370 |
> |
checkCompletedWithWrappedCFException(h); |
1371 |
> |
|
1372 |
> |
f = new CompletableFuture<Integer>(); |
1373 |
> |
g = new CompletableFuture<Integer>(); |
1374 |
> |
g.completeExceptionally(new CFException()); |
1375 |
> |
f.complete(3); |
1376 |
> |
h = f.thenCombineAsync(g, subtract); |
1377 |
> |
checkCompletedWithWrappedCFException(h); |
1378 |
|
} |
1379 |
|
|
1380 |
|
/** |
1387 |
|
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r); |
1388 |
|
f.complete(one); |
1389 |
|
checkIncomplete(g); |
1390 |
+ |
assertFalse(r.ran); |
1391 |
|
f2.complete(two); |
1392 |
|
checkCompletedWithWrappedCFException(g); |
1393 |
+ |
assertTrue(r.ran); |
1394 |
|
} |
1395 |
|
|
1396 |
|
/** |
1397 |
|
* thenCombineAsync result completes exceptionally if either source cancelled |
1398 |
|
*/ |
1399 |
|
public void testThenCombineAsync4() { |
1400 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1401 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1402 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1400 |
> |
CompletableFuture<Integer> f, g, h; |
1401 |
> |
|
1402 |
> |
f = new CompletableFuture<Integer>(); |
1403 |
> |
g = new CompletableFuture<Integer>(); |
1404 |
> |
h = f.thenCombineAsync(g, subtract); |
1405 |
|
assertTrue(f.cancel(true)); |
1406 |
< |
f2.complete(two); |
1407 |
< |
checkCompletedWithWrappedCancellationException(g); |
1406 |
> |
checkIncomplete(h); |
1407 |
> |
g.complete(1); |
1408 |
> |
checkCompletedWithWrappedCancellationException(h); |
1409 |
|
|
1410 |
|
f = new CompletableFuture<Integer>(); |
1411 |
< |
f2 = new CompletableFuture<Integer>(); |
1412 |
< |
g = f.thenCombineAsync(f2, add); |
1413 |
< |
f.complete(one); |
1414 |
< |
assertTrue(f2.cancel(true)); |
1415 |
< |
checkCompletedWithWrappedCancellationException(g); |
1411 |
> |
g = new CompletableFuture<Integer>(); |
1412 |
> |
h = f.thenCombineAsync(g, subtract); |
1413 |
> |
assertTrue(g.cancel(true)); |
1414 |
> |
checkIncomplete(h); |
1415 |
> |
f.complete(3); |
1416 |
> |
checkCompletedWithWrappedCancellationException(h); |
1417 |
> |
|
1418 |
> |
f = new CompletableFuture<Integer>(); |
1419 |
> |
g = new CompletableFuture<Integer>(); |
1420 |
> |
g.complete(3); |
1421 |
> |
assertTrue(f.cancel(true)); |
1422 |
> |
h = f.thenCombineAsync(g, subtract); |
1423 |
> |
checkCompletedWithWrappedCancellationException(h); |
1424 |
> |
|
1425 |
> |
f = new CompletableFuture<Integer>(); |
1426 |
> |
g = new CompletableFuture<Integer>(); |
1427 |
> |
f.complete(3); |
1428 |
> |
assertTrue(g.cancel(true)); |
1429 |
> |
h = f.thenCombineAsync(g, subtract); |
1430 |
> |
checkCompletedWithWrappedCancellationException(h); |
1431 |
|
} |
1432 |
|
|
1433 |
|
/** |
1986 |
|
assertTrue(f.cancel(true)); |
1987 |
|
checkCompletedWithWrappedCancellationException(g); |
1988 |
|
} |
1989 |
+ |
|
1990 |
|
/** |
1991 |
|
* thenCombineAsync result completes normally after normal |
1992 |
|
* completion of sources |
1993 |
|
*/ |
1994 |
|
public void testThenCombineAsyncE() { |
1995 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1996 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1997 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1998 |
< |
f.complete(one); |
1999 |
< |
checkIncomplete(g); |
2000 |
< |
f2.complete(two); |
2001 |
< |
checkCompletedNormally(g, three); |
1995 |
> |
CompletableFuture<Integer> f, g, h; |
1996 |
> |
ThreadExecutor e = new ThreadExecutor(); |
1997 |
> |
int count = 0; |
1998 |
> |
|
1999 |
> |
f = new CompletableFuture<Integer>(); |
2000 |
> |
g = new CompletableFuture<Integer>(); |
2001 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2002 |
> |
f.complete(3); |
2003 |
> |
checkIncomplete(h); |
2004 |
> |
g.complete(1); |
2005 |
> |
checkCompletedNormally(h, 2); |
2006 |
> |
assertEquals(++count, e.count.get()); |
2007 |
> |
|
2008 |
> |
f = new CompletableFuture<Integer>(); |
2009 |
> |
g = new CompletableFuture<Integer>(); |
2010 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2011 |
> |
g.complete(1); |
2012 |
> |
checkIncomplete(h); |
2013 |
> |
f.complete(3); |
2014 |
> |
checkCompletedNormally(h, 2); |
2015 |
> |
assertEquals(++count, e.count.get()); |
2016 |
> |
|
2017 |
> |
f = new CompletableFuture<Integer>(); |
2018 |
> |
g = new CompletableFuture<Integer>(); |
2019 |
> |
g.complete(1); |
2020 |
> |
f.complete(3); |
2021 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2022 |
> |
checkCompletedNormally(h, 2); |
2023 |
> |
assertEquals(++count, e.count.get()); |
2024 |
|
} |
2025 |
|
|
2026 |
|
/** |
2027 |
|
* thenCombineAsync result completes exceptionally after exceptional |
2028 |
< |
* completion of source |
2028 |
> |
* completion of either source |
2029 |
|
*/ |
2030 |
|
public void testThenCombineAsync2E() { |
2031 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2032 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2033 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
2031 |
> |
CompletableFuture<Integer> f, g, h; |
2032 |
> |
ThreadExecutor e = new ThreadExecutor(); |
2033 |
> |
int count = 0; |
2034 |
> |
|
2035 |
> |
f = new CompletableFuture<Integer>(); |
2036 |
> |
g = new CompletableFuture<Integer>(); |
2037 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2038 |
|
f.completeExceptionally(new CFException()); |
2039 |
< |
f2.complete(two); |
2040 |
< |
checkCompletedWithWrappedCFException(g); |
2039 |
> |
checkIncomplete(h); |
2040 |
> |
g.complete(1); |
2041 |
> |
checkCompletedWithWrappedCFException(h); |
2042 |
|
|
2043 |
|
f = new CompletableFuture<Integer>(); |
2044 |
< |
f2 = new CompletableFuture<Integer>(); |
2045 |
< |
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
2046 |
< |
f.complete(one); |
2047 |
< |
f2.completeExceptionally(new CFException()); |
2048 |
< |
checkCompletedWithWrappedCFException(g); |
2044 |
> |
g = new CompletableFuture<Integer>(); |
2045 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2046 |
> |
g.completeExceptionally(new CFException()); |
2047 |
> |
checkIncomplete(h); |
2048 |
> |
f.complete(3); |
2049 |
> |
checkCompletedWithWrappedCFException(h); |
2050 |
> |
|
2051 |
> |
f = new CompletableFuture<Integer>(); |
2052 |
> |
g = new CompletableFuture<Integer>(); |
2053 |
> |
g.completeExceptionally(new CFException()); |
2054 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2055 |
> |
checkIncomplete(h); |
2056 |
> |
f.complete(3); |
2057 |
> |
checkCompletedWithWrappedCFException(h); |
2058 |
> |
|
2059 |
> |
assertEquals(0, e.count.get()); |
2060 |
|
} |
2061 |
|
|
2062 |
|
/** |
2069 |
|
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor()); |
2070 |
|
f.complete(one); |
2071 |
|
checkIncomplete(g); |
2072 |
+ |
assertFalse(r.ran); |
2073 |
|
f2.complete(two); |
2074 |
|
checkCompletedWithWrappedCFException(g); |
2075 |
+ |
assertTrue(r.ran); |
2076 |
|
} |
2077 |
|
|
2078 |
|
/** |
2079 |
|
* thenCombineAsync result completes exceptionally if either source cancelled |
2080 |
|
*/ |
2081 |
|
public void testThenCombineAsync4E() { |
2082 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2083 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2084 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
2082 |
> |
CompletableFuture<Integer> f, g, h; |
2083 |
> |
ThreadExecutor e = new ThreadExecutor(); |
2084 |
> |
|
2085 |
> |
f = new CompletableFuture<Integer>(); |
2086 |
> |
g = new CompletableFuture<Integer>(); |
2087 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2088 |
|
assertTrue(f.cancel(true)); |
2089 |
< |
f2.complete(two); |
2090 |
< |
checkCompletedWithWrappedCancellationException(g); |
2089 |
> |
checkIncomplete(h); |
2090 |
> |
g.complete(1); |
2091 |
> |
checkCompletedWithWrappedCancellationException(h); |
2092 |
|
|
2093 |
|
f = new CompletableFuture<Integer>(); |
2094 |
< |
f2 = new CompletableFuture<Integer>(); |
2095 |
< |
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
2096 |
< |
f.complete(one); |
2097 |
< |
assertTrue(f2.cancel(true)); |
2098 |
< |
checkCompletedWithWrappedCancellationException(g); |
2094 |
> |
g = new CompletableFuture<Integer>(); |
2095 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2096 |
> |
assertTrue(g.cancel(true)); |
2097 |
> |
checkIncomplete(h); |
2098 |
> |
f.complete(3); |
2099 |
> |
checkCompletedWithWrappedCancellationException(h); |
2100 |
> |
|
2101 |
> |
f = new CompletableFuture<Integer>(); |
2102 |
> |
g = new CompletableFuture<Integer>(); |
2103 |
> |
assertTrue(g.cancel(true)); |
2104 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2105 |
> |
checkIncomplete(h); |
2106 |
> |
f.complete(3); |
2107 |
> |
checkCompletedWithWrappedCancellationException(h); |
2108 |
> |
|
2109 |
> |
f = new CompletableFuture<Integer>(); |
2110 |
> |
g = new CompletableFuture<Integer>(); |
2111 |
> |
assertTrue(f.cancel(true)); |
2112 |
> |
assertTrue(g.cancel(true)); |
2113 |
> |
h = f.thenCombineAsync(g, subtract, e); |
2114 |
> |
checkCompletedWithWrappedCancellationException(h); |
2115 |
> |
|
2116 |
> |
assertEquals(0, e.count.get()); |
2117 |
|
} |
2118 |
|
|
2119 |
|
/** |
2561 |
|
} |
2562 |
|
|
2563 |
|
/** |
2564 |
< |
* allOf returns a future completed when any components complete |
2564 |
> |
* anyOf returns a future completed when any components complete |
2565 |
|
*/ |
2566 |
|
public void testAnyOf() throws Exception { |
2567 |
|
for (int k = 1; k < 20; ++k) { |
2583 |
|
public void testNPE() { |
2584 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2585 |
|
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
2586 |
< |
CompletableFuture h; |
2587 |
< |
try { h = f.thenApply(null); } catch (NullPointerException ok) {} |
2588 |
< |
try { h = f.thenAccept(null); } catch (NullPointerException ok) {} |
2589 |
< |
try { h = f.thenRun(null); } catch (NullPointerException ok) {} |
2590 |
< |
try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {} |
2591 |
< |
try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {} |
2592 |
< |
try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {} |
2593 |
< |
try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {} |
2594 |
< |
try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {} |
2595 |
< |
try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {} |
2596 |
< |
try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {} |
2597 |
< |
try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {} |
2598 |
< |
try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {} |
2599 |
< |
try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {} |
2600 |
< |
try { h = f.exceptionally(null); } catch (NullPointerException ok) {} |
2601 |
< |
try { h = f.handle(null); } catch (NullPointerException ok) {} |
2602 |
< |
try { h = f.thenCompose(null); } catch (NullPointerException ok) {} |
2603 |
< |
|
2604 |
< |
try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {} |
2605 |
< |
try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {} |
2606 |
< |
try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {} |
2607 |
< |
try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {} |
2608 |
< |
try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {} |
2609 |
< |
try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {} |
2610 |
< |
try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {} |
2611 |
< |
try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {} |
2612 |
< |
try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {} |
2613 |
< |
try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {} |
2614 |
< |
try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {} |
2615 |
< |
try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {} |
2616 |
< |
try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {} |
2586 |
> |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2587 |
> |
CompletableFuture<?> h; |
2588 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
2589 |
> |
|
2590 |
> |
Runnable[] throwingActions = { |
2591 |
> |
() -> { CompletableFuture.supplyAsync(null); }, |
2592 |
> |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
2593 |
> |
() -> { CompletableFuture.supplyAsync(supplyOne, null); }, |
2594 |
> |
|
2595 |
> |
() -> { CompletableFuture.runAsync(null); }, |
2596 |
> |
() -> { CompletableFuture.runAsync(null, exec); }, |
2597 |
> |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
2598 |
> |
|
2599 |
> |
() -> { f.completeExceptionally(null); }, |
2600 |
> |
|
2601 |
> |
() -> { f.thenApply(null); }, |
2602 |
> |
() -> { f.thenApplyAsync(null); }, |
2603 |
> |
() -> { f.thenApplyAsync((x) -> x, null); }, |
2604 |
> |
() -> { f.thenApplyAsync(null, exec); }, |
2605 |
> |
|
2606 |
> |
() -> { f.thenAccept(null); }, |
2607 |
> |
() -> { f.thenAcceptAsync(null); }, |
2608 |
> |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
2609 |
> |
() -> { f.thenAcceptAsync(null, exec); }, |
2610 |
> |
|
2611 |
> |
() -> { f.thenRun(null); }, |
2612 |
> |
() -> { f.thenRunAsync(null); }, |
2613 |
> |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
2614 |
> |
() -> { f.thenRunAsync(null, exec); }, |
2615 |
> |
|
2616 |
> |
() -> { f.thenCombine(g, null); }, |
2617 |
> |
() -> { f.thenCombineAsync(g, null); }, |
2618 |
> |
() -> { f.thenCombineAsync(g, null, exec); }, |
2619 |
> |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
2620 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
2621 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
2622 |
> |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
2623 |
> |
|
2624 |
> |
() -> { f.thenAcceptBoth(g, null); }, |
2625 |
> |
() -> { f.thenAcceptBothAsync(g, null); }, |
2626 |
> |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
2627 |
> |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
2628 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
2629 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
2630 |
> |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
2631 |
> |
|
2632 |
> |
() -> { f.runAfterBoth(g, null); }, |
2633 |
> |
() -> { f.runAfterBothAsync(g, null); }, |
2634 |
> |
() -> { f.runAfterBothAsync(g, null, exec); }, |
2635 |
> |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
2636 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
2637 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
2638 |
> |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
2639 |
> |
|
2640 |
> |
() -> { f.applyToEither(g, null); }, |
2641 |
> |
() -> { f.applyToEitherAsync(g, null); }, |
2642 |
> |
() -> { f.applyToEitherAsync(g, null, exec); }, |
2643 |
> |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
2644 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
2645 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
2646 |
> |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
2647 |
> |
|
2648 |
> |
() -> { f.acceptEither(g, null); }, |
2649 |
> |
() -> { f.acceptEitherAsync(g, null); }, |
2650 |
> |
() -> { f.acceptEitherAsync(g, null, exec); }, |
2651 |
> |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
2652 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
2653 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
2654 |
> |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
2655 |
> |
|
2656 |
> |
() -> { f.runAfterEither(g, null); }, |
2657 |
> |
() -> { f.runAfterEitherAsync(g, null); }, |
2658 |
> |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
2659 |
> |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
2660 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
2661 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
2662 |
> |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
2663 |
> |
|
2664 |
> |
() -> { f.thenCompose(null); }, |
2665 |
> |
() -> { f.thenComposeAsync(null); }, |
2666 |
> |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
2667 |
> |
() -> { f.thenComposeAsync(null, exec); }, |
2668 |
> |
|
2669 |
> |
() -> { f.exceptionally(null); }, |
2670 |
> |
|
2671 |
> |
() -> { f.handle(null); }, |
2672 |
> |
|
2673 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
2674 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
2675 |
> |
() -> { CompletableFuture.allOf(f, null); }, |
2676 |
> |
() -> { CompletableFuture.allOf(null, f); }, |
2677 |
> |
|
2678 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
2679 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
2680 |
> |
() -> { CompletableFuture.anyOf(f, null); }, |
2681 |
> |
() -> { CompletableFuture.anyOf(null, f); }, |
2682 |
> |
}; |
2683 |
|
|
2684 |
+ |
assertThrows(NullPointerException.class, throwingActions); |
2685 |
+ |
assertEquals(0, exec.count.get()); |
2686 |
|
} |
2687 |
|
|
2429 |
– |
|
2688 |
|
} |