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; |
1505 |
|
*/ |
1506 |
|
public void testThenAccept_normalCompletion() { |
1507 |
|
for (ExecutionMode m : ExecutionMode.values()) |
1494 |
– |
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)); |
1500 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1501 |
< |
if (createIncomplete) { |
1502 |
< |
checkIncomplete(g); |
1503 |
< |
assertTrue(f.complete(v1)); |
1504 |
< |
} |
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()) |
1517 |
– |
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); |
1523 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1524 |
< |
if (createIncomplete) { |
1525 |
< |
checkIncomplete(g); |
1526 |
< |
f.completeExceptionally(ex); |
1527 |
< |
} |
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()) |
1539 |
– |
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)); |
1545 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1546 |
< |
if (createIncomplete) { |
1547 |
< |
checkIncomplete(g); |
1548 |
< |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1549 |
< |
} |
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()) |
1561 |
– |
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); |
1567 |
< |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1568 |
< |
if (createIncomplete) { |
1569 |
< |
checkIncomplete(g); |
1570 |
< |
f.complete(v1); |
1571 |
< |
} |
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); |
1591 |
< |
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 |
|
}} |
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), |