5 |
|
* http://creativecommons.org/publicdomain/zero/1.0/ |
6 |
|
*/ |
7 |
|
|
8 |
< |
import junit.framework.*; |
8 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
9 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
10 |
> |
|
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.List; |
13 |
> |
import java.util.Objects; |
14 |
|
import java.util.concurrent.Callable; |
10 |
– |
import java.util.concurrent.Executor; |
11 |
– |
import java.util.concurrent.ExecutorService; |
12 |
– |
import java.util.concurrent.Executors; |
15 |
|
import java.util.concurrent.CancellationException; |
14 |
– |
import java.util.concurrent.CountDownLatch; |
15 |
– |
import java.util.concurrent.ExecutionException; |
16 |
– |
import java.util.concurrent.Future; |
16 |
|
import java.util.concurrent.CompletableFuture; |
17 |
|
import java.util.concurrent.CompletionException; |
18 |
+ |
import java.util.concurrent.CompletionStage; |
19 |
+ |
import java.util.concurrent.ExecutionException; |
20 |
+ |
import java.util.concurrent.Executor; |
21 |
+ |
import java.util.concurrent.ForkJoinPool; |
22 |
+ |
import java.util.concurrent.ForkJoinTask; |
23 |
|
import java.util.concurrent.TimeoutException; |
24 |
+ |
import java.util.concurrent.TimeUnit; |
25 |
|
import java.util.concurrent.atomic.AtomicInteger; |
26 |
< |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
22 |
< |
import static java.util.concurrent.TimeUnit.SECONDS; |
23 |
< |
import java.util.*; |
24 |
< |
import java.util.function.Supplier; |
25 |
< |
import java.util.function.Consumer; |
26 |
> |
import java.util.concurrent.atomic.AtomicReference; |
27 |
|
import java.util.function.BiConsumer; |
27 |
– |
import java.util.function.Function; |
28 |
|
import java.util.function.BiFunction; |
29 |
+ |
import java.util.function.Consumer; |
30 |
+ |
import java.util.function.Function; |
31 |
+ |
import java.util.function.Supplier; |
32 |
+ |
|
33 |
+ |
import junit.framework.Test; |
34 |
+ |
import junit.framework.TestSuite; |
35 |
|
|
36 |
|
public class CompletableFutureTest extends JSR166TestCase { |
37 |
|
|
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run(suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
public static Test suite() { |
42 |
|
return new TestSuite(CompletableFutureTest.class); |
47 |
|
void checkIncomplete(CompletableFuture<?> f) { |
48 |
|
assertFalse(f.isDone()); |
49 |
|
assertFalse(f.isCancelled()); |
50 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
50 |
> |
assertTrue(f.toString().contains("Not completed")); |
51 |
|
try { |
52 |
|
assertNull(f.getNow(null)); |
53 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
60 |
|
} |
61 |
|
|
62 |
|
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
63 |
< |
try { |
64 |
< |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
59 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
63 |
> |
checkTimedGet(f, value); |
64 |
> |
|
65 |
|
try { |
66 |
|
assertEquals(value, f.join()); |
67 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
77 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
78 |
|
} |
79 |
|
|
80 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
80 |
> |
/** |
81 |
> |
* Returns the "raw" internal exceptional completion of f, |
82 |
> |
* without any additional wrapping with CompletionException. |
83 |
> |
*/ |
84 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
85 |
> |
// handle (and whenComplete) can distinguish between "direct" |
86 |
> |
// and "wrapped" exceptional completion |
87 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
88 |
> |
} |
89 |
> |
|
90 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
91 |
> |
boolean wrapped, |
92 |
> |
Consumer<Throwable> checker) { |
93 |
> |
Throwable cause = exceptionalCompletion(f); |
94 |
> |
if (wrapped) { |
95 |
> |
assertTrue(cause instanceof CompletionException); |
96 |
> |
cause = cause.getCause(); |
97 |
> |
} |
98 |
> |
checker.accept(cause); |
99 |
> |
|
100 |
> |
long startTime = System.nanoTime(); |
101 |
|
try { |
102 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
103 |
|
shouldThrow(); |
104 |
|
} catch (ExecutionException success) { |
105 |
< |
assertTrue(success.getCause() instanceof CFException); |
105 |
> |
assertSame(cause, success.getCause()); |
106 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
107 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
108 |
+ |
|
109 |
|
try { |
110 |
|
f.join(); |
111 |
|
shouldThrow(); |
112 |
|
} catch (CompletionException success) { |
113 |
< |
assertTrue(success.getCause() instanceof CFException); |
114 |
< |
} |
113 |
> |
assertSame(cause, success.getCause()); |
114 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
> |
|
116 |
|
try { |
117 |
|
f.getNow(null); |
118 |
|
shouldThrow(); |
119 |
|
} catch (CompletionException success) { |
120 |
< |
assertTrue(success.getCause() instanceof CFException); |
121 |
< |
} |
120 |
> |
assertSame(cause, success.getCause()); |
121 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
> |
|
123 |
|
try { |
124 |
|
f.get(); |
125 |
|
shouldThrow(); |
126 |
|
} catch (ExecutionException success) { |
127 |
< |
assertTrue(success.getCause() instanceof CFException); |
127 |
> |
assertSame(cause, success.getCause()); |
128 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
129 |
< |
assertTrue(f.isDone()); |
129 |
> |
|
130 |
|
assertFalse(f.isCancelled()); |
131 |
+ |
assertTrue(f.isDone()); |
132 |
+ |
assertTrue(f.isCompletedExceptionally()); |
133 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
134 |
|
} |
135 |
|
|
136 |
+ |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
137 |
+ |
checkCompletedExceptionally(f, true, |
138 |
+ |
(t) -> assertTrue(t instanceof CFException)); |
139 |
+ |
} |
140 |
+ |
|
141 |
+ |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
142 |
+ |
checkCompletedExceptionally(f, true, |
143 |
+ |
(t) -> assertTrue(t instanceof CancellationException)); |
144 |
+ |
} |
145 |
+ |
|
146 |
+ |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
147 |
+ |
checkCompletedExceptionally(f, false, |
148 |
+ |
(t) -> assertTrue(t instanceof TimeoutException)); |
149 |
+ |
} |
150 |
+ |
|
151 |
+ |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
152 |
+ |
Throwable ex) { |
153 |
+ |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
154 |
+ |
} |
155 |
+ |
|
156 |
+ |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
157 |
+ |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
158 |
+ |
} |
159 |
+ |
|
160 |
|
void checkCancelled(CompletableFuture<?> f) { |
161 |
+ |
long startTime = System.nanoTime(); |
162 |
|
try { |
163 |
|
f.get(LONG_DELAY_MS, MILLISECONDS); |
164 |
|
shouldThrow(); |
165 |
|
} catch (CancellationException success) { |
166 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
167 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
168 |
+ |
|
169 |
|
try { |
170 |
|
f.join(); |
171 |
|
shouldThrow(); |
179 |
|
shouldThrow(); |
180 |
|
} catch (CancellationException success) { |
181 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
124 |
– |
assertTrue(f.isDone()); |
125 |
– |
assertTrue(f.isCompletedExceptionally()); |
126 |
– |
assertTrue(f.isCancelled()); |
127 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
128 |
– |
} |
182 |
|
|
183 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
184 |
< |
try { |
132 |
< |
f.get(LONG_DELAY_MS, MILLISECONDS); |
133 |
< |
shouldThrow(); |
134 |
< |
} catch (ExecutionException success) { |
135 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
136 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
137 |
< |
try { |
138 |
< |
f.join(); |
139 |
< |
shouldThrow(); |
140 |
< |
} catch (CompletionException success) { |
141 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
142 |
< |
} |
143 |
< |
try { |
144 |
< |
f.getNow(null); |
145 |
< |
shouldThrow(); |
146 |
< |
} catch (CompletionException success) { |
147 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
148 |
< |
} |
149 |
< |
try { |
150 |
< |
f.get(); |
151 |
< |
shouldThrow(); |
152 |
< |
} catch (ExecutionException success) { |
153 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
154 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
183 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
184 |
> |
|
185 |
|
assertTrue(f.isDone()); |
156 |
– |
assertFalse(f.isCancelled()); |
186 |
|
assertTrue(f.isCompletedExceptionally()); |
187 |
+ |
assertTrue(f.isCancelled()); |
188 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
189 |
|
} |
190 |
|
|
202 |
|
* isCancelled, join, get, and getNow |
203 |
|
*/ |
204 |
|
public void testComplete() { |
205 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
206 |
+ |
{ |
207 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
208 |
|
checkIncomplete(f); |
209 |
< |
f.complete(one); |
210 |
< |
checkCompletedNormally(f, one); |
211 |
< |
} |
209 |
> |
assertTrue(f.complete(v1)); |
210 |
> |
assertFalse(f.complete(v1)); |
211 |
> |
checkCompletedNormally(f, v1); |
212 |
> |
}} |
213 |
|
|
214 |
|
/** |
215 |
|
* completeExceptionally completes exceptionally, as indicated by |
217 |
|
*/ |
218 |
|
public void testCompleteExceptionally() { |
219 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
220 |
+ |
CFException ex = new CFException(); |
221 |
|
checkIncomplete(f); |
222 |
< |
f.completeExceptionally(new CFException()); |
223 |
< |
checkCompletedWithWrappedCFException(f); |
222 |
> |
f.completeExceptionally(ex); |
223 |
> |
checkCompletedExceptionally(f, ex); |
224 |
|
} |
225 |
|
|
226 |
|
/** |
228 |
|
* methods isDone, isCancelled, join, get, and getNow |
229 |
|
*/ |
230 |
|
public void testCancel() { |
231 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
232 |
+ |
{ |
233 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
234 |
|
checkIncomplete(f); |
235 |
< |
assertTrue(f.cancel(true)); |
235 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
236 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
237 |
> |
assertTrue(f.cancel(!mayInterruptIfRunning)); |
238 |
|
checkCancelled(f); |
239 |
< |
} |
239 |
> |
}} |
240 |
|
|
241 |
|
/** |
242 |
|
* obtrudeValue forces completion with given value |
244 |
|
public void testObtrudeValue() { |
245 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
246 |
|
checkIncomplete(f); |
247 |
< |
f.complete(one); |
247 |
> |
assertTrue(f.complete(one)); |
248 |
|
checkCompletedNormally(f, one); |
249 |
|
f.obtrudeValue(three); |
250 |
|
checkCompletedNormally(f, three); |
253 |
|
f = new CompletableFuture<>(); |
254 |
|
f.obtrudeValue(three); |
255 |
|
checkCompletedNormally(f, three); |
256 |
+ |
f.obtrudeValue(null); |
257 |
+ |
checkCompletedNormally(f, null); |
258 |
|
f = new CompletableFuture<>(); |
259 |
|
f.completeExceptionally(new CFException()); |
260 |
|
f.obtrudeValue(four); |
265 |
|
* obtrudeException forces completion with given exception |
266 |
|
*/ |
267 |
|
public void testObtrudeException() { |
268 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
269 |
< |
checkIncomplete(f); |
270 |
< |
f.complete(one); |
271 |
< |
checkCompletedNormally(f, one); |
272 |
< |
f.obtrudeException(new CFException()); |
233 |
< |
checkCompletedWithWrappedCFException(f); |
268 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
269 |
> |
{ |
270 |
> |
CFException ex; |
271 |
> |
CompletableFuture<Integer> f; |
272 |
> |
|
273 |
|
f = new CompletableFuture<>(); |
274 |
< |
f.obtrudeException(new CFException()); |
275 |
< |
checkCompletedWithWrappedCFException(f); |
274 |
> |
assertTrue(f.complete(v1)); |
275 |
> |
for (int i = 0; i < 2; i++) { |
276 |
> |
f.obtrudeException(ex = new CFException()); |
277 |
> |
checkCompletedExceptionally(f, ex); |
278 |
> |
} |
279 |
> |
|
280 |
|
f = new CompletableFuture<>(); |
281 |
< |
f.completeExceptionally(new CFException()); |
282 |
< |
f.obtrudeValue(four); |
283 |
< |
checkCompletedNormally(f, four); |
284 |
< |
f.obtrudeException(new CFException()); |
285 |
< |
checkCompletedWithWrappedCFException(f); |
286 |
< |
} |
281 |
> |
for (int i = 0; i < 2; i++) { |
282 |
> |
f.obtrudeException(ex = new CFException()); |
283 |
> |
checkCompletedExceptionally(f, ex); |
284 |
> |
} |
285 |
> |
|
286 |
> |
f = new CompletableFuture<>(); |
287 |
> |
f.completeExceptionally(ex = new CFException()); |
288 |
> |
f.obtrudeValue(v1); |
289 |
> |
checkCompletedNormally(f, v1); |
290 |
> |
f.obtrudeException(ex = new CFException()); |
291 |
> |
checkCompletedExceptionally(f, ex); |
292 |
> |
f.completeExceptionally(new CFException()); |
293 |
> |
checkCompletedExceptionally(f, ex); |
294 |
> |
assertFalse(f.complete(v1)); |
295 |
> |
checkCompletedExceptionally(f, ex); |
296 |
> |
}} |
297 |
|
|
298 |
|
/** |
299 |
|
* getNumberOfDependents returns number of dependent tasks |
300 |
|
*/ |
301 |
|
public void testGetNumberOfDependents() { |
302 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
303 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
304 |
< |
CompletableFuture g = f.thenRun(new Noop()); |
305 |
< |
assertEquals(f.getNumberOfDependents(), 1); |
306 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
307 |
< |
CompletableFuture h = f.thenRun(new Noop()); |
308 |
< |
assertEquals(f.getNumberOfDependents(), 2); |
309 |
< |
f.complete(1); |
302 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
303 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
304 |
> |
{ |
305 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
306 |
> |
assertEquals(0, f.getNumberOfDependents()); |
307 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
308 |
> |
assertEquals(1, f.getNumberOfDependents()); |
309 |
> |
assertEquals(0, g.getNumberOfDependents()); |
310 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
311 |
> |
assertEquals(2, f.getNumberOfDependents()); |
312 |
> |
assertEquals(0, h.getNumberOfDependents()); |
313 |
> |
assertTrue(f.complete(v1)); |
314 |
|
checkCompletedNormally(g, null); |
315 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
316 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
317 |
< |
} |
315 |
> |
checkCompletedNormally(h, null); |
316 |
> |
assertEquals(0, f.getNumberOfDependents()); |
317 |
> |
assertEquals(0, g.getNumberOfDependents()); |
318 |
> |
assertEquals(0, h.getNumberOfDependents()); |
319 |
> |
}} |
320 |
|
|
321 |
|
/** |
322 |
|
* toString indicates current completion state |
327 |
|
f = new CompletableFuture<String>(); |
328 |
|
assertTrue(f.toString().contains("[Not completed]")); |
329 |
|
|
330 |
< |
f.complete("foo"); |
330 |
> |
assertTrue(f.complete("foo")); |
331 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
332 |
|
|
333 |
|
f = new CompletableFuture<String>(); |
334 |
< |
f.completeExceptionally(new IndexOutOfBoundsException()); |
334 |
> |
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
335 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
336 |
+ |
|
337 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
338 |
+ |
f = new CompletableFuture<String>(); |
339 |
+ |
assertTrue(f.cancel(mayInterruptIfRunning)); |
340 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
341 |
+ |
} |
342 |
|
} |
343 |
|
|
344 |
|
/** |
349 |
|
checkCompletedNormally(f, "test"); |
350 |
|
} |
351 |
|
|
352 |
< |
// Choose non-commutative actions for better coverage |
352 |
> |
abstract class CheckedAction { |
353 |
> |
int invocationCount = 0; |
354 |
> |
final ExecutionMode m; |
355 |
> |
CheckedAction(ExecutionMode m) { this.m = m; } |
356 |
> |
void invoked() { |
357 |
> |
m.checkExecutionMode(); |
358 |
> |
assertEquals(0, invocationCount++); |
359 |
> |
} |
360 |
> |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
361 |
> |
void assertInvoked() { assertEquals(1, invocationCount); } |
362 |
> |
} |
363 |
> |
|
364 |
> |
abstract class CheckedIntegerAction extends CheckedAction { |
365 |
> |
Integer value; |
366 |
> |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
367 |
> |
void assertValue(Integer expected) { |
368 |
> |
assertInvoked(); |
369 |
> |
assertEquals(expected, value); |
370 |
> |
} |
371 |
> |
} |
372 |
> |
|
373 |
> |
class IntegerSupplier extends CheckedAction |
374 |
> |
implements Supplier<Integer> |
375 |
> |
{ |
376 |
> |
final Integer value; |
377 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
378 |
> |
super(m); |
379 |
> |
this.value = value; |
380 |
> |
} |
381 |
> |
public Integer get() { |
382 |
> |
invoked(); |
383 |
> |
return value; |
384 |
> |
} |
385 |
> |
} |
386 |
> |
|
387 |
> |
// A function that handles and produces null values as well. |
388 |
> |
static Integer inc(Integer x) { |
389 |
> |
return (x == null) ? null : x + 1; |
390 |
> |
} |
391 |
> |
|
392 |
> |
class NoopConsumer extends CheckedIntegerAction |
393 |
> |
implements Consumer<Integer> |
394 |
> |
{ |
395 |
> |
NoopConsumer(ExecutionMode m) { super(m); } |
396 |
> |
public void accept(Integer x) { |
397 |
> |
invoked(); |
398 |
> |
value = x; |
399 |
> |
} |
400 |
> |
} |
401 |
|
|
402 |
< |
static final Supplier<Integer> supplyOne = |
403 |
< |
() -> Integer.valueOf(1); |
404 |
< |
static final Function<Integer, Integer> inc = |
405 |
< |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
406 |
< |
static final BiFunction<Integer, Integer, Integer> subtract = |
407 |
< |
(Integer x, Integer y) -> Integer.valueOf(x.intValue() - y.intValue()); |
408 |
< |
static final class IncAction implements Consumer<Integer> { |
409 |
< |
int value; |
410 |
< |
public void accept(Integer x) { value = x.intValue() + 1; } |
402 |
> |
class IncFunction extends CheckedIntegerAction |
403 |
> |
implements Function<Integer,Integer> |
404 |
> |
{ |
405 |
> |
IncFunction(ExecutionMode m) { super(m); } |
406 |
> |
public Integer apply(Integer x) { |
407 |
> |
invoked(); |
408 |
> |
return value = inc(x); |
409 |
> |
} |
410 |
> |
} |
411 |
> |
|
412 |
> |
// Choose non-commutative actions for better coverage |
413 |
> |
// A non-commutative function that handles and produces null values as well. |
414 |
> |
static Integer subtract(Integer x, Integer y) { |
415 |
> |
return (x == null && y == null) ? null : |
416 |
> |
((x == null) ? 42 : x.intValue()) |
417 |
> |
- ((y == null) ? 99 : y.intValue()); |
418 |
|
} |
419 |
< |
static final class SubtractAction implements BiConsumer<Integer, Integer> { |
420 |
< |
int value; |
419 |
> |
|
420 |
> |
class SubtractAction extends CheckedIntegerAction |
421 |
> |
implements BiConsumer<Integer, Integer> |
422 |
> |
{ |
423 |
> |
SubtractAction(ExecutionMode m) { super(m); } |
424 |
|
public void accept(Integer x, Integer y) { |
425 |
< |
value = x.intValue() - y.intValue(); |
425 |
> |
invoked(); |
426 |
> |
value = subtract(x, y); |
427 |
|
} |
428 |
|
} |
429 |
< |
static final class Noop implements Runnable { |
430 |
< |
boolean ran; |
431 |
< |
public void run() { ran = true; } |
429 |
> |
|
430 |
> |
class SubtractFunction extends CheckedIntegerAction |
431 |
> |
implements BiFunction<Integer, Integer, Integer> |
432 |
> |
{ |
433 |
> |
SubtractFunction(ExecutionMode m) { super(m); } |
434 |
> |
public Integer apply(Integer x, Integer y) { |
435 |
> |
invoked(); |
436 |
> |
return value = subtract(x, y); |
437 |
> |
} |
438 |
|
} |
439 |
|
|
440 |
< |
static final class FailingSupplier implements Supplier<Integer> { |
441 |
< |
boolean ran; |
442 |
< |
public Integer get() { ran = true; throw new CFException(); } |
440 |
> |
class Noop extends CheckedAction implements Runnable { |
441 |
> |
Noop(ExecutionMode m) { super(m); } |
442 |
> |
public void run() { |
443 |
> |
invoked(); |
444 |
> |
} |
445 |
|
} |
446 |
< |
static final class FailingConsumer implements Consumer<Integer> { |
447 |
< |
boolean ran; |
448 |
< |
public void accept(Integer x) { ran = true; throw new CFException(); } |
446 |
> |
|
447 |
> |
class FailingSupplier extends CheckedAction |
448 |
> |
implements Supplier<Integer> |
449 |
> |
{ |
450 |
> |
FailingSupplier(ExecutionMode m) { super(m); } |
451 |
> |
public Integer get() { |
452 |
> |
invoked(); |
453 |
> |
throw new CFException(); |
454 |
> |
} |
455 |
|
} |
456 |
< |
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
457 |
< |
boolean ran; |
458 |
< |
public void accept(Integer x, Integer y) { ran = true; throw new CFException(); } |
456 |
> |
|
457 |
> |
class FailingConsumer extends CheckedIntegerAction |
458 |
> |
implements Consumer<Integer> |
459 |
> |
{ |
460 |
> |
FailingConsumer(ExecutionMode m) { super(m); } |
461 |
> |
public void accept(Integer x) { |
462 |
> |
invoked(); |
463 |
> |
value = x; |
464 |
> |
throw new CFException(); |
465 |
> |
} |
466 |
|
} |
467 |
< |
static final class FailingFunction implements Function<Integer, Integer> { |
468 |
< |
boolean ran; |
469 |
< |
public Integer apply(Integer x) { ran = true; throw new CFException(); } |
467 |
> |
|
468 |
> |
class FailingBiConsumer extends CheckedIntegerAction |
469 |
> |
implements BiConsumer<Integer, Integer> |
470 |
> |
{ |
471 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); } |
472 |
> |
public void accept(Integer x, Integer y) { |
473 |
> |
invoked(); |
474 |
> |
value = subtract(x, y); |
475 |
> |
throw new CFException(); |
476 |
> |
} |
477 |
> |
} |
478 |
> |
|
479 |
> |
class FailingFunction extends CheckedIntegerAction |
480 |
> |
implements Function<Integer, Integer> |
481 |
> |
{ |
482 |
> |
FailingFunction(ExecutionMode m) { super(m); } |
483 |
> |
public Integer apply(Integer x) { |
484 |
> |
invoked(); |
485 |
> |
value = x; |
486 |
> |
throw new CFException(); |
487 |
> |
} |
488 |
|
} |
489 |
< |
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
490 |
< |
boolean ran; |
491 |
< |
public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); } |
489 |
> |
|
490 |
> |
class FailingBiFunction extends CheckedIntegerAction |
491 |
> |
implements BiFunction<Integer, Integer, Integer> |
492 |
> |
{ |
493 |
> |
FailingBiFunction(ExecutionMode m) { super(m); } |
494 |
> |
public Integer apply(Integer x, Integer y) { |
495 |
> |
invoked(); |
496 |
> |
value = subtract(x, y); |
497 |
> |
throw new CFException(); |
498 |
> |
} |
499 |
|
} |
500 |
< |
static final class FailingNoop implements Runnable { |
501 |
< |
boolean ran; |
502 |
< |
public void run() { ran = true; throw new CFException(); } |
500 |
> |
|
501 |
> |
class FailingRunnable extends CheckedAction implements Runnable { |
502 |
> |
FailingRunnable(ExecutionMode m) { super(m); } |
503 |
> |
public void run() { |
504 |
> |
invoked(); |
505 |
> |
throw new CFException(); |
506 |
> |
} |
507 |
|
} |
508 |
|
|
509 |
< |
static final class CompletableFutureInc |
510 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
511 |
< |
boolean ran; |
509 |
> |
class CompletableFutureInc extends CheckedIntegerAction |
510 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
511 |
> |
{ |
512 |
> |
CompletableFutureInc(ExecutionMode m) { super(m); } |
513 |
|
public CompletableFuture<Integer> apply(Integer x) { |
514 |
< |
ran = true; |
514 |
> |
invoked(); |
515 |
> |
value = x; |
516 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
517 |
< |
f.complete(Integer.valueOf(x.intValue() + 1)); |
517 |
> |
assertTrue(f.complete(inc(x))); |
518 |
|
return f; |
519 |
|
} |
520 |
|
} |
521 |
|
|
522 |
< |
static final class FailingCompletableFutureFunction |
523 |
< |
implements Function<Integer, CompletableFuture<Integer>> { |
524 |
< |
boolean ran; |
522 |
> |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
523 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
524 |
> |
{ |
525 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
526 |
|
public CompletableFuture<Integer> apply(Integer x) { |
527 |
< |
ran = true; throw new CFException(); |
527 |
> |
invoked(); |
528 |
> |
value = x; |
529 |
> |
throw new CFException(); |
530 |
|
} |
531 |
|
} |
532 |
|
|
533 |
|
// Used for explicit executor tests |
534 |
|
static final class ThreadExecutor implements Executor { |
535 |
< |
AtomicInteger count = new AtomicInteger(0); |
535 |
> |
final AtomicInteger count = new AtomicInteger(0); |
536 |
> |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
537 |
> |
static boolean startedCurrentThread() { |
538 |
> |
return Thread.currentThread().getThreadGroup() == tg; |
539 |
> |
} |
540 |
|
|
541 |
|
public void execute(Runnable r) { |
542 |
|
count.getAndIncrement(); |
543 |
< |
new Thread(r).start(); |
543 |
> |
new Thread(tg, r).start(); |
544 |
|
} |
545 |
|
} |
546 |
|
|
547 |
< |
static final class ExceptionToInteger implements Function<Throwable, Integer> { |
548 |
< |
public Integer apply(Throwable x) { return Integer.valueOf(3); } |
366 |
< |
} |
547 |
> |
static final boolean defaultExecutorIsCommonPool |
548 |
> |
= ForkJoinPool.getCommonPoolParallelism() > 1; |
549 |
|
|
550 |
< |
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
551 |
< |
boolean ran; |
552 |
< |
public Integer apply(Integer x, Throwable t) { |
553 |
< |
ran = true; |
554 |
< |
return (t == null) ? two : three; |
555 |
< |
} |
556 |
< |
} |
550 |
> |
/** |
551 |
> |
* Permits the testing of parallel code for the 3 different |
552 |
> |
* execution modes without copy/pasting all the test methods. |
553 |
> |
*/ |
554 |
> |
enum ExecutionMode { |
555 |
> |
SYNC { |
556 |
> |
public void checkExecutionMode() { |
557 |
> |
assertFalse(ThreadExecutor.startedCurrentThread()); |
558 |
> |
assertNull(ForkJoinTask.getPool()); |
559 |
> |
} |
560 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
561 |
> |
throw new UnsupportedOperationException(); |
562 |
> |
} |
563 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
564 |
> |
throw new UnsupportedOperationException(); |
565 |
> |
} |
566 |
> |
public <T> CompletableFuture<Void> thenRun |
567 |
> |
(CompletableFuture<T> f, Runnable a) { |
568 |
> |
return f.thenRun(a); |
569 |
> |
} |
570 |
> |
public <T> CompletableFuture<Void> thenAccept |
571 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
572 |
> |
return f.thenAccept(a); |
573 |
> |
} |
574 |
> |
public <T,U> CompletableFuture<U> thenApply |
575 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
576 |
> |
return f.thenApply(a); |
577 |
> |
} |
578 |
> |
public <T,U> CompletableFuture<U> thenCompose |
579 |
> |
(CompletableFuture<T> f, |
580 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
581 |
> |
return f.thenCompose(a); |
582 |
> |
} |
583 |
> |
public <T,U> CompletableFuture<U> handle |
584 |
> |
(CompletableFuture<T> f, |
585 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
586 |
> |
return f.handle(a); |
587 |
> |
} |
588 |
> |
public <T> CompletableFuture<T> whenComplete |
589 |
> |
(CompletableFuture<T> f, |
590 |
> |
BiConsumer<? super T,? super Throwable> a) { |
591 |
> |
return f.whenComplete(a); |
592 |
> |
} |
593 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
594 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
595 |
> |
return f.runAfterBoth(g, a); |
596 |
> |
} |
597 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
598 |
> |
(CompletableFuture<T> f, |
599 |
> |
CompletionStage<? extends U> g, |
600 |
> |
BiConsumer<? super T,? super U> a) { |
601 |
> |
return f.thenAcceptBoth(g, a); |
602 |
> |
} |
603 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
604 |
> |
(CompletableFuture<T> f, |
605 |
> |
CompletionStage<? extends U> g, |
606 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
607 |
> |
return f.thenCombine(g, a); |
608 |
> |
} |
609 |
> |
public <T> CompletableFuture<Void> runAfterEither |
610 |
> |
(CompletableFuture<T> f, |
611 |
> |
CompletionStage<?> g, |
612 |
> |
java.lang.Runnable a) { |
613 |
> |
return f.runAfterEither(g, a); |
614 |
> |
} |
615 |
> |
public <T> CompletableFuture<Void> acceptEither |
616 |
> |
(CompletableFuture<T> f, |
617 |
> |
CompletionStage<? extends T> g, |
618 |
> |
Consumer<? super T> a) { |
619 |
> |
return f.acceptEither(g, a); |
620 |
> |
} |
621 |
> |
public <T,U> CompletableFuture<U> applyToEither |
622 |
> |
(CompletableFuture<T> f, |
623 |
> |
CompletionStage<? extends T> g, |
624 |
> |
Function<? super T,U> a) { |
625 |
> |
return f.applyToEither(g, a); |
626 |
> |
} |
627 |
> |
}, |
628 |
> |
|
629 |
> |
ASYNC { |
630 |
> |
public void checkExecutionMode() { |
631 |
> |
assertEquals(defaultExecutorIsCommonPool, |
632 |
> |
(ForkJoinPool.commonPool() == ForkJoinTask.getPool())); |
633 |
> |
} |
634 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
635 |
> |
return CompletableFuture.runAsync(a); |
636 |
> |
} |
637 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
638 |
> |
return CompletableFuture.supplyAsync(a); |
639 |
> |
} |
640 |
> |
public <T> CompletableFuture<Void> thenRun |
641 |
> |
(CompletableFuture<T> f, Runnable a) { |
642 |
> |
return f.thenRunAsync(a); |
643 |
> |
} |
644 |
> |
public <T> CompletableFuture<Void> thenAccept |
645 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
646 |
> |
return f.thenAcceptAsync(a); |
647 |
> |
} |
648 |
> |
public <T,U> CompletableFuture<U> thenApply |
649 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
650 |
> |
return f.thenApplyAsync(a); |
651 |
> |
} |
652 |
> |
public <T,U> CompletableFuture<U> thenCompose |
653 |
> |
(CompletableFuture<T> f, |
654 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
655 |
> |
return f.thenComposeAsync(a); |
656 |
> |
} |
657 |
> |
public <T,U> CompletableFuture<U> handle |
658 |
> |
(CompletableFuture<T> f, |
659 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
660 |
> |
return f.handleAsync(a); |
661 |
> |
} |
662 |
> |
public <T> CompletableFuture<T> whenComplete |
663 |
> |
(CompletableFuture<T> f, |
664 |
> |
BiConsumer<? super T,? super Throwable> a) { |
665 |
> |
return f.whenCompleteAsync(a); |
666 |
> |
} |
667 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
668 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
669 |
> |
return f.runAfterBothAsync(g, a); |
670 |
> |
} |
671 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
672 |
> |
(CompletableFuture<T> f, |
673 |
> |
CompletionStage<? extends U> g, |
674 |
> |
BiConsumer<? super T,? super U> a) { |
675 |
> |
return f.thenAcceptBothAsync(g, a); |
676 |
> |
} |
677 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
678 |
> |
(CompletableFuture<T> f, |
679 |
> |
CompletionStage<? extends U> g, |
680 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
681 |
> |
return f.thenCombineAsync(g, a); |
682 |
> |
} |
683 |
> |
public <T> CompletableFuture<Void> runAfterEither |
684 |
> |
(CompletableFuture<T> f, |
685 |
> |
CompletionStage<?> g, |
686 |
> |
java.lang.Runnable a) { |
687 |
> |
return f.runAfterEitherAsync(g, a); |
688 |
> |
} |
689 |
> |
public <T> CompletableFuture<Void> acceptEither |
690 |
> |
(CompletableFuture<T> f, |
691 |
> |
CompletionStage<? extends T> g, |
692 |
> |
Consumer<? super T> a) { |
693 |
> |
return f.acceptEitherAsync(g, a); |
694 |
> |
} |
695 |
> |
public <T,U> CompletableFuture<U> applyToEither |
696 |
> |
(CompletableFuture<T> f, |
697 |
> |
CompletionStage<? extends T> g, |
698 |
> |
Function<? super T,U> a) { |
699 |
> |
return f.applyToEitherAsync(g, a); |
700 |
> |
} |
701 |
> |
}, |
702 |
> |
|
703 |
> |
EXECUTOR { |
704 |
> |
public void checkExecutionMode() { |
705 |
> |
assertTrue(ThreadExecutor.startedCurrentThread()); |
706 |
> |
} |
707 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
708 |
> |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
709 |
> |
} |
710 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
711 |
> |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
712 |
> |
} |
713 |
> |
public <T> CompletableFuture<Void> thenRun |
714 |
> |
(CompletableFuture<T> f, Runnable a) { |
715 |
> |
return f.thenRunAsync(a, new ThreadExecutor()); |
716 |
> |
} |
717 |
> |
public <T> CompletableFuture<Void> thenAccept |
718 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
719 |
> |
return f.thenAcceptAsync(a, new ThreadExecutor()); |
720 |
> |
} |
721 |
> |
public <T,U> CompletableFuture<U> thenApply |
722 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
723 |
> |
return f.thenApplyAsync(a, new ThreadExecutor()); |
724 |
> |
} |
725 |
> |
public <T,U> CompletableFuture<U> thenCompose |
726 |
> |
(CompletableFuture<T> f, |
727 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
728 |
> |
return f.thenComposeAsync(a, new ThreadExecutor()); |
729 |
> |
} |
730 |
> |
public <T,U> CompletableFuture<U> handle |
731 |
> |
(CompletableFuture<T> f, |
732 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
733 |
> |
return f.handleAsync(a, new ThreadExecutor()); |
734 |
> |
} |
735 |
> |
public <T> CompletableFuture<T> whenComplete |
736 |
> |
(CompletableFuture<T> f, |
737 |
> |
BiConsumer<? super T,? super Throwable> a) { |
738 |
> |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
739 |
> |
} |
740 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
741 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
742 |
> |
return f.runAfterBothAsync(g, a, new ThreadExecutor()); |
743 |
> |
} |
744 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
745 |
> |
(CompletableFuture<T> f, |
746 |
> |
CompletionStage<? extends U> g, |
747 |
> |
BiConsumer<? super T,? super U> a) { |
748 |
> |
return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); |
749 |
> |
} |
750 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
751 |
> |
(CompletableFuture<T> f, |
752 |
> |
CompletionStage<? extends U> g, |
753 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
754 |
> |
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
755 |
> |
} |
756 |
> |
public <T> CompletableFuture<Void> runAfterEither |
757 |
> |
(CompletableFuture<T> f, |
758 |
> |
CompletionStage<?> g, |
759 |
> |
java.lang.Runnable a) { |
760 |
> |
return f.runAfterEitherAsync(g, a, new ThreadExecutor()); |
761 |
> |
} |
762 |
> |
public <T> CompletableFuture<Void> acceptEither |
763 |
> |
(CompletableFuture<T> f, |
764 |
> |
CompletionStage<? extends T> g, |
765 |
> |
Consumer<? super T> a) { |
766 |
> |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
767 |
> |
} |
768 |
> |
public <T,U> CompletableFuture<U> applyToEither |
769 |
> |
(CompletableFuture<T> f, |
770 |
> |
CompletionStage<? extends T> g, |
771 |
> |
Function<? super T,U> a) { |
772 |
> |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
773 |
> |
} |
774 |
> |
}; |
775 |
> |
|
776 |
> |
public abstract void checkExecutionMode(); |
777 |
> |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
778 |
> |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
779 |
> |
public abstract <T> CompletableFuture<Void> thenRun |
780 |
> |
(CompletableFuture<T> f, Runnable a); |
781 |
> |
public abstract <T> CompletableFuture<Void> thenAccept |
782 |
> |
(CompletableFuture<T> f, Consumer<? super T> a); |
783 |
> |
public abstract <T,U> CompletableFuture<U> thenApply |
784 |
> |
(CompletableFuture<T> f, Function<? super T,U> a); |
785 |
> |
public abstract <T,U> CompletableFuture<U> thenCompose |
786 |
> |
(CompletableFuture<T> f, |
787 |
> |
Function<? super T,? extends CompletionStage<U>> a); |
788 |
> |
public abstract <T,U> CompletableFuture<U> handle |
789 |
> |
(CompletableFuture<T> f, |
790 |
> |
BiFunction<? super T,Throwable,? extends U> a); |
791 |
> |
public abstract <T> CompletableFuture<T> whenComplete |
792 |
> |
(CompletableFuture<T> f, |
793 |
> |
BiConsumer<? super T,? super Throwable> a); |
794 |
> |
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
795 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); |
796 |
> |
public abstract <T,U> CompletableFuture<Void> thenAcceptBoth |
797 |
> |
(CompletableFuture<T> f, |
798 |
> |
CompletionStage<? extends U> g, |
799 |
> |
BiConsumer<? super T,? super U> a); |
800 |
> |
public abstract <T,U,V> CompletableFuture<V> thenCombine |
801 |
> |
(CompletableFuture<T> f, |
802 |
> |
CompletionStage<? extends U> g, |
803 |
> |
BiFunction<? super T,? super U,? extends V> a); |
804 |
> |
public abstract <T> CompletableFuture<Void> runAfterEither |
805 |
> |
(CompletableFuture<T> f, |
806 |
> |
CompletionStage<?> g, |
807 |
> |
java.lang.Runnable a); |
808 |
> |
public abstract <T> CompletableFuture<Void> acceptEither |
809 |
> |
(CompletableFuture<T> f, |
810 |
> |
CompletionStage<? extends T> g, |
811 |
> |
Consumer<? super T> a); |
812 |
> |
public abstract <T,U> CompletableFuture<U> applyToEither |
813 |
> |
(CompletableFuture<T> f, |
814 |
> |
CompletionStage<? extends T> g, |
815 |
> |
Function<? super T,U> a); |
816 |
> |
} |
817 |
> |
|
818 |
> |
/** |
819 |
> |
* exceptionally action is not invoked when source completes |
820 |
> |
* normally, and source result is propagated |
821 |
> |
*/ |
822 |
> |
public void testExceptionally_normalCompletion() { |
823 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
824 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
825 |
> |
{ |
826 |
> |
final AtomicInteger a = new AtomicInteger(0); |
827 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
828 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
829 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
830 |
> |
((Throwable t) -> { |
831 |
> |
// Should not be called |
832 |
> |
a.getAndIncrement(); |
833 |
> |
throw new AssertionError(); |
834 |
> |
}); |
835 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
836 |
> |
|
837 |
> |
checkCompletedNormally(g, v1); |
838 |
> |
checkCompletedNormally(f, v1); |
839 |
> |
assertEquals(0, a.get()); |
840 |
> |
}} |
841 |
|
|
842 |
|
/** |
843 |
|
* exceptionally action completes with function value on source |
844 |
< |
* exception; otherwise with source value |
844 |
> |
* exception |
845 |
|
*/ |
846 |
< |
public void testExceptionally() { |
847 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
848 |
< |
ExceptionToInteger r = new ExceptionToInteger(); |
849 |
< |
CompletableFuture<Integer> g = f.exceptionally(r); |
850 |
< |
f.completeExceptionally(new CFException()); |
851 |
< |
checkCompletedNormally(g, three); |
846 |
> |
public void testExceptionally_exceptionalCompletion() { |
847 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
848 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
849 |
> |
{ |
850 |
> |
final AtomicInteger a = new AtomicInteger(0); |
851 |
> |
final CFException ex = new CFException(); |
852 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
853 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
854 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
855 |
> |
((Throwable t) -> { |
856 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
857 |
> |
threadAssertSame(t, ex); |
858 |
> |
a.getAndIncrement(); |
859 |
> |
return v1; |
860 |
> |
}); |
861 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
862 |
> |
|
863 |
> |
checkCompletedNormally(g, v1); |
864 |
> |
assertEquals(1, a.get()); |
865 |
> |
}} |
866 |
> |
|
867 |
> |
public void testExceptionally_exceptionalCompletionActionFailed() { |
868 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
869 |
> |
{ |
870 |
> |
final AtomicInteger a = new AtomicInteger(0); |
871 |
> |
final CFException ex1 = new CFException(); |
872 |
> |
final CFException ex2 = new CFException(); |
873 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
874 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
875 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
876 |
> |
((Throwable t) -> { |
877 |
> |
ExecutionMode.SYNC.checkExecutionMode(); |
878 |
> |
threadAssertSame(t, ex1); |
879 |
> |
a.getAndIncrement(); |
880 |
> |
throw ex2; |
881 |
> |
}); |
882 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
883 |
> |
|
884 |
> |
checkCompletedWithWrappedException(g, ex2); |
885 |
> |
assertEquals(1, a.get()); |
886 |
> |
}} |
887 |
|
|
888 |
< |
f = new CompletableFuture<>(); |
889 |
< |
r = new ExceptionToInteger(); |
890 |
< |
g = f.exceptionally(r); |
891 |
< |
f.complete(one); |
892 |
< |
checkCompletedNormally(g, one); |
893 |
< |
} |
888 |
> |
/** |
889 |
> |
* whenComplete action executes on normal completion, propagating |
890 |
> |
* source result. |
891 |
> |
*/ |
892 |
> |
public void testWhenComplete_normalCompletion1() { |
893 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
894 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
895 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
896 |
> |
{ |
897 |
> |
final AtomicInteger a = new AtomicInteger(0); |
898 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
899 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
900 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
901 |
> |
(f, |
902 |
> |
(Integer x, Throwable t) -> { |
903 |
> |
m.checkExecutionMode(); |
904 |
> |
threadAssertSame(x, v1); |
905 |
> |
threadAssertNull(t); |
906 |
> |
a.getAndIncrement(); |
907 |
> |
}); |
908 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
909 |
> |
|
910 |
> |
checkCompletedNormally(g, v1); |
911 |
> |
checkCompletedNormally(f, v1); |
912 |
> |
assertEquals(1, a.get()); |
913 |
> |
}} |
914 |
|
|
915 |
|
/** |
916 |
< |
* handle action completes normally with function value on either |
917 |
< |
* normal or exceptional completion of source |
916 |
> |
* whenComplete action executes on exceptional completion, propagating |
917 |
> |
* source result. |
918 |
|
*/ |
919 |
< |
public void testHandle() { |
920 |
< |
CompletableFuture<Integer> f, g; |
921 |
< |
IntegerHandler r; |
919 |
> |
public void testWhenComplete_exceptionalCompletion() { |
920 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
921 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
922 |
> |
{ |
923 |
> |
final AtomicInteger a = new AtomicInteger(0); |
924 |
> |
final CFException ex = new CFException(); |
925 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
926 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
927 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
928 |
> |
(f, |
929 |
> |
(Integer x, Throwable t) -> { |
930 |
> |
m.checkExecutionMode(); |
931 |
> |
threadAssertNull(x); |
932 |
> |
threadAssertSame(t, ex); |
933 |
> |
a.getAndIncrement(); |
934 |
> |
}); |
935 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
936 |
> |
|
937 |
> |
checkCompletedWithWrappedException(g, ex); |
938 |
> |
checkCompletedExceptionally(f, ex); |
939 |
> |
assertEquals(1, a.get()); |
940 |
> |
}} |
941 |
> |
|
942 |
> |
/** |
943 |
> |
* whenComplete action executes on cancelled source, propagating |
944 |
> |
* CancellationException. |
945 |
> |
*/ |
946 |
> |
public void testWhenComplete_sourceCancelled() { |
947 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
948 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
949 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
950 |
> |
{ |
951 |
> |
final AtomicInteger a = new AtomicInteger(0); |
952 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
953 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
954 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
955 |
> |
(f, |
956 |
> |
(Integer x, Throwable t) -> { |
957 |
> |
m.checkExecutionMode(); |
958 |
> |
threadAssertNull(x); |
959 |
> |
threadAssertTrue(t instanceof CancellationException); |
960 |
> |
a.getAndIncrement(); |
961 |
> |
}); |
962 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
963 |
|
|
964 |
< |
f = new CompletableFuture<>(); |
965 |
< |
f.completeExceptionally(new CFException()); |
966 |
< |
g = f.handle(r = new IntegerHandler()); |
967 |
< |
assertTrue(r.ran); |
406 |
< |
checkCompletedNormally(g, three); |
964 |
> |
checkCompletedWithWrappedCancellationException(g); |
965 |
> |
checkCancelled(f); |
966 |
> |
assertEquals(1, a.get()); |
967 |
> |
}} |
968 |
|
|
969 |
< |
f = new CompletableFuture<>(); |
970 |
< |
g = f.handle(r = new IntegerHandler()); |
971 |
< |
assertFalse(r.ran); |
972 |
< |
f.completeExceptionally(new CFException()); |
973 |
< |
checkCompletedNormally(g, three); |
974 |
< |
assertTrue(r.ran); |
969 |
> |
/** |
970 |
> |
* If a whenComplete action throws an exception when triggered by |
971 |
> |
* a normal completion, it completes exceptionally |
972 |
> |
*/ |
973 |
> |
public void testWhenComplete_actionFailed() { |
974 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
975 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
976 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
977 |
> |
{ |
978 |
> |
final AtomicInteger a = new AtomicInteger(0); |
979 |
> |
final CFException ex = new CFException(); |
980 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
981 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
982 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
983 |
> |
(f, |
984 |
> |
(Integer x, Throwable t) -> { |
985 |
> |
m.checkExecutionMode(); |
986 |
> |
threadAssertSame(x, v1); |
987 |
> |
threadAssertNull(t); |
988 |
> |
a.getAndIncrement(); |
989 |
> |
throw ex; |
990 |
> |
}); |
991 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
992 |
> |
|
993 |
> |
checkCompletedWithWrappedException(g, ex); |
994 |
> |
checkCompletedNormally(f, v1); |
995 |
> |
assertEquals(1, a.get()); |
996 |
> |
}} |
997 |
|
|
998 |
< |
f = new CompletableFuture<>(); |
999 |
< |
f.complete(one); |
1000 |
< |
g = f.handle(r = new IntegerHandler()); |
1001 |
< |
assertTrue(r.ran); |
1002 |
< |
checkCompletedNormally(g, two); |
998 |
> |
/** |
999 |
> |
* If a whenComplete action throws an exception when triggered by |
1000 |
> |
* a source completion that also throws an exception, the source |
1001 |
> |
* exception takes precedence. |
1002 |
> |
*/ |
1003 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1004 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1005 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1006 |
> |
{ |
1007 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1008 |
> |
final CFException ex1 = new CFException(); |
1009 |
> |
final CFException ex2 = new CFException(); |
1010 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1011 |
> |
|
1012 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1013 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1014 |
> |
(f, |
1015 |
> |
(Integer x, Throwable t) -> { |
1016 |
> |
m.checkExecutionMode(); |
1017 |
> |
threadAssertSame(t, ex1); |
1018 |
> |
threadAssertNull(x); |
1019 |
> |
a.getAndIncrement(); |
1020 |
> |
throw ex2; |
1021 |
> |
}); |
1022 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1023 |
> |
|
1024 |
> |
checkCompletedWithWrappedException(g, ex1); |
1025 |
> |
checkCompletedExceptionally(f, ex1); |
1026 |
> |
assertEquals(1, a.get()); |
1027 |
> |
}} |
1028 |
> |
|
1029 |
> |
/** |
1030 |
> |
* handle action completes normally with function value on normal |
1031 |
> |
* completion of source |
1032 |
> |
*/ |
1033 |
> |
public void testHandle_normalCompletion() { |
1034 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1035 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1036 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1037 |
> |
{ |
1038 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1039 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1040 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1041 |
> |
final CompletableFuture<Integer> g = m.handle |
1042 |
> |
(f, |
1043 |
> |
(Integer x, Throwable t) -> { |
1044 |
> |
m.checkExecutionMode(); |
1045 |
> |
threadAssertSame(x, v1); |
1046 |
> |
threadAssertNull(t); |
1047 |
> |
a.getAndIncrement(); |
1048 |
> |
return inc(v1); |
1049 |
> |
}); |
1050 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1051 |
> |
|
1052 |
> |
checkCompletedNormally(g, inc(v1)); |
1053 |
> |
checkCompletedNormally(f, v1); |
1054 |
> |
assertEquals(1, a.get()); |
1055 |
> |
}} |
1056 |
|
|
1057 |
< |
f = new CompletableFuture<>(); |
1058 |
< |
g = f.handle(r = new IntegerHandler()); |
1059 |
< |
assertFalse(r.ran); |
1060 |
< |
f.complete(one); |
1061 |
< |
assertTrue(r.ran); |
1062 |
< |
checkCompletedNormally(g, two); |
1063 |
< |
} |
1057 |
> |
/** |
1058 |
> |
* handle action completes normally with function value on |
1059 |
> |
* exceptional completion of source |
1060 |
> |
*/ |
1061 |
> |
public void testHandle_exceptionalCompletion() { |
1062 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1063 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1064 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1065 |
> |
{ |
1066 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1067 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1068 |
> |
final CFException ex = new CFException(); |
1069 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1070 |
> |
final CompletableFuture<Integer> g = m.handle |
1071 |
> |
(f, |
1072 |
> |
(Integer x, Throwable t) -> { |
1073 |
> |
m.checkExecutionMode(); |
1074 |
> |
threadAssertNull(x); |
1075 |
> |
threadAssertSame(t, ex); |
1076 |
> |
a.getAndIncrement(); |
1077 |
> |
return v1; |
1078 |
> |
}); |
1079 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
1080 |
> |
|
1081 |
> |
checkCompletedNormally(g, v1); |
1082 |
> |
checkCompletedExceptionally(f, ex); |
1083 |
> |
assertEquals(1, a.get()); |
1084 |
> |
}} |
1085 |
> |
|
1086 |
> |
/** |
1087 |
> |
* handle action completes normally with function value on |
1088 |
> |
* cancelled source |
1089 |
> |
*/ |
1090 |
> |
public void testHandle_sourceCancelled() { |
1091 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1092 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1093 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1094 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1095 |
> |
{ |
1096 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1097 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1098 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1099 |
> |
final CompletableFuture<Integer> g = m.handle |
1100 |
> |
(f, |
1101 |
> |
(Integer x, Throwable t) -> { |
1102 |
> |
m.checkExecutionMode(); |
1103 |
> |
threadAssertNull(x); |
1104 |
> |
threadAssertTrue(t instanceof CancellationException); |
1105 |
> |
a.getAndIncrement(); |
1106 |
> |
return v1; |
1107 |
> |
}); |
1108 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1109 |
> |
|
1110 |
> |
checkCompletedNormally(g, v1); |
1111 |
> |
checkCancelled(f); |
1112 |
> |
assertEquals(1, a.get()); |
1113 |
> |
}} |
1114 |
|
|
1115 |
|
/** |
1116 |
< |
* runAsync completes after running Runnable |
1116 |
> |
* handle result completes exceptionally if action does |
1117 |
|
*/ |
1118 |
< |
public void testRunAsync() { |
1119 |
< |
Noop r = new Noop(); |
1120 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1121 |
< |
assertNull(f.join()); |
1122 |
< |
assertTrue(r.ran); |
1123 |
< |
checkCompletedNormally(f, null); |
1124 |
< |
} |
1118 |
> |
public void testHandle_sourceFailedActionFailed() { |
1119 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1120 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1121 |
> |
{ |
1122 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1123 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1124 |
> |
final CFException ex1 = new CFException(); |
1125 |
> |
final CFException ex2 = new CFException(); |
1126 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1127 |
> |
final CompletableFuture<Integer> g = m.handle |
1128 |
> |
(f, |
1129 |
> |
(Integer x, Throwable t) -> { |
1130 |
> |
m.checkExecutionMode(); |
1131 |
> |
threadAssertNull(x); |
1132 |
> |
threadAssertSame(ex1, t); |
1133 |
> |
a.getAndIncrement(); |
1134 |
> |
throw ex2; |
1135 |
> |
}); |
1136 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1137 |
> |
|
1138 |
> |
checkCompletedWithWrappedException(g, ex2); |
1139 |
> |
checkCompletedExceptionally(f, ex1); |
1140 |
> |
assertEquals(1, a.get()); |
1141 |
> |
}} |
1142 |
> |
|
1143 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1144 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1145 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1146 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1147 |
> |
{ |
1148 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1149 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1150 |
> |
final CFException ex = new CFException(); |
1151 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
1152 |
> |
final CompletableFuture<Integer> g = m.handle |
1153 |
> |
(f, |
1154 |
> |
(Integer x, Throwable t) -> { |
1155 |
> |
m.checkExecutionMode(); |
1156 |
> |
threadAssertSame(x, v1); |
1157 |
> |
threadAssertNull(t); |
1158 |
> |
a.getAndIncrement(); |
1159 |
> |
throw ex; |
1160 |
> |
}); |
1161 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
1162 |
> |
|
1163 |
> |
checkCompletedWithWrappedException(g, ex); |
1164 |
> |
checkCompletedNormally(f, v1); |
1165 |
> |
assertEquals(1, a.get()); |
1166 |
> |
}} |
1167 |
|
|
1168 |
|
/** |
1169 |
< |
* runAsync with executor completes after running Runnable |
1169 |
> |
* runAsync completes after running Runnable |
1170 |
|
*/ |
1171 |
< |
public void testRunAsync2() { |
1172 |
< |
Noop r = new Noop(); |
1173 |
< |
ThreadExecutor exec = new ThreadExecutor(); |
1174 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, exec); |
1171 |
> |
public void testRunAsync_normalCompletion() { |
1172 |
> |
ExecutionMode[] executionModes = { |
1173 |
> |
ExecutionMode.ASYNC, |
1174 |
> |
ExecutionMode.EXECUTOR, |
1175 |
> |
}; |
1176 |
> |
for (ExecutionMode m : executionModes) |
1177 |
> |
{ |
1178 |
> |
final Noop r = new Noop(m); |
1179 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1180 |
|
assertNull(f.join()); |
448 |
– |
assertTrue(r.ran); |
1181 |
|
checkCompletedNormally(f, null); |
1182 |
< |
assertEquals(1, exec.count.get()); |
1183 |
< |
} |
1182 |
> |
r.assertInvoked(); |
1183 |
> |
}} |
1184 |
|
|
1185 |
|
/** |
1186 |
|
* failing runAsync completes exceptionally after running Runnable |
1187 |
|
*/ |
1188 |
< |
public void testRunAsync3() { |
1189 |
< |
FailingNoop r = new FailingNoop(); |
1190 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
1188 |
> |
public void testRunAsync_exceptionalCompletion() { |
1189 |
> |
ExecutionMode[] executionModes = { |
1190 |
> |
ExecutionMode.ASYNC, |
1191 |
> |
ExecutionMode.EXECUTOR, |
1192 |
> |
}; |
1193 |
> |
for (ExecutionMode m : executionModes) |
1194 |
> |
{ |
1195 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1196 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1197 |
|
checkCompletedWithWrappedCFException(f); |
1198 |
< |
assertTrue(r.ran); |
1199 |
< |
} |
1198 |
> |
r.assertInvoked(); |
1199 |
> |
}} |
1200 |
|
|
1201 |
|
/** |
1202 |
|
* supplyAsync completes with result of supplier |
1203 |
|
*/ |
1204 |
< |
public void testSupplyAsync() { |
1205 |
< |
CompletableFuture<Integer> f; |
1206 |
< |
f = CompletableFuture.supplyAsync(supplyOne); |
1207 |
< |
assertEquals(f.join(), one); |
1208 |
< |
checkCompletedNormally(f, one); |
1209 |
< |
} |
1210 |
< |
|
1211 |
< |
/** |
1212 |
< |
* supplyAsync with executor completes with result of supplier |
1213 |
< |
*/ |
1214 |
< |
public void testSupplyAsync2() { |
1215 |
< |
CompletableFuture<Integer> f; |
1216 |
< |
f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
1217 |
< |
assertEquals(f.join(), one); |
480 |
< |
checkCompletedNormally(f, one); |
481 |
< |
} |
1204 |
> |
public void testSupplyAsync_normalCompletion() { |
1205 |
> |
ExecutionMode[] executionModes = { |
1206 |
> |
ExecutionMode.ASYNC, |
1207 |
> |
ExecutionMode.EXECUTOR, |
1208 |
> |
}; |
1209 |
> |
for (ExecutionMode m : executionModes) |
1210 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1211 |
> |
{ |
1212 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1213 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1214 |
> |
assertSame(v1, f.join()); |
1215 |
> |
checkCompletedNormally(f, v1); |
1216 |
> |
r.assertInvoked(); |
1217 |
> |
}} |
1218 |
|
|
1219 |
|
/** |
1220 |
|
* Failing supplyAsync completes exceptionally |
1221 |
|
*/ |
1222 |
< |
public void testSupplyAsync3() { |
1223 |
< |
FailingSupplier r = new FailingSupplier(); |
1224 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
1222 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1223 |
> |
ExecutionMode[] executionModes = { |
1224 |
> |
ExecutionMode.ASYNC, |
1225 |
> |
ExecutionMode.EXECUTOR, |
1226 |
> |
}; |
1227 |
> |
for (ExecutionMode m : executionModes) |
1228 |
> |
{ |
1229 |
> |
FailingSupplier r = new FailingSupplier(m); |
1230 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1231 |
|
checkCompletedWithWrappedCFException(f); |
1232 |
< |
assertTrue(r.ran); |
1233 |
< |
} |
1232 |
> |
r.assertInvoked(); |
1233 |
> |
}} |
1234 |
|
|
1235 |
|
// seq completion methods |
1236 |
|
|
1237 |
|
/** |
1238 |
|
* thenRun result completes normally after normal completion of source |
1239 |
|
*/ |
1240 |
< |
public void testThenRun() { |
1241 |
< |
CompletableFuture<Integer> f; |
1242 |
< |
CompletableFuture<Void> g; |
1243 |
< |
Noop r; |
1244 |
< |
|
1245 |
< |
f = new CompletableFuture<>(); |
1246 |
< |
g = f.thenRun(r = new Noop()); |
1247 |
< |
f.complete(null); |
1248 |
< |
checkCompletedNormally(g, null); |
1249 |
< |
assertTrue(r.ran); |
1250 |
< |
|
1251 |
< |
f = new CompletableFuture<>(); |
1252 |
< |
f.complete(null); |
1253 |
< |
g = f.thenRun(r = new Noop()); |
1254 |
< |
checkCompletedNormally(g, null); |
1255 |
< |
assertTrue(r.ran); |
1256 |
< |
} |
1240 |
> |
public void testThenRun_normalCompletion() { |
1241 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1242 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1243 |
> |
{ |
1244 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1245 |
> |
final Noop[] rs = new Noop[6]; |
1246 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1247 |
> |
|
1248 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1249 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1250 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1251 |
> |
checkIncomplete(h0); |
1252 |
> |
checkIncomplete(h1); |
1253 |
> |
checkIncomplete(h2); |
1254 |
> |
assertTrue(f.complete(v1)); |
1255 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1256 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1257 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1258 |
> |
|
1259 |
> |
checkCompletedNormally(h0, null); |
1260 |
> |
checkCompletedNormally(h1, null); |
1261 |
> |
checkCompletedNormally(h2, null); |
1262 |
> |
checkCompletedNormally(h3, null); |
1263 |
> |
checkCompletedNormally(h4, null); |
1264 |
> |
checkCompletedNormally(h5, null); |
1265 |
> |
checkCompletedNormally(f, v1); |
1266 |
> |
for (Noop r : rs) r.assertInvoked(); |
1267 |
> |
}} |
1268 |
|
|
1269 |
|
/** |
1270 |
|
* thenRun result completes exceptionally after exceptional |
1271 |
|
* completion of source |
1272 |
|
*/ |
1273 |
< |
public void testThenRun2() { |
1274 |
< |
CompletableFuture<Integer> f; |
1275 |
< |
CompletableFuture<Void> g; |
1276 |
< |
Noop r; |
1277 |
< |
|
1278 |
< |
f = new CompletableFuture<>(); |
1279 |
< |
g = f.thenRun(r = new Noop()); |
1280 |
< |
f.completeExceptionally(new CFException()); |
1281 |
< |
checkCompletedWithWrappedCFException(g); |
1282 |
< |
assertFalse(r.ran); |
1283 |
< |
|
1284 |
< |
f = new CompletableFuture<>(); |
1285 |
< |
f.completeExceptionally(new CFException()); |
1286 |
< |
g = f.thenRun(r = new Noop()); |
1287 |
< |
checkCompletedWithWrappedCFException(g); |
1288 |
< |
assertFalse(r.ran); |
1289 |
< |
} |
1273 |
> |
public void testThenRun_exceptionalCompletion() { |
1274 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1275 |
> |
{ |
1276 |
> |
final CFException ex = new CFException(); |
1277 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1278 |
> |
final Noop[] rs = new Noop[6]; |
1279 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1280 |
> |
|
1281 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1282 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1283 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1284 |
> |
checkIncomplete(h0); |
1285 |
> |
checkIncomplete(h1); |
1286 |
> |
checkIncomplete(h2); |
1287 |
> |
assertTrue(f.completeExceptionally(ex)); |
1288 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1289 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1290 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1291 |
> |
|
1292 |
> |
checkCompletedWithWrappedException(h0, ex); |
1293 |
> |
checkCompletedWithWrappedException(h1, ex); |
1294 |
> |
checkCompletedWithWrappedException(h2, ex); |
1295 |
> |
checkCompletedWithWrappedException(h3, ex); |
1296 |
> |
checkCompletedWithWrappedException(h4, ex); |
1297 |
> |
checkCompletedWithWrappedException(h5, ex); |
1298 |
> |
checkCompletedExceptionally(f, ex); |
1299 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1300 |
> |
}} |
1301 |
|
|
1302 |
|
/** |
1303 |
< |
* thenRun result completes exceptionally if action does |
1303 |
> |
* thenRun result completes exceptionally if source cancelled |
1304 |
|
*/ |
1305 |
< |
public void testThenRun3() { |
1306 |
< |
CompletableFuture<Integer> f; |
1307 |
< |
CompletableFuture<Void> g; |
1308 |
< |
FailingNoop r; |
1309 |
< |
|
1310 |
< |
f = new CompletableFuture<>(); |
1311 |
< |
g = f.thenRun(r = new FailingNoop()); |
1312 |
< |
f.complete(null); |
1313 |
< |
checkCompletedWithWrappedCFException(g); |
1314 |
< |
|
1315 |
< |
f = new CompletableFuture<>(); |
1316 |
< |
f.complete(null); |
1317 |
< |
g = f.thenRun(r = new FailingNoop()); |
1318 |
< |
checkCompletedWithWrappedCFException(g); |
1319 |
< |
} |
1305 |
> |
public void testThenRun_sourceCancelled() { |
1306 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1307 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1308 |
> |
{ |
1309 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1310 |
> |
final Noop[] rs = new Noop[6]; |
1311 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
1312 |
> |
|
1313 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1314 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1315 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1316 |
> |
checkIncomplete(h0); |
1317 |
> |
checkIncomplete(h1); |
1318 |
> |
checkIncomplete(h2); |
1319 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1320 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1321 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1322 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1323 |
> |
|
1324 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1325 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1326 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1327 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1328 |
> |
checkCompletedWithWrappedCancellationException(h4); |
1329 |
> |
checkCompletedWithWrappedCancellationException(h5); |
1330 |
> |
checkCancelled(f); |
1331 |
> |
for (Noop r : rs) r.assertNotInvoked(); |
1332 |
> |
}} |
1333 |
|
|
1334 |
|
/** |
1335 |
< |
* thenRun result completes exceptionally if source cancelled |
1335 |
> |
* thenRun result completes exceptionally if action does |
1336 |
|
*/ |
1337 |
< |
public void testThenRun4() { |
1338 |
< |
CompletableFuture<Integer> f; |
1339 |
< |
CompletableFuture<Void> g; |
1340 |
< |
Noop r; |
1341 |
< |
|
1342 |
< |
f = new CompletableFuture<>(); |
1343 |
< |
g = f.thenRun(r = new Noop()); |
1344 |
< |
assertTrue(f.cancel(true)); |
1345 |
< |
checkCompletedWithWrappedCancellationException(g); |
1346 |
< |
|
1347 |
< |
f = new CompletableFuture<>(); |
1348 |
< |
assertTrue(f.cancel(true)); |
1349 |
< |
g = f.thenRun(r = new Noop()); |
1350 |
< |
checkCompletedWithWrappedCancellationException(g); |
1351 |
< |
} |
1337 |
> |
public void testThenRun_actionFailed() { |
1338 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1339 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1340 |
> |
{ |
1341 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1342 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
1343 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
1344 |
> |
|
1345 |
> |
final CompletableFuture<Void> h0 = m.thenRun(f, rs[0]); |
1346 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, f, rs[1]); |
1347 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, f, rs[2]); |
1348 |
> |
assertTrue(f.complete(v1)); |
1349 |
> |
final CompletableFuture<Void> h3 = m.thenRun(f, rs[3]); |
1350 |
> |
final CompletableFuture<Void> h4 = m.runAfterBoth(f, f, rs[4]); |
1351 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(f, f, rs[5]); |
1352 |
> |
|
1353 |
> |
checkCompletedWithWrappedCFException(h0); |
1354 |
> |
checkCompletedWithWrappedCFException(h1); |
1355 |
> |
checkCompletedWithWrappedCFException(h2); |
1356 |
> |
checkCompletedWithWrappedCFException(h3); |
1357 |
> |
checkCompletedWithWrappedCFException(h4); |
1358 |
> |
checkCompletedWithWrappedCFException(h5); |
1359 |
> |
checkCompletedNormally(f, v1); |
1360 |
> |
}} |
1361 |
|
|
1362 |
|
/** |
1363 |
|
* thenApply result completes normally after normal completion of source |
1364 |
|
*/ |
1365 |
< |
public void testThenApply() { |
1366 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1367 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1368 |
< |
f.complete(one); |
1369 |
< |
checkCompletedNormally(g, two); |
1370 |
< |
} |
1365 |
> |
public void testThenApply_normalCompletion() { |
1366 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1367 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1368 |
> |
{ |
1369 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1370 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1371 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1372 |
> |
|
1373 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1374 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1375 |
> |
checkIncomplete(h0); |
1376 |
> |
checkIncomplete(h1); |
1377 |
> |
assertTrue(f.complete(v1)); |
1378 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1379 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1380 |
> |
|
1381 |
> |
checkCompletedNormally(h0, inc(v1)); |
1382 |
> |
checkCompletedNormally(h1, inc(v1)); |
1383 |
> |
checkCompletedNormally(h2, inc(v1)); |
1384 |
> |
checkCompletedNormally(h3, inc(v1)); |
1385 |
> |
checkCompletedNormally(f, v1); |
1386 |
> |
for (IncFunction r : rs) r.assertValue(inc(v1)); |
1387 |
> |
}} |
1388 |
|
|
1389 |
|
/** |
1390 |
|
* thenApply result completes exceptionally after exceptional |
1391 |
|
* completion of source |
1392 |
|
*/ |
1393 |
< |
public void testThenApply2() { |
1394 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1395 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1396 |
< |
f.completeExceptionally(new CFException()); |
1397 |
< |
checkCompletedWithWrappedCFException(g); |
1398 |
< |
} |
1393 |
> |
public void testThenApply_exceptionalCompletion() { |
1394 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1395 |
> |
{ |
1396 |
> |
final CFException ex = new CFException(); |
1397 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1398 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1399 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1400 |
> |
|
1401 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1402 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1403 |
> |
assertTrue(f.completeExceptionally(ex)); |
1404 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1405 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1406 |
> |
|
1407 |
> |
checkCompletedWithWrappedException(h0, ex); |
1408 |
> |
checkCompletedWithWrappedException(h1, ex); |
1409 |
> |
checkCompletedWithWrappedException(h2, ex); |
1410 |
> |
checkCompletedWithWrappedException(h3, ex); |
1411 |
> |
checkCompletedExceptionally(f, ex); |
1412 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1413 |
> |
}} |
1414 |
|
|
1415 |
|
/** |
1416 |
< |
* thenApply result completes exceptionally if action does |
1416 |
> |
* thenApply result completes exceptionally if source cancelled |
1417 |
|
*/ |
1418 |
< |
public void testThenApply3() { |
1419 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1420 |
< |
CompletableFuture<Integer> g = f.thenApply(new FailingFunction()); |
1421 |
< |
f.complete(one); |
1422 |
< |
checkCompletedWithWrappedCFException(g); |
1423 |
< |
} |
1418 |
> |
public void testThenApply_sourceCancelled() { |
1419 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1420 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1421 |
> |
{ |
1422 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1423 |
> |
final IncFunction[] rs = new IncFunction[4]; |
1424 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1425 |
> |
|
1426 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1427 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1428 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1429 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1430 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1431 |
> |
|
1432 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1433 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1434 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1435 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1436 |
> |
checkCancelled(f); |
1437 |
> |
for (IncFunction r : rs) r.assertNotInvoked(); |
1438 |
> |
}} |
1439 |
|
|
1440 |
|
/** |
1441 |
< |
* thenApply result completes exceptionally if source cancelled |
1441 |
> |
* thenApply result completes exceptionally if action does |
1442 |
|
*/ |
1443 |
< |
public void testThenApply4() { |
1444 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1445 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
1446 |
< |
assertTrue(f.cancel(true)); |
1447 |
< |
checkCompletedWithWrappedCancellationException(g); |
1448 |
< |
} |
1443 |
> |
public void testThenApply_actionFailed() { |
1444 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1445 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1446 |
> |
{ |
1447 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1448 |
> |
final FailingFunction[] rs = new FailingFunction[4]; |
1449 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
1450 |
> |
|
1451 |
> |
final CompletableFuture<Integer> h0 = m.thenApply(f, rs[0]); |
1452 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(f, f, rs[1]); |
1453 |
> |
assertTrue(f.complete(v1)); |
1454 |
> |
final CompletableFuture<Integer> h2 = m.thenApply(f, rs[2]); |
1455 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(f, f, rs[3]); |
1456 |
> |
|
1457 |
> |
checkCompletedWithWrappedCFException(h0); |
1458 |
> |
checkCompletedWithWrappedCFException(h1); |
1459 |
> |
checkCompletedWithWrappedCFException(h2); |
1460 |
> |
checkCompletedWithWrappedCFException(h3); |
1461 |
> |
checkCompletedNormally(f, v1); |
1462 |
> |
}} |
1463 |
|
|
1464 |
|
/** |
1465 |
|
* thenAccept result completes normally after normal completion of source |
1466 |
|
*/ |
1467 |
< |
public void testThenAccept() { |
1468 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1469 |
< |
IncAction r = new IncAction(); |
1470 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1471 |
< |
f.complete(one); |
1472 |
< |
checkCompletedNormally(g, null); |
1473 |
< |
assertEquals(r.value, 2); |
1474 |
< |
} |
1467 |
> |
public void testThenAccept_normalCompletion() { |
1468 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1469 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1470 |
> |
{ |
1471 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1472 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1473 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1474 |
> |
|
1475 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1476 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1477 |
> |
checkIncomplete(h0); |
1478 |
> |
checkIncomplete(h1); |
1479 |
> |
assertTrue(f.complete(v1)); |
1480 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1481 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1482 |
> |
|
1483 |
> |
checkCompletedNormally(h0, null); |
1484 |
> |
checkCompletedNormally(h1, null); |
1485 |
> |
checkCompletedNormally(h2, null); |
1486 |
> |
checkCompletedNormally(h3, null); |
1487 |
> |
checkCompletedNormally(f, v1); |
1488 |
> |
for (NoopConsumer r : rs) r.assertValue(v1); |
1489 |
> |
}} |
1490 |
|
|
1491 |
|
/** |
1492 |
|
* thenAccept result completes exceptionally after exceptional |
1493 |
|
* completion of source |
1494 |
|
*/ |
1495 |
< |
public void testThenAccept2() { |
1496 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1497 |
< |
IncAction r = new IncAction(); |
1498 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1499 |
< |
f.completeExceptionally(new CFException()); |
1500 |
< |
checkCompletedWithWrappedCFException(g); |
1501 |
< |
} |
1495 |
> |
public void testThenAccept_exceptionalCompletion() { |
1496 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1497 |
> |
{ |
1498 |
> |
final CFException ex = new CFException(); |
1499 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1500 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1501 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1502 |
> |
|
1503 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1504 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1505 |
> |
assertTrue(f.completeExceptionally(ex)); |
1506 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1507 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1508 |
> |
|
1509 |
> |
checkCompletedWithWrappedException(h0, ex); |
1510 |
> |
checkCompletedWithWrappedException(h1, ex); |
1511 |
> |
checkCompletedWithWrappedException(h2, ex); |
1512 |
> |
checkCompletedWithWrappedException(h3, ex); |
1513 |
> |
checkCompletedExceptionally(f, ex); |
1514 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1515 |
> |
}} |
1516 |
|
|
1517 |
|
/** |
1518 |
< |
* thenAccept result completes exceptionally if action does |
1518 |
> |
* thenAccept result completes exceptionally if source cancelled |
1519 |
|
*/ |
1520 |
< |
public void testThenAccept3() { |
1521 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1522 |
< |
FailingConsumer r = new FailingConsumer(); |
1523 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1524 |
< |
f.complete(one); |
1525 |
< |
checkCompletedWithWrappedCFException(g); |
1526 |
< |
assertTrue(r.ran); |
1527 |
< |
} |
1520 |
> |
public void testThenAccept_sourceCancelled() { |
1521 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1522 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1523 |
> |
{ |
1524 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1525 |
> |
final NoopConsumer[] rs = new NoopConsumer[4]; |
1526 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
1527 |
> |
|
1528 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1529 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1530 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1531 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1532 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1533 |
> |
|
1534 |
> |
checkCompletedWithWrappedCancellationException(h0); |
1535 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1536 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1537 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1538 |
> |
checkCancelled(f); |
1539 |
> |
for (NoopConsumer r : rs) r.assertNotInvoked(); |
1540 |
> |
}} |
1541 |
|
|
1542 |
|
/** |
1543 |
< |
* thenAccept result completes exceptionally if source cancelled |
1543 |
> |
* thenAccept result completes exceptionally if action does |
1544 |
|
*/ |
1545 |
< |
public void testThenAccept4() { |
1546 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1547 |
< |
IncAction r = new IncAction(); |
1548 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
1549 |
< |
assertTrue(f.cancel(true)); |
1550 |
< |
checkCompletedWithWrappedCancellationException(g); |
1551 |
< |
} |
1545 |
> |
public void testThenAccept_actionFailed() { |
1546 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1547 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1548 |
> |
{ |
1549 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1550 |
> |
final FailingConsumer[] rs = new FailingConsumer[4]; |
1551 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
1552 |
> |
|
1553 |
> |
final CompletableFuture<Void> h0 = m.thenAccept(f, rs[0]); |
1554 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(f, f, rs[1]); |
1555 |
> |
assertTrue(f.complete(v1)); |
1556 |
> |
final CompletableFuture<Void> h2 = m.thenAccept(f, rs[2]); |
1557 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(f, f, rs[3]); |
1558 |
> |
|
1559 |
> |
checkCompletedWithWrappedCFException(h0); |
1560 |
> |
checkCompletedWithWrappedCFException(h1); |
1561 |
> |
checkCompletedWithWrappedCFException(h2); |
1562 |
> |
checkCompletedWithWrappedCFException(h3); |
1563 |
> |
checkCompletedNormally(f, v1); |
1564 |
> |
}} |
1565 |
|
|
1566 |
|
/** |
1567 |
|
* thenCombine result completes normally after normal completion |
1568 |
|
* of sources |
1569 |
|
*/ |
1570 |
< |
public void testThenCombine() { |
1571 |
< |
CompletableFuture<Integer> f, g, h; |
1572 |
< |
|
1573 |
< |
f = new CompletableFuture<>(); |
1574 |
< |
g = new CompletableFuture<>(); |
1575 |
< |
h = f.thenCombine(g, subtract); |
1576 |
< |
f.complete(3); |
1577 |
< |
checkIncomplete(h); |
1578 |
< |
g.complete(1); |
1579 |
< |
checkCompletedNormally(h, 2); |
1580 |
< |
|
1581 |
< |
f = new CompletableFuture<>(); |
1582 |
< |
g = new CompletableFuture<>(); |
1583 |
< |
h = f.thenCombine(g, subtract); |
1584 |
< |
g.complete(1); |
1585 |
< |
checkIncomplete(h); |
1586 |
< |
f.complete(3); |
1587 |
< |
checkCompletedNormally(h, 2); |
1588 |
< |
|
1589 |
< |
f = new CompletableFuture<>(); |
1590 |
< |
g = new CompletableFuture<>(); |
1591 |
< |
g.complete(1); |
1592 |
< |
f.complete(3); |
1593 |
< |
h = f.thenCombine(g, subtract); |
1594 |
< |
checkCompletedNormally(h, 2); |
1595 |
< |
} |
1570 |
> |
public void testThenCombine_normalCompletion() { |
1571 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1572 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1573 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1574 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1575 |
> |
{ |
1576 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1577 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1578 |
> |
final SubtractFunction[] rs = new SubtractFunction[6]; |
1579 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new SubtractFunction(m); |
1580 |
> |
|
1581 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1582 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1583 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1584 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1585 |
> |
|
1586 |
> |
final CompletableFuture<Integer> h0 = m.thenCombine(f, g, rs[0]); |
1587 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(fst, fst, rs[1]); |
1588 |
> |
assertTrue(fst.complete(w1)); |
1589 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, rs[2]); |
1590 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(fst, fst, rs[3]); |
1591 |
> |
checkIncomplete(h0); rs[0].assertNotInvoked(); |
1592 |
> |
checkIncomplete(h2); rs[2].assertNotInvoked(); |
1593 |
> |
checkCompletedNormally(h1, subtract(w1, w1)); |
1594 |
> |
checkCompletedNormally(h3, subtract(w1, w1)); |
1595 |
> |
rs[1].assertValue(subtract(w1, w1)); |
1596 |
> |
rs[3].assertValue(subtract(w1, w1)); |
1597 |
> |
assertTrue(snd.complete(w2)); |
1598 |
> |
final CompletableFuture<Integer> h4 = m.thenCombine(f, g, rs[4]); |
1599 |
> |
|
1600 |
> |
checkCompletedNormally(h0, subtract(v1, v2)); |
1601 |
> |
checkCompletedNormally(h2, subtract(v1, v2)); |
1602 |
> |
checkCompletedNormally(h4, subtract(v1, v2)); |
1603 |
> |
rs[0].assertValue(subtract(v1, v2)); |
1604 |
> |
rs[2].assertValue(subtract(v1, v2)); |
1605 |
> |
rs[4].assertValue(subtract(v1, v2)); |
1606 |
> |
|
1607 |
> |
checkCompletedNormally(f, v1); |
1608 |
> |
checkCompletedNormally(g, v2); |
1609 |
> |
}} |
1610 |
|
|
1611 |
|
/** |
1612 |
|
* thenCombine result completes exceptionally after exceptional |
1613 |
|
* completion of either source |
1614 |
|
*/ |
1615 |
< |
public void testThenCombine2() { |
1616 |
< |
CompletableFuture<Integer> f, g, h; |
1617 |
< |
|
1618 |
< |
f = new CompletableFuture<>(); |
1619 |
< |
g = new CompletableFuture<>(); |
1620 |
< |
h = f.thenCombine(g, subtract); |
1621 |
< |
f.completeExceptionally(new CFException()); |
1622 |
< |
checkIncomplete(h); |
1623 |
< |
g.complete(1); |
1624 |
< |
checkCompletedWithWrappedCFException(h); |
1625 |
< |
|
1626 |
< |
f = new CompletableFuture<>(); |
1627 |
< |
g = new CompletableFuture<>(); |
1628 |
< |
h = f.thenCombine(g, subtract); |
1629 |
< |
g.completeExceptionally(new CFException()); |
1630 |
< |
checkIncomplete(h); |
1631 |
< |
f.complete(3); |
1632 |
< |
checkCompletedWithWrappedCFException(h); |
1633 |
< |
|
1634 |
< |
f = new CompletableFuture<>(); |
1635 |
< |
g = new CompletableFuture<>(); |
1636 |
< |
f.complete(3); |
1637 |
< |
g.completeExceptionally(new CFException()); |
1638 |
< |
h = f.thenCombine(g, subtract); |
1639 |
< |
checkCompletedWithWrappedCFException(h); |
1640 |
< |
|
1641 |
< |
f = new CompletableFuture<>(); |
1642 |
< |
g = new CompletableFuture<>(); |
1643 |
< |
f.completeExceptionally(new CFException()); |
1644 |
< |
g.complete(3); |
1645 |
< |
h = f.thenCombine(g, subtract); |
1646 |
< |
checkCompletedWithWrappedCFException(h); |
1647 |
< |
} |
1615 |
> |
public void testThenCombine_exceptionalCompletion() throws Throwable { |
1616 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1617 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1618 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1619 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1620 |
> |
{ |
1621 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1622 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1623 |
> |
final CFException ex = new CFException(); |
1624 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1625 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1626 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1627 |
> |
|
1628 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1629 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1630 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1631 |
> |
() -> fst.completeExceptionally(ex) : |
1632 |
> |
() -> fst.complete(v1); |
1633 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1634 |
> |
() -> snd.complete(v1) : |
1635 |
> |
() -> snd.completeExceptionally(ex); |
1636 |
> |
|
1637 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1638 |
> |
assertTrue(complete1.call()); |
1639 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1640 |
> |
checkIncomplete(h1); |
1641 |
> |
checkIncomplete(h2); |
1642 |
> |
assertTrue(complete2.call()); |
1643 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1644 |
> |
|
1645 |
> |
checkCompletedWithWrappedException(h1, ex); |
1646 |
> |
checkCompletedWithWrappedException(h2, ex); |
1647 |
> |
checkCompletedWithWrappedException(h3, ex); |
1648 |
> |
r1.assertNotInvoked(); |
1649 |
> |
r2.assertNotInvoked(); |
1650 |
> |
r3.assertNotInvoked(); |
1651 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1652 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1653 |
> |
}} |
1654 |
|
|
1655 |
|
/** |
1656 |
< |
* thenCombine result completes exceptionally if action does |
1656 |
> |
* thenCombine result completes exceptionally if either source cancelled |
1657 |
|
*/ |
1658 |
< |
public void testThenCombine3() { |
1659 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1660 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1661 |
< |
FailingBiFunction r = new FailingBiFunction(); |
1662 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, r); |
1663 |
< |
f.complete(one); |
1664 |
< |
checkIncomplete(g); |
1665 |
< |
assertFalse(r.ran); |
1666 |
< |
f2.complete(two); |
1667 |
< |
checkCompletedWithWrappedCFException(g); |
1668 |
< |
assertTrue(r.ran); |
1669 |
< |
} |
1658 |
> |
public void testThenCombine_sourceCancelled() throws Throwable { |
1659 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1660 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1661 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1662 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1663 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1664 |
> |
{ |
1665 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1666 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1667 |
> |
final SubtractFunction r1 = new SubtractFunction(m); |
1668 |
> |
final SubtractFunction r2 = new SubtractFunction(m); |
1669 |
> |
final SubtractFunction r3 = new SubtractFunction(m); |
1670 |
> |
|
1671 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1672 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1673 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1674 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1675 |
> |
() -> fst.complete(v1); |
1676 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1677 |
> |
() -> snd.complete(v1) : |
1678 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1679 |
> |
|
1680 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1681 |
> |
assertTrue(complete1.call()); |
1682 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1683 |
> |
checkIncomplete(h1); |
1684 |
> |
checkIncomplete(h2); |
1685 |
> |
assertTrue(complete2.call()); |
1686 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1687 |
> |
|
1688 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1689 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1690 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1691 |
> |
r1.assertNotInvoked(); |
1692 |
> |
r2.assertNotInvoked(); |
1693 |
> |
r3.assertNotInvoked(); |
1694 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1695 |
> |
checkCancelled(failFirst ? fst : snd); |
1696 |
> |
}} |
1697 |
|
|
1698 |
|
/** |
1699 |
< |
* thenCombine result completes exceptionally if either source cancelled |
1699 |
> |
* thenCombine result completes exceptionally if action does |
1700 |
|
*/ |
1701 |
< |
public void testThenCombine4() { |
1702 |
< |
CompletableFuture<Integer> f, g, h; |
1703 |
< |
|
1704 |
< |
f = new CompletableFuture<>(); |
1705 |
< |
g = new CompletableFuture<>(); |
1706 |
< |
h = f.thenCombine(g, subtract); |
1707 |
< |
assertTrue(f.cancel(true)); |
1708 |
< |
checkIncomplete(h); |
1709 |
< |
g.complete(1); |
1710 |
< |
checkCompletedWithWrappedCancellationException(h); |
1711 |
< |
|
1712 |
< |
f = new CompletableFuture<>(); |
1713 |
< |
g = new CompletableFuture<>(); |
1714 |
< |
h = f.thenCombine(g, subtract); |
1715 |
< |
assertTrue(g.cancel(true)); |
1716 |
< |
checkIncomplete(h); |
1717 |
< |
f.complete(3); |
1718 |
< |
checkCompletedWithWrappedCancellationException(h); |
1719 |
< |
|
1720 |
< |
f = new CompletableFuture<>(); |
1721 |
< |
g = new CompletableFuture<>(); |
1722 |
< |
assertTrue(f.cancel(true)); |
1723 |
< |
assertTrue(g.cancel(true)); |
1724 |
< |
h = f.thenCombine(g, subtract); |
1725 |
< |
checkCompletedWithWrappedCancellationException(h); |
1726 |
< |
} |
1701 |
> |
public void testThenCombine_actionFailed() { |
1702 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1703 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1704 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1705 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1706 |
> |
{ |
1707 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1708 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1709 |
> |
final FailingBiFunction r1 = new FailingBiFunction(m); |
1710 |
> |
final FailingBiFunction r2 = new FailingBiFunction(m); |
1711 |
> |
final FailingBiFunction r3 = new FailingBiFunction(m); |
1712 |
> |
|
1713 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1714 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1715 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1716 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1717 |
> |
|
1718 |
> |
final CompletableFuture<Integer> h1 = m.thenCombine(f, g, r1); |
1719 |
> |
assertTrue(fst.complete(w1)); |
1720 |
> |
final CompletableFuture<Integer> h2 = m.thenCombine(f, g, r2); |
1721 |
> |
assertTrue(snd.complete(w2)); |
1722 |
> |
final CompletableFuture<Integer> h3 = m.thenCombine(f, g, r3); |
1723 |
> |
|
1724 |
> |
checkCompletedWithWrappedCFException(h1); |
1725 |
> |
checkCompletedWithWrappedCFException(h2); |
1726 |
> |
checkCompletedWithWrappedCFException(h3); |
1727 |
> |
r1.assertInvoked(); |
1728 |
> |
r2.assertInvoked(); |
1729 |
> |
r3.assertInvoked(); |
1730 |
> |
checkCompletedNormally(f, v1); |
1731 |
> |
checkCompletedNormally(g, v2); |
1732 |
> |
}} |
1733 |
|
|
1734 |
|
/** |
1735 |
|
* thenAcceptBoth result completes normally after normal |
1736 |
|
* completion of sources |
1737 |
|
*/ |
1738 |
< |
public void testThenAcceptBoth() { |
1739 |
< |
CompletableFuture<Integer> f, g; |
1740 |
< |
CompletableFuture<Void> h; |
1741 |
< |
SubtractAction r; |
1742 |
< |
|
1743 |
< |
f = new CompletableFuture<>(); |
1744 |
< |
g = new CompletableFuture<>(); |
1745 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1746 |
< |
f.complete(3); |
1747 |
< |
checkIncomplete(h); |
1748 |
< |
g.complete(1); |
1749 |
< |
checkCompletedNormally(h, null); |
1750 |
< |
assertEquals(r.value, 2); |
1751 |
< |
|
1752 |
< |
f = new CompletableFuture<>(); |
1753 |
< |
g = new CompletableFuture<>(); |
1754 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1755 |
< |
g.complete(1); |
1756 |
< |
checkIncomplete(h); |
1757 |
< |
f.complete(3); |
1758 |
< |
checkCompletedNormally(h, null); |
1759 |
< |
assertEquals(r.value, 2); |
1760 |
< |
|
1761 |
< |
f = new CompletableFuture<>(); |
1762 |
< |
g = new CompletableFuture<>(); |
1763 |
< |
g.complete(1); |
1764 |
< |
f.complete(3); |
1765 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1766 |
< |
checkCompletedNormally(h, null); |
1767 |
< |
assertEquals(r.value, 2); |
1768 |
< |
} |
1738 |
> |
public void testThenAcceptBoth_normalCompletion() { |
1739 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1740 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1741 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1742 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1743 |
> |
{ |
1744 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1745 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1746 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1747 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1748 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1749 |
> |
|
1750 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1751 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1752 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1753 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1754 |
> |
|
1755 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1756 |
> |
assertTrue(fst.complete(w1)); |
1757 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1758 |
> |
checkIncomplete(h1); |
1759 |
> |
checkIncomplete(h2); |
1760 |
> |
r1.assertNotInvoked(); |
1761 |
> |
r2.assertNotInvoked(); |
1762 |
> |
assertTrue(snd.complete(w2)); |
1763 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1764 |
> |
|
1765 |
> |
checkCompletedNormally(h1, null); |
1766 |
> |
checkCompletedNormally(h2, null); |
1767 |
> |
checkCompletedNormally(h3, null); |
1768 |
> |
r1.assertValue(subtract(v1, v2)); |
1769 |
> |
r2.assertValue(subtract(v1, v2)); |
1770 |
> |
r3.assertValue(subtract(v1, v2)); |
1771 |
> |
checkCompletedNormally(f, v1); |
1772 |
> |
checkCompletedNormally(g, v2); |
1773 |
> |
}} |
1774 |
|
|
1775 |
|
/** |
1776 |
|
* thenAcceptBoth result completes exceptionally after exceptional |
1777 |
|
* completion of either source |
1778 |
|
*/ |
1779 |
< |
public void testThenAcceptBoth2() { |
1780 |
< |
CompletableFuture<Integer> f, g; |
1781 |
< |
CompletableFuture<Void> h; |
1782 |
< |
SubtractAction r; |
1783 |
< |
|
1784 |
< |
f = new CompletableFuture<>(); |
1785 |
< |
g = new CompletableFuture<>(); |
1786 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1787 |
< |
f.completeExceptionally(new CFException()); |
1788 |
< |
checkIncomplete(h); |
1789 |
< |
g.complete(1); |
1790 |
< |
checkCompletedWithWrappedCFException(h); |
1791 |
< |
|
1792 |
< |
f = new CompletableFuture<>(); |
1793 |
< |
g = new CompletableFuture<>(); |
1794 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1795 |
< |
g.completeExceptionally(new CFException()); |
1796 |
< |
checkIncomplete(h); |
1797 |
< |
f.complete(3); |
1798 |
< |
checkCompletedWithWrappedCFException(h); |
1799 |
< |
|
1800 |
< |
f = new CompletableFuture<>(); |
1801 |
< |
g = new CompletableFuture<>(); |
1802 |
< |
f.complete(3); |
1803 |
< |
g.completeExceptionally(new CFException()); |
1804 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1805 |
< |
checkCompletedWithWrappedCFException(h); |
1806 |
< |
|
1807 |
< |
f = new CompletableFuture<>(); |
1808 |
< |
g = new CompletableFuture<>(); |
1809 |
< |
f.completeExceptionally(new CFException()); |
1810 |
< |
g.complete(3); |
1811 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1812 |
< |
checkCompletedWithWrappedCFException(h); |
1813 |
< |
} |
1779 |
> |
public void testThenAcceptBoth_exceptionalCompletion() throws Throwable { |
1780 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1781 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1782 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1783 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1784 |
> |
{ |
1785 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1786 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1787 |
> |
final CFException ex = new CFException(); |
1788 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1789 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1790 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1791 |
> |
|
1792 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1793 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1794 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1795 |
> |
() -> fst.completeExceptionally(ex) : |
1796 |
> |
() -> fst.complete(v1); |
1797 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1798 |
> |
() -> snd.complete(v1) : |
1799 |
> |
() -> snd.completeExceptionally(ex); |
1800 |
> |
|
1801 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1802 |
> |
assertTrue(complete1.call()); |
1803 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1804 |
> |
checkIncomplete(h1); |
1805 |
> |
checkIncomplete(h2); |
1806 |
> |
assertTrue(complete2.call()); |
1807 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1808 |
> |
|
1809 |
> |
checkCompletedWithWrappedException(h1, ex); |
1810 |
> |
checkCompletedWithWrappedException(h2, ex); |
1811 |
> |
checkCompletedWithWrappedException(h3, ex); |
1812 |
> |
r1.assertNotInvoked(); |
1813 |
> |
r2.assertNotInvoked(); |
1814 |
> |
r3.assertNotInvoked(); |
1815 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1816 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1817 |
> |
}} |
1818 |
|
|
1819 |
|
/** |
1820 |
< |
* thenAcceptBoth result completes exceptionally if action does |
1820 |
> |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1821 |
|
*/ |
1822 |
< |
public void testThenAcceptBoth3() { |
1823 |
< |
CompletableFuture<Integer> f, g; |
1824 |
< |
CompletableFuture<Void> h; |
1825 |
< |
FailingBiConsumer r; |
1826 |
< |
|
1827 |
< |
f = new CompletableFuture<>(); |
1828 |
< |
g = new CompletableFuture<>(); |
1829 |
< |
h = f.thenAcceptBoth(g, r = new FailingBiConsumer()); |
1830 |
< |
f.complete(3); |
1831 |
< |
checkIncomplete(h); |
1832 |
< |
g.complete(1); |
1833 |
< |
checkCompletedWithWrappedCFException(h); |
1834 |
< |
|
1835 |
< |
f = new CompletableFuture<>(); |
1836 |
< |
g = new CompletableFuture<>(); |
1837 |
< |
f.complete(3); |
1838 |
< |
g.complete(1); |
1839 |
< |
h = f.thenAcceptBoth(g, r = new FailingBiConsumer()); |
1840 |
< |
checkCompletedWithWrappedCFException(h); |
1841 |
< |
} |
1822 |
> |
public void testThenAcceptBoth_sourceCancelled() throws Throwable { |
1823 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1824 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1825 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1826 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1827 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1828 |
> |
{ |
1829 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1830 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1831 |
> |
final SubtractAction r1 = new SubtractAction(m); |
1832 |
> |
final SubtractAction r2 = new SubtractAction(m); |
1833 |
> |
final SubtractAction r3 = new SubtractAction(m); |
1834 |
> |
|
1835 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1836 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1837 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1838 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
1839 |
> |
() -> fst.complete(v1); |
1840 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1841 |
> |
() -> snd.complete(v1) : |
1842 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
1843 |
> |
|
1844 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1845 |
> |
assertTrue(complete1.call()); |
1846 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1847 |
> |
checkIncomplete(h1); |
1848 |
> |
checkIncomplete(h2); |
1849 |
> |
assertTrue(complete2.call()); |
1850 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1851 |
> |
|
1852 |
> |
checkCompletedWithWrappedCancellationException(h1); |
1853 |
> |
checkCompletedWithWrappedCancellationException(h2); |
1854 |
> |
checkCompletedWithWrappedCancellationException(h3); |
1855 |
> |
r1.assertNotInvoked(); |
1856 |
> |
r2.assertNotInvoked(); |
1857 |
> |
r3.assertNotInvoked(); |
1858 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1859 |
> |
checkCancelled(failFirst ? fst : snd); |
1860 |
> |
}} |
1861 |
|
|
1862 |
|
/** |
1863 |
< |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1863 |
> |
* thenAcceptBoth result completes exceptionally if action does |
1864 |
|
*/ |
1865 |
< |
public void testThenAcceptBoth4() { |
1866 |
< |
CompletableFuture<Integer> f, g; |
1867 |
< |
CompletableFuture<Void> h; |
1868 |
< |
SubtractAction r; |
1869 |
< |
|
1870 |
< |
f = new CompletableFuture<>(); |
1871 |
< |
g = new CompletableFuture<>(); |
1872 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1873 |
< |
assertTrue(f.cancel(true)); |
1874 |
< |
checkIncomplete(h); |
1875 |
< |
g.complete(1); |
1876 |
< |
checkCompletedWithWrappedCancellationException(h); |
1877 |
< |
|
1878 |
< |
f = new CompletableFuture<>(); |
1879 |
< |
g = new CompletableFuture<>(); |
1880 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1881 |
< |
assertTrue(g.cancel(true)); |
1882 |
< |
checkIncomplete(h); |
1883 |
< |
f.complete(3); |
1884 |
< |
checkCompletedWithWrappedCancellationException(h); |
1885 |
< |
|
1886 |
< |
f = new CompletableFuture<>(); |
1887 |
< |
g = new CompletableFuture<>(); |
1888 |
< |
f.complete(3); |
1889 |
< |
assertTrue(g.cancel(true)); |
1890 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
1891 |
< |
checkCompletedWithWrappedCancellationException(h); |
1892 |
< |
|
1893 |
< |
f = new CompletableFuture<>(); |
1894 |
< |
g = new CompletableFuture<>(); |
1895 |
< |
assertTrue(f.cancel(true)); |
1896 |
< |
g.complete(3); |
914 |
< |
h = f.thenAcceptBoth(g, r = new SubtractAction()); |
915 |
< |
checkCompletedWithWrappedCancellationException(h); |
916 |
< |
} |
1865 |
> |
public void testThenAcceptBoth_actionFailed() { |
1866 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1867 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1868 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1869 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1870 |
> |
{ |
1871 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1872 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1873 |
> |
final FailingBiConsumer r1 = new FailingBiConsumer(m); |
1874 |
> |
final FailingBiConsumer r2 = new FailingBiConsumer(m); |
1875 |
> |
final FailingBiConsumer r3 = new FailingBiConsumer(m); |
1876 |
> |
|
1877 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1878 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1879 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1880 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1881 |
> |
|
1882 |
> |
final CompletableFuture<Void> h1 = m.thenAcceptBoth(f, g, r1); |
1883 |
> |
assertTrue(fst.complete(w1)); |
1884 |
> |
final CompletableFuture<Void> h2 = m.thenAcceptBoth(f, g, r2); |
1885 |
> |
assertTrue(snd.complete(w2)); |
1886 |
> |
final CompletableFuture<Void> h3 = m.thenAcceptBoth(f, g, r3); |
1887 |
> |
|
1888 |
> |
checkCompletedWithWrappedCFException(h1); |
1889 |
> |
checkCompletedWithWrappedCFException(h2); |
1890 |
> |
checkCompletedWithWrappedCFException(h3); |
1891 |
> |
r1.assertInvoked(); |
1892 |
> |
r2.assertInvoked(); |
1893 |
> |
r3.assertInvoked(); |
1894 |
> |
checkCompletedNormally(f, v1); |
1895 |
> |
checkCompletedNormally(g, v2); |
1896 |
> |
}} |
1897 |
|
|
1898 |
|
/** |
1899 |
|
* runAfterBoth result completes normally after normal |
1900 |
|
* completion of sources |
1901 |
|
*/ |
1902 |
< |
public void testRunAfterBoth() { |
1903 |
< |
CompletableFuture<Integer> f, g; |
1904 |
< |
CompletableFuture<Void> h; |
1905 |
< |
Noop r; |
1906 |
< |
|
1907 |
< |
f = new CompletableFuture<>(); |
1908 |
< |
g = new CompletableFuture<>(); |
1909 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1910 |
< |
f.complete(3); |
1911 |
< |
checkIncomplete(h); |
1912 |
< |
g.complete(1); |
1913 |
< |
checkCompletedNormally(h, null); |
1914 |
< |
assertTrue(r.ran); |
1915 |
< |
|
1916 |
< |
f = new CompletableFuture<>(); |
1917 |
< |
g = new CompletableFuture<>(); |
1918 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1919 |
< |
g.complete(1); |
1920 |
< |
checkIncomplete(h); |
1921 |
< |
f.complete(3); |
1922 |
< |
checkCompletedNormally(h, null); |
1923 |
< |
assertTrue(r.ran); |
1924 |
< |
|
1925 |
< |
f = new CompletableFuture<>(); |
1926 |
< |
g = new CompletableFuture<>(); |
1927 |
< |
g.complete(1); |
1928 |
< |
f.complete(3); |
1929 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1930 |
< |
checkCompletedNormally(h, null); |
1931 |
< |
assertTrue(r.ran); |
1932 |
< |
} |
1902 |
> |
public void testRunAfterBoth_normalCompletion() { |
1903 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1904 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1905 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1906 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1907 |
> |
{ |
1908 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1909 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1910 |
> |
final Noop r1 = new Noop(m); |
1911 |
> |
final Noop r2 = new Noop(m); |
1912 |
> |
final Noop r3 = new Noop(m); |
1913 |
> |
|
1914 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1915 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1916 |
> |
final Integer w1 = fFirst ? v1 : v2; |
1917 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
1918 |
> |
|
1919 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1920 |
> |
assertTrue(fst.complete(w1)); |
1921 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1922 |
> |
checkIncomplete(h1); |
1923 |
> |
checkIncomplete(h2); |
1924 |
> |
r1.assertNotInvoked(); |
1925 |
> |
r2.assertNotInvoked(); |
1926 |
> |
assertTrue(snd.complete(w2)); |
1927 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1928 |
> |
|
1929 |
> |
checkCompletedNormally(h1, null); |
1930 |
> |
checkCompletedNormally(h2, null); |
1931 |
> |
checkCompletedNormally(h3, null); |
1932 |
> |
r1.assertInvoked(); |
1933 |
> |
r2.assertInvoked(); |
1934 |
> |
r3.assertInvoked(); |
1935 |
> |
checkCompletedNormally(f, v1); |
1936 |
> |
checkCompletedNormally(g, v2); |
1937 |
> |
}} |
1938 |
|
|
1939 |
|
/** |
1940 |
|
* runAfterBoth result completes exceptionally after exceptional |
1941 |
|
* completion of either source |
1942 |
|
*/ |
1943 |
< |
public void testRunAfterBoth2() { |
1944 |
< |
CompletableFuture<Integer> f, g; |
1945 |
< |
CompletableFuture<Void> h; |
1946 |
< |
Noop r; |
1947 |
< |
|
1948 |
< |
f = new CompletableFuture<>(); |
1949 |
< |
g = new CompletableFuture<>(); |
1950 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1951 |
< |
f.completeExceptionally(new CFException()); |
1952 |
< |
checkIncomplete(h); |
1953 |
< |
g.complete(1); |
1954 |
< |
checkCompletedWithWrappedCFException(h); |
1955 |
< |
assertFalse(r.ran); |
1956 |
< |
|
1957 |
< |
f = new CompletableFuture<>(); |
1958 |
< |
g = new CompletableFuture<>(); |
1959 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1960 |
< |
g.completeExceptionally(new CFException()); |
1961 |
< |
checkIncomplete(h); |
1962 |
< |
f.complete(3); |
1963 |
< |
checkCompletedWithWrappedCFException(h); |
1964 |
< |
assertFalse(r.ran); |
1965 |
< |
|
1966 |
< |
f = new CompletableFuture<>(); |
1967 |
< |
g = new CompletableFuture<>(); |
1968 |
< |
g.completeExceptionally(new CFException()); |
1969 |
< |
f.complete(3); |
1970 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1971 |
< |
checkCompletedWithWrappedCFException(h); |
1972 |
< |
assertFalse(r.ran); |
1973 |
< |
|
1974 |
< |
f = new CompletableFuture<>(); |
1975 |
< |
g = new CompletableFuture<>(); |
1976 |
< |
f.completeExceptionally(new CFException()); |
1977 |
< |
g.complete(1); |
1978 |
< |
h = f.runAfterBoth(g, r = new Noop()); |
1979 |
< |
checkCompletedWithWrappedCFException(h); |
1980 |
< |
assertFalse(r.ran); |
1981 |
< |
} |
1943 |
> |
public void testRunAfterBoth_exceptionalCompletion() throws Throwable { |
1944 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1945 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1946 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1947 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1948 |
> |
{ |
1949 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1950 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1951 |
> |
final CFException ex = new CFException(); |
1952 |
> |
final Noop r1 = new Noop(m); |
1953 |
> |
final Noop r2 = new Noop(m); |
1954 |
> |
final Noop r3 = new Noop(m); |
1955 |
> |
|
1956 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
1957 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
1958 |
> |
final Callable<Boolean> complete1 = failFirst ? |
1959 |
> |
() -> fst.completeExceptionally(ex) : |
1960 |
> |
() -> fst.complete(v1); |
1961 |
> |
final Callable<Boolean> complete2 = failFirst ? |
1962 |
> |
() -> snd.complete(v1) : |
1963 |
> |
() -> snd.completeExceptionally(ex); |
1964 |
> |
|
1965 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1966 |
> |
assertTrue(complete1.call()); |
1967 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1968 |
> |
checkIncomplete(h1); |
1969 |
> |
checkIncomplete(h2); |
1970 |
> |
assertTrue(complete2.call()); |
1971 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
1972 |
> |
|
1973 |
> |
checkCompletedWithWrappedException(h1, ex); |
1974 |
> |
checkCompletedWithWrappedException(h2, ex); |
1975 |
> |
checkCompletedWithWrappedException(h3, ex); |
1976 |
> |
r1.assertNotInvoked(); |
1977 |
> |
r2.assertNotInvoked(); |
1978 |
> |
r3.assertNotInvoked(); |
1979 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
1980 |
> |
checkCompletedExceptionally(failFirst ? fst : snd, ex); |
1981 |
> |
}} |
1982 |
|
|
1983 |
|
/** |
1984 |
< |
* runAfterBoth result completes exceptionally if action does |
1984 |
> |
* runAfterBoth result completes exceptionally if either source cancelled |
1985 |
|
*/ |
1986 |
< |
public void testRunAfterBoth3() { |
1987 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1988 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1989 |
< |
FailingNoop r = new FailingNoop(); |
1990 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
1991 |
< |
f.complete(one); |
1992 |
< |
checkIncomplete(g); |
1993 |
< |
f2.complete(two); |
1994 |
< |
checkCompletedWithWrappedCFException(g); |
1995 |
< |
} |
1986 |
> |
public void testRunAfterBoth_sourceCancelled() throws Throwable { |
1987 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1988 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1989 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1990 |
> |
for (boolean failFirst : new boolean[] { true, false }) |
1991 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1992 |
> |
{ |
1993 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1994 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1995 |
> |
final Noop r1 = new Noop(m); |
1996 |
> |
final Noop r2 = new Noop(m); |
1997 |
> |
final Noop r3 = new Noop(m); |
1998 |
> |
|
1999 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2000 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2001 |
> |
final Callable<Boolean> complete1 = failFirst ? |
2002 |
> |
() -> fst.cancel(mayInterruptIfRunning) : |
2003 |
> |
() -> fst.complete(v1); |
2004 |
> |
final Callable<Boolean> complete2 = failFirst ? |
2005 |
> |
() -> snd.complete(v1) : |
2006 |
> |
() -> snd.cancel(mayInterruptIfRunning); |
2007 |
> |
|
2008 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2009 |
> |
assertTrue(complete1.call()); |
2010 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2011 |
> |
checkIncomplete(h1); |
2012 |
> |
checkIncomplete(h2); |
2013 |
> |
assertTrue(complete2.call()); |
2014 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2015 |
> |
|
2016 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2017 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2018 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2019 |
> |
r1.assertNotInvoked(); |
2020 |
> |
r2.assertNotInvoked(); |
2021 |
> |
r3.assertNotInvoked(); |
2022 |
> |
checkCompletedNormally(failFirst ? snd : fst, v1); |
2023 |
> |
checkCancelled(failFirst ? fst : snd); |
2024 |
> |
}} |
2025 |
|
|
2026 |
|
/** |
2027 |
< |
* runAfterBoth result completes exceptionally if either source cancelled |
2027 |
> |
* runAfterBoth result completes exceptionally if action does |
2028 |
|
*/ |
2029 |
< |
public void testRunAfterBoth4() { |
2030 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2031 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2032 |
< |
Noop r = new Noop(); |
2033 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
2034 |
< |
assertTrue(f.cancel(true)); |
2035 |
< |
f2.complete(two); |
2036 |
< |
checkCompletedWithWrappedCancellationException(g); |
2037 |
< |
f = new CompletableFuture<>(); |
2038 |
< |
f2 = new CompletableFuture<>(); |
2039 |
< |
r = new Noop(); |
2040 |
< |
g = f.runAfterBoth(f2, r); |
2041 |
< |
f.complete(one); |
2042 |
< |
assertTrue(f2.cancel(true)); |
2043 |
< |
checkCompletedWithWrappedCancellationException(g); |
2044 |
< |
} |
2029 |
> |
public void testRunAfterBoth_actionFailed() { |
2030 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2031 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2032 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2033 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2034 |
> |
{ |
2035 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2036 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2037 |
> |
final FailingRunnable r1 = new FailingRunnable(m); |
2038 |
> |
final FailingRunnable r2 = new FailingRunnable(m); |
2039 |
> |
final FailingRunnable r3 = new FailingRunnable(m); |
2040 |
> |
|
2041 |
> |
final CompletableFuture<Integer> fst = fFirst ? f : g; |
2042 |
> |
final CompletableFuture<Integer> snd = !fFirst ? f : g; |
2043 |
> |
final Integer w1 = fFirst ? v1 : v2; |
2044 |
> |
final Integer w2 = !fFirst ? v1 : v2; |
2045 |
> |
|
2046 |
> |
final CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
2047 |
> |
assertTrue(fst.complete(w1)); |
2048 |
> |
final CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
2049 |
> |
assertTrue(snd.complete(w2)); |
2050 |
> |
final CompletableFuture<Void> h3 = m.runAfterBoth(f, g, r3); |
2051 |
> |
|
2052 |
> |
checkCompletedWithWrappedCFException(h1); |
2053 |
> |
checkCompletedWithWrappedCFException(h2); |
2054 |
> |
checkCompletedWithWrappedCFException(h3); |
2055 |
> |
r1.assertInvoked(); |
2056 |
> |
r2.assertInvoked(); |
2057 |
> |
r3.assertInvoked(); |
2058 |
> |
checkCompletedNormally(f, v1); |
2059 |
> |
checkCompletedNormally(g, v2); |
2060 |
> |
}} |
2061 |
|
|
2062 |
|
/** |
2063 |
|
* applyToEither result completes normally after normal completion |
2064 |
|
* of either source |
2065 |
|
*/ |
2066 |
< |
public void testApplyToEither() { |
2067 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2068 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2069 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
2070 |
< |
f.complete(one); |
2071 |
< |
checkCompletedNormally(g, two); |
2072 |
< |
f2.complete(one); |
2073 |
< |
checkCompletedNormally(g, two); |
2074 |
< |
|
2075 |
< |
f = new CompletableFuture<>(); |
2076 |
< |
f.complete(one); |
2077 |
< |
f2 = new CompletableFuture<>(); |
2078 |
< |
g = f.applyToEither(f2, inc); |
2079 |
< |
checkCompletedNormally(g, two); |
2080 |
< |
} |
2066 |
> |
public void testApplyToEither_normalCompletion() { |
2067 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2068 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2069 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2070 |
> |
{ |
2071 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2072 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2073 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2074 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2075 |
> |
|
2076 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2077 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2078 |
> |
checkIncomplete(h0); |
2079 |
> |
checkIncomplete(h1); |
2080 |
> |
rs[0].assertNotInvoked(); |
2081 |
> |
rs[1].assertNotInvoked(); |
2082 |
> |
f.complete(v1); |
2083 |
> |
checkCompletedNormally(h0, inc(v1)); |
2084 |
> |
checkCompletedNormally(h1, inc(v1)); |
2085 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2086 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2087 |
> |
checkCompletedNormally(h2, inc(v1)); |
2088 |
> |
checkCompletedNormally(h3, inc(v1)); |
2089 |
> |
g.complete(v2); |
2090 |
> |
|
2091 |
> |
// unspecified behavior - both source completions available |
2092 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2093 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2094 |
> |
rs[4].assertValue(h4.join()); |
2095 |
> |
rs[5].assertValue(h5.join()); |
2096 |
> |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
2097 |
> |
Objects.equals(inc(v2), h4.join())); |
2098 |
> |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
2099 |
> |
Objects.equals(inc(v2), h5.join())); |
2100 |
> |
|
2101 |
> |
checkCompletedNormally(f, v1); |
2102 |
> |
checkCompletedNormally(g, v2); |
2103 |
> |
checkCompletedNormally(h0, inc(v1)); |
2104 |
> |
checkCompletedNormally(h1, inc(v1)); |
2105 |
> |
checkCompletedNormally(h2, inc(v1)); |
2106 |
> |
checkCompletedNormally(h3, inc(v1)); |
2107 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
2108 |
> |
}} |
2109 |
|
|
2110 |
|
/** |
2111 |
|
* applyToEither result completes exceptionally after exceptional |
2112 |
|
* completion of either source |
2113 |
|
*/ |
2114 |
< |
public void testApplyToEither2() { |
2115 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2116 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2117 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
2118 |
< |
f.completeExceptionally(new CFException()); |
2119 |
< |
f2.complete(one); |
2120 |
< |
checkCompletedWithWrappedCFException(g); |
2114 |
> |
public void testApplyToEither_exceptionalCompletion() { |
2115 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2116 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2117 |
> |
{ |
2118 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2119 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2120 |
> |
final CFException ex = new CFException(); |
2121 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2122 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2123 |
> |
|
2124 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2125 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2126 |
> |
checkIncomplete(h0); |
2127 |
> |
checkIncomplete(h1); |
2128 |
> |
rs[0].assertNotInvoked(); |
2129 |
> |
rs[1].assertNotInvoked(); |
2130 |
> |
f.completeExceptionally(ex); |
2131 |
> |
checkCompletedWithWrappedException(h0, ex); |
2132 |
> |
checkCompletedWithWrappedException(h1, ex); |
2133 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2134 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2135 |
> |
checkCompletedWithWrappedException(h2, ex); |
2136 |
> |
checkCompletedWithWrappedException(h3, ex); |
2137 |
> |
g.complete(v1); |
2138 |
> |
|
2139 |
> |
// unspecified behavior - both source completions available |
2140 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2141 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2142 |
> |
try { |
2143 |
> |
assertEquals(inc(v1), h4.join()); |
2144 |
> |
rs[4].assertValue(inc(v1)); |
2145 |
> |
} catch (CompletionException ok) { |
2146 |
> |
checkCompletedWithWrappedException(h4, ex); |
2147 |
> |
rs[4].assertNotInvoked(); |
2148 |
> |
} |
2149 |
> |
try { |
2150 |
> |
assertEquals(inc(v1), h5.join()); |
2151 |
> |
rs[5].assertValue(inc(v1)); |
2152 |
> |
} catch (CompletionException ok) { |
2153 |
> |
checkCompletedWithWrappedException(h5, ex); |
2154 |
> |
rs[5].assertNotInvoked(); |
2155 |
> |
} |
2156 |
|
|
2157 |
< |
f = new CompletableFuture<>(); |
2158 |
< |
f2 = new CompletableFuture<>(); |
2159 |
< |
f2.completeExceptionally(new CFException()); |
2160 |
< |
g = f.applyToEither(f2, inc); |
2161 |
< |
checkCompletedWithWrappedCFException(g); |
2162 |
< |
} |
2157 |
> |
checkCompletedExceptionally(f, ex); |
2158 |
> |
checkCompletedNormally(g, v1); |
2159 |
> |
checkCompletedWithWrappedException(h0, ex); |
2160 |
> |
checkCompletedWithWrappedException(h1, ex); |
2161 |
> |
checkCompletedWithWrappedException(h2, ex); |
2162 |
> |
checkCompletedWithWrappedException(h3, ex); |
2163 |
> |
checkCompletedWithWrappedException(h4, ex); |
2164 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2165 |
> |
}} |
2166 |
> |
|
2167 |
> |
public void testApplyToEither_exceptionalCompletion2() { |
2168 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2169 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2170 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2171 |
> |
{ |
2172 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2173 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2174 |
> |
final CFException ex = new CFException(); |
2175 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2176 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2177 |
> |
|
2178 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2179 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2180 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2181 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2182 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2183 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2184 |
|
|
2185 |
< |
/** |
2186 |
< |
* applyToEither result completes exceptionally if action does |
2187 |
< |
*/ |
2188 |
< |
public void testApplyToEither3() { |
2189 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2190 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2191 |
< |
FailingFunction r = new FailingFunction(); |
2192 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, r); |
2193 |
< |
f2.complete(two); |
2194 |
< |
checkCompletedWithWrappedCFException(g); |
2195 |
< |
} |
2185 |
> |
// unspecified behavior - both source completions available |
2186 |
> |
try { |
2187 |
> |
assertEquals(inc(v1), h0.join()); |
2188 |
> |
rs[0].assertValue(inc(v1)); |
2189 |
> |
} catch (CompletionException ok) { |
2190 |
> |
checkCompletedWithWrappedException(h0, ex); |
2191 |
> |
rs[0].assertNotInvoked(); |
2192 |
> |
} |
2193 |
> |
try { |
2194 |
> |
assertEquals(inc(v1), h1.join()); |
2195 |
> |
rs[1].assertValue(inc(v1)); |
2196 |
> |
} catch (CompletionException ok) { |
2197 |
> |
checkCompletedWithWrappedException(h1, ex); |
2198 |
> |
rs[1].assertNotInvoked(); |
2199 |
> |
} |
2200 |
> |
try { |
2201 |
> |
assertEquals(inc(v1), h2.join()); |
2202 |
> |
rs[2].assertValue(inc(v1)); |
2203 |
> |
} catch (CompletionException ok) { |
2204 |
> |
checkCompletedWithWrappedException(h2, ex); |
2205 |
> |
rs[2].assertNotInvoked(); |
2206 |
> |
} |
2207 |
> |
try { |
2208 |
> |
assertEquals(inc(v1), h3.join()); |
2209 |
> |
rs[3].assertValue(inc(v1)); |
2210 |
> |
} catch (CompletionException ok) { |
2211 |
> |
checkCompletedWithWrappedException(h3, ex); |
2212 |
> |
rs[3].assertNotInvoked(); |
2213 |
> |
} |
2214 |
|
|
2215 |
< |
/** |
2216 |
< |
* applyToEither result completes exceptionally if either source cancelled |
2217 |
< |
*/ |
1086 |
< |
public void testApplyToEither4() { |
1087 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1088 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1089 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
1090 |
< |
assertTrue(f.cancel(true)); |
1091 |
< |
checkCompletedWithWrappedCancellationException(g); |
1092 |
< |
f = new CompletableFuture<>(); |
1093 |
< |
f2 = new CompletableFuture<>(); |
1094 |
< |
assertTrue(f2.cancel(true)); |
1095 |
< |
checkCompletedWithWrappedCancellationException(g); |
1096 |
< |
} |
2215 |
> |
checkCompletedNormally(f, v1); |
2216 |
> |
checkCompletedExceptionally(g, ex); |
2217 |
> |
}} |
2218 |
|
|
2219 |
|
/** |
2220 |
< |
* acceptEither result completes normally after normal completion |
1100 |
< |
* of either source |
2220 |
> |
* applyToEither result completes exceptionally if either source cancelled |
2221 |
|
*/ |
2222 |
< |
public void testAcceptEither() { |
2223 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2224 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2225 |
< |
IncAction r = new IncAction(); |
2226 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2227 |
< |
f.complete(one); |
2228 |
< |
checkCompletedNormally(g, null); |
2229 |
< |
f2.complete(one); |
2230 |
< |
checkCompletedNormally(g, null); |
2231 |
< |
assertEquals(r.value, 2); |
2232 |
< |
|
2233 |
< |
r = new IncAction(); |
2234 |
< |
f = new CompletableFuture<>(); |
2235 |
< |
f.complete(one); |
2236 |
< |
f2 = new CompletableFuture<>(); |
2237 |
< |
g = f.acceptEither(f2, r); |
2238 |
< |
checkCompletedNormally(g, null); |
2239 |
< |
assertEquals(r.value, 2); |
2240 |
< |
} |
2222 |
> |
public void testApplyToEither_sourceCancelled() { |
2223 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2224 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2225 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2226 |
> |
{ |
2227 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2228 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2229 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2230 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2231 |
> |
|
2232 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2233 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2234 |
> |
checkIncomplete(h0); |
2235 |
> |
checkIncomplete(h1); |
2236 |
> |
rs[0].assertNotInvoked(); |
2237 |
> |
rs[1].assertNotInvoked(); |
2238 |
> |
f.cancel(mayInterruptIfRunning); |
2239 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2240 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2241 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2242 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2243 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2244 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2245 |
> |
g.complete(v1); |
2246 |
> |
|
2247 |
> |
// unspecified behavior - both source completions available |
2248 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2249 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2250 |
> |
try { |
2251 |
> |
assertEquals(inc(v1), h4.join()); |
2252 |
> |
rs[4].assertValue(inc(v1)); |
2253 |
> |
} catch (CompletionException ok) { |
2254 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2255 |
> |
rs[4].assertNotInvoked(); |
2256 |
> |
} |
2257 |
> |
try { |
2258 |
> |
assertEquals(inc(v1), h5.join()); |
2259 |
> |
rs[5].assertValue(inc(v1)); |
2260 |
> |
} catch (CompletionException ok) { |
2261 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2262 |
> |
rs[5].assertNotInvoked(); |
2263 |
> |
} |
2264 |
|
|
2265 |
< |
/** |
2266 |
< |
* acceptEither result completes exceptionally after exceptional |
2267 |
< |
* completion of either source |
2268 |
< |
*/ |
2269 |
< |
public void testAcceptEither2() { |
2270 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2271 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2272 |
< |
IncAction r = new IncAction(); |
2273 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2274 |
< |
f.completeExceptionally(new CFException()); |
2275 |
< |
f2.complete(one); |
2276 |
< |
checkCompletedWithWrappedCFException(g); |
2265 |
> |
checkCancelled(f); |
2266 |
> |
checkCompletedNormally(g, v1); |
2267 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2268 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2269 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2270 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2271 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2272 |
> |
}} |
2273 |
> |
|
2274 |
> |
public void testApplyToEither_sourceCancelled2() { |
2275 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2276 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2277 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2278 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2279 |
> |
{ |
2280 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2281 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2282 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2283 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2284 |
> |
|
2285 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2286 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2287 |
> |
assertTrue(fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2288 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.cancel(mayInterruptIfRunning)); |
2289 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2290 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2291 |
|
|
2292 |
< |
r = new IncAction(); |
2293 |
< |
f = new CompletableFuture<>(); |
2294 |
< |
f2 = new CompletableFuture<>(); |
2295 |
< |
f2.completeExceptionally(new CFException()); |
2296 |
< |
g = f.acceptEither(f2, r); |
2297 |
< |
checkCompletedWithWrappedCFException(g); |
2298 |
< |
} |
2292 |
> |
// unspecified behavior - both source completions available |
2293 |
> |
try { |
2294 |
> |
assertEquals(inc(v1), h0.join()); |
2295 |
> |
rs[0].assertValue(inc(v1)); |
2296 |
> |
} catch (CompletionException ok) { |
2297 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2298 |
> |
rs[0].assertNotInvoked(); |
2299 |
> |
} |
2300 |
> |
try { |
2301 |
> |
assertEquals(inc(v1), h1.join()); |
2302 |
> |
rs[1].assertValue(inc(v1)); |
2303 |
> |
} catch (CompletionException ok) { |
2304 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2305 |
> |
rs[1].assertNotInvoked(); |
2306 |
> |
} |
2307 |
> |
try { |
2308 |
> |
assertEquals(inc(v1), h2.join()); |
2309 |
> |
rs[2].assertValue(inc(v1)); |
2310 |
> |
} catch (CompletionException ok) { |
2311 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2312 |
> |
rs[2].assertNotInvoked(); |
2313 |
> |
} |
2314 |
> |
try { |
2315 |
> |
assertEquals(inc(v1), h3.join()); |
2316 |
> |
rs[3].assertValue(inc(v1)); |
2317 |
> |
} catch (CompletionException ok) { |
2318 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2319 |
> |
rs[3].assertNotInvoked(); |
2320 |
> |
} |
2321 |
|
|
2322 |
< |
/** |
2323 |
< |
* acceptEither result completes exceptionally if action does |
2324 |
< |
*/ |
1146 |
< |
public void testAcceptEither3() { |
1147 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1148 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1149 |
< |
FailingConsumer r = new FailingConsumer(); |
1150 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1151 |
< |
f2.complete(two); |
1152 |
< |
checkCompletedWithWrappedCFException(g); |
1153 |
< |
} |
2322 |
> |
checkCompletedNormally(f, v1); |
2323 |
> |
checkCancelled(g); |
2324 |
> |
}} |
2325 |
|
|
2326 |
|
/** |
2327 |
< |
* acceptEither result completes exceptionally if either source cancelled |
2327 |
> |
* applyToEither result completes exceptionally if action does |
2328 |
|
*/ |
2329 |
< |
public void testAcceptEither4() { |
2330 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2331 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2332 |
< |
IncAction r = new IncAction(); |
2333 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
2334 |
< |
assertTrue(f.cancel(true)); |
2335 |
< |
checkCompletedWithWrappedCancellationException(g); |
2336 |
< |
f = new CompletableFuture<>(); |
2337 |
< |
f2 = new CompletableFuture<>(); |
2338 |
< |
assertTrue(f2.cancel(true)); |
2339 |
< |
checkCompletedWithWrappedCancellationException(g); |
2340 |
< |
} |
2329 |
> |
public void testApplyToEither_actionFailed() { |
2330 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2331 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2332 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2333 |
> |
{ |
2334 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2335 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2336 |
> |
final FailingFunction[] rs = new FailingFunction[6]; |
2337 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
2338 |
> |
|
2339 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2340 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2341 |
> |
f.complete(v1); |
2342 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2343 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2344 |
> |
checkCompletedWithWrappedCFException(h0); |
2345 |
> |
checkCompletedWithWrappedCFException(h1); |
2346 |
> |
checkCompletedWithWrappedCFException(h2); |
2347 |
> |
checkCompletedWithWrappedCFException(h3); |
2348 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2349 |
> |
|
2350 |
> |
g.complete(v2); |
2351 |
> |
|
2352 |
> |
// unspecified behavior - both source completions available |
2353 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2354 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2355 |
> |
|
2356 |
> |
checkCompletedWithWrappedCFException(h4); |
2357 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2358 |
> |
Objects.equals(v2, rs[4].value)); |
2359 |
> |
checkCompletedWithWrappedCFException(h5); |
2360 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2361 |
> |
Objects.equals(v2, rs[5].value)); |
2362 |
> |
|
2363 |
> |
checkCompletedNormally(f, v1); |
2364 |
> |
checkCompletedNormally(g, v2); |
2365 |
> |
}} |
2366 |
|
|
2367 |
|
/** |
2368 |
< |
* runAfterEither result completes normally after normal completion |
2368 |
> |
* acceptEither result completes normally after normal completion |
2369 |
|
* of either source |
2370 |
|
*/ |
2371 |
< |
public void testRunAfterEither() { |
2372 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2373 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2374 |
< |
Noop r = new Noop(); |
2375 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2376 |
< |
f.complete(one); |
2377 |
< |
checkCompletedNormally(g, null); |
2378 |
< |
f2.complete(one); |
2379 |
< |
checkCompletedNormally(g, null); |
2380 |
< |
assertTrue(r.ran); |
2381 |
< |
|
2382 |
< |
r = new Noop(); |
2383 |
< |
f = new CompletableFuture<>(); |
2384 |
< |
f.complete(one); |
2385 |
< |
f2 = new CompletableFuture<>(); |
2386 |
< |
g = f.runAfterEither(f2, r); |
2387 |
< |
checkCompletedNormally(g, null); |
2388 |
< |
assertTrue(r.ran); |
2389 |
< |
} |
2371 |
> |
public void testAcceptEither_normalCompletion() { |
2372 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2373 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2374 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2375 |
> |
{ |
2376 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2377 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2378 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2379 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2380 |
> |
|
2381 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2382 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2383 |
> |
checkIncomplete(h0); |
2384 |
> |
checkIncomplete(h1); |
2385 |
> |
rs[0].assertNotInvoked(); |
2386 |
> |
rs[1].assertNotInvoked(); |
2387 |
> |
f.complete(v1); |
2388 |
> |
checkCompletedNormally(h0, null); |
2389 |
> |
checkCompletedNormally(h1, null); |
2390 |
> |
rs[0].assertValue(v1); |
2391 |
> |
rs[1].assertValue(v1); |
2392 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2393 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2394 |
> |
checkCompletedNormally(h2, null); |
2395 |
> |
checkCompletedNormally(h3, null); |
2396 |
> |
rs[2].assertValue(v1); |
2397 |
> |
rs[3].assertValue(v1); |
2398 |
> |
g.complete(v2); |
2399 |
> |
|
2400 |
> |
// unspecified behavior - both source completions available |
2401 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2402 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2403 |
> |
checkCompletedNormally(h4, null); |
2404 |
> |
checkCompletedNormally(h5, null); |
2405 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2406 |
> |
Objects.equals(v2, rs[4].value)); |
2407 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2408 |
> |
Objects.equals(v2, rs[5].value)); |
2409 |
> |
|
2410 |
> |
checkCompletedNormally(f, v1); |
2411 |
> |
checkCompletedNormally(g, v2); |
2412 |
> |
checkCompletedNormally(h0, null); |
2413 |
> |
checkCompletedNormally(h1, null); |
2414 |
> |
checkCompletedNormally(h2, null); |
2415 |
> |
checkCompletedNormally(h3, null); |
2416 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2417 |
> |
}} |
2418 |
|
|
2419 |
|
/** |
2420 |
< |
* runAfterEither result completes exceptionally after exceptional |
2420 |
> |
* acceptEither result completes exceptionally after exceptional |
2421 |
|
* completion of either source |
2422 |
|
*/ |
2423 |
< |
public void testRunAfterEither2() { |
2424 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2425 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2426 |
< |
Noop r = new Noop(); |
2427 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2428 |
< |
f.completeExceptionally(new CFException()); |
2429 |
< |
f2.complete(one); |
2430 |
< |
checkCompletedWithWrappedCFException(g); |
2423 |
> |
public void testAcceptEither_exceptionalCompletion() { |
2424 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2425 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2426 |
> |
{ |
2427 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2428 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2429 |
> |
final CFException ex = new CFException(); |
2430 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2431 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2432 |
> |
|
2433 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2434 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2435 |
> |
checkIncomplete(h0); |
2436 |
> |
checkIncomplete(h1); |
2437 |
> |
rs[0].assertNotInvoked(); |
2438 |
> |
rs[1].assertNotInvoked(); |
2439 |
> |
f.completeExceptionally(ex); |
2440 |
> |
checkCompletedWithWrappedException(h0, ex); |
2441 |
> |
checkCompletedWithWrappedException(h1, ex); |
2442 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2443 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2444 |
> |
checkCompletedWithWrappedException(h2, ex); |
2445 |
> |
checkCompletedWithWrappedException(h3, ex); |
2446 |
> |
|
2447 |
> |
g.complete(v1); |
2448 |
> |
|
2449 |
> |
// unspecified behavior - both source completions available |
2450 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2451 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2452 |
> |
try { |
2453 |
> |
assertNull(h4.join()); |
2454 |
> |
rs[4].assertValue(v1); |
2455 |
> |
} catch (CompletionException ok) { |
2456 |
> |
checkCompletedWithWrappedException(h4, ex); |
2457 |
> |
rs[4].assertNotInvoked(); |
2458 |
> |
} |
2459 |
> |
try { |
2460 |
> |
assertNull(h5.join()); |
2461 |
> |
rs[5].assertValue(v1); |
2462 |
> |
} catch (CompletionException ok) { |
2463 |
> |
checkCompletedWithWrappedException(h5, ex); |
2464 |
> |
rs[5].assertNotInvoked(); |
2465 |
> |
} |
2466 |
|
|
2467 |
< |
r = new Noop(); |
2468 |
< |
f = new CompletableFuture<>(); |
2469 |
< |
f2 = new CompletableFuture<>(); |
2470 |
< |
f2.completeExceptionally(new CFException()); |
2471 |
< |
g = f.runAfterEither(f2, r); |
2472 |
< |
checkCompletedWithWrappedCFException(g); |
2473 |
< |
} |
2467 |
> |
checkCompletedExceptionally(f, ex); |
2468 |
> |
checkCompletedNormally(g, v1); |
2469 |
> |
checkCompletedWithWrappedException(h0, ex); |
2470 |
> |
checkCompletedWithWrappedException(h1, ex); |
2471 |
> |
checkCompletedWithWrappedException(h2, ex); |
2472 |
> |
checkCompletedWithWrappedException(h3, ex); |
2473 |
> |
checkCompletedWithWrappedException(h4, ex); |
2474 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2475 |
> |
}} |
2476 |
> |
|
2477 |
> |
public void testAcceptEither_exceptionalCompletion2() { |
2478 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2479 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2480 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2481 |
> |
{ |
2482 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2483 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2484 |
> |
final CFException ex = new CFException(); |
2485 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2486 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2487 |
> |
|
2488 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2489 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2490 |
> |
assertTrue(fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2491 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2492 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2493 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2494 |
|
|
2495 |
< |
/** |
2496 |
< |
* runAfterEither result completes exceptionally if action does |
2497 |
< |
*/ |
2498 |
< |
public void testRunAfterEither3() { |
2499 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2500 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2501 |
< |
FailingNoop r = new FailingNoop(); |
2502 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
2503 |
< |
f2.complete(two); |
2504 |
< |
checkCompletedWithWrappedCFException(g); |
2505 |
< |
} |
2495 |
> |
// unspecified behavior - both source completions available |
2496 |
> |
try { |
2497 |
> |
assertEquals(null, h0.join()); |
2498 |
> |
rs[0].assertValue(v1); |
2499 |
> |
} catch (CompletionException ok) { |
2500 |
> |
checkCompletedWithWrappedException(h0, ex); |
2501 |
> |
rs[0].assertNotInvoked(); |
2502 |
> |
} |
2503 |
> |
try { |
2504 |
> |
assertEquals(null, h1.join()); |
2505 |
> |
rs[1].assertValue(v1); |
2506 |
> |
} catch (CompletionException ok) { |
2507 |
> |
checkCompletedWithWrappedException(h1, ex); |
2508 |
> |
rs[1].assertNotInvoked(); |
2509 |
> |
} |
2510 |
> |
try { |
2511 |
> |
assertEquals(null, h2.join()); |
2512 |
> |
rs[2].assertValue(v1); |
2513 |
> |
} catch (CompletionException ok) { |
2514 |
> |
checkCompletedWithWrappedException(h2, ex); |
2515 |
> |
rs[2].assertNotInvoked(); |
2516 |
> |
} |
2517 |
> |
try { |
2518 |
> |
assertEquals(null, h3.join()); |
2519 |
> |
rs[3].assertValue(v1); |
2520 |
> |
} catch (CompletionException ok) { |
2521 |
> |
checkCompletedWithWrappedException(h3, ex); |
2522 |
> |
rs[3].assertNotInvoked(); |
2523 |
> |
} |
2524 |
|
|
2525 |
< |
/** |
2526 |
< |
* runAfterEither result completes exceptionally if either source cancelled |
2527 |
< |
*/ |
1231 |
< |
public void testRunAfterEither4() { |
1232 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1233 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1234 |
< |
Noop r = new Noop(); |
1235 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
1236 |
< |
assertTrue(f.cancel(true)); |
1237 |
< |
checkCompletedWithWrappedCancellationException(g); |
1238 |
< |
f = new CompletableFuture<>(); |
1239 |
< |
f2 = new CompletableFuture<>(); |
1240 |
< |
assertTrue(f2.cancel(true)); |
1241 |
< |
checkCompletedWithWrappedCancellationException(g); |
1242 |
< |
} |
2525 |
> |
checkCompletedNormally(f, v1); |
2526 |
> |
checkCompletedExceptionally(g, ex); |
2527 |
> |
}} |
2528 |
|
|
2529 |
|
/** |
2530 |
< |
* thenCompose result completes normally after normal completion of source |
2530 |
> |
* acceptEither result completes exceptionally if either source cancelled |
2531 |
|
*/ |
2532 |
< |
public void testThenCompose() { |
2533 |
< |
CompletableFuture<Integer> f, g; |
2534 |
< |
CompletableFutureInc r; |
2535 |
< |
|
2536 |
< |
f = new CompletableFuture<>(); |
2537 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2538 |
< |
f.complete(one); |
2539 |
< |
checkCompletedNormally(g, two); |
2540 |
< |
assertTrue(r.ran); |
2532 |
> |
public void testAcceptEither_sourceCancelled() { |
2533 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2534 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
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 NoopConsumer[] rs = new NoopConsumer[6]; |
2540 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2541 |
> |
|
2542 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2543 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2544 |
> |
checkIncomplete(h0); |
2545 |
> |
checkIncomplete(h1); |
2546 |
> |
rs[0].assertNotInvoked(); |
2547 |
> |
rs[1].assertNotInvoked(); |
2548 |
> |
f.cancel(mayInterruptIfRunning); |
2549 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2550 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2551 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2552 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2553 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2554 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2555 |
> |
|
2556 |
> |
g.complete(v1); |
2557 |
> |
|
2558 |
> |
// unspecified behavior - both source completions available |
2559 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2560 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2561 |
> |
try { |
2562 |
> |
assertNull(h4.join()); |
2563 |
> |
rs[4].assertValue(v1); |
2564 |
> |
} catch (CompletionException ok) { |
2565 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2566 |
> |
rs[4].assertNotInvoked(); |
2567 |
> |
} |
2568 |
> |
try { |
2569 |
> |
assertNull(h5.join()); |
2570 |
> |
rs[5].assertValue(v1); |
2571 |
> |
} catch (CompletionException ok) { |
2572 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2573 |
> |
rs[5].assertNotInvoked(); |
2574 |
> |
} |
2575 |
|
|
2576 |
< |
f = new CompletableFuture<>(); |
2577 |
< |
f.complete(one); |
2578 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2579 |
< |
checkCompletedNormally(g, two); |
2580 |
< |
assertTrue(r.ran); |
2581 |
< |
} |
2576 |
> |
checkCancelled(f); |
2577 |
> |
checkCompletedNormally(g, v1); |
2578 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2579 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2580 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2581 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2582 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2583 |
> |
}} |
2584 |
|
|
2585 |
|
/** |
2586 |
< |
* thenCompose result completes exceptionally after exceptional |
1266 |
< |
* completion of source |
2586 |
> |
* acceptEither result completes exceptionally if action does |
2587 |
|
*/ |
2588 |
< |
public void testThenCompose2() { |
2589 |
< |
CompletableFuture<Integer> f, g; |
2590 |
< |
CompletableFutureInc r; |
2591 |
< |
|
2592 |
< |
f = new CompletableFuture<>(); |
2593 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2594 |
< |
f.completeExceptionally(new CFException()); |
2595 |
< |
checkCompletedWithWrappedCFException(g); |
2596 |
< |
|
2597 |
< |
f = new CompletableFuture<>(); |
2598 |
< |
f.completeExceptionally(new CFException()); |
2599 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2600 |
< |
checkCompletedWithWrappedCFException(g); |
2601 |
< |
} |
2588 |
> |
public void testAcceptEither_actionFailed() { |
2589 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2590 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2591 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2592 |
> |
{ |
2593 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2594 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2595 |
> |
final FailingConsumer[] rs = new FailingConsumer[6]; |
2596 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(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 |
> |
f.complete(v1); |
2601 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2602 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2603 |
> |
checkCompletedWithWrappedCFException(h0); |
2604 |
> |
checkCompletedWithWrappedCFException(h1); |
2605 |
> |
checkCompletedWithWrappedCFException(h2); |
2606 |
> |
checkCompletedWithWrappedCFException(h3); |
2607 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2608 |
> |
|
2609 |
> |
g.complete(v2); |
2610 |
> |
|
2611 |
> |
// unspecified behavior - both source completions available |
2612 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2613 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2614 |
> |
|
2615 |
> |
checkCompletedWithWrappedCFException(h4); |
2616 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2617 |
> |
Objects.equals(v2, rs[4].value)); |
2618 |
> |
checkCompletedWithWrappedCFException(h5); |
2619 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2620 |
> |
Objects.equals(v2, rs[5].value)); |
2621 |
> |
|
2622 |
> |
checkCompletedNormally(f, v1); |
2623 |
> |
checkCompletedNormally(g, v2); |
2624 |
> |
}} |
2625 |
|
|
2626 |
|
/** |
2627 |
< |
* thenCompose result completes exceptionally if action does |
2627 |
> |
* runAfterEither result completes normally after normal completion |
2628 |
> |
* of either source |
2629 |
|
*/ |
2630 |
< |
public void testThenCompose3() { |
2631 |
< |
CompletableFuture<Integer> f, g; |
2632 |
< |
FailingCompletableFutureFunction r; |
2633 |
< |
|
2634 |
< |
f = new CompletableFuture<>(); |
2635 |
< |
g = f.thenCompose(r = new FailingCompletableFutureFunction()); |
2636 |
< |
f.complete(one); |
2637 |
< |
checkCompletedWithWrappedCFException(g); |
2638 |
< |
|
2639 |
< |
f = new CompletableFuture<>(); |
2640 |
< |
f.complete(one); |
2641 |
< |
g = f.thenCompose(r = new FailingCompletableFutureFunction()); |
2642 |
< |
checkCompletedWithWrappedCFException(g); |
2643 |
< |
} |
2630 |
> |
public void testRunAfterEither_normalCompletion() { |
2631 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2632 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2633 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2634 |
> |
{ |
2635 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2636 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2637 |
> |
final Noop[] rs = new Noop[6]; |
2638 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2639 |
> |
|
2640 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2641 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2642 |
> |
checkIncomplete(h0); |
2643 |
> |
checkIncomplete(h1); |
2644 |
> |
rs[0].assertNotInvoked(); |
2645 |
> |
rs[1].assertNotInvoked(); |
2646 |
> |
f.complete(v1); |
2647 |
> |
checkCompletedNormally(h0, null); |
2648 |
> |
checkCompletedNormally(h1, null); |
2649 |
> |
rs[0].assertInvoked(); |
2650 |
> |
rs[1].assertInvoked(); |
2651 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2652 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2653 |
> |
checkCompletedNormally(h2, null); |
2654 |
> |
checkCompletedNormally(h3, null); |
2655 |
> |
rs[2].assertInvoked(); |
2656 |
> |
rs[3].assertInvoked(); |
2657 |
> |
|
2658 |
> |
g.complete(v2); |
2659 |
> |
|
2660 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2661 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2662 |
> |
|
2663 |
> |
checkCompletedNormally(f, v1); |
2664 |
> |
checkCompletedNormally(g, v2); |
2665 |
> |
checkCompletedNormally(h0, null); |
2666 |
> |
checkCompletedNormally(h1, null); |
2667 |
> |
checkCompletedNormally(h2, null); |
2668 |
> |
checkCompletedNormally(h3, null); |
2669 |
> |
checkCompletedNormally(h4, null); |
2670 |
> |
checkCompletedNormally(h5, null); |
2671 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2672 |
> |
}} |
2673 |
|
|
2674 |
|
/** |
2675 |
< |
* thenCompose result completes exceptionally if source cancelled |
2675 |
> |
* runAfterEither result completes exceptionally after exceptional |
2676 |
> |
* completion of either source |
2677 |
|
*/ |
2678 |
< |
public void testThenCompose4() { |
2679 |
< |
CompletableFuture<Integer> f, g; |
2680 |
< |
CompletableFutureInc r; |
2681 |
< |
|
2682 |
< |
f = new CompletableFuture<>(); |
2683 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2684 |
< |
assertTrue(f.cancel(true)); |
2685 |
< |
checkCompletedWithWrappedCancellationException(g); |
2686 |
< |
|
2687 |
< |
f = new CompletableFuture<>(); |
2688 |
< |
assertTrue(f.cancel(true)); |
2689 |
< |
g = f.thenCompose(r = new CompletableFutureInc()); |
2690 |
< |
checkCompletedWithWrappedCancellationException(g); |
2691 |
< |
} |
2678 |
> |
public void testRunAfterEither_exceptionalCompletion() { |
2679 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2680 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2681 |
> |
{ |
2682 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2683 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2684 |
> |
final CFException ex = new CFException(); |
2685 |
> |
final Noop[] rs = new Noop[6]; |
2686 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2687 |
> |
|
2688 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2689 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2690 |
> |
checkIncomplete(h0); |
2691 |
> |
checkIncomplete(h1); |
2692 |
> |
rs[0].assertNotInvoked(); |
2693 |
> |
rs[1].assertNotInvoked(); |
2694 |
> |
assertTrue(f.completeExceptionally(ex)); |
2695 |
> |
checkCompletedWithWrappedException(h0, ex); |
2696 |
> |
checkCompletedWithWrappedException(h1, ex); |
2697 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2698 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2699 |
> |
checkCompletedWithWrappedException(h2, ex); |
2700 |
> |
checkCompletedWithWrappedException(h3, ex); |
2701 |
> |
|
2702 |
> |
assertTrue(g.complete(v1)); |
2703 |
> |
|
2704 |
> |
// unspecified behavior - both source completions available |
2705 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2706 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2707 |
> |
try { |
2708 |
> |
assertNull(h4.join()); |
2709 |
> |
rs[4].assertInvoked(); |
2710 |
> |
} catch (CompletionException ok) { |
2711 |
> |
checkCompletedWithWrappedException(h4, ex); |
2712 |
> |
rs[4].assertNotInvoked(); |
2713 |
> |
} |
2714 |
> |
try { |
2715 |
> |
assertNull(h5.join()); |
2716 |
> |
rs[5].assertInvoked(); |
2717 |
> |
} catch (CompletionException ok) { |
2718 |
> |
checkCompletedWithWrappedException(h5, ex); |
2719 |
> |
rs[5].assertNotInvoked(); |
2720 |
> |
} |
2721 |
|
|
2722 |
< |
// asyncs |
2722 |
> |
checkCompletedExceptionally(f, ex); |
2723 |
> |
checkCompletedNormally(g, v1); |
2724 |
> |
checkCompletedWithWrappedException(h0, ex); |
2725 |
> |
checkCompletedWithWrappedException(h1, ex); |
2726 |
> |
checkCompletedWithWrappedException(h2, ex); |
2727 |
> |
checkCompletedWithWrappedException(h3, ex); |
2728 |
> |
checkCompletedWithWrappedException(h4, ex); |
2729 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2730 |
> |
}} |
2731 |
> |
|
2732 |
> |
public void testRunAfterEither_exceptionalCompletion2() { |
2733 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2734 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2735 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2736 |
> |
{ |
2737 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2738 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2739 |
> |
final CFException ex = new CFException(); |
2740 |
> |
final Noop[] rs = new Noop[6]; |
2741 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2742 |
> |
|
2743 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2744 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2745 |
> |
assertTrue( fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2746 |
> |
assertTrue(!fFirst ? f.complete(v1) : g.completeExceptionally(ex)); |
2747 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2748 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2749 |
|
|
2750 |
< |
/** |
2751 |
< |
* thenRunAsync result completes normally after normal completion of source |
2752 |
< |
*/ |
2753 |
< |
public void testThenRunAsync() { |
2754 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2755 |
< |
Noop r = new Noop(); |
2756 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2757 |
< |
f.complete(null); |
2758 |
< |
checkCompletedNormally(g, null); |
2750 |
> |
// unspecified behavior - both source completions available |
2751 |
> |
try { |
2752 |
> |
assertEquals(null, h0.join()); |
2753 |
> |
rs[0].assertInvoked(); |
2754 |
> |
} catch (CompletionException ok) { |
2755 |
> |
checkCompletedWithWrappedException(h0, ex); |
2756 |
> |
rs[0].assertNotInvoked(); |
2757 |
> |
} |
2758 |
> |
try { |
2759 |
> |
assertEquals(null, h1.join()); |
2760 |
> |
rs[1].assertInvoked(); |
2761 |
> |
} catch (CompletionException ok) { |
2762 |
> |
checkCompletedWithWrappedException(h1, ex); |
2763 |
> |
rs[1].assertNotInvoked(); |
2764 |
> |
} |
2765 |
> |
try { |
2766 |
> |
assertEquals(null, h2.join()); |
2767 |
> |
rs[2].assertInvoked(); |
2768 |
> |
} catch (CompletionException ok) { |
2769 |
> |
checkCompletedWithWrappedException(h2, ex); |
2770 |
> |
rs[2].assertNotInvoked(); |
2771 |
> |
} |
2772 |
> |
try { |
2773 |
> |
assertEquals(null, h3.join()); |
2774 |
> |
rs[3].assertInvoked(); |
2775 |
> |
} catch (CompletionException ok) { |
2776 |
> |
checkCompletedWithWrappedException(h3, ex); |
2777 |
> |
rs[3].assertNotInvoked(); |
2778 |
> |
} |
2779 |
|
|
2780 |
< |
// reordered version |
2781 |
< |
f = new CompletableFuture<>(); |
2782 |
< |
f.complete(null); |
1334 |
< |
r = new Noop(); |
1335 |
< |
g = f.thenRunAsync(r); |
1336 |
< |
checkCompletedNormally(g, null); |
1337 |
< |
} |
2780 |
> |
checkCompletedNormally(f, v1); |
2781 |
> |
checkCompletedExceptionally(g, ex); |
2782 |
> |
}} |
2783 |
|
|
2784 |
|
/** |
2785 |
< |
* thenRunAsync result completes exceptionally after exceptional |
1341 |
< |
* completion of source |
2785 |
> |
* runAfterEither result completes exceptionally if either source cancelled |
2786 |
|
*/ |
2787 |
< |
public void testThenRunAsync2() { |
2788 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2789 |
< |
Noop r = new Noop(); |
2790 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2791 |
< |
f.completeExceptionally(new CFException()); |
2787 |
> |
public void testRunAfterEither_sourceCancelled() { |
2788 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2789 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2790 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2791 |
> |
{ |
2792 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2793 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2794 |
> |
final Noop[] rs = new Noop[6]; |
2795 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2796 |
> |
|
2797 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2798 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2799 |
> |
checkIncomplete(h0); |
2800 |
> |
checkIncomplete(h1); |
2801 |
> |
rs[0].assertNotInvoked(); |
2802 |
> |
rs[1].assertNotInvoked(); |
2803 |
> |
f.cancel(mayInterruptIfRunning); |
2804 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2805 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2806 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2807 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2808 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2809 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2810 |
> |
|
2811 |
> |
assertTrue(g.complete(v1)); |
2812 |
> |
|
2813 |
> |
// unspecified behavior - both source completions available |
2814 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2815 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2816 |
|
try { |
2817 |
< |
g.join(); |
2818 |
< |
shouldThrow(); |
2819 |
< |
} catch (CompletionException success) {} |
2820 |
< |
checkCompletedWithWrappedCFException(g); |
2821 |
< |
} |
2817 |
> |
assertNull(h4.join()); |
2818 |
> |
rs[4].assertInvoked(); |
2819 |
> |
} catch (CompletionException ok) { |
2820 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2821 |
> |
rs[4].assertNotInvoked(); |
2822 |
> |
} |
2823 |
> |
try { |
2824 |
> |
assertNull(h5.join()); |
2825 |
> |
rs[5].assertInvoked(); |
2826 |
> |
} catch (CompletionException ok) { |
2827 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2828 |
> |
rs[5].assertNotInvoked(); |
2829 |
> |
} |
2830 |
|
|
2831 |
< |
/** |
2832 |
< |
* thenRunAsync result completes exceptionally if action does |
2833 |
< |
*/ |
2834 |
< |
public void testThenRunAsync3() { |
2835 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2836 |
< |
FailingNoop r = new FailingNoop(); |
2837 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2838 |
< |
f.complete(null); |
1363 |
< |
checkCompletedWithWrappedCFException(g); |
1364 |
< |
} |
2831 |
> |
checkCancelled(f); |
2832 |
> |
checkCompletedNormally(g, v1); |
2833 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2834 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2835 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2836 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2837 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2838 |
> |
}} |
2839 |
|
|
2840 |
|
/** |
2841 |
< |
* thenRunAsync result completes exceptionally if source cancelled |
2841 |
> |
* runAfterEither result completes exceptionally if action does |
2842 |
|
*/ |
2843 |
< |
public void testThenRunAsync4() { |
2844 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2845 |
< |
Noop r = new Noop(); |
2846 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
2847 |
< |
assertTrue(f.cancel(true)); |
2848 |
< |
checkCompletedWithWrappedCancellationException(g); |
2849 |
< |
} |
2843 |
> |
public void testRunAfterEither_actionFailed() { |
2844 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2845 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2846 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2847 |
> |
{ |
2848 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2849 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2850 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
2851 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
2852 |
> |
|
2853 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2854 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2855 |
> |
assertTrue(f.complete(v1)); |
2856 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2857 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2858 |
> |
checkCompletedWithWrappedCFException(h0); |
2859 |
> |
checkCompletedWithWrappedCFException(h1); |
2860 |
> |
checkCompletedWithWrappedCFException(h2); |
2861 |
> |
checkCompletedWithWrappedCFException(h3); |
2862 |
> |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2863 |
> |
assertTrue(g.complete(v2)); |
2864 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2865 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2866 |
> |
checkCompletedWithWrappedCFException(h4); |
2867 |
> |
checkCompletedWithWrappedCFException(h5); |
2868 |
> |
|
2869 |
> |
checkCompletedNormally(f, v1); |
2870 |
> |
checkCompletedNormally(g, v2); |
2871 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2872 |
> |
}} |
2873 |
|
|
2874 |
|
/** |
2875 |
< |
* thenApplyAsync result completes normally after normal completion of source |
2875 |
> |
* thenCompose result completes normally after normal completion of source |
2876 |
|
*/ |
2877 |
< |
public void testThenApplyAsync() { |
2878 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2879 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
2880 |
< |
f.complete(one); |
2881 |
< |
checkCompletedNormally(g, two); |
2882 |
< |
} |
2877 |
> |
public void testThenCompose_normalCompletion() { |
2878 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2879 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2880 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2881 |
> |
{ |
2882 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2883 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2884 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2885 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2886 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2887 |
> |
|
2888 |
> |
checkCompletedNormally(g, inc(v1)); |
2889 |
> |
checkCompletedNormally(f, v1); |
2890 |
> |
r.assertValue(v1); |
2891 |
> |
}} |
2892 |
|
|
2893 |
|
/** |
2894 |
< |
* thenApplyAsync result completes exceptionally after exceptional |
2894 |
> |
* thenCompose result completes exceptionally after exceptional |
2895 |
|
* completion of source |
2896 |
|
*/ |
2897 |
< |
public void testThenApplyAsync2() { |
2898 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2899 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
2900 |
< |
f.completeExceptionally(new CFException()); |
2901 |
< |
checkCompletedWithWrappedCFException(g); |
2902 |
< |
} |
2897 |
> |
public void testThenCompose_exceptionalCompletion() { |
2898 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2899 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2900 |
> |
{ |
2901 |
> |
final CFException ex = new CFException(); |
2902 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2903 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2904 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
2905 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2906 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
2907 |
> |
|
2908 |
> |
checkCompletedWithWrappedException(g, ex); |
2909 |
> |
checkCompletedExceptionally(f, ex); |
2910 |
> |
r.assertNotInvoked(); |
2911 |
> |
}} |
2912 |
|
|
2913 |
|
/** |
2914 |
< |
* thenApplyAsync result completes exceptionally if action does |
1400 |
< |
*/ |
1401 |
< |
public void testThenApplyAsync3() { |
1402 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1403 |
< |
FailingFunction r = new FailingFunction(); |
1404 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(r); |
1405 |
< |
f.complete(null); |
1406 |
< |
checkCompletedWithWrappedCFException(g); |
1407 |
< |
} |
1408 |
< |
|
1409 |
< |
/** |
1410 |
< |
* thenApplyAsync result completes exceptionally if source cancelled |
1411 |
< |
*/ |
1412 |
< |
public void testThenApplyAsync4() { |
1413 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1414 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
1415 |
< |
assertTrue(f.cancel(true)); |
1416 |
< |
checkCompletedWithWrappedCancellationException(g); |
1417 |
< |
} |
1418 |
< |
|
1419 |
< |
/** |
1420 |
< |
* thenAcceptAsync result completes normally after normal |
1421 |
< |
* completion of source |
2914 |
> |
* thenCompose result completes exceptionally if action does |
2915 |
|
*/ |
2916 |
< |
public void testThenAcceptAsync() { |
2917 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2918 |
< |
IncAction r = new IncAction(); |
2919 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2920 |
< |
f.complete(one); |
2921 |
< |
checkCompletedNormally(g, null); |
2922 |
< |
assertEquals(r.value, 2); |
2923 |
< |
} |
2916 |
> |
public void testThenCompose_actionFailed() { |
2917 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2918 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2919 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2920 |
> |
{ |
2921 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2922 |
> |
final FailingCompletableFutureFunction r |
2923 |
> |
= new FailingCompletableFutureFunction(m); |
2924 |
> |
if (!createIncomplete) assertTrue(f.complete(v1)); |
2925 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2926 |
> |
if (createIncomplete) assertTrue(f.complete(v1)); |
2927 |
|
|
1432 |
– |
/** |
1433 |
– |
* thenAcceptAsync result completes exceptionally after exceptional |
1434 |
– |
* completion of source |
1435 |
– |
*/ |
1436 |
– |
public void testThenAcceptAsync2() { |
1437 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1438 |
– |
IncAction r = new IncAction(); |
1439 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1440 |
– |
f.completeExceptionally(new CFException()); |
2928 |
|
checkCompletedWithWrappedCFException(g); |
2929 |
< |
} |
2929 |
> |
checkCompletedNormally(f, v1); |
2930 |
> |
}} |
2931 |
|
|
2932 |
|
/** |
2933 |
< |
* thenAcceptAsync result completes exceptionally if action does |
2933 |
> |
* thenCompose result completes exceptionally if source cancelled |
2934 |
|
*/ |
2935 |
< |
public void testThenAcceptAsync3() { |
2936 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2937 |
< |
FailingConsumer r = new FailingConsumer(); |
2938 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
2939 |
< |
f.complete(null); |
2940 |
< |
checkCompletedWithWrappedCFException(g); |
2941 |
< |
} |
2935 |
> |
public void testThenCompose_sourceCancelled() { |
2936 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2937 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2938 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2939 |
> |
{ |
2940 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2941 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2942 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2943 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2944 |
> |
if (createIncomplete) { |
2945 |
> |
checkIncomplete(g); |
2946 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2947 |
> |
} |
2948 |
|
|
1455 |
– |
/** |
1456 |
– |
* thenAcceptAsync result completes exceptionally if source cancelled |
1457 |
– |
*/ |
1458 |
– |
public void testThenAcceptAsync4() { |
1459 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1460 |
– |
IncAction r = new IncAction(); |
1461 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1462 |
– |
assertTrue(f.cancel(true)); |
2949 |
|
checkCompletedWithWrappedCancellationException(g); |
2950 |
< |
} |
2950 |
> |
checkCancelled(f); |
2951 |
> |
}} |
2952 |
|
|
2953 |
|
/** |
2954 |
< |
* thenCombineAsync result completes normally after normal |
1468 |
< |
* completion of sources |
2954 |
> |
* thenCompose result completes exceptionally if the result of the action does |
2955 |
|
*/ |
2956 |
< |
public void testThenCombineAsync() { |
2957 |
< |
CompletableFuture<Integer> f, g, h; |
2956 |
> |
public void testThenCompose_actionReturnsFailingFuture() { |
2957 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2958 |
> |
for (int order = 0; order < 6; order++) |
2959 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2960 |
> |
{ |
2961 |
> |
final CFException ex = new CFException(); |
2962 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2963 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2964 |
> |
final CompletableFuture<Integer> h; |
2965 |
> |
// Test all permutations of orders |
2966 |
> |
switch (order) { |
2967 |
> |
case 0: |
2968 |
> |
assertTrue(f.complete(v1)); |
2969 |
> |
assertTrue(g.completeExceptionally(ex)); |
2970 |
> |
h = m.thenCompose(f, (x -> g)); |
2971 |
> |
break; |
2972 |
> |
case 1: |
2973 |
> |
assertTrue(f.complete(v1)); |
2974 |
> |
h = m.thenCompose(f, (x -> g)); |
2975 |
> |
assertTrue(g.completeExceptionally(ex)); |
2976 |
> |
break; |
2977 |
> |
case 2: |
2978 |
> |
assertTrue(g.completeExceptionally(ex)); |
2979 |
> |
assertTrue(f.complete(v1)); |
2980 |
> |
h = m.thenCompose(f, (x -> g)); |
2981 |
> |
break; |
2982 |
> |
case 3: |
2983 |
> |
assertTrue(g.completeExceptionally(ex)); |
2984 |
> |
h = m.thenCompose(f, (x -> g)); |
2985 |
> |
assertTrue(f.complete(v1)); |
2986 |
> |
break; |
2987 |
> |
case 4: |
2988 |
> |
h = m.thenCompose(f, (x -> g)); |
2989 |
> |
assertTrue(f.complete(v1)); |
2990 |
> |
assertTrue(g.completeExceptionally(ex)); |
2991 |
> |
break; |
2992 |
> |
case 5: |
2993 |
> |
h = m.thenCompose(f, (x -> g)); |
2994 |
> |
assertTrue(f.complete(v1)); |
2995 |
> |
assertTrue(g.completeExceptionally(ex)); |
2996 |
> |
break; |
2997 |
> |
default: throw new AssertionError(); |
2998 |
> |
} |
2999 |
|
|
3000 |
< |
f = new CompletableFuture<>(); |
3001 |
< |
g = new CompletableFuture<>(); |
3002 |
< |
h = f.thenCombineAsync(g, subtract); |
3003 |
< |
f.complete(3); |
1477 |
< |
checkIncomplete(h); |
1478 |
< |
g.complete(1); |
1479 |
< |
checkCompletedNormally(h, 2); |
3000 |
> |
checkCompletedExceptionally(g, ex); |
3001 |
> |
checkCompletedWithWrappedException(h, ex); |
3002 |
> |
checkCompletedNormally(f, v1); |
3003 |
> |
}} |
3004 |
|
|
3005 |
< |
f = new CompletableFuture<>(); |
1482 |
< |
g = new CompletableFuture<>(); |
1483 |
< |
h = f.thenCombineAsync(g, subtract); |
1484 |
< |
g.complete(1); |
1485 |
< |
checkIncomplete(h); |
1486 |
< |
f.complete(3); |
1487 |
< |
checkCompletedNormally(h, 2); |
1488 |
< |
|
1489 |
< |
f = new CompletableFuture<>(); |
1490 |
< |
g = new CompletableFuture<>(); |
1491 |
< |
g.complete(1); |
1492 |
< |
f.complete(3); |
1493 |
< |
h = f.thenCombineAsync(g, subtract); |
1494 |
< |
checkCompletedNormally(h, 2); |
1495 |
< |
} |
3005 |
> |
// other static methods |
3006 |
|
|
3007 |
|
/** |
3008 |
< |
* thenCombineAsync result completes exceptionally after exceptional |
3009 |
< |
* completion of either source |
3008 |
> |
* allOf(no component futures) returns a future completed normally |
3009 |
> |
* with the value null |
3010 |
|
*/ |
3011 |
< |
public void testThenCombineAsync2() { |
3012 |
< |
CompletableFuture<Integer> f, g, h; |
3013 |
< |
|
1504 |
< |
f = new CompletableFuture<>(); |
1505 |
< |
g = new CompletableFuture<>(); |
1506 |
< |
h = f.thenCombineAsync(g, subtract); |
1507 |
< |
f.completeExceptionally(new CFException()); |
1508 |
< |
checkIncomplete(h); |
1509 |
< |
g.complete(1); |
1510 |
< |
checkCompletedWithWrappedCFException(h); |
1511 |
< |
|
1512 |
< |
f = new CompletableFuture<>(); |
1513 |
< |
g = new CompletableFuture<>(); |
1514 |
< |
h = f.thenCombineAsync(g, subtract); |
1515 |
< |
g.completeExceptionally(new CFException()); |
1516 |
< |
checkIncomplete(h); |
1517 |
< |
f.complete(3); |
1518 |
< |
checkCompletedWithWrappedCFException(h); |
1519 |
< |
|
1520 |
< |
f = new CompletableFuture<>(); |
1521 |
< |
g = new CompletableFuture<>(); |
1522 |
< |
g.completeExceptionally(new CFException()); |
1523 |
< |
f.complete(3); |
1524 |
< |
h = f.thenCombineAsync(g, subtract); |
1525 |
< |
checkCompletedWithWrappedCFException(h); |
3011 |
> |
public void testAllOf_empty() throws Exception { |
3012 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(); |
3013 |
> |
checkCompletedNormally(f, null); |
3014 |
|
} |
3015 |
|
|
3016 |
|
/** |
3017 |
< |
* thenCombineAsync result completes exceptionally if action does |
3017 |
> |
* allOf returns a future completed normally with the value null |
3018 |
> |
* when all components complete normally |
3019 |
|
*/ |
3020 |
< |
public void testThenCombineAsync3() { |
3021 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3022 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3023 |
< |
FailingBiFunction r = new FailingBiFunction(); |
3024 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r); |
3025 |
< |
f.complete(one); |
3026 |
< |
checkIncomplete(g); |
3027 |
< |
assertFalse(r.ran); |
3028 |
< |
f2.complete(two); |
3029 |
< |
checkCompletedWithWrappedCFException(g); |
3030 |
< |
assertTrue(r.ran); |
3020 |
> |
public void testAllOf_normal() throws Exception { |
3021 |
> |
for (int k = 1; k < 10; k++) { |
3022 |
> |
CompletableFuture<Integer>[] fs |
3023 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3024 |
> |
for (int i = 0; i < k; i++) |
3025 |
> |
fs[i] = new CompletableFuture<>(); |
3026 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3027 |
> |
for (int i = 0; i < k; i++) { |
3028 |
> |
checkIncomplete(f); |
3029 |
> |
checkIncomplete(CompletableFuture.allOf(fs)); |
3030 |
> |
fs[i].complete(one); |
3031 |
> |
} |
3032 |
> |
checkCompletedNormally(f, null); |
3033 |
> |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3034 |
> |
} |
3035 |
|
} |
3036 |
|
|
3037 |
< |
/** |
3038 |
< |
* thenCombineAsync result completes exceptionally if either source cancelled |
3039 |
< |
*/ |
3040 |
< |
public void testThenCombineAsync4() { |
3041 |
< |
CompletableFuture<Integer> f, g, h; |
3042 |
< |
|
3043 |
< |
f = new CompletableFuture<>(); |
3044 |
< |
g = new CompletableFuture<>(); |
3045 |
< |
h = f.thenCombineAsync(g, subtract); |
3046 |
< |
assertTrue(f.cancel(true)); |
3047 |
< |
checkIncomplete(h); |
3048 |
< |
g.complete(1); |
3049 |
< |
checkCompletedWithWrappedCancellationException(h); |
3050 |
< |
|
3051 |
< |
f = new CompletableFuture<>(); |
1559 |
< |
g = new CompletableFuture<>(); |
1560 |
< |
h = f.thenCombineAsync(g, subtract); |
1561 |
< |
assertTrue(g.cancel(true)); |
1562 |
< |
checkIncomplete(h); |
1563 |
< |
f.complete(3); |
1564 |
< |
checkCompletedWithWrappedCancellationException(h); |
1565 |
< |
|
1566 |
< |
f = new CompletableFuture<>(); |
1567 |
< |
g = new CompletableFuture<>(); |
1568 |
< |
g.complete(3); |
1569 |
< |
assertTrue(f.cancel(true)); |
1570 |
< |
h = f.thenCombineAsync(g, subtract); |
1571 |
< |
checkCompletedWithWrappedCancellationException(h); |
1572 |
< |
|
1573 |
< |
f = new CompletableFuture<>(); |
1574 |
< |
g = new CompletableFuture<>(); |
1575 |
< |
f.complete(3); |
1576 |
< |
assertTrue(g.cancel(true)); |
1577 |
< |
h = f.thenCombineAsync(g, subtract); |
1578 |
< |
checkCompletedWithWrappedCancellationException(h); |
3037 |
> |
public void testAllOf_backwards() throws Exception { |
3038 |
> |
for (int k = 1; k < 10; k++) { |
3039 |
> |
CompletableFuture<Integer>[] fs |
3040 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3041 |
> |
for (int i = 0; i < k; i++) |
3042 |
> |
fs[i] = new CompletableFuture<>(); |
3043 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3044 |
> |
for (int i = k - 1; i >= 0; i--) { |
3045 |
> |
checkIncomplete(f); |
3046 |
> |
checkIncomplete(CompletableFuture.allOf(fs)); |
3047 |
> |
fs[i].complete(one); |
3048 |
> |
} |
3049 |
> |
checkCompletedNormally(f, null); |
3050 |
> |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3051 |
> |
} |
3052 |
|
} |
3053 |
|
|
3054 |
< |
/** |
3055 |
< |
* thenAcceptBothAsync result completes normally after normal |
3056 |
< |
* completion of sources |
3057 |
< |
*/ |
3058 |
< |
public void testThenAcceptBothAsync() { |
3059 |
< |
CompletableFuture<Integer> f, g; |
3060 |
< |
CompletableFuture<Void> h; |
3061 |
< |
SubtractAction r; |
3062 |
< |
|
3063 |
< |
f = new CompletableFuture<>(); |
3064 |
< |
g = new CompletableFuture<>(); |
3065 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
3066 |
< |
f.complete(3); |
3067 |
< |
checkIncomplete(h); |
3068 |
< |
g.complete(1); |
3069 |
< |
checkCompletedNormally(h, null); |
3070 |
< |
assertEquals(r.value, 2); |
3071 |
< |
|
3072 |
< |
f = new CompletableFuture<>(); |
3073 |
< |
g = new CompletableFuture<>(); |
3074 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
3075 |
< |
g.complete(1); |
1603 |
< |
checkIncomplete(h); |
1604 |
< |
f.complete(3); |
1605 |
< |
checkCompletedNormally(h, null); |
1606 |
< |
assertEquals(r.value, 2); |
1607 |
< |
|
1608 |
< |
f = new CompletableFuture<>(); |
1609 |
< |
g = new CompletableFuture<>(); |
1610 |
< |
g.complete(1); |
1611 |
< |
f.complete(3); |
1612 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1613 |
< |
checkCompletedNormally(h, null); |
1614 |
< |
assertEquals(r.value, 2); |
3054 |
> |
public void testAllOf_exceptional() throws Exception { |
3055 |
> |
for (int k = 1; k < 10; k++) { |
3056 |
> |
CompletableFuture<Integer>[] fs |
3057 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3058 |
> |
CFException ex = new CFException(); |
3059 |
> |
for (int i = 0; i < k; i++) |
3060 |
> |
fs[i] = new CompletableFuture<>(); |
3061 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
3062 |
> |
for (int i = 0; i < k; i++) { |
3063 |
> |
checkIncomplete(f); |
3064 |
> |
checkIncomplete(CompletableFuture.allOf(fs)); |
3065 |
> |
if (i != k / 2) { |
3066 |
> |
fs[i].complete(i); |
3067 |
> |
checkCompletedNormally(fs[i], i); |
3068 |
> |
} else { |
3069 |
> |
fs[i].completeExceptionally(ex); |
3070 |
> |
checkCompletedExceptionally(fs[i], ex); |
3071 |
> |
} |
3072 |
> |
} |
3073 |
> |
checkCompletedWithWrappedException(f, ex); |
3074 |
> |
checkCompletedWithWrappedException(CompletableFuture.allOf(fs), ex); |
3075 |
> |
} |
3076 |
|
} |
3077 |
|
|
3078 |
|
/** |
3079 |
< |
* thenAcceptBothAsync result completes exceptionally after exceptional |
1619 |
< |
* completion of source |
3079 |
> |
* anyOf(no component futures) returns an incomplete future |
3080 |
|
*/ |
3081 |
< |
public void testThenAcceptBothAsync2() { |
3082 |
< |
CompletableFuture<Integer> f, g; |
3083 |
< |
CompletableFuture<Void> h; |
3084 |
< |
SubtractAction r; |
3085 |
< |
|
1626 |
< |
f = new CompletableFuture<>(); |
1627 |
< |
g = new CompletableFuture<>(); |
1628 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1629 |
< |
f.completeExceptionally(new CFException()); |
1630 |
< |
checkIncomplete(h); |
1631 |
< |
g.complete(1); |
1632 |
< |
checkCompletedWithWrappedCFException(h); |
1633 |
< |
|
1634 |
< |
f = new CompletableFuture<>(); |
1635 |
< |
g = new CompletableFuture<>(); |
1636 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1637 |
< |
g.completeExceptionally(new CFException()); |
1638 |
< |
checkIncomplete(h); |
1639 |
< |
f.complete(3); |
1640 |
< |
checkCompletedWithWrappedCFException(h); |
3081 |
> |
public void testAnyOf_empty() throws Exception { |
3082 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3083 |
> |
{ |
3084 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3085 |
> |
checkIncomplete(f); |
3086 |
|
|
3087 |
< |
f = new CompletableFuture<>(); |
3088 |
< |
g = new CompletableFuture<>(); |
3089 |
< |
f.complete(3); |
1645 |
< |
g.completeExceptionally(new CFException()); |
1646 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1647 |
< |
checkCompletedWithWrappedCFException(h); |
1648 |
< |
|
1649 |
< |
f = new CompletableFuture<>(); |
1650 |
< |
g = new CompletableFuture<>(); |
1651 |
< |
f.completeExceptionally(new CFException()); |
1652 |
< |
g.complete(3); |
1653 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1654 |
< |
checkCompletedWithWrappedCFException(h); |
1655 |
< |
} |
3087 |
> |
f.complete(v1); |
3088 |
> |
checkCompletedNormally(f, v1); |
3089 |
> |
}} |
3090 |
|
|
3091 |
|
/** |
3092 |
< |
* thenAcceptBothAsync result completes exceptionally if action does |
3092 |
> |
* anyOf returns a future completed normally with a value when |
3093 |
> |
* a component future does |
3094 |
|
*/ |
3095 |
< |
public void testThenAcceptBothAsync3() { |
3096 |
< |
CompletableFuture<Integer> f, g; |
3097 |
< |
CompletableFuture<Void> h; |
3098 |
< |
FailingBiConsumer r; |
3099 |
< |
|
3100 |
< |
f = new CompletableFuture<>(); |
3101 |
< |
g = new CompletableFuture<>(); |
3102 |
< |
h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer()); |
3103 |
< |
f.complete(3); |
3104 |
< |
checkIncomplete(h); |
3105 |
< |
g.complete(1); |
3106 |
< |
checkCompletedWithWrappedCFException(h); |
3107 |
< |
|
3108 |
< |
f = new CompletableFuture<>(); |
1674 |
< |
g = new CompletableFuture<>(); |
1675 |
< |
f.complete(3); |
1676 |
< |
g.complete(1); |
1677 |
< |
h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer()); |
1678 |
< |
checkCompletedWithWrappedCFException(h); |
3095 |
> |
public void testAnyOf_normal() throws Exception { |
3096 |
> |
for (int k = 0; k < 10; k++) { |
3097 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3098 |
> |
for (int i = 0; i < k; i++) |
3099 |
> |
fs[i] = new CompletableFuture<>(); |
3100 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3101 |
> |
checkIncomplete(f); |
3102 |
> |
for (int i = 0; i < k; i++) { |
3103 |
> |
fs[i].complete(i); |
3104 |
> |
checkCompletedNormally(f, 0); |
3105 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3106 |
> |
assertTrue(0 <= x && x <= i); |
3107 |
> |
} |
3108 |
> |
} |
3109 |
|
} |
3110 |
< |
|
3111 |
< |
/** |
3112 |
< |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
3113 |
< |
*/ |
3114 |
< |
public void testThenAcceptBothAsync4() { |
3115 |
< |
CompletableFuture<Integer> f, g; |
3116 |
< |
CompletableFuture<Void> h; |
3117 |
< |
SubtractAction r; |
3118 |
< |
|
3119 |
< |
f = new CompletableFuture<>(); |
3120 |
< |
g = new CompletableFuture<>(); |
3121 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
3122 |
< |
assertTrue(f.cancel(true)); |
3123 |
< |
checkIncomplete(h); |
1694 |
< |
g.complete(1); |
1695 |
< |
checkCompletedWithWrappedCancellationException(h); |
1696 |
< |
|
1697 |
< |
f = new CompletableFuture<>(); |
1698 |
< |
g = new CompletableFuture<>(); |
1699 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1700 |
< |
assertTrue(g.cancel(true)); |
1701 |
< |
checkIncomplete(h); |
1702 |
< |
f.complete(3); |
1703 |
< |
checkCompletedWithWrappedCancellationException(h); |
1704 |
< |
|
1705 |
< |
f = new CompletableFuture<>(); |
1706 |
< |
g = new CompletableFuture<>(); |
1707 |
< |
f.complete(3); |
1708 |
< |
assertTrue(g.cancel(true)); |
1709 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1710 |
< |
checkCompletedWithWrappedCancellationException(h); |
1711 |
< |
|
1712 |
< |
f = new CompletableFuture<>(); |
1713 |
< |
g = new CompletableFuture<>(); |
1714 |
< |
assertTrue(f.cancel(true)); |
1715 |
< |
g.complete(3); |
1716 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction()); |
1717 |
< |
checkCompletedWithWrappedCancellationException(h); |
3110 |
> |
public void testAnyOf_normal_backwards() throws Exception { |
3111 |
> |
for (int k = 0; k < 10; k++) { |
3112 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3113 |
> |
for (int i = 0; i < k; i++) |
3114 |
> |
fs[i] = new CompletableFuture<>(); |
3115 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3116 |
> |
checkIncomplete(f); |
3117 |
> |
for (int i = k - 1; i >= 0; i--) { |
3118 |
> |
fs[i].complete(i); |
3119 |
> |
checkCompletedNormally(f, k - 1); |
3120 |
> |
int x = (int) CompletableFuture.anyOf(fs).join(); |
3121 |
> |
assertTrue(i <= x && x <= k - 1); |
3122 |
> |
} |
3123 |
> |
} |
3124 |
|
} |
3125 |
|
|
3126 |
|
/** |
3127 |
< |
* runAfterBothAsync result completes normally after normal |
1722 |
< |
* completion of sources |
3127 |
> |
* anyOf result completes exceptionally when any component does. |
3128 |
|
*/ |
3129 |
< |
public void testRunAfterBothAsync() { |
3130 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3131 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3132 |
< |
Noop r = new Noop(); |
3133 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
3134 |
< |
f.complete(one); |
3135 |
< |
checkIncomplete(g); |
3136 |
< |
f2.complete(two); |
3137 |
< |
checkCompletedNormally(g, null); |
3138 |
< |
assertTrue(r.ran); |
3129 |
> |
public void testAnyOf_exceptional() throws Exception { |
3130 |
> |
for (int k = 0; k < 10; k++) { |
3131 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3132 |
> |
CFException[] exs = new CFException[k]; |
3133 |
> |
for (int i = 0; i < k; i++) { |
3134 |
> |
fs[i] = new CompletableFuture<>(); |
3135 |
> |
exs[i] = new CFException(); |
3136 |
> |
} |
3137 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3138 |
> |
checkIncomplete(f); |
3139 |
> |
for (int i = 0; i < k; i++) { |
3140 |
> |
fs[i].completeExceptionally(exs[i]); |
3141 |
> |
checkCompletedWithWrappedException(f, exs[0]); |
3142 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3143 |
> |
} |
3144 |
> |
} |
3145 |
|
} |
3146 |
|
|
3147 |
< |
/** |
3148 |
< |
* runAfterBothAsync result completes exceptionally after exceptional |
3149 |
< |
* completion of source |
3150 |
< |
*/ |
3151 |
< |
public void testRunAfterBothAsync2() { |
3152 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3153 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3154 |
< |
Noop r = new Noop(); |
3155 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
3156 |
< |
f.completeExceptionally(new CFException()); |
3157 |
< |
f2.complete(two); |
3158 |
< |
checkCompletedWithWrappedCFException(g); |
3159 |
< |
|
3160 |
< |
r = new Noop(); |
3161 |
< |
f = new CompletableFuture<>(); |
3162 |
< |
f2 = new CompletableFuture<>(); |
1752 |
< |
g = f.runAfterBothAsync(f2, r); |
1753 |
< |
f.complete(one); |
1754 |
< |
f2.completeExceptionally(new CFException()); |
1755 |
< |
checkCompletedWithWrappedCFException(g); |
3147 |
> |
public void testAnyOf_exceptional_backwards() throws Exception { |
3148 |
> |
for (int k = 0; k < 10; k++) { |
3149 |
> |
CompletableFuture[] fs = new CompletableFuture[k]; |
3150 |
> |
CFException[] exs = new CFException[k]; |
3151 |
> |
for (int i = 0; i < k; i++) { |
3152 |
> |
fs[i] = new CompletableFuture<>(); |
3153 |
> |
exs[i] = new CFException(); |
3154 |
> |
} |
3155 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3156 |
> |
checkIncomplete(f); |
3157 |
> |
for (int i = k - 1; i >= 0; i--) { |
3158 |
> |
fs[i].completeExceptionally(exs[i]); |
3159 |
> |
checkCompletedWithWrappedException(f, exs[k - 1]); |
3160 |
> |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
3161 |
> |
} |
3162 |
> |
} |
3163 |
|
} |
3164 |
|
|
3165 |
|
/** |
3166 |
< |
* runAfterBothAsync result completes exceptionally if action does |
3166 |
> |
* Completion methods throw NullPointerException with null arguments |
3167 |
|
*/ |
3168 |
< |
public void testRunAfterBothAsync3() { |
3168 |
> |
public void testNPE() { |
3169 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3170 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3171 |
< |
FailingNoop r = new FailingNoop(); |
3172 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1766 |
< |
f.complete(one); |
1767 |
< |
checkIncomplete(g); |
1768 |
< |
f2.complete(two); |
1769 |
< |
checkCompletedWithWrappedCFException(g); |
1770 |
< |
} |
3170 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3171 |
> |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
3172 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
3173 |
|
|
3174 |
< |
/** |
3175 |
< |
* runAfterBothAsync result completes exceptionally if either source cancelled |
3176 |
< |
*/ |
3177 |
< |
public void testRunAfterBothAsync4() { |
3178 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3179 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3180 |
< |
Noop r = new Noop(); |
3181 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
3182 |
< |
assertTrue(f.cancel(true)); |
3183 |
< |
f2.complete(two); |
3184 |
< |
checkCompletedWithWrappedCancellationException(g); |
3174 |
> |
Runnable[] throwingActions = { |
3175 |
> |
() -> CompletableFuture.supplyAsync(null), |
3176 |
> |
() -> CompletableFuture.supplyAsync(null, exec), |
3177 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.SYNC, 42), null), |
3178 |
> |
|
3179 |
> |
() -> CompletableFuture.runAsync(null), |
3180 |
> |
() -> CompletableFuture.runAsync(null, exec), |
3181 |
> |
() -> CompletableFuture.runAsync(() -> {}, null), |
3182 |
> |
|
3183 |
> |
() -> f.completeExceptionally(null), |
3184 |
> |
|
3185 |
> |
() -> f.thenApply(null), |
3186 |
> |
() -> f.thenApplyAsync(null), |
3187 |
> |
() -> f.thenApplyAsync((x) -> x, null), |
3188 |
> |
() -> f.thenApplyAsync(null, exec), |
3189 |
> |
|
3190 |
> |
() -> f.thenAccept(null), |
3191 |
> |
() -> f.thenAcceptAsync(null), |
3192 |
> |
() -> f.thenAcceptAsync((x) -> {} , null), |
3193 |
> |
() -> f.thenAcceptAsync(null, exec), |
3194 |
> |
|
3195 |
> |
() -> f.thenRun(null), |
3196 |
> |
() -> f.thenRunAsync(null), |
3197 |
> |
() -> f.thenRunAsync(() -> {} , null), |
3198 |
> |
() -> f.thenRunAsync(null, exec), |
3199 |
> |
|
3200 |
> |
() -> f.thenCombine(g, null), |
3201 |
> |
() -> f.thenCombineAsync(g, null), |
3202 |
> |
() -> f.thenCombineAsync(g, null, exec), |
3203 |
> |
() -> f.thenCombine(nullFuture, (x, y) -> x), |
3204 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x), |
3205 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec), |
3206 |
> |
() -> f.thenCombineAsync(g, (x, y) -> x, null), |
3207 |
> |
|
3208 |
> |
() -> f.thenAcceptBoth(g, null), |
3209 |
> |
() -> f.thenAcceptBothAsync(g, null), |
3210 |
> |
() -> f.thenAcceptBothAsync(g, null, exec), |
3211 |
> |
() -> f.thenAcceptBoth(nullFuture, (x, y) -> {}), |
3212 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}), |
3213 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec), |
3214 |
> |
() -> f.thenAcceptBothAsync(g, (x, y) -> {}, null), |
3215 |
> |
|
3216 |
> |
() -> f.runAfterBoth(g, null), |
3217 |
> |
() -> f.runAfterBothAsync(g, null), |
3218 |
> |
() -> f.runAfterBothAsync(g, null, exec), |
3219 |
> |
() -> f.runAfterBoth(nullFuture, () -> {}), |
3220 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}), |
3221 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}, exec), |
3222 |
> |
() -> f.runAfterBothAsync(g, () -> {}, null), |
3223 |
> |
|
3224 |
> |
() -> f.applyToEither(g, null), |
3225 |
> |
() -> f.applyToEitherAsync(g, null), |
3226 |
> |
() -> f.applyToEitherAsync(g, null, exec), |
3227 |
> |
() -> f.applyToEither(nullFuture, (x) -> x), |
3228 |
> |
() -> f.applyToEitherAsync(nullFuture, (x) -> x), |
3229 |
> |
() -> f.applyToEitherAsync(nullFuture, (x) -> x, exec), |
3230 |
> |
() -> f.applyToEitherAsync(g, (x) -> x, null), |
3231 |
> |
|
3232 |
> |
() -> f.acceptEither(g, null), |
3233 |
> |
() -> f.acceptEitherAsync(g, null), |
3234 |
> |
() -> f.acceptEitherAsync(g, null, exec), |
3235 |
> |
() -> f.acceptEither(nullFuture, (x) -> {}), |
3236 |
> |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}), |
3237 |
> |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec), |
3238 |
> |
() -> f.acceptEitherAsync(g, (x) -> {}, null), |
3239 |
> |
|
3240 |
> |
() -> f.runAfterEither(g, null), |
3241 |
> |
() -> f.runAfterEitherAsync(g, null), |
3242 |
> |
() -> f.runAfterEitherAsync(g, null, exec), |
3243 |
> |
() -> f.runAfterEither(nullFuture, () -> {}), |
3244 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}), |
3245 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}, exec), |
3246 |
> |
() -> f.runAfterEitherAsync(g, () -> {}, null), |
3247 |
> |
|
3248 |
> |
() -> f.thenCompose(null), |
3249 |
> |
() -> f.thenComposeAsync(null), |
3250 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
3251 |
> |
() -> f.thenComposeAsync(null, exec), |
3252 |
> |
|
3253 |
> |
() -> f.exceptionally(null), |
3254 |
> |
|
3255 |
> |
() -> f.handle(null), |
3256 |
> |
|
3257 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>)null), |
3258 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>[])null), |
3259 |
> |
() -> CompletableFuture.allOf(f, null), |
3260 |
> |
() -> CompletableFuture.allOf(null, f), |
3261 |
> |
|
3262 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>)null), |
3263 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>[])null), |
3264 |
> |
() -> CompletableFuture.anyOf(f, null), |
3265 |
> |
() -> CompletableFuture.anyOf(null, f), |
3266 |
> |
|
3267 |
> |
() -> f.obtrudeException(null), |
3268 |
> |
|
3269 |
> |
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3270 |
> |
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3271 |
> |
() -> CompletableFuture.delayedExecutor(1L, null), |
3272 |
|
|
3273 |
< |
r = new Noop(); |
3274 |
< |
f = new CompletableFuture<>(); |
1786 |
< |
f2 = new CompletableFuture<>(); |
1787 |
< |
g = f.runAfterBothAsync(f2, r); |
1788 |
< |
f.complete(one); |
1789 |
< |
assertTrue(f2.cancel(true)); |
1790 |
< |
checkCompletedWithWrappedCancellationException(g); |
1791 |
< |
} |
3273 |
> |
() -> f.orTimeout(1L, null), |
3274 |
> |
() -> f.completeOnTimeout(42, 1L, null), |
3275 |
|
|
3276 |
< |
/** |
3277 |
< |
* applyToEitherAsync result completes normally after normal |
3278 |
< |
* completion of sources |
1796 |
< |
*/ |
1797 |
< |
public void testApplyToEitherAsync() { |
1798 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1799 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1800 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1801 |
< |
f.complete(one); |
1802 |
< |
checkCompletedNormally(g, two); |
3276 |
> |
() -> CompletableFuture.failedFuture(null), |
3277 |
> |
() -> CompletableFuture.failedStage(null), |
3278 |
> |
}; |
3279 |
|
|
3280 |
< |
f = new CompletableFuture<>(); |
3281 |
< |
f.complete(one); |
1806 |
< |
f2 = new CompletableFuture<>(); |
1807 |
< |
g = f.applyToEitherAsync(f2, inc); |
1808 |
< |
checkCompletedNormally(g, two); |
3280 |
> |
assertThrows(NullPointerException.class, throwingActions); |
3281 |
> |
assertEquals(0, exec.count.get()); |
3282 |
|
} |
3283 |
|
|
3284 |
|
/** |
3285 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
1813 |
< |
* completion of source |
3285 |
> |
* toCompletableFuture returns this CompletableFuture. |
3286 |
|
*/ |
3287 |
< |
public void testApplyToEitherAsync2() { |
3287 |
> |
public void testToCompletableFuture() { |
3288 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3289 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1818 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1819 |
< |
f.completeExceptionally(new CFException()); |
1820 |
< |
checkCompletedWithWrappedCFException(g); |
1821 |
< |
|
1822 |
< |
f = new CompletableFuture<>(); |
1823 |
< |
f2 = new CompletableFuture<>(); |
1824 |
< |
f2.completeExceptionally(new CFException()); |
1825 |
< |
g = f.applyToEitherAsync(f2, inc); |
1826 |
< |
f.complete(one); |
1827 |
< |
checkCompletedWithWrappedCFException(g); |
3289 |
> |
assertSame(f, f.toCompletableFuture()); |
3290 |
|
} |
3291 |
|
|
3292 |
< |
/** |
1831 |
< |
* applyToEitherAsync result completes exceptionally if action does |
1832 |
< |
*/ |
1833 |
< |
public void testApplyToEitherAsync3() { |
1834 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
1835 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
1836 |
< |
FailingFunction r = new FailingFunction(); |
1837 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1838 |
< |
f.complete(one); |
1839 |
< |
checkCompletedWithWrappedCFException(g); |
1840 |
< |
} |
3292 |
> |
// jdk9 |
3293 |
|
|
3294 |
|
/** |
3295 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
3295 |
> |
* newIncompleteFuture returns an incomplete CompletableFuture |
3296 |
|
*/ |
3297 |
< |
public void testApplyToEitherAsync4() { |
3297 |
> |
public void testNewIncompleteFuture() { |
3298 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3299 |
> |
{ |
3300 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3301 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3302 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
3303 |
< |
assertTrue(f.cancel(true)); |
3304 |
< |
checkCompletedWithWrappedCancellationException(g); |
3305 |
< |
|
3306 |
< |
f = new CompletableFuture<>(); |
3307 |
< |
f2 = new CompletableFuture<>(); |
3308 |
< |
assertTrue(f2.cancel(true)); |
3309 |
< |
g = f.applyToEitherAsync(f2, inc); |
3310 |
< |
checkCompletedWithWrappedCancellationException(g); |
1857 |
< |
} |
3301 |
> |
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3302 |
> |
checkIncomplete(f); |
3303 |
> |
checkIncomplete(g); |
3304 |
> |
f.complete(v1); |
3305 |
> |
checkCompletedNormally(f, v1); |
3306 |
> |
checkIncomplete(g); |
3307 |
> |
g.complete(v1); |
3308 |
> |
checkCompletedNormally(g, v1); |
3309 |
> |
assertSame(g.getClass(), CompletableFuture.class); |
3310 |
> |
}} |
3311 |
|
|
3312 |
|
/** |
3313 |
< |
* acceptEitherAsync result completes normally after normal |
1861 |
< |
* completion of sources |
3313 |
> |
* completedStage returns a completed CompletionStage |
3314 |
|
*/ |
3315 |
< |
public void testAcceptEitherAsync() { |
3316 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3317 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3318 |
< |
IncAction r = new IncAction(); |
3319 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
3320 |
< |
f.complete(one); |
3321 |
< |
checkCompletedNormally(g, null); |
1870 |
< |
assertEquals(r.value, 2); |
1871 |
< |
|
1872 |
< |
r = new IncAction(); |
1873 |
< |
f = new CompletableFuture<>(); |
1874 |
< |
f.complete(one); |
1875 |
< |
f2 = new CompletableFuture<>(); |
1876 |
< |
g = f.acceptEitherAsync(f2, r); |
1877 |
< |
checkCompletedNormally(g, null); |
1878 |
< |
assertEquals(r.value, 2); |
3315 |
> |
public void testCompletedStage() { |
3316 |
> |
AtomicInteger x = new AtomicInteger(0); |
3317 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3318 |
> |
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3319 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3320 |
> |
assertEquals(x.get(), 1); |
3321 |
> |
assertNull(r.get()); |
3322 |
|
} |
3323 |
|
|
3324 |
|
/** |
3325 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
3326 |
< |
* completion of source |
3325 |
> |
* defaultExecutor by default returns the commonPool if |
3326 |
> |
* it supports more than one thread. |
3327 |
|
*/ |
3328 |
< |
public void testAcceptEitherAsync2() { |
3328 |
> |
public void testDefaultExecutor() { |
3329 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3330 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3331 |
< |
IncAction r = new IncAction(); |
3332 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
3333 |
< |
f.completeExceptionally(new CFException()); |
3334 |
< |
checkCompletedWithWrappedCFException(g); |
3335 |
< |
|
1893 |
< |
r = new IncAction(); |
1894 |
< |
f = new CompletableFuture<>(); |
1895 |
< |
f2 = new CompletableFuture<>(); |
1896 |
< |
f2.completeExceptionally(new CFException()); |
1897 |
< |
g = f.acceptEitherAsync(f2, r); |
1898 |
< |
f.complete(one); |
1899 |
< |
checkCompletedWithWrappedCFException(g); |
3330 |
> |
Executor e = f.defaultExecutor(); |
3331 |
> |
Executor c = ForkJoinPool.commonPool(); |
3332 |
> |
if (ForkJoinPool.getCommonPoolParallelism() > 1) |
3333 |
> |
assertSame(e, c); |
3334 |
> |
else |
3335 |
> |
assertNotSame(e, c); |
3336 |
|
} |
3337 |
|
|
3338 |
|
/** |
3339 |
< |
* acceptEitherAsync result completes exceptionally if action does |
3339 |
> |
* failedFuture returns a CompletableFuture completed |
3340 |
> |
* exceptionally with the given Exception |
3341 |
|
*/ |
3342 |
< |
public void testAcceptEitherAsync3() { |
3343 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3344 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3345 |
< |
FailingConsumer r = new FailingConsumer(); |
1909 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1910 |
< |
f.complete(one); |
1911 |
< |
checkCompletedWithWrappedCFException(g); |
3342 |
> |
public void testFailedFuture() { |
3343 |
> |
CFException ex = new CFException(); |
3344 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3345 |
> |
checkCompletedExceptionally(f, ex); |
3346 |
|
} |
3347 |
|
|
3348 |
|
/** |
3349 |
< |
* acceptEitherAsync result completes exceptionally if either |
1916 |
< |
* source cancelled |
3349 |
> |
* failedFuture(null) throws NPE |
3350 |
|
*/ |
3351 |
< |
public void testAcceptEitherAsync4() { |
3352 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3353 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3354 |
< |
IncAction r = new IncAction(); |
3355 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1923 |
< |
assertTrue(f.cancel(true)); |
1924 |
< |
checkCompletedWithWrappedCancellationException(g); |
1925 |
< |
|
1926 |
< |
r = new IncAction(); |
1927 |
< |
f = new CompletableFuture<>(); |
1928 |
< |
f2 = new CompletableFuture<>(); |
1929 |
< |
assertTrue(f2.cancel(true)); |
1930 |
< |
g = f.acceptEitherAsync(f2, r); |
1931 |
< |
checkCompletedWithWrappedCancellationException(g); |
3351 |
> |
public void testFailedFuture_null() { |
3352 |
> |
try { |
3353 |
> |
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3354 |
> |
shouldThrow(); |
3355 |
> |
} catch (NullPointerException success) {} |
3356 |
|
} |
3357 |
|
|
3358 |
|
/** |
3359 |
< |
* runAfterEitherAsync result completes normally after normal |
3360 |
< |
* completion of sources |
3359 |
> |
* copy returns a CompletableFuture that is completed normally, |
3360 |
> |
* with the same value, when source is. |
3361 |
|
*/ |
3362 |
< |
public void testRunAfterEitherAsync() { |
3362 |
> |
public void testCopy() { |
3363 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3364 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3365 |
< |
Noop r = new Noop(); |
3366 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
3367 |
< |
f.complete(one); |
3368 |
< |
checkCompletedNormally(g, null); |
3369 |
< |
assertTrue(r.ran); |
1946 |
< |
|
1947 |
< |
r = new Noop(); |
1948 |
< |
f = new CompletableFuture<>(); |
1949 |
< |
f.complete(one); |
1950 |
< |
f2 = new CompletableFuture<>(); |
1951 |
< |
g = f.runAfterEitherAsync(f2, r); |
1952 |
< |
checkCompletedNormally(g, null); |
1953 |
< |
assertTrue(r.ran); |
3364 |
> |
CompletableFuture<Integer> g = f.copy(); |
3365 |
> |
checkIncomplete(f); |
3366 |
> |
checkIncomplete(g); |
3367 |
> |
f.complete(1); |
3368 |
> |
checkCompletedNormally(f, 1); |
3369 |
> |
checkCompletedNormally(g, 1); |
3370 |
|
} |
3371 |
|
|
3372 |
|
/** |
3373 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
3374 |
< |
* completion of source |
3373 |
> |
* copy returns a CompletableFuture that is completed exceptionally |
3374 |
> |
* when source is. |
3375 |
|
*/ |
3376 |
< |
public void testRunAfterEitherAsync2() { |
3376 |
> |
public void testCopy2() { |
3377 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3378 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3379 |
< |
Noop r = new Noop(); |
3380 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
3381 |
< |
f.completeExceptionally(new CFException()); |
3382 |
< |
checkCompletedWithWrappedCFException(g); |
3383 |
< |
|
3384 |
< |
r = new Noop(); |
1969 |
< |
f = new CompletableFuture<>(); |
1970 |
< |
f2 = new CompletableFuture<>(); |
1971 |
< |
f2.completeExceptionally(new CFException()); |
1972 |
< |
g = f.runAfterEitherAsync(f2, r); |
1973 |
< |
f.complete(one); |
1974 |
< |
checkCompletedWithWrappedCFException(g); |
3378 |
> |
CompletableFuture<Integer> g = f.copy(); |
3379 |
> |
checkIncomplete(f); |
3380 |
> |
checkIncomplete(g); |
3381 |
> |
CFException ex = new CFException(); |
3382 |
> |
f.completeExceptionally(ex); |
3383 |
> |
checkCompletedExceptionally(f, ex); |
3384 |
> |
checkCompletedWithWrappedException(g, ex); |
3385 |
|
} |
3386 |
|
|
3387 |
|
/** |
3388 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
3388 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3389 |
> |
* completed normally, with the same value, when source is. |
3390 |
|
*/ |
3391 |
< |
public void testRunAfterEitherAsync3() { |
3391 |
> |
public void testMinimalCompletionStage() { |
3392 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3393 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3394 |
< |
FailingNoop r = new FailingNoop(); |
3395 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
3396 |
< |
f.complete(one); |
3397 |
< |
checkCompletedWithWrappedCFException(g); |
3393 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3394 |
> |
AtomicInteger x = new AtomicInteger(0); |
3395 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3396 |
> |
checkIncomplete(f); |
3397 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3398 |
> |
f.complete(1); |
3399 |
> |
checkCompletedNormally(f, 1); |
3400 |
> |
assertEquals(x.get(), 1); |
3401 |
> |
assertNull(r.get()); |
3402 |
|
} |
3403 |
|
|
3404 |
|
/** |
3405 |
< |
* runAfterEitherAsync result completes exceptionally if either |
3406 |
< |
* source cancelled |
3405 |
> |
* minimalCompletionStage returns a CompletableFuture that is |
3406 |
> |
* completed exceptionally when source is. |
3407 |
|
*/ |
3408 |
< |
public void testRunAfterEitherAsync4() { |
3408 |
> |
public void testMinimalCompletionStage2() { |
3409 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3410 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
3411 |
< |
Noop r = new Noop(); |
3412 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
3413 |
< |
assertTrue(f.cancel(true)); |
3414 |
< |
checkCompletedWithWrappedCancellationException(g); |
3415 |
< |
|
3416 |
< |
r = new Noop(); |
3417 |
< |
f = new CompletableFuture<>(); |
3418 |
< |
f2 = new CompletableFuture<>(); |
3419 |
< |
assertTrue(f2.cancel(true)); |
2005 |
< |
g = f.runAfterEitherAsync(f2, r); |
2006 |
< |
checkCompletedWithWrappedCancellationException(g); |
2007 |
< |
} |
2008 |
< |
|
2009 |
< |
/** |
2010 |
< |
* thenComposeAsync result completes normally after normal |
2011 |
< |
* completion of source |
2012 |
< |
*/ |
2013 |
< |
public void testThenComposeAsync() { |
2014 |
< |
CompletableFuture<Integer> f, g; |
2015 |
< |
CompletableFutureInc r; |
2016 |
< |
|
2017 |
< |
f = new CompletableFuture<>(); |
2018 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2019 |
< |
f.complete(one); |
2020 |
< |
checkCompletedNormally(g, two); |
2021 |
< |
|
2022 |
< |
f = new CompletableFuture<>(); |
2023 |
< |
f.complete(one); |
2024 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2025 |
< |
checkCompletedNormally(g, two); |
2026 |
< |
} |
2027 |
< |
|
2028 |
< |
/** |
2029 |
< |
* thenComposeAsync result completes exceptionally after |
2030 |
< |
* exceptional completion of source |
2031 |
< |
*/ |
2032 |
< |
public void testThenComposeAsync2() { |
2033 |
< |
CompletableFuture<Integer> f, g; |
2034 |
< |
CompletableFutureInc r; |
2035 |
< |
|
2036 |
< |
f = new CompletableFuture<>(); |
2037 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2038 |
< |
f.completeExceptionally(new CFException()); |
2039 |
< |
checkCompletedWithWrappedCFException(g); |
2040 |
< |
assertFalse(r.ran); |
2041 |
< |
|
2042 |
< |
f = new CompletableFuture<>(); |
2043 |
< |
f.completeExceptionally(new CFException()); |
2044 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2045 |
< |
checkCompletedWithWrappedCFException(g); |
2046 |
< |
assertFalse(r.ran); |
2047 |
< |
} |
2048 |
< |
|
2049 |
< |
/** |
2050 |
< |
* thenComposeAsync result completes exceptionally if action does |
2051 |
< |
*/ |
2052 |
< |
public void testThenComposeAsync3() { |
2053 |
< |
CompletableFuture<Integer> f, g; |
2054 |
< |
FailingCompletableFutureFunction r; |
2055 |
< |
|
2056 |
< |
f = new CompletableFuture<>(); |
2057 |
< |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
2058 |
< |
f.complete(one); |
2059 |
< |
checkCompletedWithWrappedCFException(g); |
2060 |
< |
|
2061 |
< |
f = new CompletableFuture<>(); |
2062 |
< |
f.complete(one); |
2063 |
< |
g = f.thenComposeAsync(r = new FailingCompletableFutureFunction()); |
2064 |
< |
checkCompletedWithWrappedCFException(g); |
3410 |
> |
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3411 |
> |
AtomicInteger x = new AtomicInteger(0); |
3412 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3413 |
> |
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3414 |
> |
checkIncomplete(f); |
3415 |
> |
CFException ex = new CFException(); |
3416 |
> |
f.completeExceptionally(ex); |
3417 |
> |
checkCompletedExceptionally(f, ex); |
3418 |
> |
assertEquals(x.get(), 0); |
3419 |
> |
assertEquals(r.get().getCause(), ex); |
3420 |
|
} |
3421 |
|
|
3422 |
|
/** |
3423 |
< |
* thenComposeAsync result completes exceptionally if source cancelled |
3423 |
> |
* failedStage returns a CompletionStage completed |
3424 |
> |
* exceptionally with the given Exception |
3425 |
|
*/ |
3426 |
< |
public void testThenComposeAsync4() { |
3427 |
< |
CompletableFuture<Integer> f, g; |
3428 |
< |
CompletableFutureInc r; |
3429 |
< |
|
3430 |
< |
f = new CompletableFuture<>(); |
3431 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
3432 |
< |
assertTrue(f.cancel(true)); |
3433 |
< |
checkCompletedWithWrappedCancellationException(g); |
2078 |
< |
|
2079 |
< |
f = new CompletableFuture<>(); |
2080 |
< |
assertTrue(f.cancel(true)); |
2081 |
< |
g = f.thenComposeAsync(r = new CompletableFutureInc()); |
2082 |
< |
checkCompletedWithWrappedCancellationException(g); |
3426 |
> |
public void testFailedStage() { |
3427 |
> |
CFException ex = new CFException(); |
3428 |
> |
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3429 |
> |
AtomicInteger x = new AtomicInteger(0); |
3430 |
> |
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3431 |
> |
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3432 |
> |
assertEquals(x.get(), 0); |
3433 |
> |
assertEquals(r.get(), ex); |
3434 |
|
} |
3435 |
|
|
2085 |
– |
// async with explicit executors |
2086 |
– |
|
3436 |
|
/** |
3437 |
< |
* thenRunAsync result completes normally after normal completion of source |
3437 |
> |
* completeAsync completes with value of given supplier |
3438 |
|
*/ |
3439 |
< |
public void testThenRunAsyncE() { |
3439 |
> |
public void testCompleteAsync() { |
3440 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3441 |
> |
{ |
3442 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3443 |
< |
Noop r = new Noop(); |
3444 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
3445 |
< |
f.complete(null); |
3446 |
< |
checkCompletedNormally(g, null); |
2096 |
< |
|
2097 |
< |
// reordered version |
2098 |
< |
f = new CompletableFuture<>(); |
2099 |
< |
f.complete(null); |
2100 |
< |
r = new Noop(); |
2101 |
< |
g = f.thenRunAsync(r, new ThreadExecutor()); |
2102 |
< |
checkCompletedNormally(g, null); |
2103 |
< |
} |
3443 |
> |
f.completeAsync(() -> v1); |
3444 |
> |
f.join(); |
3445 |
> |
checkCompletedNormally(f, v1); |
3446 |
> |
}} |
3447 |
|
|
3448 |
|
/** |
3449 |
< |
* thenRunAsync result completes exceptionally after exceptional |
2107 |
< |
* completion of source |
3449 |
> |
* completeAsync completes exceptionally if given supplier throws |
3450 |
|
*/ |
3451 |
< |
public void testThenRunAsync2E() { |
3451 |
> |
public void testCompleteAsync2() { |
3452 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3453 |
< |
Noop r = new Noop(); |
3454 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
2113 |
< |
f.completeExceptionally(new CFException()); |
3453 |
> |
CFException ex = new CFException(); |
3454 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}); |
3455 |
|
try { |
3456 |
< |
g.join(); |
3456 |
> |
f.join(); |
3457 |
|
shouldThrow(); |
3458 |
|
} catch (CompletionException success) {} |
3459 |
< |
checkCompletedWithWrappedCFException(g); |
2119 |
< |
} |
2120 |
< |
|
2121 |
< |
/** |
2122 |
< |
* thenRunAsync result completes exceptionally if action does |
2123 |
< |
*/ |
2124 |
< |
public void testThenRunAsync3E() { |
2125 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2126 |
< |
FailingNoop r = new FailingNoop(); |
2127 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
2128 |
< |
f.complete(null); |
2129 |
< |
checkCompletedWithWrappedCFException(g); |
2130 |
< |
} |
2131 |
< |
|
2132 |
< |
/** |
2133 |
< |
* thenRunAsync result completes exceptionally if source cancelled |
2134 |
< |
*/ |
2135 |
< |
public void testThenRunAsync4E() { |
2136 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2137 |
< |
Noop r = new Noop(); |
2138 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
2139 |
< |
assertTrue(f.cancel(true)); |
2140 |
< |
checkCompletedWithWrappedCancellationException(g); |
2141 |
< |
} |
2142 |
< |
|
2143 |
< |
/** |
2144 |
< |
* thenApplyAsync result completes normally after normal completion of source |
2145 |
< |
*/ |
2146 |
< |
public void testThenApplyAsyncE() { |
2147 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2148 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
2149 |
< |
f.complete(one); |
2150 |
< |
checkCompletedNormally(g, two); |
3459 |
> |
checkCompletedWithWrappedException(f, ex); |
3460 |
|
} |
3461 |
|
|
3462 |
|
/** |
3463 |
< |
* thenApplyAsync result completes exceptionally after exceptional |
2155 |
< |
* completion of source |
3463 |
> |
* completeAsync with given executor completes with value of given supplier |
3464 |
|
*/ |
3465 |
< |
public void testThenApplyAsync2E() { |
3465 |
> |
public void testCompleteAsync3() { |
3466 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3467 |
> |
{ |
3468 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3469 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
3470 |
< |
f.completeExceptionally(new CFException()); |
3471 |
< |
checkCompletedWithWrappedCFException(g); |
3472 |
< |
} |
3469 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3470 |
> |
f.completeAsync(() -> v1, executor); |
3471 |
> |
assertSame(v1, f.join()); |
3472 |
> |
checkCompletedNormally(f, v1); |
3473 |
> |
assertEquals(1, executor.count.get()); |
3474 |
> |
}} |
3475 |
|
|
3476 |
|
/** |
3477 |
< |
* thenApplyAsync result completes exceptionally if action does |
3477 |
> |
* completeAsync with given executor completes exceptionally if |
3478 |
> |
* given supplier throws |
3479 |
|
*/ |
3480 |
< |
public void testThenApplyAsync3E() { |
3480 |
> |
public void testCompleteAsync4() { |
3481 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3482 |
< |
FailingFunction r = new FailingFunction(); |
3483 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor()); |
3484 |
< |
f.complete(null); |
3485 |
< |
checkCompletedWithWrappedCFException(g); |
3482 |
> |
CFException ex = new CFException(); |
3483 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3484 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3485 |
> |
try { |
3486 |
> |
f.join(); |
3487 |
> |
shouldThrow(); |
3488 |
> |
} catch (CompletionException success) {} |
3489 |
> |
checkCompletedWithWrappedException(f, ex); |
3490 |
> |
assertEquals(1, executor.count.get()); |
3491 |
|
} |
3492 |
|
|
3493 |
|
/** |
3494 |
< |
* thenApplyAsync result completes exceptionally if source cancelled |
3494 |
> |
* orTimeout completes with TimeoutException if not complete |
3495 |
|
*/ |
3496 |
< |
public void testThenApplyAsync4E() { |
3496 |
> |
public void testOrTimeout_timesOut() { |
3497 |
> |
long timeoutMillis = timeoutMillis(); |
3498 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3499 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
3500 |
< |
assertTrue(f.cancel(true)); |
3501 |
< |
checkCompletedWithWrappedCancellationException(g); |
3499 |
> |
long startTime = System.nanoTime(); |
3500 |
> |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3501 |
> |
checkCompletedWithTimeoutException(f); |
3502 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3503 |
|
} |
3504 |
|
|
3505 |
|
/** |
3506 |
< |
* thenAcceptAsync result completes normally after normal |
2187 |
< |
* completion of source |
3506 |
> |
* orTimeout completes normally if completed before timeout |
3507 |
|
*/ |
3508 |
< |
public void testThenAcceptAsyncE() { |
3508 |
> |
public void testOrTimeout_completed() { |
3509 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3510 |
> |
{ |
3511 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3512 |
< |
IncAction r = new IncAction(); |
3513 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
3514 |
< |
f.complete(one); |
3515 |
< |
checkCompletedNormally(g, null); |
3516 |
< |
assertEquals(r.value, 2); |
3517 |
< |
} |
3512 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3513 |
> |
long startTime = System.nanoTime(); |
3514 |
> |
f.complete(v1); |
3515 |
> |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3516 |
> |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3517 |
> |
g.complete(v1); |
3518 |
> |
checkCompletedNormally(f, v1); |
3519 |
> |
checkCompletedNormally(g, v1); |
3520 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3521 |
> |
}} |
3522 |
|
|
3523 |
|
/** |
3524 |
< |
* thenAcceptAsync result completes exceptionally after exceptional |
2200 |
< |
* completion of source |
3524 |
> |
* completeOnTimeout completes with given value if not complete |
3525 |
|
*/ |
3526 |
< |
public void testThenAcceptAsync2E() { |
3527 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3528 |
< |
IncAction r = new IncAction(); |
2205 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
2206 |
< |
f.completeExceptionally(new CFException()); |
2207 |
< |
checkCompletedWithWrappedCFException(g); |
3526 |
> |
public void testCompleteOnTimeout_timesOut() { |
3527 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3528 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3529 |
|
} |
3530 |
|
|
3531 |
< |
/** |
3532 |
< |
* thenAcceptAsync result completes exceptionally if action does |
2212 |
< |
*/ |
2213 |
< |
public void testThenAcceptAsync3E() { |
3531 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3532 |
> |
long timeoutMillis = timeoutMillis(); |
3533 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3534 |
< |
FailingConsumer r = new FailingConsumer(); |
3535 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
3536 |
< |
f.complete(null); |
3537 |
< |
checkCompletedWithWrappedCFException(g); |
3534 |
> |
long startTime = System.nanoTime(); |
3535 |
> |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3536 |
> |
assertSame(v, f.join()); |
3537 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3538 |
> |
f.complete(99); // should have no effect |
3539 |
> |
checkCompletedNormally(f, v); |
3540 |
|
} |
3541 |
|
|
3542 |
|
/** |
3543 |
< |
* thenAcceptAsync result completes exceptionally if source cancelled |
3543 |
> |
* completeOnTimeout has no effect if completed within timeout |
3544 |
|
*/ |
3545 |
< |
public void testThenAcceptAsync4E() { |
3545 |
> |
public void testCompleteOnTimeout_completed() { |
3546 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3547 |
> |
{ |
3548 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3549 |
< |
IncAction r = new IncAction(); |
3550 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
3551 |
< |
assertTrue(f.cancel(true)); |
3552 |
< |
checkCompletedWithWrappedCancellationException(g); |
3553 |
< |
} |
3554 |
< |
|
3555 |
< |
/** |
3556 |
< |
* thenCombineAsync result completes normally after normal |
3557 |
< |
* completion of sources |
3558 |
< |
*/ |
3559 |
< |
public void testThenCombineAsyncE() { |
3560 |
< |
CompletableFuture<Integer> f, g, h; |
3561 |
< |
ThreadExecutor e = new ThreadExecutor(); |
3562 |
< |
int count = 0; |
3563 |
< |
|
3564 |
< |
f = new CompletableFuture<>(); |
3565 |
< |
g = new CompletableFuture<>(); |
3566 |
< |
h = f.thenCombineAsync(g, subtract, e); |
3567 |
< |
f.complete(3); |
2245 |
< |
checkIncomplete(h); |
2246 |
< |
g.complete(1); |
2247 |
< |
checkCompletedNormally(h, 2); |
2248 |
< |
assertEquals(++count, e.count.get()); |
2249 |
< |
|
2250 |
< |
f = new CompletableFuture<>(); |
2251 |
< |
g = new CompletableFuture<>(); |
2252 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2253 |
< |
g.complete(1); |
2254 |
< |
checkIncomplete(h); |
2255 |
< |
f.complete(3); |
2256 |
< |
checkCompletedNormally(h, 2); |
2257 |
< |
assertEquals(++count, e.count.get()); |
2258 |
< |
|
2259 |
< |
f = new CompletableFuture<>(); |
2260 |
< |
g = new CompletableFuture<>(); |
2261 |
< |
g.complete(1); |
2262 |
< |
f.complete(3); |
2263 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2264 |
< |
checkCompletedNormally(h, 2); |
2265 |
< |
assertEquals(++count, e.count.get()); |
3549 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3550 |
> |
long startTime = System.nanoTime(); |
3551 |
> |
f.complete(v1); |
3552 |
> |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3553 |
> |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3554 |
> |
g.complete(v1); |
3555 |
> |
checkCompletedNormally(f, v1); |
3556 |
> |
checkCompletedNormally(g, v1); |
3557 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3558 |
> |
}} |
3559 |
> |
|
3560 |
> |
/** |
3561 |
> |
* delayedExecutor returns an executor that delays submission |
3562 |
> |
*/ |
3563 |
> |
public void testDelayedExecutor() { |
3564 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3565 |
> |
() -> testDelayedExecutor(null, 1), |
3566 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3567 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3568 |
|
} |
3569 |
|
|
3570 |
|
/** |
3571 |
< |
* thenCombineAsync result completes exceptionally after exceptional |
3572 |
< |
* completion of either source |
3573 |
< |
*/ |
3574 |
< |
public void testThenCombineAsync2E() { |
3575 |
< |
CompletableFuture<Integer> f, g, h; |
3576 |
< |
ThreadExecutor e = new ThreadExecutor(); |
3577 |
< |
int count = 0; |
3578 |
< |
|
3579 |
< |
f = new CompletableFuture<>(); |
3580 |
< |
g = new CompletableFuture<>(); |
3581 |
< |
h = f.thenCombineAsync(g, subtract, e); |
3582 |
< |
f.completeExceptionally(new CFException()); |
3583 |
< |
checkIncomplete(h); |
3584 |
< |
g.complete(1); |
3585 |
< |
checkCompletedWithWrappedCFException(h); |
3571 |
> |
* delayedExecutor returns an executor that delays submission |
3572 |
> |
*/ |
3573 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3574 |
> |
long timeoutMillis = timeoutMillis(); |
3575 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3576 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3577 |
> |
final Executor delayer, longDelayer; |
3578 |
> |
if (executor == null) { |
3579 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3580 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3581 |
> |
} else { |
3582 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3583 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3584 |
> |
} |
3585 |
> |
long startTime = System.nanoTime(); |
3586 |
> |
CompletableFuture<Integer> f = |
3587 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3588 |
> |
CompletableFuture<Integer> g = |
3589 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3590 |
|
|
3591 |
< |
f = new CompletableFuture<>(); |
2286 |
< |
g = new CompletableFuture<>(); |
2287 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2288 |
< |
g.completeExceptionally(new CFException()); |
2289 |
< |
checkIncomplete(h); |
2290 |
< |
f.complete(3); |
2291 |
< |
checkCompletedWithWrappedCFException(h); |
3591 |
> |
assertNull(g.getNow(null)); |
3592 |
|
|
3593 |
< |
f = new CompletableFuture<>(); |
3594 |
< |
g = new CompletableFuture<>(); |
3595 |
< |
g.completeExceptionally(new CFException()); |
3596 |
< |
h = f.thenCombineAsync(g, subtract, e); |
2297 |
< |
checkIncomplete(h); |
2298 |
< |
f.complete(3); |
2299 |
< |
checkCompletedWithWrappedCFException(h); |
3593 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3594 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3595 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3596 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3597 |
|
|
3598 |
< |
assertEquals(0, e.count.get()); |
2302 |
< |
} |
3598 |
> |
checkCompletedNormally(f, v); |
3599 |
|
|
2304 |
– |
/** |
2305 |
– |
* thenCombineAsync result completes exceptionally if action does |
2306 |
– |
*/ |
2307 |
– |
public void testThenCombineAsync3E() { |
2308 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2309 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2310 |
– |
FailingBiFunction r = new FailingBiFunction(); |
2311 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor()); |
2312 |
– |
f.complete(one); |
3600 |
|
checkIncomplete(g); |
2314 |
– |
assertFalse(r.ran); |
2315 |
– |
f2.complete(two); |
2316 |
– |
checkCompletedWithWrappedCFException(g); |
2317 |
– |
assertTrue(r.ran); |
2318 |
– |
} |
2319 |
– |
|
2320 |
– |
/** |
2321 |
– |
* thenCombineAsync result completes exceptionally if either source cancelled |
2322 |
– |
*/ |
2323 |
– |
public void testThenCombineAsync4E() { |
2324 |
– |
CompletableFuture<Integer> f, g, h; |
2325 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2326 |
– |
|
2327 |
– |
f = new CompletableFuture<>(); |
2328 |
– |
g = new CompletableFuture<>(); |
2329 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2330 |
– |
assertTrue(f.cancel(true)); |
2331 |
– |
checkIncomplete(h); |
2332 |
– |
g.complete(1); |
2333 |
– |
checkCompletedWithWrappedCancellationException(h); |
2334 |
– |
|
2335 |
– |
f = new CompletableFuture<>(); |
2336 |
– |
g = new CompletableFuture<>(); |
2337 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2338 |
– |
assertTrue(g.cancel(true)); |
2339 |
– |
checkIncomplete(h); |
2340 |
– |
f.complete(3); |
2341 |
– |
checkCompletedWithWrappedCancellationException(h); |
2342 |
– |
|
2343 |
– |
f = new CompletableFuture<>(); |
2344 |
– |
g = new CompletableFuture<>(); |
3601 |
|
assertTrue(g.cancel(true)); |
2346 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2347 |
– |
checkIncomplete(h); |
2348 |
– |
f.complete(3); |
2349 |
– |
checkCompletedWithWrappedCancellationException(h); |
2350 |
– |
|
2351 |
– |
f = new CompletableFuture<>(); |
2352 |
– |
g = new CompletableFuture<>(); |
2353 |
– |
assertTrue(f.cancel(true)); |
2354 |
– |
assertTrue(g.cancel(true)); |
2355 |
– |
h = f.thenCombineAsync(g, subtract, e); |
2356 |
– |
checkCompletedWithWrappedCancellationException(h); |
2357 |
– |
|
2358 |
– |
assertEquals(0, e.count.get()); |
2359 |
– |
} |
2360 |
– |
|
2361 |
– |
/** |
2362 |
– |
* thenAcceptBothAsync result completes normally after normal |
2363 |
– |
* completion of sources |
2364 |
– |
*/ |
2365 |
– |
public void testThenAcceptBothAsyncE() { |
2366 |
– |
CompletableFuture<Integer> f, g; |
2367 |
– |
CompletableFuture<Void> h; |
2368 |
– |
SubtractAction r; |
2369 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2370 |
– |
|
2371 |
– |
f = new CompletableFuture<>(); |
2372 |
– |
g = new CompletableFuture<>(); |
2373 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2374 |
– |
f.complete(3); |
2375 |
– |
checkIncomplete(h); |
2376 |
– |
g.complete(1); |
2377 |
– |
checkCompletedNormally(h, null); |
2378 |
– |
assertEquals(r.value, 2); |
2379 |
– |
|
2380 |
– |
f = new CompletableFuture<>(); |
2381 |
– |
g = new CompletableFuture<>(); |
2382 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2383 |
– |
g.complete(1); |
2384 |
– |
checkIncomplete(h); |
2385 |
– |
f.complete(3); |
2386 |
– |
checkCompletedNormally(h, null); |
2387 |
– |
assertEquals(r.value, 2); |
2388 |
– |
|
2389 |
– |
f = new CompletableFuture<>(); |
2390 |
– |
g = new CompletableFuture<>(); |
2391 |
– |
g.complete(1); |
2392 |
– |
f.complete(3); |
2393 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2394 |
– |
checkCompletedNormally(h, null); |
2395 |
– |
assertEquals(r.value, 2); |
2396 |
– |
|
2397 |
– |
assertEquals(3, e.count.get()); |
2398 |
– |
} |
2399 |
– |
|
2400 |
– |
/** |
2401 |
– |
* thenAcceptBothAsync result completes exceptionally after exceptional |
2402 |
– |
* completion of source |
2403 |
– |
*/ |
2404 |
– |
public void testThenAcceptBothAsync2E() { |
2405 |
– |
CompletableFuture<Integer> f, g; |
2406 |
– |
CompletableFuture<Void> h; |
2407 |
– |
SubtractAction r; |
2408 |
– |
ThreadExecutor e = new ThreadExecutor(); |
2409 |
– |
|
2410 |
– |
f = new CompletableFuture<>(); |
2411 |
– |
g = new CompletableFuture<>(); |
2412 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2413 |
– |
f.completeExceptionally(new CFException()); |
2414 |
– |
checkIncomplete(h); |
2415 |
– |
g.complete(1); |
2416 |
– |
checkCompletedWithWrappedCFException(h); |
2417 |
– |
|
2418 |
– |
f = new CompletableFuture<>(); |
2419 |
– |
g = new CompletableFuture<>(); |
2420 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2421 |
– |
g.completeExceptionally(new CFException()); |
2422 |
– |
checkIncomplete(h); |
2423 |
– |
f.complete(3); |
2424 |
– |
checkCompletedWithWrappedCFException(h); |
2425 |
– |
|
2426 |
– |
f = new CompletableFuture<>(); |
2427 |
– |
g = new CompletableFuture<>(); |
2428 |
– |
f.complete(3); |
2429 |
– |
g.completeExceptionally(new CFException()); |
2430 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2431 |
– |
checkCompletedWithWrappedCFException(h); |
2432 |
– |
|
2433 |
– |
f = new CompletableFuture<>(); |
2434 |
– |
g = new CompletableFuture<>(); |
2435 |
– |
f.completeExceptionally(new CFException()); |
2436 |
– |
g.complete(3); |
2437 |
– |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2438 |
– |
checkCompletedWithWrappedCFException(h); |
2439 |
– |
|
2440 |
– |
assertEquals(0, e.count.get()); |
3602 |
|
} |
3603 |
|
|
3604 |
< |
/** |
2444 |
< |
* thenAcceptBothAsync result completes exceptionally if action does |
2445 |
< |
*/ |
2446 |
< |
public void testThenAcceptBothAsync3E() { |
2447 |
< |
CompletableFuture<Integer> f, g; |
2448 |
< |
CompletableFuture<Void> h; |
2449 |
< |
FailingBiConsumer r; |
2450 |
< |
ThreadExecutor e = new ThreadExecutor(); |
2451 |
< |
|
2452 |
< |
f = new CompletableFuture<>(); |
2453 |
< |
g = new CompletableFuture<>(); |
2454 |
< |
h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e); |
2455 |
< |
f.complete(3); |
2456 |
< |
checkIncomplete(h); |
2457 |
< |
g.complete(1); |
2458 |
< |
checkCompletedWithWrappedCFException(h); |
2459 |
< |
|
2460 |
< |
f = new CompletableFuture<>(); |
2461 |
< |
g = new CompletableFuture<>(); |
2462 |
< |
f.complete(3); |
2463 |
< |
g.complete(1); |
2464 |
< |
h = f.thenAcceptBothAsync(g, r = new FailingBiConsumer(), e); |
2465 |
< |
checkCompletedWithWrappedCFException(h); |
3604 |
> |
//--- tests of implementation details; not part of official tck --- |
3605 |
|
|
3606 |
< |
assertEquals(2, e.count.get()); |
3607 |
< |
} |
3608 |
< |
|
3609 |
< |
/** |
3610 |
< |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
3611 |
< |
*/ |
3612 |
< |
public void testThenAcceptBothAsync4E() { |
2474 |
< |
CompletableFuture<Integer> f, g; |
2475 |
< |
CompletableFuture<Void> h; |
2476 |
< |
SubtractAction r; |
2477 |
< |
ThreadExecutor e = new ThreadExecutor(); |
2478 |
< |
|
2479 |
< |
f = new CompletableFuture<>(); |
2480 |
< |
g = new CompletableFuture<>(); |
2481 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2482 |
< |
assertTrue(f.cancel(true)); |
2483 |
< |
checkIncomplete(h); |
2484 |
< |
g.complete(1); |
2485 |
< |
checkCompletedWithWrappedCancellationException(h); |
2486 |
< |
|
2487 |
< |
f = new CompletableFuture<>(); |
2488 |
< |
g = new CompletableFuture<>(); |
2489 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2490 |
< |
assertTrue(g.cancel(true)); |
2491 |
< |
checkIncomplete(h); |
2492 |
< |
f.complete(3); |
2493 |
< |
checkCompletedWithWrappedCancellationException(h); |
2494 |
< |
|
2495 |
< |
f = new CompletableFuture<>(); |
2496 |
< |
g = new CompletableFuture<>(); |
2497 |
< |
f.complete(3); |
2498 |
< |
assertTrue(g.cancel(true)); |
2499 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2500 |
< |
checkCompletedWithWrappedCancellationException(h); |
2501 |
< |
|
2502 |
< |
f = new CompletableFuture<>(); |
2503 |
< |
g = new CompletableFuture<>(); |
2504 |
< |
assertTrue(f.cancel(true)); |
2505 |
< |
g.complete(3); |
2506 |
< |
h = f.thenAcceptBothAsync(g, r = new SubtractAction(), e); |
2507 |
< |
checkCompletedWithWrappedCancellationException(h); |
2508 |
< |
|
2509 |
< |
assertEquals(0, e.count.get()); |
2510 |
< |
} |
2511 |
< |
|
2512 |
< |
/** |
2513 |
< |
* runAfterBothAsync result completes normally after normal |
2514 |
< |
* completion of sources |
2515 |
< |
*/ |
2516 |
< |
public void testRunAfterBothAsyncE() { |
2517 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2518 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2519 |
< |
Noop r = new Noop(); |
2520 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2521 |
< |
f.complete(one); |
2522 |
< |
checkIncomplete(g); |
2523 |
< |
f2.complete(two); |
2524 |
< |
checkCompletedNormally(g, null); |
2525 |
< |
assertTrue(r.ran); |
2526 |
< |
} |
2527 |
< |
|
2528 |
< |
/** |
2529 |
< |
* runAfterBothAsync result completes exceptionally after exceptional |
2530 |
< |
* completion of source |
2531 |
< |
*/ |
2532 |
< |
public void testRunAfterBothAsync2E() { |
2533 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2534 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2535 |
< |
Noop r = new Noop(); |
2536 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2537 |
< |
f.completeExceptionally(new CFException()); |
2538 |
< |
f2.complete(two); |
2539 |
< |
checkCompletedWithWrappedCFException(g); |
2540 |
< |
|
2541 |
< |
r = new Noop(); |
2542 |
< |
f = new CompletableFuture<>(); |
2543 |
< |
f2 = new CompletableFuture<>(); |
2544 |
< |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2545 |
< |
f.complete(one); |
2546 |
< |
f2.completeExceptionally(new CFException()); |
2547 |
< |
checkCompletedWithWrappedCFException(g); |
2548 |
< |
} |
2549 |
< |
|
2550 |
< |
/** |
2551 |
< |
* runAfterBothAsync result completes exceptionally if action does |
2552 |
< |
*/ |
2553 |
< |
public void testRunAfterBothAsync3E() { |
2554 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2555 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2556 |
< |
FailingNoop r = new FailingNoop(); |
2557 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2558 |
< |
f.complete(one); |
2559 |
< |
checkIncomplete(g); |
2560 |
< |
f2.complete(two); |
2561 |
< |
checkCompletedWithWrappedCFException(g); |
2562 |
< |
} |
2563 |
< |
|
2564 |
< |
/** |
2565 |
< |
* runAfterBothAsync result completes exceptionally if either source cancelled |
2566 |
< |
*/ |
2567 |
< |
public void testRunAfterBothAsync4E() { |
2568 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2569 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2570 |
< |
Noop r = new Noop(); |
2571 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2572 |
< |
assertTrue(f.cancel(true)); |
2573 |
< |
f2.complete(two); |
2574 |
< |
checkCompletedWithWrappedCancellationException(g); |
2575 |
< |
|
2576 |
< |
r = new Noop(); |
2577 |
< |
f = new CompletableFuture<>(); |
2578 |
< |
f2 = new CompletableFuture<>(); |
2579 |
< |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2580 |
< |
f.complete(one); |
2581 |
< |
assertTrue(f2.cancel(true)); |
2582 |
< |
checkCompletedWithWrappedCancellationException(g); |
2583 |
< |
} |
2584 |
< |
|
2585 |
< |
/** |
2586 |
< |
* applyToEitherAsync result completes normally after normal |
2587 |
< |
* completion of sources |
2588 |
< |
*/ |
2589 |
< |
public void testApplyToEitherAsyncE() { |
2590 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2591 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2592 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2593 |
< |
f.complete(one); |
2594 |
< |
checkCompletedNormally(g, two); |
2595 |
< |
|
2596 |
< |
f = new CompletableFuture<>(); |
2597 |
< |
f.complete(one); |
2598 |
< |
f2 = new CompletableFuture<>(); |
2599 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2600 |
< |
checkCompletedNormally(g, two); |
2601 |
< |
} |
2602 |
< |
|
2603 |
< |
/** |
2604 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
2605 |
< |
* completion of source |
2606 |
< |
*/ |
2607 |
< |
public void testApplyToEitherAsync2E() { |
2608 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2609 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2610 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2611 |
< |
f.completeExceptionally(new CFException()); |
2612 |
< |
checkCompletedWithWrappedCFException(g); |
2613 |
< |
|
2614 |
< |
f = new CompletableFuture<>(); |
2615 |
< |
f2 = new CompletableFuture<>(); |
2616 |
< |
f2.completeExceptionally(new CFException()); |
2617 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2618 |
< |
f.complete(one); |
2619 |
< |
checkCompletedWithWrappedCFException(g); |
2620 |
< |
} |
2621 |
< |
|
2622 |
< |
/** |
2623 |
< |
* applyToEitherAsync result completes exceptionally if action does |
2624 |
< |
*/ |
2625 |
< |
public void testApplyToEitherAsync3E() { |
2626 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2627 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2628 |
< |
FailingFunction r = new FailingFunction(); |
2629 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2630 |
< |
f.complete(one); |
2631 |
< |
checkCompletedWithWrappedCFException(g); |
2632 |
< |
} |
2633 |
< |
|
2634 |
< |
/** |
2635 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
2636 |
< |
*/ |
2637 |
< |
public void testApplyToEitherAsync4E() { |
2638 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2639 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2640 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2641 |
< |
assertTrue(f.cancel(true)); |
2642 |
< |
checkCompletedWithWrappedCancellationException(g); |
2643 |
< |
|
2644 |
< |
f = new CompletableFuture<>(); |
2645 |
< |
f2 = new CompletableFuture<>(); |
2646 |
< |
assertTrue(f2.cancel(true)); |
2647 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2648 |
< |
checkCompletedWithWrappedCancellationException(g); |
2649 |
< |
} |
2650 |
< |
|
2651 |
< |
/** |
2652 |
< |
* acceptEitherAsync result completes normally after normal |
2653 |
< |
* completion of sources |
2654 |
< |
*/ |
2655 |
< |
public void testAcceptEitherAsyncE() { |
2656 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2657 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2658 |
< |
IncAction r = new IncAction(); |
2659 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2660 |
< |
f.complete(one); |
2661 |
< |
checkCompletedNormally(g, null); |
2662 |
< |
assertEquals(r.value, 2); |
2663 |
< |
|
2664 |
< |
r = new IncAction(); |
2665 |
< |
f = new CompletableFuture<>(); |
2666 |
< |
f.complete(one); |
2667 |
< |
f2 = new CompletableFuture<>(); |
2668 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2669 |
< |
checkCompletedNormally(g, null); |
2670 |
< |
assertEquals(r.value, 2); |
2671 |
< |
} |
2672 |
< |
|
2673 |
< |
/** |
2674 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
2675 |
< |
* completion of source |
2676 |
< |
*/ |
2677 |
< |
public void testAcceptEitherAsync2E() { |
2678 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2679 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2680 |
< |
IncAction r = new IncAction(); |
2681 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2682 |
< |
f.completeExceptionally(new CFException()); |
2683 |
< |
checkCompletedWithWrappedCFException(g); |
2684 |
< |
|
2685 |
< |
r = new IncAction(); |
2686 |
< |
f = new CompletableFuture<>(); |
2687 |
< |
f2 = new CompletableFuture<>(); |
2688 |
< |
f2.completeExceptionally(new CFException()); |
2689 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2690 |
< |
f.complete(one); |
2691 |
< |
checkCompletedWithWrappedCFException(g); |
2692 |
< |
} |
2693 |
< |
|
2694 |
< |
/** |
2695 |
< |
* acceptEitherAsync result completes exceptionally if action does |
2696 |
< |
*/ |
2697 |
< |
public void testAcceptEitherAsync3E() { |
2698 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2699 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2700 |
< |
FailingConsumer r = new FailingConsumer(); |
2701 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2702 |
< |
f.complete(one); |
2703 |
< |
checkCompletedWithWrappedCFException(g); |
2704 |
< |
} |
2705 |
< |
|
2706 |
< |
/** |
2707 |
< |
* acceptEitherAsync result completes exceptionally if either |
2708 |
< |
* source cancelled |
2709 |
< |
*/ |
2710 |
< |
public void testAcceptEitherAsync4E() { |
2711 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2712 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2713 |
< |
IncAction r = new IncAction(); |
2714 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2715 |
< |
assertTrue(f.cancel(true)); |
2716 |
< |
checkCompletedWithWrappedCancellationException(g); |
2717 |
< |
|
2718 |
< |
r = new IncAction(); |
2719 |
< |
f = new CompletableFuture<>(); |
2720 |
< |
f2 = new CompletableFuture<>(); |
2721 |
< |
assertTrue(f2.cancel(true)); |
2722 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2723 |
< |
checkCompletedWithWrappedCancellationException(g); |
2724 |
< |
} |
2725 |
< |
|
2726 |
< |
/** |
2727 |
< |
* runAfterEitherAsync result completes normally after normal |
2728 |
< |
* completion of sources |
2729 |
< |
*/ |
2730 |
< |
public void testRunAfterEitherAsyncE() { |
2731 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2732 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2733 |
< |
Noop r = new Noop(); |
2734 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2735 |
< |
f.complete(one); |
2736 |
< |
checkCompletedNormally(g, null); |
2737 |
< |
assertTrue(r.ran); |
2738 |
< |
|
2739 |
< |
r = new Noop(); |
2740 |
< |
f = new CompletableFuture<>(); |
2741 |
< |
f.complete(one); |
2742 |
< |
f2 = new CompletableFuture<>(); |
2743 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2744 |
< |
checkCompletedNormally(g, null); |
2745 |
< |
assertTrue(r.ran); |
2746 |
< |
} |
2747 |
< |
|
2748 |
< |
/** |
2749 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
2750 |
< |
* completion of source |
2751 |
< |
*/ |
2752 |
< |
public void testRunAfterEitherAsync2E() { |
2753 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2754 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2755 |
< |
Noop r = new Noop(); |
2756 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2757 |
< |
f.completeExceptionally(new CFException()); |
2758 |
< |
checkCompletedWithWrappedCFException(g); |
2759 |
< |
|
2760 |
< |
r = new Noop(); |
2761 |
< |
f = new CompletableFuture<>(); |
2762 |
< |
f2 = new CompletableFuture<>(); |
2763 |
< |
f2.completeExceptionally(new CFException()); |
2764 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2765 |
< |
f.complete(one); |
2766 |
< |
checkCompletedWithWrappedCFException(g); |
2767 |
< |
} |
2768 |
< |
|
2769 |
< |
/** |
2770 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
2771 |
< |
*/ |
2772 |
< |
public void testRunAfterEitherAsync3E() { |
2773 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2774 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2775 |
< |
FailingNoop r = new FailingNoop(); |
2776 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2777 |
< |
f.complete(one); |
2778 |
< |
checkCompletedWithWrappedCFException(g); |
3606 |
> |
Object resultOf(CompletableFuture<?> f) { |
3607 |
> |
try { |
3608 |
> |
java.lang.reflect.Field resultField |
3609 |
> |
= CompletableFuture.class.getDeclaredField("result"); |
3610 |
> |
resultField.setAccessible(true); |
3611 |
> |
return resultField.get(f); |
3612 |
> |
} catch (Throwable t) { throw new AssertionError(t); } |
3613 |
|
} |
3614 |
|
|
3615 |
< |
/** |
3616 |
< |
* runAfterEitherAsync result completes exceptionally if either |
3617 |
< |
* source cancelled |
3618 |
< |
*/ |
3619 |
< |
public void testRunAfterEitherAsync4E() { |
3620 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3621 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<>(); |
2788 |
< |
Noop r = new Noop(); |
2789 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2790 |
< |
assertTrue(f.cancel(true)); |
2791 |
< |
checkCompletedWithWrappedCancellationException(g); |
3615 |
> |
public void testExceptionPropagationReusesResultObject() { |
3616 |
> |
if (!testImplementationDetails) return; |
3617 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
3618 |
> |
{ |
3619 |
> |
final CFException ex = new CFException(); |
3620 |
> |
final CompletableFuture<Integer> v42 = CompletableFuture.completedFuture(42); |
3621 |
> |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3622 |
|
|
3623 |
< |
r = new Noop(); |
3624 |
< |
f = new CompletableFuture<>(); |
2795 |
< |
f2 = new CompletableFuture<>(); |
2796 |
< |
assertTrue(f2.cancel(true)); |
2797 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2798 |
< |
checkCompletedWithWrappedCancellationException(g); |
2799 |
< |
} |
3623 |
> |
List<Function<CompletableFuture<Integer>, CompletableFuture<?>>> funs |
3624 |
> |
= new ArrayList<>(); |
3625 |
|
|
3626 |
< |
/** |
3627 |
< |
* thenComposeAsync result completes normally after normal |
3628 |
< |
* completion of source |
2804 |
< |
*/ |
2805 |
< |
public void testThenComposeAsyncE() { |
2806 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2807 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
2808 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2809 |
< |
f.complete(one); |
2810 |
< |
checkCompletedNormally(g, two); |
2811 |
< |
} |
3626 |
> |
funs.add((y) -> m.thenRun(y, new Noop(m))); |
3627 |
> |
funs.add((y) -> m.thenAccept(y, new NoopConsumer(m))); |
3628 |
> |
funs.add((y) -> m.thenApply(y, new IncFunction(m))); |
3629 |
|
|
3630 |
< |
/** |
3631 |
< |
* thenComposeAsync result completes exceptionally after |
3632 |
< |
* exceptional completion of source |
2816 |
< |
*/ |
2817 |
< |
public void testThenComposeAsync2E() { |
2818 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2819 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
2820 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2821 |
< |
f.completeExceptionally(new CFException()); |
2822 |
< |
checkCompletedWithWrappedCFException(g); |
2823 |
< |
} |
3630 |
> |
funs.add((y) -> m.runAfterEither(y, incomplete, new Noop(m))); |
3631 |
> |
funs.add((y) -> m.acceptEither(y, incomplete, new NoopConsumer(m))); |
3632 |
> |
funs.add((y) -> m.applyToEither(y, incomplete, new IncFunction(m))); |
3633 |
|
|
3634 |
< |
/** |
3635 |
< |
* thenComposeAsync result completes exceptionally if action does |
3636 |
< |
*/ |
2828 |
< |
public void testThenComposeAsync3E() { |
2829 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2830 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
2831 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2832 |
< |
f.complete(one); |
2833 |
< |
checkCompletedWithWrappedCFException(g); |
2834 |
< |
} |
3634 |
> |
funs.add((y) -> m.runAfterBoth(y, v42, new Noop(m))); |
3635 |
> |
funs.add((y) -> m.thenAcceptBoth(y, v42, new SubtractAction(m))); |
3636 |
> |
funs.add((y) -> m.thenCombine(y, v42, new SubtractFunction(m))); |
3637 |
|
|
3638 |
< |
/** |
2837 |
< |
* thenComposeAsync result completes exceptionally if source cancelled |
2838 |
< |
*/ |
2839 |
< |
public void testThenComposeAsync4E() { |
2840 |
< |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2841 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
2842 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2843 |
< |
assertTrue(f.cancel(true)); |
2844 |
< |
checkCompletedWithWrappedCancellationException(g); |
2845 |
< |
} |
3638 |
> |
funs.add((y) -> m.whenComplete(y, (Integer x, Throwable t) -> {})); |
3639 |
|
|
3640 |
< |
// other static methods |
3640 |
> |
funs.add((y) -> m.thenCompose(y, new CompletableFutureInc(m))); |
3641 |
|
|
3642 |
< |
/** |
3643 |
< |
* allOf(no component futures) returns a future completed normally |
2851 |
< |
* with the value null |
2852 |
< |
*/ |
2853 |
< |
public void testAllOf_empty() throws Exception { |
2854 |
< |
CompletableFuture<Void> f = CompletableFuture.allOf(); |
2855 |
< |
checkCompletedNormally(f, null); |
2856 |
< |
} |
3642 |
> |
funs.add((y) -> CompletableFuture.allOf(new CompletableFuture<?>[] {y, v42})); |
3643 |
> |
funs.add((y) -> CompletableFuture.anyOf(new CompletableFuture<?>[] {y, incomplete})); |
3644 |
|
|
3645 |
< |
/** |
3646 |
< |
* allOf returns a future completed normally with the value null |
3647 |
< |
* when all components complete normally |
3648 |
< |
*/ |
3649 |
< |
public void testAllOf_normal() throws Exception { |
3650 |
< |
for (int k = 1; k < 20; ++k) { |
3651 |
< |
CompletableFuture<Integer>[] fs = (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
3652 |
< |
for (int i = 0; i < k; ++i) |
3653 |
< |
fs[i] = new CompletableFuture<>(); |
2867 |
< |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2868 |
< |
for (int i = 0; i < k; ++i) { |
2869 |
< |
checkIncomplete(f); |
2870 |
< |
checkIncomplete(CompletableFuture.allOf(fs)); |
2871 |
< |
fs[i].complete(one); |
2872 |
< |
} |
2873 |
< |
checkCompletedNormally(f, null); |
2874 |
< |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
3645 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3646 |
> |
fun : funs) { |
3647 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3648 |
> |
f.completeExceptionally(ex); |
3649 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3650 |
> |
checkCompletedWithWrappedException(src, ex); |
3651 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3652 |
> |
checkCompletedWithWrappedException(dep, ex); |
3653 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3654 |
|
} |
2876 |
– |
} |
3655 |
|
|
3656 |
< |
/** |
3657 |
< |
* anyOf(no component futures) returns an incomplete future |
3658 |
< |
*/ |
3659 |
< |
public void testAnyOf_empty() throws Exception { |
3660 |
< |
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
3661 |
< |
checkIncomplete(f); |
3662 |
< |
} |
3663 |
< |
|
3664 |
< |
/** |
2887 |
< |
* anyOf returns a future completed normally with a value when |
2888 |
< |
* a component future does |
2889 |
< |
*/ |
2890 |
< |
public void testAnyOf_normal() throws Exception { |
2891 |
< |
for (int k = 0; k < 10; ++k) { |
2892 |
< |
CompletableFuture[] fs = new CompletableFuture[k]; |
2893 |
< |
for (int i = 0; i < k; ++i) |
2894 |
< |
fs[i] = new CompletableFuture<>(); |
2895 |
< |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2896 |
< |
checkIncomplete(f); |
2897 |
< |
for (int i = 0; i < k; ++i) { |
2898 |
< |
fs[i].complete(one); |
2899 |
< |
checkCompletedNormally(f, one); |
2900 |
< |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
2901 |
< |
} |
3656 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3657 |
> |
fun : funs) { |
3658 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3659 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3660 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3661 |
> |
f.completeExceptionally(ex); |
3662 |
> |
checkCompletedWithWrappedException(src, ex); |
3663 |
> |
checkCompletedWithWrappedException(dep, ex); |
3664 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3665 |
|
} |
2903 |
– |
} |
3666 |
|
|
3667 |
< |
/** |
3668 |
< |
* anyOf result completes exceptionally when any component does. |
3669 |
< |
*/ |
3670 |
< |
public void testAnyOf_exceptional() throws Exception { |
3671 |
< |
for (int k = 0; k < 10; ++k) { |
3672 |
< |
CompletableFuture[] fs = new CompletableFuture[k]; |
3673 |
< |
for (int i = 0; i < k; ++i) |
3674 |
< |
fs[i] = new CompletableFuture<>(); |
3675 |
< |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
3676 |
< |
checkIncomplete(f); |
3677 |
< |
for (int i = 0; i < k; ++i) { |
2916 |
< |
fs[i].completeExceptionally(new CFException()); |
2917 |
< |
checkCompletedWithWrappedCFException(f); |
2918 |
< |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
2919 |
< |
} |
3667 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3668 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3669 |
> |
fun : funs) { |
3670 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3671 |
> |
f.cancel(mayInterruptIfRunning); |
3672 |
> |
checkCancelled(f); |
3673 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3674 |
> |
checkCompletedWithWrappedCancellationException(src); |
3675 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3676 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3677 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3678 |
|
} |
2921 |
– |
} |
2922 |
– |
|
2923 |
– |
/** |
2924 |
– |
* Completion methods throw NullPointerException with null arguments |
2925 |
– |
*/ |
2926 |
– |
public void testNPE() { |
2927 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2928 |
– |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
2929 |
– |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2930 |
– |
CompletableFuture<?> h; |
2931 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
2932 |
– |
|
2933 |
– |
Runnable[] throwingActions = { |
2934 |
– |
() -> { CompletableFuture.supplyAsync(null); }, |
2935 |
– |
() -> { CompletableFuture.supplyAsync(null, exec); }, |
2936 |
– |
() -> { CompletableFuture.supplyAsync(supplyOne, null); }, |
2937 |
– |
|
2938 |
– |
() -> { CompletableFuture.runAsync(null); }, |
2939 |
– |
() -> { CompletableFuture.runAsync(null, exec); }, |
2940 |
– |
() -> { CompletableFuture.runAsync(() -> {}, null); }, |
2941 |
– |
|
2942 |
– |
() -> { f.completeExceptionally(null); }, |
2943 |
– |
|
2944 |
– |
() -> { f.thenApply(null); }, |
2945 |
– |
() -> { f.thenApplyAsync(null); }, |
2946 |
– |
() -> { f.thenApplyAsync((x) -> x, null); }, |
2947 |
– |
() -> { f.thenApplyAsync(null, exec); }, |
2948 |
– |
|
2949 |
– |
() -> { f.thenAccept(null); }, |
2950 |
– |
() -> { f.thenAcceptAsync(null); }, |
2951 |
– |
() -> { f.thenAcceptAsync((x) -> { ; }, null); }, |
2952 |
– |
() -> { f.thenAcceptAsync(null, exec); }, |
2953 |
– |
|
2954 |
– |
() -> { f.thenRun(null); }, |
2955 |
– |
() -> { f.thenRunAsync(null); }, |
2956 |
– |
() -> { f.thenRunAsync(() -> { ; }, null); }, |
2957 |
– |
() -> { f.thenRunAsync(null, exec); }, |
2958 |
– |
|
2959 |
– |
() -> { f.thenCombine(g, null); }, |
2960 |
– |
() -> { f.thenCombineAsync(g, null); }, |
2961 |
– |
() -> { f.thenCombineAsync(g, null, exec); }, |
2962 |
– |
() -> { f.thenCombine(nullFuture, (x, y) -> x); }, |
2963 |
– |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x); }, |
2964 |
– |
() -> { f.thenCombineAsync(nullFuture, (x, y) -> x, exec); }, |
2965 |
– |
() -> { f.thenCombineAsync(g, (x, y) -> x, null); }, |
2966 |
– |
|
2967 |
– |
() -> { f.thenAcceptBoth(g, null); }, |
2968 |
– |
() -> { f.thenAcceptBothAsync(g, null); }, |
2969 |
– |
() -> { f.thenAcceptBothAsync(g, null, exec); }, |
2970 |
– |
() -> { f.thenAcceptBoth(nullFuture, (x, y) -> {}); }, |
2971 |
– |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}); }, |
2972 |
– |
() -> { f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec); }, |
2973 |
– |
() -> { f.thenAcceptBothAsync(g, (x, y) -> {}, null); }, |
2974 |
– |
|
2975 |
– |
() -> { f.runAfterBoth(g, null); }, |
2976 |
– |
() -> { f.runAfterBothAsync(g, null); }, |
2977 |
– |
() -> { f.runAfterBothAsync(g, null, exec); }, |
2978 |
– |
() -> { f.runAfterBoth(nullFuture, () -> {}); }, |
2979 |
– |
() -> { f.runAfterBothAsync(nullFuture, () -> {}); }, |
2980 |
– |
() -> { f.runAfterBothAsync(nullFuture, () -> {}, exec); }, |
2981 |
– |
() -> { f.runAfterBothAsync(g, () -> {}, null); }, |
2982 |
– |
|
2983 |
– |
() -> { f.applyToEither(g, null); }, |
2984 |
– |
() -> { f.applyToEitherAsync(g, null); }, |
2985 |
– |
() -> { f.applyToEitherAsync(g, null, exec); }, |
2986 |
– |
() -> { f.applyToEither(nullFuture, (x) -> x); }, |
2987 |
– |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x); }, |
2988 |
– |
() -> { f.applyToEitherAsync(nullFuture, (x) -> x, exec); }, |
2989 |
– |
() -> { f.applyToEitherAsync(g, (x) -> x, null); }, |
2990 |
– |
|
2991 |
– |
() -> { f.acceptEither(g, null); }, |
2992 |
– |
() -> { f.acceptEitherAsync(g, null); }, |
2993 |
– |
() -> { f.acceptEitherAsync(g, null, exec); }, |
2994 |
– |
() -> { f.acceptEither(nullFuture, (x) -> {}); }, |
2995 |
– |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}); }, |
2996 |
– |
() -> { f.acceptEitherAsync(nullFuture, (x) -> {}, exec); }, |
2997 |
– |
() -> { f.acceptEitherAsync(g, (x) -> {}, null); }, |
2998 |
– |
|
2999 |
– |
() -> { f.runAfterEither(g, null); }, |
3000 |
– |
() -> { f.runAfterEitherAsync(g, null); }, |
3001 |
– |
() -> { f.runAfterEitherAsync(g, null, exec); }, |
3002 |
– |
() -> { f.runAfterEither(nullFuture, () -> {}); }, |
3003 |
– |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}); }, |
3004 |
– |
() -> { f.runAfterEitherAsync(nullFuture, () -> {}, exec); }, |
3005 |
– |
() -> { f.runAfterEitherAsync(g, () -> {}, null); }, |
3006 |
– |
|
3007 |
– |
() -> { f.thenCompose(null); }, |
3008 |
– |
() -> { f.thenComposeAsync(null); }, |
3009 |
– |
() -> { f.thenComposeAsync(new CompletableFutureInc(), null); }, |
3010 |
– |
() -> { f.thenComposeAsync(null, exec); }, |
3011 |
– |
|
3012 |
– |
() -> { f.exceptionally(null); }, |
3013 |
– |
|
3014 |
– |
() -> { f.handle(null); }, |
3015 |
– |
|
3016 |
– |
() -> { CompletableFuture.allOf((CompletableFuture<?>)null); }, |
3017 |
– |
() -> { CompletableFuture.allOf((CompletableFuture<?>[])null); }, |
3018 |
– |
() -> { CompletableFuture.allOf(f, null); }, |
3019 |
– |
() -> { CompletableFuture.allOf(null, f); }, |
3020 |
– |
|
3021 |
– |
() -> { CompletableFuture.anyOf((CompletableFuture<?>)null); }, |
3022 |
– |
() -> { CompletableFuture.anyOf((CompletableFuture<?>[])null); }, |
3023 |
– |
() -> { CompletableFuture.anyOf(f, null); }, |
3024 |
– |
() -> { CompletableFuture.anyOf(null, f); }, |
3025 |
– |
}; |
3026 |
– |
|
3027 |
– |
assertThrows(NullPointerException.class, throwingActions); |
3028 |
– |
assertEquals(0, exec.count.get()); |
3029 |
– |
} |
3030 |
– |
|
3031 |
– |
/** |
3032 |
– |
* toCompletableFuture returns this CompletableFuture. |
3033 |
– |
*/ |
3034 |
– |
public void testToCompletableFuture() { |
3035 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3036 |
– |
assertSame(f, f.toCompletableFuture()); |
3037 |
– |
} |
3038 |
– |
|
3039 |
– |
/** |
3040 |
– |
* whenComplete action executes on normal completion, propagating |
3041 |
– |
* source result. |
3042 |
– |
*/ |
3043 |
– |
public void testWhenComplete1() { |
3044 |
– |
final AtomicInteger a = new AtomicInteger(); |
3045 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3046 |
– |
CompletableFuture<Integer> g = |
3047 |
– |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3048 |
– |
f.complete(three); |
3049 |
– |
checkCompletedNormally(f, three); |
3050 |
– |
checkCompletedNormally(g, three); |
3051 |
– |
assertEquals(a.get(), 1); |
3052 |
– |
} |
3053 |
– |
|
3054 |
– |
/** |
3055 |
– |
* whenComplete action executes on exceptional completion, propagating |
3056 |
– |
* source result. |
3057 |
– |
*/ |
3058 |
– |
public void testWhenComplete2() { |
3059 |
– |
final AtomicInteger a = new AtomicInteger(); |
3060 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3061 |
– |
CompletableFuture<Integer> g = |
3062 |
– |
f.whenComplete((Integer x, Throwable t) -> a.getAndIncrement()); |
3063 |
– |
f.completeExceptionally(new CFException()); |
3064 |
– |
assertTrue(f.isCompletedExceptionally()); |
3065 |
– |
assertTrue(g.isCompletedExceptionally()); |
3066 |
– |
assertEquals(a.get(), 1); |
3067 |
– |
} |
3068 |
– |
|
3069 |
– |
/** |
3070 |
– |
* If a whenComplete action throws an exception when triggered by |
3071 |
– |
* a normal completion, it completes exceptionally |
3072 |
– |
*/ |
3073 |
– |
public void testWhenComplete3() { |
3074 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3075 |
– |
CompletableFuture<Integer> g = |
3076 |
– |
f.whenComplete((Integer x, Throwable t) -> |
3077 |
– |
{ throw new CFException(); } ); |
3078 |
– |
f.complete(three); |
3079 |
– |
checkCompletedNormally(f, three); |
3080 |
– |
assertTrue(g.isCompletedExceptionally()); |
3081 |
– |
checkCompletedWithWrappedCFException(g); |
3082 |
– |
} |
3083 |
– |
|
3084 |
– |
/** |
3085 |
– |
* whenCompleteAsync action executes on normal completion, propagating |
3086 |
– |
* source result. |
3087 |
– |
*/ |
3088 |
– |
public void testWhenCompleteAsync1() { |
3089 |
– |
final AtomicInteger a = new AtomicInteger(); |
3090 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3091 |
– |
CompletableFuture<Integer> g = |
3092 |
– |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3093 |
– |
f.complete(three); |
3094 |
– |
checkCompletedNormally(f, three); |
3095 |
– |
checkCompletedNormally(g, three); |
3096 |
– |
assertEquals(a.get(), 1); |
3097 |
– |
} |
3098 |
– |
|
3099 |
– |
/** |
3100 |
– |
* whenCompleteAsync action executes on exceptional completion, propagating |
3101 |
– |
* source result. |
3102 |
– |
*/ |
3103 |
– |
public void testWhenCompleteAsync2() { |
3104 |
– |
final AtomicInteger a = new AtomicInteger(); |
3105 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3106 |
– |
CompletableFuture<Integer> g = |
3107 |
– |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement()); |
3108 |
– |
f.completeExceptionally(new CFException()); |
3109 |
– |
checkCompletedWithWrappedCFException(f); |
3110 |
– |
checkCompletedWithWrappedCFException(g); |
3111 |
– |
} |
3112 |
– |
|
3113 |
– |
/** |
3114 |
– |
* If a whenCompleteAsync action throws an exception when |
3115 |
– |
* triggered by a normal completion, it completes exceptionally |
3116 |
– |
*/ |
3117 |
– |
public void testWhenCompleteAsync3() { |
3118 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3119 |
– |
CompletableFuture<Integer> g = |
3120 |
– |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3121 |
– |
{ throw new CFException(); } ); |
3122 |
– |
f.complete(three); |
3123 |
– |
checkCompletedNormally(f, three); |
3124 |
– |
checkCompletedWithWrappedCFException(g); |
3125 |
– |
} |
3126 |
– |
|
3127 |
– |
/** |
3128 |
– |
* whenCompleteAsync action executes on normal completion, propagating |
3129 |
– |
* source result. |
3130 |
– |
*/ |
3131 |
– |
public void testWhenCompleteAsync1e() { |
3132 |
– |
final AtomicInteger a = new AtomicInteger(); |
3133 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
3134 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3135 |
– |
CompletableFuture<Integer> g = |
3136 |
– |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3137 |
– |
exec); |
3138 |
– |
f.complete(three); |
3139 |
– |
checkCompletedNormally(f, three); |
3140 |
– |
checkCompletedNormally(g, three); |
3141 |
– |
assertEquals(a.get(), 1); |
3142 |
– |
} |
3143 |
– |
|
3144 |
– |
/** |
3145 |
– |
* whenCompleteAsync action executes on exceptional completion, propagating |
3146 |
– |
* source result. |
3147 |
– |
*/ |
3148 |
– |
public void testWhenCompleteAsync2e() { |
3149 |
– |
final AtomicInteger a = new AtomicInteger(); |
3150 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
3151 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3152 |
– |
CompletableFuture<Integer> g = |
3153 |
– |
f.whenCompleteAsync((Integer x, Throwable t) -> a.getAndIncrement(), |
3154 |
– |
exec); |
3155 |
– |
f.completeExceptionally(new CFException()); |
3156 |
– |
checkCompletedWithWrappedCFException(f); |
3157 |
– |
checkCompletedWithWrappedCFException(g); |
3158 |
– |
} |
3159 |
– |
|
3160 |
– |
/** |
3161 |
– |
* If a whenCompleteAsync action throws an exception when triggered |
3162 |
– |
* by a normal completion, it completes exceptionally |
3163 |
– |
*/ |
3164 |
– |
public void testWhenCompleteAsync3e() { |
3165 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
3166 |
– |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3167 |
– |
CompletableFuture<Integer> g = |
3168 |
– |
f.whenCompleteAsync((Integer x, Throwable t) -> |
3169 |
– |
{ throw new CFException(); }, |
3170 |
– |
exec); |
3171 |
– |
f.complete(three); |
3172 |
– |
checkCompletedNormally(f, three); |
3173 |
– |
checkCompletedWithWrappedCFException(g); |
3174 |
– |
} |
3175 |
– |
|
3176 |
– |
/** |
3177 |
– |
* handleAsync action completes normally with function value on |
3178 |
– |
* either normal or exceptional completion of source |
3179 |
– |
*/ |
3180 |
– |
public void testHandleAsync() { |
3181 |
– |
CompletableFuture<Integer> f, g; |
3182 |
– |
IntegerHandler r; |
3183 |
– |
|
3184 |
– |
f = new CompletableFuture<>(); |
3185 |
– |
g = f.handleAsync(r = new IntegerHandler()); |
3186 |
– |
assertFalse(r.ran); |
3187 |
– |
f.completeExceptionally(new CFException()); |
3188 |
– |
checkCompletedWithWrappedCFException(f); |
3189 |
– |
checkCompletedNormally(g, three); |
3190 |
– |
assertTrue(r.ran); |
3191 |
– |
|
3192 |
– |
f = new CompletableFuture<>(); |
3193 |
– |
g = f.handleAsync(r = new IntegerHandler()); |
3194 |
– |
assertFalse(r.ran); |
3195 |
– |
f.completeExceptionally(new CFException()); |
3196 |
– |
checkCompletedWithWrappedCFException(f); |
3197 |
– |
checkCompletedNormally(g, three); |
3198 |
– |
assertTrue(r.ran); |
3199 |
– |
|
3200 |
– |
f = new CompletableFuture<>(); |
3201 |
– |
g = f.handleAsync(r = new IntegerHandler()); |
3202 |
– |
assertFalse(r.ran); |
3203 |
– |
f.complete(one); |
3204 |
– |
checkCompletedNormally(f, one); |
3205 |
– |
checkCompletedNormally(g, two); |
3206 |
– |
assertTrue(r.ran); |
3207 |
– |
|
3208 |
– |
f = new CompletableFuture<>(); |
3209 |
– |
g = f.handleAsync(r = new IntegerHandler()); |
3210 |
– |
assertFalse(r.ran); |
3211 |
– |
f.complete(one); |
3212 |
– |
checkCompletedNormally(f, one); |
3213 |
– |
checkCompletedNormally(g, two); |
3214 |
– |
assertTrue(r.ran); |
3215 |
– |
} |
3216 |
– |
|
3217 |
– |
/** |
3218 |
– |
* handleAsync action with Executor completes normally with |
3219 |
– |
* function value on either normal or exceptional completion of |
3220 |
– |
* source |
3221 |
– |
*/ |
3222 |
– |
public void testHandleAsync2() { |
3223 |
– |
CompletableFuture<Integer> f, g; |
3224 |
– |
ThreadExecutor exec = new ThreadExecutor(); |
3225 |
– |
IntegerHandler r; |
3226 |
– |
|
3227 |
– |
f = new CompletableFuture<>(); |
3228 |
– |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3229 |
– |
assertFalse(r.ran); |
3230 |
– |
f.completeExceptionally(new CFException()); |
3231 |
– |
checkCompletedWithWrappedCFException(f); |
3232 |
– |
checkCompletedNormally(g, three); |
3233 |
– |
assertTrue(r.ran); |
3679 |
|
|
3680 |
< |
f = new CompletableFuture<>(); |
3681 |
< |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3682 |
< |
assertFalse(r.ran); |
3683 |
< |
f.completeExceptionally(new CFException()); |
3684 |
< |
checkCompletedWithWrappedCFException(f); |
3685 |
< |
checkCompletedNormally(g, three); |
3686 |
< |
assertTrue(r.ran); |
3687 |
< |
|
3688 |
< |
f = new CompletableFuture<>(); |
3689 |
< |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3690 |
< |
assertFalse(r.ran); |
3691 |
< |
f.complete(one); |
3692 |
< |
checkCompletedNormally(f, one); |
3248 |
< |
checkCompletedNormally(g, two); |
3249 |
< |
assertTrue(r.ran); |
3250 |
< |
|
3251 |
< |
f = new CompletableFuture<>(); |
3252 |
< |
g = f.handleAsync(r = new IntegerHandler(), exec); |
3253 |
< |
assertFalse(r.ran); |
3254 |
< |
f.complete(one); |
3255 |
< |
checkCompletedNormally(f, one); |
3256 |
< |
checkCompletedNormally(g, two); |
3257 |
< |
assertTrue(r.ran); |
3258 |
< |
} |
3680 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
3681 |
> |
for (Function<CompletableFuture<Integer>, CompletableFuture<?>> |
3682 |
> |
fun : funs) { |
3683 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3684 |
> |
CompletableFuture<Integer> src = m.thenApply(f, new IncFunction(m)); |
3685 |
> |
CompletableFuture<?> dep = fun.apply(src); |
3686 |
> |
f.cancel(mayInterruptIfRunning); |
3687 |
> |
checkCancelled(f); |
3688 |
> |
checkCompletedWithWrappedCancellationException(src); |
3689 |
> |
checkCompletedWithWrappedCancellationException(dep); |
3690 |
> |
assertSame(resultOf(src), resultOf(dep)); |
3691 |
> |
} |
3692 |
> |
}} |
3693 |
|
|
3694 |
|
} |