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.107 by jsr166, Thu Sep 3 17:06:18 2015 UTC vs.
Revision 1.136 by jsr166, Sun Nov 15 20:17:11 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 1120 | Line 1057 | public class CompletableFutureTest exten
1057          if (!createIncomplete) assertTrue(f.complete(v1));
1058          final CompletableFuture<Integer> g = m.handle
1059              (f,
1060 <             (Integer x, Throwable t) -> {
1060 >             (Integer result, Throwable t) -> {
1061                  m.checkExecutionMode();
1062 <                threadAssertSame(x, v1);
1062 >                threadAssertSame(result, v1);
1063                  threadAssertNull(t);
1064                  a.getAndIncrement();
1065                  return inc(v1);
# Line 1149 | Line 1086 | public class CompletableFutureTest exten
1086          if (!createIncomplete) f.completeExceptionally(ex);
1087          final CompletableFuture<Integer> g = m.handle
1088              (f,
1089 <             (Integer x, Throwable t) -> {
1089 >             (Integer result, Throwable t) -> {
1090                  m.checkExecutionMode();
1091 <                threadAssertNull(x);
1091 >                threadAssertNull(result);
1092                  threadAssertSame(t, ex);
1093                  a.getAndIncrement();
1094                  return v1;
# Line 1178 | Line 1115 | public class CompletableFutureTest exten
1115          if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning));
1116          final CompletableFuture<Integer> g = m.handle
1117              (f,
1118 <             (Integer x, Throwable t) -> {
1118 >             (Integer result, Throwable t) -> {
1119                  m.checkExecutionMode();
1120 <                threadAssertNull(x);
1120 >                threadAssertNull(result);
1121                  threadAssertTrue(t instanceof CancellationException);
1122                  a.getAndIncrement();
1123                  return v1;
# Line 1193 | Line 1130 | public class CompletableFutureTest exten
1130      }}
1131  
1132      /**
1133 <     * handle result completes exceptionally if action does
1133 >     * If a "handle action" throws an exception when triggered by
1134 >     * a normal completion, it completes exceptionally
1135       */
1136 <    public void testHandle_sourceFailedActionFailed() {
1136 >    public void testHandle_sourceCompletedNormallyActionFailed() {
1137          for (ExecutionMode m : ExecutionMode.values())
1138          for (boolean createIncomplete : new boolean[] { true, false })
1139 +        for (Integer v1 : new Integer[] { 1, null })
1140      {
1141          final CompletableFuture<Integer> f = new CompletableFuture<>();
1142          final AtomicInteger a = new AtomicInteger(0);
1143 <        final CFException ex1 = new CFException();
1144 <        final CFException ex2 = new CFException();
1206 <        if (!createIncomplete) f.completeExceptionally(ex1);
1143 >        final CFException ex = new CFException();
1144 >        if (!createIncomplete) assertTrue(f.complete(v1));
1145          final CompletableFuture<Integer> g = m.handle
1146              (f,
1147 <             (Integer x, Throwable t) -> {
1147 >             (Integer result, Throwable t) -> {
1148                  m.checkExecutionMode();
1149 <                threadAssertNull(x);
1150 <                threadAssertSame(ex1, t);
1149 >                threadAssertSame(result, v1);
1150 >                threadAssertNull(t);
1151                  a.getAndIncrement();
1152 <                throw ex2;
1152 >                throw ex;
1153              });
1154 <        if (createIncomplete) f.completeExceptionally(ex1);
1154 >        if (createIncomplete) assertTrue(f.complete(v1));
1155  
1156 <        checkCompletedWithWrappedException(g, ex2);
1157 <        checkCompletedExceptionally(f, ex1);
1156 >        checkCompletedWithWrappedException(g, ex);
1157 >        checkCompletedNormally(f, v1);
1158          assertEquals(1, a.get());
1159      }}
1160  
1161 <    public void testHandle_sourceCompletedNormallyActionFailed() {
1162 <        for (ExecutionMode m : ExecutionMode.values())
1161 >    /**
1162 >     * If a "handle action" throws an exception when triggered by
1163 >     * a source completion that also throws an exception, the action
1164 >     * exception takes precedence (unlike whenComplete)
1165 >     */
1166 >    public void testHandle_sourceFailedActionFailed() {
1167          for (boolean createIncomplete : new boolean[] { true, false })
1168 <        for (Integer v1 : new Integer[] { 1, null })
1168 >        for (ExecutionMode m : ExecutionMode.values())
1169      {
1228        final CompletableFuture<Integer> f = new CompletableFuture<>();
1170          final AtomicInteger a = new AtomicInteger(0);
1171 <        final CFException ex = new CFException();
1172 <        if (!createIncomplete) assertTrue(f.complete(v1));
1171 >        final CFException ex1 = new CFException();
1172 >        final CFException ex2 = new CFException();
1173 >        final CompletableFuture<Integer> f = new CompletableFuture<>();
1174 >
1175 >        if (!createIncomplete) f.completeExceptionally(ex1);
1176          final CompletableFuture<Integer> g = m.handle
1177              (f,
1178 <             (Integer x, Throwable t) -> {
1178 >             (Integer result, Throwable t) -> {
1179                  m.checkExecutionMode();
1180 <                threadAssertSame(x, v1);
1181 <                threadAssertNull(t);
1180 >                threadAssertNull(result);
1181 >                threadAssertSame(ex1, t);
1182                  a.getAndIncrement();
1183 <                throw ex;
1183 >                throw ex2;
1184              });
1185 <        if (createIncomplete) assertTrue(f.complete(v1));
1185 >        if (createIncomplete) f.completeExceptionally(ex1);
1186  
1187 <        checkCompletedWithWrappedException(g, ex);
1188 <        checkCompletedNormally(f, v1);
1187 >        checkCompletedWithWrappedException(g, ex2);
1188 >        checkCompletedExceptionally(f, ex1);
1189          assertEquals(1, a.get());
1190      }}
1191  
# Line 3142 | Line 3086 | public class CompletableFutureTest exten
3086              for (int i = 0; i < k; i++) {
3087                  checkIncomplete(f);
3088                  checkIncomplete(CompletableFuture.allOf(fs));
3089 <                if (i != k/2) {
3089 >                if (i != k / 2) {
3090                      fs[i].complete(i);
3091                      checkCompletedNormally(fs[i], i);
3092                  } else {
# Line 3345 | Line 3289 | public class CompletableFutureTest exten
3289              () -> CompletableFuture.anyOf(null, f),
3290  
3291              () -> f.obtrudeException(null),
3292 +
3293 +            () -> CompletableFuture.delayedExecutor(1L, SECONDS, null),
3294 +            () -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()),
3295 +            () -> CompletableFuture.delayedExecutor(1L, null),
3296 +
3297 +            () -> f.orTimeout(1L, null),
3298 +            () -> f.completeOnTimeout(42, 1L, null),
3299 +
3300 +            () -> CompletableFuture.failedFuture(null),
3301 +            () -> CompletableFuture.failedStage(null),
3302          };
3303  
3304          assertThrows(NullPointerException.class, throwingActions);
# Line 3365 | Line 3319 | public class CompletableFutureTest exten
3319       * newIncompleteFuture returns an incomplete CompletableFuture
3320       */
3321      public void testNewIncompleteFuture() {
3322 +        for (Integer v1 : new Integer[] { 1, null })
3323 +    {
3324          CompletableFuture<Integer> f = new CompletableFuture<>();
3325          CompletableFuture<Integer> g = f.newIncompleteFuture();
3326          checkIncomplete(f);
3327          checkIncomplete(g);
3328 <    }
3328 >        f.complete(v1);
3329 >        checkCompletedNormally(f, v1);
3330 >        checkIncomplete(g);
3331 >        g.complete(v1);
3332 >        checkCompletedNormally(g, v1);
3333 >        assertSame(g.getClass(), CompletableFuture.class);
3334 >    }}
3335  
3336      /**
3337       * completedStage returns a completed CompletionStage
3338       */
3339      public void testCompletedStage() {
3340 <        AtomicInteger x = new AtomicInteger();
3340 >        AtomicInteger x = new AtomicInteger(0);
3341          AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3342          CompletionStage<Integer> f = CompletableFuture.completedStage(1);
3343          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
# Line 3385 | Line 3347 | public class CompletableFutureTest exten
3347  
3348      /**
3349       * defaultExecutor by default returns the commonPool if
3350 <     * it supports at least one thread.
3350 >     * it supports more than one thread.
3351       */
3352      public void testDefaultExecutor() {
3353          CompletableFuture<Integer> f = new CompletableFuture<>();
3354          Executor e = f.defaultExecutor();
3355 <        Executor c =  ForkJoinPool.commonPool();
3355 >        Executor c = ForkJoinPool.commonPool();
3356          if (ForkJoinPool.getCommonPoolParallelism() > 1)
3357              assertSame(e, c);
3358 +        else
3359 +            assertNotSame(e, c);
3360      }
3361  
3362      /**
# Line 3402 | Line 3366 | public class CompletableFutureTest exten
3366      public void testFailedFuture() {
3367          CFException ex = new CFException();
3368          CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex);
3369 <        checkCompletedExceptionallyWithRootCause(f, ex);
3369 >        checkCompletedExceptionally(f, ex);
3370      }
3371  
3372      /**
3373       * failedFuture(null) throws NPE
3374       */
3375 <    public void testFailedFuture2() {
3375 >    public void testFailedFuture_null() {
3376          try {
3377              CompletableFuture<Integer> f = CompletableFuture.failedFuture(null);
3378              shouldThrow();
# Line 3441 | Line 3405 | public class CompletableFutureTest exten
3405          CFException ex = new CFException();
3406          f.completeExceptionally(ex);
3407          checkCompletedExceptionally(f, ex);
3408 <        checkCompletedWithWrappedCFException(g);
3408 >        checkCompletedWithWrappedException(g, ex);
3409      }
3410  
3411      /**
# Line 3451 | Line 3415 | public class CompletableFutureTest exten
3415      public void testMinimalCompletionStage() {
3416          CompletableFuture<Integer> f = new CompletableFuture<>();
3417          CompletionStage<Integer> g = f.minimalCompletionStage();
3418 <        AtomicInteger x = new AtomicInteger();
3418 >        AtomicInteger x = new AtomicInteger(0);
3419          AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3420          checkIncomplete(f);
3421          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
# Line 3468 | Line 3432 | public class CompletableFutureTest exten
3432      public void testMinimalCompletionStage2() {
3433          CompletableFuture<Integer> f = new CompletableFuture<>();
3434          CompletionStage<Integer> g = f.minimalCompletionStage();
3435 <        AtomicInteger x = new AtomicInteger();
3435 >        AtomicInteger x = new AtomicInteger(0);
3436          AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3437          g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3438          checkIncomplete(f);
# Line 3480 | Line 3444 | public class CompletableFutureTest exten
3444      }
3445  
3446      /**
3447 <     * failedStage returns a Completionstage completed
3447 >     * failedStage returns a CompletionStage completed
3448       * exceptionally with the given Exception
3449       */
3450      public void testFailedStage() {
3451          CFException ex = new CFException();
3452          CompletionStage<Integer> f = CompletableFuture.failedStage(ex);
3453 <        AtomicInteger x = new AtomicInteger();
3453 >        AtomicInteger x = new AtomicInteger(0);
3454          AtomicReference<Throwable> r = new AtomicReference<Throwable>();
3455          f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);});
3456          assertEquals(x.get(), 0);
3457 <        assertEquals(r.get().getCause(), ex);
3457 >        assertEquals(r.get(), ex);
3458      }
3459  
3460      /**
3461       * completeAsync completes with value of given supplier
3462       */
3463      public void testCompleteAsync() {
3464 +        for (Integer v1 : new Integer[] { 1, null })
3465 +    {
3466          CompletableFuture<Integer> f = new CompletableFuture<>();
3467 <        f.completeAsync(() -> 1);
3467 >        f.completeAsync(() -> v1);
3468          f.join();
3469 <        checkCompletedNormally(f, 1);
3470 <    }
3469 >        checkCompletedNormally(f, v1);
3470 >    }}
3471  
3472      /**
3473       * completeAsync completes exceptionally if given supplier throws
# Line 3513 | Line 3479 | public class CompletableFutureTest exten
3479          try {
3480              f.join();
3481              shouldThrow();
3482 <        } catch (Exception success) {}
3483 <        checkCompletedWithWrappedCFException(f);
3482 >        } catch (CompletionException success) {}
3483 >        checkCompletedWithWrappedException(f, ex);
3484      }
3485  
3486      /**
3487       * completeAsync with given executor completes with value of given supplier
3488       */
3489      public void testCompleteAsync3() {
3490 +        for (Integer v1 : new Integer[] { 1, null })
3491 +    {
3492          CompletableFuture<Integer> f = new CompletableFuture<>();
3493 <        f.completeAsync(() -> 1, new ThreadExecutor());
3494 <        f.join();
3495 <        checkCompletedNormally(f, 1);
3496 <    }
3493 >        ThreadExecutor executor = new ThreadExecutor();
3494 >        f.completeAsync(() -> v1, executor);
3495 >        assertSame(v1, f.join());
3496 >        checkCompletedNormally(f, v1);
3497 >        assertEquals(1, executor.count.get());
3498 >    }}
3499  
3500      /**
3501       * completeAsync with given executor completes exceptionally if
# Line 3534 | Line 3504 | public class CompletableFutureTest exten
3504      public void testCompleteAsync4() {
3505          CompletableFuture<Integer> f = new CompletableFuture<>();
3506          CFException ex = new CFException();
3507 <        f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor());
3507 >        ThreadExecutor executor = new ThreadExecutor();
3508 >        f.completeAsync(() -> {if (true) throw ex; return 1;}, executor);
3509          try {
3510              f.join();
3511              shouldThrow();
3512 <        } catch (Exception success) {}
3513 <        checkCompletedWithWrappedCFException(f);
3512 >        } catch (CompletionException success) {}
3513 >        checkCompletedWithWrappedException(f, ex);
3514 >        assertEquals(1, executor.count.get());
3515      }
3516  
3517      /**
3518       * orTimeout completes with TimeoutException if not complete
3519       */
3520 <    public void testOrTimeout() {
3520 >    public void testOrTimeout_timesOut() {
3521 >        long timeoutMillis = timeoutMillis();
3522          CompletableFuture<Integer> f = new CompletableFuture<>();
3523 <        f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3524 <        checkCompletedExceptionallyWithTimeout(f);
3523 >        long startTime = System.nanoTime();
3524 >        f.orTimeout(timeoutMillis, MILLISECONDS);
3525 >        checkCompletedWithTimeoutException(f);
3526 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3527      }
3528  
3529      /**
3530       * orTimeout completes normally if completed before timeout
3531       */
3532 <    public void testOrTimeout2() {
3532 >    public void testOrTimeout_completed() {
3533 >        for (Integer v1 : new Integer[] { 1, null })
3534 >    {
3535          CompletableFuture<Integer> f = new CompletableFuture<>();
3536 <        f.complete(1);
3537 <        f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3538 <        checkCompletedNormally(f, 1);
3539 <    }
3536 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3537 >        long startTime = System.nanoTime();
3538 >        f.complete(v1);
3539 >        f.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3540 >        g.orTimeout(LONG_DELAY_MS, MILLISECONDS);
3541 >        g.complete(v1);
3542 >        checkCompletedNormally(f, v1);
3543 >        checkCompletedNormally(g, v1);
3544 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3545 >    }}
3546  
3547      /**
3548       * completeOnTimeout completes with given value if not complete
3549       */
3550 <    public void testCompleteOnTimeout() {
3550 >    public void testCompleteOnTimeout_timesOut() {
3551 >        testInParallel(() -> testCompleteOnTimeout_timesOut(42),
3552 >                       () -> testCompleteOnTimeout_timesOut(null));
3553 >    }
3554 >
3555 >    public void testCompleteOnTimeout_timesOut(Integer v) {
3556 >        long timeoutMillis = timeoutMillis();
3557          CompletableFuture<Integer> f = new CompletableFuture<>();
3558 <        f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3559 <        f.join();
3560 <        checkCompletedNormally(f, -1);
3558 >        long startTime = System.nanoTime();
3559 >        f.completeOnTimeout(v, timeoutMillis, MILLISECONDS);
3560 >        assertSame(v, f.join());
3561 >        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
3562 >        f.complete(99);         // should have no effect
3563 >        checkCompletedNormally(f, v);
3564      }
3565  
3566      /**
3567       * completeOnTimeout has no effect if completed within timeout
3568       */
3569 <    public void testCompleteOnTimeout2() {
3569 >    public void testCompleteOnTimeout_completed() {
3570 >        for (Integer v1 : new Integer[] { 1, null })
3571 >    {
3572          CompletableFuture<Integer> f = new CompletableFuture<>();
3573 <        f.complete(1);
3574 <        f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
3575 <        checkCompletedNormally(f, 1);
3576 <    }
3573 >        CompletableFuture<Integer> g = new CompletableFuture<>();
3574 >        long startTime = System.nanoTime();
3575 >        f.complete(v1);
3576 >        f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3577 >        g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS);
3578 >        g.complete(v1);
3579 >        checkCompletedNormally(f, v1);
3580 >        checkCompletedNormally(g, v1);
3581 >        assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2);
3582 >    }}
3583  
3584      /**
3585       * delayedExecutor returns an executor that delays submission
3586       */
3587      public void testDelayedExecutor() {
3588 <        long timeoutMillis = SMALL_DELAY_MS;
3589 <        Executor d = CompletableFuture.delayedExecutor(timeoutMillis,
3590 <                                                       MILLISECONDS);
3588 >        testInParallel(() -> testDelayedExecutor(null, null),
3589 >                       () -> testDelayedExecutor(null, 1),
3590 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1),
3591 >                       () -> testDelayedExecutor(new ThreadExecutor(), 1));
3592 >    }
3593 >
3594 >    public void testDelayedExecutor(Executor executor, Integer v) throws Exception {
3595 >        long timeoutMillis = timeoutMillis();
3596 >        // Use an "unreasonably long" long timeout to catch lingering threads
3597 >        long longTimeoutMillis = 1000 * 60 * 60 * 24;
3598 >        final Executor delayer, longDelayer;
3599 >        if (executor == null) {
3600 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS);
3601 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS);
3602 >        } else {
3603 >            delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor);
3604 >            longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor);
3605 >        }
3606          long startTime = System.nanoTime();
3607 <        CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d);
3608 <        assertNull(f.getNow(null));
3609 <        try {
3610 <            f.get(LONG_DELAY_MS, MILLISECONDS);
3611 <        } catch (Throwable fail) { threadUnexpectedException(fail); }
3612 <        assertTrue(millisElapsedSince(startTime) > timeoutMillis/2);
3613 <        checkCompletedNormally(f, 1);
3614 <    }
3607 >        CompletableFuture<Integer> f =
3608 >            CompletableFuture.supplyAsync(() -> v, delayer);
3609 >        CompletableFuture<Integer> g =
3610 >            CompletableFuture.supplyAsync(() -> v, longDelayer);
3611 >
3612 >        assertNull(g.getNow(null));
3613 >
3614 >        assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS));
3615 >        long millisElapsed = millisElapsedSince(startTime);
3616 >        assertTrue(millisElapsed >= timeoutMillis);
3617 >        assertTrue(millisElapsed < LONG_DELAY_MS / 2);
3618  
3619 <    /**
3620 <     * delayedExecutor for a given executor returns an executor that
3621 <     * delays submission
3622 <     */
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);
3619 >        checkCompletedNormally(f, v);
3620 >
3621 >        checkIncomplete(g);
3622 >        assertTrue(g.cancel(true));
3623      }
3624  
3625      //--- tests of implementation details; not part of official tck ---
# Line 3651 | Line 3656 | public class CompletableFutureTest exten
3656          funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m)));
3657          funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m)));
3658  
3659 <        funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {}));
3659 >        funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {}));
3660  
3661          funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m)));
3662  
# Line 3707 | Line 3712 | public class CompletableFutureTest exten
3712          }
3713      }}
3714  
3715 +    /**
3716 +     * Minimal completion stages throw UOE for all non-CompletionStage methods
3717 +     */
3718 +    public void testMinimalCompletionStage_minimality() {
3719 +        if (!testImplementationDetails) return;
3720 +        Function<Method, String> toSignature =
3721 +            (method) -> method.getName() + Arrays.toString(method.getParameterTypes());
3722 +        Predicate<Method> isNotStatic =
3723 +            (method) -> (method.getModifiers() & Modifier.STATIC) == 0;
3724 +        List<Method> minimalMethods =
3725 +            Stream.of(Object.class, CompletionStage.class)
3726 +            .flatMap((klazz) -> Stream.of(klazz.getMethods()))
3727 +            .filter(isNotStatic)
3728 +            .collect(Collectors.toList());
3729 +        // Methods from CompletableFuture permitted NOT to throw UOE
3730 +        String[] signatureWhitelist = {
3731 +            "newIncompleteFuture[]",
3732 +            "defaultExecutor[]",
3733 +            "minimalCompletionStage[]",
3734 +            "copy[]",
3735 +        };
3736 +        Set<String> permittedMethodSignatures =
3737 +            Stream.concat(minimalMethods.stream().map(toSignature),
3738 +                          Stream.of(signatureWhitelist))
3739 +            .collect(Collectors.toSet());
3740 +        List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods())
3741 +            .filter(isNotStatic)
3742 +            .filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method)))
3743 +            .collect(Collectors.toList());
3744 +
3745 +        CompletionStage<Integer> minimalStage =
3746 +            new CompletableFuture<Integer>().minimalCompletionStage();
3747 +
3748 +        List<Method> bugs = new ArrayList<>();
3749 +        for (Method method : allMethods) {
3750 +            Class<?>[] parameterTypes = method.getParameterTypes();
3751 +            Object[] args = new Object[parameterTypes.length];
3752 +            // Manufacture boxed primitives for primitive params
3753 +            for (int i = 0; i < args.length; i++) {
3754 +                Class<?> type = parameterTypes[i];
3755 +                if (parameterTypes[i] == boolean.class)
3756 +                    args[i] = false;
3757 +                else if (parameterTypes[i] == int.class)
3758 +                    args[i] = 0;
3759 +                else if (parameterTypes[i] == long.class)
3760 +                    args[i] = 0L;
3761 +            }
3762 +            try {
3763 +                method.invoke(minimalStage, args);
3764 +                bugs.add(method);
3765 +            }
3766 +            catch (java.lang.reflect.InvocationTargetException expected) {
3767 +                if (! (expected.getCause() instanceof UnsupportedOperationException)) {
3768 +                    bugs.add(method);
3769 +                    // expected.getCause().printStackTrace();
3770 +                }
3771 +            }
3772 +            catch (ReflectiveOperationException bad) { throw new Error(bad); }
3773 +        }
3774 +        if (!bugs.isEmpty())
3775 +            throw new Error("Methods did not throw UOE: " + bugs.toString());
3776 +    }
3777 +
3778 +    static class Monad {
3779 +        static class ZeroException extends RuntimeException {
3780 +            public ZeroException() { super("monadic zero"); }
3781 +        }
3782 +        // "return", "unit"
3783 +        static <T> CompletableFuture<T> unit(T value) {
3784 +            return completedFuture(value);
3785 +        }
3786 +        // monadic zero ?
3787 +        static <T> CompletableFuture<T> zero() {
3788 +            return failedFuture(new ZeroException());
3789 +        }
3790 +        // >=>
3791 +        static <T,U,V> Function<T, CompletableFuture<V>> compose
3792 +            (Function<T, CompletableFuture<U>> f,
3793 +             Function<U, CompletableFuture<V>> g) {
3794 +            return (x) -> f.apply(x).thenCompose(g);
3795 +        }
3796 +
3797 +        static void assertZero(CompletableFuture<?> f) {
3798 +            try {
3799 +                f.getNow(null);
3800 +                throw new AssertionFailedError("should throw");
3801 +            } catch (CompletionException success) {
3802 +                assertTrue(success.getCause() instanceof ZeroException);
3803 +            }
3804 +        }
3805 +
3806 +        static <T> void assertFutureEquals(CompletableFuture<T> f,
3807 +                                           CompletableFuture<T> g) {
3808 +            T fval = null, gval = null;
3809 +            Throwable fex = null, gex = null;
3810 +
3811 +            try { fval = f.get(); }
3812 +            catch (ExecutionException ex) { fex = ex.getCause(); }
3813 +            catch (Throwable ex) { fex = ex; }
3814 +
3815 +            try { gval = g.get(); }
3816 +            catch (ExecutionException ex) { gex = ex.getCause(); }
3817 +            catch (Throwable ex) { gex = ex; }
3818 +
3819 +            if (fex != null || gex != null)
3820 +                assertSame(fex.getClass(), gex.getClass());
3821 +            else
3822 +                assertEquals(fval, gval);
3823 +        }
3824 +
3825 +        static class PlusFuture<T> extends CompletableFuture<T> {
3826 +            AtomicReference<Throwable> firstFailure = new AtomicReference<>(null);
3827 +        }
3828 +
3829 +        // Monadic "plus"
3830 +        static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f,
3831 +                                             CompletableFuture<? extends T> g) {
3832 +            PlusFuture<T> plus = new PlusFuture<T>();
3833 +            BiConsumer<T, Throwable> action = (T result, Throwable ex) -> {
3834 +                if (ex == null) {
3835 +                    if (plus.complete(result))
3836 +                        if (plus.firstFailure.get() != null)
3837 +                            plus.firstFailure.set(null);
3838 +                }
3839 +                else if (plus.firstFailure.compareAndSet(null, ex)) {
3840 +                    if (plus.isDone())
3841 +                        plus.firstFailure.set(null);
3842 +                }
3843 +                else {
3844 +                    // first failure has precedence
3845 +                    Throwable first = plus.firstFailure.getAndSet(null);
3846 +
3847 +                    // may fail with "Self-suppression not permitted"
3848 +                    try { first.addSuppressed(ex); }
3849 +                    catch (Exception ignored) {}
3850 +
3851 +                    plus.completeExceptionally(first);
3852 +                }
3853 +            };
3854 +            f.whenComplete(action);
3855 +            g.whenComplete(action);
3856 +            return plus;
3857 +        }
3858 +    }
3859 +
3860 +    /**
3861 +     * CompletableFuture is an additive monad - sort of.
3862 +     * https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads
3863 +     */
3864 +    public void testAdditiveMonad() throws Throwable {
3865 +        Function<Long, CompletableFuture<Long>> unit = Monad::unit;
3866 +        CompletableFuture<Long> zero = Monad.zero();
3867 +
3868 +        // Some mutually non-commutative functions
3869 +        Function<Long, CompletableFuture<Long>> triple
3870 +            = (x) -> Monad.unit(3 * x);
3871 +        Function<Long, CompletableFuture<Long>> inc
3872 +            = (x) -> Monad.unit(x + 1);
3873 +
3874 +        // unit is a right identity: m >>= unit === m
3875 +        Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit),
3876 +                                 inc.apply(5L));
3877 +        // unit is a left identity: (unit x) >>= f === f x
3878 +        Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc),
3879 +                                 inc.apply(5L));
3880 +
3881 +        // associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) )
3882 +        Monad.assertFutureEquals(
3883 +            unit.apply(5L).thenCompose(inc).thenCompose(triple),
3884 +            unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple)));
3885 +
3886 +        // The case for CompletableFuture as an additive monad is weaker...
3887 +
3888 +        // zero is a monadic zero
3889 +        Monad.assertZero(zero);
3890 +
3891 +        // left zero: zero >>= f === zero
3892 +        Monad.assertZero(zero.thenCompose(inc));
3893 +        // right zero: f >>= (\x -> zero) === zero
3894 +        Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero));
3895 +
3896 +        // f plus zero === f
3897 +        Monad.assertFutureEquals(Monad.unit(5L),
3898 +                                 Monad.plus(Monad.unit(5L), zero));
3899 +        // zero plus f === f
3900 +        Monad.assertFutureEquals(Monad.unit(5L),
3901 +                                 Monad.plus(zero, Monad.unit(5L)));
3902 +        // zero plus zero === zero
3903 +        Monad.assertZero(Monad.plus(zero, zero));
3904 +        {
3905 +            CompletableFuture<Long> f = Monad.plus(Monad.unit(5L),
3906 +                                                   Monad.unit(8L));
3907 +            // non-determinism
3908 +            assertTrue(f.get() == 5L || f.get() == 8L);
3909 +        }
3910 +
3911 +        CompletableFuture<Long> godot = new CompletableFuture<>();
3912 +        // f plus godot === f (doesn't wait for godot)
3913 +        Monad.assertFutureEquals(Monad.unit(5L),
3914 +                                 Monad.plus(Monad.unit(5L), godot));
3915 +        // godot plus f === f (doesn't wait for godot)
3916 +        Monad.assertFutureEquals(Monad.unit(5L),
3917 +                                 Monad.plus(godot, Monad.unit(5L)));
3918 +    }
3919 +
3920 + //     static <U> U join(CompletionStage<U> stage) {
3921 + //         CompletableFuture<U> f = new CompletableFuture<>();
3922 + //         stage.whenComplete((v, ex) -> {
3923 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3924 + //         });
3925 + //         return f.join();
3926 + //     }
3927 +
3928 + //     static <U> boolean isDone(CompletionStage<U> stage) {
3929 + //         CompletableFuture<U> f = new CompletableFuture<>();
3930 + //         stage.whenComplete((v, ex) -> {
3931 + //             if (ex != null) f.completeExceptionally(ex); else f.complete(v);
3932 + //         });
3933 + //         return f.isDone();
3934 + //     }
3935 +
3936 + //     static <U> U join2(CompletionStage<U> stage) {
3937 + //         return stage.toCompletableFuture().copy().join();
3938 + //     }
3939 +
3940 + //     static <U> boolean isDone2(CompletionStage<U> stage) {
3941 + //         return stage.toCompletableFuture().copy().isDone();
3942 + //     }
3943 +
3944   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines