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