41 |
|
import java.util.function.Predicate; |
42 |
|
import java.util.function.Supplier; |
43 |
|
|
44 |
– |
import junit.framework.AssertionFailedError; |
44 |
|
import junit.framework.Test; |
45 |
|
import junit.framework.TestSuite; |
46 |
|
|
58 |
|
void checkIncomplete(CompletableFuture<?> f) { |
59 |
|
assertFalse(f.isDone()); |
60 |
|
assertFalse(f.isCancelled()); |
61 |
< |
assertTrue(f.toString().contains("Not completed")); |
61 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
62 |
> |
|
63 |
> |
Object result = null; |
64 |
|
try { |
65 |
< |
assertNull(f.getNow(null)); |
65 |
> |
result = f.getNow(null); |
66 |
|
} catch (Throwable fail) { threadUnexpectedException(fail); } |
67 |
+ |
assertNull(result); |
68 |
+ |
|
69 |
|
try { |
70 |
|
f.get(randomExpiredTimeout(), randomTimeUnit()); |
71 |
|
shouldThrow(); |
85 |
|
assertTrue(f.isDone()); |
86 |
|
assertFalse(f.isCancelled()); |
87 |
|
assertFalse(f.isCompletedExceptionally()); |
88 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
88 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
141 |
|
assertFalse(f.isCancelled()); |
142 |
|
assertTrue(f.isDone()); |
143 |
|
assertTrue(f.isCompletedExceptionally()); |
144 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
144 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
145 |
|
} |
146 |
|
|
147 |
|
void checkCompletedWithWrappedCFException(CompletableFuture<?> f) { |
196 |
|
assertTrue(f.isDone()); |
197 |
|
assertTrue(f.isCompletedExceptionally()); |
198 |
|
assertTrue(f.isCancelled()); |
199 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
199 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
200 |
|
} |
201 |
|
|
202 |
|
/** |
295 |
|
} |
296 |
|
|
297 |
|
f = new CompletableFuture<>(); |
298 |
< |
f.completeExceptionally(ex = new CFException()); |
298 |
> |
f.completeExceptionally(new CFException()); |
299 |
|
f.obtrudeValue(v1); |
300 |
|
checkCompletedNormally(f, v1); |
301 |
|
f.obtrudeException(ex = new CFException()); |
332 |
|
/** |
333 |
|
* toString indicates current completion state |
334 |
|
*/ |
335 |
< |
public void testToString() { |
336 |
< |
CompletableFuture<String> f; |
337 |
< |
|
338 |
< |
f = new CompletableFuture<String>(); |
339 |
< |
assertTrue(f.toString().contains("[Not completed]")); |
335 |
> |
public void testToString_incomplete() { |
336 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
337 |
> |
assertTrue(f.toString().matches(".*\\[.*Not completed.*\\]")); |
338 |
> |
if (testImplementationDetails) |
339 |
> |
assertEquals(identityString(f) + "[Not completed]", |
340 |
> |
f.toString()); |
341 |
> |
} |
342 |
|
|
343 |
+ |
public void testToString_normal() { |
344 |
+ |
CompletableFuture<String> f = new CompletableFuture<>(); |
345 |
|
assertTrue(f.complete("foo")); |
346 |
< |
assertTrue(f.toString().contains("[Completed normally]")); |
346 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed normally.*\\]")); |
347 |
> |
if (testImplementationDetails) |
348 |
> |
assertEquals(identityString(f) + "[Completed normally]", |
349 |
> |
f.toString()); |
350 |
> |
} |
351 |
|
|
352 |
< |
f = new CompletableFuture<String>(); |
352 |
> |
public void testToString_exception() { |
353 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
354 |
|
assertTrue(f.completeExceptionally(new IndexOutOfBoundsException())); |
355 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
355 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
356 |
> |
if (testImplementationDetails) |
357 |
> |
assertTrue(f.toString().startsWith( |
358 |
> |
identityString(f) + "[Completed exceptionally: ")); |
359 |
> |
} |
360 |
|
|
361 |
+ |
public void testToString_cancelled() { |
362 |
|
for (boolean mayInterruptIfRunning : new boolean[] { true, false }) { |
363 |
< |
f = new CompletableFuture<String>(); |
363 |
> |
CompletableFuture<String> f = new CompletableFuture<>(); |
364 |
|
assertTrue(f.cancel(mayInterruptIfRunning)); |
365 |
< |
assertTrue(f.toString().contains("[Completed exceptionally]")); |
365 |
> |
assertTrue(f.toString().matches(".*\\[.*Completed exceptionally.*\\]")); |
366 |
> |
if (testImplementationDetails) |
367 |
> |
assertTrue(f.toString().startsWith( |
368 |
> |
identityString(f) + "[Completed exceptionally: ")); |
369 |
|
} |
370 |
|
} |
371 |
|
|
1258 |
|
r.assertInvoked(); |
1259 |
|
}} |
1260 |
|
|
1261 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1262 |
|
public void testRunAsync_rejectingExecutor() { |
1263 |
|
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1264 |
|
try { |
1305 |
|
r.assertInvoked(); |
1306 |
|
}} |
1307 |
|
|
1308 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1309 |
|
public void testSupplyAsync_rejectingExecutor() { |
1310 |
|
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1311 |
|
try { |
3256 |
|
/** |
3257 |
|
* Completion methods throw NullPointerException with null arguments |
3258 |
|
*/ |
3259 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
3260 |
|
public void testNPE() { |
3261 |
|
CompletableFuture<Integer> f = new CompletableFuture<>(); |
3262 |
|
CompletableFuture<Integer> g = new CompletableFuture<>(); |
4193 |
|
static void assertZero(CompletableFuture<?> f) { |
4194 |
|
try { |
4195 |
|
f.getNow(null); |
4196 |
< |
throw new AssertionFailedError("should throw"); |
4196 |
> |
throw new AssertionError("should throw"); |
4197 |
|
} catch (CompletionException success) { |
4198 |
|
assertTrue(success.getCause() instanceof ZeroException); |
4199 |
|
} |
4318 |
|
} |
4319 |
|
|
4320 |
|
/** Test long recursive chains of CompletableFutures with cascading completions */ |
4321 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4322 |
|
public void testRecursiveChains() throws Throwable { |
4323 |
|
for (ExecutionMode m : ExecutionMode.values()) |
4324 |
|
for (boolean addDeadEnds : new boolean[] { true, false }) |
4343 |
|
* A single CompletableFuture with many dependents. |
4344 |
|
* A demo of scalability - runtime is O(n). |
4345 |
|
*/ |
4346 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4347 |
|
public void testManyDependents() throws Throwable { |
4348 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4349 |
|
final CompletableFuture<Void> head = new CompletableFuture<>(); |
4373 |
|
} |
4374 |
|
|
4375 |
|
/** ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4376 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
4377 |
|
public void testCoCompletionGarbageRetention() throws Throwable { |
4378 |
|
final int n = expensiveTests ? 1_000_000 : 10; |
4379 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4392 |
|
f.complete(null); |
4393 |
|
|
4394 |
|
f = new CompletableFuture<>(); |
4395 |
< |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4395 |
> |
CompletableFuture.anyOf(f, incomplete); |
4396 |
|
f.complete(null); |
4397 |
|
} |
4398 |
|
|
4410 |
|
f.complete(null); |
4411 |
|
|
4412 |
|
f = new CompletableFuture<>(); |
4413 |
< |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4413 |
> |
CompletableFuture.anyOf(incomplete, f); |
4414 |
|
f.complete(null); |
4415 |
|
} |
4416 |
|
} |