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.135 by jsr166, Sun Nov 15 20:03:08 2015 UTC vs.
Revision 1.148 by jsr166, Sun Jun 26 15:46:22 2016 UTC

# Line 876 | Line 876 | public class CompletableFutureTest exten
876          assertEquals(1, a.get());
877      }}
878  
879 +    /**
880 +     * If an "exceptionally action" throws an exception, it completes
881 +     * exceptionally with that exception
882 +     */
883      public void testExceptionally_exceptionalCompletionActionFailed() {
884          for (boolean createIncomplete : new boolean[] { true, false })
885      {
# Line 894 | Line 898 | public class CompletableFutureTest exten
898          if (createIncomplete) f.completeExceptionally(ex1);
899  
900          checkCompletedWithWrappedException(g, ex2);
901 +        checkCompletedExceptionally(f, ex1);
902          assertEquals(1, a.get());
903      }}
904  
# Line 1035 | Line 1040 | public class CompletableFutureTest exten
1040  
1041          checkCompletedWithWrappedException(g, ex1);
1042          checkCompletedExceptionally(f, ex1);
1043 +        if (testImplementationDetails) {
1044 +            assertEquals(1, ex1.getSuppressed().length);
1045 +            assertSame(ex2, ex1.getSuppressed()[0]);
1046 +        }
1047          assertEquals(1, a.get());
1048      }}
1049  
# Line 3286 | Line 3295 | public class CompletableFutureTest exten
3295              () -> f.obtrudeException(null),
3296  
3297              () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3298 <            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3298 >            () -> CompletableFuture.delayedExecutor(1L, null, exec),
3299              () -> CompletableFuture.delayedExecutor(1L, null),
3300  
3301              () -> f.orTimeout(1L, null),
# Line 3516 | Line 3525 | public class CompletableFutureTest exten
3525          long timeoutMillis = timeoutMillis();
3526          CompletableFuture<Integer> f = new CompletableFuture<>();
3527          long startTime = System.nanoTime();
3528 <        f.orTimeout(timeoutMillis, MILLISECONDS);
3528 >        assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS));
3529          checkCompletedWithTimeoutException(f);
3530          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3531      }
# Line 3531 | Line 3540 | public class CompletableFutureTest exten
3540          CompletableFuture<Integer> g = new CompletableFuture<>();
3541          long startTime = System.nanoTime();
3542          f.complete(v1);
3543 <        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3544 <        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3543 >        assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3544 >        assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS));
3545          g.complete(v1);
3546          checkCompletedNormally(f, v1);
3547          checkCompletedNormally(g, v1);
# Line 3547 | Line 3556 | public class CompletableFutureTest exten
3556                         () -> testCompleteOnTimeout_timesOut(null));
3557      }
3558  
3559 +    /**
3560 +     * completeOnTimeout completes with given value if not complete
3561 +     */
3562      public void testCompleteOnTimeout_timesOut(Integer v) {
3563          long timeoutMillis = timeoutMillis();
3564          CompletableFuture<Integer> f = new CompletableFuture<>();
3565          long startTime = System.nanoTime();
3566 <        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3566 >        assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS));
3567          assertSame(v, f.join());
3568          assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3569          f.complete(99);         // should have no effect
# Line 3568 | Line 3580 | public class CompletableFutureTest exten
3580          CompletableFuture<Integer> g = new CompletableFuture<>();
3581          long startTime = System.nanoTime();
3582          f.complete(v1);
3583 <        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3584 <        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3583 >        assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3584 >        assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS));
3585          g.complete(v1);
3586          checkCompletedNormally(f, v1);
3587          checkCompletedNormally(g, v1);
# Line 3620 | Line 3632 | public class CompletableFutureTest exten
3632      //--- tests of implementation details; not part of official tck ---
3633  
3634      Object resultOf(CompletableFuture<?> f) {
3635 +        SecurityManager sm = System.getSecurityManager();
3636 +        if (sm != null) {
3637 +            try {
3638 +                System.setSecurityManager(null);
3639 +            } catch (SecurityException giveUp) {
3640 +                return "Reflection not available";
3641 +            }
3642 +        }
3643 +
3644          try {
3645              java.lang.reflect.Field resultField
3646                  = CompletableFuture.class.getDeclaredField("result");
3647              resultField.setAccessible(true);
3648              return resultField.get(f);
3649 <        } catch (Throwable t) { throw new AssertionError(t); }
3649 >        } catch (Throwable t) {
3650 >            throw new AssertionError(t);
3651 >        } finally {
3652 >            if (sm != null) System.setSecurityManager(sm);
3653 >        }
3654      }
3655  
3656      public void testExceptionPropagationReusesResultObject() {
# Line 3648 | Line 3673 | public class CompletableFutureTest exten
3673          funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m)));
3674  
3675          funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m)));
3676 +        funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m)));
3677          funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3678 +        funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m)));
3679          funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3680 +        funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m)));
3681  
3682          funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3683  
3684          funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3685  
3686          funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42}));
3687 +        funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y}));
3688          funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete}));
3689 +        funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y}));
3690  
3691          for (Function<CompletableFuture<Integer>, CompletableFuture<?>>
3692                   fun : funs) {
# Line 3821 | Line 3851 | public class CompletableFutureTest exten
3851              AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3852          }
3853  
3854 <        // Monadic "plus"
3854 >        /** Implements "monadic plus". */
3855          static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3856                                               CompletableFuture<? extends T> g) {
3857              PlusFuture<T> plus = new PlusFuture<T>();
3858              BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3859 <                if (ex == null) {
3860 <                    if (plus.complete(result))
3861 <                        if (plus.firstFailure.get() != null)
3859 >                try {
3860 >                    if (ex == null) {
3861 >                        if (plus.complete(result))
3862 >                            if (plus.firstFailure.get() != null)
3863 >                                plus.firstFailure.set(null);
3864 >                    }
3865 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
3866 >                        if (plus.isDone())
3867                              plus.firstFailure.set(null);
3868 <                }
3869 <                else if (plus.firstFailure.compareAndSet(null, ex)) {
3870 <                    if (plus.isDone())
3871 <                        plus.firstFailure.set(null);
3872 <                }
3873 <                else {
3874 <                    // first failure has precedence
3875 <                    Throwable first = plus.firstFailure.getAndSet(null);
3876 <
3877 <                    // may fail with "Self-suppression not permitted"
3878 <                    try { first.addSuppressed(ex); }
3879 <                    catch (Exception ignored) {}
3880 <
3846 <                    plus.completeExceptionally(first);
3868 >                    }
3869 >                    else {
3870 >                        // first failure has precedence
3871 >                        Throwable first = plus.firstFailure.getAndSet(null);
3872 >
3873 >                        // may fail with "Self-suppression not permitted"
3874 >                        try { first.addSuppressed(ex); }
3875 >                        catch (Exception ignored) {}
3876 >
3877 >                        plus.completeExceptionally(first);
3878 >                    }
3879 >                } catch (Throwable unexpected) {
3880 >                    plus.completeExceptionally(unexpected);
3881                  }
3882              };
3883              f.whenComplete(action);
# Line 3912 | Line 3946 | public class CompletableFutureTest exten
3946                                   Monad.plus(godot, Monad.unit(5L)));
3947      }
3948  
3949 +    /**
3950 +     * A single CompletableFuture with many dependents.
3951 +     * A demo of scalability - runtime is O(n).
3952 +     */
3953 +    public void testManyDependents() throws Throwable {
3954 +        final int n = 1_000;
3955 +        final CompletableFuture<Void> head = new CompletableFuture<>();
3956 +        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
3957 +        final AtomicInteger count = new AtomicInteger(0);
3958 +        for (int i = 0; i < n; i++) {
3959 +            head.thenRun(() -> count.getAndIncrement());
3960 +            head.thenAccept((x) -> count.getAndIncrement());
3961 +            head.thenApply((x) -> count.getAndIncrement());
3962 +
3963 +            head.runAfterBoth(complete, () -> count.getAndIncrement());
3964 +            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
3965 +            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
3966 +            complete.runAfterBoth(head, () -> count.getAndIncrement());
3967 +            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
3968 +            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
3969 +
3970 +            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
3971 +            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3972 +            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3973 +            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
3974 +            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
3975 +            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
3976 +        }
3977 +        head.complete(null);
3978 +        assertEquals(5 * 3 * n, count.get());
3979 +    }
3980 +
3981   //     static <U> U join(CompletionStage<U> stage) {
3982   //         CompletableFuture<U> f = new CompletableFuture<>();
3983   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines