16 |
|
import java.util.concurrent.Future; |
17 |
|
import java.util.concurrent.CompletableFuture; |
18 |
|
import java.util.concurrent.CompletionException; |
19 |
+ |
import java.util.concurrent.CompletionStage; |
20 |
+ |
import java.util.concurrent.ForkJoinPool; |
21 |
+ |
import java.util.concurrent.ForkJoinTask; |
22 |
|
import java.util.concurrent.TimeoutException; |
23 |
|
import java.util.concurrent.atomic.AtomicInteger; |
24 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
56 |
|
catch (Throwable fail) { threadUnexpectedException(fail); } |
57 |
|
} |
58 |
|
|
59 |
< |
void checkCompletedNormally(CompletableFuture<?> f, Object value) { |
59 |
> |
<T> void checkCompletedNormally(CompletableFuture<T> f, T value) { |
60 |
> |
try { |
61 |
> |
assertEquals(value, f.get(LONG_DELAY_MS, MILLISECONDS)); |
62 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
63 |
|
try { |
64 |
|
assertEquals(value, f.join()); |
65 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
69 |
|
try { |
70 |
|
assertEquals(value, f.get()); |
71 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
66 |
– |
try { |
67 |
– |
assertEquals(value, f.get(0L, SECONDS)); |
68 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
72 |
|
assertTrue(f.isDone()); |
73 |
|
assertFalse(f.isCancelled()); |
74 |
+ |
assertFalse(f.isCompletedExceptionally()); |
75 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
76 |
|
} |
77 |
|
|
78 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
79 |
|
try { |
80 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
81 |
+ |
shouldThrow(); |
82 |
+ |
} catch (ExecutionException success) { |
83 |
+ |
assertTrue(success.getCause() instanceof CFException); |
84 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
85 |
+ |
try { |
86 |
|
f.join(); |
87 |
|
shouldThrow(); |
88 |
|
} catch (CompletionException success) { |
100 |
|
} catch (ExecutionException success) { |
101 |
|
assertTrue(success.getCause() instanceof CFException); |
102 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
103 |
+ |
assertTrue(f.isDone()); |
104 |
+ |
assertFalse(f.isCancelled()); |
105 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
106 |
+ |
} |
107 |
+ |
|
108 |
+ |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
109 |
+ |
Throwable ex) { |
110 |
|
try { |
111 |
< |
f.get(0L, SECONDS); |
111 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
112 |
|
shouldThrow(); |
113 |
|
} catch (ExecutionException success) { |
114 |
< |
assertTrue(success.getCause() instanceof CFException); |
114 |
> |
assertSame(ex, success.getCause()); |
115 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
116 |
> |
try { |
117 |
> |
f.join(); |
118 |
> |
shouldThrow(); |
119 |
> |
} catch (CompletionException success) { |
120 |
> |
assertSame(ex, success.getCause()); |
121 |
> |
} |
122 |
> |
try { |
123 |
> |
f.getNow(null); |
124 |
> |
shouldThrow(); |
125 |
> |
} catch (CompletionException success) { |
126 |
> |
assertSame(ex, success.getCause()); |
127 |
> |
} |
128 |
> |
try { |
129 |
> |
f.get(); |
130 |
> |
shouldThrow(); |
131 |
> |
} catch (ExecutionException success) { |
132 |
> |
assertSame(ex, success.getCause()); |
133 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
134 |
+ |
|
135 |
|
assertTrue(f.isDone()); |
136 |
|
assertFalse(f.isCancelled()); |
137 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
138 |
+ |
} |
139 |
+ |
|
140 |
+ |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
141 |
+ |
Throwable ex) { |
142 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
143 |
+ |
try { |
144 |
+ |
CompletableFuture<Throwable> spy = f.handle |
145 |
+ |
((U u, Throwable t) -> t); |
146 |
+ |
assertTrue(spy.join() instanceof CompletionException); |
147 |
+ |
assertSame(ex, spy.join().getCause()); |
148 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
149 |
+ |
} |
150 |
+ |
|
151 |
+ |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
152 |
+ |
checkCompletedExceptionallyWithRootCause(f, ex); |
153 |
+ |
try { |
154 |
+ |
CompletableFuture<Throwable> spy = f.handle |
155 |
+ |
((U u, Throwable t) -> t); |
156 |
+ |
assertSame(ex, spy.join()); |
157 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
158 |
|
} |
159 |
|
|
160 |
|
void checkCancelled(CompletableFuture<?> f) { |
161 |
|
try { |
162 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
163 |
+ |
shouldThrow(); |
164 |
+ |
} catch (CancellationException success) { |
165 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
166 |
+ |
try { |
167 |
|
f.join(); |
168 |
|
shouldThrow(); |
169 |
|
} catch (CancellationException success) {} |
176 |
|
shouldThrow(); |
177 |
|
} catch (CancellationException success) { |
178 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
117 |
– |
try { |
118 |
– |
f.get(0L, SECONDS); |
119 |
– |
shouldThrow(); |
120 |
– |
} catch (CancellationException success) { |
121 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
179 |
|
assertTrue(f.isDone()); |
180 |
+ |
assertTrue(f.isCompletedExceptionally()); |
181 |
|
assertTrue(f.isCancelled()); |
182 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
183 |
|
} |
184 |
|
|
185 |
|
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
186 |
|
try { |
187 |
+ |
f.get(LONG_DELAY_MS, MILLISECONDS); |
188 |
+ |
shouldThrow(); |
189 |
+ |
} catch (ExecutionException success) { |
190 |
+ |
assertTrue(success.getCause() instanceof CancellationException); |
191 |
+ |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
192 |
+ |
try { |
193 |
|
f.join(); |
194 |
|
shouldThrow(); |
195 |
|
} catch (CompletionException success) { |
207 |
|
} catch (ExecutionException success) { |
208 |
|
assertTrue(success.getCause() instanceof CancellationException); |
209 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
145 |
– |
try { |
146 |
– |
f.get(0L, SECONDS); |
147 |
– |
shouldThrow(); |
148 |
– |
} catch (ExecutionException success) { |
149 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
150 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
210 |
|
assertTrue(f.isDone()); |
211 |
|
assertFalse(f.isCancelled()); |
212 |
+ |
assertTrue(f.isCompletedExceptionally()); |
213 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
214 |
|
} |
215 |
|
|
216 |
|
/** |
218 |
|
* by methods isDone, isCancelled, and getNow |
219 |
|
*/ |
220 |
|
public void testConstructor() { |
221 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
221 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
222 |
|
checkIncomplete(f); |
223 |
|
} |
224 |
|
|
227 |
|
* isCancelled, join, get, and getNow |
228 |
|
*/ |
229 |
|
public void testComplete() { |
230 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
230 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
231 |
|
checkIncomplete(f); |
232 |
|
f.complete(one); |
233 |
|
checkCompletedNormally(f, one); |
238 |
|
* methods isDone, isCancelled, join, get, and getNow |
239 |
|
*/ |
240 |
|
public void testCompleteExceptionally() { |
241 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
241 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
242 |
> |
CFException ex = new CFException(); |
243 |
|
checkIncomplete(f); |
244 |
< |
f.completeExceptionally(new CFException()); |
245 |
< |
checkCompletedWithWrappedCFException(f); |
244 |
> |
f.completeExceptionally(ex); |
245 |
> |
checkCompletedExceptionally(f, ex); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
250 |
|
* methods isDone, isCancelled, join, get, and getNow |
251 |
|
*/ |
252 |
|
public void testCancel() { |
253 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
253 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
254 |
|
checkIncomplete(f); |
255 |
|
assertTrue(f.cancel(true)); |
256 |
|
checkCancelled(f); |
260 |
|
* obtrudeValue forces completion with given value |
261 |
|
*/ |
262 |
|
public void testObtrudeValue() { |
263 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
263 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
264 |
|
checkIncomplete(f); |
265 |
|
f.complete(one); |
266 |
|
checkCompletedNormally(f, one); |
268 |
|
checkCompletedNormally(f, three); |
269 |
|
f.obtrudeValue(two); |
270 |
|
checkCompletedNormally(f, two); |
271 |
< |
f = new CompletableFuture<Integer>(); |
271 |
> |
f = new CompletableFuture<>(); |
272 |
|
f.obtrudeValue(three); |
273 |
|
checkCompletedNormally(f, three); |
274 |
< |
f = new CompletableFuture<Integer>(); |
274 |
> |
f.obtrudeValue(null); |
275 |
> |
checkCompletedNormally(f, null); |
276 |
> |
f = new CompletableFuture<>(); |
277 |
|
f.completeExceptionally(new CFException()); |
278 |
|
f.obtrudeValue(four); |
279 |
|
checkCompletedNormally(f, four); |
283 |
|
* obtrudeException forces completion with given exception |
284 |
|
*/ |
285 |
|
public void testObtrudeException() { |
286 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
287 |
< |
checkIncomplete(f); |
288 |
< |
f.complete(one); |
289 |
< |
checkCompletedNormally(f, one); |
290 |
< |
f.obtrudeException(new CFException()); |
291 |
< |
checkCompletedWithWrappedCFException(f); |
292 |
< |
f = new CompletableFuture<Integer>(); |
293 |
< |
f.obtrudeException(new CFException()); |
294 |
< |
checkCompletedWithWrappedCFException(f); |
295 |
< |
f = new CompletableFuture<Integer>(); |
296 |
< |
f.completeExceptionally(new CFException()); |
297 |
< |
f.obtrudeValue(four); |
298 |
< |
checkCompletedNormally(f, four); |
299 |
< |
f.obtrudeException(new CFException()); |
300 |
< |
checkCompletedWithWrappedCFException(f); |
301 |
< |
} |
286 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
287 |
> |
{ |
288 |
> |
CFException ex; |
289 |
> |
CompletableFuture<Integer> f; |
290 |
> |
|
291 |
> |
f = new CompletableFuture<>(); |
292 |
> |
f.complete(v1); |
293 |
> |
for (int i = 0; i < 2; i++) { |
294 |
> |
f.obtrudeException(ex = new CFException()); |
295 |
> |
checkCompletedExceptionally(f, ex); |
296 |
> |
} |
297 |
> |
|
298 |
> |
f = new CompletableFuture<>(); |
299 |
> |
for (int i = 0; i < 2; i++) { |
300 |
> |
f.obtrudeException(ex = new CFException()); |
301 |
> |
checkCompletedExceptionally(f, ex); |
302 |
> |
} |
303 |
> |
|
304 |
> |
f = new CompletableFuture<>(); |
305 |
> |
f.completeExceptionally(ex = new CFException()); |
306 |
> |
f.obtrudeValue(v1); |
307 |
> |
checkCompletedNormally(f, v1); |
308 |
> |
f.obtrudeException(ex = new CFException()); |
309 |
> |
checkCompletedExceptionally(f, ex); |
310 |
> |
f.completeExceptionally(new CFException()); |
311 |
> |
checkCompletedExceptionally(f, ex); |
312 |
> |
f.complete(v1); |
313 |
> |
checkCompletedExceptionally(f, ex); |
314 |
> |
}} |
315 |
|
|
316 |
|
/** |
317 |
|
* getNumberOfDependents returns number of dependent tasks |
318 |
|
*/ |
319 |
|
public void testGetNumberOfDependents() { |
320 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
321 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
322 |
< |
CompletableFuture g = f.thenRun(new Noop()); |
323 |
< |
assertEquals(f.getNumberOfDependents(), 1); |
324 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
325 |
< |
CompletableFuture h = f.thenRun(new Noop()); |
326 |
< |
assertEquals(f.getNumberOfDependents(), 2); |
320 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
321 |
> |
{ |
322 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
323 |
> |
assertEquals(0, f.getNumberOfDependents()); |
324 |
> |
final CompletableFuture<Void> g = m.thenRun(f, new Noop(m)); |
325 |
> |
assertEquals(1, f.getNumberOfDependents()); |
326 |
> |
assertEquals(0, g.getNumberOfDependents()); |
327 |
> |
final CompletableFuture<Void> h = m.thenRun(f, new Noop(m)); |
328 |
> |
assertEquals(2, f.getNumberOfDependents()); |
329 |
> |
assertEquals(0, h.getNumberOfDependents()); |
330 |
|
f.complete(1); |
331 |
|
checkCompletedNormally(g, null); |
332 |
< |
assertEquals(f.getNumberOfDependents(), 0); |
333 |
< |
assertEquals(g.getNumberOfDependents(), 0); |
334 |
< |
} |
335 |
< |
|
332 |
> |
checkCompletedNormally(h, null); |
333 |
> |
assertEquals(0, f.getNumberOfDependents()); |
334 |
> |
assertEquals(0, g.getNumberOfDependents()); |
335 |
> |
assertEquals(0, h.getNumberOfDependents()); |
336 |
> |
}} |
337 |
|
|
338 |
|
/** |
339 |
|
* toString indicates current completion state |
350 |
|
f = new CompletableFuture<String>(); |
351 |
|
f.completeExceptionally(new IndexOutOfBoundsException()); |
352 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
353 |
+ |
|
354 |
+ |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
355 |
+ |
f = new CompletableFuture<String>(); |
356 |
+ |
f.cancel(mayInterruptIfRunning); |
357 |
+ |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
358 |
+ |
} |
359 |
|
} |
360 |
|
|
361 |
|
/** |
366 |
|
checkCompletedNormally(f, "test"); |
367 |
|
} |
368 |
|
|
369 |
< |
static final Supplier<Integer> supplyOne = |
370 |
< |
() -> Integer.valueOf(1); |
371 |
< |
static final Function<Integer, Integer> inc = |
372 |
< |
(Integer x) -> Integer.valueOf(x.intValue() + 1); |
373 |
< |
static final BiFunction<Integer, Integer, Integer> add = |
374 |
< |
(Integer x, Integer y) -> Integer.valueOf(x.intValue() + y.intValue()); |
375 |
< |
static final class IncAction implements Consumer<Integer> { |
289 |
< |
int value; |
290 |
< |
public void accept(Integer x) { value = x.intValue() + 1; } |
291 |
< |
} |
292 |
< |
static final class AddAction implements BiConsumer<Integer, Integer> { |
293 |
< |
int value; |
294 |
< |
public void accept(Integer x, Integer y) { |
295 |
< |
value = x.intValue() + y.intValue(); |
369 |
> |
abstract class CheckedAction { |
370 |
> |
int invocationCount = 0; |
371 |
> |
final ExecutionMode m; |
372 |
> |
CheckedAction(ExecutionMode m) { this.m = m; } |
373 |
> |
void invoked() { |
374 |
> |
m.checkExecutionMode(); |
375 |
> |
assertEquals(0, invocationCount++); |
376 |
|
} |
377 |
< |
} |
378 |
< |
static final class Noop implements Runnable { |
299 |
< |
boolean ran; |
300 |
< |
public void run() { ran = true; } |
301 |
< |
} |
302 |
< |
|
303 |
< |
static final class FailingSupplier implements Supplier<Integer> { |
304 |
< |
boolean ran; |
305 |
< |
public Integer get() { ran = true; throw new CFException(); } |
306 |
< |
} |
307 |
< |
static final class FailingConsumer implements Consumer<Integer> { |
308 |
< |
boolean ran; |
309 |
< |
public void accept(Integer x) { ran = true; throw new CFException(); } |
310 |
< |
} |
311 |
< |
static final class FailingBiConsumer implements BiConsumer<Integer, Integer> { |
312 |
< |
boolean ran; |
313 |
< |
public void accept(Integer x, Integer y) { ran = true; throw new CFException(); } |
314 |
< |
} |
315 |
< |
static final class FailingFunction implements Function<Integer, Integer> { |
316 |
< |
boolean ran; |
317 |
< |
public Integer apply(Integer x) { ran = true; throw new CFException(); } |
318 |
< |
} |
319 |
< |
static final class FailingBiFunction implements BiFunction<Integer, Integer, Integer> { |
320 |
< |
boolean ran; |
321 |
< |
public Integer apply(Integer x, Integer y) { ran = true; throw new CFException(); } |
322 |
< |
} |
323 |
< |
static final class FailingNoop implements Runnable { |
324 |
< |
boolean ran; |
325 |
< |
public void run() { ran = true; throw new CFException(); } |
377 |
> |
void assertNotInvoked() { assertEquals(0, invocationCount); } |
378 |
> |
void assertInvoked() { assertEquals(1, invocationCount); } |
379 |
|
} |
380 |
|
|
381 |
< |
static final class CompletableFutureInc implements Function<Integer, CompletableFuture<Integer>> { |
382 |
< |
public CompletableFuture<Integer> apply(Integer x) { |
383 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
384 |
< |
f.complete(Integer.valueOf(x.intValue() + 1)); |
385 |
< |
return f; |
381 |
> |
abstract class CheckedIntegerAction extends CheckedAction { |
382 |
> |
Integer value; |
383 |
> |
CheckedIntegerAction(ExecutionMode m) { super(m); } |
384 |
> |
void assertValue(Integer expected) { |
385 |
> |
assertInvoked(); |
386 |
> |
assertEquals(expected, value); |
387 |
|
} |
388 |
|
} |
389 |
|
|
390 |
< |
static final class FailingCompletableFutureFunction implements Function<Integer, CompletableFuture<Integer>> { |
391 |
< |
boolean ran; |
392 |
< |
public CompletableFuture<Integer> apply(Integer x) { |
393 |
< |
ran = true; throw new CFException(); |
390 |
> |
class IntegerSupplier extends CheckedAction |
391 |
> |
implements Supplier<Integer> |
392 |
> |
{ |
393 |
> |
final Integer value; |
394 |
> |
IntegerSupplier(ExecutionMode m, Integer value) { |
395 |
> |
super(m); |
396 |
> |
this.value = value; |
397 |
|
} |
398 |
< |
} |
399 |
< |
|
400 |
< |
// Used for explicit executor tests |
344 |
< |
static final class ThreadExecutor implements Executor { |
345 |
< |
public void execute(Runnable r) { |
346 |
< |
new Thread(r).start(); |
398 |
> |
public Integer get() { |
399 |
> |
invoked(); |
400 |
> |
return value; |
401 |
|
} |
402 |
|
} |
403 |
|
|
404 |
< |
static final class ExceptionToInteger implements Function<Throwable, Integer> { |
405 |
< |
public Integer apply(Throwable x) { return Integer.valueOf(3); } |
404 |
> |
// A function that handles and produces null values as well. |
405 |
> |
static Integer inc(Integer x) { |
406 |
> |
return (x == null) ? null : x + 1; |
407 |
|
} |
408 |
|
|
409 |
< |
static final class IntegerHandler implements BiFunction<Integer, Throwable, Integer> { |
410 |
< |
public Integer apply(Integer x, Throwable t) { |
411 |
< |
return (t == null) ? two : three; |
409 |
> |
class NoopConsumer extends CheckedIntegerAction |
410 |
> |
implements Consumer<Integer> |
411 |
> |
{ |
412 |
> |
NoopConsumer(ExecutionMode m) { super(m); } |
413 |
> |
public void accept(Integer x) { |
414 |
> |
invoked(); |
415 |
> |
value = x; |
416 |
|
} |
417 |
|
} |
418 |
|
|
419 |
< |
|
420 |
< |
/** |
421 |
< |
* exceptionally action completes with function value on source |
422 |
< |
* exception; otherwise with source value |
423 |
< |
*/ |
424 |
< |
public void testExceptionally() { |
425 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
426 |
< |
ExceptionToInteger r = new ExceptionToInteger(); |
368 |
< |
CompletableFuture<Integer> g = f.exceptionally(r); |
369 |
< |
f.completeExceptionally(new CFException()); |
370 |
< |
checkCompletedNormally(g, three); |
371 |
< |
|
372 |
< |
f = new CompletableFuture<Integer>(); |
373 |
< |
r = new ExceptionToInteger(); |
374 |
< |
g = f.exceptionally(r); |
375 |
< |
f.complete(one); |
376 |
< |
checkCompletedNormally(g, one); |
377 |
< |
} |
378 |
< |
|
379 |
< |
/** |
380 |
< |
* handle action completes normally with function value on either |
381 |
< |
* normal or exceptional completion of source |
382 |
< |
*/ |
383 |
< |
public void testHandle() { |
384 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
385 |
< |
IntegerHandler r = new IntegerHandler(); |
386 |
< |
CompletableFuture<Integer> g = f.handle(r); |
387 |
< |
f.completeExceptionally(new CFException()); |
388 |
< |
checkCompletedNormally(g, three); |
389 |
< |
|
390 |
< |
f = new CompletableFuture<Integer>(); |
391 |
< |
r = new IntegerHandler(); |
392 |
< |
g = f.handle(r); |
393 |
< |
f.complete(one); |
394 |
< |
checkCompletedNormally(g, two); |
395 |
< |
} |
396 |
< |
|
397 |
< |
/** |
398 |
< |
* runAsync completes after running Runnable |
399 |
< |
*/ |
400 |
< |
public void testRunAsync() { |
401 |
< |
Noop r = new Noop(); |
402 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
403 |
< |
assertNull(f.join()); |
404 |
< |
assertTrue(r.ran); |
405 |
< |
} |
406 |
< |
|
407 |
< |
/** |
408 |
< |
* runAsync with executor completes after running Runnable |
409 |
< |
*/ |
410 |
< |
public void testRunAsync2() { |
411 |
< |
Noop r = new Noop(); |
412 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r, new ThreadExecutor()); |
413 |
< |
assertNull(f.join()); |
414 |
< |
assertTrue(r.ran); |
415 |
< |
} |
416 |
< |
|
417 |
< |
/** |
418 |
< |
* failing runAsync completes exceptionally after running Runnable |
419 |
< |
*/ |
420 |
< |
public void testRunAsync3() { |
421 |
< |
FailingNoop r = new FailingNoop(); |
422 |
< |
CompletableFuture<Void> f = CompletableFuture.runAsync(r); |
423 |
< |
checkCompletedWithWrappedCFException(f); |
424 |
< |
assertTrue(r.ran); |
425 |
< |
} |
426 |
< |
|
427 |
< |
/** |
428 |
< |
* supplyAsync completes with result of supplier |
429 |
< |
*/ |
430 |
< |
public void testSupplyAsync() { |
431 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne); |
432 |
< |
assertEquals(f.join(), one); |
433 |
< |
} |
434 |
< |
|
435 |
< |
/** |
436 |
< |
* supplyAsync with executor completes with result of supplier |
437 |
< |
*/ |
438 |
< |
public void testSupplyAsync2() { |
439 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(supplyOne, new ThreadExecutor()); |
440 |
< |
assertEquals(f.join(), one); |
441 |
< |
} |
442 |
< |
|
443 |
< |
/** |
444 |
< |
* Failing supplyAsync completes exceptionally |
445 |
< |
*/ |
446 |
< |
public void testSupplyAsync3() { |
447 |
< |
FailingSupplier r = new FailingSupplier(); |
448 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(r); |
449 |
< |
checkCompletedWithWrappedCFException(f); |
450 |
< |
assertTrue(r.ran); |
451 |
< |
} |
452 |
< |
|
453 |
< |
// seq completion methods |
454 |
< |
|
455 |
< |
/** |
456 |
< |
* thenRun result completes normally after normal completion of source |
457 |
< |
*/ |
458 |
< |
public void testThenRun() { |
459 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
460 |
< |
Noop r = new Noop(); |
461 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
462 |
< |
f.complete(null); |
463 |
< |
checkCompletedNormally(g, null); |
464 |
< |
// reordered version |
465 |
< |
f = new CompletableFuture<Integer>(); |
466 |
< |
f.complete(null); |
467 |
< |
r = new Noop(); |
468 |
< |
g = f.thenRun(r); |
469 |
< |
checkCompletedNormally(g, null); |
470 |
< |
} |
471 |
< |
|
472 |
< |
/** |
473 |
< |
* thenRun result completes exceptionally after exceptional |
474 |
< |
* completion of source |
475 |
< |
*/ |
476 |
< |
public void testThenRun2() { |
477 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
478 |
< |
Noop r = new Noop(); |
479 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
480 |
< |
f.completeExceptionally(new CFException()); |
481 |
< |
checkCompletedWithWrappedCFException(g); |
482 |
< |
} |
483 |
< |
|
484 |
< |
/** |
485 |
< |
* thenRun result completes exceptionally if action does |
486 |
< |
*/ |
487 |
< |
public void testThenRun3() { |
488 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
489 |
< |
FailingNoop r = new FailingNoop(); |
490 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
491 |
< |
f.complete(null); |
492 |
< |
checkCompletedWithWrappedCFException(g); |
493 |
< |
} |
494 |
< |
|
495 |
< |
/** |
496 |
< |
* thenRun result completes exceptionally if source cancelled |
497 |
< |
*/ |
498 |
< |
public void testThenRun4() { |
499 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
500 |
< |
Noop r = new Noop(); |
501 |
< |
CompletableFuture<Void> g = f.thenRun(r); |
502 |
< |
assertTrue(f.cancel(true)); |
503 |
< |
checkCompletedWithWrappedCancellationException(g); |
504 |
< |
} |
505 |
< |
|
506 |
< |
/** |
507 |
< |
* thenApply result completes normally after normal completion of source |
508 |
< |
*/ |
509 |
< |
public void testThenApply() { |
510 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
511 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
512 |
< |
f.complete(one); |
513 |
< |
checkCompletedNormally(g, two); |
514 |
< |
} |
515 |
< |
|
516 |
< |
/** |
517 |
< |
* thenApply result completes exceptionally after exceptional |
518 |
< |
* completion of source |
519 |
< |
*/ |
520 |
< |
public void testThenApply2() { |
521 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
522 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
523 |
< |
f.completeExceptionally(new CFException()); |
524 |
< |
checkCompletedWithWrappedCFException(g); |
419 |
> |
class IncFunction extends CheckedIntegerAction |
420 |
> |
implements Function<Integer,Integer> |
421 |
> |
{ |
422 |
> |
IncFunction(ExecutionMode m) { super(m); } |
423 |
> |
public Integer apply(Integer x) { |
424 |
> |
invoked(); |
425 |
> |
return value = inc(x); |
426 |
> |
} |
427 |
|
} |
428 |
|
|
429 |
< |
/** |
430 |
< |
* thenApply result completes exceptionally if action does |
431 |
< |
*/ |
432 |
< |
public void testThenApply3() { |
433 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
434 |
< |
CompletableFuture<Integer> g = f.thenApply(new FailingFunction()); |
533 |
< |
f.complete(one); |
534 |
< |
checkCompletedWithWrappedCFException(g); |
429 |
> |
// Choose non-commutative actions for better coverage |
430 |
> |
// A non-commutative function that handles and produces null values as well. |
431 |
> |
static Integer subtract(Integer x, Integer y) { |
432 |
> |
return (x == null && y == null) ? null : |
433 |
> |
((x == null) ? 42 : x.intValue()) |
434 |
> |
- ((y == null) ? 99 : y.intValue()); |
435 |
|
} |
436 |
|
|
437 |
< |
/** |
438 |
< |
* thenApply result completes exceptionally if source cancelled |
439 |
< |
*/ |
440 |
< |
public void testThenApply4() { |
441 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
442 |
< |
CompletableFuture<Integer> g = f.thenApply(inc); |
443 |
< |
assertTrue(f.cancel(true)); |
444 |
< |
checkCompletedWithWrappedCancellationException(g); |
437 |
> |
class SubtractAction extends CheckedIntegerAction |
438 |
> |
implements BiConsumer<Integer, Integer> |
439 |
> |
{ |
440 |
> |
SubtractAction(ExecutionMode m) { super(m); } |
441 |
> |
public void accept(Integer x, Integer y) { |
442 |
> |
invoked(); |
443 |
> |
value = subtract(x, y); |
444 |
> |
} |
445 |
|
} |
446 |
|
|
447 |
< |
/** |
448 |
< |
* thenAccept result completes normally after normal completion of source |
449 |
< |
*/ |
450 |
< |
public void testThenAccept() { |
451 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
452 |
< |
IncAction r = new IncAction(); |
453 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
454 |
< |
f.complete(one); |
555 |
< |
checkCompletedNormally(g, null); |
556 |
< |
assertEquals(r.value, 2); |
447 |
> |
class SubtractFunction extends CheckedIntegerAction |
448 |
> |
implements BiFunction<Integer, Integer, Integer> |
449 |
> |
{ |
450 |
> |
SubtractFunction(ExecutionMode m) { super(m); } |
451 |
> |
public Integer apply(Integer x, Integer y) { |
452 |
> |
invoked(); |
453 |
> |
return value = subtract(x, y); |
454 |
> |
} |
455 |
|
} |
456 |
|
|
457 |
< |
/** |
458 |
< |
* thenAccept result completes exceptionally after exceptional |
459 |
< |
* completion of source |
460 |
< |
*/ |
461 |
< |
public void testThenAccept2() { |
564 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
565 |
< |
IncAction r = new IncAction(); |
566 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
567 |
< |
f.completeExceptionally(new CFException()); |
568 |
< |
checkCompletedWithWrappedCFException(g); |
457 |
> |
class Noop extends CheckedAction implements Runnable { |
458 |
> |
Noop(ExecutionMode m) { super(m); } |
459 |
> |
public void run() { |
460 |
> |
invoked(); |
461 |
> |
} |
462 |
|
} |
463 |
|
|
464 |
< |
/** |
465 |
< |
* thenAccept result completes exceptionally if action does |
466 |
< |
*/ |
467 |
< |
public void testThenAccept3() { |
468 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
469 |
< |
FailingConsumer r = new FailingConsumer(); |
470 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
471 |
< |
f.complete(one); |
579 |
< |
checkCompletedWithWrappedCFException(g); |
580 |
< |
assertTrue(r.ran); |
464 |
> |
class FailingSupplier extends CheckedAction |
465 |
> |
implements Supplier<Integer> |
466 |
> |
{ |
467 |
> |
FailingSupplier(ExecutionMode m) { super(m); } |
468 |
> |
public Integer get() { |
469 |
> |
invoked(); |
470 |
> |
throw new CFException(); |
471 |
> |
} |
472 |
|
} |
473 |
|
|
474 |
< |
/** |
475 |
< |
* thenAccept result completes exceptionally if source cancelled |
476 |
< |
*/ |
477 |
< |
public void testThenAccept4() { |
478 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
479 |
< |
IncAction r = new IncAction(); |
480 |
< |
CompletableFuture<Void> g = f.thenAccept(r); |
481 |
< |
assertTrue(f.cancel(true)); |
482 |
< |
checkCompletedWithWrappedCancellationException(g); |
474 |
> |
class FailingConsumer extends CheckedIntegerAction |
475 |
> |
implements Consumer<Integer> |
476 |
> |
{ |
477 |
> |
FailingConsumer(ExecutionMode m) { super(m); } |
478 |
> |
public void accept(Integer x) { |
479 |
> |
invoked(); |
480 |
> |
value = x; |
481 |
> |
throw new CFException(); |
482 |
> |
} |
483 |
|
} |
484 |
|
|
485 |
< |
|
486 |
< |
/** |
487 |
< |
* thenCombine result completes normally after normal completion of sources |
488 |
< |
*/ |
489 |
< |
public void testThenCombine() { |
490 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
491 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
492 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
493 |
< |
f.complete(one); |
603 |
< |
checkIncomplete(g); |
604 |
< |
f2.complete(two); |
605 |
< |
checkCompletedNormally(g, three); |
606 |
< |
|
607 |
< |
f = new CompletableFuture<Integer>(); |
608 |
< |
f.complete(one); |
609 |
< |
f2 = new CompletableFuture<Integer>(); |
610 |
< |
g = f.thenCombine(f2, add); |
611 |
< |
checkIncomplete(g); |
612 |
< |
f2.complete(two); |
613 |
< |
checkCompletedNormally(g, three); |
485 |
> |
class FailingBiConsumer extends CheckedIntegerAction |
486 |
> |
implements BiConsumer<Integer, Integer> |
487 |
> |
{ |
488 |
> |
FailingBiConsumer(ExecutionMode m) { super(m); } |
489 |
> |
public void accept(Integer x, Integer y) { |
490 |
> |
invoked(); |
491 |
> |
value = subtract(x, y); |
492 |
> |
throw new CFException(); |
493 |
> |
} |
494 |
|
} |
495 |
|
|
496 |
< |
/** |
497 |
< |
* thenCombine result completes exceptionally after exceptional |
498 |
< |
* completion of either source |
499 |
< |
*/ |
500 |
< |
public void testThenCombine2() { |
501 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
502 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
503 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
504 |
< |
f.completeExceptionally(new CFException()); |
625 |
< |
f2.complete(two); |
626 |
< |
checkCompletedWithWrappedCFException(g); |
627 |
< |
|
628 |
< |
f = new CompletableFuture<Integer>(); |
629 |
< |
f.complete(one); |
630 |
< |
f2 = new CompletableFuture<Integer>(); |
631 |
< |
g = f.thenCombine(f2, add); |
632 |
< |
f2.completeExceptionally(new CFException()); |
633 |
< |
checkCompletedWithWrappedCFException(g); |
496 |
> |
class FailingFunction extends CheckedIntegerAction |
497 |
> |
implements Function<Integer, Integer> |
498 |
> |
{ |
499 |
> |
FailingFunction(ExecutionMode m) { super(m); } |
500 |
> |
public Integer apply(Integer x) { |
501 |
> |
invoked(); |
502 |
> |
value = x; |
503 |
> |
throw new CFException(); |
504 |
> |
} |
505 |
|
} |
506 |
|
|
507 |
< |
/** |
508 |
< |
* thenCombine result completes exceptionally if action does |
509 |
< |
*/ |
510 |
< |
public void testThenCombine3() { |
511 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
512 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
513 |
< |
FailingBiFunction r = new FailingBiFunction(); |
514 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, r); |
515 |
< |
f.complete(one); |
645 |
< |
checkIncomplete(g); |
646 |
< |
f2.complete(two); |
647 |
< |
checkCompletedWithWrappedCFException(g); |
507 |
> |
class FailingBiFunction extends CheckedIntegerAction |
508 |
> |
implements BiFunction<Integer, Integer, Integer> |
509 |
> |
{ |
510 |
> |
FailingBiFunction(ExecutionMode m) { super(m); } |
511 |
> |
public Integer apply(Integer x, Integer y) { |
512 |
> |
invoked(); |
513 |
> |
value = subtract(x, y); |
514 |
> |
throw new CFException(); |
515 |
> |
} |
516 |
|
} |
517 |
|
|
518 |
< |
/** |
519 |
< |
* thenCombine result completes exceptionally if either source cancelled |
520 |
< |
*/ |
521 |
< |
public void testThenCombine4() { |
522 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
523 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
656 |
< |
CompletableFuture<Integer> g = f.thenCombine(f2, add); |
657 |
< |
assertTrue(f.cancel(true)); |
658 |
< |
f2.complete(two); |
659 |
< |
checkCompletedWithWrappedCancellationException(g); |
660 |
< |
f = new CompletableFuture<Integer>(); |
661 |
< |
f2 = new CompletableFuture<Integer>(); |
662 |
< |
g = f.thenCombine(f2, add); |
663 |
< |
f.complete(one); |
664 |
< |
assertTrue(f2.cancel(true)); |
665 |
< |
checkCompletedWithWrappedCancellationException(g); |
518 |
> |
class FailingRunnable extends CheckedAction implements Runnable { |
519 |
> |
FailingRunnable(ExecutionMode m) { super(m); } |
520 |
> |
public void run() { |
521 |
> |
invoked(); |
522 |
> |
throw new CFException(); |
523 |
> |
} |
524 |
|
} |
525 |
|
|
668 |
– |
/** |
669 |
– |
* thenAcceptBoth result completes normally after normal |
670 |
– |
* completion of sources |
671 |
– |
*/ |
672 |
– |
public void testThenAcceptBoth() { |
673 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
674 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
675 |
– |
AddAction r = new AddAction(); |
676 |
– |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
677 |
– |
f.complete(one); |
678 |
– |
checkIncomplete(g); |
679 |
– |
f2.complete(two); |
680 |
– |
checkCompletedNormally(g, null); |
681 |
– |
assertEquals(r.value, 3); |
526 |
|
|
527 |
< |
r = new AddAction(); |
528 |
< |
f = new CompletableFuture<Integer>(); |
529 |
< |
f.complete(one); |
530 |
< |
f2 = new CompletableFuture<Integer>(); |
531 |
< |
g = f.thenAcceptBoth(f2, r); |
532 |
< |
checkIncomplete(g); |
533 |
< |
f2.complete(two); |
534 |
< |
checkCompletedNormally(g, null); |
535 |
< |
assertEquals(r.value, 3); |
527 |
> |
class CompletableFutureInc extends CheckedIntegerAction |
528 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
529 |
> |
{ |
530 |
> |
CompletableFutureInc(ExecutionMode m) { super(m); } |
531 |
> |
public CompletableFuture<Integer> apply(Integer x) { |
532 |
> |
invoked(); |
533 |
> |
value = x; |
534 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
535 |
> |
f.complete(inc(x)); |
536 |
> |
return f; |
537 |
> |
} |
538 |
|
} |
539 |
|
|
540 |
< |
/** |
541 |
< |
* thenAcceptBoth result completes exceptionally after exceptional |
542 |
< |
* completion of either source |
543 |
< |
*/ |
544 |
< |
public void testThenAcceptBoth2() { |
545 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
546 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
547 |
< |
AddAction r = new AddAction(); |
548 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
703 |
< |
f.completeExceptionally(new CFException()); |
704 |
< |
f2.complete(two); |
705 |
< |
checkCompletedWithWrappedCFException(g); |
706 |
< |
|
707 |
< |
r = new AddAction(); |
708 |
< |
f = new CompletableFuture<Integer>(); |
709 |
< |
f.complete(one); |
710 |
< |
f2 = new CompletableFuture<Integer>(); |
711 |
< |
g = f.thenAcceptBoth(f2, r); |
712 |
< |
f2.completeExceptionally(new CFException()); |
713 |
< |
checkCompletedWithWrappedCFException(g); |
540 |
> |
class FailingCompletableFutureFunction extends CheckedIntegerAction |
541 |
> |
implements Function<Integer, CompletableFuture<Integer>> |
542 |
> |
{ |
543 |
> |
FailingCompletableFutureFunction(ExecutionMode m) { super(m); } |
544 |
> |
public CompletableFuture<Integer> apply(Integer x) { |
545 |
> |
invoked(); |
546 |
> |
value = x; |
547 |
> |
throw new CFException(); |
548 |
> |
} |
549 |
|
} |
550 |
|
|
551 |
< |
/** |
552 |
< |
* thenAcceptBoth result completes exceptionally if action does |
553 |
< |
*/ |
554 |
< |
public void testThenAcceptBoth3() { |
555 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
556 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
557 |
< |
FailingBiConsumer r = new FailingBiConsumer(); |
723 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
724 |
< |
f.complete(one); |
725 |
< |
checkIncomplete(g); |
726 |
< |
f2.complete(two); |
727 |
< |
checkCompletedWithWrappedCFException(g); |
728 |
< |
} |
551 |
> |
// Used for explicit executor tests |
552 |
> |
static final class ThreadExecutor implements Executor { |
553 |
> |
final AtomicInteger count = new AtomicInteger(0); |
554 |
> |
static final ThreadGroup tg = new ThreadGroup("ThreadExecutor"); |
555 |
> |
static boolean startedCurrentThread() { |
556 |
> |
return Thread.currentThread().getThreadGroup() == tg; |
557 |
> |
} |
558 |
|
|
559 |
< |
/** |
560 |
< |
* thenAcceptBoth result completes exceptionally if either source cancelled |
561 |
< |
*/ |
562 |
< |
public void testThenAcceptBoth4() { |
734 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
735 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
736 |
< |
AddAction r = new AddAction(); |
737 |
< |
CompletableFuture<Void> g = f.thenAcceptBoth(f2, r); |
738 |
< |
assertTrue(f.cancel(true)); |
739 |
< |
f2.complete(two); |
740 |
< |
checkCompletedWithWrappedCancellationException(g); |
741 |
< |
f = new CompletableFuture<Integer>(); |
742 |
< |
f2 = new CompletableFuture<Integer>(); |
743 |
< |
r = new AddAction(); |
744 |
< |
g = f.thenAcceptBoth(f2, r); |
745 |
< |
f.complete(one); |
746 |
< |
assertTrue(f2.cancel(true)); |
747 |
< |
checkCompletedWithWrappedCancellationException(g); |
559 |
> |
public void execute(Runnable r) { |
560 |
> |
count.getAndIncrement(); |
561 |
> |
new Thread(tg, r).start(); |
562 |
> |
} |
563 |
|
} |
564 |
|
|
565 |
|
/** |
566 |
< |
* runAfterBoth result completes normally after normal |
567 |
< |
* completion of sources |
566 |
> |
* Permits the testing of parallel code for the 3 different |
567 |
> |
* execution modes without copy/pasting all the test methods. |
568 |
|
*/ |
569 |
< |
public void testRunAfterBoth() { |
570 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
571 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
572 |
< |
Noop r = new Noop(); |
573 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
574 |
< |
f.complete(one); |
575 |
< |
checkIncomplete(g); |
576 |
< |
f2.complete(two); |
577 |
< |
checkCompletedNormally(g, null); |
578 |
< |
assertTrue(r.ran); |
569 |
> |
enum ExecutionMode { |
570 |
> |
DEFAULT { |
571 |
> |
public void checkExecutionMode() { |
572 |
> |
assertFalse(ThreadExecutor.startedCurrentThread()); |
573 |
> |
assertNull(ForkJoinTask.getPool()); |
574 |
> |
} |
575 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
576 |
> |
throw new UnsupportedOperationException(); |
577 |
> |
} |
578 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
579 |
> |
throw new UnsupportedOperationException(); |
580 |
> |
} |
581 |
> |
public <T> CompletableFuture<Void> thenRun |
582 |
> |
(CompletableFuture<T> f, Runnable a) { |
583 |
> |
return f.thenRun(a); |
584 |
> |
} |
585 |
> |
public <T> CompletableFuture<Void> thenAccept |
586 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
587 |
> |
return f.thenAccept(a); |
588 |
> |
} |
589 |
> |
public <T,U> CompletableFuture<U> thenApply |
590 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
591 |
> |
return f.thenApply(a); |
592 |
> |
} |
593 |
> |
public <T,U> CompletableFuture<U> thenCompose |
594 |
> |
(CompletableFuture<T> f, |
595 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
596 |
> |
return f.thenCompose(a); |
597 |
> |
} |
598 |
> |
public <T,U> CompletableFuture<U> handle |
599 |
> |
(CompletableFuture<T> f, |
600 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
601 |
> |
return f.handle(a); |
602 |
> |
} |
603 |
> |
public <T> CompletableFuture<T> whenComplete |
604 |
> |
(CompletableFuture<T> f, |
605 |
> |
BiConsumer<? super T,? super Throwable> a) { |
606 |
> |
return f.whenComplete(a); |
607 |
> |
} |
608 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
609 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
610 |
> |
return f.runAfterBoth(g, a); |
611 |
> |
} |
612 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
613 |
> |
(CompletableFuture<T> f, |
614 |
> |
CompletionStage<? extends U> g, |
615 |
> |
BiConsumer<? super T,? super U> a) { |
616 |
> |
return f.thenAcceptBoth(g, a); |
617 |
> |
} |
618 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
619 |
> |
(CompletableFuture<T> f, |
620 |
> |
CompletionStage<? extends U> g, |
621 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
622 |
> |
return f.thenCombine(g, a); |
623 |
> |
} |
624 |
> |
public <T> CompletableFuture<Void> runAfterEither |
625 |
> |
(CompletableFuture<T> f, |
626 |
> |
CompletionStage<?> g, |
627 |
> |
java.lang.Runnable a) { |
628 |
> |
return f.runAfterEither(g, a); |
629 |
> |
} |
630 |
> |
public <T> CompletableFuture<Void> acceptEither |
631 |
> |
(CompletableFuture<T> f, |
632 |
> |
CompletionStage<? extends T> g, |
633 |
> |
Consumer<? super T> a) { |
634 |
> |
return f.acceptEither(g, a); |
635 |
> |
} |
636 |
> |
public <T,U> CompletableFuture<U> applyToEither |
637 |
> |
(CompletableFuture<T> f, |
638 |
> |
CompletionStage<? extends T> g, |
639 |
> |
Function<? super T,U> a) { |
640 |
> |
return f.applyToEither(g, a); |
641 |
> |
} |
642 |
> |
}, |
643 |
|
|
644 |
< |
r = new Noop(); |
645 |
< |
f = new CompletableFuture<Integer>(); |
646 |
< |
f.complete(one); |
647 |
< |
f2 = new CompletableFuture<Integer>(); |
648 |
< |
g = f.runAfterBoth(f2, r); |
649 |
< |
checkIncomplete(g); |
650 |
< |
f2.complete(two); |
651 |
< |
checkCompletedNormally(g, null); |
652 |
< |
assertTrue(r.ran); |
653 |
< |
} |
644 |
> |
ASYNC { |
645 |
> |
public void checkExecutionMode() { |
646 |
> |
assertSame(ForkJoinPool.commonPool(), |
647 |
> |
ForkJoinTask.getPool()); |
648 |
> |
} |
649 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
650 |
> |
return CompletableFuture.runAsync(a); |
651 |
> |
} |
652 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
653 |
> |
return CompletableFuture.supplyAsync(a); |
654 |
> |
} |
655 |
> |
public <T> CompletableFuture<Void> thenRun |
656 |
> |
(CompletableFuture<T> f, Runnable a) { |
657 |
> |
return f.thenRunAsync(a); |
658 |
> |
} |
659 |
> |
public <T> CompletableFuture<Void> thenAccept |
660 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
661 |
> |
return f.thenAcceptAsync(a); |
662 |
> |
} |
663 |
> |
public <T,U> CompletableFuture<U> thenApply |
664 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
665 |
> |
return f.thenApplyAsync(a); |
666 |
> |
} |
667 |
> |
public <T,U> CompletableFuture<U> thenCompose |
668 |
> |
(CompletableFuture<T> f, |
669 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
670 |
> |
return f.thenComposeAsync(a); |
671 |
> |
} |
672 |
> |
public <T,U> CompletableFuture<U> handle |
673 |
> |
(CompletableFuture<T> f, |
674 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
675 |
> |
return f.handleAsync(a); |
676 |
> |
} |
677 |
> |
public <T> CompletableFuture<T> whenComplete |
678 |
> |
(CompletableFuture<T> f, |
679 |
> |
BiConsumer<? super T,? super Throwable> a) { |
680 |
> |
return f.whenCompleteAsync(a); |
681 |
> |
} |
682 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
683 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
684 |
> |
return f.runAfterBothAsync(g, a); |
685 |
> |
} |
686 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
687 |
> |
(CompletableFuture<T> f, |
688 |
> |
CompletionStage<? extends U> g, |
689 |
> |
BiConsumer<? super T,? super U> a) { |
690 |
> |
return f.thenAcceptBothAsync(g, a); |
691 |
> |
} |
692 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
693 |
> |
(CompletableFuture<T> f, |
694 |
> |
CompletionStage<? extends U> g, |
695 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
696 |
> |
return f.thenCombineAsync(g, a); |
697 |
> |
} |
698 |
> |
public <T> CompletableFuture<Void> runAfterEither |
699 |
> |
(CompletableFuture<T> f, |
700 |
> |
CompletionStage<?> g, |
701 |
> |
java.lang.Runnable a) { |
702 |
> |
return f.runAfterEitherAsync(g, a); |
703 |
> |
} |
704 |
> |
public <T> CompletableFuture<Void> acceptEither |
705 |
> |
(CompletableFuture<T> f, |
706 |
> |
CompletionStage<? extends T> g, |
707 |
> |
Consumer<? super T> a) { |
708 |
> |
return f.acceptEitherAsync(g, a); |
709 |
> |
} |
710 |
> |
public <T,U> CompletableFuture<U> applyToEither |
711 |
> |
(CompletableFuture<T> f, |
712 |
> |
CompletionStage<? extends T> g, |
713 |
> |
Function<? super T,U> a) { |
714 |
> |
return f.applyToEitherAsync(g, a); |
715 |
> |
} |
716 |
> |
}, |
717 |
|
|
718 |
< |
/** |
719 |
< |
* runAfterBoth result completes exceptionally after exceptional |
720 |
< |
* completion of either source |
721 |
< |
*/ |
722 |
< |
public void testRunAfterBoth2() { |
723 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
724 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
725 |
< |
Noop r = new Noop(); |
726 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
727 |
< |
f.completeExceptionally(new CFException()); |
728 |
< |
f2.complete(two); |
729 |
< |
checkCompletedWithWrappedCFException(g); |
718 |
> |
EXECUTOR { |
719 |
> |
public void checkExecutionMode() { |
720 |
> |
assertTrue(ThreadExecutor.startedCurrentThread()); |
721 |
> |
} |
722 |
> |
public CompletableFuture<Void> runAsync(Runnable a) { |
723 |
> |
return CompletableFuture.runAsync(a, new ThreadExecutor()); |
724 |
> |
} |
725 |
> |
public <U> CompletableFuture<U> supplyAsync(Supplier<U> a) { |
726 |
> |
return CompletableFuture.supplyAsync(a, new ThreadExecutor()); |
727 |
> |
} |
728 |
> |
public <T> CompletableFuture<Void> thenRun |
729 |
> |
(CompletableFuture<T> f, Runnable a) { |
730 |
> |
return f.thenRunAsync(a, new ThreadExecutor()); |
731 |
> |
} |
732 |
> |
public <T> CompletableFuture<Void> thenAccept |
733 |
> |
(CompletableFuture<T> f, Consumer<? super T> a) { |
734 |
> |
return f.thenAcceptAsync(a, new ThreadExecutor()); |
735 |
> |
} |
736 |
> |
public <T,U> CompletableFuture<U> thenApply |
737 |
> |
(CompletableFuture<T> f, Function<? super T,U> a) { |
738 |
> |
return f.thenApplyAsync(a, new ThreadExecutor()); |
739 |
> |
} |
740 |
> |
public <T,U> CompletableFuture<U> thenCompose |
741 |
> |
(CompletableFuture<T> f, |
742 |
> |
Function<? super T,? extends CompletionStage<U>> a) { |
743 |
> |
return f.thenComposeAsync(a, new ThreadExecutor()); |
744 |
> |
} |
745 |
> |
public <T,U> CompletableFuture<U> handle |
746 |
> |
(CompletableFuture<T> f, |
747 |
> |
BiFunction<? super T,Throwable,? extends U> a) { |
748 |
> |
return f.handleAsync(a, new ThreadExecutor()); |
749 |
> |
} |
750 |
> |
public <T> CompletableFuture<T> whenComplete |
751 |
> |
(CompletableFuture<T> f, |
752 |
> |
BiConsumer<? super T,? super Throwable> a) { |
753 |
> |
return f.whenCompleteAsync(a, new ThreadExecutor()); |
754 |
> |
} |
755 |
> |
public <T,U> CompletableFuture<Void> runAfterBoth |
756 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a) { |
757 |
> |
return f.runAfterBothAsync(g, a, new ThreadExecutor()); |
758 |
> |
} |
759 |
> |
public <T,U> CompletableFuture<Void> thenAcceptBoth |
760 |
> |
(CompletableFuture<T> f, |
761 |
> |
CompletionStage<? extends U> g, |
762 |
> |
BiConsumer<? super T,? super U> a) { |
763 |
> |
return f.thenAcceptBothAsync(g, a, new ThreadExecutor()); |
764 |
> |
} |
765 |
> |
public <T,U,V> CompletableFuture<V> thenCombine |
766 |
> |
(CompletableFuture<T> f, |
767 |
> |
CompletionStage<? extends U> g, |
768 |
> |
BiFunction<? super T,? super U,? extends V> a) { |
769 |
> |
return f.thenCombineAsync(g, a, new ThreadExecutor()); |
770 |
> |
} |
771 |
> |
public <T> CompletableFuture<Void> runAfterEither |
772 |
> |
(CompletableFuture<T> f, |
773 |
> |
CompletionStage<?> g, |
774 |
> |
java.lang.Runnable a) { |
775 |
> |
return f.runAfterEitherAsync(g, a, new ThreadExecutor()); |
776 |
> |
} |
777 |
> |
public <T> CompletableFuture<Void> acceptEither |
778 |
> |
(CompletableFuture<T> f, |
779 |
> |
CompletionStage<? extends T> g, |
780 |
> |
Consumer<? super T> a) { |
781 |
> |
return f.acceptEitherAsync(g, a, new ThreadExecutor()); |
782 |
> |
} |
783 |
> |
public <T,U> CompletableFuture<U> applyToEither |
784 |
> |
(CompletableFuture<T> f, |
785 |
> |
CompletionStage<? extends T> g, |
786 |
> |
Function<? super T,U> a) { |
787 |
> |
return f.applyToEitherAsync(g, a, new ThreadExecutor()); |
788 |
> |
} |
789 |
> |
}; |
790 |
|
|
791 |
< |
r = new Noop(); |
792 |
< |
f = new CompletableFuture<Integer>(); |
793 |
< |
f.complete(one); |
794 |
< |
f2 = new CompletableFuture<Integer>(); |
795 |
< |
g = f.runAfterBoth(f2, r); |
796 |
< |
f2.completeExceptionally(new CFException()); |
797 |
< |
checkCompletedWithWrappedCFException(g); |
798 |
< |
} |
791 |
> |
public abstract void checkExecutionMode(); |
792 |
> |
public abstract CompletableFuture<Void> runAsync(Runnable a); |
793 |
> |
public abstract <U> CompletableFuture<U> supplyAsync(Supplier<U> a); |
794 |
> |
public abstract <T> CompletableFuture<Void> thenRun |
795 |
> |
(CompletableFuture<T> f, Runnable a); |
796 |
> |
public abstract <T> CompletableFuture<Void> thenAccept |
797 |
> |
(CompletableFuture<T> f, Consumer<? super T> a); |
798 |
> |
public abstract <T,U> CompletableFuture<U> thenApply |
799 |
> |
(CompletableFuture<T> f, Function<? super T,U> a); |
800 |
> |
public abstract <T,U> CompletableFuture<U> thenCompose |
801 |
> |
(CompletableFuture<T> f, |
802 |
> |
Function<? super T,? extends CompletionStage<U>> a); |
803 |
> |
public abstract <T,U> CompletableFuture<U> handle |
804 |
> |
(CompletableFuture<T> f, |
805 |
> |
BiFunction<? super T,Throwable,? extends U> a); |
806 |
> |
public abstract <T> CompletableFuture<T> whenComplete |
807 |
> |
(CompletableFuture<T> f, |
808 |
> |
BiConsumer<? super T,? super Throwable> a); |
809 |
> |
public abstract <T,U> CompletableFuture<Void> runAfterBoth |
810 |
> |
(CompletableFuture<T> f, CompletableFuture<U> g, Runnable a); |
811 |
> |
public abstract <T,U> CompletableFuture<Void> thenAcceptBoth |
812 |
> |
(CompletableFuture<T> f, |
813 |
> |
CompletionStage<? extends U> g, |
814 |
> |
BiConsumer<? super T,? super U> a); |
815 |
> |
public abstract <T,U,V> CompletableFuture<V> thenCombine |
816 |
> |
(CompletableFuture<T> f, |
817 |
> |
CompletionStage<? extends U> g, |
818 |
> |
BiFunction<? super T,? super U,? extends V> a); |
819 |
> |
public abstract <T> CompletableFuture<Void> runAfterEither |
820 |
> |
(CompletableFuture<T> f, |
821 |
> |
CompletionStage<?> g, |
822 |
> |
java.lang.Runnable a); |
823 |
> |
public abstract <T> CompletableFuture<Void> acceptEither |
824 |
> |
(CompletableFuture<T> f, |
825 |
> |
CompletionStage<? extends T> g, |
826 |
> |
Consumer<? super T> a); |
827 |
> |
public abstract <T,U> CompletableFuture<U> applyToEither |
828 |
> |
(CompletableFuture<T> f, |
829 |
> |
CompletionStage<? extends T> g, |
830 |
> |
Function<? super T,U> a); |
831 |
> |
} |
832 |
> |
|
833 |
> |
/** |
834 |
> |
* exceptionally action is not invoked when source completes |
835 |
> |
* normally, and source result is propagated |
836 |
> |
*/ |
837 |
> |
public void testExceptionally_normalCompletion() { |
838 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
839 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
840 |
> |
{ |
841 |
> |
final AtomicInteger a = new AtomicInteger(0); |
842 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
843 |
> |
if (!createIncomplete) f.complete(v1); |
844 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
845 |
> |
((Throwable t) -> { |
846 |
> |
// Should not be called |
847 |
> |
a.getAndIncrement(); |
848 |
> |
throw new AssertionError(); |
849 |
> |
}); |
850 |
> |
if (createIncomplete) f.complete(v1); |
851 |
> |
|
852 |
> |
checkCompletedNormally(g, v1); |
853 |
> |
checkCompletedNormally(f, v1); |
854 |
> |
assertEquals(0, a.get()); |
855 |
> |
}} |
856 |
|
|
857 |
|
/** |
858 |
< |
* runAfterBoth result completes exceptionally if action does |
858 |
> |
* exceptionally action completes with function value on source |
859 |
> |
* exception |
860 |
|
*/ |
861 |
< |
public void testRunAfterBoth3() { |
862 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
863 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
864 |
< |
FailingNoop r = new FailingNoop(); |
865 |
< |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
866 |
< |
f.complete(one); |
867 |
< |
checkIncomplete(g); |
868 |
< |
f2.complete(two); |
869 |
< |
checkCompletedWithWrappedCFException(g); |
870 |
< |
} |
861 |
> |
public void testExceptionally_exceptionalCompletion() { |
862 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
863 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
864 |
> |
{ |
865 |
> |
final AtomicInteger a = new AtomicInteger(0); |
866 |
> |
final CFException ex = new CFException(); |
867 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
868 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
869 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
870 |
> |
((Throwable t) -> { |
871 |
> |
ExecutionMode.DEFAULT.checkExecutionMode(); |
872 |
> |
threadAssertSame(t, ex); |
873 |
> |
a.getAndIncrement(); |
874 |
> |
return v1; |
875 |
> |
}); |
876 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
877 |
> |
|
878 |
> |
checkCompletedNormally(g, v1); |
879 |
> |
assertEquals(1, a.get()); |
880 |
> |
}} |
881 |
> |
|
882 |
> |
public void testExceptionally_exceptionalCompletionActionFailed() { |
883 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
884 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
885 |
> |
{ |
886 |
> |
final AtomicInteger a = new AtomicInteger(0); |
887 |
> |
final CFException ex1 = new CFException(); |
888 |
> |
final CFException ex2 = new CFException(); |
889 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
890 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
891 |
> |
final CompletableFuture<Integer> g = f.exceptionally |
892 |
> |
((Throwable t) -> { |
893 |
> |
ExecutionMode.DEFAULT.checkExecutionMode(); |
894 |
> |
threadAssertSame(t, ex1); |
895 |
> |
a.getAndIncrement(); |
896 |
> |
throw ex2; |
897 |
> |
}); |
898 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
899 |
> |
|
900 |
> |
checkCompletedWithWrappedException(g, ex2); |
901 |
> |
assertEquals(1, a.get()); |
902 |
> |
}} |
903 |
> |
|
904 |
> |
/** |
905 |
> |
* whenComplete action executes on normal completion, propagating |
906 |
> |
* source result. |
907 |
> |
*/ |
908 |
> |
public void testWhenComplete_normalCompletion1() { |
909 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
910 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
911 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
912 |
> |
{ |
913 |
> |
final AtomicInteger a = new AtomicInteger(0); |
914 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
915 |
> |
if (!createIncomplete) f.complete(v1); |
916 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
917 |
> |
(f, |
918 |
> |
(Integer x, Throwable t) -> { |
919 |
> |
m.checkExecutionMode(); |
920 |
> |
threadAssertSame(x, v1); |
921 |
> |
threadAssertNull(t); |
922 |
> |
a.getAndIncrement(); |
923 |
> |
}); |
924 |
> |
if (createIncomplete) f.complete(v1); |
925 |
> |
|
926 |
> |
checkCompletedNormally(g, v1); |
927 |
> |
checkCompletedNormally(f, v1); |
928 |
> |
assertEquals(1, a.get()); |
929 |
> |
}} |
930 |
> |
|
931 |
> |
/** |
932 |
> |
* whenComplete action executes on exceptional completion, propagating |
933 |
> |
* source result. |
934 |
> |
*/ |
935 |
> |
public void testWhenComplete_exceptionalCompletion() { |
936 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
937 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
938 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
939 |
> |
{ |
940 |
> |
final AtomicInteger a = new AtomicInteger(0); |
941 |
> |
final CFException ex = new CFException(); |
942 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
943 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
944 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
945 |
> |
(f, |
946 |
> |
(Integer x, Throwable t) -> { |
947 |
> |
m.checkExecutionMode(); |
948 |
> |
threadAssertNull(x); |
949 |
> |
threadAssertSame(t, ex); |
950 |
> |
a.getAndIncrement(); |
951 |
> |
}); |
952 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
953 |
> |
|
954 |
> |
checkCompletedWithWrappedException(g, ex); |
955 |
> |
checkCompletedExceptionally(f, ex); |
956 |
> |
assertEquals(1, a.get()); |
957 |
> |
}} |
958 |
> |
|
959 |
> |
/** |
960 |
> |
* whenComplete action executes on cancelled source, propagating |
961 |
> |
* CancellationException. |
962 |
> |
*/ |
963 |
> |
public void testWhenComplete_sourceCancelled() { |
964 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
965 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
966 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
967 |
> |
{ |
968 |
> |
final AtomicInteger a = new AtomicInteger(0); |
969 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
970 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
971 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
972 |
> |
(f, |
973 |
> |
(Integer x, Throwable t) -> { |
974 |
> |
m.checkExecutionMode(); |
975 |
> |
threadAssertNull(x); |
976 |
> |
threadAssertTrue(t instanceof CancellationException); |
977 |
> |
a.getAndIncrement(); |
978 |
> |
}); |
979 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
980 |
|
|
812 |
– |
/** |
813 |
– |
* runAfterBoth result completes exceptionally if either source cancelled |
814 |
– |
*/ |
815 |
– |
public void testRunAfterBoth4() { |
816 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
817 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
818 |
– |
Noop r = new Noop(); |
819 |
– |
CompletableFuture<Void> g = f.runAfterBoth(f2, r); |
820 |
– |
assertTrue(f.cancel(true)); |
821 |
– |
f2.complete(two); |
981 |
|
checkCompletedWithWrappedCancellationException(g); |
982 |
< |
f = new CompletableFuture<Integer>(); |
983 |
< |
f2 = new CompletableFuture<Integer>(); |
984 |
< |
r = new Noop(); |
826 |
< |
g = f.runAfterBoth(f2, r); |
827 |
< |
f.complete(one); |
828 |
< |
assertTrue(f2.cancel(true)); |
829 |
< |
checkCompletedWithWrappedCancellationException(g); |
830 |
< |
} |
982 |
> |
checkCancelled(f); |
983 |
> |
assertEquals(1, a.get()); |
984 |
> |
}} |
985 |
|
|
986 |
|
/** |
987 |
< |
* applyToEither result completes normally after normal completion |
988 |
< |
* of either source |
987 |
> |
* If a whenComplete action throws an exception when triggered by |
988 |
> |
* a normal completion, it completes exceptionally |
989 |
|
*/ |
990 |
< |
public void testApplyToEither() { |
991 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
992 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
993 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
994 |
< |
f.complete(one); |
995 |
< |
checkCompletedNormally(g, two); |
996 |
< |
f2.complete(one); |
997 |
< |
checkCompletedNormally(g, two); |
998 |
< |
|
999 |
< |
f = new CompletableFuture<Integer>(); |
1000 |
< |
f.complete(one); |
1001 |
< |
f2 = new CompletableFuture<Integer>(); |
1002 |
< |
g = f.applyToEither(f2, inc); |
1003 |
< |
checkCompletedNormally(g, two); |
1004 |
< |
} |
990 |
> |
public void testWhenComplete_actionFailed() { |
991 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
992 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
993 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
994 |
> |
{ |
995 |
> |
final AtomicInteger a = new AtomicInteger(0); |
996 |
> |
final CFException ex = new CFException(); |
997 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
998 |
> |
if (!createIncomplete) f.complete(v1); |
999 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1000 |
> |
(f, |
1001 |
> |
(Integer x, Throwable t) -> { |
1002 |
> |
m.checkExecutionMode(); |
1003 |
> |
threadAssertSame(x, v1); |
1004 |
> |
threadAssertNull(t); |
1005 |
> |
a.getAndIncrement(); |
1006 |
> |
throw ex; |
1007 |
> |
}); |
1008 |
> |
if (createIncomplete) f.complete(v1); |
1009 |
> |
|
1010 |
> |
checkCompletedWithWrappedException(g, ex); |
1011 |
> |
checkCompletedNormally(f, v1); |
1012 |
> |
assertEquals(1, a.get()); |
1013 |
> |
}} |
1014 |
> |
|
1015 |
> |
/** |
1016 |
> |
* If a whenComplete action throws an exception when triggered by |
1017 |
> |
* a source completion that also throws an exception, the source |
1018 |
> |
* exception takes precedence. |
1019 |
> |
*/ |
1020 |
> |
public void testWhenComplete_actionFailedSourceFailed() { |
1021 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1022 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1023 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1024 |
> |
{ |
1025 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1026 |
> |
final CFException ex1 = new CFException(); |
1027 |
> |
final CFException ex2 = new CFException(); |
1028 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1029 |
> |
|
1030 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1031 |
> |
final CompletableFuture<Integer> g = m.whenComplete |
1032 |
> |
(f, |
1033 |
> |
(Integer x, Throwable t) -> { |
1034 |
> |
m.checkExecutionMode(); |
1035 |
> |
threadAssertSame(t, ex1); |
1036 |
> |
threadAssertNull(x); |
1037 |
> |
a.getAndIncrement(); |
1038 |
> |
throw ex2; |
1039 |
> |
}); |
1040 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1041 |
> |
|
1042 |
> |
checkCompletedWithWrappedException(g, ex1); |
1043 |
> |
checkCompletedExceptionally(f, ex1); |
1044 |
> |
assertEquals(1, a.get()); |
1045 |
> |
}} |
1046 |
> |
|
1047 |
> |
/** |
1048 |
> |
* handle action completes normally with function value on normal |
1049 |
> |
* completion of source |
1050 |
> |
*/ |
1051 |
> |
public void testHandle_normalCompletion() { |
1052 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1053 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1054 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1055 |
> |
{ |
1056 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1057 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1058 |
> |
if (!createIncomplete) f.complete(v1); |
1059 |
> |
final CompletableFuture<Integer> g = m.handle |
1060 |
> |
(f, |
1061 |
> |
(Integer x, Throwable t) -> { |
1062 |
> |
m.checkExecutionMode(); |
1063 |
> |
threadAssertSame(x, v1); |
1064 |
> |
threadAssertNull(t); |
1065 |
> |
a.getAndIncrement(); |
1066 |
> |
return inc(v1); |
1067 |
> |
}); |
1068 |
> |
if (createIncomplete) f.complete(v1); |
1069 |
> |
|
1070 |
> |
checkCompletedNormally(g, inc(v1)); |
1071 |
> |
checkCompletedNormally(f, v1); |
1072 |
> |
assertEquals(1, a.get()); |
1073 |
> |
}} |
1074 |
|
|
1075 |
|
/** |
1076 |
< |
* applyToEither result completes exceptionally after exceptional |
1077 |
< |
* completion of either source |
1076 |
> |
* handle action completes normally with function value on |
1077 |
> |
* exceptional completion of source |
1078 |
|
*/ |
1079 |
< |
public void testApplyToEither2() { |
1080 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1081 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1082 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
1083 |
< |
f.completeExceptionally(new CFException()); |
1084 |
< |
f2.complete(one); |
1085 |
< |
checkCompletedWithWrappedCFException(g); |
1079 |
> |
public void testHandle_exceptionalCompletion() { |
1080 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1081 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1082 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1083 |
> |
{ |
1084 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1085 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1086 |
> |
final CFException ex = new CFException(); |
1087 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1088 |
> |
final CompletableFuture<Integer> g = m.handle |
1089 |
> |
(f, |
1090 |
> |
(Integer x, Throwable t) -> { |
1091 |
> |
m.checkExecutionMode(); |
1092 |
> |
threadAssertNull(x); |
1093 |
> |
threadAssertSame(t, ex); |
1094 |
> |
a.getAndIncrement(); |
1095 |
> |
return v1; |
1096 |
> |
}); |
1097 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
1098 |
> |
|
1099 |
> |
checkCompletedNormally(g, v1); |
1100 |
> |
checkCompletedExceptionally(f, ex); |
1101 |
> |
assertEquals(1, a.get()); |
1102 |
> |
}} |
1103 |
> |
|
1104 |
> |
/** |
1105 |
> |
* handle action completes normally with function value on |
1106 |
> |
* cancelled source |
1107 |
> |
*/ |
1108 |
> |
public void testHandle_sourceCancelled() { |
1109 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1110 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1111 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1112 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1113 |
> |
{ |
1114 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1115 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1116 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1117 |
> |
final CompletableFuture<Integer> g = m.handle |
1118 |
> |
(f, |
1119 |
> |
(Integer x, Throwable t) -> { |
1120 |
> |
m.checkExecutionMode(); |
1121 |
> |
threadAssertNull(x); |
1122 |
> |
threadAssertTrue(t instanceof CancellationException); |
1123 |
> |
a.getAndIncrement(); |
1124 |
> |
return v1; |
1125 |
> |
}); |
1126 |
> |
if (createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1127 |
|
|
1128 |
< |
f = new CompletableFuture<Integer>(); |
1129 |
< |
f2 = new CompletableFuture<Integer>(); |
1130 |
< |
f2.completeExceptionally(new CFException()); |
1131 |
< |
g = f.applyToEither(f2, inc); |
868 |
< |
checkCompletedWithWrappedCFException(g); |
869 |
< |
} |
870 |
< |
|
871 |
< |
/** |
872 |
< |
* applyToEither result completes exceptionally if action does |
873 |
< |
*/ |
874 |
< |
public void testApplyToEither3() { |
875 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
876 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
877 |
< |
FailingFunction r = new FailingFunction(); |
878 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, r); |
879 |
< |
f2.complete(two); |
880 |
< |
checkCompletedWithWrappedCFException(g); |
881 |
< |
} |
1128 |
> |
checkCompletedNormally(g, v1); |
1129 |
> |
checkCancelled(f); |
1130 |
> |
assertEquals(1, a.get()); |
1131 |
> |
}} |
1132 |
|
|
1133 |
|
/** |
1134 |
< |
* applyToEither result completes exceptionally if either source cancelled |
1134 |
> |
* handle result completes exceptionally if action does |
1135 |
|
*/ |
1136 |
< |
public void testApplyToEither4() { |
1137 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1138 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1139 |
< |
CompletableFuture<Integer> g = f.applyToEither(f2, inc); |
1140 |
< |
assertTrue(f.cancel(true)); |
1141 |
< |
checkCompletedWithWrappedCancellationException(g); |
1142 |
< |
f = new CompletableFuture<Integer>(); |
1143 |
< |
f2 = new CompletableFuture<Integer>(); |
1144 |
< |
assertTrue(f2.cancel(true)); |
1145 |
< |
checkCompletedWithWrappedCancellationException(g); |
1146 |
< |
} |
1136 |
> |
public void testHandle_sourceFailedActionFailed() { |
1137 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1138 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1139 |
> |
{ |
1140 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1141 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1142 |
> |
final CFException ex1 = new CFException(); |
1143 |
> |
final CFException ex2 = new CFException(); |
1144 |
> |
if (!createIncomplete) f.completeExceptionally(ex1); |
1145 |
> |
final CompletableFuture<Integer> g = m.handle |
1146 |
> |
(f, |
1147 |
> |
(Integer x, Throwable t) -> { |
1148 |
> |
m.checkExecutionMode(); |
1149 |
> |
threadAssertNull(x); |
1150 |
> |
threadAssertSame(ex1, t); |
1151 |
> |
a.getAndIncrement(); |
1152 |
> |
throw ex2; |
1153 |
> |
}); |
1154 |
> |
if (createIncomplete) f.completeExceptionally(ex1); |
1155 |
> |
|
1156 |
> |
checkCompletedWithWrappedException(g, ex2); |
1157 |
> |
checkCompletedExceptionally(f, ex1); |
1158 |
> |
assertEquals(1, a.get()); |
1159 |
> |
}} |
1160 |
> |
|
1161 |
> |
public void testHandle_sourceCompletedNormallyActionFailed() { |
1162 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1163 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1164 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1165 |
> |
{ |
1166 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1167 |
> |
final AtomicInteger a = new AtomicInteger(0); |
1168 |
> |
final CFException ex = new CFException(); |
1169 |
> |
if (!createIncomplete) f.complete(v1); |
1170 |
> |
final CompletableFuture<Integer> g = m.handle |
1171 |
> |
(f, |
1172 |
> |
(Integer x, Throwable t) -> { |
1173 |
> |
m.checkExecutionMode(); |
1174 |
> |
threadAssertSame(x, v1); |
1175 |
> |
threadAssertNull(t); |
1176 |
> |
a.getAndIncrement(); |
1177 |
> |
throw ex; |
1178 |
> |
}); |
1179 |
> |
if (createIncomplete) f.complete(v1); |
1180 |
> |
|
1181 |
> |
checkCompletedWithWrappedException(g, ex); |
1182 |
> |
checkCompletedNormally(f, v1); |
1183 |
> |
assertEquals(1, a.get()); |
1184 |
> |
}} |
1185 |
|
|
1186 |
|
/** |
1187 |
< |
* acceptEither result completes normally after normal completion |
900 |
< |
* of either source |
1187 |
> |
* runAsync completes after running Runnable |
1188 |
|
*/ |
1189 |
< |
public void testAcceptEither() { |
1190 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1191 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1192 |
< |
IncAction r = new IncAction(); |
1193 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1194 |
< |
f.complete(one); |
1195 |
< |
checkCompletedNormally(g, null); |
1196 |
< |
f2.complete(one); |
1197 |
< |
checkCompletedNormally(g, null); |
1198 |
< |
assertEquals(r.value, 2); |
1199 |
< |
|
1200 |
< |
r = new IncAction(); |
1201 |
< |
f = new CompletableFuture<Integer>(); |
915 |
< |
f.complete(one); |
916 |
< |
f2 = new CompletableFuture<Integer>(); |
917 |
< |
g = f.acceptEither(f2, r); |
918 |
< |
checkCompletedNormally(g, null); |
919 |
< |
assertEquals(r.value, 2); |
920 |
< |
} |
1189 |
> |
public void testRunAsync_normalCompletion() { |
1190 |
> |
ExecutionMode[] executionModes = { |
1191 |
> |
ExecutionMode.ASYNC, |
1192 |
> |
ExecutionMode.EXECUTOR, |
1193 |
> |
}; |
1194 |
> |
for (ExecutionMode m : executionModes) |
1195 |
> |
{ |
1196 |
> |
final Noop r = new Noop(m); |
1197 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1198 |
> |
assertNull(f.join()); |
1199 |
> |
checkCompletedNormally(f, null); |
1200 |
> |
r.assertInvoked(); |
1201 |
> |
}} |
1202 |
|
|
1203 |
|
/** |
1204 |
< |
* acceptEither result completes exceptionally after exceptional |
924 |
< |
* completion of either source |
1204 |
> |
* failing runAsync completes exceptionally after running Runnable |
1205 |
|
*/ |
1206 |
< |
public void testAcceptEither2() { |
1207 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1208 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1209 |
< |
IncAction r = new IncAction(); |
1210 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1211 |
< |
f.completeExceptionally(new CFException()); |
1212 |
< |
f2.complete(one); |
1213 |
< |
checkCompletedWithWrappedCFException(g); |
1214 |
< |
|
1215 |
< |
r = new IncAction(); |
1216 |
< |
f = new CompletableFuture<Integer>(); |
1217 |
< |
f2 = new CompletableFuture<Integer>(); |
938 |
< |
f2.completeExceptionally(new CFException()); |
939 |
< |
g = f.acceptEither(f2, r); |
940 |
< |
checkCompletedWithWrappedCFException(g); |
941 |
< |
} |
1206 |
> |
public void testRunAsync_exceptionalCompletion() { |
1207 |
> |
ExecutionMode[] executionModes = { |
1208 |
> |
ExecutionMode.ASYNC, |
1209 |
> |
ExecutionMode.EXECUTOR, |
1210 |
> |
}; |
1211 |
> |
for (ExecutionMode m : executionModes) |
1212 |
> |
{ |
1213 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1214 |
> |
final CompletableFuture<Void> f = m.runAsync(r); |
1215 |
> |
checkCompletedWithWrappedCFException(f); |
1216 |
> |
r.assertInvoked(); |
1217 |
> |
}} |
1218 |
|
|
1219 |
|
/** |
1220 |
< |
* acceptEither result completes exceptionally if action does |
1220 |
> |
* supplyAsync completes with result of supplier |
1221 |
|
*/ |
1222 |
< |
public void testAcceptEither3() { |
1223 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1224 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1225 |
< |
FailingConsumer r = new FailingConsumer(); |
1226 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1227 |
< |
f2.complete(two); |
1228 |
< |
checkCompletedWithWrappedCFException(g); |
1229 |
< |
} |
1222 |
> |
public void testSupplyAsync_normalCompletion() { |
1223 |
> |
ExecutionMode[] executionModes = { |
1224 |
> |
ExecutionMode.ASYNC, |
1225 |
> |
ExecutionMode.EXECUTOR, |
1226 |
> |
}; |
1227 |
> |
for (ExecutionMode m : executionModes) |
1228 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1229 |
> |
{ |
1230 |
> |
final IntegerSupplier r = new IntegerSupplier(m, v1); |
1231 |
> |
final CompletableFuture<Integer> f = m.supplyAsync(r); |
1232 |
> |
assertSame(v1, f.join()); |
1233 |
> |
checkCompletedNormally(f, v1); |
1234 |
> |
r.assertInvoked(); |
1235 |
> |
}} |
1236 |
|
|
1237 |
|
/** |
1238 |
< |
* acceptEither result completes exceptionally if either source cancelled |
1238 |
> |
* Failing supplyAsync completes exceptionally |
1239 |
|
*/ |
1240 |
< |
public void testAcceptEither4() { |
1241 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1242 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1243 |
< |
IncAction r = new IncAction(); |
1244 |
< |
CompletableFuture<Void> g = f.acceptEither(f2, r); |
1245 |
< |
assertTrue(f.cancel(true)); |
1246 |
< |
checkCompletedWithWrappedCancellationException(g); |
1247 |
< |
f = new CompletableFuture<Integer>(); |
1248 |
< |
f2 = new CompletableFuture<Integer>(); |
1249 |
< |
assertTrue(f2.cancel(true)); |
1250 |
< |
checkCompletedWithWrappedCancellationException(g); |
1251 |
< |
} |
1240 |
> |
public void testSupplyAsync_exceptionalCompletion() { |
1241 |
> |
ExecutionMode[] executionModes = { |
1242 |
> |
ExecutionMode.ASYNC, |
1243 |
> |
ExecutionMode.EXECUTOR, |
1244 |
> |
}; |
1245 |
> |
for (ExecutionMode m : executionModes) |
1246 |
> |
{ |
1247 |
> |
FailingSupplier r = new FailingSupplier(m); |
1248 |
> |
CompletableFuture<Integer> f = m.supplyAsync(r); |
1249 |
> |
checkCompletedWithWrappedCFException(f); |
1250 |
> |
r.assertInvoked(); |
1251 |
> |
}} |
1252 |
|
|
1253 |
+ |
// seq completion methods |
1254 |
|
|
1255 |
|
/** |
1256 |
< |
* runAfterEither result completes normally after normal completion |
974 |
< |
* of either source |
1256 |
> |
* thenRun result completes normally after normal completion of source |
1257 |
|
*/ |
1258 |
< |
public void testRunAfterEither() { |
1259 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1260 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1261 |
< |
Noop r = new Noop(); |
1262 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
1263 |
< |
f.complete(one); |
1264 |
< |
checkCompletedNormally(g, null); |
1265 |
< |
f2.complete(one); |
1266 |
< |
checkCompletedNormally(g, null); |
1267 |
< |
assertTrue(r.ran); |
1258 |
> |
public void testThenRun_normalCompletion() { |
1259 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1260 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1261 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1262 |
> |
{ |
1263 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1264 |
> |
final Noop r = new Noop(m); |
1265 |
> |
if (!createIncomplete) f.complete(v1); |
1266 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1267 |
> |
if (createIncomplete) { |
1268 |
> |
checkIncomplete(g); |
1269 |
> |
f.complete(v1); |
1270 |
> |
} |
1271 |
|
|
987 |
– |
r = new Noop(); |
988 |
– |
f = new CompletableFuture<Integer>(); |
989 |
– |
f.complete(one); |
990 |
– |
f2 = new CompletableFuture<Integer>(); |
991 |
– |
g = f.runAfterEither(f2, r); |
1272 |
|
checkCompletedNormally(g, null); |
1273 |
< |
assertTrue(r.ran); |
1274 |
< |
} |
1275 |
< |
|
996 |
< |
/** |
997 |
< |
* runAfterEither result completes exceptionally after exceptional |
998 |
< |
* completion of either source |
999 |
< |
*/ |
1000 |
< |
public void testRunAfterEither2() { |
1001 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1002 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1003 |
< |
Noop r = new Noop(); |
1004 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
1005 |
< |
f.completeExceptionally(new CFException()); |
1006 |
< |
f2.complete(one); |
1007 |
< |
checkCompletedWithWrappedCFException(g); |
1008 |
< |
|
1009 |
< |
r = new Noop(); |
1010 |
< |
f = new CompletableFuture<Integer>(); |
1011 |
< |
f2 = new CompletableFuture<Integer>(); |
1012 |
< |
f2.completeExceptionally(new CFException()); |
1013 |
< |
g = f.runAfterEither(f2, r); |
1014 |
< |
checkCompletedWithWrappedCFException(g); |
1015 |
< |
} |
1016 |
< |
|
1017 |
< |
/** |
1018 |
< |
* runAfterEither result completes exceptionally if action does |
1019 |
< |
*/ |
1020 |
< |
public void testRunAfterEither3() { |
1021 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1022 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1023 |
< |
FailingNoop r = new FailingNoop(); |
1024 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
1025 |
< |
f2.complete(two); |
1026 |
< |
checkCompletedWithWrappedCFException(g); |
1027 |
< |
} |
1028 |
< |
|
1029 |
< |
/** |
1030 |
< |
* runAfterEither result completes exceptionally if either source cancelled |
1031 |
< |
*/ |
1032 |
< |
public void testRunAfterEither4() { |
1033 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1034 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1035 |
< |
Noop r = new Noop(); |
1036 |
< |
CompletableFuture<Void> g = f.runAfterEither(f2, r); |
1037 |
< |
assertTrue(f.cancel(true)); |
1038 |
< |
checkCompletedWithWrappedCancellationException(g); |
1039 |
< |
f = new CompletableFuture<Integer>(); |
1040 |
< |
f2 = new CompletableFuture<Integer>(); |
1041 |
< |
assertTrue(f2.cancel(true)); |
1042 |
< |
checkCompletedWithWrappedCancellationException(g); |
1043 |
< |
} |
1044 |
< |
|
1045 |
< |
/** |
1046 |
< |
* thenCompose result completes normally after normal completion of source |
1047 |
< |
*/ |
1048 |
< |
public void testThenCompose() { |
1049 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1050 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1051 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1052 |
< |
f.complete(one); |
1053 |
< |
checkCompletedNormally(g, two); |
1054 |
< |
} |
1273 |
> |
checkCompletedNormally(f, v1); |
1274 |
> |
r.assertInvoked(); |
1275 |
> |
}} |
1276 |
|
|
1277 |
|
/** |
1278 |
< |
* thenCompose result completes exceptionally after exceptional |
1278 |
> |
* thenRun result completes exceptionally after exceptional |
1279 |
|
* completion of source |
1280 |
|
*/ |
1281 |
< |
public void testThenCompose2() { |
1282 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1283 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1284 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1285 |
< |
f.completeExceptionally(new CFException()); |
1286 |
< |
checkCompletedWithWrappedCFException(g); |
1287 |
< |
} |
1288 |
< |
|
1289 |
< |
/** |
1290 |
< |
* thenCompose result completes exceptionally if action does |
1291 |
< |
*/ |
1292 |
< |
public void testThenCompose3() { |
1293 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1073 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
1074 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1075 |
< |
f.complete(one); |
1076 |
< |
checkCompletedWithWrappedCFException(g); |
1077 |
< |
} |
1078 |
< |
|
1079 |
< |
/** |
1080 |
< |
* thenCompose result completes exceptionally if source cancelled |
1081 |
< |
*/ |
1082 |
< |
public void testThenCompose4() { |
1083 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1084 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1085 |
< |
CompletableFuture<Integer> g = f.thenCompose(r); |
1086 |
< |
assertTrue(f.cancel(true)); |
1087 |
< |
checkCompletedWithWrappedCancellationException(g); |
1088 |
< |
} |
1089 |
< |
|
1090 |
< |
|
1091 |
< |
// asyncs |
1092 |
< |
|
1093 |
< |
/** |
1094 |
< |
* thenRunAsync result completes normally after normal completion of source |
1095 |
< |
*/ |
1096 |
< |
public void testThenRunAsync() { |
1097 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1098 |
< |
Noop r = new Noop(); |
1099 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1100 |
< |
f.complete(null); |
1101 |
< |
checkCompletedNormally(g, null); |
1281 |
> |
public void testThenRun_exceptionalCompletion() { |
1282 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1283 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1284 |
> |
{ |
1285 |
> |
final CFException ex = new CFException(); |
1286 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1287 |
> |
final Noop r = new Noop(m); |
1288 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1289 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1290 |
> |
if (createIncomplete) { |
1291 |
> |
checkIncomplete(g); |
1292 |
> |
f.completeExceptionally(ex); |
1293 |
> |
} |
1294 |
|
|
1295 |
< |
// reordered version |
1296 |
< |
f = new CompletableFuture<Integer>(); |
1297 |
< |
f.complete(null); |
1298 |
< |
r = new Noop(); |
1107 |
< |
g = f.thenRunAsync(r); |
1108 |
< |
checkCompletedNormally(g, null); |
1109 |
< |
} |
1295 |
> |
checkCompletedWithWrappedException(g, ex); |
1296 |
> |
checkCompletedExceptionally(f, ex); |
1297 |
> |
r.assertNotInvoked(); |
1298 |
> |
}} |
1299 |
|
|
1300 |
|
/** |
1301 |
< |
* thenRunAsync result completes exceptionally after exceptional |
1113 |
< |
* completion of source |
1301 |
> |
* thenRun result completes exceptionally if source cancelled |
1302 |
|
*/ |
1303 |
< |
public void testThenRunAsync2() { |
1304 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1305 |
< |
Noop r = new Noop(); |
1306 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1307 |
< |
f.completeExceptionally(new CFException()); |
1308 |
< |
try { |
1309 |
< |
g.join(); |
1310 |
< |
shouldThrow(); |
1311 |
< |
} catch (Exception ok) { |
1303 |
> |
public void testThenRun_sourceCancelled() { |
1304 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1305 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1306 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1307 |
> |
{ |
1308 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1309 |
> |
final Noop r = new Noop(m); |
1310 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1311 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1312 |
> |
if (createIncomplete) { |
1313 |
> |
checkIncomplete(g); |
1314 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1315 |
|
} |
1125 |
– |
checkCompletedWithWrappedCFException(g); |
1126 |
– |
} |
1316 |
|
|
1128 |
– |
/** |
1129 |
– |
* thenRunAsync result completes exceptionally if action does |
1130 |
– |
*/ |
1131 |
– |
public void testThenRunAsync3() { |
1132 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1133 |
– |
FailingNoop r = new FailingNoop(); |
1134 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1135 |
– |
f.complete(null); |
1136 |
– |
checkCompletedWithWrappedCFException(g); |
1137 |
– |
} |
1138 |
– |
|
1139 |
– |
/** |
1140 |
– |
* thenRunAsync result completes exceptionally if source cancelled |
1141 |
– |
*/ |
1142 |
– |
public void testThenRunAsync4() { |
1143 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1144 |
– |
Noop r = new Noop(); |
1145 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r); |
1146 |
– |
assertTrue(f.cancel(true)); |
1317 |
|
checkCompletedWithWrappedCancellationException(g); |
1318 |
< |
} |
1318 |
> |
checkCancelled(f); |
1319 |
> |
r.assertNotInvoked(); |
1320 |
> |
}} |
1321 |
|
|
1322 |
|
/** |
1323 |
< |
* thenApplyAsync result completes normally after normal completion of source |
1323 |
> |
* thenRun result completes exceptionally if action does |
1324 |
|
*/ |
1325 |
< |
public void testThenApplyAsync() { |
1326 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1327 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
1328 |
< |
f.complete(one); |
1329 |
< |
checkCompletedNormally(g, two); |
1330 |
< |
} |
1325 |
> |
public void testThenRun_actionFailed() { |
1326 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1327 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1328 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1329 |
> |
{ |
1330 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1331 |
> |
final FailingRunnable r = new FailingRunnable(m); |
1332 |
> |
if (!createIncomplete) f.complete(v1); |
1333 |
> |
final CompletableFuture<Void> g = m.thenRun(f, r); |
1334 |
> |
if (createIncomplete) { |
1335 |
> |
checkIncomplete(g); |
1336 |
> |
f.complete(v1); |
1337 |
> |
} |
1338 |
|
|
1160 |
– |
/** |
1161 |
– |
* thenApplyAsync result completes exceptionally after exceptional |
1162 |
– |
* completion of source |
1163 |
– |
*/ |
1164 |
– |
public void testThenApplyAsync2() { |
1165 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1166 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
1167 |
– |
f.completeExceptionally(new CFException()); |
1339 |
|
checkCompletedWithWrappedCFException(g); |
1340 |
< |
} |
1340 |
> |
checkCompletedNormally(f, v1); |
1341 |
> |
}} |
1342 |
|
|
1343 |
|
/** |
1344 |
< |
* thenApplyAsync result completes exceptionally if action does |
1344 |
> |
* thenApply result completes normally after normal completion of source |
1345 |
|
*/ |
1346 |
< |
public void testThenApplyAsync3() { |
1347 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1348 |
< |
FailingFunction r = new FailingFunction(); |
1349 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(r); |
1350 |
< |
f.complete(null); |
1351 |
< |
checkCompletedWithWrappedCFException(g); |
1352 |
< |
} |
1346 |
> |
public void testThenApply_normalCompletion() { |
1347 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1348 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1349 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1350 |
> |
{ |
1351 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1352 |
> |
final IncFunction r = new IncFunction(m); |
1353 |
> |
if (!createIncomplete) f.complete(v1); |
1354 |
> |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1355 |
> |
if (createIncomplete) { |
1356 |
> |
checkIncomplete(g); |
1357 |
> |
f.complete(v1); |
1358 |
> |
} |
1359 |
|
|
1360 |
< |
/** |
1361 |
< |
* thenApplyAsync result completes exceptionally if source cancelled |
1362 |
< |
*/ |
1363 |
< |
public void testThenApplyAsync4() { |
1186 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1187 |
< |
CompletableFuture<Integer> g = f.thenApplyAsync(inc); |
1188 |
< |
assertTrue(f.cancel(true)); |
1189 |
< |
checkCompletedWithWrappedCancellationException(g); |
1190 |
< |
} |
1360 |
> |
checkCompletedNormally(g, inc(v1)); |
1361 |
> |
checkCompletedNormally(f, v1); |
1362 |
> |
r.assertValue(inc(v1)); |
1363 |
> |
}} |
1364 |
|
|
1365 |
|
/** |
1366 |
< |
* thenAcceptAsync result completes normally after normal |
1366 |
> |
* thenApply result completes exceptionally after exceptional |
1367 |
|
* completion of source |
1368 |
|
*/ |
1369 |
< |
public void testThenAcceptAsync() { |
1370 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1371 |
< |
IncAction r = new IncAction(); |
1372 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1373 |
< |
f.complete(one); |
1374 |
< |
checkCompletedNormally(g, null); |
1375 |
< |
assertEquals(r.value, 2); |
1376 |
< |
} |
1369 |
> |
public void testThenApply_exceptionalCompletion() { |
1370 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1371 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1372 |
> |
{ |
1373 |
> |
final CFException ex = new CFException(); |
1374 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1375 |
> |
final IncFunction r = new IncFunction(m); |
1376 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1377 |
> |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1378 |
> |
if (createIncomplete) { |
1379 |
> |
checkIncomplete(g); |
1380 |
> |
f.completeExceptionally(ex); |
1381 |
> |
} |
1382 |
|
|
1383 |
< |
/** |
1384 |
< |
* thenAcceptAsync result completes exceptionally after exceptional |
1385 |
< |
* completion of source |
1386 |
< |
*/ |
1209 |
< |
public void testThenAcceptAsync2() { |
1210 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1211 |
< |
IncAction r = new IncAction(); |
1212 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1213 |
< |
f.completeExceptionally(new CFException()); |
1214 |
< |
checkCompletedWithWrappedCFException(g); |
1215 |
< |
} |
1383 |
> |
checkCompletedWithWrappedException(g, ex); |
1384 |
> |
checkCompletedExceptionally(f, ex); |
1385 |
> |
r.assertNotInvoked(); |
1386 |
> |
}} |
1387 |
|
|
1388 |
|
/** |
1389 |
< |
* thenAcceptAsync result completes exceptionally if action does |
1389 |
> |
* thenApply result completes exceptionally if source cancelled |
1390 |
|
*/ |
1391 |
< |
public void testThenAcceptAsync3() { |
1392 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1393 |
< |
FailingConsumer r = new FailingConsumer(); |
1394 |
< |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1395 |
< |
f.complete(null); |
1396 |
< |
checkCompletedWithWrappedCFException(g); |
1397 |
< |
} |
1391 |
> |
public void testThenApply_sourceCancelled() { |
1392 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1393 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1394 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1395 |
> |
{ |
1396 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1397 |
> |
final IncFunction r = new IncFunction(m); |
1398 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1399 |
> |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1400 |
> |
if (createIncomplete) { |
1401 |
> |
checkIncomplete(g); |
1402 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1403 |
> |
} |
1404 |
|
|
1228 |
– |
/** |
1229 |
– |
* thenAcceptAsync result completes exceptionally if source cancelled |
1230 |
– |
*/ |
1231 |
– |
public void testThenAcceptAsync4() { |
1232 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1233 |
– |
IncAction r = new IncAction(); |
1234 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r); |
1235 |
– |
assertTrue(f.cancel(true)); |
1405 |
|
checkCompletedWithWrappedCancellationException(g); |
1406 |
< |
} |
1407 |
< |
/** |
1408 |
< |
* thenCombineAsync result completes normally after normal |
1240 |
< |
* completion of sources |
1241 |
< |
*/ |
1242 |
< |
public void testThenCombineAsync() { |
1243 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1244 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1245 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1246 |
< |
f.complete(one); |
1247 |
< |
checkIncomplete(g); |
1248 |
< |
f2.complete(two); |
1249 |
< |
checkCompletedNormally(g, three); |
1250 |
< |
} |
1406 |
> |
checkCancelled(f); |
1407 |
> |
r.assertNotInvoked(); |
1408 |
> |
}} |
1409 |
|
|
1410 |
|
/** |
1411 |
< |
* thenCombineAsync result completes exceptionally after exceptional |
1254 |
< |
* completion of source |
1411 |
> |
* thenApply result completes exceptionally if action does |
1412 |
|
*/ |
1413 |
< |
public void testThenCombineAsync2() { |
1414 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1415 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1416 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1417 |
< |
f.completeExceptionally(new CFException()); |
1418 |
< |
f2.complete(two); |
1419 |
< |
checkCompletedWithWrappedCFException(g); |
1420 |
< |
|
1421 |
< |
f = new CompletableFuture<Integer>(); |
1422 |
< |
f2 = new CompletableFuture<Integer>(); |
1423 |
< |
g = f.thenCombineAsync(f2, add); |
1424 |
< |
f.complete(one); |
1425 |
< |
f2.completeExceptionally(new CFException()); |
1269 |
< |
checkCompletedWithWrappedCFException(g); |
1270 |
< |
} |
1413 |
> |
public void testThenApply_actionFailed() { |
1414 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1415 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1416 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1417 |
> |
{ |
1418 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1419 |
> |
final FailingFunction r = new FailingFunction(m); |
1420 |
> |
if (!createIncomplete) f.complete(v1); |
1421 |
> |
final CompletableFuture<Integer> g = m.thenApply(f, r); |
1422 |
> |
if (createIncomplete) { |
1423 |
> |
checkIncomplete(g); |
1424 |
> |
f.complete(v1); |
1425 |
> |
} |
1426 |
|
|
1272 |
– |
/** |
1273 |
– |
* thenCombineAsync result completes exceptionally if action does |
1274 |
– |
*/ |
1275 |
– |
public void testThenCombineAsync3() { |
1276 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1277 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1278 |
– |
FailingBiFunction r = new FailingBiFunction(); |
1279 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r); |
1280 |
– |
f.complete(one); |
1281 |
– |
checkIncomplete(g); |
1282 |
– |
f2.complete(two); |
1427 |
|
checkCompletedWithWrappedCFException(g); |
1428 |
< |
} |
1428 |
> |
checkCompletedNormally(f, v1); |
1429 |
> |
}} |
1430 |
|
|
1431 |
|
/** |
1432 |
< |
* thenCombineAsync result completes exceptionally if either source cancelled |
1432 |
> |
* thenAccept result completes normally after normal completion of source |
1433 |
|
*/ |
1434 |
< |
public void testThenCombineAsync4() { |
1435 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1436 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1437 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add); |
1438 |
< |
assertTrue(f.cancel(true)); |
1439 |
< |
f2.complete(two); |
1440 |
< |
checkCompletedWithWrappedCancellationException(g); |
1441 |
< |
|
1442 |
< |
f = new CompletableFuture<Integer>(); |
1443 |
< |
f2 = new CompletableFuture<Integer>(); |
1444 |
< |
g = f.thenCombineAsync(f2, add); |
1445 |
< |
f.complete(one); |
1446 |
< |
assertTrue(f2.cancel(true)); |
1302 |
< |
checkCompletedWithWrappedCancellationException(g); |
1303 |
< |
} |
1434 |
> |
public void testThenAccept_normalCompletion() { |
1435 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1436 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1437 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1438 |
> |
{ |
1439 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1440 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1441 |
> |
if (!createIncomplete) f.complete(v1); |
1442 |
> |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1443 |
> |
if (createIncomplete) { |
1444 |
> |
checkIncomplete(g); |
1445 |
> |
f.complete(v1); |
1446 |
> |
} |
1447 |
|
|
1305 |
– |
/** |
1306 |
– |
* thenAcceptBothAsync result completes normally after normal |
1307 |
– |
* completion of sources |
1308 |
– |
*/ |
1309 |
– |
public void testThenAcceptBothAsync() { |
1310 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1311 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1312 |
– |
AddAction r = new AddAction(); |
1313 |
– |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1314 |
– |
f.complete(one); |
1315 |
– |
checkIncomplete(g); |
1316 |
– |
f2.complete(two); |
1448 |
|
checkCompletedNormally(g, null); |
1449 |
< |
assertEquals(r.value, 3); |
1450 |
< |
} |
1449 |
> |
r.assertValue(v1); |
1450 |
> |
checkCompletedNormally(f, v1); |
1451 |
> |
}} |
1452 |
|
|
1453 |
|
/** |
1454 |
< |
* thenAcceptBothAsync result completes exceptionally after exceptional |
1454 |
> |
* thenAccept result completes exceptionally after exceptional |
1455 |
|
* completion of source |
1456 |
|
*/ |
1457 |
< |
public void testThenAcceptBothAsync2() { |
1458 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1459 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1460 |
< |
AddAction r = new AddAction(); |
1461 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1462 |
< |
f.completeExceptionally(new CFException()); |
1463 |
< |
f2.complete(two); |
1464 |
< |
checkCompletedWithWrappedCFException(g); |
1457 |
> |
public void testThenAccept_exceptionalCompletion() { |
1458 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1459 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1460 |
> |
{ |
1461 |
> |
final CFException ex = new CFException(); |
1462 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1463 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1464 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
1465 |
> |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1466 |
> |
if (createIncomplete) { |
1467 |
> |
checkIncomplete(g); |
1468 |
> |
f.completeExceptionally(ex); |
1469 |
> |
} |
1470 |
|
|
1471 |
< |
r = new AddAction(); |
1472 |
< |
f = new CompletableFuture<Integer>(); |
1473 |
< |
f2 = new CompletableFuture<Integer>(); |
1474 |
< |
g = f.thenAcceptBothAsync(f2, r); |
1338 |
< |
f.complete(one); |
1339 |
< |
f2.completeExceptionally(new CFException()); |
1340 |
< |
checkCompletedWithWrappedCFException(g); |
1341 |
< |
} |
1471 |
> |
checkCompletedWithWrappedException(g, ex); |
1472 |
> |
checkCompletedExceptionally(f, ex); |
1473 |
> |
r.assertNotInvoked(); |
1474 |
> |
}} |
1475 |
|
|
1476 |
|
/** |
1477 |
< |
* thenAcceptBothAsync result completes exceptionally if action does |
1345 |
< |
*/ |
1346 |
< |
public void testThenAcceptBothAsync3() { |
1347 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1348 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1349 |
< |
FailingBiConsumer r = new FailingBiConsumer(); |
1350 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1351 |
< |
f.complete(one); |
1352 |
< |
checkIncomplete(g); |
1353 |
< |
f2.complete(two); |
1354 |
< |
checkCompletedWithWrappedCFException(g); |
1355 |
< |
} |
1356 |
< |
|
1357 |
< |
/** |
1358 |
< |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
1477 |
> |
* thenAccept result completes exceptionally if source cancelled |
1478 |
|
*/ |
1479 |
< |
public void testThenAcceptBothAsync4() { |
1480 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1481 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1482 |
< |
AddAction r = new AddAction(); |
1483 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r); |
1484 |
< |
assertTrue(f.cancel(true)); |
1485 |
< |
f2.complete(two); |
1486 |
< |
checkCompletedWithWrappedCancellationException(g); |
1479 |
> |
public void testThenAccept_sourceCancelled() { |
1480 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1481 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1482 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1483 |
> |
{ |
1484 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1485 |
> |
final NoopConsumer r = new NoopConsumer(m); |
1486 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
1487 |
> |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1488 |
> |
if (createIncomplete) { |
1489 |
> |
checkIncomplete(g); |
1490 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
1491 |
> |
} |
1492 |
|
|
1369 |
– |
r = new AddAction(); |
1370 |
– |
f = new CompletableFuture<Integer>(); |
1371 |
– |
f2 = new CompletableFuture<Integer>(); |
1372 |
– |
g = f.thenAcceptBothAsync(f2, r); |
1373 |
– |
f.complete(one); |
1374 |
– |
assertTrue(f2.cancel(true)); |
1493 |
|
checkCompletedWithWrappedCancellationException(g); |
1494 |
< |
} |
1495 |
< |
|
1496 |
< |
/** |
1379 |
< |
* runAfterBothAsync result completes normally after normal |
1380 |
< |
* completion of sources |
1381 |
< |
*/ |
1382 |
< |
public void testRunAfterBothAsync() { |
1383 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1384 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1385 |
< |
Noop r = new Noop(); |
1386 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1387 |
< |
f.complete(one); |
1388 |
< |
checkIncomplete(g); |
1389 |
< |
f2.complete(two); |
1390 |
< |
checkCompletedNormally(g, null); |
1391 |
< |
assertTrue(r.ran); |
1392 |
< |
} |
1494 |
> |
checkCancelled(f); |
1495 |
> |
r.assertNotInvoked(); |
1496 |
> |
}} |
1497 |
|
|
1498 |
|
/** |
1499 |
< |
* runAfterBothAsync result completes exceptionally after exceptional |
1396 |
< |
* completion of source |
1499 |
> |
* thenAccept result completes exceptionally if action does |
1500 |
|
*/ |
1501 |
< |
public void testRunAfterBothAsync2() { |
1502 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1503 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1504 |
< |
Noop r = new Noop(); |
1505 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1506 |
< |
f.completeExceptionally(new CFException()); |
1507 |
< |
f2.complete(two); |
1508 |
< |
checkCompletedWithWrappedCFException(g); |
1509 |
< |
|
1510 |
< |
r = new Noop(); |
1511 |
< |
f = new CompletableFuture<Integer>(); |
1512 |
< |
f2 = new CompletableFuture<Integer>(); |
1513 |
< |
g = f.runAfterBothAsync(f2, r); |
1411 |
< |
f.complete(one); |
1412 |
< |
f2.completeExceptionally(new CFException()); |
1413 |
< |
checkCompletedWithWrappedCFException(g); |
1414 |
< |
} |
1501 |
> |
public void testThenAccept_actionFailed() { |
1502 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1503 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1504 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1505 |
> |
{ |
1506 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1507 |
> |
final FailingConsumer r = new FailingConsumer(m); |
1508 |
> |
if (!createIncomplete) f.complete(v1); |
1509 |
> |
final CompletableFuture<Void> g = m.thenAccept(f, r); |
1510 |
> |
if (createIncomplete) { |
1511 |
> |
checkIncomplete(g); |
1512 |
> |
f.complete(v1); |
1513 |
> |
} |
1514 |
|
|
1416 |
– |
/** |
1417 |
– |
* runAfterBothAsync result completes exceptionally if action does |
1418 |
– |
*/ |
1419 |
– |
public void testRunAfterBothAsync3() { |
1420 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1421 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1422 |
– |
FailingNoop r = new FailingNoop(); |
1423 |
– |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1424 |
– |
f.complete(one); |
1425 |
– |
checkIncomplete(g); |
1426 |
– |
f2.complete(two); |
1515 |
|
checkCompletedWithWrappedCFException(g); |
1516 |
< |
} |
1516 |
> |
checkCompletedNormally(f, v1); |
1517 |
> |
}} |
1518 |
|
|
1519 |
|
/** |
1520 |
< |
* runAfterBothAsync result completes exceptionally if either source cancelled |
1520 |
> |
* thenCombine result completes normally after normal completion |
1521 |
> |
* of sources |
1522 |
|
*/ |
1523 |
< |
public void testRunAfterBothAsync4() { |
1524 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1525 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1526 |
< |
Noop r = new Noop(); |
1527 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r); |
1528 |
< |
assertTrue(f.cancel(true)); |
1529 |
< |
f2.complete(two); |
1530 |
< |
checkCompletedWithWrappedCancellationException(g); |
1523 |
> |
public void testThenCombine_normalCompletion() { |
1524 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1525 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1526 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1527 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1528 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1529 |
> |
{ |
1530 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1531 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1532 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1533 |
> |
|
1534 |
> |
if (fFirst) f.complete(v1); else g.complete(v2); |
1535 |
> |
if (!createIncomplete) |
1536 |
> |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1537 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1538 |
> |
if (createIncomplete) { |
1539 |
> |
checkIncomplete(h); |
1540 |
> |
r.assertNotInvoked(); |
1541 |
> |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1542 |
> |
} |
1543 |
|
|
1544 |
< |
r = new Noop(); |
1545 |
< |
f = new CompletableFuture<Integer>(); |
1546 |
< |
f2 = new CompletableFuture<Integer>(); |
1547 |
< |
g = f.runAfterBothAsync(f2, r); |
1548 |
< |
f.complete(one); |
1447 |
< |
assertTrue(f2.cancel(true)); |
1448 |
< |
checkCompletedWithWrappedCancellationException(g); |
1449 |
< |
} |
1544 |
> |
checkCompletedNormally(h, subtract(v1, v2)); |
1545 |
> |
checkCompletedNormally(f, v1); |
1546 |
> |
checkCompletedNormally(g, v2); |
1547 |
> |
r.assertValue(subtract(v1, v2)); |
1548 |
> |
}} |
1549 |
|
|
1550 |
|
/** |
1551 |
< |
* applyToEitherAsync result completes normally after normal |
1552 |
< |
* completion of sources |
1551 |
> |
* thenCombine result completes exceptionally after exceptional |
1552 |
> |
* completion of either source |
1553 |
|
*/ |
1554 |
< |
public void testApplyToEitherAsync() { |
1555 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1556 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1557 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1558 |
< |
f.complete(one); |
1559 |
< |
checkCompletedNormally(g, two); |
1554 |
> |
public void testThenCombine_exceptionalCompletion() { |
1555 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1556 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1557 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1558 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1559 |
> |
{ |
1560 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1561 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1562 |
> |
final CFException ex = new CFException(); |
1563 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1564 |
> |
|
1565 |
> |
(fFirst ? f : g).complete(v1); |
1566 |
> |
if (!createIncomplete) |
1567 |
> |
(!fFirst ? f : g).completeExceptionally(ex); |
1568 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1569 |
> |
if (createIncomplete) { |
1570 |
> |
checkIncomplete(h); |
1571 |
> |
(!fFirst ? f : g).completeExceptionally(ex); |
1572 |
> |
} |
1573 |
|
|
1574 |
< |
f = new CompletableFuture<Integer>(); |
1575 |
< |
f.complete(one); |
1576 |
< |
f2 = new CompletableFuture<Integer>(); |
1577 |
< |
g = f.applyToEitherAsync(f2, inc); |
1578 |
< |
checkCompletedNormally(g, two); |
1467 |
< |
} |
1574 |
> |
checkCompletedWithWrappedException(h, ex); |
1575 |
> |
r.assertNotInvoked(); |
1576 |
> |
checkCompletedNormally(fFirst ? f : g, v1); |
1577 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1578 |
> |
}} |
1579 |
|
|
1580 |
|
/** |
1581 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
1471 |
< |
* completion of source |
1581 |
> |
* thenCombine result completes exceptionally if either source cancelled |
1582 |
|
*/ |
1583 |
< |
public void testApplyToEitherAsync2() { |
1584 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1585 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1586 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1587 |
< |
f.completeExceptionally(new CFException()); |
1588 |
< |
checkCompletedWithWrappedCFException(g); |
1583 |
> |
public void testThenCombine_sourceCancelled() { |
1584 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1585 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1586 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1587 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1588 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1589 |
> |
{ |
1590 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1591 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1592 |
> |
final SubtractFunction r = new SubtractFunction(m); |
1593 |
> |
|
1594 |
> |
(fFirst ? f : g).complete(v1); |
1595 |
> |
if (!createIncomplete) |
1596 |
> |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1597 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1598 |
> |
if (createIncomplete) { |
1599 |
> |
checkIncomplete(h); |
1600 |
> |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1601 |
> |
} |
1602 |
|
|
1603 |
< |
f = new CompletableFuture<Integer>(); |
1604 |
< |
f2 = new CompletableFuture<Integer>(); |
1605 |
< |
f2.completeExceptionally(new CFException()); |
1606 |
< |
g = f.applyToEitherAsync(f2, inc); |
1607 |
< |
f.complete(one); |
1485 |
< |
checkCompletedWithWrappedCFException(g); |
1486 |
< |
} |
1603 |
> |
checkCompletedWithWrappedCancellationException(h); |
1604 |
> |
checkCancelled(!fFirst ? f : g); |
1605 |
> |
r.assertNotInvoked(); |
1606 |
> |
checkCompletedNormally(fFirst ? f : g, v1); |
1607 |
> |
}} |
1608 |
|
|
1609 |
|
/** |
1610 |
< |
* applyToEitherAsync result completes exceptionally if action does |
1610 |
> |
* thenCombine result completes exceptionally if action does |
1611 |
|
*/ |
1612 |
< |
public void testApplyToEitherAsync3() { |
1613 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1614 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1615 |
< |
FailingFunction r = new FailingFunction(); |
1616 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r); |
1617 |
< |
f.complete(one); |
1618 |
< |
checkCompletedWithWrappedCFException(g); |
1619 |
< |
} |
1612 |
> |
public void testThenCombine_actionFailed() { |
1613 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1614 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1615 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1616 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1617 |
> |
{ |
1618 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1619 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1620 |
> |
final FailingBiFunction r = new FailingBiFunction(m); |
1621 |
> |
final CompletableFuture<Integer> h = m.thenCombine(f, g, r); |
1622 |
> |
|
1623 |
> |
if (fFirst) { |
1624 |
> |
f.complete(v1); |
1625 |
> |
g.complete(v2); |
1626 |
> |
} else { |
1627 |
> |
g.complete(v2); |
1628 |
> |
f.complete(v1); |
1629 |
> |
} |
1630 |
|
|
1631 |
< |
/** |
1632 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
1633 |
< |
*/ |
1634 |
< |
public void testApplyToEitherAsync4() { |
1504 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1505 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1506 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc); |
1507 |
< |
assertTrue(f.cancel(true)); |
1508 |
< |
checkCompletedWithWrappedCancellationException(g); |
1509 |
< |
|
1510 |
< |
f = new CompletableFuture<Integer>(); |
1511 |
< |
f2 = new CompletableFuture<Integer>(); |
1512 |
< |
assertTrue(f2.cancel(true)); |
1513 |
< |
g = f.applyToEitherAsync(f2, inc); |
1514 |
< |
checkCompletedWithWrappedCancellationException(g); |
1515 |
< |
} |
1631 |
> |
checkCompletedWithWrappedCFException(h); |
1632 |
> |
checkCompletedNormally(f, v1); |
1633 |
> |
checkCompletedNormally(g, v2); |
1634 |
> |
}} |
1635 |
|
|
1636 |
|
/** |
1637 |
< |
* acceptEitherAsync result completes normally after normal |
1637 |
> |
* thenAcceptBoth result completes normally after normal |
1638 |
|
* completion of sources |
1639 |
|
*/ |
1640 |
< |
public void testAcceptEitherAsync() { |
1641 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1642 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1643 |
< |
IncAction r = new IncAction(); |
1644 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1645 |
< |
f.complete(one); |
1646 |
< |
checkCompletedNormally(g, null); |
1647 |
< |
assertEquals(r.value, 2); |
1640 |
> |
public void testThenAcceptBoth_normalCompletion() { |
1641 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1642 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1643 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1644 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1645 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1646 |
> |
{ |
1647 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1648 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1649 |
> |
final SubtractAction r = new SubtractAction(m); |
1650 |
> |
|
1651 |
> |
if (fFirst) f.complete(v1); else g.complete(v2); |
1652 |
> |
if (!createIncomplete) |
1653 |
> |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1654 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1655 |
> |
if (createIncomplete) { |
1656 |
> |
checkIncomplete(h); |
1657 |
> |
r.assertNotInvoked(); |
1658 |
> |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1659 |
> |
} |
1660 |
|
|
1661 |
< |
r = new IncAction(); |
1662 |
< |
f = new CompletableFuture<Integer>(); |
1663 |
< |
f.complete(one); |
1664 |
< |
f2 = new CompletableFuture<Integer>(); |
1665 |
< |
g = f.acceptEitherAsync(f2, r); |
1535 |
< |
checkCompletedNormally(g, null); |
1536 |
< |
assertEquals(r.value, 2); |
1537 |
< |
} |
1661 |
> |
checkCompletedNormally(h, null); |
1662 |
> |
r.assertValue(subtract(v1, v2)); |
1663 |
> |
checkCompletedNormally(f, v1); |
1664 |
> |
checkCompletedNormally(g, v2); |
1665 |
> |
}} |
1666 |
|
|
1667 |
|
/** |
1668 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
1669 |
< |
* completion of source |
1668 |
> |
* thenAcceptBoth result completes exceptionally after exceptional |
1669 |
> |
* completion of either source |
1670 |
|
*/ |
1671 |
< |
public void testAcceptEitherAsync2() { |
1672 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1673 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1674 |
< |
IncAction r = new IncAction(); |
1675 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1676 |
< |
f.completeExceptionally(new CFException()); |
1677 |
< |
checkCompletedWithWrappedCFException(g); |
1678 |
< |
|
1679 |
< |
r = new IncAction(); |
1680 |
< |
f = new CompletableFuture<Integer>(); |
1681 |
< |
f2 = new CompletableFuture<Integer>(); |
1682 |
< |
f2.completeExceptionally(new CFException()); |
1683 |
< |
g = f.acceptEitherAsync(f2, r); |
1684 |
< |
f.complete(one); |
1685 |
< |
checkCompletedWithWrappedCFException(g); |
1686 |
< |
} |
1671 |
> |
public void testThenAcceptBoth_exceptionalCompletion() { |
1672 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1673 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1674 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1675 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1676 |
> |
{ |
1677 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1678 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1679 |
> |
final CFException ex = new CFException(); |
1680 |
> |
final SubtractAction r = new SubtractAction(m); |
1681 |
> |
|
1682 |
> |
(fFirst ? f : g).complete(v1); |
1683 |
> |
if (!createIncomplete) |
1684 |
> |
(!fFirst ? f : g).completeExceptionally(ex); |
1685 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1686 |
> |
if (createIncomplete) { |
1687 |
> |
checkIncomplete(h); |
1688 |
> |
(!fFirst ? f : g).completeExceptionally(ex); |
1689 |
> |
} |
1690 |
|
|
1691 |
< |
/** |
1692 |
< |
* acceptEitherAsync result completes exceptionally if action does |
1693 |
< |
*/ |
1694 |
< |
public void testAcceptEitherAsync3() { |
1695 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1565 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1566 |
< |
FailingConsumer r = new FailingConsumer(); |
1567 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1568 |
< |
f.complete(one); |
1569 |
< |
checkCompletedWithWrappedCFException(g); |
1570 |
< |
} |
1691 |
> |
checkCompletedWithWrappedException(h, ex); |
1692 |
> |
r.assertNotInvoked(); |
1693 |
> |
checkCompletedNormally(fFirst ? f : g, v1); |
1694 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1695 |
> |
}} |
1696 |
|
|
1697 |
|
/** |
1698 |
< |
* acceptEitherAsync result completes exceptionally if either |
1574 |
< |
* source cancelled |
1698 |
> |
* thenAcceptBoth result completes exceptionally if either source cancelled |
1699 |
|
*/ |
1700 |
< |
public void testAcceptEitherAsync4() { |
1701 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1702 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1703 |
< |
IncAction r = new IncAction(); |
1704 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r); |
1705 |
< |
assertTrue(f.cancel(true)); |
1706 |
< |
checkCompletedWithWrappedCancellationException(g); |
1700 |
> |
public void testThenAcceptBoth_sourceCancelled() { |
1701 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1702 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1703 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1704 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1705 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1706 |
> |
{ |
1707 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1708 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1709 |
> |
final SubtractAction r = new SubtractAction(m); |
1710 |
> |
|
1711 |
> |
(fFirst ? f : g).complete(v1); |
1712 |
> |
if (!createIncomplete) |
1713 |
> |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1714 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1715 |
> |
if (createIncomplete) { |
1716 |
> |
checkIncomplete(h); |
1717 |
> |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1718 |
> |
} |
1719 |
|
|
1720 |
< |
r = new IncAction(); |
1721 |
< |
f = new CompletableFuture<Integer>(); |
1722 |
< |
f2 = new CompletableFuture<Integer>(); |
1723 |
< |
assertTrue(f2.cancel(true)); |
1724 |
< |
g = f.acceptEitherAsync(f2, r); |
1589 |
< |
checkCompletedWithWrappedCancellationException(g); |
1590 |
< |
} |
1720 |
> |
checkCompletedWithWrappedCancellationException(h); |
1721 |
> |
checkCancelled(!fFirst ? f : g); |
1722 |
> |
r.assertNotInvoked(); |
1723 |
> |
checkCompletedNormally(fFirst ? f : g, v1); |
1724 |
> |
}} |
1725 |
|
|
1726 |
|
/** |
1727 |
< |
* runAfterEitherAsync result completes normally after normal |
1594 |
< |
* completion of sources |
1727 |
> |
* thenAcceptBoth result completes exceptionally if action does |
1728 |
|
*/ |
1729 |
< |
public void testRunAfterEitherAsync() { |
1730 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1731 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1732 |
< |
Noop r = new Noop(); |
1733 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1734 |
< |
f.complete(one); |
1735 |
< |
checkCompletedNormally(g, null); |
1736 |
< |
assertTrue(r.ran); |
1729 |
> |
public void testThenAcceptBoth_actionFailed() { |
1730 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1731 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1732 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1733 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1734 |
> |
{ |
1735 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1736 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1737 |
> |
final FailingBiConsumer r = new FailingBiConsumer(m); |
1738 |
> |
final CompletableFuture<Void> h = m.thenAcceptBoth(f, g, r); |
1739 |
> |
|
1740 |
> |
if (fFirst) { |
1741 |
> |
f.complete(v1); |
1742 |
> |
g.complete(v2); |
1743 |
> |
} else { |
1744 |
> |
g.complete(v2); |
1745 |
> |
f.complete(v1); |
1746 |
> |
} |
1747 |
|
|
1748 |
< |
r = new Noop(); |
1749 |
< |
f = new CompletableFuture<Integer>(); |
1750 |
< |
f.complete(one); |
1751 |
< |
f2 = new CompletableFuture<Integer>(); |
1609 |
< |
g = f.runAfterEitherAsync(f2, r); |
1610 |
< |
checkCompletedNormally(g, null); |
1611 |
< |
assertTrue(r.ran); |
1612 |
< |
} |
1748 |
> |
checkCompletedWithWrappedCFException(h); |
1749 |
> |
checkCompletedNormally(f, v1); |
1750 |
> |
checkCompletedNormally(g, v2); |
1751 |
> |
}} |
1752 |
|
|
1753 |
|
/** |
1754 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
1755 |
< |
* completion of source |
1754 |
> |
* runAfterBoth result completes normally after normal |
1755 |
> |
* completion of sources |
1756 |
|
*/ |
1757 |
< |
public void testRunAfterEitherAsync2() { |
1758 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1759 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1760 |
< |
Noop r = new Noop(); |
1761 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1762 |
< |
f.completeExceptionally(new CFException()); |
1763 |
< |
checkCompletedWithWrappedCFException(g); |
1757 |
> |
public void testRunAfterBoth_normalCompletion() { |
1758 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1759 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1760 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1761 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1762 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1763 |
> |
{ |
1764 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1765 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1766 |
> |
final Noop r = new Noop(m); |
1767 |
> |
|
1768 |
> |
if (fFirst) f.complete(v1); else g.complete(v2); |
1769 |
> |
if (!createIncomplete) |
1770 |
> |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1771 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1772 |
> |
if (createIncomplete) { |
1773 |
> |
checkIncomplete(h); |
1774 |
> |
r.assertNotInvoked(); |
1775 |
> |
if (!fFirst) f.complete(v1); else g.complete(v2); |
1776 |
> |
} |
1777 |
|
|
1778 |
< |
r = new Noop(); |
1779 |
< |
f = new CompletableFuture<Integer>(); |
1780 |
< |
f2 = new CompletableFuture<Integer>(); |
1781 |
< |
f2.completeExceptionally(new CFException()); |
1782 |
< |
g = f.runAfterEitherAsync(f2, r); |
1631 |
< |
f.complete(one); |
1632 |
< |
checkCompletedWithWrappedCFException(g); |
1633 |
< |
} |
1778 |
> |
checkCompletedNormally(h, null); |
1779 |
> |
r.assertInvoked(); |
1780 |
> |
checkCompletedNormally(f, v1); |
1781 |
> |
checkCompletedNormally(g, v2); |
1782 |
> |
}} |
1783 |
|
|
1784 |
|
/** |
1785 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
1786 |
< |
*/ |
1638 |
< |
public void testRunAfterEitherAsync3() { |
1639 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1640 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1641 |
< |
FailingNoop r = new FailingNoop(); |
1642 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1643 |
< |
f.complete(one); |
1644 |
< |
checkCompletedWithWrappedCFException(g); |
1645 |
< |
} |
1646 |
< |
|
1647 |
< |
/** |
1648 |
< |
* runAfterEitherAsync result completes exceptionally if either |
1649 |
< |
* source cancelled |
1785 |
> |
* runAfterBoth result completes exceptionally after exceptional |
1786 |
> |
* completion of either source |
1787 |
|
*/ |
1788 |
< |
public void testRunAfterEitherAsync4() { |
1789 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1790 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1791 |
< |
Noop r = new Noop(); |
1792 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r); |
1793 |
< |
assertTrue(f.cancel(true)); |
1794 |
< |
checkCompletedWithWrappedCancellationException(g); |
1795 |
< |
|
1796 |
< |
r = new Noop(); |
1797 |
< |
f = new CompletableFuture<Integer>(); |
1798 |
< |
f2 = new CompletableFuture<Integer>(); |
1799 |
< |
assertTrue(f2.cancel(true)); |
1800 |
< |
g = f.runAfterEitherAsync(f2, r); |
1801 |
< |
checkCompletedWithWrappedCancellationException(g); |
1802 |
< |
} |
1788 |
> |
public void testRunAfterBoth_exceptionalCompletion() { |
1789 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1790 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1791 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1792 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1793 |
> |
{ |
1794 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1795 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1796 |
> |
final CFException ex = new CFException(); |
1797 |
> |
final Noop r = new Noop(m); |
1798 |
> |
|
1799 |
> |
(fFirst ? f : g).complete(v1); |
1800 |
> |
if (!createIncomplete) |
1801 |
> |
(!fFirst ? f : g).completeExceptionally(ex); |
1802 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1803 |
> |
if (createIncomplete) { |
1804 |
> |
checkIncomplete(h); |
1805 |
> |
(!fFirst ? f : g).completeExceptionally(ex); |
1806 |
> |
} |
1807 |
|
|
1808 |
< |
/** |
1809 |
< |
* thenComposeAsync result completes normally after normal |
1810 |
< |
* completion of source |
1811 |
< |
*/ |
1812 |
< |
public void testThenComposeAsync() { |
1672 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1673 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1674 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1675 |
< |
f.complete(one); |
1676 |
< |
checkCompletedNormally(g, two); |
1677 |
< |
} |
1808 |
> |
checkCompletedWithWrappedException(h, ex); |
1809 |
> |
r.assertNotInvoked(); |
1810 |
> |
checkCompletedNormally(fFirst ? f : g, v1); |
1811 |
> |
checkCompletedExceptionally(!fFirst ? f : g, ex); |
1812 |
> |
}} |
1813 |
|
|
1814 |
|
/** |
1815 |
< |
* thenComposeAsync result completes exceptionally after |
1681 |
< |
* exceptional completion of source |
1815 |
> |
* runAfterBoth result completes exceptionally if either source cancelled |
1816 |
|
*/ |
1817 |
< |
public void testThenComposeAsync2() { |
1818 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1819 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1820 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1821 |
< |
f.completeExceptionally(new CFException()); |
1822 |
< |
checkCompletedWithWrappedCFException(g); |
1823 |
< |
} |
1817 |
> |
public void testRunAfterBoth_sourceCancelled() { |
1818 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1819 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
1820 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
1821 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1822 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1823 |
> |
{ |
1824 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1825 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1826 |
> |
final Noop r = new Noop(m); |
1827 |
> |
|
1828 |
> |
(fFirst ? f : g).complete(v1); |
1829 |
> |
if (!createIncomplete) |
1830 |
> |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1831 |
> |
final CompletableFuture<Void> h = m.runAfterBoth(f, g, r); |
1832 |
> |
if (createIncomplete) { |
1833 |
> |
checkIncomplete(h); |
1834 |
> |
assertTrue((!fFirst ? f : g).cancel(mayInterruptIfRunning)); |
1835 |
> |
} |
1836 |
|
|
1837 |
< |
/** |
1838 |
< |
* thenComposeAsync result completes exceptionally if action does |
1839 |
< |
*/ |
1840 |
< |
public void testThenComposeAsync3() { |
1841 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1696 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
1697 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1698 |
< |
f.complete(one); |
1699 |
< |
checkCompletedWithWrappedCFException(g); |
1700 |
< |
} |
1837 |
> |
checkCompletedWithWrappedCancellationException(h); |
1838 |
> |
checkCancelled(!fFirst ? f : g); |
1839 |
> |
r.assertNotInvoked(); |
1840 |
> |
checkCompletedNormally(fFirst ? f : g, v1); |
1841 |
> |
}} |
1842 |
|
|
1843 |
|
/** |
1844 |
< |
* thenComposeAsync result completes exceptionally if source cancelled |
1844 |
> |
* runAfterBoth result completes exceptionally if action does |
1845 |
|
*/ |
1846 |
< |
public void testThenComposeAsync4() { |
1847 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1848 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
1849 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r); |
1850 |
< |
assertTrue(f.cancel(true)); |
1851 |
< |
checkCompletedWithWrappedCancellationException(g); |
1852 |
< |
} |
1853 |
< |
|
1846 |
> |
public void testRunAfterBoth_actionFailed() { |
1847 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1848 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1849 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1850 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1851 |
> |
{ |
1852 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1853 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1854 |
> |
final FailingRunnable r1 = new FailingRunnable(m); |
1855 |
> |
final FailingRunnable r2 = new FailingRunnable(m); |
1856 |
> |
|
1857 |
> |
CompletableFuture<Void> h1 = m.runAfterBoth(f, g, r1); |
1858 |
> |
if (fFirst) { |
1859 |
> |
f.complete(v1); |
1860 |
> |
g.complete(v2); |
1861 |
> |
} else { |
1862 |
> |
g.complete(v2); |
1863 |
> |
f.complete(v1); |
1864 |
> |
} |
1865 |
> |
CompletableFuture<Void> h2 = m.runAfterBoth(f, g, r2); |
1866 |
|
|
1867 |
< |
// async with explicit executors |
1867 |
> |
checkCompletedWithWrappedCFException(h1); |
1868 |
> |
checkCompletedWithWrappedCFException(h2); |
1869 |
> |
checkCompletedNormally(f, v1); |
1870 |
> |
checkCompletedNormally(g, v2); |
1871 |
> |
}} |
1872 |
|
|
1873 |
|
/** |
1874 |
< |
* thenRunAsync result completes normally after normal completion of source |
1874 |
> |
* applyToEither result completes normally after normal completion |
1875 |
> |
* of either source |
1876 |
|
*/ |
1877 |
< |
public void testThenRunAsyncE() { |
1878 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1879 |
< |
Noop r = new Noop(); |
1880 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1881 |
< |
f.complete(null); |
1882 |
< |
checkCompletedNormally(g, null); |
1883 |
< |
|
1884 |
< |
// reordered version |
1885 |
< |
f = new CompletableFuture<Integer>(); |
1886 |
< |
f.complete(null); |
1887 |
< |
r = new Noop(); |
1888 |
< |
g = f.thenRunAsync(r, new ThreadExecutor()); |
1889 |
< |
checkCompletedNormally(g, null); |
1890 |
< |
} |
1877 |
> |
public void testApplyToEither_normalCompletion() { |
1878 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1879 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1880 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
1881 |
> |
{ |
1882 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1883 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1884 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1885 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1886 |
> |
|
1887 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1888 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1889 |
> |
checkIncomplete(h0); |
1890 |
> |
checkIncomplete(h1); |
1891 |
> |
rs[0].assertNotInvoked(); |
1892 |
> |
rs[1].assertNotInvoked(); |
1893 |
> |
f.complete(v1); |
1894 |
> |
checkCompletedNormally(h0, inc(v1)); |
1895 |
> |
checkCompletedNormally(h1, inc(v1)); |
1896 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1897 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1898 |
> |
checkCompletedNormally(h2, inc(v1)); |
1899 |
> |
checkCompletedNormally(h3, inc(v1)); |
1900 |
> |
g.complete(v2); |
1901 |
> |
|
1902 |
> |
// unspecified behavior - both source completions available |
1903 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
1904 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
1905 |
> |
rs[4].assertValue(h4.join()); |
1906 |
> |
rs[5].assertValue(h5.join()); |
1907 |
> |
assertTrue(Objects.equals(inc(v1), h4.join()) || |
1908 |
> |
Objects.equals(inc(v2), h4.join())); |
1909 |
> |
assertTrue(Objects.equals(inc(v1), h5.join()) || |
1910 |
> |
Objects.equals(inc(v2), h5.join())); |
1911 |
> |
|
1912 |
> |
checkCompletedNormally(f, v1); |
1913 |
> |
checkCompletedNormally(g, v2); |
1914 |
> |
checkCompletedNormally(h0, inc(v1)); |
1915 |
> |
checkCompletedNormally(h1, inc(v1)); |
1916 |
> |
checkCompletedNormally(h2, inc(v1)); |
1917 |
> |
checkCompletedNormally(h3, inc(v1)); |
1918 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(inc(v1)); |
1919 |
> |
}} |
1920 |
|
|
1921 |
|
/** |
1922 |
< |
* thenRunAsync result completes exceptionally after exceptional |
1923 |
< |
* completion of source |
1922 |
> |
* applyToEither result completes exceptionally after exceptional |
1923 |
> |
* completion of either source |
1924 |
|
*/ |
1925 |
< |
public void testThenRunAsync2E() { |
1926 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1927 |
< |
Noop r = new Noop(); |
1928 |
< |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1929 |
< |
f.completeExceptionally(new CFException()); |
1925 |
> |
public void testApplyToEither_exceptionalCompletion() { |
1926 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1927 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1928 |
> |
{ |
1929 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1930 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1931 |
> |
final CFException ex = new CFException(); |
1932 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1933 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1934 |
> |
|
1935 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1936 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1937 |
> |
checkIncomplete(h0); |
1938 |
> |
checkIncomplete(h1); |
1939 |
> |
rs[0].assertNotInvoked(); |
1940 |
> |
rs[1].assertNotInvoked(); |
1941 |
> |
f.completeExceptionally(ex); |
1942 |
> |
checkCompletedWithWrappedException(h0, ex); |
1943 |
> |
checkCompletedWithWrappedException(h1, ex); |
1944 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1945 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
1946 |
> |
checkCompletedWithWrappedException(h2, ex); |
1947 |
> |
checkCompletedWithWrappedException(h3, ex); |
1948 |
> |
g.complete(v1); |
1949 |
> |
|
1950 |
> |
// unspecified behavior - both source completions available |
1951 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
1952 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
1953 |
|
try { |
1954 |
< |
g.join(); |
1955 |
< |
shouldThrow(); |
1956 |
< |
} catch (Exception ok) { |
1954 |
> |
assertEquals(inc(v1), h4.join()); |
1955 |
> |
rs[4].assertValue(inc(v1)); |
1956 |
> |
} catch (CompletionException ok) { |
1957 |
> |
checkCompletedWithWrappedException(h4, ex); |
1958 |
> |
rs[4].assertNotInvoked(); |
1959 |
> |
} |
1960 |
> |
try { |
1961 |
> |
assertEquals(inc(v1), h5.join()); |
1962 |
> |
rs[5].assertValue(inc(v1)); |
1963 |
> |
} catch (CompletionException ok) { |
1964 |
> |
checkCompletedWithWrappedException(h5, ex); |
1965 |
> |
rs[5].assertNotInvoked(); |
1966 |
|
} |
1748 |
– |
checkCompletedWithWrappedCFException(g); |
1749 |
– |
} |
1750 |
– |
|
1751 |
– |
/** |
1752 |
– |
* thenRunAsync result completes exceptionally if action does |
1753 |
– |
*/ |
1754 |
– |
public void testThenRunAsync3E() { |
1755 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1756 |
– |
FailingNoop r = new FailingNoop(); |
1757 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1758 |
– |
f.complete(null); |
1759 |
– |
checkCompletedWithWrappedCFException(g); |
1760 |
– |
} |
1761 |
– |
|
1762 |
– |
/** |
1763 |
– |
* thenRunAsync result completes exceptionally if source cancelled |
1764 |
– |
*/ |
1765 |
– |
public void testThenRunAsync4E() { |
1766 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1767 |
– |
Noop r = new Noop(); |
1768 |
– |
CompletableFuture<Void> g = f.thenRunAsync(r, new ThreadExecutor()); |
1769 |
– |
assertTrue(f.cancel(true)); |
1770 |
– |
checkCompletedWithWrappedCancellationException(g); |
1771 |
– |
} |
1772 |
– |
|
1773 |
– |
/** |
1774 |
– |
* thenApplyAsync result completes normally after normal completion of source |
1775 |
– |
*/ |
1776 |
– |
public void testThenApplyAsyncE() { |
1777 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1778 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
1779 |
– |
f.complete(one); |
1780 |
– |
checkCompletedNormally(g, two); |
1781 |
– |
} |
1782 |
– |
|
1783 |
– |
/** |
1784 |
– |
* thenApplyAsync result completes exceptionally after exceptional |
1785 |
– |
* completion of source |
1786 |
– |
*/ |
1787 |
– |
public void testThenApplyAsync2E() { |
1788 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1789 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
1790 |
– |
f.completeExceptionally(new CFException()); |
1791 |
– |
checkCompletedWithWrappedCFException(g); |
1792 |
– |
} |
1793 |
– |
|
1794 |
– |
/** |
1795 |
– |
* thenApplyAsync result completes exceptionally if action does |
1796 |
– |
*/ |
1797 |
– |
public void testThenApplyAsync3E() { |
1798 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1799 |
– |
FailingFunction r = new FailingFunction(); |
1800 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(r, new ThreadExecutor()); |
1801 |
– |
f.complete(null); |
1802 |
– |
checkCompletedWithWrappedCFException(g); |
1803 |
– |
} |
1804 |
– |
|
1805 |
– |
/** |
1806 |
– |
* thenApplyAsync result completes exceptionally if source cancelled |
1807 |
– |
*/ |
1808 |
– |
public void testThenApplyAsync4E() { |
1809 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1810 |
– |
CompletableFuture<Integer> g = f.thenApplyAsync(inc, new ThreadExecutor()); |
1811 |
– |
assertTrue(f.cancel(true)); |
1812 |
– |
checkCompletedWithWrappedCancellationException(g); |
1813 |
– |
} |
1814 |
– |
|
1815 |
– |
/** |
1816 |
– |
* thenAcceptAsync result completes normally after normal |
1817 |
– |
* completion of source |
1818 |
– |
*/ |
1819 |
– |
public void testThenAcceptAsyncE() { |
1820 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1821 |
– |
IncAction r = new IncAction(); |
1822 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
1823 |
– |
f.complete(one); |
1824 |
– |
checkCompletedNormally(g, null); |
1825 |
– |
assertEquals(r.value, 2); |
1826 |
– |
} |
1827 |
– |
|
1828 |
– |
/** |
1829 |
– |
* thenAcceptAsync result completes exceptionally after exceptional |
1830 |
– |
* completion of source |
1831 |
– |
*/ |
1832 |
– |
public void testThenAcceptAsync2E() { |
1833 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1834 |
– |
IncAction r = new IncAction(); |
1835 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
1836 |
– |
f.completeExceptionally(new CFException()); |
1837 |
– |
checkCompletedWithWrappedCFException(g); |
1838 |
– |
} |
1839 |
– |
|
1840 |
– |
/** |
1841 |
– |
* thenAcceptAsync result completes exceptionally if action does |
1842 |
– |
*/ |
1843 |
– |
public void testThenAcceptAsync3E() { |
1844 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1845 |
– |
FailingConsumer r = new FailingConsumer(); |
1846 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
1847 |
– |
f.complete(null); |
1848 |
– |
checkCompletedWithWrappedCFException(g); |
1849 |
– |
} |
1850 |
– |
|
1851 |
– |
/** |
1852 |
– |
* thenAcceptAsync result completes exceptionally if source cancelled |
1853 |
– |
*/ |
1854 |
– |
public void testThenAcceptAsync4E() { |
1855 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1856 |
– |
IncAction r = new IncAction(); |
1857 |
– |
CompletableFuture<Void> g = f.thenAcceptAsync(r, new ThreadExecutor()); |
1858 |
– |
assertTrue(f.cancel(true)); |
1859 |
– |
checkCompletedWithWrappedCancellationException(g); |
1860 |
– |
} |
1861 |
– |
/** |
1862 |
– |
* thenCombineAsync result completes normally after normal |
1863 |
– |
* completion of sources |
1864 |
– |
*/ |
1865 |
– |
public void testThenCombineAsyncE() { |
1866 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1867 |
– |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1868 |
– |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1869 |
– |
f.complete(one); |
1870 |
– |
checkIncomplete(g); |
1871 |
– |
f2.complete(two); |
1872 |
– |
checkCompletedNormally(g, three); |
1873 |
– |
} |
1967 |
|
|
1968 |
< |
/** |
1969 |
< |
* thenCombineAsync result completes exceptionally after exceptional |
1970 |
< |
* completion of source |
1971 |
< |
*/ |
1972 |
< |
public void testThenCombineAsync2E() { |
1973 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1974 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1975 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
1976 |
< |
f.completeExceptionally(new CFException()); |
1977 |
< |
f2.complete(two); |
1978 |
< |
checkCompletedWithWrappedCFException(g); |
1968 |
> |
checkCompletedExceptionally(f, ex); |
1969 |
> |
checkCompletedNormally(g, v1); |
1970 |
> |
checkCompletedWithWrappedException(h0, ex); |
1971 |
> |
checkCompletedWithWrappedException(h1, ex); |
1972 |
> |
checkCompletedWithWrappedException(h2, ex); |
1973 |
> |
checkCompletedWithWrappedException(h3, ex); |
1974 |
> |
checkCompletedWithWrappedException(h4, ex); |
1975 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
1976 |
> |
}} |
1977 |
> |
|
1978 |
> |
public void testApplyToEither_exceptionalCompletion2() { |
1979 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
1980 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
1981 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
1982 |
> |
{ |
1983 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
1984 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
1985 |
> |
final CFException ex = new CFException(); |
1986 |
> |
final IncFunction[] rs = new IncFunction[6]; |
1987 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
1988 |
> |
|
1989 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
1990 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
1991 |
> |
if (fFirst) { |
1992 |
> |
f.complete(v1); |
1993 |
> |
g.completeExceptionally(ex); |
1994 |
> |
} else { |
1995 |
> |
g.completeExceptionally(ex); |
1996 |
> |
f.complete(v1); |
1997 |
> |
} |
1998 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
1999 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2000 |
|
|
2001 |
< |
f = new CompletableFuture<Integer>(); |
2002 |
< |
f2 = new CompletableFuture<Integer>(); |
2003 |
< |
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
2004 |
< |
f.complete(one); |
2005 |
< |
f2.completeExceptionally(new CFException()); |
2006 |
< |
checkCompletedWithWrappedCFException(g); |
2007 |
< |
} |
2001 |
> |
// unspecified behavior - both source completions available |
2002 |
> |
try { |
2003 |
> |
assertEquals(inc(v1), h0.join()); |
2004 |
> |
rs[0].assertValue(inc(v1)); |
2005 |
> |
} catch (CompletionException ok) { |
2006 |
> |
checkCompletedWithWrappedException(h0, ex); |
2007 |
> |
rs[0].assertNotInvoked(); |
2008 |
> |
} |
2009 |
> |
try { |
2010 |
> |
assertEquals(inc(v1), h1.join()); |
2011 |
> |
rs[1].assertValue(inc(v1)); |
2012 |
> |
} catch (CompletionException ok) { |
2013 |
> |
checkCompletedWithWrappedException(h1, ex); |
2014 |
> |
rs[1].assertNotInvoked(); |
2015 |
> |
} |
2016 |
> |
try { |
2017 |
> |
assertEquals(inc(v1), h2.join()); |
2018 |
> |
rs[2].assertValue(inc(v1)); |
2019 |
> |
} catch (CompletionException ok) { |
2020 |
> |
checkCompletedWithWrappedException(h2, ex); |
2021 |
> |
rs[2].assertNotInvoked(); |
2022 |
> |
} |
2023 |
> |
try { |
2024 |
> |
assertEquals(inc(v1), h3.join()); |
2025 |
> |
rs[3].assertValue(inc(v1)); |
2026 |
> |
} catch (CompletionException ok) { |
2027 |
> |
checkCompletedWithWrappedException(h3, ex); |
2028 |
> |
rs[3].assertNotInvoked(); |
2029 |
> |
} |
2030 |
|
|
2031 |
< |
/** |
2032 |
< |
* thenCombineAsync result completes exceptionally if action does |
2033 |
< |
*/ |
1898 |
< |
public void testThenCombineAsync3E() { |
1899 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1900 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1901 |
< |
FailingBiFunction r = new FailingBiFunction(); |
1902 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, r, new ThreadExecutor()); |
1903 |
< |
f.complete(one); |
1904 |
< |
checkIncomplete(g); |
1905 |
< |
f2.complete(two); |
1906 |
< |
checkCompletedWithWrappedCFException(g); |
1907 |
< |
} |
2031 |
> |
checkCompletedNormally(f, v1); |
2032 |
> |
checkCompletedExceptionally(g, ex); |
2033 |
> |
}} |
2034 |
|
|
2035 |
|
/** |
2036 |
< |
* thenCombineAsync result completes exceptionally if either source cancelled |
2036 |
> |
* applyToEither result completes exceptionally if either source cancelled |
2037 |
|
*/ |
2038 |
< |
public void testThenCombineAsync4E() { |
2039 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2040 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2041 |
< |
CompletableFuture<Integer> g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
2042 |
< |
assertTrue(f.cancel(true)); |
2043 |
< |
f2.complete(two); |
2044 |
< |
checkCompletedWithWrappedCancellationException(g); |
2038 |
> |
public void testApplyToEither_sourceCancelled() { |
2039 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2040 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2041 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2042 |
> |
{ |
2043 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2044 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2045 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2046 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2047 |
> |
|
2048 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2049 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2050 |
> |
checkIncomplete(h0); |
2051 |
> |
checkIncomplete(h1); |
2052 |
> |
rs[0].assertNotInvoked(); |
2053 |
> |
rs[1].assertNotInvoked(); |
2054 |
> |
f.cancel(mayInterruptIfRunning); |
2055 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2056 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2057 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2058 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2059 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2060 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2061 |
> |
g.complete(v1); |
2062 |
> |
|
2063 |
> |
// unspecified behavior - both source completions available |
2064 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2065 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2066 |
> |
try { |
2067 |
> |
assertEquals(inc(v1), h4.join()); |
2068 |
> |
rs[4].assertValue(inc(v1)); |
2069 |
> |
} catch (CompletionException ok) { |
2070 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2071 |
> |
rs[4].assertNotInvoked(); |
2072 |
> |
} |
2073 |
> |
try { |
2074 |
> |
assertEquals(inc(v1), h5.join()); |
2075 |
> |
rs[5].assertValue(inc(v1)); |
2076 |
> |
} catch (CompletionException ok) { |
2077 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2078 |
> |
rs[5].assertNotInvoked(); |
2079 |
> |
} |
2080 |
|
|
2081 |
< |
f = new CompletableFuture<Integer>(); |
2082 |
< |
f2 = new CompletableFuture<Integer>(); |
2083 |
< |
g = f.thenCombineAsync(f2, add, new ThreadExecutor()); |
2084 |
< |
f.complete(one); |
2085 |
< |
assertTrue(f2.cancel(true)); |
2086 |
< |
checkCompletedWithWrappedCancellationException(g); |
2087 |
< |
} |
2081 |
> |
checkCancelled(f); |
2082 |
> |
checkCompletedNormally(g, v1); |
2083 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2084 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2085 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2086 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2087 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2088 |
> |
}} |
2089 |
> |
|
2090 |
> |
public void testApplyToEither_sourceCancelled2() { |
2091 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2092 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2093 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2094 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2095 |
> |
{ |
2096 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2097 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2098 |
> |
final IncFunction[] rs = new IncFunction[6]; |
2099 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new IncFunction(m); |
2100 |
> |
|
2101 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2102 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2103 |
> |
if (fFirst) { |
2104 |
> |
f.complete(v1); |
2105 |
> |
g.cancel(mayInterruptIfRunning); |
2106 |
> |
} else { |
2107 |
> |
g.cancel(mayInterruptIfRunning); |
2108 |
> |
f.complete(v1); |
2109 |
> |
} |
2110 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2111 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2112 |
|
|
2113 |
< |
/** |
2114 |
< |
* thenAcceptBothAsync result completes normally after normal |
2115 |
< |
* completion of sources |
2116 |
< |
*/ |
2117 |
< |
public void testThenAcceptBothAsyncE() { |
2118 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2119 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2120 |
< |
AddAction r = new AddAction(); |
2121 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2122 |
< |
f.complete(one); |
2123 |
< |
checkIncomplete(g); |
2124 |
< |
f2.complete(two); |
2125 |
< |
checkCompletedNormally(g, null); |
2126 |
< |
assertEquals(r.value, 3); |
2127 |
< |
} |
2113 |
> |
// unspecified behavior - both source completions available |
2114 |
> |
try { |
2115 |
> |
assertEquals(inc(v1), h0.join()); |
2116 |
> |
rs[0].assertValue(inc(v1)); |
2117 |
> |
} catch (CompletionException ok) { |
2118 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2119 |
> |
rs[0].assertNotInvoked(); |
2120 |
> |
} |
2121 |
> |
try { |
2122 |
> |
assertEquals(inc(v1), h1.join()); |
2123 |
> |
rs[1].assertValue(inc(v1)); |
2124 |
> |
} catch (CompletionException ok) { |
2125 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2126 |
> |
rs[1].assertNotInvoked(); |
2127 |
> |
} |
2128 |
> |
try { |
2129 |
> |
assertEquals(inc(v1), h2.join()); |
2130 |
> |
rs[2].assertValue(inc(v1)); |
2131 |
> |
} catch (CompletionException ok) { |
2132 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2133 |
> |
rs[2].assertNotInvoked(); |
2134 |
> |
} |
2135 |
> |
try { |
2136 |
> |
assertEquals(inc(v1), h3.join()); |
2137 |
> |
rs[3].assertValue(inc(v1)); |
2138 |
> |
} catch (CompletionException ok) { |
2139 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2140 |
> |
rs[3].assertNotInvoked(); |
2141 |
> |
} |
2142 |
|
|
2143 |
< |
/** |
2144 |
< |
* thenAcceptBothAsync result completes exceptionally after exceptional |
2145 |
< |
* completion of source |
1947 |
< |
*/ |
1948 |
< |
public void testThenAcceptBothAsync2E() { |
1949 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
1950 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
1951 |
< |
AddAction r = new AddAction(); |
1952 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1953 |
< |
f.completeExceptionally(new CFException()); |
1954 |
< |
f2.complete(two); |
1955 |
< |
checkCompletedWithWrappedCFException(g); |
1956 |
< |
|
1957 |
< |
r = new AddAction(); |
1958 |
< |
f = new CompletableFuture<Integer>(); |
1959 |
< |
f2 = new CompletableFuture<Integer>(); |
1960 |
< |
g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
1961 |
< |
f.complete(one); |
1962 |
< |
f2.completeExceptionally(new CFException()); |
1963 |
< |
checkCompletedWithWrappedCFException(g); |
1964 |
< |
} |
2143 |
> |
checkCompletedNormally(f, v1); |
2144 |
> |
checkCancelled(g); |
2145 |
> |
}} |
2146 |
|
|
2147 |
|
/** |
2148 |
< |
* thenAcceptBothAsync result completes exceptionally if action does |
2148 |
> |
* applyToEither result completes exceptionally if action does |
2149 |
|
*/ |
2150 |
< |
public void testThenAcceptBothAsync3E() { |
2151 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2152 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2153 |
< |
FailingBiConsumer r = new FailingBiConsumer(); |
2154 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2155 |
< |
f.complete(one); |
2156 |
< |
checkIncomplete(g); |
2157 |
< |
f2.complete(two); |
2158 |
< |
checkCompletedWithWrappedCFException(g); |
2159 |
< |
} |
2150 |
> |
public void testApplyToEither_actionFailed() { |
2151 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2152 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2153 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2154 |
> |
{ |
2155 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2156 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2157 |
> |
final FailingFunction[] rs = new FailingFunction[6]; |
2158 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingFunction(m); |
2159 |
> |
|
2160 |
> |
final CompletableFuture<Integer> h0 = m.applyToEither(f, g, rs[0]); |
2161 |
> |
final CompletableFuture<Integer> h1 = m.applyToEither(g, f, rs[1]); |
2162 |
> |
f.complete(v1); |
2163 |
> |
final CompletableFuture<Integer> h2 = m.applyToEither(f, g, rs[2]); |
2164 |
> |
final CompletableFuture<Integer> h3 = m.applyToEither(g, f, rs[3]); |
2165 |
> |
checkCompletedWithWrappedCFException(h0); |
2166 |
> |
checkCompletedWithWrappedCFException(h1); |
2167 |
> |
checkCompletedWithWrappedCFException(h2); |
2168 |
> |
checkCompletedWithWrappedCFException(h3); |
2169 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2170 |
> |
|
2171 |
> |
g.complete(v2); |
2172 |
> |
|
2173 |
> |
// unspecified behavior - both source completions available |
2174 |
> |
final CompletableFuture<Integer> h4 = m.applyToEither(f, g, rs[4]); |
2175 |
> |
final CompletableFuture<Integer> h5 = m.applyToEither(g, f, rs[5]); |
2176 |
> |
|
2177 |
> |
checkCompletedWithWrappedCFException(h4); |
2178 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2179 |
> |
Objects.equals(v2, rs[4].value)); |
2180 |
> |
checkCompletedWithWrappedCFException(h5); |
2181 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2182 |
> |
Objects.equals(v2, rs[5].value)); |
2183 |
> |
|
2184 |
> |
checkCompletedNormally(f, v1); |
2185 |
> |
checkCompletedNormally(g, v2); |
2186 |
> |
}} |
2187 |
|
|
2188 |
|
/** |
2189 |
< |
* thenAcceptBothAsync result completes exceptionally if either source cancelled |
2189 |
> |
* acceptEither result completes normally after normal completion |
2190 |
> |
* of either source |
2191 |
|
*/ |
2192 |
< |
public void testThenAcceptBothAsync4E() { |
2193 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2194 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2195 |
< |
AddAction r = new AddAction(); |
2196 |
< |
CompletableFuture<Void> g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2197 |
< |
assertTrue(f.cancel(true)); |
2198 |
< |
f2.complete(two); |
2199 |
< |
checkCompletedWithWrappedCancellationException(g); |
2200 |
< |
|
2201 |
< |
r = new AddAction(); |
2202 |
< |
f = new CompletableFuture<Integer>(); |
2203 |
< |
f2 = new CompletableFuture<Integer>(); |
2204 |
< |
g = f.thenAcceptBothAsync(f2, r, new ThreadExecutor()); |
2205 |
< |
f.complete(one); |
2206 |
< |
assertTrue(f2.cancel(true)); |
2207 |
< |
checkCompletedWithWrappedCancellationException(g); |
2208 |
< |
} |
2192 |
> |
public void testAcceptEither_normalCompletion() { |
2193 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2194 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2195 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2196 |
> |
{ |
2197 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2198 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2199 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2200 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2201 |
> |
|
2202 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2203 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2204 |
> |
checkIncomplete(h0); |
2205 |
> |
checkIncomplete(h1); |
2206 |
> |
rs[0].assertNotInvoked(); |
2207 |
> |
rs[1].assertNotInvoked(); |
2208 |
> |
f.complete(v1); |
2209 |
> |
checkCompletedNormally(h0, null); |
2210 |
> |
checkCompletedNormally(h1, null); |
2211 |
> |
rs[0].assertValue(v1); |
2212 |
> |
rs[1].assertValue(v1); |
2213 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2214 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2215 |
> |
checkCompletedNormally(h2, null); |
2216 |
> |
checkCompletedNormally(h3, null); |
2217 |
> |
rs[2].assertValue(v1); |
2218 |
> |
rs[3].assertValue(v1); |
2219 |
> |
g.complete(v2); |
2220 |
> |
|
2221 |
> |
// unspecified behavior - both source completions available |
2222 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2223 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2224 |
> |
checkCompletedNormally(h4, null); |
2225 |
> |
checkCompletedNormally(h5, null); |
2226 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2227 |
> |
Objects.equals(v2, rs[4].value)); |
2228 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2229 |
> |
Objects.equals(v2, rs[5].value)); |
2230 |
> |
|
2231 |
> |
checkCompletedNormally(f, v1); |
2232 |
> |
checkCompletedNormally(g, v2); |
2233 |
> |
checkCompletedNormally(h0, null); |
2234 |
> |
checkCompletedNormally(h1, null); |
2235 |
> |
checkCompletedNormally(h2, null); |
2236 |
> |
checkCompletedNormally(h3, null); |
2237 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2238 |
> |
}} |
2239 |
|
|
2240 |
|
/** |
2241 |
< |
* runAfterBothAsync result completes normally after normal |
2242 |
< |
* completion of sources |
2241 |
> |
* acceptEither result completes exceptionally after exceptional |
2242 |
> |
* completion of either source |
2243 |
|
*/ |
2244 |
< |
public void testRunAfterBothAsyncE() { |
2245 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2246 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2247 |
< |
Noop r = new Noop(); |
2248 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2249 |
< |
f.complete(one); |
2250 |
< |
checkIncomplete(g); |
2251 |
< |
f2.complete(two); |
2252 |
< |
checkCompletedNormally(g, null); |
2253 |
< |
assertTrue(r.ran); |
2254 |
< |
} |
2244 |
> |
public void testAcceptEither_exceptionalCompletion() { |
2245 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2246 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2247 |
> |
{ |
2248 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2249 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2250 |
> |
final CFException ex = new CFException(); |
2251 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2252 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2253 |
> |
|
2254 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2255 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2256 |
> |
checkIncomplete(h0); |
2257 |
> |
checkIncomplete(h1); |
2258 |
> |
rs[0].assertNotInvoked(); |
2259 |
> |
rs[1].assertNotInvoked(); |
2260 |
> |
f.completeExceptionally(ex); |
2261 |
> |
checkCompletedWithWrappedException(h0, ex); |
2262 |
> |
checkCompletedWithWrappedException(h1, ex); |
2263 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2264 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2265 |
> |
checkCompletedWithWrappedException(h2, ex); |
2266 |
> |
checkCompletedWithWrappedException(h3, ex); |
2267 |
> |
|
2268 |
> |
g.complete(v1); |
2269 |
> |
|
2270 |
> |
// unspecified behavior - both source completions available |
2271 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2272 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2273 |
> |
try { |
2274 |
> |
assertNull(h4.join()); |
2275 |
> |
rs[4].assertValue(v1); |
2276 |
> |
} catch (CompletionException ok) { |
2277 |
> |
checkCompletedWithWrappedException(h4, ex); |
2278 |
> |
rs[4].assertNotInvoked(); |
2279 |
> |
} |
2280 |
> |
try { |
2281 |
> |
assertNull(h5.join()); |
2282 |
> |
rs[5].assertValue(v1); |
2283 |
> |
} catch (CompletionException ok) { |
2284 |
> |
checkCompletedWithWrappedException(h5, ex); |
2285 |
> |
rs[5].assertNotInvoked(); |
2286 |
> |
} |
2287 |
|
|
2288 |
< |
/** |
2289 |
< |
* runAfterBothAsync result completes exceptionally after exceptional |
2290 |
< |
* completion of source |
2291 |
< |
*/ |
2292 |
< |
public void testRunAfterBothAsync2E() { |
2293 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2294 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2295 |
< |
Noop r = new Noop(); |
2296 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2297 |
< |
f.completeExceptionally(new CFException()); |
2298 |
< |
f2.complete(two); |
2299 |
< |
checkCompletedWithWrappedCFException(g); |
2288 |
> |
checkCompletedExceptionally(f, ex); |
2289 |
> |
checkCompletedNormally(g, v1); |
2290 |
> |
checkCompletedWithWrappedException(h0, ex); |
2291 |
> |
checkCompletedWithWrappedException(h1, ex); |
2292 |
> |
checkCompletedWithWrappedException(h2, ex); |
2293 |
> |
checkCompletedWithWrappedException(h3, ex); |
2294 |
> |
checkCompletedWithWrappedException(h4, ex); |
2295 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2296 |
> |
}} |
2297 |
> |
|
2298 |
> |
public void testAcceptEither_exceptionalCompletion2() { |
2299 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2300 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2301 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2302 |
> |
{ |
2303 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2304 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2305 |
> |
final CFException ex = new CFException(); |
2306 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2307 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2308 |
> |
|
2309 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2310 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2311 |
> |
if (fFirst) { |
2312 |
> |
f.complete(v1); |
2313 |
> |
g.completeExceptionally(ex); |
2314 |
> |
} else { |
2315 |
> |
g.completeExceptionally(ex); |
2316 |
> |
f.complete(v1); |
2317 |
> |
} |
2318 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2319 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2320 |
|
|
2321 |
< |
r = new Noop(); |
2322 |
< |
f = new CompletableFuture<Integer>(); |
2323 |
< |
f2 = new CompletableFuture<Integer>(); |
2324 |
< |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2325 |
< |
f.complete(one); |
2326 |
< |
f2.completeExceptionally(new CFException()); |
2327 |
< |
checkCompletedWithWrappedCFException(g); |
2328 |
< |
} |
2321 |
> |
// unspecified behavior - both source completions available |
2322 |
> |
try { |
2323 |
> |
assertEquals(null, h0.join()); |
2324 |
> |
rs[0].assertValue(v1); |
2325 |
> |
} catch (CompletionException ok) { |
2326 |
> |
checkCompletedWithWrappedException(h0, ex); |
2327 |
> |
rs[0].assertNotInvoked(); |
2328 |
> |
} |
2329 |
> |
try { |
2330 |
> |
assertEquals(null, h1.join()); |
2331 |
> |
rs[1].assertValue(v1); |
2332 |
> |
} catch (CompletionException ok) { |
2333 |
> |
checkCompletedWithWrappedException(h1, ex); |
2334 |
> |
rs[1].assertNotInvoked(); |
2335 |
> |
} |
2336 |
> |
try { |
2337 |
> |
assertEquals(null, h2.join()); |
2338 |
> |
rs[2].assertValue(v1); |
2339 |
> |
} catch (CompletionException ok) { |
2340 |
> |
checkCompletedWithWrappedException(h2, ex); |
2341 |
> |
rs[2].assertNotInvoked(); |
2342 |
> |
} |
2343 |
> |
try { |
2344 |
> |
assertEquals(null, h3.join()); |
2345 |
> |
rs[3].assertValue(v1); |
2346 |
> |
} catch (CompletionException ok) { |
2347 |
> |
checkCompletedWithWrappedException(h3, ex); |
2348 |
> |
rs[3].assertNotInvoked(); |
2349 |
> |
} |
2350 |
|
|
2351 |
< |
/** |
2352 |
< |
* runAfterBothAsync result completes exceptionally if action does |
2353 |
< |
*/ |
2042 |
< |
public void testRunAfterBothAsync3E() { |
2043 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2044 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2045 |
< |
FailingNoop r = new FailingNoop(); |
2046 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2047 |
< |
f.complete(one); |
2048 |
< |
checkIncomplete(g); |
2049 |
< |
f2.complete(two); |
2050 |
< |
checkCompletedWithWrappedCFException(g); |
2051 |
< |
} |
2351 |
> |
checkCompletedNormally(f, v1); |
2352 |
> |
checkCompletedExceptionally(g, ex); |
2353 |
> |
}} |
2354 |
|
|
2355 |
|
/** |
2356 |
< |
* runAfterBothAsync result completes exceptionally if either source cancelled |
2356 |
> |
* acceptEither result completes exceptionally if either source cancelled |
2357 |
|
*/ |
2358 |
< |
public void testRunAfterBothAsync4E() { |
2359 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2360 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2361 |
< |
Noop r = new Noop(); |
2362 |
< |
CompletableFuture<Void> g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2363 |
< |
assertTrue(f.cancel(true)); |
2364 |
< |
f2.complete(two); |
2365 |
< |
checkCompletedWithWrappedCancellationException(g); |
2358 |
> |
public void testAcceptEither_sourceCancelled() { |
2359 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2360 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2361 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2362 |
> |
{ |
2363 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2364 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2365 |
> |
final NoopConsumer[] rs = new NoopConsumer[6]; |
2366 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new NoopConsumer(m); |
2367 |
> |
|
2368 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2369 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2370 |
> |
checkIncomplete(h0); |
2371 |
> |
checkIncomplete(h1); |
2372 |
> |
rs[0].assertNotInvoked(); |
2373 |
> |
rs[1].assertNotInvoked(); |
2374 |
> |
f.cancel(mayInterruptIfRunning); |
2375 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2376 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2377 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2378 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2379 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2380 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2381 |
> |
|
2382 |
> |
g.complete(v1); |
2383 |
> |
|
2384 |
> |
// unspecified behavior - both source completions available |
2385 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2386 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2387 |
> |
try { |
2388 |
> |
assertNull(h4.join()); |
2389 |
> |
rs[4].assertValue(v1); |
2390 |
> |
} catch (CompletionException ok) { |
2391 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2392 |
> |
rs[4].assertNotInvoked(); |
2393 |
> |
} |
2394 |
> |
try { |
2395 |
> |
assertNull(h5.join()); |
2396 |
> |
rs[5].assertValue(v1); |
2397 |
> |
} catch (CompletionException ok) { |
2398 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2399 |
> |
rs[5].assertNotInvoked(); |
2400 |
> |
} |
2401 |
|
|
2402 |
< |
r = new Noop(); |
2403 |
< |
f = new CompletableFuture<Integer>(); |
2404 |
< |
f2 = new CompletableFuture<Integer>(); |
2405 |
< |
g = f.runAfterBothAsync(f2, r, new ThreadExecutor()); |
2406 |
< |
f.complete(one); |
2407 |
< |
assertTrue(f2.cancel(true)); |
2408 |
< |
checkCompletedWithWrappedCancellationException(g); |
2409 |
< |
} |
2402 |
> |
checkCancelled(f); |
2403 |
> |
checkCompletedNormally(g, v1); |
2404 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2405 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2406 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2407 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2408 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2409 |
> |
}} |
2410 |
|
|
2411 |
|
/** |
2412 |
< |
* applyToEitherAsync result completes normally after normal |
2076 |
< |
* completion of sources |
2412 |
> |
* acceptEither result completes exceptionally if action does |
2413 |
|
*/ |
2414 |
< |
public void testApplyToEitherAsyncE() { |
2415 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2416 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2417 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2418 |
< |
f.complete(one); |
2419 |
< |
checkCompletedNormally(g, two); |
2420 |
< |
|
2421 |
< |
f = new CompletableFuture<Integer>(); |
2422 |
< |
f.complete(one); |
2423 |
< |
f2 = new CompletableFuture<Integer>(); |
2424 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2425 |
< |
checkCompletedNormally(g, two); |
2426 |
< |
} |
2414 |
> |
public void testAcceptEither_actionFailed() { |
2415 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2416 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2417 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2418 |
> |
{ |
2419 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2420 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2421 |
> |
final FailingConsumer[] rs = new FailingConsumer[6]; |
2422 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingConsumer(m); |
2423 |
> |
|
2424 |
> |
final CompletableFuture<Void> h0 = m.acceptEither(f, g, rs[0]); |
2425 |
> |
final CompletableFuture<Void> h1 = m.acceptEither(g, f, rs[1]); |
2426 |
> |
f.complete(v1); |
2427 |
> |
final CompletableFuture<Void> h2 = m.acceptEither(f, g, rs[2]); |
2428 |
> |
final CompletableFuture<Void> h3 = m.acceptEither(g, f, rs[3]); |
2429 |
> |
checkCompletedWithWrappedCFException(h0); |
2430 |
> |
checkCompletedWithWrappedCFException(h1); |
2431 |
> |
checkCompletedWithWrappedCFException(h2); |
2432 |
> |
checkCompletedWithWrappedCFException(h3); |
2433 |
> |
for (int i = 0; i < 4; i++) rs[i].assertValue(v1); |
2434 |
> |
|
2435 |
> |
g.complete(v2); |
2436 |
> |
|
2437 |
> |
// unspecified behavior - both source completions available |
2438 |
> |
final CompletableFuture<Void> h4 = m.acceptEither(f, g, rs[4]); |
2439 |
> |
final CompletableFuture<Void> h5 = m.acceptEither(g, f, rs[5]); |
2440 |
> |
|
2441 |
> |
checkCompletedWithWrappedCFException(h4); |
2442 |
> |
assertTrue(Objects.equals(v1, rs[4].value) || |
2443 |
> |
Objects.equals(v2, rs[4].value)); |
2444 |
> |
checkCompletedWithWrappedCFException(h5); |
2445 |
> |
assertTrue(Objects.equals(v1, rs[5].value) || |
2446 |
> |
Objects.equals(v2, rs[5].value)); |
2447 |
> |
|
2448 |
> |
checkCompletedNormally(f, v1); |
2449 |
> |
checkCompletedNormally(g, v2); |
2450 |
> |
}} |
2451 |
|
|
2452 |
|
/** |
2453 |
< |
* applyToEitherAsync result completes exceptionally after exceptional |
2454 |
< |
* completion of source |
2095 |
< |
*/ |
2096 |
< |
public void testApplyToEitherAsync2E() { |
2097 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2098 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2099 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2100 |
< |
f.completeExceptionally(new CFException()); |
2101 |
< |
checkCompletedWithWrappedCFException(g); |
2102 |
< |
|
2103 |
< |
f = new CompletableFuture<Integer>(); |
2104 |
< |
f2 = new CompletableFuture<Integer>(); |
2105 |
< |
f2.completeExceptionally(new CFException()); |
2106 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2107 |
< |
f.complete(one); |
2108 |
< |
checkCompletedWithWrappedCFException(g); |
2109 |
< |
} |
2110 |
< |
|
2111 |
< |
/** |
2112 |
< |
* applyToEitherAsync result completes exceptionally if action does |
2453 |
> |
* runAfterEither result completes normally after normal completion |
2454 |
> |
* of either source |
2455 |
|
*/ |
2456 |
< |
public void testApplyToEitherAsync3E() { |
2457 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2458 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2459 |
< |
FailingFunction r = new FailingFunction(); |
2460 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, r, new ThreadExecutor()); |
2461 |
< |
f.complete(one); |
2462 |
< |
checkCompletedWithWrappedCFException(g); |
2463 |
< |
} |
2456 |
> |
public void testRunAfterEither_normalCompletion() { |
2457 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2458 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2459 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2460 |
> |
{ |
2461 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2462 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2463 |
> |
final Noop[] rs = new Noop[6]; |
2464 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2465 |
> |
|
2466 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2467 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2468 |
> |
checkIncomplete(h0); |
2469 |
> |
checkIncomplete(h1); |
2470 |
> |
rs[0].assertNotInvoked(); |
2471 |
> |
rs[1].assertNotInvoked(); |
2472 |
> |
f.complete(v1); |
2473 |
> |
checkCompletedNormally(h0, null); |
2474 |
> |
checkCompletedNormally(h1, null); |
2475 |
> |
rs[0].assertInvoked(); |
2476 |
> |
rs[1].assertInvoked(); |
2477 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2478 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2479 |
> |
checkCompletedNormally(h2, null); |
2480 |
> |
checkCompletedNormally(h3, null); |
2481 |
> |
rs[2].assertInvoked(); |
2482 |
> |
rs[3].assertInvoked(); |
2483 |
> |
|
2484 |
> |
g.complete(v2); |
2485 |
> |
|
2486 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2487 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2488 |
> |
|
2489 |
> |
checkCompletedNormally(f, v1); |
2490 |
> |
checkCompletedNormally(g, v2); |
2491 |
> |
checkCompletedNormally(h0, null); |
2492 |
> |
checkCompletedNormally(h1, null); |
2493 |
> |
checkCompletedNormally(h2, null); |
2494 |
> |
checkCompletedNormally(h3, null); |
2495 |
> |
checkCompletedNormally(h4, null); |
2496 |
> |
checkCompletedNormally(h5, null); |
2497 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2498 |
> |
}} |
2499 |
|
|
2500 |
|
/** |
2501 |
< |
* applyToEitherAsync result completes exceptionally if either source cancelled |
2501 |
> |
* runAfterEither result completes exceptionally after exceptional |
2502 |
> |
* completion of either source |
2503 |
|
*/ |
2504 |
< |
public void testApplyToEitherAsync4E() { |
2505 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2506 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2507 |
< |
CompletableFuture<Integer> g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2508 |
< |
assertTrue(f.cancel(true)); |
2509 |
< |
checkCompletedWithWrappedCancellationException(g); |
2504 |
> |
public void testRunAfterEither_exceptionalCompletion() { |
2505 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2506 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2507 |
> |
{ |
2508 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2509 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2510 |
> |
final CFException ex = new CFException(); |
2511 |
> |
final Noop[] rs = new Noop[6]; |
2512 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2513 |
> |
|
2514 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2515 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2516 |
> |
checkIncomplete(h0); |
2517 |
> |
checkIncomplete(h1); |
2518 |
> |
rs[0].assertNotInvoked(); |
2519 |
> |
rs[1].assertNotInvoked(); |
2520 |
> |
f.completeExceptionally(ex); |
2521 |
> |
checkCompletedWithWrappedException(h0, ex); |
2522 |
> |
checkCompletedWithWrappedException(h1, ex); |
2523 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2524 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2525 |
> |
checkCompletedWithWrappedException(h2, ex); |
2526 |
> |
checkCompletedWithWrappedException(h3, ex); |
2527 |
> |
|
2528 |
> |
g.complete(v1); |
2529 |
> |
|
2530 |
> |
// unspecified behavior - both source completions available |
2531 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2532 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2533 |
> |
try { |
2534 |
> |
assertNull(h4.join()); |
2535 |
> |
rs[4].assertInvoked(); |
2536 |
> |
} catch (CompletionException ok) { |
2537 |
> |
checkCompletedWithWrappedException(h4, ex); |
2538 |
> |
rs[4].assertNotInvoked(); |
2539 |
> |
} |
2540 |
> |
try { |
2541 |
> |
assertNull(h5.join()); |
2542 |
> |
rs[5].assertInvoked(); |
2543 |
> |
} catch (CompletionException ok) { |
2544 |
> |
checkCompletedWithWrappedException(h5, ex); |
2545 |
> |
rs[5].assertNotInvoked(); |
2546 |
> |
} |
2547 |
|
|
2548 |
< |
f = new CompletableFuture<Integer>(); |
2549 |
< |
f2 = new CompletableFuture<Integer>(); |
2550 |
< |
assertTrue(f2.cancel(true)); |
2551 |
< |
g = f.applyToEitherAsync(f2, inc, new ThreadExecutor()); |
2552 |
< |
checkCompletedWithWrappedCancellationException(g); |
2553 |
< |
} |
2548 |
> |
checkCompletedExceptionally(f, ex); |
2549 |
> |
checkCompletedNormally(g, v1); |
2550 |
> |
checkCompletedWithWrappedException(h0, ex); |
2551 |
> |
checkCompletedWithWrappedException(h1, ex); |
2552 |
> |
checkCompletedWithWrappedException(h2, ex); |
2553 |
> |
checkCompletedWithWrappedException(h3, ex); |
2554 |
> |
checkCompletedWithWrappedException(h4, ex); |
2555 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2556 |
> |
}} |
2557 |
> |
|
2558 |
> |
public void testRunAfterEither_exceptionalCompletion2() { |
2559 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2560 |
> |
for (boolean fFirst : new boolean[] { true, false }) |
2561 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2562 |
> |
{ |
2563 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2564 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2565 |
> |
final CFException ex = new CFException(); |
2566 |
> |
final Noop[] rs = new Noop[6]; |
2567 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2568 |
> |
|
2569 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2570 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2571 |
> |
if (fFirst) { |
2572 |
> |
f.complete(v1); |
2573 |
> |
g.completeExceptionally(ex); |
2574 |
> |
} else { |
2575 |
> |
g.completeExceptionally(ex); |
2576 |
> |
f.complete(v1); |
2577 |
> |
} |
2578 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2579 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2580 |
|
|
2581 |
< |
/** |
2582 |
< |
* acceptEitherAsync result completes normally after normal |
2583 |
< |
* completion of sources |
2584 |
< |
*/ |
2585 |
< |
public void testAcceptEitherAsyncE() { |
2586 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2587 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2588 |
< |
IncAction r = new IncAction(); |
2589 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2590 |
< |
f.complete(one); |
2591 |
< |
checkCompletedNormally(g, null); |
2592 |
< |
assertEquals(r.value, 2); |
2581 |
> |
// unspecified behavior - both source completions available |
2582 |
> |
try { |
2583 |
> |
assertEquals(null, h0.join()); |
2584 |
> |
rs[0].assertInvoked(); |
2585 |
> |
} catch (CompletionException ok) { |
2586 |
> |
checkCompletedWithWrappedException(h0, ex); |
2587 |
> |
rs[0].assertNotInvoked(); |
2588 |
> |
} |
2589 |
> |
try { |
2590 |
> |
assertEquals(null, h1.join()); |
2591 |
> |
rs[1].assertInvoked(); |
2592 |
> |
} catch (CompletionException ok) { |
2593 |
> |
checkCompletedWithWrappedException(h1, ex); |
2594 |
> |
rs[1].assertNotInvoked(); |
2595 |
> |
} |
2596 |
> |
try { |
2597 |
> |
assertEquals(null, h2.join()); |
2598 |
> |
rs[2].assertInvoked(); |
2599 |
> |
} catch (CompletionException ok) { |
2600 |
> |
checkCompletedWithWrappedException(h2, ex); |
2601 |
> |
rs[2].assertNotInvoked(); |
2602 |
> |
} |
2603 |
> |
try { |
2604 |
> |
assertEquals(null, h3.join()); |
2605 |
> |
rs[3].assertInvoked(); |
2606 |
> |
} catch (CompletionException ok) { |
2607 |
> |
checkCompletedWithWrappedException(h3, ex); |
2608 |
> |
rs[3].assertNotInvoked(); |
2609 |
> |
} |
2610 |
|
|
2611 |
< |
r = new IncAction(); |
2612 |
< |
f = new CompletableFuture<Integer>(); |
2613 |
< |
f.complete(one); |
2156 |
< |
f2 = new CompletableFuture<Integer>(); |
2157 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2158 |
< |
checkCompletedNormally(g, null); |
2159 |
< |
assertEquals(r.value, 2); |
2160 |
< |
} |
2611 |
> |
checkCompletedNormally(f, v1); |
2612 |
> |
checkCompletedExceptionally(g, ex); |
2613 |
> |
}} |
2614 |
|
|
2615 |
|
/** |
2616 |
< |
* acceptEitherAsync result completes exceptionally after exceptional |
2164 |
< |
* completion of source |
2616 |
> |
* runAfterEither result completes exceptionally if either source cancelled |
2617 |
|
*/ |
2618 |
< |
public void testAcceptEitherAsync2E() { |
2619 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2620 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2621 |
< |
IncAction r = new IncAction(); |
2622 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2623 |
< |
f.completeExceptionally(new CFException()); |
2624 |
< |
checkCompletedWithWrappedCFException(g); |
2625 |
< |
|
2626 |
< |
r = new IncAction(); |
2627 |
< |
f = new CompletableFuture<Integer>(); |
2628 |
< |
f2 = new CompletableFuture<Integer>(); |
2629 |
< |
f2.completeExceptionally(new CFException()); |
2630 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2631 |
< |
f.complete(one); |
2632 |
< |
checkCompletedWithWrappedCFException(g); |
2633 |
< |
} |
2618 |
> |
public void testRunAfterEither_sourceCancelled() { |
2619 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2620 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2621 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2622 |
> |
{ |
2623 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2624 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2625 |
> |
final Noop[] rs = new Noop[6]; |
2626 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new Noop(m); |
2627 |
> |
|
2628 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2629 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2630 |
> |
checkIncomplete(h0); |
2631 |
> |
checkIncomplete(h1); |
2632 |
> |
rs[0].assertNotInvoked(); |
2633 |
> |
rs[1].assertNotInvoked(); |
2634 |
> |
f.cancel(mayInterruptIfRunning); |
2635 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2636 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2637 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2638 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2639 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2640 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2641 |
> |
|
2642 |
> |
g.complete(v1); |
2643 |
> |
|
2644 |
> |
// unspecified behavior - both source completions available |
2645 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2646 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2647 |
> |
try { |
2648 |
> |
assertNull(h4.join()); |
2649 |
> |
rs[4].assertInvoked(); |
2650 |
> |
} catch (CompletionException ok) { |
2651 |
> |
checkCompletedWithWrappedCancellationException(h4); |
2652 |
> |
rs[4].assertNotInvoked(); |
2653 |
> |
} |
2654 |
> |
try { |
2655 |
> |
assertNull(h5.join()); |
2656 |
> |
rs[5].assertInvoked(); |
2657 |
> |
} catch (CompletionException ok) { |
2658 |
> |
checkCompletedWithWrappedCancellationException(h5); |
2659 |
> |
rs[5].assertNotInvoked(); |
2660 |
> |
} |
2661 |
|
|
2662 |
< |
/** |
2663 |
< |
* acceptEitherAsync result completes exceptionally if action does |
2664 |
< |
*/ |
2665 |
< |
public void testAcceptEitherAsync3E() { |
2666 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2667 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2668 |
< |
FailingConsumer r = new FailingConsumer(); |
2669 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2191 |
< |
f.complete(one); |
2192 |
< |
checkCompletedWithWrappedCFException(g); |
2193 |
< |
} |
2662 |
> |
checkCancelled(f); |
2663 |
> |
checkCompletedNormally(g, v1); |
2664 |
> |
checkCompletedWithWrappedCancellationException(h0); |
2665 |
> |
checkCompletedWithWrappedCancellationException(h1); |
2666 |
> |
checkCompletedWithWrappedCancellationException(h2); |
2667 |
> |
checkCompletedWithWrappedCancellationException(h3); |
2668 |
> |
for (int i = 0; i < 4; i++) rs[i].assertNotInvoked(); |
2669 |
> |
}} |
2670 |
|
|
2671 |
|
/** |
2672 |
< |
* acceptEitherAsync result completes exceptionally if either |
2197 |
< |
* source cancelled |
2672 |
> |
* runAfterEither result completes exceptionally if action does |
2673 |
|
*/ |
2674 |
< |
public void testAcceptEitherAsync4E() { |
2675 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2676 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2677 |
< |
IncAction r = new IncAction(); |
2678 |
< |
CompletableFuture<Void> g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2679 |
< |
assertTrue(f.cancel(true)); |
2680 |
< |
checkCompletedWithWrappedCancellationException(g); |
2681 |
< |
|
2682 |
< |
r = new IncAction(); |
2683 |
< |
f = new CompletableFuture<Integer>(); |
2684 |
< |
f2 = new CompletableFuture<Integer>(); |
2685 |
< |
assertTrue(f2.cancel(true)); |
2686 |
< |
g = f.acceptEitherAsync(f2, r, new ThreadExecutor()); |
2687 |
< |
checkCompletedWithWrappedCancellationException(g); |
2688 |
< |
} |
2674 |
> |
public void testRunAfterEither_actionFailed() { |
2675 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2676 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2677 |
> |
for (Integer v2 : new Integer[] { 2, null }) |
2678 |
> |
{ |
2679 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2680 |
> |
final CompletableFuture<Integer> g = new CompletableFuture<>(); |
2681 |
> |
final FailingRunnable[] rs = new FailingRunnable[6]; |
2682 |
> |
for (int i = 0; i < rs.length; i++) rs[i] = new FailingRunnable(m); |
2683 |
> |
|
2684 |
> |
final CompletableFuture<Void> h0 = m.runAfterEither(f, g, rs[0]); |
2685 |
> |
final CompletableFuture<Void> h1 = m.runAfterEither(g, f, rs[1]); |
2686 |
> |
f.complete(v1); |
2687 |
> |
final CompletableFuture<Void> h2 = m.runAfterEither(f, g, rs[2]); |
2688 |
> |
final CompletableFuture<Void> h3 = m.runAfterEither(g, f, rs[3]); |
2689 |
> |
checkCompletedWithWrappedCFException(h0); |
2690 |
> |
checkCompletedWithWrappedCFException(h1); |
2691 |
> |
checkCompletedWithWrappedCFException(h2); |
2692 |
> |
checkCompletedWithWrappedCFException(h3); |
2693 |
> |
for (int i = 0; i < 4; i++) rs[i].assertInvoked(); |
2694 |
> |
g.complete(v2); |
2695 |
> |
final CompletableFuture<Void> h4 = m.runAfterEither(f, g, rs[4]); |
2696 |
> |
final CompletableFuture<Void> h5 = m.runAfterEither(g, f, rs[5]); |
2697 |
> |
checkCompletedWithWrappedCFException(h4); |
2698 |
> |
checkCompletedWithWrappedCFException(h5); |
2699 |
> |
|
2700 |
> |
checkCompletedNormally(f, v1); |
2701 |
> |
checkCompletedNormally(g, v2); |
2702 |
> |
for (int i = 0; i < 6; i++) rs[i].assertInvoked(); |
2703 |
> |
}} |
2704 |
|
|
2705 |
|
/** |
2706 |
< |
* runAfterEitherAsync result completes normally after normal |
2217 |
< |
* completion of sources |
2706 |
> |
* thenCompose result completes normally after normal completion of source |
2707 |
|
*/ |
2708 |
< |
public void testRunAfterEitherAsyncE() { |
2709 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2710 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2711 |
< |
Noop r = new Noop(); |
2712 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2713 |
< |
f.complete(one); |
2714 |
< |
checkCompletedNormally(g, null); |
2715 |
< |
assertTrue(r.ran); |
2716 |
< |
|
2717 |
< |
r = new Noop(); |
2718 |
< |
f = new CompletableFuture<Integer>(); |
2719 |
< |
f.complete(one); |
2720 |
< |
f2 = new CompletableFuture<Integer>(); |
2721 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2722 |
< |
checkCompletedNormally(g, null); |
2234 |
< |
assertTrue(r.ran); |
2235 |
< |
} |
2708 |
> |
public void testThenCompose_normalCompletion() { |
2709 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2710 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2711 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2712 |
> |
{ |
2713 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2714 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2715 |
> |
if (!createIncomplete) f.complete(v1); |
2716 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2717 |
> |
if (createIncomplete) f.complete(v1); |
2718 |
> |
|
2719 |
> |
checkCompletedNormally(g, inc(v1)); |
2720 |
> |
checkCompletedNormally(f, v1); |
2721 |
> |
r.assertValue(v1); |
2722 |
> |
}} |
2723 |
|
|
2724 |
|
/** |
2725 |
< |
* runAfterEitherAsync result completes exceptionally after exceptional |
2725 |
> |
* thenCompose result completes exceptionally after exceptional |
2726 |
|
* completion of source |
2727 |
|
*/ |
2728 |
< |
public void testRunAfterEitherAsync2E() { |
2729 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2730 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2731 |
< |
Noop r = new Noop(); |
2732 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2733 |
< |
f.completeExceptionally(new CFException()); |
2734 |
< |
checkCompletedWithWrappedCFException(g); |
2735 |
< |
|
2736 |
< |
r = new Noop(); |
2737 |
< |
f = new CompletableFuture<Integer>(); |
2738 |
< |
f2 = new CompletableFuture<Integer>(); |
2739 |
< |
f2.completeExceptionally(new CFException()); |
2740 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2741 |
< |
f.complete(one); |
2742 |
< |
checkCompletedWithWrappedCFException(g); |
2256 |
< |
} |
2257 |
< |
|
2258 |
< |
/** |
2259 |
< |
* runAfterEitherAsync result completes exceptionally if action does |
2260 |
< |
*/ |
2261 |
< |
public void testRunAfterEitherAsync3E() { |
2262 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2263 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2264 |
< |
FailingNoop r = new FailingNoop(); |
2265 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2266 |
< |
f.complete(one); |
2267 |
< |
checkCompletedWithWrappedCFException(g); |
2268 |
< |
} |
2269 |
< |
|
2270 |
< |
/** |
2271 |
< |
* runAfterEitherAsync result completes exceptionally if either |
2272 |
< |
* source cancelled |
2273 |
< |
*/ |
2274 |
< |
public void testRunAfterEitherAsync4E() { |
2275 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2276 |
< |
CompletableFuture<Integer> f2 = new CompletableFuture<Integer>(); |
2277 |
< |
Noop r = new Noop(); |
2278 |
< |
CompletableFuture<Void> g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2279 |
< |
assertTrue(f.cancel(true)); |
2280 |
< |
checkCompletedWithWrappedCancellationException(g); |
2281 |
< |
|
2282 |
< |
r = new Noop(); |
2283 |
< |
f = new CompletableFuture<Integer>(); |
2284 |
< |
f2 = new CompletableFuture<Integer>(); |
2285 |
< |
assertTrue(f2.cancel(true)); |
2286 |
< |
g = f.runAfterEitherAsync(f2, r, new ThreadExecutor()); |
2287 |
< |
checkCompletedWithWrappedCancellationException(g); |
2288 |
< |
} |
2728 |
> |
public void testThenCompose_exceptionalCompletion() { |
2729 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2730 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2731 |
> |
{ |
2732 |
> |
final CFException ex = new CFException(); |
2733 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2734 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2735 |
> |
if (!createIncomplete) f.completeExceptionally(ex); |
2736 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2737 |
> |
if (createIncomplete) f.completeExceptionally(ex); |
2738 |
> |
|
2739 |
> |
checkCompletedWithWrappedException(g, ex); |
2740 |
> |
checkCompletedExceptionally(f, ex); |
2741 |
> |
r.assertNotInvoked(); |
2742 |
> |
}} |
2743 |
|
|
2744 |
|
/** |
2745 |
< |
* thenComposeAsync result completes normally after normal |
2292 |
< |
* completion of source |
2745 |
> |
* thenCompose result completes exceptionally if action does |
2746 |
|
*/ |
2747 |
< |
public void testThenComposeAsyncE() { |
2748 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2749 |
< |
CompletableFutureInc r = new CompletableFutureInc(); |
2750 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2751 |
< |
f.complete(one); |
2752 |
< |
checkCompletedNormally(g, two); |
2753 |
< |
} |
2747 |
> |
public void testThenCompose_actionFailed() { |
2748 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2749 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2750 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
2751 |
> |
{ |
2752 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2753 |
> |
final FailingCompletableFutureFunction r |
2754 |
> |
= new FailingCompletableFutureFunction(m); |
2755 |
> |
if (!createIncomplete) f.complete(v1); |
2756 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2757 |
> |
if (createIncomplete) f.complete(v1); |
2758 |
|
|
2302 |
– |
/** |
2303 |
– |
* thenComposeAsync result completes exceptionally after |
2304 |
– |
* exceptional completion of source |
2305 |
– |
*/ |
2306 |
– |
public void testThenComposeAsync2E() { |
2307 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2308 |
– |
CompletableFutureInc r = new CompletableFutureInc(); |
2309 |
– |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2310 |
– |
f.completeExceptionally(new CFException()); |
2759 |
|
checkCompletedWithWrappedCFException(g); |
2760 |
< |
} |
2760 |
> |
checkCompletedNormally(f, v1); |
2761 |
> |
}} |
2762 |
|
|
2763 |
|
/** |
2764 |
< |
* thenComposeAsync result completes exceptionally if action does |
2764 |
> |
* thenCompose result completes exceptionally if source cancelled |
2765 |
|
*/ |
2766 |
< |
public void testThenComposeAsync3E() { |
2767 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2768 |
< |
FailingCompletableFutureFunction r = new FailingCompletableFutureFunction(); |
2769 |
< |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2770 |
< |
f.complete(one); |
2771 |
< |
checkCompletedWithWrappedCFException(g); |
2772 |
< |
} |
2766 |
> |
public void testThenCompose_sourceCancelled() { |
2767 |
> |
for (ExecutionMode m : ExecutionMode.values()) |
2768 |
> |
for (boolean createIncomplete : new boolean[] { true, false }) |
2769 |
> |
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) |
2770 |
> |
{ |
2771 |
> |
final CompletableFuture<Integer> f = new CompletableFuture<>(); |
2772 |
> |
final CompletableFutureInc r = new CompletableFutureInc(m); |
2773 |
> |
if (!createIncomplete) assertTrue(f.cancel(mayInterruptIfRunning)); |
2774 |
> |
final CompletableFuture<Integer> g = m.thenCompose(f, r); |
2775 |
> |
if (createIncomplete) { |
2776 |
> |
checkIncomplete(g); |
2777 |
> |
assertTrue(f.cancel(mayInterruptIfRunning)); |
2778 |
> |
} |
2779 |
|
|
2325 |
– |
/** |
2326 |
– |
* thenComposeAsync result completes exceptionally if source cancelled |
2327 |
– |
*/ |
2328 |
– |
public void testThenComposeAsync4E() { |
2329 |
– |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2330 |
– |
CompletableFutureInc r = new CompletableFutureInc(); |
2331 |
– |
CompletableFuture<Integer> g = f.thenComposeAsync(r, new ThreadExecutor()); |
2332 |
– |
assertTrue(f.cancel(true)); |
2780 |
|
checkCompletedWithWrappedCancellationException(g); |
2781 |
< |
} |
2781 |
> |
checkCancelled(f); |
2782 |
> |
}} |
2783 |
|
|
2784 |
|
// other static methods |
2785 |
|
|
2788 |
|
* with the value null |
2789 |
|
*/ |
2790 |
|
public void testAllOf_empty() throws Exception { |
2791 |
< |
CompletableFuture<?> f = CompletableFuture.allOf(); |
2791 |
> |
CompletableFuture<Void> f = CompletableFuture.allOf(); |
2792 |
|
checkCompletedNormally(f, null); |
2793 |
|
} |
2794 |
|
|
2795 |
|
/** |
2796 |
< |
* allOf returns a future completed when all components complete |
2796 |
> |
* allOf returns a future completed normally with the value null |
2797 |
> |
* when all components complete normally |
2798 |
|
*/ |
2799 |
< |
public void testAllOf() throws Exception { |
2799 |
> |
public void testAllOf_normal() throws Exception { |
2800 |
|
for (int k = 1; k < 20; ++k) { |
2801 |
< |
CompletableFuture[] fs = new CompletableFuture[k]; |
2801 |
> |
CompletableFuture<Integer>[] fs |
2802 |
> |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2803 |
|
for (int i = 0; i < k; ++i) |
2804 |
< |
fs[i] = new CompletableFuture<Integer>(); |
2804 |
> |
fs[i] = new CompletableFuture<>(); |
2805 |
|
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2806 |
|
for (int i = 0; i < k; ++i) { |
2807 |
|
checkIncomplete(f); |
2808 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2809 |
|
fs[i].complete(one); |
2810 |
|
} |
2811 |
|
checkCompletedNormally(f, null); |
2812 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
2813 |
+ |
} |
2814 |
+ |
} |
2815 |
+ |
|
2816 |
+ |
public void testAllOf_backwards() throws Exception { |
2817 |
+ |
for (int k = 1; k < 20; ++k) { |
2818 |
+ |
CompletableFuture<Integer>[] fs |
2819 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture[k]; |
2820 |
+ |
for (int i = 0; i < k; ++i) |
2821 |
+ |
fs[i] = new CompletableFuture<>(); |
2822 |
+ |
CompletableFuture<Void> f = CompletableFuture.allOf(fs); |
2823 |
+ |
for (int i = k - 1; i >= 0; i--) { |
2824 |
+ |
checkIncomplete(f); |
2825 |
+ |
checkIncomplete(CompletableFuture.allOf(fs)); |
2826 |
+ |
fs[i].complete(one); |
2827 |
+ |
} |
2828 |
+ |
checkCompletedNormally(f, null); |
2829 |
+ |
checkCompletedNormally(CompletableFuture.allOf(fs), null); |
2830 |
|
} |
2831 |
|
} |
2832 |
|
|
2834 |
|
* anyOf(no component futures) returns an incomplete future |
2835 |
|
*/ |
2836 |
|
public void testAnyOf_empty() throws Exception { |
2837 |
< |
CompletableFuture<?> f = CompletableFuture.anyOf(); |
2837 |
> |
CompletableFuture<Object> f = CompletableFuture.anyOf(); |
2838 |
|
checkIncomplete(f); |
2839 |
|
} |
2840 |
|
|
2841 |
|
/** |
2842 |
< |
* allOf returns a future completed when any components complete |
2842 |
> |
* anyOf returns a future completed normally with a value when |
2843 |
> |
* a component future does |
2844 |
|
*/ |
2845 |
< |
public void testAnyOf() throws Exception { |
2846 |
< |
for (int k = 1; k < 20; ++k) { |
2845 |
> |
public void testAnyOf_normal() throws Exception { |
2846 |
> |
for (int k = 0; k < 10; ++k) { |
2847 |
|
CompletableFuture[] fs = new CompletableFuture[k]; |
2848 |
|
for (int i = 0; i < k; ++i) |
2849 |
< |
fs[i] = new CompletableFuture<Integer>(); |
2849 |
> |
fs[i] = new CompletableFuture<>(); |
2850 |
|
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2851 |
|
checkIncomplete(f); |
2852 |
|
for (int i = 0; i < k; ++i) { |
2853 |
|
fs[i].complete(one); |
2854 |
|
checkCompletedNormally(f, one); |
2855 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), one); |
2856 |
+ |
} |
2857 |
+ |
} |
2858 |
+ |
} |
2859 |
+ |
|
2860 |
+ |
/** |
2861 |
+ |
* anyOf result completes exceptionally when any component does. |
2862 |
+ |
*/ |
2863 |
+ |
public void testAnyOf_exceptional() throws Exception { |
2864 |
+ |
for (int k = 0; k < 10; ++k) { |
2865 |
+ |
CompletableFuture[] fs = new CompletableFuture[k]; |
2866 |
+ |
for (int i = 0; i < k; ++i) |
2867 |
+ |
fs[i] = new CompletableFuture<>(); |
2868 |
+ |
CompletableFuture<Object> f = CompletableFuture.anyOf(fs); |
2869 |
+ |
checkIncomplete(f); |
2870 |
+ |
for (int i = 0; i < k; ++i) { |
2871 |
+ |
fs[i].completeExceptionally(new CFException()); |
2872 |
+ |
checkCompletedWithWrappedCFException(f); |
2873 |
+ |
checkCompletedWithWrappedCFException(CompletableFuture.anyOf(fs)); |
2874 |
|
} |
2875 |
|
} |
2876 |
|
} |
2879 |
|
* Completion methods throw NullPointerException with null arguments |
2880 |
|
*/ |
2881 |
|
public void testNPE() { |
2882 |
< |
CompletableFuture<Integer> f = new CompletableFuture<Integer>(); |
2883 |
< |
CompletableFuture<Integer> g = new CompletableFuture<Integer>(); |
2884 |
< |
CompletableFuture h; |
2885 |
< |
try { h = f.thenApply(null); } catch (NullPointerException ok) {} |
2886 |
< |
try { h = f.thenAccept(null); } catch (NullPointerException ok) {} |
2887 |
< |
try { h = f.thenRun(null); } catch (NullPointerException ok) {} |
2888 |
< |
try { h = f.thenCombine(g, null); } catch (NullPointerException ok) {} |
2889 |
< |
try { h = f.thenCombine(null, null); } catch (NullPointerException ok) {} |
2890 |
< |
try { h = f.applyToEither(g, null); } catch (NullPointerException ok) {} |
2891 |
< |
try { h = f.applyToEither(null, null); } catch (NullPointerException ok) {} |
2892 |
< |
try { h = f.thenAcceptBoth(g, null); } catch (NullPointerException ok) {} |
2893 |
< |
try { h = f.thenAcceptBoth(null, null); } catch (NullPointerException ok) {} |
2894 |
< |
try { h = f.runAfterEither(g, null); } catch (NullPointerException ok) {} |
2895 |
< |
try { h = f.runAfterEither(null, null); } catch (NullPointerException ok) {} |
2896 |
< |
try { h = f.runAfterBoth(g, null); } catch (NullPointerException ok) {} |
2897 |
< |
try { h = f.runAfterBoth(null, null); } catch (NullPointerException ok) {} |
2898 |
< |
try { h = f.exceptionally(null); } catch (NullPointerException ok) {} |
2899 |
< |
try { h = f.handle(null); } catch (NullPointerException ok) {} |
2900 |
< |
try { h = f.thenCompose(null); } catch (NullPointerException ok) {} |
2901 |
< |
|
2902 |
< |
try { h = f.thenApplyAsync(null); } catch (NullPointerException ok) {} |
2903 |
< |
try { h = f.thenAcceptAsync(null); } catch (NullPointerException ok) {} |
2904 |
< |
try { h = f.thenRunAsync(null); } catch (NullPointerException ok) {} |
2905 |
< |
try { h = f.thenCombineAsync(g, null); } catch (NullPointerException ok) {} |
2906 |
< |
try { h = f.thenCombineAsync(null, null); } catch (NullPointerException ok) {} |
2907 |
< |
try { h = f.applyToEitherAsync(g, null); } catch (NullPointerException ok) {} |
2908 |
< |
try { h = f.applyToEitherAsync(null, null); } catch (NullPointerException ok) {} |
2909 |
< |
try { h = f.thenAcceptBothAsync(g, null); } catch (NullPointerException ok) {} |
2910 |
< |
try { h = f.thenAcceptBothAsync(null, null); } catch (NullPointerException ok) {} |
2911 |
< |
try { h = f.runAfterEitherAsync(g, null); } catch (NullPointerException ok) {} |
2912 |
< |
try { h = f.runAfterEitherAsync(null, null); } catch (NullPointerException ok) {} |
2913 |
< |
try { h = f.runAfterBothAsync(g, null); } catch (NullPointerException ok) {} |
2914 |
< |
try { h = f.runAfterBothAsync(null, null); } catch (NullPointerException ok) {} |
2915 |
< |
|
2882 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2883 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
2884 |
> |
CompletableFuture<Integer> nullFuture = (CompletableFuture<Integer>)null; |
2885 |
> |
CompletableFuture<?> h; |
2886 |
> |
ThreadExecutor exec = new ThreadExecutor(); |
2887 |
> |
|
2888 |
> |
Runnable[] throwingActions = { |
2889 |
> |
() -> CompletableFuture.supplyAsync(null), |
2890 |
> |
() -> CompletableFuture.supplyAsync(null, exec), |
2891 |
> |
() -> CompletableFuture.supplyAsync(new IntegerSupplier(ExecutionMode.DEFAULT, 42), null), |
2892 |
> |
|
2893 |
> |
() -> CompletableFuture.runAsync(null), |
2894 |
> |
() -> CompletableFuture.runAsync(null, exec), |
2895 |
> |
() -> CompletableFuture.runAsync(() -> {}, null), |
2896 |
> |
|
2897 |
> |
() -> f.completeExceptionally(null), |
2898 |
> |
|
2899 |
> |
() -> f.thenApply(null), |
2900 |
> |
() -> f.thenApplyAsync(null), |
2901 |
> |
() -> f.thenApplyAsync((x) -> x, null), |
2902 |
> |
() -> f.thenApplyAsync(null, exec), |
2903 |
> |
|
2904 |
> |
() -> f.thenAccept(null), |
2905 |
> |
() -> f.thenAcceptAsync(null), |
2906 |
> |
() -> f.thenAcceptAsync((x) -> {} , null), |
2907 |
> |
() -> f.thenAcceptAsync(null, exec), |
2908 |
> |
|
2909 |
> |
() -> f.thenRun(null), |
2910 |
> |
() -> f.thenRunAsync(null), |
2911 |
> |
() -> f.thenRunAsync(() -> {} , null), |
2912 |
> |
() -> f.thenRunAsync(null, exec), |
2913 |
> |
|
2914 |
> |
() -> f.thenCombine(g, null), |
2915 |
> |
() -> f.thenCombineAsync(g, null), |
2916 |
> |
() -> f.thenCombineAsync(g, null, exec), |
2917 |
> |
() -> f.thenCombine(nullFuture, (x, y) -> x), |
2918 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x), |
2919 |
> |
() -> f.thenCombineAsync(nullFuture, (x, y) -> x, exec), |
2920 |
> |
() -> f.thenCombineAsync(g, (x, y) -> x, null), |
2921 |
> |
|
2922 |
> |
() -> f.thenAcceptBoth(g, null), |
2923 |
> |
() -> f.thenAcceptBothAsync(g, null), |
2924 |
> |
() -> f.thenAcceptBothAsync(g, null, exec), |
2925 |
> |
() -> f.thenAcceptBoth(nullFuture, (x, y) -> {}), |
2926 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}), |
2927 |
> |
() -> f.thenAcceptBothAsync(nullFuture, (x, y) -> {}, exec), |
2928 |
> |
() -> f.thenAcceptBothAsync(g, (x, y) -> {}, null), |
2929 |
> |
|
2930 |
> |
() -> f.runAfterBoth(g, null), |
2931 |
> |
() -> f.runAfterBothAsync(g, null), |
2932 |
> |
() -> f.runAfterBothAsync(g, null, exec), |
2933 |
> |
() -> f.runAfterBoth(nullFuture, () -> {}), |
2934 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}), |
2935 |
> |
() -> f.runAfterBothAsync(nullFuture, () -> {}, exec), |
2936 |
> |
() -> f.runAfterBothAsync(g, () -> {}, null), |
2937 |
> |
|
2938 |
> |
() -> f.applyToEither(g, null), |
2939 |
> |
() -> f.applyToEitherAsync(g, null), |
2940 |
> |
() -> f.applyToEitherAsync(g, null, exec), |
2941 |
> |
() -> f.applyToEither(nullFuture, (x) -> x), |
2942 |
> |
() -> f.applyToEitherAsync(nullFuture, (x) -> x), |
2943 |
> |
() -> f.applyToEitherAsync(nullFuture, (x) -> x, exec), |
2944 |
> |
() -> f.applyToEitherAsync(g, (x) -> x, null), |
2945 |
> |
|
2946 |
> |
() -> f.acceptEither(g, null), |
2947 |
> |
() -> f.acceptEitherAsync(g, null), |
2948 |
> |
() -> f.acceptEitherAsync(g, null, exec), |
2949 |
> |
() -> f.acceptEither(nullFuture, (x) -> {}), |
2950 |
> |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}), |
2951 |
> |
() -> f.acceptEitherAsync(nullFuture, (x) -> {}, exec), |
2952 |
> |
() -> f.acceptEitherAsync(g, (x) -> {}, null), |
2953 |
> |
|
2954 |
> |
() -> f.runAfterEither(g, null), |
2955 |
> |
() -> f.runAfterEitherAsync(g, null), |
2956 |
> |
() -> f.runAfterEitherAsync(g, null, exec), |
2957 |
> |
() -> f.runAfterEither(nullFuture, () -> {}), |
2958 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}), |
2959 |
> |
() -> f.runAfterEitherAsync(nullFuture, () -> {}, exec), |
2960 |
> |
() -> f.runAfterEitherAsync(g, () -> {}, null), |
2961 |
> |
|
2962 |
> |
() -> f.thenCompose(null), |
2963 |
> |
() -> f.thenComposeAsync(null), |
2964 |
> |
() -> f.thenComposeAsync(new CompletableFutureInc(ExecutionMode.EXECUTOR), null), |
2965 |
> |
() -> f.thenComposeAsync(null, exec), |
2966 |
> |
|
2967 |
> |
() -> f.exceptionally(null), |
2968 |
> |
|
2969 |
> |
() -> f.handle(null), |
2970 |
> |
|
2971 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>)null), |
2972 |
> |
() -> CompletableFuture.allOf((CompletableFuture<?>[])null), |
2973 |
> |
() -> CompletableFuture.allOf(f, null), |
2974 |
> |
() -> CompletableFuture.allOf(null, f), |
2975 |
> |
|
2976 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>)null), |
2977 |
> |
() -> CompletableFuture.anyOf((CompletableFuture<?>[])null), |
2978 |
> |
() -> CompletableFuture.anyOf(f, null), |
2979 |
> |
() -> CompletableFuture.anyOf(null, f), |
2980 |
> |
|
2981 |
> |
() -> f.obtrudeException(null), |
2982 |
> |
}; |
2983 |
> |
|
2984 |
> |
assertThrows(NullPointerException.class, throwingActions); |
2985 |
> |
assertEquals(0, exec.count.get()); |
2986 |
> |
} |
2987 |
> |
|
2988 |
> |
/** |
2989 |
> |
* toCompletableFuture returns this CompletableFuture. |
2990 |
> |
*/ |
2991 |
> |
public void testToCompletableFuture() { |
2992 |
> |
CompletableFuture<Integer> f = new CompletableFuture<>(); |
2993 |
> |
assertSame(f, f.toCompletableFuture()); |
2994 |
|
} |
2995 |
|
|
2429 |
– |
|
2996 |
|
} |