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; |
24 |
– |
import java.util.concurrent.TimeUnit; |
35 |
|
import java.util.concurrent.atomic.AtomicInteger; |
36 |
|
import java.util.concurrent.atomic.AtomicReference; |
37 |
|
import java.util.function.BiConsumer; |
38 |
|
import java.util.function.BiFunction; |
39 |
|
import java.util.function.Consumer; |
40 |
|
import java.util.function.Function; |
41 |
+ |
import java.util.function.Predicate; |
42 |
|
import java.util.function.Supplier; |
43 |
|
|
44 |
+ |
import junit.framework.AssertionFailedError; |
45 |
|
import junit.framework.Test; |
46 |
|
import junit.framework.TestSuite; |
47 |
|
|
89 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
90 |
|
} |
91 |
|
|
92 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
93 |
< |
long startTime = System.nanoTime(); |
94 |
< |
long timeoutMillis = LONG_DELAY_MS; |
95 |
< |
try { |
96 |
< |
f.get(timeoutMillis, MILLISECONDS); |
97 |
< |
shouldThrow(); |
98 |
< |
} catch (ExecutionException success) { |
99 |
< |
assertTrue(success.getCause() instanceof CFException); |
100 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
101 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
102 |
< |
|
103 |
< |
try { |
104 |
< |
f.join(); |
105 |
< |
shouldThrow(); |
106 |
< |
} catch (CompletionException success) { |
107 |
< |
assertTrue(success.getCause() instanceof CFException); |
108 |
< |
} |
97 |
< |
try { |
98 |
< |
f.getNow(null); |
99 |
< |
shouldThrow(); |
100 |
< |
} catch (CompletionException success) { |
101 |
< |
assertTrue(success.getCause() instanceof CFException); |
92 |
> |
/** |
93 |
> |
* Returns the "raw" internal exceptional completion of f, |
94 |
> |
* without any additional wrapping with CompletionException. |
95 |
> |
*/ |
96 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
97 |
> |
// handle (and whenComplete) can distinguish between "direct" |
98 |
> |
// and "wrapped" exceptional completion |
99 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
100 |
> |
} |
101 |
> |
|
102 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
103 |
> |
boolean wrapped, |
104 |
> |
Consumer<Throwable> checker) { |
105 |
> |
Throwable cause = exceptionalCompletion(f); |
106 |
> |
if (wrapped) { |
107 |
> |
assertTrue(cause instanceof CompletionException); |
108 |
> |
cause = cause.getCause(); |
109 |
|
} |
110 |
< |
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 |
< |
} |
110 |
> |
checker.accept(cause); |
111 |
|
|
114 |
– |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
115 |
– |
Throwable ex) { |
112 |
|
long startTime = System.nanoTime(); |
117 |
– |
long timeoutMillis = LONG_DELAY_MS; |
113 |
|
try { |
114 |
< |
f.get(timeoutMillis, MILLISECONDS); |
114 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
115 |
|
shouldThrow(); |
116 |
|
} catch (ExecutionException success) { |
117 |
< |
assertSame(ex, success.getCause()); |
117 |
> |
assertSame(cause, success.getCause()); |
118 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
119 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
119 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
120 |
|
|
121 |
|
try { |
122 |
|
f.join(); |
123 |
|
shouldThrow(); |
124 |
|
} catch (CompletionException success) { |
125 |
< |
assertSame(ex, success.getCause()); |
126 |
< |
} |
125 |
> |
assertSame(cause, success.getCause()); |
126 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
127 |
> |
|
128 |
|
try { |
129 |
|
f.getNow(null); |
130 |
|
shouldThrow(); |
131 |
|
} catch (CompletionException success) { |
132 |
< |
assertSame(ex, success.getCause()); |
133 |
< |
} |
132 |
> |
assertSame(cause, success.getCause()); |
133 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
> |
|
135 |
|
try { |
136 |
|
f.get(); |
137 |
|
shouldThrow(); |
138 |
|
} catch (ExecutionException success) { |
139 |
< |
assertSame(ex, success.getCause()); |
139 |
> |
assertSame(cause, success.getCause()); |
140 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
141 |
|
|
145 |
– |
assertTrue(f.isDone()); |
142 |
|
assertFalse(f.isCancelled()); |
143 |
+ |
assertTrue(f.isDone()); |
144 |
+ |
assertTrue(f.isCompletedExceptionally()); |
145 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
146 |
|
} |
147 |
|
|
148 |
< |
<U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) { |
149 |
< |
long startTime = System.nanoTime(); |
150 |
< |
long timeoutMillis = LONG_DELAY_MS; |
151 |
< |
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 |
< |
} |
148 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
149 |
> |
checkCompletedExceptionally(f, true, |
150 |
> |
(t) -> assertTrue(t instanceof CFException)); |
151 |
> |
} |
152 |
|
|
153 |
< |
try { |
154 |
< |
f.get(); |
155 |
< |
shouldThrow(); |
156 |
< |
} catch (ExecutionException ex) { |
179 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
180 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
> |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
154 |
> |
checkCompletedExceptionally(f, true, |
155 |
> |
(t) -> assertTrue(t instanceof CancellationException)); |
156 |
> |
} |
157 |
|
|
158 |
< |
assertTrue(f.isDone()); |
159 |
< |
assertFalse(f.isCancelled()); |
160 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
158 |
> |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
159 |
> |
checkCompletedExceptionally(f, false, |
160 |
> |
(t) -> assertTrue(t instanceof TimeoutException)); |
161 |
|
} |
162 |
|
|
163 |
< |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
164 |
< |
Throwable ex) { |
165 |
< |
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); } |
163 |
> |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
164 |
> |
Throwable ex) { |
165 |
> |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
166 |
|
} |
167 |
|
|
168 |
< |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
169 |
< |
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); } |
168 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
169 |
> |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
170 |
|
} |
171 |
|
|
172 |
|
void checkCancelled(CompletableFuture<?> f) { |
173 |
|
long startTime = System.nanoTime(); |
209 |
– |
long timeoutMillis = LONG_DELAY_MS; |
174 |
|
try { |
175 |
< |
f.get(timeoutMillis, MILLISECONDS); |
175 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
176 |
|
shouldThrow(); |
177 |
|
} catch (CancellationException success) { |
178 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
179 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
179 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
180 |
|
|
181 |
|
try { |
182 |
|
f.join(); |
191 |
|
shouldThrow(); |
192 |
|
} catch (CancellationException success) { |
193 |
|
} 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 |
– |
} |
194 |
|
|
195 |
< |
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); |
195 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
196 |
|
|
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); } |
197 |
|
assertTrue(f.isDone()); |
266 |
– |
assertFalse(f.isCancelled()); |
198 |
|
assertTrue(f.isCompletedExceptionally()); |
199 |
+ |
assertTrue(f.isCancelled()); |
200 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
201 |
|
} |
202 |
|
|
361 |
|
checkCompletedNormally(f, "test"); |
362 |
|
} |
363 |
|
|
364 |
< |
abstract class CheckedAction { |
364 |
> |
abstract static class CheckedAction { |
365 |
|
int invocationCount = 0; |
366 |
|
final ExecutionMode m; |
367 |
|
CheckedAction(ExecutionMode m) { this.m = m; } |
373 |
|
void assertInvoked() { assertEquals(1, invocationCount); } |
374 |
|
} |
375 |
|
|
376 |
< |
abstract class CheckedIntegerAction extends CheckedAction { |
376 |
> |
abstract static class CheckedIntegerAction extends CheckedAction { |
377 |
|
Integer value; |
378 |
|
CheckedIntegerAction(ExecutionMode m) { super(m); } |
379 |
|
void assertValue(Integer expected) { |
382 |
|
} |
383 |
|
} |
384 |
|
|
385 |
< |
class IntegerSupplier extends CheckedAction |
385 |
> |
static class IntegerSupplier extends CheckedAction |
386 |
|
implements Supplier<Integer> |
387 |
|
{ |
388 |
|
final Integer value; |
401 |
|
return (x == null) ? null : x + 1; |
402 |
|
} |
403 |
|
|
404 |
< |
class NoopConsumer extends CheckedIntegerAction |
404 |
> |
static class NoopConsumer extends CheckedIntegerAction |
405 |
|
implements Consumer<Integer> |
406 |
|
{ |
407 |
|
NoopConsumer(ExecutionMode m) { super(m); } |
411 |
|
} |
412 |
|
} |
413 |
|
|
414 |
< |
class IncFunction extends CheckedIntegerAction |
414 |
> |
static class IncFunction extends CheckedIntegerAction |
415 |
|
implements Function<Integer,Integer> |
416 |
|
{ |
417 |
|
IncFunction(ExecutionMode m) { super(m); } |
429 |
|
- ((y == null) ? 99 : y.intValue()); |
430 |
|
} |
431 |
|
|
432 |
< |
class SubtractAction extends CheckedIntegerAction |
432 |
> |
static class SubtractAction extends CheckedIntegerAction |
433 |
|
implements BiConsumer<Integer, Integer> |
434 |
|
{ |
435 |
|
SubtractAction(ExecutionMode m) { super(m); } |
439 |
|
} |
440 |
|
} |
441 |
|
|
442 |
< |
class SubtractFunction extends CheckedIntegerAction |
442 |
> |
static class SubtractFunction extends CheckedIntegerAction |
443 |
|
implements BiFunction<Integer, Integer, Integer> |
444 |
|
{ |
445 |
|
SubtractFunction(ExecutionMode m) { super(m); } |
449 |
|
} |
450 |
|
} |
451 |
|
|
452 |
< |
class Noop extends CheckedAction implements Runnable { |
452 |
> |
static class Noop extends CheckedAction implements Runnable { |
453 |
|
Noop(ExecutionMode m) { super(m); } |
454 |
|
public void run() { |
455 |
|
invoked(); |
456 |
|
} |
457 |
|
} |
458 |
|
|
459 |
< |
class FailingSupplier extends CheckedAction |
459 |
> |
static class FailingSupplier extends CheckedAction |
460 |
|
implements Supplier<Integer> |
461 |
|
{ |
462 |
< |
FailingSupplier(ExecutionMode m) { super(m); } |
462 |
> |
final CFException ex; |
463 |
> |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
464 |
|
public Integer get() { |
465 |
|
invoked(); |
466 |
< |
throw new CFException(); |
466 |
> |
throw ex; |
467 |
|
} |
468 |
|
} |
469 |
|
|
470 |
< |
class FailingConsumer extends CheckedIntegerAction |
470 |
> |
static class FailingConsumer extends CheckedIntegerAction |
471 |
|
implements Consumer<Integer> |
472 |
|
{ |
473 |
< |
FailingConsumer(ExecutionMode m) { super(m); } |
473 |
> |
final CFException ex; |
474 |
> |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
475 |
|
public void accept(Integer x) { |
476 |
|
invoked(); |
477 |
|
value = x; |
478 |
< |
throw new CFException(); |
478 |
> |
throw ex; |
479 |
|
} |
480 |
|
} |
481 |
|
|
482 |
< |
class FailingBiConsumer extends CheckedIntegerAction |
482 |
> |
static class FailingBiConsumer extends CheckedIntegerAction |
483 |
|
implements BiConsumer<Integer, Integer> |
484 |
|
{ |
485 |
< |
FailingBiConsumer(ExecutionMode m) { super(m); } |
485 |
> |
final CFException ex; |
486 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
487 |
|
public void accept(Integer x, Integer y) { |
488 |
|
invoked(); |
489 |
|
value = subtract(x, y); |
490 |
< |
throw new CFException(); |
490 |
> |
throw ex; |
491 |
|
} |
492 |
|
} |
493 |
|
|
494 |
< |
class FailingFunction extends CheckedIntegerAction |
494 |
> |
static class FailingFunction extends CheckedIntegerAction |
495 |
|
implements Function<Integer, Integer> |
496 |
|
{ |
497 |
< |
FailingFunction(ExecutionMode m) { super(m); } |
497 |
> |
final CFException ex; |
498 |
> |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
499 |
|
public Integer apply(Integer x) { |
500 |
|
invoked(); |
501 |
|
value = x; |
502 |
< |
throw new CFException(); |
502 |
> |
throw ex; |
503 |
|
} |
504 |
|
} |
505 |
|
|
506 |
< |
class FailingBiFunction extends CheckedIntegerAction |
506 |
> |
static class FailingBiFunction extends CheckedIntegerAction |
507 |
|
implements BiFunction<Integer, Integer, Integer> |
508 |
|
{ |
509 |
< |
FailingBiFunction(ExecutionMode m) { super(m); } |
509 |
> |
final CFException ex; |
510 |
> |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
511 |
|
public Integer apply(Integer x, Integer y) { |
512 |
|
invoked(); |
513 |
|
value = subtract(x, y); |
514 |
< |
throw new CFException(); |
514 |
> |
throw ex; |
515 |
|
} |
516 |
|
} |
517 |
|
|
518 |
< |
class FailingRunnable extends CheckedAction implements Runnable { |
519 |
< |
FailingRunnable(ExecutionMode m) { super(m); } |
518 |
> |
static class FailingRunnable extends CheckedAction implements Runnable { |
519 |
> |
final CFException ex; |
520 |
> |
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
521 |
|
public void run() { |
522 |
|
invoked(); |
523 |
< |
throw new CFException(); |
523 |
> |
throw ex; |
524 |
|
} |
525 |
|
} |
526 |
|
|
527 |
< |
class CompletableFutureInc extends CheckedIntegerAction |
527 |
> |
static class CompletableFutureInc extends CheckedIntegerAction |
528 |
|
implements Function<Integer, CompletableFuture<Integer>> |
529 |
|
{ |
530 |
|
CompletableFutureInc(ExecutionMode m) { super(m); } |
537 |
|
} |
538 |
|
} |
539 |
|
|
540 |
< |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
540 |
> |
static class FailingCompletableFutureFunction extends CheckedIntegerAction |
541 |
|
implements Function<Integer, CompletableFuture<Integer>> |
542 |
|
{ |
543 |
< |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
543 |
> |
final CFException ex; |
544 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
545 |
|
public CompletableFuture<Integer> apply(Integer x) { |
546 |
|
invoked(); |
547 |
|
value = x; |
548 |
< |
throw new CFException(); |
548 |
> |
throw ex; |
549 |
> |
} |
550 |
> |
} |
551 |
> |
|
552 |
> |
static class CountingRejectingExecutor implements Executor { |
553 |
> |
final RejectedExecutionException ex = new RejectedExecutionException(); |
554 |
> |
final AtomicInteger count = new AtomicInteger(0); |
555 |
> |
public void execute(Runnable r) { |
556 |
> |
count.getAndIncrement(); |
557 |
> |
throw ex; |
558 |
|
} |
559 |
|
} |
560 |
|
|
856 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
857 |
|
final CompletableFuture<Integer> g = f.exceptionally |
858 |
|
((Throwable t) -> { |
911 |
– |
// Should not be called |
859 |
|
a.getAndIncrement(); |
860 |
< |
throw new AssertionError(); |
860 |
> |
threadFail("should not be called"); |
861 |
> |
return null; // unreached |
862 |
|
}); |
863 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
864 |
|
|
892 |
|
assertEquals(1, a.get()); |
893 |
|
}} |
894 |
|
|
895 |
+ |
/** |
896 |
+ |
* If an "exceptionally action" throws an exception, it completes |
897 |
+ |
* exceptionally with that exception |
898 |
+ |
*/ |
899 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
900 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
901 |
|
{ |
914 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
915 |
|
|
916 |
|
checkCompletedWithWrappedException(g, ex2); |
917 |
+ |
checkCompletedExceptionally(f, ex1); |
918 |
|
assertEquals(1, a.get()); |
919 |
|
}} |
920 |
|
|
922 |
|
* whenComplete action executes on normal completion, propagating |
923 |
|
* source result. |
924 |
|
*/ |
925 |
< |
public void testWhenComplete_normalCompletion1() { |
925 |
> |
public void testWhenComplete_normalCompletion() { |
926 |
|
for (ExecutionMode m : ExecutionMode.values()) |
927 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
928 |
|
for (Integer v1 : new Integer[] { 1, null }) |
932 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
933 |
|
final CompletableFuture<Integer> g = m.whenComplete |
934 |
|
(f, |
935 |
< |
(Integer x, Throwable t) -> { |
935 |
> |
(Integer result, Throwable t) -> { |
936 |
|
m.checkExecutionMode(); |
937 |
< |
threadAssertSame(x, v1); |
937 |
> |
threadAssertSame(result, v1); |
938 |
|
threadAssertNull(t); |
939 |
|
a.getAndIncrement(); |
940 |
|
}); |
959 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
960 |
|
final CompletableFuture<Integer> g = m.whenComplete |
961 |
|
(f, |
962 |
< |
(Integer x, Throwable t) -> { |
962 |
> |
(Integer result, Throwable t) -> { |
963 |
|
m.checkExecutionMode(); |
964 |
< |
threadAssertNull(x); |
964 |
> |
threadAssertNull(result); |
965 |
|
threadAssertSame(t, ex); |
966 |
|
a.getAndIncrement(); |
967 |
|
}); |
986 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
987 |
|
final CompletableFuture<Integer> g = m.whenComplete |
988 |
|
(f, |
989 |
< |
(Integer x, Throwable t) -> { |
989 |
> |
(Integer result, Throwable t) -> { |
990 |
|
m.checkExecutionMode(); |
991 |
< |
threadAssertNull(x); |
991 |
> |
threadAssertNull(result); |
992 |
|
threadAssertTrue(t instanceof CancellationException); |
993 |
|
a.getAndIncrement(); |
994 |
|
}); |
1003 |
|
* If a whenComplete action throws an exception when triggered by |
1004 |
|
* a normal completion, it completes exceptionally |
1005 |
|
*/ |
1006 |
< |
public void testWhenComplete_actionFailed() { |
1006 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
1007 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1008 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1009 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1014 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1015 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1016 |
|
(f, |
1017 |
< |
(Integer x, Throwable t) -> { |
1017 |
> |
(Integer result, Throwable t) -> { |
1018 |
|
m.checkExecutionMode(); |
1019 |
< |
threadAssertSame(x, v1); |
1019 |
> |
threadAssertSame(result, v1); |
1020 |
|
threadAssertNull(t); |
1021 |
|
a.getAndIncrement(); |
1022 |
|
throw ex; |
1031 |
|
/** |
1032 |
|
* If a whenComplete action throws an exception when triggered by |
1033 |
|
* a source completion that also throws an exception, the source |
1034 |
< |
* exception takes precedence. |
1034 |
> |
* exception takes precedence (unlike handle) |
1035 |
|
*/ |
1036 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1036 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1037 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1038 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1039 |
|
{ |
1045 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1046 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1047 |
|
(f, |
1048 |
< |
(Integer x, Throwable t) -> { |
1048 |
> |
(Integer result, Throwable t) -> { |
1049 |
|
m.checkExecutionMode(); |
1050 |
|
threadAssertSame(t, ex1); |
1051 |
< |
threadAssertNull(x); |
1051 |
> |
threadAssertNull(result); |
1052 |
|
a.getAndIncrement(); |
1053 |
|
throw ex2; |
1054 |
|
}); |
1056 |
|
|
1057 |
|
checkCompletedWithWrappedException(g, ex1); |
1058 |
|
checkCompletedExceptionally(f, ex1); |
1059 |
+ |
if (testImplementationDetails) { |
1060 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1061 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1062 |
+ |
} |
1063 |
|
assertEquals(1, a.get()); |
1064 |
|
}} |
1065 |
|
|
1077 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1078 |
|
final CompletableFuture<Integer> g = m.handle |
1079 |
|
(f, |
1080 |
< |
(Integer x, Throwable t) -> { |
1080 |
> |
(Integer result, Throwable t) -> { |
1081 |
|
m.checkExecutionMode(); |
1082 |
< |
threadAssertSame(x, v1); |
1082 |
> |
threadAssertSame(result, v1); |
1083 |
|
threadAssertNull(t); |
1084 |
|
a.getAndIncrement(); |
1085 |
|
return inc(v1); |
1106 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1107 |
|
final CompletableFuture<Integer> g = m.handle |
1108 |
|
(f, |
1109 |
< |
(Integer x, Throwable t) -> { |
1109 |
> |
(Integer result, Throwable t) -> { |
1110 |
|
m.checkExecutionMode(); |
1111 |
< |
threadAssertNull(x); |
1111 |
> |
threadAssertNull(result); |
1112 |
|
threadAssertSame(t, ex); |
1113 |
|
a.getAndIncrement(); |
1114 |
|
return v1; |
1135 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1136 |
|
final CompletableFuture<Integer> g = m.handle |
1137 |
|
(f, |
1138 |
< |
(Integer x, Throwable t) -> { |
1138 |
> |
(Integer result, Throwable t) -> { |
1139 |
|
m.checkExecutionMode(); |
1140 |
< |
threadAssertNull(x); |
1140 |
> |
threadAssertNull(result); |
1141 |
|
threadAssertTrue(t instanceof CancellationException); |
1142 |
|
a.getAndIncrement(); |
1143 |
|
return v1; |
1150 |
|
}} |
1151 |
|
|
1152 |
|
/** |
1153 |
< |
* handle result completes exceptionally if action does |
1153 |
> |
* If a "handle action" throws an exception when triggered by |
1154 |
> |
* a normal completion, it completes exceptionally |
1155 |
|
*/ |
1156 |
< |
public void testHandle_sourceFailedActionFailed() { |
1156 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1157 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1158 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1159 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1160 |
|
{ |
1161 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1162 |
|
final AtomicInteger a = new AtomicInteger(0); |
1163 |
< |
final CFException ex1 = new CFException(); |
1164 |
< |
final CFException ex2 = new CFException(); |
1206 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1163 |
> |
final CFException ex = new CFException(); |
1164 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1165 |
|
final CompletableFuture<Integer> g = m.handle |
1166 |
|
(f, |
1167 |
< |
(Integer x, Throwable t) -> { |
1167 |
> |
(Integer result, Throwable t) -> { |
1168 |
|
m.checkExecutionMode(); |
1169 |
< |
threadAssertNull(x); |
1170 |
< |
threadAssertSame(ex1, t); |
1169 |
> |
threadAssertSame(result, v1); |
1170 |
> |
threadAssertNull(t); |
1171 |
|
a.getAndIncrement(); |
1172 |
< |
throw ex2; |
1172 |
> |
throw ex; |
1173 |
|
}); |
1174 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1174 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1175 |
|
|
1176 |
< |
checkCompletedWithWrappedException(g, ex2); |
1177 |
< |
checkCompletedExceptionally(f, ex1); |
1176 |
> |
checkCompletedWithWrappedException(g, ex); |
1177 |
> |
checkCompletedNormally(f, v1); |
1178 |
|
assertEquals(1, a.get()); |
1179 |
|
}} |
1180 |
|
|
1181 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1182 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1181 |
> |
/** |
1182 |
> |
* If a "handle action" throws an exception when triggered by |
1183 |
> |
* a source completion that also throws an exception, the action |
1184 |
> |
* exception takes precedence (unlike whenComplete) |
1185 |
> |
*/ |
1186 |
> |
public void testHandle_sourceFailedActionFailed() { |
1187 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1188 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1188 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1189 |
|
{ |
1228 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1190 |
|
final AtomicInteger a = new AtomicInteger(0); |
1191 |
< |
final CFException ex = new CFException(); |
1192 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1191 |
> |
final CFException ex1 = new CFException(); |
1192 |
> |
final CFException ex2 = new CFException(); |
1193 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1194 |
> |
|
1195 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1196 |
|
final CompletableFuture<Integer> g = m.handle |
1197 |
|
(f, |
1198 |
< |
(Integer x, Throwable t) -> { |
1198 |
> |
(Integer result, Throwable t) -> { |
1199 |
|
m.checkExecutionMode(); |
1200 |
< |
threadAssertSame(x, v1); |
1201 |
< |
threadAssertNull(t); |
1200 |
> |
threadAssertNull(result); |
1201 |
> |
threadAssertSame(ex1, t); |
1202 |
|
a.getAndIncrement(); |
1203 |
< |
throw ex; |
1203 |
> |
throw ex2; |
1204 |
|
}); |
1205 |
< |
if (createIncomplete) assertTrue(f.complete(v1)); |
1205 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1206 |
|
|
1207 |
< |
checkCompletedWithWrappedException(g, ex); |
1208 |
< |
checkCompletedNormally(f, v1); |
1207 |
> |
checkCompletedWithWrappedException(g, ex2); |
1208 |
> |
checkCompletedExceptionally(f, ex1); |
1209 |
|
assertEquals(1, a.get()); |
1210 |
|
}} |
1211 |
|
|
1238 |
|
{ |
1239 |
|
final FailingRunnable r = new FailingRunnable(m); |
1240 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1241 |
< |
checkCompletedWithWrappedCFException(f); |
1241 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1242 |
|
r.assertInvoked(); |
1243 |
|
}} |
1244 |
|
|
1245 |
+ |
public void testRunAsync_rejectingExecutor() { |
1246 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1247 |
+ |
try { |
1248 |
+ |
CompletableFuture.runAsync(() -> {}, e); |
1249 |
+ |
shouldThrow(); |
1250 |
+ |
} catch (Throwable t) { |
1251 |
+ |
assertSame(e.ex, t); |
1252 |
+ |
} |
1253 |
+ |
|
1254 |
+ |
assertEquals(1, e.count.get()); |
1255 |
+ |
} |
1256 |
+ |
|
1257 |
|
/** |
1258 |
|
* supplyAsync completes with result of supplier |
1259 |
|
*/ |
1284 |
|
{ |
1285 |
|
FailingSupplier r = new FailingSupplier(m); |
1286 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1287 |
< |
checkCompletedWithWrappedCFException(f); |
1287 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1288 |
|
r.assertInvoked(); |
1289 |
|
}} |
1290 |
|
|
1291 |
+ |
public void testSupplyAsync_rejectingExecutor() { |
1292 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1293 |
+ |
try { |
1294 |
+ |
CompletableFuture.supplyAsync(() -> null, e); |
1295 |
+ |
shouldThrow(); |
1296 |
+ |
} catch (Throwable t) { |
1297 |
+ |
assertSame(e.ex, t); |
1298 |
+ |
} |
1299 |
+ |
|
1300 |
+ |
assertEquals(1, e.count.get()); |
1301 |
+ |
} |
1302 |
+ |
|
1303 |
|
// seq completion methods |
1304 |
|
|
1305 |
|
/** |
1418 |
|
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1419 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1420 |
|
|
1421 |
< |
checkCompletedWithWrappedCFException(h0); |
1422 |
< |
checkCompletedWithWrappedCFException(h1); |
1423 |
< |
checkCompletedWithWrappedCFException(h2); |
1424 |
< |
checkCompletedWithWrappedCFException(h3); |
1425 |
< |
checkCompletedWithWrappedCFException(h4); |
1426 |
< |
checkCompletedWithWrappedCFException(h5); |
1421 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1422 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1423 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1424 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1425 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1426 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1427 |
|
checkCompletedNormally(f, v1); |
1428 |
|
}} |
1429 |
|
|
1522 |
|
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1523 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1524 |
|
|
1525 |
< |
checkCompletedWithWrappedCFException(h0); |
1526 |
< |
checkCompletedWithWrappedCFException(h1); |
1527 |
< |
checkCompletedWithWrappedCFException(h2); |
1528 |
< |
checkCompletedWithWrappedCFException(h3); |
1525 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1526 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1527 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1528 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1529 |
|
checkCompletedNormally(f, v1); |
1530 |
|
}} |
1531 |
|
|
1624 |
|
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1625 |
|
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1626 |
|
|
1627 |
< |
checkCompletedWithWrappedCFException(h0); |
1628 |
< |
checkCompletedWithWrappedCFException(h1); |
1629 |
< |
checkCompletedWithWrappedCFException(h2); |
1630 |
< |
checkCompletedWithWrappedCFException(h3); |
1627 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1628 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1629 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1630 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1631 |
|
checkCompletedNormally(f, v1); |
1632 |
|
}} |
1633 |
|
|
1789 |
|
assertTrue(snd.complete(w2)); |
1790 |
|
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1791 |
|
|
1792 |
< |
checkCompletedWithWrappedCFException(h1); |
1793 |
< |
checkCompletedWithWrappedCFException(h2); |
1794 |
< |
checkCompletedWithWrappedCFException(h3); |
1792 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1793 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1794 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1795 |
|
r1.assertInvoked(); |
1796 |
|
r2.assertInvoked(); |
1797 |
|
r3.assertInvoked(); |
1953 |
|
assertTrue(snd.complete(w2)); |
1954 |
|
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1955 |
|
|
1956 |
< |
checkCompletedWithWrappedCFException(h1); |
1957 |
< |
checkCompletedWithWrappedCFException(h2); |
1958 |
< |
checkCompletedWithWrappedCFException(h3); |
1956 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1957 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1958 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1959 |
|
r1.assertInvoked(); |
1960 |
|
r2.assertInvoked(); |
1961 |
|
r3.assertInvoked(); |
2117 |
|
assertTrue(snd.complete(w2)); |
2118 |
|
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2119 |
|
|
2120 |
< |
checkCompletedWithWrappedCFException(h1); |
2121 |
< |
checkCompletedWithWrappedCFException(h2); |
2122 |
< |
checkCompletedWithWrappedCFException(h3); |
2120 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2121 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2122 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2123 |
|
r1.assertInvoked(); |
2124 |
|
r2.assertInvoked(); |
2125 |
|
r3.assertInvoked(); |
2409 |
|
f.complete(v1); |
2410 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2411 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2412 |
< |
checkCompletedWithWrappedCFException(h0); |
2413 |
< |
checkCompletedWithWrappedCFException(h1); |
2414 |
< |
checkCompletedWithWrappedCFException(h2); |
2415 |
< |
checkCompletedWithWrappedCFException(h3); |
2412 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2413 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2414 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2415 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2416 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2417 |
|
|
2418 |
|
g.complete(v2); |
2421 |
|
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2422 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2423 |
|
|
2424 |
< |
checkCompletedWithWrappedCFException(h4); |
2424 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2425 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2426 |
|
Objects.equals(v2, rs[4].value)); |
2427 |
< |
checkCompletedWithWrappedCFException(h5); |
2427 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2428 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2429 |
|
Objects.equals(v2, rs[5].value)); |
2430 |
|
|
2668 |
|
f.complete(v1); |
2669 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2670 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2671 |
< |
checkCompletedWithWrappedCFException(h0); |
2672 |
< |
checkCompletedWithWrappedCFException(h1); |
2673 |
< |
checkCompletedWithWrappedCFException(h2); |
2674 |
< |
checkCompletedWithWrappedCFException(h3); |
2671 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2672 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2673 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2674 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2675 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2676 |
|
|
2677 |
|
g.complete(v2); |
2680 |
|
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2681 |
|
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2682 |
|
|
2683 |
< |
checkCompletedWithWrappedCFException(h4); |
2683 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2684 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2685 |
|
Objects.equals(v2, rs[4].value)); |
2686 |
< |
checkCompletedWithWrappedCFException(h5); |
2686 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2687 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2688 |
|
Objects.equals(v2, rs[5].value)); |
2689 |
|
|
2699 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2700 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2701 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2702 |
+ |
for (boolean pushNop : new boolean[] { true, false }) |
2703 |
|
{ |
2704 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2705 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2712 |
|
checkIncomplete(h1); |
2713 |
|
rs[0].assertNotInvoked(); |
2714 |
|
rs[1].assertNotInvoked(); |
2715 |
+ |
if (pushNop) { // ad hoc test of intra-completion interference |
2716 |
+ |
m.thenRun(f, () -> {}); |
2717 |
+ |
m.thenRun(g, () -> {}); |
2718 |
+ |
} |
2719 |
|
f.complete(v1); |
2720 |
|
checkCompletedNormally(h0, null); |
2721 |
|
checkCompletedNormally(h1, null); |
2928 |
|
assertTrue(f.complete(v1)); |
2929 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2930 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2931 |
< |
checkCompletedWithWrappedCFException(h0); |
2932 |
< |
checkCompletedWithWrappedCFException(h1); |
2933 |
< |
checkCompletedWithWrappedCFException(h2); |
2934 |
< |
checkCompletedWithWrappedCFException(h3); |
2931 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2932 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2933 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2934 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2935 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2936 |
|
assertTrue(g.complete(v2)); |
2937 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2938 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2939 |
< |
checkCompletedWithWrappedCFException(h4); |
2940 |
< |
checkCompletedWithWrappedCFException(h5); |
2939 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2940 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2941 |
|
|
2942 |
|
checkCompletedNormally(f, v1); |
2943 |
|
checkCompletedNormally(g, v2); |
2998 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2999 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
3000 |
|
|
3001 |
< |
checkCompletedWithWrappedCFException(g); |
3001 |
> |
checkCompletedWithWrappedException(g, r.ex); |
3002 |
|
checkCompletedNormally(f, v1); |
3003 |
|
}} |
3004 |
|
|
3107 |
|
} |
3108 |
|
} |
3109 |
|
|
3110 |
< |
public void testAllOf_backwards() throws Exception { |
3110 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3111 |
|
for (int k = 1; k < 10; k++) { |
3112 |
|
CompletableFuture<Integer>[] fs |
3113 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3135 |
|
for (int i = 0; i < k; i++) { |
3136 |
|
checkIncomplete(f); |
3137 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3138 |
< |
if (i != k/2) { |
3138 |
> |
if (i != k / 2) { |
3139 |
|
fs[i].complete(i); |
3140 |
|
checkCompletedNormally(fs[i], i); |
3141 |
|
} else { |
3338 |
|
() -> CompletableFuture.anyOf(null, f), |
3339 |
|
|
3340 |
|
() -> f.obtrudeException(null), |
3341 |
+ |
|
3342 |
+ |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3343 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3344 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null), |
3345 |
+ |
|
3346 |
+ |
() -> f.orTimeout(1L, null), |
3347 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3348 |
+ |
|
3349 |
+ |
() -> CompletableFuture.failedFuture(null), |
3350 |
+ |
() -> CompletableFuture.failedStage(null), |
3351 |
|
}; |
3352 |
|
|
3353 |
|
assertThrows(NullPointerException.class, throwingActions); |
3355 |
|
} |
3356 |
|
|
3357 |
|
/** |
3358 |
+ |
* Test submissions to an executor that rejects all tasks. |
3359 |
+ |
*/ |
3360 |
+ |
public void testRejectingExecutor() { |
3361 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3362 |
+ |
{ |
3363 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3364 |
+ |
|
3365 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3366 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3367 |
+ |
|
3368 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3369 |
+ |
|
3370 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3371 |
+ |
srcs.add(complete); |
3372 |
+ |
srcs.add(incomplete); |
3373 |
+ |
|
3374 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3375 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3376 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3377 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3378 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3379 |
+ |
|
3380 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3381 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3382 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3383 |
+ |
|
3384 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3385 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3386 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3387 |
+ |
|
3388 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3389 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3390 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3391 |
+ |
|
3392 |
+ |
for (CompletableFuture<?> future : fs) { |
3393 |
+ |
if (src.isDone()) |
3394 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3395 |
+ |
else |
3396 |
+ |
checkIncomplete(future); |
3397 |
+ |
} |
3398 |
+ |
futures.addAll(fs); |
3399 |
+ |
} |
3400 |
+ |
|
3401 |
+ |
{ |
3402 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3403 |
+ |
|
3404 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3405 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3406 |
+ |
|
3407 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3408 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3409 |
+ |
|
3410 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3411 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3412 |
+ |
|
3413 |
+ |
for (CompletableFuture<?> future : fs) |
3414 |
+ |
checkIncomplete(future); |
3415 |
+ |
futures.addAll(fs); |
3416 |
+ |
} |
3417 |
+ |
|
3418 |
+ |
{ |
3419 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3420 |
+ |
|
3421 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3422 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3423 |
+ |
|
3424 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3425 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3426 |
+ |
|
3427 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3428 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3429 |
+ |
|
3430 |
+ |
for (CompletableFuture<?> future : fs) |
3431 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3432 |
+ |
futures.addAll(fs); |
3433 |
+ |
} |
3434 |
+ |
|
3435 |
+ |
incomplete.complete(v); |
3436 |
+ |
|
3437 |
+ |
for (CompletableFuture<?> future : futures) |
3438 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3439 |
+ |
|
3440 |
+ |
assertEquals(futures.size(), e.count.get()); |
3441 |
+ |
}} |
3442 |
+ |
|
3443 |
+ |
/** |
3444 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3445 |
+ |
* should never be invoked because the dependent future is |
3446 |
+ |
* explicitly completed. |
3447 |
+ |
*/ |
3448 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3449 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3450 |
+ |
{ |
3451 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3452 |
+ |
|
3453 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3454 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3455 |
+ |
|
3456 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3457 |
+ |
|
3458 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3459 |
+ |
srcs.add(complete); |
3460 |
+ |
srcs.add(incomplete); |
3461 |
+ |
|
3462 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3463 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3464 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3465 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3466 |
+ |
|
3467 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3468 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3469 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3470 |
+ |
|
3471 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3472 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3473 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3474 |
+ |
|
3475 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3476 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3477 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3478 |
+ |
|
3479 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3480 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3481 |
+ |
|
3482 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3483 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3484 |
+ |
|
3485 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3486 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3487 |
+ |
|
3488 |
+ |
for (CompletableFuture<?> future : fs) |
3489 |
+ |
checkIncomplete(future); |
3490 |
+ |
|
3491 |
+ |
for (CompletableFuture<?> future : fs) |
3492 |
+ |
future.complete(null); |
3493 |
+ |
|
3494 |
+ |
incomplete.complete(v); |
3495 |
+ |
|
3496 |
+ |
for (CompletableFuture<?> future : fs) |
3497 |
+ |
checkCompletedNormally(future, null); |
3498 |
+ |
|
3499 |
+ |
assertEquals(0, e.count.get()); |
3500 |
+ |
}} |
3501 |
+ |
|
3502 |
+ |
/** |
3503 |
|
* toCompletableFuture returns this CompletableFuture. |
3504 |
|
*/ |
3505 |
|
public void testToCompletableFuture() { |
3513 |
|
* newIncompleteFuture returns an incomplete CompletableFuture |
3514 |
|
*/ |
3515 |
|
public void testNewIncompleteFuture() { |
3516 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3517 |
+ |
{ |
3518 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3519 |
|
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3520 |
|
checkIncomplete(f); |
3521 |
|
checkIncomplete(g); |
3522 |
< |
} |
3522 |
> |
f.complete(v1); |
3523 |
> |
checkCompletedNormally(f, v1); |
3524 |
> |
checkIncomplete(g); |
3525 |
> |
g.complete(v1); |
3526 |
> |
checkCompletedNormally(g, v1); |
3527 |
> |
assertSame(g.getClass(), CompletableFuture.class); |
3528 |
> |
}} |
3529 |
|
|
3530 |
|
/** |
3531 |
|
* completedStage returns a completed CompletionStage |
3532 |
|
*/ |
3533 |
|
public void testCompletedStage() { |
3534 |
< |
AtomicInteger x = new AtomicInteger(); |
3534 |
> |
AtomicInteger x = new AtomicInteger(0); |
3535 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3536 |
|
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3537 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3560 |
|
public void testFailedFuture() { |
3561 |
|
CFException ex = new CFException(); |
3562 |
|
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3563 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
3563 |
> |
checkCompletedExceptionally(f, ex); |
3564 |
|
} |
3565 |
|
|
3566 |
|
/** |
3567 |
|
* failedFuture(null) throws NPE |
3568 |
|
*/ |
3569 |
< |
public void testFailedFuture2() { |
3569 |
> |
public void testFailedFuture_null() { |
3570 |
|
try { |
3571 |
|
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3572 |
|
shouldThrow(); |
3577 |
|
* copy returns a CompletableFuture that is completed normally, |
3578 |
|
* with the same value, when source is. |
3579 |
|
*/ |
3580 |
< |
public void testCopy() { |
3580 |
> |
public void testCopy_normalCompletion() { |
3581 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3582 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3583 |
> |
{ |
3584 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3585 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
3586 |
|
CompletableFuture<Integer> g = f.copy(); |
3587 |
< |
checkIncomplete(f); |
3588 |
< |
checkIncomplete(g); |
3589 |
< |
f.complete(1); |
3590 |
< |
checkCompletedNormally(f, 1); |
3591 |
< |
checkCompletedNormally(g, 1); |
3592 |
< |
} |
3587 |
> |
if (createIncomplete) { |
3588 |
> |
checkIncomplete(f); |
3589 |
> |
checkIncomplete(g); |
3590 |
> |
assertTrue(f.complete(v1)); |
3591 |
> |
} |
3592 |
> |
checkCompletedNormally(f, v1); |
3593 |
> |
checkCompletedNormally(g, v1); |
3594 |
> |
}} |
3595 |
|
|
3596 |
|
/** |
3597 |
|
* copy returns a CompletableFuture that is completed exceptionally |
3598 |
|
* when source is. |
3599 |
|
*/ |
3600 |
< |
public void testCopy2() { |
3600 |
> |
public void testCopy_exceptionalCompletion() { |
3601 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
3602 |
> |
{ |
3603 |
> |
CFException ex = new CFException(); |
3604 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3605 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
3606 |
|
CompletableFuture<Integer> g = f.copy(); |
3607 |
< |
checkIncomplete(f); |
3608 |
< |
checkIncomplete(g); |
3609 |
< |
CFException ex = new CFException(); |
3610 |
< |
f.completeExceptionally(ex); |
3607 |
> |
if (createIncomplete) { |
3608 |
> |
checkIncomplete(f); |
3609 |
> |
checkIncomplete(g); |
3610 |
> |
f.completeExceptionally(ex); |
3611 |
> |
} |
3612 |
|
checkCompletedExceptionally(f, ex); |
3613 |
< |
checkCompletedWithWrappedCFException(g); |
3613 |
> |
checkCompletedWithWrappedException(g, ex); |
3614 |
> |
}} |
3615 |
> |
|
3616 |
> |
/** |
3617 |
> |
* Completion of a copy does not complete its source. |
3618 |
> |
*/ |
3619 |
> |
public void testCopy_oneWayPropagation() { |
3620 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3621 |
> |
assertTrue(f.copy().complete(1)); |
3622 |
> |
assertTrue(f.copy().complete(null)); |
3623 |
> |
assertTrue(f.copy().cancel(true)); |
3624 |
> |
assertTrue(f.copy().cancel(false)); |
3625 |
> |
assertTrue(f.copy().completeExceptionally(new CFException())); |
3626 |
> |
checkIncomplete(f); |
3627 |
|
} |
3628 |
|
|
3629 |
|
/** |
3633 |
|
public void testMinimalCompletionStage() { |
3634 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3635 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3636 |
< |
AtomicInteger x = new AtomicInteger(); |
3636 |
> |
AtomicInteger x = new AtomicInteger(0); |
3637 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3638 |
|
checkIncomplete(f); |
3639 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3650 |
|
public void testMinimalCompletionStage2() { |
3651 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3652 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3653 |
< |
AtomicInteger x = new AtomicInteger(); |
3653 |
> |
AtomicInteger x = new AtomicInteger(0); |
3654 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3655 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3656 |
|
checkIncomplete(f); |
3668 |
|
public void testFailedStage() { |
3669 |
|
CFException ex = new CFException(); |
3670 |
|
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3671 |
< |
AtomicInteger x = new AtomicInteger(); |
3671 |
> |
AtomicInteger x = new AtomicInteger(0); |
3672 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3673 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3674 |
|
assertEquals(x.get(), 0); |
3675 |
< |
assertEquals(r.get().getCause(), ex); |
3675 |
> |
assertEquals(r.get(), ex); |
3676 |
|
} |
3677 |
|
|
3678 |
|
/** |
3679 |
|
* completeAsync completes with value of given supplier |
3680 |
|
*/ |
3681 |
|
public void testCompleteAsync() { |
3682 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3683 |
+ |
{ |
3684 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3685 |
< |
f.completeAsync(() -> 1); |
3685 |
> |
f.completeAsync(() -> v1); |
3686 |
|
f.join(); |
3687 |
< |
checkCompletedNormally(f, 1); |
3688 |
< |
} |
3687 |
> |
checkCompletedNormally(f, v1); |
3688 |
> |
}} |
3689 |
|
|
3690 |
|
/** |
3691 |
|
* completeAsync completes exceptionally if given supplier throws |
3697 |
|
try { |
3698 |
|
f.join(); |
3699 |
|
shouldThrow(); |
3700 |
< |
} catch (Exception success) {} |
3701 |
< |
checkCompletedWithWrappedCFException(f); |
3700 |
> |
} catch (CompletionException success) {} |
3701 |
> |
checkCompletedWithWrappedException(f, ex); |
3702 |
|
} |
3703 |
|
|
3704 |
|
/** |
3705 |
|
* completeAsync with given executor completes with value of given supplier |
3706 |
|
*/ |
3707 |
|
public void testCompleteAsync3() { |
3708 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3709 |
+ |
{ |
3710 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3711 |
< |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3712 |
< |
f.join(); |
3713 |
< |
checkCompletedNormally(f, 1); |
3714 |
< |
} |
3711 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3712 |
> |
f.completeAsync(() -> v1, executor); |
3713 |
> |
assertSame(v1, f.join()); |
3714 |
> |
checkCompletedNormally(f, v1); |
3715 |
> |
assertEquals(1, executor.count.get()); |
3716 |
> |
}} |
3717 |
|
|
3718 |
|
/** |
3719 |
|
* completeAsync with given executor completes exceptionally if |
3722 |
|
public void testCompleteAsync4() { |
3723 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3724 |
|
CFException ex = new CFException(); |
3725 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3725 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3726 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3727 |
|
try { |
3728 |
|
f.join(); |
3729 |
|
shouldThrow(); |
3730 |
< |
} catch (Exception success) {} |
3731 |
< |
checkCompletedWithWrappedCFException(f); |
3730 |
> |
} catch (CompletionException success) {} |
3731 |
> |
checkCompletedWithWrappedException(f, ex); |
3732 |
> |
assertEquals(1, executor.count.get()); |
3733 |
|
} |
3734 |
|
|
3735 |
|
/** |
3736 |
|
* orTimeout completes with TimeoutException if not complete |
3737 |
|
*/ |
3738 |
< |
public void testOrTimeout() { |
3738 |
> |
public void testOrTimeout_timesOut() { |
3739 |
> |
long timeoutMillis = timeoutMillis(); |
3740 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3741 |
< |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3742 |
< |
checkCompletedExceptionallyWithTimeout(f); |
3741 |
> |
long startTime = System.nanoTime(); |
3742 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3743 |
> |
checkCompletedWithTimeoutException(f); |
3744 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3745 |
|
} |
3746 |
|
|
3747 |
|
/** |
3748 |
|
* orTimeout completes normally if completed before timeout |
3749 |
|
*/ |
3750 |
< |
public void testOrTimeout2() { |
3750 |
> |
public void testOrTimeout_completed() { |
3751 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3752 |
> |
{ |
3753 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3754 |
< |
f.complete(1); |
3755 |
< |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3756 |
< |
checkCompletedNormally(f, 1); |
3754 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3755 |
> |
long startTime = System.nanoTime(); |
3756 |
> |
f.complete(v1); |
3757 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3758 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3759 |
> |
g.complete(v1); |
3760 |
> |
checkCompletedNormally(f, v1); |
3761 |
> |
checkCompletedNormally(g, v1); |
3762 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3763 |
> |
}} |
3764 |
> |
|
3765 |
> |
/** |
3766 |
> |
* completeOnTimeout completes with given value if not complete |
3767 |
> |
*/ |
3768 |
> |
public void testCompleteOnTimeout_timesOut() { |
3769 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3770 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3771 |
|
} |
3772 |
|
|
3773 |
|
/** |
3774 |
|
* completeOnTimeout completes with given value if not complete |
3775 |
|
*/ |
3776 |
< |
public void testCompleteOnTimeout() { |
3776 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3777 |
> |
long timeoutMillis = timeoutMillis(); |
3778 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3779 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3780 |
< |
f.join(); |
3781 |
< |
checkCompletedNormally(f, -1); |
3779 |
> |
long startTime = System.nanoTime(); |
3780 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3781 |
> |
assertSame(v, f.join()); |
3782 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3783 |
> |
f.complete(99); // should have no effect |
3784 |
> |
checkCompletedNormally(f, v); |
3785 |
|
} |
3786 |
|
|
3787 |
|
/** |
3788 |
|
* completeOnTimeout has no effect if completed within timeout |
3789 |
|
*/ |
3790 |
< |
public void testCompleteOnTimeout2() { |
3790 |
> |
public void testCompleteOnTimeout_completed() { |
3791 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3792 |
> |
{ |
3793 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3794 |
< |
f.complete(1); |
3795 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3796 |
< |
checkCompletedNormally(f, 1); |
3797 |
< |
} |
3794 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3795 |
> |
long startTime = System.nanoTime(); |
3796 |
> |
f.complete(v1); |
3797 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3798 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3799 |
> |
g.complete(v1); |
3800 |
> |
checkCompletedNormally(f, v1); |
3801 |
> |
checkCompletedNormally(g, v1); |
3802 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3803 |
> |
}} |
3804 |
|
|
3805 |
|
/** |
3806 |
|
* delayedExecutor returns an executor that delays submission |
3807 |
|
*/ |
3808 |
|
public void testDelayedExecutor() { |
3809 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3810 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3811 |
< |
MILLISECONDS); |
3809 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3810 |
> |
() -> testDelayedExecutor(null, 1), |
3811 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3812 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3813 |
> |
} |
3814 |
> |
|
3815 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3816 |
> |
long timeoutMillis = timeoutMillis(); |
3817 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3818 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3819 |
> |
final Executor delayer, longDelayer; |
3820 |
> |
if (executor == null) { |
3821 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3822 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3823 |
> |
} else { |
3824 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3825 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3826 |
> |
} |
3827 |
|
long startTime = System.nanoTime(); |
3828 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3829 |
< |
assertNull(f.getNow(null)); |
3830 |
< |
try { |
3831 |
< |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3832 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3833 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3834 |
< |
checkCompletedNormally(f, 1); |
3835 |
< |
} |
3828 |
> |
CompletableFuture<Integer> f = |
3829 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3830 |
> |
CompletableFuture<Integer> g = |
3831 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3832 |
> |
|
3833 |
> |
assertNull(g.getNow(null)); |
3834 |
> |
|
3835 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3836 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3837 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3838 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3839 |
|
|
3840 |
< |
/** |
3841 |
< |
* delayedExecutor for a given executor returns an executor that |
3842 |
< |
* delays submission |
3843 |
< |
*/ |
3607 |
< |
public void testDelayedExecutor2() { |
3608 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3609 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3610 |
< |
MILLISECONDS, |
3611 |
< |
new ThreadExecutor()); |
3612 |
< |
long startTime = System.nanoTime(); |
3613 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3614 |
< |
assertNull(f.getNow(null)); |
3615 |
< |
try { |
3616 |
< |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3617 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3618 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3619 |
< |
checkCompletedNormally(f, 1); |
3840 |
> |
checkCompletedNormally(f, v); |
3841 |
> |
|
3842 |
> |
checkIncomplete(g); |
3843 |
> |
assertTrue(g.cancel(true)); |
3844 |
|
} |
3845 |
|
|
3846 |
|
//--- tests of implementation details; not part of official tck --- |
3847 |
|
|
3848 |
|
Object resultOf(CompletableFuture<?> f) { |
3849 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3850 |
+ |
if (sm != null) { |
3851 |
+ |
try { |
3852 |
+ |
System.setSecurityManager(null); |
3853 |
+ |
} catch (SecurityException giveUp) { |
3854 |
+ |
return "Reflection not available"; |
3855 |
+ |
} |
3856 |
+ |
} |
3857 |
+ |
|
3858 |
|
try { |
3859 |
|
java.lang.reflect.Field resultField |
3860 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3861 |
|
resultField.setAccessible(true); |
3862 |
|
return resultField.get(f); |
3863 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3863 |
> |
} catch (Throwable t) { |
3864 |
> |
throw new AssertionError(t); |
3865 |
> |
} finally { |
3866 |
> |
if (sm != null) System.setSecurityManager(sm); |
3867 |
> |
} |
3868 |
|
} |
3869 |
|
|
3870 |
|
public void testExceptionPropagationReusesResultObject() { |
3875 |
|
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3876 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3877 |
|
|
3878 |
+ |
final Runnable noopRunnable = new Noop(m); |
3879 |
+ |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3880 |
+ |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3881 |
+ |
|
3882 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3883 |
|
= new ArrayList<>(); |
3884 |
|
|
3885 |
< |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3886 |
< |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3887 |
< |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3888 |
< |
|
3889 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3890 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3891 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3885 |
> |
funs.add((y) -> m.thenRun(y, noopRunnable)); |
3886 |
> |
funs.add((y) -> m.thenAccept(y, noopConsumer)); |
3887 |
> |
funs.add((y) -> m.thenApply(y, incFunction)); |
3888 |
> |
|
3889 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable)); |
3890 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer)); |
3891 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, incFunction)); |
3892 |
|
|
3893 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3893 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable)); |
3894 |
> |
funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable)); |
3895 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3896 |
+ |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3897 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3898 |
+ |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3899 |
|
|
3900 |
< |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3900 |
> |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3901 |
|
|
3902 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3903 |
|
|
3904 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3905 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3904 |
> |
funs.add((y) -> CompletableFuture.allOf(y)); |
3905 |
> |
funs.add((y) -> CompletableFuture.allOf(y, v42)); |
3906 |
> |
funs.add((y) -> CompletableFuture.allOf(v42, y)); |
3907 |
> |
funs.add((y) -> CompletableFuture.anyOf(y)); |
3908 |
> |
funs.add((y) -> CompletableFuture.anyOf(y, incomplete)); |
3909 |
> |
funs.add((y) -> CompletableFuture.anyOf(incomplete, y)); |
3910 |
|
|
3911 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3912 |
|
fun : funs) { |
3913 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3914 |
|
f.completeExceptionally(ex); |
3915 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3915 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3916 |
|
checkCompletedWithWrappedException(src, ex); |
3917 |
|
CompletableFuture<?> dep = fun.apply(src); |
3918 |
|
checkCompletedWithWrappedException(dep, ex); |
3922 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3923 |
|
fun : funs) { |
3924 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3925 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3925 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3926 |
|
CompletableFuture<?> dep = fun.apply(src); |
3927 |
|
f.completeExceptionally(ex); |
3928 |
|
checkCompletedWithWrappedException(src, ex); |
3936 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3937 |
|
f.cancel(mayInterruptIfRunning); |
3938 |
|
checkCancelled(f); |
3939 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3939 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3940 |
|
checkCompletedWithWrappedCancellationException(src); |
3941 |
|
CompletableFuture<?> dep = fun.apply(src); |
3942 |
|
checkCompletedWithWrappedCancellationException(dep); |
3947 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3948 |
|
fun : funs) { |
3949 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3950 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3950 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3951 |
|
CompletableFuture<?> dep = fun.apply(src); |
3952 |
|
f.cancel(mayInterruptIfRunning); |
3953 |
|
checkCancelled(f); |
3957 |
|
} |
3958 |
|
}} |
3959 |
|
|
3960 |
+ |
/** |
3961 |
+ |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3962 |
+ |
*/ |
3963 |
+ |
public void testMinimalCompletionStage_minimality() { |
3964 |
+ |
if (!testImplementationDetails) return; |
3965 |
+ |
Function<Method, String> toSignature = |
3966 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3967 |
+ |
Predicate<Method> isNotStatic = |
3968 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3969 |
+ |
List<Method> minimalMethods = |
3970 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3971 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3972 |
+ |
.filter(isNotStatic) |
3973 |
+ |
.collect(Collectors.toList()); |
3974 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3975 |
+ |
String[] signatureWhitelist = { |
3976 |
+ |
"newIncompleteFuture[]", |
3977 |
+ |
"defaultExecutor[]", |
3978 |
+ |
"minimalCompletionStage[]", |
3979 |
+ |
"copy[]", |
3980 |
+ |
}; |
3981 |
+ |
Set<String> permittedMethodSignatures = |
3982 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3983 |
+ |
Stream.of(signatureWhitelist)) |
3984 |
+ |
.collect(Collectors.toSet()); |
3985 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3986 |
+ |
.filter(isNotStatic) |
3987 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3988 |
+ |
.collect(Collectors.toList()); |
3989 |
+ |
|
3990 |
+ |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3991 |
+ |
CompletionStage<Integer> min = |
3992 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3993 |
+ |
stages.add(min); |
3994 |
+ |
stages.add(min.thenApply(x -> x)); |
3995 |
+ |
stages.add(CompletableFuture.completedStage(1)); |
3996 |
+ |
stages.add(CompletableFuture.failedStage(new CFException())); |
3997 |
+ |
|
3998 |
+ |
List<Method> bugs = new ArrayList<>(); |
3999 |
+ |
for (Method method : allMethods) { |
4000 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
4001 |
+ |
Object[] args = new Object[parameterTypes.length]; |
4002 |
+ |
// Manufacture boxed primitives for primitive params |
4003 |
+ |
for (int i = 0; i < args.length; i++) { |
4004 |
+ |
Class<?> type = parameterTypes[i]; |
4005 |
+ |
if (parameterTypes[i] == boolean.class) |
4006 |
+ |
args[i] = false; |
4007 |
+ |
else if (parameterTypes[i] == int.class) |
4008 |
+ |
args[i] = 0; |
4009 |
+ |
else if (parameterTypes[i] == long.class) |
4010 |
+ |
args[i] = 0L; |
4011 |
+ |
} |
4012 |
+ |
for (CompletionStage<Integer> stage : stages) { |
4013 |
+ |
try { |
4014 |
+ |
method.invoke(stage, args); |
4015 |
+ |
bugs.add(method); |
4016 |
+ |
} |
4017 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
4018 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
4019 |
+ |
bugs.add(method); |
4020 |
+ |
// expected.getCause().printStackTrace(); |
4021 |
+ |
} |
4022 |
+ |
} |
4023 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4024 |
+ |
} |
4025 |
+ |
} |
4026 |
+ |
if (!bugs.isEmpty()) |
4027 |
+ |
throw new Error("Methods did not throw UOE: " + bugs); |
4028 |
+ |
} |
4029 |
+ |
|
4030 |
+ |
/** |
4031 |
+ |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4032 |
+ |
* is completed normally, with the same value, when source is. |
4033 |
+ |
*/ |
4034 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() { |
4035 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4036 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4037 |
+ |
{ |
4038 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4039 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4040 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4041 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4042 |
+ |
if (createIncomplete) { |
4043 |
+ |
checkIncomplete(f); |
4044 |
+ |
checkIncomplete(g); |
4045 |
+ |
assertTrue(f.complete(v1)); |
4046 |
+ |
} |
4047 |
+ |
checkCompletedNormally(f, v1); |
4048 |
+ |
checkCompletedNormally(g, v1); |
4049 |
+ |
}} |
4050 |
+ |
|
4051 |
+ |
/** |
4052 |
+ |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
4053 |
+ |
* is completed exceptionally when source is. |
4054 |
+ |
*/ |
4055 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() { |
4056 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4057 |
+ |
{ |
4058 |
+ |
CFException ex = new CFException(); |
4059 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4060 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4061 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
4062 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4063 |
+ |
if (createIncomplete) { |
4064 |
+ |
checkIncomplete(f); |
4065 |
+ |
checkIncomplete(g); |
4066 |
+ |
f.completeExceptionally(ex); |
4067 |
+ |
} |
4068 |
+ |
checkCompletedExceptionally(f, ex); |
4069 |
+ |
checkCompletedWithWrappedException(g, ex); |
4070 |
+ |
}} |
4071 |
+ |
|
4072 |
+ |
/** |
4073 |
+ |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4074 |
+ |
*/ |
4075 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4076 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4077 |
+ |
{ |
4078 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4079 |
+ |
CompletionStage minimal = f.minimalCompletionStage(); |
4080 |
+ |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4081 |
+ |
assertTrue(g.complete(v1)); |
4082 |
+ |
checkCompletedNormally(g, v1); |
4083 |
+ |
checkIncomplete(f); |
4084 |
+ |
checkIncomplete(minimal.toCompletableFuture()); |
4085 |
+ |
}} |
4086 |
+ |
|
4087 |
+ |
/** |
4088 |
+ |
* minimalStage.toCompletableFuture().join() awaits completion |
4089 |
+ |
*/ |
4090 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4091 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4092 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4093 |
+ |
{ |
4094 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4095 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4096 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4097 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4098 |
+ |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4099 |
+ |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4100 |
+ |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4101 |
+ |
}} |
4102 |
+ |
|
4103 |
+ |
/** |
4104 |
+ |
* Completion of a toCompletableFuture copy of a minimal stage |
4105 |
+ |
* does not complete its source. |
4106 |
+ |
*/ |
4107 |
+ |
public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() { |
4108 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4109 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
4110 |
+ |
assertTrue(g.toCompletableFuture().complete(1)); |
4111 |
+ |
assertTrue(g.toCompletableFuture().complete(null)); |
4112 |
+ |
assertTrue(g.toCompletableFuture().cancel(true)); |
4113 |
+ |
assertTrue(g.toCompletableFuture().cancel(false)); |
4114 |
+ |
assertTrue(g.toCompletableFuture().completeExceptionally(new CFException())); |
4115 |
+ |
checkIncomplete(g.toCompletableFuture()); |
4116 |
+ |
f.complete(1); |
4117 |
+ |
checkCompletedNormally(g.toCompletableFuture(), 1); |
4118 |
+ |
} |
4119 |
+ |
|
4120 |
+ |
/** Demo utility method for external reliable toCompletableFuture */ |
4121 |
+ |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4122 |
+ |
CompletableFuture<T> f = new CompletableFuture<>(); |
4123 |
+ |
stage.handle((T t, Throwable ex) -> { |
4124 |
+ |
if (ex != null) f.completeExceptionally(ex); |
4125 |
+ |
else f.complete(t); |
4126 |
+ |
return null; |
4127 |
+ |
}); |
4128 |
+ |
return f; |
4129 |
+ |
} |
4130 |
+ |
|
4131 |
+ |
/** Demo utility method to join a CompletionStage */ |
4132 |
+ |
static <T> T join(CompletionStage<T> stage) { |
4133 |
+ |
return toCompletableFuture(stage).join(); |
4134 |
+ |
} |
4135 |
+ |
|
4136 |
+ |
/** |
4137 |
+ |
* Joining a minimal stage "by hand" works |
4138 |
+ |
*/ |
4139 |
+ |
public void testMinimalCompletionStage_join_by_hand() { |
4140 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
4141 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
4142 |
+ |
{ |
4143 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4144 |
+ |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4145 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4146 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4147 |
+ |
minimal.thenAccept((x) -> g.complete(x)); |
4148 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
4149 |
+ |
g.join(); |
4150 |
+ |
checkCompletedNormally(g, v1); |
4151 |
+ |
checkCompletedNormally(f, v1); |
4152 |
+ |
assertEquals(v1, join(minimal)); |
4153 |
+ |
}} |
4154 |
+ |
|
4155 |
+ |
static class Monad { |
4156 |
+ |
static class ZeroException extends RuntimeException { |
4157 |
+ |
public ZeroException() { super("monadic zero"); } |
4158 |
+ |
} |
4159 |
+ |
// "return", "unit" |
4160 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
4161 |
+ |
return completedFuture(value); |
4162 |
+ |
} |
4163 |
+ |
// monadic zero ? |
4164 |
+ |
static <T> CompletableFuture<T> zero() { |
4165 |
+ |
return failedFuture(new ZeroException()); |
4166 |
+ |
} |
4167 |
+ |
// >=> |
4168 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
4169 |
+ |
(Function<T, CompletableFuture<U>> f, |
4170 |
+ |
Function<U, CompletableFuture<V>> g) { |
4171 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
4172 |
+ |
} |
4173 |
+ |
|
4174 |
+ |
static void assertZero(CompletableFuture<?> f) { |
4175 |
+ |
try { |
4176 |
+ |
f.getNow(null); |
4177 |
+ |
throw new AssertionFailedError("should throw"); |
4178 |
+ |
} catch (CompletionException success) { |
4179 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
4180 |
+ |
} |
4181 |
+ |
} |
4182 |
+ |
|
4183 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
4184 |
+ |
CompletableFuture<T> g) { |
4185 |
+ |
T fval = null, gval = null; |
4186 |
+ |
Throwable fex = null, gex = null; |
4187 |
+ |
|
4188 |
+ |
try { fval = f.get(); } |
4189 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
4190 |
+ |
catch (Throwable ex) { fex = ex; } |
4191 |
+ |
|
4192 |
+ |
try { gval = g.get(); } |
4193 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
4194 |
+ |
catch (Throwable ex) { gex = ex; } |
4195 |
+ |
|
4196 |
+ |
if (fex != null || gex != null) |
4197 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
4198 |
+ |
else |
4199 |
+ |
assertEquals(fval, gval); |
4200 |
+ |
} |
4201 |
+ |
|
4202 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
4203 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
4204 |
+ |
} |
4205 |
+ |
|
4206 |
+ |
/** Implements "monadic plus". */ |
4207 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
4208 |
+ |
CompletableFuture<? extends T> g) { |
4209 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
4210 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
4211 |
+ |
try { |
4212 |
+ |
if (ex == null) { |
4213 |
+ |
if (plus.complete(result)) |
4214 |
+ |
if (plus.firstFailure.get() != null) |
4215 |
+ |
plus.firstFailure.set(null); |
4216 |
+ |
} |
4217 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
4218 |
+ |
if (plus.isDone()) |
4219 |
+ |
plus.firstFailure.set(null); |
4220 |
+ |
} |
4221 |
+ |
else { |
4222 |
+ |
// first failure has precedence |
4223 |
+ |
Throwable first = plus.firstFailure.getAndSet(null); |
4224 |
+ |
|
4225 |
+ |
// may fail with "Self-suppression not permitted" |
4226 |
+ |
try { first.addSuppressed(ex); } |
4227 |
+ |
catch (Exception ignored) {} |
4228 |
+ |
|
4229 |
+ |
plus.completeExceptionally(first); |
4230 |
+ |
} |
4231 |
+ |
} catch (Throwable unexpected) { |
4232 |
+ |
plus.completeExceptionally(unexpected); |
4233 |
+ |
} |
4234 |
+ |
}; |
4235 |
+ |
f.whenComplete(action); |
4236 |
+ |
g.whenComplete(action); |
4237 |
+ |
return plus; |
4238 |
+ |
} |
4239 |
+ |
} |
4240 |
+ |
|
4241 |
+ |
/** |
4242 |
+ |
* CompletableFuture is an additive monad - sort of. |
4243 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
4244 |
+ |
*/ |
4245 |
+ |
public void testAdditiveMonad() throws Throwable { |
4246 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
4247 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
4248 |
+ |
|
4249 |
+ |
// Some mutually non-commutative functions |
4250 |
+ |
Function<Long, CompletableFuture<Long>> triple |
4251 |
+ |
= (x) -> Monad.unit(3 * x); |
4252 |
+ |
Function<Long, CompletableFuture<Long>> inc |
4253 |
+ |
= (x) -> Monad.unit(x + 1); |
4254 |
+ |
|
4255 |
+ |
// unit is a right identity: m >>= unit === m |
4256 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
4257 |
+ |
inc.apply(5L)); |
4258 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
4259 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
4260 |
+ |
inc.apply(5L)); |
4261 |
+ |
|
4262 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
4263 |
+ |
Monad.assertFutureEquals( |
4264 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
4265 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
4266 |
+ |
|
4267 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
4268 |
+ |
|
4269 |
+ |
// zero is a monadic zero |
4270 |
+ |
Monad.assertZero(zero); |
4271 |
+ |
|
4272 |
+ |
// left zero: zero >>= f === zero |
4273 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
4274 |
+ |
// right zero: f >>= (\x -> zero) === zero |
4275 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
4276 |
+ |
|
4277 |
+ |
// f plus zero === f |
4278 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4279 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
4280 |
+ |
// zero plus f === f |
4281 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4282 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
4283 |
+ |
// zero plus zero === zero |
4284 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
4285 |
+ |
{ |
4286 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
4287 |
+ |
Monad.unit(8L)); |
4288 |
+ |
// non-determinism |
4289 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
4290 |
+ |
} |
4291 |
+ |
|
4292 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
4293 |
+ |
// f plus godot === f (doesn't wait for godot) |
4294 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4295 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
4296 |
+ |
// godot plus f === f (doesn't wait for godot) |
4297 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
4298 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
4299 |
+ |
} |
4300 |
+ |
|
4301 |
+ |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4302 |
+ |
public void testRecursiveChains() throws Throwable { |
4303 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
4304 |
+ |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4305 |
+ |
{ |
4306 |
+ |
final int val = 42; |
4307 |
+ |
final int n = expensiveTests ? 1_000 : 2; |
4308 |
+ |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4309 |
+ |
CompletableFuture<Integer> tail = head; |
4310 |
+ |
for (int i = 0; i < n; i++) { |
4311 |
+ |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4312 |
+ |
tail = m.thenApply(tail, v -> v + 1); |
4313 |
+ |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4314 |
+ |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4315 |
+ |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4316 |
+ |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4317 |
+ |
} |
4318 |
+ |
head.complete(val); |
4319 |
+ |
assertEquals(val + 3 * n, (int) tail.join()); |
4320 |
+ |
}} |
4321 |
+ |
|
4322 |
+ |
/** |
4323 |
+ |
* A single CompletableFuture with many dependents. |
4324 |
+ |
* A demo of scalability - runtime is O(n). |
4325 |
+ |
*/ |
4326 |
+ |
public void testManyDependents() throws Throwable { |
4327 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4328 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4329 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4330 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
4331 |
+ |
for (int i = 0; i < n; i++) { |
4332 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
4333 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
4334 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
4335 |
+ |
|
4336 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4337 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4338 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4339 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4340 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4341 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4342 |
+ |
|
4343 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4344 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4345 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4346 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4347 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4348 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4349 |
+ |
} |
4350 |
+ |
head.complete(null); |
4351 |
+ |
assertEquals(5 * 3 * n, count.get()); |
4352 |
+ |
} |
4353 |
+ |
|
4354 |
+ |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4355 |
+ |
public void testCoCompletionGarbageRetention() throws Throwable { |
4356 |
+ |
final int n = expensiveTests ? 1_000_000 : 10; |
4357 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4358 |
+ |
CompletableFuture<Integer> f; |
4359 |
+ |
for (int i = 0; i < n; i++) { |
4360 |
+ |
f = new CompletableFuture<>(); |
4361 |
+ |
f.runAfterEither(incomplete, () -> {}); |
4362 |
+ |
f.complete(null); |
4363 |
+ |
|
4364 |
+ |
f = new CompletableFuture<>(); |
4365 |
+ |
f.acceptEither(incomplete, (x) -> {}); |
4366 |
+ |
f.complete(null); |
4367 |
+ |
|
4368 |
+ |
f = new CompletableFuture<>(); |
4369 |
+ |
f.applyToEither(incomplete, (x) -> x); |
4370 |
+ |
f.complete(null); |
4371 |
+ |
|
4372 |
+ |
f = new CompletableFuture<>(); |
4373 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4374 |
+ |
f.complete(null); |
4375 |
+ |
} |
4376 |
+ |
|
4377 |
+ |
for (int i = 0; i < n; i++) { |
4378 |
+ |
f = new CompletableFuture<>(); |
4379 |
+ |
incomplete.runAfterEither(f, () -> {}); |
4380 |
+ |
f.complete(null); |
4381 |
+ |
|
4382 |
+ |
f = new CompletableFuture<>(); |
4383 |
+ |
incomplete.acceptEither(f, (x) -> {}); |
4384 |
+ |
f.complete(null); |
4385 |
+ |
|
4386 |
+ |
f = new CompletableFuture<>(); |
4387 |
+ |
incomplete.applyToEither(f, (x) -> x); |
4388 |
+ |
f.complete(null); |
4389 |
+ |
|
4390 |
+ |
f = new CompletableFuture<>(); |
4391 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4392 |
+ |
f.complete(null); |
4393 |
+ |
} |
4394 |
+ |
} |
4395 |
+ |
|
4396 |
+ |
/** |
4397 |
+ |
* Reproduction recipe for: |
4398 |
+ |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4399 |
+ |
* cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A |
4400 |
+ |
*/ |
4401 |
+ |
public void testAnyOfGarbageRetention() throws Throwable { |
4402 |
+ |
for (Integer v : new Integer[] { 1, null }) |
4403 |
+ |
{ |
4404 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4405 |
+ |
CompletableFuture<Integer>[] fs |
4406 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4407 |
+ |
for (int i = 0; i < fs.length; i++) |
4408 |
+ |
fs[i] = new CompletableFuture<>(); |
4409 |
+ |
fs[fs.length - 1].complete(v); |
4410 |
+ |
for (int i = 0; i < n; i++) |
4411 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4412 |
+ |
}} |
4413 |
+ |
|
4414 |
+ |
/** |
4415 |
+ |
* Checks for garbage retention with allOf. |
4416 |
+ |
* |
4417 |
+ |
* As of 2016-07, fails with OOME: |
4418 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4419 |
+ |
*/ |
4420 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4421 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4422 |
+ |
CompletableFuture<Integer>[] fs |
4423 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4424 |
+ |
for (int i = 0; i < fs.length; i++) |
4425 |
+ |
fs[i] = new CompletableFuture<>(); |
4426 |
+ |
for (int i = 0; i < n; i++) |
4427 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4428 |
+ |
} |
4429 |
+ |
|
4430 |
+ |
/** |
4431 |
+ |
* Checks for garbage retention when a dependent future is |
4432 |
+ |
* cancelled and garbage-collected. |
4433 |
+ |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4434 |
+ |
* |
4435 |
+ |
* As of 2016-07, fails with OOME: |
4436 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
4437 |
+ |
*/ |
4438 |
+ |
public void testCancelledGarbageRetention() throws Throwable { |
4439 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4440 |
+ |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4441 |
+ |
for (int i = 0; i < n; i++) |
4442 |
+ |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4443 |
+ |
} |
4444 |
+ |
|
4445 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
4446 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4447 |
+ |
// stage.whenComplete((v, ex) -> { |
4448 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4449 |
+ |
// }); |
4450 |
+ |
// return f.join(); |
4451 |
+ |
// } |
4452 |
+ |
|
4453 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
4454 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4455 |
+ |
// stage.whenComplete((v, ex) -> { |
4456 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4457 |
+ |
// }); |
4458 |
+ |
// return f.isDone(); |
4459 |
+ |
// } |
4460 |
+ |
|
4461 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
4462 |
+ |
// return stage.toCompletableFuture().copy().join(); |
4463 |
+ |
// } |
4464 |
+ |
|
4465 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
4466 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
4467 |
+ |
// } |
4468 |
+ |
|
4469 |
|
} |