8 |
|
import junit.framework.*; |
9 |
|
import java.util.concurrent.Callable; |
10 |
|
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
11 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
12 |
|
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
13 |
|
import java.util.concurrent.CompletableFuture; |
14 |
|
import java.util.concurrent.CompletionException; |
15 |
|
import java.util.concurrent.CompletionStage; |
53 |
|
} |
54 |
|
|
55 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
56 |
< |
try { |
57 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
56 |
> |
checkTimedGet(f, value); |
57 |
> |
|
58 |
|
try { |
59 |
|
assertEquals(value, f.join()); |
60 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
71 |
|
} |
72 |
|
|
73 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
74 |
+ |
long startTime = System.nanoTime(); |
75 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
76 |
|
try { |
77 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
77 |
> |
f.get(timeoutMillis, MILLISECONDS); |
78 |
|
shouldThrow(); |
79 |
|
} catch (ExecutionException success) { |
80 |
|
assertTrue(success.getCause() instanceof CFException); |
81 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
82 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
83 |
+ |
|
84 |
|
try { |
85 |
|
f.join(); |
86 |
|
shouldThrow(); |
106 |
|
|
107 |
|
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
108 |
|
Throwable ex) { |
109 |
+ |
long startTime = System.nanoTime(); |
110 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
111 |
|
try { |
112 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
112 |
> |
f.get(timeoutMillis, MILLISECONDS); |
113 |
|
shouldThrow(); |
114 |
|
} catch (ExecutionException success) { |
115 |
|
assertSame(ex, success.getCause()); |
116 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
117 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
118 |
+ |
|
119 |
|
try { |
120 |
|
f.join(); |
121 |
|
shouldThrow(); |
161 |
|
} |
162 |
|
|
163 |
|
void checkCancelled(CompletableFuture<?> f) { |
164 |
+ |
long startTime = System.nanoTime(); |
165 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
166 |
|
try { |
167 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
167 |
> |
f.get(timeoutMillis, MILLISECONDS); |
168 |
|
shouldThrow(); |
169 |
|
} catch (CancellationException success) { |
170 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
171 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
172 |
+ |
|
173 |
|
try { |
174 |
|
f.join(); |
175 |
|
shouldThrow(); |
190 |
|
} |
191 |
|
|
192 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
193 |
+ |
long startTime = System.nanoTime(); |
194 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
195 |
|
try { |
196 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
196 |
> |
f.get(timeoutMillis, MILLISECONDS); |
197 |
|
shouldThrow(); |
198 |
|
} catch (ExecutionException success) { |
199 |
|
assertTrue(success.getCause() instanceof CancellationException); |
200 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
201 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
202 |
+ |
|
203 |
|
try { |
204 |
|
f.join(); |
205 |
|
shouldThrow(); |
580 |
|
} |
581 |
|
} |
582 |
|
|
583 |
+ |
static final boolean defaultExecutorIsCommonPool |
584 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
585 |
+ |
|
586 |
|
/** |
587 |
|
* Permits the testing of parallel code for the 3 different |
588 |
|
* execution modes without copy/pasting all the test methods. |
589 |
|
*/ |
590 |
|
enum ExecutionMode { |
591 |
< |
DEFAULT { |
591 |
> |
SYNC { |
592 |
|
public void checkExecutionMode() { |
593 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
594 |
|
assertNull(ForkJoinTask.getPool()); |
664 |
|
|
665 |
|
ASYNC { |
666 |
|
public void checkExecutionMode() { |
667 |
< |
assertSame(ForkJoinPool.commonPool(), |
668 |
< |
ForkJoinTask.getPool()); |
667 |
> |
assertEquals(defaultExecutorIsCommonPool, |
668 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
669 |
|
} |
670 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
671 |
|
return CompletableFuture.runAsync(a); |
889 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
890 |
|
final CompletableFuture<Integer> g = f.exceptionally |
891 |
|
((Throwable t) -> { |
892 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
892 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
893 |
|
threadAssertSame(t, ex); |
894 |
|
a.getAndIncrement(); |
895 |
|
return v1; |
911 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
912 |
|
final CompletableFuture<Integer> g = f.exceptionally |
913 |
|
((Throwable t) -> { |
914 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
914 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
915 |
|
threadAssertSame(t, ex1); |
916 |
|
a.getAndIncrement(); |
917 |
|
throw ex2; |
1278 |
|
*/ |
1279 |
|
public void testThenRun_normalCompletion() { |
1280 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1267 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1281 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1282 |
|
{ |
1283 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1284 |
< |
final Noop r = new Noop(m); |
1285 |
< |
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 |
< |
} |
1284 |
> |
final Noop[] rs = new Noop[6]; |
1285 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1286 |
|
|
1287 |
< |
checkCompletedNormally(g, null); |
1287 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1288 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1289 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1290 |
> |
checkIncomplete(h0); |
1291 |
> |
checkIncomplete(h1); |
1292 |
> |
checkIncomplete(h2); |
1293 |
> |
assertTrue(f.complete(v1)); |
1294 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1295 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1296 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1297 |
> |
|
1298 |
> |
checkCompletedNormally(h0, null); |
1299 |
> |
checkCompletedNormally(h1, null); |
1300 |
> |
checkCompletedNormally(h2, null); |
1301 |
> |
checkCompletedNormally(h3, null); |
1302 |
> |
checkCompletedNormally(h4, null); |
1303 |
> |
checkCompletedNormally(h5, null); |
1304 |
|
checkCompletedNormally(f, v1); |
1305 |
< |
r.assertInvoked(); |
1305 |
> |
for (Noop r : rs) r.assertInvoked(); |
1306 |
|
}} |
1307 |
|
|
1308 |
|
/** |
1311 |
|
*/ |
1312 |
|
public void testThenRun_exceptionalCompletion() { |
1313 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1290 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1314 |
|
{ |
1315 |
|
final CFException ex = new CFException(); |
1316 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1317 |
< |
final Noop r = new Noop(m); |
1318 |
< |
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 |
< |
} |
1317 |
> |
final Noop[] rs = new Noop[6]; |
1318 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1319 |
|
|
1320 |
< |
checkCompletedWithWrappedException(g, ex); |
1320 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1321 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1322 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1323 |
> |
checkIncomplete(h0); |
1324 |
> |
checkIncomplete(h1); |
1325 |
> |
checkIncomplete(h2); |
1326 |
> |
assertTrue(f.completeExceptionally(ex)); |
1327 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1328 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1329 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1330 |
> |
|
1331 |
> |
checkCompletedWithWrappedException(h0, ex); |
1332 |
> |
checkCompletedWithWrappedException(h1, ex); |
1333 |
> |
checkCompletedWithWrappedException(h2, ex); |
1334 |
> |
checkCompletedWithWrappedException(h3, ex); |
1335 |
> |
checkCompletedWithWrappedException(h4, ex); |
1336 |
> |
checkCompletedWithWrappedException(h5, ex); |
1337 |
|
checkCompletedExceptionally(f, ex); |
1338 |
< |
r.assertNotInvoked(); |
1338 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1339 |
|
}} |
1340 |
|
|
1341 |
|
/** |
1343 |
|
*/ |
1344 |
|
public void testThenRun_sourceCancelled() { |
1345 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1312 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1346 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1347 |
|
{ |
1348 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1349 |
< |
final Noop r = new Noop(m); |
1350 |
< |
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 |
< |
} |
1349 |
> |
final Noop[] rs = new Noop[6]; |
1350 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1351 |
|
|
1352 |
< |
checkCompletedWithWrappedCancellationException(g); |
1352 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1353 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1354 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1355 |
> |
checkIncomplete(h0); |
1356 |
> |
checkIncomplete(h1); |
1357 |
> |
checkIncomplete(h2); |
1358 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1359 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1360 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1361 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1362 |
> |
|
1363 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1364 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1365 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1366 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1367 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1368 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1369 |
|
checkCancelled(f); |
1370 |
< |
r.assertNotInvoked(); |
1370 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1371 |
|
}} |
1372 |
|
|
1373 |
|
/** |
1375 |
|
*/ |
1376 |
|
public void testThenRun_actionFailed() { |
1377 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1334 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1378 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1379 |
|
{ |
1380 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1381 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1382 |
< |
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 |
< |
} |
1381 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1382 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1383 |
|
|
1384 |
< |
checkCompletedWithWrappedCFException(g); |
1384 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1385 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1386 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1387 |
> |
assertTrue(f.complete(v1)); |
1388 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1389 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1390 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1391 |
> |
|
1392 |
> |
checkCompletedWithWrappedCFException(h0); |
1393 |
> |
checkCompletedWithWrappedCFException(h1); |
1394 |
> |
checkCompletedWithWrappedCFException(h2); |
1395 |
> |
checkCompletedWithWrappedCFException(h3); |
1396 |
> |
checkCompletedWithWrappedCFException(h4); |
1397 |
> |
checkCompletedWithWrappedCFException(h5); |
1398 |
|
checkCompletedNormally(f, v1); |
1399 |
|
}} |
1400 |
|
|
1403 |
|
*/ |
1404 |
|
public void testThenApply_normalCompletion() { |
1405 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1355 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1406 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1407 |
|
{ |
1408 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1409 |
< |
final IncFunction r = new IncFunction(m); |
1410 |
< |
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 |
< |
} |
1409 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1410 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1411 |
|
|
1412 |
< |
checkCompletedNormally(g, inc(v1)); |
1412 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1413 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1414 |
> |
checkIncomplete(h0); |
1415 |
> |
checkIncomplete(h1); |
1416 |
> |
assertTrue(f.complete(v1)); |
1417 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1418 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1419 |
> |
|
1420 |
> |
checkCompletedNormally(h0, inc(v1)); |
1421 |
> |
checkCompletedNormally(h1, inc(v1)); |
1422 |
> |
checkCompletedNormally(h2, inc(v1)); |
1423 |
> |
checkCompletedNormally(h3, inc(v1)); |
1424 |
|
checkCompletedNormally(f, v1); |
1425 |
< |
r.assertValue(inc(v1)); |
1425 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1426 |
|
}} |
1427 |
|
|
1428 |
|
/** |
1431 |
|
*/ |
1432 |
|
public void testThenApply_exceptionalCompletion() { |
1433 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1378 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1434 |
|
{ |
1435 |
|
final CFException ex = new CFException(); |
1436 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1437 |
< |
final IncFunction r = new IncFunction(m); |
1438 |
< |
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 |
< |
} |
1437 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1438 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1439 |
|
|
1440 |
< |
checkCompletedWithWrappedException(g, ex); |
1440 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1441 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1442 |
> |
assertTrue(f.completeExceptionally(ex)); |
1443 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1444 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1445 |
> |
|
1446 |
> |
checkCompletedWithWrappedException(h0, ex); |
1447 |
> |
checkCompletedWithWrappedException(h1, ex); |
1448 |
> |
checkCompletedWithWrappedException(h2, ex); |
1449 |
> |
checkCompletedWithWrappedException(h3, ex); |
1450 |
|
checkCompletedExceptionally(f, ex); |
1451 |
< |
r.assertNotInvoked(); |
1451 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1452 |
|
}} |
1453 |
|
|
1454 |
|
/** |
1456 |
|
*/ |
1457 |
|
public void testThenApply_sourceCancelled() { |
1458 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1400 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1459 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1460 |
|
{ |
1461 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1462 |
< |
final IncFunction r = new IncFunction(m); |
1463 |
< |
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 |
< |
} |
1462 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1463 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1464 |
|
|
1465 |
< |
checkCompletedWithWrappedCancellationException(g); |
1465 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1466 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1467 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1468 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1469 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1470 |
> |
|
1471 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1472 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1473 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1474 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1475 |
|
checkCancelled(f); |
1476 |
< |
r.assertNotInvoked(); |
1476 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1477 |
|
}} |
1478 |
|
|
1479 |
|
/** |
1481 |
|
*/ |
1482 |
|
public void testThenApply_actionFailed() { |
1483 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1422 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1484 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1485 |
|
{ |
1486 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1487 |
< |
final FailingFunction r = new FailingFunction(m); |
1488 |
< |
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 |
< |
} |
1487 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1488 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1489 |
|
|
1490 |
< |
checkCompletedWithWrappedCFException(g); |
1490 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1491 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1492 |
> |
assertTrue(f.complete(v1)); |
1493 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1494 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1495 |
> |
|
1496 |
> |
checkCompletedWithWrappedCFException(h0); |
1497 |
> |
checkCompletedWithWrappedCFException(h1); |
1498 |
> |
checkCompletedWithWrappedCFException(h2); |
1499 |
> |
checkCompletedWithWrappedCFException(h3); |
1500 |
|
checkCompletedNormally(f, v1); |
1501 |
|
}} |
1502 |
|
|
1505 |
|
*/ |
1506 |
|
public void testThenAccept_normalCompletion() { |
1507 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1443 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1508 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1509 |
|
{ |
1510 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1511 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1512 |
< |
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 |
< |
} |
1511 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1512 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1513 |
|
|
1514 |
< |
checkCompletedNormally(g, null); |
1515 |
< |
r.assertValue(v1); |
1514 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1515 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1516 |
> |
checkIncomplete(h0); |
1517 |
> |
checkIncomplete(h1); |
1518 |
> |
assertTrue(f.complete(v1)); |
1519 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1520 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1521 |
> |
|
1522 |
> |
checkCompletedNormally(h0, null); |
1523 |
> |
checkCompletedNormally(h1, null); |
1524 |
> |
checkCompletedNormally(h2, null); |
1525 |
> |
checkCompletedNormally(h3, null); |
1526 |
|
checkCompletedNormally(f, v1); |
1527 |
+ |
for (NoopConsumer r : rs) r.assertValue(v1); |
1528 |
|
}} |
1529 |
|
|
1530 |
|
/** |
1533 |
|
*/ |
1534 |
|
public void testThenAccept_exceptionalCompletion() { |
1535 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1466 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1536 |
|
{ |
1537 |
|
final CFException ex = new CFException(); |
1538 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1539 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1540 |
< |
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 |
< |
} |
1539 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1540 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1541 |
|
|
1542 |
< |
checkCompletedWithWrappedException(g, ex); |
1542 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1543 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1544 |
> |
assertTrue(f.completeExceptionally(ex)); |
1545 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1546 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1547 |
> |
|
1548 |
> |
checkCompletedWithWrappedException(h0, ex); |
1549 |
> |
checkCompletedWithWrappedException(h1, ex); |
1550 |
> |
checkCompletedWithWrappedException(h2, ex); |
1551 |
> |
checkCompletedWithWrappedException(h3, ex); |
1552 |
|
checkCompletedExceptionally(f, ex); |
1553 |
< |
r.assertNotInvoked(); |
1553 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1554 |
|
}} |
1555 |
|
|
1556 |
|
/** |
1558 |
|
*/ |
1559 |
|
public void testThenAccept_sourceCancelled() { |
1560 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1488 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1561 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1562 |
|
{ |
1563 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1564 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1565 |
< |
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 |
< |
} |
1564 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1565 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1566 |
|
|
1567 |
< |
checkCompletedWithWrappedCancellationException(g); |
1567 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1568 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1569 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1570 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1571 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1572 |
> |
|
1573 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1574 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1575 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1576 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1577 |
|
checkCancelled(f); |
1578 |
< |
r.assertNotInvoked(); |
1578 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1579 |
|
}} |
1580 |
|
|
1581 |
|
/** |
1583 |
|
*/ |
1584 |
|
public void testThenAccept_actionFailed() { |
1585 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1510 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1586 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1587 |
|
{ |
1588 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1589 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1590 |
< |
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 |
< |
} |
1589 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1590 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1591 |
|
|
1592 |
< |
checkCompletedWithWrappedCFException(g); |
1592 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1593 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1594 |
> |
assertTrue(f.complete(v1)); |
1595 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1596 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1597 |
> |
|
1598 |
> |
checkCompletedWithWrappedCFException(h0); |
1599 |
> |
checkCompletedWithWrappedCFException(h1); |
1600 |
> |
checkCompletedWithWrappedCFException(h2); |
1601 |
> |
checkCompletedWithWrappedCFException(h3); |
1602 |
|
checkCompletedNormally(f, v1); |
1603 |
|
}} |
1604 |
|
|
1614 |
|
{ |
1615 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1616 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1617 |
< |
final SubtractFunction r1 = new SubtractFunction(m); |
1618 |
< |
final SubtractFunction r2 = new SubtractFunction(m); |
1540 |
< |
final SubtractFunction r3 = new SubtractFunction(m); |
1617 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1618 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1619 |
|
|
1620 |
|
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1621 |
|
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1622 |
|
final Integer w1 = fFirst ? v1 : v2; |
1623 |
|
final Integer w2 = !fFirst ? v1 : v2; |
1624 |
|
|
1625 |
< |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1625 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1626 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1627 |
|
assertTrue(fst.complete(w1)); |
1628 |
< |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1629 |
< |
checkIncomplete(h1); |
1630 |
< |
checkIncomplete(h2); |
1631 |
< |
r1.assertNotInvoked(); |
1632 |
< |
r2.assertNotInvoked(); |
1628 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1629 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1630 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1631 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1632 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1633 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1634 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1635 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1636 |
|
assertTrue(snd.complete(w2)); |
1637 |
< |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1637 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1638 |
|
|
1639 |
< |
checkCompletedNormally(h1, subtract(v1, v2)); |
1639 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1640 |
|
checkCompletedNormally(h2, subtract(v1, v2)); |
1641 |
< |
checkCompletedNormally(h3, subtract(v1, v2)); |
1642 |
< |
r1.assertValue(subtract(v1, v2)); |
1643 |
< |
r2.assertValue(subtract(v1, v2)); |
1644 |
< |
r3.assertValue(subtract(v1, v2)); |
1641 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1642 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1643 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1644 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1645 |
> |
|
1646 |
|
checkCompletedNormally(f, v1); |
1647 |
|
checkCompletedNormally(g, v2); |
1648 |
|
}} |
1763 |
|
checkCompletedWithWrappedCFException(h1); |
1764 |
|
checkCompletedWithWrappedCFException(h2); |
1765 |
|
checkCompletedWithWrappedCFException(h3); |
1766 |
+ |
r1.assertInvoked(); |
1767 |
+ |
r2.assertInvoked(); |
1768 |
+ |
r3.assertInvoked(); |
1769 |
|
checkCompletedNormally(f, v1); |
1770 |
|
checkCompletedNormally(g, v2); |
1771 |
|
}} |
1776 |
|
*/ |
1777 |
|
public void testThenAcceptBoth_normalCompletion() { |
1778 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1693 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1779 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1780 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1781 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1782 |
|
{ |
1783 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1784 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1785 |
< |
final SubtractAction r = new SubtractAction(m); |
1785 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1786 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1787 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1788 |
|
|
1789 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1790 |
< |
if (!createIncomplete) |
1791 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1792 |
< |
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 |
< |
} |
1789 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1790 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1791 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1792 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1793 |
|
|
1794 |
< |
checkCompletedNormally(h, null); |
1795 |
< |
r.assertValue(subtract(v1, v2)); |
1794 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1795 |
> |
assertTrue(fst.complete(w1)); |
1796 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1797 |
> |
checkIncomplete(h1); |
1798 |
> |
checkIncomplete(h2); |
1799 |
> |
r1.assertNotInvoked(); |
1800 |
> |
r2.assertNotInvoked(); |
1801 |
> |
assertTrue(snd.complete(w2)); |
1802 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1803 |
> |
|
1804 |
> |
checkCompletedNormally(h1, null); |
1805 |
> |
checkCompletedNormally(h2, null); |
1806 |
> |
checkCompletedNormally(h3, null); |
1807 |
> |
r1.assertValue(subtract(v1, v2)); |
1808 |
> |
r2.assertValue(subtract(v1, v2)); |
1809 |
> |
r3.assertValue(subtract(v1, v2)); |
1810 |
|
checkCompletedNormally(f, v1); |
1811 |
|
checkCompletedNormally(g, v2); |
1812 |
|
}} |
1815 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1816 |
|
* completion of either source |
1817 |
|
*/ |
1818 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1818 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1819 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1724 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1820 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1821 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1822 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1823 |
|
{ |
1824 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1825 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1826 |
|
final CFException ex = new CFException(); |
1827 |
< |
final SubtractAction r = new SubtractAction(m); |
1827 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1828 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1829 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1830 |
|
|
1831 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1832 |
< |
if (!createIncomplete) |
1833 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1834 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1835 |
< |
if (createIncomplete) { |
1836 |
< |
checkIncomplete(h); |
1837 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1838 |
< |
} |
1831 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1832 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1833 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1834 |
> |
() -> fst.completeExceptionally(ex) : |
1835 |
> |
() -> fst.complete(v1); |
1836 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1837 |
> |
() -> snd.complete(v1) : |
1838 |
> |
() -> snd.completeExceptionally(ex); |
1839 |
|
|
1840 |
< |
checkCompletedWithWrappedException(h, ex); |
1841 |
< |
r.assertNotInvoked(); |
1842 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1843 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1840 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1841 |
> |
assertTrue(complete1.call()); |
1842 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1843 |
> |
checkIncomplete(h1); |
1844 |
> |
checkIncomplete(h2); |
1845 |
> |
assertTrue(complete2.call()); |
1846 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1847 |
> |
|
1848 |
> |
checkCompletedWithWrappedException(h1, ex); |
1849 |
> |
checkCompletedWithWrappedException(h2, ex); |
1850 |
> |
checkCompletedWithWrappedException(h3, ex); |
1851 |
> |
r1.assertNotInvoked(); |
1852 |
> |
r2.assertNotInvoked(); |
1853 |
> |
r3.assertNotInvoked(); |
1854 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1855 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1856 |
|
}} |
1857 |
|
|
1858 |
|
/** |
1859 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1860 |
|
*/ |
1861 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1861 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1862 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1863 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1754 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1864 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1865 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1866 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1867 |
|
{ |
1868 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1869 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1870 |
< |
final SubtractAction r = new SubtractAction(m); |
1870 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1871 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1872 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1873 |
|
|
1874 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1875 |
< |
if (!createIncomplete) |
1876 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1877 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1878 |
< |
if (createIncomplete) { |
1879 |
< |
checkIncomplete(h); |
1880 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1881 |
< |
} |
1874 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1875 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1876 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1877 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1878 |
> |
() -> fst.complete(v1); |
1879 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1880 |
> |
() -> snd.complete(v1) : |
1881 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1882 |
|
|
1883 |
< |
checkCompletedWithWrappedCancellationException(h); |
1884 |
< |
checkCancelled(!fFirst ? f : g); |
1885 |
< |
r.assertNotInvoked(); |
1886 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1883 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1884 |
> |
assertTrue(complete1.call()); |
1885 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1886 |
> |
checkIncomplete(h1); |
1887 |
> |
checkIncomplete(h2); |
1888 |
> |
assertTrue(complete2.call()); |
1889 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1890 |
> |
|
1891 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1892 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1893 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1894 |
> |
r1.assertNotInvoked(); |
1895 |
> |
r2.assertNotInvoked(); |
1896 |
> |
r3.assertNotInvoked(); |
1897 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1898 |
> |
checkCancelled(failFirst ? fst : snd); |
1899 |
|
}} |
1900 |
|
|
1901 |
|
/** |
1909 |
|
{ |
1910 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1911 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1912 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1913 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1912 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1913 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1914 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1915 |
|
|
1916 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1917 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1916 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1917 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1918 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1919 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1920 |
> |
|
1921 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1922 |
> |
assertTrue(fst.complete(w1)); |
1923 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1924 |
> |
assertTrue(snd.complete(w2)); |
1925 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1926 |
|
|
1927 |
< |
checkCompletedWithWrappedCFException(h); |
1927 |
> |
checkCompletedWithWrappedCFException(h1); |
1928 |
> |
checkCompletedWithWrappedCFException(h2); |
1929 |
> |
checkCompletedWithWrappedCFException(h3); |
1930 |
> |
r1.assertInvoked(); |
1931 |
> |
r2.assertInvoked(); |
1932 |
> |
r3.assertInvoked(); |
1933 |
|
checkCompletedNormally(f, v1); |
1934 |
|
checkCompletedNormally(g, v2); |
1935 |
|
}} |
1940 |
|
*/ |
1941 |
|
public void testRunAfterBoth_normalCompletion() { |
1942 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1805 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1943 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1944 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1945 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1946 |
|
{ |
1947 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1948 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1949 |
< |
final Noop r = new Noop(m); |
1949 |
> |
final Noop r1 = new Noop(m); |
1950 |
> |
final Noop r2 = new Noop(m); |
1951 |
> |
final Noop r3 = new Noop(m); |
1952 |
|
|
1953 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1954 |
< |
if (!createIncomplete) |
1955 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1956 |
< |
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 |
< |
} |
1953 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1954 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1955 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1956 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1957 |
|
|
1958 |
< |
checkCompletedNormally(h, null); |
1959 |
< |
r.assertInvoked(); |
1958 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1959 |
> |
assertTrue(fst.complete(w1)); |
1960 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1961 |
> |
checkIncomplete(h1); |
1962 |
> |
checkIncomplete(h2); |
1963 |
> |
r1.assertNotInvoked(); |
1964 |
> |
r2.assertNotInvoked(); |
1965 |
> |
assertTrue(snd.complete(w2)); |
1966 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1967 |
> |
|
1968 |
> |
checkCompletedNormally(h1, null); |
1969 |
> |
checkCompletedNormally(h2, null); |
1970 |
> |
checkCompletedNormally(h3, null); |
1971 |
> |
r1.assertInvoked(); |
1972 |
> |
r2.assertInvoked(); |
1973 |
> |
r3.assertInvoked(); |
1974 |
|
checkCompletedNormally(f, v1); |
1975 |
|
checkCompletedNormally(g, v2); |
1976 |
|
}} |
1979 |
|
* runAfterBoth result completes exceptionally after exceptional |
1980 |
|
* completion of either source |
1981 |
|
*/ |
1982 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
1982 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1983 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1836 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1984 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1985 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1986 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1987 |
|
{ |
1988 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1989 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1990 |
|
final CFException ex = new CFException(); |
1991 |
< |
final Noop r = new Noop(m); |
1991 |
> |
final Noop r1 = new Noop(m); |
1992 |
> |
final Noop r2 = new Noop(m); |
1993 |
> |
final Noop r3 = new Noop(m); |
1994 |
|
|
1995 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1996 |
< |
if (!createIncomplete) |
1997 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1998 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1999 |
< |
if (createIncomplete) { |
2000 |
< |
checkIncomplete(h); |
2001 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
2002 |
< |
} |
1995 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1996 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1997 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1998 |
> |
() -> fst.completeExceptionally(ex) : |
1999 |
> |
() -> fst.complete(v1); |
2000 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2001 |
> |
() -> snd.complete(v1) : |
2002 |
> |
() -> snd.completeExceptionally(ex); |
2003 |
|
|
2004 |
< |
checkCompletedWithWrappedException(h, ex); |
2005 |
< |
r.assertNotInvoked(); |
2006 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2007 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
2004 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2005 |
> |
assertTrue(complete1.call()); |
2006 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2007 |
> |
checkIncomplete(h1); |
2008 |
> |
checkIncomplete(h2); |
2009 |
> |
assertTrue(complete2.call()); |
2010 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2011 |
> |
|
2012 |
> |
checkCompletedWithWrappedException(h1, ex); |
2013 |
> |
checkCompletedWithWrappedException(h2, ex); |
2014 |
> |
checkCompletedWithWrappedException(h3, ex); |
2015 |
> |
r1.assertNotInvoked(); |
2016 |
> |
r2.assertNotInvoked(); |
2017 |
> |
r3.assertNotInvoked(); |
2018 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2019 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2020 |
|
}} |
2021 |
|
|
2022 |
|
/** |
2023 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
2024 |
|
*/ |
2025 |
< |
public void testRunAfterBoth_sourceCancelled() { |
2025 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2026 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2027 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1866 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2028 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2029 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2030 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2031 |
|
{ |
2032 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2033 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2034 |
< |
final Noop r = new Noop(m); |
2034 |
> |
final Noop r1 = new Noop(m); |
2035 |
> |
final Noop r2 = new Noop(m); |
2036 |
> |
final Noop r3 = new Noop(m); |
2037 |
|
|
2038 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
2039 |
< |
if (!createIncomplete) |
2040 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2041 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2042 |
< |
if (createIncomplete) { |
2043 |
< |
checkIncomplete(h); |
2044 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2045 |
< |
} |
2038 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2039 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2040 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2041 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2042 |
> |
() -> fst.complete(v1); |
2043 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2044 |
> |
() -> snd.complete(v1) : |
2045 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2046 |
|
|
2047 |
< |
checkCompletedWithWrappedCancellationException(h); |
2048 |
< |
checkCancelled(!fFirst ? f : g); |
2049 |
< |
r.assertNotInvoked(); |
2050 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2047 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2048 |
> |
assertTrue(complete1.call()); |
2049 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2050 |
> |
checkIncomplete(h1); |
2051 |
> |
checkIncomplete(h2); |
2052 |
> |
assertTrue(complete2.call()); |
2053 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2054 |
> |
|
2055 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2056 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2057 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2058 |
> |
r1.assertNotInvoked(); |
2059 |
> |
r2.assertNotInvoked(); |
2060 |
> |
r3.assertNotInvoked(); |
2061 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2062 |
> |
checkCancelled(failFirst ? fst : snd); |
2063 |
|
}} |
2064 |
|
|
2065 |
|
/** |
2075 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2076 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
2077 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
2078 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
2079 |
|
|
2080 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2081 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
2082 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
2083 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2080 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2081 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2082 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2083 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2084 |
> |
|
2085 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2086 |
> |
assertTrue(fst.complete(w1)); |
2087 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2088 |
> |
assertTrue(snd.complete(w2)); |
2089 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2090 |
|
|
2091 |
|
checkCompletedWithWrappedCFException(h1); |
2092 |
|
checkCompletedWithWrappedCFException(h2); |
2093 |
+ |
checkCompletedWithWrappedCFException(h3); |
2094 |
+ |
r1.assertInvoked(); |
2095 |
+ |
r2.assertInvoked(); |
2096 |
+ |
r3.assertInvoked(); |
2097 |
|
checkCompletedNormally(f, v1); |
2098 |
|
checkCompletedNormally(g, v2); |
2099 |
|
}} |
3005 |
|
* when all components complete normally |
3006 |
|
*/ |
3007 |
|
public void testAllOf_normal() throws Exception { |
3008 |
< |
for (int k = 1; k < 20; ++k) { |
3008 |
> |
for (int k = 1; k < 10; k++) { |
3009 |
|
CompletableFuture<Integer>[] fs |
3010 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3011 |
< |
for (int i = 0; i < k; ++i) |
3011 |
> |
for (int i = 0; i < k; i++) |
3012 |
|
fs[i] = new CompletableFuture<>(); |
3013 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3014 |
< |
for (int i = 0; i < k; ++i) { |
3014 |
> |
for (int i = 0; i < k; i++) { |
3015 |
|
checkIncomplete(f); |
3016 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3017 |
|
fs[i].complete(one); |
3022 |
|
} |
3023 |
|
|
3024 |
|
public void testAllOf_backwards() throws Exception { |
3025 |
< |
for (int k = 1; k < 20; ++k) { |
3025 |
> |
for (int k = 1; k < 10; k++) { |
3026 |
|
CompletableFuture<Integer>[] fs |
3027 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3028 |
< |
for (int i = 0; i < k; ++i) |
3028 |
> |
for (int i = 0; i < k; i++) |
3029 |
|
fs[i] = new CompletableFuture<>(); |
3030 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3031 |
|
for (int i = k - 1; i >= 0; i--) { |
3038 |
|
} |
3039 |
|
} |
3040 |
|
|
3041 |
+ |
public void testAllOf_exceptional() throws Exception { |
3042 |
+ |
for (int k = 1; k < 10; k++) { |
3043 |
+ |
CompletableFuture<Integer>[] fs |
3044 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3045 |
+ |
CFException ex = new CFException(); |
3046 |
+ |
for (int i = 0; i < k; i++) |
3047 |
+ |
fs[i] = new CompletableFuture<>(); |
3048 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3049 |
+ |
for (int i = 0; i < k; i++) { |
3050 |
+ |
checkIncomplete(f); |
3051 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3052 |
+ |
if (i != k/2) { |
3053 |
+ |
fs[i].complete(i); |
3054 |
+ |
checkCompletedNormally(fs[i], i); |
3055 |
+ |
} else { |
3056 |
+ |
fs[i].completeExceptionally(ex); |
3057 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3058 |
+ |
} |
3059 |
+ |
} |
3060 |
+ |
checkCompletedWithWrappedException(f, ex); |
3061 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3062 |
+ |
} |
3063 |
+ |
} |
3064 |
+ |
|
3065 |
|
/** |
3066 |
|
* anyOf(no component futures) returns an incomplete future |
3067 |
|
*/ |
3068 |
|
public void testAnyOf_empty() throws Exception { |
3069 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3070 |
+ |
{ |
3071 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3072 |
|
checkIncomplete(f); |
3073 |
< |
} |
3073 |
> |
|
3074 |
> |
f.complete(v1); |
3075 |
> |
checkCompletedNormally(f, v1); |
3076 |
> |
}} |
3077 |
|
|
3078 |
|
/** |
3079 |
|
* anyOf returns a future completed normally with a value when |
3080 |
|
* a component future does |
3081 |
|
*/ |
3082 |
|
public void testAnyOf_normal() throws Exception { |
3083 |
< |
for (int k = 0; k < 10; ++k) { |
3083 |
> |
for (int k = 0; k < 10; k++) { |
3084 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3085 |
< |
for (int i = 0; i < k; ++i) |
3085 |
> |
for (int i = 0; i < k; i++) |
3086 |
|
fs[i] = new CompletableFuture<>(); |
3087 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3088 |
|
checkIncomplete(f); |
3089 |
< |
for (int i = 0; i < k; ++i) { |
3090 |
< |
fs[i].complete(one); |
3091 |
< |
checkCompletedNormally(f, one); |
3092 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3089 |
> |
for (int i = 0; i < k; i++) { |
3090 |
> |
fs[i].complete(i); |
3091 |
> |
checkCompletedNormally(f, 0); |
3092 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3093 |
> |
assertTrue(0 <= x && x <= i); |
3094 |
> |
} |
3095 |
> |
} |
3096 |
> |
} |
3097 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3098 |
> |
for (int k = 0; k < 10; k++) { |
3099 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3100 |
> |
for (int i = 0; i < k; i++) |
3101 |
> |
fs[i] = new CompletableFuture<>(); |
3102 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3103 |
> |
checkIncomplete(f); |
3104 |
> |
for (int i = k - 1; i >= 0; i--) { |
3105 |
> |
fs[i].complete(i); |
3106 |
> |
checkCompletedNormally(f, k - 1); |
3107 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3108 |
> |
assertTrue(i <= x && x <= k - 1); |
3109 |
|
} |
3110 |
|
} |
3111 |
|
} |
3114 |
|
* anyOf result completes exceptionally when any component does. |
3115 |
|
*/ |
3116 |
|
public void testAnyOf_exceptional() throws Exception { |
3117 |
< |
for (int k = 0; k < 10; ++k) { |
3117 |
> |
for (int k = 0; k < 10; k++) { |
3118 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3119 |
> |
CFException[] exs = new CFException[k]; |
3120 |
> |
for (int i = 0; i < k; i++) { |
3121 |
> |
fs[i] = new CompletableFuture<>(); |
3122 |
> |
exs[i] = new CFException(); |
3123 |
> |
} |
3124 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3125 |
> |
checkIncomplete(f); |
3126 |
> |
for (int i = 0; i < k; i++) { |
3127 |
> |
fs[i].completeExceptionally(exs[i]); |
3128 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3129 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3130 |
> |
} |
3131 |
> |
} |
3132 |
> |
} |
3133 |
> |
|
3134 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3135 |
> |
for (int k = 0; k < 10; k++) { |
3136 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3137 |
< |
for (int i = 0; i < k; ++i) |
3137 |
> |
CFException[] exs = new CFException[k]; |
3138 |
> |
for (int i = 0; i < k; i++) { |
3139 |
|
fs[i] = new CompletableFuture<>(); |
3140 |
+ |
exs[i] = new CFException(); |
3141 |
+ |
} |
3142 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3143 |
|
checkIncomplete(f); |
3144 |
< |
for (int i = 0; i < k; ++i) { |
3145 |
< |
fs[i].completeExceptionally(new CFException()); |
3146 |
< |
checkCompletedWithWrappedCFException(f); |
3144 |
> |
for (int i = k - 1; i >= 0; i--) { |
3145 |
> |
fs[i].completeExceptionally(exs[i]); |
3146 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3147 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3148 |
|
} |
3149 |
|
} |
3162 |
|
Runnable[] throwingActions = { |
3163 |
|
() -> CompletableFuture.supplyAsync(null), |
3164 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3165 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3165 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3166 |
|
|
3167 |
|
() -> CompletableFuture.runAsync(null), |
3168 |
|
() -> CompletableFuture.runAsync(null, exec), |
3267 |
|
assertSame(f, f.toCompletableFuture()); |
3268 |
|
} |
3269 |
|
|
3270 |
< |
// public void testRunAfterEither_resultDeterminedAtTimeOfCreation() { |
3271 |
< |
// for (ExecutionMode m : ExecutionMode.values()) |
3272 |
< |
// for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3273 |
< |
// for (Integer v1 : new Integer[] { 1, null }) |
3274 |
< |
// { |
3275 |
< |
// final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3276 |
< |
// final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3277 |
< |
// final Noop[] rs = new Noop[2]; |
3278 |
< |
// for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
3279 |
< |
// f.complete(v1); |
3280 |
< |
// final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
3281 |
< |
// final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
3282 |
< |
// assertTrue(g.cancel(mayInterruptIfRunning)); |
3283 |
< |
// checkCompletedNormally(h0, null); |
3284 |
< |
// checkCompletedNormally(h1, null); |
3285 |
< |
// for (Noop r : rs) r.assertInvoked(); |
3286 |
< |
// }} |
3270 |
> |
//--- tests of implementation details; not part of official tck --- |
3271 |
> |
|
3272 |
> |
Object resultOf(CompletableFuture<?> f) { |
3273 |
> |
try { |
3274 |
> |
java.lang.reflect.Field resultField |
3275 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3276 |
> |
resultField.setAccessible(true); |
3277 |
> |
return resultField.get(f); |
3278 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3279 |
> |
} |
3280 |
> |
|
3281 |
> |
public void testExceptionPropagationReusesResultObject() { |
3282 |
> |
if (!testImplementationDetails) return; |
3283 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3284 |
> |
{ |
3285 |
> |
final CFException ex = new CFException(); |
3286 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3287 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3288 |
> |
|
3289 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3290 |
> |
= new ArrayList<>(); |
3291 |
> |
|
3292 |
> |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3293 |
> |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3294 |
> |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3295 |
> |
|
3296 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3297 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3298 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3299 |
> |
|
3300 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3301 |
> |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3302 |
> |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3303 |
> |
|
3304 |
> |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3305 |
> |
|
3306 |
> |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3307 |
> |
|
3308 |
> |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3309 |
> |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3310 |
> |
|
3311 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3312 |
> |
fun : funs) { |
3313 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3314 |
> |
f.completeExceptionally(ex); |
3315 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3316 |
> |
checkCompletedWithWrappedException(src, ex); |
3317 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3318 |
> |
checkCompletedWithWrappedException(dep, ex); |
3319 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3320 |
> |
} |
3321 |
> |
|
3322 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3323 |
> |
fun : funs) { |
3324 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3325 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3326 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3327 |
> |
f.completeExceptionally(ex); |
3328 |
> |
checkCompletedWithWrappedException(src, ex); |
3329 |
> |
checkCompletedWithWrappedException(dep, ex); |
3330 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3331 |
> |
} |
3332 |
> |
|
3333 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3334 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3335 |
> |
fun : funs) { |
3336 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3337 |
> |
f.cancel(mayInterruptIfRunning); |
3338 |
> |
checkCancelled(f); |
3339 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3340 |
> |
checkCompletedWithWrappedCancellationException(src); |
3341 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3342 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3343 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3344 |
> |
} |
3345 |
> |
|
3346 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3347 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3348 |
> |
fun : funs) { |
3349 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3350 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3351 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3352 |
> |
f.cancel(mayInterruptIfRunning); |
3353 |
> |
checkCancelled(f); |
3354 |
> |
checkCompletedWithWrappedCancellationException(src); |
3355 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3356 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3357 |
> |
} |
3358 |
> |
}} |
3359 |
|
|
3360 |
|
} |