55 |
|
|
56 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
57 |
|
try { |
58 |
+ |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
59 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
60 |
+ |
try { |
61 |
|
assertEquals(value, f.join()); |
62 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
63 |
|
try { |
66 |
|
try { |
67 |
|
assertEquals(value, f.get()); |
68 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
66 |
– |
try { |
67 |
– |
assertEquals(value, f.get(0L, SECONDS)); |
68 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
|
assertTrue(f.isDone()); |
70 |
|
assertFalse(f.isCancelled()); |
71 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
73 |
|
|
74 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
75 |
|
try { |
76 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
77 |
+ |
shouldThrow(); |
78 |
+ |
} catch (ExecutionException success) { |
79 |
+ |
assertTrue(success.getCause() instanceof CFException); |
80 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
81 |
+ |
try { |
82 |
|
f.join(); |
83 |
|
shouldThrow(); |
84 |
|
} catch (CompletionException success) { |
96 |
|
} catch (ExecutionException success) { |
97 |
|
assertTrue(success.getCause() instanceof CFException); |
98 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
93 |
– |
try { |
94 |
– |
f.get(0L, SECONDS); |
95 |
– |
shouldThrow(); |
96 |
– |
} catch (ExecutionException success) { |
97 |
– |
assertTrue(success.getCause() instanceof CFException); |
98 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
99 |
|
assertTrue(f.isDone()); |
100 |
|
assertFalse(f.isCancelled()); |
101 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
103 |
|
|
104 |
|
void checkCancelled(CompletableFuture<?> f) { |
105 |
|
try { |
106 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
107 |
+ |
shouldThrow(); |
108 |
+ |
} catch (CancellationException success) { |
109 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
110 |
+ |
try { |
111 |
|
f.join(); |
112 |
|
shouldThrow(); |
113 |
|
} catch (CancellationException success) {} |
120 |
|
shouldThrow(); |
121 |
|
} catch (CancellationException success) { |
122 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
118 |
– |
try { |
119 |
– |
f.get(0L, SECONDS); |
120 |
– |
shouldThrow(); |
121 |
– |
} catch (CancellationException success) { |
122 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
123 |
|
assertTrue(f.isDone()); |
124 |
|
assertTrue(f.isCancelled()); |
125 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
127 |
|
|
128 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
129 |
|
try { |
130 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
131 |
+ |
shouldThrow(); |
132 |
+ |
} catch (ExecutionException success) { |
133 |
+ |
assertTrue(success.getCause() instanceof CancellationException); |
134 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
135 |
+ |
try { |
136 |
|
f.join(); |
137 |
|
shouldThrow(); |
138 |
|
} catch (CompletionException success) { |
150 |
|
} catch (ExecutionException success) { |
151 |
|
assertTrue(success.getCause() instanceof CancellationException); |
152 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
147 |
– |
try { |
148 |
– |
f.get(0L, SECONDS); |
149 |
– |
shouldThrow(); |
150 |
– |
} catch (ExecutionException success) { |
151 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
152 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
153 |
|
assertTrue(f.isDone()); |
154 |
|
assertFalse(f.isCancelled()); |
155 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
332 |
|
|
333 |
|
static final class CompletableFutureInc |
334 |
|
implements Function<Integer, CompletableFuture<Integer>> { |
335 |
+ |
boolean ran; |
336 |
|
public CompletableFuture<Integer> apply(Integer x) { |
337 |
+ |
ran = true; |
338 |
|
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
339 |
|
f.complete(Integer.valueOf(x.intValue() + 1)); |
340 |
|
return f; |
1799 |
|
* completion of source |
1800 |
|
*/ |
1801 |
|
public void testThenComposeAsync() { |
1802 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1803 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1804 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1802 |
> |
CompletableFuture<Integer> f, g; |
1803 |
> |
CompletableFutureInc r; |
1804 |
> |
|
1805 |
> |
f = new CompletableFuture<Integer>(); |
1806 |
> |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1807 |
|
f.complete(one); |
1808 |
|
checkCompletedNormally(g, two); |
1809 |
+ |
|
1810 |
+ |
f = new CompletableFuture<Integer>(); |
1811 |
+ |
f.complete(one); |
1812 |
+ |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1813 |
+ |
checkCompletedNormally(g, two); |
1814 |
|
} |
1815 |
|
|
1816 |
|
/** |
1818 |
|
* exceptional completion of source |
1819 |
|
*/ |
1820 |
|
public void testThenComposeAsync2() { |
1821 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1822 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1823 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1821 |
> |
CompletableFuture<Integer> f, g; |
1822 |
> |
CompletableFutureInc r; |
1823 |
> |
|
1824 |
> |
f = new CompletableFuture<Integer>(); |
1825 |
> |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1826 |
> |
f.completeExceptionally(new CFException()); |
1827 |
> |
checkCompletedWithWrappedCFException(g); |
1828 |
> |
assertFalse(r.ran); |
1829 |
> |
|
1830 |
> |
f = new CompletableFuture<Integer>(); |
1831 |
|
f.completeExceptionally(new CFException()); |
1832 |
+ |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1833 |
|
checkCompletedWithWrappedCFException(g); |
1834 |
+ |
assertFalse(r.ran); |
1835 |
|
} |
1836 |
|
|
1837 |
|
/** |
1838 |
|
* thenComposeAsync result completes exceptionally if action does |
1839 |
|
*/ |
1840 |
|
public void testThenComposeAsync3() { |
1841 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1842 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
1843 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1841 |
> |
CompletableFuture<Integer> f, g; |
1842 |
> |
FailingCompletableFutureFunction r; |
1843 |
> |
|
1844 |
> |
f = new CompletableFuture<Integer>(); |
1845 |
> |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
1846 |
|
f.complete(one); |
1847 |
|
checkCompletedWithWrappedCFException(g); |
1848 |
+ |
|
1849 |
+ |
f = new CompletableFuture<Integer>(); |
1850 |
+ |
f.complete(one); |
1851 |
+ |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
1852 |
+ |
checkCompletedWithWrappedCFException(g); |
1853 |
|
} |
1854 |
|
|
1855 |
|
/** |
1856 |
|
* thenComposeAsync result completes exceptionally if source cancelled |
1857 |
|
*/ |
1858 |
|
public void testThenComposeAsync4() { |
1859 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1860 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1861 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1859 |
> |
CompletableFuture<Integer> f, g; |
1860 |
> |
CompletableFutureInc r; |
1861 |
> |
|
1862 |
> |
f = new CompletableFuture<Integer>(); |
1863 |
> |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1864 |
> |
assertTrue(f.cancel(true)); |
1865 |
> |
checkCompletedWithWrappedCancellationException(g); |
1866 |
> |
|
1867 |
> |
f = new CompletableFuture<Integer>(); |
1868 |
|
assertTrue(f.cancel(true)); |
1869 |
+ |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1870 |
|
checkCompletedWithWrappedCancellationException(g); |
1871 |
|
} |
1872 |
|
|