41 |
|
import java.util.function.Predicate; |
42 |
|
import java.util.function.Supplier; |
43 |
|
|
44 |
– |
import junit.framework.AssertionFailedError; |
44 |
|
import junit.framework.Test; |
45 |
|
import junit.framework.TestSuite; |
46 |
|
|
58 |
|
void checkIncomplete(CompletableFuture<?> f) { |
59 |
|
assertFalse(f.isDone()); |
60 |
|
assertFalse(f.isCancelled()); |
61 |
< |
assertTrue(f.toString().contains("Not completed")); |
61 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
62 |
> |
|
63 |
> |
Object result = null; |
64 |
|
try { |
65 |
< |
assertNull(f.getNow(null)); |
65 |
> |
result = f.getNow(null); |
66 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
67 |
+ |
assertNull(result); |
68 |
+ |
|
69 |
|
try { |
70 |
< |
f.get(0L, SECONDS); |
70 |
> |
f.get(randomExpiredTimeout(), randomTimeUnit()); |
71 |
|
shouldThrow(); |
72 |
|
} |
73 |
|
catch (TimeoutException success) {} |
74 |
|
catch (Throwable fail) { threadUnexpectedException(fail); } |
75 |
|
} |
76 |
|
|
77 |
< |
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
78 |
< |
checkTimedGet(f, value); |
77 |
> |
<T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) { |
78 |
> |
checkTimedGet(f, expectedValue); |
79 |
|
|
80 |
+ |
assertEquals(expectedValue, f.join()); |
81 |
+ |
assertEquals(expectedValue, f.getNow(null)); |
82 |
+ |
|
83 |
+ |
T result = null; |
84 |
|
try { |
85 |
< |
assertEquals(value, f.join()); |
79 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
80 |
< |
try { |
81 |
< |
assertEquals(value, f.getNow(null)); |
82 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
83 |
< |
try { |
84 |
< |
assertEquals(value, f.get()); |
85 |
> |
result = f.get(); |
86 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
87 |
+ |
assertEquals(expectedValue, result); |
88 |
+ |
|
89 |
|
assertTrue(f.isDone()); |
90 |
|
assertFalse(f.isCancelled()); |
91 |
|
assertFalse(f.isCompletedExceptionally()); |
92 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
92 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
145 |
|
assertFalse(f.isCancelled()); |
146 |
|
assertTrue(f.isDone()); |
147 |
|
assertTrue(f.isCompletedExceptionally()); |
148 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
148 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
149 |
|
} |
150 |
|
|
151 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
200 |
|
assertTrue(f.isDone()); |
201 |
|
assertTrue(f.isCompletedExceptionally()); |
202 |
|
assertTrue(f.isCancelled()); |
203 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
203 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
204 |
|
} |
205 |
|
|
206 |
|
/** |
299 |
|
} |
300 |
|
|
301 |
|
f = new CompletableFuture<>(); |
302 |
< |
f.completeExceptionally(ex = new CFException()); |
302 |
> |
f.completeExceptionally(new CFException()); |
303 |
|
f.obtrudeValue(v1); |
304 |
|
checkCompletedNormally(f, v1); |
305 |
|
f.obtrudeException(ex = new CFException()); |
336 |
|
/** |
337 |
|
* toString indicates current completion state |
338 |
|
*/ |
339 |
< |
public void testToString() { |
340 |
< |
CompletableFuture<String> f; |
341 |
< |
|
342 |
< |
f = new CompletableFuture<String>(); |
343 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
339 |
> |
public void testToString_incomplete() { |
340 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
341 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
342 |
> |
if (testImplementationDetails) |
343 |
> |
assertEquals(identityString(f) + "[Not completed]", |
344 |
> |
f.toString()); |
345 |
> |
} |
346 |
|
|
347 |
+ |
public void testToString_normal() { |
348 |
+ |
CompletableFuture<String> f = new CompletableFuture<>(); |
349 |
|
assertTrue(f.complete("foo")); |
350 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
350 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
351 |
> |
if (testImplementationDetails) |
352 |
> |
assertEquals(identityString(f) + "[Completed normally]", |
353 |
> |
f.toString()); |
354 |
> |
} |
355 |
|
|
356 |
< |
f = new CompletableFuture<String>(); |
356 |
> |
public void testToString_exception() { |
357 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
358 |
|
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
359 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
359 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
360 |
> |
if (testImplementationDetails) |
361 |
> |
assertTrue(f.toString().startsWith( |
362 |
> |
identityString(f) + "[Completed exceptionally: ")); |
363 |
> |
} |
364 |
|
|
365 |
+ |
public void testToString_cancelled() { |
366 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
367 |
< |
f = new CompletableFuture<String>(); |
367 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
368 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
369 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
369 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
370 |
> |
if (testImplementationDetails) |
371 |
> |
assertTrue(f.toString().startsWith( |
372 |
> |
identityString(f) + "[Completed exceptionally: ")); |
373 |
|
} |
374 |
|
} |
375 |
|
|
557 |
|
} |
558 |
|
} |
559 |
|
|
560 |
+ |
static class FailingExceptionalCompletableFutureFunction extends CheckedAction |
561 |
+ |
implements Function<Throwable, CompletableFuture<Integer>> |
562 |
+ |
{ |
563 |
+ |
final CFException ex; |
564 |
+ |
FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
565 |
+ |
public CompletableFuture<Integer> apply(Throwable x) { |
566 |
+ |
invoked(); |
567 |
+ |
throw ex; |
568 |
+ |
} |
569 |
+ |
} |
570 |
+ |
|
571 |
+ |
static class ExceptionalCompletableFutureFunction extends CheckedAction |
572 |
+ |
implements Function<Throwable, CompletionStage<Integer>> { |
573 |
+ |
final Integer value = 3; |
574 |
+ |
ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); } |
575 |
+ |
public CompletionStage<Integer> apply(Throwable x) { |
576 |
+ |
invoked(); |
577 |
+ |
CompletableFuture<Integer> d = new CompletableFuture<Integer>(); |
578 |
+ |
d.complete(value); |
579 |
+ |
return d; |
580 |
+ |
} |
581 |
+ |
} |
582 |
+ |
|
583 |
|
static class FailingCompletableFutureFunction extends CheckedIntegerAction |
584 |
|
implements Function<Integer, CompletableFuture<Integer>> |
585 |
|
{ |
695 |
|
Function<? super T,U> a) { |
696 |
|
return f.applyToEither(g, a); |
697 |
|
} |
698 |
+ |
public <T> CompletableFuture<T> exceptionally |
699 |
+ |
(CompletableFuture<T> f, |
700 |
+ |
Function<Throwable, ? extends T> fn) { |
701 |
+ |
return f.exceptionally(fn); |
702 |
+ |
} |
703 |
+ |
public <T> CompletableFuture<T> exceptionallyCompose |
704 |
+ |
(CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { |
705 |
+ |
return f.exceptionallyCompose(fn); |
706 |
+ |
} |
707 |
|
}, |
656 |
– |
|
708 |
|
ASYNC { |
709 |
|
public void checkExecutionMode() { |
710 |
|
assertEquals(defaultExecutorIsCommonPool, |
777 |
|
Function<? super T,U> a) { |
778 |
|
return f.applyToEitherAsync(g, a); |
779 |
|
} |
780 |
+ |
public <T> CompletableFuture<T> exceptionally |
781 |
+ |
(CompletableFuture<T> f, |
782 |
+ |
Function<Throwable, ? extends T> fn) { |
783 |
+ |
return f.exceptionallyAsync(fn); |
784 |
+ |
} |
785 |
+ |
|
786 |
+ |
public <T> CompletableFuture<T> exceptionallyCompose |
787 |
+ |
(CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { |
788 |
+ |
return f.exceptionallyComposeAsync(fn); |
789 |
+ |
} |
790 |
+ |
|
791 |
|
}, |
792 |
|
|
793 |
|
EXECUTOR { |
861 |
|
Function<? super T,U> a) { |
862 |
|
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
863 |
|
} |
864 |
+ |
public <T> CompletableFuture<T> exceptionally |
865 |
+ |
(CompletableFuture<T> f, |
866 |
+ |
Function<Throwable, ? extends T> fn) { |
867 |
+ |
return f.exceptionallyAsync(fn, new ThreadExecutor()); |
868 |
+ |
} |
869 |
+ |
public <T> CompletableFuture<T> exceptionallyCompose |
870 |
+ |
(CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { |
871 |
+ |
return f.exceptionallyComposeAsync(fn, new ThreadExecutor()); |
872 |
+ |
} |
873 |
+ |
|
874 |
|
}; |
875 |
|
|
876 |
|
public abstract void checkExecutionMode(); |
913 |
|
(CompletableFuture<T> f, |
914 |
|
CompletionStage<? extends T> g, |
915 |
|
Function<? super T,U> a); |
916 |
+ |
public abstract <T> CompletableFuture<T> exceptionally |
917 |
+ |
(CompletableFuture<T> f, |
918 |
+ |
Function<Throwable, ? extends T> fn); |
919 |
+ |
public abstract <T> CompletableFuture<T> exceptionallyCompose |
920 |
+ |
(CompletableFuture<T> f, |
921 |
+ |
Function<Throwable, ? extends CompletionStage<T>> fn); |
922 |
|
} |
923 |
|
|
924 |
|
/** |
926 |
|
* normally, and source result is propagated |
927 |
|
*/ |
928 |
|
public void testExceptionally_normalCompletion() { |
929 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
930 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
931 |
|
for (Integer v1 : new Integer[] { 1, null }) |
932 |
|
{ |
933 |
|
final AtomicInteger a = new AtomicInteger(0); |
934 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
935 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
936 |
< |
final CompletableFuture<Integer> g = f.exceptionally |
937 |
< |
((Throwable t) -> { |
936 |
> |
final CompletableFuture<Integer> g = m.exceptionally |
937 |
> |
(f, (Throwable t) -> { |
938 |
|
a.getAndIncrement(); |
939 |
|
threadFail("should not be called"); |
940 |
|
return null; // unreached |
951 |
|
* exception |
952 |
|
*/ |
953 |
|
public void testExceptionally_exceptionalCompletion() { |
954 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
955 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
956 |
|
for (Integer v1 : new Integer[] { 1, null }) |
957 |
|
{ |
959 |
|
final CFException ex = new CFException(); |
960 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
961 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
962 |
< |
final CompletableFuture<Integer> g = f.exceptionally |
963 |
< |
((Throwable t) -> { |
964 |
< |
ExecutionMode.SYNC.checkExecutionMode(); |
962 |
> |
final CompletableFuture<Integer> g = m.exceptionally |
963 |
> |
(f, (Throwable t) -> { |
964 |
> |
m.checkExecutionMode(); |
965 |
|
threadAssertSame(t, ex); |
966 |
|
a.getAndIncrement(); |
967 |
|
return v1; |
977 |
|
* exceptionally with that exception |
978 |
|
*/ |
979 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
980 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
981 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
982 |
|
{ |
983 |
|
final AtomicInteger a = new AtomicInteger(0); |
985 |
|
final CFException ex2 = new CFException(); |
986 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
987 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
988 |
< |
final CompletableFuture<Integer> g = f.exceptionally |
989 |
< |
((Throwable t) -> { |
990 |
< |
ExecutionMode.SYNC.checkExecutionMode(); |
988 |
> |
final CompletableFuture<Integer> g = m.exceptionally |
989 |
> |
(f, (Throwable t) -> { |
990 |
> |
m.checkExecutionMode(); |
991 |
|
threadAssertSame(t, ex1); |
992 |
|
a.getAndIncrement(); |
993 |
|
throw ex2; |
1323 |
|
r.assertInvoked(); |
1324 |
|
}} |
1325 |
|
|
1326 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1327 |
|
public void testRunAsync_rejectingExecutor() { |
1328 |
|
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1329 |
|
try { |
1370 |
|
r.assertInvoked(); |
1371 |
|
}} |
1372 |
|
|
1373 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1374 |
|
public void testSupplyAsync_rejectingExecutor() { |
1375 |
|
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1376 |
|
try { |
2645 |
|
|
2646 |
|
// unspecified behavior - both source completions available |
2647 |
|
try { |
2648 |
< |
assertEquals(null, h0.join()); |
2648 |
> |
assertNull(h0.join()); |
2649 |
|
rs[0].assertValue(v1); |
2650 |
|
} catch (CompletionException ok) { |
2651 |
|
checkCompletedWithWrappedException(h0, ex); |
2652 |
|
rs[0].assertNotInvoked(); |
2653 |
|
} |
2654 |
|
try { |
2655 |
< |
assertEquals(null, h1.join()); |
2655 |
> |
assertNull(h1.join()); |
2656 |
|
rs[1].assertValue(v1); |
2657 |
|
} catch (CompletionException ok) { |
2658 |
|
checkCompletedWithWrappedException(h1, ex); |
2659 |
|
rs[1].assertNotInvoked(); |
2660 |
|
} |
2661 |
|
try { |
2662 |
< |
assertEquals(null, h2.join()); |
2662 |
> |
assertNull(h2.join()); |
2663 |
|
rs[2].assertValue(v1); |
2664 |
|
} catch (CompletionException ok) { |
2665 |
|
checkCompletedWithWrappedException(h2, ex); |
2666 |
|
rs[2].assertNotInvoked(); |
2667 |
|
} |
2668 |
|
try { |
2669 |
< |
assertEquals(null, h3.join()); |
2669 |
> |
assertNull(h3.join()); |
2670 |
|
rs[3].assertValue(v1); |
2671 |
|
} catch (CompletionException ok) { |
2672 |
|
checkCompletedWithWrappedException(h3, ex); |
2905 |
|
|
2906 |
|
// unspecified behavior - both source completions available |
2907 |
|
try { |
2908 |
< |
assertEquals(null, h0.join()); |
2908 |
> |
assertNull(h0.join()); |
2909 |
|
rs[0].assertInvoked(); |
2910 |
|
} catch (CompletionException ok) { |
2911 |
|
checkCompletedWithWrappedException(h0, ex); |
2912 |
|
rs[0].assertNotInvoked(); |
2913 |
|
} |
2914 |
|
try { |
2915 |
< |
assertEquals(null, h1.join()); |
2915 |
> |
assertNull(h1.join()); |
2916 |
|
rs[1].assertInvoked(); |
2917 |
|
} catch (CompletionException ok) { |
2918 |
|
checkCompletedWithWrappedException(h1, ex); |
2919 |
|
rs[1].assertNotInvoked(); |
2920 |
|
} |
2921 |
|
try { |
2922 |
< |
assertEquals(null, h2.join()); |
2922 |
> |
assertNull(h2.join()); |
2923 |
|
rs[2].assertInvoked(); |
2924 |
|
} catch (CompletionException ok) { |
2925 |
|
checkCompletedWithWrappedException(h2, ex); |
2926 |
|
rs[2].assertNotInvoked(); |
2927 |
|
} |
2928 |
|
try { |
2929 |
< |
assertEquals(null, h3.join()); |
2929 |
> |
assertNull(h3.join()); |
2930 |
|
rs[3].assertInvoked(); |
2931 |
|
} catch (CompletionException ok) { |
2932 |
|
checkCompletedWithWrappedException(h3, ex); |
3158 |
|
checkCompletedNormally(f, v1); |
3159 |
|
}} |
3160 |
|
|
3161 |
+ |
/** |
3162 |
+ |
* exceptionallyCompose result completes normally after normal |
3163 |
+ |
* completion of source |
3164 |
+ |
*/ |
3165 |
+ |
public void testExceptionallyCompose_normalCompletion() { |
3166 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3167 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
3168 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3169 |
+ |
{ |
3170 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3171 |
+ |
final ExceptionalCompletableFutureFunction r = |
3172 |
+ |
new ExceptionalCompletableFutureFunction(m); |
3173 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
3174 |
+ |
final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
3175 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
3176 |
+ |
|
3177 |
+ |
checkCompletedNormally(f, v1); |
3178 |
+ |
checkCompletedNormally(g, v1); |
3179 |
+ |
r.assertNotInvoked(); |
3180 |
+ |
}} |
3181 |
+ |
|
3182 |
+ |
/** |
3183 |
+ |
* exceptionallyCompose result completes normally after exceptional |
3184 |
+ |
* completion of source |
3185 |
+ |
*/ |
3186 |
+ |
public void testExceptionallyCompose_exceptionalCompletion() { |
3187 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3188 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
3189 |
+ |
{ |
3190 |
+ |
final CFException ex = new CFException(); |
3191 |
+ |
final ExceptionalCompletableFutureFunction r = |
3192 |
+ |
new ExceptionalCompletableFutureFunction(m); |
3193 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3194 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
3195 |
+ |
final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
3196 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
3197 |
+ |
|
3198 |
+ |
checkCompletedExceptionally(f, ex); |
3199 |
+ |
checkCompletedNormally(g, r.value); |
3200 |
+ |
r.assertInvoked(); |
3201 |
+ |
}} |
3202 |
+ |
|
3203 |
+ |
/** |
3204 |
+ |
* exceptionallyCompose completes exceptionally on exception if action does |
3205 |
+ |
*/ |
3206 |
+ |
public void testExceptionallyCompose_actionFailed() { |
3207 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3208 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
3209 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3210 |
+ |
{ |
3211 |
+ |
final CFException ex = new CFException(); |
3212 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3213 |
+ |
final FailingExceptionalCompletableFutureFunction r |
3214 |
+ |
= new FailingExceptionalCompletableFutureFunction(m); |
3215 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
3216 |
+ |
final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
3217 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
3218 |
+ |
|
3219 |
+ |
checkCompletedExceptionally(f, ex); |
3220 |
+ |
checkCompletedWithWrappedException(g, r.ex); |
3221 |
+ |
r.assertInvoked(); |
3222 |
+ |
}} |
3223 |
+ |
|
3224 |
+ |
|
3225 |
|
// other static methods |
3226 |
|
|
3227 |
|
/** |
3385 |
|
/** |
3386 |
|
* Completion methods throw NullPointerException with null arguments |
3387 |
|
*/ |
3388 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
3389 |
|
public void testNPE() { |
3390 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3391 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3680 |
|
*/ |
3681 |
|
public void testCompletedStage() { |
3682 |
|
AtomicInteger x = new AtomicInteger(0); |
3683 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3683 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3684 |
|
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3685 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3686 |
|
assertEquals(x.get(), 1); |
3782 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3783 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3784 |
|
AtomicInteger x = new AtomicInteger(0); |
3785 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3785 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3786 |
|
checkIncomplete(f); |
3787 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3788 |
|
f.complete(1); |
3799 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3800 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3801 |
|
AtomicInteger x = new AtomicInteger(0); |
3802 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3802 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3803 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3804 |
|
checkIncomplete(f); |
3805 |
|
CFException ex = new CFException(); |
3817 |
|
CFException ex = new CFException(); |
3818 |
|
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3819 |
|
AtomicInteger x = new AtomicInteger(0); |
3820 |
< |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3820 |
> |
AtomicReference<Throwable> r = new AtomicReference<>(); |
3821 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3822 |
|
assertEquals(x.get(), 0); |
3823 |
|
assertEquals(r.get(), ex); |
4322 |
|
static void assertZero(CompletableFuture<?> f) { |
4323 |
|
try { |
4324 |
|
f.getNow(null); |
4325 |
< |
throw new AssertionFailedError("should throw"); |
4325 |
> |
throw new AssertionError("should throw"); |
4326 |
|
} catch (CompletionException success) { |
4327 |
|
assertTrue(success.getCause() instanceof ZeroException); |
4328 |
|
} |
4447 |
|
} |
4448 |
|
|
4449 |
|
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4450 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4451 |
|
public void testRecursiveChains() throws Throwable { |
4452 |
|
for (ExecutionMode m : ExecutionMode.values()) |
4453 |
|
for (boolean addDeadEnds : new boolean[] { true, false }) |
4472 |
|
* A single CompletableFuture with many dependents. |
4473 |
|
* A demo of scalability - runtime is O(n). |
4474 |
|
*/ |
4475 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4476 |
|
public void testManyDependents() throws Throwable { |
4477 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4478 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4502 |
|
} |
4503 |
|
|
4504 |
|
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4505 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4506 |
|
public void testCoCompletionGarbageRetention() throws Throwable { |
4507 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4508 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4521 |
|
f.complete(null); |
4522 |
|
|
4523 |
|
f = new CompletableFuture<>(); |
4524 |
< |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4524 |
> |
CompletableFuture.anyOf(f, incomplete); |
4525 |
|
f.complete(null); |
4526 |
|
} |
4527 |
|
|
4539 |
|
f.complete(null); |
4540 |
|
|
4541 |
|
f = new CompletableFuture<>(); |
4542 |
< |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4542 |
> |
CompletableFuture.anyOf(incomplete, f); |
4543 |
|
f.complete(null); |
4544 |
|
} |
4545 |
|
} |
4633 |
|
// return stage.toCompletableFuture().copy().isDone(); |
4634 |
|
// } |
4635 |
|
|
4636 |
+ |
// For testing default implementations |
4637 |
+ |
// Only non-default interface methods defined. |
4638 |
+ |
static final class DelegatedCompletionStage<T> implements CompletionStage<T> { |
4639 |
+ |
final CompletableFuture<T> cf; |
4640 |
+ |
DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; } |
4641 |
+ |
public CompletableFuture<T> toCompletableFuture() { |
4642 |
+ |
return cf; } |
4643 |
+ |
public CompletionStage<Void> thenRun |
4644 |
+ |
(Runnable action) { |
4645 |
+ |
return cf.thenRun(action); } |
4646 |
+ |
public CompletionStage<Void> thenRunAsync |
4647 |
+ |
(Runnable action) { |
4648 |
+ |
return cf.thenRunAsync(action); } |
4649 |
+ |
public CompletionStage<Void> thenRunAsync |
4650 |
+ |
(Runnable action, |
4651 |
+ |
Executor executor) { |
4652 |
+ |
return cf.thenRunAsync(action, executor); } |
4653 |
+ |
public CompletionStage<Void> thenAccept |
4654 |
+ |
(Consumer<? super T> action) { |
4655 |
+ |
return cf.thenAccept(action); } |
4656 |
+ |
public CompletionStage<Void> thenAcceptAsync |
4657 |
+ |
(Consumer<? super T> action) { |
4658 |
+ |
return cf.thenAcceptAsync(action); } |
4659 |
+ |
public CompletionStage<Void> thenAcceptAsync |
4660 |
+ |
(Consumer<? super T> action, |
4661 |
+ |
Executor executor) { |
4662 |
+ |
return cf.thenAcceptAsync(action, executor); } |
4663 |
+ |
public <U> CompletionStage<U> thenApply |
4664 |
+ |
(Function<? super T,? extends U> a) { |
4665 |
+ |
return cf.thenApply(a); } |
4666 |
+ |
public <U> CompletionStage<U> thenApplyAsync |
4667 |
+ |
(Function<? super T,? extends U> fn) { |
4668 |
+ |
return cf.thenApplyAsync(fn); } |
4669 |
+ |
public <U> CompletionStage<U> thenApplyAsync |
4670 |
+ |
(Function<? super T,? extends U> fn, |
4671 |
+ |
Executor executor) { |
4672 |
+ |
return cf.thenApplyAsync(fn, executor); } |
4673 |
+ |
public <U,V> CompletionStage<V> thenCombine |
4674 |
+ |
(CompletionStage<? extends U> other, |
4675 |
+ |
BiFunction<? super T,? super U,? extends V> fn) { |
4676 |
+ |
return cf.thenCombine(other, fn); } |
4677 |
+ |
public <U,V> CompletionStage<V> thenCombineAsync |
4678 |
+ |
(CompletionStage<? extends U> other, |
4679 |
+ |
BiFunction<? super T,? super U,? extends V> fn) { |
4680 |
+ |
return cf.thenCombineAsync(other, fn); } |
4681 |
+ |
public <U,V> CompletionStage<V> thenCombineAsync |
4682 |
+ |
(CompletionStage<? extends U> other, |
4683 |
+ |
BiFunction<? super T,? super U,? extends V> fn, |
4684 |
+ |
Executor executor) { |
4685 |
+ |
return cf.thenCombineAsync(other, fn, executor); } |
4686 |
+ |
public <U> CompletionStage<Void> thenAcceptBoth |
4687 |
+ |
(CompletionStage<? extends U> other, |
4688 |
+ |
BiConsumer<? super T, ? super U> action) { |
4689 |
+ |
return cf.thenAcceptBoth(other, action); } |
4690 |
+ |
public <U> CompletionStage<Void> thenAcceptBothAsync |
4691 |
+ |
(CompletionStage<? extends U> other, |
4692 |
+ |
BiConsumer<? super T, ? super U> action) { |
4693 |
+ |
return cf.thenAcceptBothAsync(other, action); } |
4694 |
+ |
public <U> CompletionStage<Void> thenAcceptBothAsync |
4695 |
+ |
(CompletionStage<? extends U> other, |
4696 |
+ |
BiConsumer<? super T, ? super U> action, |
4697 |
+ |
Executor executor) { |
4698 |
+ |
return cf.thenAcceptBothAsync(other, action, executor); } |
4699 |
+ |
public CompletionStage<Void> runAfterBoth |
4700 |
+ |
(CompletionStage<?> other, |
4701 |
+ |
Runnable action) { |
4702 |
+ |
return cf.runAfterBoth(other, action); } |
4703 |
+ |
public CompletionStage<Void> runAfterBothAsync |
4704 |
+ |
(CompletionStage<?> other, |
4705 |
+ |
Runnable action) { |
4706 |
+ |
return cf.runAfterBothAsync(other, action); } |
4707 |
+ |
public CompletionStage<Void> runAfterBothAsync |
4708 |
+ |
(CompletionStage<?> other, |
4709 |
+ |
Runnable action, |
4710 |
+ |
Executor executor) { |
4711 |
+ |
return cf.runAfterBothAsync(other, action, executor); } |
4712 |
+ |
public <U> CompletionStage<U> applyToEither |
4713 |
+ |
(CompletionStage<? extends T> other, |
4714 |
+ |
Function<? super T, U> fn) { |
4715 |
+ |
return cf.applyToEither(other, fn); } |
4716 |
+ |
public <U> CompletionStage<U> applyToEitherAsync |
4717 |
+ |
(CompletionStage<? extends T> other, |
4718 |
+ |
Function<? super T, U> fn) { |
4719 |
+ |
return cf.applyToEitherAsync(other, fn); } |
4720 |
+ |
public <U> CompletionStage<U> applyToEitherAsync |
4721 |
+ |
(CompletionStage<? extends T> other, |
4722 |
+ |
Function<? super T, U> fn, |
4723 |
+ |
Executor executor) { |
4724 |
+ |
return cf.applyToEitherAsync(other, fn, executor); } |
4725 |
+ |
public CompletionStage<Void> acceptEither |
4726 |
+ |
(CompletionStage<? extends T> other, |
4727 |
+ |
Consumer<? super T> action) { |
4728 |
+ |
return cf.acceptEither(other, action); } |
4729 |
+ |
public CompletionStage<Void> acceptEitherAsync |
4730 |
+ |
(CompletionStage<? extends T> other, |
4731 |
+ |
Consumer<? super T> action) { |
4732 |
+ |
return cf.acceptEitherAsync(other, action); } |
4733 |
+ |
public CompletionStage<Void> acceptEitherAsync |
4734 |
+ |
(CompletionStage<? extends T> other, |
4735 |
+ |
Consumer<? super T> action, |
4736 |
+ |
Executor executor) { |
4737 |
+ |
return cf.acceptEitherAsync(other, action, executor); } |
4738 |
+ |
public CompletionStage<Void> runAfterEither |
4739 |
+ |
(CompletionStage<?> other, |
4740 |
+ |
Runnable action) { |
4741 |
+ |
return cf.runAfterEither(other, action); } |
4742 |
+ |
public CompletionStage<Void> runAfterEitherAsync |
4743 |
+ |
(CompletionStage<?> other, |
4744 |
+ |
Runnable action) { |
4745 |
+ |
return cf.runAfterEitherAsync(other, action); } |
4746 |
+ |
public CompletionStage<Void> runAfterEitherAsync |
4747 |
+ |
(CompletionStage<?> other, |
4748 |
+ |
Runnable action, |
4749 |
+ |
Executor executor) { |
4750 |
+ |
return cf.runAfterEitherAsync(other, action, executor); } |
4751 |
+ |
public <U> CompletionStage<U> thenCompose |
4752 |
+ |
(Function<? super T, ? extends CompletionStage<U>> fn) { |
4753 |
+ |
return cf.thenCompose(fn); } |
4754 |
+ |
public <U> CompletionStage<U> thenComposeAsync |
4755 |
+ |
(Function<? super T, ? extends CompletionStage<U>> fn) { |
4756 |
+ |
return cf.thenComposeAsync(fn); } |
4757 |
+ |
public <U> CompletionStage<U> thenComposeAsync |
4758 |
+ |
(Function<? super T, ? extends CompletionStage<U>> fn, |
4759 |
+ |
Executor executor) { |
4760 |
+ |
return cf.thenComposeAsync(fn, executor); } |
4761 |
+ |
public <U> CompletionStage<U> handle |
4762 |
+ |
(BiFunction<? super T, Throwable, ? extends U> fn) { |
4763 |
+ |
return cf.handle(fn); } |
4764 |
+ |
public <U> CompletionStage<U> handleAsync |
4765 |
+ |
(BiFunction<? super T, Throwable, ? extends U> fn) { |
4766 |
+ |
return cf.handleAsync(fn); } |
4767 |
+ |
public <U> CompletionStage<U> handleAsync |
4768 |
+ |
(BiFunction<? super T, Throwable, ? extends U> fn, |
4769 |
+ |
Executor executor) { |
4770 |
+ |
return cf.handleAsync(fn, executor); } |
4771 |
+ |
public CompletionStage<T> whenComplete |
4772 |
+ |
(BiConsumer<? super T, ? super Throwable> action) { |
4773 |
+ |
return cf.whenComplete(action); } |
4774 |
+ |
public CompletionStage<T> whenCompleteAsync |
4775 |
+ |
(BiConsumer<? super T, ? super Throwable> action) { |
4776 |
+ |
return cf.whenCompleteAsync(action); } |
4777 |
+ |
public CompletionStage<T> whenCompleteAsync |
4778 |
+ |
(BiConsumer<? super T, ? super Throwable> action, |
4779 |
+ |
Executor executor) { |
4780 |
+ |
return cf.whenCompleteAsync(action, executor); } |
4781 |
+ |
public CompletionStage<T> exceptionally |
4782 |
+ |
(Function<Throwable, ? extends T> fn) { |
4783 |
+ |
return cf.exceptionally(fn); } |
4784 |
+ |
} |
4785 |
+ |
|
4786 |
+ |
/** |
4787 |
+ |
* default-implemented exceptionallyAsync action completes with |
4788 |
+ |
* function value on source exception |
4789 |
+ |
*/ |
4790 |
+ |
public void testDefaulExceptionallyAsync_exceptionalCompletion() { |
4791 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4792 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4793 |
+ |
{ |
4794 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
4795 |
+ |
final CFException ex = new CFException(); |
4796 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4797 |
+ |
final DelegatedCompletionStage<Integer> d = |
4798 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4799 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
4800 |
+ |
final CompletionStage<Integer> g = d.exceptionallyAsync |
4801 |
+ |
((Throwable t) -> { |
4802 |
+ |
threadAssertSame(t, ex); |
4803 |
+ |
a.getAndIncrement(); |
4804 |
+ |
return v1; |
4805 |
+ |
}); |
4806 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
4807 |
+ |
|
4808 |
+ |
checkCompletedNormally(g.toCompletableFuture(), v1); |
4809 |
+ |
assertEquals(1, a.get()); |
4810 |
+ |
}} |
4811 |
+ |
|
4812 |
+ |
/** |
4813 |
+ |
* Under default implementation, if an "exceptionally action" |
4814 |
+ |
* throws an exception, it completes exceptionally with that |
4815 |
+ |
* exception |
4816 |
+ |
*/ |
4817 |
+ |
public void testDefaulExceptionallyAsync_exceptionalCompletionActionFailed() { |
4818 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4819 |
+ |
{ |
4820 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
4821 |
+ |
final CFException ex1 = new CFException(); |
4822 |
+ |
final CFException ex2 = new CFException(); |
4823 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4824 |
+ |
final DelegatedCompletionStage<Integer> d = |
4825 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4826 |
+ |
if (!createIncomplete) f.completeExceptionally(ex1); |
4827 |
+ |
final CompletionStage<Integer> g = d.exceptionallyAsync |
4828 |
+ |
((Throwable t) -> { |
4829 |
+ |
threadAssertSame(t, ex1); |
4830 |
+ |
a.getAndIncrement(); |
4831 |
+ |
throw ex2; |
4832 |
+ |
}); |
4833 |
+ |
if (createIncomplete) f.completeExceptionally(ex1); |
4834 |
+ |
|
4835 |
+ |
checkCompletedWithWrappedException(g.toCompletableFuture(), ex2); |
4836 |
+ |
checkCompletedExceptionally(f, ex1); |
4837 |
+ |
checkCompletedExceptionally(d.toCompletableFuture(), ex1); |
4838 |
+ |
assertEquals(1, a.get()); |
4839 |
+ |
}} |
4840 |
+ |
|
4841 |
+ |
/** |
4842 |
+ |
* defult exceptionallyCompose result completes normally after normal |
4843 |
+ |
* completion of source |
4844 |
+ |
*/ |
4845 |
+ |
public void testDefaultExceptionallyCompose_normalCompletion() { |
4846 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4847 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4848 |
+ |
{ |
4849 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4850 |
+ |
final ExceptionalCompletableFutureFunction r = |
4851 |
+ |
new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
4852 |
+ |
final DelegatedCompletionStage<Integer> d = |
4853 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4854 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4855 |
+ |
final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
4856 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4857 |
+ |
|
4858 |
+ |
checkCompletedNormally(f, v1); |
4859 |
+ |
checkCompletedNormally(g.toCompletableFuture(), v1); |
4860 |
+ |
r.assertNotInvoked(); |
4861 |
+ |
}} |
4862 |
+ |
|
4863 |
+ |
/** |
4864 |
+ |
* default-implemented exceptionallyCompose result completes |
4865 |
+ |
* normally after exceptional completion of source |
4866 |
+ |
*/ |
4867 |
+ |
public void testDefaultExceptionallyCompose_exceptionalCompletion() { |
4868 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4869 |
+ |
{ |
4870 |
+ |
final CFException ex = new CFException(); |
4871 |
+ |
final ExceptionalCompletableFutureFunction r = |
4872 |
+ |
new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
4873 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4874 |
+ |
final DelegatedCompletionStage<Integer> d = |
4875 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4876 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
4877 |
+ |
final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
4878 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
4879 |
+ |
|
4880 |
+ |
checkCompletedExceptionally(f, ex); |
4881 |
+ |
checkCompletedNormally(g.toCompletableFuture(), r.value); |
4882 |
+ |
r.assertInvoked(); |
4883 |
+ |
}} |
4884 |
+ |
|
4885 |
+ |
/** |
4886 |
+ |
* default-implemented exceptionallyCompose completes |
4887 |
+ |
* exceptionally on exception if action does |
4888 |
+ |
*/ |
4889 |
+ |
public void testDefaultExceptionallyCompose_actionFailed() { |
4890 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4891 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4892 |
+ |
{ |
4893 |
+ |
final CFException ex = new CFException(); |
4894 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4895 |
+ |
final FailingExceptionalCompletableFutureFunction r |
4896 |
+ |
= new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
4897 |
+ |
final DelegatedCompletionStage<Integer> d = |
4898 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4899 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
4900 |
+ |
final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
4901 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
4902 |
+ |
|
4903 |
+ |
checkCompletedExceptionally(f, ex); |
4904 |
+ |
checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
4905 |
+ |
r.assertInvoked(); |
4906 |
+ |
}} |
4907 |
+ |
|
4908 |
+ |
/** |
4909 |
+ |
* defult exceptionallyComposeAsync result completes normally after normal |
4910 |
+ |
* completion of source |
4911 |
+ |
*/ |
4912 |
+ |
public void testDefaultExceptionallyComposeAsync_normalCompletion() { |
4913 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4914 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4915 |
+ |
{ |
4916 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4917 |
+ |
final ExceptionalCompletableFutureFunction r = |
4918 |
+ |
new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
4919 |
+ |
final DelegatedCompletionStage<Integer> d = |
4920 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4921 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4922 |
+ |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
4923 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4924 |
+ |
|
4925 |
+ |
checkCompletedNormally(f, v1); |
4926 |
+ |
checkCompletedNormally(g.toCompletableFuture(), v1); |
4927 |
+ |
r.assertNotInvoked(); |
4928 |
+ |
}} |
4929 |
+ |
|
4930 |
+ |
/** |
4931 |
+ |
* default-implemented exceptionallyComposeAsync result completes |
4932 |
+ |
* normally after exceptional completion of source |
4933 |
+ |
*/ |
4934 |
+ |
public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() { |
4935 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4936 |
+ |
{ |
4937 |
+ |
final CFException ex = new CFException(); |
4938 |
+ |
final ExceptionalCompletableFutureFunction r = |
4939 |
+ |
new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
4940 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4941 |
+ |
final DelegatedCompletionStage<Integer> d = |
4942 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4943 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
4944 |
+ |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
4945 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
4946 |
+ |
|
4947 |
+ |
checkCompletedExceptionally(f, ex); |
4948 |
+ |
checkCompletedNormally(g.toCompletableFuture(), r.value); |
4949 |
+ |
r.assertInvoked(); |
4950 |
+ |
}} |
4951 |
+ |
|
4952 |
+ |
/** |
4953 |
+ |
* default-implemented exceptionallyComposeAsync completes |
4954 |
+ |
* exceptionally on exception if action does |
4955 |
+ |
*/ |
4956 |
+ |
public void testDefaultExceptionallyComposeAsync_actionFailed() { |
4957 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4958 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4959 |
+ |
{ |
4960 |
+ |
final CFException ex = new CFException(); |
4961 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4962 |
+ |
final FailingExceptionalCompletableFutureFunction r |
4963 |
+ |
= new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
4964 |
+ |
final DelegatedCompletionStage<Integer> d = |
4965 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4966 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
4967 |
+ |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
4968 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
4969 |
+ |
|
4970 |
+ |
checkCompletedExceptionally(f, ex); |
4971 |
+ |
checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
4972 |
+ |
r.assertInvoked(); |
4973 |
+ |
}} |
4974 |
+ |
|
4975 |
+ |
|
4976 |
+ |
/** |
4977 |
+ |
* defult exceptionallyComposeAsync result completes normally after normal |
4978 |
+ |
* completion of source |
4979 |
+ |
*/ |
4980 |
+ |
public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() { |
4981 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4982 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4983 |
+ |
{ |
4984 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
4985 |
+ |
final ExceptionalCompletableFutureFunction r = |
4986 |
+ |
new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
4987 |
+ |
final DelegatedCompletionStage<Integer> d = |
4988 |
+ |
new DelegatedCompletionStage<Integer>(f); |
4989 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4990 |
+ |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
4991 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4992 |
+ |
|
4993 |
+ |
checkCompletedNormally(f, v1); |
4994 |
+ |
checkCompletedNormally(g.toCompletableFuture(), v1); |
4995 |
+ |
r.assertNotInvoked(); |
4996 |
+ |
}} |
4997 |
+ |
|
4998 |
+ |
/** |
4999 |
+ |
* default-implemented exceptionallyComposeAsync result completes |
5000 |
+ |
* normally after exceptional completion of source |
5001 |
+ |
*/ |
5002 |
+ |
public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() { |
5003 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
5004 |
+ |
{ |
5005 |
+ |
final CFException ex = new CFException(); |
5006 |
+ |
final ExceptionalCompletableFutureFunction r = |
5007 |
+ |
new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
5008 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
5009 |
+ |
final DelegatedCompletionStage<Integer> d = |
5010 |
+ |
new DelegatedCompletionStage<Integer>(f); |
5011 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
5012 |
+ |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
5013 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
5014 |
+ |
|
5015 |
+ |
checkCompletedExceptionally(f, ex); |
5016 |
+ |
checkCompletedNormally(g.toCompletableFuture(), r.value); |
5017 |
+ |
r.assertInvoked(); |
5018 |
+ |
}} |
5019 |
+ |
|
5020 |
+ |
/** |
5021 |
+ |
* default-implemented exceptionallyComposeAsync completes |
5022 |
+ |
* exceptionally on exception if action does |
5023 |
+ |
*/ |
5024 |
+ |
public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() { |
5025 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
5026 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
5027 |
+ |
{ |
5028 |
+ |
final CFException ex = new CFException(); |
5029 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
5030 |
+ |
final FailingExceptionalCompletableFutureFunction r |
5031 |
+ |
= new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
5032 |
+ |
final DelegatedCompletionStage<Integer> d = |
5033 |
+ |
new DelegatedCompletionStage<Integer>(f); |
5034 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
5035 |
+ |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
5036 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
5037 |
+ |
|
5038 |
+ |
checkCompletedExceptionally(f, ex); |
5039 |
+ |
checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
5040 |
+ |
r.assertInvoked(); |
5041 |
+ |
}} |
5042 |
+ |
|
5043 |
|
} |
5044 |
+ |
|