345 |
|
|
346 |
|
// Used for explicit executor tests |
347 |
|
static final class ThreadExecutor implements Executor { |
348 |
+ |
AtomicInteger count = new AtomicInteger(0); |
349 |
+ |
|
350 |
|
public void execute(Runnable r) { |
351 |
+ |
count.getAndIncrement(); |
352 |
|
new Thread(r).start(); |
353 |
|
} |
354 |
|
} |
358 |
|
} |
359 |
|
|
360 |
|
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
361 |
+ |
boolean ran; |
362 |
|
public Integer apply(Integer x, Throwable t) { |
363 |
+ |
ran = true; |
364 |
|
return (t == null) ? two : three; |
365 |
|
} |
366 |
|
} |
389 |
|
* normal or exceptional completion of source |
390 |
|
*/ |
391 |
|
public void testHandle() { |
392 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
393 |
< |
IntegerHandler r = new IntegerHandler(); |
394 |
< |
CompletableFuture<Integer> g = f.handle(r); |
392 |
> |
CompletableFuture<Integer> f, g; |
393 |
> |
IntegerHandler r; |
394 |
> |
|
395 |
> |
f = new CompletableFuture<Integer>(); |
396 |
|
f.completeExceptionally(new CFException()); |
397 |
+ |
g = f.handle(r = new IntegerHandler()); |
398 |
+ |
assertTrue(r.ran); |
399 |
|
checkCompletedNormally(g, three); |
400 |
|
|
401 |
|
f = new CompletableFuture<Integer>(); |
402 |
< |
r = new IntegerHandler(); |
403 |
< |
g = f.handle(r); |
402 |
> |
g = f.handle(r = new IntegerHandler()); |
403 |
> |
assertFalse(r.ran); |
404 |
> |
f.completeExceptionally(new CFException()); |
405 |
> |
checkCompletedNormally(g, three); |
406 |
> |
assertTrue(r.ran); |
407 |
> |
|
408 |
> |
f = new CompletableFuture<Integer>(); |
409 |
> |
f.complete(one); |
410 |
> |
g = f.handle(r = new IntegerHandler()); |
411 |
> |
assertTrue(r.ran); |
412 |
> |
checkCompletedNormally(g, two); |
413 |
> |
|
414 |
> |
f = new CompletableFuture<Integer>(); |
415 |
> |
g = f.handle(r = new IntegerHandler()); |
416 |
> |
assertFalse(r.ran); |
417 |
|
f.complete(one); |
418 |
+ |
assertTrue(r.ran); |
419 |
|
checkCompletedNormally(g, two); |
420 |
|
} |
421 |
|
|
435 |
|
*/ |
436 |
|
public void testRunAsync2() { |
437 |
|
Noop r = new Noop(); |
438 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor()); |
438 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
439 |
> |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
440 |
|
assertNull(f.join()); |
441 |
|
assertTrue(r.ran); |
442 |
|
checkCompletedNormally(f, null); |
443 |
+ |
assertEquals(1, exec.count.get()); |
444 |
|
} |
445 |
|
|
446 |
|
/** |
457 |
|
* supplyAsync completes with result of supplier |
458 |
|
*/ |
459 |
|
public void testSupplyAsync() { |
460 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne); |
460 |
> |
CompletableFuture<Integer> f; |
461 |
> |
f = CompletableFuture.supplyAsync(supplyOne); |
462 |
|
assertEquals(f.join(), one); |
463 |
+ |
checkCompletedNormally(f, one); |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
|
* supplyAsync with executor completes with result of supplier |
468 |
|
*/ |
469 |
|
public void testSupplyAsync2() { |
470 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
470 |
> |
CompletableFuture<Integer> f; |
471 |
> |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
472 |
|
assertEquals(f.join(), one); |
473 |
+ |
checkCompletedNormally(f, one); |
474 |
|
} |
475 |
|
|
476 |
|
/** |
2427 |
|
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
2428 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2429 |
|
CompletableFuture<?> h; |
2430 |
< |
Executor exec = new ThreadExecutor(); |
2430 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
2431 |
|
|
2432 |
|
Runnable[] throwingActions = { |
2433 |
|
() -> { CompletableFuture.supplyAsync(null); }, |
2434 |
|
() -> { CompletableFuture.supplyAsync(null, exec); }, |
2435 |
< |
() -> { CompletableFuture.supplyAsync(() -> one, null); }, |
2435 |
> |
() -> { CompletableFuture.supplyAsync(supplyOne, null); }, |
2436 |
|
|
2437 |
|
() -> { CompletableFuture.runAsync(null); }, |
2438 |
|
() -> { CompletableFuture.runAsync(null, exec); }, |
2521 |
|
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
2522 |
|
() -> { CompletableFuture.anyOf(f, null); }, |
2523 |
|
() -> { CompletableFuture.anyOf(null, f); }, |
2496 |
– |
|
2497 |
– |
// TODO: Crashes javac with lambda-8-2013-03-31... |
2498 |
– |
//() -> { CompletableFuture<?> x = f.thenAccept(null); }, |
2499 |
– |
//() -> { CompletableFuture<Void> x = f.thenRun(null); }, |
2500 |
– |
//() -> { CompletableFuture<Integer> x = f.thenApply(() -> { ; }); }, |
2524 |
|
}; |
2525 |
|
|
2526 |
|
assertThrows(NullPointerException.class, throwingActions); |
2527 |
+ |
assertEquals(0, exec.count.get()); |
2528 |
|
} |
2529 |
|
|
2530 |
|
} |