53 |
|
catch (Throwable fail) { threadUnexpectedException(fail); } |
54 |
|
} |
55 |
|
|
56 |
< |
void checkCompletedNormally(CompletableFuture<?> f, Object value) { |
56 |
> |
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
57 |
|
try { |
58 |
|
assertEquals(value, f.join()); |
59 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
98 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
|
assertTrue(f.isDone()); |
100 |
|
assertFalse(f.isCancelled()); |
101 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
102 |
|
} |
103 |
|
|
104 |
|
void checkCancelled(CompletableFuture<?> f) { |
122 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
|
assertTrue(f.isDone()); |
124 |
|
assertTrue(f.isCancelled()); |
125 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
126 |
|
} |
127 |
|
|
128 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
152 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
|
assertTrue(f.isDone()); |
154 |
|
assertFalse(f.isCancelled()); |
155 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
156 |
|
} |
157 |
|
|
158 |
|
/** |
275 |
|
} |
276 |
|
|
277 |
|
/** |
278 |
< |
* completedFuture returns a completed CompletableFuture with give value |
278 |
> |
* completedFuture returns a completed CompletableFuture with given value |
279 |
|
*/ |
280 |
|
public void testCompletedFuture() { |
281 |
|
CompletableFuture<String> f = CompletableFuture.completedFuture("test"); |
355 |
|
} |
356 |
|
|
357 |
|
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
358 |
+ |
boolean ran; |
359 |
|
public Integer apply(Integer x, Throwable t) { |
360 |
+ |
ran = true; |
361 |
|
return (t == null) ? two : three; |
362 |
|
} |
363 |
|
} |
386 |
|
* normal or exceptional completion of source |
387 |
|
*/ |
388 |
|
public void testHandle() { |
389 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
390 |
< |
IntegerHandler r = new IntegerHandler(); |
391 |
< |
CompletableFuture<Integer> g = f.handle(r); |
389 |
> |
CompletableFuture<Integer> f, g; |
390 |
> |
IntegerHandler r; |
391 |
> |
|
392 |
> |
f = new CompletableFuture<Integer>(); |
393 |
> |
f.completeExceptionally(new CFException()); |
394 |
> |
g = f.handle(r = new IntegerHandler()); |
395 |
> |
assertTrue(r.ran); |
396 |
> |
checkCompletedNormally(g, three); |
397 |
> |
|
398 |
> |
f = new CompletableFuture<Integer>(); |
399 |
> |
g = f.handle(r = new IntegerHandler()); |
400 |
> |
assertFalse(r.ran); |
401 |
|
f.completeExceptionally(new CFException()); |
402 |
|
checkCompletedNormally(g, three); |
403 |
+ |
assertTrue(r.ran); |
404 |
|
|
405 |
|
f = new CompletableFuture<Integer>(); |
391 |
– |
r = new IntegerHandler(); |
392 |
– |
g = f.handle(r); |
406 |
|
f.complete(one); |
407 |
+ |
g = f.handle(r = new IntegerHandler()); |
408 |
+ |
assertTrue(r.ran); |
409 |
+ |
checkCompletedNormally(g, two); |
410 |
+ |
|
411 |
+ |
f = new CompletableFuture<Integer>(); |
412 |
+ |
g = f.handle(r = new IntegerHandler()); |
413 |
+ |
assertFalse(r.ran); |
414 |
+ |
f.complete(one); |
415 |
+ |
assertTrue(r.ran); |
416 |
|
checkCompletedNormally(g, two); |
417 |
|
} |
418 |
|
|
424 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
425 |
|
assertNull(f.join()); |
426 |
|
assertTrue(r.ran); |
427 |
+ |
checkCompletedNormally(f, null); |
428 |
|
} |
429 |
|
|
430 |
|
/** |
435 |
|
CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor()); |
436 |
|
assertNull(f.join()); |
437 |
|
assertTrue(r.ran); |
438 |
+ |
checkCompletedNormally(f, null); |
439 |
|
} |
440 |
|
|
441 |
|
/** |
2394 |
|
} |
2395 |
|
|
2396 |
|
/** |
2397 |
< |
* allOf returns a future completed when any components complete |
2397 |
> |
* anyOf returns a future completed when any components complete |
2398 |
|
*/ |
2399 |
|
public void testAnyOf() throws Exception { |
2400 |
|
for (int k = 1; k < 20; ++k) { |
2416 |
|
public void testNPE() { |
2417 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2418 |
|
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
2419 |
< |
CompletableFuture h; |
2420 |
< |
try { h = f.thenApply(null); } catch (NullPointerException ok) {} |
2421 |
< |
try { h = f.thenAccept(null); } catch (NullPointerException ok) {} |
2422 |
< |
try { h = f.thenRun(null); } catch (NullPointerException ok) {} |
2423 |
< |
try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {} |
2424 |
< |
try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {} |
2425 |
< |
try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {} |
2426 |
< |
try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {} |
2427 |
< |
try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {} |
2428 |
< |
try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {} |
2429 |
< |
try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {} |
2430 |
< |
try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {} |
2431 |
< |
try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {} |
2432 |
< |
try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {} |
2433 |
< |
try { h = f.exceptionally(null); } catch (NullPointerException ok) {} |
2434 |
< |
try { h = f.handle(null); } catch (NullPointerException ok) {} |
2435 |
< |
try { h = f.thenCompose(null); } catch (NullPointerException ok) {} |
2436 |
< |
|
2437 |
< |
try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {} |
2438 |
< |
try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {} |
2439 |
< |
try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {} |
2440 |
< |
try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {} |
2441 |
< |
try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {} |
2442 |
< |
try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {} |
2443 |
< |
try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {} |
2444 |
< |
try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {} |
2445 |
< |
try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {} |
2446 |
< |
try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {} |
2447 |
< |
try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {} |
2448 |
< |
try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {} |
2449 |
< |
try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {} |
2419 |
> |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2420 |
> |
CompletableFuture<?> h; |
2421 |
> |
Executor exec = new ThreadExecutor(); |
2422 |
> |
|
2423 |
> |
Runnable[] throwingActions = { |
2424 |
> |
() -> { CompletableFuture.supplyAsync(null); }, |
2425 |
> |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
2426 |
> |
() -> { CompletableFuture.supplyAsync(() -> one, null); }, |
2427 |
> |
|
2428 |
> |
() -> { CompletableFuture.runAsync(null); }, |
2429 |
> |
() -> { CompletableFuture.runAsync(null, exec); }, |
2430 |
> |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
2431 |
> |
|
2432 |
> |
() -> { f.completeExceptionally(null); }, |
2433 |
> |
|
2434 |
> |
() -> { f.thenApply(null); }, |
2435 |
> |
() -> { f.thenApplyAsync(null); }, |
2436 |
> |
() -> { f.thenApplyAsync((x) -> x, null); }, |
2437 |
> |
() -> { f.thenApplyAsync(null, exec); }, |
2438 |
> |
|
2439 |
> |
() -> { f.thenAccept(null); }, |
2440 |
> |
() -> { f.thenAcceptAsync(null); }, |
2441 |
> |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
2442 |
> |
() -> { f.thenAcceptAsync(null, exec); }, |
2443 |
> |
|
2444 |
> |
() -> { f.thenRun(null); }, |
2445 |
> |
() -> { f.thenRunAsync(null); }, |
2446 |
> |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
2447 |
> |
() -> { f.thenRunAsync(null, exec); }, |
2448 |
> |
|
2449 |
> |
() -> { f.thenCombine(g, null); }, |
2450 |
> |
() -> { f.thenCombineAsync(g, null); }, |
2451 |
> |
() -> { f.thenCombineAsync(g, null, exec); }, |
2452 |
> |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
2453 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
2454 |
> |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
2455 |
> |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
2456 |
> |
|
2457 |
> |
() -> { f.thenAcceptBoth(g, null); }, |
2458 |
> |
() -> { f.thenAcceptBothAsync(g, null); }, |
2459 |
> |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
2460 |
> |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
2461 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
2462 |
> |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
2463 |
> |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
2464 |
> |
|
2465 |
> |
() -> { f.runAfterBoth(g, null); }, |
2466 |
> |
() -> { f.runAfterBothAsync(g, null); }, |
2467 |
> |
() -> { f.runAfterBothAsync(g, null, exec); }, |
2468 |
> |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
2469 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
2470 |
> |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
2471 |
> |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
2472 |
> |
|
2473 |
> |
() -> { f.applyToEither(g, null); }, |
2474 |
> |
() -> { f.applyToEitherAsync(g, null); }, |
2475 |
> |
() -> { f.applyToEitherAsync(g, null, exec); }, |
2476 |
> |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
2477 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
2478 |
> |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
2479 |
> |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
2480 |
> |
|
2481 |
> |
() -> { f.acceptEither(g, null); }, |
2482 |
> |
() -> { f.acceptEitherAsync(g, null); }, |
2483 |
> |
() -> { f.acceptEitherAsync(g, null, exec); }, |
2484 |
> |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
2485 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
2486 |
> |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
2487 |
> |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
2488 |
> |
|
2489 |
> |
() -> { f.runAfterEither(g, null); }, |
2490 |
> |
() -> { f.runAfterEitherAsync(g, null); }, |
2491 |
> |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
2492 |
> |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
2493 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
2494 |
> |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
2495 |
> |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
2496 |
> |
|
2497 |
> |
() -> { f.thenCompose(null); }, |
2498 |
> |
() -> { f.thenComposeAsync(null); }, |
2499 |
> |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
2500 |
> |
() -> { f.thenComposeAsync(null, exec); }, |
2501 |
> |
|
2502 |
> |
() -> { f.exceptionally(null); }, |
2503 |
> |
|
2504 |
> |
() -> { f.handle(null); }, |
2505 |
> |
|
2506 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
2507 |
> |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
2508 |
> |
() -> { CompletableFuture.allOf(f, null); }, |
2509 |
> |
() -> { CompletableFuture.allOf(null, f); }, |
2510 |
> |
|
2511 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
2512 |
> |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
2513 |
> |
() -> { CompletableFuture.anyOf(f, null); }, |
2514 |
> |
() -> { CompletableFuture.anyOf(null, f); }, |
2515 |
> |
|
2516 |
> |
// TODO: Crashes javac with lambda-8-2013-03-31... |
2517 |
> |
//() -> { CompletableFuture<?> x = f.thenAccept(null); }, |
2518 |
> |
//() -> { CompletableFuture<Void> x = f.thenRun(null); }, |
2519 |
> |
//() -> { CompletableFuture<Integer> x = f.thenApply(() -> { ; }); }, |
2520 |
> |
}; |
2521 |
|
|
2522 |
+ |
assertThrows(NullPointerException.class, throwingActions); |
2523 |
|
} |
2524 |
|
|
2429 |
– |
|
2525 |
|
} |