8 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
|
|
11 |
+ |
import java.lang.reflect.Method; |
12 |
+ |
import java.lang.reflect.Modifier; |
13 |
+ |
|
14 |
+ |
import java.util.stream.Collectors; |
15 |
+ |
import java.util.stream.Stream; |
16 |
+ |
|
17 |
|
import java.util.ArrayList; |
18 |
+ |
import java.util.Arrays; |
19 |
|
import java.util.List; |
20 |
|
import java.util.Objects; |
21 |
+ |
import java.util.Set; |
22 |
|
import java.util.concurrent.Callable; |
23 |
|
import java.util.concurrent.CancellationException; |
24 |
|
import java.util.concurrent.CompletableFuture; |
29 |
|
import java.util.concurrent.ForkJoinPool; |
30 |
|
import java.util.concurrent.ForkJoinTask; |
31 |
|
import java.util.concurrent.TimeoutException; |
32 |
+ |
import java.util.concurrent.TimeUnit; |
33 |
|
import java.util.concurrent.atomic.AtomicInteger; |
34 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
35 |
|
import java.util.function.BiConsumer; |
36 |
|
import java.util.function.BiFunction; |
37 |
|
import java.util.function.Consumer; |
44 |
|
public class CompletableFutureTest extends JSR166TestCase { |
45 |
|
|
46 |
|
public static void main(String[] args) { |
47 |
< |
junit.textui.TestRunner.run(suite()); |
47 |
> |
main(suite(), args); |
48 |
|
} |
49 |
|
public static Test suite() { |
50 |
|
return new TestSuite(CompletableFutureTest.class); |
55 |
|
void checkIncomplete(CompletableFuture<?> f) { |
56 |
|
assertFalse(f.isDone()); |
57 |
|
assertFalse(f.isCancelled()); |
58 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
58 |
> |
assertTrue(f.toString().contains("Not completed")); |
59 |
|
try { |
60 |
|
assertNull(f.getNow(null)); |
61 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
85 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
86 |
|
} |
87 |
|
|
88 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
88 |
> |
/** |
89 |
> |
* Returns the "raw" internal exceptional completion of f, |
90 |
> |
* without any additional wrapping with CompletionException. |
91 |
> |
*/ |
92 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
93 |
> |
// handle (and whenComplete) can distinguish between "direct" |
94 |
> |
// and "wrapped" exceptional completion |
95 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
96 |
> |
} |
97 |
> |
|
98 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
99 |
> |
boolean wrapped, |
100 |
> |
Consumer<Throwable> checker) { |
101 |
> |
Throwable cause = exceptionalCompletion(f); |
102 |
> |
if (wrapped) { |
103 |
> |
assertTrue(cause instanceof CompletionException); |
104 |
> |
cause = cause.getCause(); |
105 |
> |
} |
106 |
> |
checker.accept(cause); |
107 |
> |
|
108 |
|
long startTime = System.nanoTime(); |
80 |
– |
long timeoutMillis = LONG_DELAY_MS; |
109 |
|
try { |
110 |
< |
f.get(timeoutMillis, MILLISECONDS); |
110 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
111 |
|
shouldThrow(); |
112 |
|
} catch (ExecutionException success) { |
113 |
< |
assertTrue(success.getCause() instanceof CFException); |
113 |
> |
assertSame(cause, success.getCause()); |
114 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
115 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
116 |
|
|
117 |
|
try { |
118 |
|
f.join(); |
119 |
|
shouldThrow(); |
120 |
|
} catch (CompletionException success) { |
121 |
< |
assertTrue(success.getCause() instanceof CFException); |
122 |
< |
} |
121 |
> |
assertSame(cause, success.getCause()); |
122 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
> |
|
124 |
|
try { |
125 |
|
f.getNow(null); |
126 |
|
shouldThrow(); |
127 |
|
} catch (CompletionException success) { |
128 |
< |
assertTrue(success.getCause() instanceof CFException); |
129 |
< |
} |
128 |
> |
assertSame(cause, success.getCause()); |
129 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
130 |
> |
|
131 |
|
try { |
132 |
|
f.get(); |
133 |
|
shouldThrow(); |
134 |
|
} catch (ExecutionException success) { |
135 |
< |
assertTrue(success.getCause() instanceof CFException); |
135 |
> |
assertSame(cause, success.getCause()); |
136 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
137 |
< |
assertTrue(f.isDone()); |
137 |
> |
|
138 |
|
assertFalse(f.isCancelled()); |
139 |
+ |
assertTrue(f.isDone()); |
140 |
+ |
assertTrue(f.isCompletedExceptionally()); |
141 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
142 |
|
} |
143 |
|
|
144 |
< |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
145 |
< |
Throwable ex) { |
146 |
< |
long startTime = System.nanoTime(); |
147 |
< |
long timeoutMillis = LONG_DELAY_MS; |
116 |
< |
try { |
117 |
< |
f.get(timeoutMillis, MILLISECONDS); |
118 |
< |
shouldThrow(); |
119 |
< |
} catch (ExecutionException success) { |
120 |
< |
assertSame(ex, success.getCause()); |
121 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
144 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
145 |
> |
checkCompletedExceptionally(f, true, |
146 |
> |
(t) -> assertTrue(t instanceof CFException)); |
147 |
> |
} |
148 |
|
|
149 |
< |
try { |
150 |
< |
f.join(); |
151 |
< |
shouldThrow(); |
152 |
< |
} catch (CompletionException success) { |
128 |
< |
assertSame(ex, success.getCause()); |
129 |
< |
} |
130 |
< |
try { |
131 |
< |
f.getNow(null); |
132 |
< |
shouldThrow(); |
133 |
< |
} catch (CompletionException success) { |
134 |
< |
assertSame(ex, success.getCause()); |
135 |
< |
} |
136 |
< |
try { |
137 |
< |
f.get(); |
138 |
< |
shouldThrow(); |
139 |
< |
} catch (ExecutionException success) { |
140 |
< |
assertSame(ex, success.getCause()); |
141 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
> |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
150 |
> |
checkCompletedExceptionally(f, true, |
151 |
> |
(t) -> assertTrue(t instanceof CancellationException)); |
152 |
> |
} |
153 |
|
|
154 |
< |
assertTrue(f.isDone()); |
155 |
< |
assertFalse(f.isCancelled()); |
156 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
154 |
> |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
155 |
> |
checkCompletedExceptionally(f, false, |
156 |
> |
(t) -> assertTrue(t instanceof TimeoutException)); |
157 |
|
} |
158 |
|
|
159 |
< |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
160 |
< |
Throwable ex) { |
161 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
151 |
< |
try { |
152 |
< |
CompletableFuture<Throwable> spy = f.handle |
153 |
< |
((U u, Throwable t) -> t); |
154 |
< |
assertTrue(spy.join() instanceof CompletionException); |
155 |
< |
assertSame(ex, spy.join().getCause()); |
156 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
> |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
160 |
> |
Throwable ex) { |
161 |
> |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
162 |
|
} |
163 |
|
|
164 |
< |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
165 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
161 |
< |
try { |
162 |
< |
CompletableFuture<Throwable> spy = f.handle |
163 |
< |
((U u, Throwable t) -> t); |
164 |
< |
assertSame(ex, spy.join()); |
165 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
164 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
165 |
> |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
166 |
|
} |
167 |
|
|
168 |
|
void checkCancelled(CompletableFuture<?> f) { |
169 |
|
long startTime = System.nanoTime(); |
170 |
– |
long timeoutMillis = LONG_DELAY_MS; |
170 |
|
try { |
171 |
< |
f.get(timeoutMillis, MILLISECONDS); |
171 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
172 |
|
shouldThrow(); |
173 |
|
} catch (CancellationException success) { |
174 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
175 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
175 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
176 |
|
|
177 |
|
try { |
178 |
|
f.join(); |
187 |
|
shouldThrow(); |
188 |
|
} catch (CancellationException success) { |
189 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
191 |
– |
assertTrue(f.isDone()); |
192 |
– |
assertTrue(f.isCompletedExceptionally()); |
193 |
– |
assertTrue(f.isCancelled()); |
194 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
195 |
– |
} |
190 |
|
|
191 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
198 |
< |
long startTime = System.nanoTime(); |
199 |
< |
long timeoutMillis = LONG_DELAY_MS; |
200 |
< |
try { |
201 |
< |
f.get(timeoutMillis, MILLISECONDS); |
202 |
< |
shouldThrow(); |
203 |
< |
} catch (ExecutionException success) { |
204 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
205 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
206 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
191 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
192 |
|
|
208 |
– |
try { |
209 |
– |
f.join(); |
210 |
– |
shouldThrow(); |
211 |
– |
} catch (CompletionException success) { |
212 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
213 |
– |
} |
214 |
– |
try { |
215 |
– |
f.getNow(null); |
216 |
– |
shouldThrow(); |
217 |
– |
} catch (CompletionException success) { |
218 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
219 |
– |
} |
220 |
– |
try { |
221 |
– |
f.get(); |
222 |
– |
shouldThrow(); |
223 |
– |
} catch (ExecutionException success) { |
224 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
225 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
193 |
|
assertTrue(f.isDone()); |
227 |
– |
assertFalse(f.isCancelled()); |
194 |
|
assertTrue(f.isCompletedExceptionally()); |
195 |
+ |
assertTrue(f.isCancelled()); |
196 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
197 |
|
} |
198 |
|
|
3070 |
|
for (int i = 0; i < k; i++) { |
3071 |
|
checkIncomplete(f); |
3072 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3073 |
< |
if (i != k/2) { |
3073 |
> |
if (i != k / 2) { |
3074 |
|
fs[i].complete(i); |
3075 |
|
checkCompletedNormally(fs[i], i); |
3076 |
|
} else { |
3273 |
|
() -> CompletableFuture.anyOf(null, f), |
3274 |
|
|
3275 |
|
() -> f.obtrudeException(null), |
3276 |
+ |
|
3277 |
+ |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3278 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3279 |
+ |
() -> CompletableFuture.delayedExecutor(1L, null), |
3280 |
+ |
|
3281 |
+ |
() -> f.orTimeout(1L, null), |
3282 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3283 |
+ |
|
3284 |
+ |
() -> CompletableFuture.failedFuture(null), |
3285 |
+ |
() -> CompletableFuture.failedStage(null), |
3286 |
|
}; |
3287 |
|
|
3288 |
|
assertThrows(NullPointerException.class, throwingActions); |
3297 |
|
assertSame(f, f.toCompletableFuture()); |
3298 |
|
} |
3299 |
|
|
3300 |
+ |
// jdk9 |
3301 |
+ |
|
3302 |
+ |
/** |
3303 |
+ |
* newIncompleteFuture returns an incomplete CompletableFuture |
3304 |
+ |
*/ |
3305 |
+ |
public void testNewIncompleteFuture() { |
3306 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3307 |
+ |
{ |
3308 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3309 |
+ |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3310 |
+ |
checkIncomplete(f); |
3311 |
+ |
checkIncomplete(g); |
3312 |
+ |
f.complete(v1); |
3313 |
+ |
checkCompletedNormally(f, v1); |
3314 |
+ |
checkIncomplete(g); |
3315 |
+ |
g.complete(v1); |
3316 |
+ |
checkCompletedNormally(g, v1); |
3317 |
+ |
assertSame(g.getClass(), CompletableFuture.class); |
3318 |
+ |
}} |
3319 |
+ |
|
3320 |
+ |
/** |
3321 |
+ |
* completedStage returns a completed CompletionStage |
3322 |
+ |
*/ |
3323 |
+ |
public void testCompletedStage() { |
3324 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3325 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3326 |
+ |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3327 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3328 |
+ |
assertEquals(x.get(), 1); |
3329 |
+ |
assertNull(r.get()); |
3330 |
+ |
} |
3331 |
+ |
|
3332 |
+ |
/** |
3333 |
+ |
* defaultExecutor by default returns the commonPool if |
3334 |
+ |
* it supports more than one thread. |
3335 |
+ |
*/ |
3336 |
+ |
public void testDefaultExecutor() { |
3337 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3338 |
+ |
Executor e = f.defaultExecutor(); |
3339 |
+ |
Executor c = ForkJoinPool.commonPool(); |
3340 |
+ |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3341 |
+ |
assertSame(e, c); |
3342 |
+ |
else |
3343 |
+ |
assertNotSame(e, c); |
3344 |
+ |
} |
3345 |
+ |
|
3346 |
+ |
/** |
3347 |
+ |
* failedFuture returns a CompletableFuture completed |
3348 |
+ |
* exceptionally with the given Exception |
3349 |
+ |
*/ |
3350 |
+ |
public void testFailedFuture() { |
3351 |
+ |
CFException ex = new CFException(); |
3352 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3353 |
+ |
checkCompletedExceptionally(f, ex); |
3354 |
+ |
} |
3355 |
+ |
|
3356 |
+ |
/** |
3357 |
+ |
* failedFuture(null) throws NPE |
3358 |
+ |
*/ |
3359 |
+ |
public void testFailedFuture_null() { |
3360 |
+ |
try { |
3361 |
+ |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3362 |
+ |
shouldThrow(); |
3363 |
+ |
} catch (NullPointerException success) {} |
3364 |
+ |
} |
3365 |
+ |
|
3366 |
+ |
/** |
3367 |
+ |
* copy returns a CompletableFuture that is completed normally, |
3368 |
+ |
* with the same value, when source is. |
3369 |
+ |
*/ |
3370 |
+ |
public void testCopy() { |
3371 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3372 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3373 |
+ |
checkIncomplete(f); |
3374 |
+ |
checkIncomplete(g); |
3375 |
+ |
f.complete(1); |
3376 |
+ |
checkCompletedNormally(f, 1); |
3377 |
+ |
checkCompletedNormally(g, 1); |
3378 |
+ |
} |
3379 |
+ |
|
3380 |
+ |
/** |
3381 |
+ |
* copy returns a CompletableFuture that is completed exceptionally |
3382 |
+ |
* when source is. |
3383 |
+ |
*/ |
3384 |
+ |
public void testCopy2() { |
3385 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3386 |
+ |
CompletableFuture<Integer> g = f.copy(); |
3387 |
+ |
checkIncomplete(f); |
3388 |
+ |
checkIncomplete(g); |
3389 |
+ |
CFException ex = new CFException(); |
3390 |
+ |
f.completeExceptionally(ex); |
3391 |
+ |
checkCompletedExceptionally(f, ex); |
3392 |
+ |
checkCompletedWithWrappedException(g, ex); |
3393 |
+ |
} |
3394 |
+ |
|
3395 |
+ |
/** |
3396 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3397 |
+ |
* completed normally, with the same value, when source is. |
3398 |
+ |
*/ |
3399 |
+ |
public void testMinimalCompletionStage() { |
3400 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3401 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3402 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3403 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3404 |
+ |
checkIncomplete(f); |
3405 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3406 |
+ |
f.complete(1); |
3407 |
+ |
checkCompletedNormally(f, 1); |
3408 |
+ |
assertEquals(x.get(), 1); |
3409 |
+ |
assertNull(r.get()); |
3410 |
+ |
} |
3411 |
+ |
|
3412 |
+ |
/** |
3413 |
+ |
* minimalCompletionStage returns a CompletableFuture that is |
3414 |
+ |
* completed exceptionally when source is. |
3415 |
+ |
*/ |
3416 |
+ |
public void testMinimalCompletionStage2() { |
3417 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3418 |
+ |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3419 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3420 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3421 |
+ |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3422 |
+ |
checkIncomplete(f); |
3423 |
+ |
CFException ex = new CFException(); |
3424 |
+ |
f.completeExceptionally(ex); |
3425 |
+ |
checkCompletedExceptionally(f, ex); |
3426 |
+ |
assertEquals(x.get(), 0); |
3427 |
+ |
assertEquals(r.get().getCause(), ex); |
3428 |
+ |
} |
3429 |
+ |
|
3430 |
+ |
/** |
3431 |
+ |
* failedStage returns a CompletionStage completed |
3432 |
+ |
* exceptionally with the given Exception |
3433 |
+ |
*/ |
3434 |
+ |
public void testFailedStage() { |
3435 |
+ |
CFException ex = new CFException(); |
3436 |
+ |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3437 |
+ |
AtomicInteger x = new AtomicInteger(0); |
3438 |
+ |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3439 |
+ |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3440 |
+ |
assertEquals(x.get(), 0); |
3441 |
+ |
assertEquals(r.get(), ex); |
3442 |
+ |
} |
3443 |
+ |
|
3444 |
+ |
/** |
3445 |
+ |
* completeAsync completes with value of given supplier |
3446 |
+ |
*/ |
3447 |
+ |
public void testCompleteAsync() { |
3448 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3449 |
+ |
{ |
3450 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3451 |
+ |
f.completeAsync(() -> v1); |
3452 |
+ |
f.join(); |
3453 |
+ |
checkCompletedNormally(f, v1); |
3454 |
+ |
}} |
3455 |
+ |
|
3456 |
+ |
/** |
3457 |
+ |
* completeAsync completes exceptionally if given supplier throws |
3458 |
+ |
*/ |
3459 |
+ |
public void testCompleteAsync2() { |
3460 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3461 |
+ |
CFException ex = new CFException(); |
3462 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3463 |
+ |
try { |
3464 |
+ |
f.join(); |
3465 |
+ |
shouldThrow(); |
3466 |
+ |
} catch (CompletionException success) {} |
3467 |
+ |
checkCompletedWithWrappedException(f, ex); |
3468 |
+ |
} |
3469 |
+ |
|
3470 |
+ |
/** |
3471 |
+ |
* completeAsync with given executor completes with value of given supplier |
3472 |
+ |
*/ |
3473 |
+ |
public void testCompleteAsync3() { |
3474 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3475 |
+ |
{ |
3476 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3477 |
+ |
ThreadExecutor executor = new ThreadExecutor(); |
3478 |
+ |
f.completeAsync(() -> v1, executor); |
3479 |
+ |
assertSame(v1, f.join()); |
3480 |
+ |
checkCompletedNormally(f, v1); |
3481 |
+ |
assertEquals(1, executor.count.get()); |
3482 |
+ |
}} |
3483 |
+ |
|
3484 |
+ |
/** |
3485 |
+ |
* completeAsync with given executor completes exceptionally if |
3486 |
+ |
* given supplier throws |
3487 |
+ |
*/ |
3488 |
+ |
public void testCompleteAsync4() { |
3489 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3490 |
+ |
CFException ex = new CFException(); |
3491 |
+ |
ThreadExecutor executor = new ThreadExecutor(); |
3492 |
+ |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3493 |
+ |
try { |
3494 |
+ |
f.join(); |
3495 |
+ |
shouldThrow(); |
3496 |
+ |
} catch (CompletionException success) {} |
3497 |
+ |
checkCompletedWithWrappedException(f, ex); |
3498 |
+ |
assertEquals(1, executor.count.get()); |
3499 |
+ |
} |
3500 |
+ |
|
3501 |
+ |
/** |
3502 |
+ |
* orTimeout completes with TimeoutException if not complete |
3503 |
+ |
*/ |
3504 |
+ |
public void testOrTimeout_timesOut() { |
3505 |
+ |
long timeoutMillis = timeoutMillis(); |
3506 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3507 |
+ |
long startTime = System.nanoTime(); |
3508 |
+ |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3509 |
+ |
checkCompletedWithTimeoutException(f); |
3510 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3511 |
+ |
} |
3512 |
+ |
|
3513 |
+ |
/** |
3514 |
+ |
* orTimeout completes normally if completed before timeout |
3515 |
+ |
*/ |
3516 |
+ |
public void testOrTimeout_completed() { |
3517 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3518 |
+ |
{ |
3519 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3520 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3521 |
+ |
long startTime = System.nanoTime(); |
3522 |
+ |
f.complete(v1); |
3523 |
+ |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3524 |
+ |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3525 |
+ |
g.complete(v1); |
3526 |
+ |
checkCompletedNormally(f, v1); |
3527 |
+ |
checkCompletedNormally(g, v1); |
3528 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3529 |
+ |
}} |
3530 |
+ |
|
3531 |
+ |
/** |
3532 |
+ |
* completeOnTimeout completes with given value if not complete |
3533 |
+ |
*/ |
3534 |
+ |
public void testCompleteOnTimeout_timesOut() { |
3535 |
+ |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3536 |
+ |
() -> testCompleteOnTimeout_timesOut(null)); |
3537 |
+ |
} |
3538 |
+ |
|
3539 |
+ |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3540 |
+ |
long timeoutMillis = timeoutMillis(); |
3541 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3542 |
+ |
long startTime = System.nanoTime(); |
3543 |
+ |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3544 |
+ |
assertSame(v, f.join()); |
3545 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3546 |
+ |
f.complete(99); // should have no effect |
3547 |
+ |
checkCompletedNormally(f, v); |
3548 |
+ |
} |
3549 |
+ |
|
3550 |
+ |
/** |
3551 |
+ |
* completeOnTimeout has no effect if completed within timeout |
3552 |
+ |
*/ |
3553 |
+ |
public void testCompleteOnTimeout_completed() { |
3554 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3555 |
+ |
{ |
3556 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3557 |
+ |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3558 |
+ |
long startTime = System.nanoTime(); |
3559 |
+ |
f.complete(v1); |
3560 |
+ |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3561 |
+ |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3562 |
+ |
g.complete(v1); |
3563 |
+ |
checkCompletedNormally(f, v1); |
3564 |
+ |
checkCompletedNormally(g, v1); |
3565 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3566 |
+ |
}} |
3567 |
+ |
|
3568 |
+ |
/** |
3569 |
+ |
* delayedExecutor returns an executor that delays submission |
3570 |
+ |
*/ |
3571 |
+ |
public void testDelayedExecutor() { |
3572 |
+ |
testInParallel(() -> testDelayedExecutor(null, null), |
3573 |
+ |
() -> testDelayedExecutor(null, 1), |
3574 |
+ |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3575 |
+ |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3576 |
+ |
} |
3577 |
+ |
|
3578 |
+ |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3579 |
+ |
long timeoutMillis = timeoutMillis(); |
3580 |
+ |
// Use an "unreasonably long" long timeout to catch lingering threads |
3581 |
+ |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3582 |
+ |
final Executor delayer, longDelayer; |
3583 |
+ |
if (executor == null) { |
3584 |
+ |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3585 |
+ |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3586 |
+ |
} else { |
3587 |
+ |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3588 |
+ |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3589 |
+ |
} |
3590 |
+ |
long startTime = System.nanoTime(); |
3591 |
+ |
CompletableFuture<Integer> f = |
3592 |
+ |
CompletableFuture.supplyAsync(() -> v, delayer); |
3593 |
+ |
CompletableFuture<Integer> g = |
3594 |
+ |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3595 |
+ |
|
3596 |
+ |
assertNull(g.getNow(null)); |
3597 |
+ |
|
3598 |
+ |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3599 |
+ |
long millisElapsed = millisElapsedSince(startTime); |
3600 |
+ |
assertTrue(millisElapsed >= timeoutMillis); |
3601 |
+ |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3602 |
+ |
|
3603 |
+ |
checkCompletedNormally(f, v); |
3604 |
+ |
|
3605 |
+ |
checkIncomplete(g); |
3606 |
+ |
assertTrue(g.cancel(true)); |
3607 |
+ |
} |
3608 |
+ |
|
3609 |
|
//--- tests of implementation details; not part of official tck --- |
3610 |
|
|
3611 |
|
Object resultOf(CompletableFuture<?> f) { |
3696 |
|
} |
3697 |
|
}} |
3698 |
|
|
3699 |
+ |
/** |
3700 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3701 |
+ |
*/ |
3702 |
+ |
public void testMinimalCompletionStage_minimality() { |
3703 |
+ |
if (!testImplementationDetails) return; |
3704 |
+ |
Function<Method, String> toSignature = |
3705 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3706 |
+ |
List<Method> minimalMethods = |
3707 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3708 |
+ |
.map((klazz) -> Stream.of(klazz.getMethods())) |
3709 |
+ |
.reduce(Stream::concat) |
3710 |
+ |
.orElseGet(Stream::empty) |
3711 |
+ |
.filter((method) -> (method.getModifiers() & Modifier.STATIC) == 0) |
3712 |
+ |
.collect(Collectors.toList()); |
3713 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3714 |
+ |
String[] signatureWhitelist = { |
3715 |
+ |
"newIncompleteFuture[]", |
3716 |
+ |
"defaultExecutor[]", |
3717 |
+ |
"minimalCompletionStage[]", |
3718 |
+ |
"copy[]", |
3719 |
+ |
}; |
3720 |
+ |
Set<String> permittedMethodSignatures = |
3721 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3722 |
+ |
Stream.of(signatureWhitelist)) |
3723 |
+ |
.collect(Collectors.toSet()); |
3724 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3725 |
+ |
.filter((method) -> (method.getModifiers() & Modifier.STATIC) == 0) |
3726 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3727 |
+ |
.collect(Collectors.toList()); |
3728 |
+ |
|
3729 |
+ |
CompletionStage<Integer> minimalStage = |
3730 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3731 |
+ |
|
3732 |
+ |
List<Method> bugs = new ArrayList<>(); |
3733 |
+ |
for (Method method : allMethods) { |
3734 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3735 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3736 |
+ |
// Manufacture boxed primitives for primitive params |
3737 |
+ |
for (int i = 0; i < args.length; i++) { |
3738 |
+ |
Class<?> type = parameterTypes[i]; |
3739 |
+ |
if (parameterTypes[i] == boolean.class) |
3740 |
+ |
args[i] = false; |
3741 |
+ |
else if (parameterTypes[i] == int.class) |
3742 |
+ |
args[i] = 0; |
3743 |
+ |
else if (parameterTypes[i] == long.class) |
3744 |
+ |
args[i] = 0L; |
3745 |
+ |
} |
3746 |
+ |
try { |
3747 |
+ |
method.invoke(minimalStage, args); |
3748 |
+ |
bugs.add(method); |
3749 |
+ |
} |
3750 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3751 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3752 |
+ |
bugs.add(method); |
3753 |
+ |
// expected.getCause().printStackTrace(); |
3754 |
+ |
} |
3755 |
+ |
} |
3756 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3757 |
+ |
} |
3758 |
+ |
if (!bugs.isEmpty()) |
3759 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3760 |
+ |
} |
3761 |
+ |
|
3762 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
3763 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3764 |
+ |
// stage.whenComplete((v, ex) -> { |
3765 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3766 |
+ |
// }); |
3767 |
+ |
// return f.join(); |
3768 |
+ |
// } |
3769 |
+ |
|
3770 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3771 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3772 |
+ |
// stage.whenComplete((v, ex) -> { |
3773 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3774 |
+ |
// }); |
3775 |
+ |
// return f.isDone(); |
3776 |
+ |
// } |
3777 |
+ |
|
3778 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
3779 |
+ |
// return stage.toCompletableFuture().copy().join(); |
3780 |
+ |
// } |
3781 |
+ |
|
3782 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
3783 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
3784 |
+ |
// } |
3785 |
+ |
|
3786 |
|
} |