ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/CompletableFutureTest.java
(Generate patch)

Comparing jsr166/src/test/tck/CompletableFutureTest.java (file contents):
Revision 1.100 by jsr166, Thu Jan 15 18:34:19 2015 UTC vs.
Revision 1.114 by jsr166, Fri Sep 4 19:59:18 2015 UTC

# Line 21 | Line 21 | import java.util.concurrent.Executor;
21   import java.util.concurrent.ForkJoinPool;
22   import java.util.concurrent.ForkJoinTask;
23   import java.util.concurrent.TimeoutException;
24 + import java.util.concurrent.TimeUnit;
25   import java.util.concurrent.atomic.AtomicInteger;
26 + import java.util.concurrent.atomic.AtomicReference;
27   import java.util.function.BiConsumer;
28   import java.util.function.BiFunction;
29   import java.util.function.Consumer;
# Line 34 | Line 36 | import junit.framework.TestSuite;
36   public class CompletableFutureTest extends JSR166TestCase {
37  
38      public static void main(String[] args) {
39 <        junit.textui.TestRunner.run(suite());
39 >        main(suite(), args);
40      }
41      public static Test suite() {
42          return new TestSuite(CompletableFutureTest.class);
# Line 45 | Line 47 | public class CompletableFutureTest exten
47      void checkIncomplete(CompletableFuture<?> f) {
48          assertFalse(f.isDone());
49          assertFalse(f.isCancelled());
50 <        assertTrue(f.toString().contains("[Not completed]"));
50 >        assertTrue(f.toString().contains("Not completed"));
51          try {
52              assertNull(f.getNow(null));
53          } catch (Throwable fail) { threadUnexpectedException(fail); }
# Line 145 | Line 147 | public class CompletableFutureTest exten
147          assertTrue(f.toString().contains("[Completed exceptionally]"));
148      }
149  
150 +    <U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) {
151 +        long startTime = System.nanoTime();
152 +        long timeoutMillis = LONG_DELAY_MS;
153 +        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 +        }
174 +
175 +        try {
176 +            f.get();
177 +            shouldThrow();
178 +        } catch (ExecutionException ex) {
179 +            assertTrue(ex.getCause() instanceof TimeoutException);
180 +        } catch (Throwable fail) { threadUnexpectedException(fail); }
181 +
182 +        assertTrue(f.isDone());
183 +        assertFalse(f.isCancelled());
184 +        assertTrue(f.toString().contains("[Completed exceptionally]"));
185 +    }
186 +
187      <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
188                                                  Throwable ex) {
189          checkCompletedExceptionallyWithRootCause(f, ex);
# Line 273 | Line 312 | public class CompletableFutureTest exten
312      {
313          CompletableFuture<Integer> f = new CompletableFuture<>();
314          checkIncomplete(f);
315 <        assertTrue(f.cancel(true));
316 <        assertTrue(f.cancel(true));
315 >        assertTrue(f.cancel(mayInterruptIfRunning));
316 >        assertTrue(f.cancel(mayInterruptIfRunning));
317 >        assertTrue(f.cancel(!mayInterruptIfRunning));
318          checkCancelled(f);
319      }}
320  
# Line 906 | Line 946 | public class CompletableFutureTest exten
946  
947      public void testExceptionally_exceptionalCompletionActionFailed() {
948          for (boolean createIncomplete : new boolean[] { true, false })
909        for (Integer v1 : new Integer[] { 1, null })
949      {
950          final AtomicInteger a = new AtomicInteger(0);
951          final CFException ex1 = new CFException();
# Line 960 | Line 999 | public class CompletableFutureTest exten
999      public void testWhenComplete_exceptionalCompletion() {
1000          for (ExecutionMode m : ExecutionMode.values())
1001          for (boolean createIncomplete : new boolean[] { true, false })
963        for (Integer v1 : new Integer[] { 1, null })
1002      {
1003          final AtomicInteger a = new AtomicInteger(0);
1004          final CFException ex = new CFException();
# Line 1045 | Line 1083 | public class CompletableFutureTest exten
1083      public void testWhenComplete_actionFailedSourceFailed() {
1084          for (boolean createIncomplete : new boolean[] { true, false })
1085          for (ExecutionMode m : ExecutionMode.values())
1048        for (Integer v1 : new Integer[] { 1, null })
1086      {
1087          final AtomicInteger a = new AtomicInteger(0);
1088          final CFException ex1 = new CFException();
# Line 3212 | Line 3249 | public class CompletableFutureTest exten
3249          CompletableFuture<Integer> f = new CompletableFuture<>();
3250          CompletableFuture<Integer> g = new CompletableFuture<>();
3251          CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null;
3215        CompletableFuture<?> h;
3252          ThreadExecutor exec = new ThreadExecutor();
3253  
3254          Runnable[] throwingActions = {
# Line 3323 | Line 3359 | public class CompletableFutureTest exten
3359          assertSame(f, f.toCompletableFuture());
3360      }
3361  
3362 +    // jdk9
3363 +
3364 +    /**
3365 +     * newIncompleteFuture returns an incomplete CompletableFuture
3366 +     */
3367 +    public void testNewIncompleteFuture() {
3368 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3369 +        CompletableFuture<Integer> g = f.newIncompleteFuture();
3370 +        checkIncomplete(f);
3371 +        checkIncomplete(g);
3372 +    }
3373 +
3374 +    /**
3375 +     * completedStage returns a completed CompletionStage
3376 +     */
3377 +    public void testCompletedStage() {
3378 +        AtomicInteger x = new AtomicInteger();
3379 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3380 +        CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3381 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3382 +        assertEquals(x.get(), 1);
3383 +        assertNull(r.get());
3384 +    }
3385 +
3386 +    /**
3387 +     * defaultExecutor by default returns the commonPool if
3388 +     * it supports more than one thread.
3389 +     */
3390 +    public void testDefaultExecutor() {
3391 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3392 +        Executor e = f.defaultExecutor();
3393 +        Executor c = ForkJoinPool.commonPool();
3394 +        if (ForkJoinPool.getCommonPoolParallelism() > 1)
3395 +            assertSame(e, c);
3396 +        else
3397 +            assertNotSame(e, c);
3398 +    }
3399 +
3400 +    /**
3401 +     * failedFuture returns a CompletableFuture completed
3402 +     * exceptionally with the given Exception
3403 +     */
3404 +    public void testFailedFuture() {
3405 +        CFException ex = new CFException();
3406 +        CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3407 +        checkCompletedExceptionallyWithRootCause(f, ex);
3408 +    }
3409 +
3410 +    /**
3411 +     * failedFuture(null) throws NPE
3412 +     */
3413 +    public void testFailedFuture2() {
3414 +        try {
3415 +            CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3416 +            shouldThrow();
3417 +        } catch (NullPointerException success) {}
3418 +    }
3419 +
3420 +    /**
3421 +     * copy returns a CompletableFuture that is completed normally,
3422 +     * with the same value, when source is.
3423 +     */
3424 +    public void testCopy() {
3425 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3426 +        CompletableFuture<Integer> g = f.copy();
3427 +        checkIncomplete(f);
3428 +        checkIncomplete(g);
3429 +        f.complete(1);
3430 +        checkCompletedNormally(f, 1);
3431 +        checkCompletedNormally(g, 1);
3432 +    }
3433 +
3434 +    /**
3435 +     * copy returns a CompletableFuture that is completed exceptionally
3436 +     * when source is.
3437 +     */
3438 +    public void testCopy2() {
3439 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3440 +        CompletableFuture<Integer> g = f.copy();
3441 +        checkIncomplete(f);
3442 +        checkIncomplete(g);
3443 +        CFException ex = new CFException();
3444 +        f.completeExceptionally(ex);
3445 +        checkCompletedExceptionally(f, ex);
3446 +        checkCompletedWithWrappedCFException(g);
3447 +    }
3448 +
3449 +    /**
3450 +     * minimalCompletionStage returns a CompletableFuture that is
3451 +     * completed normally, with the same value, when source is.
3452 +     */
3453 +    public void testMinimalCompletionStage() {
3454 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3455 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3456 +        AtomicInteger x = new AtomicInteger();
3457 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3458 +        checkIncomplete(f);
3459 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3460 +        f.complete(1);
3461 +        checkCompletedNormally(f, 1);
3462 +        assertEquals(x.get(), 1);
3463 +        assertNull(r.get());
3464 +    }
3465 +
3466 +    /**
3467 +     * minimalCompletionStage returns a CompletableFuture that is
3468 +     * completed exceptionally when source is.
3469 +     */
3470 +    public void testMinimalCompletionStage2() {
3471 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3472 +        CompletionStage<Integer> g = f.minimalCompletionStage();
3473 +        AtomicInteger x = new AtomicInteger();
3474 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3475 +        g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3476 +        checkIncomplete(f);
3477 +        CFException ex = new CFException();
3478 +        f.completeExceptionally(ex);
3479 +        checkCompletedExceptionally(f, ex);
3480 +        assertEquals(x.get(), 0);
3481 +        assertEquals(r.get().getCause(), ex);
3482 +    }
3483 +
3484 +    /**
3485 +     * failedStage returns a CompletionStage completed
3486 +     * exceptionally with the given Exception
3487 +     */
3488 +    public void testFailedStage() {
3489 +        CFException ex = new CFException();
3490 +        CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3491 +        AtomicInteger x = new AtomicInteger();
3492 +        AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3493 +        f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3494 +        assertEquals(x.get(), 0);
3495 +        assertEquals(r.get().getCause(), ex);
3496 +    }
3497 +
3498 +    /**
3499 +     * completeAsync completes with value of given supplier
3500 +     */
3501 +    public void testCompleteAsync() {
3502 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3503 +        f.completeAsync(() -> 1);
3504 +        f.join();
3505 +        checkCompletedNormally(f, 1);
3506 +    }
3507 +
3508 +    /**
3509 +     * completeAsync completes exceptionally if given supplier throws
3510 +     */
3511 +    public void testCompleteAsync2() {
3512 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3513 +        CFException ex = new CFException();
3514 +        f.completeAsync(() -> {if (true) throw ex; return 1;});
3515 +        try {
3516 +            f.join();
3517 +            shouldThrow();
3518 +        } catch (Exception success) {}
3519 +        checkCompletedWithWrappedCFException(f);
3520 +    }
3521 +
3522 +    /**
3523 +     * completeAsync with given executor completes with value of given supplier
3524 +     */
3525 +    public void testCompleteAsync3() {
3526 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3527 +        f.completeAsync(() -> 1, new ThreadExecutor());
3528 +        f.join();
3529 +        checkCompletedNormally(f, 1);
3530 +    }
3531 +
3532 +    /**
3533 +     * completeAsync with given executor completes exceptionally if
3534 +     * given supplier throws
3535 +     */
3536 +    public void testCompleteAsync4() {
3537 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3538 +        CFException ex = new CFException();
3539 +        f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor());
3540 +        try {
3541 +            f.join();
3542 +            shouldThrow();
3543 +        } catch (Exception success) {}
3544 +        checkCompletedWithWrappedCFException(f);
3545 +    }
3546 +
3547 +    /**
3548 +     * orTimeout completes with TimeoutException if not complete
3549 +     */
3550 +    public void testOrTimeout() {
3551 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3552 +        f.orTimeout(SHORT_DELAY_MS, MILLISECONDS);
3553 +        checkCompletedExceptionallyWithTimeout(f);
3554 +    }
3555 +
3556 +    /**
3557 +     * orTimeout completes normally if completed before timeout
3558 +     */
3559 +    public void testOrTimeout2() {
3560 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3561 +        f.complete(1);
3562 +        f.orTimeout(SHORT_DELAY_MS, MILLISECONDS);
3563 +        checkCompletedNormally(f, 1);
3564 +    }
3565 +
3566 +    /**
3567 +     * completeOnTimeout completes with given value if not complete
3568 +     */
3569 +    public void testCompleteOnTimeout() {
3570 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3571 +        f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS);
3572 +        f.join();
3573 +        checkCompletedNormally(f, -1);
3574 +    }
3575 +
3576 +    /**
3577 +     * completeOnTimeout has no effect if completed within timeout
3578 +     */
3579 +    public void testCompleteOnTimeout2() {
3580 +        CompletableFuture<Integer> f = new CompletableFuture<>();
3581 +        f.complete(1);
3582 +        f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS);
3583 +        checkCompletedNormally(f, 1);
3584 +    }
3585 +
3586 +    /**
3587 +     * delayedExecutor returns an executor that delays submission
3588 +     */
3589 +    public void testDelayedExecutor() throws Exception {
3590 +        long timeoutMillis = SMALL_DELAY_MS;
3591 +        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3592 +                                                       MILLISECONDS);
3593 +        long startTime = System.nanoTime();
3594 +        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3595 +        assertNull(f.getNow(null));
3596 +        assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS));
3597 +        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3598 +        checkCompletedNormally(f, 1);
3599 +    }
3600 +
3601 +    /**
3602 +     * delayedExecutor for a given executor returns an executor that
3603 +     * delays submission
3604 +     */
3605 +    public void testDelayedExecutor2() throws Exception {
3606 +        long timeoutMillis = SMALL_DELAY_MS;
3607 +        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3608 +                                                       MILLISECONDS,
3609 +                                                       new ThreadExecutor());
3610 +        long startTime = System.nanoTime();
3611 +        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3612 +        assertNull(f.getNow(null));
3613 +        assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS));
3614 +        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3615 +        checkCompletedNormally(f, 1);
3616 +    }
3617 +
3618      //--- tests of implementation details; not part of official tck ---
3619  
3620      Object resultOf(CompletableFuture<?> f) {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines