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.110 by dl, Fri Sep 4 10:56:14 2015 UTC vs.
Revision 1.138 by jsr166, Sun Nov 15 23:31:51 2015 UTC

# Line 7 | Line 7
7  
8   import static java.util.concurrent.TimeUnit.MILLISECONDS;
9   import static java.util.concurrent.TimeUnit.SECONDS;
10 + import static java.util.concurrent.CompletableFuture.completedFuture;
11 + import static java.util.concurrent.CompletableFuture.failedFuture;
12 +
13 + import java.lang.reflect.Method;
14 + import java.lang.reflect.Modifier;
15 +
16 + import java.util.stream.Collectors;
17 + import java.util.stream.Stream;
18  
19   import java.util.ArrayList;
20 + import java.util.Arrays;
21   import java.util.List;
22   import java.util.Objects;
23 + import java.util.Set;
24   import java.util.concurrent.Callable;
25   import java.util.concurrent.CancellationException;
26   import java.util.concurrent.CompletableFuture;
# Line 28 | Line 38 | import java.util.function.BiConsumer;
38   import java.util.function.BiFunction;
39   import java.util.function.Consumer;
40   import java.util.function.Function;
41 + import java.util.function.Predicate;
42   import java.util.function.Supplier;
43  
44 + import junit.framework.AssertionFailedError;
45   import junit.framework.Test;
46   import junit.framework.TestSuite;
47  
# Line 77 | Line 89 | public class CompletableFutureTest exten
89          assertTrue(f.toString().contains("[Completed normally]"));
90      }
91  
92 <    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
93 <        long startTime = System.nanoTime();
94 <        long timeoutMillis = LONG_DELAY_MS;
95 <        try {
96 <            f.get(timeoutMillis, MILLISECONDS);
97 <            shouldThrow();
98 <        } catch (ExecutionException success) {
99 <            assertTrue(success.getCause() instanceof CFException);
100 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
101 <        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
102 <
103 <        try {
104 <            f.join();
105 <            shouldThrow();
106 <        } catch (CompletionException success) {
107 <            assertTrue(success.getCause() instanceof CFException);
108 <        }
97 <        try {
98 <            f.getNow(null);
99 <            shouldThrow();
100 <        } catch (CompletionException success) {
101 <            assertTrue(success.getCause() instanceof CFException);
92 >    /**
93 >     * Returns the "raw" internal exceptional completion of f,
94 >     * without any additional wrapping with CompletionException.
95 >     */
96 >    <U> Throwable exceptionalCompletion(CompletableFuture<U> f) {
97 >        // handle (and whenComplete) can distinguish between "direct"
98 >        // and "wrapped" exceptional completion
99 >        return f.handle((U u, Throwable t) -> t).join();
100 >    }
101 >
102 >    void checkCompletedExceptionally(CompletableFuture<?> f,
103 >                                     boolean wrapped,
104 >                                     Consumer<Throwable> checker) {
105 >        Throwable cause = exceptionalCompletion(f);
106 >        if (wrapped) {
107 >            assertTrue(cause instanceof CompletionException);
108 >            cause = cause.getCause();
109          }
110 <        try {
104 <            f.get();
105 <            shouldThrow();
106 <        } catch (ExecutionException success) {
107 <            assertTrue(success.getCause() instanceof CFException);
108 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
109 <        assertTrue(f.isDone());
110 <        assertFalse(f.isCancelled());
111 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
112 <    }
110 >        checker.accept(cause);
111  
114    <U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f,
115                                                      Throwable ex) {
112          long startTime = System.nanoTime();
117        long timeoutMillis = LONG_DELAY_MS;
113          try {
114 <            f.get(timeoutMillis, MILLISECONDS);
114 >            f.get(LONG_DELAY_MS, MILLISECONDS);
115              shouldThrow();
116          } catch (ExecutionException success) {
117 <            assertSame(ex, success.getCause());
117 >            assertSame(cause, success.getCause());
118          } catch (Throwable fail) { threadUnexpectedException(fail); }
119 <        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
119 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
120  
121          try {
122              f.join();
123              shouldThrow();
124          } catch (CompletionException success) {
125 <            assertSame(ex, success.getCause());
126 <        }
125 >            assertSame(cause, success.getCause());
126 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
127 >
128          try {
129              f.getNow(null);
130              shouldThrow();
131          } catch (CompletionException success) {
132 <            assertSame(ex, success.getCause());
133 <        }
132 >            assertSame(cause, success.getCause());
133 >        } catch (Throwable fail) { threadUnexpectedException(fail); }
134 >
135          try {
136              f.get();
137              shouldThrow();
138          } catch (ExecutionException success) {
139 <            assertSame(ex, success.getCause());
139 >            assertSame(cause, success.getCause());
140          } catch (Throwable fail) { threadUnexpectedException(fail); }
141  
145        assertTrue(f.isDone());
142          assertFalse(f.isCancelled());
143 +        assertTrue(f.isDone());
144 +        assertTrue(f.isCompletedExceptionally());
145          assertTrue(f.toString().contains("[Completed exceptionally]"));
146      }
147  
148 <    <U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) {
149 <        long startTime = System.nanoTime();
150 <        long timeoutMillis = LONG_DELAY_MS;
151 <        try {
154 <            f.get(timeoutMillis, MILLISECONDS);
155 <            shouldThrow();
156 <        } catch (ExecutionException ex) {
157 <            assertTrue(ex.getCause() instanceof TimeoutException);
158 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
159 <        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
160 <
161 <        try {
162 <            f.join();
163 <            shouldThrow();
164 <        } catch (Throwable ex) {
165 <            assertTrue(ex.getCause() instanceof TimeoutException);
166 <        }
167 <
168 <        try {
169 <            f.getNow(null);
170 <            shouldThrow();
171 <        } catch (Throwable ex) {
172 <            assertTrue(ex.getCause() instanceof TimeoutException);
173 <        }
148 >    void checkCompletedWithWrappedCFException(CompletableFuture<?> f) {
149 >        checkCompletedExceptionally(f, true,
150 >            (t) -> assertTrue(t instanceof CFException));
151 >    }
152  
153 <        try {
154 <            f.get();
155 <            shouldThrow();
156 <        } catch (ExecutionException ex) {
179 <            assertTrue(ex.getCause() instanceof TimeoutException);
180 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
153 >    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
154 >        checkCompletedExceptionally(f, true,
155 >            (t) -> assertTrue(t instanceof CancellationException));
156 >    }
157  
158 <        assertTrue(f.isDone());
159 <        assertFalse(f.isCancelled());
160 <        assertTrue(f.toString().contains("[Completed exceptionally]"));
158 >    void checkCompletedWithTimeoutException(CompletableFuture<?> f) {
159 >        checkCompletedExceptionally(f, false,
160 >            (t) -> assertTrue(t instanceof TimeoutException));
161      }
162  
163 <    <U> void checkCompletedWithWrappedException(CompletableFuture<U> f,
164 <                                                Throwable ex) {
165 <        checkCompletedExceptionallyWithRootCause(f, ex);
190 <        try {
191 <            CompletableFuture<Throwable> spy = f.handle
192 <                ((U u, Throwable t) -> t);
193 <            assertTrue(spy.join() instanceof CompletionException);
194 <            assertSame(ex, spy.join().getCause());
195 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
163 >    void checkCompletedWithWrappedException(CompletableFuture<?> f,
164 >                                            Throwable ex) {
165 >        checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex));
166      }
167  
168 <    <U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) {
169 <        checkCompletedExceptionallyWithRootCause(f, ex);
200 <        try {
201 <            CompletableFuture<Throwable> spy = f.handle
202 <                ((U u, Throwable t) -> t);
203 <            assertSame(ex, spy.join());
204 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
168 >    void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) {
169 >        checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex));
170      }
171  
172      void checkCancelled(CompletableFuture<?> f) {
173          long startTime = System.nanoTime();
209        long timeoutMillis = LONG_DELAY_MS;
174          try {
175 <            f.get(timeoutMillis, MILLISECONDS);
175 >            f.get(LONG_DELAY_MS, MILLISECONDS);
176              shouldThrow();
177          } catch (CancellationException success) {
178          } catch (Throwable fail) { threadUnexpectedException(fail); }
179 <        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
179 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
180  
181          try {
182              f.join();
# Line 227 | Line 191 | public class CompletableFutureTest exten
191              shouldThrow();
192          } catch (CancellationException success) {
193          } catch (Throwable fail) { threadUnexpectedException(fail); }
230        assertTrue(f.isDone());
231        assertTrue(f.isCompletedExceptionally());
232        assertTrue(f.isCancelled());
233        assertTrue(f.toString().contains("[Completed exceptionally]"));
234    }
194  
195 <    void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) {
237 <        long startTime = System.nanoTime();
238 <        long timeoutMillis = LONG_DELAY_MS;
239 <        try {
240 <            f.get(timeoutMillis, MILLISECONDS);
241 <            shouldThrow();
242 <        } catch (ExecutionException success) {
243 <            assertTrue(success.getCause() instanceof CancellationException);
244 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
245 <        assertTrue(millisElapsedSince(startTime) < timeoutMillis/2);
195 >        assertTrue(exceptionalCompletion(f) instanceof CancellationException);
196  
247        try {
248            f.join();
249            shouldThrow();
250        } catch (CompletionException success) {
251            assertTrue(success.getCause() instanceof CancellationException);
252        }
253        try {
254            f.getNow(null);
255            shouldThrow();
256        } catch (CompletionException success) {
257            assertTrue(success.getCause() instanceof CancellationException);
258        }
259        try {
260            f.get();
261            shouldThrow();
262        } catch (ExecutionException success) {
263            assertTrue(success.getCause() instanceof CancellationException);
264        } catch (Throwable fail) { threadUnexpectedException(fail); }
197          assertTrue(f.isDone());
266        assertFalse(f.isCancelled());
198          assertTrue(f.isCompletedExceptionally());
199 +        assertTrue(f.isCancelled());
200          assertTrue(f.toString().contains("[Completed exceptionally]"));
201      }
202  
# Line 908 | Line 840 | public class CompletableFutureTest exten
840          if (!createIncomplete) assertTrue(f.complete(v1));
841          final CompletableFuture<Integer> g = f.exceptionally
842              ((Throwable t) -> {
911                // Should not be called
843                  a.getAndIncrement();
844 <                throw new AssertionError();
844 >                threadFail("should not be called");
845 >                return null;            // unreached
846              });
847          if (createIncomplete) assertTrue(f.complete(v1));
848  
# Line 944 | 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 962 | 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 969 | Line 906 | public class CompletableFutureTest exten
906       * whenComplete action executes on normal completion, propagating
907       * source result.
908       */
909 <    public void testWhenComplete_normalCompletion1() {
909 >    public void testWhenComplete_normalCompletion() {
910          for (ExecutionMode m : ExecutionMode.values())
911          for (boolean createIncomplete : new boolean[] { true, false })
912          for (Integer v1 : new Integer[] { 1, null })
# Line 979 | 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 1006 | 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 1033 | 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 1050 | Line 987 | public class CompletableFutureTest exten
987       * If a whenComplete action throws an exception when triggered by
988       * a normal completion, it completes exceptionally
989       */
990 <    public void testWhenComplete_actionFailed() {
990 >    public void testWhenComplete_sourceCompletedNormallyActionFailed() {
991          for (boolean createIncomplete : new boolean[] { true, false })
992          for (ExecutionMode m : ExecutionMode.values())
993          for (Integer v1 : new Integer[] { 1, null })
# Line 1061 | 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 1078 | 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 1092 | 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 1103 | 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 1120 | 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 1149 | 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 1178 | 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 1193 | 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();
1206 <        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      {
1228        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 3142 | Line 3088 | public class CompletableFutureTest exten
3088              for (int i = 0; i < k; i++) {
3089                  checkIncomplete(f);
3090                  checkIncomplete(CompletableFuture.allOf(fs));
3091 <                if (i != k/2) {
3091 >                if (i != k / 2) {
3092                      fs[i].complete(i);
3093                      checkCompletedNormally(fs[i], i);
3094                  } else {
# Line 3345 | Line 3291 | public class CompletableFutureTest exten
3291              () -> CompletableFuture.anyOf(null, f),
3292  
3293              () -> f.obtrudeException(null),
3294 +
3295 +            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3296 +            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3297 +            () -> CompletableFuture.delayedExecutor(1L, null),
3298 +
3299 +            () -> f.orTimeout(1L, null),
3300 +            () -> f.completeOnTimeout(42, 1L, null),
3301 +
3302 +            () -> CompletableFuture.failedFuture(null),
3303 +            () -> CompletableFuture.failedStage(null),
3304          };
3305  
3306          assertThrows(NullPointerException.class, throwingActions);
# Line 3365 | Line 3321 | public class CompletableFutureTest exten
3321       * newIncompleteFuture returns an incomplete CompletableFuture
3322       */
3323      public void testNewIncompleteFuture() {
3324 +        for (Integer v1 : new Integer[] { 1, null })
3325 +    {
3326          CompletableFuture<Integer> f = new CompletableFuture<>();
3327          CompletableFuture<Integer> g = f.newIncompleteFuture();
3328          checkIncomplete(f);
3329          checkIncomplete(g);
3330 <    }
3330 >        f.complete(v1);
3331 >        checkCompletedNormally(f, v1);
3332 >        checkIncomplete(g);
3333 >        g.complete(v1);
3334 >        checkCompletedNormally(g, v1);
3335 >        assertSame(g.getClass(), CompletableFuture.class);
3336 >    }}
3337  
3338      /**
3339       * completedStage returns a completed CompletionStage
3340       */
3341      public void testCompletedStage() {
3342 <        AtomicInteger x = new AtomicInteger();
3342 >        AtomicInteger x = new AtomicInteger(0);
3343          AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3344          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3345          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
# Line 3393 | Line 3357 | public class CompletableFutureTest exten
3357          Executor c = ForkJoinPool.commonPool();
3358          if (ForkJoinPool.getCommonPoolParallelism() > 1)
3359              assertSame(e, c);
3360 +        else
3361 +            assertNotSame(e, c);
3362      }
3363  
3364      /**
# Line 3402 | Line 3368 | public class CompletableFutureTest exten
3368      public void testFailedFuture() {
3369          CFException ex = new CFException();
3370          CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3371 <        checkCompletedExceptionallyWithRootCause(f, ex);
3371 >        checkCompletedExceptionally(f, ex);
3372      }
3373  
3374      /**
3375       * failedFuture(null) throws NPE
3376       */
3377 <    public void testFailedFuture2() {
3377 >    public void testFailedFuture_null() {
3378          try {
3379              CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3380              shouldThrow();
# Line 3441 | Line 3407 | public class CompletableFutureTest exten
3407          CFException ex = new CFException();
3408          f.completeExceptionally(ex);
3409          checkCompletedExceptionally(f, ex);
3410 <        checkCompletedWithWrappedCFException(g);
3410 >        checkCompletedWithWrappedException(g, ex);
3411      }
3412  
3413      /**
# Line 3451 | Line 3417 | public class CompletableFutureTest exten
3417      public void testMinimalCompletionStage() {
3418          CompletableFuture<Integer> f = new CompletableFuture<>();
3419          CompletionStage<Integer> g = f.minimalCompletionStage();
3420 <        AtomicInteger x = new AtomicInteger();
3420 >        AtomicInteger x = new AtomicInteger(0);
3421          AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3422          checkIncomplete(f);
3423          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
# Line 3468 | Line 3434 | public class CompletableFutureTest exten
3434      public void testMinimalCompletionStage2() {
3435          CompletableFuture<Integer> f = new CompletableFuture<>();
3436          CompletionStage<Integer> g = f.minimalCompletionStage();
3437 <        AtomicInteger x = new AtomicInteger();
3437 >        AtomicInteger x = new AtomicInteger(0);
3438          AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3439          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3440          checkIncomplete(f);
# Line 3486 | Line 3452 | public class CompletableFutureTest exten
3452      public void testFailedStage() {
3453          CFException ex = new CFException();
3454          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3455 <        AtomicInteger x = new AtomicInteger();
3455 >        AtomicInteger x = new AtomicInteger(0);
3456          AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3457          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3458          assertEquals(x.get(), 0);
3459 <        assertEquals(r.get().getCause(), ex);
3459 >        assertEquals(r.get(), ex);
3460      }
3461  
3462      /**
3463       * completeAsync completes with value of given supplier
3464       */
3465      public void testCompleteAsync() {
3466 +        for (Integer v1 : new Integer[] { 1, null })
3467 +    {
3468          CompletableFuture<Integer> f = new CompletableFuture<>();
3469 <        f.completeAsync(() -> 1);
3469 >        f.completeAsync(() -> v1);
3470          f.join();
3471 <        checkCompletedNormally(f, 1);
3472 <    }
3471 >        checkCompletedNormally(f, v1);
3472 >    }}
3473  
3474      /**
3475       * completeAsync completes exceptionally if given supplier throws
# Line 3513 | Line 3481 | public class CompletableFutureTest exten
3481          try {
3482              f.join();
3483              shouldThrow();
3484 <        } catch (Exception success) {}
3485 <        checkCompletedWithWrappedCFException(f);
3484 >        } catch (CompletionException success) {}
3485 >        checkCompletedWithWrappedException(f, ex);
3486      }
3487  
3488      /**
3489       * completeAsync with given executor completes with value of given supplier
3490       */
3491      public void testCompleteAsync3() {
3492 +        for (Integer v1 : new Integer[] { 1, null })
3493 +    {
3494          CompletableFuture<Integer> f = new CompletableFuture<>();
3495 <        f.completeAsync(() -> 1, new ThreadExecutor());
3496 <        f.join();
3497 <        checkCompletedNormally(f, 1);
3498 <    }
3495 >        ThreadExecutor executor = new ThreadExecutor();
3496 >        f.completeAsync(() -> v1, executor);
3497 >        assertSame(v1, f.join());
3498 >        checkCompletedNormally(f, v1);
3499 >        assertEquals(1, executor.count.get());
3500 >    }}
3501  
3502      /**
3503       * completeAsync with given executor completes exceptionally if
# Line 3534 | Line 3506 | public class CompletableFutureTest exten
3506      public void testCompleteAsync4() {
3507          CompletableFuture<Integer> f = new CompletableFuture<>();
3508          CFException ex = new CFException();
3509 <        f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor());
3509 >        ThreadExecutor executor = new ThreadExecutor();
3510 >        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3511          try {
3512              f.join();
3513              shouldThrow();
3514 <        } catch (Exception success) {}
3515 <        checkCompletedWithWrappedCFException(f);
3514 >        } catch (CompletionException success) {}
3515 >        checkCompletedWithWrappedException(f, ex);
3516 >        assertEquals(1, executor.count.get());
3517      }
3518  
3519      /**
3520       * orTimeout completes with TimeoutException if not complete
3521       */
3522 <    public void testOrTimeout() {
3522 >    public void testOrTimeout_timesOut() {
3523 >        long timeoutMillis = timeoutMillis();
3524          CompletableFuture<Integer> f = new CompletableFuture<>();
3525 <        f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3526 <        checkCompletedExceptionallyWithTimeout(f);
3525 >        long startTime = System.nanoTime();
3526 >        f.orTimeout(timeoutMillis, MILLISECONDS);
3527 >        checkCompletedWithTimeoutException(f);
3528 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3529      }
3530  
3531      /**
3532       * orTimeout completes normally if completed before timeout
3533       */
3534 <    public void testOrTimeout2() {
3534 >    public void testOrTimeout_completed() {
3535 >        for (Integer v1 : new Integer[] { 1, null })
3536 >    {
3537          CompletableFuture<Integer> f = new CompletableFuture<>();
3538 <        f.complete(1);
3539 <        f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3540 <        checkCompletedNormally(f, 1);
3541 <    }
3538 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3539 >        long startTime = System.nanoTime();
3540 >        f.complete(v1);
3541 >        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3542 >        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3543 >        g.complete(v1);
3544 >        checkCompletedNormally(f, v1);
3545 >        checkCompletedNormally(g, v1);
3546 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3547 >    }}
3548  
3549      /**
3550       * completeOnTimeout completes with given value if not complete
3551       */
3552 <    public void testCompleteOnTimeout() {
3552 >    public void testCompleteOnTimeout_timesOut() {
3553 >        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3554 >                       () -> testCompleteOnTimeout_timesOut(null));
3555 >    }
3556 >
3557 >    public void testCompleteOnTimeout_timesOut(Integer v) {
3558 >        long timeoutMillis = timeoutMillis();
3559          CompletableFuture<Integer> f = new CompletableFuture<>();
3560 <        f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3561 <        f.join();
3562 <        checkCompletedNormally(f, -1);
3560 >        long startTime = System.nanoTime();
3561 >        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3562 >        assertSame(v, f.join());
3563 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3564 >        f.complete(99);         // should have no effect
3565 >        checkCompletedNormally(f, v);
3566      }
3567  
3568      /**
3569       * completeOnTimeout has no effect if completed within timeout
3570       */
3571 <    public void testCompleteOnTimeout2() {
3571 >    public void testCompleteOnTimeout_completed() {
3572 >        for (Integer v1 : new Integer[] { 1, null })
3573 >    {
3574          CompletableFuture<Integer> f = new CompletableFuture<>();
3575 <        f.complete(1);
3576 <        f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3577 <        checkCompletedNormally(f, 1);
3578 <    }
3575 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3576 >        long startTime = System.nanoTime();
3577 >        f.complete(v1);
3578 >        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3579 >        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3580 >        g.complete(v1);
3581 >        checkCompletedNormally(f, v1);
3582 >        checkCompletedNormally(g, v1);
3583 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3584 >    }}
3585  
3586      /**
3587       * delayedExecutor returns an executor that delays submission
3588       */
3589      public void testDelayedExecutor() {
3590 <        long timeoutMillis = SMALL_DELAY_MS;
3591 <        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3592 <                                                       MILLISECONDS);
3590 >        testInParallel(() -> testDelayedExecutor(null, null),
3591 >                       () -> testDelayedExecutor(null, 1),
3592 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3593 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3594 >    }
3595 >
3596 >    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3597 >        long timeoutMillis = timeoutMillis();
3598 >        // Use an "unreasonably long" long timeout to catch lingering threads
3599 >        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3600 >        final Executor delayer, longDelayer;
3601 >        if (executor == null) {
3602 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3603 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3604 >        } else {
3605 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3606 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3607 >        }
3608          long startTime = System.nanoTime();
3609 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3610 <        assertNull(f.getNow(null));
3611 <        try {
3612 <            f.get(LONG_DELAY_MS, MILLISECONDS);
3613 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
3614 <        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3615 <        checkCompletedNormally(f, 1);
3616 <    }
3609 >        CompletableFuture<Integer> f =
3610 >            CompletableFuture.supplyAsync(() -> v, delayer);
3611 >        CompletableFuture<Integer> g =
3612 >            CompletableFuture.supplyAsync(() -> v, longDelayer);
3613 >
3614 >        assertNull(g.getNow(null));
3615 >
3616 >        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3617 >        long millisElapsed = millisElapsedSince(startTime);
3618 >        assertTrue(millisElapsed >= timeoutMillis);
3619 >        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3620  
3621 <    /**
3622 <     * delayedExecutor for a given executor returns an executor that
3623 <     * delays submission
3624 <     */
3605 <    public void testDelayedExecutor2() {
3606 <        long timeoutMillis = SMALL_DELAY_MS;
3607 <        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3608 <                                                       MILLISECONDS,
3609 <                                                       new ThreadExecutor());
3610 <        long startTime = System.nanoTime();
3611 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3612 <        assertNull(f.getNow(null));
3613 <        try {
3614 <            f.get(LONG_DELAY_MS, MILLISECONDS);
3615 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
3616 <        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3617 <        checkCompletedNormally(f, 1);
3621 >        checkCompletedNormally(f, v);
3622 >
3623 >        checkIncomplete(g);
3624 >        assertTrue(g.cancel(true));
3625      }
3626  
3627      //--- tests of implementation details; not part of official tck ---
# Line 3651 | 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 3707 | Line 3714 | public class CompletableFutureTest exten
3714          }
3715      }}
3716  
3717 +    /**
3718 +     * Minimal completion stages throw UOE for all non-CompletionStage methods
3719 +     */
3720 +    public void testMinimalCompletionStage_minimality() {
3721 +        if (!testImplementationDetails) return;
3722 +        Function<Method, String> toSignature =
3723 +            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3724 +        Predicate<Method> isNotStatic =
3725 +            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3726 +        List<Method> minimalMethods =
3727 +            Stream.of(Object.class, CompletionStage.class)
3728 +            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3729 +            .filter(isNotStatic)
3730 +            .collect(Collectors.toList());
3731 +        // Methods from CompletableFuture permitted NOT to throw UOE
3732 +        String[] signatureWhitelist = {
3733 +            "newIncompleteFuture[]",
3734 +            "defaultExecutor[]",
3735 +            "minimalCompletionStage[]",
3736 +            "copy[]",
3737 +        };
3738 +        Set<String> permittedMethodSignatures =
3739 +            Stream.concat(minimalMethods.stream().map(toSignature),
3740 +                          Stream.of(signatureWhitelist))
3741 +            .collect(Collectors.toSet());
3742 +        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3743 +            .filter(isNotStatic)
3744 +            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3745 +            .collect(Collectors.toList());
3746 +
3747 +        CompletionStage<Integer> minimalStage =
3748 +            new CompletableFuture<Integer>().minimalCompletionStage();
3749 +
3750 +        List<Method> bugs = new ArrayList<>();
3751 +        for (Method method : allMethods) {
3752 +            Class<?>[] parameterTypes = method.getParameterTypes();
3753 +            Object[] args = new Object[parameterTypes.length];
3754 +            // Manufacture boxed primitives for primitive params
3755 +            for (int i = 0; i < args.length; i++) {
3756 +                Class<?> type = parameterTypes[i];
3757 +                if (parameterTypes[i] == boolean.class)
3758 +                    args[i] = false;
3759 +                else if (parameterTypes[i] == int.class)
3760 +                    args[i] = 0;
3761 +                else if (parameterTypes[i] == long.class)
3762 +                    args[i] = 0L;
3763 +            }
3764 +            try {
3765 +                method.invoke(minimalStage, args);
3766 +                bugs.add(method);
3767 +            }
3768 +            catch (java.lang.reflect.InvocationTargetException expected) {
3769 +                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3770 +                    bugs.add(method);
3771 +                    // expected.getCause().printStackTrace();
3772 +                }
3773 +            }
3774 +            catch (ReflectiveOperationException bad) { throw new Error(bad); }
3775 +        }
3776 +        if (!bugs.isEmpty())
3777 +            throw new Error("Methods did not throw UOE: " + bugs.toString());
3778 +    }
3779 +
3780 +    static class Monad {
3781 +        static class ZeroException extends RuntimeException {
3782 +            public ZeroException() { super("monadic zero"); }
3783 +        }
3784 +        // "return", "unit"
3785 +        static <T> CompletableFuture<T> unit(T value) {
3786 +            return completedFuture(value);
3787 +        }
3788 +        // monadic zero ?
3789 +        static <T> CompletableFuture<T> zero() {
3790 +            return failedFuture(new ZeroException());
3791 +        }
3792 +        // >=>
3793 +        static <T,U,V> Function<T, CompletableFuture<V>> compose
3794 +            (Function<T, CompletableFuture<U>> f,
3795 +             Function<U, CompletableFuture<V>> g) {
3796 +            return (x) -> f.apply(x).thenCompose(g);
3797 +        }
3798 +
3799 +        static void assertZero(CompletableFuture<?> f) {
3800 +            try {
3801 +                f.getNow(null);
3802 +                throw new AssertionFailedError("should throw");
3803 +            } catch (CompletionException success) {
3804 +                assertTrue(success.getCause() instanceof ZeroException);
3805 +            }
3806 +        }
3807 +
3808 +        static <T> void assertFutureEquals(CompletableFuture<T> f,
3809 +                                           CompletableFuture<T> g) {
3810 +            T fval = null, gval = null;
3811 +            Throwable fex = null, gex = null;
3812 +
3813 +            try { fval = f.get(); }
3814 +            catch (ExecutionException ex) { fex = ex.getCause(); }
3815 +            catch (Throwable ex) { fex = ex; }
3816 +
3817 +            try { gval = g.get(); }
3818 +            catch (ExecutionException ex) { gex = ex.getCause(); }
3819 +            catch (Throwable ex) { gex = ex; }
3820 +
3821 +            if (fex != null || gex != null)
3822 +                assertSame(fex.getClass(), gex.getClass());
3823 +            else
3824 +                assertEquals(fval, gval);
3825 +        }
3826 +
3827 +        static class PlusFuture<T> extends CompletableFuture<T> {
3828 +            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3829 +        }
3830 +
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 +                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 {
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);
3861 +            g.whenComplete(action);
3862 +            return plus;
3863 +        }
3864 +    }
3865 +
3866 +    /**
3867 +     * CompletableFuture is an additive monad - sort of.
3868 +     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3869 +     */
3870 +    public void testAdditiveMonad() throws Throwable {
3871 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3872 +        CompletableFuture<Long> zero = Monad.zero();
3873 +
3874 +        // Some mutually non-commutative functions
3875 +        Function<Long, CompletableFuture<Long>> triple
3876 +            = (x) -> Monad.unit(3 * x);
3877 +        Function<Long, CompletableFuture<Long>> inc
3878 +            = (x) -> Monad.unit(x + 1);
3879 +
3880 +        // unit is a right identity: m >>= unit === m
3881 +        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3882 +                                 inc.apply(5L));
3883 +        // unit is a left identity: (unit x) >>= f === f x
3884 +        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3885 +                                 inc.apply(5L));
3886 +
3887 +        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3888 +        Monad.assertFutureEquals(
3889 +            unit.apply(5L).thenCompose(inc).thenCompose(triple),
3890 +            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3891 +
3892 +        // The case for CompletableFuture as an additive monad is weaker...
3893 +
3894 +        // zero is a monadic zero
3895 +        Monad.assertZero(zero);
3896 +
3897 +        // left zero: zero >>= f === zero
3898 +        Monad.assertZero(zero.thenCompose(inc));
3899 +        // right zero: f >>= (\x -> zero) === zero
3900 +        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3901 +
3902 +        // f plus zero === f
3903 +        Monad.assertFutureEquals(Monad.unit(5L),
3904 +                                 Monad.plus(Monad.unit(5L), zero));
3905 +        // zero plus f === f
3906 +        Monad.assertFutureEquals(Monad.unit(5L),
3907 +                                 Monad.plus(zero, Monad.unit(5L)));
3908 +        // zero plus zero === zero
3909 +        Monad.assertZero(Monad.plus(zero, zero));
3910 +        {
3911 +            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3912 +                                                   Monad.unit(8L));
3913 +            // non-determinism
3914 +            assertTrue(f.get() == 5L || f.get() == 8L);
3915 +        }
3916 +
3917 +        CompletableFuture<Long> godot = new CompletableFuture<>();
3918 +        // f plus godot === f (doesn't wait for godot)
3919 +        Monad.assertFutureEquals(Monad.unit(5L),
3920 +                                 Monad.plus(Monad.unit(5L), godot));
3921 +        // godot plus f === f (doesn't wait for godot)
3922 +        Monad.assertFutureEquals(Monad.unit(5L),
3923 +                                 Monad.plus(godot, Monad.unit(5L)));
3924 +    }
3925 +
3926 + //     static <U> U join(CompletionStage<U> stage) {
3927 + //         CompletableFuture<U> f = new CompletableFuture<>();
3928 + //         stage.whenComplete((v, ex) -> {
3929 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3930 + //         });
3931 + //         return f.join();
3932 + //     }
3933 +
3934 + //     static <U> boolean isDone(CompletionStage<U> stage) {
3935 + //         CompletableFuture<U> f = new CompletableFuture<>();
3936 + //         stage.whenComplete((v, ex) -> {
3937 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3938 + //         });
3939 + //         return f.isDone();
3940 + //     }
3941 +
3942 + //     static <U> U join2(CompletionStage<U> stage) {
3943 + //         return stage.toCompletableFuture().copy().join();
3944 + //     }
3945 +
3946 + //     static <U> boolean isDone2(CompletionStage<U> stage) {
3947 + //         return stage.toCompletableFuture().copy().isDone();
3948 + //     }
3949 +
3950   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines