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.138 by jsr166, Sun Nov 15 23:31:51 2015 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 +        assertEquals(1, ex1.getSuppressed().length);
1044 +        assertSame(ex2, ex1.getSuppressed()[0]);
1045          assertEquals(1, a.get());
1046      }}
1047  
# Line 3821 | Line 3828 | public class CompletableFutureTest exten
3828              AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3829          }
3830  
3831 <        // Monadic "plus"
3831 >        /** Implements "monadic plus". */
3832          static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3833                                               CompletableFuture<? extends T> g) {
3834              PlusFuture<T> plus = new PlusFuture<T>();
3835              BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3836 <                if (ex == null) {
3837 <                    if (plus.complete(result))
3838 <                        if (plus.firstFailure.get() != null)
3836 >                try {
3837 >                    if (ex == null) {
3838 >                        if (plus.complete(result))
3839 >                            if (plus.firstFailure.get() != null)
3840 >                                plus.firstFailure.set(null);
3841 >                    }
3842 >                    else if (plus.firstFailure.compareAndSet(null, ex)) {
3843 >                        if (plus.isDone())
3844                              plus.firstFailure.set(null);
3845 <                }
3846 <                else if (plus.firstFailure.compareAndSet(null, ex)) {
3847 <                    if (plus.isDone())
3848 <                        plus.firstFailure.set(null);
3849 <                }
3850 <                else {
3851 <                    // first failure has precedence
3852 <                    Throwable first = plus.firstFailure.getAndSet(null);
3853 <
3854 <                    // may fail with "Self-suppression not permitted"
3855 <                    try { first.addSuppressed(ex); }
3856 <                    catch (Exception ignored) {}
3857 <
3846 <                    plus.completeExceptionally(first);
3845 >                    }
3846 >                    else {
3847 >                        // first failure has precedence
3848 >                        Throwable first = plus.firstFailure.getAndSet(null);
3849 >
3850 >                        // may fail with "Self-suppression not permitted"
3851 >                        try { first.addSuppressed(ex); }
3852 >                        catch (Exception ignored) {}
3853 >
3854 >                        plus.completeExceptionally(first);
3855 >                    }
3856 >                } catch (Throwable unexpected) {
3857 >                    plus.completeExceptionally(unexpected);
3858                  }
3859              };
3860              f.whenComplete(action);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines