77 |
|
assertTrue(f.toString().contains("[Completed normally]")); |
78 |
|
} |
79 |
|
|
80 |
< |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
81 |
< |
long startTime = System.nanoTime(); |
82 |
< |
long timeoutMillis = LONG_DELAY_MS; |
83 |
< |
try { |
84 |
< |
f.get(timeoutMillis, MILLISECONDS); |
85 |
< |
shouldThrow(); |
86 |
< |
} catch (ExecutionException success) { |
87 |
< |
assertTrue(success.getCause() instanceof CFException); |
88 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
89 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
90 |
< |
|
91 |
< |
try { |
92 |
< |
f.join(); |
93 |
< |
shouldThrow(); |
94 |
< |
} catch (CompletionException success) { |
95 |
< |
assertTrue(success.getCause() instanceof CFException); |
96 |
< |
} |
97 |
< |
try { |
98 |
< |
f.getNow(null); |
99 |
< |
shouldThrow(); |
100 |
< |
} catch (CompletionException success) { |
101 |
< |
assertTrue(success.getCause() instanceof CFException); |
80 |
> |
/** |
81 |
> |
* Returns the "raw" internal exceptional completion of f, |
82 |
> |
* without any additional wrapping with CompletionException. |
83 |
> |
*/ |
84 |
> |
<U> Throwable exceptionalCompletion(CompletableFuture<U> f) { |
85 |
> |
// handle (and whenComplete) can distinguish between "direct" |
86 |
> |
// and "wrapped" exceptional completion |
87 |
> |
return f.handle((U u, Throwable t) -> t).join(); |
88 |
> |
} |
89 |
> |
|
90 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, |
91 |
> |
boolean wrapped, |
92 |
> |
Consumer<Throwable> checker) { |
93 |
> |
Throwable cause = exceptionalCompletion(f); |
94 |
> |
if (wrapped) { |
95 |
> |
assertTrue(cause instanceof CompletionException); |
96 |
> |
cause = cause.getCause(); |
97 |
|
} |
98 |
< |
try { |
104 |
< |
f.get(); |
105 |
< |
shouldThrow(); |
106 |
< |
} catch (ExecutionException success) { |
107 |
< |
assertTrue(success.getCause() instanceof CFException); |
108 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
109 |
< |
assertTrue(f.isDone()); |
110 |
< |
assertFalse(f.isCancelled()); |
111 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
112 |
< |
} |
98 |
> |
checker.accept(cause); |
99 |
|
|
114 |
– |
<U> void checkCompletedExceptionallyWithRootCause(CompletableFuture<U> f, |
115 |
– |
Throwable ex) { |
100 |
|
long startTime = System.nanoTime(); |
117 |
– |
long timeoutMillis = LONG_DELAY_MS; |
101 |
|
try { |
102 |
< |
f.get(timeoutMillis, MILLISECONDS); |
102 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
103 |
|
shouldThrow(); |
104 |
|
} catch (ExecutionException success) { |
105 |
< |
assertSame(ex, success.getCause()); |
105 |
> |
assertSame(cause, success.getCause()); |
106 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
107 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
107 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
108 |
|
|
109 |
|
try { |
110 |
|
f.join(); |
111 |
|
shouldThrow(); |
112 |
|
} catch (CompletionException success) { |
113 |
< |
assertSame(ex, success.getCause()); |
114 |
< |
} |
113 |
> |
assertSame(cause, success.getCause()); |
114 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
115 |
> |
|
116 |
|
try { |
117 |
|
f.getNow(null); |
118 |
|
shouldThrow(); |
119 |
|
} catch (CompletionException success) { |
120 |
< |
assertSame(ex, success.getCause()); |
121 |
< |
} |
120 |
> |
assertSame(cause, success.getCause()); |
121 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
122 |
> |
|
123 |
|
try { |
124 |
|
f.get(); |
125 |
|
shouldThrow(); |
126 |
|
} catch (ExecutionException success) { |
127 |
< |
assertSame(ex, success.getCause()); |
127 |
> |
assertSame(cause, success.getCause()); |
128 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
129 |
|
|
145 |
– |
assertTrue(f.isDone()); |
130 |
|
assertFalse(f.isCancelled()); |
131 |
+ |
assertTrue(f.isDone()); |
132 |
+ |
assertTrue(f.isCompletedExceptionally()); |
133 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
134 |
|
} |
135 |
|
|
136 |
< |
<U> void checkCompletedExceptionallyWithTimeout(CompletableFuture<U> f) { |
137 |
< |
long startTime = System.nanoTime(); |
138 |
< |
long timeoutMillis = LONG_DELAY_MS; |
139 |
< |
try { |
154 |
< |
f.get(timeoutMillis, MILLISECONDS); |
155 |
< |
shouldThrow(); |
156 |
< |
} catch (ExecutionException ex) { |
157 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
158 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
159 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
160 |
< |
|
161 |
< |
try { |
162 |
< |
f.join(); |
163 |
< |
shouldThrow(); |
164 |
< |
} catch (Throwable ex) { |
165 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
166 |
< |
} |
167 |
< |
|
168 |
< |
try { |
169 |
< |
f.getNow(null); |
170 |
< |
shouldThrow(); |
171 |
< |
} catch (Throwable ex) { |
172 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
173 |
< |
} |
136 |
> |
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
137 |
> |
checkCompletedExceptionally(f, true, |
138 |
> |
(t) -> assertTrue(t instanceof CFException)); |
139 |
> |
} |
140 |
|
|
141 |
< |
try { |
142 |
< |
f.get(); |
143 |
< |
shouldThrow(); |
144 |
< |
} catch (ExecutionException ex) { |
179 |
< |
assertTrue(ex.getCause() instanceof TimeoutException); |
180 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
141 |
> |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
142 |
> |
checkCompletedExceptionally(f, true, |
143 |
> |
(t) -> assertTrue(t instanceof CancellationException)); |
144 |
> |
} |
145 |
|
|
146 |
< |
assertTrue(f.isDone()); |
147 |
< |
assertFalse(f.isCancelled()); |
148 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
146 |
> |
void checkCompletedWithTimeoutException(CompletableFuture<?> f) { |
147 |
> |
checkCompletedExceptionally(f, false, |
148 |
> |
(t) -> assertTrue(t instanceof TimeoutException)); |
149 |
|
} |
150 |
|
|
151 |
< |
<U> void checkCompletedWithWrappedException(CompletableFuture<U> f, |
152 |
< |
Throwable ex) { |
153 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
190 |
< |
try { |
191 |
< |
CompletableFuture<Throwable> spy = f.handle |
192 |
< |
((U u, Throwable t) -> t); |
193 |
< |
assertTrue(spy.join() instanceof CompletionException); |
194 |
< |
assertSame(ex, spy.join().getCause()); |
195 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
151 |
> |
void checkCompletedWithWrappedException(CompletableFuture<?> f, |
152 |
> |
Throwable ex) { |
153 |
> |
checkCompletedExceptionally(f, true, (t) -> assertSame(t, ex)); |
154 |
|
} |
155 |
|
|
156 |
< |
<U> void checkCompletedExceptionally(CompletableFuture<U> f, Throwable ex) { |
157 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
200 |
< |
try { |
201 |
< |
CompletableFuture<Throwable> spy = f.handle |
202 |
< |
((U u, Throwable t) -> t); |
203 |
< |
assertSame(ex, spy.join()); |
204 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
156 |
> |
void checkCompletedExceptionally(CompletableFuture<?> f, Throwable ex) { |
157 |
> |
checkCompletedExceptionally(f, false, (t) -> assertSame(t, ex)); |
158 |
|
} |
159 |
|
|
160 |
|
void checkCancelled(CompletableFuture<?> f) { |
161 |
|
long startTime = System.nanoTime(); |
209 |
– |
long timeoutMillis = LONG_DELAY_MS; |
162 |
|
try { |
163 |
< |
f.get(timeoutMillis, MILLISECONDS); |
163 |
> |
f.get(LONG_DELAY_MS, MILLISECONDS); |
164 |
|
shouldThrow(); |
165 |
|
} catch (CancellationException success) { |
166 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
167 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
167 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
168 |
|
|
169 |
|
try { |
170 |
|
f.join(); |
179 |
|
shouldThrow(); |
180 |
|
} catch (CancellationException success) { |
181 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
230 |
– |
assertTrue(f.isDone()); |
231 |
– |
assertTrue(f.isCompletedExceptionally()); |
232 |
– |
assertTrue(f.isCancelled()); |
233 |
– |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
234 |
– |
} |
182 |
|
|
183 |
< |
void checkCompletedWithWrappedCancellationException(CompletableFuture<?> f) { |
237 |
< |
long startTime = System.nanoTime(); |
238 |
< |
long timeoutMillis = LONG_DELAY_MS; |
239 |
< |
try { |
240 |
< |
f.get(timeoutMillis, MILLISECONDS); |
241 |
< |
shouldThrow(); |
242 |
< |
} catch (ExecutionException success) { |
243 |
< |
assertTrue(success.getCause() instanceof CancellationException); |
244 |
< |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
245 |
< |
assertTrue(millisElapsedSince(startTime) < timeoutMillis/2); |
183 |
> |
assertTrue(exceptionalCompletion(f) instanceof CancellationException); |
184 |
|
|
247 |
– |
try { |
248 |
– |
f.join(); |
249 |
– |
shouldThrow(); |
250 |
– |
} catch (CompletionException success) { |
251 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
252 |
– |
} |
253 |
– |
try { |
254 |
– |
f.getNow(null); |
255 |
– |
shouldThrow(); |
256 |
– |
} catch (CompletionException success) { |
257 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
258 |
– |
} |
259 |
– |
try { |
260 |
– |
f.get(); |
261 |
– |
shouldThrow(); |
262 |
– |
} catch (ExecutionException success) { |
263 |
– |
assertTrue(success.getCause() instanceof CancellationException); |
264 |
– |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
185 |
|
assertTrue(f.isDone()); |
266 |
– |
assertFalse(f.isCancelled()); |
186 |
|
assertTrue(f.isCompletedExceptionally()); |
187 |
+ |
assertTrue(f.isCancelled()); |
188 |
|
assertTrue(f.toString().contains("[Completed exceptionally]")); |
189 |
|
} |
190 |
|
|
3062 |
|
for (int i = 0; i < k; i++) { |
3063 |
|
checkIncomplete(f); |
3064 |
|
checkIncomplete(CompletableFuture.allOf(fs)); |
3065 |
< |
if (i != k/2) { |
3065 |
> |
if (i != k / 2) { |
3066 |
|
fs[i].complete(i); |
3067 |
|
checkCompletedNormally(fs[i], i); |
3068 |
|
} else { |
3269 |
|
() -> CompletableFuture.delayedExecutor(1L, SECONDS, null), |
3270 |
|
() -> CompletableFuture.delayedExecutor(1L, null, new ThreadExecutor()), |
3271 |
|
() -> CompletableFuture.delayedExecutor(1L, null), |
3272 |
+ |
|
3273 |
+ |
() -> f.orTimeout(1L, null), |
3274 |
+ |
() -> f.completeOnTimeout(42, 1L, null), |
3275 |
+ |
|
3276 |
+ |
() -> CompletableFuture.failedFuture(null), |
3277 |
+ |
() -> CompletableFuture.failedStage(null), |
3278 |
|
}; |
3279 |
|
|
3280 |
|
assertThrows(NullPointerException.class, throwingActions); |
3295 |
|
* newIncompleteFuture returns an incomplete CompletableFuture |
3296 |
|
*/ |
3297 |
|
public void testNewIncompleteFuture() { |
3298 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3299 |
+ |
{ |
3300 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3301 |
|
CompletableFuture<Integer> g = f.newIncompleteFuture(); |
3302 |
|
checkIncomplete(f); |
3303 |
|
checkIncomplete(g); |
3304 |
< |
} |
3304 |
> |
f.complete(v1); |
3305 |
> |
checkCompletedNormally(f, v1); |
3306 |
> |
checkIncomplete(g); |
3307 |
> |
g.complete(v1); |
3308 |
> |
checkCompletedNormally(g, v1); |
3309 |
> |
assertSame(g.getClass(), CompletableFuture.class); |
3310 |
> |
}} |
3311 |
|
|
3312 |
|
/** |
3313 |
|
* completedStage returns a completed CompletionStage |
3314 |
|
*/ |
3315 |
|
public void testCompletedStage() { |
3316 |
< |
AtomicInteger x = new AtomicInteger(); |
3316 |
> |
AtomicInteger x = new AtomicInteger(0); |
3317 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3318 |
|
CompletionStage<Integer> f = CompletableFuture.completedStage(1); |
3319 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3342 |
|
public void testFailedFuture() { |
3343 |
|
CFException ex = new CFException(); |
3344 |
|
CompletableFuture<Integer> f = CompletableFuture.failedFuture(ex); |
3345 |
< |
checkCompletedExceptionallyWithRootCause(f, ex); |
3345 |
> |
checkCompletedExceptionally(f, ex); |
3346 |
|
} |
3347 |
|
|
3348 |
|
/** |
3349 |
|
* failedFuture(null) throws NPE |
3350 |
|
*/ |
3351 |
< |
public void testFailedFuture2() { |
3351 |
> |
public void testFailedFuture_null() { |
3352 |
|
try { |
3353 |
|
CompletableFuture<Integer> f = CompletableFuture.failedFuture(null); |
3354 |
|
shouldThrow(); |
3381 |
|
CFException ex = new CFException(); |
3382 |
|
f.completeExceptionally(ex); |
3383 |
|
checkCompletedExceptionally(f, ex); |
3384 |
< |
checkCompletedWithWrappedCFException(g); |
3384 |
> |
checkCompletedWithWrappedException(g, ex); |
3385 |
|
} |
3386 |
|
|
3387 |
|
/** |
3391 |
|
public void testMinimalCompletionStage() { |
3392 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3393 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3394 |
< |
AtomicInteger x = new AtomicInteger(); |
3394 |
> |
AtomicInteger x = new AtomicInteger(0); |
3395 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3396 |
|
checkIncomplete(f); |
3397 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3408 |
|
public void testMinimalCompletionStage2() { |
3409 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3410 |
|
CompletionStage<Integer> g = f.minimalCompletionStage(); |
3411 |
< |
AtomicInteger x = new AtomicInteger(); |
3411 |
> |
AtomicInteger x = new AtomicInteger(0); |
3412 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3413 |
|
g.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3414 |
|
checkIncomplete(f); |
3426 |
|
public void testFailedStage() { |
3427 |
|
CFException ex = new CFException(); |
3428 |
|
CompletionStage<Integer> f = CompletableFuture.failedStage(ex); |
3429 |
< |
AtomicInteger x = new AtomicInteger(); |
3429 |
> |
AtomicInteger x = new AtomicInteger(0); |
3430 |
|
AtomicReference<Throwable> r = new AtomicReference<Throwable>(); |
3431 |
|
f.whenComplete((v, e) -> {if (e != null) r.set(e); else x.set(v);}); |
3432 |
|
assertEquals(x.get(), 0); |
3433 |
< |
assertEquals(r.get().getCause(), ex); |
3433 |
> |
assertEquals(r.get(), ex); |
3434 |
|
} |
3435 |
|
|
3436 |
|
/** |
3437 |
|
* completeAsync completes with value of given supplier |
3438 |
|
*/ |
3439 |
|
public void testCompleteAsync() { |
3440 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3441 |
+ |
{ |
3442 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3443 |
< |
f.completeAsync(() -> 1); |
3443 |
> |
f.completeAsync(() -> v1); |
3444 |
|
f.join(); |
3445 |
< |
checkCompletedNormally(f, 1); |
3446 |
< |
} |
3445 |
> |
checkCompletedNormally(f, v1); |
3446 |
> |
}} |
3447 |
|
|
3448 |
|
/** |
3449 |
|
* completeAsync completes exceptionally if given supplier throws |
3455 |
|
try { |
3456 |
|
f.join(); |
3457 |
|
shouldThrow(); |
3458 |
< |
} catch (Exception success) {} |
3459 |
< |
checkCompletedWithWrappedCFException(f); |
3458 |
> |
} catch (CompletionException success) {} |
3459 |
> |
checkCompletedWithWrappedException(f, ex); |
3460 |
|
} |
3461 |
|
|
3462 |
|
/** |
3463 |
|
* completeAsync with given executor completes with value of given supplier |
3464 |
|
*/ |
3465 |
|
public void testCompleteAsync3() { |
3466 |
+ |
for (Integer v1 : new Integer[] { 1, null }) |
3467 |
+ |
{ |
3468 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3469 |
< |
f.completeAsync(() -> 1, new ThreadExecutor()); |
3470 |
< |
f.join(); |
3471 |
< |
checkCompletedNormally(f, 1); |
3472 |
< |
} |
3469 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3470 |
> |
f.completeAsync(() -> v1, executor); |
3471 |
> |
assertSame(v1, f.join()); |
3472 |
> |
checkCompletedNormally(f, v1); |
3473 |
> |
assertEquals(1, executor.count.get()); |
3474 |
> |
}} |
3475 |
|
|
3476 |
|
/** |
3477 |
|
* completeAsync with given executor completes exceptionally if |
3480 |
|
public void testCompleteAsync4() { |
3481 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3482 |
|
CFException ex = new CFException(); |
3483 |
< |
f.completeAsync(() -> {if (true) throw ex; return 1;}, new ThreadExecutor()); |
3483 |
> |
ThreadExecutor executor = new ThreadExecutor(); |
3484 |
> |
f.completeAsync(() -> {if (true) throw ex; return 1;}, executor); |
3485 |
|
try { |
3486 |
|
f.join(); |
3487 |
|
shouldThrow(); |
3488 |
< |
} catch (Exception success) {} |
3489 |
< |
checkCompletedWithWrappedCFException(f); |
3488 |
> |
} catch (CompletionException success) {} |
3489 |
> |
checkCompletedWithWrappedException(f, ex); |
3490 |
> |
assertEquals(1, executor.count.get()); |
3491 |
|
} |
3492 |
|
|
3493 |
|
/** |
3494 |
|
* orTimeout completes with TimeoutException if not complete |
3495 |
|
*/ |
3496 |
< |
public void testOrTimeout() { |
3496 |
> |
public void testOrTimeout_timesOut() { |
3497 |
> |
long timeoutMillis = timeoutMillis(); |
3498 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3499 |
< |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3500 |
< |
checkCompletedExceptionallyWithTimeout(f); |
3499 |
> |
long startTime = System.nanoTime(); |
3500 |
> |
f.orTimeout(timeoutMillis, MILLISECONDS); |
3501 |
> |
checkCompletedWithTimeoutException(f); |
3502 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3503 |
|
} |
3504 |
|
|
3505 |
|
/** |
3506 |
|
* orTimeout completes normally if completed before timeout |
3507 |
|
*/ |
3508 |
< |
public void testOrTimeout2() { |
3508 |
> |
public void testOrTimeout_completed() { |
3509 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3510 |
> |
{ |
3511 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3512 |
< |
f.complete(1); |
3513 |
< |
f.orTimeout(SHORT_DELAY_MS, MILLISECONDS); |
3514 |
< |
checkCompletedNormally(f, 1); |
3515 |
< |
} |
3512 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3513 |
> |
long startTime = System.nanoTime(); |
3514 |
> |
f.complete(v1); |
3515 |
> |
f.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3516 |
> |
g.orTimeout(LONG_DELAY_MS, MILLISECONDS); |
3517 |
> |
g.complete(v1); |
3518 |
> |
checkCompletedNormally(f, v1); |
3519 |
> |
checkCompletedNormally(g, v1); |
3520 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3521 |
> |
}} |
3522 |
|
|
3523 |
|
/** |
3524 |
|
* completeOnTimeout completes with given value if not complete |
3525 |
|
*/ |
3526 |
< |
public void testCompleteOnTimeout() { |
3526 |
> |
public void testCompleteOnTimeout_timesOut() { |
3527 |
> |
testInParallel(() -> testCompleteOnTimeout_timesOut(42), |
3528 |
> |
() -> testCompleteOnTimeout_timesOut(null)); |
3529 |
> |
} |
3530 |
> |
|
3531 |
> |
public void testCompleteOnTimeout_timesOut(Integer v) { |
3532 |
> |
long timeoutMillis = timeoutMillis(); |
3533 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3534 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3535 |
< |
f.join(); |
3536 |
< |
checkCompletedNormally(f, -1); |
3534 |
> |
long startTime = System.nanoTime(); |
3535 |
> |
f.completeOnTimeout(v, timeoutMillis, MILLISECONDS); |
3536 |
> |
assertSame(v, f.join()); |
3537 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
3538 |
> |
f.complete(99); // should have no effect |
3539 |
> |
checkCompletedNormally(f, v); |
3540 |
|
} |
3541 |
|
|
3542 |
|
/** |
3543 |
|
* completeOnTimeout has no effect if completed within timeout |
3544 |
|
*/ |
3545 |
< |
public void testCompleteOnTimeout2() { |
3545 |
> |
public void testCompleteOnTimeout_completed() { |
3546 |
> |
for (Integer v1 : new Integer[] { 1, null }) |
3547 |
> |
{ |
3548 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3549 |
< |
f.complete(1); |
3550 |
< |
f.completeOnTimeout(-1, SHORT_DELAY_MS, MILLISECONDS); |
3551 |
< |
checkCompletedNormally(f, 1); |
3552 |
< |
} |
3549 |
> |
CompletableFuture<Integer> g = new CompletableFuture<>(); |
3550 |
> |
long startTime = System.nanoTime(); |
3551 |
> |
f.complete(v1); |
3552 |
> |
f.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3553 |
> |
g.completeOnTimeout(-1, LONG_DELAY_MS, MILLISECONDS); |
3554 |
> |
g.complete(v1); |
3555 |
> |
checkCompletedNormally(f, v1); |
3556 |
> |
checkCompletedNormally(g, v1); |
3557 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS / 2); |
3558 |
> |
}} |
3559 |
|
|
3560 |
|
/** |
3561 |
|
* delayedExecutor returns an executor that delays submission |
3562 |
|
*/ |
3563 |
< |
public void testDelayedExecutor() throws Exception { |
3564 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3565 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3566 |
< |
MILLISECONDS); |
3567 |
< |
long startTime = System.nanoTime(); |
3598 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3599 |
< |
assertNull(f.getNow(null)); |
3600 |
< |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3601 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3602 |
< |
checkCompletedNormally(f, 1); |
3563 |
> |
public void testDelayedExecutor() { |
3564 |
> |
testInParallel(() -> testDelayedExecutor(null, null), |
3565 |
> |
() -> testDelayedExecutor(null, 1), |
3566 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1), |
3567 |
> |
() -> testDelayedExecutor(new ThreadExecutor(), 1)); |
3568 |
|
} |
3569 |
|
|
3570 |
|
/** |
3571 |
< |
* delayedExecutor for a given executor returns an executor that |
3607 |
< |
* delays submission |
3571 |
> |
* delayedExecutor returns an executor that delays submission |
3572 |
|
*/ |
3573 |
< |
public void testDelayedExecutor2() throws Exception { |
3574 |
< |
long timeoutMillis = SMALL_DELAY_MS; |
3575 |
< |
Executor d = CompletableFuture.delayedExecutor(timeoutMillis, |
3576 |
< |
MILLISECONDS, |
3577 |
< |
new ThreadExecutor()); |
3573 |
> |
public void testDelayedExecutor(Executor executor, Integer v) throws Exception { |
3574 |
> |
long timeoutMillis = timeoutMillis(); |
3575 |
> |
// Use an "unreasonably long" long timeout to catch lingering threads |
3576 |
> |
long longTimeoutMillis = 1000 * 60 * 60 * 24; |
3577 |
> |
final Executor delayer, longDelayer; |
3578 |
> |
if (executor == null) { |
3579 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS); |
3580 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS); |
3581 |
> |
} else { |
3582 |
> |
delayer = CompletableFuture.delayedExecutor(timeoutMillis, MILLISECONDS, executor); |
3583 |
> |
longDelayer = CompletableFuture.delayedExecutor(longTimeoutMillis, MILLISECONDS, executor); |
3584 |
> |
} |
3585 |
|
long startTime = System.nanoTime(); |
3586 |
< |
CompletableFuture<Integer> f = CompletableFuture.supplyAsync(() -> 1, d); |
3587 |
< |
assertNull(f.getNow(null)); |
3588 |
< |
assertEquals(1, (int) f.get(LONG_DELAY_MS, MILLISECONDS)); |
3589 |
< |
assertTrue(millisElapsedSince(startTime) > timeoutMillis/2); |
3590 |
< |
checkCompletedNormally(f, 1); |
3586 |
> |
CompletableFuture<Integer> f = |
3587 |
> |
CompletableFuture.supplyAsync(() -> v, delayer); |
3588 |
> |
CompletableFuture<Integer> g = |
3589 |
> |
CompletableFuture.supplyAsync(() -> v, longDelayer); |
3590 |
> |
|
3591 |
> |
assertNull(g.getNow(null)); |
3592 |
> |
|
3593 |
> |
assertSame(v, f.get(LONG_DELAY_MS, MILLISECONDS)); |
3594 |
> |
long millisElapsed = millisElapsedSince(startTime); |
3595 |
> |
assertTrue(millisElapsed >= timeoutMillis); |
3596 |
> |
assertTrue(millisElapsed < LONG_DELAY_MS / 2); |
3597 |
> |
|
3598 |
> |
checkCompletedNormally(f, v); |
3599 |
> |
|
3600 |
> |
checkIncomplete(g); |
3601 |
> |
assertTrue(g.cancel(true)); |
3602 |
|
} |
3603 |
|
|
3604 |
|
//--- tests of implementation details; not part of official tck --- |