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.141 by jsr166, Tue Mar 29 04:42:54 2016 UTC

# Line 1040 | 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 3826 | Line 3830 | public class CompletableFutureTest exten
3830              AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3831          }
3832  
3833 <        // Monadic "plus"
3833 >        /** Implements "monadic plus". */
3834          static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3835                                               CompletableFuture<? extends T> g) {
3836              PlusFuture<T> plus = new PlusFuture<T>();
3837              BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3838 <                if (ex == null) {
3839 <                    if (plus.complete(result))
3840 <                        if (plus.firstFailure.get() != null)
3838 >                try {
3839 >                    if (ex == null) {
3840 >                        if (plus.complete(result))
3841 >                            if (plus.firstFailure.get() != null)
3842 >                                plus.firstFailure.set(null);
3843 >                    }
3844 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
3845 >                        if (plus.isDone())
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 {
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 <
3851 <                    plus.completeExceptionally(first);
3847 >                    }
3848 >                    else {
3849 >                        // first failure has precedence
3850 >                        Throwable first = plus.firstFailure.getAndSet(null);
3851 >
3852 >                        // may fail with "Self-suppression not permitted"
3853 >                        try { first.addSuppressed(ex); }
3854 >                        catch (Exception ignored) {}
3855 >
3856 >                        plus.completeExceptionally(first);
3857 >                    }
3858 >                } catch (Throwable unexpected) {
3859 >                    plus.completeExceptionally(unexpected);
3860                  }
3861              };
3862              f.whenComplete(action);
# Line 3917 | Line 3925 | public class CompletableFutureTest exten
3925                                   Monad.plus(godot, Monad.unit(5L)));
3926      }
3927  
3928 +    /**
3929 +     * A single CompletableFuture with many dependents.
3930 +     * A demo of scalability - runtime is O(n).
3931 +     */
3932 +    public void testManyDependents() throws Throwable {
3933 +        final int n = 1_000;
3934 +        final CompletableFuture<Void> head = new CompletableFuture<>();
3935 +        final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null);
3936 +        final AtomicInteger count = new AtomicInteger(0);
3937 +        for (int i = 0; i < n; i++) {
3938 +            head.thenRun(() -> count.getAndIncrement());
3939 +            head.thenAccept((x) -> count.getAndIncrement());
3940 +            head.thenApply((x) -> count.getAndIncrement());
3941 +
3942 +            head.runAfterBoth(complete, () -> count.getAndIncrement());
3943 +            head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement());
3944 +            head.thenCombine(complete, (x, y) -> count.getAndIncrement());
3945 +            complete.runAfterBoth(head, () -> count.getAndIncrement());
3946 +            complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement());
3947 +            complete.thenCombine(head, (x, y) -> count.getAndIncrement());
3948 +
3949 +            head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement());
3950 +            head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3951 +            head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement());
3952 +            new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement());
3953 +            new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement());
3954 +            new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement());
3955 +        }
3956 +        head.complete(null);
3957 +        assertEquals(5 * 3 * n, count.get());
3958 +    }
3959 +
3960   //     static <U> U join(CompletionStage<U> stage) {
3961   //         CompletableFuture<U> f = new CompletableFuture<>();
3962   //         stage.whenComplete((v, ex) -> {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines