68 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
|
assertTrue(f.isDone()); |
70 |
|
assertFalse(f.isCancelled()); |
71 |
+ |
assertFalse(f.isCompletedExceptionally()); |
72 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
73 |
|
} |
74 |
|
|
122 |
|
} catch (CancellationException success) { |
123 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
|
assertTrue(f.isDone()); |
125 |
+ |
assertTrue(f.isCompletedExceptionally()); |
126 |
|
assertTrue(f.isCancelled()); |
127 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
128 |
|
} |
154 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
155 |
|
assertTrue(f.isDone()); |
156 |
|
assertFalse(f.isCancelled()); |
157 |
+ |
assertTrue(f.isCompletedExceptionally()); |
158 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
159 |
|
} |
160 |
|
|
259 |
|
assertEquals(g.getNumberOfDependents(), 0); |
260 |
|
} |
261 |
|
|
259 |
– |
|
262 |
|
/** |
263 |
|
* toString indicates current completion state |
264 |
|
*/ |
373 |
|
} |
374 |
|
} |
375 |
|
|
374 |
– |
|
376 |
|
/** |
377 |
|
* exceptionally action completes with function value on source |
378 |
< |
* exception; otherwise with source value |
378 |
> |
* exception; otherwise with source value |
379 |
|
*/ |
380 |
|
public void testExceptionally() { |
381 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
496 |
|
* thenRun result completes normally after normal completion of source |
497 |
|
*/ |
498 |
|
public void testThenRun() { |
499 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
500 |
< |
Noop r = new Noop(); |
501 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
499 |
> |
CompletableFuture<Integer> f; |
500 |
> |
CompletableFuture<Void> g; |
501 |
> |
Noop r; |
502 |
> |
|
503 |
> |
f = new CompletableFuture<>(); |
504 |
> |
g = f.thenRun(r = new Noop()); |
505 |
|
f.complete(null); |
506 |
|
checkCompletedNormally(g, null); |
507 |
< |
// reordered version |
507 |
> |
assertTrue(r.ran); |
508 |
> |
|
509 |
|
f = new CompletableFuture<>(); |
510 |
|
f.complete(null); |
511 |
< |
r = new Noop(); |
507 |
< |
g = f.thenRun(r); |
511 |
> |
g = f.thenRun(r = new Noop()); |
512 |
|
checkCompletedNormally(g, null); |
513 |
+ |
assertTrue(r.ran); |
514 |
|
} |
515 |
|
|
516 |
|
/** |
518 |
|
* completion of source |
519 |
|
*/ |
520 |
|
public void testThenRun2() { |
521 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
522 |
< |
Noop r = new Noop(); |
523 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
521 |
> |
CompletableFuture<Integer> f; |
522 |
> |
CompletableFuture<Void> g; |
523 |
> |
Noop r; |
524 |
> |
|
525 |
> |
f = new CompletableFuture<>(); |
526 |
> |
g = f.thenRun(r = new Noop()); |
527 |
|
f.completeExceptionally(new CFException()); |
528 |
|
checkCompletedWithWrappedCFException(g); |
529 |
+ |
assertFalse(r.ran); |
530 |
+ |
|
531 |
+ |
f = new CompletableFuture<>(); |
532 |
+ |
f.completeExceptionally(new CFException()); |
533 |
+ |
g = f.thenRun(r = new Noop()); |
534 |
+ |
checkCompletedWithWrappedCFException(g); |
535 |
+ |
assertFalse(r.ran); |
536 |
|
} |
537 |
|
|
538 |
|
/** |
539 |
|
* thenRun result completes exceptionally if action does |
540 |
|
*/ |
541 |
|
public void testThenRun3() { |
542 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
543 |
< |
FailingNoop r = new FailingNoop(); |
544 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
542 |
> |
CompletableFuture<Integer> f; |
543 |
> |
CompletableFuture<Void> g; |
544 |
> |
FailingNoop r; |
545 |
> |
|
546 |
> |
f = new CompletableFuture<>(); |
547 |
> |
g = f.thenRun(r = new FailingNoop()); |
548 |
|
f.complete(null); |
549 |
|
checkCompletedWithWrappedCFException(g); |
550 |
+ |
|
551 |
+ |
f = new CompletableFuture<>(); |
552 |
+ |
f.complete(null); |
553 |
+ |
g = f.thenRun(r = new FailingNoop()); |
554 |
+ |
checkCompletedWithWrappedCFException(g); |
555 |
|
} |
556 |
|
|
557 |
|
/** |
558 |
|
* thenRun result completes exceptionally if source cancelled |
559 |
|
*/ |
560 |
|
public void testThenRun4() { |
561 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
562 |
< |
Noop r = new Noop(); |
563 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
561 |
> |
CompletableFuture<Integer> f; |
562 |
> |
CompletableFuture<Void> g; |
563 |
> |
Noop r; |
564 |
> |
|
565 |
> |
f = new CompletableFuture<>(); |
566 |
> |
g = f.thenRun(r = new Noop()); |
567 |
|
assertTrue(f.cancel(true)); |
568 |
|
checkCompletedWithWrappedCancellationException(g); |
569 |
+ |
|
570 |
+ |
f = new CompletableFuture<>(); |
571 |
+ |
assertTrue(f.cancel(true)); |
572 |
+ |
g = f.thenRun(r = new Noop()); |
573 |
+ |
checkCompletedWithWrappedCancellationException(g); |
574 |
|
} |
575 |
|
|
576 |
|
/** |
661 |
|
checkCompletedWithWrappedCancellationException(g); |
662 |
|
} |
663 |
|
|
633 |
– |
|
664 |
|
/** |
665 |
|
* thenCombine result completes normally after normal completion |
666 |
|
* of sources |
1168 |
|
checkCompletedWithWrappedCancellationException(g); |
1169 |
|
} |
1170 |
|
|
1141 |
– |
|
1171 |
|
/** |
1172 |
|
* runAfterEither result completes normally after normal completion |
1173 |
|
* of either source |
1316 |
|
checkCompletedWithWrappedCancellationException(g); |
1317 |
|
} |
1318 |
|
|
1290 |
– |
|
1319 |
|
// asyncs |
1320 |
|
|
1321 |
|
/** |
1348 |
|
try { |
1349 |
|
g.join(); |
1350 |
|
shouldThrow(); |
1351 |
< |
} catch (Exception ok) { |
1324 |
< |
} |
1351 |
> |
} catch (CompletionException success) {} |
1352 |
|
checkCompletedWithWrappedCFException(g); |
1353 |
|
} |
1354 |
|
|
2082 |
|
checkCompletedWithWrappedCancellationException(g); |
2083 |
|
} |
2084 |
|
|
2058 |
– |
|
2085 |
|
// async with explicit executors |
2086 |
|
|
2087 |
|
/** |
2114 |
|
try { |
2115 |
|
g.join(); |
2116 |
|
shouldThrow(); |
2117 |
< |
} catch (Exception ok) { |
2092 |
< |
} |
2117 |
> |
} catch (CompletionException success) {} |
2118 |
|
checkCompletedWithWrappedCFException(g); |
2119 |
|
} |
2120 |
|
|
2851 |
|
* with the value null |
2852 |
|
*/ |
2853 |
|
public void testAllOf_empty() throws Exception { |
2854 |
< |
CompletableFuture<?> f = CompletableFuture.allOf(); |
2854 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(); |
2855 |
|
checkCompletedNormally(f, null); |
2856 |
|
} |
2857 |
|
|
2858 |
|
/** |
2859 |
< |
* allOf returns a future completed when all components complete |
2859 |
> |
* allOf returns a future completed normally with the value null |
2860 |
> |
* when all components complete normally |
2861 |
|
*/ |
2862 |
< |
public void testAllOf() throws Exception { |
2862 |
> |
public void testAllOf_normal() throws Exception { |
2863 |
|
for (int k = 1; k < 20; ++k) { |
2864 |
|
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2865 |
|
for (int i = 0; i < k; ++i) |
2867 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2868 |
|
for (int i = 0; i < k; ++i) { |
2869 |
|
checkIncomplete(f); |
2870 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2871 |
|
fs[i].complete(one); |
2872 |
|
} |
2873 |
|
checkCompletedNormally(f, null); |
2874 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
2875 |
|
} |
2876 |
|
} |
2877 |
|
|
2879 |
|
* anyOf(no component futures) returns an incomplete future |
2880 |
|
*/ |
2881 |
|
public void testAnyOf_empty() throws Exception { |
2882 |
< |
CompletableFuture<?> f = CompletableFuture.anyOf(); |
2882 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
2883 |
|
checkIncomplete(f); |
2884 |
|
} |
2885 |
|
|
2886 |
|
/** |
2887 |
< |
* anyOf returns a future completed when any components complete |
2887 |
> |
* anyOf returns a future completed normally with a value when |
2888 |
> |
* a component future does |
2889 |
|
*/ |
2890 |
< |
public void testAnyOf() throws Exception { |
2891 |
< |
for (int k = 1; k < 20; ++k) { |
2890 |
> |
public void testAnyOf_normal() throws Exception { |
2891 |
> |
for (int k = 0; k < 10; ++k) { |
2892 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2893 |
|
for (int i = 0; i < k; ++i) |
2894 |
|
fs[i] = new CompletableFuture<>(); |
2897 |
|
for (int i = 0; i < k; ++i) { |
2898 |
|
fs[i].complete(one); |
2899 |
|
checkCompletedNormally(f, one); |
2900 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
2901 |
+ |
} |
2902 |
+ |
} |
2903 |
+ |
} |
2904 |
+ |
|
2905 |
+ |
/** |
2906 |
+ |
* anyOf result completes exceptionally when any component does. |
2907 |
+ |
*/ |
2908 |
+ |
public void testAnyOf_exceptional() throws Exception { |
2909 |
+ |
for (int k = 0; k < 10; ++k) { |
2910 |
+ |
CompletableFuture[] fs = new CompletableFuture[k]; |
2911 |
+ |
for (int i = 0; i < k; ++i) |
2912 |
+ |
fs[i] = new CompletableFuture<>(); |
2913 |
+ |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2914 |
+ |
checkIncomplete(f); |
2915 |
+ |
for (int i = 0; i < k; ++i) { |
2916 |
+ |
fs[i].completeExceptionally(new CFException()); |
2917 |
+ |
checkCompletedWithWrappedCFException(f); |
2918 |
+ |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
2919 |
|
} |
2920 |
|
} |
2921 |
|
} |
2931 |
|
ThreadExecutor exec = new ThreadExecutor(); |
2932 |
|
|
2933 |
|
Runnable[] throwingActions = { |
2934 |
< |
() -> { CompletableFuture.supplyAsync(null); }, |
2935 |
< |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
2936 |
< |
() -> { CompletableFuture.supplyAsync(supplyOne, null); }, |
2937 |
< |
|
2938 |
< |
() -> { CompletableFuture.runAsync(null); }, |
2939 |
< |
() -> { CompletableFuture.runAsync(null, exec); }, |
2940 |
< |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
2941 |
< |
|
2942 |
< |
() -> { f.completeExceptionally(null); }, |
2943 |
< |
|
2944 |
< |
() -> { f.thenApply(null); }, |
2945 |
< |
() -> { f.thenApplyAsync(null); }, |
2946 |
< |
() -> { f.thenApplyAsync((x) -> x, null); }, |
2947 |
< |
() -> { f.thenApplyAsync(null, exec); }, |
2948 |
< |
|
2949 |
< |
() -> { f.thenAccept(null); }, |
2950 |
< |
() -> { f.thenAcceptAsync(null); }, |
2951 |
< |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
2952 |
< |
() -> { f.thenAcceptAsync(null, exec); }, |
2953 |
< |
|
2954 |
< |
() -> { f.thenRun(null); }, |
2955 |
< |
() -> { f.thenRunAsync(null); }, |
2956 |
< |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
2957 |
< |
() -> { f.thenRunAsync(null, exec); }, |
2958 |
< |
|
2959 |
< |
() -> { f.thenCombine(g, null); }, |
2960 |
< |
() -> { f.thenCombineAsync(g, null); }, |
2961 |
< |
() -> { f.thenCombineAsync(g, null, exec); }, |
2962 |
< |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
2963 |
< |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
2964 |
< |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
2965 |
< |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
2966 |
< |
|
2967 |
< |
() -> { f.thenAcceptBoth(g, null); }, |
2968 |
< |
() -> { f.thenAcceptBothAsync(g, null); }, |
2969 |
< |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
2970 |
< |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
2971 |
< |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
2972 |
< |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
2973 |
< |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
2974 |
< |
|
2975 |
< |
() -> { f.runAfterBoth(g, null); }, |
2976 |
< |
() -> { f.runAfterBothAsync(g, null); }, |
2977 |
< |
() -> { f.runAfterBothAsync(g, null, exec); }, |
2978 |
< |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
2979 |
< |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
2980 |
< |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
2981 |
< |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
2982 |
< |
|
2983 |
< |
() -> { f.applyToEither(g, null); }, |
2984 |
< |
() -> { f.applyToEitherAsync(g, null); }, |
2985 |
< |
() -> { f.applyToEitherAsync(g, null, exec); }, |
2986 |
< |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
2987 |
< |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
2988 |
< |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
2989 |
< |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
2990 |
< |
|
2991 |
< |
() -> { f.acceptEither(g, null); }, |
2992 |
< |
() -> { f.acceptEitherAsync(g, null); }, |
2993 |
< |
() -> { f.acceptEitherAsync(g, null, exec); }, |
2994 |
< |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
2995 |
< |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
2996 |
< |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
2997 |
< |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
2998 |
< |
|
2999 |
< |
() -> { f.runAfterEither(g, null); }, |
3000 |
< |
() -> { f.runAfterEitherAsync(g, null); }, |
3001 |
< |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
3002 |
< |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
3003 |
< |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
3004 |
< |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
3005 |
< |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
3006 |
< |
|
3007 |
< |
() -> { f.thenCompose(null); }, |
3008 |
< |
() -> { f.thenComposeAsync(null); }, |
3009 |
< |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
3010 |
< |
() -> { f.thenComposeAsync(null, exec); }, |
3011 |
< |
|
3012 |
< |
() -> { f.exceptionally(null); }, |
3013 |
< |
|
3014 |
< |
() -> { f.handle(null); }, |
3015 |
< |
|
3016 |
< |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
3017 |
< |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
3018 |
< |
() -> { CompletableFuture.allOf(f, null); }, |
3019 |
< |
() -> { CompletableFuture.allOf(null, f); }, |
3020 |
< |
|
3021 |
< |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
3022 |
< |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
3023 |
< |
() -> { CompletableFuture.anyOf(f, null); }, |
3024 |
< |
() -> { CompletableFuture.anyOf(null, f); }, |
2934 |
> |
() -> CompletableFuture.supplyAsync(null), |
2935 |
> |
() -> CompletableFuture.supplyAsync(null, exec), |
2936 |
> |
() -> CompletableFuture.supplyAsync(supplyOne, null), |
2937 |
> |
|
2938 |
> |
() -> CompletableFuture.runAsync(null), |
2939 |
> |
() -> CompletableFuture.runAsync(null, exec), |
2940 |
> |
() -> CompletableFuture.runAsync(() -> {}, null), |
2941 |
> |
|
2942 |
> |
() -> f.completeExceptionally(null), |
2943 |
> |
|
2944 |
> |
() -> f.thenApply(null), |
2945 |
> |
() -> f.thenApplyAsync(null), |
2946 |
> |
() -> f.thenApplyAsync((x) -> x, null), |
2947 |
> |
() -> f.thenApplyAsync(null, exec), |
2948 |
> |
|
2949 |
> |
() -> f.thenAccept(null), |
2950 |
> |
() -> f.thenAcceptAsync(null), |
2951 |
> |
() -> f.thenAcceptAsync((x) -> {} , null), |
2952 |
> |
() -> f.thenAcceptAsync(null, exec), |
2953 |
> |
|
2954 |
> |
() -> f.thenRun(null), |
2955 |
> |
() -> f.thenRunAsync(null), |
2956 |
> |
() -> f.thenRunAsync(() -> {} , null), |
2957 |
> |
() -> f.thenRunAsync(null, exec), |
2958 |
> |
|
2959 |
> |
() -> f.thenCombine(g, null), |
2960 |
> |
() -> f.thenCombineAsync(g, null), |
2961 |
> |
() -> f.thenCombineAsync(g, null, exec), |
2962 |
> |
() -> f.thenCombine(nullFuture, (x, y) -> x), |
2963 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x), |
2964 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec), |
2965 |
> |
() -> f.thenCombineAsync(g, (x, y) -> x, null), |
2966 |
> |
|
2967 |
> |
() -> f.thenAcceptBoth(g, null), |
2968 |
> |
() -> f.thenAcceptBothAsync(g, null), |
2969 |
> |
() -> f.thenAcceptBothAsync(g, null, exec), |
2970 |
> |
() -> f.thenAcceptBoth(nullFuture, (x, y) -> {}), |
2971 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}), |
2972 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec), |
2973 |
> |
() -> f.thenAcceptBothAsync(g, (x, y) -> {}, null), |
2974 |
> |
|
2975 |
> |
() -> f.runAfterBoth(g, null), |
2976 |
> |
() -> f.runAfterBothAsync(g, null), |
2977 |
> |
() -> f.runAfterBothAsync(g, null, exec), |
2978 |
> |
() -> f.runAfterBoth(nullFuture, () -> {}), |
2979 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}), |
2980 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}, exec), |
2981 |
> |
() -> f.runAfterBothAsync(g, () -> {}, null), |
2982 |
> |
|
2983 |
> |
() -> f.applyToEither(g, null), |
2984 |
> |
() -> f.applyToEitherAsync(g, null), |
2985 |
> |
() -> f.applyToEitherAsync(g, null, exec), |
2986 |
> |
() -> f.applyToEither(nullFuture, (x) -> x), |
2987 |
> |
() -> f.applyToEitherAsync(nullFuture, (x) -> x), |
2988 |
> |
() -> f.applyToEitherAsync(nullFuture, (x) -> x, exec), |
2989 |
> |
() -> f.applyToEitherAsync(g, (x) -> x, null), |
2990 |
> |
|
2991 |
> |
() -> f.acceptEither(g, null), |
2992 |
> |
() -> f.acceptEitherAsync(g, null), |
2993 |
> |
() -> f.acceptEitherAsync(g, null, exec), |
2994 |
> |
() -> f.acceptEither(nullFuture, (x) -> {}), |
2995 |
> |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}), |
2996 |
> |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec), |
2997 |
> |
() -> f.acceptEitherAsync(g, (x) -> {}, null), |
2998 |
> |
|
2999 |
> |
() -> f.runAfterEither(g, null), |
3000 |
> |
() -> f.runAfterEitherAsync(g, null), |
3001 |
> |
() -> f.runAfterEitherAsync(g, null, exec), |
3002 |
> |
() -> f.runAfterEither(nullFuture, () -> {}), |
3003 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}), |
3004 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}, exec), |
3005 |
> |
() -> f.runAfterEitherAsync(g, () -> {}, null), |
3006 |
> |
|
3007 |
> |
() -> f.thenCompose(null), |
3008 |
> |
() -> f.thenComposeAsync(null), |
3009 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(), null), |
3010 |
> |
() -> f.thenComposeAsync(null, exec), |
3011 |
> |
|
3012 |
> |
() -> f.exceptionally(null), |
3013 |
> |
|
3014 |
> |
() -> f.handle(null), |
3015 |
> |
|
3016 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>)null), |
3017 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>[])null), |
3018 |
> |
() -> CompletableFuture.allOf(f, null), |
3019 |
> |
() -> CompletableFuture.allOf(null, f), |
3020 |
> |
|
3021 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>)null), |
3022 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>[])null), |
3023 |
> |
() -> CompletableFuture.anyOf(f, null), |
3024 |
> |
() -> CompletableFuture.anyOf(null, f), |
3025 |
> |
|
3026 |
> |
() -> f.obtrudeException(null), |
3027 |
|
}; |
3028 |
|
|
3029 |
|
assertThrows(NullPointerException.class, throwingActions); |
3030 |
|
assertEquals(0, exec.count.get()); |
3031 |
|
} |
3032 |
|
|
3033 |
+ |
/** |
3034 |
+ |
* toCompletableFuture returns this CompletableFuture. |
3035 |
+ |
*/ |
3036 |
+ |
public void testToCompletableFuture() { |
3037 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3038 |
+ |
assertSame(f, f.toCompletableFuture()); |
3039 |
+ |
} |
3040 |
+ |
|
3041 |
+ |
/** |
3042 |
+ |
* whenComplete action executes on normal completion, propagating |
3043 |
+ |
* source result. |
3044 |
+ |
*/ |
3045 |
+ |
public void testWhenComplete1() { |
3046 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3047 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3048 |
+ |
CompletableFuture<Integer> g = |
3049 |
+ |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3050 |
+ |
f.complete(three); |
3051 |
+ |
checkCompletedNormally(f, three); |
3052 |
+ |
checkCompletedNormally(g, three); |
3053 |
+ |
assertEquals(a.get(), 1); |
3054 |
+ |
} |
3055 |
+ |
|
3056 |
+ |
/** |
3057 |
+ |
* whenComplete action executes on exceptional completion, propagating |
3058 |
+ |
* source result. |
3059 |
+ |
*/ |
3060 |
+ |
public void testWhenComplete2() { |
3061 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3062 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3063 |
+ |
CompletableFuture<Integer> g = |
3064 |
+ |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3065 |
+ |
f.completeExceptionally(new CFException()); |
3066 |
+ |
assertTrue(f.isCompletedExceptionally()); |
3067 |
+ |
assertTrue(g.isCompletedExceptionally()); |
3068 |
+ |
assertEquals(a.get(), 1); |
3069 |
+ |
} |
3070 |
+ |
|
3071 |
+ |
/** |
3072 |
+ |
* If a whenComplete action throws an exception when triggered by |
3073 |
+ |
* a normal completion, it completes exceptionally |
3074 |
+ |
*/ |
3075 |
+ |
public void testWhenComplete3() { |
3076 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3077 |
+ |
CompletableFuture<Integer> g = |
3078 |
+ |
f.whenComplete((Integer x, Throwable t) -> |
3079 |
+ |
{ throw new CFException(); } ); |
3080 |
+ |
f.complete(three); |
3081 |
+ |
checkCompletedNormally(f, three); |
3082 |
+ |
assertTrue(g.isCompletedExceptionally()); |
3083 |
+ |
checkCompletedWithWrappedCFException(g); |
3084 |
+ |
} |
3085 |
+ |
|
3086 |
+ |
/** |
3087 |
+ |
* whenCompleteAsync action executes on normal completion, propagating |
3088 |
+ |
* source result. |
3089 |
+ |
*/ |
3090 |
+ |
public void testWhenCompleteAsync1() { |
3091 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3092 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3093 |
+ |
CompletableFuture<Integer> g = |
3094 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3095 |
+ |
f.complete(three); |
3096 |
+ |
checkCompletedNormally(f, three); |
3097 |
+ |
checkCompletedNormally(g, three); |
3098 |
+ |
assertEquals(a.get(), 1); |
3099 |
+ |
} |
3100 |
+ |
|
3101 |
+ |
/** |
3102 |
+ |
* whenCompleteAsync action executes on exceptional completion, propagating |
3103 |
+ |
* source result. |
3104 |
+ |
*/ |
3105 |
+ |
public void testWhenCompleteAsync2() { |
3106 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3107 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3108 |
+ |
CompletableFuture<Integer> g = |
3109 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3110 |
+ |
f.completeExceptionally(new CFException()); |
3111 |
+ |
checkCompletedWithWrappedCFException(f); |
3112 |
+ |
checkCompletedWithWrappedCFException(g); |
3113 |
+ |
} |
3114 |
+ |
|
3115 |
+ |
/** |
3116 |
+ |
* If a whenCompleteAsync action throws an exception when |
3117 |
+ |
* triggered by a normal completion, it completes exceptionally |
3118 |
+ |
*/ |
3119 |
+ |
public void testWhenCompleteAsync3() { |
3120 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3121 |
+ |
CompletableFuture<Integer> g = |
3122 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3123 |
+ |
{ throw new CFException(); } ); |
3124 |
+ |
f.complete(three); |
3125 |
+ |
checkCompletedNormally(f, three); |
3126 |
+ |
checkCompletedWithWrappedCFException(g); |
3127 |
+ |
} |
3128 |
+ |
|
3129 |
+ |
/** |
3130 |
+ |
* whenCompleteAsync action executes on normal completion, propagating |
3131 |
+ |
* source result. |
3132 |
+ |
*/ |
3133 |
+ |
public void testWhenCompleteAsync1e() { |
3134 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3135 |
+ |
ThreadExecutor exec = new ThreadExecutor(); |
3136 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3137 |
+ |
CompletableFuture<Integer> g = |
3138 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3139 |
+ |
exec); |
3140 |
+ |
f.complete(three); |
3141 |
+ |
checkCompletedNormally(f, three); |
3142 |
+ |
checkCompletedNormally(g, three); |
3143 |
+ |
assertEquals(a.get(), 1); |
3144 |
+ |
} |
3145 |
+ |
|
3146 |
+ |
/** |
3147 |
+ |
* whenCompleteAsync action executes on exceptional completion, propagating |
3148 |
+ |
* source result. |
3149 |
+ |
*/ |
3150 |
+ |
public void testWhenCompleteAsync2e() { |
3151 |
+ |
final AtomicInteger a = new AtomicInteger(); |
3152 |
+ |
ThreadExecutor exec = new ThreadExecutor(); |
3153 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3154 |
+ |
CompletableFuture<Integer> g = |
3155 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3156 |
+ |
exec); |
3157 |
+ |
f.completeExceptionally(new CFException()); |
3158 |
+ |
checkCompletedWithWrappedCFException(f); |
3159 |
+ |
checkCompletedWithWrappedCFException(g); |
3160 |
+ |
} |
3161 |
+ |
|
3162 |
+ |
/** |
3163 |
+ |
* If a whenCompleteAsync action throws an exception when triggered |
3164 |
+ |
* by a normal completion, it completes exceptionally |
3165 |
+ |
*/ |
3166 |
+ |
public void testWhenCompleteAsync3e() { |
3167 |
+ |
ThreadExecutor exec = new ThreadExecutor(); |
3168 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3169 |
+ |
CompletableFuture<Integer> g = |
3170 |
+ |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3171 |
+ |
{ throw new CFException(); }, |
3172 |
+ |
exec); |
3173 |
+ |
f.complete(three); |
3174 |
+ |
checkCompletedNormally(f, three); |
3175 |
+ |
checkCompletedWithWrappedCFException(g); |
3176 |
+ |
} |
3177 |
+ |
|
3178 |
+ |
/** |
3179 |
+ |
* handleAsync action completes normally with function value on |
3180 |
+ |
* either normal or exceptional completion of source |
3181 |
+ |
*/ |
3182 |
+ |
public void testHandleAsync() { |
3183 |
+ |
CompletableFuture<Integer> f, g; |
3184 |
+ |
IntegerHandler r; |
3185 |
+ |
|
3186 |
+ |
f = new CompletableFuture<>(); |
3187 |
+ |
g = f.handleAsync(r = new IntegerHandler()); |
3188 |
+ |
assertFalse(r.ran); |
3189 |
+ |
f.completeExceptionally(new CFException()); |
3190 |
+ |
checkCompletedWithWrappedCFException(f); |
3191 |
+ |
checkCompletedNormally(g, three); |
3192 |
+ |
assertTrue(r.ran); |
3193 |
+ |
|
3194 |
+ |
f = new CompletableFuture<>(); |
3195 |
+ |
g = f.handleAsync(r = new IntegerHandler()); |
3196 |
+ |
assertFalse(r.ran); |
3197 |
+ |
f.completeExceptionally(new CFException()); |
3198 |
+ |
checkCompletedWithWrappedCFException(f); |
3199 |
+ |
checkCompletedNormally(g, three); |
3200 |
+ |
assertTrue(r.ran); |
3201 |
+ |
|
3202 |
+ |
f = new CompletableFuture<>(); |
3203 |
+ |
g = f.handleAsync(r = new IntegerHandler()); |
3204 |
+ |
assertFalse(r.ran); |
3205 |
+ |
f.complete(one); |
3206 |
+ |
checkCompletedNormally(f, one); |
3207 |
+ |
checkCompletedNormally(g, two); |
3208 |
+ |
assertTrue(r.ran); |
3209 |
+ |
|
3210 |
+ |
f = new CompletableFuture<>(); |
3211 |
+ |
g = f.handleAsync(r = new IntegerHandler()); |
3212 |
+ |
assertFalse(r.ran); |
3213 |
+ |
f.complete(one); |
3214 |
+ |
checkCompletedNormally(f, one); |
3215 |
+ |
checkCompletedNormally(g, two); |
3216 |
+ |
assertTrue(r.ran); |
3217 |
+ |
} |
3218 |
+ |
|
3219 |
+ |
/** |
3220 |
+ |
* handleAsync action with Executor completes normally with |
3221 |
+ |
* function value on either normal or exceptional completion of |
3222 |
+ |
* source |
3223 |
+ |
*/ |
3224 |
+ |
public void testHandleAsync2() { |
3225 |
+ |
CompletableFuture<Integer> f, g; |
3226 |
+ |
ThreadExecutor exec = new ThreadExecutor(); |
3227 |
+ |
IntegerHandler r; |
3228 |
+ |
|
3229 |
+ |
f = new CompletableFuture<>(); |
3230 |
+ |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3231 |
+ |
assertFalse(r.ran); |
3232 |
+ |
f.completeExceptionally(new CFException()); |
3233 |
+ |
checkCompletedWithWrappedCFException(f); |
3234 |
+ |
checkCompletedNormally(g, three); |
3235 |
+ |
assertTrue(r.ran); |
3236 |
+ |
|
3237 |
+ |
f = new CompletableFuture<>(); |
3238 |
+ |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3239 |
+ |
assertFalse(r.ran); |
3240 |
+ |
f.completeExceptionally(new CFException()); |
3241 |
+ |
checkCompletedWithWrappedCFException(f); |
3242 |
+ |
checkCompletedNormally(g, three); |
3243 |
+ |
assertTrue(r.ran); |
3244 |
+ |
|
3245 |
+ |
f = new CompletableFuture<>(); |
3246 |
+ |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3247 |
+ |
assertFalse(r.ran); |
3248 |
+ |
f.complete(one); |
3249 |
+ |
checkCompletedNormally(f, one); |
3250 |
+ |
checkCompletedNormally(g, two); |
3251 |
+ |
assertTrue(r.ran); |
3252 |
+ |
|
3253 |
+ |
f = new CompletableFuture<>(); |
3254 |
+ |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3255 |
+ |
assertFalse(r.ran); |
3256 |
+ |
f.complete(one); |
3257 |
+ |
checkCompletedNormally(f, one); |
3258 |
+ |
checkCompletedNormally(g, two); |
3259 |
+ |
assertTrue(r.ran); |
3260 |
+ |
} |
3261 |
+ |
|
3262 |
|
} |