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(randomExpiredTimeout(), randomTimeUnit()); |
71 |
|
shouldThrow(); |
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 |
< |
assertEquals(value, f.getNow(null)); |
80 |
< |
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 |
|
}, |
652 |
– |
|
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 |
|
/** |
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 |
|
} |
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 |
|
} |