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; |
30 |
|
import java.util.concurrent.Executor; |
31 |
|
import java.util.concurrent.ForkJoinPool; |
32 |
|
import java.util.concurrent.ForkJoinTask; |
33 |
+ |
import java.util.concurrent.RejectedExecutionException; |
34 |
|
import java.util.concurrent.TimeoutException; |
32 |
– |
import java.util.concurrent.TimeUnit; |
35 |
|
import java.util.concurrent.atomic.AtomicInteger; |
36 |
|
import java.util.concurrent.atomic.AtomicReference; |
37 |
|
import java.util.function.BiConsumer; |
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 |
|
|
361 |
|
checkCompletedNormally(f, "test"); |
362 |
|
} |
363 |
|
|
364 |
< |
abstract class CheckedAction { |
364 |
> |
abstract static class CheckedAction { |
365 |
|
int invocationCount = 0; |
366 |
|
final ExecutionMode m; |
367 |
|
CheckedAction(ExecutionMode m) { this.m = m; } |
373 |
|
void assertInvoked() { assertEquals(1, invocationCount); } |
374 |
|
} |
375 |
|
|
376 |
< |
abstract class CheckedIntegerAction extends CheckedAction { |
376 |
> |
abstract static class CheckedIntegerAction extends CheckedAction { |
377 |
|
Integer value; |
378 |
|
CheckedIntegerAction(ExecutionMode m) { super(m); } |
379 |
|
void assertValue(Integer expected) { |
382 |
|
} |
383 |
|
} |
384 |
|
|
385 |
< |
class IntegerSupplier extends CheckedAction |
385 |
> |
static class IntegerSupplier extends CheckedAction |
386 |
|
implements Supplier<Integer> |
387 |
|
{ |
388 |
|
final Integer value; |
401 |
|
return (x == null) ? null : x + 1; |
402 |
|
} |
403 |
|
|
404 |
< |
class NoopConsumer extends CheckedIntegerAction |
404 |
> |
static class NoopConsumer extends CheckedIntegerAction |
405 |
|
implements Consumer<Integer> |
406 |
|
{ |
407 |
|
NoopConsumer(ExecutionMode m) { super(m); } |
411 |
|
} |
412 |
|
} |
413 |
|
|
414 |
< |
class IncFunction extends CheckedIntegerAction |
414 |
> |
static class IncFunction extends CheckedIntegerAction |
415 |
|
implements Function<Integer,Integer> |
416 |
|
{ |
417 |
|
IncFunction(ExecutionMode m) { super(m); } |
429 |
|
- ((y == null) ? 99 : y.intValue()); |
430 |
|
} |
431 |
|
|
432 |
< |
class SubtractAction extends CheckedIntegerAction |
432 |
> |
static class SubtractAction extends CheckedIntegerAction |
433 |
|
implements BiConsumer<Integer, Integer> |
434 |
|
{ |
435 |
|
SubtractAction(ExecutionMode m) { super(m); } |
439 |
|
} |
440 |
|
} |
441 |
|
|
442 |
< |
class SubtractFunction extends CheckedIntegerAction |
442 |
> |
static class SubtractFunction extends CheckedIntegerAction |
443 |
|
implements BiFunction<Integer, Integer, Integer> |
444 |
|
{ |
445 |
|
SubtractFunction(ExecutionMode m) { super(m); } |
449 |
|
} |
450 |
|
} |
451 |
|
|
452 |
< |
class Noop extends CheckedAction implements Runnable { |
452 |
> |
static class Noop extends CheckedAction implements Runnable { |
453 |
|
Noop(ExecutionMode m) { super(m); } |
454 |
|
public void run() { |
455 |
|
invoked(); |
456 |
|
} |
457 |
|
} |
458 |
|
|
459 |
< |
class FailingSupplier extends CheckedAction |
459 |
> |
static 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 |
470 |
> |
static 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 |
482 |
> |
static 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 |
494 |
> |
static 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 |
506 |
> |
static 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); } |
518 |
> |
static class FailingRunnable extends CheckedAction implements Runnable { |
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 |
|
|
527 |
< |
class CompletableFutureInc extends CheckedIntegerAction |
527 |
> |
static class CompletableFutureInc extends CheckedIntegerAction |
528 |
|
implements Function<Integer, CompletableFuture<Integer>> |
529 |
|
{ |
530 |
|
CompletableFutureInc(ExecutionMode m) { super(m); } |
537 |
|
} |
538 |
|
} |
539 |
|
|
540 |
< |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
540 |
> |
static 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 |
> |
|
552 |
> |
static class CountingRejectingExecutor implements Executor { |
553 |
> |
final RejectedExecutionException ex = new RejectedExecutionException(); |
554 |
> |
final AtomicInteger count = new AtomicInteger(0); |
555 |
> |
public void execute(Runnable r) { |
556 |
> |
count.getAndIncrement(); |
557 |
> |
throw ex; |
558 |
|
} |
559 |
|
} |
560 |
|
|
892 |
|
assertEquals(1, a.get()); |
893 |
|
}} |
894 |
|
|
895 |
+ |
/** |
896 |
+ |
* If an "exceptionally action" throws an exception, it completes |
897 |
+ |
* exceptionally with that exception |
898 |
+ |
*/ |
899 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
900 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
901 |
|
{ |
914 |
|
if (createIncomplete) f.completeExceptionally(ex1); |
915 |
|
|
916 |
|
checkCompletedWithWrappedException(g, ex2); |
917 |
+ |
checkCompletedExceptionally(f, ex1); |
918 |
|
assertEquals(1, a.get()); |
919 |
|
}} |
920 |
|
|
932 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
933 |
|
final CompletableFuture<Integer> g = m.whenComplete |
934 |
|
(f, |
935 |
< |
(Integer x, Throwable t) -> { |
935 |
> |
(Integer result, Throwable t) -> { |
936 |
|
m.checkExecutionMode(); |
937 |
< |
threadAssertSame(x, v1); |
937 |
> |
threadAssertSame(result, v1); |
938 |
|
threadAssertNull(t); |
939 |
|
a.getAndIncrement(); |
940 |
|
}); |
959 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
960 |
|
final CompletableFuture<Integer> g = m.whenComplete |
961 |
|
(f, |
962 |
< |
(Integer x, Throwable t) -> { |
962 |
> |
(Integer result, Throwable t) -> { |
963 |
|
m.checkExecutionMode(); |
964 |
< |
threadAssertNull(x); |
964 |
> |
threadAssertNull(result); |
965 |
|
threadAssertSame(t, ex); |
966 |
|
a.getAndIncrement(); |
967 |
|
}); |
986 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
987 |
|
final CompletableFuture<Integer> g = m.whenComplete |
988 |
|
(f, |
989 |
< |
(Integer x, Throwable t) -> { |
989 |
> |
(Integer result, Throwable t) -> { |
990 |
|
m.checkExecutionMode(); |
991 |
< |
threadAssertNull(x); |
991 |
> |
threadAssertNull(result); |
992 |
|
threadAssertTrue(t instanceof CancellationException); |
993 |
|
a.getAndIncrement(); |
994 |
|
}); |
1003 |
|
* If a whenComplete action throws an exception when triggered by |
1004 |
|
* a normal completion, it completes exceptionally |
1005 |
|
*/ |
1006 |
< |
public void testWhenComplete_actionFailed() { |
1006 |
> |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
1007 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1008 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1009 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1014 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1015 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1016 |
|
(f, |
1017 |
< |
(Integer x, Throwable t) -> { |
1017 |
> |
(Integer result, Throwable t) -> { |
1018 |
|
m.checkExecutionMode(); |
1019 |
< |
threadAssertSame(x, v1); |
1019 |
> |
threadAssertSame(result, v1); |
1020 |
|
threadAssertNull(t); |
1021 |
|
a.getAndIncrement(); |
1022 |
|
throw ex; |
1031 |
|
/** |
1032 |
|
* If a whenComplete action throws an exception when triggered by |
1033 |
|
* a source completion that also throws an exception, the source |
1034 |
< |
* exception takes precedence. |
1034 |
> |
* exception takes precedence (unlike handle) |
1035 |
|
*/ |
1036 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
1036 |
> |
public void testWhenComplete_sourceFailedActionFailed() { |
1037 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1038 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1039 |
|
{ |
1045 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
1046 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1047 |
|
(f, |
1048 |
< |
(Integer x, Throwable t) -> { |
1048 |
> |
(Integer result, Throwable t) -> { |
1049 |
|
m.checkExecutionMode(); |
1050 |
|
threadAssertSame(t, ex1); |
1051 |
< |
threadAssertNull(x); |
1051 |
> |
threadAssertNull(result); |
1052 |
|
a.getAndIncrement(); |
1053 |
|
throw ex2; |
1054 |
|
}); |
1056 |
|
|
1057 |
|
checkCompletedWithWrappedException(g, ex1); |
1058 |
|
checkCompletedExceptionally(f, ex1); |
1059 |
+ |
if (testImplementationDetails) { |
1060 |
+ |
assertEquals(1, ex1.getSuppressed().length); |
1061 |
+ |
assertSame(ex2, ex1.getSuppressed()[0]); |
1062 |
+ |
} |
1063 |
|
assertEquals(1, a.get()); |
1064 |
|
}} |
1065 |
|
|
1077 |
|
if (!createIncomplete) assertTrue(f.complete(v1)); |
1078 |
|
final CompletableFuture<Integer> g = m.handle |
1079 |
|
(f, |
1080 |
< |
(Integer x, Throwable t) -> { |
1080 |
> |
(Integer result, Throwable t) -> { |
1081 |
|
m.checkExecutionMode(); |
1082 |
< |
threadAssertSame(x, v1); |
1082 |
> |
threadAssertSame(result, v1); |
1083 |
|
threadAssertNull(t); |
1084 |
|
a.getAndIncrement(); |
1085 |
|
return inc(v1); |
1106 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
1107 |
|
final CompletableFuture<Integer> g = m.handle |
1108 |
|
(f, |
1109 |
< |
(Integer x, Throwable t) -> { |
1109 |
> |
(Integer result, Throwable t) -> { |
1110 |
|
m.checkExecutionMode(); |
1111 |
< |
threadAssertNull(x); |
1111 |
> |
threadAssertNull(result); |
1112 |
|
threadAssertSame(t, ex); |
1113 |
|
a.getAndIncrement(); |
1114 |
|
return v1; |
1135 |
|
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1136 |
|
final CompletableFuture<Integer> g = m.handle |
1137 |
|
(f, |
1138 |
< |
(Integer x, Throwable t) -> { |
1138 |
> |
(Integer result, Throwable t) -> { |
1139 |
|
m.checkExecutionMode(); |
1140 |
< |
threadAssertNull(x); |
1140 |
> |
threadAssertNull(result); |
1141 |
|
threadAssertTrue(t instanceof CancellationException); |
1142 |
|
a.getAndIncrement(); |
1143 |
|
return v1; |
1150 |
|
}} |
1151 |
|
|
1152 |
|
/** |
1153 |
< |
* handle result completes exceptionally if action does |
1153 |
> |
* If a "handle action" throws an exception when triggered by |
1154 |
> |
* a normal completion, it completes exceptionally |
1155 |
|
*/ |
1156 |
< |
public void testHandle_sourceFailedActionFailed() { |
1156 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1157 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1158 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1159 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1160 |
|
{ |
1161 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1162 |
|
final AtomicInteger a = new AtomicInteger(0); |
1163 |
< |
final CFException ex1 = new CFException(); |
1164 |
< |
final CFException ex2 = new CFException(); |
1135 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
1163 |
> |
final CFException ex = new CFException(); |
1164 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1165 |
|
final CompletableFuture<Integer> g = m.handle |
1166 |
|
(f, |
1167 |
< |
(Integer x, Throwable t) -> { |
1167 |
> |
(Integer result, Throwable t) -> { |
1168 |
|
m.checkExecutionMode(); |
1169 |
< |
threadAssertNull(x); |
1170 |
< |
threadAssertSame(ex1, t); |
1169 |
> |
threadAssertSame(result, v1); |
1170 |
> |
threadAssertNull(t); |
1171 |
|
a.getAndIncrement(); |
1172 |
< |
throw ex2; |
1172 |
> |
throw ex; |
1173 |
|
}); |
1174 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
1174 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1175 |
|
|
1176 |
< |
checkCompletedWithWrappedException(g, ex2); |
1177 |
< |
checkCompletedExceptionally(f, ex1); |
1176 |
> |
checkCompletedWithWrappedException(g, ex); |
1177 |
> |
checkCompletedNormally(f, v1); |
1178 |
|
assertEquals(1, a.get()); |
1179 |
|
}} |
1180 |
|
|
1181 |
< |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1182 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
1181 |
> |
/** |
1182 |
> |
* If a "handle action" throws an exception when triggered by |
1183 |
> |
* a source completion that also throws an exception, the action |
1184 |
> |
* exception takes precedence (unlike whenComplete) |
1185 |
> |
*/ |
1186 |
> |
public void testHandle_sourceFailedActionFailed() { |
1187 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1188 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
1188 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1189 |
|
{ |
1157 |
– |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1190 |
|
final AtomicInteger a = new AtomicInteger(0); |
1191 |
< |
final CFException ex = new CFException(); |
1192 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1191 |
> |
final CFException ex1 = new CFException(); |
1192 |
> |
final CFException ex2 = new CFException(); |
1193 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1194 |
> |
|
1195 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1196 |
|
final CompletableFuture<Integer> g = m.handle |
1197 |
|
(f, |
1198 |
< |
(Integer x, Throwable t) -> { |
1198 |
> |
(Integer result, Throwable t) -> { |
1199 |
|
m.checkExecutionMode(); |
1200 |
< |
threadAssertSame(x, v1); |
1201 |
< |
threadAssertNull(t); |
1200 |
> |
threadAssertNull(result); |
1201 |
> |
threadAssertSame(ex1, t); |
1202 |
|
a.getAndIncrement(); |
1203 |
< |
throw ex; |
1203 |
> |
throw ex2; |
1204 |
|
}); |
1205 |
< |
if (createIncomplete) assertTrue(f.complete(v1)); |
1205 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1206 |
|
|
1207 |
< |
checkCompletedWithWrappedException(g, ex); |
1208 |
< |
checkCompletedNormally(f, v1); |
1207 |
> |
checkCompletedWithWrappedException(g, ex2); |
1208 |
> |
checkCompletedExceptionally(f, ex1); |
1209 |
|
assertEquals(1, a.get()); |
1210 |
|
}} |
1211 |
|
|
1238 |
|
{ |
1239 |
|
final FailingRunnable r = new FailingRunnable(m); |
1240 |
|
final CompletableFuture<Void> f = m.runAsync(r); |
1241 |
< |
checkCompletedWithWrappedCFException(f); |
1241 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1242 |
|
r.assertInvoked(); |
1243 |
|
}} |
1244 |
|
|
1245 |
+ |
public void testRunAsync_rejectingExecutor() { |
1246 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1247 |
+ |
try { |
1248 |
+ |
CompletableFuture.runAsync(() -> {}, e); |
1249 |
+ |
shouldThrow(); |
1250 |
+ |
} catch (Throwable t) { |
1251 |
+ |
assertSame(e.ex, t); |
1252 |
+ |
} |
1253 |
+ |
|
1254 |
+ |
assertEquals(1, e.count.get()); |
1255 |
+ |
} |
1256 |
+ |
|
1257 |
|
/** |
1258 |
|
* supplyAsync completes with result of supplier |
1259 |
|
*/ |
1284 |
|
{ |
1285 |
|
FailingSupplier r = new FailingSupplier(m); |
1286 |
|
CompletableFuture<Integer> f = m.supplyAsync(r); |
1287 |
< |
checkCompletedWithWrappedCFException(f); |
1287 |
> |
checkCompletedWithWrappedException(f, r.ex); |
1288 |
|
r.assertInvoked(); |
1289 |
|
}} |
1290 |
|
|
1291 |
+ |
public void testSupplyAsync_rejectingExecutor() { |
1292 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1293 |
+ |
try { |
1294 |
+ |
CompletableFuture.supplyAsync(() -> null, e); |
1295 |
+ |
shouldThrow(); |
1296 |
+ |
} catch (Throwable t) { |
1297 |
+ |
assertSame(e.ex, t); |
1298 |
+ |
} |
1299 |
+ |
|
1300 |
+ |
assertEquals(1, e.count.get()); |
1301 |
+ |
} |
1302 |
+ |
|
1303 |
|
// seq completion methods |
1304 |
|
|
1305 |
|
/** |
1418 |
|
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1419 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1420 |
|
|
1421 |
< |
checkCompletedWithWrappedCFException(h0); |
1422 |
< |
checkCompletedWithWrappedCFException(h1); |
1423 |
< |
checkCompletedWithWrappedCFException(h2); |
1424 |
< |
checkCompletedWithWrappedCFException(h3); |
1425 |
< |
checkCompletedWithWrappedCFException(h4); |
1426 |
< |
checkCompletedWithWrappedCFException(h5); |
1421 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1422 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1423 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1424 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1425 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
1426 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
1427 |
|
checkCompletedNormally(f, v1); |
1428 |
|
}} |
1429 |
|
|
1522 |
|
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1523 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1524 |
|
|
1525 |
< |
checkCompletedWithWrappedCFException(h0); |
1526 |
< |
checkCompletedWithWrappedCFException(h1); |
1527 |
< |
checkCompletedWithWrappedCFException(h2); |
1528 |
< |
checkCompletedWithWrappedCFException(h3); |
1525 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1526 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1527 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1528 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1529 |
|
checkCompletedNormally(f, v1); |
1530 |
|
}} |
1531 |
|
|
1624 |
|
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1625 |
|
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1626 |
|
|
1627 |
< |
checkCompletedWithWrappedCFException(h0); |
1628 |
< |
checkCompletedWithWrappedCFException(h1); |
1629 |
< |
checkCompletedWithWrappedCFException(h2); |
1630 |
< |
checkCompletedWithWrappedCFException(h3); |
1627 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
1628 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
1629 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
1630 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
1631 |
|
checkCompletedNormally(f, v1); |
1632 |
|
}} |
1633 |
|
|
1789 |
|
assertTrue(snd.complete(w2)); |
1790 |
|
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1791 |
|
|
1792 |
< |
checkCompletedWithWrappedCFException(h1); |
1793 |
< |
checkCompletedWithWrappedCFException(h2); |
1794 |
< |
checkCompletedWithWrappedCFException(h3); |
1792 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1793 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1794 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1795 |
|
r1.assertInvoked(); |
1796 |
|
r2.assertInvoked(); |
1797 |
|
r3.assertInvoked(); |
1953 |
|
assertTrue(snd.complete(w2)); |
1954 |
|
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1955 |
|
|
1956 |
< |
checkCompletedWithWrappedCFException(h1); |
1957 |
< |
checkCompletedWithWrappedCFException(h2); |
1958 |
< |
checkCompletedWithWrappedCFException(h3); |
1956 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
1957 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
1958 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
1959 |
|
r1.assertInvoked(); |
1960 |
|
r2.assertInvoked(); |
1961 |
|
r3.assertInvoked(); |
2117 |
|
assertTrue(snd.complete(w2)); |
2118 |
|
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2119 |
|
|
2120 |
< |
checkCompletedWithWrappedCFException(h1); |
2121 |
< |
checkCompletedWithWrappedCFException(h2); |
2122 |
< |
checkCompletedWithWrappedCFException(h3); |
2120 |
> |
checkCompletedWithWrappedException(h1, r1.ex); |
2121 |
> |
checkCompletedWithWrappedException(h2, r2.ex); |
2122 |
> |
checkCompletedWithWrappedException(h3, r3.ex); |
2123 |
|
r1.assertInvoked(); |
2124 |
|
r2.assertInvoked(); |
2125 |
|
r3.assertInvoked(); |
2409 |
|
f.complete(v1); |
2410 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2411 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2412 |
< |
checkCompletedWithWrappedCFException(h0); |
2413 |
< |
checkCompletedWithWrappedCFException(h1); |
2414 |
< |
checkCompletedWithWrappedCFException(h2); |
2415 |
< |
checkCompletedWithWrappedCFException(h3); |
2412 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2413 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2414 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2415 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2416 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2417 |
|
|
2418 |
|
g.complete(v2); |
2421 |
|
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2422 |
|
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2423 |
|
|
2424 |
< |
checkCompletedWithWrappedCFException(h4); |
2424 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2425 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2426 |
|
Objects.equals(v2, rs[4].value)); |
2427 |
< |
checkCompletedWithWrappedCFException(h5); |
2427 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2428 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2429 |
|
Objects.equals(v2, rs[5].value)); |
2430 |
|
|
2668 |
|
f.complete(v1); |
2669 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2670 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2671 |
< |
checkCompletedWithWrappedCFException(h0); |
2672 |
< |
checkCompletedWithWrappedCFException(h1); |
2673 |
< |
checkCompletedWithWrappedCFException(h2); |
2674 |
< |
checkCompletedWithWrappedCFException(h3); |
2671 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2672 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2673 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2674 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2675 |
|
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2676 |
|
|
2677 |
|
g.complete(v2); |
2680 |
|
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2681 |
|
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2682 |
|
|
2683 |
< |
checkCompletedWithWrappedCFException(h4); |
2683 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2684 |
|
assertTrue(Objects.equals(v1, rs[4].value) || |
2685 |
|
Objects.equals(v2, rs[4].value)); |
2686 |
< |
checkCompletedWithWrappedCFException(h5); |
2686 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2687 |
|
assertTrue(Objects.equals(v1, rs[5].value) || |
2688 |
|
Objects.equals(v2, rs[5].value)); |
2689 |
|
|
2699 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2700 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2701 |
|
for (Integer v2 : new Integer[] { 2, null }) |
2702 |
+ |
for (boolean pushNop : new boolean[] { true, false }) |
2703 |
|
{ |
2704 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2705 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2712 |
|
checkIncomplete(h1); |
2713 |
|
rs[0].assertNotInvoked(); |
2714 |
|
rs[1].assertNotInvoked(); |
2715 |
+ |
if (pushNop) { // ad hoc test of intra-completion interference |
2716 |
+ |
m.thenRun(f, () -> {}); |
2717 |
+ |
m.thenRun(g, () -> {}); |
2718 |
+ |
} |
2719 |
|
f.complete(v1); |
2720 |
|
checkCompletedNormally(h0, null); |
2721 |
|
checkCompletedNormally(h1, null); |
2928 |
|
assertTrue(f.complete(v1)); |
2929 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2930 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2931 |
< |
checkCompletedWithWrappedCFException(h0); |
2932 |
< |
checkCompletedWithWrappedCFException(h1); |
2933 |
< |
checkCompletedWithWrappedCFException(h2); |
2934 |
< |
checkCompletedWithWrappedCFException(h3); |
2931 |
> |
checkCompletedWithWrappedException(h0, rs[0].ex); |
2932 |
> |
checkCompletedWithWrappedException(h1, rs[1].ex); |
2933 |
> |
checkCompletedWithWrappedException(h2, rs[2].ex); |
2934 |
> |
checkCompletedWithWrappedException(h3, rs[3].ex); |
2935 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2936 |
|
assertTrue(g.complete(v2)); |
2937 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2938 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2939 |
< |
checkCompletedWithWrappedCFException(h4); |
2940 |
< |
checkCompletedWithWrappedCFException(h5); |
2939 |
> |
checkCompletedWithWrappedException(h4, rs[4].ex); |
2940 |
> |
checkCompletedWithWrappedException(h5, rs[5].ex); |
2941 |
|
|
2942 |
|
checkCompletedNormally(f, v1); |
2943 |
|
checkCompletedNormally(g, v2); |
2998 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2999 |
|
if (createIncomplete) assertTrue(f.complete(v1)); |
3000 |
|
|
3001 |
< |
checkCompletedWithWrappedCFException(g); |
3001 |
> |
checkCompletedWithWrappedException(g, r.ex); |
3002 |
|
checkCompletedNormally(f, v1); |
3003 |
|
}} |
3004 |
|
|
3107 |
|
} |
3108 |
|
} |
3109 |
|
|
3110 |
< |
public void testAllOf_backwards() throws Exception { |
3110 |
> |
public void testAllOf_normal_backwards() throws Exception { |
3111 |
|
for (int k = 1; k < 10; k++) { |
3112 |
|
CompletableFuture<Integer>[] fs |
3113 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3340 |
|
() -> f.obtrudeException(null), |
3341 |
|
|
3342 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3343 |
< |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3343 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
3344 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3345 |
|
|
3346 |
|
() -> f.orTimeout(1L, null), |
3355 |
|
} |
3356 |
|
|
3357 |
|
/** |
3358 |
+ |
* Test submissions to an executor that rejects all tasks. |
3359 |
+ |
*/ |
3360 |
+ |
public void testRejectingExecutor() { |
3361 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3362 |
+ |
{ |
3363 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3364 |
+ |
|
3365 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3366 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3367 |
+ |
|
3368 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3369 |
+ |
|
3370 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3371 |
+ |
srcs.add(complete); |
3372 |
+ |
srcs.add(incomplete); |
3373 |
+ |
|
3374 |
+ |
for (CompletableFuture<Integer> src : srcs) { |
3375 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3376 |
+ |
fs.add(src.thenRunAsync(() -> {}, e)); |
3377 |
+ |
fs.add(src.thenAcceptAsync((z) -> {}, e)); |
3378 |
+ |
fs.add(src.thenApplyAsync((z) -> z, e)); |
3379 |
+ |
|
3380 |
+ |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
3381 |
+ |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
3382 |
+ |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
3383 |
+ |
|
3384 |
+ |
fs.add(src.applyToEitherAsync(src, (z) -> z, e)); |
3385 |
+ |
fs.add(src.acceptEitherAsync(src, (z) -> {}, e)); |
3386 |
+ |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
3387 |
+ |
|
3388 |
+ |
fs.add(src.thenComposeAsync((z) -> null, e)); |
3389 |
+ |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
3390 |
+ |
fs.add(src.handleAsync((z, t) -> null, e)); |
3391 |
+ |
|
3392 |
+ |
for (CompletableFuture<?> future : fs) { |
3393 |
+ |
if (src.isDone()) |
3394 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3395 |
+ |
else |
3396 |
+ |
checkIncomplete(future); |
3397 |
+ |
} |
3398 |
+ |
futures.addAll(fs); |
3399 |
+ |
} |
3400 |
+ |
|
3401 |
+ |
{ |
3402 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3403 |
+ |
|
3404 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3405 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3406 |
+ |
|
3407 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3408 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3409 |
+ |
|
3410 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3411 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3412 |
+ |
|
3413 |
+ |
for (CompletableFuture<?> future : fs) |
3414 |
+ |
checkIncomplete(future); |
3415 |
+ |
futures.addAll(fs); |
3416 |
+ |
} |
3417 |
+ |
|
3418 |
+ |
{ |
3419 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3420 |
+ |
|
3421 |
+ |
fs.add(complete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3422 |
+ |
fs.add(incomplete.applyToEitherAsync(complete, (z) -> z, e)); |
3423 |
+ |
|
3424 |
+ |
fs.add(complete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3425 |
+ |
fs.add(incomplete.acceptEitherAsync(complete, (z) -> {}, e)); |
3426 |
+ |
|
3427 |
+ |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3428 |
+ |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3429 |
+ |
|
3430 |
+ |
for (CompletableFuture<?> future : fs) |
3431 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3432 |
+ |
futures.addAll(fs); |
3433 |
+ |
} |
3434 |
+ |
|
3435 |
+ |
incomplete.complete(v); |
3436 |
+ |
|
3437 |
+ |
for (CompletableFuture<?> future : futures) |
3438 |
+ |
checkCompletedWithWrappedException(future, e.ex); |
3439 |
+ |
|
3440 |
+ |
assertEquals(futures.size(), e.count.get()); |
3441 |
+ |
}} |
3442 |
+ |
|
3443 |
+ |
/** |
3444 |
+ |
* Test submissions to an executor that rejects all tasks, but |
3445 |
+ |
* should never be invoked because the dependent future is |
3446 |
+ |
* explicitly completed. |
3447 |
+ |
*/ |
3448 |
+ |
public void testRejectingExecutorNeverInvoked() { |
3449 |
+ |
for (Integer v : new Integer[] { 1, null }) |
3450 |
+ |
{ |
3451 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3452 |
+ |
|
3453 |
+ |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3454 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3455 |
+ |
|
3456 |
+ |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
3457 |
+ |
|
3458 |
+ |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
3459 |
+ |
srcs.add(complete); |
3460 |
+ |
srcs.add(incomplete); |
3461 |
+ |
|
3462 |
+ |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
3463 |
+ |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
3464 |
+ |
fs.add(incomplete.thenAcceptAsync((z) -> {}, e)); |
3465 |
+ |
fs.add(incomplete.thenApplyAsync((z) -> z, e)); |
3466 |
+ |
|
3467 |
+ |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3468 |
+ |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3469 |
+ |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
3470 |
+ |
|
3471 |
+ |
fs.add(incomplete.applyToEitherAsync(incomplete, (z) -> z, e)); |
3472 |
+ |
fs.add(incomplete.acceptEitherAsync(incomplete, (z) -> {}, e)); |
3473 |
+ |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
3474 |
+ |
|
3475 |
+ |
fs.add(incomplete.thenComposeAsync((z) -> null, e)); |
3476 |
+ |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
3477 |
+ |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
3478 |
+ |
|
3479 |
+ |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
3480 |
+ |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
3481 |
+ |
|
3482 |
+ |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
3483 |
+ |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
3484 |
+ |
|
3485 |
+ |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
3486 |
+ |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
3487 |
+ |
|
3488 |
+ |
for (CompletableFuture<?> future : fs) |
3489 |
+ |
checkIncomplete(future); |
3490 |
+ |
|
3491 |
+ |
for (CompletableFuture<?> future : fs) |
3492 |
+ |
future.complete(null); |
3493 |
+ |
|
3494 |
+ |
incomplete.complete(v); |
3495 |
+ |
|
3496 |
+ |
for (CompletableFuture<?> future : fs) |
3497 |
+ |
checkCompletedNormally(future, null); |
3498 |
+ |
|
3499 |
+ |
assertEquals(0, e.count.get()); |
3500 |
+ |
}} |
3501 |
+ |
|
3502 |
+ |
/** |
3503 |
|
* toCompletableFuture returns this CompletableFuture. |
3504 |
|
*/ |
3505 |
|
public void testToCompletableFuture() { |
3715 |
|
long timeoutMillis = timeoutMillis(); |
3716 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3717 |
|
long startTime = System.nanoTime(); |
3718 |
< |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3718 |
> |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
3719 |
|
checkCompletedWithTimeoutException(f); |
3720 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3721 |
|
} |
3730 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3731 |
|
long startTime = System.nanoTime(); |
3732 |
|
f.complete(v1); |
3733 |
< |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3734 |
< |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3733 |
> |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3734 |
> |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
3735 |
|
g.complete(v1); |
3736 |
|
checkCompletedNormally(f, v1); |
3737 |
|
checkCompletedNormally(g, v1); |
3746 |
|
() -> testCompleteOnTimeout_timesOut(null)); |
3747 |
|
} |
3748 |
|
|
3749 |
+ |
/** |
3750 |
+ |
* completeOnTimeout completes with given value if not complete |
3751 |
+ |
*/ |
3752 |
|
public void testCompleteOnTimeout_timesOut(Integer v) { |
3753 |
|
long timeoutMillis = timeoutMillis(); |
3754 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3755 |
|
long startTime = System.nanoTime(); |
3756 |
< |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3756 |
> |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
3757 |
|
assertSame(v, f.join()); |
3758 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3759 |
|
f.complete(99); // should have no effect |
3770 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3771 |
|
long startTime = System.nanoTime(); |
3772 |
|
f.complete(v1); |
3773 |
< |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3774 |
< |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3773 |
> |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3774 |
> |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
3775 |
|
g.complete(v1); |
3776 |
|
checkCompletedNormally(f, v1); |
3777 |
|
checkCompletedNormally(g, v1); |
3822 |
|
//--- tests of implementation details; not part of official tck --- |
3823 |
|
|
3824 |
|
Object resultOf(CompletableFuture<?> f) { |
3825 |
+ |
SecurityManager sm = System.getSecurityManager(); |
3826 |
+ |
if (sm != null) { |
3827 |
+ |
try { |
3828 |
+ |
System.setSecurityManager(null); |
3829 |
+ |
} catch (SecurityException giveUp) { |
3830 |
+ |
return "Reflection not available"; |
3831 |
+ |
} |
3832 |
+ |
} |
3833 |
+ |
|
3834 |
|
try { |
3835 |
|
java.lang.reflect.Field resultField |
3836 |
|
= CompletableFuture.class.getDeclaredField("result"); |
3837 |
|
resultField.setAccessible(true); |
3838 |
|
return resultField.get(f); |
3839 |
< |
} catch (Throwable t) { throw new AssertionError(t); } |
3839 |
> |
} catch (Throwable t) { |
3840 |
> |
throw new AssertionError(t); |
3841 |
> |
} finally { |
3842 |
> |
if (sm != null) System.setSecurityManager(sm); |
3843 |
> |
} |
3844 |
|
} |
3845 |
|
|
3846 |
|
public void testExceptionPropagationReusesResultObject() { |
3851 |
|
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3852 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3853 |
|
|
3854 |
+ |
final Runnable noopRunnable = new Noop(m); |
3855 |
+ |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
3856 |
+ |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
3857 |
+ |
|
3858 |
|
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3859 |
|
= new ArrayList<>(); |
3860 |
|
|
3861 |
< |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3862 |
< |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3863 |
< |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3864 |
< |
|
3865 |
< |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3866 |
< |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3867 |
< |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3861 |
> |
funs.add((y) -> m.thenRun(y, noopRunnable)); |
3862 |
> |
funs.add((y) -> m.thenAccept(y, noopConsumer)); |
3863 |
> |
funs.add((y) -> m.thenApply(y, incFunction)); |
3864 |
> |
|
3865 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, noopRunnable)); |
3866 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, noopConsumer)); |
3867 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, incFunction)); |
3868 |
|
|
3869 |
< |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3869 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, noopRunnable)); |
3870 |
> |
funs.add((y) -> m.runAfterBoth(v42, y, noopRunnable)); |
3871 |
|
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3872 |
+ |
funs.add((y) -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
3873 |
|
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3874 |
+ |
funs.add((y) -> m.thenCombine(v42, y, new SubtractFunction(m))); |
3875 |
|
|
3876 |
< |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3876 |
> |
funs.add((y) -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
3877 |
|
|
3878 |
|
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3879 |
|
|
3880 |
< |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3881 |
< |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3880 |
> |
funs.add((y) -> CompletableFuture.allOf(y)); |
3881 |
> |
funs.add((y) -> CompletableFuture.allOf(y, v42)); |
3882 |
> |
funs.add((y) -> CompletableFuture.allOf(v42, y)); |
3883 |
> |
funs.add((y) -> CompletableFuture.anyOf(y)); |
3884 |
> |
funs.add((y) -> CompletableFuture.anyOf(y, incomplete)); |
3885 |
> |
funs.add((y) -> CompletableFuture.anyOf(incomplete, y)); |
3886 |
|
|
3887 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3888 |
|
fun : funs) { |
3889 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3890 |
|
f.completeExceptionally(ex); |
3891 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3891 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3892 |
|
checkCompletedWithWrappedException(src, ex); |
3893 |
|
CompletableFuture<?> dep = fun.apply(src); |
3894 |
|
checkCompletedWithWrappedException(dep, ex); |
3898 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3899 |
|
fun : funs) { |
3900 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3901 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3901 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3902 |
|
CompletableFuture<?> dep = fun.apply(src); |
3903 |
|
f.completeExceptionally(ex); |
3904 |
|
checkCompletedWithWrappedException(src, ex); |
3912 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3913 |
|
f.cancel(mayInterruptIfRunning); |
3914 |
|
checkCancelled(f); |
3915 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3915 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3916 |
|
checkCompletedWithWrappedCancellationException(src); |
3917 |
|
CompletableFuture<?> dep = fun.apply(src); |
3918 |
|
checkCompletedWithWrappedCancellationException(dep); |
3923 |
|
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3924 |
|
fun : funs) { |
3925 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3926 |
< |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3926 |
> |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
3927 |
|
CompletableFuture<?> dep = fun.apply(src); |
3928 |
|
f.cancel(mayInterruptIfRunning); |
3929 |
|
checkCancelled(f); |
3934 |
|
}} |
3935 |
|
|
3936 |
|
/** |
3937 |
< |
* Minimal completion stages throw UOE for all non-CompletionStage methods |
3937 |
> |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
3938 |
|
*/ |
3939 |
|
public void testMinimalCompletionStage_minimality() { |
3940 |
|
if (!testImplementationDetails) return; |
3963 |
|
.filter((method) -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
3964 |
|
.collect(Collectors.toList()); |
3965 |
|
|
3966 |
< |
CompletionStage<Integer> minimalStage = |
3966 |
> |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
3967 |
> |
CompletionStage<Integer> min = |
3968 |
|
new CompletableFuture<Integer>().minimalCompletionStage(); |
3969 |
+ |
stages.add(min); |
3970 |
+ |
stages.add(min.thenApply(x -> x)); |
3971 |
+ |
stages.add(CompletableFuture.completedStage(1)); |
3972 |
+ |
stages.add(CompletableFuture.failedStage(new CFException())); |
3973 |
|
|
3974 |
|
List<Method> bugs = new ArrayList<>(); |
3975 |
|
for (Method method : allMethods) { |
3985 |
|
else if (parameterTypes[i] == long.class) |
3986 |
|
args[i] = 0L; |
3987 |
|
} |
3988 |
< |
try { |
3989 |
< |
method.invoke(minimalStage, args); |
3990 |
< |
bugs.add(method); |
3750 |
< |
} |
3751 |
< |
catch (java.lang.reflect.InvocationTargetException expected) { |
3752 |
< |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3988 |
> |
for (CompletionStage<Integer> stage : stages) { |
3989 |
> |
try { |
3990 |
> |
method.invoke(stage, args); |
3991 |
|
bugs.add(method); |
3754 |
– |
// expected.getCause().printStackTrace(); |
3992 |
|
} |
3993 |
+ |
catch (java.lang.reflect.InvocationTargetException expected) { |
3994 |
+ |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
3995 |
+ |
bugs.add(method); |
3996 |
+ |
// expected.getCause().printStackTrace(); |
3997 |
+ |
} |
3998 |
+ |
} |
3999 |
+ |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4000 |
|
} |
3757 |
– |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
4001 |
|
} |
4002 |
|
if (!bugs.isEmpty()) |
4003 |
< |
throw new Error("Methods did not throw UOE: " + bugs.toString()); |
4003 |
> |
throw new Error("Methods did not throw UOE: " + bugs); |
4004 |
> |
} |
4005 |
> |
|
4006 |
> |
/** |
4007 |
> |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
4008 |
> |
*/ |
4009 |
> |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
4010 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4011 |
> |
{ |
4012 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4013 |
> |
CompletionStage minimal = f.minimalCompletionStage(); |
4014 |
> |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
4015 |
> |
g.complete(v1); |
4016 |
> |
checkCompletedNormally(g, v1); |
4017 |
> |
checkIncomplete(f); |
4018 |
> |
checkIncomplete(minimal.toCompletableFuture()); |
4019 |
> |
}} |
4020 |
> |
|
4021 |
> |
/** |
4022 |
> |
* minimalStage.toCompletableFuture().join() awaits completion |
4023 |
> |
*/ |
4024 |
> |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
4025 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4026 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4027 |
> |
{ |
4028 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4029 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4030 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4031 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
4032 |
> |
assertEquals(v1, minimal.toCompletableFuture().join()); |
4033 |
> |
assertEquals(v1, minimal.toCompletableFuture().get()); |
4034 |
> |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
4035 |
> |
}} |
4036 |
> |
|
4037 |
> |
/** Demo utility method for external reliable toCompletableFuture */ |
4038 |
> |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
4039 |
> |
CompletableFuture<T> f = new CompletableFuture<>(); |
4040 |
> |
stage.handle((T t, Throwable ex) -> { |
4041 |
> |
if (ex != null) f.completeExceptionally(ex); |
4042 |
> |
else f.complete(t); |
4043 |
> |
return null; |
4044 |
> |
}); |
4045 |
> |
return f; |
4046 |
> |
} |
4047 |
> |
|
4048 |
> |
/** Demo utility method to join a CompletionStage */ |
4049 |
> |
static <T> T join(CompletionStage<T> stage) { |
4050 |
> |
return toCompletableFuture(stage).join(); |
4051 |
> |
} |
4052 |
> |
|
4053 |
> |
/** |
4054 |
> |
* Joining a minimal stage "by hand" works |
4055 |
> |
*/ |
4056 |
> |
public void testMinimalCompletionStage_join_by_hand() { |
4057 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
4058 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
4059 |
> |
{ |
4060 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
4061 |
> |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
4062 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4063 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
4064 |
> |
minimal.thenAccept((x) -> g.complete(x)); |
4065 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
4066 |
> |
g.join(); |
4067 |
> |
checkCompletedNormally(g, v1); |
4068 |
> |
checkCompletedNormally(f, v1); |
4069 |
> |
assertEquals(v1, join(minimal)); |
4070 |
> |
}} |
4071 |
> |
|
4072 |
> |
static class Monad { |
4073 |
> |
static class ZeroException extends RuntimeException { |
4074 |
> |
public ZeroException() { super("monadic zero"); } |
4075 |
> |
} |
4076 |
> |
// "return", "unit" |
4077 |
> |
static <T> CompletableFuture<T> unit(T value) { |
4078 |
> |
return completedFuture(value); |
4079 |
> |
} |
4080 |
> |
// monadic zero ? |
4081 |
> |
static <T> CompletableFuture<T> zero() { |
4082 |
> |
return failedFuture(new ZeroException()); |
4083 |
> |
} |
4084 |
> |
// >=> |
4085 |
> |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
4086 |
> |
(Function<T, CompletableFuture<U>> f, |
4087 |
> |
Function<U, CompletableFuture<V>> g) { |
4088 |
> |
return (x) -> f.apply(x).thenCompose(g); |
4089 |
> |
} |
4090 |
> |
|
4091 |
> |
static void assertZero(CompletableFuture<?> f) { |
4092 |
> |
try { |
4093 |
> |
f.getNow(null); |
4094 |
> |
throw new AssertionFailedError("should throw"); |
4095 |
> |
} catch (CompletionException success) { |
4096 |
> |
assertTrue(success.getCause() instanceof ZeroException); |
4097 |
> |
} |
4098 |
> |
} |
4099 |
> |
|
4100 |
> |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
4101 |
> |
CompletableFuture<T> g) { |
4102 |
> |
T fval = null, gval = null; |
4103 |
> |
Throwable fex = null, gex = null; |
4104 |
> |
|
4105 |
> |
try { fval = f.get(); } |
4106 |
> |
catch (ExecutionException ex) { fex = ex.getCause(); } |
4107 |
> |
catch (Throwable ex) { fex = ex; } |
4108 |
> |
|
4109 |
> |
try { gval = g.get(); } |
4110 |
> |
catch (ExecutionException ex) { gex = ex.getCause(); } |
4111 |
> |
catch (Throwable ex) { gex = ex; } |
4112 |
> |
|
4113 |
> |
if (fex != null || gex != null) |
4114 |
> |
assertSame(fex.getClass(), gex.getClass()); |
4115 |
> |
else |
4116 |
> |
assertEquals(fval, gval); |
4117 |
> |
} |
4118 |
> |
|
4119 |
> |
static class PlusFuture<T> extends CompletableFuture<T> { |
4120 |
> |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
4121 |
> |
} |
4122 |
> |
|
4123 |
> |
/** Implements "monadic plus". */ |
4124 |
> |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
4125 |
> |
CompletableFuture<? extends T> g) { |
4126 |
> |
PlusFuture<T> plus = new PlusFuture<T>(); |
4127 |
> |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
4128 |
> |
try { |
4129 |
> |
if (ex == null) { |
4130 |
> |
if (plus.complete(result)) |
4131 |
> |
if (plus.firstFailure.get() != null) |
4132 |
> |
plus.firstFailure.set(null); |
4133 |
> |
} |
4134 |
> |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
4135 |
> |
if (plus.isDone()) |
4136 |
> |
plus.firstFailure.set(null); |
4137 |
> |
} |
4138 |
> |
else { |
4139 |
> |
// first failure has precedence |
4140 |
> |
Throwable first = plus.firstFailure.getAndSet(null); |
4141 |
> |
|
4142 |
> |
// may fail with "Self-suppression not permitted" |
4143 |
> |
try { first.addSuppressed(ex); } |
4144 |
> |
catch (Exception ignored) {} |
4145 |
> |
|
4146 |
> |
plus.completeExceptionally(first); |
4147 |
> |
} |
4148 |
> |
} catch (Throwable unexpected) { |
4149 |
> |
plus.completeExceptionally(unexpected); |
4150 |
> |
} |
4151 |
> |
}; |
4152 |
> |
f.whenComplete(action); |
4153 |
> |
g.whenComplete(action); |
4154 |
> |
return plus; |
4155 |
> |
} |
4156 |
> |
} |
4157 |
> |
|
4158 |
> |
/** |
4159 |
> |
* CompletableFuture is an additive monad - sort of. |
4160 |
> |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
4161 |
> |
*/ |
4162 |
> |
public void testAdditiveMonad() throws Throwable { |
4163 |
> |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
4164 |
> |
CompletableFuture<Long> zero = Monad.zero(); |
4165 |
> |
|
4166 |
> |
// Some mutually non-commutative functions |
4167 |
> |
Function<Long, CompletableFuture<Long>> triple |
4168 |
> |
= (x) -> Monad.unit(3 * x); |
4169 |
> |
Function<Long, CompletableFuture<Long>> inc |
4170 |
> |
= (x) -> Monad.unit(x + 1); |
4171 |
> |
|
4172 |
> |
// unit is a right identity: m >>= unit === m |
4173 |
> |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
4174 |
> |
inc.apply(5L)); |
4175 |
> |
// unit is a left identity: (unit x) >>= f === f x |
4176 |
> |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
4177 |
> |
inc.apply(5L)); |
4178 |
> |
|
4179 |
> |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
4180 |
> |
Monad.assertFutureEquals( |
4181 |
> |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
4182 |
> |
unit.apply(5L).thenCompose((x) -> inc.apply(x).thenCompose(triple))); |
4183 |
> |
|
4184 |
> |
// The case for CompletableFuture as an additive monad is weaker... |
4185 |
> |
|
4186 |
> |
// zero is a monadic zero |
4187 |
> |
Monad.assertZero(zero); |
4188 |
> |
|
4189 |
> |
// left zero: zero >>= f === zero |
4190 |
> |
Monad.assertZero(zero.thenCompose(inc)); |
4191 |
> |
// right zero: f >>= (\x -> zero) === zero |
4192 |
> |
Monad.assertZero(inc.apply(5L).thenCompose((x) -> zero)); |
4193 |
> |
|
4194 |
> |
// f plus zero === f |
4195 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
4196 |
> |
Monad.plus(Monad.unit(5L), zero)); |
4197 |
> |
// zero plus f === f |
4198 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
4199 |
> |
Monad.plus(zero, Monad.unit(5L))); |
4200 |
> |
// zero plus zero === zero |
4201 |
> |
Monad.assertZero(Monad.plus(zero, zero)); |
4202 |
> |
{ |
4203 |
> |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
4204 |
> |
Monad.unit(8L)); |
4205 |
> |
// non-determinism |
4206 |
> |
assertTrue(f.get() == 5L || f.get() == 8L); |
4207 |
> |
} |
4208 |
> |
|
4209 |
> |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
4210 |
> |
// f plus godot === f (doesn't wait for godot) |
4211 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
4212 |
> |
Monad.plus(Monad.unit(5L), godot)); |
4213 |
> |
// godot plus f === f (doesn't wait for godot) |
4214 |
> |
Monad.assertFutureEquals(Monad.unit(5L), |
4215 |
> |
Monad.plus(godot, Monad.unit(5L))); |
4216 |
> |
} |
4217 |
> |
|
4218 |
> |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4219 |
> |
public void testRecursiveChains() throws Throwable { |
4220 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
4221 |
> |
for (boolean addDeadEnds : new boolean[] { true, false }) |
4222 |
> |
{ |
4223 |
> |
final int val = 42; |
4224 |
> |
final int n = expensiveTests ? 1_000 : 2; |
4225 |
> |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
4226 |
> |
CompletableFuture<Integer> tail = head; |
4227 |
> |
for (int i = 0; i < n; i++) { |
4228 |
> |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
4229 |
> |
tail = m.thenApply(tail, v -> v + 1); |
4230 |
> |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
4231 |
> |
tail = m.applyToEither(tail, tail, v -> v + 1); |
4232 |
> |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
4233 |
> |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
4234 |
> |
} |
4235 |
> |
head.complete(val); |
4236 |
> |
assertEquals(val + 3 * n, (int) tail.join()); |
4237 |
> |
}} |
4238 |
> |
|
4239 |
> |
/** |
4240 |
> |
* A single CompletableFuture with many dependents. |
4241 |
> |
* A demo of scalability - runtime is O(n). |
4242 |
> |
*/ |
4243 |
> |
public void testManyDependents() throws Throwable { |
4244 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4245 |
> |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4246 |
> |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
4247 |
> |
final AtomicInteger count = new AtomicInteger(0); |
4248 |
> |
for (int i = 0; i < n; i++) { |
4249 |
> |
head.thenRun(() -> count.getAndIncrement()); |
4250 |
> |
head.thenAccept((x) -> count.getAndIncrement()); |
4251 |
> |
head.thenApply((x) -> count.getAndIncrement()); |
4252 |
> |
|
4253 |
> |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
4254 |
> |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
4255 |
> |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
4256 |
> |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
4257 |
> |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
4258 |
> |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
4259 |
> |
|
4260 |
> |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
4261 |
> |
head.acceptEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4262 |
> |
head.applyToEither(new CompletableFuture<Void>(), (x) -> count.getAndIncrement()); |
4263 |
> |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
4264 |
> |
new CompletableFuture<Void>().acceptEither(head, (x) -> count.getAndIncrement()); |
4265 |
> |
new CompletableFuture<Void>().applyToEither(head, (x) -> count.getAndIncrement()); |
4266 |
> |
} |
4267 |
> |
head.complete(null); |
4268 |
> |
assertEquals(5 * 3 * n, count.get()); |
4269 |
> |
} |
4270 |
> |
|
4271 |
> |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4272 |
> |
public void testCoCompletionGarbageRetention() throws Throwable { |
4273 |
> |
final int n = expensiveTests ? 1_000_000 : 10; |
4274 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4275 |
> |
CompletableFuture<Integer> f; |
4276 |
> |
for (int i = 0; i < n; i++) { |
4277 |
> |
f = new CompletableFuture<>(); |
4278 |
> |
f.runAfterEither(incomplete, () -> {}); |
4279 |
> |
f.complete(null); |
4280 |
> |
|
4281 |
> |
f = new CompletableFuture<>(); |
4282 |
> |
f.acceptEither(incomplete, (x) -> {}); |
4283 |
> |
f.complete(null); |
4284 |
> |
|
4285 |
> |
f = new CompletableFuture<>(); |
4286 |
> |
f.applyToEither(incomplete, (x) -> x); |
4287 |
> |
f.complete(null); |
4288 |
> |
|
4289 |
> |
f = new CompletableFuture<>(); |
4290 |
> |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4291 |
> |
f.complete(null); |
4292 |
> |
} |
4293 |
> |
|
4294 |
> |
for (int i = 0; i < n; i++) { |
4295 |
> |
f = new CompletableFuture<>(); |
4296 |
> |
incomplete.runAfterEither(f, () -> {}); |
4297 |
> |
f.complete(null); |
4298 |
> |
|
4299 |
> |
f = new CompletableFuture<>(); |
4300 |
> |
incomplete.acceptEither(f, (x) -> {}); |
4301 |
> |
f.complete(null); |
4302 |
> |
|
4303 |
> |
f = new CompletableFuture<>(); |
4304 |
> |
incomplete.applyToEither(f, (x) -> x); |
4305 |
> |
f.complete(null); |
4306 |
> |
|
4307 |
> |
f = new CompletableFuture<>(); |
4308 |
> |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4309 |
> |
f.complete(null); |
4310 |
> |
} |
4311 |
> |
} |
4312 |
> |
|
4313 |
> |
/** |
4314 |
> |
* Reproduction recipe for: |
4315 |
> |
* 8160402: Garbage retention with CompletableFuture.anyOf |
4316 |
> |
* cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A |
4317 |
> |
*/ |
4318 |
> |
public void testAnyOfGarbageRetention() throws Throwable { |
4319 |
> |
for (Integer v : new Integer[] { 1, null }) |
4320 |
> |
{ |
4321 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4322 |
> |
CompletableFuture<Integer>[] fs |
4323 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4324 |
> |
for (int i = 0; i < fs.length; i++) |
4325 |
> |
fs[i] = new CompletableFuture<>(); |
4326 |
> |
fs[fs.length - 1].complete(v); |
4327 |
> |
for (int i = 0; i < n; i++) |
4328 |
> |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4329 |
> |
}} |
4330 |
> |
|
4331 |
> |
/** |
4332 |
> |
* Checks for garbage retention with allOf. |
4333 |
> |
* |
4334 |
> |
* As of 2016-07, fails with OOME: |
4335 |
> |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
4336 |
> |
*/ |
4337 |
> |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4338 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4339 |
> |
CompletableFuture<Integer>[] fs |
4340 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4341 |
> |
for (int i = 0; i < fs.length; i++) |
4342 |
> |
fs[i] = new CompletableFuture<>(); |
4343 |
> |
for (int i = 0; i < n; i++) |
4344 |
> |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4345 |
> |
} |
4346 |
> |
|
4347 |
> |
/** |
4348 |
> |
* Checks for garbage retention when a dependent future is |
4349 |
> |
* cancelled and garbage-collected. |
4350 |
> |
* 8161600: Garbage retention when source CompletableFutures are never completed |
4351 |
> |
* |
4352 |
> |
* As of 2016-07, fails with OOME: |
4353 |
> |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
4354 |
> |
*/ |
4355 |
> |
public void testCancelledGarbageRetention() throws Throwable { |
4356 |
> |
final int n = expensiveTests ? 100_000 : 10; |
4357 |
> |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
4358 |
> |
for (int i = 0; i < n; i++) |
4359 |
> |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
4360 |
|
} |
4361 |
|
|
4362 |
|
// static <U> U join(CompletionStage<U> stage) { |