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; |
1127 |
|
* thenCompose result completes normally after normal completion of source |
1128 |
|
*/ |
1129 |
|
public void testThenCompose() { |
1130 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1131 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1132 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1130 |
> |
CompletableFuture<Integer> f, g; |
1131 |
> |
CompletableFutureInc r; |
1132 |
> |
|
1133 |
> |
f = new CompletableFuture<Integer>(); |
1134 |
> |
g = f.thenCompose(r = new CompletableFutureInc()); |
1135 |
|
f.complete(one); |
1136 |
|
checkCompletedNormally(g, two); |
1137 |
+ |
assertTrue(r.ran); |
1138 |
+ |
|
1139 |
+ |
f = new CompletableFuture<Integer>(); |
1140 |
+ |
f.complete(one); |
1141 |
+ |
g = f.thenCompose(r = new CompletableFutureInc()); |
1142 |
+ |
checkCompletedNormally(g, two); |
1143 |
+ |
assertTrue(r.ran); |
1144 |
|
} |
1145 |
|
|
1146 |
|
/** |
1148 |
|
* completion of source |
1149 |
|
*/ |
1150 |
|
public void testThenCompose2() { |
1151 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1152 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1153 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1151 |
> |
CompletableFuture<Integer> f, g; |
1152 |
> |
CompletableFutureInc r; |
1153 |
> |
|
1154 |
> |
f = new CompletableFuture<Integer>(); |
1155 |
> |
g = f.thenCompose(r = new CompletableFutureInc()); |
1156 |
> |
f.completeExceptionally(new CFException()); |
1157 |
> |
checkCompletedWithWrappedCFException(g); |
1158 |
> |
|
1159 |
> |
f = new CompletableFuture<Integer>(); |
1160 |
|
f.completeExceptionally(new CFException()); |
1161 |
+ |
g = f.thenCompose(r = new CompletableFutureInc()); |
1162 |
|
checkCompletedWithWrappedCFException(g); |
1163 |
|
} |
1164 |
|
|
1166 |
|
* thenCompose result completes exceptionally if action does |
1167 |
|
*/ |
1168 |
|
public void testThenCompose3() { |
1169 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1170 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
1171 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1169 |
> |
CompletableFuture<Integer> f, g; |
1170 |
> |
FailingCompletableFutureFunction r; |
1171 |
> |
|
1172 |
> |
f = new CompletableFuture<Integer>(); |
1173 |
> |
g = f.thenCompose(r = new FailingCompletableFutureFunction()); |
1174 |
|
f.complete(one); |
1175 |
|
checkCompletedWithWrappedCFException(g); |
1176 |
+ |
|
1177 |
+ |
f = new CompletableFuture<Integer>(); |
1178 |
+ |
f.complete(one); |
1179 |
+ |
g = f.thenCompose(r = new FailingCompletableFutureFunction()); |
1180 |
+ |
checkCompletedWithWrappedCFException(g); |
1181 |
|
} |
1182 |
|
|
1183 |
|
/** |
1184 |
|
* thenCompose result completes exceptionally if source cancelled |
1185 |
|
*/ |
1186 |
|
public void testThenCompose4() { |
1187 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1188 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1189 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1187 |
> |
CompletableFuture<Integer> f, g; |
1188 |
> |
CompletableFutureInc r; |
1189 |
> |
|
1190 |
> |
f = new CompletableFuture<Integer>(); |
1191 |
> |
g = f.thenCompose(r = new CompletableFutureInc()); |
1192 |
|
assertTrue(f.cancel(true)); |
1193 |
|
checkCompletedWithWrappedCancellationException(g); |
1194 |
+ |
|
1195 |
+ |
f = new CompletableFuture<Integer>(); |
1196 |
+ |
assertTrue(f.cancel(true)); |
1197 |
+ |
g = f.thenCompose(r = new CompletableFutureInc()); |
1198 |
+ |
checkCompletedWithWrappedCancellationException(g); |
1199 |
|
} |
1200 |
|
|
1201 |
|
|
1829 |
|
* completion of source |
1830 |
|
*/ |
1831 |
|
public void testThenComposeAsync() { |
1832 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1833 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1834 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1832 |
> |
CompletableFuture<Integer> f, g; |
1833 |
> |
CompletableFutureInc r; |
1834 |
> |
|
1835 |
> |
f = new CompletableFuture<Integer>(); |
1836 |
> |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1837 |
> |
f.complete(one); |
1838 |
> |
checkCompletedNormally(g, two); |
1839 |
> |
|
1840 |
> |
f = new CompletableFuture<Integer>(); |
1841 |
|
f.complete(one); |
1842 |
+ |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1843 |
|
checkCompletedNormally(g, two); |
1844 |
|
} |
1845 |
|
|
1848 |
|
* exceptional completion of source |
1849 |
|
*/ |
1850 |
|
public void testThenComposeAsync2() { |
1851 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1852 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1853 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1851 |
> |
CompletableFuture<Integer> f, g; |
1852 |
> |
CompletableFutureInc r; |
1853 |
> |
|
1854 |
> |
f = new CompletableFuture<Integer>(); |
1855 |
> |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1856 |
|
f.completeExceptionally(new CFException()); |
1857 |
|
checkCompletedWithWrappedCFException(g); |
1858 |
+ |
assertFalse(r.ran); |
1859 |
+ |
|
1860 |
+ |
f = new CompletableFuture<Integer>(); |
1861 |
+ |
f.completeExceptionally(new CFException()); |
1862 |
+ |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1863 |
+ |
checkCompletedWithWrappedCFException(g); |
1864 |
+ |
assertFalse(r.ran); |
1865 |
|
} |
1866 |
|
|
1867 |
|
/** |
1868 |
|
* thenComposeAsync result completes exceptionally if action does |
1869 |
|
*/ |
1870 |
|
public void testThenComposeAsync3() { |
1871 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1872 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
1873 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1871 |
> |
CompletableFuture<Integer> f, g; |
1872 |
> |
FailingCompletableFutureFunction r; |
1873 |
> |
|
1874 |
> |
f = new CompletableFuture<Integer>(); |
1875 |
> |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
1876 |
|
f.complete(one); |
1877 |
|
checkCompletedWithWrappedCFException(g); |
1878 |
+ |
|
1879 |
+ |
f = new CompletableFuture<Integer>(); |
1880 |
+ |
f.complete(one); |
1881 |
+ |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
1882 |
+ |
checkCompletedWithWrappedCFException(g); |
1883 |
|
} |
1884 |
|
|
1885 |
|
/** |
1886 |
|
* thenComposeAsync result completes exceptionally if source cancelled |
1887 |
|
*/ |
1888 |
|
public void testThenComposeAsync4() { |
1889 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1890 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1891 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1889 |
> |
CompletableFuture<Integer> f, g; |
1890 |
> |
CompletableFutureInc r; |
1891 |
> |
|
1892 |
> |
f = new CompletableFuture<Integer>(); |
1893 |
> |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1894 |
> |
assertTrue(f.cancel(true)); |
1895 |
> |
checkCompletedWithWrappedCancellationException(g); |
1896 |
> |
|
1897 |
> |
f = new CompletableFuture<Integer>(); |
1898 |
|
assertTrue(f.cancel(true)); |
1899 |
+ |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
1900 |
|
checkCompletedWithWrappedCancellationException(g); |
1901 |
|
} |
1902 |
|
|