5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
> |
import static java.util.concurrent.CompletableFuture.completedFuture; |
11 |
> |
import static java.util.concurrent.CompletableFuture.failedFuture; |
12 |
> |
|
13 |
> |
import java.lang.reflect.Method; |
14 |
> |
import java.lang.reflect.Modifier; |
15 |
> |
|
16 |
> |
import java.util.stream.Collectors; |
17 |
> |
import java.util.stream.Stream; |
18 |
> |
|
19 |
> |
import java.util.ArrayList; |
20 |
> |
import java.util.Arrays; |
21 |
> |
import java.util.List; |
22 |
> |
import java.util.Objects; |
23 |
> |
import java.util.Set; |
24 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
25 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
26 |
|
import java.util.concurrent.CompletableFuture; |
27 |
|
import java.util.concurrent.CompletionException; |
28 |
|
import java.util.concurrent.CompletionStage; |
29 |
+ |
import java.util.concurrent.ExecutionException; |
30 |
+ |
import java.util.concurrent.Executor; |
31 |
|
import java.util.concurrent.ForkJoinPool; |
32 |
|
import java.util.concurrent.ForkJoinTask; |
33 |
+ |
import java.util.concurrent.RejectedExecutionException; |
34 |
|
import java.util.concurrent.TimeoutException; |
35 |
+ |
import java.util.concurrent.TimeUnit; |
36 |
|
import java.util.concurrent.atomic.AtomicInteger; |
37 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
25 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
26 |
< |
import java.util.*; |
27 |
< |
import java.util.function.Supplier; |
28 |
< |
import java.util.function.Consumer; |
37 |
> |
import java.util.concurrent.atomic.AtomicReference; |
38 |
|
import java.util.function.BiConsumer; |
30 |
– |
import java.util.function.Function; |
39 |
|
import java.util.function.BiFunction; |
40 |
+ |
import java.util.function.Consumer; |
41 |
+ |
import java.util.function.Function; |
42 |
+ |
import java.util.function.Predicate; |
43 |
+ |
import java.util.function.Supplier; |
44 |
+ |
|
45 |
+ |
import junit.framework.AssertionFailedError; |
46 |
+ |
import junit.framework.Test; |
47 |
+ |
import junit.framework.TestSuite; |
48 |
|
|
49 |
|
public class CompletableFutureTest extends JSR166TestCase { |
50 |
|
|
51 |
|
public static void main(String[] args) { |
52 |
< |
junit.textui.TestRunner.run(suite()); |
52 |
> |
main(suite(), args); |
53 |
|
} |
54 |
|
public static Test suite() { |
55 |
|
return new TestSuite(CompletableFutureTest.class); |
60 |
|
void checkIncomplete(CompletableFuture<?> f) { |
61 |
|
assertFalse(f.isDone()); |
62 |
|
assertFalse(f.isCancelled()); |
63 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
63 |
> |
assertTrue(f.toString().contains("Not completed")); |
64 |
|
try { |
65 |
|
assertNull(f.getNow(null)); |
66 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
90 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
91 |
|
} |
92 |
|
|
93 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
93 |
> |
/** |
94 |
> |
* Returns the "raw" internal exceptional completion of f, |
95 |
> |
* without any additional wrapping with CompletionException. |
96 |
> |
*/ |
97 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
98 |
> |
// handle (and whenComplete) can distinguish between "direct" |
99 |
> |
// and "wrapped" exceptional completion |
100 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
101 |
> |
} |
102 |
> |
|
103 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
104 |
> |
boolean wrapped, |
105 |
> |
Consumer<Throwable> checker) { |
106 |
> |
Throwable cause = exceptionalCompletion(f); |
107 |
> |
if (wrapped) { |
108 |
> |
assertTrue(cause instanceof CompletionException); |
109 |
> |
cause = cause.getCause(); |
110 |
> |
} |
111 |
> |
checker.accept(cause); |
112 |
> |
|
113 |
|
long startTime = System.nanoTime(); |
79 |
– |
long timeoutMillis = LONG_DELAY_MS; |
114 |
|
try { |
115 |
< |
f.get(timeoutMillis, MILLISECONDS); |
115 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
116 |
|
shouldThrow(); |
117 |
|
} catch (ExecutionException success) { |
118 |
< |
assertTrue(success.getCause() instanceof CFException); |
118 |
> |
assertSame(cause, success.getCause()); |
119 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
120 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
120 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
121 |
|
|
122 |
|
try { |
123 |
|
f.join(); |
124 |
|
shouldThrow(); |
125 |
|
} catch (CompletionException success) { |
126 |
< |
assertTrue(success.getCause() instanceof CFException); |
127 |
< |
} |
126 |
> |
assertSame(cause, success.getCause()); |
127 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
128 |
> |
|
129 |
|
try { |
130 |
|
f.getNow(null); |
131 |
|
shouldThrow(); |
132 |
|
} catch (CompletionException success) { |
133 |
< |
assertTrue(success.getCause() instanceof CFException); |
134 |
< |
} |
133 |
> |
assertSame(cause, success.getCause()); |
134 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
135 |
> |
|
136 |
|
try { |
137 |
|
f.get(); |
138 |
|
shouldThrow(); |
139 |
|
} catch (ExecutionException success) { |
140 |
< |
assertTrue(success.getCause() instanceof CFException); |
140 |
> |
assertSame(cause, success.getCause()); |
141 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
142 |
< |
assertTrue(f.isDone()); |
142 |
> |
|
143 |
|
assertFalse(f.isCancelled()); |
144 |
+ |
assertTrue(f.isDone()); |
145 |
+ |
assertTrue(f.isCompletedExceptionally()); |
146 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
147 |
|
} |
148 |
|
|
149 |
< |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
150 |
< |
Throwable ex) { |
151 |
< |
long startTime = System.nanoTime(); |
152 |
< |
long timeoutMillis = LONG_DELAY_MS; |
115 |
< |
try { |
116 |
< |
f.get(timeoutMillis, MILLISECONDS); |
117 |
< |
shouldThrow(); |
118 |
< |
} catch (ExecutionException success) { |
119 |
< |
assertSame(ex, success.getCause()); |
120 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
121 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
149 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
150 |
> |
checkCompletedExceptionally(f, true, |
151 |
> |
(t) -> assertTrue(t instanceof CFException)); |
152 |
> |
} |
153 |
|
|
154 |
< |
try { |
155 |
< |
f.join(); |
156 |
< |
shouldThrow(); |
157 |
< |
} catch (CompletionException success) { |
127 |
< |
assertSame(ex, success.getCause()); |
128 |
< |
} |
129 |
< |
try { |
130 |
< |
f.getNow(null); |
131 |
< |
shouldThrow(); |
132 |
< |
} catch (CompletionException success) { |
133 |
< |
assertSame(ex, success.getCause()); |
134 |
< |
} |
135 |
< |
try { |
136 |
< |
f.get(); |
137 |
< |
shouldThrow(); |
138 |
< |
} catch (ExecutionException success) { |
139 |
< |
assertSame(ex, success.getCause()); |
140 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
154 |
> |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
155 |
> |
checkCompletedExceptionally(f, true, |
156 |
> |
(t) -> assertTrue(t instanceof CancellationException)); |
157 |
> |
} |
158 |
|
|
159 |
< |
assertTrue(f.isDone()); |
160 |
< |
assertFalse(f.isCancelled()); |
161 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
159 |
> |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
160 |
> |
checkCompletedExceptionally(f, false, |
161 |
> |
(t) -> assertTrue(t instanceof TimeoutException)); |
162 |
|
} |
163 |
|
|
164 |
< |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
165 |
< |
Throwable ex) { |
166 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
150 |
< |
try { |
151 |
< |
CompletableFuture<Throwable> spy = f.handle |
152 |
< |
((U u, Throwable t) -> t); |
153 |
< |
assertTrue(spy.join() instanceof CompletionException); |
154 |
< |
assertSame(ex, spy.join().getCause()); |
155 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
164 |
> |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
165 |
> |
Throwable ex) { |
166 |
> |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
167 |
|
} |
168 |
|
|
169 |
< |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
170 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
160 |
< |
try { |
161 |
< |
CompletableFuture<Throwable> spy = f.handle |
162 |
< |
((U u, Throwable t) -> t); |
163 |
< |
assertSame(ex, spy.join()); |
164 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
169 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
170 |
> |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
171 |
|
} |
172 |
|
|
173 |
|
void checkCancelled(CompletableFuture<?> f) { |
174 |
|
long startTime = System.nanoTime(); |
169 |
– |
long timeoutMillis = LONG_DELAY_MS; |
175 |
|
try { |
176 |
< |
f.get(timeoutMillis, MILLISECONDS); |
176 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
177 |
|
shouldThrow(); |
178 |
|
} catch (CancellationException success) { |
179 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
180 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
180 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
181 |
|
|
182 |
|
try { |
183 |
|
f.join(); |
192 |
|
shouldThrow(); |
193 |
|
} catch (CancellationException success) { |
194 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
190 |
– |
assertTrue(f.isDone()); |
191 |
– |
assertTrue(f.isCompletedExceptionally()); |
192 |
– |
assertTrue(f.isCancelled()); |
193 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
194 |
– |
} |
195 |
|
|
196 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
197 |
< |
long startTime = System.nanoTime(); |
198 |
< |
long timeoutMillis = LONG_DELAY_MS; |
199 |
< |
try { |
200 |
< |
f.get(timeoutMillis, MILLISECONDS); |
201 |
< |
shouldThrow(); |
202 |
< |
} catch (ExecutionException success) { |
203 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
204 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
205 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
196 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
197 |
|
|
207 |
– |
try { |
208 |
– |
f.join(); |
209 |
– |
shouldThrow(); |
210 |
– |
} catch (CompletionException success) { |
211 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
212 |
– |
} |
213 |
– |
try { |
214 |
– |
f.getNow(null); |
215 |
– |
shouldThrow(); |
216 |
– |
} catch (CompletionException success) { |
217 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
218 |
– |
} |
219 |
– |
try { |
220 |
– |
f.get(); |
221 |
– |
shouldThrow(); |
222 |
– |
} catch (ExecutionException success) { |
223 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
224 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
198 |
|
assertTrue(f.isDone()); |
226 |
– |
assertFalse(f.isCancelled()); |
199 |
|
assertTrue(f.isCompletedExceptionally()); |
200 |
+ |
assertTrue(f.isCancelled()); |
201 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
202 |
|
} |
203 |
|
|
245 |
|
{ |
246 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
247 |
|
checkIncomplete(f); |
248 |
< |
assertTrue(f.cancel(true)); |
249 |
< |
assertTrue(f.cancel(true)); |
248 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
249 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
250 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
251 |
|
checkCancelled(f); |
252 |
|
}} |
253 |
|
|
460 |
|
class FailingSupplier extends CheckedAction |
461 |
|
implements Supplier<Integer> |
462 |
|
{ |
463 |
< |
FailingSupplier(ExecutionMode m) { super(m); } |
463 |
> |
final CFException ex; |
464 |
> |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
465 |
|
public Integer get() { |
466 |
|
invoked(); |
467 |
< |
throw new CFException(); |
467 |
> |
throw ex; |
468 |
|
} |
469 |
|
} |
470 |
|
|
471 |
|
class FailingConsumer extends CheckedIntegerAction |
472 |
|
implements Consumer<Integer> |
473 |
|
{ |
474 |
< |
FailingConsumer(ExecutionMode m) { super(m); } |
474 |
> |
final CFException ex; |
475 |
> |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
476 |
|
public void accept(Integer x) { |
477 |
|
invoked(); |
478 |
|
value = x; |
479 |
< |
throw new CFException(); |
479 |
> |
throw ex; |
480 |
|
} |
481 |
|
} |
482 |
|
|
483 |
|
class FailingBiConsumer extends CheckedIntegerAction |
484 |
|
implements BiConsumer<Integer, Integer> |
485 |
|
{ |
486 |
< |
FailingBiConsumer(ExecutionMode m) { super(m); } |
486 |
> |
final CFException ex; |
487 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
488 |
|
public void accept(Integer x, Integer y) { |
489 |
|
invoked(); |
490 |
|
value = subtract(x, y); |
491 |
< |
throw new CFException(); |
491 |
> |
throw ex; |
492 |
|
} |
493 |
|
} |
494 |
|
|
495 |
|
class FailingFunction extends CheckedIntegerAction |
496 |
|
implements Function<Integer, Integer> |
497 |
|
{ |
498 |
< |
FailingFunction(ExecutionMode m) { super(m); } |
498 |
> |
final CFException ex; |
499 |
> |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
500 |
|
public Integer apply(Integer x) { |
501 |
|
invoked(); |
502 |
|
value = x; |
503 |
< |
throw new CFException(); |
503 |
> |
throw ex; |
504 |
|
} |
505 |
|
} |
506 |
|
|
507 |
|
class FailingBiFunction extends CheckedIntegerAction |
508 |
|
implements BiFunction<Integer, Integer, Integer> |
509 |
|
{ |
510 |
< |
FailingBiFunction(ExecutionMode m) { super(m); } |
510 |
> |
final CFException ex; |
511 |
> |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
512 |
|
public Integer apply(Integer x, Integer y) { |
513 |
|
invoked(); |
514 |
|
value = subtract(x, y); |
515 |
< |
throw new CFException(); |
515 |
> |
throw ex; |
516 |
|
} |
517 |
|
} |
518 |
|
|
519 |
|
class FailingRunnable extends CheckedAction implements Runnable { |
520 |
< |
FailingRunnable(ExecutionMode m) { super(m); } |
520 |
> |
final CFException ex; |
521 |
> |
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
522 |
|
public void run() { |
523 |
|
invoked(); |
524 |
< |
throw new CFException(); |
524 |
> |
throw ex; |
525 |
|
} |
526 |
|
} |
527 |
|
|
548 |
– |
|
528 |
|
class CompletableFutureInc extends CheckedIntegerAction |
529 |
|
implements Function<Integer, CompletableFuture<Integer>> |
530 |
|
{ |
541 |
|
class FailingCompletableFutureFunction extends CheckedIntegerAction |
542 |
|
implements Function<Integer, CompletableFuture<Integer>> |
543 |
|
{ |
544 |
< |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
544 |
> |
final CFException ex; |
545 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
546 |
|
public CompletableFuture<Integer> apply(Integer x) { |
547 |
|
invoked(); |
548 |
|
value = x; |
549 |
< |
throw new CFException(); |
549 |
> |
throw ex; |
550 |
|
} |
551 |
|
} |
552 |
|
|
848 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
849 |
|
final CompletableFuture<Integer> g = f.exceptionally |
850 |
|
((Throwable t) -> { |
871 |
– |
// Should not be called |
851 |
|
a.getAndIncrement(); |
852 |
< |
throw new AssertionError(); |
852 |
> |
threadFail("should not be called"); |
853 |
> |
return null; // unreached |
854 |
|
}); |
855 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
856 |
|
|
884 |
|
assertEquals(1, a.get()); |
885 |
|
}} |
886 |
|
|
887 |
+ |
/** |
888 |
+ |
* If an "exceptionally action" throws an exception, it completes |
889 |
+ |
* exceptionally with that exception |
890 |
+ |
*/ |
891 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
892 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
909 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
893 |
|
{ |
894 |
|
final AtomicInteger a = new AtomicInteger(0); |
895 |
|
final CFException ex1 = new CFException(); |
906 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
907 |
|
|
908 |
|
checkCompletedWithWrappedException(g, ex2); |
909 |
+ |
checkCompletedExceptionally(f, ex1); |
910 |
|
assertEquals(1, a.get()); |
911 |
|
}} |
912 |
|
|
914 |
|
* whenComplete action executes on normal completion, propagating |
915 |
|
* source result. |
916 |
|
*/ |
917 |
< |
public void testWhenComplete_normalCompletion1() { |
917 |
> |
public void testWhenComplete_normalCompletion() { |
918 |
|
for (ExecutionMode m : ExecutionMode.values()) |
919 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
920 |
|
for (Integer v1 : new Integer[] { 1, null }) |
924 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
925 |
|
final CompletableFuture<Integer> g = m.whenComplete |
926 |
|
(f, |
927 |
< |
(Integer x, Throwable t) -> { |
927 |
> |
(Integer result, Throwable t) -> { |
928 |
|
m.checkExecutionMode(); |
929 |
< |
threadAssertSame(x, v1); |
929 |
> |
threadAssertSame(result, v1); |
930 |
|
threadAssertNull(t); |
931 |
|
a.getAndIncrement(); |
932 |
|
}); |
944 |
|
public void testWhenComplete_exceptionalCompletion() { |
945 |
|
for (ExecutionMode m : ExecutionMode.values()) |
946 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
963 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
947 |
|
{ |
948 |
|
final AtomicInteger a = new AtomicInteger(0); |
949 |
|
final CFException ex = new CFException(); |
951 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
952 |
|
final CompletableFuture<Integer> g = m.whenComplete |
953 |
|
(f, |
954 |
< |
(Integer x, Throwable t) -> { |
954 |
> |
(Integer result, Throwable t) -> { |
955 |
|
m.checkExecutionMode(); |
956 |
< |
threadAssertNull(x); |
956 |
> |
threadAssertNull(result); |
957 |
|
threadAssertSame(t, ex); |
958 |
|
a.getAndIncrement(); |
959 |
|
}); |
978 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
979 |
|
final CompletableFuture<Integer> g = m.whenComplete |
980 |
|
(f, |
981 |
< |
(Integer x, Throwable t) -> { |
981 |
> |
(Integer result, Throwable t) -> { |
982 |
|
m.checkExecutionMode(); |
983 |
< |
threadAssertNull(x); |
983 |
> |
threadAssertNull(result); |
984 |
|
threadAssertTrue(t instanceof CancellationException); |
985 |
|
a.getAndIncrement(); |
986 |
|
}); |
995 |
|
* If a whenComplete action throws an exception when triggered by |
996 |
|
* a normal completion, it completes exceptionally |
997 |
|
*/ |
998 |
< |
public void testWhenComplete_actionFailed() { |
998 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
999 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1000 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1001 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1006 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1007 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1008 |
|
(f, |
1009 |
< |
(Integer x, Throwable t) -> { |
1009 |
> |
(Integer result, Throwable t) -> { |
1010 |
|
m.checkExecutionMode(); |
1011 |
< |
threadAssertSame(x, v1); |
1011 |
> |
threadAssertSame(result, v1); |
1012 |
|
threadAssertNull(t); |
1013 |
|
a.getAndIncrement(); |
1014 |
|
throw ex; |
1023 |
|
/** |
1024 |
|
* If a whenComplete action throws an exception when triggered by |
1025 |
|
* a source completion that also throws an exception, the source |
1026 |
< |
* exception takes precedence. |
1026 |
> |
* exception takes precedence (unlike handle) |
1027 |
|
*/ |
1028 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1028 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1029 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1030 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1048 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1031 |
|
{ |
1032 |
|
final AtomicInteger a = new AtomicInteger(0); |
1033 |
|
final CFException ex1 = new CFException(); |
1037 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1038 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1039 |
|
(f, |
1040 |
< |
(Integer x, Throwable t) -> { |
1040 |
> |
(Integer result, Throwable t) -> { |
1041 |
|
m.checkExecutionMode(); |
1042 |
|
threadAssertSame(t, ex1); |
1043 |
< |
threadAssertNull(x); |
1043 |
> |
threadAssertNull(result); |
1044 |
|
a.getAndIncrement(); |
1045 |
|
throw ex2; |
1046 |
|
}); |
1048 |
|
|
1049 |
|
checkCompletedWithWrappedException(g, ex1); |
1050 |
|
checkCompletedExceptionally(f, ex1); |
1051 |
+ |
if (testImplementationDetails) { |
1052 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1053 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1054 |
+ |
} |
1055 |
|
assertEquals(1, a.get()); |
1056 |
|
}} |
1057 |
|
|
1069 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1070 |
|
final CompletableFuture<Integer> g = m.handle |
1071 |
|
(f, |
1072 |
< |
(Integer x, Throwable t) -> { |
1072 |
> |
(Integer result, Throwable t) -> { |
1073 |
|
m.checkExecutionMode(); |
1074 |
< |
threadAssertSame(x, v1); |
1074 |
> |
threadAssertSame(result, v1); |
1075 |
|
threadAssertNull(t); |
1076 |
|
a.getAndIncrement(); |
1077 |
|
return inc(v1); |
1098 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1099 |
|
final CompletableFuture<Integer> g = m.handle |
1100 |
|
(f, |
1101 |
< |
(Integer x, Throwable t) -> { |
1101 |
> |
(Integer result, Throwable t) -> { |
1102 |
|
m.checkExecutionMode(); |
1103 |
< |
threadAssertNull(x); |
1103 |
> |
threadAssertNull(result); |
1104 |
|
threadAssertSame(t, ex); |
1105 |
|
a.getAndIncrement(); |
1106 |
|
return v1; |
1127 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1128 |
|
final CompletableFuture<Integer> g = m.handle |
1129 |
|
(f, |
1130 |
< |
(Integer x, Throwable t) -> { |
1130 |
> |
(Integer result, Throwable t) -> { |
1131 |
|
m.checkExecutionMode(); |
1132 |
< |
threadAssertNull(x); |
1132 |
> |
threadAssertNull(result); |
1133 |
|
threadAssertTrue(t instanceof CancellationException); |
1134 |
|
a.getAndIncrement(); |
1135 |
|
return v1; |
1142 |
|
}} |
1143 |
|
|
1144 |
|
/** |
1145 |
< |
* handle result completes exceptionally if action does |
1145 |
> |
* If a "handle action" throws an exception when triggered by |
1146 |
> |
* a normal completion, it completes exceptionally |
1147 |
|
*/ |
1148 |
< |
public void testHandle_sourceFailedActionFailed() { |
1148 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1149 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1150 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1151 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1152 |
|
{ |
1153 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1154 |
|
final AtomicInteger a = new AtomicInteger(0); |
1155 |
< |
final CFException ex1 = new CFException(); |
1156 |
< |
final CFException ex2 = new CFException(); |
1169 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1155 |
> |
final CFException ex = new CFException(); |
1156 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1157 |
|
final CompletableFuture<Integer> g = m.handle |
1158 |
|
(f, |
1159 |
< |
(Integer x, Throwable t) -> { |
1159 |
> |
(Integer result, Throwable t) -> { |
1160 |
|
m.checkExecutionMode(); |
1161 |
< |
threadAssertNull(x); |
1162 |
< |
threadAssertSame(ex1, t); |
1161 |
> |
threadAssertSame(result, v1); |
1162 |
> |
threadAssertNull(t); |
1163 |
|
a.getAndIncrement(); |
1164 |
< |
throw ex2; |
1164 |
> |
throw ex; |
1165 |
|
}); |
1166 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1166 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1167 |
|
|
1168 |
< |
checkCompletedWithWrappedException(g, ex2); |
1169 |
< |
checkCompletedExceptionally(f, ex1); |
1168 |
> |
checkCompletedWithWrappedException(g, ex); |
1169 |
> |
checkCompletedNormally(f, v1); |
1170 |
|
assertEquals(1, a.get()); |
1171 |
|
}} |
1172 |
|
|
1173 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1174 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1173 |
> |
/** |
1174 |
> |
* If a "handle action" throws an exception when triggered by |
1175 |
> |
* a source completion that also throws an exception, the action |
1176 |
> |
* exception takes precedence (unlike whenComplete) |
1177 |
> |
*/ |
1178 |
> |
public void testHandle_sourceFailedActionFailed() { |
1179 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1180 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1180 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1181 |
|
{ |
1191 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1182 |
|
final AtomicInteger a = new AtomicInteger(0); |
1183 |
< |
final CFException ex = new CFException(); |
1184 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1183 |
> |
final CFException ex1 = new CFException(); |
1184 |
> |
final CFException ex2 = new CFException(); |
1185 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1186 |
> |
|
1187 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1188 |
|
final CompletableFuture<Integer> g = m.handle |
1189 |
|
(f, |
1190 |
< |
(Integer x, Throwable t) -> { |
1190 |
> |
(Integer result, Throwable t) -> { |
1191 |
|
m.checkExecutionMode(); |
1192 |
< |
threadAssertSame(x, v1); |
1193 |
< |
threadAssertNull(t); |
1192 |
> |
threadAssertNull(result); |
1193 |
> |
threadAssertSame(ex1, t); |
1194 |
|
a.getAndIncrement(); |
1195 |
< |
throw ex; |
1195 |
> |
throw ex2; |
1196 |
|
}); |
1197 |
< |
if (createIncomplete) assertTrue(f.complete(v1)); |
1197 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1198 |
|
|
1199 |
< |
checkCompletedWithWrappedException(g, ex); |
1200 |
< |
checkCompletedNormally(f, v1); |
1199 |
> |
checkCompletedWithWrappedException(g, ex2); |
1200 |
> |
checkCompletedExceptionally(f, ex1); |
1201 |
|
assertEquals(1, a.get()); |
1202 |
|
}} |
1203 |
|
|
1230 |
|
{ |
1231 |
|
final FailingRunnable r = new FailingRunnable(m); |
1232 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1233 |
< |
checkCompletedWithWrappedCFException(f); |
1233 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1234 |
|
r.assertInvoked(); |
1235 |
|
}} |
1236 |
|
|
1264 |
|
{ |
1265 |
|
FailingSupplier r = new FailingSupplier(m); |
1266 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1267 |
< |
checkCompletedWithWrappedCFException(f); |
1267 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1268 |
|
r.assertInvoked(); |
1269 |
|
}} |
1270 |
|
|
1386 |
|
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1387 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1388 |
|
|
1389 |
< |
checkCompletedWithWrappedCFException(h0); |
1390 |
< |
checkCompletedWithWrappedCFException(h1); |
1391 |
< |
checkCompletedWithWrappedCFException(h2); |
1392 |
< |
checkCompletedWithWrappedCFException(h3); |
1393 |
< |
checkCompletedWithWrappedCFException(h4); |
1394 |
< |
checkCompletedWithWrappedCFException(h5); |
1389 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1390 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1391 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1392 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1393 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1394 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1395 |
|
checkCompletedNormally(f, v1); |
1396 |
|
}} |
1397 |
|
|
1490 |
|
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1491 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1492 |
|
|
1493 |
< |
checkCompletedWithWrappedCFException(h0); |
1494 |
< |
checkCompletedWithWrappedCFException(h1); |
1495 |
< |
checkCompletedWithWrappedCFException(h2); |
1496 |
< |
checkCompletedWithWrappedCFException(h3); |
1493 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1494 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1495 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1496 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1497 |
|
checkCompletedNormally(f, v1); |
1498 |
|
}} |
1499 |
|
|
1592 |
|
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1593 |
|
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1594 |
|
|
1595 |
< |
checkCompletedWithWrappedCFException(h0); |
1596 |
< |
checkCompletedWithWrappedCFException(h1); |
1597 |
< |
checkCompletedWithWrappedCFException(h2); |
1598 |
< |
checkCompletedWithWrappedCFException(h3); |
1595 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1596 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1597 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1598 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1599 |
|
checkCompletedNormally(f, v1); |
1600 |
|
}} |
1601 |
|
|
1757 |
|
assertTrue(snd.complete(w2)); |
1758 |
|
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1759 |
|
|
1760 |
< |
checkCompletedWithWrappedCFException(h1); |
1761 |
< |
checkCompletedWithWrappedCFException(h2); |
1762 |
< |
checkCompletedWithWrappedCFException(h3); |
1760 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1761 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1762 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1763 |
|
r1.assertInvoked(); |
1764 |
|
r2.assertInvoked(); |
1765 |
|
r3.assertInvoked(); |
1921 |
|
assertTrue(snd.complete(w2)); |
1922 |
|
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1923 |
|
|
1924 |
< |
checkCompletedWithWrappedCFException(h1); |
1925 |
< |
checkCompletedWithWrappedCFException(h2); |
1926 |
< |
checkCompletedWithWrappedCFException(h3); |
1924 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1925 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1926 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1927 |
|
r1.assertInvoked(); |
1928 |
|
r2.assertInvoked(); |
1929 |
|
r3.assertInvoked(); |
2085 |
|
assertTrue(snd.complete(w2)); |
2086 |
|
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2087 |
|
|
2088 |
< |
checkCompletedWithWrappedCFException(h1); |
2089 |
< |
checkCompletedWithWrappedCFException(h2); |
2090 |
< |
checkCompletedWithWrappedCFException(h3); |
2088 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2089 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2090 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2091 |
|
r1.assertInvoked(); |
2092 |
|
r2.assertInvoked(); |
2093 |
|
r3.assertInvoked(); |
2377 |
|
f.complete(v1); |
2378 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2379 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2380 |
< |
checkCompletedWithWrappedCFException(h0); |
2381 |
< |
checkCompletedWithWrappedCFException(h1); |
2382 |
< |
checkCompletedWithWrappedCFException(h2); |
2383 |
< |
checkCompletedWithWrappedCFException(h3); |
2380 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2381 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2382 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2383 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2384 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2385 |
|
|
2386 |
|
g.complete(v2); |
2389 |
|
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2390 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2391 |
|
|
2392 |
< |
checkCompletedWithWrappedCFException(h4); |
2392 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2393 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2394 |
|
Objects.equals(v2, rs[4].value)); |
2395 |
< |
checkCompletedWithWrappedCFException(h5); |
2395 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2396 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2397 |
|
Objects.equals(v2, rs[5].value)); |
2398 |
|
|
2636 |
|
f.complete(v1); |
2637 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2638 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2639 |
< |
checkCompletedWithWrappedCFException(h0); |
2640 |
< |
checkCompletedWithWrappedCFException(h1); |
2641 |
< |
checkCompletedWithWrappedCFException(h2); |
2642 |
< |
checkCompletedWithWrappedCFException(h3); |
2639 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2640 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2641 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2642 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2643 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2644 |
|
|
2645 |
|
g.complete(v2); |
2648 |
|
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2649 |
|
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2650 |
|
|
2651 |
< |
checkCompletedWithWrappedCFException(h4); |
2651 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2652 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2653 |
|
Objects.equals(v2, rs[4].value)); |
2654 |
< |
checkCompletedWithWrappedCFException(h5); |
2654 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2655 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2656 |
|
Objects.equals(v2, rs[5].value)); |
2657 |
|
|
2891 |
|
assertTrue(f.complete(v1)); |
2892 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2893 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2894 |
< |
checkCompletedWithWrappedCFException(h0); |
2895 |
< |
checkCompletedWithWrappedCFException(h1); |
2896 |
< |
checkCompletedWithWrappedCFException(h2); |
2897 |
< |
checkCompletedWithWrappedCFException(h3); |
2894 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2895 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2896 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2897 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2898 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2899 |
|
assertTrue(g.complete(v2)); |
2900 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2901 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2902 |
< |
checkCompletedWithWrappedCFException(h4); |
2903 |
< |
checkCompletedWithWrappedCFException(h5); |
2902 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2903 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2904 |
|
|
2905 |
|
checkCompletedNormally(f, v1); |
2906 |
|
checkCompletedNormally(g, v2); |
2961 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2962 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
2963 |
|
|
2964 |
< |
checkCompletedWithWrappedCFException(g); |
2964 |
> |
checkCompletedWithWrappedException(g, r.ex); |
2965 |
|
checkCompletedNormally(f, v1); |
2966 |
|
}} |
2967 |
|
|
2986 |
|
checkCancelled(f); |
2987 |
|
}} |
2988 |
|
|
2989 |
+ |
/** |
2990 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
2991 |
+ |
*/ |
2992 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
2993 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2994 |
+ |
for (int order = 0; order < 6; order++) |
2995 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
2996 |
+ |
{ |
2997 |
+ |
final CFException ex = new CFException(); |
2998 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2999 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3000 |
+ |
final CompletableFuture<Integer> h; |
3001 |
+ |
// Test all permutations of orders |
3002 |
+ |
switch (order) { |
3003 |
+ |
case 0: |
3004 |
+ |
assertTrue(f.complete(v1)); |
3005 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3006 |
+ |
h = m.thenCompose(f, (x -> g)); |
3007 |
+ |
break; |
3008 |
+ |
case 1: |
3009 |
+ |
assertTrue(f.complete(v1)); |
3010 |
+ |
h = m.thenCompose(f, (x -> g)); |
3011 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3012 |
+ |
break; |
3013 |
+ |
case 2: |
3014 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3015 |
+ |
assertTrue(f.complete(v1)); |
3016 |
+ |
h = m.thenCompose(f, (x -> g)); |
3017 |
+ |
break; |
3018 |
+ |
case 3: |
3019 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3020 |
+ |
h = m.thenCompose(f, (x -> g)); |
3021 |
+ |
assertTrue(f.complete(v1)); |
3022 |
+ |
break; |
3023 |
+ |
case 4: |
3024 |
+ |
h = m.thenCompose(f, (x -> g)); |
3025 |
+ |
assertTrue(f.complete(v1)); |
3026 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3027 |
+ |
break; |
3028 |
+ |
case 5: |
3029 |
+ |
h = m.thenCompose(f, (x -> g)); |
3030 |
+ |
assertTrue(f.complete(v1)); |
3031 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3032 |
+ |
break; |
3033 |
+ |
default: throw new AssertionError(); |
3034 |
+ |
} |
3035 |
+ |
|
3036 |
+ |
checkCompletedExceptionally(g, ex); |
3037 |
+ |
checkCompletedWithWrappedException(h, ex); |
3038 |
+ |
checkCompletedNormally(f, v1); |
3039 |
+ |
}} |
3040 |
+ |
|
3041 |
|
// other static methods |
3042 |
|
|
3043 |
|
/** |
3070 |
|
} |
3071 |
|
} |
3072 |
|
|
3073 |
< |
public void testAllOf_backwards() throws Exception { |
3073 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3074 |
|
for (int k = 1; k < 10; k++) { |
3075 |
|
CompletableFuture<Integer>[] fs |
3076 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3098 |
|
for (int i = 0; i < k; i++) { |
3099 |
|
checkIncomplete(f); |
3100 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3101 |
< |
if (i != k/2) { |
3101 |
> |
if (i != k / 2) { |
3102 |
|
fs[i].complete(i); |
3103 |
|
checkCompletedNormally(fs[i], i); |
3104 |
|
} else { |
3205 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3206 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3207 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
3163 |
– |
CompletableFuture<?> h; |
3208 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3209 |
|
|
3210 |
|
Runnable[] throwingActions = { |
3301 |
|
() -> CompletableFuture.anyOf(null, f), |
3302 |
|
|
3303 |
|
() -> f.obtrudeException(null), |
3304 |
+ |
|
3305 |
+ |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3306 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3307 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null), |
3308 |
+ |
|
3309 |
+ |
() -> f.orTimeout(1L, null), |
3310 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3311 |
+ |
|
3312 |
+ |
() -> CompletableFuture.failedFuture(null), |
3313 |
+ |
() -> CompletableFuture.failedStage(null), |
3314 |
|
}; |
3315 |
|
|
3316 |
|
assertThrows(NullPointerException.class, throwingActions); |
3318 |
|
} |
3319 |
|
|
3320 |
|
/** |
3321 |
+ |
* Test submissions to an executor that rejects all tasks. |
3322 |
+ |
*/ |
3323 |
+ |
public void testRejectingExecutor() { |
3324 |
+ |
final RejectedExecutionException ex = new RejectedExecutionException(); |
3325 |
+ |
final Executor e = (Runnable r) -> { throw ex; }; |
3326 |
+ |
|
3327 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3328 |
+ |
|
3329 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3330 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3331 |
+ |
|
3332 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3333 |
+ |
|
3334 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3335 |
+ |
srcs.add(complete); |
3336 |
+ |
srcs.add(incomplete); |
3337 |
+ |
|
3338 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3339 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3340 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3341 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3342 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3343 |
+ |
|
3344 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3345 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3346 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3347 |
+ |
|
3348 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3349 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3350 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3351 |
+ |
|
3352 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3353 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3354 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3355 |
+ |
|
3356 |
+ |
for (CompletableFuture<?> future : fs) { |
3357 |
+ |
if (src.isDone()) |
3358 |
+ |
checkCompletedWithWrappedException(future, ex); |
3359 |
+ |
else |
3360 |
+ |
checkIncomplete(future); |
3361 |
+ |
} |
3362 |
+ |
futures.addAll(fs); |
3363 |
+ |
} |
3364 |
+ |
|
3365 |
+ |
{ |
3366 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3367 |
+ |
|
3368 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3369 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3370 |
+ |
|
3371 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3372 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3373 |
+ |
|
3374 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3375 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3376 |
+ |
|
3377 |
+ |
for (CompletableFuture<?> future : fs) |
3378 |
+ |
checkIncomplete(future); |
3379 |
+ |
futures.addAll(fs); |
3380 |
+ |
} |
3381 |
+ |
|
3382 |
+ |
{ |
3383 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3384 |
+ |
|
3385 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3386 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3387 |
+ |
|
3388 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3389 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3390 |
+ |
|
3391 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3392 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3393 |
+ |
|
3394 |
+ |
for (CompletableFuture<?> future : fs) |
3395 |
+ |
checkCompletedWithWrappedException(future, ex); |
3396 |
+ |
futures.addAll(fs); |
3397 |
+ |
} |
3398 |
+ |
|
3399 |
+ |
incomplete.complete(v); |
3400 |
+ |
|
3401 |
+ |
for (CompletableFuture<?> future : futures) |
3402 |
+ |
checkCompletedWithWrappedException(future, ex); |
3403 |
+ |
} |
3404 |
+ |
} |
3405 |
+ |
|
3406 |
+ |
/** |
3407 |
|
* toCompletableFuture returns this CompletableFuture. |
3408 |
|
*/ |
3409 |
|
public void testToCompletableFuture() { |
3411 |
|
assertSame(f, f.toCompletableFuture()); |
3412 |
|
} |
3413 |
|
|
3414 |
+ |
// jdk9 |
3415 |
+ |
|
3416 |
+ |
/** |
3417 |
+ |
* newIncompleteFuture returns an incomplete CompletableFuture |
3418 |
+ |
*/ |
3419 |
+ |
public void testNewIncompleteFuture() { |
3420 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3421 |
+ |
{ |
3422 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3423 |
+ |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3424 |
+ |
checkIncomplete(f); |
3425 |
+ |
checkIncomplete(g); |
3426 |
+ |
f.complete(v1); |
3427 |
+ |
checkCompletedNormally(f, v1); |
3428 |
+ |
checkIncomplete(g); |
3429 |
+ |
g.complete(v1); |
3430 |
+ |
checkCompletedNormally(g, v1); |
3431 |
+ |
assertSame(g.getClass(), CompletableFuture.class); |
3432 |
+ |
}} |
3433 |
+ |
|
3434 |
+ |
/** |
3435 |
+ |
* completedStage returns a completed CompletionStage |
3436 |
+ |
*/ |
3437 |
+ |
public void testCompletedStage() { |
3438 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3439 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3440 |
+ |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3441 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3442 |
+ |
assertEquals(x.get(), 1); |
3443 |
+ |
assertNull(r.get()); |
3444 |
+ |
} |
3445 |
+ |
|
3446 |
+ |
/** |
3447 |
+ |
* defaultExecutor by default returns the commonPool if |
3448 |
+ |
* it supports more than one thread. |
3449 |
+ |
*/ |
3450 |
+ |
public void testDefaultExecutor() { |
3451 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3452 |
+ |
Executor e = f.defaultExecutor(); |
3453 |
+ |
Executor c = ForkJoinPool.commonPool(); |
3454 |
+ |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3455 |
+ |
assertSame(e, c); |
3456 |
+ |
else |
3457 |
+ |
assertNotSame(e, c); |
3458 |
+ |
} |
3459 |
+ |
|
3460 |
+ |
/** |
3461 |
+ |
* failedFuture returns a CompletableFuture completed |
3462 |
+ |
* exceptionally with the given Exception |
3463 |
+ |
*/ |
3464 |
+ |
public void testFailedFuture() { |
3465 |
+ |
CFException ex = new CFException(); |
3466 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3467 |
+ |
checkCompletedExceptionally(f, ex); |
3468 |
+ |
} |
3469 |
+ |
|
3470 |
+ |
/** |
3471 |
+ |
* failedFuture(null) throws NPE |
3472 |
+ |
*/ |
3473 |
+ |
public void testFailedFuture_null() { |
3474 |
+ |
try { |
3475 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3476 |
+ |
shouldThrow(); |
3477 |
+ |
} catch (NullPointerException success) {} |
3478 |
+ |
} |
3479 |
+ |
|
3480 |
+ |
/** |
3481 |
+ |
* copy returns a CompletableFuture that is completed normally, |
3482 |
+ |
* with the same value, when source is. |
3483 |
+ |
*/ |
3484 |
+ |
public void testCopy() { |
3485 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3486 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3487 |
+ |
checkIncomplete(f); |
3488 |
+ |
checkIncomplete(g); |
3489 |
+ |
f.complete(1); |
3490 |
+ |
checkCompletedNormally(f, 1); |
3491 |
+ |
checkCompletedNormally(g, 1); |
3492 |
+ |
} |
3493 |
+ |
|
3494 |
+ |
/** |
3495 |
+ |
* copy returns a CompletableFuture that is completed exceptionally |
3496 |
+ |
* when source is. |
3497 |
+ |
*/ |
3498 |
+ |
public void testCopy2() { |
3499 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3500 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3501 |
+ |
checkIncomplete(f); |
3502 |
+ |
checkIncomplete(g); |
3503 |
+ |
CFException ex = new CFException(); |
3504 |
+ |
f.completeExceptionally(ex); |
3505 |
+ |
checkCompletedExceptionally(f, ex); |
3506 |
+ |
checkCompletedWithWrappedException(g, ex); |
3507 |
+ |
} |
3508 |
+ |
|
3509 |
+ |
/** |
3510 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3511 |
+ |
* completed normally, with the same value, when source is. |
3512 |
+ |
*/ |
3513 |
+ |
public void testMinimalCompletionStage() { |
3514 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3515 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3516 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3517 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3518 |
+ |
checkIncomplete(f); |
3519 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3520 |
+ |
f.complete(1); |
3521 |
+ |
checkCompletedNormally(f, 1); |
3522 |
+ |
assertEquals(x.get(), 1); |
3523 |
+ |
assertNull(r.get()); |
3524 |
+ |
} |
3525 |
+ |
|
3526 |
+ |
/** |
3527 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3528 |
+ |
* completed exceptionally when source is. |
3529 |
+ |
*/ |
3530 |
+ |
public void testMinimalCompletionStage2() { |
3531 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3532 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3533 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3534 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3535 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3536 |
+ |
checkIncomplete(f); |
3537 |
+ |
CFException ex = new CFException(); |
3538 |
+ |
f.completeExceptionally(ex); |
3539 |
+ |
checkCompletedExceptionally(f, ex); |
3540 |
+ |
assertEquals(x.get(), 0); |
3541 |
+ |
assertEquals(r.get().getCause(), ex); |
3542 |
+ |
} |
3543 |
+ |
|
3544 |
+ |
/** |
3545 |
+ |
* failedStage returns a CompletionStage completed |
3546 |
+ |
* exceptionally with the given Exception |
3547 |
+ |
*/ |
3548 |
+ |
public void testFailedStage() { |
3549 |
+ |
CFException ex = new CFException(); |
3550 |
+ |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3551 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3552 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3553 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3554 |
+ |
assertEquals(x.get(), 0); |
3555 |
+ |
assertEquals(r.get(), ex); |
3556 |
+ |
} |
3557 |
+ |
|
3558 |
+ |
/** |
3559 |
+ |
* completeAsync completes with value of given supplier |
3560 |
+ |
*/ |
3561 |
+ |
public void testCompleteAsync() { |
3562 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3563 |
+ |
{ |
3564 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3565 |
+ |
f.completeAsync(() -> v1); |
3566 |
+ |
f.join(); |
3567 |
+ |
checkCompletedNormally(f, v1); |
3568 |
+ |
}} |
3569 |
+ |
|
3570 |
+ |
/** |
3571 |
+ |
* completeAsync completes exceptionally if given supplier throws |
3572 |
+ |
*/ |
3573 |
+ |
public void testCompleteAsync2() { |
3574 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3575 |
+ |
CFException ex = new CFException(); |
3576 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3577 |
+ |
try { |
3578 |
+ |
f.join(); |
3579 |
+ |
shouldThrow(); |
3580 |
+ |
} catch (CompletionException success) {} |
3581 |
+ |
checkCompletedWithWrappedException(f, ex); |
3582 |
+ |
} |
3583 |
+ |
|
3584 |
+ |
/** |
3585 |
+ |
* completeAsync with given executor completes with value of given supplier |
3586 |
+ |
*/ |
3587 |
+ |
public void testCompleteAsync3() { |
3588 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3589 |
+ |
{ |
3590 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3591 |
+ |
ThreadExecutor executor = new ThreadExecutor(); |
3592 |
+ |
f.completeAsync(() -> v1, executor); |
3593 |
+ |
assertSame(v1, f.join()); |
3594 |
+ |
checkCompletedNormally(f, v1); |
3595 |
+ |
assertEquals(1, executor.count.get()); |
3596 |
+ |
}} |
3597 |
+ |
|
3598 |
+ |
/** |
3599 |
+ |
* completeAsync with given executor completes exceptionally if |
3600 |
+ |
* given supplier throws |
3601 |
+ |
*/ |
3602 |
+ |
public void testCompleteAsync4() { |
3603 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3604 |
+ |
CFException ex = new CFException(); |
3605 |
+ |
ThreadExecutor executor = new ThreadExecutor(); |
3606 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3607 |
+ |
try { |
3608 |
+ |
f.join(); |
3609 |
+ |
shouldThrow(); |
3610 |
+ |
} catch (CompletionException success) {} |
3611 |
+ |
checkCompletedWithWrappedException(f, ex); |
3612 |
+ |
assertEquals(1, executor.count.get()); |
3613 |
+ |
} |
3614 |
+ |
|
3615 |
+ |
/** |
3616 |
+ |
* orTimeout completes with TimeoutException if not complete |
3617 |
+ |
*/ |
3618 |
+ |
public void testOrTimeout_timesOut() { |
3619 |
+ |
long timeoutMillis = timeoutMillis(); |
3620 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3621 |
+ |
long startTime = System.nanoTime(); |
3622 |
+ |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3623 |
+ |
checkCompletedWithTimeoutException(f); |
3624 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3625 |
+ |
} |
3626 |
+ |
|
3627 |
+ |
/** |
3628 |
+ |
* orTimeout completes normally if completed before timeout |
3629 |
+ |
*/ |
3630 |
+ |
public void testOrTimeout_completed() { |
3631 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3632 |
+ |
{ |
3633 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3634 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3635 |
+ |
long startTime = System.nanoTime(); |
3636 |
+ |
f.complete(v1); |
3637 |
+ |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3638 |
+ |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3639 |
+ |
g.complete(v1); |
3640 |
+ |
checkCompletedNormally(f, v1); |
3641 |
+ |
checkCompletedNormally(g, v1); |
3642 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3643 |
+ |
}} |
3644 |
+ |
|
3645 |
+ |
/** |
3646 |
+ |
* completeOnTimeout completes with given value if not complete |
3647 |
+ |
*/ |
3648 |
+ |
public void testCompleteOnTimeout_timesOut() { |
3649 |
+ |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3650 |
+ |
() -> testCompleteOnTimeout_timesOut(null)); |
3651 |
+ |
} |
3652 |
+ |
|
3653 |
+ |
/** |
3654 |
+ |
* completeOnTimeout completes with given value if not complete |
3655 |
+ |
*/ |
3656 |
+ |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3657 |
+ |
long timeoutMillis = timeoutMillis(); |
3658 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3659 |
+ |
long startTime = System.nanoTime(); |
3660 |
+ |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3661 |
+ |
assertSame(v, f.join()); |
3662 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3663 |
+ |
f.complete(99); // should have no effect |
3664 |
+ |
checkCompletedNormally(f, v); |
3665 |
+ |
} |
3666 |
+ |
|
3667 |
+ |
/** |
3668 |
+ |
* completeOnTimeout has no effect if completed within timeout |
3669 |
+ |
*/ |
3670 |
+ |
public void testCompleteOnTimeout_completed() { |
3671 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3672 |
+ |
{ |
3673 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3674 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3675 |
+ |
long startTime = System.nanoTime(); |
3676 |
+ |
f.complete(v1); |
3677 |
+ |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3678 |
+ |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3679 |
+ |
g.complete(v1); |
3680 |
+ |
checkCompletedNormally(f, v1); |
3681 |
+ |
checkCompletedNormally(g, v1); |
3682 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3683 |
+ |
}} |
3684 |
+ |
|
3685 |
+ |
/** |
3686 |
+ |
* delayedExecutor returns an executor that delays submission |
3687 |
+ |
*/ |
3688 |
+ |
public void testDelayedExecutor() { |
3689 |
+ |
testInParallel(() -> testDelayedExecutor(null, null), |
3690 |
+ |
() -> testDelayedExecutor(null, 1), |
3691 |
+ |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3692 |
+ |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3693 |
+ |
} |
3694 |
+ |
|
3695 |
+ |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3696 |
+ |
long timeoutMillis = timeoutMillis(); |
3697 |
+ |
// Use an "unreasonably long" long timeout to catch lingering threads |
3698 |
+ |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3699 |
+ |
final Executor delayer, longDelayer; |
3700 |
+ |
if (executor == null) { |
3701 |
+ |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3702 |
+ |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3703 |
+ |
} else { |
3704 |
+ |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3705 |
+ |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3706 |
+ |
} |
3707 |
+ |
long startTime = System.nanoTime(); |
3708 |
+ |
CompletableFuture<Integer> f = |
3709 |
+ |
CompletableFuture.supplyAsync(() -> v, delayer); |
3710 |
+ |
CompletableFuture<Integer> g = |
3711 |
+ |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3712 |
+ |
|
3713 |
+ |
assertNull(g.getNow(null)); |
3714 |
+ |
|
3715 |
+ |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3716 |
+ |
long millisElapsed = millisElapsedSince(startTime); |
3717 |
+ |
assertTrue(millisElapsed >= timeoutMillis); |
3718 |
+ |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3719 |
+ |
|
3720 |
+ |
checkCompletedNormally(f, v); |
3721 |
+ |
|
3722 |
+ |
checkIncomplete(g); |
3723 |
+ |
assertTrue(g.cancel(true)); |
3724 |
+ |
} |
3725 |
+ |
|
3726 |
|
//--- tests of implementation details; not part of official tck --- |
3727 |
|
|
3728 |
|
Object resultOf(CompletableFuture<?> f) { |
3729 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3730 |
+ |
if (sm != null) { |
3731 |
+ |
try { |
3732 |
+ |
System.setSecurityManager(null); |
3733 |
+ |
} catch (SecurityException giveUp) { |
3734 |
+ |
return "Reflection not available"; |
3735 |
+ |
} |
3736 |
+ |
} |
3737 |
+ |
|
3738 |
|
try { |
3739 |
|
java.lang.reflect.Field resultField |
3740 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3741 |
|
resultField.setAccessible(true); |
3742 |
|
return resultField.get(f); |
3743 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3743 |
> |
} catch (Throwable t) { |
3744 |
> |
throw new AssertionError(t); |
3745 |
> |
} finally { |
3746 |
> |
if (sm != null) System.setSecurityManager(sm); |
3747 |
> |
} |
3748 |
|
} |
3749 |
|
|
3750 |
|
public void testExceptionPropagationReusesResultObject() { |
3767 |
|
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3768 |
|
|
3769 |
|
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3770 |
+ |
funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); |
3771 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3772 |
+ |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3773 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3774 |
+ |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3775 |
|
|
3776 |
< |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3776 |
> |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3777 |
|
|
3778 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3779 |
|
|
3780 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y})); |
3781 |
|
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3782 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y})); |
3783 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y})); |
3784 |
|
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3785 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y})); |
3786 |
|
|
3787 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3788 |
|
fun : funs) { |
3833 |
|
} |
3834 |
|
}} |
3835 |
|
|
3836 |
+ |
/** |
3837 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3838 |
+ |
*/ |
3839 |
+ |
public void testMinimalCompletionStage_minimality() { |
3840 |
+ |
if (!testImplementationDetails) return; |
3841 |
+ |
Function<Method, String> toSignature = |
3842 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3843 |
+ |
Predicate<Method> isNotStatic = |
3844 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3845 |
+ |
List<Method> minimalMethods = |
3846 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3847 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3848 |
+ |
.filter(isNotStatic) |
3849 |
+ |
.collect(Collectors.toList()); |
3850 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3851 |
+ |
String[] signatureWhitelist = { |
3852 |
+ |
"newIncompleteFuture[]", |
3853 |
+ |
"defaultExecutor[]", |
3854 |
+ |
"minimalCompletionStage[]", |
3855 |
+ |
"copy[]", |
3856 |
+ |
}; |
3857 |
+ |
Set<String> permittedMethodSignatures = |
3858 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3859 |
+ |
Stream.of(signatureWhitelist)) |
3860 |
+ |
.collect(Collectors.toSet()); |
3861 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3862 |
+ |
.filter(isNotStatic) |
3863 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3864 |
+ |
.collect(Collectors.toList()); |
3865 |
+ |
|
3866 |
+ |
CompletionStage<Integer> minimalStage = |
3867 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3868 |
+ |
|
3869 |
+ |
List<Method> bugs = new ArrayList<>(); |
3870 |
+ |
for (Method method : allMethods) { |
3871 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3872 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3873 |
+ |
// Manufacture boxed primitives for primitive params |
3874 |
+ |
for (int i = 0; i < args.length; i++) { |
3875 |
+ |
Class<?> type = parameterTypes[i]; |
3876 |
+ |
if (parameterTypes[i] == boolean.class) |
3877 |
+ |
args[i] = false; |
3878 |
+ |
else if (parameterTypes[i] == int.class) |
3879 |
+ |
args[i] = 0; |
3880 |
+ |
else if (parameterTypes[i] == long.class) |
3881 |
+ |
args[i] = 0L; |
3882 |
+ |
} |
3883 |
+ |
try { |
3884 |
+ |
method.invoke(minimalStage, args); |
3885 |
+ |
bugs.add(method); |
3886 |
+ |
} |
3887 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3888 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3889 |
+ |
bugs.add(method); |
3890 |
+ |
// expected.getCause().printStackTrace(); |
3891 |
+ |
} |
3892 |
+ |
} |
3893 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3894 |
+ |
} |
3895 |
+ |
if (!bugs.isEmpty()) |
3896 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3897 |
+ |
} |
3898 |
+ |
|
3899 |
+ |
static class Monad { |
3900 |
+ |
static class ZeroException extends RuntimeException { |
3901 |
+ |
public ZeroException() { super("monadic zero"); } |
3902 |
+ |
} |
3903 |
+ |
// "return", "unit" |
3904 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
3905 |
+ |
return completedFuture(value); |
3906 |
+ |
} |
3907 |
+ |
// monadic zero ? |
3908 |
+ |
static <T> CompletableFuture<T> zero() { |
3909 |
+ |
return failedFuture(new ZeroException()); |
3910 |
+ |
} |
3911 |
+ |
// >=> |
3912 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3913 |
+ |
(Function<T, CompletableFuture<U>> f, |
3914 |
+ |
Function<U, CompletableFuture<V>> g) { |
3915 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
3916 |
+ |
} |
3917 |
+ |
|
3918 |
+ |
static void assertZero(CompletableFuture<?> f) { |
3919 |
+ |
try { |
3920 |
+ |
f.getNow(null); |
3921 |
+ |
throw new AssertionFailedError("should throw"); |
3922 |
+ |
} catch (CompletionException success) { |
3923 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
3924 |
+ |
} |
3925 |
+ |
} |
3926 |
+ |
|
3927 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
3928 |
+ |
CompletableFuture<T> g) { |
3929 |
+ |
T fval = null, gval = null; |
3930 |
+ |
Throwable fex = null, gex = null; |
3931 |
+ |
|
3932 |
+ |
try { fval = f.get(); } |
3933 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
3934 |
+ |
catch (Throwable ex) { fex = ex; } |
3935 |
+ |
|
3936 |
+ |
try { gval = g.get(); } |
3937 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
3938 |
+ |
catch (Throwable ex) { gex = ex; } |
3939 |
+ |
|
3940 |
+ |
if (fex != null || gex != null) |
3941 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
3942 |
+ |
else |
3943 |
+ |
assertEquals(fval, gval); |
3944 |
+ |
} |
3945 |
+ |
|
3946 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
3947 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3948 |
+ |
} |
3949 |
+ |
|
3950 |
+ |
/** Implements "monadic plus". */ |
3951 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3952 |
+ |
CompletableFuture<? extends T> g) { |
3953 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
3954 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3955 |
+ |
try { |
3956 |
+ |
if (ex == null) { |
3957 |
+ |
if (plus.complete(result)) |
3958 |
+ |
if (plus.firstFailure.get() != null) |
3959 |
+ |
plus.firstFailure.set(null); |
3960 |
+ |
} |
3961 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3962 |
+ |
if (plus.isDone()) |
3963 |
+ |
plus.firstFailure.set(null); |
3964 |
+ |
} |
3965 |
+ |
else { |
3966 |
+ |
// first failure has precedence |
3967 |
+ |
Throwable first = plus.firstFailure.getAndSet(null); |
3968 |
+ |
|
3969 |
+ |
// may fail with "Self-suppression not permitted" |
3970 |
+ |
try { first.addSuppressed(ex); } |
3971 |
+ |
catch (Exception ignored) {} |
3972 |
+ |
|
3973 |
+ |
plus.completeExceptionally(first); |
3974 |
+ |
} |
3975 |
+ |
} catch (Throwable unexpected) { |
3976 |
+ |
plus.completeExceptionally(unexpected); |
3977 |
+ |
} |
3978 |
+ |
}; |
3979 |
+ |
f.whenComplete(action); |
3980 |
+ |
g.whenComplete(action); |
3981 |
+ |
return plus; |
3982 |
+ |
} |
3983 |
+ |
} |
3984 |
+ |
|
3985 |
+ |
/** |
3986 |
+ |
* CompletableFuture is an additive monad - sort of. |
3987 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
3988 |
+ |
*/ |
3989 |
+ |
public void testAdditiveMonad() throws Throwable { |
3990 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3991 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3992 |
+ |
|
3993 |
+ |
// Some mutually non-commutative functions |
3994 |
+ |
Function<Long, CompletableFuture<Long>> triple |
3995 |
+ |
= (x) -> Monad.unit(3 * x); |
3996 |
+ |
Function<Long, CompletableFuture<Long>> inc |
3997 |
+ |
= (x) -> Monad.unit(x + 1); |
3998 |
+ |
|
3999 |
+ |
// unit is a right identity: m >>= unit === m |
4000 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
4001 |
+ |
inc.apply(5L)); |
4002 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
4003 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
4004 |
+ |
inc.apply(5L)); |
4005 |
+ |
|
4006 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
4007 |
+ |
Monad.assertFutureEquals( |
4008 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
4009 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
4010 |
+ |
|
4011 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
4012 |
+ |
|
4013 |
+ |
// zero is a monadic zero |
4014 |
+ |
Monad.assertZero(zero); |
4015 |
+ |
|
4016 |
+ |
// left zero: zero >>= f === zero |
4017 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
4018 |
+ |
// right zero: f >>= (\x -> zero) === zero |
4019 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
4020 |
+ |
|
4021 |
+ |
// f plus zero === f |
4022 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4023 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
4024 |
+ |
// zero plus f === f |
4025 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4026 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
4027 |
+ |
// zero plus zero === zero |
4028 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
4029 |
+ |
{ |
4030 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
4031 |
+ |
Monad.unit(8L)); |
4032 |
+ |
// non-determinism |
4033 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
4034 |
+ |
} |
4035 |
+ |
|
4036 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
4037 |
+ |
// f plus godot === f (doesn't wait for godot) |
4038 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4039 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
4040 |
+ |
// godot plus f === f (doesn't wait for godot) |
4041 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4042 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
4043 |
+ |
} |
4044 |
+ |
|
4045 |
+ |
/** |
4046 |
+ |
* A single CompletableFuture with many dependents. |
4047 |
+ |
* A demo of scalability - runtime is O(n). |
4048 |
+ |
*/ |
4049 |
+ |
public void testManyDependents() throws Throwable { |
4050 |
+ |
final int n = 1_000; |
4051 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4052 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4053 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
4054 |
+ |
for (int i = 0; i < n; i++) { |
4055 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
4056 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
4057 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
4058 |
+ |
|
4059 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4060 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4061 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4062 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4063 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4064 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4065 |
+ |
|
4066 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4067 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4068 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4069 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4070 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4071 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4072 |
+ |
} |
4073 |
+ |
head.complete(null); |
4074 |
+ |
assertEquals(5 * 3 * n, count.get()); |
4075 |
+ |
} |
4076 |
+ |
|
4077 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
4078 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4079 |
+ |
// stage.whenComplete((v, ex) -> { |
4080 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4081 |
+ |
// }); |
4082 |
+ |
// return f.join(); |
4083 |
+ |
// } |
4084 |
+ |
|
4085 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
4086 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4087 |
+ |
// stage.whenComplete((v, ex) -> { |
4088 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4089 |
+ |
// }); |
4090 |
+ |
// return f.isDone(); |
4091 |
+ |
// } |
4092 |
+ |
|
4093 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
4094 |
+ |
// return stage.toCompletableFuture().copy().join(); |
4095 |
+ |
// } |
4096 |
+ |
|
4097 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
4098 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
4099 |
+ |
// } |
4100 |
+ |
|
4101 |
|
} |