5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.List; |
13 |
> |
import java.util.Objects; |
14 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
15 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
16 |
|
import java.util.concurrent.CompletableFuture; |
17 |
|
import java.util.concurrent.CompletionException; |
18 |
|
import java.util.concurrent.CompletionStage; |
19 |
+ |
import java.util.concurrent.ExecutionException; |
20 |
+ |
import java.util.concurrent.Executor; |
21 |
|
import java.util.concurrent.ForkJoinPool; |
22 |
|
import java.util.concurrent.ForkJoinTask; |
23 |
|
import java.util.concurrent.TimeoutException; |
24 |
|
import java.util.concurrent.atomic.AtomicInteger; |
24 |
– |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
25 |
– |
import static java.util.concurrent.TimeUnit.SECONDS; |
26 |
– |
import java.util.*; |
27 |
– |
import java.util.function.Supplier; |
28 |
– |
import java.util.function.Consumer; |
25 |
|
import java.util.function.BiConsumer; |
30 |
– |
import java.util.function.Function; |
26 |
|
import java.util.function.BiFunction; |
27 |
+ |
import java.util.function.Consumer; |
28 |
+ |
import java.util.function.Function; |
29 |
+ |
import java.util.function.Supplier; |
30 |
+ |
|
31 |
+ |
import junit.framework.Test; |
32 |
+ |
import junit.framework.TestSuite; |
33 |
|
|
34 |
|
public class CompletableFutureTest extends JSR166TestCase { |
35 |
|
|
58 |
|
} |
59 |
|
|
60 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
61 |
< |
try { |
62 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
61 |
> |
checkTimedGet(f, value); |
62 |
> |
|
63 |
|
try { |
64 |
|
assertEquals(value, f.join()); |
65 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
76 |
|
} |
77 |
|
|
78 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
79 |
+ |
long startTime = System.nanoTime(); |
80 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
81 |
|
try { |
82 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
82 |
> |
f.get(timeoutMillis, MILLISECONDS); |
83 |
|
shouldThrow(); |
84 |
|
} catch (ExecutionException success) { |
85 |
|
assertTrue(success.getCause() instanceof CFException); |
86 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
87 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
88 |
+ |
|
89 |
|
try { |
90 |
|
f.join(); |
91 |
|
shouldThrow(); |
111 |
|
|
112 |
|
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
113 |
|
Throwable ex) { |
114 |
+ |
long startTime = System.nanoTime(); |
115 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
116 |
|
try { |
117 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
117 |
> |
f.get(timeoutMillis, MILLISECONDS); |
118 |
|
shouldThrow(); |
119 |
|
} catch (ExecutionException success) { |
120 |
|
assertSame(ex, success.getCause()); |
121 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
123 |
+ |
|
124 |
|
try { |
125 |
|
f.join(); |
126 |
|
shouldThrow(); |
166 |
|
} |
167 |
|
|
168 |
|
void checkCancelled(CompletableFuture<?> f) { |
169 |
+ |
long startTime = System.nanoTime(); |
170 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
171 |
|
try { |
172 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
172 |
> |
f.get(timeoutMillis, MILLISECONDS); |
173 |
|
shouldThrow(); |
174 |
|
} catch (CancellationException success) { |
175 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
176 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
177 |
+ |
|
178 |
|
try { |
179 |
|
f.join(); |
180 |
|
shouldThrow(); |
195 |
|
} |
196 |
|
|
197 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
198 |
+ |
long startTime = System.nanoTime(); |
199 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
200 |
|
try { |
201 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
201 |
> |
f.get(timeoutMillis, MILLISECONDS); |
202 |
|
shouldThrow(); |
203 |
|
} catch (ExecutionException success) { |
204 |
|
assertTrue(success.getCause() instanceof CancellationException); |
205 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
206 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
207 |
+ |
|
208 |
|
try { |
209 |
|
f.join(); |
210 |
|
shouldThrow(); |
243 |
|
* isCancelled, join, get, and getNow |
244 |
|
*/ |
245 |
|
public void testComplete() { |
246 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
247 |
+ |
{ |
248 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
249 |
|
checkIncomplete(f); |
250 |
< |
f.complete(one); |
251 |
< |
checkCompletedNormally(f, one); |
252 |
< |
} |
250 |
> |
assertTrue(f.complete(v1)); |
251 |
> |
assertFalse(f.complete(v1)); |
252 |
> |
checkCompletedNormally(f, v1); |
253 |
> |
}} |
254 |
|
|
255 |
|
/** |
256 |
|
* completeExceptionally completes exceptionally, as indicated by |
269 |
|
* methods isDone, isCancelled, join, get, and getNow |
270 |
|
*/ |
271 |
|
public void testCancel() { |
272 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
273 |
+ |
{ |
274 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
275 |
|
checkIncomplete(f); |
276 |
< |
assertTrue(f.cancel(true)); |
276 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
277 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
278 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
279 |
|
checkCancelled(f); |
280 |
< |
} |
280 |
> |
}} |
281 |
|
|
282 |
|
/** |
283 |
|
* obtrudeValue forces completion with given value |
285 |
|
public void testObtrudeValue() { |
286 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
287 |
|
checkIncomplete(f); |
288 |
< |
f.complete(one); |
288 |
> |
assertTrue(f.complete(one)); |
289 |
|
checkCompletedNormally(f, one); |
290 |
|
f.obtrudeValue(three); |
291 |
|
checkCompletedNormally(f, three); |
312 |
|
CompletableFuture<Integer> f; |
313 |
|
|
314 |
|
f = new CompletableFuture<>(); |
315 |
< |
f.complete(v1); |
315 |
> |
assertTrue(f.complete(v1)); |
316 |
|
for (int i = 0; i < 2; i++) { |
317 |
|
f.obtrudeException(ex = new CFException()); |
318 |
|
checkCompletedExceptionally(f, ex); |
332 |
|
checkCompletedExceptionally(f, ex); |
333 |
|
f.completeExceptionally(new CFException()); |
334 |
|
checkCompletedExceptionally(f, ex); |
335 |
< |
f.complete(v1); |
335 |
> |
assertFalse(f.complete(v1)); |
336 |
|
checkCompletedExceptionally(f, ex); |
337 |
|
}} |
338 |
|
|
341 |
|
*/ |
342 |
|
public void testGetNumberOfDependents() { |
343 |
|
for (ExecutionMode m : ExecutionMode.values()) |
344 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
345 |
|
{ |
346 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
347 |
|
assertEquals(0, f.getNumberOfDependents()); |
351 |
|
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
352 |
|
assertEquals(2, f.getNumberOfDependents()); |
353 |
|
assertEquals(0, h.getNumberOfDependents()); |
354 |
< |
f.complete(1); |
354 |
> |
assertTrue(f.complete(v1)); |
355 |
|
checkCompletedNormally(g, null); |
356 |
|
checkCompletedNormally(h, null); |
357 |
|
assertEquals(0, f.getNumberOfDependents()); |
368 |
|
f = new CompletableFuture<String>(); |
369 |
|
assertTrue(f.toString().contains("[Not completed]")); |
370 |
|
|
371 |
< |
f.complete("foo"); |
371 |
> |
assertTrue(f.complete("foo")); |
372 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
373 |
|
|
374 |
|
f = new CompletableFuture<String>(); |
375 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
375 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
376 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
377 |
|
|
378 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
379 |
|
f = new CompletableFuture<String>(); |
380 |
< |
f.cancel(mayInterruptIfRunning); |
380 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
381 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
382 |
|
} |
383 |
|
} |
547 |
|
} |
548 |
|
} |
549 |
|
|
526 |
– |
|
550 |
|
class CompletableFutureInc extends CheckedIntegerAction |
551 |
|
implements Function<Integer, CompletableFuture<Integer>> |
552 |
|
{ |
555 |
|
invoked(); |
556 |
|
value = x; |
557 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
558 |
< |
f.complete(inc(x)); |
558 |
> |
assertTrue(f.complete(inc(x))); |
559 |
|
return f; |
560 |
|
} |
561 |
|
} |
585 |
|
} |
586 |
|
} |
587 |
|
|
588 |
+ |
static final boolean defaultExecutorIsCommonPool |
589 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
590 |
+ |
|
591 |
|
/** |
592 |
|
* Permits the testing of parallel code for the 3 different |
593 |
|
* execution modes without copy/pasting all the test methods. |
594 |
|
*/ |
595 |
|
enum ExecutionMode { |
596 |
< |
DEFAULT { |
596 |
> |
SYNC { |
597 |
|
public void checkExecutionMode() { |
598 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
599 |
|
assertNull(ForkJoinTask.getPool()); |
669 |
|
|
670 |
|
ASYNC { |
671 |
|
public void checkExecutionMode() { |
672 |
< |
assertSame(ForkJoinPool.commonPool(), |
673 |
< |
ForkJoinTask.getPool()); |
672 |
> |
assertEquals(defaultExecutorIsCommonPool, |
673 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
674 |
|
} |
675 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
676 |
|
return CompletableFuture.runAsync(a); |
866 |
|
{ |
867 |
|
final AtomicInteger a = new AtomicInteger(0); |
868 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
869 |
< |
if (!createIncomplete) f.complete(v1); |
869 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
870 |
|
final CompletableFuture<Integer> g = f.exceptionally |
871 |
|
((Throwable t) -> { |
872 |
|
// Should not be called |
873 |
|
a.getAndIncrement(); |
874 |
|
throw new AssertionError(); |
875 |
|
}); |
876 |
< |
if (createIncomplete) f.complete(v1); |
876 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
877 |
|
|
878 |
|
checkCompletedNormally(g, v1); |
879 |
|
checkCompletedNormally(f, v1); |
894 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
895 |
|
final CompletableFuture<Integer> g = f.exceptionally |
896 |
|
((Throwable t) -> { |
897 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
897 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
898 |
|
threadAssertSame(t, ex); |
899 |
|
a.getAndIncrement(); |
900 |
|
return v1; |
907 |
|
|
908 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
909 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
884 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
910 |
|
{ |
911 |
|
final AtomicInteger a = new AtomicInteger(0); |
912 |
|
final CFException ex1 = new CFException(); |
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; |
937 |
|
{ |
938 |
|
final AtomicInteger a = new AtomicInteger(0); |
939 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
940 |
< |
if (!createIncomplete) f.complete(v1); |
940 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
941 |
|
final CompletableFuture<Integer> g = m.whenComplete |
942 |
|
(f, |
943 |
|
(Integer x, Throwable t) -> { |
946 |
|
threadAssertNull(t); |
947 |
|
a.getAndIncrement(); |
948 |
|
}); |
949 |
< |
if (createIncomplete) f.complete(v1); |
949 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
950 |
|
|
951 |
|
checkCompletedNormally(g, v1); |
952 |
|
checkCompletedNormally(f, v1); |
960 |
|
public void testWhenComplete_exceptionalCompletion() { |
961 |
|
for (ExecutionMode m : ExecutionMode.values()) |
962 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
938 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
963 |
|
{ |
964 |
|
final AtomicInteger a = new AtomicInteger(0); |
965 |
|
final CFException ex = new CFException(); |
1019 |
|
final AtomicInteger a = new AtomicInteger(0); |
1020 |
|
final CFException ex = new CFException(); |
1021 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1022 |
< |
if (!createIncomplete) f.complete(v1); |
1022 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1023 |
|
final CompletableFuture<Integer> g = m.whenComplete |
1024 |
|
(f, |
1025 |
|
(Integer x, Throwable t) -> { |
1029 |
|
a.getAndIncrement(); |
1030 |
|
throw ex; |
1031 |
|
}); |
1032 |
< |
if (createIncomplete) f.complete(v1); |
1032 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1033 |
|
|
1034 |
|
checkCompletedWithWrappedException(g, ex); |
1035 |
|
checkCompletedNormally(f, v1); |
1044 |
|
public void testWhenComplete_actionFailedSourceFailed() { |
1045 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1046 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1023 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1047 |
|
{ |
1048 |
|
final AtomicInteger a = new AtomicInteger(0); |
1049 |
|
final CFException ex1 = new CFException(); |
1078 |
|
{ |
1079 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1080 |
|
final AtomicInteger a = new AtomicInteger(0); |
1081 |
< |
if (!createIncomplete) f.complete(v1); |
1081 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1082 |
|
final CompletableFuture<Integer> g = m.handle |
1083 |
|
(f, |
1084 |
|
(Integer x, Throwable t) -> { |
1088 |
|
a.getAndIncrement(); |
1089 |
|
return inc(v1); |
1090 |
|
}); |
1091 |
< |
if (createIncomplete) f.complete(v1); |
1091 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1092 |
|
|
1093 |
|
checkCompletedNormally(g, inc(v1)); |
1094 |
|
checkCompletedNormally(f, v1); |
1189 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1190 |
|
final AtomicInteger a = new AtomicInteger(0); |
1191 |
|
final CFException ex = new CFException(); |
1192 |
< |
if (!createIncomplete) f.complete(v1); |
1192 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1193 |
|
final CompletableFuture<Integer> g = m.handle |
1194 |
|
(f, |
1195 |
|
(Integer x, Throwable t) -> { |
1199 |
|
a.getAndIncrement(); |
1200 |
|
throw ex; |
1201 |
|
}); |
1202 |
< |
if (createIncomplete) f.complete(v1); |
1202 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1203 |
|
|
1204 |
|
checkCompletedWithWrappedException(g, ex); |
1205 |
|
checkCompletedNormally(f, v1); |
1280 |
|
*/ |
1281 |
|
public void testThenRun_normalCompletion() { |
1282 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1260 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1283 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1284 |
|
{ |
1285 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1286 |
< |
final Noop r = new Noop(m); |
1287 |
< |
if (!createIncomplete) f.complete(v1); |
1266 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1267 |
< |
if (createIncomplete) { |
1268 |
< |
checkIncomplete(g); |
1269 |
< |
f.complete(v1); |
1270 |
< |
} |
1286 |
> |
final Noop[] rs = new Noop[6]; |
1287 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1288 |
|
|
1289 |
< |
checkCompletedNormally(g, null); |
1289 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1290 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1291 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1292 |
> |
checkIncomplete(h0); |
1293 |
> |
checkIncomplete(h1); |
1294 |
> |
checkIncomplete(h2); |
1295 |
> |
assertTrue(f.complete(v1)); |
1296 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1297 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1298 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1299 |
> |
|
1300 |
> |
checkCompletedNormally(h0, null); |
1301 |
> |
checkCompletedNormally(h1, null); |
1302 |
> |
checkCompletedNormally(h2, null); |
1303 |
> |
checkCompletedNormally(h3, null); |
1304 |
> |
checkCompletedNormally(h4, null); |
1305 |
> |
checkCompletedNormally(h5, null); |
1306 |
|
checkCompletedNormally(f, v1); |
1307 |
< |
r.assertInvoked(); |
1307 |
> |
for (Noop r : rs) r.assertInvoked(); |
1308 |
|
}} |
1309 |
|
|
1310 |
|
/** |
1313 |
|
*/ |
1314 |
|
public void testThenRun_exceptionalCompletion() { |
1315 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1283 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1316 |
|
{ |
1317 |
|
final CFException ex = new CFException(); |
1318 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1319 |
< |
final Noop r = new Noop(m); |
1320 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1289 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1290 |
< |
if (createIncomplete) { |
1291 |
< |
checkIncomplete(g); |
1292 |
< |
f.completeExceptionally(ex); |
1293 |
< |
} |
1319 |
> |
final Noop[] rs = new Noop[6]; |
1320 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1321 |
|
|
1322 |
< |
checkCompletedWithWrappedException(g, ex); |
1322 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1323 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1324 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1325 |
> |
checkIncomplete(h0); |
1326 |
> |
checkIncomplete(h1); |
1327 |
> |
checkIncomplete(h2); |
1328 |
> |
assertTrue(f.completeExceptionally(ex)); |
1329 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1330 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1331 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1332 |
> |
|
1333 |
> |
checkCompletedWithWrappedException(h0, ex); |
1334 |
> |
checkCompletedWithWrappedException(h1, ex); |
1335 |
> |
checkCompletedWithWrappedException(h2, ex); |
1336 |
> |
checkCompletedWithWrappedException(h3, ex); |
1337 |
> |
checkCompletedWithWrappedException(h4, ex); |
1338 |
> |
checkCompletedWithWrappedException(h5, ex); |
1339 |
|
checkCompletedExceptionally(f, ex); |
1340 |
< |
r.assertNotInvoked(); |
1340 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1341 |
|
}} |
1342 |
|
|
1343 |
|
/** |
1345 |
|
*/ |
1346 |
|
public void testThenRun_sourceCancelled() { |
1347 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1305 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1348 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1349 |
|
{ |
1350 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1351 |
< |
final Noop r = new Noop(m); |
1352 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1311 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1312 |
< |
if (createIncomplete) { |
1313 |
< |
checkIncomplete(g); |
1314 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1315 |
< |
} |
1351 |
> |
final Noop[] rs = new Noop[6]; |
1352 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1353 |
|
|
1354 |
< |
checkCompletedWithWrappedCancellationException(g); |
1354 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1355 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1356 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1357 |
> |
checkIncomplete(h0); |
1358 |
> |
checkIncomplete(h1); |
1359 |
> |
checkIncomplete(h2); |
1360 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1361 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1362 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1363 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1364 |
> |
|
1365 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1366 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1367 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1368 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1369 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1370 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1371 |
|
checkCancelled(f); |
1372 |
< |
r.assertNotInvoked(); |
1372 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1373 |
|
}} |
1374 |
|
|
1375 |
|
/** |
1377 |
|
*/ |
1378 |
|
public void testThenRun_actionFailed() { |
1379 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1327 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1380 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1381 |
|
{ |
1382 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1383 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1384 |
< |
if (!createIncomplete) f.complete(v1); |
1333 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1334 |
< |
if (createIncomplete) { |
1335 |
< |
checkIncomplete(g); |
1336 |
< |
f.complete(v1); |
1337 |
< |
} |
1383 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1384 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1385 |
|
|
1386 |
< |
checkCompletedWithWrappedCFException(g); |
1386 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1387 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1388 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1389 |
> |
assertTrue(f.complete(v1)); |
1390 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1391 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1392 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1393 |
> |
|
1394 |
> |
checkCompletedWithWrappedCFException(h0); |
1395 |
> |
checkCompletedWithWrappedCFException(h1); |
1396 |
> |
checkCompletedWithWrappedCFException(h2); |
1397 |
> |
checkCompletedWithWrappedCFException(h3); |
1398 |
> |
checkCompletedWithWrappedCFException(h4); |
1399 |
> |
checkCompletedWithWrappedCFException(h5); |
1400 |
|
checkCompletedNormally(f, v1); |
1401 |
|
}} |
1402 |
|
|
1405 |
|
*/ |
1406 |
|
public void testThenApply_normalCompletion() { |
1407 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1348 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1408 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1409 |
|
{ |
1410 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1411 |
< |
final IncFunction r = new IncFunction(m); |
1412 |
< |
if (!createIncomplete) f.complete(v1); |
1354 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1355 |
< |
if (createIncomplete) { |
1356 |
< |
checkIncomplete(g); |
1357 |
< |
f.complete(v1); |
1358 |
< |
} |
1411 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1412 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1413 |
|
|
1414 |
< |
checkCompletedNormally(g, inc(v1)); |
1414 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1415 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1416 |
> |
checkIncomplete(h0); |
1417 |
> |
checkIncomplete(h1); |
1418 |
> |
assertTrue(f.complete(v1)); |
1419 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1420 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1421 |
> |
|
1422 |
> |
checkCompletedNormally(h0, inc(v1)); |
1423 |
> |
checkCompletedNormally(h1, inc(v1)); |
1424 |
> |
checkCompletedNormally(h2, inc(v1)); |
1425 |
> |
checkCompletedNormally(h3, inc(v1)); |
1426 |
|
checkCompletedNormally(f, v1); |
1427 |
< |
r.assertValue(inc(v1)); |
1427 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1428 |
|
}} |
1429 |
|
|
1430 |
|
/** |
1433 |
|
*/ |
1434 |
|
public void testThenApply_exceptionalCompletion() { |
1435 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1371 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1436 |
|
{ |
1437 |
|
final CFException ex = new CFException(); |
1438 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1439 |
< |
final IncFunction r = new IncFunction(m); |
1440 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1377 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1378 |
< |
if (createIncomplete) { |
1379 |
< |
checkIncomplete(g); |
1380 |
< |
f.completeExceptionally(ex); |
1381 |
< |
} |
1439 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1440 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1441 |
|
|
1442 |
< |
checkCompletedWithWrappedException(g, ex); |
1442 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1443 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1444 |
> |
assertTrue(f.completeExceptionally(ex)); |
1445 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1446 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1447 |
> |
|
1448 |
> |
checkCompletedWithWrappedException(h0, ex); |
1449 |
> |
checkCompletedWithWrappedException(h1, ex); |
1450 |
> |
checkCompletedWithWrappedException(h2, ex); |
1451 |
> |
checkCompletedWithWrappedException(h3, ex); |
1452 |
|
checkCompletedExceptionally(f, ex); |
1453 |
< |
r.assertNotInvoked(); |
1453 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1454 |
|
}} |
1455 |
|
|
1456 |
|
/** |
1458 |
|
*/ |
1459 |
|
public void testThenApply_sourceCancelled() { |
1460 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1393 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1461 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1462 |
|
{ |
1463 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1464 |
< |
final IncFunction r = new IncFunction(m); |
1465 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1399 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1400 |
< |
if (createIncomplete) { |
1401 |
< |
checkIncomplete(g); |
1402 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1403 |
< |
} |
1464 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1465 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1466 |
|
|
1467 |
< |
checkCompletedWithWrappedCancellationException(g); |
1467 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1468 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1469 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1470 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1471 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1472 |
> |
|
1473 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1474 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1475 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1476 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1477 |
|
checkCancelled(f); |
1478 |
< |
r.assertNotInvoked(); |
1478 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1479 |
|
}} |
1480 |
|
|
1481 |
|
/** |
1483 |
|
*/ |
1484 |
|
public void testThenApply_actionFailed() { |
1485 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1415 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1486 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1487 |
|
{ |
1488 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1489 |
< |
final FailingFunction r = new FailingFunction(m); |
1490 |
< |
if (!createIncomplete) f.complete(v1); |
1421 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1422 |
< |
if (createIncomplete) { |
1423 |
< |
checkIncomplete(g); |
1424 |
< |
f.complete(v1); |
1425 |
< |
} |
1489 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1490 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1491 |
|
|
1492 |
< |
checkCompletedWithWrappedCFException(g); |
1492 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1493 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1494 |
> |
assertTrue(f.complete(v1)); |
1495 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1496 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1497 |
> |
|
1498 |
> |
checkCompletedWithWrappedCFException(h0); |
1499 |
> |
checkCompletedWithWrappedCFException(h1); |
1500 |
> |
checkCompletedWithWrappedCFException(h2); |
1501 |
> |
checkCompletedWithWrappedCFException(h3); |
1502 |
|
checkCompletedNormally(f, v1); |
1503 |
|
}} |
1504 |
|
|
1507 |
|
*/ |
1508 |
|
public void testThenAccept_normalCompletion() { |
1509 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1436 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1510 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1511 |
|
{ |
1512 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1513 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1514 |
< |
if (!createIncomplete) f.complete(v1); |
1442 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1443 |
< |
if (createIncomplete) { |
1444 |
< |
checkIncomplete(g); |
1445 |
< |
f.complete(v1); |
1446 |
< |
} |
1513 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1514 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1515 |
|
|
1516 |
< |
checkCompletedNormally(g, null); |
1517 |
< |
r.assertValue(v1); |
1516 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1517 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1518 |
> |
checkIncomplete(h0); |
1519 |
> |
checkIncomplete(h1); |
1520 |
> |
assertTrue(f.complete(v1)); |
1521 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1522 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1523 |
> |
|
1524 |
> |
checkCompletedNormally(h0, null); |
1525 |
> |
checkCompletedNormally(h1, null); |
1526 |
> |
checkCompletedNormally(h2, null); |
1527 |
> |
checkCompletedNormally(h3, null); |
1528 |
|
checkCompletedNormally(f, v1); |
1529 |
+ |
for (NoopConsumer r : rs) r.assertValue(v1); |
1530 |
|
}} |
1531 |
|
|
1532 |
|
/** |
1535 |
|
*/ |
1536 |
|
public void testThenAccept_exceptionalCompletion() { |
1537 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1459 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1538 |
|
{ |
1539 |
|
final CFException ex = new CFException(); |
1540 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1541 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1542 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1465 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1466 |
< |
if (createIncomplete) { |
1467 |
< |
checkIncomplete(g); |
1468 |
< |
f.completeExceptionally(ex); |
1469 |
< |
} |
1541 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1542 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1543 |
|
|
1544 |
< |
checkCompletedWithWrappedException(g, ex); |
1544 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1545 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1546 |
> |
assertTrue(f.completeExceptionally(ex)); |
1547 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1548 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1549 |
> |
|
1550 |
> |
checkCompletedWithWrappedException(h0, ex); |
1551 |
> |
checkCompletedWithWrappedException(h1, ex); |
1552 |
> |
checkCompletedWithWrappedException(h2, ex); |
1553 |
> |
checkCompletedWithWrappedException(h3, ex); |
1554 |
|
checkCompletedExceptionally(f, ex); |
1555 |
< |
r.assertNotInvoked(); |
1555 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1556 |
|
}} |
1557 |
|
|
1558 |
|
/** |
1560 |
|
*/ |
1561 |
|
public void testThenAccept_sourceCancelled() { |
1562 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1481 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1563 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1564 |
|
{ |
1565 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1566 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1567 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1487 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1488 |
< |
if (createIncomplete) { |
1489 |
< |
checkIncomplete(g); |
1490 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1491 |
< |
} |
1566 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1567 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1568 |
|
|
1569 |
< |
checkCompletedWithWrappedCancellationException(g); |
1569 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1570 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1571 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1572 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1573 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1574 |
> |
|
1575 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1576 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1577 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1578 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1579 |
|
checkCancelled(f); |
1580 |
< |
r.assertNotInvoked(); |
1580 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1581 |
|
}} |
1582 |
|
|
1583 |
|
/** |
1585 |
|
*/ |
1586 |
|
public void testThenAccept_actionFailed() { |
1587 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1503 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1588 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1589 |
|
{ |
1590 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1591 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1592 |
< |
if (!createIncomplete) f.complete(v1); |
1509 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1510 |
< |
if (createIncomplete) { |
1511 |
< |
checkIncomplete(g); |
1512 |
< |
f.complete(v1); |
1513 |
< |
} |
1591 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1592 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1593 |
|
|
1594 |
< |
checkCompletedWithWrappedCFException(g); |
1594 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1595 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1596 |
> |
assertTrue(f.complete(v1)); |
1597 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1598 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1599 |
> |
|
1600 |
> |
checkCompletedWithWrappedCFException(h0); |
1601 |
> |
checkCompletedWithWrappedCFException(h1); |
1602 |
> |
checkCompletedWithWrappedCFException(h2); |
1603 |
> |
checkCompletedWithWrappedCFException(h3); |
1604 |
|
checkCompletedNormally(f, v1); |
1605 |
|
}} |
1606 |
|
|
1610 |
|
*/ |
1611 |
|
public void testThenCombine_normalCompletion() { |
1612 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1525 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1613 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1614 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1615 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1616 |
|
{ |
1617 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1618 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1619 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1619 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1620 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1621 |
|
|
1622 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1623 |
< |
if (!createIncomplete) |
1624 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1625 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1626 |
< |
if (createIncomplete) { |
1627 |
< |
checkIncomplete(h); |
1628 |
< |
r.assertNotInvoked(); |
1629 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1630 |
< |
} |
1622 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1623 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1624 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1625 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1626 |
> |
|
1627 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1628 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1629 |
> |
assertTrue(fst.complete(w1)); |
1630 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1631 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1632 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1633 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1634 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1635 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1636 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1637 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1638 |
> |
assertTrue(snd.complete(w2)); |
1639 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1640 |
> |
|
1641 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1642 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1643 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1644 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1645 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1646 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1647 |
|
|
1544 |
– |
checkCompletedNormally(h, subtract(v1, v2)); |
1648 |
|
checkCompletedNormally(f, v1); |
1649 |
|
checkCompletedNormally(g, v2); |
1547 |
– |
r.assertValue(subtract(v1, v2)); |
1650 |
|
}} |
1651 |
|
|
1652 |
|
/** |
1653 |
|
* thenCombine result completes exceptionally after exceptional |
1654 |
|
* completion of either source |
1655 |
|
*/ |
1656 |
< |
public void testThenCombine_exceptionalCompletion() { |
1656 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1657 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1556 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1658 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1659 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1660 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1661 |
|
{ |
1662 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1663 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1664 |
|
final CFException ex = new CFException(); |
1665 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1666 |
< |
|
1667 |
< |
(fFirst ? f : g).complete(v1); |
1668 |
< |
if (!createIncomplete) |
1669 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1670 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1671 |
< |
if (createIncomplete) { |
1672 |
< |
checkIncomplete(h); |
1673 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1674 |
< |
} |
1665 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1666 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1667 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1668 |
> |
|
1669 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1670 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1671 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1672 |
> |
() -> fst.completeExceptionally(ex) : |
1673 |
> |
() -> fst.complete(v1); |
1674 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1675 |
> |
() -> snd.complete(v1) : |
1676 |
> |
() -> snd.completeExceptionally(ex); |
1677 |
> |
|
1678 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1679 |
> |
assertTrue(complete1.call()); |
1680 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1681 |
> |
checkIncomplete(h1); |
1682 |
> |
checkIncomplete(h2); |
1683 |
> |
assertTrue(complete2.call()); |
1684 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1685 |
|
|
1686 |
< |
checkCompletedWithWrappedException(h, ex); |
1687 |
< |
r.assertNotInvoked(); |
1688 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1689 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1686 |
> |
checkCompletedWithWrappedException(h1, ex); |
1687 |
> |
checkCompletedWithWrappedException(h2, ex); |
1688 |
> |
checkCompletedWithWrappedException(h3, ex); |
1689 |
> |
r1.assertNotInvoked(); |
1690 |
> |
r2.assertNotInvoked(); |
1691 |
> |
r3.assertNotInvoked(); |
1692 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1693 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1694 |
|
}} |
1695 |
|
|
1696 |
|
/** |
1697 |
|
* thenCombine result completes exceptionally if either source cancelled |
1698 |
|
*/ |
1699 |
< |
public void testThenCombine_sourceCancelled() { |
1699 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1700 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1701 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1586 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1702 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1703 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1704 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1705 |
|
{ |
1706 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1707 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1708 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1708 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1709 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1710 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1711 |
|
|
1712 |
< |
(fFirst ? f : g).complete(v1); |
1713 |
< |
if (!createIncomplete) |
1714 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1715 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1716 |
< |
if (createIncomplete) { |
1717 |
< |
checkIncomplete(h); |
1718 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1719 |
< |
} |
1712 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1713 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1714 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1715 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1716 |
> |
() -> fst.complete(v1); |
1717 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1718 |
> |
() -> snd.complete(v1) : |
1719 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1720 |
|
|
1721 |
< |
checkCompletedWithWrappedCancellationException(h); |
1722 |
< |
checkCancelled(!fFirst ? f : g); |
1723 |
< |
r.assertNotInvoked(); |
1724 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1721 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1722 |
> |
assertTrue(complete1.call()); |
1723 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1724 |
> |
checkIncomplete(h1); |
1725 |
> |
checkIncomplete(h2); |
1726 |
> |
assertTrue(complete2.call()); |
1727 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1728 |
> |
|
1729 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1730 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1731 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1732 |
> |
r1.assertNotInvoked(); |
1733 |
> |
r2.assertNotInvoked(); |
1734 |
> |
r3.assertNotInvoked(); |
1735 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1736 |
> |
checkCancelled(failFirst ? fst : snd); |
1737 |
|
}} |
1738 |
|
|
1739 |
|
/** |
1747 |
|
{ |
1748 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1749 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1750 |
< |
final FailingBiFunction r = new FailingBiFunction(m); |
1751 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1750 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1751 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1752 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1753 |
> |
|
1754 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1755 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1756 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1757 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1758 |
> |
|
1759 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1760 |
> |
assertTrue(fst.complete(w1)); |
1761 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1762 |
> |
assertTrue(snd.complete(w2)); |
1763 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1764 |
|
|
1765 |
< |
if (fFirst) { |
1766 |
< |
f.complete(v1); |
1767 |
< |
g.complete(v2); |
1768 |
< |
} else { |
1769 |
< |
g.complete(v2); |
1770 |
< |
f.complete(v1); |
1629 |
< |
} |
1630 |
< |
|
1631 |
< |
checkCompletedWithWrappedCFException(h); |
1765 |
> |
checkCompletedWithWrappedCFException(h1); |
1766 |
> |
checkCompletedWithWrappedCFException(h2); |
1767 |
> |
checkCompletedWithWrappedCFException(h3); |
1768 |
> |
r1.assertInvoked(); |
1769 |
> |
r2.assertInvoked(); |
1770 |
> |
r3.assertInvoked(); |
1771 |
|
checkCompletedNormally(f, v1); |
1772 |
|
checkCompletedNormally(g, v2); |
1773 |
|
}} |
1778 |
|
*/ |
1779 |
|
public void testThenAcceptBoth_normalCompletion() { |
1780 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1642 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1781 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1782 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1783 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1784 |
|
{ |
1785 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1786 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1787 |
< |
final SubtractAction r = new SubtractAction(m); |
1788 |
< |
|
1789 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1790 |
< |
if (!createIncomplete) |
1791 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1792 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1793 |
< |
if (createIncomplete) { |
1794 |
< |
checkIncomplete(h); |
1795 |
< |
r.assertNotInvoked(); |
1796 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1797 |
< |
} |
1787 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1788 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1789 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1790 |
> |
|
1791 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1792 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1793 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1794 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1795 |
> |
|
1796 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1797 |
> |
assertTrue(fst.complete(w1)); |
1798 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1799 |
> |
checkIncomplete(h1); |
1800 |
> |
checkIncomplete(h2); |
1801 |
> |
r1.assertNotInvoked(); |
1802 |
> |
r2.assertNotInvoked(); |
1803 |
> |
assertTrue(snd.complete(w2)); |
1804 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1805 |
|
|
1806 |
< |
checkCompletedNormally(h, null); |
1807 |
< |
r.assertValue(subtract(v1, v2)); |
1806 |
> |
checkCompletedNormally(h1, null); |
1807 |
> |
checkCompletedNormally(h2, null); |
1808 |
> |
checkCompletedNormally(h3, null); |
1809 |
> |
r1.assertValue(subtract(v1, v2)); |
1810 |
> |
r2.assertValue(subtract(v1, v2)); |
1811 |
> |
r3.assertValue(subtract(v1, v2)); |
1812 |
|
checkCompletedNormally(f, v1); |
1813 |
|
checkCompletedNormally(g, v2); |
1814 |
|
}} |
1817 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1818 |
|
* completion of either source |
1819 |
|
*/ |
1820 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1820 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1821 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1673 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1822 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1823 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1824 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1825 |
|
{ |
1826 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1827 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1828 |
|
final CFException ex = new CFException(); |
1829 |
< |
final SubtractAction r = new SubtractAction(m); |
1830 |
< |
|
1831 |
< |
(fFirst ? f : g).complete(v1); |
1832 |
< |
if (!createIncomplete) |
1833 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1834 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1835 |
< |
if (createIncomplete) { |
1836 |
< |
checkIncomplete(h); |
1837 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1838 |
< |
} |
1829 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1830 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1831 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1832 |
> |
|
1833 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1834 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1835 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1836 |
> |
() -> fst.completeExceptionally(ex) : |
1837 |
> |
() -> fst.complete(v1); |
1838 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1839 |
> |
() -> snd.complete(v1) : |
1840 |
> |
() -> snd.completeExceptionally(ex); |
1841 |
> |
|
1842 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1843 |
> |
assertTrue(complete1.call()); |
1844 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1845 |
> |
checkIncomplete(h1); |
1846 |
> |
checkIncomplete(h2); |
1847 |
> |
assertTrue(complete2.call()); |
1848 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1849 |
|
|
1850 |
< |
checkCompletedWithWrappedException(h, ex); |
1851 |
< |
r.assertNotInvoked(); |
1852 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1853 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1850 |
> |
checkCompletedWithWrappedException(h1, ex); |
1851 |
> |
checkCompletedWithWrappedException(h2, ex); |
1852 |
> |
checkCompletedWithWrappedException(h3, ex); |
1853 |
> |
r1.assertNotInvoked(); |
1854 |
> |
r2.assertNotInvoked(); |
1855 |
> |
r3.assertNotInvoked(); |
1856 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1857 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1858 |
|
}} |
1859 |
|
|
1860 |
|
/** |
1861 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1862 |
|
*/ |
1863 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1863 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1864 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1865 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1703 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1866 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1867 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1868 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1869 |
|
{ |
1870 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1871 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1872 |
< |
final SubtractAction r = new SubtractAction(m); |
1872 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1873 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1874 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1875 |
|
|
1876 |
< |
(fFirst ? f : g).complete(v1); |
1877 |
< |
if (!createIncomplete) |
1878 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1879 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1880 |
< |
if (createIncomplete) { |
1881 |
< |
checkIncomplete(h); |
1882 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1883 |
< |
} |
1876 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1877 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1878 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1879 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1880 |
> |
() -> fst.complete(v1); |
1881 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1882 |
> |
() -> snd.complete(v1) : |
1883 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1884 |
|
|
1885 |
< |
checkCompletedWithWrappedCancellationException(h); |
1886 |
< |
checkCancelled(!fFirst ? f : g); |
1887 |
< |
r.assertNotInvoked(); |
1888 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1885 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1886 |
> |
assertTrue(complete1.call()); |
1887 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1888 |
> |
checkIncomplete(h1); |
1889 |
> |
checkIncomplete(h2); |
1890 |
> |
assertTrue(complete2.call()); |
1891 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1892 |
> |
|
1893 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1894 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1895 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1896 |
> |
r1.assertNotInvoked(); |
1897 |
> |
r2.assertNotInvoked(); |
1898 |
> |
r3.assertNotInvoked(); |
1899 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1900 |
> |
checkCancelled(failFirst ? fst : snd); |
1901 |
|
}} |
1902 |
|
|
1903 |
|
/** |
1911 |
|
{ |
1912 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1913 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1914 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1915 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1916 |
< |
|
1917 |
< |
if (fFirst) { |
1918 |
< |
f.complete(v1); |
1919 |
< |
g.complete(v2); |
1920 |
< |
} else { |
1921 |
< |
g.complete(v2); |
1922 |
< |
f.complete(v1); |
1923 |
< |
} |
1914 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1915 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1916 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1917 |
> |
|
1918 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1919 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1920 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1921 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1922 |
> |
|
1923 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1924 |
> |
assertTrue(fst.complete(w1)); |
1925 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1926 |
> |
assertTrue(snd.complete(w2)); |
1927 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1928 |
|
|
1929 |
< |
checkCompletedWithWrappedCFException(h); |
1929 |
> |
checkCompletedWithWrappedCFException(h1); |
1930 |
> |
checkCompletedWithWrappedCFException(h2); |
1931 |
> |
checkCompletedWithWrappedCFException(h3); |
1932 |
> |
r1.assertInvoked(); |
1933 |
> |
r2.assertInvoked(); |
1934 |
> |
r3.assertInvoked(); |
1935 |
|
checkCompletedNormally(f, v1); |
1936 |
|
checkCompletedNormally(g, v2); |
1937 |
|
}} |
1942 |
|
*/ |
1943 |
|
public void testRunAfterBoth_normalCompletion() { |
1944 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1759 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1945 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1946 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1947 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1948 |
|
{ |
1949 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1950 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1951 |
< |
final Noop r = new Noop(m); |
1952 |
< |
|
1953 |
< |
if (fFirst) f.complete(v1); else g.complete(v2); |
1954 |
< |
if (!createIncomplete) |
1955 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1956 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1957 |
< |
if (createIncomplete) { |
1958 |
< |
checkIncomplete(h); |
1959 |
< |
r.assertNotInvoked(); |
1960 |
< |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1961 |
< |
} |
1951 |
> |
final Noop r1 = new Noop(m); |
1952 |
> |
final Noop r2 = new Noop(m); |
1953 |
> |
final Noop r3 = new Noop(m); |
1954 |
> |
|
1955 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1956 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1957 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1958 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1959 |
> |
|
1960 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1961 |
> |
assertTrue(fst.complete(w1)); |
1962 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1963 |
> |
checkIncomplete(h1); |
1964 |
> |
checkIncomplete(h2); |
1965 |
> |
r1.assertNotInvoked(); |
1966 |
> |
r2.assertNotInvoked(); |
1967 |
> |
assertTrue(snd.complete(w2)); |
1968 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1969 |
|
|
1970 |
< |
checkCompletedNormally(h, null); |
1971 |
< |
r.assertInvoked(); |
1970 |
> |
checkCompletedNormally(h1, null); |
1971 |
> |
checkCompletedNormally(h2, null); |
1972 |
> |
checkCompletedNormally(h3, null); |
1973 |
> |
r1.assertInvoked(); |
1974 |
> |
r2.assertInvoked(); |
1975 |
> |
r3.assertInvoked(); |
1976 |
|
checkCompletedNormally(f, v1); |
1977 |
|
checkCompletedNormally(g, v2); |
1978 |
|
}} |
1981 |
|
* runAfterBoth result completes exceptionally after exceptional |
1982 |
|
* completion of either source |
1983 |
|
*/ |
1984 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
1984 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1985 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1790 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1986 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1987 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1988 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1989 |
|
{ |
1990 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1991 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1992 |
|
final CFException ex = new CFException(); |
1993 |
< |
final Noop r = new Noop(m); |
1994 |
< |
|
1995 |
< |
(fFirst ? f : g).complete(v1); |
1996 |
< |
if (!createIncomplete) |
1997 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
1998 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1999 |
< |
if (createIncomplete) { |
2000 |
< |
checkIncomplete(h); |
2001 |
< |
(!fFirst ? f : g).completeExceptionally(ex); |
2002 |
< |
} |
1993 |
> |
final Noop r1 = new Noop(m); |
1994 |
> |
final Noop r2 = new Noop(m); |
1995 |
> |
final Noop r3 = new Noop(m); |
1996 |
> |
|
1997 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1998 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1999 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2000 |
> |
() -> fst.completeExceptionally(ex) : |
2001 |
> |
() -> fst.complete(v1); |
2002 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2003 |
> |
() -> snd.complete(v1) : |
2004 |
> |
() -> snd.completeExceptionally(ex); |
2005 |
> |
|
2006 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2007 |
> |
assertTrue(complete1.call()); |
2008 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2009 |
> |
checkIncomplete(h1); |
2010 |
> |
checkIncomplete(h2); |
2011 |
> |
assertTrue(complete2.call()); |
2012 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2013 |
|
|
2014 |
< |
checkCompletedWithWrappedException(h, ex); |
2015 |
< |
r.assertNotInvoked(); |
2016 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2017 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
2014 |
> |
checkCompletedWithWrappedException(h1, ex); |
2015 |
> |
checkCompletedWithWrappedException(h2, ex); |
2016 |
> |
checkCompletedWithWrappedException(h3, ex); |
2017 |
> |
r1.assertNotInvoked(); |
2018 |
> |
r2.assertNotInvoked(); |
2019 |
> |
r3.assertNotInvoked(); |
2020 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2021 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2022 |
|
}} |
2023 |
|
|
2024 |
|
/** |
2025 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
2026 |
|
*/ |
2027 |
< |
public void testRunAfterBoth_sourceCancelled() { |
2027 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2028 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2029 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1820 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2030 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2031 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2032 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2033 |
|
{ |
2034 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2035 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2036 |
< |
final Noop r = new Noop(m); |
2036 |
> |
final Noop r1 = new Noop(m); |
2037 |
> |
final Noop r2 = new Noop(m); |
2038 |
> |
final Noop r3 = new Noop(m); |
2039 |
|
|
2040 |
< |
(fFirst ? f : g).complete(v1); |
2041 |
< |
if (!createIncomplete) |
2042 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2043 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2044 |
< |
if (createIncomplete) { |
2045 |
< |
checkIncomplete(h); |
2046 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2047 |
< |
} |
2040 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2041 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2042 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2043 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2044 |
> |
() -> fst.complete(v1); |
2045 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2046 |
> |
() -> snd.complete(v1) : |
2047 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2048 |
|
|
2049 |
< |
checkCompletedWithWrappedCancellationException(h); |
2050 |
< |
checkCancelled(!fFirst ? f : g); |
2051 |
< |
r.assertNotInvoked(); |
2052 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2049 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2050 |
> |
assertTrue(complete1.call()); |
2051 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2052 |
> |
checkIncomplete(h1); |
2053 |
> |
checkIncomplete(h2); |
2054 |
> |
assertTrue(complete2.call()); |
2055 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2056 |
> |
|
2057 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2058 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2059 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2060 |
> |
r1.assertNotInvoked(); |
2061 |
> |
r2.assertNotInvoked(); |
2062 |
> |
r3.assertNotInvoked(); |
2063 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2064 |
> |
checkCancelled(failFirst ? fst : snd); |
2065 |
|
}} |
2066 |
|
|
2067 |
|
/** |
2077 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2078 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
2079 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
2080 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
2081 |
|
|
2082 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2083 |
< |
if (fFirst) { |
2084 |
< |
f.complete(v1); |
2085 |
< |
g.complete(v2); |
2086 |
< |
} else { |
2087 |
< |
g.complete(v2); |
2088 |
< |
f.complete(v1); |
2089 |
< |
} |
2090 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2082 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2083 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2084 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2085 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2086 |
> |
|
2087 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2088 |
> |
assertTrue(fst.complete(w1)); |
2089 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2090 |
> |
assertTrue(snd.complete(w2)); |
2091 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2092 |
|
|
2093 |
|
checkCompletedWithWrappedCFException(h1); |
2094 |
|
checkCompletedWithWrappedCFException(h2); |
2095 |
+ |
checkCompletedWithWrappedCFException(h3); |
2096 |
+ |
r1.assertInvoked(); |
2097 |
+ |
r2.assertInvoked(); |
2098 |
+ |
r3.assertInvoked(); |
2099 |
|
checkCompletedNormally(f, v1); |
2100 |
|
checkCompletedNormally(g, v2); |
2101 |
|
}} |
2218 |
|
|
2219 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2220 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2221 |
< |
if (fFirst) { |
2222 |
< |
f.complete(v1); |
1993 |
< |
g.completeExceptionally(ex); |
1994 |
< |
} else { |
1995 |
< |
g.completeExceptionally(ex); |
1996 |
< |
f.complete(v1); |
1997 |
< |
} |
2221 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2222 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2223 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2224 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2225 |
|
|
2325 |
|
|
2326 |
|
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2327 |
|
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2328 |
< |
if (fFirst) { |
2329 |
< |
f.complete(v1); |
2105 |
< |
g.cancel(mayInterruptIfRunning); |
2106 |
< |
} else { |
2107 |
< |
g.cancel(mayInterruptIfRunning); |
2108 |
< |
f.complete(v1); |
2109 |
< |
} |
2328 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2329 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2330 |
|
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2331 |
|
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2332 |
|
|
2528 |
|
|
2529 |
|
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2530 |
|
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2531 |
< |
if (fFirst) { |
2532 |
< |
f.complete(v1); |
2313 |
< |
g.completeExceptionally(ex); |
2314 |
< |
} else { |
2315 |
< |
g.completeExceptionally(ex); |
2316 |
< |
f.complete(v1); |
2317 |
< |
} |
2531 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2532 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2533 |
|
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2534 |
|
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2535 |
|
|
2732 |
|
checkIncomplete(h1); |
2733 |
|
rs[0].assertNotInvoked(); |
2734 |
|
rs[1].assertNotInvoked(); |
2735 |
< |
f.completeExceptionally(ex); |
2735 |
> |
assertTrue(f.completeExceptionally(ex)); |
2736 |
|
checkCompletedWithWrappedException(h0, ex); |
2737 |
|
checkCompletedWithWrappedException(h1, ex); |
2738 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2740 |
|
checkCompletedWithWrappedException(h2, ex); |
2741 |
|
checkCompletedWithWrappedException(h3, ex); |
2742 |
|
|
2743 |
< |
g.complete(v1); |
2743 |
> |
assertTrue(g.complete(v1)); |
2744 |
|
|
2745 |
|
// unspecified behavior - both source completions available |
2746 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2783 |
|
|
2784 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2785 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2786 |
< |
if (fFirst) { |
2787 |
< |
f.complete(v1); |
2573 |
< |
g.completeExceptionally(ex); |
2574 |
< |
} else { |
2575 |
< |
g.completeExceptionally(ex); |
2576 |
< |
f.complete(v1); |
2577 |
< |
} |
2786 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2787 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2788 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2789 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2790 |
|
|
2849 |
|
checkCompletedWithWrappedCancellationException(h2); |
2850 |
|
checkCompletedWithWrappedCancellationException(h3); |
2851 |
|
|
2852 |
< |
g.complete(v1); |
2852 |
> |
assertTrue(g.complete(v1)); |
2853 |
|
|
2854 |
|
// unspecified behavior - both source completions available |
2855 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2893 |
|
|
2894 |
|
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2895 |
|
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2896 |
< |
f.complete(v1); |
2896 |
> |
assertTrue(f.complete(v1)); |
2897 |
|
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2898 |
|
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2899 |
|
checkCompletedWithWrappedCFException(h0); |
2901 |
|
checkCompletedWithWrappedCFException(h2); |
2902 |
|
checkCompletedWithWrappedCFException(h3); |
2903 |
|
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2904 |
< |
g.complete(v2); |
2904 |
> |
assertTrue(g.complete(v2)); |
2905 |
|
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2906 |
|
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2907 |
|
checkCompletedWithWrappedCFException(h4); |
2922 |
|
{ |
2923 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2924 |
|
final CompletableFutureInc r = new CompletableFutureInc(m); |
2925 |
< |
if (!createIncomplete) f.complete(v1); |
2925 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2926 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2927 |
< |
if (createIncomplete) f.complete(v1); |
2927 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2928 |
|
|
2929 |
|
checkCompletedNormally(g, inc(v1)); |
2930 |
|
checkCompletedNormally(f, v1); |
2962 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2963 |
|
final FailingCompletableFutureFunction r |
2964 |
|
= new FailingCompletableFutureFunction(m); |
2965 |
< |
if (!createIncomplete) f.complete(v1); |
2965 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2966 |
|
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2967 |
< |
if (createIncomplete) f.complete(v1); |
2967 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2968 |
|
|
2969 |
|
checkCompletedWithWrappedCFException(g); |
2970 |
|
checkCompletedNormally(f, v1); |
2991 |
|
checkCancelled(f); |
2992 |
|
}} |
2993 |
|
|
2994 |
+ |
/** |
2995 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
2996 |
+ |
*/ |
2997 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
2998 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
2999 |
+ |
for (int order = 0; order < 6; order++) |
3000 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3001 |
+ |
{ |
3002 |
+ |
final CFException ex = new CFException(); |
3003 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3004 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3005 |
+ |
final CompletableFuture<Integer> h; |
3006 |
+ |
// Test all permutations of orders |
3007 |
+ |
switch (order) { |
3008 |
+ |
case 0: |
3009 |
+ |
assertTrue(f.complete(v1)); |
3010 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3011 |
+ |
h = m.thenCompose(f, (x -> g)); |
3012 |
+ |
break; |
3013 |
+ |
case 1: |
3014 |
+ |
assertTrue(f.complete(v1)); |
3015 |
+ |
h = m.thenCompose(f, (x -> g)); |
3016 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3017 |
+ |
break; |
3018 |
+ |
case 2: |
3019 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3020 |
+ |
assertTrue(f.complete(v1)); |
3021 |
+ |
h = m.thenCompose(f, (x -> g)); |
3022 |
+ |
break; |
3023 |
+ |
case 3: |
3024 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3025 |
+ |
h = m.thenCompose(f, (x -> g)); |
3026 |
+ |
assertTrue(f.complete(v1)); |
3027 |
+ |
break; |
3028 |
+ |
case 4: |
3029 |
+ |
h = m.thenCompose(f, (x -> g)); |
3030 |
+ |
assertTrue(f.complete(v1)); |
3031 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3032 |
+ |
break; |
3033 |
+ |
case 5: |
3034 |
+ |
h = m.thenCompose(f, (x -> g)); |
3035 |
+ |
assertTrue(f.complete(v1)); |
3036 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3037 |
+ |
break; |
3038 |
+ |
default: throw new AssertionError(); |
3039 |
+ |
} |
3040 |
+ |
|
3041 |
+ |
checkCompletedExceptionally(g, ex); |
3042 |
+ |
checkCompletedWithWrappedException(h, ex); |
3043 |
+ |
checkCompletedNormally(f, v1); |
3044 |
+ |
}} |
3045 |
+ |
|
3046 |
|
// other static methods |
3047 |
|
|
3048 |
|
/** |
3059 |
|
* when all components complete normally |
3060 |
|
*/ |
3061 |
|
public void testAllOf_normal() throws Exception { |
3062 |
< |
for (int k = 1; k < 20; ++k) { |
3062 |
> |
for (int k = 1; k < 10; k++) { |
3063 |
|
CompletableFuture<Integer>[] fs |
3064 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3065 |
< |
for (int i = 0; i < k; ++i) |
3065 |
> |
for (int i = 0; i < k; i++) |
3066 |
|
fs[i] = new CompletableFuture<>(); |
3067 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3068 |
< |
for (int i = 0; i < k; ++i) { |
3068 |
> |
for (int i = 0; i < k; i++) { |
3069 |
|
checkIncomplete(f); |
3070 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3071 |
|
fs[i].complete(one); |
3076 |
|
} |
3077 |
|
|
3078 |
|
public void testAllOf_backwards() throws Exception { |
3079 |
< |
for (int k = 1; k < 20; ++k) { |
3079 |
> |
for (int k = 1; k < 10; k++) { |
3080 |
|
CompletableFuture<Integer>[] fs |
3081 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3082 |
< |
for (int i = 0; i < k; ++i) |
3082 |
> |
for (int i = 0; i < k; i++) |
3083 |
|
fs[i] = new CompletableFuture<>(); |
3084 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3085 |
|
for (int i = k - 1; i >= 0; i--) { |
3092 |
|
} |
3093 |
|
} |
3094 |
|
|
3095 |
+ |
public void testAllOf_exceptional() throws Exception { |
3096 |
+ |
for (int k = 1; k < 10; k++) { |
3097 |
+ |
CompletableFuture<Integer>[] fs |
3098 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3099 |
+ |
CFException ex = new CFException(); |
3100 |
+ |
for (int i = 0; i < k; i++) |
3101 |
+ |
fs[i] = new CompletableFuture<>(); |
3102 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3103 |
+ |
for (int i = 0; i < k; i++) { |
3104 |
+ |
checkIncomplete(f); |
3105 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3106 |
+ |
if (i != k/2) { |
3107 |
+ |
fs[i].complete(i); |
3108 |
+ |
checkCompletedNormally(fs[i], i); |
3109 |
+ |
} else { |
3110 |
+ |
fs[i].completeExceptionally(ex); |
3111 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3112 |
+ |
} |
3113 |
+ |
} |
3114 |
+ |
checkCompletedWithWrappedException(f, ex); |
3115 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3116 |
+ |
} |
3117 |
+ |
} |
3118 |
+ |
|
3119 |
|
/** |
3120 |
|
* anyOf(no component futures) returns an incomplete future |
3121 |
|
*/ |
3122 |
|
public void testAnyOf_empty() throws Exception { |
3123 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3124 |
+ |
{ |
3125 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3126 |
|
checkIncomplete(f); |
3127 |
< |
} |
3127 |
> |
|
3128 |
> |
f.complete(v1); |
3129 |
> |
checkCompletedNormally(f, v1); |
3130 |
> |
}} |
3131 |
|
|
3132 |
|
/** |
3133 |
|
* anyOf returns a future completed normally with a value when |
3134 |
|
* a component future does |
3135 |
|
*/ |
3136 |
|
public void testAnyOf_normal() throws Exception { |
3137 |
< |
for (int k = 0; k < 10; ++k) { |
3137 |
> |
for (int k = 0; k < 10; k++) { |
3138 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3139 |
< |
for (int i = 0; i < k; ++i) |
3139 |
> |
for (int i = 0; i < k; i++) |
3140 |
|
fs[i] = new CompletableFuture<>(); |
3141 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3142 |
|
checkIncomplete(f); |
3143 |
< |
for (int i = 0; i < k; ++i) { |
3144 |
< |
fs[i].complete(one); |
3145 |
< |
checkCompletedNormally(f, one); |
3146 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3143 |
> |
for (int i = 0; i < k; i++) { |
3144 |
> |
fs[i].complete(i); |
3145 |
> |
checkCompletedNormally(f, 0); |
3146 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3147 |
> |
assertTrue(0 <= x && x <= i); |
3148 |
> |
} |
3149 |
> |
} |
3150 |
> |
} |
3151 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3152 |
> |
for (int k = 0; k < 10; k++) { |
3153 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3154 |
> |
for (int i = 0; i < k; i++) |
3155 |
> |
fs[i] = new CompletableFuture<>(); |
3156 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3157 |
> |
checkIncomplete(f); |
3158 |
> |
for (int i = k - 1; i >= 0; i--) { |
3159 |
> |
fs[i].complete(i); |
3160 |
> |
checkCompletedNormally(f, k - 1); |
3161 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3162 |
> |
assertTrue(i <= x && x <= k - 1); |
3163 |
|
} |
3164 |
|
} |
3165 |
|
} |
3168 |
|
* anyOf result completes exceptionally when any component does. |
3169 |
|
*/ |
3170 |
|
public void testAnyOf_exceptional() throws Exception { |
3171 |
< |
for (int k = 0; k < 10; ++k) { |
3171 |
> |
for (int k = 0; k < 10; k++) { |
3172 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3173 |
> |
CFException[] exs = new CFException[k]; |
3174 |
> |
for (int i = 0; i < k; i++) { |
3175 |
> |
fs[i] = new CompletableFuture<>(); |
3176 |
> |
exs[i] = new CFException(); |
3177 |
> |
} |
3178 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3179 |
> |
checkIncomplete(f); |
3180 |
> |
for (int i = 0; i < k; i++) { |
3181 |
> |
fs[i].completeExceptionally(exs[i]); |
3182 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3183 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3184 |
> |
} |
3185 |
> |
} |
3186 |
> |
} |
3187 |
> |
|
3188 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3189 |
> |
for (int k = 0; k < 10; k++) { |
3190 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3191 |
< |
for (int i = 0; i < k; ++i) |
3191 |
> |
CFException[] exs = new CFException[k]; |
3192 |
> |
for (int i = 0; i < k; i++) { |
3193 |
|
fs[i] = new CompletableFuture<>(); |
3194 |
+ |
exs[i] = new CFException(); |
3195 |
+ |
} |
3196 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3197 |
|
checkIncomplete(f); |
3198 |
< |
for (int i = 0; i < k; ++i) { |
3199 |
< |
fs[i].completeExceptionally(new CFException()); |
3200 |
< |
checkCompletedWithWrappedCFException(f); |
3198 |
> |
for (int i = k - 1; i >= 0; i--) { |
3199 |
> |
fs[i].completeExceptionally(exs[i]); |
3200 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3201 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3202 |
|
} |
3203 |
|
} |
3210 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3211 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3212 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2885 |
– |
CompletableFuture<?> h; |
3213 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3214 |
|
|
3215 |
|
Runnable[] throwingActions = { |
3216 |
|
() -> CompletableFuture.supplyAsync(null), |
3217 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3218 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3218 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3219 |
|
|
3220 |
|
() -> CompletableFuture.runAsync(null), |
3221 |
|
() -> CompletableFuture.runAsync(null, exec), |
3320 |
|
assertSame(f, f.toCompletableFuture()); |
3321 |
|
} |
3322 |
|
|
3323 |
+ |
//--- tests of implementation details; not part of official tck --- |
3324 |
+ |
|
3325 |
+ |
Object resultOf(CompletableFuture<?> f) { |
3326 |
+ |
try { |
3327 |
+ |
java.lang.reflect.Field resultField |
3328 |
+ |
= CompletableFuture.class.getDeclaredField("result"); |
3329 |
+ |
resultField.setAccessible(true); |
3330 |
+ |
return resultField.get(f); |
3331 |
+ |
} catch (Throwable t) { throw new AssertionError(t); } |
3332 |
+ |
} |
3333 |
+ |
|
3334 |
+ |
public void testExceptionPropagationReusesResultObject() { |
3335 |
+ |
if (!testImplementationDetails) return; |
3336 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3337 |
+ |
{ |
3338 |
+ |
final CFException ex = new CFException(); |
3339 |
+ |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3340 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3341 |
+ |
|
3342 |
+ |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3343 |
+ |
= new ArrayList<>(); |
3344 |
+ |
|
3345 |
+ |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3346 |
+ |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3347 |
+ |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3348 |
+ |
|
3349 |
+ |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3350 |
+ |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3351 |
+ |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3352 |
+ |
|
3353 |
+ |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3354 |
+ |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3355 |
+ |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3356 |
+ |
|
3357 |
+ |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3358 |
+ |
|
3359 |
+ |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3360 |
+ |
|
3361 |
+ |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3362 |
+ |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3363 |
+ |
|
3364 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3365 |
+ |
fun : funs) { |
3366 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3367 |
+ |
f.completeExceptionally(ex); |
3368 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3369 |
+ |
checkCompletedWithWrappedException(src, ex); |
3370 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3371 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3372 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3373 |
+ |
} |
3374 |
+ |
|
3375 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3376 |
+ |
fun : funs) { |
3377 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3378 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3379 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3380 |
+ |
f.completeExceptionally(ex); |
3381 |
+ |
checkCompletedWithWrappedException(src, ex); |
3382 |
+ |
checkCompletedWithWrappedException(dep, ex); |
3383 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3384 |
+ |
} |
3385 |
+ |
|
3386 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3387 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3388 |
+ |
fun : funs) { |
3389 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3390 |
+ |
f.cancel(mayInterruptIfRunning); |
3391 |
+ |
checkCancelled(f); |
3392 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3393 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3394 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3395 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3396 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3397 |
+ |
} |
3398 |
+ |
|
3399 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3400 |
+ |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3401 |
+ |
fun : funs) { |
3402 |
+ |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3403 |
+ |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3404 |
+ |
CompletableFuture<?> dep = fun.apply(src); |
3405 |
+ |
f.cancel(mayInterruptIfRunning); |
3406 |
+ |
checkCancelled(f); |
3407 |
+ |
checkCompletedWithWrappedCancellationException(src); |
3408 |
+ |
checkCompletedWithWrappedCancellationException(dep); |
3409 |
+ |
assertSame(resultOf(src), resultOf(dep)); |
3410 |
+ |
} |
3411 |
+ |
}} |
3412 |
+ |
|
3413 |
|
} |