57 |
|
} |
58 |
|
|
59 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
60 |
< |
try { |
61 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
60 |
> |
checkTimedGet(f, value); |
61 |
> |
|
62 |
|
try { |
63 |
|
assertEquals(value, f.join()); |
64 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
75 |
|
} |
76 |
|
|
77 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
78 |
+ |
long startTime = System.nanoTime(); |
79 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
80 |
|
try { |
81 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
81 |
> |
f.get(timeoutMillis, MILLISECONDS); |
82 |
|
shouldThrow(); |
83 |
|
} catch (ExecutionException success) { |
84 |
|
assertTrue(success.getCause() instanceof CFException); |
85 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
86 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
87 |
+ |
|
88 |
|
try { |
89 |
|
f.join(); |
90 |
|
shouldThrow(); |
110 |
|
|
111 |
|
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
112 |
|
Throwable ex) { |
113 |
+ |
long startTime = System.nanoTime(); |
114 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
115 |
|
try { |
116 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
116 |
> |
f.get(timeoutMillis, MILLISECONDS); |
117 |
|
shouldThrow(); |
118 |
|
} catch (ExecutionException success) { |
119 |
|
assertSame(ex, success.getCause()); |
120 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
121 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
122 |
+ |
|
123 |
|
try { |
124 |
|
f.join(); |
125 |
|
shouldThrow(); |
165 |
|
} |
166 |
|
|
167 |
|
void checkCancelled(CompletableFuture<?> f) { |
168 |
+ |
long startTime = System.nanoTime(); |
169 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
170 |
|
try { |
171 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
171 |
> |
f.get(timeoutMillis, MILLISECONDS); |
172 |
|
shouldThrow(); |
173 |
|
} catch (CancellationException success) { |
174 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
175 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
176 |
+ |
|
177 |
|
try { |
178 |
|
f.join(); |
179 |
|
shouldThrow(); |
194 |
|
} |
195 |
|
|
196 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
197 |
+ |
long startTime = System.nanoTime(); |
198 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
199 |
|
try { |
200 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
200 |
> |
f.get(timeoutMillis, MILLISECONDS); |
201 |
|
shouldThrow(); |
202 |
|
} catch (ExecutionException success) { |
203 |
|
assertTrue(success.getCause() instanceof CancellationException); |
204 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
205 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
206 |
+ |
|
207 |
|
try { |
208 |
|
f.join(); |
209 |
|
shouldThrow(); |
242 |
|
* isCancelled, join, get, and getNow |
243 |
|
*/ |
244 |
|
public void testComplete() { |
245 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
246 |
+ |
{ |
247 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
248 |
|
checkIncomplete(f); |
249 |
< |
f.complete(one); |
250 |
< |
checkCompletedNormally(f, one); |
251 |
< |
} |
249 |
> |
assertTrue(f.complete(v1)); |
250 |
> |
assertFalse(f.complete(v1)); |
251 |
> |
checkCompletedNormally(f, v1); |
252 |
> |
}} |
253 |
|
|
254 |
|
/** |
255 |
|
* completeExceptionally completes exceptionally, as indicated by |
268 |
|
* methods isDone, isCancelled, join, get, and getNow |
269 |
|
*/ |
270 |
|
public void testCancel() { |
271 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
272 |
+ |
{ |
273 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
274 |
|
checkIncomplete(f); |
275 |
|
assertTrue(f.cancel(true)); |
276 |
+ |
assertTrue(f.cancel(true)); |
277 |
|
checkCancelled(f); |
278 |
< |
} |
278 |
> |
}} |
279 |
|
|
280 |
|
/** |
281 |
|
* obtrudeValue forces completion with given value |
283 |
|
public void testObtrudeValue() { |
284 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
285 |
|
checkIncomplete(f); |
286 |
< |
f.complete(one); |
286 |
> |
assertTrue(f.complete(one)); |
287 |
|
checkCompletedNormally(f, one); |
288 |
|
f.obtrudeValue(three); |
289 |
|
checkCompletedNormally(f, three); |
310 |
|
CompletableFuture<Integer> f; |
311 |
|
|
312 |
|
f = new CompletableFuture<>(); |
313 |
< |
f.complete(v1); |
313 |
> |
assertTrue(f.complete(v1)); |
314 |
|
for (int i = 0; i < 2; i++) { |
315 |
|
f.obtrudeException(ex = new CFException()); |
316 |
|
checkCompletedExceptionally(f, ex); |
330 |
|
checkCompletedExceptionally(f, ex); |
331 |
|
f.completeExceptionally(new CFException()); |
332 |
|
checkCompletedExceptionally(f, ex); |
333 |
< |
f.complete(v1); |
333 |
> |
assertFalse(f.complete(v1)); |
334 |
|
checkCompletedExceptionally(f, ex); |
335 |
|
}} |
336 |
|
|
338 |
|
* getNumberOfDependents returns number of dependent tasks |
339 |
|
*/ |
340 |
|
public void testGetNumberOfDependents() { |
341 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
342 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
343 |
+ |
{ |
344 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
345 |
|
assertEquals(0, f.getNumberOfDependents()); |
346 |
< |
CompletableFuture g = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
346 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
347 |
|
assertEquals(1, f.getNumberOfDependents()); |
348 |
|
assertEquals(0, g.getNumberOfDependents()); |
349 |
< |
CompletableFuture h = f.thenRun(new Noop(ExecutionMode.DEFAULT)); |
349 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
350 |
|
assertEquals(2, f.getNumberOfDependents()); |
351 |
< |
f.complete(1); |
351 |
> |
assertEquals(0, h.getNumberOfDependents()); |
352 |
> |
assertTrue(f.complete(v1)); |
353 |
|
checkCompletedNormally(g, null); |
354 |
+ |
checkCompletedNormally(h, null); |
355 |
|
assertEquals(0, f.getNumberOfDependents()); |
356 |
|
assertEquals(0, g.getNumberOfDependents()); |
357 |
< |
} |
357 |
> |
assertEquals(0, h.getNumberOfDependents()); |
358 |
> |
}} |
359 |
|
|
360 |
|
/** |
361 |
|
* toString indicates current completion state |
366 |
|
f = new CompletableFuture<String>(); |
367 |
|
assertTrue(f.toString().contains("[Not completed]")); |
368 |
|
|
369 |
< |
f.complete("foo"); |
369 |
> |
assertTrue(f.complete("foo")); |
370 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
371 |
|
|
372 |
|
f = new CompletableFuture<String>(); |
373 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
373 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
374 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
375 |
+ |
|
376 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
377 |
+ |
f = new CompletableFuture<String>(); |
378 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
379 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
380 |
+ |
} |
381 |
|
} |
382 |
|
|
383 |
|
/** |
554 |
|
invoked(); |
555 |
|
value = x; |
556 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
557 |
< |
f.complete(inc(x)); |
557 |
> |
assertTrue(f.complete(inc(x))); |
558 |
|
return f; |
559 |
|
} |
560 |
|
} |
584 |
|
} |
585 |
|
} |
586 |
|
|
587 |
+ |
static final boolean defaultExecutorIsCommonPool |
588 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
589 |
+ |
|
590 |
|
/** |
591 |
|
* Permits the testing of parallel code for the 3 different |
592 |
|
* execution modes without copy/pasting all the test methods. |
593 |
|
*/ |
594 |
|
enum ExecutionMode { |
595 |
< |
DEFAULT { |
595 |
> |
SYNC { |
596 |
|
public void checkExecutionMode() { |
597 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
598 |
|
assertNull(ForkJoinTask.getPool()); |
668 |
|
|
669 |
|
ASYNC { |
670 |
|
public void checkExecutionMode() { |
671 |
< |
assertSame(ForkJoinPool.commonPool(), |
672 |
< |
ForkJoinTask.getPool()); |
671 |
> |
assertEquals(defaultExecutorIsCommonPool, |
672 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
673 |
|
} |
674 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
675 |
|
return CompletableFuture.runAsync(a); |
865 |
|
{ |
866 |
|
final AtomicInteger a = new AtomicInteger(0); |
867 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
868 |
< |
if (!createIncomplete) f.complete(v1); |
868 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
869 |
|
final CompletableFuture<Integer> g = f.exceptionally |
870 |
|
((Throwable t) -> { |
871 |
|
// Should not be called |
872 |
|
a.getAndIncrement(); |
873 |
|
throw new AssertionError(); |
874 |
|
}); |
875 |
< |
if (createIncomplete) f.complete(v1); |
875 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
876 |
|
|
877 |
|
checkCompletedNormally(g, v1); |
878 |
|
checkCompletedNormally(f, v1); |
879 |
|
assertEquals(0, a.get()); |
880 |
|
}} |
881 |
|
|
846 |
– |
|
882 |
|
/** |
883 |
|
* exceptionally action completes with function value on source |
884 |
|
* exception |
893 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
894 |
|
final CompletableFuture<Integer> g = f.exceptionally |
895 |
|
((Throwable t) -> { |
896 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
896 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
897 |
|
threadAssertSame(t, ex); |
898 |
|
a.getAndIncrement(); |
899 |
|
return v1; |
915 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
916 |
|
final CompletableFuture<Integer> g = f.exceptionally |
917 |
|
((Throwable t) -> { |
918 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
918 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
919 |
|
threadAssertSame(t, ex1); |
920 |
|
a.getAndIncrement(); |
921 |
|
throw ex2; |
927 |
|
}} |
928 |
|
|
929 |
|
/** |
930 |
+ |
* whenComplete action executes on normal completion, propagating |
931 |
+ |
* source result. |
932 |
+ |
*/ |
933 |
+ |
public void testWhenComplete_normalCompletion1() { |
934 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
935 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
936 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
937 |
+ |
{ |
938 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
939 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
940 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
941 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
942 |
+ |
(f, |
943 |
+ |
(Integer x, Throwable t) -> { |
944 |
+ |
m.checkExecutionMode(); |
945 |
+ |
threadAssertSame(x, v1); |
946 |
+ |
threadAssertNull(t); |
947 |
+ |
a.getAndIncrement(); |
948 |
+ |
}); |
949 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
950 |
+ |
|
951 |
+ |
checkCompletedNormally(g, v1); |
952 |
+ |
checkCompletedNormally(f, v1); |
953 |
+ |
assertEquals(1, a.get()); |
954 |
+ |
}} |
955 |
+ |
|
956 |
+ |
/** |
957 |
+ |
* whenComplete action executes on exceptional completion, propagating |
958 |
+ |
* source result. |
959 |
+ |
*/ |
960 |
+ |
public void testWhenComplete_exceptionalCompletion() { |
961 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
962 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
963 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
964 |
+ |
{ |
965 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
966 |
+ |
final CFException ex = new CFException(); |
967 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
968 |
+ |
if (!createIncomplete) f.completeExceptionally(ex); |
969 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
970 |
+ |
(f, |
971 |
+ |
(Integer x, Throwable t) -> { |
972 |
+ |
m.checkExecutionMode(); |
973 |
+ |
threadAssertNull(x); |
974 |
+ |
threadAssertSame(t, ex); |
975 |
+ |
a.getAndIncrement(); |
976 |
+ |
}); |
977 |
+ |
if (createIncomplete) f.completeExceptionally(ex); |
978 |
+ |
|
979 |
+ |
checkCompletedWithWrappedException(g, ex); |
980 |
+ |
checkCompletedExceptionally(f, ex); |
981 |
+ |
assertEquals(1, a.get()); |
982 |
+ |
}} |
983 |
+ |
|
984 |
+ |
/** |
985 |
+ |
* whenComplete action executes on cancelled source, propagating |
986 |
+ |
* CancellationException. |
987 |
+ |
*/ |
988 |
+ |
public void testWhenComplete_sourceCancelled() { |
989 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
990 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
991 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
992 |
+ |
{ |
993 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
994 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
995 |
+ |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
996 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
997 |
+ |
(f, |
998 |
+ |
(Integer x, Throwable t) -> { |
999 |
+ |
m.checkExecutionMode(); |
1000 |
+ |
threadAssertNull(x); |
1001 |
+ |
threadAssertTrue(t instanceof CancellationException); |
1002 |
+ |
a.getAndIncrement(); |
1003 |
+ |
}); |
1004 |
+ |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1005 |
+ |
|
1006 |
+ |
checkCompletedWithWrappedCancellationException(g); |
1007 |
+ |
checkCancelled(f); |
1008 |
+ |
assertEquals(1, a.get()); |
1009 |
+ |
}} |
1010 |
+ |
|
1011 |
+ |
/** |
1012 |
+ |
* If a whenComplete action throws an exception when triggered by |
1013 |
+ |
* a normal completion, it completes exceptionally |
1014 |
+ |
*/ |
1015 |
+ |
public void testWhenComplete_actionFailed() { |
1016 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1017 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1018 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1019 |
+ |
{ |
1020 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
1021 |
+ |
final CFException ex = new CFException(); |
1022 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1023 |
+ |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1024 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1025 |
+ |
(f, |
1026 |
+ |
(Integer x, Throwable t) -> { |
1027 |
+ |
m.checkExecutionMode(); |
1028 |
+ |
threadAssertSame(x, v1); |
1029 |
+ |
threadAssertNull(t); |
1030 |
+ |
a.getAndIncrement(); |
1031 |
+ |
throw ex; |
1032 |
+ |
}); |
1033 |
+ |
if (createIncomplete) assertTrue(f.complete(v1)); |
1034 |
+ |
|
1035 |
+ |
checkCompletedWithWrappedException(g, ex); |
1036 |
+ |
checkCompletedNormally(f, v1); |
1037 |
+ |
assertEquals(1, a.get()); |
1038 |
+ |
}} |
1039 |
+ |
|
1040 |
+ |
/** |
1041 |
+ |
* If a whenComplete action throws an exception when triggered by |
1042 |
+ |
* a source completion that also throws an exception, the source |
1043 |
+ |
* exception takes precedence. |
1044 |
+ |
*/ |
1045 |
+ |
public void testWhenComplete_actionFailedSourceFailed() { |
1046 |
+ |
for (boolean createIncomplete : new boolean[] { true, false }) |
1047 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
1048 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
1049 |
+ |
{ |
1050 |
+ |
final AtomicInteger a = new AtomicInteger(0); |
1051 |
+ |
final CFException ex1 = new CFException(); |
1052 |
+ |
final CFException ex2 = new CFException(); |
1053 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1054 |
+ |
|
1055 |
+ |
if (!createIncomplete) f.completeExceptionally(ex1); |
1056 |
+ |
final CompletableFuture<Integer> g = m.whenComplete |
1057 |
+ |
(f, |
1058 |
+ |
(Integer x, Throwable t) -> { |
1059 |
+ |
m.checkExecutionMode(); |
1060 |
+ |
threadAssertSame(t, ex1); |
1061 |
+ |
threadAssertNull(x); |
1062 |
+ |
a.getAndIncrement(); |
1063 |
+ |
throw ex2; |
1064 |
+ |
}); |
1065 |
+ |
if (createIncomplete) f.completeExceptionally(ex1); |
1066 |
+ |
|
1067 |
+ |
checkCompletedWithWrappedException(g, ex1); |
1068 |
+ |
checkCompletedExceptionally(f, ex1); |
1069 |
+ |
assertEquals(1, a.get()); |
1070 |
+ |
}} |
1071 |
+ |
|
1072 |
+ |
/** |
1073 |
|
* handle action completes normally with function value on normal |
1074 |
|
* completion of source |
1075 |
|
*/ |
1080 |
|
{ |
1081 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1082 |
|
final AtomicInteger a = new AtomicInteger(0); |
1083 |
< |
if (!createIncomplete) f.complete(v1); |
1083 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1084 |
|
final CompletableFuture<Integer> g = m.handle |
1085 |
|
(f, |
1086 |
|
(Integer x, Throwable t) -> { |
1090 |
|
a.getAndIncrement(); |
1091 |
|
return inc(v1); |
1092 |
|
}); |
1093 |
< |
if (createIncomplete) f.complete(v1); |
1093 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1094 |
|
|
1095 |
|
checkCompletedNormally(g, inc(v1)); |
1096 |
|
checkCompletedNormally(f, v1); |
1191 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1192 |
|
final AtomicInteger a = new AtomicInteger(0); |
1193 |
|
final CFException ex = new CFException(); |
1194 |
< |
if (!createIncomplete) f.complete(v1); |
1194 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1195 |
|
final CompletableFuture<Integer> g = m.handle |
1196 |
|
(f, |
1197 |
|
(Integer x, Throwable t) -> { |
1201 |
|
a.getAndIncrement(); |
1202 |
|
throw ex; |
1203 |
|
}); |
1204 |
< |
if (createIncomplete) f.complete(v1); |
1204 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1205 |
|
|
1206 |
|
checkCompletedWithWrappedException(g, ex); |
1207 |
|
checkCompletedNormally(f, v1); |
1282 |
|
*/ |
1283 |
|
public void testThenRun_normalCompletion() { |
1284 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1107 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1285 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1286 |
|
{ |
1287 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1288 |
< |
final Noop r = new Noop(m); |
1289 |
< |
if (!createIncomplete) f.complete(v1); |
1113 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1114 |
< |
if (createIncomplete) { |
1115 |
< |
checkIncomplete(g); |
1116 |
< |
f.complete(v1); |
1117 |
< |
} |
1288 |
> |
final Noop[] rs = new Noop[6]; |
1289 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1290 |
|
|
1291 |
< |
checkCompletedNormally(g, null); |
1291 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1292 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1293 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1294 |
> |
checkIncomplete(h0); |
1295 |
> |
checkIncomplete(h1); |
1296 |
> |
checkIncomplete(h2); |
1297 |
> |
assertTrue(f.complete(v1)); |
1298 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1299 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1300 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1301 |
> |
|
1302 |
> |
checkCompletedNormally(h0, null); |
1303 |
> |
checkCompletedNormally(h1, null); |
1304 |
> |
checkCompletedNormally(h2, null); |
1305 |
> |
checkCompletedNormally(h3, null); |
1306 |
> |
checkCompletedNormally(h4, null); |
1307 |
> |
checkCompletedNormally(h5, null); |
1308 |
|
checkCompletedNormally(f, v1); |
1309 |
< |
r.assertInvoked(); |
1309 |
> |
for (Noop r : rs) r.assertInvoked(); |
1310 |
|
}} |
1311 |
|
|
1312 |
|
/** |
1315 |
|
*/ |
1316 |
|
public void testThenRun_exceptionalCompletion() { |
1317 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1130 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1318 |
|
{ |
1319 |
|
final CFException ex = new CFException(); |
1320 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1321 |
< |
final Noop r = new Noop(m); |
1322 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1136 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1137 |
< |
if (createIncomplete) { |
1138 |
< |
checkIncomplete(g); |
1139 |
< |
f.completeExceptionally(ex); |
1140 |
< |
} |
1321 |
> |
final Noop[] rs = new Noop[6]; |
1322 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1323 |
|
|
1324 |
< |
checkCompletedWithWrappedException(g, ex); |
1324 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1325 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1326 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1327 |
> |
checkIncomplete(h0); |
1328 |
> |
checkIncomplete(h1); |
1329 |
> |
checkIncomplete(h2); |
1330 |
> |
assertTrue(f.completeExceptionally(ex)); |
1331 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1332 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1333 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1334 |
> |
|
1335 |
> |
checkCompletedWithWrappedException(h0, ex); |
1336 |
> |
checkCompletedWithWrappedException(h1, ex); |
1337 |
> |
checkCompletedWithWrappedException(h2, ex); |
1338 |
> |
checkCompletedWithWrappedException(h3, ex); |
1339 |
> |
checkCompletedWithWrappedException(h4, ex); |
1340 |
> |
checkCompletedWithWrappedException(h5, ex); |
1341 |
|
checkCompletedExceptionally(f, ex); |
1342 |
< |
r.assertNotInvoked(); |
1342 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1343 |
|
}} |
1344 |
|
|
1345 |
|
/** |
1347 |
|
*/ |
1348 |
|
public void testThenRun_sourceCancelled() { |
1349 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1152 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1350 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1351 |
|
{ |
1352 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1353 |
< |
final Noop r = new Noop(m); |
1354 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1158 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1159 |
< |
if (createIncomplete) { |
1160 |
< |
checkIncomplete(g); |
1161 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1162 |
< |
} |
1353 |
> |
final Noop[] rs = new Noop[6]; |
1354 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1355 |
|
|
1356 |
< |
checkCompletedWithWrappedCancellationException(g); |
1356 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1357 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1358 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1359 |
> |
checkIncomplete(h0); |
1360 |
> |
checkIncomplete(h1); |
1361 |
> |
checkIncomplete(h2); |
1362 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1363 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1364 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1365 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1366 |
> |
|
1367 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1368 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1369 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1370 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1371 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1372 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1373 |
|
checkCancelled(f); |
1374 |
< |
r.assertNotInvoked(); |
1374 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1375 |
|
}} |
1376 |
|
|
1377 |
|
/** |
1379 |
|
*/ |
1380 |
|
public void testThenRun_actionFailed() { |
1381 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1174 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1382 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1383 |
|
{ |
1384 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1385 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1386 |
< |
if (!createIncomplete) f.complete(v1); |
1180 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1181 |
< |
if (createIncomplete) { |
1182 |
< |
checkIncomplete(g); |
1183 |
< |
f.complete(v1); |
1184 |
< |
} |
1385 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1386 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1387 |
|
|
1388 |
< |
checkCompletedWithWrappedCFException(g); |
1388 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1389 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1390 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1391 |
> |
assertTrue(f.complete(v1)); |
1392 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1393 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1394 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1395 |
> |
|
1396 |
> |
checkCompletedWithWrappedCFException(h0); |
1397 |
> |
checkCompletedWithWrappedCFException(h1); |
1398 |
> |
checkCompletedWithWrappedCFException(h2); |
1399 |
> |
checkCompletedWithWrappedCFException(h3); |
1400 |
> |
checkCompletedWithWrappedCFException(h4); |
1401 |
> |
checkCompletedWithWrappedCFException(h5); |
1402 |
|
checkCompletedNormally(f, v1); |
1403 |
|
}} |
1404 |
|
|
1407 |
|
*/ |
1408 |
|
public void testThenApply_normalCompletion() { |
1409 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1195 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1410 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1411 |
|
{ |
1412 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1413 |
< |
final IncFunction r = new IncFunction(m); |
1414 |
< |
if (!createIncomplete) f.complete(v1); |
1201 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1202 |
< |
if (createIncomplete) { |
1203 |
< |
checkIncomplete(g); |
1204 |
< |
f.complete(v1); |
1205 |
< |
} |
1413 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1414 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1415 |
|
|
1416 |
< |
checkCompletedNormally(g, inc(v1)); |
1416 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1417 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1418 |
> |
checkIncomplete(h0); |
1419 |
> |
checkIncomplete(h1); |
1420 |
> |
assertTrue(f.complete(v1)); |
1421 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1422 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1423 |
> |
|
1424 |
> |
checkCompletedNormally(h0, inc(v1)); |
1425 |
> |
checkCompletedNormally(h1, inc(v1)); |
1426 |
> |
checkCompletedNormally(h2, inc(v1)); |
1427 |
> |
checkCompletedNormally(h3, inc(v1)); |
1428 |
|
checkCompletedNormally(f, v1); |
1429 |
< |
r.assertValue(inc(v1)); |
1429 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1430 |
|
}} |
1431 |
|
|
1432 |
|
/** |
1435 |
|
*/ |
1436 |
|
public void testThenApply_exceptionalCompletion() { |
1437 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1218 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1438 |
|
{ |
1439 |
|
final CFException ex = new CFException(); |
1440 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1441 |
< |
final IncFunction r = new IncFunction(m); |
1442 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1224 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1225 |
< |
if (createIncomplete) { |
1226 |
< |
checkIncomplete(g); |
1227 |
< |
f.completeExceptionally(ex); |
1228 |
< |
} |
1441 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1442 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1443 |
|
|
1444 |
< |
checkCompletedWithWrappedException(g, ex); |
1444 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1445 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1446 |
> |
assertTrue(f.completeExceptionally(ex)); |
1447 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1448 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1449 |
> |
|
1450 |
> |
checkCompletedWithWrappedException(h0, ex); |
1451 |
> |
checkCompletedWithWrappedException(h1, ex); |
1452 |
> |
checkCompletedWithWrappedException(h2, ex); |
1453 |
> |
checkCompletedWithWrappedException(h3, ex); |
1454 |
|
checkCompletedExceptionally(f, ex); |
1455 |
< |
r.assertNotInvoked(); |
1455 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1456 |
|
}} |
1457 |
|
|
1458 |
|
/** |
1460 |
|
*/ |
1461 |
|
public void testThenApply_sourceCancelled() { |
1462 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1240 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1463 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1464 |
|
{ |
1465 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1466 |
< |
final IncFunction r = new IncFunction(m); |
1467 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1246 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1247 |
< |
if (createIncomplete) { |
1248 |
< |
checkIncomplete(g); |
1249 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1250 |
< |
} |
1466 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1467 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1468 |
|
|
1469 |
< |
checkCompletedWithWrappedCancellationException(g); |
1469 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1470 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1471 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1472 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1473 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1474 |
> |
|
1475 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1476 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1477 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1478 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1479 |
|
checkCancelled(f); |
1480 |
< |
r.assertNotInvoked(); |
1480 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1481 |
|
}} |
1482 |
|
|
1483 |
|
/** |
1485 |
|
*/ |
1486 |
|
public void testThenApply_actionFailed() { |
1487 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1262 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1488 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1489 |
|
{ |
1490 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1491 |
< |
final FailingFunction r = new FailingFunction(m); |
1492 |
< |
if (!createIncomplete) f.complete(v1); |
1268 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1269 |
< |
if (createIncomplete) { |
1270 |
< |
checkIncomplete(g); |
1271 |
< |
f.complete(v1); |
1272 |
< |
} |
1491 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1492 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1493 |
|
|
1494 |
< |
checkCompletedWithWrappedCFException(g); |
1494 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1495 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1496 |
> |
assertTrue(f.complete(v1)); |
1497 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1498 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1499 |
> |
|
1500 |
> |
checkCompletedWithWrappedCFException(h0); |
1501 |
> |
checkCompletedWithWrappedCFException(h1); |
1502 |
> |
checkCompletedWithWrappedCFException(h2); |
1503 |
> |
checkCompletedWithWrappedCFException(h3); |
1504 |
|
checkCompletedNormally(f, v1); |
1505 |
|
}} |
1506 |
|
|
1509 |
|
*/ |
1510 |
|
public void testThenAccept_normalCompletion() { |
1511 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1283 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1512 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1513 |
|
{ |
1514 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1515 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1516 |
< |
if (!createIncomplete) f.complete(v1); |
1289 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1290 |
< |
if (createIncomplete) { |
1291 |
< |
checkIncomplete(g); |
1292 |
< |
f.complete(v1); |
1293 |
< |
} |
1515 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1516 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1517 |
|
|
1518 |
< |
checkCompletedNormally(g, null); |
1519 |
< |
r.assertValue(v1); |
1518 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1519 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1520 |
> |
checkIncomplete(h0); |
1521 |
> |
checkIncomplete(h1); |
1522 |
> |
assertTrue(f.complete(v1)); |
1523 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1524 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1525 |
> |
|
1526 |
> |
checkCompletedNormally(h0, null); |
1527 |
> |
checkCompletedNormally(h1, null); |
1528 |
> |
checkCompletedNormally(h2, null); |
1529 |
> |
checkCompletedNormally(h3, null); |
1530 |
|
checkCompletedNormally(f, v1); |
1531 |
+ |
for (NoopConsumer r : rs) r.assertValue(v1); |
1532 |
|
}} |
1533 |
|
|
1534 |
|
/** |
1537 |
|
*/ |
1538 |
|
public void testThenAccept_exceptionalCompletion() { |
1539 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1306 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1540 |
|
{ |
1541 |
|
final CFException ex = new CFException(); |
1542 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1543 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1544 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1312 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1313 |
< |
if (createIncomplete) { |
1314 |
< |
checkIncomplete(g); |
1315 |
< |
f.completeExceptionally(ex); |
1316 |
< |
} |
1543 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1544 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1545 |
|
|
1546 |
< |
checkCompletedWithWrappedException(g, ex); |
1546 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1547 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1548 |
> |
assertTrue(f.completeExceptionally(ex)); |
1549 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1550 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1551 |
> |
|
1552 |
> |
checkCompletedWithWrappedException(h0, ex); |
1553 |
> |
checkCompletedWithWrappedException(h1, ex); |
1554 |
> |
checkCompletedWithWrappedException(h2, ex); |
1555 |
> |
checkCompletedWithWrappedException(h3, ex); |
1556 |
|
checkCompletedExceptionally(f, ex); |
1557 |
< |
r.assertNotInvoked(); |
1557 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1558 |
|
}} |
1559 |
|
|
1560 |
|
/** |
1562 |
|
*/ |
1563 |
|
public void testThenAccept_sourceCancelled() { |
1564 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1328 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1565 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1566 |
|
{ |
1567 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1568 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1569 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1334 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1335 |
< |
if (createIncomplete) { |
1336 |
< |
checkIncomplete(g); |
1337 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1338 |
< |
} |
1568 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1569 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1570 |
|
|
1571 |
< |
checkCompletedWithWrappedCancellationException(g); |
1571 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1572 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1573 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1574 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1575 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1576 |
> |
|
1577 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1578 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1579 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1580 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1581 |
|
checkCancelled(f); |
1582 |
< |
r.assertNotInvoked(); |
1582 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1583 |
|
}} |
1584 |
|
|
1585 |
|
/** |
1587 |
|
*/ |
1588 |
|
public void testThenAccept_actionFailed() { |
1589 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1350 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1590 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1591 |
|
{ |
1592 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1593 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1594 |
< |
if (!createIncomplete) f.complete(v1); |
1356 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1357 |
< |
if (createIncomplete) { |
1358 |
< |
checkIncomplete(g); |
1359 |
< |
f.complete(v1); |
1360 |
< |
} |
1593 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1594 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1595 |
|
|
1596 |
< |
checkCompletedWithWrappedCFException(g); |
1596 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1597 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1598 |
> |
assertTrue(f.complete(v1)); |
1599 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1600 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1601 |
> |
|
1602 |
> |
checkCompletedWithWrappedCFException(h0); |
1603 |
> |
checkCompletedWithWrappedCFException(h1); |
1604 |
> |
checkCompletedWithWrappedCFException(h2); |
1605 |
> |
checkCompletedWithWrappedCFException(h3); |
1606 |
|
checkCompletedNormally(f, v1); |
1607 |
|
}} |
1608 |
|
|
1612 |
|
*/ |
1613 |
|
public void testThenCombine_normalCompletion() { |
1614 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1372 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1615 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1616 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1617 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1618 |
|
{ |
1619 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1620 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1621 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1621 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1622 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1623 |
|
|
1624 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1625 |
< |
if (!createIncomplete) |
1626 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1627 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1628 |
< |
if (createIncomplete) { |
1629 |
< |
checkIncomplete(h); |
1630 |
< |
r.assertNotInvoked(); |
1631 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1632 |
< |
} |
1624 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1625 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1626 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1627 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1628 |
> |
|
1629 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1630 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1631 |
> |
assertTrue(fst.complete(w1)); |
1632 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1633 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1634 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1635 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1636 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1637 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1638 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1639 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1640 |
> |
assertTrue(snd.complete(w2)); |
1641 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1642 |
> |
|
1643 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1644 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1645 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1646 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1647 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1648 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1649 |
|
|
1391 |
– |
checkCompletedNormally(h, subtract(v1, v2)); |
1650 |
|
checkCompletedNormally(f, v1); |
1651 |
|
checkCompletedNormally(g, v2); |
1394 |
– |
r.assertValue(subtract(v1, v2)); |
1652 |
|
}} |
1653 |
|
|
1654 |
|
/** |
1655 |
|
* thenCombine result completes exceptionally after exceptional |
1656 |
|
* completion of either source |
1657 |
|
*/ |
1658 |
< |
public void testThenCombine_exceptionalCompletion() { |
1658 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1659 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1403 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1660 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1661 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1662 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1663 |
|
{ |
1664 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1665 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1666 |
|
final CFException ex = new CFException(); |
1667 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1668 |
< |
|
1669 |
< |
(fFirst ? f : g).complete(v1); |
1670 |
< |
if (!createIncomplete) |
1671 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1672 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1673 |
< |
if (createIncomplete) { |
1674 |
< |
checkIncomplete(h); |
1675 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1676 |
< |
} |
1667 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1668 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1669 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1670 |
> |
|
1671 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1672 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1673 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1674 |
> |
() -> fst.completeExceptionally(ex) : |
1675 |
> |
() -> fst.complete(v1); |
1676 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1677 |
> |
() -> snd.complete(v1) : |
1678 |
> |
() -> snd.completeExceptionally(ex); |
1679 |
> |
|
1680 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1681 |
> |
assertTrue(complete1.call()); |
1682 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1683 |
> |
checkIncomplete(h1); |
1684 |
> |
checkIncomplete(h2); |
1685 |
> |
assertTrue(complete2.call()); |
1686 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1687 |
|
|
1688 |
< |
checkCompletedWithWrappedException(h, ex); |
1689 |
< |
r.assertNotInvoked(); |
1690 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1691 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1688 |
> |
checkCompletedWithWrappedException(h1, ex); |
1689 |
> |
checkCompletedWithWrappedException(h2, ex); |
1690 |
> |
checkCompletedWithWrappedException(h3, ex); |
1691 |
> |
r1.assertNotInvoked(); |
1692 |
> |
r2.assertNotInvoked(); |
1693 |
> |
r3.assertNotInvoked(); |
1694 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1695 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1696 |
|
}} |
1697 |
|
|
1698 |
|
/** |
1699 |
|
* thenCombine result completes exceptionally if either source cancelled |
1700 |
|
*/ |
1701 |
< |
public void testThenCombine_sourceCancelled() { |
1701 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1702 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1703 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1433 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1704 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1705 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1706 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1707 |
|
{ |
1708 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1709 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1710 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1710 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1711 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1712 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1713 |
|
|
1714 |
< |
(fFirst ? f : g).complete(v1); |
1715 |
< |
if (!createIncomplete) |
1716 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1717 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1718 |
< |
if (createIncomplete) { |
1719 |
< |
checkIncomplete(h); |
1720 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1721 |
< |
} |
1714 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1715 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1716 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1717 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1718 |
> |
() -> fst.complete(v1); |
1719 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1720 |
> |
() -> snd.complete(v1) : |
1721 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1722 |
|
|
1723 |
< |
checkCompletedWithWrappedCancellationException(h); |
1724 |
< |
checkCancelled(!fFirst ? f : g); |
1725 |
< |
r.assertNotInvoked(); |
1726 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1723 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1724 |
> |
assertTrue(complete1.call()); |
1725 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1726 |
> |
checkIncomplete(h1); |
1727 |
> |
checkIncomplete(h2); |
1728 |
> |
assertTrue(complete2.call()); |
1729 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1730 |
> |
|
1731 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1732 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1733 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1734 |
> |
r1.assertNotInvoked(); |
1735 |
> |
r2.assertNotInvoked(); |
1736 |
> |
r3.assertNotInvoked(); |
1737 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1738 |
> |
checkCancelled(failFirst ? fst : snd); |
1739 |
|
}} |
1740 |
|
|
1741 |
|
/** |
1749 |
|
{ |
1750 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1751 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1752 |
< |
final FailingBiFunction r = new FailingBiFunction(m); |
1753 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1754 |
< |
|
1755 |
< |
if (fFirst) { |
1756 |
< |
f.complete(v1); |
1757 |
< |
g.complete(v2); |
1758 |
< |
} else { |
1759 |
< |
g.complete(v2); |
1760 |
< |
f.complete(v1); |
1761 |
< |
} |
1752 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1753 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1754 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1755 |
> |
|
1756 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1757 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1758 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1759 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1760 |
> |
|
1761 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1762 |
> |
assertTrue(fst.complete(w1)); |
1763 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1764 |
> |
assertTrue(snd.complete(w2)); |
1765 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1766 |
|
|
1767 |
< |
checkCompletedWithWrappedCFException(h); |
1767 |
> |
checkCompletedWithWrappedCFException(h1); |
1768 |
> |
checkCompletedWithWrappedCFException(h2); |
1769 |
> |
checkCompletedWithWrappedCFException(h3); |
1770 |
> |
r1.assertInvoked(); |
1771 |
> |
r2.assertInvoked(); |
1772 |
> |
r3.assertInvoked(); |
1773 |
|
checkCompletedNormally(f, v1); |
1774 |
|
checkCompletedNormally(g, v2); |
1775 |
|
}} |
1780 |
|
*/ |
1781 |
|
public void testThenAcceptBoth_normalCompletion() { |
1782 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1489 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1783 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1784 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1785 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1786 |
|
{ |
1787 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1788 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1789 |
< |
final SubtractAction r = new SubtractAction(m); |
1790 |
< |
|
1791 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1792 |
< |
if (!createIncomplete) |
1793 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1794 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1795 |
< |
if (createIncomplete) { |
1796 |
< |
checkIncomplete(h); |
1797 |
< |
r.assertNotInvoked(); |
1798 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1799 |
< |
} |
1789 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1790 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1791 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1792 |
> |
|
1793 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1794 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1795 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1796 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1797 |
> |
|
1798 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1799 |
> |
assertTrue(fst.complete(w1)); |
1800 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1801 |
> |
checkIncomplete(h1); |
1802 |
> |
checkIncomplete(h2); |
1803 |
> |
r1.assertNotInvoked(); |
1804 |
> |
r2.assertNotInvoked(); |
1805 |
> |
assertTrue(snd.complete(w2)); |
1806 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1807 |
|
|
1808 |
< |
checkCompletedNormally(h, null); |
1809 |
< |
r.assertValue(subtract(v1, v2)); |
1808 |
> |
checkCompletedNormally(h1, null); |
1809 |
> |
checkCompletedNormally(h2, null); |
1810 |
> |
checkCompletedNormally(h3, null); |
1811 |
> |
r1.assertValue(subtract(v1, v2)); |
1812 |
> |
r2.assertValue(subtract(v1, v2)); |
1813 |
> |
r3.assertValue(subtract(v1, v2)); |
1814 |
|
checkCompletedNormally(f, v1); |
1815 |
|
checkCompletedNormally(g, v2); |
1816 |
|
}} |
1819 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1820 |
|
* completion of either source |
1821 |
|
*/ |
1822 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1822 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1823 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1520 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1824 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1825 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1826 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1827 |
|
{ |
1828 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1829 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1830 |
|
final CFException ex = new CFException(); |
1831 |
< |
final SubtractAction r = new SubtractAction(m); |
1832 |
< |
|
1833 |
< |
(fFirst ? f : g).complete(v1); |
1834 |
< |
if (!createIncomplete) |
1835 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1836 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1837 |
< |
if (createIncomplete) { |
1838 |
< |
checkIncomplete(h); |
1839 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1840 |
< |
} |
1831 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1832 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1833 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1834 |
> |
|
1835 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1836 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1837 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1838 |
> |
() -> fst.completeExceptionally(ex) : |
1839 |
> |
() -> fst.complete(v1); |
1840 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1841 |
> |
() -> snd.complete(v1) : |
1842 |
> |
() -> snd.completeExceptionally(ex); |
1843 |
> |
|
1844 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1845 |
> |
assertTrue(complete1.call()); |
1846 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1847 |
> |
checkIncomplete(h1); |
1848 |
> |
checkIncomplete(h2); |
1849 |
> |
assertTrue(complete2.call()); |
1850 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1851 |
|
|
1852 |
< |
checkCompletedWithWrappedException(h, ex); |
1853 |
< |
r.assertNotInvoked(); |
1854 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1855 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1852 |
> |
checkCompletedWithWrappedException(h1, ex); |
1853 |
> |
checkCompletedWithWrappedException(h2, ex); |
1854 |
> |
checkCompletedWithWrappedException(h3, ex); |
1855 |
> |
r1.assertNotInvoked(); |
1856 |
> |
r2.assertNotInvoked(); |
1857 |
> |
r3.assertNotInvoked(); |
1858 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1859 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1860 |
|
}} |
1861 |
|
|
1862 |
|
/** |
1863 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1864 |
|
*/ |
1865 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1865 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1866 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1867 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1550 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1868 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1869 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1870 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1871 |
|
{ |
1872 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1873 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1874 |
< |
final SubtractAction r = new SubtractAction(m); |
1874 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1875 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1876 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1877 |
|
|
1878 |
< |
(fFirst ? f : g).complete(v1); |
1879 |
< |
if (!createIncomplete) |
1880 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1881 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1882 |
< |
if (createIncomplete) { |
1883 |
< |
checkIncomplete(h); |
1884 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1885 |
< |
} |
1878 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1879 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1880 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1881 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1882 |
> |
() -> fst.complete(v1); |
1883 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1884 |
> |
() -> snd.complete(v1) : |
1885 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1886 |
|
|
1887 |
< |
checkCompletedWithWrappedCancellationException(h); |
1888 |
< |
checkCancelled(!fFirst ? f : g); |
1889 |
< |
r.assertNotInvoked(); |
1890 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1887 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1888 |
> |
assertTrue(complete1.call()); |
1889 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1890 |
> |
checkIncomplete(h1); |
1891 |
> |
checkIncomplete(h2); |
1892 |
> |
assertTrue(complete2.call()); |
1893 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1894 |
> |
|
1895 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1896 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1897 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1898 |
> |
r1.assertNotInvoked(); |
1899 |
> |
r2.assertNotInvoked(); |
1900 |
> |
r3.assertNotInvoked(); |
1901 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1902 |
> |
checkCancelled(failFirst ? fst : snd); |
1903 |
|
}} |
1904 |
|
|
1905 |
|
/** |
1913 |
|
{ |
1914 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1915 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1916 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1917 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1916 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1917 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1918 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1919 |
> |
|
1920 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1921 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1922 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1923 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1924 |
> |
|
1925 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1926 |
> |
assertTrue(fst.complete(w1)); |
1927 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1928 |
> |
assertTrue(snd.complete(w2)); |
1929 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1930 |
|
|
1931 |
< |
if (fFirst) { |
1932 |
< |
f.complete(v1); |
1933 |
< |
g.complete(v2); |
1934 |
< |
} else { |
1935 |
< |
g.complete(v2); |
1936 |
< |
f.complete(v1); |
1593 |
< |
} |
1594 |
< |
|
1595 |
< |
checkCompletedWithWrappedCFException(h); |
1931 |
> |
checkCompletedWithWrappedCFException(h1); |
1932 |
> |
checkCompletedWithWrappedCFException(h2); |
1933 |
> |
checkCompletedWithWrappedCFException(h3); |
1934 |
> |
r1.assertInvoked(); |
1935 |
> |
r2.assertInvoked(); |
1936 |
> |
r3.assertInvoked(); |
1937 |
|
checkCompletedNormally(f, v1); |
1938 |
|
checkCompletedNormally(g, v2); |
1939 |
|
}} |
1944 |
|
*/ |
1945 |
|
public void testRunAfterBoth_normalCompletion() { |
1946 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1606 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1947 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1948 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1949 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1950 |
|
{ |
1951 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1952 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1953 |
< |
final Noop r = new Noop(m); |
1954 |
< |
|
1955 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1956 |
< |
if (!createIncomplete) |
1957 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1958 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1959 |
< |
if (createIncomplete) { |
1960 |
< |
checkIncomplete(h); |
1961 |
< |
r.assertNotInvoked(); |
1962 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1963 |
< |
} |
1953 |
> |
final Noop r1 = new Noop(m); |
1954 |
> |
final Noop r2 = new Noop(m); |
1955 |
> |
final Noop r3 = new Noop(m); |
1956 |
> |
|
1957 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1958 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1959 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1960 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1961 |
> |
|
1962 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1963 |
> |
assertTrue(fst.complete(w1)); |
1964 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1965 |
> |
checkIncomplete(h1); |
1966 |
> |
checkIncomplete(h2); |
1967 |
> |
r1.assertNotInvoked(); |
1968 |
> |
r2.assertNotInvoked(); |
1969 |
> |
assertTrue(snd.complete(w2)); |
1970 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1971 |
|
|
1972 |
< |
checkCompletedNormally(h, null); |
1973 |
< |
r.assertInvoked(); |
1972 |
> |
checkCompletedNormally(h1, null); |
1973 |
> |
checkCompletedNormally(h2, null); |
1974 |
> |
checkCompletedNormally(h3, null); |
1975 |
> |
r1.assertInvoked(); |
1976 |
> |
r2.assertInvoked(); |
1977 |
> |
r3.assertInvoked(); |
1978 |
|
checkCompletedNormally(f, v1); |
1979 |
|
checkCompletedNormally(g, v2); |
1980 |
|
}} |
1983 |
|
* runAfterBoth result completes exceptionally after exceptional |
1984 |
|
* completion of either source |
1985 |
|
*/ |
1986 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
1986 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1987 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1637 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1988 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1989 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1990 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1991 |
|
{ |
1992 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1993 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1994 |
|
final CFException ex = new CFException(); |
1995 |
< |
final Noop r = new Noop(m); |
1996 |
< |
|
1997 |
< |
(fFirst ? f : g).complete(v1); |
1998 |
< |
if (!createIncomplete) |
1999 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
2000 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2001 |
< |
if (createIncomplete) { |
2002 |
< |
checkIncomplete(h); |
2003 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
2004 |
< |
} |
1995 |
> |
final Noop r1 = new Noop(m); |
1996 |
> |
final Noop r2 = new Noop(m); |
1997 |
> |
final Noop r3 = new Noop(m); |
1998 |
> |
|
1999 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2000 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2001 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2002 |
> |
() -> fst.completeExceptionally(ex) : |
2003 |
> |
() -> fst.complete(v1); |
2004 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2005 |
> |
() -> snd.complete(v1) : |
2006 |
> |
() -> snd.completeExceptionally(ex); |
2007 |
> |
|
2008 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2009 |
> |
assertTrue(complete1.call()); |
2010 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2011 |
> |
checkIncomplete(h1); |
2012 |
> |
checkIncomplete(h2); |
2013 |
> |
assertTrue(complete2.call()); |
2014 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2015 |
|
|
2016 |
< |
checkCompletedWithWrappedException(h, ex); |
2017 |
< |
r.assertNotInvoked(); |
2018 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2019 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
2016 |
> |
checkCompletedWithWrappedException(h1, ex); |
2017 |
> |
checkCompletedWithWrappedException(h2, ex); |
2018 |
> |
checkCompletedWithWrappedException(h3, ex); |
2019 |
> |
r1.assertNotInvoked(); |
2020 |
> |
r2.assertNotInvoked(); |
2021 |
> |
r3.assertNotInvoked(); |
2022 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2023 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2024 |
|
}} |
2025 |
|
|
2026 |
|
/** |
2027 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
2028 |
|
*/ |
2029 |
< |
public void testRunAfterBoth_sourceCancelled() { |
2029 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2030 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2031 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1667 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2032 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2033 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2034 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2035 |
|
{ |
2036 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2037 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2038 |
< |
final Noop r = new Noop(m); |
2038 |
> |
final Noop r1 = new Noop(m); |
2039 |
> |
final Noop r2 = new Noop(m); |
2040 |
> |
final Noop r3 = new Noop(m); |
2041 |
|
|
2042 |
+ |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2043 |
+ |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2044 |
+ |
final Callable<Boolean> complete1 = failFirst ? |
2045 |
+ |
() -> fst.cancel(mayInterruptIfRunning) : |
2046 |
+ |
() -> fst.complete(v1); |
2047 |
+ |
final Callable<Boolean> complete2 = failFirst ? |
2048 |
+ |
() -> snd.complete(v1) : |
2049 |
+ |
() -> snd.cancel(mayInterruptIfRunning); |
2050 |
|
|
2051 |
< |
(fFirst ? f : g).complete(v1); |
2052 |
< |
if (!createIncomplete) |
2053 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2054 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2055 |
< |
if (createIncomplete) { |
2056 |
< |
checkIncomplete(h); |
2057 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1683 |
< |
} |
2051 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2052 |
> |
assertTrue(complete1.call()); |
2053 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2054 |
> |
checkIncomplete(h1); |
2055 |
> |
checkIncomplete(h2); |
2056 |
> |
assertTrue(complete2.call()); |
2057 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2058 |
|
|
2059 |
< |
checkCompletedWithWrappedCancellationException(h); |
2060 |
< |
checkCancelled(!fFirst ? f : g); |
2061 |
< |
r.assertNotInvoked(); |
2062 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2059 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2060 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2061 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2062 |
> |
r1.assertNotInvoked(); |
2063 |
> |
r2.assertNotInvoked(); |
2064 |
> |
r3.assertNotInvoked(); |
2065 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2066 |
> |
checkCancelled(failFirst ? fst : snd); |
2067 |
|
}} |
2068 |
|
|
2069 |
|
/** |
2079 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2080 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
2081 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
2082 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
2083 |
|
|
2084 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2085 |
< |
if (fFirst) { |
2086 |
< |
f.complete(v1); |
2087 |
< |
g.complete(v2); |
2088 |
< |
} else { |
2089 |
< |
g.complete(v2); |
2090 |
< |
f.complete(v1); |
2091 |
< |
} |
2092 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2084 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2085 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2086 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2087 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2088 |
> |
|
2089 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2090 |
> |
assertTrue(fst.complete(w1)); |
2091 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2092 |
> |
assertTrue(snd.complete(w2)); |
2093 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2094 |
|
|
2095 |
|
checkCompletedWithWrappedCFException(h1); |
2096 |
|
checkCompletedWithWrappedCFException(h2); |
2097 |
+ |
checkCompletedWithWrappedCFException(h3); |
2098 |
+ |
r1.assertInvoked(); |
2099 |
+ |
r2.assertInvoked(); |
2100 |
+ |
r3.assertInvoked(); |
2101 |
|
checkCompletedNormally(f, v1); |
2102 |
|
checkCompletedNormally(g, v2); |
2103 |
|
}} |
2220 |
|
|
2221 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2222 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2223 |
< |
if (fFirst) { |
2224 |
< |
f.complete(v1); |
1841 |
< |
g.completeExceptionally(ex); |
1842 |
< |
} else { |
1843 |
< |
g.completeExceptionally(ex); |
1844 |
< |
f.complete(v1); |
1845 |
< |
} |
2223 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2224 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2225 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2226 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2227 |
|
|
2327 |
|
|
2328 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2329 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2330 |
< |
if (fFirst) { |
2331 |
< |
f.complete(v1); |
1953 |
< |
g.cancel(mayInterruptIfRunning); |
1954 |
< |
} else { |
1955 |
< |
g.cancel(mayInterruptIfRunning); |
1956 |
< |
f.complete(v1); |
1957 |
< |
} |
2330 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2331 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2332 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2333 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2334 |
|
|
2530 |
|
|
2531 |
|
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2532 |
|
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2533 |
< |
if (fFirst) { |
2534 |
< |
f.complete(v1); |
2161 |
< |
g.completeExceptionally(ex); |
2162 |
< |
} else { |
2163 |
< |
g.completeExceptionally(ex); |
2164 |
< |
f.complete(v1); |
2165 |
< |
} |
2533 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2534 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2535 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2536 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2537 |
|
|
2734 |
|
checkIncomplete(h1); |
2735 |
|
rs[0].assertNotInvoked(); |
2736 |
|
rs[1].assertNotInvoked(); |
2737 |
< |
f.completeExceptionally(ex); |
2737 |
> |
assertTrue(f.completeExceptionally(ex)); |
2738 |
|
checkCompletedWithWrappedException(h0, ex); |
2739 |
|
checkCompletedWithWrappedException(h1, ex); |
2740 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2742 |
|
checkCompletedWithWrappedException(h2, ex); |
2743 |
|
checkCompletedWithWrappedException(h3, ex); |
2744 |
|
|
2745 |
< |
g.complete(v1); |
2745 |
> |
assertTrue(g.complete(v1)); |
2746 |
|
|
2747 |
|
// unspecified behavior - both source completions available |
2748 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2785 |
|
|
2786 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2787 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2788 |
< |
if (fFirst) { |
2789 |
< |
f.complete(v1); |
2421 |
< |
g.completeExceptionally(ex); |
2422 |
< |
} else { |
2423 |
< |
g.completeExceptionally(ex); |
2424 |
< |
f.complete(v1); |
2425 |
< |
} |
2788 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2789 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2790 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2791 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2792 |
|
|
2851 |
|
checkCompletedWithWrappedCancellationException(h2); |
2852 |
|
checkCompletedWithWrappedCancellationException(h3); |
2853 |
|
|
2854 |
< |
g.complete(v1); |
2854 |
> |
assertTrue(g.complete(v1)); |
2855 |
|
|
2856 |
|
// unspecified behavior - both source completions available |
2857 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2895 |
|
|
2896 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2897 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2898 |
< |
f.complete(v1); |
2898 |
> |
assertTrue(f.complete(v1)); |
2899 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2900 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2901 |
|
checkCompletedWithWrappedCFException(h0); |
2903 |
|
checkCompletedWithWrappedCFException(h2); |
2904 |
|
checkCompletedWithWrappedCFException(h3); |
2905 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2906 |
< |
g.complete(v2); |
2906 |
> |
assertTrue(g.complete(v2)); |
2907 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2908 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2909 |
|
checkCompletedWithWrappedCFException(h4); |
2924 |
|
{ |
2925 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2926 |
|
final CompletableFutureInc r = new CompletableFutureInc(m); |
2927 |
< |
if (!createIncomplete) f.complete(v1); |
2927 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2928 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2929 |
< |
if (createIncomplete) f.complete(v1); |
2929 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2930 |
|
|
2931 |
|
checkCompletedNormally(g, inc(v1)); |
2932 |
|
checkCompletedNormally(f, v1); |
2964 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2965 |
|
final FailingCompletableFutureFunction r |
2966 |
|
= new FailingCompletableFutureFunction(m); |
2967 |
< |
if (!createIncomplete) f.complete(v1); |
2967 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2968 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2969 |
< |
if (createIncomplete) f.complete(v1); |
2969 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2970 |
|
|
2971 |
|
checkCompletedWithWrappedCFException(g); |
2972 |
|
checkCompletedNormally(f, v1); |
3009 |
|
* when all components complete normally |
3010 |
|
*/ |
3011 |
|
public void testAllOf_normal() throws Exception { |
3012 |
< |
for (int k = 1; k < 20; ++k) { |
3012 |
> |
for (int k = 1; k < 10; k++) { |
3013 |
|
CompletableFuture<Integer>[] fs |
3014 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3015 |
< |
for (int i = 0; i < k; ++i) |
3015 |
> |
for (int i = 0; i < k; i++) |
3016 |
|
fs[i] = new CompletableFuture<>(); |
3017 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3018 |
< |
for (int i = 0; i < k; ++i) { |
3018 |
> |
for (int i = 0; i < k; i++) { |
3019 |
|
checkIncomplete(f); |
3020 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3021 |
|
fs[i].complete(one); |
3026 |
|
} |
3027 |
|
|
3028 |
|
public void testAllOf_backwards() throws Exception { |
3029 |
< |
for (int k = 1; k < 20; ++k) { |
3029 |
> |
for (int k = 1; k < 10; k++) { |
3030 |
|
CompletableFuture<Integer>[] fs |
3031 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3032 |
< |
for (int i = 0; i < k; ++i) |
3032 |
> |
for (int i = 0; i < k; i++) |
3033 |
|
fs[i] = new CompletableFuture<>(); |
3034 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3035 |
|
for (int i = k - 1; i >= 0; i--) { |
3042 |
|
} |
3043 |
|
} |
3044 |
|
|
3045 |
+ |
public void testAllOf_exceptional() throws Exception { |
3046 |
+ |
for (int k = 1; k < 10; k++) { |
3047 |
+ |
CompletableFuture<Integer>[] fs |
3048 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3049 |
+ |
CFException ex = new CFException(); |
3050 |
+ |
for (int i = 0; i < k; i++) |
3051 |
+ |
fs[i] = new CompletableFuture<>(); |
3052 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3053 |
+ |
for (int i = 0; i < k; i++) { |
3054 |
+ |
checkIncomplete(f); |
3055 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3056 |
+ |
if (i != k/2) { |
3057 |
+ |
fs[i].complete(i); |
3058 |
+ |
checkCompletedNormally(fs[i], i); |
3059 |
+ |
} else { |
3060 |
+ |
fs[i].completeExceptionally(ex); |
3061 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3062 |
+ |
} |
3063 |
+ |
} |
3064 |
+ |
checkCompletedWithWrappedException(f, ex); |
3065 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3066 |
+ |
} |
3067 |
+ |
} |
3068 |
+ |
|
3069 |
|
/** |
3070 |
|
* anyOf(no component futures) returns an incomplete future |
3071 |
|
*/ |
3072 |
|
public void testAnyOf_empty() throws Exception { |
3073 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3074 |
+ |
{ |
3075 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3076 |
|
checkIncomplete(f); |
3077 |
< |
} |
3077 |
> |
|
3078 |
> |
f.complete(v1); |
3079 |
> |
checkCompletedNormally(f, v1); |
3080 |
> |
}} |
3081 |
|
|
3082 |
|
/** |
3083 |
|
* anyOf returns a future completed normally with a value when |
3084 |
|
* a component future does |
3085 |
|
*/ |
3086 |
|
public void testAnyOf_normal() throws Exception { |
3087 |
< |
for (int k = 0; k < 10; ++k) { |
3087 |
> |
for (int k = 0; k < 10; k++) { |
3088 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3089 |
< |
for (int i = 0; i < k; ++i) |
3089 |
> |
for (int i = 0; i < k; i++) |
3090 |
|
fs[i] = new CompletableFuture<>(); |
3091 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3092 |
|
checkIncomplete(f); |
3093 |
< |
for (int i = 0; i < k; ++i) { |
3094 |
< |
fs[i].complete(one); |
3095 |
< |
checkCompletedNormally(f, one); |
3096 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3093 |
> |
for (int i = 0; i < k; i++) { |
3094 |
> |
fs[i].complete(i); |
3095 |
> |
checkCompletedNormally(f, 0); |
3096 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3097 |
> |
assertTrue(0 <= x && x <= i); |
3098 |
> |
} |
3099 |
> |
} |
3100 |
> |
} |
3101 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3102 |
> |
for (int k = 0; k < 10; k++) { |
3103 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3104 |
> |
for (int i = 0; i < k; i++) |
3105 |
> |
fs[i] = new CompletableFuture<>(); |
3106 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3107 |
> |
checkIncomplete(f); |
3108 |
> |
for (int i = k - 1; i >= 0; i--) { |
3109 |
> |
fs[i].complete(i); |
3110 |
> |
checkCompletedNormally(f, k - 1); |
3111 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3112 |
> |
assertTrue(i <= x && x <= k - 1); |
3113 |
|
} |
3114 |
|
} |
3115 |
|
} |
3118 |
|
* anyOf result completes exceptionally when any component does. |
3119 |
|
*/ |
3120 |
|
public void testAnyOf_exceptional() throws Exception { |
3121 |
< |
for (int k = 0; k < 10; ++k) { |
3121 |
> |
for (int k = 0; k < 10; k++) { |
3122 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3123 |
> |
CFException[] exs = new CFException[k]; |
3124 |
> |
for (int i = 0; i < k; i++) { |
3125 |
> |
fs[i] = new CompletableFuture<>(); |
3126 |
> |
exs[i] = new CFException(); |
3127 |
> |
} |
3128 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3129 |
> |
checkIncomplete(f); |
3130 |
> |
for (int i = 0; i < k; i++) { |
3131 |
> |
fs[i].completeExceptionally(exs[i]); |
3132 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3133 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3134 |
> |
} |
3135 |
> |
} |
3136 |
> |
} |
3137 |
> |
|
3138 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3139 |
> |
for (int k = 0; k < 10; k++) { |
3140 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3141 |
< |
for (int i = 0; i < k; ++i) |
3141 |
> |
CFException[] exs = new CFException[k]; |
3142 |
> |
for (int i = 0; i < k; i++) { |
3143 |
|
fs[i] = new CompletableFuture<>(); |
3144 |
+ |
exs[i] = new CFException(); |
3145 |
+ |
} |
3146 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3147 |
|
checkIncomplete(f); |
3148 |
< |
for (int i = 0; i < k; ++i) { |
3149 |
< |
fs[i].completeExceptionally(new CFException()); |
3150 |
< |
checkCompletedWithWrappedCFException(f); |
3148 |
> |
for (int i = k - 1; i >= 0; i--) { |
3149 |
> |
fs[i].completeExceptionally(exs[i]); |
3150 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3151 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3152 |
|
} |
3153 |
|
} |
3166 |
|
Runnable[] throwingActions = { |
3167 |
|
() -> CompletableFuture.supplyAsync(null), |
3168 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3169 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3169 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3170 |
|
|
3171 |
|
() -> CompletableFuture.runAsync(null), |
3172 |
|
() -> CompletableFuture.runAsync(null, exec), |
3271 |
|
assertSame(f, f.toCompletableFuture()); |
3272 |
|
} |
3273 |
|
|
3274 |
< |
/** |
2845 |
< |
* whenComplete action executes on normal completion, propagating |
2846 |
< |
* source result. |
2847 |
< |
*/ |
2848 |
< |
public void testWhenComplete_normalCompletion1() { |
2849 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2850 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2851 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2852 |
< |
{ |
2853 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2854 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2855 |
< |
if (!createIncomplete) f.complete(v1); |
2856 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2857 |
< |
(f, |
2858 |
< |
(Integer x, Throwable t) -> { |
2859 |
< |
m.checkExecutionMode(); |
2860 |
< |
threadAssertSame(x, v1); |
2861 |
< |
threadAssertNull(t); |
2862 |
< |
a.getAndIncrement(); |
2863 |
< |
}); |
2864 |
< |
if (createIncomplete) f.complete(v1); |
3274 |
> |
//--- tests of implementation details; not part of official tck --- |
3275 |
|
|
3276 |
< |
checkCompletedNormally(g, v1); |
3277 |
< |
checkCompletedNormally(f, v1); |
3278 |
< |
assertEquals(1, a.get()); |
3279 |
< |
}} |
3276 |
> |
Object resultOf(CompletableFuture<?> f) { |
3277 |
> |
try { |
3278 |
> |
java.lang.reflect.Field resultField |
3279 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3280 |
> |
resultField.setAccessible(true); |
3281 |
> |
return resultField.get(f); |
3282 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3283 |
> |
} |
3284 |
|
|
3285 |
< |
/** |
3286 |
< |
* whenComplete action executes on exceptional completion, propagating |
2873 |
< |
* source result. |
2874 |
< |
*/ |
2875 |
< |
public void testWhenComplete_exceptionalCompletion() { |
3285 |
> |
public void testExceptionPropagationReusesResultObject() { |
3286 |
> |
if (!testImplementationDetails) return; |
3287 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2877 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2878 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
3288 |
|
{ |
2880 |
– |
final AtomicInteger a = new AtomicInteger(0); |
3289 |
|
final CFException ex = new CFException(); |
3290 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3291 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
2884 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2885 |
< |
(f, |
2886 |
< |
(Integer x, Throwable t) -> { |
2887 |
< |
m.checkExecutionMode(); |
2888 |
< |
threadAssertNull(x); |
2889 |
< |
threadAssertSame(t, ex); |
2890 |
< |
a.getAndIncrement(); |
2891 |
< |
}); |
2892 |
< |
if (createIncomplete) f.completeExceptionally(ex); |
2893 |
< |
checkCompletedExceptionally(f, ex); |
2894 |
< |
checkCompletedWithWrappedException(g, ex); |
2895 |
< |
assertEquals(1, a.get()); |
2896 |
< |
}} |
3290 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3291 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3292 |
|
|
3293 |
< |
/** |
3294 |
< |
* whenComplete action executes on cancelled source, propagating |
2900 |
< |
* CancellationException. |
2901 |
< |
*/ |
2902 |
< |
public void testWhenComplete_sourceCancelled() { |
2903 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2904 |
< |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2905 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2906 |
< |
{ |
2907 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2908 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2909 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2910 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2911 |
< |
(f, |
2912 |
< |
(Integer x, Throwable t) -> { |
2913 |
< |
m.checkExecutionMode(); |
2914 |
< |
threadAssertNull(x); |
2915 |
< |
threadAssertTrue(t instanceof CancellationException); |
2916 |
< |
a.getAndIncrement(); |
2917 |
< |
}); |
2918 |
< |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
3293 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3294 |
> |
= new ArrayList<>(); |
3295 |
|
|
3296 |
< |
checkCompletedWithWrappedCancellationException(g); |
3297 |
< |
checkCancelled(f); |
3298 |
< |
assertEquals(1, a.get()); |
2923 |
< |
}} |
3296 |
> |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3297 |
> |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3298 |
> |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3299 |
|
|
3300 |
< |
/** |
3301 |
< |
* If a whenComplete action throws an exception when triggered by |
3302 |
< |
* a normal completion, it completes exceptionally |
2928 |
< |
*/ |
2929 |
< |
public void testWhenComplete_actionFailed() { |
2930 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2931 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2932 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2933 |
< |
{ |
2934 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2935 |
< |
final CFException ex = new CFException(); |
2936 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2937 |
< |
if (!createIncomplete) f.complete(v1); |
2938 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2939 |
< |
(f, |
2940 |
< |
(Integer x, Throwable t) -> { |
2941 |
< |
m.checkExecutionMode(); |
2942 |
< |
threadAssertSame(x, v1); |
2943 |
< |
threadAssertNull(t); |
2944 |
< |
a.getAndIncrement(); |
2945 |
< |
throw ex; |
2946 |
< |
}); |
2947 |
< |
if (createIncomplete) f.complete(v1); |
2948 |
< |
checkCompletedNormally(f, v1); |
2949 |
< |
checkCompletedWithWrappedException(g, ex); |
2950 |
< |
assertEquals(1, a.get()); |
2951 |
< |
}} |
3300 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3301 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3302 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3303 |
|
|
3304 |
< |
/** |
3305 |
< |
* If a whenComplete action throws an exception when triggered by |
3306 |
< |
* a source completion that also throws an exception, the source |
2956 |
< |
* exception takes precedence. |
2957 |
< |
*/ |
2958 |
< |
public void testWhenComplete_actionFailedSourceFailed() { |
2959 |
< |
for (boolean createIncomplete : new boolean[] { true, false }) |
2960 |
< |
for (ExecutionMode m : ExecutionMode.values()) |
2961 |
< |
for (Integer v1 : new Integer[] { 1, null }) |
2962 |
< |
{ |
2963 |
< |
final AtomicInteger a = new AtomicInteger(0); |
2964 |
< |
final CFException ex1 = new CFException(); |
2965 |
< |
final CFException ex2 = new CFException(); |
2966 |
< |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3304 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3305 |
> |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3306 |
> |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3307 |
|
|
3308 |
< |
if (!createIncomplete) f.completeExceptionally(ex1); |
2969 |
< |
final CompletableFuture<Integer> g = m.whenComplete |
2970 |
< |
(f, |
2971 |
< |
(Integer x, Throwable t) -> { |
2972 |
< |
m.checkExecutionMode(); |
2973 |
< |
threadAssertSame(t, ex1); |
2974 |
< |
threadAssertNull(x); |
2975 |
< |
a.getAndIncrement(); |
2976 |
< |
throw ex2; |
2977 |
< |
}); |
2978 |
< |
if (createIncomplete) f.completeExceptionally(ex1); |
3308 |
> |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3309 |
|
|
3310 |
< |
checkCompletedExceptionally(f, ex1); |
3311 |
< |
checkCompletedWithWrappedException(g, ex1); |
3312 |
< |
assertEquals(1, a.get()); |
3310 |
> |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3311 |
> |
|
3312 |
> |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3313 |
> |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3314 |
> |
|
3315 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3316 |
> |
fun : funs) { |
3317 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3318 |
> |
f.completeExceptionally(ex); |
3319 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3320 |
> |
checkCompletedWithWrappedException(src, ex); |
3321 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3322 |
> |
checkCompletedWithWrappedException(dep, ex); |
3323 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3324 |
> |
} |
3325 |
> |
|
3326 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3327 |
> |
fun : funs) { |
3328 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3329 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3330 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3331 |
> |
f.completeExceptionally(ex); |
3332 |
> |
checkCompletedWithWrappedException(src, ex); |
3333 |
> |
checkCompletedWithWrappedException(dep, ex); |
3334 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3335 |
> |
} |
3336 |
> |
|
3337 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3338 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3339 |
> |
fun : funs) { |
3340 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3341 |
> |
f.cancel(mayInterruptIfRunning); |
3342 |
> |
checkCancelled(f); |
3343 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3344 |
> |
checkCompletedWithWrappedCancellationException(src); |
3345 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3346 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3347 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3348 |
> |
} |
3349 |
> |
|
3350 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3351 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3352 |
> |
fun : funs) { |
3353 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3354 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3355 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3356 |
> |
f.cancel(mayInterruptIfRunning); |
3357 |
> |
checkCancelled(f); |
3358 |
> |
checkCompletedWithWrappedCancellationException(src); |
3359 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3360 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3361 |
> |
} |
3362 |
|
}} |
3363 |
|
|
3364 |
|
} |