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; |
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 |
|
|
840 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
841 |
|
final CompletableFuture<Integer> g = f.exceptionally |
842 |
|
((Throwable t) -> { |
911 |
– |
// Should not be called |
843 |
|
a.getAndIncrement(); |
844 |
< |
throw new AssertionError(); |
844 |
> |
threadFail("should not be called"); |
845 |
> |
return null; // unreached |
846 |
|
}); |
847 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
848 |
|
|
901 |
|
* whenComplete action executes on normal completion, propagating |
902 |
|
* source result. |
903 |
|
*/ |
904 |
< |
public void testWhenComplete_normalCompletion1() { |
904 |
> |
public void testWhenComplete_normalCompletion() { |
905 |
|
for (ExecutionMode m : ExecutionMode.values()) |
906 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
907 |
|
for (Integer v1 : new Integer[] { 1, null }) |
3074 |
|
for (int i = 0; i < k; i++) { |
3075 |
|
checkIncomplete(f); |
3076 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3077 |
< |
if (i != k/2) { |
3077 |
> |
if (i != k / 2) { |
3078 |
|
fs[i].complete(i); |
3079 |
|
checkCompletedNormally(fs[i], i); |
3080 |
|
} else { |
3284 |
|
|
3285 |
|
() -> f.orTimeout(1L, null), |
3286 |
|
() -> f.completeOnTimeout(42, 1L, null), |
3287 |
+ |
|
3288 |
+ |
() -> CompletableFuture.failedFuture(null), |
3289 |
+ |
() -> CompletableFuture.failedStage(null), |
3290 |
|
}; |
3291 |
|
|
3292 |
|
assertThrows(NullPointerException.class, throwingActions); |
3393 |
|
CFException ex = new CFException(); |
3394 |
|
f.completeExceptionally(ex); |
3395 |
|
checkCompletedExceptionally(f, ex); |
3396 |
< |
checkCompletedWithWrappedCFException(g); |
3396 |
> |
checkCompletedWithWrappedException(g, ex); |
3397 |
|
} |
3398 |
|
|
3399 |
|
/** |
3449 |
|
* completeAsync completes with value of given supplier |
3450 |
|
*/ |
3451 |
|
public void testCompleteAsync() { |
3452 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3453 |
+ |
{ |
3454 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3455 |
< |
f.completeAsync(() -> 1); |
3455 |
> |
f.completeAsync(() -> v1); |
3456 |
|
f.join(); |
3457 |
< |
checkCompletedNormally(f, 1); |
3458 |
< |
} |
3457 |
> |
checkCompletedNormally(f, v1); |
3458 |
> |
}} |
3459 |
|
|
3460 |
|
/** |
3461 |
|
* completeAsync completes exceptionally if given supplier throws |
3467 |
|
try { |
3468 |
|
f.join(); |
3469 |
|
shouldThrow(); |
3470 |
< |
} catch (Exception success) {} |
3471 |
< |
checkCompletedWithWrappedCFException(f); |
3470 |
> |
} catch (CompletionException success) {} |
3471 |
> |
checkCompletedWithWrappedException(f, ex); |
3472 |
|
} |
3473 |
|
|
3474 |
|
/** |
3475 |
|
* completeAsync with given executor completes with value of given supplier |
3476 |
|
*/ |
3477 |
|
public void testCompleteAsync3() { |
3478 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3479 |
+ |
{ |
3480 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3481 |
< |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3482 |
< |
f.join(); |
3483 |
< |
checkCompletedNormally(f, 1); |
3484 |
< |
} |
3481 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3482 |
> |
f.completeAsync(() -> v1, executor); |
3483 |
> |
assertSame(v1, f.join()); |
3484 |
> |
checkCompletedNormally(f, v1); |
3485 |
> |
assertEquals(1, executor.count.get()); |
3486 |
> |
}} |
3487 |
|
|
3488 |
|
/** |
3489 |
|
* completeAsync with given executor completes exceptionally if |
3492 |
|
public void testCompleteAsync4() { |
3493 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3494 |
|
CFException ex = new CFException(); |
3495 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3495 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3496 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3497 |
|
try { |
3498 |
|
f.join(); |
3499 |
|
shouldThrow(); |
3500 |
< |
} catch (Exception success) {} |
3501 |
< |
checkCompletedWithWrappedCFException(f); |
3500 |
> |
} catch (CompletionException success) {} |
3501 |
> |
checkCompletedWithWrappedException(f, ex); |
3502 |
> |
assertEquals(1, executor.count.get()); |
3503 |
|
} |
3504 |
|
|
3505 |
|
/** |
3506 |
|
* orTimeout completes with TimeoutException if not complete |
3507 |
|
*/ |
3508 |
< |
public void testOrTimeout() { |
3508 |
> |
public void testOrTimeout_timesOut() { |
3509 |
> |
long timeoutMillis = timeoutMillis(); |
3510 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3511 |
< |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3512 |
< |
checkCompletedExceptionallyWithTimeout(f); |
3511 |
> |
long startTime = System.nanoTime(); |
3512 |
> |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3513 |
> |
checkCompletedWithTimeoutException(f); |
3514 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3515 |
|
} |
3516 |
|
|
3517 |
|
/** |
3518 |
|
* orTimeout completes normally if completed before timeout |
3519 |
|
*/ |
3520 |
< |
public void testOrTimeout2() { |
3520 |
> |
public void testOrTimeout_completed() { |
3521 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3522 |
> |
{ |
3523 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3524 |
< |
f.complete(1); |
3525 |
< |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3526 |
< |
checkCompletedNormally(f, 1); |
3527 |
< |
} |
3524 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3525 |
> |
long startTime = System.nanoTime(); |
3526 |
> |
f.complete(v1); |
3527 |
> |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3528 |
> |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3529 |
> |
g.complete(v1); |
3530 |
> |
checkCompletedNormally(f, v1); |
3531 |
> |
checkCompletedNormally(g, v1); |
3532 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3533 |
> |
}} |
3534 |
|
|
3535 |
|
/** |
3536 |
|
* completeOnTimeout completes with given value if not complete |
3537 |
|
*/ |
3538 |
< |
public void testCompleteOnTimeout() { |
3538 |
> |
public void testCompleteOnTimeout_timesOut() { |
3539 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3540 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3541 |
> |
} |
3542 |
> |
|
3543 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3544 |
> |
long timeoutMillis = timeoutMillis(); |
3545 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3546 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3547 |
< |
f.join(); |
3548 |
< |
checkCompletedNormally(f, -1); |
3546 |
> |
long startTime = System.nanoTime(); |
3547 |
> |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3548 |
> |
assertSame(v, f.join()); |
3549 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3550 |
> |
f.complete(99); // should have no effect |
3551 |
> |
checkCompletedNormally(f, v); |
3552 |
|
} |
3553 |
|
|
3554 |
|
/** |
3555 |
|
* completeOnTimeout has no effect if completed within timeout |
3556 |
|
*/ |
3557 |
< |
public void testCompleteOnTimeout2() { |
3557 |
> |
public void testCompleteOnTimeout_completed() { |
3558 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3559 |
> |
{ |
3560 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3561 |
< |
f.complete(1); |
3562 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3563 |
< |
checkCompletedNormally(f, 1); |
3564 |
< |
} |
3561 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3562 |
> |
long startTime = System.nanoTime(); |
3563 |
> |
f.complete(v1); |
3564 |
> |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3565 |
> |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3566 |
> |
g.complete(v1); |
3567 |
> |
checkCompletedNormally(f, v1); |
3568 |
> |
checkCompletedNormally(g, v1); |
3569 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3570 |
> |
}} |
3571 |
|
|
3572 |
|
/** |
3573 |
|
* delayedExecutor returns an executor that delays submission |
3574 |
|
*/ |
3575 |
< |
public void testDelayedExecutor() throws Exception { |
3576 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3577 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3578 |
< |
MILLISECONDS); |
3575 |
> |
public void testDelayedExecutor() { |
3576 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3577 |
> |
() -> testDelayedExecutor(null, 1), |
3578 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3579 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3580 |
> |
} |
3581 |
> |
|
3582 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3583 |
> |
long timeoutMillis = timeoutMillis(); |
3584 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3585 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3586 |
> |
final Executor delayer, longDelayer; |
3587 |
> |
if (executor == null) { |
3588 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3589 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3590 |
> |
} else { |
3591 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3592 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3593 |
> |
} |
3594 |
|
long startTime = System.nanoTime(); |
3595 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3596 |
< |
assertNull(f.getNow(null)); |
3597 |
< |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3598 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3599 |
< |
checkCompletedNormally(f, 1); |
3600 |
< |
} |
3595 |
> |
CompletableFuture<Integer> f = |
3596 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3597 |
> |
CompletableFuture<Integer> g = |
3598 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3599 |
> |
|
3600 |
> |
assertNull(g.getNow(null)); |
3601 |
> |
|
3602 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3603 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3604 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3605 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3606 |
|
|
3607 |
< |
/** |
3608 |
< |
* delayedExecutor for a given executor returns an executor that |
3609 |
< |
* delays submission |
3610 |
< |
*/ |
3620 |
< |
public void testDelayedExecutor2() throws Exception { |
3621 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3622 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3623 |
< |
MILLISECONDS, |
3624 |
< |
new ThreadExecutor()); |
3625 |
< |
long startTime = System.nanoTime(); |
3626 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3627 |
< |
assertNull(f.getNow(null)); |
3628 |
< |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3629 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3630 |
< |
checkCompletedNormally(f, 1); |
3607 |
> |
checkCompletedNormally(f, v); |
3608 |
> |
|
3609 |
> |
checkIncomplete(g); |
3610 |
> |
assertTrue(g.cancel(true)); |
3611 |
|
} |
3612 |
|
|
3613 |
|
//--- tests of implementation details; not part of official tck --- |
3700 |
|
} |
3701 |
|
}} |
3702 |
|
|
3703 |
+ |
/** |
3704 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3705 |
+ |
*/ |
3706 |
+ |
public void testMinimalCompletionStage_minimality() { |
3707 |
+ |
if (!testImplementationDetails) return; |
3708 |
+ |
Function<Method, String> toSignature = |
3709 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3710 |
+ |
Predicate<Method> isNotStatic = |
3711 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3712 |
+ |
List<Method> minimalMethods = |
3713 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3714 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3715 |
+ |
.filter(isNotStatic) |
3716 |
+ |
.collect(Collectors.toList()); |
3717 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3718 |
+ |
String[] signatureWhitelist = { |
3719 |
+ |
"newIncompleteFuture[]", |
3720 |
+ |
"defaultExecutor[]", |
3721 |
+ |
"minimalCompletionStage[]", |
3722 |
+ |
"copy[]", |
3723 |
+ |
}; |
3724 |
+ |
Set<String> permittedMethodSignatures = |
3725 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3726 |
+ |
Stream.of(signatureWhitelist)) |
3727 |
+ |
.collect(Collectors.toSet()); |
3728 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3729 |
+ |
.filter(isNotStatic) |
3730 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3731 |
+ |
.collect(Collectors.toList()); |
3732 |
+ |
|
3733 |
+ |
CompletionStage<Integer> minimalStage = |
3734 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3735 |
+ |
|
3736 |
+ |
List<Method> bugs = new ArrayList<>(); |
3737 |
+ |
for (Method method : allMethods) { |
3738 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3739 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3740 |
+ |
// Manufacture boxed primitives for primitive params |
3741 |
+ |
for (int i = 0; i < args.length; i++) { |
3742 |
+ |
Class<?> type = parameterTypes[i]; |
3743 |
+ |
if (parameterTypes[i] == boolean.class) |
3744 |
+ |
args[i] = false; |
3745 |
+ |
else if (parameterTypes[i] == int.class) |
3746 |
+ |
args[i] = 0; |
3747 |
+ |
else if (parameterTypes[i] == long.class) |
3748 |
+ |
args[i] = 0L; |
3749 |
+ |
} |
3750 |
+ |
try { |
3751 |
+ |
method.invoke(minimalStage, args); |
3752 |
+ |
bugs.add(method); |
3753 |
+ |
} |
3754 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3755 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3756 |
+ |
bugs.add(method); |
3757 |
+ |
// expected.getCause().printStackTrace(); |
3758 |
+ |
} |
3759 |
+ |
} |
3760 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3761 |
+ |
} |
3762 |
+ |
if (!bugs.isEmpty()) |
3763 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3764 |
+ |
} |
3765 |
+ |
|
3766 |
+ |
static class Monad { |
3767 |
+ |
static class MonadError extends Error { |
3768 |
+ |
public MonadError() { super("monadic zero"); } |
3769 |
+ |
} |
3770 |
+ |
// "return", "unit" |
3771 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
3772 |
+ |
return completedFuture(value); |
3773 |
+ |
} |
3774 |
+ |
// monadic zero ? |
3775 |
+ |
static <T> CompletableFuture<T> zero() { |
3776 |
+ |
return failedFuture(new MonadError()); |
3777 |
+ |
} |
3778 |
+ |
// >=> |
3779 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3780 |
+ |
(Function<T, CompletableFuture<U>> f, |
3781 |
+ |
Function<U, CompletableFuture<V>> g) { |
3782 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
3783 |
+ |
} |
3784 |
+ |
|
3785 |
+ |
static void assertZero(CompletableFuture<?> f) { |
3786 |
+ |
try { |
3787 |
+ |
f.getNow(null); |
3788 |
+ |
throw new AssertionFailedError("should throw"); |
3789 |
+ |
} catch (CompletionException success) { |
3790 |
+ |
assertTrue(success.getCause() instanceof MonadError); |
3791 |
+ |
} |
3792 |
+ |
} |
3793 |
+ |
|
3794 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
3795 |
+ |
CompletableFuture<T> g) { |
3796 |
+ |
T fval = null, gval = null; |
3797 |
+ |
Throwable fex = null, gex = null; |
3798 |
+ |
|
3799 |
+ |
try { fval = f.get(); } |
3800 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
3801 |
+ |
catch (Throwable ex) { fex = ex; } |
3802 |
+ |
|
3803 |
+ |
try { gval = g.get(); } |
3804 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
3805 |
+ |
catch (Throwable ex) { gex = ex; } |
3806 |
+ |
|
3807 |
+ |
if (fex != null || gex != null) |
3808 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
3809 |
+ |
else |
3810 |
+ |
assertEquals(fval, gval); |
3811 |
+ |
} |
3812 |
+ |
|
3813 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
3814 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3815 |
+ |
} |
3816 |
+ |
|
3817 |
+ |
// Monadic "plus" |
3818 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3819 |
+ |
CompletableFuture<? extends T> g) { |
3820 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
3821 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3822 |
+ |
if (result != null) { |
3823 |
+ |
if (plus.complete(result)) |
3824 |
+ |
if (plus.firstFailure.get() != null) |
3825 |
+ |
plus.firstFailure.set(null); |
3826 |
+ |
} |
3827 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3828 |
+ |
if (plus.isDone()) |
3829 |
+ |
plus.firstFailure.set(null); |
3830 |
+ |
} else { |
3831 |
+ |
if (plus.completeExceptionally(ex)) |
3832 |
+ |
plus.firstFailure.set(null); |
3833 |
+ |
} |
3834 |
+ |
}; |
3835 |
+ |
f.whenComplete(action); |
3836 |
+ |
g.whenComplete(action); |
3837 |
+ |
return plus; |
3838 |
+ |
} |
3839 |
+ |
} |
3840 |
+ |
|
3841 |
+ |
/** |
3842 |
+ |
* CompletableFuture is an additive monad - sort of. |
3843 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
3844 |
+ |
*/ |
3845 |
+ |
public void testAdditiveMonad() throws Throwable { |
3846 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3847 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3848 |
+ |
|
3849 |
+ |
// Some mutually non-commutative functions |
3850 |
+ |
Function<Long, CompletableFuture<Long>> triple |
3851 |
+ |
= (x) -> Monad.unit(3 * x); |
3852 |
+ |
Function<Long, CompletableFuture<Long>> inc |
3853 |
+ |
= (x) -> Monad.unit(x + 1); |
3854 |
+ |
|
3855 |
+ |
// unit is a right identity: m >>= unit === m |
3856 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
3857 |
+ |
inc.apply(5L)); |
3858 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
3859 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
3860 |
+ |
inc.apply(5L)); |
3861 |
+ |
|
3862 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
3863 |
+ |
Monad.assertFutureEquals( |
3864 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
3865 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
3866 |
+ |
|
3867 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
3868 |
+ |
|
3869 |
+ |
// zero is a monadic zero |
3870 |
+ |
Monad.assertZero(zero); |
3871 |
+ |
|
3872 |
+ |
// left zero: zero >>= f === zero |
3873 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
3874 |
+ |
// right zero: f >>= (\x -> zero) === zero |
3875 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
3876 |
+ |
|
3877 |
+ |
// f plus zero === f |
3878 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3879 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
3880 |
+ |
// zero plus f === f |
3881 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3882 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
3883 |
+ |
// zero plus zero === zero |
3884 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
3885 |
+ |
{ |
3886 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
3887 |
+ |
Monad.unit(8L)); |
3888 |
+ |
// non-determinism |
3889 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
3890 |
+ |
} |
3891 |
+ |
|
3892 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
3893 |
+ |
// f plus godot === f (doesn't wait for godot) |
3894 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3895 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
3896 |
+ |
// godot plus f === f (doesn't wait for godot) |
3897 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3898 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
3899 |
+ |
} |
3900 |
+ |
|
3901 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
3902 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3903 |
+ |
// stage.whenComplete((v, ex) -> { |
3904 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3905 |
+ |
// }); |
3906 |
+ |
// return f.join(); |
3907 |
+ |
// } |
3908 |
+ |
|
3909 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3910 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3911 |
+ |
// stage.whenComplete((v, ex) -> { |
3912 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3913 |
+ |
// }); |
3914 |
+ |
// return f.isDone(); |
3915 |
+ |
// } |
3916 |
+ |
|
3917 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
3918 |
+ |
// return stage.toCompletableFuture().copy().join(); |
3919 |
+ |
// } |
3920 |
+ |
|
3921 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
3922 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
3923 |
+ |
// } |
3924 |
+ |
|
3925 |
|
} |