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; |
35 |
|
import java.util.concurrent.TimeUnit; |
36 |
|
import java.util.concurrent.atomic.AtomicInteger; |
3318 |
|
} |
3319 |
|
|
3320 |
|
/** |
3321 |
+ |
* Test submissions to an executor that rejects all tasks. |
3322 |
+ |
*/ |
3323 |
+ |
public void testRejectingExecutor() { |
3324 |
+ |
final RejectedExecutionException ex = new RejectedExecutionException(); |
3325 |
+ |
final Executor e = (Runnable r) -> { throw ex; }; |
3326 |
+ |
|
3327 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3328 |
+ |
|
3329 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3330 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3331 |
+ |
|
3332 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3333 |
+ |
|
3334 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3335 |
+ |
srcs.add(complete); |
3336 |
+ |
srcs.add(incomplete); |
3337 |
+ |
|
3338 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3339 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3340 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3341 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3342 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3343 |
+ |
|
3344 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3345 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3346 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3347 |
+ |
|
3348 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3349 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3350 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3351 |
+ |
|
3352 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3353 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3354 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3355 |
+ |
|
3356 |
+ |
for (CompletableFuture<?> future : fs) { |
3357 |
+ |
if (src.isDone()) |
3358 |
+ |
checkCompletedWithWrappedException(future, ex); |
3359 |
+ |
else |
3360 |
+ |
checkIncomplete(future); |
3361 |
+ |
} |
3362 |
+ |
futures.addAll(fs); |
3363 |
+ |
} |
3364 |
+ |
|
3365 |
+ |
{ |
3366 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3367 |
+ |
|
3368 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3369 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3370 |
+ |
|
3371 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3372 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3373 |
+ |
|
3374 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3375 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3376 |
+ |
|
3377 |
+ |
for (CompletableFuture<?> future : fs) |
3378 |
+ |
checkIncomplete(future); |
3379 |
+ |
futures.addAll(fs); |
3380 |
+ |
} |
3381 |
+ |
|
3382 |
+ |
{ |
3383 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3384 |
+ |
|
3385 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3386 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3387 |
+ |
|
3388 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3389 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3390 |
+ |
|
3391 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3392 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3393 |
+ |
|
3394 |
+ |
for (CompletableFuture<?> future : fs) |
3395 |
+ |
checkCompletedWithWrappedException(future, ex); |
3396 |
+ |
futures.addAll(fs); |
3397 |
+ |
} |
3398 |
+ |
|
3399 |
+ |
incomplete.complete(v); |
3400 |
+ |
|
3401 |
+ |
for (CompletableFuture<?> future : futures) |
3402 |
+ |
checkCompletedWithWrappedException(future, ex); |
3403 |
+ |
} |
3404 |
+ |
} |
3405 |
+ |
|
3406 |
+ |
/** |
3407 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3408 |
+ |
* should never be invoked because the dependent future is |
3409 |
+ |
* explicitly completed. |
3410 |
+ |
*/ |
3411 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3412 |
+ |
final RejectedExecutionException ex = new RejectedExecutionException(); |
3413 |
+ |
class CountingRejectingExecutor implements Executor { |
3414 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
3415 |
+ |
public void execute(Runnable r) { |
3416 |
+ |
count.getAndIncrement(); |
3417 |
+ |
throw ex; |
3418 |
+ |
} |
3419 |
+ |
} |
3420 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3421 |
+ |
|
3422 |
+ |
for (Integer v : new Integer[] { 1, null }) { |
3423 |
+ |
|
3424 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3425 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3426 |
+ |
|
3427 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3428 |
+ |
|
3429 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3430 |
+ |
srcs.add(complete); |
3431 |
+ |
srcs.add(incomplete); |
3432 |
+ |
|
3433 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3434 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3435 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3436 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3437 |
+ |
|
3438 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3439 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3440 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3441 |
+ |
|
3442 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3443 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3444 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3445 |
+ |
|
3446 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3447 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3448 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3449 |
+ |
|
3450 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3451 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3452 |
+ |
|
3453 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3454 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3455 |
+ |
|
3456 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3457 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3458 |
+ |
|
3459 |
+ |
for (CompletableFuture<?> future : fs) |
3460 |
+ |
checkIncomplete(future); |
3461 |
+ |
|
3462 |
+ |
for (CompletableFuture<?> future : fs) |
3463 |
+ |
future.complete(null); |
3464 |
+ |
|
3465 |
+ |
incomplete.complete(v); |
3466 |
+ |
|
3467 |
+ |
for (CompletableFuture<?> future : fs) |
3468 |
+ |
checkCompletedNormally(future, null); |
3469 |
+ |
|
3470 |
+ |
assertEquals(0, e.count.get()); |
3471 |
+ |
|
3472 |
+ |
} |
3473 |
+ |
} |
3474 |
+ |
|
3475 |
+ |
/** |
3476 |
|
* toCompletableFuture returns this CompletableFuture. |
3477 |
|
*/ |
3478 |
|
public void testToCompletableFuture() { |