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.TimeUnit; |
25 |
|
import java.util.concurrent.atomic.AtomicInteger; |
26 |
< |
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; |
26 |
> |
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import java.util.function.BiConsumer; |
30 |
– |
import java.util.function.Function; |
28 |
|
import java.util.function.BiFunction; |
29 |
+ |
import java.util.function.Consumer; |
30 |
+ |
import java.util.function.Function; |
31 |
+ |
import java.util.function.Supplier; |
32 |
+ |
|
33 |
+ |
import junit.framework.Test; |
34 |
+ |
import junit.framework.TestSuite; |
35 |
|
|
36 |
|
public class CompletableFutureTest extends JSR166TestCase { |
37 |
|
|
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run(suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
public static Test suite() { |
42 |
|
return new TestSuite(CompletableFutureTest.class); |
47 |
|
void checkIncomplete(CompletableFuture<?> f) { |
48 |
|
assertFalse(f.isDone()); |
49 |
|
assertFalse(f.isCancelled()); |
50 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
50 |
> |
assertTrue(f.toString().contains("Not completed")); |
51 |
|
try { |
52 |
|
assertNull(f.getNow(null)); |
53 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
60 |
|
} |
61 |
|
|
62 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
63 |
< |
try { |
64 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
63 |
> |
checkTimedGet(f, value); |
64 |
> |
|
65 |
|
try { |
66 |
|
assertEquals(value, f.join()); |
67 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
78 |
|
} |
79 |
|
|
80 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
81 |
+ |
long startTime = System.nanoTime(); |
82 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
83 |
|
try { |
84 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
84 |
> |
f.get(timeoutMillis, MILLISECONDS); |
85 |
|
shouldThrow(); |
86 |
|
} catch (ExecutionException success) { |
87 |
|
assertTrue(success.getCause() instanceof CFException); |
88 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
89 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
90 |
+ |
|
91 |
|
try { |
92 |
|
f.join(); |
93 |
|
shouldThrow(); |
113 |
|
|
114 |
|
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
115 |
|
Throwable ex) { |
116 |
+ |
long startTime = System.nanoTime(); |
117 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
118 |
|
try { |
119 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
119 |
> |
f.get(timeoutMillis, MILLISECONDS); |
120 |
|
shouldThrow(); |
121 |
|
} catch (ExecutionException success) { |
122 |
|
assertSame(ex, success.getCause()); |
123 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
125 |
+ |
|
126 |
|
try { |
127 |
|
f.join(); |
128 |
|
shouldThrow(); |
147 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
148 |
|
} |
149 |
|
|
150 |
+ |
<U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) { |
151 |
+ |
long startTime = System.nanoTime(); |
152 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
153 |
+ |
try { |
154 |
+ |
f.get(timeoutMillis, MILLISECONDS); |
155 |
+ |
shouldThrow(); |
156 |
+ |
} catch (ExecutionException ex) { |
157 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
158 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
160 |
+ |
|
161 |
+ |
try { |
162 |
+ |
f.join(); |
163 |
+ |
shouldThrow(); |
164 |
+ |
} catch (Throwable ex) { |
165 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
166 |
+ |
} |
167 |
+ |
|
168 |
+ |
try { |
169 |
+ |
f.getNow(null); |
170 |
+ |
shouldThrow(); |
171 |
+ |
} catch (Throwable ex) { |
172 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
173 |
+ |
} |
174 |
+ |
|
175 |
+ |
try { |
176 |
+ |
f.get(); |
177 |
+ |
shouldThrow(); |
178 |
+ |
} catch (ExecutionException ex) { |
179 |
+ |
assertTrue(ex.getCause() instanceof TimeoutException); |
180 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
181 |
+ |
|
182 |
+ |
assertTrue(f.isDone()); |
183 |
+ |
assertFalse(f.isCancelled()); |
184 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
185 |
+ |
} |
186 |
+ |
|
187 |
|
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
188 |
|
Throwable ex) { |
189 |
|
checkCompletedExceptionallyWithRootCause(f, ex); |
205 |
|
} |
206 |
|
|
207 |
|
void checkCancelled(CompletableFuture<?> f) { |
208 |
+ |
long startTime = System.nanoTime(); |
209 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
210 |
|
try { |
211 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
211 |
> |
f.get(timeoutMillis, MILLISECONDS); |
212 |
|
shouldThrow(); |
213 |
|
} catch (CancellationException success) { |
214 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
215 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
216 |
+ |
|
217 |
|
try { |
218 |
|
f.join(); |
219 |
|
shouldThrow(); |
234 |
|
} |
235 |
|
|
236 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
237 |
+ |
long startTime = System.nanoTime(); |
238 |
+ |
long timeoutMillis = LONG_DELAY_MS; |
239 |
|
try { |
240 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
240 |
> |
f.get(timeoutMillis, MILLISECONDS); |
241 |
|
shouldThrow(); |
242 |
|
} catch (ExecutionException success) { |
243 |
|
assertTrue(success.getCause() instanceof CancellationException); |
244 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
245 |
+ |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
246 |
+ |
|
247 |
|
try { |
248 |
|
f.join(); |
249 |
|
shouldThrow(); |
312 |
|
{ |
313 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
314 |
|
checkIncomplete(f); |
315 |
< |
assertTrue(f.cancel(true)); |
316 |
< |
assertTrue(f.cancel(true)); |
315 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
316 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
317 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
318 |
|
checkCancelled(f); |
319 |
|
}} |
320 |
|
|
586 |
|
} |
587 |
|
} |
588 |
|
|
533 |
– |
|
589 |
|
class CompletableFutureInc extends CheckedIntegerAction |
590 |
|
implements Function<Integer, CompletableFuture<Integer>> |
591 |
|
{ |
624 |
|
} |
625 |
|
} |
626 |
|
|
627 |
+ |
static final boolean defaultExecutorIsCommonPool |
628 |
+ |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
629 |
+ |
|
630 |
|
/** |
631 |
|
* Permits the testing of parallel code for the 3 different |
632 |
|
* execution modes without copy/pasting all the test methods. |
633 |
|
*/ |
634 |
|
enum ExecutionMode { |
635 |
< |
DEFAULT { |
635 |
> |
SYNC { |
636 |
|
public void checkExecutionMode() { |
637 |
|
assertFalse(ThreadExecutor.startedCurrentThread()); |
638 |
|
assertNull(ForkJoinTask.getPool()); |
708 |
|
|
709 |
|
ASYNC { |
710 |
|
public void checkExecutionMode() { |
711 |
< |
assertSame(ForkJoinPool.commonPool(), |
712 |
< |
ForkJoinTask.getPool()); |
711 |
> |
assertEquals(defaultExecutorIsCommonPool, |
712 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
713 |
|
} |
714 |
|
public CompletableFuture<Void> runAsync(Runnable a) { |
715 |
|
return CompletableFuture.runAsync(a); |
933 |
|
if (!createIncomplete) f.completeExceptionally(ex); |
934 |
|
final CompletableFuture<Integer> g = f.exceptionally |
935 |
|
((Throwable t) -> { |
936 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
936 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
937 |
|
threadAssertSame(t, ex); |
938 |
|
a.getAndIncrement(); |
939 |
|
return v1; |
946 |
|
|
947 |
|
public void testExceptionally_exceptionalCompletionActionFailed() { |
948 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
891 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
949 |
|
{ |
950 |
|
final AtomicInteger a = new AtomicInteger(0); |
951 |
|
final CFException ex1 = new CFException(); |
954 |
|
if (!createIncomplete) f.completeExceptionally(ex1); |
955 |
|
final CompletableFuture<Integer> g = f.exceptionally |
956 |
|
((Throwable t) -> { |
957 |
< |
ExecutionMode.DEFAULT.checkExecutionMode(); |
957 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
958 |
|
threadAssertSame(t, ex1); |
959 |
|
a.getAndIncrement(); |
960 |
|
throw ex2; |
999 |
|
public void testWhenComplete_exceptionalCompletion() { |
1000 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1001 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
945 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1002 |
|
{ |
1003 |
|
final AtomicInteger a = new AtomicInteger(0); |
1004 |
|
final CFException ex = new CFException(); |
1083 |
|
public void testWhenComplete_actionFailedSourceFailed() { |
1084 |
|
for (boolean createIncomplete : new boolean[] { true, false }) |
1085 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1030 |
– |
for (Integer v1 : new Integer[] { 1, null }) |
1086 |
|
{ |
1087 |
|
final AtomicInteger a = new AtomicInteger(0); |
1088 |
|
final CFException ex1 = new CFException(); |
1319 |
|
*/ |
1320 |
|
public void testThenRun_normalCompletion() { |
1321 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1267 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1322 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1323 |
|
{ |
1324 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1325 |
< |
final Noop r = new Noop(m); |
1326 |
< |
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 |
< |
} |
1325 |
> |
final Noop[] rs = new Noop[6]; |
1326 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1327 |
|
|
1328 |
< |
checkCompletedNormally(g, null); |
1328 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1329 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1330 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1331 |
> |
checkIncomplete(h0); |
1332 |
> |
checkIncomplete(h1); |
1333 |
> |
checkIncomplete(h2); |
1334 |
> |
assertTrue(f.complete(v1)); |
1335 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1336 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1337 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1338 |
> |
|
1339 |
> |
checkCompletedNormally(h0, null); |
1340 |
> |
checkCompletedNormally(h1, null); |
1341 |
> |
checkCompletedNormally(h2, null); |
1342 |
> |
checkCompletedNormally(h3, null); |
1343 |
> |
checkCompletedNormally(h4, null); |
1344 |
> |
checkCompletedNormally(h5, null); |
1345 |
|
checkCompletedNormally(f, v1); |
1346 |
< |
r.assertInvoked(); |
1346 |
> |
for (Noop r : rs) r.assertInvoked(); |
1347 |
|
}} |
1348 |
|
|
1349 |
|
/** |
1352 |
|
*/ |
1353 |
|
public void testThenRun_exceptionalCompletion() { |
1354 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1290 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1355 |
|
{ |
1356 |
|
final CFException ex = new CFException(); |
1357 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1358 |
< |
final Noop r = new Noop(m); |
1359 |
< |
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 |
< |
} |
1358 |
> |
final Noop[] rs = new Noop[6]; |
1359 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1360 |
|
|
1361 |
< |
checkCompletedWithWrappedException(g, ex); |
1361 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1362 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1363 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1364 |
> |
checkIncomplete(h0); |
1365 |
> |
checkIncomplete(h1); |
1366 |
> |
checkIncomplete(h2); |
1367 |
> |
assertTrue(f.completeExceptionally(ex)); |
1368 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1369 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1370 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1371 |
> |
|
1372 |
> |
checkCompletedWithWrappedException(h0, ex); |
1373 |
> |
checkCompletedWithWrappedException(h1, ex); |
1374 |
> |
checkCompletedWithWrappedException(h2, ex); |
1375 |
> |
checkCompletedWithWrappedException(h3, ex); |
1376 |
> |
checkCompletedWithWrappedException(h4, ex); |
1377 |
> |
checkCompletedWithWrappedException(h5, ex); |
1378 |
|
checkCompletedExceptionally(f, ex); |
1379 |
< |
r.assertNotInvoked(); |
1379 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1380 |
|
}} |
1381 |
|
|
1382 |
|
/** |
1384 |
|
*/ |
1385 |
|
public void testThenRun_sourceCancelled() { |
1386 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1312 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1387 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1388 |
|
{ |
1389 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1390 |
< |
final Noop r = new Noop(m); |
1391 |
< |
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 |
< |
} |
1390 |
> |
final Noop[] rs = new Noop[6]; |
1391 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1392 |
|
|
1393 |
< |
checkCompletedWithWrappedCancellationException(g); |
1393 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1394 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1395 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1396 |
> |
checkIncomplete(h0); |
1397 |
> |
checkIncomplete(h1); |
1398 |
> |
checkIncomplete(h2); |
1399 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1400 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1401 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1402 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1403 |
> |
|
1404 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1405 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1406 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1407 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1408 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1409 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1410 |
|
checkCancelled(f); |
1411 |
< |
r.assertNotInvoked(); |
1411 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1412 |
|
}} |
1413 |
|
|
1414 |
|
/** |
1416 |
|
*/ |
1417 |
|
public void testThenRun_actionFailed() { |
1418 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1334 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1419 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1420 |
|
{ |
1421 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1422 |
< |
final FailingRunnable r = new FailingRunnable(m); |
1423 |
< |
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 |
< |
} |
1422 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1423 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1424 |
|
|
1425 |
< |
checkCompletedWithWrappedCFException(g); |
1425 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1426 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1427 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1428 |
> |
assertTrue(f.complete(v1)); |
1429 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1430 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1431 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1432 |
> |
|
1433 |
> |
checkCompletedWithWrappedCFException(h0); |
1434 |
> |
checkCompletedWithWrappedCFException(h1); |
1435 |
> |
checkCompletedWithWrappedCFException(h2); |
1436 |
> |
checkCompletedWithWrappedCFException(h3); |
1437 |
> |
checkCompletedWithWrappedCFException(h4); |
1438 |
> |
checkCompletedWithWrappedCFException(h5); |
1439 |
|
checkCompletedNormally(f, v1); |
1440 |
|
}} |
1441 |
|
|
1444 |
|
*/ |
1445 |
|
public void testThenApply_normalCompletion() { |
1446 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1355 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1447 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1448 |
|
{ |
1449 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1450 |
< |
final IncFunction r = new IncFunction(m); |
1451 |
< |
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 |
< |
} |
1450 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1451 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1452 |
|
|
1453 |
< |
checkCompletedNormally(g, inc(v1)); |
1453 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1454 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1455 |
> |
checkIncomplete(h0); |
1456 |
> |
checkIncomplete(h1); |
1457 |
> |
assertTrue(f.complete(v1)); |
1458 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1459 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1460 |
> |
|
1461 |
> |
checkCompletedNormally(h0, inc(v1)); |
1462 |
> |
checkCompletedNormally(h1, inc(v1)); |
1463 |
> |
checkCompletedNormally(h2, inc(v1)); |
1464 |
> |
checkCompletedNormally(h3, inc(v1)); |
1465 |
|
checkCompletedNormally(f, v1); |
1466 |
< |
r.assertValue(inc(v1)); |
1466 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1467 |
|
}} |
1468 |
|
|
1469 |
|
/** |
1472 |
|
*/ |
1473 |
|
public void testThenApply_exceptionalCompletion() { |
1474 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1378 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1475 |
|
{ |
1476 |
|
final CFException ex = new CFException(); |
1477 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1478 |
< |
final IncFunction r = new IncFunction(m); |
1479 |
< |
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 |
< |
} |
1478 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1479 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1480 |
|
|
1481 |
< |
checkCompletedWithWrappedException(g, ex); |
1481 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1482 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1483 |
> |
assertTrue(f.completeExceptionally(ex)); |
1484 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1485 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1486 |
> |
|
1487 |
> |
checkCompletedWithWrappedException(h0, ex); |
1488 |
> |
checkCompletedWithWrappedException(h1, ex); |
1489 |
> |
checkCompletedWithWrappedException(h2, ex); |
1490 |
> |
checkCompletedWithWrappedException(h3, ex); |
1491 |
|
checkCompletedExceptionally(f, ex); |
1492 |
< |
r.assertNotInvoked(); |
1492 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1493 |
|
}} |
1494 |
|
|
1495 |
|
/** |
1497 |
|
*/ |
1498 |
|
public void testThenApply_sourceCancelled() { |
1499 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1400 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1500 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1501 |
|
{ |
1502 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1503 |
< |
final IncFunction r = new IncFunction(m); |
1504 |
< |
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 |
< |
} |
1503 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1504 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1505 |
|
|
1506 |
< |
checkCompletedWithWrappedCancellationException(g); |
1506 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1507 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1508 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1509 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1510 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1511 |
> |
|
1512 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1513 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1514 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1515 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1516 |
|
checkCancelled(f); |
1517 |
< |
r.assertNotInvoked(); |
1517 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1518 |
|
}} |
1519 |
|
|
1520 |
|
/** |
1522 |
|
*/ |
1523 |
|
public void testThenApply_actionFailed() { |
1524 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1422 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1525 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1526 |
|
{ |
1527 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1528 |
< |
final FailingFunction r = new FailingFunction(m); |
1529 |
< |
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 |
< |
} |
1528 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1529 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1530 |
|
|
1531 |
< |
checkCompletedWithWrappedCFException(g); |
1531 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1532 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1533 |
> |
assertTrue(f.complete(v1)); |
1534 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1535 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1536 |
> |
|
1537 |
> |
checkCompletedWithWrappedCFException(h0); |
1538 |
> |
checkCompletedWithWrappedCFException(h1); |
1539 |
> |
checkCompletedWithWrappedCFException(h2); |
1540 |
> |
checkCompletedWithWrappedCFException(h3); |
1541 |
|
checkCompletedNormally(f, v1); |
1542 |
|
}} |
1543 |
|
|
1546 |
|
*/ |
1547 |
|
public void testThenAccept_normalCompletion() { |
1548 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1443 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1549 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1550 |
|
{ |
1551 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1552 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1553 |
< |
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 |
< |
} |
1552 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1553 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1554 |
|
|
1555 |
< |
checkCompletedNormally(g, null); |
1556 |
< |
r.assertValue(v1); |
1555 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1556 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1557 |
> |
checkIncomplete(h0); |
1558 |
> |
checkIncomplete(h1); |
1559 |
> |
assertTrue(f.complete(v1)); |
1560 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1561 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1562 |
> |
|
1563 |
> |
checkCompletedNormally(h0, null); |
1564 |
> |
checkCompletedNormally(h1, null); |
1565 |
> |
checkCompletedNormally(h2, null); |
1566 |
> |
checkCompletedNormally(h3, null); |
1567 |
|
checkCompletedNormally(f, v1); |
1568 |
+ |
for (NoopConsumer r : rs) r.assertValue(v1); |
1569 |
|
}} |
1570 |
|
|
1571 |
|
/** |
1574 |
|
*/ |
1575 |
|
public void testThenAccept_exceptionalCompletion() { |
1576 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1466 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1577 |
|
{ |
1578 |
|
final CFException ex = new CFException(); |
1579 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1580 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1581 |
< |
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 |
< |
} |
1580 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1581 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1582 |
|
|
1583 |
< |
checkCompletedWithWrappedException(g, ex); |
1583 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1584 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1585 |
> |
assertTrue(f.completeExceptionally(ex)); |
1586 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1587 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1588 |
> |
|
1589 |
> |
checkCompletedWithWrappedException(h0, ex); |
1590 |
> |
checkCompletedWithWrappedException(h1, ex); |
1591 |
> |
checkCompletedWithWrappedException(h2, ex); |
1592 |
> |
checkCompletedWithWrappedException(h3, ex); |
1593 |
|
checkCompletedExceptionally(f, ex); |
1594 |
< |
r.assertNotInvoked(); |
1594 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1595 |
|
}} |
1596 |
|
|
1597 |
|
/** |
1599 |
|
*/ |
1600 |
|
public void testThenAccept_sourceCancelled() { |
1601 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1488 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1602 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1603 |
|
{ |
1604 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1605 |
< |
final NoopConsumer r = new NoopConsumer(m); |
1606 |
< |
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 |
< |
} |
1605 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1606 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1607 |
|
|
1608 |
< |
checkCompletedWithWrappedCancellationException(g); |
1608 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1609 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1610 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1611 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1612 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1613 |
> |
|
1614 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1615 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1616 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1617 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1618 |
|
checkCancelled(f); |
1619 |
< |
r.assertNotInvoked(); |
1619 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1620 |
|
}} |
1621 |
|
|
1622 |
|
/** |
1624 |
|
*/ |
1625 |
|
public void testThenAccept_actionFailed() { |
1626 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1510 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1627 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1628 |
|
{ |
1629 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1630 |
< |
final FailingConsumer r = new FailingConsumer(m); |
1631 |
< |
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 |
< |
} |
1630 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1631 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1632 |
|
|
1633 |
< |
checkCompletedWithWrappedCFException(g); |
1633 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1634 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1635 |
> |
assertTrue(f.complete(v1)); |
1636 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1637 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1638 |
> |
|
1639 |
> |
checkCompletedWithWrappedCFException(h0); |
1640 |
> |
checkCompletedWithWrappedCFException(h1); |
1641 |
> |
checkCompletedWithWrappedCFException(h2); |
1642 |
> |
checkCompletedWithWrappedCFException(h3); |
1643 |
|
checkCompletedNormally(f, v1); |
1644 |
|
}} |
1645 |
|
|
1649 |
|
*/ |
1650 |
|
public void testThenCombine_normalCompletion() { |
1651 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1532 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1652 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1653 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1654 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1655 |
|
{ |
1656 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1657 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1658 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1658 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1659 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1660 |
|
|
1661 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1662 |
< |
if (!createIncomplete) |
1663 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1664 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1665 |
< |
if (createIncomplete) { |
1666 |
< |
checkIncomplete(h); |
1667 |
< |
r.assertNotInvoked(); |
1668 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1669 |
< |
} |
1661 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1662 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1663 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1664 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1665 |
> |
|
1666 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1667 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1668 |
> |
assertTrue(fst.complete(w1)); |
1669 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1670 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1671 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1672 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1673 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1674 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1675 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1676 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1677 |
> |
assertTrue(snd.complete(w2)); |
1678 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1679 |
> |
|
1680 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1681 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1682 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1683 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1684 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1685 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1686 |
|
|
1551 |
– |
checkCompletedNormally(h, subtract(v1, v2)); |
1687 |
|
checkCompletedNormally(f, v1); |
1688 |
|
checkCompletedNormally(g, v2); |
1554 |
– |
r.assertValue(subtract(v1, v2)); |
1689 |
|
}} |
1690 |
|
|
1691 |
|
/** |
1692 |
|
* thenCombine result completes exceptionally after exceptional |
1693 |
|
* completion of either source |
1694 |
|
*/ |
1695 |
< |
public void testThenCombine_exceptionalCompletion() { |
1695 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1696 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1563 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1697 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1698 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1699 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1700 |
|
{ |
1701 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1702 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1703 |
|
final CFException ex = new CFException(); |
1704 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1705 |
< |
|
1706 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1707 |
< |
if (!createIncomplete) |
1708 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1709 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1710 |
< |
if (createIncomplete) { |
1711 |
< |
checkIncomplete(h); |
1712 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1713 |
< |
} |
1704 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1705 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1706 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1707 |
> |
|
1708 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1709 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1710 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1711 |
> |
() -> fst.completeExceptionally(ex) : |
1712 |
> |
() -> fst.complete(v1); |
1713 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1714 |
> |
() -> snd.complete(v1) : |
1715 |
> |
() -> snd.completeExceptionally(ex); |
1716 |
> |
|
1717 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1718 |
> |
assertTrue(complete1.call()); |
1719 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1720 |
> |
checkIncomplete(h1); |
1721 |
> |
checkIncomplete(h2); |
1722 |
> |
assertTrue(complete2.call()); |
1723 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1724 |
|
|
1725 |
< |
checkCompletedWithWrappedException(h, ex); |
1726 |
< |
r.assertNotInvoked(); |
1727 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1728 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1725 |
> |
checkCompletedWithWrappedException(h1, ex); |
1726 |
> |
checkCompletedWithWrappedException(h2, ex); |
1727 |
> |
checkCompletedWithWrappedException(h3, ex); |
1728 |
> |
r1.assertNotInvoked(); |
1729 |
> |
r2.assertNotInvoked(); |
1730 |
> |
r3.assertNotInvoked(); |
1731 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1732 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1733 |
|
}} |
1734 |
|
|
1735 |
|
/** |
1736 |
|
* thenCombine result completes exceptionally if either source cancelled |
1737 |
|
*/ |
1738 |
< |
public void testThenCombine_sourceCancelled() { |
1738 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1739 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1740 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1593 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1741 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1742 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1743 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1744 |
|
{ |
1745 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1746 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1747 |
< |
final SubtractFunction r = new SubtractFunction(m); |
1747 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1748 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1749 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1750 |
|
|
1751 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1752 |
< |
if (!createIncomplete) |
1753 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1754 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1755 |
< |
if (createIncomplete) { |
1756 |
< |
checkIncomplete(h); |
1757 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1758 |
< |
} |
1751 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1752 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1753 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1754 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1755 |
> |
() -> fst.complete(v1); |
1756 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1757 |
> |
() -> snd.complete(v1) : |
1758 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1759 |
|
|
1760 |
< |
checkCompletedWithWrappedCancellationException(h); |
1761 |
< |
checkCancelled(!fFirst ? f : g); |
1762 |
< |
r.assertNotInvoked(); |
1763 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1760 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1761 |
> |
assertTrue(complete1.call()); |
1762 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1763 |
> |
checkIncomplete(h1); |
1764 |
> |
checkIncomplete(h2); |
1765 |
> |
assertTrue(complete2.call()); |
1766 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1767 |
> |
|
1768 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1769 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1770 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1771 |
> |
r1.assertNotInvoked(); |
1772 |
> |
r2.assertNotInvoked(); |
1773 |
> |
r3.assertNotInvoked(); |
1774 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1775 |
> |
checkCancelled(failFirst ? fst : snd); |
1776 |
|
}} |
1777 |
|
|
1778 |
|
/** |
1786 |
|
{ |
1787 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1788 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1789 |
< |
final FailingBiFunction r = new FailingBiFunction(m); |
1790 |
< |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1789 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1790 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1791 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1792 |
> |
|
1793 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1794 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1795 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1796 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1797 |
> |
|
1798 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1799 |
> |
assertTrue(fst.complete(w1)); |
1800 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1801 |
> |
assertTrue(snd.complete(w2)); |
1802 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1803 |
|
|
1804 |
< |
assertTrue( fFirst ? f.complete(v1) : g.complete(v2)); |
1805 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1806 |
< |
|
1807 |
< |
checkCompletedWithWrappedCFException(h); |
1804 |
> |
checkCompletedWithWrappedCFException(h1); |
1805 |
> |
checkCompletedWithWrappedCFException(h2); |
1806 |
> |
checkCompletedWithWrappedCFException(h3); |
1807 |
> |
r1.assertInvoked(); |
1808 |
> |
r2.assertInvoked(); |
1809 |
> |
r3.assertInvoked(); |
1810 |
|
checkCompletedNormally(f, v1); |
1811 |
|
checkCompletedNormally(g, v2); |
1812 |
|
}} |
1817 |
|
*/ |
1818 |
|
public void testThenAcceptBoth_normalCompletion() { |
1819 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1644 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1820 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1821 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1822 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1823 |
|
{ |
1824 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1825 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1826 |
< |
final SubtractAction r = new SubtractAction(m); |
1827 |
< |
|
1828 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1829 |
< |
if (!createIncomplete) |
1830 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1831 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1832 |
< |
if (createIncomplete) { |
1833 |
< |
checkIncomplete(h); |
1834 |
< |
r.assertNotInvoked(); |
1835 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1836 |
< |
} |
1826 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1827 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1828 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1829 |
> |
|
1830 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1831 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1832 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1833 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1834 |
> |
|
1835 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1836 |
> |
assertTrue(fst.complete(w1)); |
1837 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1838 |
> |
checkIncomplete(h1); |
1839 |
> |
checkIncomplete(h2); |
1840 |
> |
r1.assertNotInvoked(); |
1841 |
> |
r2.assertNotInvoked(); |
1842 |
> |
assertTrue(snd.complete(w2)); |
1843 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1844 |
|
|
1845 |
< |
checkCompletedNormally(h, null); |
1846 |
< |
r.assertValue(subtract(v1, v2)); |
1845 |
> |
checkCompletedNormally(h1, null); |
1846 |
> |
checkCompletedNormally(h2, null); |
1847 |
> |
checkCompletedNormally(h3, null); |
1848 |
> |
r1.assertValue(subtract(v1, v2)); |
1849 |
> |
r2.assertValue(subtract(v1, v2)); |
1850 |
> |
r3.assertValue(subtract(v1, v2)); |
1851 |
|
checkCompletedNormally(f, v1); |
1852 |
|
checkCompletedNormally(g, v2); |
1853 |
|
}} |
1856 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1857 |
|
* completion of either source |
1858 |
|
*/ |
1859 |
< |
public void testThenAcceptBoth_exceptionalCompletion() { |
1859 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1860 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1675 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1861 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1862 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1863 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1864 |
|
{ |
1865 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1866 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1867 |
|
final CFException ex = new CFException(); |
1868 |
< |
final SubtractAction r = new SubtractAction(m); |
1869 |
< |
|
1870 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1871 |
< |
if (!createIncomplete) |
1872 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1873 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1874 |
< |
if (createIncomplete) { |
1875 |
< |
checkIncomplete(h); |
1876 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
1877 |
< |
} |
1868 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1869 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1870 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1871 |
> |
|
1872 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1873 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1874 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1875 |
> |
() -> fst.completeExceptionally(ex) : |
1876 |
> |
() -> fst.complete(v1); |
1877 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1878 |
> |
() -> snd.complete(v1) : |
1879 |
> |
() -> snd.completeExceptionally(ex); |
1880 |
> |
|
1881 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1882 |
> |
assertTrue(complete1.call()); |
1883 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1884 |
> |
checkIncomplete(h1); |
1885 |
> |
checkIncomplete(h2); |
1886 |
> |
assertTrue(complete2.call()); |
1887 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1888 |
|
|
1889 |
< |
checkCompletedWithWrappedException(h, ex); |
1890 |
< |
r.assertNotInvoked(); |
1891 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1892 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1889 |
> |
checkCompletedWithWrappedException(h1, ex); |
1890 |
> |
checkCompletedWithWrappedException(h2, ex); |
1891 |
> |
checkCompletedWithWrappedException(h3, ex); |
1892 |
> |
r1.assertNotInvoked(); |
1893 |
> |
r2.assertNotInvoked(); |
1894 |
> |
r3.assertNotInvoked(); |
1895 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1896 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1897 |
|
}} |
1898 |
|
|
1899 |
|
/** |
1900 |
|
* thenAcceptBoth result completes exceptionally if either source cancelled |
1901 |
|
*/ |
1902 |
< |
public void testThenAcceptBoth_sourceCancelled() { |
1902 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1903 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1904 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1705 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1905 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1906 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
1907 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1908 |
|
{ |
1909 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1910 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1911 |
< |
final SubtractAction r = new SubtractAction(m); |
1911 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1912 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1913 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1914 |
|
|
1915 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
1916 |
< |
if (!createIncomplete) |
1917 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1918 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1919 |
< |
if (createIncomplete) { |
1920 |
< |
checkIncomplete(h); |
1921 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1922 |
< |
} |
1915 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1916 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1917 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1918 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1919 |
> |
() -> fst.complete(v1); |
1920 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1921 |
> |
() -> snd.complete(v1) : |
1922 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1923 |
|
|
1924 |
< |
checkCompletedWithWrappedCancellationException(h); |
1925 |
< |
checkCancelled(!fFirst ? f : g); |
1926 |
< |
r.assertNotInvoked(); |
1927 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
1924 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1925 |
> |
assertTrue(complete1.call()); |
1926 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1927 |
> |
checkIncomplete(h1); |
1928 |
> |
checkIncomplete(h2); |
1929 |
> |
assertTrue(complete2.call()); |
1930 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1931 |
> |
|
1932 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1933 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1934 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1935 |
> |
r1.assertNotInvoked(); |
1936 |
> |
r2.assertNotInvoked(); |
1937 |
> |
r3.assertNotInvoked(); |
1938 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1939 |
> |
checkCancelled(failFirst ? fst : snd); |
1940 |
|
}} |
1941 |
|
|
1942 |
|
/** |
1950 |
|
{ |
1951 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1952 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1953 |
< |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1954 |
< |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1955 |
< |
|
1956 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1957 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1953 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1954 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1955 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1956 |
> |
|
1957 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1958 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1959 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1960 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1961 |
> |
|
1962 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1963 |
> |
assertTrue(fst.complete(w1)); |
1964 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1965 |
> |
assertTrue(snd.complete(w2)); |
1966 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1967 |
|
|
1968 |
< |
checkCompletedWithWrappedCFException(h); |
1968 |
> |
checkCompletedWithWrappedCFException(h1); |
1969 |
> |
checkCompletedWithWrappedCFException(h2); |
1970 |
> |
checkCompletedWithWrappedCFException(h3); |
1971 |
> |
r1.assertInvoked(); |
1972 |
> |
r2.assertInvoked(); |
1973 |
> |
r3.assertInvoked(); |
1974 |
|
checkCompletedNormally(f, v1); |
1975 |
|
checkCompletedNormally(g, v2); |
1976 |
|
}} |
1981 |
|
*/ |
1982 |
|
public void testRunAfterBoth_normalCompletion() { |
1983 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1756 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
1984 |
|
for (boolean fFirst : new boolean[] { true, false }) |
1985 |
|
for (Integer v1 : new Integer[] { 1, null }) |
1986 |
|
for (Integer v2 : new Integer[] { 2, null }) |
1987 |
|
{ |
1988 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1989 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1990 |
< |
final Noop r = new Noop(m); |
1991 |
< |
|
1992 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
1993 |
< |
if (!createIncomplete) |
1994 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
1995 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1996 |
< |
if (createIncomplete) { |
1997 |
< |
checkIncomplete(h); |
1998 |
< |
r.assertNotInvoked(); |
1999 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
2000 |
< |
} |
1990 |
> |
final Noop r1 = new Noop(m); |
1991 |
> |
final Noop r2 = new Noop(m); |
1992 |
> |
final Noop r3 = new Noop(m); |
1993 |
> |
|
1994 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1995 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1996 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1997 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1998 |
> |
|
1999 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2000 |
> |
assertTrue(fst.complete(w1)); |
2001 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2002 |
> |
checkIncomplete(h1); |
2003 |
> |
checkIncomplete(h2); |
2004 |
> |
r1.assertNotInvoked(); |
2005 |
> |
r2.assertNotInvoked(); |
2006 |
> |
assertTrue(snd.complete(w2)); |
2007 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2008 |
|
|
2009 |
< |
checkCompletedNormally(h, null); |
2010 |
< |
r.assertInvoked(); |
2009 |
> |
checkCompletedNormally(h1, null); |
2010 |
> |
checkCompletedNormally(h2, null); |
2011 |
> |
checkCompletedNormally(h3, null); |
2012 |
> |
r1.assertInvoked(); |
2013 |
> |
r2.assertInvoked(); |
2014 |
> |
r3.assertInvoked(); |
2015 |
|
checkCompletedNormally(f, v1); |
2016 |
|
checkCompletedNormally(g, v2); |
2017 |
|
}} |
2020 |
|
* runAfterBoth result completes exceptionally after exceptional |
2021 |
|
* completion of either source |
2022 |
|
*/ |
2023 |
< |
public void testRunAfterBoth_exceptionalCompletion() { |
2023 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
2024 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1787 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2025 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2026 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2027 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2028 |
|
{ |
2029 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2030 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2031 |
|
final CFException ex = new CFException(); |
2032 |
< |
final Noop r = new Noop(m); |
2033 |
< |
|
2034 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
2035 |
< |
if (!createIncomplete) |
2036 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
2037 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2038 |
< |
if (createIncomplete) { |
2039 |
< |
checkIncomplete(h); |
2040 |
< |
assertTrue((!fFirst ? f : g).completeExceptionally(ex)); |
2041 |
< |
} |
2032 |
> |
final Noop r1 = new Noop(m); |
2033 |
> |
final Noop r2 = new Noop(m); |
2034 |
> |
final Noop r3 = new Noop(m); |
2035 |
> |
|
2036 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2037 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2038 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2039 |
> |
() -> fst.completeExceptionally(ex) : |
2040 |
> |
() -> fst.complete(v1); |
2041 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2042 |
> |
() -> snd.complete(v1) : |
2043 |
> |
() -> snd.completeExceptionally(ex); |
2044 |
> |
|
2045 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2046 |
> |
assertTrue(complete1.call()); |
2047 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2048 |
> |
checkIncomplete(h1); |
2049 |
> |
checkIncomplete(h2); |
2050 |
> |
assertTrue(complete2.call()); |
2051 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2052 |
|
|
2053 |
< |
checkCompletedWithWrappedException(h, ex); |
2054 |
< |
r.assertNotInvoked(); |
2055 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2056 |
< |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
2053 |
> |
checkCompletedWithWrappedException(h1, ex); |
2054 |
> |
checkCompletedWithWrappedException(h2, ex); |
2055 |
> |
checkCompletedWithWrappedException(h3, ex); |
2056 |
> |
r1.assertNotInvoked(); |
2057 |
> |
r2.assertNotInvoked(); |
2058 |
> |
r3.assertNotInvoked(); |
2059 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2060 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
2061 |
|
}} |
2062 |
|
|
2063 |
|
/** |
2064 |
|
* runAfterBoth result completes exceptionally if either source cancelled |
2065 |
|
*/ |
2066 |
< |
public void testRunAfterBoth_sourceCancelled() { |
2066 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
2067 |
|
for (ExecutionMode m : ExecutionMode.values()) |
2068 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1817 |
– |
for (boolean createIncomplete : new boolean[] { true, false }) |
2069 |
|
for (boolean fFirst : new boolean[] { true, false }) |
2070 |
+ |
for (boolean failFirst : new boolean[] { true, false }) |
2071 |
|
for (Integer v1 : new Integer[] { 1, null }) |
2072 |
|
{ |
2073 |
|
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2074 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2075 |
< |
final Noop r = new Noop(m); |
2075 |
> |
final Noop r1 = new Noop(m); |
2076 |
> |
final Noop r2 = new Noop(m); |
2077 |
> |
final Noop r3 = new Noop(m); |
2078 |
|
|
2079 |
< |
assertTrue((fFirst ? f : g).complete(v1)); |
2080 |
< |
if (!createIncomplete) |
2081 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2082 |
< |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
2083 |
< |
if (createIncomplete) { |
2084 |
< |
checkIncomplete(h); |
2085 |
< |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
2086 |
< |
} |
2079 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2080 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2081 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2082 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2083 |
> |
() -> fst.complete(v1); |
2084 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2085 |
> |
() -> snd.complete(v1) : |
2086 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2087 |
|
|
2088 |
< |
checkCompletedWithWrappedCancellationException(h); |
2089 |
< |
checkCancelled(!fFirst ? f : g); |
2090 |
< |
r.assertNotInvoked(); |
2091 |
< |
checkCompletedNormally(fFirst ? f : g, v1); |
2088 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2089 |
> |
assertTrue(complete1.call()); |
2090 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2091 |
> |
checkIncomplete(h1); |
2092 |
> |
checkIncomplete(h2); |
2093 |
> |
assertTrue(complete2.call()); |
2094 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2095 |
> |
|
2096 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2097 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2098 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2099 |
> |
r1.assertNotInvoked(); |
2100 |
> |
r2.assertNotInvoked(); |
2101 |
> |
r3.assertNotInvoked(); |
2102 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2103 |
> |
checkCancelled(failFirst ? fst : snd); |
2104 |
|
}} |
2105 |
|
|
2106 |
|
/** |
2116 |
|
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2117 |
|
final FailingRunnable r1 = new FailingRunnable(m); |
2118 |
|
final FailingRunnable r2 = new FailingRunnable(m); |
2119 |
+ |
final FailingRunnable r3 = new FailingRunnable(m); |
2120 |
|
|
2121 |
< |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2122 |
< |
assertTrue(fFirst ? f.complete(v1) : g.complete(v2)); |
2123 |
< |
assertTrue(!fFirst ? f.complete(v1) : g.complete(v2)); |
2124 |
< |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2121 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2122 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2123 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2124 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2125 |
> |
|
2126 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2127 |
> |
assertTrue(fst.complete(w1)); |
2128 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2129 |
> |
assertTrue(snd.complete(w2)); |
2130 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2131 |
|
|
2132 |
|
checkCompletedWithWrappedCFException(h1); |
2133 |
|
checkCompletedWithWrappedCFException(h2); |
2134 |
+ |
checkCompletedWithWrappedCFException(h3); |
2135 |
+ |
r1.assertInvoked(); |
2136 |
+ |
r2.assertInvoked(); |
2137 |
+ |
r3.assertInvoked(); |
2138 |
|
checkCompletedNormally(f, v1); |
2139 |
|
checkCompletedNormally(g, v2); |
2140 |
|
}} |
3030 |
|
checkCancelled(f); |
3031 |
|
}} |
3032 |
|
|
3033 |
+ |
/** |
3034 |
+ |
* thenCompose result completes exceptionally if the result of the action does |
3035 |
+ |
*/ |
3036 |
+ |
public void testThenCompose_actionReturnsFailingFuture() { |
3037 |
+ |
for (ExecutionMode m : ExecutionMode.values()) |
3038 |
+ |
for (int order = 0; order < 6; order++) |
3039 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3040 |
+ |
{ |
3041 |
+ |
final CFException ex = new CFException(); |
3042 |
+ |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3043 |
+ |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3044 |
+ |
final CompletableFuture<Integer> h; |
3045 |
+ |
// Test all permutations of orders |
3046 |
+ |
switch (order) { |
3047 |
+ |
case 0: |
3048 |
+ |
assertTrue(f.complete(v1)); |
3049 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3050 |
+ |
h = m.thenCompose(f, (x -> g)); |
3051 |
+ |
break; |
3052 |
+ |
case 1: |
3053 |
+ |
assertTrue(f.complete(v1)); |
3054 |
+ |
h = m.thenCompose(f, (x -> g)); |
3055 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3056 |
+ |
break; |
3057 |
+ |
case 2: |
3058 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3059 |
+ |
assertTrue(f.complete(v1)); |
3060 |
+ |
h = m.thenCompose(f, (x -> g)); |
3061 |
+ |
break; |
3062 |
+ |
case 3: |
3063 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3064 |
+ |
h = m.thenCompose(f, (x -> g)); |
3065 |
+ |
assertTrue(f.complete(v1)); |
3066 |
+ |
break; |
3067 |
+ |
case 4: |
3068 |
+ |
h = m.thenCompose(f, (x -> g)); |
3069 |
+ |
assertTrue(f.complete(v1)); |
3070 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3071 |
+ |
break; |
3072 |
+ |
case 5: |
3073 |
+ |
h = m.thenCompose(f, (x -> g)); |
3074 |
+ |
assertTrue(f.complete(v1)); |
3075 |
+ |
assertTrue(g.completeExceptionally(ex)); |
3076 |
+ |
break; |
3077 |
+ |
default: throw new AssertionError(); |
3078 |
+ |
} |
3079 |
+ |
|
3080 |
+ |
checkCompletedExceptionally(g, ex); |
3081 |
+ |
checkCompletedWithWrappedException(h, ex); |
3082 |
+ |
checkCompletedNormally(f, v1); |
3083 |
+ |
}} |
3084 |
+ |
|
3085 |
|
// other static methods |
3086 |
|
|
3087 |
|
/** |
3098 |
|
* when all components complete normally |
3099 |
|
*/ |
3100 |
|
public void testAllOf_normal() throws Exception { |
3101 |
< |
for (int k = 1; k < 20; ++k) { |
3101 |
> |
for (int k = 1; k < 10; k++) { |
3102 |
|
CompletableFuture<Integer>[] fs |
3103 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3104 |
< |
for (int i = 0; i < k; ++i) |
3104 |
> |
for (int i = 0; i < k; i++) |
3105 |
|
fs[i] = new CompletableFuture<>(); |
3106 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3107 |
< |
for (int i = 0; i < k; ++i) { |
3107 |
> |
for (int i = 0; i < k; i++) { |
3108 |
|
checkIncomplete(f); |
3109 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3110 |
|
fs[i].complete(one); |
3115 |
|
} |
3116 |
|
|
3117 |
|
public void testAllOf_backwards() throws Exception { |
3118 |
< |
for (int k = 1; k < 20; ++k) { |
3118 |
> |
for (int k = 1; k < 10; k++) { |
3119 |
|
CompletableFuture<Integer>[] fs |
3120 |
|
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3121 |
< |
for (int i = 0; i < k; ++i) |
3121 |
> |
for (int i = 0; i < k; i++) |
3122 |
|
fs[i] = new CompletableFuture<>(); |
3123 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3124 |
|
for (int i = k - 1; i >= 0; i--) { |
3131 |
|
} |
3132 |
|
} |
3133 |
|
|
3134 |
+ |
public void testAllOf_exceptional() throws Exception { |
3135 |
+ |
for (int k = 1; k < 10; k++) { |
3136 |
+ |
CompletableFuture<Integer>[] fs |
3137 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3138 |
+ |
CFException ex = new CFException(); |
3139 |
+ |
for (int i = 0; i < k; i++) |
3140 |
+ |
fs[i] = new CompletableFuture<>(); |
3141 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3142 |
+ |
for (int i = 0; i < k; i++) { |
3143 |
+ |
checkIncomplete(f); |
3144 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
3145 |
+ |
if (i != k/2) { |
3146 |
+ |
fs[i].complete(i); |
3147 |
+ |
checkCompletedNormally(fs[i], i); |
3148 |
+ |
} else { |
3149 |
+ |
fs[i].completeExceptionally(ex); |
3150 |
+ |
checkCompletedExceptionally(fs[i], ex); |
3151 |
+ |
} |
3152 |
+ |
} |
3153 |
+ |
checkCompletedWithWrappedException(f, ex); |
3154 |
+ |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3155 |
+ |
} |
3156 |
+ |
} |
3157 |
+ |
|
3158 |
|
/** |
3159 |
|
* anyOf(no component futures) returns an incomplete future |
3160 |
|
*/ |
3161 |
|
public void testAnyOf_empty() throws Exception { |
3162 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3163 |
+ |
{ |
3164 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3165 |
|
checkIncomplete(f); |
3166 |
< |
} |
3166 |
> |
|
3167 |
> |
f.complete(v1); |
3168 |
> |
checkCompletedNormally(f, v1); |
3169 |
> |
}} |
3170 |
|
|
3171 |
|
/** |
3172 |
|
* anyOf returns a future completed normally with a value when |
3173 |
|
* a component future does |
3174 |
|
*/ |
3175 |
|
public void testAnyOf_normal() throws Exception { |
3176 |
< |
for (int k = 0; k < 10; ++k) { |
3176 |
> |
for (int k = 0; k < 10; k++) { |
3177 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3178 |
< |
for (int i = 0; i < k; ++i) |
3178 |
> |
for (int i = 0; i < k; i++) |
3179 |
|
fs[i] = new CompletableFuture<>(); |
3180 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3181 |
|
checkIncomplete(f); |
3182 |
< |
for (int i = 0; i < k; ++i) { |
3183 |
< |
fs[i].complete(one); |
3184 |
< |
checkCompletedNormally(f, one); |
3185 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
3182 |
> |
for (int i = 0; i < k; i++) { |
3183 |
> |
fs[i].complete(i); |
3184 |
> |
checkCompletedNormally(f, 0); |
3185 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3186 |
> |
assertTrue(0 <= x && x <= i); |
3187 |
> |
} |
3188 |
> |
} |
3189 |
> |
} |
3190 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3191 |
> |
for (int k = 0; k < 10; k++) { |
3192 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3193 |
> |
for (int i = 0; i < k; i++) |
3194 |
> |
fs[i] = new CompletableFuture<>(); |
3195 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3196 |
> |
checkIncomplete(f); |
3197 |
> |
for (int i = k - 1; i >= 0; i--) { |
3198 |
> |
fs[i].complete(i); |
3199 |
> |
checkCompletedNormally(f, k - 1); |
3200 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3201 |
> |
assertTrue(i <= x && x <= k - 1); |
3202 |
|
} |
3203 |
|
} |
3204 |
|
} |
3207 |
|
* anyOf result completes exceptionally when any component does. |
3208 |
|
*/ |
3209 |
|
public void testAnyOf_exceptional() throws Exception { |
3210 |
< |
for (int k = 0; k < 10; ++k) { |
3210 |
> |
for (int k = 0; k < 10; k++) { |
3211 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3212 |
> |
CFException[] exs = new CFException[k]; |
3213 |
> |
for (int i = 0; i < k; i++) { |
3214 |
> |
fs[i] = new CompletableFuture<>(); |
3215 |
> |
exs[i] = new CFException(); |
3216 |
> |
} |
3217 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3218 |
> |
checkIncomplete(f); |
3219 |
> |
for (int i = 0; i < k; i++) { |
3220 |
> |
fs[i].completeExceptionally(exs[i]); |
3221 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3222 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3223 |
> |
} |
3224 |
> |
} |
3225 |
> |
} |
3226 |
> |
|
3227 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3228 |
> |
for (int k = 0; k < 10; k++) { |
3229 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
3230 |
< |
for (int i = 0; i < k; ++i) |
3230 |
> |
CFException[] exs = new CFException[k]; |
3231 |
> |
for (int i = 0; i < k; i++) { |
3232 |
|
fs[i] = new CompletableFuture<>(); |
3233 |
+ |
exs[i] = new CFException(); |
3234 |
+ |
} |
3235 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3236 |
|
checkIncomplete(f); |
3237 |
< |
for (int i = 0; i < k; ++i) { |
3238 |
< |
fs[i].completeExceptionally(new CFException()); |
3239 |
< |
checkCompletedWithWrappedCFException(f); |
3237 |
> |
for (int i = k - 1; i >= 0; i--) { |
3238 |
> |
fs[i].completeExceptionally(exs[i]); |
3239 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3240 |
|
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3241 |
|
} |
3242 |
|
} |
3249 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3250 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3251 |
|
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2857 |
– |
CompletableFuture<?> h; |
3252 |
|
ThreadExecutor exec = new ThreadExecutor(); |
3253 |
|
|
3254 |
|
Runnable[] throwingActions = { |
3255 |
|
() -> CompletableFuture.supplyAsync(null), |
3256 |
|
() -> CompletableFuture.supplyAsync(null, exec), |
3257 |
< |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
3257 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3258 |
|
|
3259 |
|
() -> CompletableFuture.runAsync(null), |
3260 |
|
() -> CompletableFuture.runAsync(null, exec), |
3333 |
|
() -> f.exceptionally(null), |
3334 |
|
|
3335 |
|
() -> f.handle(null), |
2942 |
– |
|
3336 |
|
() -> CompletableFuture.allOf((CompletableFuture<?>)null), |
3337 |
|
() -> CompletableFuture.allOf((CompletableFuture<?>[])null), |
3338 |
|
() -> CompletableFuture.allOf(f, null), |
3358 |
|
assertSame(f, f.toCompletableFuture()); |
3359 |
|
} |
3360 |
|
|
3361 |
< |
// public void testRunAfterEither_resultDeterminedAtTimeOfCreation() { |
3362 |
< |
// for (ExecutionMode m : ExecutionMode.values()) |
3363 |
< |
// for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3364 |
< |
// for (Integer v1 : new Integer[] { 1, null }) |
3365 |
< |
// { |
3366 |
< |
// final CompletableFuture<Integer> f = new CompletableFuture<>(); |
3367 |
< |
// final CompletableFuture<Integer> g = new CompletableFuture<>(); |
3368 |
< |
// final Noop[] rs = new Noop[2]; |
3369 |
< |
// for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
3370 |
< |
// f.complete(v1); |
3371 |
< |
// final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
3372 |
< |
// final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
3373 |
< |
// assertTrue(g.cancel(mayInterruptIfRunning)); |
3374 |
< |
// checkCompletedNormally(h0, null); |
3375 |
< |
// checkCompletedNormally(h1, null); |
3376 |
< |
// for (Noop r : rs) r.assertInvoked(); |
3377 |
< |
// }} |
3361 |
> |
// jdk9 |
3362 |
> |
|
3363 |
> |
/** |
3364 |
> |
* newIncompleteFuture returns an incomplete CompletableFuture |
3365 |
> |
*/ |
3366 |
> |
public void testNewIncompleteFuture() { |
3367 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3368 |
> |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3369 |
> |
checkIncomplete(f); |
3370 |
> |
checkIncomplete(g); |
3371 |
> |
} |
3372 |
> |
|
3373 |
> |
/** |
3374 |
> |
* completedStage returns a completed CompletionStage |
3375 |
> |
*/ |
3376 |
> |
public void testCompletedStage() { |
3377 |
> |
AtomicInteger x = new AtomicInteger(); |
3378 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3379 |
> |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3380 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3381 |
> |
assertEquals(x.get(), 1); |
3382 |
> |
assertNull(r.get()); |
3383 |
> |
} |
3384 |
> |
|
3385 |
> |
/** |
3386 |
> |
* defaultExecutor by default returns the commonPool if |
3387 |
> |
* it supports at least one thread. |
3388 |
> |
*/ |
3389 |
> |
public void testDefaultExecutor() { |
3390 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3391 |
> |
Executor e = f.defaultExecutor(); |
3392 |
> |
Executor c = ForkJoinPool.commonPool(); |
3393 |
> |
if (ForkJoinPool.getCommonPoolParallelism() > 0) |
3394 |
> |
assertSame(e, c); |
3395 |
> |
} |
3396 |
> |
|
3397 |
> |
/** |
3398 |
> |
* failedFuture returns a CompletableFuture completed |
3399 |
> |
* exceptionally with the given Exception |
3400 |
> |
*/ |
3401 |
> |
public void testFailedFuture() { |
3402 |
> |
CFException ex = new CFException(); |
3403 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3404 |
> |
checkCompletedExceptionallyWithRootCause(f, ex); |
3405 |
> |
} |
3406 |
> |
|
3407 |
> |
/** |
3408 |
> |
* failedFuture(null) throws NPE |
3409 |
> |
*/ |
3410 |
> |
public void testFailedFuture2() { |
3411 |
> |
try { |
3412 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3413 |
> |
shouldThrow(); |
3414 |
> |
} catch (NullPointerException success) {} |
3415 |
> |
} |
3416 |
> |
|
3417 |
> |
/** |
3418 |
> |
* copy returns a CompletableFuture that is completed normally, |
3419 |
> |
* with the same value, when source is. |
3420 |
> |
*/ |
3421 |
> |
public void testCopy() { |
3422 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3423 |
> |
CompletableFuture<Integer> g = f.copy(); |
3424 |
> |
checkIncomplete(f); |
3425 |
> |
checkIncomplete(g); |
3426 |
> |
f.complete(1); |
3427 |
> |
checkCompletedNormally(f, 1); |
3428 |
> |
checkCompletedNormally(g, 1); |
3429 |
> |
} |
3430 |
> |
|
3431 |
> |
/** |
3432 |
> |
* copy returns a CompletableFuture that is completed exceptionally |
3433 |
> |
* when source is. |
3434 |
> |
*/ |
3435 |
> |
public void testCopy2() { |
3436 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3437 |
> |
CompletableFuture<Integer> g = f.copy(); |
3438 |
> |
checkIncomplete(f); |
3439 |
> |
checkIncomplete(g); |
3440 |
> |
CFException ex = new CFException(); |
3441 |
> |
f.completeExceptionally(ex); |
3442 |
> |
checkCompletedExceptionally(f, ex); |
3443 |
> |
checkCompletedWithWrappedCFException(g); |
3444 |
> |
} |
3445 |
> |
|
3446 |
> |
/** |
3447 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3448 |
> |
* completed normally, with the same value, when source is. |
3449 |
> |
*/ |
3450 |
> |
public void testMinimalCompletionStage() { |
3451 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3452 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3453 |
> |
AtomicInteger x = new AtomicInteger(); |
3454 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3455 |
> |
checkIncomplete(f); |
3456 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3457 |
> |
f.complete(1); |
3458 |
> |
checkCompletedNormally(f, 1); |
3459 |
> |
assertEquals(x.get(), 1); |
3460 |
> |
assertNull(r.get()); |
3461 |
> |
} |
3462 |
> |
|
3463 |
> |
/** |
3464 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3465 |
> |
* completed exceptionally when source is. |
3466 |
> |
*/ |
3467 |
> |
public void testMinimalCompletionStage2() { |
3468 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3469 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3470 |
> |
AtomicInteger x = new AtomicInteger(); |
3471 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3472 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3473 |
> |
checkIncomplete(f); |
3474 |
> |
CFException ex = new CFException(); |
3475 |
> |
f.completeExceptionally(ex); |
3476 |
> |
checkCompletedExceptionally(f, ex); |
3477 |
> |
assertEquals(x.get(), 0); |
3478 |
> |
assertEquals(r.get().getCause(), ex); |
3479 |
> |
} |
3480 |
> |
|
3481 |
> |
/** |
3482 |
> |
* failedStage returns a Completionstage completed |
3483 |
> |
* exceptionally with the given Exception |
3484 |
> |
*/ |
3485 |
> |
public void testFailedStage() { |
3486 |
> |
CFException ex = new CFException(); |
3487 |
> |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3488 |
> |
AtomicInteger x = new AtomicInteger(); |
3489 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3490 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3491 |
> |
assertEquals(x.get(), 0); |
3492 |
> |
assertEquals(r.get().getCause(), ex); |
3493 |
> |
} |
3494 |
> |
|
3495 |
> |
/** |
3496 |
> |
* completeAsync completes with value of given supplier |
3497 |
> |
*/ |
3498 |
> |
public void testCompleteAsync() { |
3499 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3500 |
> |
f.completeAsync(() -> 1); |
3501 |
> |
f.join(); |
3502 |
> |
checkCompletedNormally(f, 1); |
3503 |
> |
} |
3504 |
> |
|
3505 |
> |
/** |
3506 |
> |
* completeAsync completes exceptionally if given supplier throws |
3507 |
> |
*/ |
3508 |
> |
public void testCompleteAsync2() { |
3509 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3510 |
> |
CFException ex = new CFException(); |
3511 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3512 |
> |
try { |
3513 |
> |
f.join(); |
3514 |
> |
shouldThrow(); |
3515 |
> |
} catch (Exception success) {} |
3516 |
> |
checkCompletedWithWrappedCFException(f); |
3517 |
> |
} |
3518 |
> |
|
3519 |
> |
/** |
3520 |
> |
* completeAsync with given executor completes with value of given supplier |
3521 |
> |
*/ |
3522 |
> |
public void testCompleteAsync3() { |
3523 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3524 |
> |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3525 |
> |
f.join(); |
3526 |
> |
checkCompletedNormally(f, 1); |
3527 |
> |
} |
3528 |
> |
|
3529 |
> |
/** |
3530 |
> |
* completeAsync with given executor completes exceptionally if |
3531 |
> |
* given supplier throws |
3532 |
> |
*/ |
3533 |
> |
public void testCompleteAsync4() { |
3534 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3535 |
> |
CFException ex = new CFException(); |
3536 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3537 |
> |
try { |
3538 |
> |
f.join(); |
3539 |
> |
shouldThrow(); |
3540 |
> |
} catch (Exception success) {} |
3541 |
> |
checkCompletedWithWrappedCFException(f); |
3542 |
> |
} |
3543 |
> |
|
3544 |
> |
/** |
3545 |
> |
* orTimeout completes with TimeoutException if not complete |
3546 |
> |
*/ |
3547 |
> |
public void testOrTimeout() { |
3548 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3549 |
> |
f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3550 |
> |
checkCompletedExceptionallyWithTimeout(f); |
3551 |
> |
} |
3552 |
> |
|
3553 |
> |
/** |
3554 |
> |
* orTimeout completes normally if completed before timeout |
3555 |
> |
*/ |
3556 |
> |
public void testOrTimeout2() { |
3557 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3558 |
> |
f.complete(1); |
3559 |
> |
f.orTimeout(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3560 |
> |
checkCompletedNormally(f, 1); |
3561 |
> |
} |
3562 |
> |
|
3563 |
> |
/** |
3564 |
> |
* completeOnTimeout completes with given value if not complete |
3565 |
> |
*/ |
3566 |
> |
public void testCompleteOnTimeout() { |
3567 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3568 |
> |
f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3569 |
> |
f.join(); |
3570 |
> |
checkCompletedNormally(f, -1); |
3571 |
> |
} |
3572 |
> |
|
3573 |
> |
/** |
3574 |
> |
* completeOnTimeout has no effect if completed within timeout |
3575 |
> |
*/ |
3576 |
> |
public void testCompleteOnTimeout2() { |
3577 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3578 |
> |
f.complete(1); |
3579 |
> |
f.completeOnTimeout(-1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
3580 |
> |
checkCompletedNormally(f, 1); |
3581 |
> |
} |
3582 |
> |
|
3583 |
> |
/** |
3584 |
> |
* delayedExecutor returns an executor that delays submission |
3585 |
> |
*/ |
3586 |
> |
public void testDelayedExecutor() { |
3587 |
> |
long timeoutMillis = SMALL_DELAY_MS; |
3588 |
> |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3589 |
> |
MILLISECONDS); |
3590 |
> |
long startTime = System.nanoTime(); |
3591 |
> |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3592 |
> |
assertNull(f.getNow(null)); |
3593 |
> |
try { |
3594 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
3595 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3596 |
> |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3597 |
> |
checkCompletedNormally(f, 1); |
3598 |
> |
} |
3599 |
> |
|
3600 |
> |
/** |
3601 |
> |
* delayedExecutor for a given executor returns an executor that |
3602 |
> |
* delays submission |
3603 |
> |
*/ |
3604 |
> |
public void testDelayedExecutor2() { |
3605 |
> |
long timeoutMillis = SMALL_DELAY_MS; |
3606 |
> |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3607 |
> |
MILLISECONDS, |
3608 |
> |
new ThreadExecutor()); |
3609 |
> |
long startTime = System.nanoTime(); |
3610 |
> |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3611 |
> |
assertNull(f.getNow(null)); |
3612 |
> |
try { |
3613 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
3614 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
3615 |
> |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3616 |
> |
checkCompletedNormally(f, 1); |
3617 |
> |
} |
3618 |
> |
|
3619 |
> |
//--- tests of implementation details; not part of official tck --- |
3620 |
> |
|
3621 |
> |
Object resultOf(CompletableFuture<?> f) { |
3622 |
> |
try { |
3623 |
> |
java.lang.reflect.Field resultField |
3624 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3625 |
> |
resultField.setAccessible(true); |
3626 |
> |
return resultField.get(f); |
3627 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3628 |
> |
} |
3629 |
> |
|
3630 |
> |
public void testExceptionPropagationReusesResultObject() { |
3631 |
> |
if (!testImplementationDetails) return; |
3632 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3633 |
> |
{ |
3634 |
> |
final CFException ex = new CFException(); |
3635 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3636 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3637 |
> |
|
3638 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3639 |
> |
= new ArrayList<>(); |
3640 |
> |
|
3641 |
> |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3642 |
> |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3643 |
> |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3644 |
> |
|
3645 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3646 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3647 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3648 |
> |
|
3649 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3650 |
> |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3651 |
> |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3652 |
> |
|
3653 |
> |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3654 |
> |
|
3655 |
> |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3656 |
> |
|
3657 |
> |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3658 |
> |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3659 |
> |
|
3660 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3661 |
> |
fun : funs) { |
3662 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3663 |
> |
f.completeExceptionally(ex); |
3664 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3665 |
> |
checkCompletedWithWrappedException(src, ex); |
3666 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3667 |
> |
checkCompletedWithWrappedException(dep, ex); |
3668 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3669 |
> |
} |
3670 |
> |
|
3671 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3672 |
> |
fun : funs) { |
3673 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3674 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3675 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3676 |
> |
f.completeExceptionally(ex); |
3677 |
> |
checkCompletedWithWrappedException(src, ex); |
3678 |
> |
checkCompletedWithWrappedException(dep, ex); |
3679 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3680 |
> |
} |
3681 |
> |
|
3682 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3683 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3684 |
> |
fun : funs) { |
3685 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3686 |
> |
f.cancel(mayInterruptIfRunning); |
3687 |
> |
checkCancelled(f); |
3688 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3689 |
> |
checkCompletedWithWrappedCancellationException(src); |
3690 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3691 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3692 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3693 |
> |
} |
3694 |
> |
|
3695 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3696 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3697 |
> |
fun : funs) { |
3698 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3699 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3700 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3701 |
> |
f.cancel(mayInterruptIfRunning); |
3702 |
> |
checkCancelled(f); |
3703 |
> |
checkCompletedWithWrappedCancellationException(src); |
3704 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3705 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3706 |
> |
} |
3707 |
> |
}} |
3708 |
|
|
3709 |
|
} |