7 |
|
|
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; |
25 |
|
import java.util.concurrent.CancellationException; |
26 |
|
import java.util.concurrent.CompletableFuture; |
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; |
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 |
|
|
90 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
91 |
|
} |
92 |
|
|
93 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
94 |
< |
long startTime = System.nanoTime(); |
95 |
< |
long timeoutMillis = LONG_DELAY_MS; |
96 |
< |
try { |
97 |
< |
f.get(timeoutMillis, MILLISECONDS); |
98 |
< |
shouldThrow(); |
99 |
< |
} catch (ExecutionException success) { |
100 |
< |
assertTrue(success.getCause() instanceof CFException); |
101 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
102 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
103 |
< |
|
104 |
< |
try { |
105 |
< |
f.join(); |
106 |
< |
shouldThrow(); |
107 |
< |
} catch (CompletionException success) { |
108 |
< |
assertTrue(success.getCause() instanceof CFException); |
109 |
< |
} |
97 |
< |
try { |
98 |
< |
f.getNow(null); |
99 |
< |
shouldThrow(); |
100 |
< |
} catch (CompletionException success) { |
101 |
< |
assertTrue(success.getCause() instanceof CFException); |
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 |
< |
try { |
104 |
< |
f.get(); |
105 |
< |
shouldThrow(); |
106 |
< |
} catch (ExecutionException success) { |
107 |
< |
assertTrue(success.getCause() instanceof CFException); |
108 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
109 |
< |
assertTrue(f.isDone()); |
110 |
< |
assertFalse(f.isCancelled()); |
111 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
112 |
< |
} |
111 |
> |
checker.accept(cause); |
112 |
|
|
114 |
– |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
115 |
– |
Throwable ex) { |
113 |
|
long startTime = System.nanoTime(); |
117 |
– |
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 |
< |
assertSame(ex, success.getCause()); |
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 |
< |
assertSame(ex, success.getCause()); |
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 |
< |
assertSame(ex, success.getCause()); |
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 |
< |
assertSame(ex, success.getCause()); |
140 |
> |
assertSame(cause, success.getCause()); |
141 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
142 |
|
|
145 |
– |
assertTrue(f.isDone()); |
143 |
|
assertFalse(f.isCancelled()); |
144 |
+ |
assertTrue(f.isDone()); |
145 |
+ |
assertTrue(f.isCompletedExceptionally()); |
146 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
147 |
|
} |
148 |
|
|
149 |
< |
<U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) { |
150 |
< |
long startTime = System.nanoTime(); |
151 |
< |
long timeoutMillis = LONG_DELAY_MS; |
152 |
< |
try { |
154 |
< |
f.get(timeoutMillis, MILLISECONDS); |
155 |
< |
shouldThrow(); |
156 |
< |
} catch (ExecutionException ex) { |
157 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
158 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
160 |
< |
|
161 |
< |
try { |
162 |
< |
f.join(); |
163 |
< |
shouldThrow(); |
164 |
< |
} catch (Throwable ex) { |
165 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
166 |
< |
} |
167 |
< |
|
168 |
< |
try { |
169 |
< |
f.getNow(null); |
170 |
< |
shouldThrow(); |
171 |
< |
} catch (Throwable ex) { |
172 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
173 |
< |
} |
149 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
150 |
> |
checkCompletedExceptionally(f, true, |
151 |
> |
(t) -> assertTrue(t instanceof CFException)); |
152 |
> |
} |
153 |
|
|
154 |
< |
try { |
155 |
< |
f.get(); |
156 |
< |
shouldThrow(); |
157 |
< |
} catch (ExecutionException ex) { |
179 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
180 |
< |
} 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); |
190 |
< |
try { |
191 |
< |
CompletableFuture<Throwable> spy = f.handle |
192 |
< |
((U u, Throwable t) -> t); |
193 |
< |
assertTrue(spy.join() instanceof CompletionException); |
194 |
< |
assertSame(ex, spy.join().getCause()); |
195 |
< |
} 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); |
200 |
< |
try { |
201 |
< |
CompletableFuture<Throwable> spy = f.handle |
202 |
< |
((U u, Throwable t) -> t); |
203 |
< |
assertSame(ex, spy.join()); |
204 |
< |
} 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(); |
209 |
– |
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); } |
230 |
– |
assertTrue(f.isDone()); |
231 |
– |
assertTrue(f.isCompletedExceptionally()); |
232 |
– |
assertTrue(f.isCancelled()); |
233 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
234 |
– |
} |
195 |
|
|
196 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
237 |
< |
long startTime = System.nanoTime(); |
238 |
< |
long timeoutMillis = LONG_DELAY_MS; |
239 |
< |
try { |
240 |
< |
f.get(timeoutMillis, MILLISECONDS); |
241 |
< |
shouldThrow(); |
242 |
< |
} catch (ExecutionException success) { |
243 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
244 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
245 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
196 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
197 |
|
|
247 |
– |
try { |
248 |
– |
f.join(); |
249 |
– |
shouldThrow(); |
250 |
– |
} catch (CompletionException success) { |
251 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
252 |
– |
} |
253 |
– |
try { |
254 |
– |
f.getNow(null); |
255 |
– |
shouldThrow(); |
256 |
– |
} catch (CompletionException success) { |
257 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
258 |
– |
} |
259 |
– |
try { |
260 |
– |
f.get(); |
261 |
– |
shouldThrow(); |
262 |
– |
} catch (ExecutionException success) { |
263 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
264 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
198 |
|
assertTrue(f.isDone()); |
266 |
– |
assertFalse(f.isCancelled()); |
199 |
|
assertTrue(f.isCompletedExceptionally()); |
200 |
+ |
assertTrue(f.isCancelled()); |
201 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
202 |
|
} |
203 |
|
|
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 |
|
|
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 |
> |
|
553 |
> |
static class CountingRejectingExecutor implements Executor { |
554 |
> |
final RejectedExecutionException ex = new RejectedExecutionException(); |
555 |
> |
final AtomicInteger count = new AtomicInteger(0); |
556 |
> |
public void execute(Runnable r) { |
557 |
> |
count.getAndIncrement(); |
558 |
> |
throw ex; |
559 |
|
} |
560 |
|
} |
561 |
|
|
857 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
858 |
|
final CompletableFuture<Integer> g = f.exceptionally |
859 |
|
((Throwable t) -> { |
911 |
– |
// Should not be called |
860 |
|
a.getAndIncrement(); |
861 |
< |
throw new AssertionError(); |
861 |
> |
threadFail("should not be called"); |
862 |
> |
return null; // unreached |
863 |
|
}); |
864 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
865 |
|
|
893 |
|
assertEquals(1, a.get()); |
894 |
|
}} |
895 |
|
|
896 |
+ |
/** |
897 |
+ |
* If an "exceptionally action" throws an exception, it completes |
898 |
+ |
* exceptionally with that exception |
899 |
+ |
*/ |
900 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
901 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
902 |
|
{ |
915 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
916 |
|
|
917 |
|
checkCompletedWithWrappedException(g, ex2); |
918 |
+ |
checkCompletedExceptionally(f, ex1); |
919 |
|
assertEquals(1, a.get()); |
920 |
|
}} |
921 |
|
|
923 |
|
* whenComplete action executes on normal completion, propagating |
924 |
|
* source result. |
925 |
|
*/ |
926 |
< |
public void testWhenComplete_normalCompletion1() { |
926 |
> |
public void testWhenComplete_normalCompletion() { |
927 |
|
for (ExecutionMode m : ExecutionMode.values()) |
928 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
929 |
|
for (Integer v1 : new Integer[] { 1, null }) |
933 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
934 |
|
final CompletableFuture<Integer> g = m.whenComplete |
935 |
|
(f, |
936 |
< |
(Integer x, Throwable t) -> { |
936 |
> |
(Integer result, Throwable t) -> { |
937 |
|
m.checkExecutionMode(); |
938 |
< |
threadAssertSame(x, v1); |
938 |
> |
threadAssertSame(result, v1); |
939 |
|
threadAssertNull(t); |
940 |
|
a.getAndIncrement(); |
941 |
|
}); |
960 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
961 |
|
final CompletableFuture<Integer> g = m.whenComplete |
962 |
|
(f, |
963 |
< |
(Integer x, Throwable t) -> { |
963 |
> |
(Integer result, Throwable t) -> { |
964 |
|
m.checkExecutionMode(); |
965 |
< |
threadAssertNull(x); |
965 |
> |
threadAssertNull(result); |
966 |
|
threadAssertSame(t, ex); |
967 |
|
a.getAndIncrement(); |
968 |
|
}); |
987 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
988 |
|
final CompletableFuture<Integer> g = m.whenComplete |
989 |
|
(f, |
990 |
< |
(Integer x, Throwable t) -> { |
990 |
> |
(Integer result, Throwable t) -> { |
991 |
|
m.checkExecutionMode(); |
992 |
< |
threadAssertNull(x); |
992 |
> |
threadAssertNull(result); |
993 |
|
threadAssertTrue(t instanceof CancellationException); |
994 |
|
a.getAndIncrement(); |
995 |
|
}); |
1004 |
|
* If a whenComplete action throws an exception when triggered by |
1005 |
|
* a normal completion, it completes exceptionally |
1006 |
|
*/ |
1007 |
< |
public void testWhenComplete_actionFailed() { |
1007 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
1008 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1009 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1010 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1015 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1016 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1017 |
|
(f, |
1018 |
< |
(Integer x, Throwable t) -> { |
1018 |
> |
(Integer result, Throwable t) -> { |
1019 |
|
m.checkExecutionMode(); |
1020 |
< |
threadAssertSame(x, v1); |
1020 |
> |
threadAssertSame(result, v1); |
1021 |
|
threadAssertNull(t); |
1022 |
|
a.getAndIncrement(); |
1023 |
|
throw ex; |
1032 |
|
/** |
1033 |
|
* If a whenComplete action throws an exception when triggered by |
1034 |
|
* a source completion that also throws an exception, the source |
1035 |
< |
* exception takes precedence. |
1035 |
> |
* exception takes precedence (unlike handle) |
1036 |
|
*/ |
1037 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1037 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1038 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1039 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1040 |
|
{ |
1046 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1047 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1048 |
|
(f, |
1049 |
< |
(Integer x, Throwable t) -> { |
1049 |
> |
(Integer result, Throwable t) -> { |
1050 |
|
m.checkExecutionMode(); |
1051 |
|
threadAssertSame(t, ex1); |
1052 |
< |
threadAssertNull(x); |
1052 |
> |
threadAssertNull(result); |
1053 |
|
a.getAndIncrement(); |
1054 |
|
throw ex2; |
1055 |
|
}); |
1057 |
|
|
1058 |
|
checkCompletedWithWrappedException(g, ex1); |
1059 |
|
checkCompletedExceptionally(f, ex1); |
1060 |
+ |
if (testImplementationDetails) { |
1061 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1062 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1063 |
+ |
} |
1064 |
|
assertEquals(1, a.get()); |
1065 |
|
}} |
1066 |
|
|
1078 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1079 |
|
final CompletableFuture<Integer> g = m.handle |
1080 |
|
(f, |
1081 |
< |
(Integer x, Throwable t) -> { |
1081 |
> |
(Integer result, Throwable t) -> { |
1082 |
|
m.checkExecutionMode(); |
1083 |
< |
threadAssertSame(x, v1); |
1083 |
> |
threadAssertSame(result, v1); |
1084 |
|
threadAssertNull(t); |
1085 |
|
a.getAndIncrement(); |
1086 |
|
return inc(v1); |
1107 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1108 |
|
final CompletableFuture<Integer> g = m.handle |
1109 |
|
(f, |
1110 |
< |
(Integer x, Throwable t) -> { |
1110 |
> |
(Integer result, Throwable t) -> { |
1111 |
|
m.checkExecutionMode(); |
1112 |
< |
threadAssertNull(x); |
1112 |
> |
threadAssertNull(result); |
1113 |
|
threadAssertSame(t, ex); |
1114 |
|
a.getAndIncrement(); |
1115 |
|
return v1; |
1136 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1137 |
|
final CompletableFuture<Integer> g = m.handle |
1138 |
|
(f, |
1139 |
< |
(Integer x, Throwable t) -> { |
1139 |
> |
(Integer result, Throwable t) -> { |
1140 |
|
m.checkExecutionMode(); |
1141 |
< |
threadAssertNull(x); |
1141 |
> |
threadAssertNull(result); |
1142 |
|
threadAssertTrue(t instanceof CancellationException); |
1143 |
|
a.getAndIncrement(); |
1144 |
|
return v1; |
1151 |
|
}} |
1152 |
|
|
1153 |
|
/** |
1154 |
< |
* handle result completes exceptionally if action does |
1154 |
> |
* If a "handle action" throws an exception when triggered by |
1155 |
> |
* a normal completion, it completes exceptionally |
1156 |
|
*/ |
1157 |
< |
public void testHandle_sourceFailedActionFailed() { |
1157 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1158 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1159 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1160 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1161 |
|
{ |
1162 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1163 |
|
final AtomicInteger a = new AtomicInteger(0); |
1164 |
< |
final CFException ex1 = new CFException(); |
1165 |
< |
final CFException ex2 = new CFException(); |
1206 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1164 |
> |
final CFException ex = new CFException(); |
1165 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1166 |
|
final CompletableFuture<Integer> g = m.handle |
1167 |
|
(f, |
1168 |
< |
(Integer x, Throwable t) -> { |
1168 |
> |
(Integer result, Throwable t) -> { |
1169 |
|
m.checkExecutionMode(); |
1170 |
< |
threadAssertNull(x); |
1171 |
< |
threadAssertSame(ex1, t); |
1170 |
> |
threadAssertSame(result, v1); |
1171 |
> |
threadAssertNull(t); |
1172 |
|
a.getAndIncrement(); |
1173 |
< |
throw ex2; |
1173 |
> |
throw ex; |
1174 |
|
}); |
1175 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1175 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1176 |
|
|
1177 |
< |
checkCompletedWithWrappedException(g, ex2); |
1178 |
< |
checkCompletedExceptionally(f, ex1); |
1177 |
> |
checkCompletedWithWrappedException(g, ex); |
1178 |
> |
checkCompletedNormally(f, v1); |
1179 |
|
assertEquals(1, a.get()); |
1180 |
|
}} |
1181 |
|
|
1182 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1183 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1182 |
> |
/** |
1183 |
> |
* If a "handle action" throws an exception when triggered by |
1184 |
> |
* a source completion that also throws an exception, the action |
1185 |
> |
* exception takes precedence (unlike whenComplete) |
1186 |
> |
*/ |
1187 |
> |
public void testHandle_sourceFailedActionFailed() { |
1188 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1189 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1189 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1190 |
|
{ |
1228 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1191 |
|
final AtomicInteger a = new AtomicInteger(0); |
1192 |
< |
final CFException ex = new CFException(); |
1193 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1192 |
> |
final CFException ex1 = new CFException(); |
1193 |
> |
final CFException ex2 = new CFException(); |
1194 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1195 |
> |
|
1196 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1197 |
|
final CompletableFuture<Integer> g = m.handle |
1198 |
|
(f, |
1199 |
< |
(Integer x, Throwable t) -> { |
1199 |
> |
(Integer result, Throwable t) -> { |
1200 |
|
m.checkExecutionMode(); |
1201 |
< |
threadAssertSame(x, v1); |
1202 |
< |
threadAssertNull(t); |
1201 |
> |
threadAssertNull(result); |
1202 |
> |
threadAssertSame(ex1, t); |
1203 |
|
a.getAndIncrement(); |
1204 |
< |
throw ex; |
1204 |
> |
throw ex2; |
1205 |
|
}); |
1206 |
< |
if (createIncomplete) assertTrue(f.complete(v1)); |
1206 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1207 |
|
|
1208 |
< |
checkCompletedWithWrappedException(g, ex); |
1209 |
< |
checkCompletedNormally(f, v1); |
1208 |
> |
checkCompletedWithWrappedException(g, ex2); |
1209 |
> |
checkCompletedExceptionally(f, ex1); |
1210 |
|
assertEquals(1, a.get()); |
1211 |
|
}} |
1212 |
|
|
1239 |
|
{ |
1240 |
|
final FailingRunnable r = new FailingRunnable(m); |
1241 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1242 |
< |
checkCompletedWithWrappedCFException(f); |
1242 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1243 |
|
r.assertInvoked(); |
1244 |
|
}} |
1245 |
|
|
1246 |
+ |
public void testRunAsync_rejectingExecutor() { |
1247 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1248 |
+ |
try { |
1249 |
+ |
CompletableFuture.runAsync(() -> {}, e); |
1250 |
+ |
shouldThrow(); |
1251 |
+ |
} catch (Throwable t) { |
1252 |
+ |
assertSame(e.ex, t); |
1253 |
+ |
} |
1254 |
+ |
|
1255 |
+ |
assertEquals(1, e.count.get()); |
1256 |
+ |
} |
1257 |
+ |
|
1258 |
|
/** |
1259 |
|
* supplyAsync completes with result of supplier |
1260 |
|
*/ |
1285 |
|
{ |
1286 |
|
FailingSupplier r = new FailingSupplier(m); |
1287 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1288 |
< |
checkCompletedWithWrappedCFException(f); |
1288 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1289 |
|
r.assertInvoked(); |
1290 |
|
}} |
1291 |
|
|
1292 |
+ |
public void testSupplyAsync_rejectingExecutor() { |
1293 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1294 |
+ |
try { |
1295 |
+ |
CompletableFuture.supplyAsync(() -> null, e); |
1296 |
+ |
shouldThrow(); |
1297 |
+ |
} catch (Throwable t) { |
1298 |
+ |
assertSame(e.ex, t); |
1299 |
+ |
} |
1300 |
+ |
|
1301 |
+ |
assertEquals(1, e.count.get()); |
1302 |
+ |
} |
1303 |
+ |
|
1304 |
|
// seq completion methods |
1305 |
|
|
1306 |
|
/** |
1419 |
|
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1420 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1421 |
|
|
1422 |
< |
checkCompletedWithWrappedCFException(h0); |
1423 |
< |
checkCompletedWithWrappedCFException(h1); |
1424 |
< |
checkCompletedWithWrappedCFException(h2); |
1425 |
< |
checkCompletedWithWrappedCFException(h3); |
1426 |
< |
checkCompletedWithWrappedCFException(h4); |
1427 |
< |
checkCompletedWithWrappedCFException(h5); |
1422 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1423 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1424 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1425 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1426 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1427 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1428 |
|
checkCompletedNormally(f, v1); |
1429 |
|
}} |
1430 |
|
|
1523 |
|
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1524 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1525 |
|
|
1526 |
< |
checkCompletedWithWrappedCFException(h0); |
1527 |
< |
checkCompletedWithWrappedCFException(h1); |
1528 |
< |
checkCompletedWithWrappedCFException(h2); |
1529 |
< |
checkCompletedWithWrappedCFException(h3); |
1526 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1527 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1528 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1529 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1530 |
|
checkCompletedNormally(f, v1); |
1531 |
|
}} |
1532 |
|
|
1625 |
|
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1626 |
|
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1627 |
|
|
1628 |
< |
checkCompletedWithWrappedCFException(h0); |
1629 |
< |
checkCompletedWithWrappedCFException(h1); |
1630 |
< |
checkCompletedWithWrappedCFException(h2); |
1631 |
< |
checkCompletedWithWrappedCFException(h3); |
1628 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1629 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1630 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1631 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1632 |
|
checkCompletedNormally(f, v1); |
1633 |
|
}} |
1634 |
|
|
1790 |
|
assertTrue(snd.complete(w2)); |
1791 |
|
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1792 |
|
|
1793 |
< |
checkCompletedWithWrappedCFException(h1); |
1794 |
< |
checkCompletedWithWrappedCFException(h2); |
1795 |
< |
checkCompletedWithWrappedCFException(h3); |
1793 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1794 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1795 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1796 |
|
r1.assertInvoked(); |
1797 |
|
r2.assertInvoked(); |
1798 |
|
r3.assertInvoked(); |
1954 |
|
assertTrue(snd.complete(w2)); |
1955 |
|
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1956 |
|
|
1957 |
< |
checkCompletedWithWrappedCFException(h1); |
1958 |
< |
checkCompletedWithWrappedCFException(h2); |
1959 |
< |
checkCompletedWithWrappedCFException(h3); |
1957 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1958 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1959 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1960 |
|
r1.assertInvoked(); |
1961 |
|
r2.assertInvoked(); |
1962 |
|
r3.assertInvoked(); |
2118 |
|
assertTrue(snd.complete(w2)); |
2119 |
|
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2120 |
|
|
2121 |
< |
checkCompletedWithWrappedCFException(h1); |
2122 |
< |
checkCompletedWithWrappedCFException(h2); |
2123 |
< |
checkCompletedWithWrappedCFException(h3); |
2121 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2122 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2123 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2124 |
|
r1.assertInvoked(); |
2125 |
|
r2.assertInvoked(); |
2126 |
|
r3.assertInvoked(); |
2410 |
|
f.complete(v1); |
2411 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2412 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2413 |
< |
checkCompletedWithWrappedCFException(h0); |
2414 |
< |
checkCompletedWithWrappedCFException(h1); |
2415 |
< |
checkCompletedWithWrappedCFException(h2); |
2416 |
< |
checkCompletedWithWrappedCFException(h3); |
2413 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2414 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2415 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2416 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2417 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2418 |
|
|
2419 |
|
g.complete(v2); |
2422 |
|
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2423 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2424 |
|
|
2425 |
< |
checkCompletedWithWrappedCFException(h4); |
2425 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2426 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2427 |
|
Objects.equals(v2, rs[4].value)); |
2428 |
< |
checkCompletedWithWrappedCFException(h5); |
2428 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2429 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2430 |
|
Objects.equals(v2, rs[5].value)); |
2431 |
|
|
2669 |
|
f.complete(v1); |
2670 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2671 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2672 |
< |
checkCompletedWithWrappedCFException(h0); |
2673 |
< |
checkCompletedWithWrappedCFException(h1); |
2674 |
< |
checkCompletedWithWrappedCFException(h2); |
2675 |
< |
checkCompletedWithWrappedCFException(h3); |
2672 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2673 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2674 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2675 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2676 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2677 |
|
|
2678 |
|
g.complete(v2); |
2681 |
|
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2682 |
|
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2683 |
|
|
2684 |
< |
checkCompletedWithWrappedCFException(h4); |
2684 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2685 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2686 |
|
Objects.equals(v2, rs[4].value)); |
2687 |
< |
checkCompletedWithWrappedCFException(h5); |
2687 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2688 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2689 |
|
Objects.equals(v2, rs[5].value)); |
2690 |
|
|
2700 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2701 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2702 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2703 |
+ |
for (boolean pushNop : new boolean[] { true, false }) |
2704 |
|
{ |
2705 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2706 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2713 |
|
checkIncomplete(h1); |
2714 |
|
rs[0].assertNotInvoked(); |
2715 |
|
rs[1].assertNotInvoked(); |
2716 |
+ |
if (pushNop) { // ad hoc test of intra-completion interference |
2717 |
+ |
m.thenRun(f, () -> {}); |
2718 |
+ |
m.thenRun(g, () -> {}); |
2719 |
+ |
} |
2720 |
|
f.complete(v1); |
2721 |
|
checkCompletedNormally(h0, null); |
2722 |
|
checkCompletedNormally(h1, null); |
2929 |
|
assertTrue(f.complete(v1)); |
2930 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2931 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2932 |
< |
checkCompletedWithWrappedCFException(h0); |
2933 |
< |
checkCompletedWithWrappedCFException(h1); |
2934 |
< |
checkCompletedWithWrappedCFException(h2); |
2935 |
< |
checkCompletedWithWrappedCFException(h3); |
2932 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2933 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2934 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2935 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2936 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2937 |
|
assertTrue(g.complete(v2)); |
2938 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2939 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2940 |
< |
checkCompletedWithWrappedCFException(h4); |
2941 |
< |
checkCompletedWithWrappedCFException(h5); |
2940 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2941 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2942 |
|
|
2943 |
|
checkCompletedNormally(f, v1); |
2944 |
|
checkCompletedNormally(g, v2); |
2999 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
3000 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
3001 |
|
|
3002 |
< |
checkCompletedWithWrappedCFException(g); |
3002 |
> |
checkCompletedWithWrappedException(g, r.ex); |
3003 |
|
checkCompletedNormally(f, v1); |
3004 |
|
}} |
3005 |
|
|
3108 |
|
} |
3109 |
|
} |
3110 |
|
|
3111 |
< |
public void testAllOf_backwards() throws Exception { |
3111 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3112 |
|
for (int k = 1; k < 10; k++) { |
3113 |
|
CompletableFuture<Integer>[] fs |
3114 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3136 |
|
for (int i = 0; i < k; i++) { |
3137 |
|
checkIncomplete(f); |
3138 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3139 |
< |
if (i != k/2) { |
3139 |
> |
if (i != k / 2) { |
3140 |
|
fs[i].complete(i); |
3141 |
|
checkCompletedNormally(fs[i], i); |
3142 |
|
} else { |
3341 |
|
() -> f.obtrudeException(null), |
3342 |
|
|
3343 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3344 |
< |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3344 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3345 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3346 |
|
|
3347 |
|
() -> f.orTimeout(1L, null), |
3348 |
|
() -> f.completeOnTimeout(42, 1L, null), |
3349 |
+ |
|
3350 |
+ |
() -> CompletableFuture.failedFuture(null), |
3351 |
+ |
() -> CompletableFuture.failedStage(null), |
3352 |
|
}; |
3353 |
|
|
3354 |
|
assertThrows(NullPointerException.class, throwingActions); |
3356 |
|
} |
3357 |
|
|
3358 |
|
/** |
3359 |
+ |
* Test submissions to an executor that rejects all tasks. |
3360 |
+ |
*/ |
3361 |
+ |
public void testRejectingExecutor() { |
3362 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3363 |
+ |
{ |
3364 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3365 |
+ |
|
3366 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3367 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3368 |
+ |
|
3369 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3370 |
+ |
|
3371 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3372 |
+ |
srcs.add(complete); |
3373 |
+ |
srcs.add(incomplete); |
3374 |
+ |
|
3375 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3376 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3377 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3378 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3379 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3380 |
+ |
|
3381 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3382 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3383 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3384 |
+ |
|
3385 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3386 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3387 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3388 |
+ |
|
3389 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3390 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3391 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3392 |
+ |
|
3393 |
+ |
for (CompletableFuture<?> future : fs) { |
3394 |
+ |
if (src.isDone()) |
3395 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3396 |
+ |
else |
3397 |
+ |
checkIncomplete(future); |
3398 |
+ |
} |
3399 |
+ |
futures.addAll(fs); |
3400 |
+ |
} |
3401 |
+ |
|
3402 |
+ |
{ |
3403 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3404 |
+ |
|
3405 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3406 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3407 |
+ |
|
3408 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3409 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3410 |
+ |
|
3411 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3412 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3413 |
+ |
|
3414 |
+ |
for (CompletableFuture<?> future : fs) |
3415 |
+ |
checkIncomplete(future); |
3416 |
+ |
futures.addAll(fs); |
3417 |
+ |
} |
3418 |
+ |
|
3419 |
+ |
{ |
3420 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3421 |
+ |
|
3422 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3423 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3424 |
+ |
|
3425 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3426 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3427 |
+ |
|
3428 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3429 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3430 |
+ |
|
3431 |
+ |
for (CompletableFuture<?> future : fs) |
3432 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3433 |
+ |
futures.addAll(fs); |
3434 |
+ |
} |
3435 |
+ |
|
3436 |
+ |
incomplete.complete(v); |
3437 |
+ |
|
3438 |
+ |
for (CompletableFuture<?> future : futures) |
3439 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3440 |
+ |
|
3441 |
+ |
assertEquals(futures.size(), e.count.get()); |
3442 |
+ |
}} |
3443 |
+ |
|
3444 |
+ |
/** |
3445 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3446 |
+ |
* should never be invoked because the dependent future is |
3447 |
+ |
* explicitly completed. |
3448 |
+ |
*/ |
3449 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3450 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3451 |
+ |
{ |
3452 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3453 |
+ |
|
3454 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3455 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3456 |
+ |
|
3457 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3458 |
+ |
|
3459 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3460 |
+ |
srcs.add(complete); |
3461 |
+ |
srcs.add(incomplete); |
3462 |
+ |
|
3463 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3464 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3465 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3466 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3467 |
+ |
|
3468 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3469 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3470 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3471 |
+ |
|
3472 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3473 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3474 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3475 |
+ |
|
3476 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3477 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3478 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3479 |
+ |
|
3480 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3481 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3482 |
+ |
|
3483 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3484 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3485 |
+ |
|
3486 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3487 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3488 |
+ |
|
3489 |
+ |
for (CompletableFuture<?> future : fs) |
3490 |
+ |
checkIncomplete(future); |
3491 |
+ |
|
3492 |
+ |
for (CompletableFuture<?> future : fs) |
3493 |
+ |
future.complete(null); |
3494 |
+ |
|
3495 |
+ |
incomplete.complete(v); |
3496 |
+ |
|
3497 |
+ |
for (CompletableFuture<?> future : fs) |
3498 |
+ |
checkCompletedNormally(future, null); |
3499 |
+ |
|
3500 |
+ |
assertEquals(0, e.count.get()); |
3501 |
+ |
}} |
3502 |
+ |
|
3503 |
+ |
/** |
3504 |
|
* toCompletableFuture returns this CompletableFuture. |
3505 |
|
*/ |
3506 |
|
public void testToCompletableFuture() { |
3600 |
|
CFException ex = new CFException(); |
3601 |
|
f.completeExceptionally(ex); |
3602 |
|
checkCompletedExceptionally(f, ex); |
3603 |
< |
checkCompletedWithWrappedCFException(g); |
3603 |
> |
checkCompletedWithWrappedException(g, ex); |
3604 |
|
} |
3605 |
|
|
3606 |
|
/** |
3656 |
|
* completeAsync completes with value of given supplier |
3657 |
|
*/ |
3658 |
|
public void testCompleteAsync() { |
3659 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3660 |
+ |
{ |
3661 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3662 |
< |
f.completeAsync(() -> 1); |
3662 |
> |
f.completeAsync(() -> v1); |
3663 |
|
f.join(); |
3664 |
< |
checkCompletedNormally(f, 1); |
3665 |
< |
} |
3664 |
> |
checkCompletedNormally(f, v1); |
3665 |
> |
}} |
3666 |
|
|
3667 |
|
/** |
3668 |
|
* completeAsync completes exceptionally if given supplier throws |
3674 |
|
try { |
3675 |
|
f.join(); |
3676 |
|
shouldThrow(); |
3677 |
< |
} catch (Exception success) {} |
3678 |
< |
checkCompletedWithWrappedCFException(f); |
3677 |
> |
} catch (CompletionException success) {} |
3678 |
> |
checkCompletedWithWrappedException(f, ex); |
3679 |
|
} |
3680 |
|
|
3681 |
|
/** |
3682 |
|
* completeAsync with given executor completes with value of given supplier |
3683 |
|
*/ |
3684 |
|
public void testCompleteAsync3() { |
3685 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3686 |
+ |
{ |
3687 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3688 |
< |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3689 |
< |
f.join(); |
3690 |
< |
checkCompletedNormally(f, 1); |
3691 |
< |
} |
3688 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3689 |
> |
f.completeAsync(() -> v1, executor); |
3690 |
> |
assertSame(v1, f.join()); |
3691 |
> |
checkCompletedNormally(f, v1); |
3692 |
> |
assertEquals(1, executor.count.get()); |
3693 |
> |
}} |
3694 |
|
|
3695 |
|
/** |
3696 |
|
* completeAsync with given executor completes exceptionally if |
3699 |
|
public void testCompleteAsync4() { |
3700 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3701 |
|
CFException ex = new CFException(); |
3702 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3702 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3703 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3704 |
|
try { |
3705 |
|
f.join(); |
3706 |
|
shouldThrow(); |
3707 |
< |
} catch (Exception success) {} |
3708 |
< |
checkCompletedWithWrappedCFException(f); |
3707 |
> |
} catch (CompletionException success) {} |
3708 |
> |
checkCompletedWithWrappedException(f, ex); |
3709 |
> |
assertEquals(1, executor.count.get()); |
3710 |
|
} |
3711 |
|
|
3712 |
|
/** |
3713 |
|
* orTimeout completes with TimeoutException if not complete |
3714 |
|
*/ |
3715 |
< |
public void testOrTimeout() { |
3715 |
> |
public void testOrTimeout_timesOut() { |
3716 |
> |
long timeoutMillis = timeoutMillis(); |
3717 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3718 |
< |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3719 |
< |
checkCompletedExceptionallyWithTimeout(f); |
3718 |
> |
long startTime = System.nanoTime(); |
3719 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3720 |
> |
checkCompletedWithTimeoutException(f); |
3721 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3722 |
|
} |
3723 |
|
|
3724 |
|
/** |
3725 |
|
* orTimeout completes normally if completed before timeout |
3726 |
|
*/ |
3727 |
< |
public void testOrTimeout2() { |
3727 |
> |
public void testOrTimeout_completed() { |
3728 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3729 |
> |
{ |
3730 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3731 |
< |
f.complete(1); |
3732 |
< |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3733 |
< |
checkCompletedNormally(f, 1); |
3734 |
< |
} |
3731 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3732 |
> |
long startTime = System.nanoTime(); |
3733 |
> |
f.complete(v1); |
3734 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3735 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3736 |
> |
g.complete(v1); |
3737 |
> |
checkCompletedNormally(f, v1); |
3738 |
> |
checkCompletedNormally(g, v1); |
3739 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3740 |
> |
}} |
3741 |
|
|
3742 |
|
/** |
3743 |
|
* completeOnTimeout completes with given value if not complete |
3744 |
|
*/ |
3745 |
< |
public void testCompleteOnTimeout() { |
3746 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3747 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3587 |
< |
f.join(); |
3588 |
< |
checkCompletedNormally(f, -1); |
3745 |
> |
public void testCompleteOnTimeout_timesOut() { |
3746 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3747 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3748 |
|
} |
3749 |
|
|
3750 |
|
/** |
3751 |
< |
* completeOnTimeout has no effect if completed within timeout |
3751 |
> |
* completeOnTimeout completes with given value if not complete |
3752 |
|
*/ |
3753 |
< |
public void testCompleteOnTimeout2() { |
3753 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3754 |
> |
long timeoutMillis = timeoutMillis(); |
3755 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3756 |
< |
f.complete(1); |
3757 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3758 |
< |
checkCompletedNormally(f, 1); |
3756 |
> |
long startTime = System.nanoTime(); |
3757 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3758 |
> |
assertSame(v, f.join()); |
3759 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3760 |
> |
f.complete(99); // should have no effect |
3761 |
> |
checkCompletedNormally(f, v); |
3762 |
|
} |
3763 |
|
|
3764 |
|
/** |
3765 |
< |
* delayedExecutor returns an executor that delays submission |
3765 |
> |
* completeOnTimeout has no effect if completed within timeout |
3766 |
|
*/ |
3767 |
< |
public void testDelayedExecutor() throws Exception { |
3768 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3769 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3770 |
< |
MILLISECONDS); |
3767 |
> |
public void testCompleteOnTimeout_completed() { |
3768 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3769 |
> |
{ |
3770 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3771 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3772 |
|
long startTime = System.nanoTime(); |
3773 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3774 |
< |
assertNull(f.getNow(null)); |
3775 |
< |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3776 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3777 |
< |
checkCompletedNormally(f, 1); |
3778 |
< |
} |
3773 |
> |
f.complete(v1); |
3774 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3775 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3776 |
> |
g.complete(v1); |
3777 |
> |
checkCompletedNormally(f, v1); |
3778 |
> |
checkCompletedNormally(g, v1); |
3779 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3780 |
> |
}} |
3781 |
|
|
3782 |
|
/** |
3783 |
< |
* delayedExecutor for a given executor returns an executor that |
3618 |
< |
* delays submission |
3783 |
> |
* delayedExecutor returns an executor that delays submission |
3784 |
|
*/ |
3785 |
< |
public void testDelayedExecutor2() throws Exception { |
3786 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3787 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3788 |
< |
MILLISECONDS, |
3789 |
< |
new ThreadExecutor()); |
3785 |
> |
public void testDelayedExecutor() { |
3786 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3787 |
> |
() -> testDelayedExecutor(null, 1), |
3788 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3789 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3790 |
> |
} |
3791 |
> |
|
3792 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3793 |
> |
long timeoutMillis = timeoutMillis(); |
3794 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3795 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3796 |
> |
final Executor delayer, longDelayer; |
3797 |
> |
if (executor == null) { |
3798 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3799 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3800 |
> |
} else { |
3801 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3802 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3803 |
> |
} |
3804 |
|
long startTime = System.nanoTime(); |
3805 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3806 |
< |
assertNull(f.getNow(null)); |
3807 |
< |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3808 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3809 |
< |
checkCompletedNormally(f, 1); |
3805 |
> |
CompletableFuture<Integer> f = |
3806 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3807 |
> |
CompletableFuture<Integer> g = |
3808 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3809 |
> |
|
3810 |
> |
assertNull(g.getNow(null)); |
3811 |
> |
|
3812 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3813 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3814 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3815 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3816 |
> |
|
3817 |
> |
checkCompletedNormally(f, v); |
3818 |
> |
|
3819 |
> |
checkIncomplete(g); |
3820 |
> |
assertTrue(g.cancel(true)); |
3821 |
|
} |
3822 |
|
|
3823 |
|
//--- tests of implementation details; not part of official tck --- |
3824 |
|
|
3825 |
|
Object resultOf(CompletableFuture<?> f) { |
3826 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3827 |
+ |
if (sm != null) { |
3828 |
+ |
try { |
3829 |
+ |
System.setSecurityManager(null); |
3830 |
+ |
} catch (SecurityException giveUp) { |
3831 |
+ |
return "Reflection not available"; |
3832 |
+ |
} |
3833 |
+ |
} |
3834 |
+ |
|
3835 |
|
try { |
3836 |
|
java.lang.reflect.Field resultField |
3837 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3838 |
|
resultField.setAccessible(true); |
3839 |
|
return resultField.get(f); |
3840 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3840 |
> |
} catch (Throwable t) { |
3841 |
> |
throw new AssertionError(t); |
3842 |
> |
} finally { |
3843 |
> |
if (sm != null) System.setSecurityManager(sm); |
3844 |
> |
} |
3845 |
|
} |
3846 |
|
|
3847 |
|
public void testExceptionPropagationReusesResultObject() { |
3852 |
|
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3853 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3854 |
|
|
3855 |
+ |
final Runnable noopRunnable = new Noop(m); |
3856 |
+ |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3857 |
+ |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3858 |
+ |
|
3859 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3860 |
|
= new ArrayList<>(); |
3861 |
|
|
3862 |
< |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3863 |
< |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3864 |
< |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3865 |
< |
|
3866 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3867 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3868 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3862 |
> |
funs.add((y) -> m.thenRun(y, noopRunnable)); |
3863 |
> |
funs.add((y) -> m.thenAccept(y, noopConsumer)); |
3864 |
> |
funs.add((y) -> m.thenApply(y, incFunction)); |
3865 |
> |
|
3866 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable)); |
3867 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer)); |
3868 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, incFunction)); |
3869 |
|
|
3870 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3870 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable)); |
3871 |
> |
funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable)); |
3872 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3873 |
+ |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3874 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3875 |
+ |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3876 |
|
|
3877 |
< |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3877 |
> |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3878 |
|
|
3879 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3880 |
|
|
3881 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3882 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3881 |
> |
funs.add((y) -> CompletableFuture.allOf(y)); |
3882 |
> |
funs.add((y) -> CompletableFuture.allOf(y, v42)); |
3883 |
> |
funs.add((y) -> CompletableFuture.allOf(v42, y)); |
3884 |
> |
funs.add((y) -> CompletableFuture.anyOf(y)); |
3885 |
> |
funs.add((y) -> CompletableFuture.anyOf(y, incomplete)); |
3886 |
> |
funs.add((y) -> CompletableFuture.anyOf(incomplete, y)); |
3887 |
|
|
3888 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3889 |
|
fun : funs) { |
3890 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3891 |
|
f.completeExceptionally(ex); |
3892 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3892 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3893 |
|
checkCompletedWithWrappedException(src, ex); |
3894 |
|
CompletableFuture<?> dep = fun.apply(src); |
3895 |
|
checkCompletedWithWrappedException(dep, ex); |
3899 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3900 |
|
fun : funs) { |
3901 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3902 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3902 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3903 |
|
CompletableFuture<?> dep = fun.apply(src); |
3904 |
|
f.completeExceptionally(ex); |
3905 |
|
checkCompletedWithWrappedException(src, ex); |
3913 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3914 |
|
f.cancel(mayInterruptIfRunning); |
3915 |
|
checkCancelled(f); |
3916 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3916 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3917 |
|
checkCompletedWithWrappedCancellationException(src); |
3918 |
|
CompletableFuture<?> dep = fun.apply(src); |
3919 |
|
checkCompletedWithWrappedCancellationException(dep); |
3924 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3925 |
|
fun : funs) { |
3926 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3927 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3927 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3928 |
|
CompletableFuture<?> dep = fun.apply(src); |
3929 |
|
f.cancel(mayInterruptIfRunning); |
3930 |
|
checkCancelled(f); |
3934 |
|
} |
3935 |
|
}} |
3936 |
|
|
3937 |
+ |
/** |
3938 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3939 |
+ |
*/ |
3940 |
+ |
public void testMinimalCompletionStage_minimality() { |
3941 |
+ |
if (!testImplementationDetails) return; |
3942 |
+ |
Function<Method, String> toSignature = |
3943 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3944 |
+ |
Predicate<Method> isNotStatic = |
3945 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3946 |
+ |
List<Method> minimalMethods = |
3947 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3948 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3949 |
+ |
.filter(isNotStatic) |
3950 |
+ |
.collect(Collectors.toList()); |
3951 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3952 |
+ |
String[] signatureWhitelist = { |
3953 |
+ |
"newIncompleteFuture[]", |
3954 |
+ |
"defaultExecutor[]", |
3955 |
+ |
"minimalCompletionStage[]", |
3956 |
+ |
"copy[]", |
3957 |
+ |
}; |
3958 |
+ |
Set<String> permittedMethodSignatures = |
3959 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3960 |
+ |
Stream.of(signatureWhitelist)) |
3961 |
+ |
.collect(Collectors.toSet()); |
3962 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3963 |
+ |
.filter(isNotStatic) |
3964 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3965 |
+ |
.collect(Collectors.toList()); |
3966 |
+ |
|
3967 |
+ |
CompletionStage<Integer> minimalStage = |
3968 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3969 |
+ |
|
3970 |
+ |
List<Method> bugs = new ArrayList<>(); |
3971 |
+ |
for (Method method : allMethods) { |
3972 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3973 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3974 |
+ |
// Manufacture boxed primitives for primitive params |
3975 |
+ |
for (int i = 0; i < args.length; i++) { |
3976 |
+ |
Class<?> type = parameterTypes[i]; |
3977 |
+ |
if (parameterTypes[i] == boolean.class) |
3978 |
+ |
args[i] = false; |
3979 |
+ |
else if (parameterTypes[i] == int.class) |
3980 |
+ |
args[i] = 0; |
3981 |
+ |
else if (parameterTypes[i] == long.class) |
3982 |
+ |
args[i] = 0L; |
3983 |
+ |
} |
3984 |
+ |
try { |
3985 |
+ |
method.invoke(minimalStage, args); |
3986 |
+ |
bugs.add(method); |
3987 |
+ |
} |
3988 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3989 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3990 |
+ |
bugs.add(method); |
3991 |
+ |
// expected.getCause().printStackTrace(); |
3992 |
+ |
} |
3993 |
+ |
} |
3994 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3995 |
+ |
} |
3996 |
+ |
if (!bugs.isEmpty()) |
3997 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3998 |
+ |
} |
3999 |
+ |
|
4000 |
+ |
static class Monad { |
4001 |
+ |
static class ZeroException extends RuntimeException { |
4002 |
+ |
public ZeroException() { super("monadic zero"); } |
4003 |
+ |
} |
4004 |
+ |
// "return", "unit" |
4005 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
4006 |
+ |
return completedFuture(value); |
4007 |
+ |
} |
4008 |
+ |
// monadic zero ? |
4009 |
+ |
static <T> CompletableFuture<T> zero() { |
4010 |
+ |
return failedFuture(new ZeroException()); |
4011 |
+ |
} |
4012 |
+ |
// >=> |
4013 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
4014 |
+ |
(Function<T, CompletableFuture<U>> f, |
4015 |
+ |
Function<U, CompletableFuture<V>> g) { |
4016 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
4017 |
+ |
} |
4018 |
+ |
|
4019 |
+ |
static void assertZero(CompletableFuture<?> f) { |
4020 |
+ |
try { |
4021 |
+ |
f.getNow(null); |
4022 |
+ |
throw new AssertionFailedError("should throw"); |
4023 |
+ |
} catch (CompletionException success) { |
4024 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
4025 |
+ |
} |
4026 |
+ |
} |
4027 |
+ |
|
4028 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
4029 |
+ |
CompletableFuture<T> g) { |
4030 |
+ |
T fval = null, gval = null; |
4031 |
+ |
Throwable fex = null, gex = null; |
4032 |
+ |
|
4033 |
+ |
try { fval = f.get(); } |
4034 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
4035 |
+ |
catch (Throwable ex) { fex = ex; } |
4036 |
+ |
|
4037 |
+ |
try { gval = g.get(); } |
4038 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
4039 |
+ |
catch (Throwable ex) { gex = ex; } |
4040 |
+ |
|
4041 |
+ |
if (fex != null || gex != null) |
4042 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
4043 |
+ |
else |
4044 |
+ |
assertEquals(fval, gval); |
4045 |
+ |
} |
4046 |
+ |
|
4047 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
4048 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
4049 |
+ |
} |
4050 |
+ |
|
4051 |
+ |
/** Implements "monadic plus". */ |
4052 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
4053 |
+ |
CompletableFuture<? extends T> g) { |
4054 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
4055 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
4056 |
+ |
try { |
4057 |
+ |
if (ex == null) { |
4058 |
+ |
if (plus.complete(result)) |
4059 |
+ |
if (plus.firstFailure.get() != null) |
4060 |
+ |
plus.firstFailure.set(null); |
4061 |
+ |
} |
4062 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
4063 |
+ |
if (plus.isDone()) |
4064 |
+ |
plus.firstFailure.set(null); |
4065 |
+ |
} |
4066 |
+ |
else { |
4067 |
+ |
// first failure has precedence |
4068 |
+ |
Throwable first = plus.firstFailure.getAndSet(null); |
4069 |
+ |
|
4070 |
+ |
// may fail with "Self-suppression not permitted" |
4071 |
+ |
try { first.addSuppressed(ex); } |
4072 |
+ |
catch (Exception ignored) {} |
4073 |
+ |
|
4074 |
+ |
plus.completeExceptionally(first); |
4075 |
+ |
} |
4076 |
+ |
} catch (Throwable unexpected) { |
4077 |
+ |
plus.completeExceptionally(unexpected); |
4078 |
+ |
} |
4079 |
+ |
}; |
4080 |
+ |
f.whenComplete(action); |
4081 |
+ |
g.whenComplete(action); |
4082 |
+ |
return plus; |
4083 |
+ |
} |
4084 |
+ |
} |
4085 |
+ |
|
4086 |
+ |
/** |
4087 |
+ |
* CompletableFuture is an additive monad - sort of. |
4088 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
4089 |
+ |
*/ |
4090 |
+ |
public void testAdditiveMonad() throws Throwable { |
4091 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
4092 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
4093 |
+ |
|
4094 |
+ |
// Some mutually non-commutative functions |
4095 |
+ |
Function<Long, CompletableFuture<Long>> triple |
4096 |
+ |
= (x) -> Monad.unit(3 * x); |
4097 |
+ |
Function<Long, CompletableFuture<Long>> inc |
4098 |
+ |
= (x) -> Monad.unit(x + 1); |
4099 |
+ |
|
4100 |
+ |
// unit is a right identity: m >>= unit === m |
4101 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
4102 |
+ |
inc.apply(5L)); |
4103 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
4104 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
4105 |
+ |
inc.apply(5L)); |
4106 |
+ |
|
4107 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
4108 |
+ |
Monad.assertFutureEquals( |
4109 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
4110 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
4111 |
+ |
|
4112 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
4113 |
+ |
|
4114 |
+ |
// zero is a monadic zero |
4115 |
+ |
Monad.assertZero(zero); |
4116 |
+ |
|
4117 |
+ |
// left zero: zero >>= f === zero |
4118 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
4119 |
+ |
// right zero: f >>= (\x -> zero) === zero |
4120 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
4121 |
+ |
|
4122 |
+ |
// f plus zero === f |
4123 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4124 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
4125 |
+ |
// zero plus f === f |
4126 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4127 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
4128 |
+ |
// zero plus zero === zero |
4129 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
4130 |
+ |
{ |
4131 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
4132 |
+ |
Monad.unit(8L)); |
4133 |
+ |
// non-determinism |
4134 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
4135 |
+ |
} |
4136 |
+ |
|
4137 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
4138 |
+ |
// f plus godot === f (doesn't wait for godot) |
4139 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4140 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
4141 |
+ |
// godot plus f === f (doesn't wait for godot) |
4142 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4143 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
4144 |
+ |
} |
4145 |
+ |
|
4146 |
+ |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4147 |
+ |
public void testRecursiveChains() throws Throwable { |
4148 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
4149 |
+ |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4150 |
+ |
{ |
4151 |
+ |
final int val = 42; |
4152 |
+ |
final int n = expensiveTests ? 1_000 : 2; |
4153 |
+ |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4154 |
+ |
CompletableFuture<Integer> tail = head; |
4155 |
+ |
for (int i = 0; i < n; i++) { |
4156 |
+ |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4157 |
+ |
tail = m.thenApply(tail, v -> v + 1); |
4158 |
+ |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4159 |
+ |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4160 |
+ |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4161 |
+ |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4162 |
+ |
} |
4163 |
+ |
head.complete(val); |
4164 |
+ |
assertEquals(val + 3 * n, (int) tail.join()); |
4165 |
+ |
}} |
4166 |
+ |
|
4167 |
+ |
/** |
4168 |
+ |
* A single CompletableFuture with many dependents. |
4169 |
+ |
* A demo of scalability - runtime is O(n). |
4170 |
+ |
*/ |
4171 |
+ |
public void testManyDependents() throws Throwable { |
4172 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4173 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4174 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4175 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
4176 |
+ |
for (int i = 0; i < n; i++) { |
4177 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
4178 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
4179 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
4180 |
+ |
|
4181 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4182 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4183 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4184 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4185 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4186 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4187 |
+ |
|
4188 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4189 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4190 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4191 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4192 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4193 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4194 |
+ |
} |
4195 |
+ |
head.complete(null); |
4196 |
+ |
assertEquals(5 * 3 * n, count.get()); |
4197 |
+ |
} |
4198 |
+ |
|
4199 |
+ |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4200 |
+ |
public void testCoCompletionGarbageRetention() throws Throwable { |
4201 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4202 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4203 |
+ |
CompletableFuture<Integer> f; |
4204 |
+ |
for (int i = 0; i < n; i++) { |
4205 |
+ |
f = new CompletableFuture<>(); |
4206 |
+ |
f.runAfterEither(incomplete, () -> {}); |
4207 |
+ |
f.complete(null); |
4208 |
+ |
|
4209 |
+ |
f = new CompletableFuture<>(); |
4210 |
+ |
f.acceptEither(incomplete, (x) -> {}); |
4211 |
+ |
f.complete(null); |
4212 |
+ |
|
4213 |
+ |
f = new CompletableFuture<>(); |
4214 |
+ |
f.applyToEither(incomplete, (x) -> x); |
4215 |
+ |
f.complete(null); |
4216 |
+ |
|
4217 |
+ |
f = new CompletableFuture<>(); |
4218 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4219 |
+ |
f.complete(null); |
4220 |
+ |
} |
4221 |
+ |
|
4222 |
+ |
for (int i = 0; i < n; i++) { |
4223 |
+ |
f = new CompletableFuture<>(); |
4224 |
+ |
incomplete.runAfterEither(f, () -> {}); |
4225 |
+ |
f.complete(null); |
4226 |
+ |
|
4227 |
+ |
f = new CompletableFuture<>(); |
4228 |
+ |
incomplete.acceptEither(f, (x) -> {}); |
4229 |
+ |
f.complete(null); |
4230 |
+ |
|
4231 |
+ |
f = new CompletableFuture<>(); |
4232 |
+ |
incomplete.applyToEither(f, (x) -> x); |
4233 |
+ |
f.complete(null); |
4234 |
+ |
|
4235 |
+ |
f = new CompletableFuture<>(); |
4236 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4237 |
+ |
f.complete(null); |
4238 |
+ |
} |
4239 |
+ |
} |
4240 |
+ |
|
4241 |
+ |
/* |
4242 |
+ |
* Tests below currently fail in stress mode due to memory retention. |
4243 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck |
4244 |
+ |
*/ |
4245 |
+ |
|
4246 |
+ |
/** Checks for garbage retention with anyOf. */ |
4247 |
+ |
public void testAnyOfGarbageRetention() throws Throwable { |
4248 |
+ |
for (Integer v : new Integer[] { 1, null }) |
4249 |
+ |
{ |
4250 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4251 |
+ |
CompletableFuture<Integer>[] fs |
4252 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4253 |
+ |
for (int i = 0; i < fs.length; i++) |
4254 |
+ |
fs[i] = new CompletableFuture<>(); |
4255 |
+ |
fs[fs.length - 1].complete(v); |
4256 |
+ |
for (int i = 0; i < n; i++) |
4257 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4258 |
+ |
}} |
4259 |
+ |
|
4260 |
+ |
/** Checks for garbage retention with allOf. */ |
4261 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4262 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4263 |
+ |
CompletableFuture<Integer>[] fs |
4264 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4265 |
+ |
for (int i = 0; i < fs.length; i++) |
4266 |
+ |
fs[i] = new CompletableFuture<>(); |
4267 |
+ |
for (int i = 0; i < n; i++) |
4268 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4269 |
+ |
} |
4270 |
+ |
|
4271 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
4272 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4273 |
+ |
// stage.whenComplete((v, ex) -> { |
4274 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4275 |
+ |
// }); |
4276 |
+ |
// return f.join(); |
4277 |
+ |
// } |
4278 |
+ |
|
4279 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
4280 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4281 |
+ |
// stage.whenComplete((v, ex) -> { |
4282 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4283 |
+ |
// }); |
4284 |
+ |
// return f.isDone(); |
4285 |
+ |
// } |
4286 |
+ |
|
4287 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
4288 |
+ |
// return stage.toCompletableFuture().copy().join(); |
4289 |
+ |
// } |
4290 |
+ |
|
4291 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
4292 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
4293 |
+ |
// } |
4294 |
+ |
|
4295 |
|
} |