550 |
|
} |
551 |
|
} |
552 |
|
|
553 |
+ |
static class CountingRejectingExecutor implements Executor { |
554 |
+ |
final RejectedExecutionException ex = new RejectedExecutionException(); |
555 |
+ |
final AtomicInteger count = new AtomicInteger(0); |
556 |
+ |
public void execute(Runnable r) { |
557 |
+ |
count.getAndIncrement(); |
558 |
+ |
throw ex; |
559 |
+ |
} |
560 |
+ |
} |
561 |
+ |
|
562 |
|
// Used for explicit executor tests |
563 |
|
static final class ThreadExecutor implements Executor { |
564 |
|
final AtomicInteger count = new AtomicInteger(0); |
1243 |
|
r.assertInvoked(); |
1244 |
|
}} |
1245 |
|
|
1246 |
+ |
public void testRunAsync_rejectingExecutor() { |
1247 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1248 |
+ |
try { |
1249 |
+ |
CompletableFuture.runAsync(() -> {}, e); |
1250 |
+ |
shouldThrow(); |
1251 |
+ |
} catch (Throwable t) { |
1252 |
+ |
assertSame(e.ex, t); |
1253 |
+ |
} |
1254 |
+ |
|
1255 |
+ |
assertEquals(1, e.count.get()); |
1256 |
+ |
} |
1257 |
+ |
|
1258 |
|
/** |
1259 |
|
* supplyAsync completes with result of supplier |
1260 |
|
*/ |
1289 |
|
r.assertInvoked(); |
1290 |
|
}} |
1291 |
|
|
1292 |
+ |
public void testSupplyAsync_rejectingExecutor() { |
1293 |
+ |
CountingRejectingExecutor e = new CountingRejectingExecutor(); |
1294 |
+ |
try { |
1295 |
+ |
CompletableFuture.supplyAsync(() -> null, e); |
1296 |
+ |
shouldThrow(); |
1297 |
+ |
} catch (Throwable t) { |
1298 |
+ |
assertSame(e.ex, t); |
1299 |
+ |
} |
1300 |
+ |
|
1301 |
+ |
assertEquals(1, e.count.get()); |
1302 |
+ |
} |
1303 |
+ |
|
1304 |
|
// seq completion methods |
1305 |
|
|
1306 |
|
/** |
3354 |
|
* Test submissions to an executor that rejects all tasks. |
3355 |
|
*/ |
3356 |
|
public void testRejectingExecutor() { |
3324 |
– |
final RejectedExecutionException ex = new RejectedExecutionException(); |
3325 |
– |
final Executor e = (Runnable r) -> { throw ex; }; |
3326 |
– |
|
3357 |
|
for (Integer v : new Integer[] { 1, null }) { |
3358 |
|
|
3359 |
+ |
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3360 |
+ |
|
3361 |
|
final CompletableFuture<Integer> complete = CompletableFuture.completedFuture(v); |
3362 |
|
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
3363 |
|
|
3387 |
|
|
3388 |
|
for (CompletableFuture<?> future : fs) { |
3389 |
|
if (src.isDone()) |
3390 |
< |
checkCompletedWithWrappedException(future, ex); |
3390 |
> |
checkCompletedWithWrappedException(future, e.ex); |
3391 |
|
else |
3392 |
|
checkIncomplete(future); |
3393 |
|
} |
3424 |
|
fs.add(incomplete.runAfterEitherAsync(complete, () -> {}, e)); |
3425 |
|
|
3426 |
|
for (CompletableFuture<?> future : fs) |
3427 |
< |
checkCompletedWithWrappedException(future, ex); |
3427 |
> |
checkCompletedWithWrappedException(future, e.ex); |
3428 |
|
futures.addAll(fs); |
3429 |
|
} |
3430 |
|
|
3431 |
|
incomplete.complete(v); |
3432 |
|
|
3433 |
|
for (CompletableFuture<?> future : futures) |
3434 |
< |
checkCompletedWithWrappedException(future, ex); |
3434 |
> |
checkCompletedWithWrappedException(future, e.ex); |
3435 |
> |
|
3436 |
> |
assertEquals(futures.size(), e.count.get()); |
3437 |
> |
|
3438 |
|
} |
3439 |
|
} |
3440 |
|
|
3444 |
|
* explicitly completed. |
3445 |
|
*/ |
3446 |
|
public void testRejectingExecutorNeverInvoked() { |
3412 |
– |
final RejectedExecutionException ex = new RejectedExecutionException(); |
3413 |
– |
class CountingRejectingExecutor implements Executor { |
3414 |
– |
final AtomicInteger count = new AtomicInteger(0); |
3415 |
– |
public void execute(Runnable r) { |
3416 |
– |
count.getAndIncrement(); |
3417 |
– |
throw ex; |
3418 |
– |
} |
3419 |
– |
} |
3447 |
|
final CountingRejectingExecutor e = new CountingRejectingExecutor(); |
3448 |
|
|
3449 |
|
for (Integer v : new Integer[] { 1, null }) { |
4170 |
|
assertEquals(5 * 3 * n, count.get()); |
4171 |
|
} |
4172 |
|
|
4173 |
+ |
/** ant -Dvmoptions=-Xmx8m -Djsr166.tckTestClass=CompletableFutureTest tck */ |
4174 |
+ |
public void testCoCompletionGarbage() throws Throwable { |
4175 |
+ |
// final int n = 3_000_000; |
4176 |
+ |
final int n = 100; |
4177 |
+ |
final CompletableFuture<Integer> incomplete = new CompletableFuture<>(); |
4178 |
+ |
CompletableFuture<Integer> f; |
4179 |
+ |
for (int i = 0; i < n; i++) { |
4180 |
+ |
f = new CompletableFuture<>(); |
4181 |
+ |
f.runAfterEither(incomplete, () -> {}); |
4182 |
+ |
f.complete(null); |
4183 |
+ |
|
4184 |
+ |
f = new CompletableFuture<>(); |
4185 |
+ |
f.acceptEither(incomplete, (x) -> {}); |
4186 |
+ |
f.complete(null); |
4187 |
+ |
|
4188 |
+ |
f = new CompletableFuture<>(); |
4189 |
+ |
f.applyToEither(incomplete, (x) -> x); |
4190 |
+ |
f.complete(null); |
4191 |
+ |
|
4192 |
+ |
f = new CompletableFuture<>(); |
4193 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { f, incomplete }); |
4194 |
+ |
f.complete(null); |
4195 |
+ |
} |
4196 |
+ |
|
4197 |
+ |
for (int i = 0; i < n; i++) { |
4198 |
+ |
f = new CompletableFuture<>(); |
4199 |
+ |
incomplete.runAfterEither(f, () -> {}); |
4200 |
+ |
f.complete(null); |
4201 |
+ |
|
4202 |
+ |
f = new CompletableFuture<>(); |
4203 |
+ |
incomplete.acceptEither(f, (x) -> {}); |
4204 |
+ |
f.complete(null); |
4205 |
+ |
|
4206 |
+ |
f = new CompletableFuture<>(); |
4207 |
+ |
incomplete.applyToEither(f, (x) -> x); |
4208 |
+ |
f.complete(null); |
4209 |
+ |
|
4210 |
+ |
f = new CompletableFuture<>(); |
4211 |
+ |
CompletableFuture.anyOf(new CompletableFuture<?>[] { incomplete, f }); |
4212 |
+ |
f.complete(null); |
4213 |
+ |
} |
4214 |
+ |
} |
4215 |
+ |
|
4216 |
+ |
/* |
4217 |
+ |
* Tests below currently fail in stress mode due to memory retention. |
4218 |
+ |
* ant -Dvmoptions=-Xmx8m -Djsr166.expensiveTests=true -Djsr166.tckTestClass=CompletableFutureTest tck |
4219 |
+ |
*/ |
4220 |
+ |
|
4221 |
+ |
/** Checks for garbage retention with anyOf. */ |
4222 |
+ |
public void testAnyOfGarbageRetention() throws Throwable { |
4223 |
+ |
for (Integer v : new Integer[] { 1, null }) |
4224 |
+ |
{ |
4225 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4226 |
+ |
CompletableFuture<Integer>[] fs |
4227 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4228 |
+ |
for (int i = 0; i < fs.length; i++) |
4229 |
+ |
fs[i] = new CompletableFuture<>(); |
4230 |
+ |
fs[fs.length - 1].complete(v); |
4231 |
+ |
for (int i = 0; i < n; i++) |
4232 |
+ |
checkCompletedNormally(CompletableFuture.anyOf(fs), v); |
4233 |
+ |
}} |
4234 |
+ |
|
4235 |
+ |
/** Checks for garbage retention with allOf. */ |
4236 |
+ |
public void testCancelledAllOfGarbageRetention() throws Throwable { |
4237 |
+ |
final int n = expensiveTests ? 100_000 : 10; |
4238 |
+ |
CompletableFuture<Integer>[] fs |
4239 |
+ |
= (CompletableFuture<Integer>[]) new CompletableFuture<?>[100]; |
4240 |
+ |
for (int i = 0; i < fs.length; i++) |
4241 |
+ |
fs[i] = new CompletableFuture<>(); |
4242 |
+ |
for (int i = 0; i < n; i++) |
4243 |
+ |
assertTrue(CompletableFuture.allOf(fs).cancel(false)); |
4244 |
+ |
} |
4245 |
+ |
|
4246 |
|
// static <U> U join(CompletionStage<U> stage) { |
4247 |
|
// CompletableFuture<U> f = new CompletableFuture<>(); |
4248 |
|
// stage.whenComplete((v, ex) -> { |