author | dl |
Wed, 28 Nov 2018 15:25:14 -0800 | |
changeset 52730 | 345266000aba |
parent 51950 | a1c24d06e2b5 |
child 54686 | 09f09b4e7808 |
permissions | -rw-r--r-- |
35394 | 1 |
/* |
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
3 |
* |
|
4 |
* This code is free software; you can redistribute it and/or modify it |
|
5 |
* under the terms of the GNU General Public License version 2 only, as |
|
6 |
* published by the Free Software Foundation. |
|
7 |
* |
|
8 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
9 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
10 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
11 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
12 |
* accompanied this code). |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License version |
|
15 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
16 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
17 |
* |
|
18 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
19 |
* or visit www.oracle.com if you need additional information or have any |
|
20 |
* questions. |
|
21 |
*/ |
|
22 |
||
23 |
/* |
|
24 |
* This file is available under and governed by the GNU General Public |
|
25 |
* License version 2 only, as published by the Free Software Foundation. |
|
26 |
* However, the following notice accompanied the original version of this |
|
27 |
* file: |
|
28 |
* |
|
29 |
* Written by Doug Lea and Martin Buchholz with assistance from |
|
30 |
* members of JCP JSR-166 Expert Group and released to the public |
|
31 |
* domain, as explained at |
|
32 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
|
33 |
*/ |
|
34 |
||
35 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
|
36 |
import static java.util.concurrent.TimeUnit.SECONDS; |
|
37 |
import static java.util.concurrent.CompletableFuture.completedFuture; |
|
38 |
import static java.util.concurrent.CompletableFuture.failedFuture; |
|
39 |
||
40 |
import java.lang.reflect.Method; |
|
41 |
import java.lang.reflect.Modifier; |
|
42 |
||
43 |
import java.util.stream.Collectors; |
|
44 |
import java.util.stream.Stream; |
|
45 |
||
46 |
import java.util.ArrayList; |
|
47 |
import java.util.Arrays; |
|
48 |
import java.util.List; |
|
49 |
import java.util.Objects; |
|
50 |
import java.util.Set; |
|
51 |
import java.util.concurrent.Callable; |
|
52 |
import java.util.concurrent.CancellationException; |
|
53 |
import java.util.concurrent.CompletableFuture; |
|
54 |
import java.util.concurrent.CompletionException; |
|
55 |
import java.util.concurrent.CompletionStage; |
|
56 |
import java.util.concurrent.ExecutionException; |
|
57 |
import java.util.concurrent.Executor; |
|
58 |
import java.util.concurrent.ForkJoinPool; |
|
59 |
import java.util.concurrent.ForkJoinTask; |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
60 |
import java.util.concurrent.RejectedExecutionException; |
35394 | 61 |
import java.util.concurrent.TimeoutException; |
62 |
import java.util.concurrent.atomic.AtomicInteger; |
|
63 |
import java.util.concurrent.atomic.AtomicReference; |
|
64 |
import java.util.function.BiConsumer; |
|
65 |
import java.util.function.BiFunction; |
|
66 |
import java.util.function.Consumer; |
|
67 |
import java.util.function.Function; |
|
68 |
import java.util.function.Predicate; |
|
69 |
import java.util.function.Supplier; |
|
70 |
||
71 |
import junit.framework.Test; |
|
72 |
import junit.framework.TestSuite; |
|
73 |
||
74 |
public class CompletableFutureTest extends JSR166TestCase { |
|
75 |
||
76 |
public static void main(String[] args) { |
|
77 |
main(suite(), args); |
|
78 |
} |
|
79 |
public static Test suite() { |
|
80 |
return new TestSuite(CompletableFutureTest.class); |
|
81 |
} |
|
82 |
||
83 |
static class CFException extends RuntimeException {} |
|
84 |
||
85 |
void checkIncomplete(CompletableFuture<?> f) { |
|
86 |
assertFalse(f.isDone()); |
|
87 |
assertFalse(f.isCancelled()); |
|
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
88 |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
89 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
90 |
Object result = null; |
35394 | 91 |
try { |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
92 |
result = f.getNow(null); |
35394 | 93 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
94 |
assertNull(result); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
95 |
|
35394 | 96 |
try { |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
97 |
f.get(randomExpiredTimeout(), randomTimeUnit()); |
35394 | 98 |
shouldThrow(); |
99 |
} |
|
100 |
catch (TimeoutException success) {} |
|
101 |
catch (Throwable fail) { threadUnexpectedException(fail); } |
|
102 |
} |
|
103 |
||
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
104 |
<T> void checkCompletedNormally(CompletableFuture<T> f, T expectedValue) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
105 |
checkTimedGet(f, expectedValue); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
106 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
107 |
assertEquals(expectedValue, f.join()); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
108 |
assertEquals(expectedValue, f.getNow(null)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
109 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
110 |
T result = null; |
35394 | 111 |
try { |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
112 |
result = f.get(); |
35394 | 113 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
114 |
assertEquals(expectedValue, result); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
115 |
|
35394 | 116 |
assertTrue(f.isDone()); |
117 |
assertFalse(f.isCancelled()); |
|
118 |
assertFalse(f.isCompletedExceptionally()); |
|
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
119 |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
35394 | 120 |
} |
121 |
||
122 |
/** |
|
123 |
* Returns the "raw" internal exceptional completion of f, |
|
124 |
* without any additional wrapping with CompletionException. |
|
125 |
*/ |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
126 |
Throwable exceptionalCompletion(CompletableFuture<?> f) { |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
127 |
// handle (and whenComplete and exceptionally) can distinguish |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
128 |
// between "direct" and "wrapped" exceptional completion |
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
129 |
return f.handle((u, t) -> t).join(); |
35394 | 130 |
} |
131 |
||
132 |
void checkCompletedExceptionally(CompletableFuture<?> f, |
|
133 |
boolean wrapped, |
|
134 |
Consumer<Throwable> checker) { |
|
135 |
Throwable cause = exceptionalCompletion(f); |
|
136 |
if (wrapped) { |
|
137 |
assertTrue(cause instanceof CompletionException); |
|
138 |
cause = cause.getCause(); |
|
139 |
} |
|
140 |
checker.accept(cause); |
|
141 |
||
142 |
long startTime = System.nanoTime(); |
|
143 |
try { |
|
144 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
|
145 |
shouldThrow(); |
|
146 |
} catch (ExecutionException success) { |
|
147 |
assertSame(cause, success.getCause()); |
|
148 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
149 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
|
150 |
||
151 |
try { |
|
152 |
f.join(); |
|
153 |
shouldThrow(); |
|
154 |
} catch (CompletionException success) { |
|
155 |
assertSame(cause, success.getCause()); |
|
156 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
157 |
||
158 |
try { |
|
159 |
f.getNow(null); |
|
160 |
shouldThrow(); |
|
161 |
} catch (CompletionException success) { |
|
162 |
assertSame(cause, success.getCause()); |
|
163 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
164 |
||
165 |
try { |
|
166 |
f.get(); |
|
167 |
shouldThrow(); |
|
168 |
} catch (ExecutionException success) { |
|
169 |
assertSame(cause, success.getCause()); |
|
170 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
171 |
||
172 |
assertFalse(f.isCancelled()); |
|
173 |
assertTrue(f.isDone()); |
|
174 |
assertTrue(f.isCompletedExceptionally()); |
|
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
175 |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
35394 | 176 |
} |
177 |
||
178 |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
|
179 |
checkCompletedExceptionally(f, true, |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
180 |
t -> assertTrue(t instanceof CFException)); |
35394 | 181 |
} |
182 |
||
183 |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
|
184 |
checkCompletedExceptionally(f, true, |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
185 |
t -> assertTrue(t instanceof CancellationException)); |
35394 | 186 |
} |
187 |
||
188 |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
|
189 |
checkCompletedExceptionally(f, false, |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
190 |
t -> assertTrue(t instanceof TimeoutException)); |
35394 | 191 |
} |
192 |
||
193 |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
|
194 |
Throwable ex) { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
195 |
checkCompletedExceptionally(f, true, t -> assertSame(t, ex)); |
35394 | 196 |
} |
197 |
||
198 |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
199 |
checkCompletedExceptionally(f, false, t -> assertSame(t, ex)); |
35394 | 200 |
} |
201 |
||
202 |
void checkCancelled(CompletableFuture<?> f) { |
|
203 |
long startTime = System.nanoTime(); |
|
204 |
try { |
|
205 |
f.get(LONG_DELAY_MS, MILLISECONDS); |
|
206 |
shouldThrow(); |
|
207 |
} catch (CancellationException success) { |
|
208 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
209 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
|
210 |
||
211 |
try { |
|
212 |
f.join(); |
|
213 |
shouldThrow(); |
|
214 |
} catch (CancellationException success) {} |
|
215 |
try { |
|
216 |
f.getNow(null); |
|
217 |
shouldThrow(); |
|
218 |
} catch (CancellationException success) {} |
|
219 |
try { |
|
220 |
f.get(); |
|
221 |
shouldThrow(); |
|
222 |
} catch (CancellationException success) { |
|
223 |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
|
224 |
||
225 |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
|
226 |
||
227 |
assertTrue(f.isDone()); |
|
228 |
assertTrue(f.isCompletedExceptionally()); |
|
229 |
assertTrue(f.isCancelled()); |
|
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
230 |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
35394 | 231 |
} |
232 |
||
233 |
/** |
|
234 |
* A newly constructed CompletableFuture is incomplete, as indicated |
|
235 |
* by methods isDone, isCancelled, and getNow |
|
236 |
*/ |
|
237 |
public void testConstructor() { |
|
238 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
239 |
checkIncomplete(f); |
|
240 |
} |
|
241 |
||
242 |
/** |
|
243 |
* complete completes normally, as indicated by methods isDone, |
|
244 |
* isCancelled, join, get, and getNow |
|
245 |
*/ |
|
246 |
public void testComplete() { |
|
247 |
for (Integer v1 : new Integer[] { 1, null }) |
|
248 |
{ |
|
249 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
250 |
checkIncomplete(f); |
|
251 |
assertTrue(f.complete(v1)); |
|
252 |
assertFalse(f.complete(v1)); |
|
253 |
checkCompletedNormally(f, v1); |
|
254 |
}} |
|
255 |
||
256 |
/** |
|
257 |
* completeExceptionally completes exceptionally, as indicated by |
|
258 |
* methods isDone, isCancelled, join, get, and getNow |
|
259 |
*/ |
|
260 |
public void testCompleteExceptionally() { |
|
261 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
262 |
CFException ex = new CFException(); |
|
263 |
checkIncomplete(f); |
|
264 |
f.completeExceptionally(ex); |
|
265 |
checkCompletedExceptionally(f, ex); |
|
266 |
} |
|
267 |
||
268 |
/** |
|
269 |
* cancel completes exceptionally and reports cancelled, as indicated by |
|
270 |
* methods isDone, isCancelled, join, get, and getNow |
|
271 |
*/ |
|
272 |
public void testCancel() { |
|
273 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
274 |
{ |
|
275 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
276 |
checkIncomplete(f); |
|
277 |
assertTrue(f.cancel(mayInterruptIfRunning)); |
|
278 |
assertTrue(f.cancel(mayInterruptIfRunning)); |
|
279 |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
|
280 |
checkCancelled(f); |
|
281 |
}} |
|
282 |
||
283 |
/** |
|
284 |
* obtrudeValue forces completion with given value |
|
285 |
*/ |
|
286 |
public void testObtrudeValue() { |
|
287 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
288 |
checkIncomplete(f); |
|
289 |
assertTrue(f.complete(one)); |
|
290 |
checkCompletedNormally(f, one); |
|
291 |
f.obtrudeValue(three); |
|
292 |
checkCompletedNormally(f, three); |
|
293 |
f.obtrudeValue(two); |
|
294 |
checkCompletedNormally(f, two); |
|
295 |
f = new CompletableFuture<>(); |
|
296 |
f.obtrudeValue(three); |
|
297 |
checkCompletedNormally(f, three); |
|
298 |
f.obtrudeValue(null); |
|
299 |
checkCompletedNormally(f, null); |
|
300 |
f = new CompletableFuture<>(); |
|
301 |
f.completeExceptionally(new CFException()); |
|
302 |
f.obtrudeValue(four); |
|
303 |
checkCompletedNormally(f, four); |
|
304 |
} |
|
305 |
||
306 |
/** |
|
307 |
* obtrudeException forces completion with given exception |
|
308 |
*/ |
|
309 |
public void testObtrudeException() { |
|
310 |
for (Integer v1 : new Integer[] { 1, null }) |
|
311 |
{ |
|
312 |
CFException ex; |
|
313 |
CompletableFuture<Integer> f; |
|
314 |
||
315 |
f = new CompletableFuture<>(); |
|
316 |
assertTrue(f.complete(v1)); |
|
317 |
for (int i = 0; i < 2; i++) { |
|
318 |
f.obtrudeException(ex = new CFException()); |
|
319 |
checkCompletedExceptionally(f, ex); |
|
320 |
} |
|
321 |
||
322 |
f = new CompletableFuture<>(); |
|
323 |
for (int i = 0; i < 2; i++) { |
|
324 |
f.obtrudeException(ex = new CFException()); |
|
325 |
checkCompletedExceptionally(f, ex); |
|
326 |
} |
|
327 |
||
328 |
f = new CompletableFuture<>(); |
|
48843
21efc1774302
8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents:
48541
diff
changeset
|
329 |
f.completeExceptionally(new CFException()); |
35394 | 330 |
f.obtrudeValue(v1); |
331 |
checkCompletedNormally(f, v1); |
|
332 |
f.obtrudeException(ex = new CFException()); |
|
333 |
checkCompletedExceptionally(f, ex); |
|
334 |
f.completeExceptionally(new CFException()); |
|
335 |
checkCompletedExceptionally(f, ex); |
|
336 |
assertFalse(f.complete(v1)); |
|
337 |
checkCompletedExceptionally(f, ex); |
|
338 |
}} |
|
339 |
||
340 |
/** |
|
341 |
* getNumberOfDependents returns number of dependent tasks |
|
342 |
*/ |
|
343 |
public void testGetNumberOfDependents() { |
|
344 |
for (ExecutionMode m : ExecutionMode.values()) |
|
345 |
for (Integer v1 : new Integer[] { 1, null }) |
|
346 |
{ |
|
347 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
348 |
assertEquals(0, f.getNumberOfDependents()); |
|
349 |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
|
350 |
assertEquals(1, f.getNumberOfDependents()); |
|
351 |
assertEquals(0, g.getNumberOfDependents()); |
|
352 |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
|
353 |
assertEquals(2, f.getNumberOfDependents()); |
|
354 |
assertEquals(0, h.getNumberOfDependents()); |
|
355 |
assertTrue(f.complete(v1)); |
|
356 |
checkCompletedNormally(g, null); |
|
357 |
checkCompletedNormally(h, null); |
|
358 |
assertEquals(0, f.getNumberOfDependents()); |
|
359 |
assertEquals(0, g.getNumberOfDependents()); |
|
360 |
assertEquals(0, h.getNumberOfDependents()); |
|
361 |
}} |
|
362 |
||
363 |
/** |
|
364 |
* toString indicates current completion state |
|
365 |
*/ |
|
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
366 |
public void testToString_incomplete() { |
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47306
diff
changeset
|
367 |
CompletableFuture<String> f = new CompletableFuture<>(); |
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
368 |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
369 |
if (testImplementationDetails) |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
370 |
assertEquals(identityString(f) + "[Not completed]", |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
371 |
f.toString()); |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
372 |
} |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
373 |
|
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
374 |
public void testToString_normal() { |
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47306
diff
changeset
|
375 |
CompletableFuture<String> f = new CompletableFuture<>(); |
35394 | 376 |
assertTrue(f.complete("foo")); |
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
377 |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
378 |
if (testImplementationDetails) |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
379 |
assertEquals(identityString(f) + "[Completed normally]", |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
380 |
f.toString()); |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
381 |
} |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
382 |
|
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
383 |
public void testToString_exception() { |
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47306
diff
changeset
|
384 |
CompletableFuture<String> f = new CompletableFuture<>(); |
35394 | 385 |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
386 |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
387 |
if (testImplementationDetails) |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
388 |
assertTrue(f.toString().startsWith( |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
389 |
identityString(f) + "[Completed exceptionally: ")); |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
390 |
} |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
391 |
|
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
392 |
public void testToString_cancelled() { |
35394 | 393 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
48541
946e34c2dec9
8193300: Miscellaneous changes imported from jsr166 CVS 2018-01
dl
parents:
47306
diff
changeset
|
394 |
CompletableFuture<String> f = new CompletableFuture<>(); |
35394 | 395 |
assertTrue(f.cancel(mayInterruptIfRunning)); |
47306
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
396 |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
397 |
if (testImplementationDetails) |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
398 |
assertTrue(f.toString().startsWith( |
90b7465b9ac7
8186265: Make toString() methods of "task" objects more useful
dl
parents:
47216
diff
changeset
|
399 |
identityString(f) + "[Completed exceptionally: ")); |
35394 | 400 |
} |
401 |
} |
|
402 |
||
403 |
/** |
|
404 |
* completedFuture returns a completed CompletableFuture with given value |
|
405 |
*/ |
|
406 |
public void testCompletedFuture() { |
|
407 |
CompletableFuture<String> f = CompletableFuture.completedFuture("test"); |
|
408 |
checkCompletedNormally(f, "test"); |
|
409 |
} |
|
410 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
411 |
abstract static class CheckedAction { |
35394 | 412 |
int invocationCount = 0; |
413 |
final ExecutionMode m; |
|
414 |
CheckedAction(ExecutionMode m) { this.m = m; } |
|
415 |
void invoked() { |
|
416 |
m.checkExecutionMode(); |
|
417 |
assertEquals(0, invocationCount++); |
|
418 |
} |
|
419 |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
|
420 |
void assertInvoked() { assertEquals(1, invocationCount); } |
|
421 |
} |
|
422 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
423 |
abstract static class CheckedIntegerAction extends CheckedAction { |
35394 | 424 |
Integer value; |
425 |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
|
426 |
void assertValue(Integer expected) { |
|
427 |
assertInvoked(); |
|
428 |
assertEquals(expected, value); |
|
429 |
} |
|
430 |
} |
|
431 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
432 |
static class IntegerSupplier extends CheckedAction |
35394 | 433 |
implements Supplier<Integer> |
434 |
{ |
|
435 |
final Integer value; |
|
436 |
IntegerSupplier(ExecutionMode m, Integer value) { |
|
437 |
super(m); |
|
438 |
this.value = value; |
|
439 |
} |
|
440 |
public Integer get() { |
|
441 |
invoked(); |
|
442 |
return value; |
|
443 |
} |
|
444 |
} |
|
445 |
||
446 |
// A function that handles and produces null values as well. |
|
447 |
static Integer inc(Integer x) { |
|
448 |
return (x == null) ? null : x + 1; |
|
449 |
} |
|
450 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
451 |
static class NoopConsumer extends CheckedIntegerAction |
35394 | 452 |
implements Consumer<Integer> |
453 |
{ |
|
454 |
NoopConsumer(ExecutionMode m) { super(m); } |
|
455 |
public void accept(Integer x) { |
|
456 |
invoked(); |
|
457 |
value = x; |
|
458 |
} |
|
459 |
} |
|
460 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
461 |
static class IncFunction extends CheckedIntegerAction |
35394 | 462 |
implements Function<Integer,Integer> |
463 |
{ |
|
464 |
IncFunction(ExecutionMode m) { super(m); } |
|
465 |
public Integer apply(Integer x) { |
|
466 |
invoked(); |
|
467 |
return value = inc(x); |
|
468 |
} |
|
469 |
} |
|
470 |
||
471 |
// Choose non-commutative actions for better coverage |
|
472 |
// A non-commutative function that handles and produces null values as well. |
|
473 |
static Integer subtract(Integer x, Integer y) { |
|
474 |
return (x == null && y == null) ? null : |
|
475 |
((x == null) ? 42 : x.intValue()) |
|
476 |
- ((y == null) ? 99 : y.intValue()); |
|
477 |
} |
|
478 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
479 |
static class SubtractAction extends CheckedIntegerAction |
35394 | 480 |
implements BiConsumer<Integer, Integer> |
481 |
{ |
|
482 |
SubtractAction(ExecutionMode m) { super(m); } |
|
483 |
public void accept(Integer x, Integer y) { |
|
484 |
invoked(); |
|
485 |
value = subtract(x, y); |
|
486 |
} |
|
487 |
} |
|
488 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
489 |
static class SubtractFunction extends CheckedIntegerAction |
35394 | 490 |
implements BiFunction<Integer, Integer, Integer> |
491 |
{ |
|
492 |
SubtractFunction(ExecutionMode m) { super(m); } |
|
493 |
public Integer apply(Integer x, Integer y) { |
|
494 |
invoked(); |
|
495 |
return value = subtract(x, y); |
|
496 |
} |
|
497 |
} |
|
498 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
499 |
static class Noop extends CheckedAction implements Runnable { |
35394 | 500 |
Noop(ExecutionMode m) { super(m); } |
501 |
public void run() { |
|
502 |
invoked(); |
|
503 |
} |
|
504 |
} |
|
505 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
506 |
static class FailingSupplier extends CheckedAction |
35394 | 507 |
implements Supplier<Integer> |
508 |
{ |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
509 |
final CFException ex; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
510 |
FailingSupplier(ExecutionMode m) { super(m); ex = new CFException(); } |
35394 | 511 |
public Integer get() { |
512 |
invoked(); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
513 |
throw ex; |
35394 | 514 |
} |
515 |
} |
|
516 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
517 |
static class FailingConsumer extends CheckedIntegerAction |
35394 | 518 |
implements Consumer<Integer> |
519 |
{ |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
520 |
final CFException ex; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
521 |
FailingConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
35394 | 522 |
public void accept(Integer x) { |
523 |
invoked(); |
|
524 |
value = x; |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
525 |
throw ex; |
35394 | 526 |
} |
527 |
} |
|
528 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
529 |
static class FailingBiConsumer extends CheckedIntegerAction |
35394 | 530 |
implements BiConsumer<Integer, Integer> |
531 |
{ |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
532 |
final CFException ex; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
533 |
FailingBiConsumer(ExecutionMode m) { super(m); ex = new CFException(); } |
35394 | 534 |
public void accept(Integer x, Integer y) { |
535 |
invoked(); |
|
536 |
value = subtract(x, y); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
537 |
throw ex; |
35394 | 538 |
} |
539 |
} |
|
540 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
541 |
static class FailingFunction extends CheckedIntegerAction |
35394 | 542 |
implements Function<Integer, Integer> |
543 |
{ |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
544 |
final CFException ex; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
545 |
FailingFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
35394 | 546 |
public Integer apply(Integer x) { |
547 |
invoked(); |
|
548 |
value = x; |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
549 |
throw ex; |
35394 | 550 |
} |
551 |
} |
|
552 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
553 |
static class FailingBiFunction extends CheckedIntegerAction |
35394 | 554 |
implements BiFunction<Integer, Integer, Integer> |
555 |
{ |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
556 |
final CFException ex; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
557 |
FailingBiFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
35394 | 558 |
public Integer apply(Integer x, Integer y) { |
559 |
invoked(); |
|
560 |
value = subtract(x, y); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
561 |
throw ex; |
35394 | 562 |
} |
563 |
} |
|
564 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
565 |
static class FailingRunnable extends CheckedAction implements Runnable { |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
566 |
final CFException ex; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
567 |
FailingRunnable(ExecutionMode m) { super(m); ex = new CFException(); } |
35394 | 568 |
public void run() { |
569 |
invoked(); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
570 |
throw ex; |
35394 | 571 |
} |
572 |
} |
|
573 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
574 |
static class CompletableFutureInc extends CheckedIntegerAction |
35394 | 575 |
implements Function<Integer, CompletableFuture<Integer>> |
576 |
{ |
|
577 |
CompletableFutureInc(ExecutionMode m) { super(m); } |
|
578 |
public CompletableFuture<Integer> apply(Integer x) { |
|
579 |
invoked(); |
|
580 |
value = x; |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
581 |
return CompletableFuture.completedFuture(inc(x)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
582 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
583 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
584 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
585 |
static class FailingExceptionalCompletableFutureFunction extends CheckedAction |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
586 |
implements Function<Throwable, CompletableFuture<Integer>> |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
587 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
588 |
final CFException ex; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
589 |
FailingExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
590 |
public CompletableFuture<Integer> apply(Throwable x) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
591 |
invoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
592 |
throw ex; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
593 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
594 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
595 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
596 |
static class ExceptionalCompletableFutureFunction extends CheckedAction |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
597 |
implements Function<Throwable, CompletionStage<Integer>> { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
598 |
final Integer value = 3; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
599 |
ExceptionalCompletableFutureFunction(ExecutionMode m) { super(m); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
600 |
public CompletionStage<Integer> apply(Throwable x) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
601 |
invoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
602 |
return CompletableFuture.completedFuture(value); |
35394 | 603 |
} |
604 |
} |
|
605 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
606 |
static class FailingCompletableFutureFunction extends CheckedIntegerAction |
35394 | 607 |
implements Function<Integer, CompletableFuture<Integer>> |
608 |
{ |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
609 |
final CFException ex; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
610 |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); ex = new CFException(); } |
35394 | 611 |
public CompletableFuture<Integer> apply(Integer x) { |
612 |
invoked(); |
|
613 |
value = x; |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
614 |
throw ex; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
615 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
616 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
617 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
618 |
static class CountingRejectingExecutor implements Executor { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
619 |
final RejectedExecutionException ex = new RejectedExecutionException(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
620 |
final AtomicInteger count = new AtomicInteger(0); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
621 |
public void execute(Runnable r) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
622 |
count.getAndIncrement(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
623 |
throw ex; |
35394 | 624 |
} |
625 |
} |
|
626 |
||
627 |
// Used for explicit executor tests |
|
628 |
static final class ThreadExecutor implements Executor { |
|
629 |
final AtomicInteger count = new AtomicInteger(0); |
|
630 |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
|
631 |
static boolean startedCurrentThread() { |
|
632 |
return Thread.currentThread().getThreadGroup() == tg; |
|
633 |
} |
|
634 |
||
635 |
public void execute(Runnable r) { |
|
636 |
count.getAndIncrement(); |
|
637 |
new Thread(tg, r).start(); |
|
638 |
} |
|
639 |
} |
|
640 |
||
641 |
static final boolean defaultExecutorIsCommonPool |
|
642 |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
|
643 |
||
644 |
/** |
|
645 |
* Permits the testing of parallel code for the 3 different |
|
646 |
* execution modes without copy/pasting all the test methods. |
|
647 |
*/ |
|
648 |
enum ExecutionMode { |
|
649 |
SYNC { |
|
650 |
public void checkExecutionMode() { |
|
651 |
assertFalse(ThreadExecutor.startedCurrentThread()); |
|
652 |
assertNull(ForkJoinTask.getPool()); |
|
653 |
} |
|
654 |
public CompletableFuture<Void> runAsync(Runnable a) { |
|
655 |
throw new UnsupportedOperationException(); |
|
656 |
} |
|
657 |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
|
658 |
throw new UnsupportedOperationException(); |
|
659 |
} |
|
660 |
public <T> CompletableFuture<Void> thenRun |
|
661 |
(CompletableFuture<T> f, Runnable a) { |
|
662 |
return f.thenRun(a); |
|
663 |
} |
|
664 |
public <T> CompletableFuture<Void> thenAccept |
|
665 |
(CompletableFuture<T> f, Consumer<? super T> a) { |
|
666 |
return f.thenAccept(a); |
|
667 |
} |
|
668 |
public <T,U> CompletableFuture<U> thenApply |
|
669 |
(CompletableFuture<T> f, Function<? super T,U> a) { |
|
670 |
return f.thenApply(a); |
|
671 |
} |
|
672 |
public <T,U> CompletableFuture<U> thenCompose |
|
673 |
(CompletableFuture<T> f, |
|
674 |
Function<? super T,? extends CompletionStage<U>> a) { |
|
675 |
return f.thenCompose(a); |
|
676 |
} |
|
677 |
public <T,U> CompletableFuture<U> handle |
|
678 |
(CompletableFuture<T> f, |
|
679 |
BiFunction<? super T,Throwable,? extends U> a) { |
|
680 |
return f.handle(a); |
|
681 |
} |
|
682 |
public <T> CompletableFuture<T> whenComplete |
|
683 |
(CompletableFuture<T> f, |
|
684 |
BiConsumer<? super T,? super Throwable> a) { |
|
685 |
return f.whenComplete(a); |
|
686 |
} |
|
687 |
public <T,U> CompletableFuture<Void> runAfterBoth |
|
688 |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
|
689 |
return f.runAfterBoth(g, a); |
|
690 |
} |
|
691 |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
|
692 |
(CompletableFuture<T> f, |
|
693 |
CompletionStage<? extends U> g, |
|
694 |
BiConsumer<? super T,? super U> a) { |
|
695 |
return f.thenAcceptBoth(g, a); |
|
696 |
} |
|
697 |
public <T,U,V> CompletableFuture<V> thenCombine |
|
698 |
(CompletableFuture<T> f, |
|
699 |
CompletionStage<? extends U> g, |
|
700 |
BiFunction<? super T,? super U,? extends V> a) { |
|
701 |
return f.thenCombine(g, a); |
|
702 |
} |
|
703 |
public <T> CompletableFuture<Void> runAfterEither |
|
704 |
(CompletableFuture<T> f, |
|
705 |
CompletionStage<?> g, |
|
706 |
java.lang.Runnable a) { |
|
707 |
return f.runAfterEither(g, a); |
|
708 |
} |
|
709 |
public <T> CompletableFuture<Void> acceptEither |
|
710 |
(CompletableFuture<T> f, |
|
711 |
CompletionStage<? extends T> g, |
|
712 |
Consumer<? super T> a) { |
|
713 |
return f.acceptEither(g, a); |
|
714 |
} |
|
715 |
public <T,U> CompletableFuture<U> applyToEither |
|
716 |
(CompletableFuture<T> f, |
|
717 |
CompletionStage<? extends T> g, |
|
718 |
Function<? super T,U> a) { |
|
719 |
return f.applyToEither(g, a); |
|
720 |
} |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
721 |
public <T> CompletableFuture<T> exceptionally |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
722 |
(CompletableFuture<T> f, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
723 |
Function<Throwable, ? extends T> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
724 |
return f.exceptionally(fn); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
725 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
726 |
public <T> CompletableFuture<T> exceptionallyCompose |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
727 |
(CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
728 |
return f.exceptionallyCompose(fn); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
729 |
} |
35394 | 730 |
}, |
731 |
ASYNC { |
|
732 |
public void checkExecutionMode() { |
|
733 |
assertEquals(defaultExecutorIsCommonPool, |
|
734 |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
|
735 |
} |
|
736 |
public CompletableFuture<Void> runAsync(Runnable a) { |
|
737 |
return CompletableFuture.runAsync(a); |
|
738 |
} |
|
739 |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
|
740 |
return CompletableFuture.supplyAsync(a); |
|
741 |
} |
|
742 |
public <T> CompletableFuture<Void> thenRun |
|
743 |
(CompletableFuture<T> f, Runnable a) { |
|
744 |
return f.thenRunAsync(a); |
|
745 |
} |
|
746 |
public <T> CompletableFuture<Void> thenAccept |
|
747 |
(CompletableFuture<T> f, Consumer<? super T> a) { |
|
748 |
return f.thenAcceptAsync(a); |
|
749 |
} |
|
750 |
public <T,U> CompletableFuture<U> thenApply |
|
751 |
(CompletableFuture<T> f, Function<? super T,U> a) { |
|
752 |
return f.thenApplyAsync(a); |
|
753 |
} |
|
754 |
public <T,U> CompletableFuture<U> thenCompose |
|
755 |
(CompletableFuture<T> f, |
|
756 |
Function<? super T,? extends CompletionStage<U>> a) { |
|
757 |
return f.thenComposeAsync(a); |
|
758 |
} |
|
759 |
public <T,U> CompletableFuture<U> handle |
|
760 |
(CompletableFuture<T> f, |
|
761 |
BiFunction<? super T,Throwable,? extends U> a) { |
|
762 |
return f.handleAsync(a); |
|
763 |
} |
|
764 |
public <T> CompletableFuture<T> whenComplete |
|
765 |
(CompletableFuture<T> f, |
|
766 |
BiConsumer<? super T,? super Throwable> a) { |
|
767 |
return f.whenCompleteAsync(a); |
|
768 |
} |
|
769 |
public <T,U> CompletableFuture<Void> runAfterBoth |
|
770 |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
|
771 |
return f.runAfterBothAsync(g, a); |
|
772 |
} |
|
773 |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
|
774 |
(CompletableFuture<T> f, |
|
775 |
CompletionStage<? extends U> g, |
|
776 |
BiConsumer<? super T,? super U> a) { |
|
777 |
return f.thenAcceptBothAsync(g, a); |
|
778 |
} |
|
779 |
public <T,U,V> CompletableFuture<V> thenCombine |
|
780 |
(CompletableFuture<T> f, |
|
781 |
CompletionStage<? extends U> g, |
|
782 |
BiFunction<? super T,? super U,? extends V> a) { |
|
783 |
return f.thenCombineAsync(g, a); |
|
784 |
} |
|
785 |
public <T> CompletableFuture<Void> runAfterEither |
|
786 |
(CompletableFuture<T> f, |
|
787 |
CompletionStage<?> g, |
|
788 |
java.lang.Runnable a) { |
|
789 |
return f.runAfterEitherAsync(g, a); |
|
790 |
} |
|
791 |
public <T> CompletableFuture<Void> acceptEither |
|
792 |
(CompletableFuture<T> f, |
|
793 |
CompletionStage<? extends T> g, |
|
794 |
Consumer<? super T> a) { |
|
795 |
return f.acceptEitherAsync(g, a); |
|
796 |
} |
|
797 |
public <T,U> CompletableFuture<U> applyToEither |
|
798 |
(CompletableFuture<T> f, |
|
799 |
CompletionStage<? extends T> g, |
|
800 |
Function<? super T,U> a) { |
|
801 |
return f.applyToEitherAsync(g, a); |
|
802 |
} |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
803 |
public <T> CompletableFuture<T> exceptionally |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
804 |
(CompletableFuture<T> f, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
805 |
Function<Throwable, ? extends T> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
806 |
return f.exceptionallyAsync(fn); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
807 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
808 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
809 |
public <T> CompletableFuture<T> exceptionallyCompose |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
810 |
(CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
811 |
return f.exceptionallyComposeAsync(fn); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
812 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
813 |
|
35394 | 814 |
}, |
815 |
||
816 |
EXECUTOR { |
|
817 |
public void checkExecutionMode() { |
|
818 |
assertTrue(ThreadExecutor.startedCurrentThread()); |
|
819 |
} |
|
820 |
public CompletableFuture<Void> runAsync(Runnable a) { |
|
821 |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
|
822 |
} |
|
823 |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
|
824 |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
|
825 |
} |
|
826 |
public <T> CompletableFuture<Void> thenRun |
|
827 |
(CompletableFuture<T> f, Runnable a) { |
|
828 |
return f.thenRunAsync(a, new ThreadExecutor()); |
|
829 |
} |
|
830 |
public <T> CompletableFuture<Void> thenAccept |
|
831 |
(CompletableFuture<T> f, Consumer<? super T> a) { |
|
832 |
return f.thenAcceptAsync(a, new ThreadExecutor()); |
|
833 |
} |
|
834 |
public <T,U> CompletableFuture<U> thenApply |
|
835 |
(CompletableFuture<T> f, Function<? super T,U> a) { |
|
836 |
return f.thenApplyAsync(a, new ThreadExecutor()); |
|
837 |
} |
|
838 |
public <T,U> CompletableFuture<U> thenCompose |
|
839 |
(CompletableFuture<T> f, |
|
840 |
Function<? super T,? extends CompletionStage<U>> a) { |
|
841 |
return f.thenComposeAsync(a, new ThreadExecutor()); |
|
842 |
} |
|
843 |
public <T,U> CompletableFuture<U> handle |
|
844 |
(CompletableFuture<T> f, |
|
845 |
BiFunction<? super T,Throwable,? extends U> a) { |
|
846 |
return f.handleAsync(a, new ThreadExecutor()); |
|
847 |
} |
|
848 |
public <T> CompletableFuture<T> whenComplete |
|
849 |
(CompletableFuture<T> f, |
|
850 |
BiConsumer<? super T,? super Throwable> a) { |
|
851 |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
|
852 |
} |
|
853 |
public <T,U> CompletableFuture<Void> runAfterBoth |
|
854 |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
|
855 |
return f.runAfterBothAsync(g, a, new ThreadExecutor()); |
|
856 |
} |
|
857 |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
|
858 |
(CompletableFuture<T> f, |
|
859 |
CompletionStage<? extends U> g, |
|
860 |
BiConsumer<? super T,? super U> a) { |
|
861 |
return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); |
|
862 |
} |
|
863 |
public <T,U,V> CompletableFuture<V> thenCombine |
|
864 |
(CompletableFuture<T> f, |
|
865 |
CompletionStage<? extends U> g, |
|
866 |
BiFunction<? super T,? super U,? extends V> a) { |
|
867 |
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
|
868 |
} |
|
869 |
public <T> CompletableFuture<Void> runAfterEither |
|
870 |
(CompletableFuture<T> f, |
|
871 |
CompletionStage<?> g, |
|
872 |
java.lang.Runnable a) { |
|
873 |
return f.runAfterEitherAsync(g, a, new ThreadExecutor()); |
|
874 |
} |
|
875 |
public <T> CompletableFuture<Void> acceptEither |
|
876 |
(CompletableFuture<T> f, |
|
877 |
CompletionStage<? extends T> g, |
|
878 |
Consumer<? super T> a) { |
|
879 |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
|
880 |
} |
|
881 |
public <T,U> CompletableFuture<U> applyToEither |
|
882 |
(CompletableFuture<T> f, |
|
883 |
CompletionStage<? extends T> g, |
|
884 |
Function<? super T,U> a) { |
|
885 |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
|
886 |
} |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
887 |
public <T> CompletableFuture<T> exceptionally |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
888 |
(CompletableFuture<T> f, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
889 |
Function<Throwable, ? extends T> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
890 |
return f.exceptionallyAsync(fn, new ThreadExecutor()); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
891 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
892 |
public <T> CompletableFuture<T> exceptionallyCompose |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
893 |
(CompletableFuture<T> f, Function<Throwable, ? extends CompletionStage<T>> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
894 |
return f.exceptionallyComposeAsync(fn, new ThreadExecutor()); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
895 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
896 |
|
35394 | 897 |
}; |
898 |
||
899 |
public abstract void checkExecutionMode(); |
|
900 |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
|
901 |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
|
902 |
public abstract <T> CompletableFuture<Void> thenRun |
|
903 |
(CompletableFuture<T> f, Runnable a); |
|
904 |
public abstract <T> CompletableFuture<Void> thenAccept |
|
905 |
(CompletableFuture<T> f, Consumer<? super T> a); |
|
906 |
public abstract <T,U> CompletableFuture<U> thenApply |
|
907 |
(CompletableFuture<T> f, Function<? super T,U> a); |
|
908 |
public abstract <T,U> CompletableFuture<U> thenCompose |
|
909 |
(CompletableFuture<T> f, |
|
910 |
Function<? super T,? extends CompletionStage<U>> a); |
|
911 |
public abstract <T,U> CompletableFuture<U> handle |
|
912 |
(CompletableFuture<T> f, |
|
913 |
BiFunction<? super T,Throwable,? extends U> a); |
|
914 |
public abstract <T> CompletableFuture<T> whenComplete |
|
915 |
(CompletableFuture<T> f, |
|
916 |
BiConsumer<? super T,? super Throwable> a); |
|
917 |
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
|
918 |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); |
|
919 |
public abstract <T,U> CompletableFuture<Void> thenAcceptBoth |
|
920 |
(CompletableFuture<T> f, |
|
921 |
CompletionStage<? extends U> g, |
|
922 |
BiConsumer<? super T,? super U> a); |
|
923 |
public abstract <T,U,V> CompletableFuture<V> thenCombine |
|
924 |
(CompletableFuture<T> f, |
|
925 |
CompletionStage<? extends U> g, |
|
926 |
BiFunction<? super T,? super U,? extends V> a); |
|
927 |
public abstract <T> CompletableFuture<Void> runAfterEither |
|
928 |
(CompletableFuture<T> f, |
|
929 |
CompletionStage<?> g, |
|
930 |
java.lang.Runnable a); |
|
931 |
public abstract <T> CompletableFuture<Void> acceptEither |
|
932 |
(CompletableFuture<T> f, |
|
933 |
CompletionStage<? extends T> g, |
|
934 |
Consumer<? super T> a); |
|
935 |
public abstract <T,U> CompletableFuture<U> applyToEither |
|
936 |
(CompletableFuture<T> f, |
|
937 |
CompletionStage<? extends T> g, |
|
938 |
Function<? super T,U> a); |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
939 |
public abstract <T> CompletableFuture<T> exceptionally |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
940 |
(CompletableFuture<T> f, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
941 |
Function<Throwable, ? extends T> fn); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
942 |
public abstract <T> CompletableFuture<T> exceptionallyCompose |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
943 |
(CompletableFuture<T> f, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
944 |
Function<Throwable, ? extends CompletionStage<T>> fn); |
35394 | 945 |
} |
946 |
||
947 |
/** |
|
948 |
* exceptionally action is not invoked when source completes |
|
949 |
* normally, and source result is propagated |
|
950 |
*/ |
|
951 |
public void testExceptionally_normalCompletion() { |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
952 |
for (ExecutionMode m : ExecutionMode.values()) |
35394 | 953 |
for (boolean createIncomplete : new boolean[] { true, false }) |
954 |
for (Integer v1 : new Integer[] { 1, null }) |
|
955 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
956 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 957 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
958 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
959 |
final CompletableFuture<Integer> g = m.exceptionally |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
960 |
(f, (Throwable t) -> { |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
961 |
ran.getAndIncrement(); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
962 |
throw new AssertionError("should not be called"); |
35394 | 963 |
}); |
964 |
if (createIncomplete) assertTrue(f.complete(v1)); |
|
965 |
||
966 |
checkCompletedNormally(g, v1); |
|
967 |
checkCompletedNormally(f, v1); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
968 |
assertEquals(0, ran.get()); |
35394 | 969 |
}} |
970 |
||
971 |
/** |
|
972 |
* exceptionally action completes with function value on source |
|
973 |
* exception |
|
974 |
*/ |
|
975 |
public void testExceptionally_exceptionalCompletion() { |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
976 |
for (ExecutionMode m : ExecutionMode.values()) |
35394 | 977 |
for (boolean createIncomplete : new boolean[] { true, false }) |
978 |
for (Integer v1 : new Integer[] { 1, null }) |
|
979 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
980 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 981 |
final CFException ex = new CFException(); |
982 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
983 |
if (!createIncomplete) f.completeExceptionally(ex); |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
984 |
final CompletableFuture<Integer> g = m.exceptionally |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
985 |
(f, (Throwable t) -> { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
986 |
m.checkExecutionMode(); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
987 |
assertSame(t, ex); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
988 |
ran.getAndIncrement(); |
35394 | 989 |
return v1; |
990 |
}); |
|
991 |
if (createIncomplete) f.completeExceptionally(ex); |
|
992 |
||
993 |
checkCompletedNormally(g, v1); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
994 |
assertEquals(1, ran.get()); |
35394 | 995 |
}} |
996 |
||
997 |
/** |
|
998 |
* If an "exceptionally action" throws an exception, it completes |
|
999 |
* exceptionally with that exception |
|
1000 |
*/ |
|
1001 |
public void testExceptionally_exceptionalCompletionActionFailed() { |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
1002 |
for (ExecutionMode m : ExecutionMode.values()) |
35394 | 1003 |
for (boolean createIncomplete : new boolean[] { true, false }) |
1004 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1005 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1006 |
final CFException ex1 = new CFException(); |
1007 |
final CFException ex2 = new CFException(); |
|
1008 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1009 |
if (!createIncomplete) f.completeExceptionally(ex1); |
|
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
1010 |
final CompletableFuture<Integer> g = m.exceptionally |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
1011 |
(f, (Throwable t) -> { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
1012 |
m.checkExecutionMode(); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1013 |
assertSame(t, ex1); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1014 |
ran.getAndIncrement(); |
35394 | 1015 |
throw ex2; |
1016 |
}); |
|
1017 |
if (createIncomplete) f.completeExceptionally(ex1); |
|
1018 |
||
1019 |
checkCompletedWithWrappedException(g, ex2); |
|
1020 |
checkCompletedExceptionally(f, ex1); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1021 |
assertEquals(1, ran.get()); |
35394 | 1022 |
}} |
1023 |
||
1024 |
/** |
|
1025 |
* whenComplete action executes on normal completion, propagating |
|
1026 |
* source result. |
|
1027 |
*/ |
|
1028 |
public void testWhenComplete_normalCompletion() { |
|
1029 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1030 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1031 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1032 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1033 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1034 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1035 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
|
1036 |
final CompletableFuture<Integer> g = m.whenComplete |
|
1037 |
(f, |
|
1038 |
(Integer result, Throwable t) -> { |
|
1039 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1040 |
assertSame(result, v1); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1041 |
assertNull(t); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1042 |
ran.getAndIncrement(); |
35394 | 1043 |
}); |
1044 |
if (createIncomplete) assertTrue(f.complete(v1)); |
|
1045 |
||
1046 |
checkCompletedNormally(g, v1); |
|
1047 |
checkCompletedNormally(f, v1); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1048 |
assertEquals(1, ran.get()); |
35394 | 1049 |
}} |
1050 |
||
1051 |
/** |
|
1052 |
* whenComplete action executes on exceptional completion, propagating |
|
1053 |
* source result. |
|
1054 |
*/ |
|
1055 |
public void testWhenComplete_exceptionalCompletion() { |
|
1056 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1057 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1058 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1059 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1060 |
final CFException ex = new CFException(); |
1061 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1062 |
if (!createIncomplete) f.completeExceptionally(ex); |
|
1063 |
final CompletableFuture<Integer> g = m.whenComplete |
|
1064 |
(f, |
|
1065 |
(Integer result, Throwable t) -> { |
|
1066 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1067 |
assertNull(result); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1068 |
assertSame(t, ex); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1069 |
ran.getAndIncrement(); |
35394 | 1070 |
}); |
1071 |
if (createIncomplete) f.completeExceptionally(ex); |
|
1072 |
||
1073 |
checkCompletedWithWrappedException(g, ex); |
|
1074 |
checkCompletedExceptionally(f, ex); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1075 |
assertEquals(1, ran.get()); |
35394 | 1076 |
}} |
1077 |
||
1078 |
/** |
|
1079 |
* whenComplete action executes on cancelled source, propagating |
|
1080 |
* CancellationException. |
|
1081 |
*/ |
|
1082 |
public void testWhenComplete_sourceCancelled() { |
|
1083 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1084 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
1085 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1086 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1087 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1088 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1089 |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
|
1090 |
final CompletableFuture<Integer> g = m.whenComplete |
|
1091 |
(f, |
|
1092 |
(Integer result, Throwable t) -> { |
|
1093 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1094 |
assertNull(result); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1095 |
assertTrue(t instanceof CancellationException); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1096 |
ran.getAndIncrement(); |
35394 | 1097 |
}); |
1098 |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
|
1099 |
||
1100 |
checkCompletedWithWrappedCancellationException(g); |
|
1101 |
checkCancelled(f); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1102 |
assertEquals(1, ran.get()); |
35394 | 1103 |
}} |
1104 |
||
1105 |
/** |
|
1106 |
* If a whenComplete action throws an exception when triggered by |
|
1107 |
* a normal completion, it completes exceptionally |
|
1108 |
*/ |
|
1109 |
public void testWhenComplete_sourceCompletedNormallyActionFailed() { |
|
1110 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1111 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1112 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1113 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1114 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1115 |
final CFException ex = new CFException(); |
1116 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1117 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
|
1118 |
final CompletableFuture<Integer> g = m.whenComplete |
|
1119 |
(f, |
|
1120 |
(Integer result, Throwable t) -> { |
|
1121 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1122 |
assertSame(result, v1); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1123 |
assertNull(t); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1124 |
ran.getAndIncrement(); |
35394 | 1125 |
throw ex; |
1126 |
}); |
|
1127 |
if (createIncomplete) assertTrue(f.complete(v1)); |
|
1128 |
||
1129 |
checkCompletedWithWrappedException(g, ex); |
|
1130 |
checkCompletedNormally(f, v1); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1131 |
assertEquals(1, ran.get()); |
35394 | 1132 |
}} |
1133 |
||
1134 |
/** |
|
1135 |
* If a whenComplete action throws an exception when triggered by |
|
1136 |
* a source completion that also throws an exception, the source |
|
1137 |
* exception takes precedence (unlike handle) |
|
1138 |
*/ |
|
1139 |
public void testWhenComplete_sourceFailedActionFailed() { |
|
1140 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1141 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1142 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1143 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1144 |
final CFException ex1 = new CFException(); |
1145 |
final CFException ex2 = new CFException(); |
|
1146 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1147 |
||
1148 |
if (!createIncomplete) f.completeExceptionally(ex1); |
|
1149 |
final CompletableFuture<Integer> g = m.whenComplete |
|
1150 |
(f, |
|
1151 |
(Integer result, Throwable t) -> { |
|
1152 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1153 |
assertSame(t, ex1); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1154 |
assertNull(result); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1155 |
ran.getAndIncrement(); |
35394 | 1156 |
throw ex2; |
1157 |
}); |
|
1158 |
if (createIncomplete) f.completeExceptionally(ex1); |
|
1159 |
||
1160 |
checkCompletedWithWrappedException(g, ex1); |
|
1161 |
checkCompletedExceptionally(f, ex1); |
|
35980
1fc60a70d779
8139927: Improve documentation for CompletableFuture composition
dl
parents:
35397
diff
changeset
|
1162 |
if (testImplementationDetails) { |
1fc60a70d779
8139927: Improve documentation for CompletableFuture composition
dl
parents:
35397
diff
changeset
|
1163 |
assertEquals(1, ex1.getSuppressed().length); |
1fc60a70d779
8139927: Improve documentation for CompletableFuture composition
dl
parents:
35397
diff
changeset
|
1164 |
assertSame(ex2, ex1.getSuppressed()[0]); |
1fc60a70d779
8139927: Improve documentation for CompletableFuture composition
dl
parents:
35397
diff
changeset
|
1165 |
} |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1166 |
assertEquals(1, ran.get()); |
35394 | 1167 |
}} |
1168 |
||
1169 |
/** |
|
1170 |
* handle action completes normally with function value on normal |
|
1171 |
* completion of source |
|
1172 |
*/ |
|
1173 |
public void testHandle_normalCompletion() { |
|
1174 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1175 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1176 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1177 |
{ |
|
1178 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1179 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1180 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1181 |
final CompletableFuture<Integer> g = m.handle |
|
1182 |
(f, |
|
1183 |
(Integer result, Throwable t) -> { |
|
1184 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1185 |
assertSame(result, v1); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1186 |
assertNull(t); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1187 |
ran.getAndIncrement(); |
35394 | 1188 |
return inc(v1); |
1189 |
}); |
|
1190 |
if (createIncomplete) assertTrue(f.complete(v1)); |
|
1191 |
||
1192 |
checkCompletedNormally(g, inc(v1)); |
|
1193 |
checkCompletedNormally(f, v1); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1194 |
assertEquals(1, ran.get()); |
35394 | 1195 |
}} |
1196 |
||
1197 |
/** |
|
1198 |
* handle action completes normally with function value on |
|
1199 |
* exceptional completion of source |
|
1200 |
*/ |
|
1201 |
public void testHandle_exceptionalCompletion() { |
|
1202 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1203 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1204 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1205 |
{ |
|
1206 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1207 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1208 |
final CFException ex = new CFException(); |
1209 |
if (!createIncomplete) f.completeExceptionally(ex); |
|
1210 |
final CompletableFuture<Integer> g = m.handle |
|
1211 |
(f, |
|
1212 |
(Integer result, Throwable t) -> { |
|
1213 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1214 |
assertNull(result); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1215 |
assertSame(t, ex); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1216 |
ran.getAndIncrement(); |
35394 | 1217 |
return v1; |
1218 |
}); |
|
1219 |
if (createIncomplete) f.completeExceptionally(ex); |
|
1220 |
||
1221 |
checkCompletedNormally(g, v1); |
|
1222 |
checkCompletedExceptionally(f, ex); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1223 |
assertEquals(1, ran.get()); |
35394 | 1224 |
}} |
1225 |
||
1226 |
/** |
|
1227 |
* handle action completes normally with function value on |
|
1228 |
* cancelled source |
|
1229 |
*/ |
|
1230 |
public void testHandle_sourceCancelled() { |
|
1231 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1232 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
1233 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1234 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1235 |
{ |
|
1236 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1237 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1238 |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1239 |
final CompletableFuture<Integer> g = m.handle |
|
1240 |
(f, |
|
1241 |
(Integer result, Throwable t) -> { |
|
1242 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1243 |
assertNull(result); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1244 |
assertTrue(t instanceof CancellationException); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1245 |
ran.getAndIncrement(); |
35394 | 1246 |
return v1; |
1247 |
}); |
|
1248 |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
|
1249 |
||
1250 |
checkCompletedNormally(g, v1); |
|
1251 |
checkCancelled(f); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1252 |
assertEquals(1, ran.get()); |
35394 | 1253 |
}} |
1254 |
||
1255 |
/** |
|
1256 |
* If a "handle action" throws an exception when triggered by |
|
1257 |
* a normal completion, it completes exceptionally |
|
1258 |
*/ |
|
1259 |
public void testHandle_sourceCompletedNormallyActionFailed() { |
|
1260 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1261 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1262 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1263 |
{ |
|
1264 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1265 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1266 |
final CFException ex = new CFException(); |
1267 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
|
1268 |
final CompletableFuture<Integer> g = m.handle |
|
1269 |
(f, |
|
1270 |
(Integer result, Throwable t) -> { |
|
1271 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1272 |
assertSame(result, v1); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1273 |
assertNull(t); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1274 |
ran.getAndIncrement(); |
35394 | 1275 |
throw ex; |
1276 |
}); |
|
1277 |
if (createIncomplete) assertTrue(f.complete(v1)); |
|
1278 |
||
1279 |
checkCompletedWithWrappedException(g, ex); |
|
1280 |
checkCompletedNormally(f, v1); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1281 |
assertEquals(1, ran.get()); |
35394 | 1282 |
}} |
1283 |
||
1284 |
/** |
|
1285 |
* If a "handle action" throws an exception when triggered by |
|
1286 |
* a source completion that also throws an exception, the action |
|
1287 |
* exception takes precedence (unlike whenComplete) |
|
1288 |
*/ |
|
1289 |
public void testHandle_sourceFailedActionFailed() { |
|
1290 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
1291 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1292 |
{ |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1293 |
final AtomicInteger ran = new AtomicInteger(0); |
35394 | 1294 |
final CFException ex1 = new CFException(); |
1295 |
final CFException ex2 = new CFException(); |
|
1296 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1297 |
||
1298 |
if (!createIncomplete) f.completeExceptionally(ex1); |
|
1299 |
final CompletableFuture<Integer> g = m.handle |
|
1300 |
(f, |
|
1301 |
(Integer result, Throwable t) -> { |
|
1302 |
m.checkExecutionMode(); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1303 |
assertNull(result); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1304 |
assertSame(ex1, t); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1305 |
ran.getAndIncrement(); |
35394 | 1306 |
throw ex2; |
1307 |
}); |
|
1308 |
if (createIncomplete) f.completeExceptionally(ex1); |
|
1309 |
||
1310 |
checkCompletedWithWrappedException(g, ex2); |
|
1311 |
checkCompletedExceptionally(f, ex1); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
1312 |
assertEquals(1, ran.get()); |
35394 | 1313 |
}} |
1314 |
||
1315 |
/** |
|
1316 |
* runAsync completes after running Runnable |
|
1317 |
*/ |
|
1318 |
public void testRunAsync_normalCompletion() { |
|
1319 |
ExecutionMode[] executionModes = { |
|
1320 |
ExecutionMode.ASYNC, |
|
1321 |
ExecutionMode.EXECUTOR, |
|
1322 |
}; |
|
1323 |
for (ExecutionMode m : executionModes) |
|
1324 |
{ |
|
1325 |
final Noop r = new Noop(m); |
|
1326 |
final CompletableFuture<Void> f = m.runAsync(r); |
|
1327 |
assertNull(f.join()); |
|
1328 |
checkCompletedNormally(f, null); |
|
1329 |
r.assertInvoked(); |
|
1330 |
}} |
|
1331 |
||
1332 |
/** |
|
1333 |
* failing runAsync completes exceptionally after running Runnable |
|
1334 |
*/ |
|
1335 |
public void testRunAsync_exceptionalCompletion() { |
|
1336 |
ExecutionMode[] executionModes = { |
|
1337 |
ExecutionMode.ASYNC, |
|
1338 |
ExecutionMode.EXECUTOR, |
|
1339 |
}; |
|
1340 |
for (ExecutionMode m : executionModes) |
|
1341 |
{ |
|
1342 |
final FailingRunnable r = new FailingRunnable(m); |
|
1343 |
final CompletableFuture<Void> f = m.runAsync(r); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1344 |
checkCompletedWithWrappedException(f, r.ex); |
35394 | 1345 |
r.assertInvoked(); |
1346 |
}} |
|
1347 |
||
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
1348 |
@SuppressWarnings("FutureReturnValueIgnored") |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1349 |
public void testRunAsync_rejectingExecutor() { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1350 |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1351 |
try { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1352 |
CompletableFuture.runAsync(() -> {}, e); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1353 |
shouldThrow(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1354 |
} catch (Throwable t) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1355 |
assertSame(e.ex, t); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1356 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1357 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1358 |
assertEquals(1, e.count.get()); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1359 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1360 |
|
35394 | 1361 |
/** |
1362 |
* supplyAsync completes with result of supplier |
|
1363 |
*/ |
|
1364 |
public void testSupplyAsync_normalCompletion() { |
|
1365 |
ExecutionMode[] executionModes = { |
|
1366 |
ExecutionMode.ASYNC, |
|
1367 |
ExecutionMode.EXECUTOR, |
|
1368 |
}; |
|
1369 |
for (ExecutionMode m : executionModes) |
|
1370 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1371 |
{ |
|
1372 |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
|
1373 |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
|
1374 |
assertSame(v1, f.join()); |
|
1375 |
checkCompletedNormally(f, v1); |
|
1376 |
r.assertInvoked(); |
|
1377 |
}} |
|
1378 |
||
1379 |
/** |
|
1380 |
* Failing supplyAsync completes exceptionally |
|
1381 |
*/ |
|
1382 |
public void testSupplyAsync_exceptionalCompletion() { |
|
1383 |
ExecutionMode[] executionModes = { |
|
1384 |
ExecutionMode.ASYNC, |
|
1385 |
ExecutionMode.EXECUTOR, |
|
1386 |
}; |
|
1387 |
for (ExecutionMode m : executionModes) |
|
1388 |
{ |
|
1389 |
FailingSupplier r = new FailingSupplier(m); |
|
1390 |
CompletableFuture<Integer> f = m.supplyAsync(r); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1391 |
checkCompletedWithWrappedException(f, r.ex); |
35394 | 1392 |
r.assertInvoked(); |
1393 |
}} |
|
1394 |
||
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
1395 |
@SuppressWarnings("FutureReturnValueIgnored") |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1396 |
public void testSupplyAsync_rejectingExecutor() { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1397 |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1398 |
try { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1399 |
CompletableFuture.supplyAsync(() -> null, e); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1400 |
shouldThrow(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1401 |
} catch (Throwable t) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1402 |
assertSame(e.ex, t); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1403 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1404 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1405 |
assertEquals(1, e.count.get()); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1406 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1407 |
|
35394 | 1408 |
// seq completion methods |
1409 |
||
1410 |
/** |
|
1411 |
* thenRun result completes normally after normal completion of source |
|
1412 |
*/ |
|
1413 |
public void testThenRun_normalCompletion() { |
|
1414 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1415 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1416 |
{ |
|
1417 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1418 |
final Noop[] rs = new Noop[6]; |
|
1419 |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
|
1420 |
||
1421 |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
|
1422 |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
|
1423 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
|
1424 |
checkIncomplete(h0); |
|
1425 |
checkIncomplete(h1); |
|
1426 |
checkIncomplete(h2); |
|
1427 |
assertTrue(f.complete(v1)); |
|
1428 |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
|
1429 |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
|
1430 |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
|
1431 |
||
1432 |
checkCompletedNormally(h0, null); |
|
1433 |
checkCompletedNormally(h1, null); |
|
1434 |
checkCompletedNormally(h2, null); |
|
1435 |
checkCompletedNormally(h3, null); |
|
1436 |
checkCompletedNormally(h4, null); |
|
1437 |
checkCompletedNormally(h5, null); |
|
1438 |
checkCompletedNormally(f, v1); |
|
1439 |
for (Noop r : rs) r.assertInvoked(); |
|
1440 |
}} |
|
1441 |
||
1442 |
/** |
|
1443 |
* thenRun result completes exceptionally after exceptional |
|
1444 |
* completion of source |
|
1445 |
*/ |
|
1446 |
public void testThenRun_exceptionalCompletion() { |
|
1447 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1448 |
{ |
|
1449 |
final CFException ex = new CFException(); |
|
1450 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1451 |
final Noop[] rs = new Noop[6]; |
|
1452 |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
|
1453 |
||
1454 |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
|
1455 |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
|
1456 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
|
1457 |
checkIncomplete(h0); |
|
1458 |
checkIncomplete(h1); |
|
1459 |
checkIncomplete(h2); |
|
1460 |
assertTrue(f.completeExceptionally(ex)); |
|
1461 |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
|
1462 |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
|
1463 |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
|
1464 |
||
1465 |
checkCompletedWithWrappedException(h0, ex); |
|
1466 |
checkCompletedWithWrappedException(h1, ex); |
|
1467 |
checkCompletedWithWrappedException(h2, ex); |
|
1468 |
checkCompletedWithWrappedException(h3, ex); |
|
1469 |
checkCompletedWithWrappedException(h4, ex); |
|
1470 |
checkCompletedWithWrappedException(h5, ex); |
|
1471 |
checkCompletedExceptionally(f, ex); |
|
1472 |
for (Noop r : rs) r.assertNotInvoked(); |
|
1473 |
}} |
|
1474 |
||
1475 |
/** |
|
1476 |
* thenRun result completes exceptionally if source cancelled |
|
1477 |
*/ |
|
1478 |
public void testThenRun_sourceCancelled() { |
|
1479 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1480 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
1481 |
{ |
|
1482 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1483 |
final Noop[] rs = new Noop[6]; |
|
1484 |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
|
1485 |
||
1486 |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
|
1487 |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
|
1488 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
|
1489 |
checkIncomplete(h0); |
|
1490 |
checkIncomplete(h1); |
|
1491 |
checkIncomplete(h2); |
|
1492 |
assertTrue(f.cancel(mayInterruptIfRunning)); |
|
1493 |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
|
1494 |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
|
1495 |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
|
1496 |
||
1497 |
checkCompletedWithWrappedCancellationException(h0); |
|
1498 |
checkCompletedWithWrappedCancellationException(h1); |
|
1499 |
checkCompletedWithWrappedCancellationException(h2); |
|
1500 |
checkCompletedWithWrappedCancellationException(h3); |
|
1501 |
checkCompletedWithWrappedCancellationException(h4); |
|
1502 |
checkCompletedWithWrappedCancellationException(h5); |
|
1503 |
checkCancelled(f); |
|
1504 |
for (Noop r : rs) r.assertNotInvoked(); |
|
1505 |
}} |
|
1506 |
||
1507 |
/** |
|
1508 |
* thenRun result completes exceptionally if action does |
|
1509 |
*/ |
|
1510 |
public void testThenRun_actionFailed() { |
|
1511 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1512 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1513 |
{ |
|
1514 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1515 |
final FailingRunnable[] rs = new FailingRunnable[6]; |
|
1516 |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
|
1517 |
||
1518 |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
|
1519 |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
|
1520 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
|
1521 |
assertTrue(f.complete(v1)); |
|
1522 |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
|
1523 |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
|
1524 |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
|
1525 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1526 |
checkCompletedWithWrappedException(h0, rs[0].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1527 |
checkCompletedWithWrappedException(h1, rs[1].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1528 |
checkCompletedWithWrappedException(h2, rs[2].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1529 |
checkCompletedWithWrappedException(h3, rs[3].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1530 |
checkCompletedWithWrappedException(h4, rs[4].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1531 |
checkCompletedWithWrappedException(h5, rs[5].ex); |
35394 | 1532 |
checkCompletedNormally(f, v1); |
1533 |
}} |
|
1534 |
||
1535 |
/** |
|
1536 |
* thenApply result completes normally after normal completion of source |
|
1537 |
*/ |
|
1538 |
public void testThenApply_normalCompletion() { |
|
1539 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1540 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1541 |
{ |
|
1542 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1543 |
final IncFunction[] rs = new IncFunction[4]; |
|
1544 |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
|
1545 |
||
1546 |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
|
1547 |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
|
1548 |
checkIncomplete(h0); |
|
1549 |
checkIncomplete(h1); |
|
1550 |
assertTrue(f.complete(v1)); |
|
1551 |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
|
1552 |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
|
1553 |
||
1554 |
checkCompletedNormally(h0, inc(v1)); |
|
1555 |
checkCompletedNormally(h1, inc(v1)); |
|
1556 |
checkCompletedNormally(h2, inc(v1)); |
|
1557 |
checkCompletedNormally(h3, inc(v1)); |
|
1558 |
checkCompletedNormally(f, v1); |
|
1559 |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
|
1560 |
}} |
|
1561 |
||
1562 |
/** |
|
1563 |
* thenApply result completes exceptionally after exceptional |
|
1564 |
* completion of source |
|
1565 |
*/ |
|
1566 |
public void testThenApply_exceptionalCompletion() { |
|
1567 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1568 |
{ |
|
1569 |
final CFException ex = new CFException(); |
|
1570 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1571 |
final IncFunction[] rs = new IncFunction[4]; |
|
1572 |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
|
1573 |
||
1574 |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
|
1575 |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
|
1576 |
assertTrue(f.completeExceptionally(ex)); |
|
1577 |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
|
1578 |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
|
1579 |
||
1580 |
checkCompletedWithWrappedException(h0, ex); |
|
1581 |
checkCompletedWithWrappedException(h1, ex); |
|
1582 |
checkCompletedWithWrappedException(h2, ex); |
|
1583 |
checkCompletedWithWrappedException(h3, ex); |
|
1584 |
checkCompletedExceptionally(f, ex); |
|
1585 |
for (IncFunction r : rs) r.assertNotInvoked(); |
|
1586 |
}} |
|
1587 |
||
1588 |
/** |
|
1589 |
* thenApply result completes exceptionally if source cancelled |
|
1590 |
*/ |
|
1591 |
public void testThenApply_sourceCancelled() { |
|
1592 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1593 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
1594 |
{ |
|
1595 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1596 |
final IncFunction[] rs = new IncFunction[4]; |
|
1597 |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
|
1598 |
||
1599 |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
|
1600 |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
|
1601 |
assertTrue(f.cancel(mayInterruptIfRunning)); |
|
1602 |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
|
1603 |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
|
1604 |
||
1605 |
checkCompletedWithWrappedCancellationException(h0); |
|
1606 |
checkCompletedWithWrappedCancellationException(h1); |
|
1607 |
checkCompletedWithWrappedCancellationException(h2); |
|
1608 |
checkCompletedWithWrappedCancellationException(h3); |
|
1609 |
checkCancelled(f); |
|
1610 |
for (IncFunction r : rs) r.assertNotInvoked(); |
|
1611 |
}} |
|
1612 |
||
1613 |
/** |
|
1614 |
* thenApply result completes exceptionally if action does |
|
1615 |
*/ |
|
1616 |
public void testThenApply_actionFailed() { |
|
1617 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1618 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1619 |
{ |
|
1620 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1621 |
final FailingFunction[] rs = new FailingFunction[4]; |
|
1622 |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
|
1623 |
||
1624 |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
|
1625 |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
|
1626 |
assertTrue(f.complete(v1)); |
|
1627 |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
|
1628 |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
|
1629 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1630 |
checkCompletedWithWrappedException(h0, rs[0].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1631 |
checkCompletedWithWrappedException(h1, rs[1].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1632 |
checkCompletedWithWrappedException(h2, rs[2].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1633 |
checkCompletedWithWrappedException(h3, rs[3].ex); |
35394 | 1634 |
checkCompletedNormally(f, v1); |
1635 |
}} |
|
1636 |
||
1637 |
/** |
|
1638 |
* thenAccept result completes normally after normal completion of source |
|
1639 |
*/ |
|
1640 |
public void testThenAccept_normalCompletion() { |
|
1641 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1642 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1643 |
{ |
|
1644 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1645 |
final NoopConsumer[] rs = new NoopConsumer[4]; |
|
1646 |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
|
1647 |
||
1648 |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
|
1649 |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
|
1650 |
checkIncomplete(h0); |
|
1651 |
checkIncomplete(h1); |
|
1652 |
assertTrue(f.complete(v1)); |
|
1653 |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
|
1654 |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
|
1655 |
||
1656 |
checkCompletedNormally(h0, null); |
|
1657 |
checkCompletedNormally(h1, null); |
|
1658 |
checkCompletedNormally(h2, null); |
|
1659 |
checkCompletedNormally(h3, null); |
|
1660 |
checkCompletedNormally(f, v1); |
|
1661 |
for (NoopConsumer r : rs) r.assertValue(v1); |
|
1662 |
}} |
|
1663 |
||
1664 |
/** |
|
1665 |
* thenAccept result completes exceptionally after exceptional |
|
1666 |
* completion of source |
|
1667 |
*/ |
|
1668 |
public void testThenAccept_exceptionalCompletion() { |
|
1669 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1670 |
{ |
|
1671 |
final CFException ex = new CFException(); |
|
1672 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1673 |
final NoopConsumer[] rs = new NoopConsumer[4]; |
|
1674 |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
|
1675 |
||
1676 |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
|
1677 |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
|
1678 |
assertTrue(f.completeExceptionally(ex)); |
|
1679 |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
|
1680 |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
|
1681 |
||
1682 |
checkCompletedWithWrappedException(h0, ex); |
|
1683 |
checkCompletedWithWrappedException(h1, ex); |
|
1684 |
checkCompletedWithWrappedException(h2, ex); |
|
1685 |
checkCompletedWithWrappedException(h3, ex); |
|
1686 |
checkCompletedExceptionally(f, ex); |
|
1687 |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
|
1688 |
}} |
|
1689 |
||
1690 |
/** |
|
1691 |
* thenAccept result completes exceptionally if source cancelled |
|
1692 |
*/ |
|
1693 |
public void testThenAccept_sourceCancelled() { |
|
1694 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1695 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
1696 |
{ |
|
1697 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1698 |
final NoopConsumer[] rs = new NoopConsumer[4]; |
|
1699 |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
|
1700 |
||
1701 |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
|
1702 |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
|
1703 |
assertTrue(f.cancel(mayInterruptIfRunning)); |
|
1704 |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
|
1705 |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
|
1706 |
||
1707 |
checkCompletedWithWrappedCancellationException(h0); |
|
1708 |
checkCompletedWithWrappedCancellationException(h1); |
|
1709 |
checkCompletedWithWrappedCancellationException(h2); |
|
1710 |
checkCompletedWithWrappedCancellationException(h3); |
|
1711 |
checkCancelled(f); |
|
1712 |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
|
1713 |
}} |
|
1714 |
||
1715 |
/** |
|
1716 |
* thenAccept result completes exceptionally if action does |
|
1717 |
*/ |
|
1718 |
public void testThenAccept_actionFailed() { |
|
1719 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1720 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1721 |
{ |
|
1722 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1723 |
final FailingConsumer[] rs = new FailingConsumer[4]; |
|
1724 |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
|
1725 |
||
1726 |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
|
1727 |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
|
1728 |
assertTrue(f.complete(v1)); |
|
1729 |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
|
1730 |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
|
1731 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1732 |
checkCompletedWithWrappedException(h0, rs[0].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1733 |
checkCompletedWithWrappedException(h1, rs[1].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1734 |
checkCompletedWithWrappedException(h2, rs[2].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1735 |
checkCompletedWithWrappedException(h3, rs[3].ex); |
35394 | 1736 |
checkCompletedNormally(f, v1); |
1737 |
}} |
|
1738 |
||
1739 |
/** |
|
1740 |
* thenCombine result completes normally after normal completion |
|
1741 |
* of sources |
|
1742 |
*/ |
|
1743 |
public void testThenCombine_normalCompletion() { |
|
1744 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1745 |
for (boolean fFirst : new boolean[] { true, false }) |
|
1746 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1747 |
for (Integer v2 : new Integer[] { 2, null }) |
|
1748 |
{ |
|
1749 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1750 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
1751 |
final SubtractFunction[] rs = new SubtractFunction[6]; |
|
1752 |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
|
1753 |
||
1754 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
1755 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
1756 |
final Integer w1 = fFirst ? v1 : v2; |
|
1757 |
final Integer w2 = !fFirst ? v1 : v2; |
|
1758 |
||
1759 |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
|
1760 |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
|
1761 |
assertTrue(fst.complete(w1)); |
|
1762 |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
|
1763 |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
|
1764 |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
|
1765 |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
|
1766 |
checkCompletedNormally(h1, subtract(w1, w1)); |
|
1767 |
checkCompletedNormally(h3, subtract(w1, w1)); |
|
1768 |
rs[1].assertValue(subtract(w1, w1)); |
|
1769 |
rs[3].assertValue(subtract(w1, w1)); |
|
1770 |
assertTrue(snd.complete(w2)); |
|
1771 |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
|
1772 |
||
1773 |
checkCompletedNormally(h0, subtract(v1, v2)); |
|
1774 |
checkCompletedNormally(h2, subtract(v1, v2)); |
|
1775 |
checkCompletedNormally(h4, subtract(v1, v2)); |
|
1776 |
rs[0].assertValue(subtract(v1, v2)); |
|
1777 |
rs[2].assertValue(subtract(v1, v2)); |
|
1778 |
rs[4].assertValue(subtract(v1, v2)); |
|
1779 |
||
1780 |
checkCompletedNormally(f, v1); |
|
1781 |
checkCompletedNormally(g, v2); |
|
1782 |
}} |
|
1783 |
||
1784 |
/** |
|
1785 |
* thenCombine result completes exceptionally after exceptional |
|
1786 |
* completion of either source |
|
1787 |
*/ |
|
1788 |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
|
1789 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1790 |
for (boolean fFirst : new boolean[] { true, false }) |
|
1791 |
for (boolean failFirst : new boolean[] { true, false }) |
|
1792 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1793 |
{ |
|
1794 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1795 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
1796 |
final CFException ex = new CFException(); |
|
1797 |
final SubtractFunction r1 = new SubtractFunction(m); |
|
1798 |
final SubtractFunction r2 = new SubtractFunction(m); |
|
1799 |
final SubtractFunction r3 = new SubtractFunction(m); |
|
1800 |
||
1801 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
1802 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
1803 |
final Callable<Boolean> complete1 = failFirst ? |
|
1804 |
() -> fst.completeExceptionally(ex) : |
|
1805 |
() -> fst.complete(v1); |
|
1806 |
final Callable<Boolean> complete2 = failFirst ? |
|
1807 |
() -> snd.complete(v1) : |
|
1808 |
() -> snd.completeExceptionally(ex); |
|
1809 |
||
1810 |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
|
1811 |
assertTrue(complete1.call()); |
|
1812 |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
|
1813 |
checkIncomplete(h1); |
|
1814 |
checkIncomplete(h2); |
|
1815 |
assertTrue(complete2.call()); |
|
1816 |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
|
1817 |
||
1818 |
checkCompletedWithWrappedException(h1, ex); |
|
1819 |
checkCompletedWithWrappedException(h2, ex); |
|
1820 |
checkCompletedWithWrappedException(h3, ex); |
|
1821 |
r1.assertNotInvoked(); |
|
1822 |
r2.assertNotInvoked(); |
|
1823 |
r3.assertNotInvoked(); |
|
1824 |
checkCompletedNormally(failFirst ? snd : fst, v1); |
|
1825 |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
|
1826 |
}} |
|
1827 |
||
1828 |
/** |
|
1829 |
* thenCombine result completes exceptionally if either source cancelled |
|
1830 |
*/ |
|
1831 |
public void testThenCombine_sourceCancelled() throws Throwable { |
|
1832 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1833 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
1834 |
for (boolean fFirst : new boolean[] { true, false }) |
|
1835 |
for (boolean failFirst : new boolean[] { true, false }) |
|
1836 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1837 |
{ |
|
1838 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1839 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
1840 |
final SubtractFunction r1 = new SubtractFunction(m); |
|
1841 |
final SubtractFunction r2 = new SubtractFunction(m); |
|
1842 |
final SubtractFunction r3 = new SubtractFunction(m); |
|
1843 |
||
1844 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
1845 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
1846 |
final Callable<Boolean> complete1 = failFirst ? |
|
1847 |
() -> fst.cancel(mayInterruptIfRunning) : |
|
1848 |
() -> fst.complete(v1); |
|
1849 |
final Callable<Boolean> complete2 = failFirst ? |
|
1850 |
() -> snd.complete(v1) : |
|
1851 |
() -> snd.cancel(mayInterruptIfRunning); |
|
1852 |
||
1853 |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
|
1854 |
assertTrue(complete1.call()); |
|
1855 |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
|
1856 |
checkIncomplete(h1); |
|
1857 |
checkIncomplete(h2); |
|
1858 |
assertTrue(complete2.call()); |
|
1859 |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
|
1860 |
||
1861 |
checkCompletedWithWrappedCancellationException(h1); |
|
1862 |
checkCompletedWithWrappedCancellationException(h2); |
|
1863 |
checkCompletedWithWrappedCancellationException(h3); |
|
1864 |
r1.assertNotInvoked(); |
|
1865 |
r2.assertNotInvoked(); |
|
1866 |
r3.assertNotInvoked(); |
|
1867 |
checkCompletedNormally(failFirst ? snd : fst, v1); |
|
1868 |
checkCancelled(failFirst ? fst : snd); |
|
1869 |
}} |
|
1870 |
||
1871 |
/** |
|
1872 |
* thenCombine result completes exceptionally if action does |
|
1873 |
*/ |
|
1874 |
public void testThenCombine_actionFailed() { |
|
1875 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1876 |
for (boolean fFirst : new boolean[] { true, false }) |
|
1877 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1878 |
for (Integer v2 : new Integer[] { 2, null }) |
|
1879 |
{ |
|
1880 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1881 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
1882 |
final FailingBiFunction r1 = new FailingBiFunction(m); |
|
1883 |
final FailingBiFunction r2 = new FailingBiFunction(m); |
|
1884 |
final FailingBiFunction r3 = new FailingBiFunction(m); |
|
1885 |
||
1886 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
1887 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
1888 |
final Integer w1 = fFirst ? v1 : v2; |
|
1889 |
final Integer w2 = !fFirst ? v1 : v2; |
|
1890 |
||
1891 |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
|
1892 |
assertTrue(fst.complete(w1)); |
|
1893 |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
|
1894 |
assertTrue(snd.complete(w2)); |
|
1895 |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
|
1896 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1897 |
checkCompletedWithWrappedException(h1, r1.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1898 |
checkCompletedWithWrappedException(h2, r2.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
1899 |
checkCompletedWithWrappedException(h3, r3.ex); |
35394 | 1900 |
r1.assertInvoked(); |
1901 |
r2.assertInvoked(); |
|
1902 |
r3.assertInvoked(); |
|
1903 |
checkCompletedNormally(f, v1); |
|
1904 |
checkCompletedNormally(g, v2); |
|
1905 |
}} |
|
1906 |
||
1907 |
/** |
|
1908 |
* thenAcceptBoth result completes normally after normal |
|
1909 |
* completion of sources |
|
1910 |
*/ |
|
1911 |
public void testThenAcceptBoth_normalCompletion() { |
|
1912 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1913 |
for (boolean fFirst : new boolean[] { true, false }) |
|
1914 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1915 |
for (Integer v2 : new Integer[] { 2, null }) |
|
1916 |
{ |
|
1917 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1918 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
1919 |
final SubtractAction r1 = new SubtractAction(m); |
|
1920 |
final SubtractAction r2 = new SubtractAction(m); |
|
1921 |
final SubtractAction r3 = new SubtractAction(m); |
|
1922 |
||
1923 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
1924 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
1925 |
final Integer w1 = fFirst ? v1 : v2; |
|
1926 |
final Integer w2 = !fFirst ? v1 : v2; |
|
1927 |
||
1928 |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
|
1929 |
assertTrue(fst.complete(w1)); |
|
1930 |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
|
1931 |
checkIncomplete(h1); |
|
1932 |
checkIncomplete(h2); |
|
1933 |
r1.assertNotInvoked(); |
|
1934 |
r2.assertNotInvoked(); |
|
1935 |
assertTrue(snd.complete(w2)); |
|
1936 |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
|
1937 |
||
1938 |
checkCompletedNormally(h1, null); |
|
1939 |
checkCompletedNormally(h2, null); |
|
1940 |
checkCompletedNormally(h3, null); |
|
1941 |
r1.assertValue(subtract(v1, v2)); |
|
1942 |
r2.assertValue(subtract(v1, v2)); |
|
1943 |
r3.assertValue(subtract(v1, v2)); |
|
1944 |
checkCompletedNormally(f, v1); |
|
1945 |
checkCompletedNormally(g, v2); |
|
1946 |
}} |
|
1947 |
||
1948 |
/** |
|
1949 |
* thenAcceptBoth result completes exceptionally after exceptional |
|
1950 |
* completion of either source |
|
1951 |
*/ |
|
1952 |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
|
1953 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1954 |
for (boolean fFirst : new boolean[] { true, false }) |
|
1955 |
for (boolean failFirst : new boolean[] { true, false }) |
|
1956 |
for (Integer v1 : new Integer[] { 1, null }) |
|
1957 |
{ |
|
1958 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
1959 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
1960 |
final CFException ex = new CFException(); |
|
1961 |
final SubtractAction r1 = new SubtractAction(m); |
|
1962 |
final SubtractAction r2 = new SubtractAction(m); |
|
1963 |
final SubtractAction r3 = new SubtractAction(m); |
|
1964 |
||
1965 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
1966 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
1967 |
final Callable<Boolean> complete1 = failFirst ? |
|
1968 |
() -> fst.completeExceptionally(ex) : |
|
1969 |
() -> fst.complete(v1); |
|
1970 |
final Callable<Boolean> complete2 = failFirst ? |
|
1971 |
() -> snd.complete(v1) : |
|
1972 |
() -> snd.completeExceptionally(ex); |
|
1973 |
||
1974 |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
|
1975 |
assertTrue(complete1.call()); |
|
1976 |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
|
1977 |
checkIncomplete(h1); |
|
1978 |
checkIncomplete(h2); |
|
1979 |
assertTrue(complete2.call()); |
|
1980 |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
|
1981 |
||
1982 |
checkCompletedWithWrappedException(h1, ex); |
|
1983 |
checkCompletedWithWrappedException(h2, ex); |
|
1984 |
checkCompletedWithWrappedException(h3, ex); |
|
1985 |
r1.assertNotInvoked(); |
|
1986 |
r2.assertNotInvoked(); |
|
1987 |
r3.assertNotInvoked(); |
|
1988 |
checkCompletedNormally(failFirst ? snd : fst, v1); |
|
1989 |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
|
1990 |
}} |
|
1991 |
||
1992 |
/** |
|
1993 |
* thenAcceptBoth result completes exceptionally if either source cancelled |
|
1994 |
*/ |
|
1995 |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
|
1996 |
for (ExecutionMode m : ExecutionMode.values()) |
|
1997 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
1998 |
for (boolean fFirst : new boolean[] { true, false }) |
|
1999 |
for (boolean failFirst : new boolean[] { true, false }) |
|
2000 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2001 |
{ |
|
2002 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2003 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2004 |
final SubtractAction r1 = new SubtractAction(m); |
|
2005 |
final SubtractAction r2 = new SubtractAction(m); |
|
2006 |
final SubtractAction r3 = new SubtractAction(m); |
|
2007 |
||
2008 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
2009 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
2010 |
final Callable<Boolean> complete1 = failFirst ? |
|
2011 |
() -> fst.cancel(mayInterruptIfRunning) : |
|
2012 |
() -> fst.complete(v1); |
|
2013 |
final Callable<Boolean> complete2 = failFirst ? |
|
2014 |
() -> snd.complete(v1) : |
|
2015 |
() -> snd.cancel(mayInterruptIfRunning); |
|
2016 |
||
2017 |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
|
2018 |
assertTrue(complete1.call()); |
|
2019 |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
|
2020 |
checkIncomplete(h1); |
|
2021 |
checkIncomplete(h2); |
|
2022 |
assertTrue(complete2.call()); |
|
2023 |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
|
2024 |
||
2025 |
checkCompletedWithWrappedCancellationException(h1); |
|
2026 |
checkCompletedWithWrappedCancellationException(h2); |
|
2027 |
checkCompletedWithWrappedCancellationException(h3); |
|
2028 |
r1.assertNotInvoked(); |
|
2029 |
r2.assertNotInvoked(); |
|
2030 |
r3.assertNotInvoked(); |
|
2031 |
checkCompletedNormally(failFirst ? snd : fst, v1); |
|
2032 |
checkCancelled(failFirst ? fst : snd); |
|
2033 |
}} |
|
2034 |
||
2035 |
/** |
|
2036 |
* thenAcceptBoth result completes exceptionally if action does |
|
2037 |
*/ |
|
2038 |
public void testThenAcceptBoth_actionFailed() { |
|
2039 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2040 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2041 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2042 |
for (Integer v2 : new Integer[] { 2, null }) |
|
2043 |
{ |
|
2044 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2045 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2046 |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
|
2047 |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
|
2048 |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
|
2049 |
||
2050 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
2051 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
2052 |
final Integer w1 = fFirst ? v1 : v2; |
|
2053 |
final Integer w2 = !fFirst ? v1 : v2; |
|
2054 |
||
2055 |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
|
2056 |
assertTrue(fst.complete(w1)); |
|
2057 |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
|
2058 |
assertTrue(snd.complete(w2)); |
|
2059 |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
|
2060 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2061 |
checkCompletedWithWrappedException(h1, r1.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2062 |
checkCompletedWithWrappedException(h2, r2.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2063 |
checkCompletedWithWrappedException(h3, r3.ex); |
35394 | 2064 |
r1.assertInvoked(); |
2065 |
r2.assertInvoked(); |
|
2066 |
r3.assertInvoked(); |
|
2067 |
checkCompletedNormally(f, v1); |
|
2068 |
checkCompletedNormally(g, v2); |
|
2069 |
}} |
|
2070 |
||
2071 |
/** |
|
2072 |
* runAfterBoth result completes normally after normal |
|
2073 |
* completion of sources |
|
2074 |
*/ |
|
2075 |
public void testRunAfterBoth_normalCompletion() { |
|
2076 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2077 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2078 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2079 |
for (Integer v2 : new Integer[] { 2, null }) |
|
2080 |
{ |
|
2081 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2082 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2083 |
final Noop r1 = new Noop(m); |
|
2084 |
final Noop r2 = new Noop(m); |
|
2085 |
final Noop r3 = new Noop(m); |
|
2086 |
||
2087 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
2088 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
2089 |
final Integer w1 = fFirst ? v1 : v2; |
|
2090 |
final Integer w2 = !fFirst ? v1 : v2; |
|
2091 |
||
2092 |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
|
2093 |
assertTrue(fst.complete(w1)); |
|
2094 |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
|
2095 |
checkIncomplete(h1); |
|
2096 |
checkIncomplete(h2); |
|
2097 |
r1.assertNotInvoked(); |
|
2098 |
r2.assertNotInvoked(); |
|
2099 |
assertTrue(snd.complete(w2)); |
|
2100 |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
|
2101 |
||
2102 |
checkCompletedNormally(h1, null); |
|
2103 |
checkCompletedNormally(h2, null); |
|
2104 |
checkCompletedNormally(h3, null); |
|
2105 |
r1.assertInvoked(); |
|
2106 |
r2.assertInvoked(); |
|
2107 |
r3.assertInvoked(); |
|
2108 |
checkCompletedNormally(f, v1); |
|
2109 |
checkCompletedNormally(g, v2); |
|
2110 |
}} |
|
2111 |
||
2112 |
/** |
|
2113 |
* runAfterBoth result completes exceptionally after exceptional |
|
2114 |
* completion of either source |
|
2115 |
*/ |
|
2116 |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
|
2117 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2118 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2119 |
for (boolean failFirst : new boolean[] { true, false }) |
|
2120 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2121 |
{ |
|
2122 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2123 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2124 |
final CFException ex = new CFException(); |
|
2125 |
final Noop r1 = new Noop(m); |
|
2126 |
final Noop r2 = new Noop(m); |
|
2127 |
final Noop r3 = new Noop(m); |
|
2128 |
||
2129 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
2130 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
2131 |
final Callable<Boolean> complete1 = failFirst ? |
|
2132 |
() -> fst.completeExceptionally(ex) : |
|
2133 |
() -> fst.complete(v1); |
|
2134 |
final Callable<Boolean> complete2 = failFirst ? |
|
2135 |
() -> snd.complete(v1) : |
|
2136 |
() -> snd.completeExceptionally(ex); |
|
2137 |
||
2138 |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
|
2139 |
assertTrue(complete1.call()); |
|
2140 |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
|
2141 |
checkIncomplete(h1); |
|
2142 |
checkIncomplete(h2); |
|
2143 |
assertTrue(complete2.call()); |
|
2144 |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
|
2145 |
||
2146 |
checkCompletedWithWrappedException(h1, ex); |
|
2147 |
checkCompletedWithWrappedException(h2, ex); |
|
2148 |
checkCompletedWithWrappedException(h3, ex); |
|
2149 |
r1.assertNotInvoked(); |
|
2150 |
r2.assertNotInvoked(); |
|
2151 |
r3.assertNotInvoked(); |
|
2152 |
checkCompletedNormally(failFirst ? snd : fst, v1); |
|
2153 |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
|
2154 |
}} |
|
2155 |
||
2156 |
/** |
|
2157 |
* runAfterBoth result completes exceptionally if either source cancelled |
|
2158 |
*/ |
|
2159 |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
|
2160 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2161 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
2162 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2163 |
for (boolean failFirst : new boolean[] { true, false }) |
|
2164 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2165 |
{ |
|
2166 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2167 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2168 |
final Noop r1 = new Noop(m); |
|
2169 |
final Noop r2 = new Noop(m); |
|
2170 |
final Noop r3 = new Noop(m); |
|
2171 |
||
2172 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
2173 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
2174 |
final Callable<Boolean> complete1 = failFirst ? |
|
2175 |
() -> fst.cancel(mayInterruptIfRunning) : |
|
2176 |
() -> fst.complete(v1); |
|
2177 |
final Callable<Boolean> complete2 = failFirst ? |
|
2178 |
() -> snd.complete(v1) : |
|
2179 |
() -> snd.cancel(mayInterruptIfRunning); |
|
2180 |
||
2181 |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
|
2182 |
assertTrue(complete1.call()); |
|
2183 |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
|
2184 |
checkIncomplete(h1); |
|
2185 |
checkIncomplete(h2); |
|
2186 |
assertTrue(complete2.call()); |
|
2187 |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
|
2188 |
||
2189 |
checkCompletedWithWrappedCancellationException(h1); |
|
2190 |
checkCompletedWithWrappedCancellationException(h2); |
|
2191 |
checkCompletedWithWrappedCancellationException(h3); |
|
2192 |
r1.assertNotInvoked(); |
|
2193 |
r2.assertNotInvoked(); |
|
2194 |
r3.assertNotInvoked(); |
|
2195 |
checkCompletedNormally(failFirst ? snd : fst, v1); |
|
2196 |
checkCancelled(failFirst ? fst : snd); |
|
2197 |
}} |
|
2198 |
||
2199 |
/** |
|
2200 |
* runAfterBoth result completes exceptionally if action does |
|
2201 |
*/ |
|
2202 |
public void testRunAfterBoth_actionFailed() { |
|
2203 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2204 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2205 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2206 |
for (Integer v2 : new Integer[] { 2, null }) |
|
2207 |
{ |
|
2208 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2209 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2210 |
final FailingRunnable r1 = new FailingRunnable(m); |
|
2211 |
final FailingRunnable r2 = new FailingRunnable(m); |
|
2212 |
final FailingRunnable r3 = new FailingRunnable(m); |
|
2213 |
||
2214 |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
|
2215 |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
|
2216 |
final Integer w1 = fFirst ? v1 : v2; |
|
2217 |
final Integer w2 = !fFirst ? v1 : v2; |
|
2218 |
||
2219 |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
|
2220 |
assertTrue(fst.complete(w1)); |
|
2221 |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
|
2222 |
assertTrue(snd.complete(w2)); |
|
2223 |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
|
2224 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2225 |
checkCompletedWithWrappedException(h1, r1.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2226 |
checkCompletedWithWrappedException(h2, r2.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2227 |
checkCompletedWithWrappedException(h3, r3.ex); |
35394 | 2228 |
r1.assertInvoked(); |
2229 |
r2.assertInvoked(); |
|
2230 |
r3.assertInvoked(); |
|
2231 |
checkCompletedNormally(f, v1); |
|
2232 |
checkCompletedNormally(g, v2); |
|
2233 |
}} |
|
2234 |
||
2235 |
/** |
|
2236 |
* applyToEither result completes normally after normal completion |
|
2237 |
* of either source |
|
2238 |
*/ |
|
2239 |
public void testApplyToEither_normalCompletion() { |
|
2240 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2241 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2242 |
for (Integer v2 : new Integer[] { 2, null }) |
|
2243 |
{ |
|
2244 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2245 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2246 |
final IncFunction[] rs = new IncFunction[6]; |
|
2247 |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
|
2248 |
||
2249 |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
|
2250 |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
|
2251 |
checkIncomplete(h0); |
|
2252 |
checkIncomplete(h1); |
|
2253 |
rs[0].assertNotInvoked(); |
|
2254 |
rs[1].assertNotInvoked(); |
|
2255 |
f.complete(v1); |
|
2256 |
checkCompletedNormally(h0, inc(v1)); |
|
2257 |
checkCompletedNormally(h1, inc(v1)); |
|
2258 |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
|
2259 |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
|
2260 |
checkCompletedNormally(h2, inc(v1)); |
|
2261 |
checkCompletedNormally(h3, inc(v1)); |
|
2262 |
g.complete(v2); |
|
2263 |
||
2264 |
// unspecified behavior - both source completions available |
|
2265 |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
|
2266 |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
|
2267 |
rs[4].assertValue(h4.join()); |
|
2268 |
rs[5].assertValue(h5.join()); |
|
2269 |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
|
2270 |
Objects.equals(inc(v2), h4.join())); |
|
2271 |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
|
2272 |
Objects.equals(inc(v2), h5.join())); |
|
2273 |
||
2274 |
checkCompletedNormally(f, v1); |
|
2275 |
checkCompletedNormally(g, v2); |
|
2276 |
checkCompletedNormally(h0, inc(v1)); |
|
2277 |
checkCompletedNormally(h1, inc(v1)); |
|
2278 |
checkCompletedNormally(h2, inc(v1)); |
|
2279 |
checkCompletedNormally(h3, inc(v1)); |
|
2280 |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
|
2281 |
}} |
|
2282 |
||
2283 |
/** |
|
2284 |
* applyToEither result completes exceptionally after exceptional |
|
2285 |
* completion of either source |
|
2286 |
*/ |
|
2287 |
public void testApplyToEither_exceptionalCompletion() { |
|
2288 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2289 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2290 |
{ |
|
2291 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2292 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2293 |
final CFException ex = new CFException(); |
|
2294 |
final IncFunction[] rs = new IncFunction[6]; |
|
2295 |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
|
2296 |
||
2297 |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
|
2298 |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
|
2299 |
checkIncomplete(h0); |
|
2300 |
checkIncomplete(h1); |
|
2301 |
rs[0].assertNotInvoked(); |
|
2302 |
rs[1].assertNotInvoked(); |
|
2303 |
f.completeExceptionally(ex); |
|
2304 |
checkCompletedWithWrappedException(h0, ex); |
|
2305 |
checkCompletedWithWrappedException(h1, ex); |
|
2306 |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
|
2307 |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
|
2308 |
checkCompletedWithWrappedException(h2, ex); |
|
2309 |
checkCompletedWithWrappedException(h3, ex); |
|
2310 |
g.complete(v1); |
|
2311 |
||
2312 |
// unspecified behavior - both source completions available |
|
2313 |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
|
2314 |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
|
2315 |
try { |
|
2316 |
assertEquals(inc(v1), h4.join()); |
|
2317 |
rs[4].assertValue(inc(v1)); |
|
2318 |
} catch (CompletionException ok) { |
|
2319 |
checkCompletedWithWrappedException(h4, ex); |
|
2320 |
rs[4].assertNotInvoked(); |
|
2321 |
} |
|
2322 |
try { |
|
2323 |
assertEquals(inc(v1), h5.join()); |
|
2324 |
rs[5].assertValue(inc(v1)); |
|
2325 |
} catch (CompletionException ok) { |
|
2326 |
checkCompletedWithWrappedException(h5, ex); |
|
2327 |
rs[5].assertNotInvoked(); |
|
2328 |
} |
|
2329 |
||
2330 |
checkCompletedExceptionally(f, ex); |
|
2331 |
checkCompletedNormally(g, v1); |
|
2332 |
checkCompletedWithWrappedException(h0, ex); |
|
2333 |
checkCompletedWithWrappedException(h1, ex); |
|
2334 |
checkCompletedWithWrappedException(h2, ex); |
|
2335 |
checkCompletedWithWrappedException(h3, ex); |
|
2336 |
checkCompletedWithWrappedException(h4, ex); |
|
2337 |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
|
2338 |
}} |
|
2339 |
||
2340 |
public void testApplyToEither_exceptionalCompletion2() { |
|
2341 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2342 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2343 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2344 |
{ |
|
2345 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2346 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2347 |
final CFException ex = new CFException(); |
|
2348 |
final IncFunction[] rs = new IncFunction[6]; |
|
2349 |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
|
2350 |
||
2351 |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
|
2352 |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
|
2353 |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
|
2354 |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
|
2355 |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
|
2356 |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
|
2357 |
||
2358 |
// unspecified behavior - both source completions available |
|
2359 |
try { |
|
2360 |
assertEquals(inc(v1), h0.join()); |
|
2361 |
rs[0].assertValue(inc(v1)); |
|
2362 |
} catch (CompletionException ok) { |
|
2363 |
checkCompletedWithWrappedException(h0, ex); |
|
2364 |
rs[0].assertNotInvoked(); |
|
2365 |
} |
|
2366 |
try { |
|
2367 |
assertEquals(inc(v1), h1.join()); |
|
2368 |
rs[1].assertValue(inc(v1)); |
|
2369 |
} catch (CompletionException ok) { |
|
2370 |
checkCompletedWithWrappedException(h1, ex); |
|
2371 |
rs[1].assertNotInvoked(); |
|
2372 |
} |
|
2373 |
try { |
|
2374 |
assertEquals(inc(v1), h2.join()); |
|
2375 |
rs[2].assertValue(inc(v1)); |
|
2376 |
} catch (CompletionException ok) { |
|
2377 |
checkCompletedWithWrappedException(h2, ex); |
|
2378 |
rs[2].assertNotInvoked(); |
|
2379 |
} |
|
2380 |
try { |
|
2381 |
assertEquals(inc(v1), h3.join()); |
|
2382 |
rs[3].assertValue(inc(v1)); |
|
2383 |
} catch (CompletionException ok) { |
|
2384 |
checkCompletedWithWrappedException(h3, ex); |
|
2385 |
rs[3].assertNotInvoked(); |
|
2386 |
} |
|
2387 |
||
2388 |
checkCompletedNormally(f, v1); |
|
2389 |
checkCompletedExceptionally(g, ex); |
|
2390 |
}} |
|
2391 |
||
2392 |
/** |
|
2393 |
* applyToEither result completes exceptionally if either source cancelled |
|
2394 |
*/ |
|
2395 |
public void testApplyToEither_sourceCancelled() { |
|
2396 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2397 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
2398 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2399 |
{ |
|
2400 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2401 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2402 |
final IncFunction[] rs = new IncFunction[6]; |
|
2403 |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
|
2404 |
||
2405 |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
|
2406 |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
|
2407 |
checkIncomplete(h0); |
|
2408 |
checkIncomplete(h1); |
|
2409 |
rs[0].assertNotInvoked(); |
|
2410 |
rs[1].assertNotInvoked(); |
|
2411 |
f.cancel(mayInterruptIfRunning); |
|
2412 |
checkCompletedWithWrappedCancellationException(h0); |
|
2413 |
checkCompletedWithWrappedCancellationException(h1); |
|
2414 |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
|
2415 |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
|
2416 |
checkCompletedWithWrappedCancellationException(h2); |
|
2417 |
checkCompletedWithWrappedCancellationException(h3); |
|
2418 |
g.complete(v1); |
|
2419 |
||
2420 |
// unspecified behavior - both source completions available |
|
2421 |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
|
2422 |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
|
2423 |
try { |
|
2424 |
assertEquals(inc(v1), h4.join()); |
|
2425 |
rs[4].assertValue(inc(v1)); |
|
2426 |
} catch (CompletionException ok) { |
|
2427 |
checkCompletedWithWrappedCancellationException(h4); |
|
2428 |
rs[4].assertNotInvoked(); |
|
2429 |
} |
|
2430 |
try { |
|
2431 |
assertEquals(inc(v1), h5.join()); |
|
2432 |
rs[5].assertValue(inc(v1)); |
|
2433 |
} catch (CompletionException ok) { |
|
2434 |
checkCompletedWithWrappedCancellationException(h5); |
|
2435 |
rs[5].assertNotInvoked(); |
|
2436 |
} |
|
2437 |
||
2438 |
checkCancelled(f); |
|
2439 |
checkCompletedNormally(g, v1); |
|
2440 |
checkCompletedWithWrappedCancellationException(h0); |
|
2441 |
checkCompletedWithWrappedCancellationException(h1); |
|
2442 |
checkCompletedWithWrappedCancellationException(h2); |
|
2443 |
checkCompletedWithWrappedCancellationException(h3); |
|
2444 |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
|
2445 |
}} |
|
2446 |
||
2447 |
public void testApplyToEither_sourceCancelled2() { |
|
2448 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2449 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
2450 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2451 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2452 |
{ |
|
2453 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2454 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2455 |
final IncFunction[] rs = new IncFunction[6]; |
|
2456 |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
|
2457 |
||
2458 |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
|
2459 |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
|
2460 |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
|
2461 |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
|
2462 |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
|
2463 |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
|
2464 |
||
2465 |
// unspecified behavior - both source completions available |
|
2466 |
try { |
|
2467 |
assertEquals(inc(v1), h0.join()); |
|
2468 |
rs[0].assertValue(inc(v1)); |
|
2469 |
} catch (CompletionException ok) { |
|
2470 |
checkCompletedWithWrappedCancellationException(h0); |
|
2471 |
rs[0].assertNotInvoked(); |
|
2472 |
} |
|
2473 |
try { |
|
2474 |
assertEquals(inc(v1), h1.join()); |
|
2475 |
rs[1].assertValue(inc(v1)); |
|
2476 |
} catch (CompletionException ok) { |
|
2477 |
checkCompletedWithWrappedCancellationException(h1); |
|
2478 |
rs[1].assertNotInvoked(); |
|
2479 |
} |
|
2480 |
try { |
|
2481 |
assertEquals(inc(v1), h2.join()); |
|
2482 |
rs[2].assertValue(inc(v1)); |
|
2483 |
} catch (CompletionException ok) { |
|
2484 |
checkCompletedWithWrappedCancellationException(h2); |
|
2485 |
rs[2].assertNotInvoked(); |
|
2486 |
} |
|
2487 |
try { |
|
2488 |
assertEquals(inc(v1), h3.join()); |
|
2489 |
rs[3].assertValue(inc(v1)); |
|
2490 |
} catch (CompletionException ok) { |
|
2491 |
checkCompletedWithWrappedCancellationException(h3); |
|
2492 |
rs[3].assertNotInvoked(); |
|
2493 |
} |
|
2494 |
||
2495 |
checkCompletedNormally(f, v1); |
|
2496 |
checkCancelled(g); |
|
2497 |
}} |
|
2498 |
||
2499 |
/** |
|
2500 |
* applyToEither result completes exceptionally if action does |
|
2501 |
*/ |
|
2502 |
public void testApplyToEither_actionFailed() { |
|
2503 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2504 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2505 |
for (Integer v2 : new Integer[] { 2, null }) |
|
2506 |
{ |
|
2507 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2508 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2509 |
final FailingFunction[] rs = new FailingFunction[6]; |
|
2510 |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
|
2511 |
||
2512 |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
|
2513 |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
|
2514 |
f.complete(v1); |
|
2515 |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
|
2516 |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2517 |
checkCompletedWithWrappedException(h0, rs[0].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2518 |
checkCompletedWithWrappedException(h1, rs[1].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2519 |
checkCompletedWithWrappedException(h2, rs[2].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2520 |
checkCompletedWithWrappedException(h3, rs[3].ex); |
35394 | 2521 |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2522 |
||
2523 |
g.complete(v2); |
|
2524 |
||
2525 |
// unspecified behavior - both source completions available |
|
2526 |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
|
2527 |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
|
2528 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2529 |
checkCompletedWithWrappedException(h4, rs[4].ex); |
35394 | 2530 |
assertTrue(Objects.equals(v1, rs[4].value) || |
2531 |
Objects.equals(v2, rs[4].value)); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2532 |
checkCompletedWithWrappedException(h5, rs[5].ex); |
35394 | 2533 |
assertTrue(Objects.equals(v1, rs[5].value) || |
2534 |
Objects.equals(v2, rs[5].value)); |
|
2535 |
||
2536 |
checkCompletedNormally(f, v1); |
|
2537 |
checkCompletedNormally(g, v2); |
|
2538 |
}} |
|
2539 |
||
2540 |
/** |
|
2541 |
* acceptEither result completes normally after normal completion |
|
2542 |
* of either source |
|
2543 |
*/ |
|
2544 |
public void testAcceptEither_normalCompletion() { |
|
2545 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2546 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2547 |
for (Integer v2 : new Integer[] { 2, null }) |
|
2548 |
{ |
|
2549 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2550 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2551 |
final NoopConsumer[] rs = new NoopConsumer[6]; |
|
2552 |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
|
2553 |
||
2554 |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
|
2555 |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
|
2556 |
checkIncomplete(h0); |
|
2557 |
checkIncomplete(h1); |
|
2558 |
rs[0].assertNotInvoked(); |
|
2559 |
rs[1].assertNotInvoked(); |
|
2560 |
f.complete(v1); |
|
2561 |
checkCompletedNormally(h0, null); |
|
2562 |
checkCompletedNormally(h1, null); |
|
2563 |
rs[0].assertValue(v1); |
|
2564 |
rs[1].assertValue(v1); |
|
2565 |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
|
2566 |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
|
2567 |
checkCompletedNormally(h2, null); |
|
2568 |
checkCompletedNormally(h3, null); |
|
2569 |
rs[2].assertValue(v1); |
|
2570 |
rs[3].assertValue(v1); |
|
2571 |
g.complete(v2); |
|
2572 |
||
2573 |
// unspecified behavior - both source completions available |
|
2574 |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
|
2575 |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
|
2576 |
checkCompletedNormally(h4, null); |
|
2577 |
checkCompletedNormally(h5, null); |
|
2578 |
assertTrue(Objects.equals(v1, rs[4].value) || |
|
2579 |
Objects.equals(v2, rs[4].value)); |
|
2580 |
assertTrue(Objects.equals(v1, rs[5].value) || |
|
2581 |
Objects.equals(v2, rs[5].value)); |
|
2582 |
||
2583 |
checkCompletedNormally(f, v1); |
|
2584 |
checkCompletedNormally(g, v2); |
|
2585 |
checkCompletedNormally(h0, null); |
|
2586 |
checkCompletedNormally(h1, null); |
|
2587 |
checkCompletedNormally(h2, null); |
|
2588 |
checkCompletedNormally(h3, null); |
|
2589 |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
|
2590 |
}} |
|
2591 |
||
2592 |
/** |
|
2593 |
* acceptEither result completes exceptionally after exceptional |
|
2594 |
* completion of either source |
|
2595 |
*/ |
|
2596 |
public void testAcceptEither_exceptionalCompletion() { |
|
2597 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2598 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2599 |
{ |
|
2600 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2601 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2602 |
final CFException ex = new CFException(); |
|
2603 |
final NoopConsumer[] rs = new NoopConsumer[6]; |
|
2604 |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
|
2605 |
||
2606 |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
|
2607 |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
|
2608 |
checkIncomplete(h0); |
|
2609 |
checkIncomplete(h1); |
|
2610 |
rs[0].assertNotInvoked(); |
|
2611 |
rs[1].assertNotInvoked(); |
|
2612 |
f.completeExceptionally(ex); |
|
2613 |
checkCompletedWithWrappedException(h0, ex); |
|
2614 |
checkCompletedWithWrappedException(h1, ex); |
|
2615 |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
|
2616 |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
|
2617 |
checkCompletedWithWrappedException(h2, ex); |
|
2618 |
checkCompletedWithWrappedException(h3, ex); |
|
2619 |
||
2620 |
g.complete(v1); |
|
2621 |
||
2622 |
// unspecified behavior - both source completions available |
|
2623 |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
|
2624 |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
|
2625 |
try { |
|
2626 |
assertNull(h4.join()); |
|
2627 |
rs[4].assertValue(v1); |
|
2628 |
} catch (CompletionException ok) { |
|
2629 |
checkCompletedWithWrappedException(h4, ex); |
|
2630 |
rs[4].assertNotInvoked(); |
|
2631 |
} |
|
2632 |
try { |
|
2633 |
assertNull(h5.join()); |
|
2634 |
rs[5].assertValue(v1); |
|
2635 |
} catch (CompletionException ok) { |
|
2636 |
checkCompletedWithWrappedException(h5, ex); |
|
2637 |
rs[5].assertNotInvoked(); |
|
2638 |
} |
|
2639 |
||
2640 |
checkCompletedExceptionally(f, ex); |
|
2641 |
checkCompletedNormally(g, v1); |
|
2642 |
checkCompletedWithWrappedException(h0, ex); |
|
2643 |
checkCompletedWithWrappedException(h1, ex); |
|
2644 |
checkCompletedWithWrappedException(h2, ex); |
|
2645 |
checkCompletedWithWrappedException(h3, ex); |
|
2646 |
checkCompletedWithWrappedException(h4, ex); |
|
2647 |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
|
2648 |
}} |
|
2649 |
||
2650 |
public void testAcceptEither_exceptionalCompletion2() { |
|
2651 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2652 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2653 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2654 |
{ |
|
2655 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2656 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2657 |
final CFException ex = new CFException(); |
|
2658 |
final NoopConsumer[] rs = new NoopConsumer[6]; |
|
2659 |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
|
2660 |
||
2661 |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
|
2662 |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
|
2663 |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
|
2664 |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
|
2665 |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
|
2666 |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
|
2667 |
||
2668 |
// unspecified behavior - both source completions available |
|
2669 |
try { |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
2670 |
assertNull(h0.join()); |
35394 | 2671 |
rs[0].assertValue(v1); |
2672 |
} catch (CompletionException ok) { |
|
2673 |
checkCompletedWithWrappedException(h0, ex); |
|
2674 |
rs[0].assertNotInvoked(); |
|
2675 |
} |
|
2676 |
try { |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
2677 |
assertNull(h1.join()); |
35394 | 2678 |
rs[1].assertValue(v1); |
2679 |
} catch (CompletionException ok) { |
|
2680 |
checkCompletedWithWrappedException(h1, ex); |
|
2681 |
rs[1].assertNotInvoked(); |
|
2682 |
} |
|
2683 |
try { |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
2684 |
assertNull(h2.join()); |
35394 | 2685 |
rs[2].assertValue(v1); |
2686 |
} catch (CompletionException ok) { |
|
2687 |
checkCompletedWithWrappedException(h2, ex); |
|
2688 |
rs[2].assertNotInvoked(); |
|
2689 |
} |
|
2690 |
try { |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
2691 |
assertNull(h3.join()); |
35394 | 2692 |
rs[3].assertValue(v1); |
2693 |
} catch (CompletionException ok) { |
|
2694 |
checkCompletedWithWrappedException(h3, ex); |
|
2695 |
rs[3].assertNotInvoked(); |
|
2696 |
} |
|
2697 |
||
2698 |
checkCompletedNormally(f, v1); |
|
2699 |
checkCompletedExceptionally(g, ex); |
|
2700 |
}} |
|
2701 |
||
2702 |
/** |
|
2703 |
* acceptEither result completes exceptionally if either source cancelled |
|
2704 |
*/ |
|
2705 |
public void testAcceptEither_sourceCancelled() { |
|
2706 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2707 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
2708 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2709 |
{ |
|
2710 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2711 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2712 |
final NoopConsumer[] rs = new NoopConsumer[6]; |
|
2713 |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
|
2714 |
||
2715 |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
|
2716 |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
|
2717 |
checkIncomplete(h0); |
|
2718 |
checkIncomplete(h1); |
|
2719 |
rs[0].assertNotInvoked(); |
|
2720 |
rs[1].assertNotInvoked(); |
|
2721 |
f.cancel(mayInterruptIfRunning); |
|
2722 |
checkCompletedWithWrappedCancellationException(h0); |
|
2723 |
checkCompletedWithWrappedCancellationException(h1); |
|
2724 |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
|
2725 |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
|
2726 |
checkCompletedWithWrappedCancellationException(h2); |
|
2727 |
checkCompletedWithWrappedCancellationException(h3); |
|
2728 |
||
2729 |
g.complete(v1); |
|
2730 |
||
2731 |
// unspecified behavior - both source completions available |
|
2732 |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
|
2733 |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
|
2734 |
try { |
|
2735 |
assertNull(h4.join()); |
|
2736 |
rs[4].assertValue(v1); |
|
2737 |
} catch (CompletionException ok) { |
|
2738 |
checkCompletedWithWrappedCancellationException(h4); |
|
2739 |
rs[4].assertNotInvoked(); |
|
2740 |
} |
|
2741 |
try { |
|
2742 |
assertNull(h5.join()); |
|
2743 |
rs[5].assertValue(v1); |
|
2744 |
} catch (CompletionException ok) { |
|
2745 |
checkCompletedWithWrappedCancellationException(h5); |
|
2746 |
rs[5].assertNotInvoked(); |
|
2747 |
} |
|
2748 |
||
2749 |
checkCancelled(f); |
|
2750 |
checkCompletedNormally(g, v1); |
|
2751 |
checkCompletedWithWrappedCancellationException(h0); |
|
2752 |
checkCompletedWithWrappedCancellationException(h1); |
|
2753 |
checkCompletedWithWrappedCancellationException(h2); |
|
2754 |
checkCompletedWithWrappedCancellationException(h3); |
|
2755 |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
|
2756 |
}} |
|
2757 |
||
2758 |
/** |
|
2759 |
* acceptEither result completes exceptionally if action does |
|
2760 |
*/ |
|
2761 |
public void testAcceptEither_actionFailed() { |
|
2762 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2763 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2764 |
for (Integer v2 : new Integer[] { 2, null }) |
|
2765 |
{ |
|
2766 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2767 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2768 |
final FailingConsumer[] rs = new FailingConsumer[6]; |
|
2769 |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
|
2770 |
||
2771 |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
|
2772 |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
|
2773 |
f.complete(v1); |
|
2774 |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
|
2775 |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2776 |
checkCompletedWithWrappedException(h0, rs[0].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2777 |
checkCompletedWithWrappedException(h1, rs[1].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2778 |
checkCompletedWithWrappedException(h2, rs[2].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2779 |
checkCompletedWithWrappedException(h3, rs[3].ex); |
35394 | 2780 |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2781 |
||
2782 |
g.complete(v2); |
|
2783 |
||
2784 |
// unspecified behavior - both source completions available |
|
2785 |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
|
2786 |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
|
2787 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2788 |
checkCompletedWithWrappedException(h4, rs[4].ex); |
35394 | 2789 |
assertTrue(Objects.equals(v1, rs[4].value) || |
2790 |
Objects.equals(v2, rs[4].value)); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2791 |
checkCompletedWithWrappedException(h5, rs[5].ex); |
35394 | 2792 |
assertTrue(Objects.equals(v1, rs[5].value) || |
2793 |
Objects.equals(v2, rs[5].value)); |
|
2794 |
||
2795 |
checkCompletedNormally(f, v1); |
|
2796 |
checkCompletedNormally(g, v2); |
|
2797 |
}} |
|
2798 |
||
2799 |
/** |
|
2800 |
* runAfterEither result completes normally after normal completion |
|
2801 |
* of either source |
|
2802 |
*/ |
|
2803 |
public void testRunAfterEither_normalCompletion() { |
|
2804 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2805 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2806 |
for (Integer v2 : new Integer[] { 2, null }) |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2807 |
for (boolean pushNop : new boolean[] { true, false }) |
35394 | 2808 |
{ |
2809 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2810 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2811 |
final Noop[] rs = new Noop[6]; |
|
2812 |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
|
2813 |
||
2814 |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
|
2815 |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
|
2816 |
checkIncomplete(h0); |
|
2817 |
checkIncomplete(h1); |
|
2818 |
rs[0].assertNotInvoked(); |
|
2819 |
rs[1].assertNotInvoked(); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2820 |
if (pushNop) { // ad hoc test of intra-completion interference |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2821 |
m.thenRun(f, () -> {}); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2822 |
m.thenRun(g, () -> {}); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
2823 |
} |
35394 | 2824 |
f.complete(v1); |
2825 |
checkCompletedNormally(h0, null); |
|
2826 |
checkCompletedNormally(h1, null); |
|
2827 |
rs[0].assertInvoked(); |
|
2828 |
rs[1].assertInvoked(); |
|
2829 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
|
2830 |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
|
2831 |
checkCompletedNormally(h2, null); |
|
2832 |
checkCompletedNormally(h3, null); |
|
2833 |
rs[2].assertInvoked(); |
|
2834 |
rs[3].assertInvoked(); |
|
2835 |
||
2836 |
g.complete(v2); |
|
2837 |
||
2838 |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
|
2839 |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
|
2840 |
||
2841 |
checkCompletedNormally(f, v1); |
|
2842 |
checkCompletedNormally(g, v2); |
|
2843 |
checkCompletedNormally(h0, null); |
|
2844 |
checkCompletedNormally(h1, null); |
|
2845 |
checkCompletedNormally(h2, null); |
|
2846 |
checkCompletedNormally(h3, null); |
|
2847 |
checkCompletedNormally(h4, null); |
|
2848 |
checkCompletedNormally(h5, null); |
|
2849 |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
|
2850 |
}} |
|
2851 |
||
2852 |
/** |
|
2853 |
* runAfterEither result completes exceptionally after exceptional |
|
2854 |
* completion of either source |
|
2855 |
*/ |
|
2856 |
public void testRunAfterEither_exceptionalCompletion() { |
|
2857 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2858 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2859 |
{ |
|
2860 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2861 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2862 |
final CFException ex = new CFException(); |
|
2863 |
final Noop[] rs = new Noop[6]; |
|
2864 |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
|
2865 |
||
2866 |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
|
2867 |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
|
2868 |
checkIncomplete(h0); |
|
2869 |
checkIncomplete(h1); |
|
2870 |
rs[0].assertNotInvoked(); |
|
2871 |
rs[1].assertNotInvoked(); |
|
2872 |
assertTrue(f.completeExceptionally(ex)); |
|
2873 |
checkCompletedWithWrappedException(h0, ex); |
|
2874 |
checkCompletedWithWrappedException(h1, ex); |
|
2875 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
|
2876 |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
|
2877 |
checkCompletedWithWrappedException(h2, ex); |
|
2878 |
checkCompletedWithWrappedException(h3, ex); |
|
2879 |
||
2880 |
assertTrue(g.complete(v1)); |
|
2881 |
||
2882 |
// unspecified behavior - both source completions available |
|
2883 |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
|
2884 |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
|
2885 |
try { |
|
2886 |
assertNull(h4.join()); |
|
2887 |
rs[4].assertInvoked(); |
|
2888 |
} catch (CompletionException ok) { |
|
2889 |
checkCompletedWithWrappedException(h4, ex); |
|
2890 |
rs[4].assertNotInvoked(); |
|
2891 |
} |
|
2892 |
try { |
|
2893 |
assertNull(h5.join()); |
|
2894 |
rs[5].assertInvoked(); |
|
2895 |
} catch (CompletionException ok) { |
|
2896 |
checkCompletedWithWrappedException(h5, ex); |
|
2897 |
rs[5].assertNotInvoked(); |
|
2898 |
} |
|
2899 |
||
2900 |
checkCompletedExceptionally(f, ex); |
|
2901 |
checkCompletedNormally(g, v1); |
|
2902 |
checkCompletedWithWrappedException(h0, ex); |
|
2903 |
checkCompletedWithWrappedException(h1, ex); |
|
2904 |
checkCompletedWithWrappedException(h2, ex); |
|
2905 |
checkCompletedWithWrappedException(h3, ex); |
|
2906 |
checkCompletedWithWrappedException(h4, ex); |
|
2907 |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
|
2908 |
}} |
|
2909 |
||
2910 |
public void testRunAfterEither_exceptionalCompletion2() { |
|
2911 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2912 |
for (boolean fFirst : new boolean[] { true, false }) |
|
2913 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2914 |
{ |
|
2915 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2916 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2917 |
final CFException ex = new CFException(); |
|
2918 |
final Noop[] rs = new Noop[6]; |
|
2919 |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
|
2920 |
||
2921 |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
|
2922 |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
|
2923 |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
|
2924 |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
|
2925 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
|
2926 |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
|
2927 |
||
2928 |
// unspecified behavior - both source completions available |
|
2929 |
try { |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
2930 |
assertNull(h0.join()); |
35394 | 2931 |
rs[0].assertInvoked(); |
2932 |
} catch (CompletionException ok) { |
|
2933 |
checkCompletedWithWrappedException(h0, ex); |
|
2934 |
rs[0].assertNotInvoked(); |
|
2935 |
} |
|
2936 |
try { |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
2937 |
assertNull(h1.join()); |
35394 | 2938 |
rs[1].assertInvoked(); |
2939 |
} catch (CompletionException ok) { |
|
2940 |
checkCompletedWithWrappedException(h1, ex); |
|
2941 |
rs[1].assertNotInvoked(); |
|
2942 |
} |
|
2943 |
try { |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
2944 |
assertNull(h2.join()); |
35394 | 2945 |
rs[2].assertInvoked(); |
2946 |
} catch (CompletionException ok) { |
|
2947 |
checkCompletedWithWrappedException(h2, ex); |
|
2948 |
rs[2].assertNotInvoked(); |
|
2949 |
} |
|
2950 |
try { |
|
44589
64d9270bd24c
8176543: Miscellaneous changes imported from jsr166 CVS 2017-04
dl
parents:
43522
diff
changeset
|
2951 |
assertNull(h3.join()); |
35394 | 2952 |
rs[3].assertInvoked(); |
2953 |
} catch (CompletionException ok) { |
|
2954 |
checkCompletedWithWrappedException(h3, ex); |
|
2955 |
rs[3].assertNotInvoked(); |
|
2956 |
} |
|
2957 |
||
2958 |
checkCompletedNormally(f, v1); |
|
2959 |
checkCompletedExceptionally(g, ex); |
|
2960 |
}} |
|
2961 |
||
2962 |
/** |
|
2963 |
* runAfterEither result completes exceptionally if either source cancelled |
|
2964 |
*/ |
|
2965 |
public void testRunAfterEither_sourceCancelled() { |
|
2966 |
for (ExecutionMode m : ExecutionMode.values()) |
|
2967 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
2968 |
for (Integer v1 : new Integer[] { 1, null }) |
|
2969 |
{ |
|
2970 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
2971 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
2972 |
final Noop[] rs = new Noop[6]; |
|
2973 |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
|
2974 |
||
2975 |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
|
2976 |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
|
2977 |
checkIncomplete(h0); |
|
2978 |
checkIncomplete(h1); |
|
2979 |
rs[0].assertNotInvoked(); |
|
2980 |
rs[1].assertNotInvoked(); |
|
2981 |
f.cancel(mayInterruptIfRunning); |
|
2982 |
checkCompletedWithWrappedCancellationException(h0); |
|
2983 |
checkCompletedWithWrappedCancellationException(h1); |
|
2984 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
|
2985 |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
|
2986 |
checkCompletedWithWrappedCancellationException(h2); |
|
2987 |
checkCompletedWithWrappedCancellationException(h3); |
|
2988 |
||
2989 |
assertTrue(g.complete(v1)); |
|
2990 |
||
2991 |
// unspecified behavior - both source completions available |
|
2992 |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
|
2993 |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
|
2994 |
try { |
|
2995 |
assertNull(h4.join()); |
|
2996 |
rs[4].assertInvoked(); |
|
2997 |
} catch (CompletionException ok) { |
|
2998 |
checkCompletedWithWrappedCancellationException(h4); |
|
2999 |
rs[4].assertNotInvoked(); |
|
3000 |
} |
|
3001 |
try { |
|
3002 |
assertNull(h5.join()); |
|
3003 |
rs[5].assertInvoked(); |
|
3004 |
} catch (CompletionException ok) { |
|
3005 |
checkCompletedWithWrappedCancellationException(h5); |
|
3006 |
rs[5].assertNotInvoked(); |
|
3007 |
} |
|
3008 |
||
3009 |
checkCancelled(f); |
|
3010 |
checkCompletedNormally(g, v1); |
|
3011 |
checkCompletedWithWrappedCancellationException(h0); |
|
3012 |
checkCompletedWithWrappedCancellationException(h1); |
|
3013 |
checkCompletedWithWrappedCancellationException(h2); |
|
3014 |
checkCompletedWithWrappedCancellationException(h3); |
|
3015 |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
|
3016 |
}} |
|
3017 |
||
3018 |
/** |
|
3019 |
* runAfterEither result completes exceptionally if action does |
|
3020 |
*/ |
|
3021 |
public void testRunAfterEither_actionFailed() { |
|
3022 |
for (ExecutionMode m : ExecutionMode.values()) |
|
3023 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3024 |
for (Integer v2 : new Integer[] { 2, null }) |
|
3025 |
{ |
|
3026 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3027 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
3028 |
final FailingRunnable[] rs = new FailingRunnable[6]; |
|
3029 |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
|
3030 |
||
3031 |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
|
3032 |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
|
3033 |
assertTrue(f.complete(v1)); |
|
3034 |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
|
3035 |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3036 |
checkCompletedWithWrappedException(h0, rs[0].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3037 |
checkCompletedWithWrappedException(h1, rs[1].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3038 |
checkCompletedWithWrappedException(h2, rs[2].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3039 |
checkCompletedWithWrappedException(h3, rs[3].ex); |
35394 | 3040 |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
3041 |
assertTrue(g.complete(v2)); |
|
3042 |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
|
3043 |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3044 |
checkCompletedWithWrappedException(h4, rs[4].ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3045 |
checkCompletedWithWrappedException(h5, rs[5].ex); |
35394 | 3046 |
|
3047 |
checkCompletedNormally(f, v1); |
|
3048 |
checkCompletedNormally(g, v2); |
|
3049 |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
|
3050 |
}} |
|
3051 |
||
3052 |
/** |
|
3053 |
* thenCompose result completes normally after normal completion of source |
|
3054 |
*/ |
|
3055 |
public void testThenCompose_normalCompletion() { |
|
3056 |
for (ExecutionMode m : ExecutionMode.values()) |
|
3057 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
3058 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3059 |
{ |
|
3060 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3061 |
final CompletableFutureInc r = new CompletableFutureInc(m); |
|
3062 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
|
3063 |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
|
3064 |
if (createIncomplete) assertTrue(f.complete(v1)); |
|
3065 |
||
3066 |
checkCompletedNormally(g, inc(v1)); |
|
3067 |
checkCompletedNormally(f, v1); |
|
3068 |
r.assertValue(v1); |
|
3069 |
}} |
|
3070 |
||
3071 |
/** |
|
3072 |
* thenCompose result completes exceptionally after exceptional |
|
3073 |
* completion of source |
|
3074 |
*/ |
|
3075 |
public void testThenCompose_exceptionalCompletion() { |
|
3076 |
for (ExecutionMode m : ExecutionMode.values()) |
|
3077 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
3078 |
{ |
|
3079 |
final CFException ex = new CFException(); |
|
3080 |
final CompletableFutureInc r = new CompletableFutureInc(m); |
|
3081 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3082 |
if (!createIncomplete) f.completeExceptionally(ex); |
|
3083 |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
|
3084 |
if (createIncomplete) f.completeExceptionally(ex); |
|
3085 |
||
3086 |
checkCompletedWithWrappedException(g, ex); |
|
3087 |
checkCompletedExceptionally(f, ex); |
|
3088 |
r.assertNotInvoked(); |
|
3089 |
}} |
|
3090 |
||
3091 |
/** |
|
3092 |
* thenCompose result completes exceptionally if action does |
|
3093 |
*/ |
|
3094 |
public void testThenCompose_actionFailed() { |
|
3095 |
for (ExecutionMode m : ExecutionMode.values()) |
|
3096 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
3097 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3098 |
{ |
|
3099 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3100 |
final FailingCompletableFutureFunction r |
|
3101 |
= new FailingCompletableFutureFunction(m); |
|
3102 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
|
3103 |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
|
3104 |
if (createIncomplete) assertTrue(f.complete(v1)); |
|
3105 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3106 |
checkCompletedWithWrappedException(g, r.ex); |
35394 | 3107 |
checkCompletedNormally(f, v1); |
3108 |
}} |
|
3109 |
||
3110 |
/** |
|
3111 |
* thenCompose result completes exceptionally if source cancelled |
|
3112 |
*/ |
|
3113 |
public void testThenCompose_sourceCancelled() { |
|
3114 |
for (ExecutionMode m : ExecutionMode.values()) |
|
3115 |
for (boolean createIncomplete : new boolean[] { true, false }) |
|
3116 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
3117 |
{ |
|
3118 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3119 |
final CompletableFutureInc r = new CompletableFutureInc(m); |
|
3120 |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
|
3121 |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
|
3122 |
if (createIncomplete) { |
|
3123 |
checkIncomplete(g); |
|
3124 |
assertTrue(f.cancel(mayInterruptIfRunning)); |
|
3125 |
} |
|
3126 |
||
3127 |
checkCompletedWithWrappedCancellationException(g); |
|
3128 |
checkCancelled(f); |
|
3129 |
}} |
|
3130 |
||
3131 |
/** |
|
3132 |
* thenCompose result completes exceptionally if the result of the action does |
|
3133 |
*/ |
|
3134 |
public void testThenCompose_actionReturnsFailingFuture() { |
|
3135 |
for (ExecutionMode m : ExecutionMode.values()) |
|
3136 |
for (int order = 0; order < 6; order++) |
|
3137 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3138 |
{ |
|
3139 |
final CFException ex = new CFException(); |
|
3140 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3141 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
3142 |
final CompletableFuture<Integer> h; |
|
3143 |
// Test all permutations of orders |
|
3144 |
switch (order) { |
|
3145 |
case 0: |
|
3146 |
assertTrue(f.complete(v1)); |
|
3147 |
assertTrue(g.completeExceptionally(ex)); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3148 |
h = m.thenCompose(f, x -> g); |
35394 | 3149 |
break; |
3150 |
case 1: |
|
3151 |
assertTrue(f.complete(v1)); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3152 |
h = m.thenCompose(f, x -> g); |
35394 | 3153 |
assertTrue(g.completeExceptionally(ex)); |
3154 |
break; |
|
3155 |
case 2: |
|
3156 |
assertTrue(g.completeExceptionally(ex)); |
|
3157 |
assertTrue(f.complete(v1)); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3158 |
h = m.thenCompose(f, x -> g); |
35394 | 3159 |
break; |
3160 |
case 3: |
|
3161 |
assertTrue(g.completeExceptionally(ex)); |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3162 |
h = m.thenCompose(f, x -> g); |
35394 | 3163 |
assertTrue(f.complete(v1)); |
3164 |
break; |
|
3165 |
case 4: |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3166 |
h = m.thenCompose(f, x -> g); |
35394 | 3167 |
assertTrue(f.complete(v1)); |
3168 |
assertTrue(g.completeExceptionally(ex)); |
|
3169 |
break; |
|
3170 |
case 5: |
|
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3171 |
h = m.thenCompose(f, x -> g); |
35394 | 3172 |
assertTrue(f.complete(v1)); |
3173 |
assertTrue(g.completeExceptionally(ex)); |
|
3174 |
break; |
|
3175 |
default: throw new AssertionError(); |
|
3176 |
} |
|
3177 |
||
3178 |
checkCompletedExceptionally(g, ex); |
|
3179 |
checkCompletedWithWrappedException(h, ex); |
|
3180 |
checkCompletedNormally(f, v1); |
|
3181 |
}} |
|
3182 |
||
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3183 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3184 |
* exceptionallyCompose result completes normally after normal |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3185 |
* completion of source |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3186 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3187 |
public void testExceptionallyCompose_normalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3188 |
for (ExecutionMode m : ExecutionMode.values()) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3189 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3190 |
for (Integer v1 : new Integer[] { 1, null }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3191 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3192 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3193 |
final ExceptionalCompletableFutureFunction r = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3194 |
new ExceptionalCompletableFutureFunction(m); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3195 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3196 |
final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3197 |
if (createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3198 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3199 |
checkCompletedNormally(f, v1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3200 |
checkCompletedNormally(g, v1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3201 |
r.assertNotInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3202 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3203 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3204 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3205 |
* exceptionallyCompose result completes normally after exceptional |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3206 |
* completion of source |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3207 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3208 |
public void testExceptionallyCompose_exceptionalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3209 |
for (ExecutionMode m : ExecutionMode.values()) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3210 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3211 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3212 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3213 |
final ExceptionalCompletableFutureFunction r = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3214 |
new ExceptionalCompletableFutureFunction(m); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3215 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3216 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3217 |
final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3218 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3219 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3220 |
checkCompletedExceptionally(f, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3221 |
checkCompletedNormally(g, r.value); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3222 |
r.assertInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3223 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3224 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3225 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3226 |
* exceptionallyCompose completes exceptionally on exception if action does |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3227 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3228 |
public void testExceptionallyCompose_actionFailed() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3229 |
for (ExecutionMode m : ExecutionMode.values()) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3230 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3231 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3232 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3233 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3234 |
final FailingExceptionalCompletableFutureFunction r |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3235 |
= new FailingExceptionalCompletableFutureFunction(m); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3236 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3237 |
final CompletableFuture<Integer> g = m.exceptionallyCompose(f, r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3238 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3239 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3240 |
checkCompletedExceptionally(f, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3241 |
checkCompletedWithWrappedException(g, r.ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3242 |
r.assertInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3243 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3244 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3245 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3246 |
* exceptionallyCompose result completes exceptionally if the |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3247 |
* result of the action does |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3248 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3249 |
public void testExceptionallyCompose_actionReturnsFailingFuture() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3250 |
for (ExecutionMode m : ExecutionMode.values()) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3251 |
for (int order = 0; order < 6; order++) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3252 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3253 |
final CFException ex0 = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3254 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3255 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3256 |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3257 |
final CompletableFuture<Integer> h; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3258 |
// Test all permutations of orders |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3259 |
switch (order) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3260 |
case 0: |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3261 |
assertTrue(f.completeExceptionally(ex0)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3262 |
assertTrue(g.completeExceptionally(ex)); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3263 |
h = m.exceptionallyCompose(f, x -> g); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3264 |
break; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3265 |
case 1: |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3266 |
assertTrue(f.completeExceptionally(ex0)); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3267 |
h = m.exceptionallyCompose(f, x -> g); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3268 |
assertTrue(g.completeExceptionally(ex)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3269 |
break; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3270 |
case 2: |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3271 |
assertTrue(g.completeExceptionally(ex)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3272 |
assertTrue(f.completeExceptionally(ex0)); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3273 |
h = m.exceptionallyCompose(f, x -> g); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3274 |
break; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3275 |
case 3: |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3276 |
assertTrue(g.completeExceptionally(ex)); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3277 |
h = m.exceptionallyCompose(f, x -> g); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3278 |
assertTrue(f.completeExceptionally(ex0)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3279 |
break; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3280 |
case 4: |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3281 |
h = m.exceptionallyCompose(f, x -> g); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3282 |
assertTrue(f.completeExceptionally(ex0)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3283 |
assertTrue(g.completeExceptionally(ex)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3284 |
break; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3285 |
case 5: |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
3286 |
h = m.exceptionallyCompose(f, x -> g); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3287 |
assertTrue(f.completeExceptionally(ex0)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3288 |
assertTrue(g.completeExceptionally(ex)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3289 |
break; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3290 |
default: throw new AssertionError(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3291 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3292 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3293 |
checkCompletedExceptionally(g, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3294 |
checkCompletedWithWrappedException(h, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3295 |
checkCompletedExceptionally(f, ex0); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3296 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
3297 |
|
35394 | 3298 |
// other static methods |
3299 |
||
3300 |
/** |
|
3301 |
* allOf(no component futures) returns a future completed normally |
|
3302 |
* with the value null |
|
3303 |
*/ |
|
3304 |
public void testAllOf_empty() throws Exception { |
|
3305 |
CompletableFuture<Void> f = CompletableFuture.allOf(); |
|
3306 |
checkCompletedNormally(f, null); |
|
3307 |
} |
|
3308 |
||
3309 |
/** |
|
3310 |
* allOf returns a future completed normally with the value null |
|
3311 |
* when all components complete normally |
|
3312 |
*/ |
|
3313 |
public void testAllOf_normal() throws Exception { |
|
3314 |
for (int k = 1; k < 10; k++) { |
|
3315 |
CompletableFuture<Integer>[] fs |
|
3316 |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
|
3317 |
for (int i = 0; i < k; i++) |
|
3318 |
fs[i] = new CompletableFuture<>(); |
|
3319 |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
|
3320 |
for (int i = 0; i < k; i++) { |
|
3321 |
checkIncomplete(f); |
|
3322 |
checkIncomplete(CompletableFuture.allOf(fs)); |
|
3323 |
fs[i].complete(one); |
|
3324 |
} |
|
3325 |
checkCompletedNormally(f, null); |
|
3326 |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
|
3327 |
} |
|
3328 |
} |
|
3329 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3330 |
public void testAllOf_normal_backwards() throws Exception { |
35394 | 3331 |
for (int k = 1; k < 10; k++) { |
3332 |
CompletableFuture<Integer>[] fs |
|
3333 |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
|
3334 |
for (int i = 0; i < k; i++) |
|
3335 |
fs[i] = new CompletableFuture<>(); |
|
3336 |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
|
3337 |
for (int i = k - 1; i >= 0; i--) { |
|
3338 |
checkIncomplete(f); |
|
3339 |
checkIncomplete(CompletableFuture.allOf(fs)); |
|
3340 |
fs[i].complete(one); |
|
3341 |
} |
|
3342 |
checkCompletedNormally(f, null); |
|
3343 |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
|
3344 |
} |
|
3345 |
} |
|
3346 |
||
3347 |
public void testAllOf_exceptional() throws Exception { |
|
3348 |
for (int k = 1; k < 10; k++) { |
|
3349 |
CompletableFuture<Integer>[] fs |
|
3350 |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
|
3351 |
CFException ex = new CFException(); |
|
3352 |
for (int i = 0; i < k; i++) |
|
3353 |
fs[i] = new CompletableFuture<>(); |
|
3354 |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
|
3355 |
for (int i = 0; i < k; i++) { |
|
3356 |
checkIncomplete(f); |
|
3357 |
checkIncomplete(CompletableFuture.allOf(fs)); |
|
3358 |
if (i != k / 2) { |
|
3359 |
fs[i].complete(i); |
|
3360 |
checkCompletedNormally(fs[i], i); |
|
3361 |
} else { |
|
3362 |
fs[i].completeExceptionally(ex); |
|
3363 |
checkCompletedExceptionally(fs[i], ex); |
|
3364 |
} |
|
3365 |
} |
|
3366 |
checkCompletedWithWrappedException(f, ex); |
|
3367 |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
|
3368 |
} |
|
3369 |
} |
|
3370 |
||
3371 |
/** |
|
3372 |
* anyOf(no component futures) returns an incomplete future |
|
3373 |
*/ |
|
3374 |
public void testAnyOf_empty() throws Exception { |
|
3375 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3376 |
{ |
|
3377 |
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
|
3378 |
checkIncomplete(f); |
|
3379 |
||
3380 |
f.complete(v1); |
|
3381 |
checkCompletedNormally(f, v1); |
|
3382 |
}} |
|
3383 |
||
3384 |
/** |
|
3385 |
* anyOf returns a future completed normally with a value when |
|
3386 |
* a component future does |
|
3387 |
*/ |
|
3388 |
public void testAnyOf_normal() throws Exception { |
|
3389 |
for (int k = 0; k < 10; k++) { |
|
3390 |
CompletableFuture[] fs = new CompletableFuture[k]; |
|
3391 |
for (int i = 0; i < k; i++) |
|
3392 |
fs[i] = new CompletableFuture<>(); |
|
3393 |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
|
3394 |
checkIncomplete(f); |
|
3395 |
for (int i = 0; i < k; i++) { |
|
3396 |
fs[i].complete(i); |
|
3397 |
checkCompletedNormally(f, 0); |
|
3398 |
int x = (int) CompletableFuture.anyOf(fs).join(); |
|
3399 |
assertTrue(0 <= x && x <= i); |
|
3400 |
} |
|
3401 |
} |
|
3402 |
} |
|
3403 |
public void testAnyOf_normal_backwards() throws Exception { |
|
3404 |
for (int k = 0; k < 10; k++) { |
|
3405 |
CompletableFuture[] fs = new CompletableFuture[k]; |
|
3406 |
for (int i = 0; i < k; i++) |
|
3407 |
fs[i] = new CompletableFuture<>(); |
|
3408 |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
|
3409 |
checkIncomplete(f); |
|
3410 |
for (int i = k - 1; i >= 0; i--) { |
|
3411 |
fs[i].complete(i); |
|
3412 |
checkCompletedNormally(f, k - 1); |
|
3413 |
int x = (int) CompletableFuture.anyOf(fs).join(); |
|
3414 |
assertTrue(i <= x && x <= k - 1); |
|
3415 |
} |
|
3416 |
} |
|
3417 |
} |
|
3418 |
||
3419 |
/** |
|
3420 |
* anyOf result completes exceptionally when any component does. |
|
3421 |
*/ |
|
3422 |
public void testAnyOf_exceptional() throws Exception { |
|
3423 |
for (int k = 0; k < 10; k++) { |
|
3424 |
CompletableFuture[] fs = new CompletableFuture[k]; |
|
3425 |
CFException[] exs = new CFException[k]; |
|
3426 |
for (int i = 0; i < k; i++) { |
|
3427 |
fs[i] = new CompletableFuture<>(); |
|
3428 |
exs[i] = new CFException(); |
|
3429 |
} |
|
3430 |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
|
3431 |
checkIncomplete(f); |
|
3432 |
for (int i = 0; i < k; i++) { |
|
3433 |
fs[i].completeExceptionally(exs[i]); |
|
3434 |
checkCompletedWithWrappedException(f, exs[0]); |
|
3435 |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
|
3436 |
} |
|
3437 |
} |
|
3438 |
} |
|
3439 |
||
3440 |
public void testAnyOf_exceptional_backwards() throws Exception { |
|
3441 |
for (int k = 0; k < 10; k++) { |
|
3442 |
CompletableFuture[] fs = new CompletableFuture[k]; |
|
3443 |
CFException[] exs = new CFException[k]; |
|
3444 |
for (int i = 0; i < k; i++) { |
|
3445 |
fs[i] = new CompletableFuture<>(); |
|
3446 |
exs[i] = new CFException(); |
|
3447 |
} |
|
3448 |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
|
3449 |
checkIncomplete(f); |
|
3450 |
for (int i = k - 1; i >= 0; i--) { |
|
3451 |
fs[i].completeExceptionally(exs[i]); |
|
3452 |
checkCompletedWithWrappedException(f, exs[k - 1]); |
|
3453 |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
|
3454 |
} |
|
3455 |
} |
|
3456 |
} |
|
3457 |
||
3458 |
/** |
|
3459 |
* Completion methods throw NullPointerException with null arguments |
|
3460 |
*/ |
|
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
3461 |
@SuppressWarnings("FutureReturnValueIgnored") |
35394 | 3462 |
public void testNPE() { |
3463 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3464 |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
3465 |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
|
3466 |
ThreadExecutor exec = new ThreadExecutor(); |
|
3467 |
||
3468 |
Runnable[] throwingActions = { |
|
3469 |
() -> CompletableFuture.supplyAsync(null), |
|
3470 |
() -> CompletableFuture.supplyAsync(null, exec), |
|
3471 |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
|
3472 |
||
3473 |
() -> CompletableFuture.runAsync(null), |
|
3474 |
() -> CompletableFuture.runAsync(null, exec), |
|
3475 |
() -> CompletableFuture.runAsync(() -> {}, null), |
|
3476 |
||
3477 |
() -> f.completeExceptionally(null), |
|
3478 |
||
3479 |
() -> f.thenApply(null), |
|
3480 |
() -> f.thenApplyAsync(null), |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3481 |
() -> f.thenApplyAsync(x -> x, null), |
35394 | 3482 |
() -> f.thenApplyAsync(null, exec), |
3483 |
||
3484 |
() -> f.thenAccept(null), |
|
3485 |
() -> f.thenAcceptAsync(null), |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3486 |
() -> f.thenAcceptAsync(x -> {} , null), |
35394 | 3487 |
() -> f.thenAcceptAsync(null, exec), |
3488 |
||
3489 |
() -> f.thenRun(null), |
|
3490 |
() -> f.thenRunAsync(null), |
|
3491 |
() -> f.thenRunAsync(() -> {} , null), |
|
3492 |
() -> f.thenRunAsync(null, exec), |
|
3493 |
||
3494 |
() -> f.thenCombine(g, null), |
|
3495 |
() -> f.thenCombineAsync(g, null), |
|
3496 |
() -> f.thenCombineAsync(g, null, exec), |
|
3497 |
() -> f.thenCombine(nullFuture, (x, y) -> x), |
|
3498 |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x), |
|
3499 |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec), |
|
3500 |
() -> f.thenCombineAsync(g, (x, y) -> x, null), |
|
3501 |
||
3502 |
() -> f.thenAcceptBoth(g, null), |
|
3503 |
() -> f.thenAcceptBothAsync(g, null), |
|
3504 |
() -> f.thenAcceptBothAsync(g, null, exec), |
|
3505 |
() -> f.thenAcceptBoth(nullFuture, (x, y) -> {}), |
|
3506 |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}), |
|
3507 |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec), |
|
3508 |
() -> f.thenAcceptBothAsync(g, (x, y) -> {}, null), |
|
3509 |
||
3510 |
() -> f.runAfterBoth(g, null), |
|
3511 |
() -> f.runAfterBothAsync(g, null), |
|
3512 |
() -> f.runAfterBothAsync(g, null, exec), |
|
3513 |
() -> f.runAfterBoth(nullFuture, () -> {}), |
|
3514 |
() -> f.runAfterBothAsync(nullFuture, () -> {}), |
|
3515 |
() -> f.runAfterBothAsync(nullFuture, () -> {}, exec), |
|
3516 |
() -> f.runAfterBothAsync(g, () -> {}, null), |
|
3517 |
||
3518 |
() -> f.applyToEither(g, null), |
|
3519 |
() -> f.applyToEitherAsync(g, null), |
|
3520 |
() -> f.applyToEitherAsync(g, null, exec), |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3521 |
() -> f.applyToEither(nullFuture, x -> x), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3522 |
() -> f.applyToEitherAsync(nullFuture, x -> x), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3523 |
() -> f.applyToEitherAsync(nullFuture, x -> x, exec), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3524 |
() -> f.applyToEitherAsync(g, x -> x, null), |
35394 | 3525 |
|
3526 |
() -> f.acceptEither(g, null), |
|
3527 |
() -> f.acceptEitherAsync(g, null), |
|
3528 |
() -> f.acceptEitherAsync(g, null, exec), |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3529 |
() -> f.acceptEither(nullFuture, x -> {}), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3530 |
() -> f.acceptEitherAsync(nullFuture, x -> {}), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3531 |
() -> f.acceptEitherAsync(nullFuture, x -> {}, exec), |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3532 |
() -> f.acceptEitherAsync(g, x -> {}, null), |
35394 | 3533 |
|
3534 |
() -> f.runAfterEither(g, null), |
|
3535 |
() -> f.runAfterEitherAsync(g, null), |
|
3536 |
() -> f.runAfterEitherAsync(g, null, exec), |
|
3537 |
() -> f.runAfterEither(nullFuture, () -> {}), |
|
3538 |
() -> f.runAfterEitherAsync(nullFuture, () -> {}), |
|
3539 |
() -> f.runAfterEitherAsync(nullFuture, () -> {}, exec), |
|
3540 |
() -> f.runAfterEitherAsync(g, () -> {}, null), |
|
3541 |
||
3542 |
() -> f.thenCompose(null), |
|
3543 |
() -> f.thenComposeAsync(null), |
|
3544 |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
|
3545 |
() -> f.thenComposeAsync(null, exec), |
|
3546 |
||
3547 |
() -> f.exceptionally(null), |
|
3548 |
||
3549 |
() -> f.handle(null), |
|
3550 |
||
3551 |
() -> CompletableFuture.allOf((CompletableFuture<?>)null), |
|
3552 |
() -> CompletableFuture.allOf((CompletableFuture<?>[])null), |
|
3553 |
() -> CompletableFuture.allOf(f, null), |
|
3554 |
() -> CompletableFuture.allOf(null, f), |
|
3555 |
||
3556 |
() -> CompletableFuture.anyOf((CompletableFuture<?>)null), |
|
3557 |
() -> CompletableFuture.anyOf((CompletableFuture<?>[])null), |
|
3558 |
() -> CompletableFuture.anyOf(f, null), |
|
3559 |
() -> CompletableFuture.anyOf(null, f), |
|
3560 |
||
3561 |
() -> f.obtrudeException(null), |
|
3562 |
||
3563 |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
|
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
3564 |
() -> CompletableFuture.delayedExecutor(1L, null, exec), |
35394 | 3565 |
() -> CompletableFuture.delayedExecutor(1L, null), |
3566 |
||
3567 |
() -> f.orTimeout(1L, null), |
|
3568 |
() -> f.completeOnTimeout(42, 1L, null), |
|
3569 |
||
3570 |
() -> CompletableFuture.failedFuture(null), |
|
3571 |
() -> CompletableFuture.failedStage(null), |
|
3572 |
}; |
|
3573 |
||
3574 |
assertThrows(NullPointerException.class, throwingActions); |
|
3575 |
assertEquals(0, exec.count.get()); |
|
3576 |
} |
|
3577 |
||
3578 |
/** |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3579 |
* Test submissions to an executor that rejects all tasks. |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3580 |
*/ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3581 |
public void testRejectingExecutor() { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3582 |
for (Integer v : new Integer[] { 1, null }) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3583 |
{ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3584 |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3585 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3586 |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3587 |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3588 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3589 |
List<CompletableFuture<?>> futures = new ArrayList<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3590 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3591 |
List<CompletableFuture<Integer>> srcs = new ArrayList<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3592 |
srcs.add(complete); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3593 |
srcs.add(incomplete); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3594 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3595 |
for (CompletableFuture<Integer> src : srcs) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3596 |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3597 |
fs.add(src.thenRunAsync(() -> {}, e)); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3598 |
fs.add(src.thenAcceptAsync(z -> {}, e)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3599 |
fs.add(src.thenApplyAsync(z -> z, e)); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3600 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3601 |
fs.add(src.thenCombineAsync(src, (x, y) -> x, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3602 |
fs.add(src.thenAcceptBothAsync(src, (x, y) -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3603 |
fs.add(src.runAfterBothAsync(src, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3604 |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3605 |
fs.add(src.applyToEitherAsync(src, z -> z, e)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3606 |
fs.add(src.acceptEitherAsync(src, z -> {}, e)); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3607 |
fs.add(src.runAfterEitherAsync(src, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3608 |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3609 |
fs.add(src.thenComposeAsync(z -> null, e)); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3610 |
fs.add(src.whenCompleteAsync((z, t) -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3611 |
fs.add(src.handleAsync((z, t) -> null, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3612 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3613 |
for (CompletableFuture<?> future : fs) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3614 |
if (src.isDone()) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3615 |
checkCompletedWithWrappedException(future, e.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3616 |
else |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3617 |
checkIncomplete(future); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3618 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3619 |
futures.addAll(fs); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3620 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3621 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3622 |
{ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3623 |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3624 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3625 |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3626 |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3627 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3628 |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3629 |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3630 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3631 |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3632 |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3633 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3634 |
for (CompletableFuture<?> future : fs) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3635 |
checkIncomplete(future); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3636 |
futures.addAll(fs); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3637 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3638 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3639 |
{ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3640 |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3641 |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3642 |
fs.add(complete.applyToEitherAsync(incomplete, z -> z, e)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3643 |
fs.add(incomplete.applyToEitherAsync(complete, z -> z, e)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3644 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3645 |
fs.add(complete.acceptEitherAsync(incomplete, z -> {}, e)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3646 |
fs.add(incomplete.acceptEitherAsync(complete, z -> {}, e)); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3647 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3648 |
fs.add(complete.runAfterEitherAsync(incomplete, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3649 |
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3650 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3651 |
for (CompletableFuture<?> future : fs) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3652 |
checkCompletedWithWrappedException(future, e.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3653 |
futures.addAll(fs); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3654 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3655 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3656 |
incomplete.complete(v); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3657 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3658 |
for (CompletableFuture<?> future : futures) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3659 |
checkCompletedWithWrappedException(future, e.ex); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3660 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3661 |
assertEquals(futures.size(), e.count.get()); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3662 |
}} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3663 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3664 |
/** |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3665 |
* Test submissions to an executor that rejects all tasks, but |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3666 |
* should never be invoked because the dependent future is |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3667 |
* explicitly completed. |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3668 |
*/ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3669 |
public void testRejectingExecutorNeverInvoked() { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3670 |
for (Integer v : new Integer[] { 1, null }) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3671 |
{ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3672 |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3673 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3674 |
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3675 |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3676 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3677 |
List<CompletableFuture<?>> fs = new ArrayList<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3678 |
fs.add(incomplete.thenRunAsync(() -> {}, e)); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3679 |
fs.add(incomplete.thenAcceptAsync(z -> {}, e)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3680 |
fs.add(incomplete.thenApplyAsync(z -> z, e)); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3681 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3682 |
fs.add(incomplete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3683 |
fs.add(incomplete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3684 |
fs.add(incomplete.runAfterBothAsync(incomplete, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3685 |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3686 |
fs.add(incomplete.applyToEitherAsync(incomplete, z -> z, e)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3687 |
fs.add(incomplete.acceptEitherAsync(incomplete, z -> {}, e)); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3688 |
fs.add(incomplete.runAfterEitherAsync(incomplete, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3689 |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3690 |
fs.add(incomplete.thenComposeAsync(z -> null, e)); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3691 |
fs.add(incomplete.whenCompleteAsync((z, t) -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3692 |
fs.add(incomplete.handleAsync((z, t) -> null, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3693 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3694 |
fs.add(complete.thenCombineAsync(incomplete, (x, y) -> x, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3695 |
fs.add(incomplete.thenCombineAsync(complete, (x, y) -> x, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3696 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3697 |
fs.add(complete.thenAcceptBothAsync(incomplete, (x, y) -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3698 |
fs.add(incomplete.thenAcceptBothAsync(complete, (x, y) -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3699 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3700 |
fs.add(complete.runAfterBothAsync(incomplete, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3701 |
fs.add(incomplete.runAfterBothAsync(complete, () -> {}, e)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3702 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3703 |
for (CompletableFuture<?> future : fs) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3704 |
checkIncomplete(future); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3705 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3706 |
for (CompletableFuture<?> future : fs) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3707 |
future.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3708 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3709 |
incomplete.complete(v); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3710 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3711 |
for (CompletableFuture<?> future : fs) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3712 |
checkCompletedNormally(future, null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3713 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3714 |
assertEquals(0, e.count.get()); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3715 |
}} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3716 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
3717 |
/** |
35394 | 3718 |
* toCompletableFuture returns this CompletableFuture. |
3719 |
*/ |
|
3720 |
public void testToCompletableFuture() { |
|
3721 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3722 |
assertSame(f, f.toCompletableFuture()); |
|
3723 |
} |
|
3724 |
||
3725 |
// jdk9 |
|
3726 |
||
3727 |
/** |
|
3728 |
* newIncompleteFuture returns an incomplete CompletableFuture |
|
3729 |
*/ |
|
3730 |
public void testNewIncompleteFuture() { |
|
3731 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3732 |
{ |
|
3733 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3734 |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
|
3735 |
checkIncomplete(f); |
|
3736 |
checkIncomplete(g); |
|
3737 |
f.complete(v1); |
|
3738 |
checkCompletedNormally(f, v1); |
|
3739 |
checkIncomplete(g); |
|
3740 |
g.complete(v1); |
|
3741 |
checkCompletedNormally(g, v1); |
|
3742 |
assertSame(g.getClass(), CompletableFuture.class); |
|
3743 |
}} |
|
3744 |
||
3745 |
/** |
|
3746 |
* completedStage returns a completed CompletionStage |
|
3747 |
*/ |
|
3748 |
public void testCompletedStage() { |
|
3749 |
AtomicInteger x = new AtomicInteger(0); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
3750 |
AtomicReference<Throwable> r = new AtomicReference<>(); |
35394 | 3751 |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3752 |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
|
3753 |
assertEquals(x.get(), 1); |
|
3754 |
assertNull(r.get()); |
|
3755 |
} |
|
3756 |
||
3757 |
/** |
|
3758 |
* defaultExecutor by default returns the commonPool if |
|
3759 |
* it supports more than one thread. |
|
3760 |
*/ |
|
3761 |
public void testDefaultExecutor() { |
|
3762 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3763 |
Executor e = f.defaultExecutor(); |
|
3764 |
Executor c = ForkJoinPool.commonPool(); |
|
3765 |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
|
3766 |
assertSame(e, c); |
|
3767 |
else |
|
3768 |
assertNotSame(e, c); |
|
3769 |
} |
|
3770 |
||
3771 |
/** |
|
3772 |
* failedFuture returns a CompletableFuture completed |
|
3773 |
* exceptionally with the given Exception |
|
3774 |
*/ |
|
3775 |
public void testFailedFuture() { |
|
3776 |
CFException ex = new CFException(); |
|
3777 |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
|
3778 |
checkCompletedExceptionally(f, ex); |
|
3779 |
} |
|
3780 |
||
3781 |
/** |
|
3782 |
* failedFuture(null) throws NPE |
|
3783 |
*/ |
|
3784 |
public void testFailedFuture_null() { |
|
3785 |
try { |
|
3786 |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
|
3787 |
shouldThrow(); |
|
3788 |
} catch (NullPointerException success) {} |
|
3789 |
} |
|
3790 |
||
3791 |
/** |
|
3792 |
* copy returns a CompletableFuture that is completed normally, |
|
3793 |
* with the same value, when source is. |
|
3794 |
*/ |
|
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3795 |
public void testCopy_normalCompletion() { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3796 |
for (boolean createIncomplete : new boolean[] { true, false }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3797 |
for (Integer v1 : new Integer[] { 1, null }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3798 |
{ |
35394 | 3799 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3800 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
35394 | 3801 |
CompletableFuture<Integer> g = f.copy(); |
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3802 |
if (createIncomplete) { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3803 |
checkIncomplete(f); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3804 |
checkIncomplete(g); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3805 |
assertTrue(f.complete(v1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3806 |
} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3807 |
checkCompletedNormally(f, v1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3808 |
checkCompletedNormally(g, v1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3809 |
}} |
35394 | 3810 |
|
3811 |
/** |
|
3812 |
* copy returns a CompletableFuture that is completed exceptionally |
|
3813 |
* when source is. |
|
3814 |
*/ |
|
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3815 |
public void testCopy_exceptionalCompletion() { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3816 |
for (boolean createIncomplete : new boolean[] { true, false }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3817 |
{ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3818 |
CFException ex = new CFException(); |
35394 | 3819 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3820 |
if (!createIncomplete) f.completeExceptionally(ex); |
35394 | 3821 |
CompletableFuture<Integer> g = f.copy(); |
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3822 |
if (createIncomplete) { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3823 |
checkIncomplete(f); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3824 |
checkIncomplete(g); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3825 |
f.completeExceptionally(ex); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3826 |
} |
35394 | 3827 |
checkCompletedExceptionally(f, ex); |
3828 |
checkCompletedWithWrappedException(g, ex); |
|
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3829 |
}} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3830 |
|
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3831 |
/** |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3832 |
* Completion of a copy does not complete its source. |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3833 |
*/ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3834 |
public void testCopy_oneWayPropagation() { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3835 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3836 |
assertTrue(f.copy().complete(1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3837 |
assertTrue(f.copy().complete(null)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3838 |
assertTrue(f.copy().cancel(true)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3839 |
assertTrue(f.copy().cancel(false)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3840 |
assertTrue(f.copy().completeExceptionally(new CFException())); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
3841 |
checkIncomplete(f); |
35394 | 3842 |
} |
3843 |
||
3844 |
/** |
|
3845 |
* minimalCompletionStage returns a CompletableFuture that is |
|
3846 |
* completed normally, with the same value, when source is. |
|
3847 |
*/ |
|
3848 |
public void testMinimalCompletionStage() { |
|
3849 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3850 |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
|
3851 |
AtomicInteger x = new AtomicInteger(0); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
3852 |
AtomicReference<Throwable> r = new AtomicReference<>(); |
35394 | 3853 |
checkIncomplete(f); |
3854 |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
|
3855 |
f.complete(1); |
|
3856 |
checkCompletedNormally(f, 1); |
|
3857 |
assertEquals(x.get(), 1); |
|
3858 |
assertNull(r.get()); |
|
3859 |
} |
|
3860 |
||
3861 |
/** |
|
3862 |
* minimalCompletionStage returns a CompletableFuture that is |
|
3863 |
* completed exceptionally when source is. |
|
3864 |
*/ |
|
3865 |
public void testMinimalCompletionStage2() { |
|
3866 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3867 |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
|
3868 |
AtomicInteger x = new AtomicInteger(0); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
3869 |
AtomicReference<Throwable> r = new AtomicReference<>(); |
35394 | 3870 |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3871 |
checkIncomplete(f); |
|
3872 |
CFException ex = new CFException(); |
|
3873 |
f.completeExceptionally(ex); |
|
3874 |
checkCompletedExceptionally(f, ex); |
|
3875 |
assertEquals(x.get(), 0); |
|
3876 |
assertEquals(r.get().getCause(), ex); |
|
3877 |
} |
|
3878 |
||
3879 |
/** |
|
3880 |
* failedStage returns a CompletionStage completed |
|
3881 |
* exceptionally with the given Exception |
|
3882 |
*/ |
|
3883 |
public void testFailedStage() { |
|
3884 |
CFException ex = new CFException(); |
|
3885 |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
|
3886 |
AtomicInteger x = new AtomicInteger(0); |
|
43522
f9c6f543c4db
8171886: Miscellaneous changes imported from jsr166 CVS 2017-02
dl
parents:
42322
diff
changeset
|
3887 |
AtomicReference<Throwable> r = new AtomicReference<>(); |
35394 | 3888 |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3889 |
assertEquals(x.get(), 0); |
|
3890 |
assertEquals(r.get(), ex); |
|
3891 |
} |
|
3892 |
||
3893 |
/** |
|
3894 |
* completeAsync completes with value of given supplier |
|
3895 |
*/ |
|
3896 |
public void testCompleteAsync() { |
|
3897 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3898 |
{ |
|
3899 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3900 |
f.completeAsync(() -> v1); |
|
3901 |
f.join(); |
|
3902 |
checkCompletedNormally(f, v1); |
|
3903 |
}} |
|
3904 |
||
3905 |
/** |
|
3906 |
* completeAsync completes exceptionally if given supplier throws |
|
3907 |
*/ |
|
3908 |
public void testCompleteAsync2() { |
|
3909 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3910 |
CFException ex = new CFException(); |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3911 |
f.completeAsync(() -> { throw ex; }); |
35394 | 3912 |
try { |
3913 |
f.join(); |
|
3914 |
shouldThrow(); |
|
3915 |
} catch (CompletionException success) {} |
|
3916 |
checkCompletedWithWrappedException(f, ex); |
|
3917 |
} |
|
3918 |
||
3919 |
/** |
|
3920 |
* completeAsync with given executor completes with value of given supplier |
|
3921 |
*/ |
|
3922 |
public void testCompleteAsync3() { |
|
3923 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3924 |
{ |
|
3925 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3926 |
ThreadExecutor executor = new ThreadExecutor(); |
|
3927 |
f.completeAsync(() -> v1, executor); |
|
3928 |
assertSame(v1, f.join()); |
|
3929 |
checkCompletedNormally(f, v1); |
|
3930 |
assertEquals(1, executor.count.get()); |
|
3931 |
}} |
|
3932 |
||
3933 |
/** |
|
3934 |
* completeAsync with given executor completes exceptionally if |
|
3935 |
* given supplier throws |
|
3936 |
*/ |
|
3937 |
public void testCompleteAsync4() { |
|
3938 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3939 |
CFException ex = new CFException(); |
|
3940 |
ThreadExecutor executor = new ThreadExecutor(); |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
3941 |
f.completeAsync(() -> { throw ex; }, executor); |
35394 | 3942 |
try { |
3943 |
f.join(); |
|
3944 |
shouldThrow(); |
|
3945 |
} catch (CompletionException success) {} |
|
3946 |
checkCompletedWithWrappedException(f, ex); |
|
3947 |
assertEquals(1, executor.count.get()); |
|
3948 |
} |
|
3949 |
||
3950 |
/** |
|
3951 |
* orTimeout completes with TimeoutException if not complete |
|
3952 |
*/ |
|
3953 |
public void testOrTimeout_timesOut() { |
|
3954 |
long timeoutMillis = timeoutMillis(); |
|
3955 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3956 |
long startTime = System.nanoTime(); |
|
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
3957 |
assertSame(f, f.orTimeout(timeoutMillis, MILLISECONDS)); |
35394 | 3958 |
checkCompletedWithTimeoutException(f); |
3959 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
|
3960 |
} |
|
3961 |
||
3962 |
/** |
|
3963 |
* orTimeout completes normally if completed before timeout |
|
3964 |
*/ |
|
3965 |
public void testOrTimeout_completed() { |
|
3966 |
for (Integer v1 : new Integer[] { 1, null }) |
|
3967 |
{ |
|
3968 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3969 |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
3970 |
long startTime = System.nanoTime(); |
|
3971 |
f.complete(v1); |
|
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
3972 |
assertSame(f, f.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
3973 |
assertSame(g, g.orTimeout(LONG_DELAY_MS, MILLISECONDS)); |
35394 | 3974 |
g.complete(v1); |
3975 |
checkCompletedNormally(f, v1); |
|
3976 |
checkCompletedNormally(g, v1); |
|
3977 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
|
3978 |
}} |
|
3979 |
||
3980 |
/** |
|
3981 |
* completeOnTimeout completes with given value if not complete |
|
3982 |
*/ |
|
3983 |
public void testCompleteOnTimeout_timesOut() { |
|
3984 |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
|
3985 |
() -> testCompleteOnTimeout_timesOut(null)); |
|
3986 |
} |
|
3987 |
||
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
3988 |
/** |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
3989 |
* completeOnTimeout completes with given value if not complete |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
3990 |
*/ |
35394 | 3991 |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3992 |
long timeoutMillis = timeoutMillis(); |
|
3993 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
3994 |
long startTime = System.nanoTime(); |
|
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
3995 |
assertSame(f, f.completeOnTimeout(v, timeoutMillis, MILLISECONDS)); |
35394 | 3996 |
assertSame(v, f.join()); |
3997 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
|
3998 |
f.complete(99); // should have no effect |
|
3999 |
checkCompletedNormally(f, v); |
|
4000 |
} |
|
4001 |
||
4002 |
/** |
|
4003 |
* completeOnTimeout has no effect if completed within timeout |
|
4004 |
*/ |
|
4005 |
public void testCompleteOnTimeout_completed() { |
|
4006 |
for (Integer v1 : new Integer[] { 1, null }) |
|
4007 |
{ |
|
4008 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4009 |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
|
4010 |
long startTime = System.nanoTime(); |
|
4011 |
f.complete(v1); |
|
38551
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
4012 |
assertSame(f, f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
82c48058acc2
8153768: Miscellaneous changes imported from jsr166 CVS 2016-05
dl
parents:
36954
diff
changeset
|
4013 |
assertSame(g, g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS)); |
35394 | 4014 |
g.complete(v1); |
4015 |
checkCompletedNormally(f, v1); |
|
4016 |
checkCompletedNormally(g, v1); |
|
4017 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
|
4018 |
}} |
|
4019 |
||
4020 |
/** |
|
4021 |
* delayedExecutor returns an executor that delays submission |
|
4022 |
*/ |
|
4023 |
public void testDelayedExecutor() { |
|
4024 |
testInParallel(() -> testDelayedExecutor(null, null), |
|
4025 |
() -> testDelayedExecutor(null, 1), |
|
4026 |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
|
4027 |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
|
4028 |
} |
|
4029 |
||
4030 |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
|
4031 |
long timeoutMillis = timeoutMillis(); |
|
4032 |
// Use an "unreasonably long" long timeout to catch lingering threads |
|
4033 |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
|
4034 |
final Executor delayer, longDelayer; |
|
4035 |
if (executor == null) { |
|
4036 |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
|
4037 |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
|
4038 |
} else { |
|
4039 |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
|
4040 |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
|
4041 |
} |
|
4042 |
long startTime = System.nanoTime(); |
|
4043 |
CompletableFuture<Integer> f = |
|
4044 |
CompletableFuture.supplyAsync(() -> v, delayer); |
|
4045 |
CompletableFuture<Integer> g = |
|
4046 |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
|
4047 |
||
4048 |
assertNull(g.getNow(null)); |
|
4049 |
||
4050 |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
|
4051 |
long millisElapsed = millisElapsedSince(startTime); |
|
4052 |
assertTrue(millisElapsed >= timeoutMillis); |
|
4053 |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
|
4054 |
||
4055 |
checkCompletedNormally(f, v); |
|
4056 |
||
4057 |
checkIncomplete(g); |
|
4058 |
assertTrue(g.cancel(true)); |
|
4059 |
} |
|
4060 |
||
4061 |
//--- tests of implementation details; not part of official tck --- |
|
4062 |
||
4063 |
Object resultOf(CompletableFuture<?> f) { |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4064 |
SecurityManager sm = System.getSecurityManager(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4065 |
if (sm != null) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4066 |
try { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4067 |
System.setSecurityManager(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4068 |
} catch (SecurityException giveUp) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4069 |
return "Reflection not available"; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4070 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4071 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4072 |
|
35394 | 4073 |
try { |
4074 |
java.lang.reflect.Field resultField |
|
4075 |
= CompletableFuture.class.getDeclaredField("result"); |
|
4076 |
resultField.setAccessible(true); |
|
4077 |
return resultField.get(f); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4078 |
} catch (Throwable t) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4079 |
throw new AssertionError(t); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4080 |
} finally { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4081 |
if (sm != null) System.setSecurityManager(sm); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4082 |
} |
35394 | 4083 |
} |
4084 |
||
4085 |
public void testExceptionPropagationReusesResultObject() { |
|
4086 |
if (!testImplementationDetails) return; |
|
4087 |
for (ExecutionMode m : ExecutionMode.values()) |
|
4088 |
{ |
|
4089 |
final CFException ex = new CFException(); |
|
4090 |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
|
4091 |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
|
4092 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4093 |
final Runnable noopRunnable = new Noop(m); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4094 |
final Consumer<Integer> noopConsumer = new NoopConsumer(m); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4095 |
final Function<Integer, Integer> incFunction = new IncFunction(m); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4096 |
|
35394 | 4097 |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
4098 |
= new ArrayList<>(); |
|
4099 |
||
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4100 |
funs.add(y -> m.thenRun(y, noopRunnable)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4101 |
funs.add(y -> m.thenAccept(y, noopConsumer)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4102 |
funs.add(y -> m.thenApply(y, incFunction)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4103 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4104 |
funs.add(y -> m.runAfterEither(y, incomplete, noopRunnable)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4105 |
funs.add(y -> m.acceptEither(y, incomplete, noopConsumer)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4106 |
funs.add(y -> m.applyToEither(y, incomplete, incFunction)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4107 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4108 |
funs.add(y -> m.runAfterBoth(y, v42, noopRunnable)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4109 |
funs.add(y -> m.runAfterBoth(v42, y, noopRunnable)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4110 |
funs.add(y -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4111 |
funs.add(y -> m.thenAcceptBoth(v42, y, new SubtractAction(m))); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4112 |
funs.add(y -> m.thenCombine(y, v42, new SubtractFunction(m))); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4113 |
funs.add(y -> m.thenCombine(v42, y, new SubtractFunction(m))); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4114 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4115 |
funs.add(y -> m.whenComplete(y, (Integer r, Throwable t) -> {})); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4116 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4117 |
funs.add(y -> m.thenCompose(y, new CompletableFutureInc(m))); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4118 |
|
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4119 |
funs.add(y -> CompletableFuture.allOf(y)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4120 |
funs.add(y -> CompletableFuture.allOf(y, v42)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4121 |
funs.add(y -> CompletableFuture.allOf(v42, y)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4122 |
funs.add(y -> CompletableFuture.anyOf(y)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4123 |
funs.add(y -> CompletableFuture.anyOf(y, incomplete)); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4124 |
funs.add(y -> CompletableFuture.anyOf(incomplete, y)); |
35394 | 4125 |
|
4126 |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
|
4127 |
fun : funs) { |
|
4128 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4129 |
f.completeExceptionally(ex); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4130 |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
35394 | 4131 |
checkCompletedWithWrappedException(src, ex); |
4132 |
CompletableFuture<?> dep = fun.apply(src); |
|
4133 |
checkCompletedWithWrappedException(dep, ex); |
|
4134 |
assertSame(resultOf(src), resultOf(dep)); |
|
4135 |
} |
|
4136 |
||
4137 |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
|
4138 |
fun : funs) { |
|
4139 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4140 |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
35394 | 4141 |
CompletableFuture<?> dep = fun.apply(src); |
4142 |
f.completeExceptionally(ex); |
|
4143 |
checkCompletedWithWrappedException(src, ex); |
|
4144 |
checkCompletedWithWrappedException(dep, ex); |
|
4145 |
assertSame(resultOf(src), resultOf(dep)); |
|
4146 |
} |
|
4147 |
||
4148 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
4149 |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
|
4150 |
fun : funs) { |
|
4151 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
4152 |
f.cancel(mayInterruptIfRunning); |
|
4153 |
checkCancelled(f); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4154 |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
35394 | 4155 |
checkCompletedWithWrappedCancellationException(src); |
4156 |
CompletableFuture<?> dep = fun.apply(src); |
|
4157 |
checkCompletedWithWrappedCancellationException(dep); |
|
4158 |
assertSame(resultOf(src), resultOf(dep)); |
|
4159 |
} |
|
4160 |
||
4161 |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
|
4162 |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
|
4163 |
fun : funs) { |
|
4164 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4165 |
CompletableFuture<Integer> src = m.thenApply(f, incFunction); |
35394 | 4166 |
CompletableFuture<?> dep = fun.apply(src); |
4167 |
f.cancel(mayInterruptIfRunning); |
|
4168 |
checkCancelled(f); |
|
4169 |
checkCompletedWithWrappedCancellationException(src); |
|
4170 |
checkCompletedWithWrappedCancellationException(dep); |
|
4171 |
assertSame(resultOf(src), resultOf(dep)); |
|
4172 |
} |
|
4173 |
}} |
|
4174 |
||
4175 |
/** |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4176 |
* Minimal completion stages throw UOE for most non-CompletionStage methods |
35394 | 4177 |
*/ |
4178 |
public void testMinimalCompletionStage_minimality() { |
|
4179 |
if (!testImplementationDetails) return; |
|
4180 |
Function<Method, String> toSignature = |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4181 |
method -> method.getName() + Arrays.toString(method.getParameterTypes()); |
35394 | 4182 |
Predicate<Method> isNotStatic = |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4183 |
method -> (method.getModifiers() & Modifier.STATIC) == 0; |
35394 | 4184 |
List<Method> minimalMethods = |
4185 |
Stream.of(Object.class, CompletionStage.class) |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4186 |
.flatMap(klazz -> Stream.of(klazz.getMethods())) |
35394 | 4187 |
.filter(isNotStatic) |
4188 |
.collect(Collectors.toList()); |
|
4189 |
// Methods from CompletableFuture permitted NOT to throw UOE |
|
4190 |
String[] signatureWhitelist = { |
|
4191 |
"newIncompleteFuture[]", |
|
4192 |
"defaultExecutor[]", |
|
4193 |
"minimalCompletionStage[]", |
|
4194 |
"copy[]", |
|
4195 |
}; |
|
4196 |
Set<String> permittedMethodSignatures = |
|
4197 |
Stream.concat(minimalMethods.stream().map(toSignature), |
|
4198 |
Stream.of(signatureWhitelist)) |
|
4199 |
.collect(Collectors.toSet()); |
|
4200 |
List<Method> allMethods = Stream.of(CompletableFuture.class.getMethods()) |
|
4201 |
.filter(isNotStatic) |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4202 |
.filter(method -> !permittedMethodSignatures.contains(toSignature.apply(method))) |
35394 | 4203 |
.collect(Collectors.toList()); |
4204 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4205 |
List<CompletionStage<Integer>> stages = new ArrayList<>(); |
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4206 |
CompletionStage<Integer> min = |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4207 |
new CompletableFuture<Integer>().minimalCompletionStage(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4208 |
stages.add(min); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4209 |
stages.add(min.thenApply(x -> x)); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4210 |
stages.add(CompletableFuture.completedStage(1)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4211 |
stages.add(CompletableFuture.failedStage(new CFException())); |
35394 | 4212 |
|
4213 |
List<Method> bugs = new ArrayList<>(); |
|
4214 |
for (Method method : allMethods) { |
|
4215 |
Class<?>[] parameterTypes = method.getParameterTypes(); |
|
4216 |
Object[] args = new Object[parameterTypes.length]; |
|
4217 |
// Manufacture boxed primitives for primitive params |
|
4218 |
for (int i = 0; i < args.length; i++) { |
|
4219 |
Class<?> type = parameterTypes[i]; |
|
4220 |
if (parameterTypes[i] == boolean.class) |
|
4221 |
args[i] = false; |
|
4222 |
else if (parameterTypes[i] == int.class) |
|
4223 |
args[i] = 0; |
|
4224 |
else if (parameterTypes[i] == long.class) |
|
4225 |
args[i] = 0L; |
|
4226 |
} |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4227 |
for (CompletionStage<Integer> stage : stages) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4228 |
try { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4229 |
method.invoke(stage, args); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4230 |
bugs.add(method); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4231 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4232 |
catch (java.lang.reflect.InvocationTargetException expected) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4233 |
if (! (expected.getCause() instanceof UnsupportedOperationException)) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4234 |
bugs.add(method); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4235 |
// expected.getCause().printStackTrace(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4236 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4237 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4238 |
catch (ReflectiveOperationException bad) { throw new Error(bad); } |
35394 | 4239 |
} |
4240 |
} |
|
4241 |
if (!bugs.isEmpty()) |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4242 |
throw new Error("Methods did not throw UOE: " + bugs); |
35394 | 4243 |
} |
4244 |
||
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4245 |
/** |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4246 |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4247 |
* is completed normally, with the same value, when source is. |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4248 |
*/ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4249 |
public void testMinimalCompletionStage_toCompletableFuture_normalCompletion() { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4250 |
for (boolean createIncomplete : new boolean[] { true, false }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4251 |
for (Integer v1 : new Integer[] { 1, null }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4252 |
{ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4253 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4254 |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4255 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4256 |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4257 |
if (createIncomplete) { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4258 |
checkIncomplete(f); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4259 |
checkIncomplete(g); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4260 |
assertTrue(f.complete(v1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4261 |
} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4262 |
checkCompletedNormally(f, v1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4263 |
checkCompletedNormally(g, v1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4264 |
}} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4265 |
|
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4266 |
/** |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4267 |
* minimalStage.toCompletableFuture() returns a CompletableFuture that |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4268 |
* is completed exceptionally when source is. |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4269 |
*/ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4270 |
public void testMinimalCompletionStage_toCompletableFuture_exceptionalCompletion() { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4271 |
for (boolean createIncomplete : new boolean[] { true, false }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4272 |
{ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4273 |
CFException ex = new CFException(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4274 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4275 |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4276 |
if (!createIncomplete) f.completeExceptionally(ex); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4277 |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4278 |
if (createIncomplete) { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4279 |
checkIncomplete(f); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4280 |
checkIncomplete(g); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4281 |
f.completeExceptionally(ex); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4282 |
} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4283 |
checkCompletedExceptionally(f, ex); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4284 |
checkCompletedWithWrappedException(g, ex); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4285 |
}} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4286 |
|
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4287 |
/** |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4288 |
* minimalStage.toCompletableFuture() gives mutable CompletableFuture |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4289 |
*/ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4290 |
public void testMinimalCompletionStage_toCompletableFuture_mutable() { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4291 |
for (Integer v1 : new Integer[] { 1, null }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4292 |
{ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4293 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4294 |
CompletionStage minimal = f.minimalCompletionStage(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4295 |
CompletableFuture<Integer> g = minimal.toCompletableFuture(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4296 |
assertTrue(g.complete(v1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4297 |
checkCompletedNormally(g, v1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4298 |
checkIncomplete(f); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4299 |
checkIncomplete(minimal.toCompletableFuture()); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4300 |
}} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4301 |
|
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4302 |
/** |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4303 |
* minimalStage.toCompletableFuture().join() awaits completion |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4304 |
*/ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4305 |
public void testMinimalCompletionStage_toCompletableFuture_join() throws Exception { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4306 |
for (boolean createIncomplete : new boolean[] { true, false }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4307 |
for (Integer v1 : new Integer[] { 1, null }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4308 |
{ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4309 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4310 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4311 |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4312 |
if (createIncomplete) assertTrue(f.complete(v1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4313 |
assertEquals(v1, minimal.toCompletableFuture().join()); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4314 |
assertEquals(v1, minimal.toCompletableFuture().get()); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4315 |
checkCompletedNormally(minimal.toCompletableFuture(), v1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4316 |
}} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4317 |
|
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4318 |
/** |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4319 |
* Completion of a toCompletableFuture copy of a minimal stage |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4320 |
* does not complete its source. |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4321 |
*/ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4322 |
public void testMinimalCompletionStage_toCompletableFuture_oneWayPropagation() { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4323 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4324 |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4325 |
assertTrue(g.toCompletableFuture().complete(1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4326 |
assertTrue(g.toCompletableFuture().complete(null)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4327 |
assertTrue(g.toCompletableFuture().cancel(true)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4328 |
assertTrue(g.toCompletableFuture().cancel(false)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4329 |
assertTrue(g.toCompletableFuture().completeExceptionally(new CFException())); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4330 |
checkIncomplete(g.toCompletableFuture()); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4331 |
f.complete(1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4332 |
checkCompletedNormally(g.toCompletableFuture(), 1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4333 |
} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4334 |
|
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4335 |
/** Demo utility method for external reliable toCompletableFuture */ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4336 |
static <T> CompletableFuture<T> toCompletableFuture(CompletionStage<T> stage) { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4337 |
CompletableFuture<T> f = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4338 |
stage.handle((T t, Throwable ex) -> { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4339 |
if (ex != null) f.completeExceptionally(ex); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4340 |
else f.complete(t); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4341 |
return null; |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4342 |
}); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4343 |
return f; |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4344 |
} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4345 |
|
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4346 |
/** Demo utility method to join a CompletionStage */ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4347 |
static <T> T join(CompletionStage<T> stage) { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4348 |
return toCompletableFuture(stage).join(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4349 |
} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4350 |
|
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4351 |
/** |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4352 |
* Joining a minimal stage "by hand" works |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4353 |
*/ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4354 |
public void testMinimalCompletionStage_join_by_hand() { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4355 |
for (boolean createIncomplete : new boolean[] { true, false }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4356 |
for (Integer v1 : new Integer[] { 1, null }) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4357 |
{ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4358 |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4359 |
CompletionStage<Integer> minimal = f.minimalCompletionStage(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4360 |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4361 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4362 |
minimal.thenAccept(x -> g.complete(x)); |
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4363 |
if (createIncomplete) assertTrue(f.complete(v1)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4364 |
g.join(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4365 |
checkCompletedNormally(g, v1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4366 |
checkCompletedNormally(f, v1); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4367 |
assertEquals(v1, join(minimal)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4368 |
}} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4369 |
|
35394 | 4370 |
static class Monad { |
4371 |
static class ZeroException extends RuntimeException { |
|
4372 |
public ZeroException() { super("monadic zero"); } |
|
4373 |
} |
|
4374 |
// "return", "unit" |
|
4375 |
static <T> CompletableFuture<T> unit(T value) { |
|
4376 |
return completedFuture(value); |
|
4377 |
} |
|
4378 |
// monadic zero ? |
|
4379 |
static <T> CompletableFuture<T> zero() { |
|
4380 |
return failedFuture(new ZeroException()); |
|
4381 |
} |
|
4382 |
// >=> |
|
4383 |
static <T,U,V> Function<T, CompletableFuture<V>> compose |
|
4384 |
(Function<T, CompletableFuture<U>> f, |
|
4385 |
Function<U, CompletableFuture<V>> g) { |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4386 |
return x -> f.apply(x).thenCompose(g); |
35394 | 4387 |
} |
4388 |
||
4389 |
static void assertZero(CompletableFuture<?> f) { |
|
4390 |
try { |
|
4391 |
f.getNow(null); |
|
48843
21efc1774302
8195590: Miscellaneous changes imported from jsr166 CVS 2018-02
dl
parents:
48541
diff
changeset
|
4392 |
throw new AssertionError("should throw"); |
35394 | 4393 |
} catch (CompletionException success) { |
4394 |
assertTrue(success.getCause() instanceof ZeroException); |
|
4395 |
} |
|
4396 |
} |
|
4397 |
||
4398 |
static <T> void assertFutureEquals(CompletableFuture<T> f, |
|
4399 |
CompletableFuture<T> g) { |
|
4400 |
T fval = null, gval = null; |
|
4401 |
Throwable fex = null, gex = null; |
|
4402 |
||
4403 |
try { fval = f.get(); } |
|
4404 |
catch (ExecutionException ex) { fex = ex.getCause(); } |
|
4405 |
catch (Throwable ex) { fex = ex; } |
|
4406 |
||
4407 |
try { gval = g.get(); } |
|
4408 |
catch (ExecutionException ex) { gex = ex.getCause(); } |
|
4409 |
catch (Throwable ex) { gex = ex; } |
|
4410 |
||
4411 |
if (fex != null || gex != null) |
|
4412 |
assertSame(fex.getClass(), gex.getClass()); |
|
4413 |
else |
|
4414 |
assertEquals(fval, gval); |
|
4415 |
} |
|
4416 |
||
4417 |
static class PlusFuture<T> extends CompletableFuture<T> { |
|
4418 |
AtomicReference<Throwable> firstFailure = new AtomicReference<>(null); |
|
4419 |
} |
|
4420 |
||
4421 |
/** Implements "monadic plus". */ |
|
4422 |
static <T> CompletableFuture<T> plus(CompletableFuture<? extends T> f, |
|
4423 |
CompletableFuture<? extends T> g) { |
|
4424 |
PlusFuture<T> plus = new PlusFuture<T>(); |
|
4425 |
BiConsumer<T, Throwable> action = (T result, Throwable ex) -> { |
|
4426 |
try { |
|
4427 |
if (ex == null) { |
|
4428 |
if (plus.complete(result)) |
|
4429 |
if (plus.firstFailure.get() != null) |
|
4430 |
plus.firstFailure.set(null); |
|
4431 |
} |
|
4432 |
else if (plus.firstFailure.compareAndSet(null, ex)) { |
|
4433 |
if (plus.isDone()) |
|
4434 |
plus.firstFailure.set(null); |
|
4435 |
} |
|
4436 |
else { |
|
4437 |
// first failure has precedence |
|
4438 |
Throwable first = plus.firstFailure.getAndSet(null); |
|
4439 |
||
4440 |
// may fail with "Self-suppression not permitted" |
|
4441 |
try { first.addSuppressed(ex); } |
|
4442 |
catch (Exception ignored) {} |
|
4443 |
||
4444 |
plus.completeExceptionally(first); |
|
4445 |
} |
|
4446 |
} catch (Throwable unexpected) { |
|
4447 |
plus.completeExceptionally(unexpected); |
|
4448 |
} |
|
4449 |
}; |
|
4450 |
f.whenComplete(action); |
|
4451 |
g.whenComplete(action); |
|
4452 |
return plus; |
|
4453 |
} |
|
4454 |
} |
|
4455 |
||
4456 |
/** |
|
4457 |
* CompletableFuture is an additive monad - sort of. |
|
4458 |
* https://en.wikipedia.org/wiki/Monad_(functional_programming)#Additive_monads |
|
4459 |
*/ |
|
4460 |
public void testAdditiveMonad() throws Throwable { |
|
4461 |
Function<Long, CompletableFuture<Long>> unit = Monad::unit; |
|
4462 |
CompletableFuture<Long> zero = Monad.zero(); |
|
4463 |
||
4464 |
// Some mutually non-commutative functions |
|
4465 |
Function<Long, CompletableFuture<Long>> triple |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4466 |
= x -> Monad.unit(3 * x); |
35394 | 4467 |
Function<Long, CompletableFuture<Long>> inc |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4468 |
= x -> Monad.unit(x + 1); |
35394 | 4469 |
|
4470 |
// unit is a right identity: m >>= unit === m |
|
4471 |
Monad.assertFutureEquals(inc.apply(5L).thenCompose(unit), |
|
4472 |
inc.apply(5L)); |
|
4473 |
// unit is a left identity: (unit x) >>= f === f x |
|
4474 |
Monad.assertFutureEquals(unit.apply(5L).thenCompose(inc), |
|
4475 |
inc.apply(5L)); |
|
4476 |
||
4477 |
// associativity: (m >>= f) >>= g === m >>= ( \x -> (f x >>= g) ) |
|
4478 |
Monad.assertFutureEquals( |
|
4479 |
unit.apply(5L).thenCompose(inc).thenCompose(triple), |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4480 |
unit.apply(5L).thenCompose(x -> inc.apply(x).thenCompose(triple))); |
35394 | 4481 |
|
4482 |
// The case for CompletableFuture as an additive monad is weaker... |
|
4483 |
||
4484 |
// zero is a monadic zero |
|
4485 |
Monad.assertZero(zero); |
|
4486 |
||
4487 |
// left zero: zero >>= f === zero |
|
4488 |
Monad.assertZero(zero.thenCompose(inc)); |
|
4489 |
// right zero: f >>= (\x -> zero) === zero |
|
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4490 |
Monad.assertZero(inc.apply(5L).thenCompose(x -> zero)); |
35394 | 4491 |
|
4492 |
// f plus zero === f |
|
4493 |
Monad.assertFutureEquals(Monad.unit(5L), |
|
4494 |
Monad.plus(Monad.unit(5L), zero)); |
|
4495 |
// zero plus f === f |
|
4496 |
Monad.assertFutureEquals(Monad.unit(5L), |
|
4497 |
Monad.plus(zero, Monad.unit(5L))); |
|
4498 |
// zero plus zero === zero |
|
4499 |
Monad.assertZero(Monad.plus(zero, zero)); |
|
4500 |
{ |
|
4501 |
CompletableFuture<Long> f = Monad.plus(Monad.unit(5L), |
|
4502 |
Monad.unit(8L)); |
|
4503 |
// non-determinism |
|
4504 |
assertTrue(f.get() == 5L || f.get() == 8L); |
|
4505 |
} |
|
4506 |
||
4507 |
CompletableFuture<Long> godot = new CompletableFuture<>(); |
|
4508 |
// f plus godot === f (doesn't wait for godot) |
|
4509 |
Monad.assertFutureEquals(Monad.unit(5L), |
|
4510 |
Monad.plus(Monad.unit(5L), godot)); |
|
4511 |
// godot plus f === f (doesn't wait for godot) |
|
4512 |
Monad.assertFutureEquals(Monad.unit(5L), |
|
4513 |
Monad.plus(godot, Monad.unit(5L))); |
|
4514 |
} |
|
4515 |
||
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4516 |
/** Test long recursive chains of CompletableFutures with cascading completions */ |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
4517 |
@SuppressWarnings("FutureReturnValueIgnored") |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4518 |
public void testRecursiveChains() throws Throwable { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4519 |
for (ExecutionMode m : ExecutionMode.values()) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4520 |
for (boolean addDeadEnds : new boolean[] { true, false }) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4521 |
{ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4522 |
final int val = 42; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4523 |
final int n = expensiveTests ? 1_000 : 2; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4524 |
CompletableFuture<Integer> head = new CompletableFuture<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4525 |
CompletableFuture<Integer> tail = head; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4526 |
for (int i = 0; i < n; i++) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4527 |
if (addDeadEnds) m.thenApply(tail, v -> v + 1); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4528 |
tail = m.thenApply(tail, v -> v + 1); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4529 |
if (addDeadEnds) m.applyToEither(tail, tail, v -> v + 1); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4530 |
tail = m.applyToEither(tail, tail, v -> v + 1); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4531 |
if (addDeadEnds) m.thenCombine(tail, tail, (v, w) -> v + 1); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4532 |
tail = m.thenCombine(tail, tail, (v, w) -> v + 1); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4533 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4534 |
head.complete(val); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4535 |
assertEquals(val + 3 * n, (int) tail.join()); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4536 |
}} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4537 |
|
36954
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4538 |
/** |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4539 |
* A single CompletableFuture with many dependents. |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4540 |
* A demo of scalability - runtime is O(n). |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4541 |
*/ |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
4542 |
@SuppressWarnings("FutureReturnValueIgnored") |
36954
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4543 |
public void testManyDependents() throws Throwable { |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4544 |
final int n = expensiveTests ? 1_000_000 : 10; |
36954
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4545 |
final CompletableFuture<Void> head = new CompletableFuture<>(); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4546 |
final CompletableFuture<Void> complete = CompletableFuture.completedFuture((Void)null); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4547 |
final AtomicInteger count = new AtomicInteger(0); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4548 |
for (int i = 0; i < n; i++) { |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4549 |
head.thenRun(() -> count.getAndIncrement()); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4550 |
head.thenAccept(x -> count.getAndIncrement()); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4551 |
head.thenApply(x -> count.getAndIncrement()); |
36954
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4552 |
|
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4553 |
head.runAfterBoth(complete, () -> count.getAndIncrement()); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4554 |
head.thenAcceptBoth(complete, (x, y) -> count.getAndIncrement()); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4555 |
head.thenCombine(complete, (x, y) -> count.getAndIncrement()); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4556 |
complete.runAfterBoth(head, () -> count.getAndIncrement()); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4557 |
complete.thenAcceptBoth(head, (x, y) -> count.getAndIncrement()); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4558 |
complete.thenCombine(head, (x, y) -> count.getAndIncrement()); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4559 |
|
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4560 |
head.runAfterEither(new CompletableFuture<Void>(), () -> count.getAndIncrement()); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4561 |
head.acceptEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4562 |
head.applyToEither(new CompletableFuture<Void>(), x -> count.getAndIncrement()); |
36954
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4563 |
new CompletableFuture<Void>().runAfterEither(head, () -> count.getAndIncrement()); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4564 |
new CompletableFuture<Void>().acceptEither(head, x -> count.getAndIncrement()); |
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4565 |
new CompletableFuture<Void>().applyToEither(head, x -> count.getAndIncrement()); |
36954
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4566 |
} |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4567 |
head.complete(null); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4568 |
assertEquals(5 * 3 * n, count.get()); |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4569 |
} |
7e9b8a9b0074
8152924: Improve scalability of CompletableFuture with large number of dependents
dl
parents:
35980
diff
changeset
|
4570 |
|
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4571 |
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
45937
646816090183
8178409: Miscellaneous changes imported from jsr166 CVS 2017-07
dl
parents:
44589
diff
changeset
|
4572 |
@SuppressWarnings("FutureReturnValueIgnored") |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4573 |
public void testCoCompletionGarbageRetention() throws Throwable { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4574 |
final int n = expensiveTests ? 1_000_000 : 10; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4575 |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4576 |
CompletableFuture<Integer> f; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4577 |
for (int i = 0; i < n; i++) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4578 |
f = new CompletableFuture<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4579 |
f.runAfterEither(incomplete, () -> {}); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4580 |
f.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4581 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4582 |
f = new CompletableFuture<>(); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4583 |
f.acceptEither(incomplete, x -> {}); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4584 |
f.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4585 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4586 |
f = new CompletableFuture<>(); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4587 |
f.applyToEither(incomplete, x -> x); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4588 |
f.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4589 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4590 |
f = new CompletableFuture<>(); |
50764
5637aca18f1d
8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents:
48843
diff
changeset
|
4591 |
CompletableFuture.anyOf(f, incomplete); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4592 |
f.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4593 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4594 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4595 |
for (int i = 0; i < n; i++) { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4596 |
f = new CompletableFuture<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4597 |
incomplete.runAfterEither(f, () -> {}); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4598 |
f.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4599 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4600 |
f = new CompletableFuture<>(); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4601 |
incomplete.acceptEither(f, x -> {}); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4602 |
f.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4603 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4604 |
f = new CompletableFuture<>(); |
42322
c3474fef4fe4
8166646: Miscellaneous changes imported from jsr166 CVS 2016-10
dl
parents:
41128
diff
changeset
|
4605 |
incomplete.applyToEither(f, x -> x); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4606 |
f.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4607 |
|
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4608 |
f = new CompletableFuture<>(); |
50764
5637aca18f1d
8203681: Miscellaneous changes imported from jsr166 CVS 2018-06
dl
parents:
48843
diff
changeset
|
4609 |
CompletableFuture.anyOf(incomplete, f); |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4610 |
f.complete(null); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4611 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4612 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4613 |
|
39778
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4614 |
/** |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4615 |
* Reproduction recipe for: |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4616 |
* 8160402: Garbage retention with CompletableFuture.anyOf |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4617 |
* cvs update -D '2016-05-01' ./src/main/java/util/concurrent/CompletableFuture.java && ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testAnyOfGarbageRetention tck; cvs update -A |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4618 |
*/ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4619 |
public void testAnyOfGarbageRetention() throws Throwable { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4620 |
for (Integer v : new Integer[] { 1, null }) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4621 |
{ |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4622 |
final int n = expensiveTests ? 100_000 : 10; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4623 |
CompletableFuture<Integer>[] fs |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4624 |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4625 |
for (int i = 0; i < fs.length; i++) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4626 |
fs[i] = new CompletableFuture<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4627 |
fs[fs.length - 1].complete(v); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4628 |
for (int i = 0; i < n; i++) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4629 |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4630 |
}} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4631 |
|
39778
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4632 |
/** |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4633 |
* Checks for garbage retention with allOf. |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4634 |
* |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4635 |
* As of 2016-07, fails with OOME: |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4636 |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledAllOfGarbageRetention tck |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4637 |
*/ |
39724
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4638 |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4639 |
final int n = expensiveTests ? 100_000 : 10; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4640 |
CompletableFuture<Integer>[] fs |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4641 |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4642 |
for (int i = 0; i < fs.length; i++) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4643 |
fs[i] = new CompletableFuture<>(); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4644 |
for (int i = 0; i < n; i++) |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4645 |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4646 |
} |
d935d42f6d93
8157522: Performance improvements to CompletableFuture
dl
parents:
38551
diff
changeset
|
4647 |
|
39778
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4648 |
/** |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4649 |
* Checks for garbage retention when a dependent future is |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4650 |
* cancelled and garbage-collected. |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4651 |
* 8161600: Garbage retention when source CompletableFutures are never completed |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4652 |
* |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4653 |
* As of 2016-07, fails with OOME: |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4654 |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testCancelledGarbageRetention tck |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4655 |
*/ |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4656 |
public void testCancelledGarbageRetention() throws Throwable { |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4657 |
final int n = expensiveTests ? 100_000 : 10; |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4658 |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4659 |
for (int i = 0; i < n; i++) |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4660 |
assertTrue(neverCompleted.thenRun(() -> {}).cancel(true)); |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4661 |
} |
5cda06c52cdd
8160402: Garbage retention with CompletableFuture.anyOf
dl
parents:
39724
diff
changeset
|
4662 |
|
41128
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4663 |
/** |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4664 |
* Checks for garbage retention when MinimalStage.toCompletableFuture() |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4665 |
* is invoked many times. |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4666 |
* 8161600: Garbage retention when source CompletableFutures are never completed |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4667 |
* |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4668 |
* As of 2016-07, fails with OOME: |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4669 |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest -Djsr166.methodFilter=testToCompletableFutureGarbageRetention tck |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4670 |
*/ |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4671 |
public void testToCompletableFutureGarbageRetention() throws Throwable { |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4672 |
final int n = expensiveTests ? 900_000 : 10; |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4673 |
CompletableFuture<Integer> neverCompleted = new CompletableFuture<>(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4674 |
CompletionStage minimal = neverCompleted.minimalCompletionStage(); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4675 |
for (int i = 0; i < n; i++) |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4676 |
assertTrue(minimal.toCompletableFuture().cancel(true)); |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4677 |
} |
0dfc0bc2196c
8166465: CompletableFuture.minimalCompletionStage().toCompletableFuture() should be non-minimal
dl
parents:
40817
diff
changeset
|
4678 |
|
35394 | 4679 |
// static <U> U join(CompletionStage<U> stage) { |
4680 |
// CompletableFuture<U> f = new CompletableFuture<>(); |
|
4681 |
// stage.whenComplete((v, ex) -> { |
|
4682 |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
|
4683 |
// }); |
|
4684 |
// return f.join(); |
|
4685 |
// } |
|
4686 |
||
4687 |
// static <U> boolean isDone(CompletionStage<U> stage) { |
|
4688 |
// CompletableFuture<U> f = new CompletableFuture<>(); |
|
4689 |
// stage.whenComplete((v, ex) -> { |
|
4690 |
// if (ex != null) f.completeExceptionally(ex); else f.complete(v); |
|
4691 |
// }); |
|
4692 |
// return f.isDone(); |
|
4693 |
// } |
|
4694 |
||
4695 |
// static <U> U join2(CompletionStage<U> stage) { |
|
4696 |
// return stage.toCompletableFuture().copy().join(); |
|
4697 |
// } |
|
4698 |
||
4699 |
// static <U> boolean isDone2(CompletionStage<U> stage) { |
|
4700 |
// return stage.toCompletableFuture().copy().isDone(); |
|
4701 |
// } |
|
4702 |
||
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4703 |
// For testing default implementations |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4704 |
// Only non-default interface methods defined. |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4705 |
static final class DelegatedCompletionStage<T> implements CompletionStage<T> { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4706 |
final CompletableFuture<T> cf; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4707 |
DelegatedCompletionStage(CompletableFuture<T> cf) { this.cf = cf; } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4708 |
public CompletableFuture<T> toCompletableFuture() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4709 |
return cf; } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4710 |
public CompletionStage<Void> thenRun |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4711 |
(Runnable action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4712 |
return cf.thenRun(action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4713 |
public CompletionStage<Void> thenRunAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4714 |
(Runnable action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4715 |
return cf.thenRunAsync(action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4716 |
public CompletionStage<Void> thenRunAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4717 |
(Runnable action, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4718 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4719 |
return cf.thenRunAsync(action, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4720 |
public CompletionStage<Void> thenAccept |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4721 |
(Consumer<? super T> action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4722 |
return cf.thenAccept(action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4723 |
public CompletionStage<Void> thenAcceptAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4724 |
(Consumer<? super T> action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4725 |
return cf.thenAcceptAsync(action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4726 |
public CompletionStage<Void> thenAcceptAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4727 |
(Consumer<? super T> action, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4728 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4729 |
return cf.thenAcceptAsync(action, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4730 |
public <U> CompletionStage<U> thenApply |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4731 |
(Function<? super T,? extends U> a) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4732 |
return cf.thenApply(a); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4733 |
public <U> CompletionStage<U> thenApplyAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4734 |
(Function<? super T,? extends U> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4735 |
return cf.thenApplyAsync(fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4736 |
public <U> CompletionStage<U> thenApplyAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4737 |
(Function<? super T,? extends U> fn, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4738 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4739 |
return cf.thenApplyAsync(fn, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4740 |
public <U,V> CompletionStage<V> thenCombine |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4741 |
(CompletionStage<? extends U> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4742 |
BiFunction<? super T,? super U,? extends V> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4743 |
return cf.thenCombine(other, fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4744 |
public <U,V> CompletionStage<V> thenCombineAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4745 |
(CompletionStage<? extends U> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4746 |
BiFunction<? super T,? super U,? extends V> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4747 |
return cf.thenCombineAsync(other, fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4748 |
public <U,V> CompletionStage<V> thenCombineAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4749 |
(CompletionStage<? extends U> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4750 |
BiFunction<? super T,? super U,? extends V> fn, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4751 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4752 |
return cf.thenCombineAsync(other, fn, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4753 |
public <U> CompletionStage<Void> thenAcceptBoth |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4754 |
(CompletionStage<? extends U> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4755 |
BiConsumer<? super T, ? super U> action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4756 |
return cf.thenAcceptBoth(other, action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4757 |
public <U> CompletionStage<Void> thenAcceptBothAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4758 |
(CompletionStage<? extends U> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4759 |
BiConsumer<? super T, ? super U> action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4760 |
return cf.thenAcceptBothAsync(other, action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4761 |
public <U> CompletionStage<Void> thenAcceptBothAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4762 |
(CompletionStage<? extends U> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4763 |
BiConsumer<? super T, ? super U> action, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4764 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4765 |
return cf.thenAcceptBothAsync(other, action, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4766 |
public CompletionStage<Void> runAfterBoth |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4767 |
(CompletionStage<?> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4768 |
Runnable action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4769 |
return cf.runAfterBoth(other, action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4770 |
public CompletionStage<Void> runAfterBothAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4771 |
(CompletionStage<?> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4772 |
Runnable action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4773 |
return cf.runAfterBothAsync(other, action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4774 |
public CompletionStage<Void> runAfterBothAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4775 |
(CompletionStage<?> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4776 |
Runnable action, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4777 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4778 |
return cf.runAfterBothAsync(other, action, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4779 |
public <U> CompletionStage<U> applyToEither |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4780 |
(CompletionStage<? extends T> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4781 |
Function<? super T, U> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4782 |
return cf.applyToEither(other, fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4783 |
public <U> CompletionStage<U> applyToEitherAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4784 |
(CompletionStage<? extends T> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4785 |
Function<? super T, U> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4786 |
return cf.applyToEitherAsync(other, fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4787 |
public <U> CompletionStage<U> applyToEitherAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4788 |
(CompletionStage<? extends T> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4789 |
Function<? super T, U> fn, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4790 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4791 |
return cf.applyToEitherAsync(other, fn, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4792 |
public CompletionStage<Void> acceptEither |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4793 |
(CompletionStage<? extends T> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4794 |
Consumer<? super T> action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4795 |
return cf.acceptEither(other, action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4796 |
public CompletionStage<Void> acceptEitherAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4797 |
(CompletionStage<? extends T> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4798 |
Consumer<? super T> action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4799 |
return cf.acceptEitherAsync(other, action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4800 |
public CompletionStage<Void> acceptEitherAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4801 |
(CompletionStage<? extends T> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4802 |
Consumer<? super T> action, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4803 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4804 |
return cf.acceptEitherAsync(other, action, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4805 |
public CompletionStage<Void> runAfterEither |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4806 |
(CompletionStage<?> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4807 |
Runnable action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4808 |
return cf.runAfterEither(other, action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4809 |
public CompletionStage<Void> runAfterEitherAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4810 |
(CompletionStage<?> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4811 |
Runnable action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4812 |
return cf.runAfterEitherAsync(other, action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4813 |
public CompletionStage<Void> runAfterEitherAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4814 |
(CompletionStage<?> other, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4815 |
Runnable action, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4816 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4817 |
return cf.runAfterEitherAsync(other, action, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4818 |
public <U> CompletionStage<U> thenCompose |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4819 |
(Function<? super T, ? extends CompletionStage<U>> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4820 |
return cf.thenCompose(fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4821 |
public <U> CompletionStage<U> thenComposeAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4822 |
(Function<? super T, ? extends CompletionStage<U>> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4823 |
return cf.thenComposeAsync(fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4824 |
public <U> CompletionStage<U> thenComposeAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4825 |
(Function<? super T, ? extends CompletionStage<U>> fn, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4826 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4827 |
return cf.thenComposeAsync(fn, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4828 |
public <U> CompletionStage<U> handle |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4829 |
(BiFunction<? super T, Throwable, ? extends U> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4830 |
return cf.handle(fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4831 |
public <U> CompletionStage<U> handleAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4832 |
(BiFunction<? super T, Throwable, ? extends U> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4833 |
return cf.handleAsync(fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4834 |
public <U> CompletionStage<U> handleAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4835 |
(BiFunction<? super T, Throwable, ? extends U> fn, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4836 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4837 |
return cf.handleAsync(fn, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4838 |
public CompletionStage<T> whenComplete |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4839 |
(BiConsumer<? super T, ? super Throwable> action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4840 |
return cf.whenComplete(action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4841 |
public CompletionStage<T> whenCompleteAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4842 |
(BiConsumer<? super T, ? super Throwable> action) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4843 |
return cf.whenCompleteAsync(action); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4844 |
public CompletionStage<T> whenCompleteAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4845 |
(BiConsumer<? super T, ? super Throwable> action, |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4846 |
Executor executor) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4847 |
return cf.whenCompleteAsync(action, executor); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4848 |
public CompletionStage<T> exceptionally |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4849 |
(Function<Throwable, ? extends T> fn) { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4850 |
return cf.exceptionally(fn); } |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4851 |
} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4852 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4853 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4854 |
* default-implemented exceptionallyAsync action is not invoked when |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4855 |
* source completes normally, and source result is propagated |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4856 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4857 |
public void testDefaultExceptionallyAsync_normalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4858 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4859 |
for (Integer v1 : new Integer[] { 1, null }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4860 |
{ |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4861 |
final AtomicInteger ran = new AtomicInteger(0); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4862 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4863 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4864 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4865 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4866 |
final CompletionStage<Integer> g = d.exceptionallyAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4867 |
((Throwable t) -> { |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4868 |
ran.getAndIncrement(); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4869 |
throw new AssertionError("should not be called"); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4870 |
}); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4871 |
if (createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4872 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4873 |
checkCompletedNormally(g.toCompletableFuture(), v1); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4874 |
checkCompletedNormally(f, v1); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4875 |
assertEquals(0, ran.get()); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4876 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4877 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4878 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4879 |
* default-implemented exceptionallyAsync action completes with |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4880 |
* function value on source exception |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4881 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4882 |
public void testDefaultExceptionallyAsync_exceptionalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4883 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4884 |
for (Integer v1 : new Integer[] { 1, null }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4885 |
{ |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4886 |
final AtomicInteger ran = new AtomicInteger(0); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4887 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4888 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4889 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4890 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4891 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4892 |
final CompletionStage<Integer> g = d.exceptionallyAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4893 |
((Throwable t) -> { |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4894 |
assertSame(t, ex); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4895 |
ran.getAndIncrement(); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4896 |
return v1; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4897 |
}); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4898 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4899 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4900 |
checkCompletedNormally(g.toCompletableFuture(), v1); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4901 |
checkCompletedExceptionally(f, ex); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4902 |
assertEquals(1, ran.get()); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4903 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4904 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4905 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4906 |
* Under default implementation, if an "exceptionally action" |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4907 |
* throws an exception, it completes exceptionally with that |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4908 |
* exception |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4909 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4910 |
public void testDefaultExceptionallyAsync_exceptionalCompletionActionFailed() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4911 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4912 |
{ |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4913 |
final AtomicInteger ran = new AtomicInteger(0); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4914 |
final CFException ex1 = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4915 |
final CFException ex2 = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4916 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4917 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4918 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4919 |
if (!createIncomplete) f.completeExceptionally(ex1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4920 |
final CompletionStage<Integer> g = d.exceptionallyAsync |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4921 |
((Throwable t) -> { |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4922 |
assertSame(t, ex1); |
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4923 |
ran.getAndIncrement(); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4924 |
throw ex2; |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4925 |
}); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4926 |
if (createIncomplete) f.completeExceptionally(ex1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4927 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4928 |
checkCompletedWithWrappedException(g.toCompletableFuture(), ex2); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4929 |
checkCompletedExceptionally(f, ex1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4930 |
checkCompletedExceptionally(d.toCompletableFuture(), ex1); |
52730
345266000aba
8211283: Miscellaneous changes imported from jsr166 CVS 2018-11
dl
parents:
51950
diff
changeset
|
4931 |
assertEquals(1, ran.get()); |
51950
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4932 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4933 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4934 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4935 |
* default-implemented exceptionallyCompose result completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4936 |
* normally after normal completion of source |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4937 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4938 |
public void testDefaultExceptionallyCompose_normalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4939 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4940 |
for (Integer v1 : new Integer[] { 1, null }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4941 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4942 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4943 |
final ExceptionalCompletableFutureFunction r = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4944 |
new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4945 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4946 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4947 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4948 |
final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4949 |
if (createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4950 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4951 |
checkCompletedNormally(f, v1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4952 |
checkCompletedNormally(g.toCompletableFuture(), v1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4953 |
r.assertNotInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4954 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4955 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4956 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4957 |
* default-implemented exceptionallyCompose result completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4958 |
* normally after exceptional completion of source |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4959 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4960 |
public void testDefaultExceptionallyCompose_exceptionalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4961 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4962 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4963 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4964 |
final ExceptionalCompletableFutureFunction r = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4965 |
new ExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4966 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4967 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4968 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4969 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4970 |
final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4971 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4972 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4973 |
checkCompletedExceptionally(f, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4974 |
checkCompletedNormally(g.toCompletableFuture(), r.value); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4975 |
r.assertInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4976 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4977 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4978 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4979 |
* default-implemented exceptionallyCompose completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4980 |
* exceptionally on exception if action does |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4981 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4982 |
public void testDefaultExceptionallyCompose_actionFailed() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4983 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4984 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4985 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4986 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4987 |
final FailingExceptionalCompletableFutureFunction r |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4988 |
= new FailingExceptionalCompletableFutureFunction(ExecutionMode.SYNC); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4989 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4990 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4991 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4992 |
final CompletionStage<Integer> g = d.exceptionallyCompose(r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4993 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4994 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4995 |
checkCompletedExceptionally(f, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4996 |
checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4997 |
r.assertInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4998 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
4999 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5000 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5001 |
* default-implemented exceptionallyComposeAsync result completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5002 |
* normally after normal completion of source |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5003 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5004 |
public void testDefaultExceptionallyComposeAsync_normalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5005 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5006 |
for (Integer v1 : new Integer[] { 1, null }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5007 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5008 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5009 |
final ExceptionalCompletableFutureFunction r = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5010 |
new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5011 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5012 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5013 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5014 |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5015 |
if (createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5016 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5017 |
checkCompletedNormally(f, v1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5018 |
checkCompletedNormally(g.toCompletableFuture(), v1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5019 |
r.assertNotInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5020 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5021 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5022 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5023 |
* default-implemented exceptionallyComposeAsync result completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5024 |
* normally after exceptional completion of source |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5025 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5026 |
public void testDefaultExceptionallyComposeAsync_exceptionalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5027 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5028 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5029 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5030 |
final ExceptionalCompletableFutureFunction r = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5031 |
new ExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5032 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5033 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5034 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5035 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5036 |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5037 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5038 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5039 |
checkCompletedExceptionally(f, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5040 |
checkCompletedNormally(g.toCompletableFuture(), r.value); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5041 |
r.assertInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5042 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5043 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5044 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5045 |
* default-implemented exceptionallyComposeAsync completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5046 |
* exceptionally on exception if action does |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5047 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5048 |
public void testDefaultExceptionallyComposeAsync_actionFailed() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5049 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5050 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5051 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5052 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5053 |
final FailingExceptionalCompletableFutureFunction r |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5054 |
= new FailingExceptionalCompletableFutureFunction(ExecutionMode.ASYNC); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5055 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5056 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5057 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5058 |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5059 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5060 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5061 |
checkCompletedExceptionally(f, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5062 |
checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5063 |
r.assertInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5064 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5065 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5066 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5067 |
* default-implemented exceptionallyComposeAsync result completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5068 |
* normally after normal completion of source |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5069 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5070 |
public void testDefaultExceptionallyComposeAsyncExecutor_normalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5071 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5072 |
for (Integer v1 : new Integer[] { 1, null }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5073 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5074 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5075 |
final ExceptionalCompletableFutureFunction r = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5076 |
new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5077 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5078 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5079 |
if (!createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5080 |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5081 |
if (createIncomplete) assertTrue(f.complete(v1)); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5082 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5083 |
checkCompletedNormally(f, v1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5084 |
checkCompletedNormally(g.toCompletableFuture(), v1); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5085 |
r.assertNotInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5086 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5087 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5088 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5089 |
* default-implemented exceptionallyComposeAsync result completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5090 |
* normally after exceptional completion of source |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5091 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5092 |
public void testDefaultExceptionallyComposeAsyncExecutor_exceptionalCompletion() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5093 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5094 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5095 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5096 |
final ExceptionalCompletableFutureFunction r = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5097 |
new ExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5098 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5099 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5100 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5101 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5102 |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5103 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5104 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5105 |
checkCompletedExceptionally(f, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5106 |
checkCompletedNormally(g.toCompletableFuture(), r.value); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5107 |
r.assertInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5108 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5109 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5110 |
/** |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5111 |
* default-implemented exceptionallyComposeAsync completes |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5112 |
* exceptionally on exception if action does |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5113 |
*/ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5114 |
public void testDefaultExceptionallyComposeAsyncExecutor_actionFailed() { |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5115 |
for (boolean createIncomplete : new boolean[] { true, false }) |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5116 |
{ |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5117 |
final CFException ex = new CFException(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5118 |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5119 |
final FailingExceptionalCompletableFutureFunction r |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5120 |
= new FailingExceptionalCompletableFutureFunction(ExecutionMode.EXECUTOR); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5121 |
final DelegatedCompletionStage<Integer> d = |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5122 |
new DelegatedCompletionStage<Integer>(f); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5123 |
if (!createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5124 |
final CompletionStage<Integer> g = d.exceptionallyComposeAsync(r, new ThreadExecutor()); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5125 |
if (createIncomplete) f.completeExceptionally(ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5126 |
|
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5127 |
checkCompletedExceptionally(f, ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5128 |
checkCompletedWithWrappedException(g.toCompletableFuture(), r.ex); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5129 |
r.assertInvoked(); |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5130 |
}} |
a1c24d06e2b5
8210971: Add exception handling methods to CompletionStage and CompletableFuture
dl
parents:
50764
diff
changeset
|
5131 |
|
35394 | 5132 |
} |