64 |
|
} |
65 |
|
} |
66 |
|
|
67 |
+ |
static class MyError extends Error {} |
68 |
+ |
|
69 |
|
// to test handlers |
70 |
|
static class FailingFJWSubclass extends ForkJoinWorkerThread { |
71 |
|
public FailingFJWSubclass(ForkJoinPool p) { super(p) ; } |
72 |
< |
protected void onStart() { super.onStart(); throw new Error(); } |
72 |
> |
protected void onStart() { super.onStart(); throw new MyError(); } |
73 |
|
} |
74 |
|
|
75 |
|
static class FailingThreadFactory |
164 |
|
*/ |
165 |
|
public void testDefaultInitialState() { |
166 |
|
ForkJoinPool p = new ForkJoinPool(1); |
167 |
< |
try { |
167 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
168 |
|
assertSame(ForkJoinPool.defaultForkJoinWorkerThreadFactory, |
169 |
|
p.getFactory()); |
170 |
|
assertFalse(p.getAsyncMode()); |
176 |
|
assertFalse(p.isShutdown()); |
177 |
|
assertFalse(p.isTerminating()); |
178 |
|
assertFalse(p.isTerminated()); |
177 |
– |
} finally { |
178 |
– |
joinPool(p); |
179 |
|
} |
180 |
|
} |
181 |
|
|
204 |
|
*/ |
205 |
|
public void testGetParallelism() { |
206 |
|
ForkJoinPool p = new ForkJoinPool(1); |
207 |
< |
try { |
207 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
208 |
|
assertEquals(1, p.getParallelism()); |
209 |
– |
} finally { |
210 |
– |
joinPool(p); |
209 |
|
} |
210 |
|
} |
211 |
|
|
213 |
|
* getPoolSize returns number of started workers. |
214 |
|
*/ |
215 |
|
public void testGetPoolSize() { |
216 |
< |
ForkJoinPool p = new ForkJoinPool(1); |
217 |
< |
try { |
216 |
> |
final CountDownLatch taskStarted = new CountDownLatch(1); |
217 |
> |
final CountDownLatch done = new CountDownLatch(1); |
218 |
> |
final ForkJoinPool p = new ForkJoinPool(1); |
219 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
220 |
|
assertEquals(0, p.getActiveThreadCount()); |
221 |
< |
Future<String> future = p.submit(new StringTask()); |
221 |
> |
final Runnable task = new CheckedRunnable() { |
222 |
> |
public void realRun() throws InterruptedException { |
223 |
> |
taskStarted.countDown(); |
224 |
> |
assertEquals(1, p.getPoolSize()); |
225 |
> |
assertEquals(1, p.getActiveThreadCount()); |
226 |
> |
done.await(); |
227 |
> |
}}; |
228 |
> |
Future<?> future = p.submit(task); |
229 |
> |
await(taskStarted); |
230 |
|
assertEquals(1, p.getPoolSize()); |
231 |
< |
} finally { |
232 |
< |
joinPool(p); |
231 |
> |
assertEquals(1, p.getActiveThreadCount()); |
232 |
> |
done.countDown(); |
233 |
|
} |
234 |
+ |
assertEquals(0, p.getPoolSize()); |
235 |
+ |
assertEquals(0, p.getActiveThreadCount()); |
236 |
|
} |
237 |
|
|
238 |
|
/** |
240 |
|
*/ |
241 |
|
public void testAwaitTermination_timesOut() throws InterruptedException { |
242 |
|
ForkJoinPool p = new ForkJoinPool(1); |
243 |
< |
assertFalse(p.isTerminated()); |
244 |
< |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
245 |
< |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
246 |
< |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
247 |
< |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
248 |
< |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
249 |
< |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
250 |
< |
long timeoutNanos = 999999L; |
251 |
< |
long startTime = System.nanoTime(); |
252 |
< |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
253 |
< |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
254 |
< |
assertFalse(p.isTerminated()); |
255 |
< |
startTime = System.nanoTime(); |
256 |
< |
long timeoutMillis = timeoutMillis(); |
257 |
< |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
258 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
259 |
< |
assertFalse(p.isTerminated()); |
260 |
< |
p.shutdown(); |
261 |
< |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
262 |
< |
assertTrue(p.isTerminated()); |
243 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
244 |
> |
assertFalse(p.isTerminated()); |
245 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
246 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
247 |
> |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
248 |
> |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
249 |
> |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
250 |
> |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
251 |
> |
long timeoutNanos = 999999L; |
252 |
> |
long startTime = System.nanoTime(); |
253 |
> |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
254 |
> |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
255 |
> |
assertFalse(p.isTerminated()); |
256 |
> |
startTime = System.nanoTime(); |
257 |
> |
long timeoutMillis = timeoutMillis(); |
258 |
> |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
259 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
260 |
> |
assertFalse(p.isTerminated()); |
261 |
> |
p.shutdown(); |
262 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
263 |
> |
assertTrue(p.isTerminated()); |
264 |
> |
} |
265 |
|
} |
266 |
|
|
267 |
|
/** |
272 |
|
*/ |
273 |
|
public void testSetUncaughtExceptionHandler() throws InterruptedException { |
274 |
|
final CountDownLatch uehInvoked = new CountDownLatch(1); |
275 |
< |
final Thread.UncaughtExceptionHandler eh = |
275 |
> |
final Thread.UncaughtExceptionHandler ueh = |
276 |
|
new Thread.UncaughtExceptionHandler() { |
277 |
|
public void uncaughtException(Thread t, Throwable e) { |
278 |
+ |
threadAssertTrue(e instanceof MyError); |
279 |
+ |
threadAssertTrue(t instanceof FailingFJWSubclass); |
280 |
|
uehInvoked.countDown(); |
281 |
|
}}; |
282 |
|
ForkJoinPool p = new ForkJoinPool(1, new FailingThreadFactory(), |
283 |
< |
eh, false); |
284 |
< |
try { |
285 |
< |
assertSame(eh, p.getUncaughtExceptionHandler()); |
283 |
> |
ueh, false); |
284 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
285 |
> |
assertSame(ueh, p.getUncaughtExceptionHandler()); |
286 |
|
try { |
287 |
|
p.execute(new FibTask(8)); |
288 |
< |
assertTrue(uehInvoked.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
289 |
< |
} catch (RejectedExecutionException ok) { |
288 |
> |
await(uehInvoked); |
289 |
> |
} finally { |
290 |
> |
p.shutdownNow(); // failure might have prevented processing task |
291 |
|
} |
277 |
– |
} finally { |
278 |
– |
p.shutdownNow(); // failure might have prevented processing task |
279 |
– |
joinPool(p); |
292 |
|
} |
293 |
|
} |
294 |
|
|
300 |
|
*/ |
301 |
|
public void testIsQuiescent() throws Exception { |
302 |
|
ForkJoinPool p = new ForkJoinPool(2); |
303 |
< |
try { |
303 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
304 |
|
assertTrue(p.isQuiescent()); |
305 |
|
long startTime = System.nanoTime(); |
306 |
|
FibTask f = new FibTask(20); |
319 |
|
|
320 |
|
assertTrue(p.isQuiescent()); |
321 |
|
assertFalse(p.getAsyncMode()); |
310 |
– |
assertEquals(0, p.getActiveThreadCount()); |
322 |
|
assertEquals(0, p.getQueuedTaskCount()); |
323 |
|
assertEquals(0, p.getQueuedSubmissionCount()); |
324 |
|
assertFalse(p.hasQueuedSubmissions()); |
325 |
+ |
while (p.getActiveThreadCount() != 0 |
326 |
+ |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
327 |
+ |
Thread.yield(); |
328 |
|
assertFalse(p.isShutdown()); |
329 |
|
assertFalse(p.isTerminating()); |
330 |
|
assertFalse(p.isTerminated()); |
331 |
|
assertTrue(f.isDone()); |
332 |
|
assertEquals(6765, (int) f.get()); |
333 |
< |
} finally { |
320 |
< |
joinPool(p); |
333 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
334 |
|
} |
335 |
|
} |
336 |
|
|
339 |
|
*/ |
340 |
|
public void testSubmitForkJoinTask() throws Throwable { |
341 |
|
ForkJoinPool p = new ForkJoinPool(1); |
342 |
< |
try { |
342 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
343 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
344 |
|
assertEquals(21, (int) f.get()); |
332 |
– |
} finally { |
333 |
– |
joinPool(p); |
345 |
|
} |
346 |
|
} |
347 |
|
|
350 |
|
*/ |
351 |
|
public void testSubmitAfterShutdown() { |
352 |
|
ForkJoinPool p = new ForkJoinPool(1); |
353 |
< |
try { |
353 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
354 |
|
p.shutdown(); |
355 |
|
assertTrue(p.isShutdown()); |
356 |
|
try { |
357 |
|
ForkJoinTask<Integer> f = p.submit(new FibTask(8)); |
358 |
|
shouldThrow(); |
359 |
|
} catch (RejectedExecutionException success) {} |
349 |
– |
} finally { |
350 |
– |
joinPool(p); |
360 |
|
} |
361 |
|
} |
362 |
|
|
382 |
|
public void testPollSubmission() { |
383 |
|
final CountDownLatch done = new CountDownLatch(1); |
384 |
|
SubFJP p = new SubFJP(); |
385 |
< |
try { |
385 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
386 |
|
ForkJoinTask a = p.submit(awaiter(done)); |
387 |
|
ForkJoinTask b = p.submit(awaiter(done)); |
388 |
|
ForkJoinTask c = p.submit(awaiter(done)); |
389 |
|
ForkJoinTask r = p.pollSubmission(); |
390 |
|
assertTrue(r == a || r == b || r == c); |
391 |
|
assertFalse(r.isDone()); |
383 |
– |
} finally { |
392 |
|
done.countDown(); |
385 |
– |
joinPool(p); |
393 |
|
} |
394 |
|
} |
395 |
|
|
399 |
|
public void testDrainTasksTo() { |
400 |
|
final CountDownLatch done = new CountDownLatch(1); |
401 |
|
SubFJP p = new SubFJP(); |
402 |
< |
try { |
402 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
403 |
|
ForkJoinTask a = p.submit(awaiter(done)); |
404 |
|
ForkJoinTask b = p.submit(awaiter(done)); |
405 |
|
ForkJoinTask c = p.submit(awaiter(done)); |
410 |
|
assertTrue(r == a || r == b || r == c); |
411 |
|
assertFalse(r.isDone()); |
412 |
|
} |
406 |
– |
} finally { |
413 |
|
done.countDown(); |
408 |
– |
joinPool(p); |
414 |
|
} |
415 |
|
} |
416 |
|
|
421 |
|
*/ |
422 |
|
public void testExecuteRunnable() throws Throwable { |
423 |
|
ExecutorService e = new ForkJoinPool(1); |
424 |
< |
try { |
424 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
425 |
|
final AtomicBoolean done = new AtomicBoolean(false); |
426 |
|
Future<?> future = e.submit(new CheckedRunnable() { |
427 |
|
public void realRun() { |
432 |
|
assertTrue(done.get()); |
433 |
|
assertTrue(future.isDone()); |
434 |
|
assertFalse(future.isCancelled()); |
430 |
– |
} finally { |
431 |
– |
joinPool(e); |
435 |
|
} |
436 |
|
} |
437 |
|
|
440 |
|
*/ |
441 |
|
public void testSubmitCallable() throws Throwable { |
442 |
|
ExecutorService e = new ForkJoinPool(1); |
443 |
< |
try { |
443 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
444 |
|
Future<String> future = e.submit(new StringTask()); |
445 |
|
assertSame(TEST_STRING, future.get()); |
446 |
|
assertTrue(future.isDone()); |
447 |
|
assertFalse(future.isCancelled()); |
445 |
– |
} finally { |
446 |
– |
joinPool(e); |
448 |
|
} |
449 |
|
} |
450 |
|
|
453 |
|
*/ |
454 |
|
public void testSubmitRunnable() throws Throwable { |
455 |
|
ExecutorService e = new ForkJoinPool(1); |
456 |
< |
try { |
456 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
457 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
458 |
|
assertNull(future.get()); |
459 |
|
assertTrue(future.isDone()); |
460 |
|
assertFalse(future.isCancelled()); |
460 |
– |
} finally { |
461 |
– |
joinPool(e); |
461 |
|
} |
462 |
|
} |
463 |
|
|
466 |
|
*/ |
467 |
|
public void testSubmitRunnable2() throws Throwable { |
468 |
|
ExecutorService e = new ForkJoinPool(1); |
469 |
< |
try { |
469 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
470 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
471 |
|
assertSame(TEST_STRING, future.get()); |
472 |
|
assertTrue(future.isDone()); |
473 |
|
assertFalse(future.isCancelled()); |
475 |
– |
} finally { |
476 |
– |
joinPool(e); |
474 |
|
} |
475 |
|
} |
476 |
|
|
483 |
|
Runnable r = new CheckedRunnable() { |
484 |
|
public void realRun() throws Exception { |
485 |
|
ExecutorService e = new ForkJoinPool(1); |
486 |
< |
try { |
486 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
487 |
|
Future future = e.submit(callable); |
488 |
|
assertSame(TEST_STRING, future.get()); |
492 |
– |
} finally { |
493 |
– |
joinPool(e); |
489 |
|
} |
490 |
|
}}; |
491 |
|
|
502 |
|
Runnable r = new CheckedRunnable() { |
503 |
|
public void realRun() throws Exception { |
504 |
|
ExecutorService e = new ForkJoinPool(1); |
505 |
< |
try { |
505 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
506 |
|
Future future = e.submit(callable); |
507 |
|
assertSame(TEST_STRING, future.get()); |
513 |
– |
} finally { |
514 |
– |
joinPool(e); |
508 |
|
} |
509 |
|
}}; |
510 |
|
|
521 |
|
Runnable r = new CheckedRunnable() { |
522 |
|
public void realRun() throws Exception { |
523 |
|
ExecutorService e = new ForkJoinPool(1); |
524 |
< |
try { |
524 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
525 |
|
Future future = e.submit(callable); |
526 |
|
try { |
527 |
|
future.get(); |
529 |
|
} catch (ExecutionException success) { |
530 |
|
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
531 |
|
} |
539 |
– |
} finally { |
540 |
– |
joinPool(e); |
532 |
|
} |
533 |
|
}}; |
534 |
|
|
540 |
|
*/ |
541 |
|
public void testExecuteNullRunnable() { |
542 |
|
ExecutorService e = new ForkJoinPool(1); |
543 |
< |
try { |
544 |
< |
Future<?> future = e.submit((Runnable) null); |
545 |
< |
shouldThrow(); |
546 |
< |
} catch (NullPointerException success) { |
547 |
< |
} finally { |
557 |
< |
joinPool(e); |
543 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
544 |
> |
try { |
545 |
> |
Future<?> future = e.submit((Runnable) null); |
546 |
> |
shouldThrow(); |
547 |
> |
} catch (NullPointerException success) {} |
548 |
|
} |
549 |
|
} |
550 |
|
|
553 |
|
*/ |
554 |
|
public void testSubmitNullCallable() { |
555 |
|
ExecutorService e = new ForkJoinPool(1); |
556 |
< |
try { |
557 |
< |
Future<String> future = e.submit((Callable) null); |
558 |
< |
shouldThrow(); |
559 |
< |
} catch (NullPointerException success) { |
560 |
< |
} finally { |
571 |
< |
joinPool(e); |
556 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
557 |
> |
try { |
558 |
> |
Future<String> future = e.submit((Callable) null); |
559 |
> |
shouldThrow(); |
560 |
> |
} catch (NullPointerException success) {} |
561 |
|
} |
562 |
|
} |
563 |
|
|
567 |
|
public void testInterruptedSubmit() throws InterruptedException { |
568 |
|
final CountDownLatch submitted = new CountDownLatch(1); |
569 |
|
final CountDownLatch quittingTime = new CountDownLatch(1); |
581 |
– |
final ExecutorService p = new ForkJoinPool(1); |
570 |
|
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
571 |
|
public Void realCall() throws InterruptedException { |
572 |
< |
assertTrue(quittingTime.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
572 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
573 |
|
return null; |
574 |
|
}}; |
575 |
< |
try { |
575 |
> |
final ExecutorService p = new ForkJoinPool(1); |
576 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
577 |
|
Thread t = new Thread(new CheckedInterruptedRunnable() { |
578 |
|
public void realRun() throws Exception { |
579 |
|
Future<Void> future = p.submit(awaiter); |
581 |
|
future.get(); |
582 |
|
}}); |
583 |
|
t.start(); |
584 |
< |
assertTrue(submitted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
584 |
> |
await(submitted); |
585 |
|
t.interrupt(); |
586 |
< |
t.join(); |
598 |
< |
} finally { |
599 |
< |
quittingTime.countDown(); |
600 |
< |
joinPool(p); |
586 |
> |
awaitTermination(t); |
587 |
|
} |
588 |
|
} |
589 |
|
|
593 |
|
*/ |
594 |
|
public void testSubmitEE() throws Throwable { |
595 |
|
ForkJoinPool p = new ForkJoinPool(1); |
596 |
< |
try { |
597 |
< |
p.submit(new Callable() { |
598 |
< |
public Object call() { throw new ArithmeticException(); }}) |
599 |
< |
.get(); |
600 |
< |
shouldThrow(); |
601 |
< |
} catch (ExecutionException success) { |
602 |
< |
assertTrue(success.getCause() instanceof ArithmeticException); |
603 |
< |
} finally { |
604 |
< |
joinPool(p); |
596 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
597 |
> |
try { |
598 |
> |
p.submit(new Callable() { |
599 |
> |
public Object call() { throw new ArithmeticException(); }}) |
600 |
> |
.get(); |
601 |
> |
shouldThrow(); |
602 |
> |
} catch (ExecutionException success) { |
603 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
604 |
> |
} |
605 |
|
} |
606 |
|
} |
607 |
|
|
610 |
|
*/ |
611 |
|
public void testInvokeAny1() throws Throwable { |
612 |
|
ExecutorService e = new ForkJoinPool(1); |
613 |
< |
try { |
614 |
< |
e.invokeAny(null); |
615 |
< |
shouldThrow(); |
616 |
< |
} catch (NullPointerException success) { |
617 |
< |
} finally { |
632 |
< |
joinPool(e); |
613 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
614 |
> |
try { |
615 |
> |
e.invokeAny(null); |
616 |
> |
shouldThrow(); |
617 |
> |
} catch (NullPointerException success) {} |
618 |
|
} |
619 |
|
} |
620 |
|
|
623 |
|
*/ |
624 |
|
public void testInvokeAny2() throws Throwable { |
625 |
|
ExecutorService e = new ForkJoinPool(1); |
626 |
< |
try { |
627 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
628 |
< |
shouldThrow(); |
629 |
< |
} catch (IllegalArgumentException success) { |
630 |
< |
} finally { |
646 |
< |
joinPool(e); |
626 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
627 |
> |
try { |
628 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
629 |
> |
shouldThrow(); |
630 |
> |
} catch (IllegalArgumentException success) {} |
631 |
|
} |
632 |
|
} |
633 |
|
|
636 |
|
*/ |
637 |
|
public void testInvokeAny3() throws Throwable { |
638 |
|
ExecutorService e = new ForkJoinPool(1); |
639 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
640 |
< |
l.add(null); |
641 |
< |
try { |
642 |
< |
e.invokeAny(l); |
643 |
< |
shouldThrow(); |
644 |
< |
} catch (NullPointerException success) { |
645 |
< |
} finally { |
662 |
< |
joinPool(e); |
639 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
640 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
641 |
> |
l.add(null); |
642 |
> |
try { |
643 |
> |
e.invokeAny(l); |
644 |
> |
shouldThrow(); |
645 |
> |
} catch (NullPointerException success) {} |
646 |
|
} |
647 |
|
} |
648 |
|
|
652 |
|
public void testInvokeAny4() throws Throwable { |
653 |
|
CountDownLatch latch = new CountDownLatch(1); |
654 |
|
ExecutorService e = new ForkJoinPool(1); |
655 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
656 |
< |
l.add(latchAwaitingStringTask(latch)); |
657 |
< |
l.add(null); |
658 |
< |
try { |
659 |
< |
e.invokeAny(l); |
660 |
< |
shouldThrow(); |
661 |
< |
} catch (NullPointerException success) { |
662 |
< |
} finally { |
655 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
656 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
657 |
> |
l.add(latchAwaitingStringTask(latch)); |
658 |
> |
l.add(null); |
659 |
> |
try { |
660 |
> |
e.invokeAny(l); |
661 |
> |
shouldThrow(); |
662 |
> |
} catch (NullPointerException success) {} |
663 |
|
latch.countDown(); |
681 |
– |
joinPool(e); |
664 |
|
} |
665 |
|
} |
666 |
|
|
669 |
|
*/ |
670 |
|
public void testInvokeAny5() throws Throwable { |
671 |
|
ExecutorService e = new ForkJoinPool(1); |
672 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
673 |
< |
l.add(new NPETask()); |
674 |
< |
try { |
675 |
< |
e.invokeAny(l); |
676 |
< |
shouldThrow(); |
677 |
< |
} catch (ExecutionException success) { |
678 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
679 |
< |
} finally { |
680 |
< |
joinPool(e); |
672 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
673 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
674 |
> |
l.add(new NPETask()); |
675 |
> |
try { |
676 |
> |
e.invokeAny(l); |
677 |
> |
shouldThrow(); |
678 |
> |
} catch (ExecutionException success) { |
679 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
680 |
> |
} |
681 |
|
} |
682 |
|
} |
683 |
|
|
686 |
|
*/ |
687 |
|
public void testInvokeAny6() throws Throwable { |
688 |
|
ExecutorService e = new ForkJoinPool(1); |
689 |
< |
try { |
689 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
690 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
691 |
|
l.add(new StringTask()); |
692 |
|
l.add(new StringTask()); |
693 |
|
String result = e.invokeAny(l); |
694 |
|
assertSame(TEST_STRING, result); |
713 |
– |
} finally { |
714 |
– |
joinPool(e); |
695 |
|
} |
696 |
|
} |
697 |
|
|
700 |
|
*/ |
701 |
|
public void testInvokeAll1() throws Throwable { |
702 |
|
ExecutorService e = new ForkJoinPool(1); |
703 |
< |
try { |
704 |
< |
e.invokeAll(null); |
705 |
< |
shouldThrow(); |
706 |
< |
} catch (NullPointerException success) { |
707 |
< |
} finally { |
728 |
< |
joinPool(e); |
703 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
704 |
> |
try { |
705 |
> |
e.invokeAll(null); |
706 |
> |
shouldThrow(); |
707 |
> |
} catch (NullPointerException success) {} |
708 |
|
} |
709 |
|
} |
710 |
|
|
713 |
|
*/ |
714 |
|
public void testInvokeAll2() throws InterruptedException { |
715 |
|
ExecutorService e = new ForkJoinPool(1); |
716 |
< |
try { |
716 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
717 |
|
List<Future<String>> r |
718 |
|
= e.invokeAll(new ArrayList<Callable<String>>()); |
719 |
|
assertTrue(r.isEmpty()); |
741 |
– |
} finally { |
742 |
– |
joinPool(e); |
720 |
|
} |
721 |
|
} |
722 |
|
|
725 |
|
*/ |
726 |
|
public void testInvokeAll3() throws InterruptedException { |
727 |
|
ExecutorService e = new ForkJoinPool(1); |
728 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
729 |
< |
l.add(new StringTask()); |
730 |
< |
l.add(null); |
731 |
< |
try { |
732 |
< |
e.invokeAll(l); |
733 |
< |
shouldThrow(); |
734 |
< |
} catch (NullPointerException success) { |
735 |
< |
} finally { |
759 |
< |
joinPool(e); |
728 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
729 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
730 |
> |
l.add(new StringTask()); |
731 |
> |
l.add(null); |
732 |
> |
try { |
733 |
> |
e.invokeAll(l); |
734 |
> |
shouldThrow(); |
735 |
> |
} catch (NullPointerException success) {} |
736 |
|
} |
737 |
|
} |
738 |
|
|
742 |
|
*/ |
743 |
|
public void testInvokeAll4() throws Throwable { |
744 |
|
ExecutorService e = new ForkJoinPool(1); |
745 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
746 |
< |
l.add(new NPETask()); |
747 |
< |
List<Future<String>> futures = e.invokeAll(l); |
748 |
< |
assertEquals(1, futures.size()); |
749 |
< |
try { |
750 |
< |
futures.get(0).get(); |
751 |
< |
shouldThrow(); |
752 |
< |
} catch (ExecutionException success) { |
753 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
754 |
< |
} finally { |
755 |
< |
joinPool(e); |
745 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
746 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
747 |
> |
l.add(new NPETask()); |
748 |
> |
List<Future<String>> futures = e.invokeAll(l); |
749 |
> |
assertEquals(1, futures.size()); |
750 |
> |
try { |
751 |
> |
futures.get(0).get(); |
752 |
> |
shouldThrow(); |
753 |
> |
} catch (ExecutionException success) { |
754 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
755 |
> |
} |
756 |
|
} |
757 |
|
} |
758 |
|
|
761 |
|
*/ |
762 |
|
public void testInvokeAll5() throws Throwable { |
763 |
|
ExecutorService e = new ForkJoinPool(1); |
764 |
< |
try { |
764 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
765 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
766 |
|
l.add(new StringTask()); |
767 |
|
l.add(new StringTask()); |
769 |
|
assertEquals(2, futures.size()); |
770 |
|
for (Future<String> future : futures) |
771 |
|
assertSame(TEST_STRING, future.get()); |
796 |
– |
} finally { |
797 |
– |
joinPool(e); |
772 |
|
} |
773 |
|
} |
774 |
|
|
777 |
|
*/ |
778 |
|
public void testTimedInvokeAny1() throws Throwable { |
779 |
|
ExecutorService e = new ForkJoinPool(1); |
780 |
< |
try { |
781 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
782 |
< |
shouldThrow(); |
783 |
< |
} catch (NullPointerException success) { |
784 |
< |
} finally { |
811 |
< |
joinPool(e); |
780 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
781 |
> |
try { |
782 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
783 |
> |
shouldThrow(); |
784 |
> |
} catch (NullPointerException success) {} |
785 |
|
} |
786 |
|
} |
787 |
|
|
790 |
|
*/ |
791 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Throwable { |
792 |
|
ExecutorService e = new ForkJoinPool(1); |
793 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
794 |
< |
l.add(new StringTask()); |
795 |
< |
try { |
796 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
797 |
< |
shouldThrow(); |
798 |
< |
} catch (NullPointerException success) { |
799 |
< |
} finally { |
827 |
< |
joinPool(e); |
793 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
794 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
795 |
> |
l.add(new StringTask()); |
796 |
> |
try { |
797 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
798 |
> |
shouldThrow(); |
799 |
> |
} catch (NullPointerException success) {} |
800 |
|
} |
801 |
|
} |
802 |
|
|
805 |
|
*/ |
806 |
|
public void testTimedInvokeAny2() throws Throwable { |
807 |
|
ExecutorService e = new ForkJoinPool(1); |
808 |
< |
try { |
809 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), |
810 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
811 |
< |
shouldThrow(); |
812 |
< |
} catch (IllegalArgumentException success) { |
813 |
< |
} finally { |
842 |
< |
joinPool(e); |
808 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
809 |
> |
try { |
810 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
811 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
812 |
> |
shouldThrow(); |
813 |
> |
} catch (IllegalArgumentException success) {} |
814 |
|
} |
815 |
|
} |
816 |
|
|
820 |
|
public void testTimedInvokeAny3() throws Throwable { |
821 |
|
CountDownLatch latch = new CountDownLatch(1); |
822 |
|
ExecutorService e = new ForkJoinPool(1); |
823 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
824 |
< |
l.add(latchAwaitingStringTask(latch)); |
825 |
< |
l.add(null); |
826 |
< |
try { |
827 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
828 |
< |
shouldThrow(); |
829 |
< |
} catch (NullPointerException success) { |
830 |
< |
} finally { |
823 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
824 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
825 |
> |
l.add(latchAwaitingStringTask(latch)); |
826 |
> |
l.add(null); |
827 |
> |
try { |
828 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
829 |
> |
shouldThrow(); |
830 |
> |
} catch (NullPointerException success) {} |
831 |
|
latch.countDown(); |
861 |
– |
joinPool(e); |
832 |
|
} |
833 |
|
} |
834 |
|
|
837 |
|
*/ |
838 |
|
public void testTimedInvokeAny4() throws Throwable { |
839 |
|
ExecutorService e = new ForkJoinPool(1); |
840 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
841 |
< |
l.add(new NPETask()); |
842 |
< |
try { |
843 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
844 |
< |
shouldThrow(); |
845 |
< |
} catch (ExecutionException success) { |
846 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
847 |
< |
} finally { |
848 |
< |
joinPool(e); |
840 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
841 |
> |
long startTime = System.nanoTime(); |
842 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
843 |
> |
l.add(new NPETask()); |
844 |
> |
try { |
845 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
846 |
> |
shouldThrow(); |
847 |
> |
} catch (ExecutionException success) { |
848 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
849 |
> |
} |
850 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
851 |
|
} |
852 |
|
} |
853 |
|
|
856 |
|
*/ |
857 |
|
public void testTimedInvokeAny5() throws Throwable { |
858 |
|
ExecutorService e = new ForkJoinPool(1); |
859 |
< |
try { |
859 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
860 |
> |
long startTime = System.nanoTime(); |
861 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
862 |
|
l.add(new StringTask()); |
863 |
|
l.add(new StringTask()); |
864 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
864 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
865 |
|
assertSame(TEST_STRING, result); |
866 |
< |
} finally { |
894 |
< |
joinPool(e); |
866 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
867 |
|
} |
868 |
|
} |
869 |
|
|
872 |
|
*/ |
873 |
|
public void testTimedInvokeAll1() throws Throwable { |
874 |
|
ExecutorService e = new ForkJoinPool(1); |
875 |
< |
try { |
876 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
877 |
< |
shouldThrow(); |
878 |
< |
} catch (NullPointerException success) { |
879 |
< |
} finally { |
908 |
< |
joinPool(e); |
875 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
876 |
> |
try { |
877 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
878 |
> |
shouldThrow(); |
879 |
> |
} catch (NullPointerException success) {} |
880 |
|
} |
881 |
|
} |
882 |
|
|
885 |
|
*/ |
886 |
|
public void testTimedInvokeAllNullTimeUnit() throws Throwable { |
887 |
|
ExecutorService e = new ForkJoinPool(1); |
888 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
889 |
< |
l.add(new StringTask()); |
890 |
< |
try { |
891 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
892 |
< |
shouldThrow(); |
893 |
< |
} catch (NullPointerException success) { |
894 |
< |
} finally { |
924 |
< |
joinPool(e); |
888 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
889 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
890 |
> |
l.add(new StringTask()); |
891 |
> |
try { |
892 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
893 |
> |
shouldThrow(); |
894 |
> |
} catch (NullPointerException success) {} |
895 |
|
} |
896 |
|
} |
897 |
|
|
900 |
|
*/ |
901 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
902 |
|
ExecutorService e = new ForkJoinPool(1); |
903 |
< |
try { |
903 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
904 |
|
List<Future<String>> r |
905 |
|
= e.invokeAll(new ArrayList<Callable<String>>(), |
906 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
907 |
|
assertTrue(r.isEmpty()); |
938 |
– |
} finally { |
939 |
– |
joinPool(e); |
908 |
|
} |
909 |
|
} |
910 |
|
|
913 |
|
*/ |
914 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
915 |
|
ExecutorService e = new ForkJoinPool(1); |
916 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
917 |
< |
l.add(new StringTask()); |
918 |
< |
l.add(null); |
919 |
< |
try { |
920 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
921 |
< |
shouldThrow(); |
922 |
< |
} catch (NullPointerException success) { |
923 |
< |
} finally { |
956 |
< |
joinPool(e); |
916 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
917 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
918 |
> |
l.add(new StringTask()); |
919 |
> |
l.add(null); |
920 |
> |
try { |
921 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
922 |
> |
shouldThrow(); |
923 |
> |
} catch (NullPointerException success) {} |
924 |
|
} |
925 |
|
} |
926 |
|
|
929 |
|
*/ |
930 |
|
public void testTimedInvokeAll4() throws Throwable { |
931 |
|
ExecutorService e = new ForkJoinPool(1); |
932 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
933 |
< |
l.add(new NPETask()); |
934 |
< |
List<Future<String>> futures |
935 |
< |
= e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
936 |
< |
assertEquals(1, futures.size()); |
937 |
< |
try { |
938 |
< |
futures.get(0).get(); |
939 |
< |
shouldThrow(); |
940 |
< |
} catch (ExecutionException success) { |
941 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
942 |
< |
} finally { |
943 |
< |
joinPool(e); |
932 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
933 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
934 |
> |
l.add(new NPETask()); |
935 |
> |
List<Future<String>> futures |
936 |
> |
= e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
937 |
> |
assertEquals(1, futures.size()); |
938 |
> |
try { |
939 |
> |
futures.get(0).get(); |
940 |
> |
shouldThrow(); |
941 |
> |
} catch (ExecutionException success) { |
942 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
943 |
> |
} |
944 |
|
} |
945 |
|
} |
946 |
|
|
948 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
949 |
|
*/ |
950 |
|
public void testTimedInvokeAll5() throws Throwable { |
951 |
< |
try (PoolCleaner<ForkJoinPool> cleaner |
952 |
< |
= cleaner(new ForkJoinPool(1))) { |
986 |
< |
ForkJoinPool e = cleaner.pool; |
951 |
> |
ForkJoinPool e = new ForkJoinPool(1); |
952 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
953 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
954 |
|
l.add(new StringTask()); |
955 |
|
l.add(new StringTask()); |