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(); |
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); |
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; |
916 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
917 |
|
final CompletableFuture<Integer> g = f.exceptionally |
918 |
|
((Throwable t) -> { |
919 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
919 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
920 |
|
threadAssertSame(t, ex1); |
921 |
|
a.getAndIncrement(); |
922 |
|
throw ex2; |
1283 |
|
*/ |
1284 |
|
public void testThenRun_normalCompletion() { |
1285 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1267 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1286 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1287 |
|
{ |
1288 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1289 |
< |
final Noop r = new Noop(m); |
1290 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1273 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1274 |
< |
if (createIncomplete) { |
1275 |
< |
checkIncomplete(g); |
1276 |
< |
assertTrue(f.complete(v1)); |
1277 |
< |
} |
1289 |
> |
final Noop[] rs = new Noop[6]; |
1290 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1291 |
|
|
1292 |
< |
checkCompletedNormally(g, null); |
1292 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1293 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1294 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1295 |
> |
checkIncomplete(h0); |
1296 |
> |
checkIncomplete(h1); |
1297 |
> |
checkIncomplete(h2); |
1298 |
> |
assertTrue(f.complete(v1)); |
1299 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1300 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1301 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1302 |
> |
|
1303 |
> |
checkCompletedNormally(h0, null); |
1304 |
> |
checkCompletedNormally(h1, null); |
1305 |
> |
checkCompletedNormally(h2, null); |
1306 |
> |
checkCompletedNormally(h3, null); |
1307 |
> |
checkCompletedNormally(h4, null); |
1308 |
> |
checkCompletedNormally(h5, null); |
1309 |
|
checkCompletedNormally(f, v1); |
1310 |
< |
r.assertInvoked(); |
1310 |
> |
for (Noop r : rs) r.assertInvoked(); |
1311 |
|
}} |
1312 |
|
|
1313 |
|
/** |
1316 |
|
*/ |
1317 |
|
public void testThenRun_exceptionalCompletion() { |
1318 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1290 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1319 |
|
{ |
1320 |
|
final CFException ex = new CFException(); |
1321 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1322 |
< |
final Noop r = new Noop(m); |
1323 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1296 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1297 |
< |
if (createIncomplete) { |
1298 |
< |
checkIncomplete(g); |
1299 |
< |
f.completeExceptionally(ex); |
1300 |
< |
} |
1322 |
> |
final Noop[] rs = new Noop[6]; |
1323 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1324 |
|
|
1325 |
< |
checkCompletedWithWrappedException(g, ex); |
1325 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1326 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1327 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1328 |
> |
checkIncomplete(h0); |
1329 |
> |
checkIncomplete(h1); |
1330 |
> |
checkIncomplete(h2); |
1331 |
> |
assertTrue(f.completeExceptionally(ex)); |
1332 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1333 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1334 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1335 |
> |
|
1336 |
> |
checkCompletedWithWrappedException(h0, ex); |
1337 |
> |
checkCompletedWithWrappedException(h1, ex); |
1338 |
> |
checkCompletedWithWrappedException(h2, ex); |
1339 |
> |
checkCompletedWithWrappedException(h3, ex); |
1340 |
> |
checkCompletedWithWrappedException(h4, ex); |
1341 |
> |
checkCompletedWithWrappedException(h5, ex); |
1342 |
|
checkCompletedExceptionally(f, ex); |
1343 |
< |
r.assertNotInvoked(); |
1343 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1344 |
|
}} |
1345 |
|
|
1346 |
|
/** |
1348 |
|
*/ |
1349 |
|
public void testThenRun_sourceCancelled() { |
1350 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1312 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1351 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1352 |
|
{ |
1353 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1354 |
< |
final Noop r = new Noop(m); |
1355 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1318 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1319 |
< |
if (createIncomplete) { |
1320 |
< |
checkIncomplete(g); |
1321 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1322 |
< |
} |
1354 |
> |
final Noop[] rs = new Noop[6]; |
1355 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1356 |
|
|
1357 |
< |
checkCompletedWithWrappedCancellationException(g); |
1357 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1358 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1359 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1360 |
> |
checkIncomplete(h0); |
1361 |
> |
checkIncomplete(h1); |
1362 |
> |
checkIncomplete(h2); |
1363 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1364 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1365 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1366 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1367 |
> |
|
1368 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1369 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1370 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1371 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1372 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1373 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1374 |
|
checkCancelled(f); |
1375 |
< |
r.assertNotInvoked(); |
1375 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1376 |
|
}} |
1377 |
|
|
1378 |
|
/** |
1380 |
|
*/ |
1381 |
|
public void testThenRun_actionFailed() { |
1382 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1334 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1383 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1384 |
|
{ |
1385 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1386 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1387 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1340 |
< |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1341 |
< |
if (createIncomplete) { |
1342 |
< |
checkIncomplete(g); |
1343 |
< |
assertTrue(f.complete(v1)); |
1344 |
< |
} |
1386 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1387 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1388 |
|
|
1389 |
< |
checkCompletedWithWrappedCFException(g); |
1389 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1390 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1391 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1392 |
> |
assertTrue(f.complete(v1)); |
1393 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1394 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1395 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1396 |
> |
|
1397 |
> |
checkCompletedWithWrappedCFException(h0); |
1398 |
> |
checkCompletedWithWrappedCFException(h1); |
1399 |
> |
checkCompletedWithWrappedCFException(h2); |
1400 |
> |
checkCompletedWithWrappedCFException(h3); |
1401 |
> |
checkCompletedWithWrappedCFException(h4); |
1402 |
> |
checkCompletedWithWrappedCFException(h5); |
1403 |
|
checkCompletedNormally(f, v1); |
1404 |
|
}} |
1405 |
|
|
1408 |
|
*/ |
1409 |
|
public void testThenApply_normalCompletion() { |
1410 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1355 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1411 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1412 |
|
{ |
1413 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1414 |
< |
final IncFunction r = new IncFunction(m); |
1415 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1361 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1362 |
< |
if (createIncomplete) { |
1363 |
< |
checkIncomplete(g); |
1364 |
< |
assertTrue(f.complete(v1)); |
1365 |
< |
} |
1414 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1415 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1416 |
|
|
1417 |
< |
checkCompletedNormally(g, inc(v1)); |
1417 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1418 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1419 |
> |
checkIncomplete(h0); |
1420 |
> |
checkIncomplete(h1); |
1421 |
> |
assertTrue(f.complete(v1)); |
1422 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1423 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1424 |
> |
|
1425 |
> |
checkCompletedNormally(h0, inc(v1)); |
1426 |
> |
checkCompletedNormally(h1, inc(v1)); |
1427 |
> |
checkCompletedNormally(h2, inc(v1)); |
1428 |
> |
checkCompletedNormally(h3, inc(v1)); |
1429 |
|
checkCompletedNormally(f, v1); |
1430 |
< |
r.assertValue(inc(v1)); |
1430 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1431 |
|
}} |
1432 |
|
|
1433 |
|
/** |
1436 |
|
*/ |
1437 |
|
public void testThenApply_exceptionalCompletion() { |
1438 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1378 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1439 |
|
{ |
1440 |
|
final CFException ex = new CFException(); |
1441 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1442 |
< |
final IncFunction r = new IncFunction(m); |
1443 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1384 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1385 |
< |
if (createIncomplete) { |
1386 |
< |
checkIncomplete(g); |
1387 |
< |
f.completeExceptionally(ex); |
1388 |
< |
} |
1442 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1443 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1444 |
|
|
1445 |
< |
checkCompletedWithWrappedException(g, ex); |
1445 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1446 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1447 |
> |
assertTrue(f.completeExceptionally(ex)); |
1448 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1449 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1450 |
> |
|
1451 |
> |
checkCompletedWithWrappedException(h0, ex); |
1452 |
> |
checkCompletedWithWrappedException(h1, ex); |
1453 |
> |
checkCompletedWithWrappedException(h2, ex); |
1454 |
> |
checkCompletedWithWrappedException(h3, ex); |
1455 |
|
checkCompletedExceptionally(f, ex); |
1456 |
< |
r.assertNotInvoked(); |
1456 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1457 |
|
}} |
1458 |
|
|
1459 |
|
/** |
1461 |
|
*/ |
1462 |
|
public void testThenApply_sourceCancelled() { |
1463 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1400 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1464 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1465 |
|
{ |
1466 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1467 |
< |
final IncFunction r = new IncFunction(m); |
1468 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1406 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1407 |
< |
if (createIncomplete) { |
1408 |
< |
checkIncomplete(g); |
1409 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1410 |
< |
} |
1467 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1468 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1469 |
|
|
1470 |
< |
checkCompletedWithWrappedCancellationException(g); |
1470 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1471 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1472 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1473 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1474 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1475 |
> |
|
1476 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1477 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1478 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1479 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1480 |
|
checkCancelled(f); |
1481 |
< |
r.assertNotInvoked(); |
1481 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1482 |
|
}} |
1483 |
|
|
1484 |
|
/** |
1486 |
|
*/ |
1487 |
|
public void testThenApply_actionFailed() { |
1488 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1422 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1489 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1490 |
|
{ |
1491 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1492 |
< |
final FailingFunction r = new FailingFunction(m); |
1493 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1428 |
< |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1429 |
< |
if (createIncomplete) { |
1430 |
< |
checkIncomplete(g); |
1431 |
< |
assertTrue(f.complete(v1)); |
1432 |
< |
} |
1492 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1493 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1494 |
|
|
1495 |
< |
checkCompletedWithWrappedCFException(g); |
1495 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1496 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1497 |
> |
assertTrue(f.complete(v1)); |
1498 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1499 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1500 |
> |
|
1501 |
> |
checkCompletedWithWrappedCFException(h0); |
1502 |
> |
checkCompletedWithWrappedCFException(h1); |
1503 |
> |
checkCompletedWithWrappedCFException(h2); |
1504 |
> |
checkCompletedWithWrappedCFException(h3); |
1505 |
|
checkCompletedNormally(f, v1); |
1506 |
|
}} |
1507 |
|
|
1510 |
|
*/ |
1511 |
|
public void testThenAccept_normalCompletion() { |
1512 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1443 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1513 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1514 |
|
{ |
1515 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1516 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1517 |
< |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1449 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1450 |
< |
if (createIncomplete) { |
1451 |
< |
checkIncomplete(g); |
1452 |
< |
assertTrue(f.complete(v1)); |
1453 |
< |
} |
1516 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1517 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1518 |
|
|
1519 |
< |
checkCompletedNormally(g, null); |
1520 |
< |
r.assertValue(v1); |
1519 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1520 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1521 |
> |
checkIncomplete(h0); |
1522 |
> |
checkIncomplete(h1); |
1523 |
> |
assertTrue(f.complete(v1)); |
1524 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1525 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1526 |
> |
|
1527 |
> |
checkCompletedNormally(h0, null); |
1528 |
> |
checkCompletedNormally(h1, null); |
1529 |
> |
checkCompletedNormally(h2, null); |
1530 |
> |
checkCompletedNormally(h3, null); |
1531 |
|
checkCompletedNormally(f, v1); |
1532 |
+ |
for (NoopConsumer r : rs) r.assertValue(v1); |
1533 |
|
}} |
1534 |
|
|
1535 |
|
/** |
1538 |
|
*/ |
1539 |
|
public void testThenAccept_exceptionalCompletion() { |
1540 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1466 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1541 |
|
{ |
1542 |
|
final CFException ex = new CFException(); |
1543 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1544 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1545 |
< |
if (!createIncomplete) f.completeExceptionally(ex); |
1472 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1473 |
< |
if (createIncomplete) { |
1474 |
< |
checkIncomplete(g); |
1475 |
< |
f.completeExceptionally(ex); |
1476 |
< |
} |
1544 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1545 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1546 |
|
|
1547 |
< |
checkCompletedWithWrappedException(g, ex); |
1547 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1548 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1549 |
> |
assertTrue(f.completeExceptionally(ex)); |
1550 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1551 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1552 |
> |
|
1553 |
> |
checkCompletedWithWrappedException(h0, ex); |
1554 |
> |
checkCompletedWithWrappedException(h1, ex); |
1555 |
> |
checkCompletedWithWrappedException(h2, ex); |
1556 |
> |
checkCompletedWithWrappedException(h3, ex); |
1557 |
|
checkCompletedExceptionally(f, ex); |
1558 |
< |
r.assertNotInvoked(); |
1558 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1559 |
|
}} |
1560 |
|
|
1561 |
|
/** |
1563 |
|
*/ |
1564 |
|
public void testThenAccept_sourceCancelled() { |
1565 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1488 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1566 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1567 |
|
{ |
1568 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1569 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1570 |
< |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1494 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1495 |
< |
if (createIncomplete) { |
1496 |
< |
checkIncomplete(g); |
1497 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1498 |
< |
} |
1569 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1570 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1571 |
|
|
1572 |
< |
checkCompletedWithWrappedCancellationException(g); |
1572 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1573 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1574 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1575 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1576 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1577 |
> |
|
1578 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1579 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1580 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1581 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1582 |
|
checkCancelled(f); |
1583 |
< |
r.assertNotInvoked(); |
1583 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1584 |
|
}} |
1585 |
|
|
1586 |
|
/** |
1588 |
|
*/ |
1589 |
|
public void testThenAccept_actionFailed() { |
1590 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1510 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1591 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1592 |
|
{ |
1593 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1594 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1595 |
< |
if (!createIncomplete) f.complete(v1); |
1516 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1517 |
< |
if (createIncomplete) { |
1518 |
< |
checkIncomplete(g); |
1519 |
< |
f.complete(v1); |
1520 |
< |
} |
1594 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1595 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1596 |
|
|
1597 |
< |
checkCompletedWithWrappedCFException(g); |
1597 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1598 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1599 |
> |
assertTrue(f.complete(v1)); |
1600 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1601 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1602 |
> |
|
1603 |
> |
checkCompletedWithWrappedCFException(h0); |
1604 |
> |
checkCompletedWithWrappedCFException(h1); |
1605 |
> |
checkCompletedWithWrappedCFException(h2); |
1606 |
> |
checkCompletedWithWrappedCFException(h3); |
1607 |
|
checkCompletedNormally(f, v1); |
1608 |
|
}} |
1609 |
|
|
1619 |
|
{ |
1620 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1621 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1622 |
< |
final SubtractFunction r1 = new SubtractFunction(m); |
1623 |
< |
final SubtractFunction r2 = new SubtractFunction(m); |
1540 |
< |
final SubtractFunction r3 = new SubtractFunction(m); |
1622 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1623 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1624 |
|
|
1625 |
|
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1626 |
|
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1627 |
|
final Integer w1 = fFirst ? v1 : v2; |
1628 |
|
final Integer w2 = !fFirst ? v1 : v2; |
1629 |
|
|
1630 |
< |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1630 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1631 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1632 |
|
assertTrue(fst.complete(w1)); |
1633 |
< |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1634 |
< |
checkIncomplete(h1); |
1635 |
< |
checkIncomplete(h2); |
1636 |
< |
r1.assertNotInvoked(); |
1637 |
< |
r2.assertNotInvoked(); |
1633 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1634 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1635 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1636 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1637 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1638 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1639 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1640 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1641 |
|
assertTrue(snd.complete(w2)); |
1642 |
< |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1642 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1643 |
|
|
1644 |
< |
checkCompletedNormally(h1, subtract(v1, v2)); |
1644 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1645 |
|
checkCompletedNormally(h2, subtract(v1, v2)); |
1646 |
< |
checkCompletedNormally(h3, subtract(v1, v2)); |
1647 |
< |
r1.assertValue(subtract(v1, v2)); |
1648 |
< |
r2.assertValue(subtract(v1, v2)); |
1649 |
< |
r3.assertValue(subtract(v1, v2)); |
1646 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1647 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1648 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1649 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1650 |
> |
|
1651 |
|
checkCompletedNormally(f, v1); |
1652 |
|
checkCompletedNormally(g, v2); |
1653 |
|
}} |
1768 |
|
checkCompletedWithWrappedCFException(h1); |
1769 |
|
checkCompletedWithWrappedCFException(h2); |
1770 |
|
checkCompletedWithWrappedCFException(h3); |
1771 |
+ |
r1.assertInvoked(); |
1772 |
+ |
r2.assertInvoked(); |
1773 |
+ |
r3.assertInvoked(); |
1774 |
|
checkCompletedNormally(f, v1); |
1775 |
|
checkCompletedNormally(g, v2); |
1776 |
|
}} |
1781 |
|
*/ |
1782 |
|
public void testThenAcceptBoth_normalCompletion() { |
1783 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1693 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1784 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1785 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1786 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1787 |
|
{ |
1788 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1789 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1790 |
< |
final SubtractAction r = new SubtractAction(m); |
1790 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1791 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1792 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1793 |
|
|
1794 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1795 |
< |
if (!createIncomplete) |
1796 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1797 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1706 |
< |
if (createIncomplete) { |
1707 |
< |
checkIncomplete(h); |
1708 |
< |
r.assertNotInvoked(); |
1709 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1710 |
< |
} |
1794 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1795 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1796 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1797 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1798 |
|
|
1799 |
< |
checkCompletedNormally(h, null); |
1800 |
< |
r.assertValue(subtract(v1, v2)); |
1799 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1800 |
> |
assertTrue(fst.complete(w1)); |
1801 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1802 |
> |
checkIncomplete(h1); |
1803 |
> |
checkIncomplete(h2); |
1804 |
> |
r1.assertNotInvoked(); |
1805 |
> |
r2.assertNotInvoked(); |
1806 |
> |
assertTrue(snd.complete(w2)); |
1807 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1808 |
> |
|
1809 |
> |
checkCompletedNormally(h1, null); |
1810 |
> |
checkCompletedNormally(h2, null); |
1811 |
> |
checkCompletedNormally(h3, null); |
1812 |
> |
r1.assertValue(subtract(v1, v2)); |
1813 |
> |
r2.assertValue(subtract(v1, v2)); |
1814 |
> |
r3.assertValue(subtract(v1, v2)); |
1815 |
|
checkCompletedNormally(f, v1); |
1816 |
|
checkCompletedNormally(g, v2); |
1817 |
|
}} |
1820 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1821 |
|
* completion of either source |
1822 |
|
*/ |
1823 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1823 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1824 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1724 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1825 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1826 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1827 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1828 |
|
{ |
1829 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1830 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1831 |
|
final CFException ex = new CFException(); |
1832 |
< |
final SubtractAction r = new SubtractAction(m); |
1832 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1833 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1834 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1835 |
|
|
1836 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1837 |
< |
if (!createIncomplete) |
1838 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1839 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1840 |
< |
if (createIncomplete) { |
1841 |
< |
checkIncomplete(h); |
1842 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1843 |
< |
} |
1836 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1837 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1838 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1839 |
> |
() -> fst.completeExceptionally(ex) : |
1840 |
> |
() -> fst.complete(v1); |
1841 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1842 |
> |
() -> snd.complete(v1) : |
1843 |
> |
() -> snd.completeExceptionally(ex); |
1844 |
|
|
1845 |
< |
checkCompletedWithWrappedException(h, ex); |
1846 |
< |
r.assertNotInvoked(); |
1847 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1848 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1845 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1846 |
> |
assertTrue(complete1.call()); |
1847 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1848 |
> |
checkIncomplete(h1); |
1849 |
> |
checkIncomplete(h2); |
1850 |
> |
assertTrue(complete2.call()); |
1851 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1852 |
> |
|
1853 |
> |
checkCompletedWithWrappedException(h1, ex); |
1854 |
> |
checkCompletedWithWrappedException(h2, ex); |
1855 |
> |
checkCompletedWithWrappedException(h3, ex); |
1856 |
> |
r1.assertNotInvoked(); |
1857 |
> |
r2.assertNotInvoked(); |
1858 |
> |
r3.assertNotInvoked(); |
1859 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1860 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1861 |
|
}} |
1862 |
|
|
1863 |
|
/** |
1864 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1865 |
|
*/ |
1866 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1866 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1867 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1868 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1754 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1869 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1870 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1871 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1872 |
|
{ |
1873 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1874 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1875 |
< |
final SubtractAction r = new SubtractAction(m); |
1875 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1876 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1877 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1878 |
|
|
1879 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1880 |
< |
if (!createIncomplete) |
1881 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1882 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1883 |
< |
if (createIncomplete) { |
1884 |
< |
checkIncomplete(h); |
1885 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1886 |
< |
} |
1879 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1880 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1881 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1882 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1883 |
> |
() -> fst.complete(v1); |
1884 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1885 |
> |
() -> snd.complete(v1) : |
1886 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1887 |
|
|
1888 |
< |
checkCompletedWithWrappedCancellationException(h); |
1889 |
< |
checkCancelled(!fFirst ? f : g); |
1890 |
< |
r.assertNotInvoked(); |
1891 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1888 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1889 |
> |
assertTrue(complete1.call()); |
1890 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1891 |
> |
checkIncomplete(h1); |
1892 |
> |
checkIncomplete(h2); |
1893 |
> |
assertTrue(complete2.call()); |
1894 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1895 |
> |
|
1896 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1897 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1898 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1899 |
> |
r1.assertNotInvoked(); |
1900 |
> |
r2.assertNotInvoked(); |
1901 |
> |
r3.assertNotInvoked(); |
1902 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1903 |
> |
checkCancelled(failFirst ? fst : snd); |
1904 |
|
}} |
1905 |
|
|
1906 |
|
/** |
1914 |
|
{ |
1915 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1916 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1917 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1918 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1917 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1918 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1919 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1920 |
|
|
1921 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1922 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1921 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1922 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1923 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1924 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1925 |
|
|
1926 |
< |
checkCompletedWithWrappedCFException(h); |
1926 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1927 |
> |
assertTrue(fst.complete(w1)); |
1928 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1929 |
> |
assertTrue(snd.complete(w2)); |
1930 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1931 |
> |
|
1932 |
> |
checkCompletedWithWrappedCFException(h1); |
1933 |
> |
checkCompletedWithWrappedCFException(h2); |
1934 |
> |
checkCompletedWithWrappedCFException(h3); |
1935 |
> |
r1.assertInvoked(); |
1936 |
> |
r2.assertInvoked(); |
1937 |
> |
r3.assertInvoked(); |
1938 |
|
checkCompletedNormally(f, v1); |
1939 |
|
checkCompletedNormally(g, v2); |
1940 |
|
}} |
1945 |
|
*/ |
1946 |
|
public void testRunAfterBoth_normalCompletion() { |
1947 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1805 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1948 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1949 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1950 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1951 |
|
{ |
1952 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1953 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1954 |
< |
final Noop r = new Noop(m); |
1954 |
> |
final Noop r1 = new Noop(m); |
1955 |
> |
final Noop r2 = new Noop(m); |
1956 |
> |
final Noop r3 = new Noop(m); |
1957 |
|
|
1958 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1959 |
< |
if (!createIncomplete) |
1960 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1961 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1818 |
< |
if (createIncomplete) { |
1819 |
< |
checkIncomplete(h); |
1820 |
< |
r.assertNotInvoked(); |
1821 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1822 |
< |
} |
1958 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1959 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1960 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1961 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1962 |
|
|
1963 |
< |
checkCompletedNormally(h, null); |
1964 |
< |
r.assertInvoked(); |
1963 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1964 |
> |
assertTrue(fst.complete(w1)); |
1965 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1966 |
> |
checkIncomplete(h1); |
1967 |
> |
checkIncomplete(h2); |
1968 |
> |
r1.assertNotInvoked(); |
1969 |
> |
r2.assertNotInvoked(); |
1970 |
> |
assertTrue(snd.complete(w2)); |
1971 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1972 |
> |
|
1973 |
> |
checkCompletedNormally(h1, null); |
1974 |
> |
checkCompletedNormally(h2, null); |
1975 |
> |
checkCompletedNormally(h3, null); |
1976 |
> |
r1.assertInvoked(); |
1977 |
> |
r2.assertInvoked(); |
1978 |
> |
r3.assertInvoked(); |
1979 |
|
checkCompletedNormally(f, v1); |
1980 |
|
checkCompletedNormally(g, v2); |
1981 |
|
}} |
1984 |
|
* runAfterBoth result completes exceptionally after exceptional |
1985 |
|
* completion of either source |
1986 |
|
*/ |
1987 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
1987 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1988 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1836 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1989 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1990 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1991 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1992 |
|
{ |
1993 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1994 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1995 |
|
final CFException ex = new CFException(); |
1996 |
< |
final Noop r = new Noop(m); |
1996 |
> |
final Noop r1 = new Noop(m); |
1997 |
> |
final Noop r2 = new Noop(m); |
1998 |
> |
final Noop r3 = new Noop(m); |
1999 |
|
|
2000 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
2001 |
< |
if (!createIncomplete) |
2002 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
2003 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2004 |
< |
if (createIncomplete) { |
2005 |
< |
checkIncomplete(h); |
2006 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
2007 |
< |
} |
2000 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2001 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2002 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2003 |
> |
() -> fst.completeExceptionally(ex) : |
2004 |
> |
() -> fst.complete(v1); |
2005 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2006 |
> |
() -> snd.complete(v1) : |
2007 |
> |
() -> snd.completeExceptionally(ex); |
2008 |
|
|
2009 |
< |
checkCompletedWithWrappedException(h, ex); |
2010 |
< |
r.assertNotInvoked(); |
2011 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2012 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
2009 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2010 |
> |
assertTrue(complete1.call()); |
2011 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2012 |
> |
checkIncomplete(h1); |
2013 |
> |
checkIncomplete(h2); |
2014 |
> |
assertTrue(complete2.call()); |
2015 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2016 |
> |
|
2017 |
> |
checkCompletedWithWrappedException(h1, ex); |
2018 |
> |
checkCompletedWithWrappedException(h2, ex); |
2019 |
> |
checkCompletedWithWrappedException(h3, ex); |
2020 |
> |
r1.assertNotInvoked(); |
2021 |
> |
r2.assertNotInvoked(); |
2022 |
> |
r3.assertNotInvoked(); |
2023 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2024 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2025 |
|
}} |
2026 |
|
|
2027 |
|
/** |
2028 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
2029 |
|
*/ |
2030 |
< |
public void testRunAfterBoth_sourceCancelled() { |
2030 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2031 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2032 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1866 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2033 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2034 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2035 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2036 |
|
{ |
2037 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2038 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2039 |
< |
final Noop r = new Noop(m); |
2039 |
> |
final Noop r1 = new Noop(m); |
2040 |
> |
final Noop r2 = new Noop(m); |
2041 |
> |
final Noop r3 = new Noop(m); |
2042 |
|
|
2043 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
2044 |
< |
if (!createIncomplete) |
2045 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2046 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2047 |
< |
if (createIncomplete) { |
2048 |
< |
checkIncomplete(h); |
2049 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2050 |
< |
} |
2043 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2044 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2045 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2046 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2047 |
> |
() -> fst.complete(v1); |
2048 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2049 |
> |
() -> snd.complete(v1) : |
2050 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2051 |
|
|
2052 |
< |
checkCompletedWithWrappedCancellationException(h); |
2053 |
< |
checkCancelled(!fFirst ? f : g); |
2054 |
< |
r.assertNotInvoked(); |
2055 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2052 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2053 |
> |
assertTrue(complete1.call()); |
2054 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2055 |
> |
checkIncomplete(h1); |
2056 |
> |
checkIncomplete(h2); |
2057 |
> |
assertTrue(complete2.call()); |
2058 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2059 |
> |
|
2060 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2061 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2062 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2063 |
> |
r1.assertNotInvoked(); |
2064 |
> |
r2.assertNotInvoked(); |
2065 |
> |
r3.assertNotInvoked(); |
2066 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2067 |
> |
checkCancelled(failFirst ? fst : snd); |
2068 |
|
}} |
2069 |
|
|
2070 |
|
/** |
2080 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2081 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
2082 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
2083 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
2084 |
|
|
2085 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2086 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
2087 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
2088 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2085 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2086 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2087 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2088 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2089 |
> |
|
2090 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2091 |
> |
assertTrue(fst.complete(w1)); |
2092 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2093 |
> |
assertTrue(snd.complete(w2)); |
2094 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2095 |
|
|
2096 |
|
checkCompletedWithWrappedCFException(h1); |
2097 |
|
checkCompletedWithWrappedCFException(h2); |
2098 |
+ |
checkCompletedWithWrappedCFException(h3); |
2099 |
+ |
r1.assertInvoked(); |
2100 |
+ |
r2.assertInvoked(); |
2101 |
+ |
r3.assertInvoked(); |
2102 |
|
checkCompletedNormally(f, v1); |
2103 |
|
checkCompletedNormally(g, v2); |
2104 |
|
}} |
3010 |
|
* when all components complete normally |
3011 |
|
*/ |
3012 |
|
public void testAllOf_normal() throws Exception { |
3013 |
< |
for (int k = 1; k < 20; ++k) { |
3013 |
> |
for (int k = 1; k < 10; k++) { |
3014 |
|
CompletableFuture<Integer>[] fs |
3015 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3016 |
< |
for (int i = 0; i < k; ++i) |
3016 |
> |
for (int i = 0; i < k; i++) |
3017 |
|
fs[i] = new CompletableFuture<>(); |
3018 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3019 |
< |
for (int i = 0; i < k; ++i) { |
3019 |
> |
for (int i = 0; i < k; i++) { |
3020 |
|
checkIncomplete(f); |
3021 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3022 |
|
fs[i].complete(one); |
3027 |
|
} |
3028 |
|
|
3029 |
|
public void testAllOf_backwards() throws Exception { |
3030 |
< |
for (int k = 1; k < 20; ++k) { |
3030 |
> |
for (int k = 1; k < 10; k++) { |
3031 |
|
CompletableFuture<Integer>[] fs |
3032 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3033 |
< |
for (int i = 0; i < k; ++i) |
3033 |
> |
for (int i = 0; i < k; i++) |
3034 |
|
fs[i] = new CompletableFuture<>(); |
3035 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3036 |
|
for (int i = k - 1; i >= 0; i--) { |
3043 |
|
} |
3044 |
|
} |
3045 |
|
|
3046 |
+ |
public void testAllOf_exceptional() throws Exception { |
3047 |
+ |
for (int k = 1; k < 10; k++) { |
3048 |
+ |
CompletableFuture<Integer>[] fs |
3049 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3050 |
+ |
CFException ex = new CFException(); |
3051 |
+ |
for (int i = 0; i < k; i++) |
3052 |
+ |
fs[i] = new CompletableFuture<>(); |
3053 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3054 |
+ |
for (int i = 0; i < k; i++) { |
3055 |
+ |
checkIncomplete(f); |
3056 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3057 |
+ |
if (i != k/2) { |
3058 |
+ |
fs[i].complete(i); |
3059 |
+ |
checkCompletedNormally(fs[i], i); |
3060 |
+ |
} else { |
3061 |
+ |
fs[i].completeExceptionally(ex); |
3062 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3063 |
+ |
} |
3064 |
+ |
} |
3065 |
+ |
checkCompletedWithWrappedException(f, ex); |
3066 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3067 |
+ |
} |
3068 |
+ |
} |
3069 |
+ |
|
3070 |
|
/** |
3071 |
|
* anyOf(no component futures) returns an incomplete future |
3072 |
|
*/ |
3073 |
|
public void testAnyOf_empty() throws Exception { |
3074 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3075 |
+ |
{ |
3076 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3077 |
|
checkIncomplete(f); |
3078 |
< |
} |
3078 |
> |
|
3079 |
> |
f.complete(v1); |
3080 |
> |
checkCompletedNormally(f, v1); |
3081 |
> |
}} |
3082 |
|
|
3083 |
|
/** |
3084 |
|
* anyOf returns a future completed normally with a value when |
3085 |
|
* a component future does |
3086 |
|
*/ |
3087 |
|
public void testAnyOf_normal() throws Exception { |
3088 |
< |
for (int k = 0; k < 10; ++k) { |
3088 |
> |
for (int k = 0; k < 10; k++) { |
3089 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3090 |
< |
for (int i = 0; i < k; ++i) |
3090 |
> |
for (int i = 0; i < k; i++) |
3091 |
|
fs[i] = new CompletableFuture<>(); |
3092 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3093 |
|
checkIncomplete(f); |
3094 |
< |
for (int i = 0; i < k; ++i) { |
3095 |
< |
fs[i].complete(one); |
3096 |
< |
checkCompletedNormally(f, one); |
3097 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3094 |
> |
for (int i = 0; i < k; i++) { |
3095 |
> |
fs[i].complete(i); |
3096 |
> |
checkCompletedNormally(f, 0); |
3097 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3098 |
> |
assertTrue(0 <= x && x <= i); |
3099 |
> |
} |
3100 |
> |
} |
3101 |
> |
} |
3102 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3103 |
> |
for (int k = 0; k < 10; k++) { |
3104 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3105 |
> |
for (int i = 0; i < k; i++) |
3106 |
> |
fs[i] = new CompletableFuture<>(); |
3107 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3108 |
> |
checkIncomplete(f); |
3109 |
> |
for (int i = k - 1; i >= 0; i--) { |
3110 |
> |
fs[i].complete(i); |
3111 |
> |
checkCompletedNormally(f, k - 1); |
3112 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3113 |
> |
assertTrue(i <= x && x <= k - 1); |
3114 |
|
} |
3115 |
|
} |
3116 |
|
} |
3119 |
|
* anyOf result completes exceptionally when any component does. |
3120 |
|
*/ |
3121 |
|
public void testAnyOf_exceptional() throws Exception { |
3122 |
< |
for (int k = 0; k < 10; ++k) { |
3122 |
> |
for (int k = 0; k < 10; k++) { |
3123 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3124 |
< |
for (int i = 0; i < k; ++i) |
3124 |
> |
CFException[] exs = new CFException[k]; |
3125 |
> |
for (int i = 0; i < k; i++) { |
3126 |
|
fs[i] = new CompletableFuture<>(); |
3127 |
+ |
exs[i] = new CFException(); |
3128 |
+ |
} |
3129 |
+ |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3130 |
+ |
checkIncomplete(f); |
3131 |
+ |
for (int i = 0; i < k; i++) { |
3132 |
+ |
fs[i].completeExceptionally(exs[i]); |
3133 |
+ |
checkCompletedWithWrappedException(f, exs[0]); |
3134 |
+ |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3135 |
+ |
} |
3136 |
+ |
} |
3137 |
+ |
} |
3138 |
+ |
|
3139 |
+ |
public void testAnyOf_exceptional_backwards() throws Exception { |
3140 |
+ |
for (int k = 0; k < 10; k++) { |
3141 |
+ |
CompletableFuture[] fs = new CompletableFuture[k]; |
3142 |
+ |
CFException[] exs = new CFException[k]; |
3143 |
+ |
for (int i = 0; i < k; i++) { |
3144 |
+ |
fs[i] = new CompletableFuture<>(); |
3145 |
+ |
exs[i] = new CFException(); |
3146 |
+ |
} |
3147 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3148 |
|
checkIncomplete(f); |
3149 |
< |
for (int i = 0; i < k; ++i) { |
3150 |
< |
fs[i].completeExceptionally(new CFException()); |
3151 |
< |
checkCompletedWithWrappedCFException(f); |
3149 |
> |
for (int i = k - 1; i >= 0; i--) { |
3150 |
> |
fs[i].completeExceptionally(exs[i]); |
3151 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3152 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3153 |
|
} |
3154 |
|
} |
3167 |
|
Runnable[] throwingActions = { |
3168 |
|
() -> CompletableFuture.supplyAsync(null), |
3169 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3170 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3170 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3171 |
|
|
3172 |
|
() -> CompletableFuture.runAsync(null), |
3173 |
|
() -> CompletableFuture.runAsync(null, exec), |
3272 |
|
assertSame(f, f.toCompletableFuture()); |
3273 |
|
} |
3274 |
|
|
3275 |
< |
// public void testRunAfterEither_resultDeterminedAtTimeOfCreation() { |
3276 |
< |
// for (ExecutionMode m : ExecutionMode.values()) |
3277 |
< |
// for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3278 |
< |
// for (Integer v1 : new Integer[] { 1, null }) |
3279 |
< |
// { |
3280 |
< |
// final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3281 |
< |
// final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3282 |
< |
// final Noop[] rs = new Noop[2]; |
3283 |
< |
// for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
3284 |
< |
// f.complete(v1); |
3285 |
< |
// final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
3286 |
< |
// final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
3287 |
< |
// assertTrue(g.cancel(mayInterruptIfRunning)); |
3288 |
< |
// checkCompletedNormally(h0, null); |
3289 |
< |
// checkCompletedNormally(h1, null); |
3290 |
< |
// for (Noop r : rs) r.assertInvoked(); |
3291 |
< |
// }} |
3275 |
> |
//--- tests of implementation details; not part of official tck --- |
3276 |
> |
|
3277 |
> |
Object resultOf(CompletableFuture<?> f) { |
3278 |
> |
try { |
3279 |
> |
java.lang.reflect.Field resultField |
3280 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3281 |
> |
resultField.setAccessible(true); |
3282 |
> |
return resultField.get(f); |
3283 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3284 |
> |
} |
3285 |
> |
|
3286 |
> |
public void testExceptionPropagationReusesResultObject() { |
3287 |
> |
if (!testImplementationDetails) return; |
3288 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3289 |
> |
{ |
3290 |
> |
final CFException ex = new CFException(); |
3291 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3292 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3293 |
> |
|
3294 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3295 |
> |
= new ArrayList<>(); |
3296 |
> |
|
3297 |
> |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3298 |
> |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3299 |
> |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3300 |
> |
|
3301 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3302 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3303 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3304 |
> |
|
3305 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3306 |
> |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3307 |
> |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3308 |
> |
|
3309 |
> |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3310 |
> |
|
3311 |
> |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3312 |
> |
|
3313 |
> |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3314 |
> |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3315 |
> |
|
3316 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3317 |
> |
fun : funs) { |
3318 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3319 |
> |
f.completeExceptionally(ex); |
3320 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3321 |
> |
checkCompletedWithWrappedException(src, ex); |
3322 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3323 |
> |
checkCompletedWithWrappedException(dep, ex); |
3324 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3325 |
> |
} |
3326 |
> |
|
3327 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3328 |
> |
fun : funs) { |
3329 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3330 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3331 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3332 |
> |
f.completeExceptionally(ex); |
3333 |
> |
checkCompletedWithWrappedException(src, ex); |
3334 |
> |
checkCompletedWithWrappedException(dep, ex); |
3335 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3336 |
> |
} |
3337 |
> |
|
3338 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3339 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3340 |
> |
fun : funs) { |
3341 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3342 |
> |
f.cancel(mayInterruptIfRunning); |
3343 |
> |
checkCancelled(f); |
3344 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3345 |
> |
checkCompletedWithWrappedCancellationException(src); |
3346 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3347 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3348 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3349 |
> |
} |
3350 |
> |
|
3351 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3352 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3353 |
> |
fun : funs) { |
3354 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3355 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3356 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3357 |
> |
f.cancel(mayInterruptIfRunning); |
3358 |
> |
checkCancelled(f); |
3359 |
> |
checkCompletedWithWrappedCancellationException(src); |
3360 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3361 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3362 |
> |
} |
3363 |
> |
}} |
3364 |
|
|
3365 |
|
} |