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.136 by jsr166, Sun Nov 15 20:17:11 2015 UTC vs.
Revision 1.142 by dl, Fri Apr 1 23:18:00 2016 UTC

# Line 640 | Line 640 | public class CompletableFutureTest exten
640  
641          ASYNC {
642              public void checkExecutionMode() {
643 <                assertEquals(defaultExecutorIsCommonPool,
644 <                             (ForkJoinPool.commonPool() == ForkJoinTask.getPool()));
643 >                // If tests are added that may run across different
644 >                // pools, this needs to be weakened to no-op.
645 >                ForkJoinPool p = ForkJoinTask.getPool();
646 >                assertTrue(p == null ||
647 >                           (defaultExecutorIsCommonPool &&
648 >                            p == ForkJoinPool.commonPool()));
649              }
650              public CompletableFuture<Void> runAsync(Runnable a) {
651                  return CompletableFuture.runAsync(a);
# Line 1040 | Line 1044 | public class CompletableFutureTest exten
1044  
1045          checkCompletedWithWrappedException(g, ex1);
1046          checkCompletedExceptionally(f, ex1);
1047 +        if (testImplementationDetails) {
1048 +            assertEquals(1, ex1.getSuppressed().length);
1049 +            assertSame(ex2, ex1.getSuppressed()[0]);
1050 +        }
1051          assertEquals(1, a.get());
1052      }}
1053  
# Line 3826 | Line 3834 | public class CompletableFutureTest exten
3834              AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3835          }
3836  
3837 <        // Monadic "plus"
3837 >        /** Implements "monadic plus". */
3838          static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3839                                               CompletableFuture<? extends T> g) {
3840              PlusFuture<T> plus = new PlusFuture<T>();
3841              BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3842 <                if (ex == null) {
3843 <                    if (plus.complete(result))
3844 <                        if (plus.firstFailure.get() != null)
3842 >                try {
3843 >                    if (ex == null) {
3844 >                        if (plus.complete(result))
3845 >                            if (plus.firstFailure.get() != null)
3846 >                                plus.firstFailure.set(null);
3847 >                    }
3848 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
3849 >                        if (plus.isDone())
3850                              plus.firstFailure.set(null);
3851 <                }
3852 <                else if (plus.firstFailure.compareAndSet(null, ex)) {
3853 <                    if (plus.isDone())
3854 <                        plus.firstFailure.set(null);
3855 <                }
3856 <                else {
3857 <                    // first failure has precedence
3858 <                    Throwable first = plus.firstFailure.getAndSet(null);
3859 <
3860 <                    // may fail with "Self-suppression not permitted"
3861 <                    try { first.addSuppressed(ex); }
3862 <                    catch (Exception ignored) {}
3863 <
3851 <                    plus.completeExceptionally(first);
3851 >                    }
3852 >                    else {
3853 >                        // first failure has precedence
3854 >                        Throwable first = plus.firstFailure.getAndSet(null);
3855 >
3856 >                        // may fail with "Self-suppression not permitted"
3857 >                        try { first.addSuppressed(ex); }
3858 >                        catch (Exception ignored) {}
3859 >
3860 >                        plus.completeExceptionally(first);
3861 >                    }
3862 >                } catch (Throwable unexpected) {
3863 >                    plus.completeExceptionally(unexpected);
3864                  }
3865              };
3866              f.whenComplete(action);
# Line 3917 | Line 3929 | public class CompletableFutureTest exten
3929                                   Monad.plus(godot, Monad.unit(5L)));
3930      }
3931  
3932 +    /**
3933 +     * A single CompletableFuture with many dependents.
3934 +     * A demo of scalability - runtime is O(n).
3935 +     */
3936 +    public void testManyDependents() throws Throwable {
3937 +        final int n = 1_000;
3938 +        final CompletableFuture<Void> head = new CompletableFuture<>();
3939 +        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
3940 +        final AtomicInteger count = new AtomicInteger(0);
3941 +        for (int i = 0; i < n; i++) {
3942 +            head.thenRun(() -> count.getAndIncrement());
3943 +            head.thenAccept((x) -> count.getAndIncrement());
3944 +            head.thenApply((x) -> count.getAndIncrement());
3945 +
3946 +            head.runAfterBoth(complete, () -> count.getAndIncrement());
3947 +            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
3948 +            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
3949 +            complete.runAfterBoth(head, () -> count.getAndIncrement());
3950 +            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
3951 +            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
3952 +
3953 +            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
3954 +            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3955 +            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3956 +            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
3957 +            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
3958 +            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
3959 +        }
3960 +        head.complete(null);
3961 +        assertEquals(5 * 3 * n, count.get());
3962 +    }
3963 +
3964   //     static <U> U join(CompletionStage<U> stage) {
3965   //         CompletableFuture<U> f = new CompletableFuture<>();
3966   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines