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.132 by jsr166, Sun Nov 15 19:37:48 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 911 | Line 916 | public class CompletableFutureTest exten
916          if (!createIncomplete) assertTrue(f.complete(v1));
917          final CompletableFuture<Integer> g = m.whenComplete
918              (f,
919 <             (Integer x, Throwable t) -> {
919 >             (Integer result, Throwable t) -> {
920                  m.checkExecutionMode();
921 <                threadAssertSame(x, v1);
921 >                threadAssertSame(result, v1);
922                  threadAssertNull(t);
923                  a.getAndIncrement();
924              });
# Line 938 | Line 943 | public class CompletableFutureTest exten
943          if (!createIncomplete) f.completeExceptionally(ex);
944          final CompletableFuture<Integer> g = m.whenComplete
945              (f,
946 <             (Integer x, Throwable t) -> {
946 >             (Integer result, Throwable t) -> {
947                  m.checkExecutionMode();
948 <                threadAssertNull(x);
948 >                threadAssertNull(result);
949                  threadAssertSame(t, ex);
950                  a.getAndIncrement();
951              });
# Line 965 | Line 970 | public class CompletableFutureTest exten
970          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
971          final CompletableFuture<Integer> g = m.whenComplete
972              (f,
973 <             (Integer x, Throwable t) -> {
973 >             (Integer result, Throwable t) -> {
974                  m.checkExecutionMode();
975 <                threadAssertNull(x);
975 >                threadAssertNull(result);
976                  threadAssertTrue(t instanceof CancellationException);
977                  a.getAndIncrement();
978              });
# Line 993 | Line 998 | public class CompletableFutureTest exten
998          if (!createIncomplete) assertTrue(f.complete(v1));
999          final CompletableFuture<Integer> g = m.whenComplete
1000              (f,
1001 <             (Integer x, Throwable t) -> {
1001 >             (Integer result, Throwable t) -> {
1002                  m.checkExecutionMode();
1003 <                threadAssertSame(x, v1);
1003 >                threadAssertSame(result, v1);
1004                  threadAssertNull(t);
1005                  a.getAndIncrement();
1006                  throw ex;
# Line 1010 | Line 1015 | public class CompletableFutureTest exten
1015      /**
1016       * If a whenComplete action throws an exception when triggered by
1017       * a source completion that also throws an exception, the source
1018 <     * exception takes precedence.
1018 >     * exception takes precedence (unlike handle)
1019       */
1020 <    public void testWhenComplete_actionFailedSourceFailed() {
1020 >    public void testWhenComplete_sourceFailedActionFailed() {
1021          for (boolean createIncomplete : new boolean[] { true, false })
1022          for (ExecutionMode m : ExecutionMode.values())
1023      {
# Line 1024 | Line 1029 | public class CompletableFutureTest exten
1029          if (!createIncomplete) f.completeExceptionally(ex1);
1030          final CompletableFuture<Integer> g = m.whenComplete
1031              (f,
1032 <             (Integer x, Throwable t) -> {
1032 >             (Integer result, Throwable t) -> {
1033                  m.checkExecutionMode();
1034                  threadAssertSame(t, ex1);
1035 <                threadAssertNull(x);
1035 >                threadAssertNull(result);
1036                  a.getAndIncrement();
1037                  throw ex2;
1038              });
# 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 1052 | Line 1059 | public class CompletableFutureTest exten
1059          if (!createIncomplete) assertTrue(f.complete(v1));
1060          final CompletableFuture<Integer> g = m.handle
1061              (f,
1062 <             (Integer x, Throwable t) -> {
1062 >             (Integer result, Throwable t) -> {
1063                  m.checkExecutionMode();
1064 <                threadAssertSame(x, v1);
1064 >                threadAssertSame(result, v1);
1065                  threadAssertNull(t);
1066                  a.getAndIncrement();
1067                  return inc(v1);
# Line 1081 | Line 1088 | public class CompletableFutureTest exten
1088          if (!createIncomplete) f.completeExceptionally(ex);
1089          final CompletableFuture<Integer> g = m.handle
1090              (f,
1091 <             (Integer x, Throwable t) -> {
1091 >             (Integer result, Throwable t) -> {
1092                  m.checkExecutionMode();
1093 <                threadAssertNull(x);
1093 >                threadAssertNull(result);
1094                  threadAssertSame(t, ex);
1095                  a.getAndIncrement();
1096                  return v1;
# Line 1110 | Line 1117 | public class CompletableFutureTest exten
1117          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1118          final CompletableFuture<Integer> g = m.handle
1119              (f,
1120 <             (Integer x, Throwable t) -> {
1120 >             (Integer result, Throwable t) -> {
1121                  m.checkExecutionMode();
1122 <                threadAssertNull(x);
1122 >                threadAssertNull(result);
1123                  threadAssertTrue(t instanceof CancellationException);
1124                  a.getAndIncrement();
1125                  return v1;
# Line 1125 | Line 1132 | public class CompletableFutureTest exten
1132      }}
1133  
1134      /**
1135 <     * handle result completes exceptionally if action does
1135 >     * If a "handle action" throws an exception when triggered by
1136 >     * a normal completion, it completes exceptionally
1137       */
1138 <    public void testHandle_sourceFailedActionFailed() {
1138 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1139          for (ExecutionMode m : ExecutionMode.values())
1140          for (boolean createIncomplete : new boolean[] { true, false })
1141 +        for (Integer v1 : new Integer[] { 1, null })
1142      {
1143          final CompletableFuture<Integer> f = new CompletableFuture<>();
1144          final AtomicInteger a = new AtomicInteger(0);
1145 <        final CFException ex1 = new CFException();
1146 <        final CFException ex2 = new CFException();
1138 <        if (!createIncomplete) f.completeExceptionally(ex1);
1145 >        final CFException ex = new CFException();
1146 >        if (!createIncomplete) assertTrue(f.complete(v1));
1147          final CompletableFuture<Integer> g = m.handle
1148              (f,
1149 <             (Integer x, Throwable t) -> {
1149 >             (Integer result, Throwable t) -> {
1150                  m.checkExecutionMode();
1151 <                threadAssertNull(x);
1152 <                threadAssertSame(ex1, t);
1151 >                threadAssertSame(result, v1);
1152 >                threadAssertNull(t);
1153                  a.getAndIncrement();
1154 <                throw ex2;
1154 >                throw ex;
1155              });
1156 <        if (createIncomplete) f.completeExceptionally(ex1);
1156 >        if (createIncomplete) assertTrue(f.complete(v1));
1157  
1158 <        checkCompletedWithWrappedException(g, ex2);
1159 <        checkCompletedExceptionally(f, ex1);
1158 >        checkCompletedWithWrappedException(g, ex);
1159 >        checkCompletedNormally(f, v1);
1160          assertEquals(1, a.get());
1161      }}
1162  
1163 <    public void testHandle_sourceCompletedNormallyActionFailed() {
1164 <        for (ExecutionMode m : ExecutionMode.values())
1163 >    /**
1164 >     * If a "handle action" throws an exception when triggered by
1165 >     * a source completion that also throws an exception, the action
1166 >     * exception takes precedence (unlike whenComplete)
1167 >     */
1168 >    public void testHandle_sourceFailedActionFailed() {
1169          for (boolean createIncomplete : new boolean[] { true, false })
1170 <        for (Integer v1 : new Integer[] { 1, null })
1170 >        for (ExecutionMode m : ExecutionMode.values())
1171      {
1160        final CompletableFuture<Integer> f = new CompletableFuture<>();
1172          final AtomicInteger a = new AtomicInteger(0);
1173 <        final CFException ex = new CFException();
1174 <        if (!createIncomplete) assertTrue(f.complete(v1));
1173 >        final CFException ex1 = new CFException();
1174 >        final CFException ex2 = new CFException();
1175 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1176 >
1177 >        if (!createIncomplete) f.completeExceptionally(ex1);
1178          final CompletableFuture<Integer> g = m.handle
1179              (f,
1180 <             (Integer x, Throwable t) -> {
1180 >             (Integer result, Throwable t) -> {
1181                  m.checkExecutionMode();
1182 <                threadAssertSame(x, v1);
1183 <                threadAssertNull(t);
1182 >                threadAssertNull(result);
1183 >                threadAssertSame(ex1, t);
1184                  a.getAndIncrement();
1185 <                throw ex;
1185 >                throw ex2;
1186              });
1187 <        if (createIncomplete) assertTrue(f.complete(v1));
1187 >        if (createIncomplete) f.completeExceptionally(ex1);
1188  
1189 <        checkCompletedWithWrappedException(g, ex);
1190 <        checkCompletedNormally(f, v1);
1189 >        checkCompletedWithWrappedException(g, ex2);
1190 >        checkCompletedExceptionally(f, ex1);
1191          assertEquals(1, a.get());
1192      }}
1193  
# Line 3644 | Line 3658 | public class CompletableFutureTest exten
3658          funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3659          funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3660  
3661 <        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3661 >        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3662  
3663          funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3664  
# Line 3814 | 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 <
3839 <                    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