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()); |
82 |
< |
assertEquals(value, f.getNow(null)); |
83 |
< |
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()); |
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 |
|
}, |
672 |
– |
|
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; |
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 |
|
/** |