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; |
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 |
|
|
459 |
|
class FailingSupplier extends CheckedAction |
460 |
|
implements Supplier<Integer> |
461 |
|
{ |
462 |
< |
FailingSupplier(ExecutionMode m) { super(m); } |
462 |
> |
final CFException ex; |
463 |
> |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
464 |
|
public Integer get() { |
465 |
|
invoked(); |
466 |
< |
throw new CFException(); |
466 |
> |
throw ex; |
467 |
|
} |
468 |
|
} |
469 |
|
|
470 |
|
class FailingConsumer extends CheckedIntegerAction |
471 |
|
implements Consumer<Integer> |
472 |
|
{ |
473 |
< |
FailingConsumer(ExecutionMode m) { super(m); } |
473 |
> |
final CFException ex; |
474 |
> |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
475 |
|
public void accept(Integer x) { |
476 |
|
invoked(); |
477 |
|
value = x; |
478 |
< |
throw new CFException(); |
478 |
> |
throw ex; |
479 |
|
} |
480 |
|
} |
481 |
|
|
482 |
|
class FailingBiConsumer extends CheckedIntegerAction |
483 |
|
implements BiConsumer<Integer, Integer> |
484 |
|
{ |
485 |
< |
FailingBiConsumer(ExecutionMode m) { super(m); } |
485 |
> |
final CFException ex; |
486 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
487 |
|
public void accept(Integer x, Integer y) { |
488 |
|
invoked(); |
489 |
|
value = subtract(x, y); |
490 |
< |
throw new CFException(); |
490 |
> |
throw ex; |
491 |
|
} |
492 |
|
} |
493 |
|
|
494 |
|
class FailingFunction extends CheckedIntegerAction |
495 |
|
implements Function<Integer, Integer> |
496 |
|
{ |
497 |
< |
FailingFunction(ExecutionMode m) { super(m); } |
497 |
> |
final CFException ex; |
498 |
> |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
499 |
|
public Integer apply(Integer x) { |
500 |
|
invoked(); |
501 |
|
value = x; |
502 |
< |
throw new CFException(); |
502 |
> |
throw ex; |
503 |
|
} |
504 |
|
} |
505 |
|
|
506 |
|
class FailingBiFunction extends CheckedIntegerAction |
507 |
|
implements BiFunction<Integer, Integer, Integer> |
508 |
|
{ |
509 |
< |
FailingBiFunction(ExecutionMode m) { super(m); } |
509 |
> |
final CFException ex; |
510 |
> |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
511 |
|
public Integer apply(Integer x, Integer y) { |
512 |
|
invoked(); |
513 |
|
value = subtract(x, y); |
514 |
< |
throw new CFException(); |
514 |
> |
throw ex; |
515 |
|
} |
516 |
|
} |
517 |
|
|
518 |
|
class FailingRunnable extends CheckedAction implements Runnable { |
519 |
< |
FailingRunnable(ExecutionMode m) { super(m); } |
519 |
> |
final CFException ex; |
520 |
> |
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
521 |
|
public void run() { |
522 |
|
invoked(); |
523 |
< |
throw new CFException(); |
523 |
> |
throw ex; |
524 |
|
} |
525 |
|
} |
526 |
|
|
540 |
|
class FailingCompletableFutureFunction extends CheckedIntegerAction |
541 |
|
implements Function<Integer, CompletableFuture<Integer>> |
542 |
|
{ |
543 |
< |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
543 |
> |
final CFException ex; |
544 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
545 |
|
public CompletableFuture<Integer> apply(Integer x) { |
546 |
|
invoked(); |
547 |
|
value = x; |
548 |
< |
throw new CFException(); |
548 |
> |
throw ex; |
549 |
|
} |
550 |
|
} |
551 |
|
|
847 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
848 |
|
final CompletableFuture<Integer> g = f.exceptionally |
849 |
|
((Throwable t) -> { |
831 |
– |
// Should not be called |
850 |
|
a.getAndIncrement(); |
851 |
< |
throw new AssertionError(); |
851 |
> |
threadFail("should not be called"); |
852 |
> |
return null; // unreached |
853 |
|
}); |
854 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
855 |
|
|
883 |
|
assertEquals(1, a.get()); |
884 |
|
}} |
885 |
|
|
886 |
+ |
/** |
887 |
+ |
* If an "exceptionally action" throws an exception, it completes |
888 |
+ |
* exceptionally with that exception |
889 |
+ |
*/ |
890 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
891 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
892 |
|
{ |
905 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
906 |
|
|
907 |
|
checkCompletedWithWrappedException(g, ex2); |
908 |
+ |
checkCompletedExceptionally(f, ex1); |
909 |
|
assertEquals(1, a.get()); |
910 |
|
}} |
911 |
|
|
913 |
|
* whenComplete action executes on normal completion, propagating |
914 |
|
* source result. |
915 |
|
*/ |
916 |
< |
public void testWhenComplete_normalCompletion1() { |
916 |
> |
public void testWhenComplete_normalCompletion() { |
917 |
|
for (ExecutionMode m : ExecutionMode.values()) |
918 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
919 |
|
for (Integer v1 : new Integer[] { 1, null }) |
923 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
924 |
|
final CompletableFuture<Integer> g = m.whenComplete |
925 |
|
(f, |
926 |
< |
(Integer x, Throwable t) -> { |
926 |
> |
(Integer result, Throwable t) -> { |
927 |
|
m.checkExecutionMode(); |
928 |
< |
threadAssertSame(x, v1); |
928 |
> |
threadAssertSame(result, v1); |
929 |
|
threadAssertNull(t); |
930 |
|
a.getAndIncrement(); |
931 |
|
}); |
950 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
951 |
|
final CompletableFuture<Integer> g = m.whenComplete |
952 |
|
(f, |
953 |
< |
(Integer x, Throwable t) -> { |
953 |
> |
(Integer result, Throwable t) -> { |
954 |
|
m.checkExecutionMode(); |
955 |
< |
threadAssertNull(x); |
955 |
> |
threadAssertNull(result); |
956 |
|
threadAssertSame(t, ex); |
957 |
|
a.getAndIncrement(); |
958 |
|
}); |
977 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
978 |
|
final CompletableFuture<Integer> g = m.whenComplete |
979 |
|
(f, |
980 |
< |
(Integer x, Throwable t) -> { |
980 |
> |
(Integer result, Throwable t) -> { |
981 |
|
m.checkExecutionMode(); |
982 |
< |
threadAssertNull(x); |
982 |
> |
threadAssertNull(result); |
983 |
|
threadAssertTrue(t instanceof CancellationException); |
984 |
|
a.getAndIncrement(); |
985 |
|
}); |
994 |
|
* If a whenComplete action throws an exception when triggered by |
995 |
|
* a normal completion, it completes exceptionally |
996 |
|
*/ |
997 |
< |
public void testWhenComplete_actionFailed() { |
997 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
998 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
999 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1000 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1005 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1006 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1007 |
|
(f, |
1008 |
< |
(Integer x, Throwable t) -> { |
1008 |
> |
(Integer result, Throwable t) -> { |
1009 |
|
m.checkExecutionMode(); |
1010 |
< |
threadAssertSame(x, v1); |
1010 |
> |
threadAssertSame(result, v1); |
1011 |
|
threadAssertNull(t); |
1012 |
|
a.getAndIncrement(); |
1013 |
|
throw ex; |
1022 |
|
/** |
1023 |
|
* If a whenComplete action throws an exception when triggered by |
1024 |
|
* a source completion that also throws an exception, the source |
1025 |
< |
* exception takes precedence. |
1025 |
> |
* exception takes precedence (unlike handle) |
1026 |
|
*/ |
1027 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1027 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1028 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1029 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1030 |
|
{ |
1036 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1037 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1038 |
|
(f, |
1039 |
< |
(Integer x, Throwable t) -> { |
1039 |
> |
(Integer result, Throwable t) -> { |
1040 |
|
m.checkExecutionMode(); |
1041 |
|
threadAssertSame(t, ex1); |
1042 |
< |
threadAssertNull(x); |
1042 |
> |
threadAssertNull(result); |
1043 |
|
a.getAndIncrement(); |
1044 |
|
throw ex2; |
1045 |
|
}); |
1047 |
|
|
1048 |
|
checkCompletedWithWrappedException(g, ex1); |
1049 |
|
checkCompletedExceptionally(f, ex1); |
1050 |
+ |
if (testImplementationDetails) { |
1051 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1052 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1053 |
+ |
} |
1054 |
|
assertEquals(1, a.get()); |
1055 |
|
}} |
1056 |
|
|
1068 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1069 |
|
final CompletableFuture<Integer> g = m.handle |
1070 |
|
(f, |
1071 |
< |
(Integer x, Throwable t) -> { |
1071 |
> |
(Integer result, Throwable t) -> { |
1072 |
|
m.checkExecutionMode(); |
1073 |
< |
threadAssertSame(x, v1); |
1073 |
> |
threadAssertSame(result, v1); |
1074 |
|
threadAssertNull(t); |
1075 |
|
a.getAndIncrement(); |
1076 |
|
return inc(v1); |
1097 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1098 |
|
final CompletableFuture<Integer> g = m.handle |
1099 |
|
(f, |
1100 |
< |
(Integer x, Throwable t) -> { |
1100 |
> |
(Integer result, Throwable t) -> { |
1101 |
|
m.checkExecutionMode(); |
1102 |
< |
threadAssertNull(x); |
1102 |
> |
threadAssertNull(result); |
1103 |
|
threadAssertSame(t, ex); |
1104 |
|
a.getAndIncrement(); |
1105 |
|
return v1; |
1126 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1127 |
|
final CompletableFuture<Integer> g = m.handle |
1128 |
|
(f, |
1129 |
< |
(Integer x, Throwable t) -> { |
1129 |
> |
(Integer result, Throwable t) -> { |
1130 |
|
m.checkExecutionMode(); |
1131 |
< |
threadAssertNull(x); |
1131 |
> |
threadAssertNull(result); |
1132 |
|
threadAssertTrue(t instanceof CancellationException); |
1133 |
|
a.getAndIncrement(); |
1134 |
|
return v1; |
1141 |
|
}} |
1142 |
|
|
1143 |
|
/** |
1144 |
< |
* handle result completes exceptionally if action does |
1144 |
> |
* If a "handle action" throws an exception when triggered by |
1145 |
> |
* a normal completion, it completes exceptionally |
1146 |
|
*/ |
1147 |
< |
public void testHandle_sourceFailedActionFailed() { |
1147 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1148 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1149 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1150 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1151 |
|
{ |
1152 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1153 |
|
final AtomicInteger a = new AtomicInteger(0); |
1154 |
< |
final CFException ex1 = new CFException(); |
1155 |
< |
final CFException ex2 = new CFException(); |
1126 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1154 |
> |
final CFException ex = new CFException(); |
1155 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1156 |
|
final CompletableFuture<Integer> g = m.handle |
1157 |
|
(f, |
1158 |
< |
(Integer x, Throwable t) -> { |
1158 |
> |
(Integer result, Throwable t) -> { |
1159 |
|
m.checkExecutionMode(); |
1160 |
< |
threadAssertNull(x); |
1161 |
< |
threadAssertSame(ex1, t); |
1160 |
> |
threadAssertSame(result, v1); |
1161 |
> |
threadAssertNull(t); |
1162 |
|
a.getAndIncrement(); |
1163 |
< |
throw ex2; |
1163 |
> |
throw ex; |
1164 |
|
}); |
1165 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1165 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1166 |
|
|
1167 |
< |
checkCompletedWithWrappedException(g, ex2); |
1168 |
< |
checkCompletedExceptionally(f, ex1); |
1167 |
> |
checkCompletedWithWrappedException(g, ex); |
1168 |
> |
checkCompletedNormally(f, v1); |
1169 |
|
assertEquals(1, a.get()); |
1170 |
|
}} |
1171 |
|
|
1172 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1173 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1172 |
> |
/** |
1173 |
> |
* If a "handle action" throws an exception when triggered by |
1174 |
> |
* a source completion that also throws an exception, the action |
1175 |
> |
* exception takes precedence (unlike whenComplete) |
1176 |
> |
*/ |
1177 |
> |
public void testHandle_sourceFailedActionFailed() { |
1178 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1179 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1179 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1180 |
|
{ |
1148 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1181 |
|
final AtomicInteger a = new AtomicInteger(0); |
1182 |
< |
final CFException ex = new CFException(); |
1183 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1182 |
> |
final CFException ex1 = new CFException(); |
1183 |
> |
final CFException ex2 = new CFException(); |
1184 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1185 |
> |
|
1186 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1187 |
|
final CompletableFuture<Integer> g = m.handle |
1188 |
|
(f, |
1189 |
< |
(Integer x, Throwable t) -> { |
1189 |
> |
(Integer result, Throwable t) -> { |
1190 |
|
m.checkExecutionMode(); |
1191 |
< |
threadAssertSame(x, v1); |
1192 |
< |
threadAssertNull(t); |
1191 |
> |
threadAssertNull(result); |
1192 |
> |
threadAssertSame(ex1, t); |
1193 |
|
a.getAndIncrement(); |
1194 |
< |
throw ex; |
1194 |
> |
throw ex2; |
1195 |
|
}); |
1196 |
< |
if (createIncomplete) assertTrue(f.complete(v1)); |
1196 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1197 |
|
|
1198 |
< |
checkCompletedWithWrappedException(g, ex); |
1199 |
< |
checkCompletedNormally(f, v1); |
1198 |
> |
checkCompletedWithWrappedException(g, ex2); |
1199 |
> |
checkCompletedExceptionally(f, ex1); |
1200 |
|
assertEquals(1, a.get()); |
1201 |
|
}} |
1202 |
|
|
1229 |
|
{ |
1230 |
|
final FailingRunnable r = new FailingRunnable(m); |
1231 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1232 |
< |
checkCompletedWithWrappedCFException(f); |
1232 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1233 |
|
r.assertInvoked(); |
1234 |
|
}} |
1235 |
|
|
1263 |
|
{ |
1264 |
|
FailingSupplier r = new FailingSupplier(m); |
1265 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1266 |
< |
checkCompletedWithWrappedCFException(f); |
1266 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1267 |
|
r.assertInvoked(); |
1268 |
|
}} |
1269 |
|
|
1385 |
|
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1386 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1387 |
|
|
1388 |
< |
checkCompletedWithWrappedCFException(h0); |
1389 |
< |
checkCompletedWithWrappedCFException(h1); |
1390 |
< |
checkCompletedWithWrappedCFException(h2); |
1391 |
< |
checkCompletedWithWrappedCFException(h3); |
1392 |
< |
checkCompletedWithWrappedCFException(h4); |
1393 |
< |
checkCompletedWithWrappedCFException(h5); |
1388 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1389 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1390 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1391 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1392 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1393 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1394 |
|
checkCompletedNormally(f, v1); |
1395 |
|
}} |
1396 |
|
|
1489 |
|
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1490 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1491 |
|
|
1492 |
< |
checkCompletedWithWrappedCFException(h0); |
1493 |
< |
checkCompletedWithWrappedCFException(h1); |
1494 |
< |
checkCompletedWithWrappedCFException(h2); |
1495 |
< |
checkCompletedWithWrappedCFException(h3); |
1492 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1493 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1494 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1495 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1496 |
|
checkCompletedNormally(f, v1); |
1497 |
|
}} |
1498 |
|
|
1591 |
|
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1592 |
|
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1593 |
|
|
1594 |
< |
checkCompletedWithWrappedCFException(h0); |
1595 |
< |
checkCompletedWithWrappedCFException(h1); |
1596 |
< |
checkCompletedWithWrappedCFException(h2); |
1597 |
< |
checkCompletedWithWrappedCFException(h3); |
1594 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1595 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1596 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1597 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1598 |
|
checkCompletedNormally(f, v1); |
1599 |
|
}} |
1600 |
|
|
1756 |
|
assertTrue(snd.complete(w2)); |
1757 |
|
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1758 |
|
|
1759 |
< |
checkCompletedWithWrappedCFException(h1); |
1760 |
< |
checkCompletedWithWrappedCFException(h2); |
1761 |
< |
checkCompletedWithWrappedCFException(h3); |
1759 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1760 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1761 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1762 |
|
r1.assertInvoked(); |
1763 |
|
r2.assertInvoked(); |
1764 |
|
r3.assertInvoked(); |
1920 |
|
assertTrue(snd.complete(w2)); |
1921 |
|
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1922 |
|
|
1923 |
< |
checkCompletedWithWrappedCFException(h1); |
1924 |
< |
checkCompletedWithWrappedCFException(h2); |
1925 |
< |
checkCompletedWithWrappedCFException(h3); |
1923 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1924 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1925 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1926 |
|
r1.assertInvoked(); |
1927 |
|
r2.assertInvoked(); |
1928 |
|
r3.assertInvoked(); |
2084 |
|
assertTrue(snd.complete(w2)); |
2085 |
|
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2086 |
|
|
2087 |
< |
checkCompletedWithWrappedCFException(h1); |
2088 |
< |
checkCompletedWithWrappedCFException(h2); |
2089 |
< |
checkCompletedWithWrappedCFException(h3); |
2087 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2088 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2089 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2090 |
|
r1.assertInvoked(); |
2091 |
|
r2.assertInvoked(); |
2092 |
|
r3.assertInvoked(); |
2376 |
|
f.complete(v1); |
2377 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2378 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2379 |
< |
checkCompletedWithWrappedCFException(h0); |
2380 |
< |
checkCompletedWithWrappedCFException(h1); |
2381 |
< |
checkCompletedWithWrappedCFException(h2); |
2382 |
< |
checkCompletedWithWrappedCFException(h3); |
2379 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2380 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2381 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2382 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2383 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2384 |
|
|
2385 |
|
g.complete(v2); |
2388 |
|
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2389 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2390 |
|
|
2391 |
< |
checkCompletedWithWrappedCFException(h4); |
2391 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2392 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2393 |
|
Objects.equals(v2, rs[4].value)); |
2394 |
< |
checkCompletedWithWrappedCFException(h5); |
2394 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2395 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2396 |
|
Objects.equals(v2, rs[5].value)); |
2397 |
|
|
2635 |
|
f.complete(v1); |
2636 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2637 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2638 |
< |
checkCompletedWithWrappedCFException(h0); |
2639 |
< |
checkCompletedWithWrappedCFException(h1); |
2640 |
< |
checkCompletedWithWrappedCFException(h2); |
2641 |
< |
checkCompletedWithWrappedCFException(h3); |
2638 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2639 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2640 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2641 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2642 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2643 |
|
|
2644 |
|
g.complete(v2); |
2647 |
|
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2648 |
|
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2649 |
|
|
2650 |
< |
checkCompletedWithWrappedCFException(h4); |
2650 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2651 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2652 |
|
Objects.equals(v2, rs[4].value)); |
2653 |
< |
checkCompletedWithWrappedCFException(h5); |
2653 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2654 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2655 |
|
Objects.equals(v2, rs[5].value)); |
2656 |
|
|
2890 |
|
assertTrue(f.complete(v1)); |
2891 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2892 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2893 |
< |
checkCompletedWithWrappedCFException(h0); |
2894 |
< |
checkCompletedWithWrappedCFException(h1); |
2895 |
< |
checkCompletedWithWrappedCFException(h2); |
2896 |
< |
checkCompletedWithWrappedCFException(h3); |
2893 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2894 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2895 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2896 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2897 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2898 |
|
assertTrue(g.complete(v2)); |
2899 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2900 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2901 |
< |
checkCompletedWithWrappedCFException(h4); |
2902 |
< |
checkCompletedWithWrappedCFException(h5); |
2901 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2902 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2903 |
|
|
2904 |
|
checkCompletedNormally(f, v1); |
2905 |
|
checkCompletedNormally(g, v2); |
2960 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2961 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
2962 |
|
|
2963 |
< |
checkCompletedWithWrappedCFException(g); |
2963 |
> |
checkCompletedWithWrappedException(g, r.ex); |
2964 |
|
checkCompletedNormally(f, v1); |
2965 |
|
}} |
2966 |
|
|
3069 |
|
} |
3070 |
|
} |
3071 |
|
|
3072 |
< |
public void testAllOf_backwards() throws Exception { |
3072 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3073 |
|
for (int k = 1; k < 10; k++) { |
3074 |
|
CompletableFuture<Integer>[] fs |
3075 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3302 |
|
() -> f.obtrudeException(null), |
3303 |
|
|
3304 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3305 |
< |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3305 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3306 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3307 |
|
|
3308 |
|
() -> f.orTimeout(1L, null), |
3532 |
|
long timeoutMillis = timeoutMillis(); |
3533 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3534 |
|
long startTime = System.nanoTime(); |
3535 |
< |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3535 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3536 |
|
checkCompletedWithTimeoutException(f); |
3537 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3538 |
|
} |
3547 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3548 |
|
long startTime = System.nanoTime(); |
3549 |
|
f.complete(v1); |
3550 |
< |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3551 |
< |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3550 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3551 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3552 |
|
g.complete(v1); |
3553 |
|
checkCompletedNormally(f, v1); |
3554 |
|
checkCompletedNormally(g, v1); |
3563 |
|
() -> testCompleteOnTimeout_timesOut(null)); |
3564 |
|
} |
3565 |
|
|
3566 |
+ |
/** |
3567 |
+ |
* completeOnTimeout completes with given value if not complete |
3568 |
+ |
*/ |
3569 |
|
public void testCompleteOnTimeout_timesOut(Integer v) { |
3570 |
|
long timeoutMillis = timeoutMillis(); |
3571 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3572 |
|
long startTime = System.nanoTime(); |
3573 |
< |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3573 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3574 |
|
assertSame(v, f.join()); |
3575 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3576 |
|
f.complete(99); // should have no effect |
3587 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3588 |
|
long startTime = System.nanoTime(); |
3589 |
|
f.complete(v1); |
3590 |
< |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3591 |
< |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3590 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3591 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3592 |
|
g.complete(v1); |
3593 |
|
checkCompletedNormally(f, v1); |
3594 |
|
checkCompletedNormally(g, v1); |
3605 |
|
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3606 |
|
} |
3607 |
|
|
3570 |
– |
/** |
3571 |
– |
* delayedExecutor returns an executor that delays submission |
3572 |
– |
*/ |
3608 |
|
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3609 |
|
long timeoutMillis = timeoutMillis(); |
3610 |
|
// Use an "unreasonably long" long timeout to catch lingering threads |
3639 |
|
//--- tests of implementation details; not part of official tck --- |
3640 |
|
|
3641 |
|
Object resultOf(CompletableFuture<?> f) { |
3642 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3643 |
+ |
if (sm != null) { |
3644 |
+ |
try { |
3645 |
+ |
System.setSecurityManager(null); |
3646 |
+ |
} catch (SecurityException giveUp) { |
3647 |
+ |
return "Reflection not available"; |
3648 |
+ |
} |
3649 |
+ |
} |
3650 |
+ |
|
3651 |
|
try { |
3652 |
|
java.lang.reflect.Field resultField |
3653 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3654 |
|
resultField.setAccessible(true); |
3655 |
|
return resultField.get(f); |
3656 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3656 |
> |
} catch (Throwable t) { |
3657 |
> |
throw new AssertionError(t); |
3658 |
> |
} finally { |
3659 |
> |
if (sm != null) System.setSecurityManager(sm); |
3660 |
> |
} |
3661 |
|
} |
3662 |
|
|
3663 |
|
public void testExceptionPropagationReusesResultObject() { |
3680 |
|
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3681 |
|
|
3682 |
|
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3683 |
+ |
funs.add((y) -> m.runAfterBoth(v42, y, new Noop(m))); |
3684 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3685 |
+ |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3686 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3687 |
+ |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3688 |
|
|
3689 |
< |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3689 |
> |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3690 |
|
|
3691 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3692 |
|
|
3693 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y})); |
3694 |
|
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3695 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {v42, y})); |
3696 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y})); |
3697 |
|
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3698 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {incomplete, y})); |
3699 |
|
|
3700 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3701 |
|
fun : funs) { |
3746 |
|
} |
3747 |
|
}} |
3748 |
|
|
3749 |
+ |
/** |
3750 |
+ |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3751 |
+ |
*/ |
3752 |
+ |
public void testMinimalCompletionStage_minimality() { |
3753 |
+ |
if (!testImplementationDetails) return; |
3754 |
+ |
Function<Method, String> toSignature = |
3755 |
+ |
(method) -> method.getName() + Arrays.toString(method.getParameterTypes()); |
3756 |
+ |
Predicate<Method> isNotStatic = |
3757 |
+ |
(method) -> (method.getModifiers() & Modifier.STATIC) == 0; |
3758 |
+ |
List<Method> minimalMethods = |
3759 |
+ |
Stream.of(Object.class, CompletionStage.class) |
3760 |
+ |
.flatMap((klazz) -> Stream.of(klazz.getMethods())) |
3761 |
+ |
.filter(isNotStatic) |
3762 |
+ |
.collect(Collectors.toList()); |
3763 |
+ |
// Methods from CompletableFuture permitted NOT to throw UOE |
3764 |
+ |
String[] signatureWhitelist = { |
3765 |
+ |
"newIncompleteFuture[]", |
3766 |
+ |
"defaultExecutor[]", |
3767 |
+ |
"minimalCompletionStage[]", |
3768 |
+ |
"copy[]", |
3769 |
+ |
}; |
3770 |
+ |
Set<String> permittedMethodSignatures = |
3771 |
+ |
Stream.concat(minimalMethods.stream().map(toSignature), |
3772 |
+ |
Stream.of(signatureWhitelist)) |
3773 |
+ |
.collect(Collectors.toSet()); |
3774 |
+ |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
3775 |
+ |
.filter(isNotStatic) |
3776 |
+ |
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3777 |
+ |
.collect(Collectors.toList()); |
3778 |
+ |
|
3779 |
+ |
CompletionStage<Integer> minimalStage = |
3780 |
+ |
new CompletableFuture<Integer>().minimalCompletionStage(); |
3781 |
+ |
|
3782 |
+ |
List<Method> bugs = new ArrayList<>(); |
3783 |
+ |
for (Method method : allMethods) { |
3784 |
+ |
Class<?>[] parameterTypes = method.getParameterTypes(); |
3785 |
+ |
Object[] args = new Object[parameterTypes.length]; |
3786 |
+ |
// Manufacture boxed primitives for primitive params |
3787 |
+ |
for (int i = 0; i < args.length; i++) { |
3788 |
+ |
Class<?> type = parameterTypes[i]; |
3789 |
+ |
if (parameterTypes[i] == boolean.class) |
3790 |
+ |
args[i] = false; |
3791 |
+ |
else if (parameterTypes[i] == int.class) |
3792 |
+ |
args[i] = 0; |
3793 |
+ |
else if (parameterTypes[i] == long.class) |
3794 |
+ |
args[i] = 0L; |
3795 |
+ |
} |
3796 |
+ |
try { |
3797 |
+ |
method.invoke(minimalStage, args); |
3798 |
+ |
bugs.add(method); |
3799 |
+ |
} |
3800 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3801 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3802 |
+ |
bugs.add(method); |
3803 |
+ |
// expected.getCause().printStackTrace(); |
3804 |
+ |
} |
3805 |
+ |
} |
3806 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
3807 |
+ |
} |
3808 |
+ |
if (!bugs.isEmpty()) |
3809 |
+ |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
3810 |
+ |
} |
3811 |
+ |
|
3812 |
+ |
static class Monad { |
3813 |
+ |
static class ZeroException extends RuntimeException { |
3814 |
+ |
public ZeroException() { super("monadic zero"); } |
3815 |
+ |
} |
3816 |
+ |
// "return", "unit" |
3817 |
+ |
static <T> CompletableFuture<T> unit(T value) { |
3818 |
+ |
return completedFuture(value); |
3819 |
+ |
} |
3820 |
+ |
// monadic zero ? |
3821 |
+ |
static <T> CompletableFuture<T> zero() { |
3822 |
+ |
return failedFuture(new ZeroException()); |
3823 |
+ |
} |
3824 |
+ |
// >=> |
3825 |
+ |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
3826 |
+ |
(Function<T, CompletableFuture<U>> f, |
3827 |
+ |
Function<U, CompletableFuture<V>> g) { |
3828 |
+ |
return (x) -> f.apply(x).thenCompose(g); |
3829 |
+ |
} |
3830 |
+ |
|
3831 |
+ |
static void assertZero(CompletableFuture<?> f) { |
3832 |
+ |
try { |
3833 |
+ |
f.getNow(null); |
3834 |
+ |
throw new AssertionFailedError("should throw"); |
3835 |
+ |
} catch (CompletionException success) { |
3836 |
+ |
assertTrue(success.getCause() instanceof ZeroException); |
3837 |
+ |
} |
3838 |
+ |
} |
3839 |
+ |
|
3840 |
+ |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
3841 |
+ |
CompletableFuture<T> g) { |
3842 |
+ |
T fval = null, gval = null; |
3843 |
+ |
Throwable fex = null, gex = null; |
3844 |
+ |
|
3845 |
+ |
try { fval = f.get(); } |
3846 |
+ |
catch (ExecutionException ex) { fex = ex.getCause(); } |
3847 |
+ |
catch (Throwable ex) { fex = ex; } |
3848 |
+ |
|
3849 |
+ |
try { gval = g.get(); } |
3850 |
+ |
catch (ExecutionException ex) { gex = ex.getCause(); } |
3851 |
+ |
catch (Throwable ex) { gex = ex; } |
3852 |
+ |
|
3853 |
+ |
if (fex != null || gex != null) |
3854 |
+ |
assertSame(fex.getClass(), gex.getClass()); |
3855 |
+ |
else |
3856 |
+ |
assertEquals(fval, gval); |
3857 |
+ |
} |
3858 |
+ |
|
3859 |
+ |
static class PlusFuture<T> extends CompletableFuture<T> { |
3860 |
+ |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
3861 |
+ |
} |
3862 |
+ |
|
3863 |
+ |
/** Implements "monadic plus". */ |
3864 |
+ |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
3865 |
+ |
CompletableFuture<? extends T> g) { |
3866 |
+ |
PlusFuture<T> plus = new PlusFuture<T>(); |
3867 |
+ |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
3868 |
+ |
try { |
3869 |
+ |
if (ex == null) { |
3870 |
+ |
if (plus.complete(result)) |
3871 |
+ |
if (plus.firstFailure.get() != null) |
3872 |
+ |
plus.firstFailure.set(null); |
3873 |
+ |
} |
3874 |
+ |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
3875 |
+ |
if (plus.isDone()) |
3876 |
+ |
plus.firstFailure.set(null); |
3877 |
+ |
} |
3878 |
+ |
else { |
3879 |
+ |
// first failure has precedence |
3880 |
+ |
Throwable first = plus.firstFailure.getAndSet(null); |
3881 |
+ |
|
3882 |
+ |
// may fail with "Self-suppression not permitted" |
3883 |
+ |
try { first.addSuppressed(ex); } |
3884 |
+ |
catch (Exception ignored) {} |
3885 |
+ |
|
3886 |
+ |
plus.completeExceptionally(first); |
3887 |
+ |
} |
3888 |
+ |
} catch (Throwable unexpected) { |
3889 |
+ |
plus.completeExceptionally(unexpected); |
3890 |
+ |
} |
3891 |
+ |
}; |
3892 |
+ |
f.whenComplete(action); |
3893 |
+ |
g.whenComplete(action); |
3894 |
+ |
return plus; |
3895 |
+ |
} |
3896 |
+ |
} |
3897 |
+ |
|
3898 |
+ |
/** |
3899 |
+ |
* CompletableFuture is an additive monad - sort of. |
3900 |
+ |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
3901 |
+ |
*/ |
3902 |
+ |
public void testAdditiveMonad() throws Throwable { |
3903 |
+ |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
3904 |
+ |
CompletableFuture<Long> zero = Monad.zero(); |
3905 |
+ |
|
3906 |
+ |
// Some mutually non-commutative functions |
3907 |
+ |
Function<Long, CompletableFuture<Long>> triple |
3908 |
+ |
= (x) -> Monad.unit(3 * x); |
3909 |
+ |
Function<Long, CompletableFuture<Long>> inc |
3910 |
+ |
= (x) -> Monad.unit(x + 1); |
3911 |
+ |
|
3912 |
+ |
// unit is a right identity: m >>= unit === m |
3913 |
+ |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
3914 |
+ |
inc.apply(5L)); |
3915 |
+ |
// unit is a left identity: (unit x) >>= f === f x |
3916 |
+ |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
3917 |
+ |
inc.apply(5L)); |
3918 |
+ |
|
3919 |
+ |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
3920 |
+ |
Monad.assertFutureEquals( |
3921 |
+ |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
3922 |
+ |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
3923 |
+ |
|
3924 |
+ |
// The case for CompletableFuture as an additive monad is weaker... |
3925 |
+ |
|
3926 |
+ |
// zero is a monadic zero |
3927 |
+ |
Monad.assertZero(zero); |
3928 |
+ |
|
3929 |
+ |
// left zero: zero >>= f === zero |
3930 |
+ |
Monad.assertZero(zero.thenCompose(inc)); |
3931 |
+ |
// right zero: f >>= (\x -> zero) === zero |
3932 |
+ |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
3933 |
+ |
|
3934 |
+ |
// f plus zero === f |
3935 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3936 |
+ |
Monad.plus(Monad.unit(5L), zero)); |
3937 |
+ |
// zero plus f === f |
3938 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3939 |
+ |
Monad.plus(zero, Monad.unit(5L))); |
3940 |
+ |
// zero plus zero === zero |
3941 |
+ |
Monad.assertZero(Monad.plus(zero, zero)); |
3942 |
+ |
{ |
3943 |
+ |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
3944 |
+ |
Monad.unit(8L)); |
3945 |
+ |
// non-determinism |
3946 |
+ |
assertTrue(f.get() == 5L || f.get() == 8L); |
3947 |
+ |
} |
3948 |
+ |
|
3949 |
+ |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
3950 |
+ |
// f plus godot === f (doesn't wait for godot) |
3951 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3952 |
+ |
Monad.plus(Monad.unit(5L), godot)); |
3953 |
+ |
// godot plus f === f (doesn't wait for godot) |
3954 |
+ |
Monad.assertFutureEquals(Monad.unit(5L), |
3955 |
+ |
Monad.plus(godot, Monad.unit(5L))); |
3956 |
+ |
} |
3957 |
+ |
|
3958 |
+ |
/** |
3959 |
+ |
* A single CompletableFuture with many dependents. |
3960 |
+ |
* A demo of scalability - runtime is O(n). |
3961 |
+ |
*/ |
3962 |
+ |
public void testManyDependents() throws Throwable { |
3963 |
+ |
final int n = 1_000; |
3964 |
+ |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
3965 |
+ |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
3966 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
3967 |
+ |
for (int i = 0; i < n; i++) { |
3968 |
+ |
head.thenRun(() -> count.getAndIncrement()); |
3969 |
+ |
head.thenAccept((x) -> count.getAndIncrement()); |
3970 |
+ |
head.thenApply((x) -> count.getAndIncrement()); |
3971 |
+ |
|
3972 |
+ |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
3973 |
+ |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
3974 |
+ |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
3975 |
+ |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
3976 |
+ |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
3977 |
+ |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
3978 |
+ |
|
3979 |
+ |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
3980 |
+ |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3981 |
+ |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
3982 |
+ |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
3983 |
+ |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
3984 |
+ |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
3985 |
+ |
} |
3986 |
+ |
head.complete(null); |
3987 |
+ |
assertEquals(5 * 3 * n, count.get()); |
3988 |
+ |
} |
3989 |
+ |
|
3990 |
+ |
// static <U> U join(CompletionStage<U> stage) { |
3991 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
3992 |
+ |
// stage.whenComplete((v, ex) -> { |
3993 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
3994 |
+ |
// }); |
3995 |
+ |
// return f.join(); |
3996 |
+ |
// } |
3997 |
+ |
|
3998 |
+ |
// static <U> boolean isDone(CompletionStage<U> stage) { |
3999 |
+ |
// CompletableFuture<U> f = new CompletableFuture<>(); |
4000 |
+ |
// stage.whenComplete((v, ex) -> { |
4001 |
+ |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
4002 |
+ |
// }); |
4003 |
+ |
// return f.isDone(); |
4004 |
+ |
// } |
4005 |
+ |
|
4006 |
+ |
// static <U> U join2(CompletionStage<U> stage) { |
4007 |
+ |
// return stage.toCompletableFuture().copy().join(); |
4008 |
+ |
// } |
4009 |
+ |
|
4010 |
+ |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
4011 |
+ |
// return stage.toCompletableFuture().copy().isDone(); |
4012 |
+ |
// } |
4013 |
+ |
|
4014 |
|
} |