154 |
|
private PublicFutureTask(final Runnable runnable, Object result, |
155 |
|
final AtomicInteger runCount) { |
156 |
|
super(new Runnable() { |
157 |
< |
public void run() { |
158 |
< |
runCount.getAndIncrement(); |
159 |
< |
runnable.run(); |
160 |
< |
}}, result); |
157 |
> |
public void run() { |
158 |
> |
runCount.getAndIncrement(); |
159 |
> |
runnable.run(); |
160 |
> |
}}, result); |
161 |
|
this.runCount = runCount; |
162 |
|
} |
163 |
|
PublicFutureTask(Callable callable) { |
319 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
320 |
|
assertTrue(task.cancel(false)); |
321 |
|
task.run(); |
322 |
+ |
assertEquals(0, task.runCount()); |
323 |
|
assertEquals(0, task.setCount()); |
324 |
|
assertEquals(0, task.setExceptionCount()); |
325 |
+ |
assertTrue(task.isCancelled()); |
326 |
+ |
assertTrue(task.isDone()); |
327 |
|
tryToConfuseDoneTask(task); |
325 |
– |
checkCancelled(task); |
328 |
|
assertEquals(0, task.runCount()); |
329 |
+ |
checkCancelled(task); |
330 |
|
} |
331 |
|
|
332 |
|
/** |
336 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
337 |
|
assertTrue(task.cancel(true)); |
338 |
|
task.run(); |
339 |
+ |
assertEquals(0, task.runCount()); |
340 |
|
assertEquals(0, task.setCount()); |
341 |
|
assertEquals(0, task.setExceptionCount()); |
342 |
+ |
assertTrue(task.isCancelled()); |
343 |
+ |
assertTrue(task.isDone()); |
344 |
|
tryToConfuseDoneTask(task); |
339 |
– |
checkCancelled(task); |
345 |
|
assertEquals(0, task.runCount()); |
346 |
+ |
checkCancelled(task); |
347 |
|
} |
348 |
|
|
349 |
|
/** |
353 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
354 |
|
task.run(); |
355 |
|
assertFalse(task.cancel(false)); |
356 |
+ |
assertEquals(1, task.runCount()); |
357 |
|
assertEquals(1, task.setCount()); |
358 |
|
assertEquals(0, task.setExceptionCount()); |
359 |
|
tryToConfuseDoneTask(task); |
368 |
|
PublicFutureTask task = new PublicFutureTask(new NoOpCallable()); |
369 |
|
task.run(); |
370 |
|
assertFalse(task.cancel(true)); |
371 |
+ |
assertEquals(1, task.runCount()); |
372 |
|
assertEquals(1, task.setCount()); |
373 |
|
assertEquals(0, task.setExceptionCount()); |
374 |
|
tryToConfuseDoneTask(task); |
395 |
|
await(pleaseCancel); |
396 |
|
assertTrue(task.cancel(true)); |
397 |
|
assertTrue(task.isCancelled()); |
398 |
+ |
assertTrue(task.isDone()); |
399 |
|
awaitTermination(t); |
400 |
|
assertEquals(1, task.runCount()); |
401 |
|
assertEquals(1, task.setCount()); |
405 |
|
} |
406 |
|
|
407 |
|
/** |
408 |
+ |
* cancel(true) tries to interrupt a running task, but |
409 |
+ |
* Thread.interrupt throws (simulating a restrictive security |
410 |
+ |
* manager) |
411 |
+ |
*/ |
412 |
+ |
public void testCancelInterrupt_ThrowsSecurityException() { |
413 |
+ |
final CountDownLatch pleaseCancel = new CountDownLatch(1); |
414 |
+ |
final CountDownLatch cancelled = new CountDownLatch(1); |
415 |
+ |
final PublicFutureTask task = |
416 |
+ |
new PublicFutureTask(new CheckedRunnable() { |
417 |
+ |
public void realRun() { |
418 |
+ |
pleaseCancel.countDown(); |
419 |
+ |
await(cancelled); |
420 |
+ |
assertFalse(Thread.interrupted()); |
421 |
+ |
}}); |
422 |
+ |
|
423 |
+ |
final Thread t = new Thread(task) { |
424 |
+ |
// Simulate a restrictive security manager. |
425 |
+ |
@Override public void interrupt() { |
426 |
+ |
throw new SecurityException(); |
427 |
+ |
}}; |
428 |
+ |
t.setDaemon(true); |
429 |
+ |
t.start(); |
430 |
+ |
|
431 |
+ |
await(pleaseCancel); |
432 |
+ |
try { |
433 |
+ |
task.cancel(true); |
434 |
+ |
shouldThrow(); |
435 |
+ |
} catch (SecurityException expected) {} |
436 |
+ |
|
437 |
+ |
// We failed to deliver the interrupt, but the world retains |
438 |
+ |
// its sanity, as if we had done task.cancel(false) |
439 |
+ |
assertTrue(task.isCancelled()); |
440 |
+ |
assertTrue(task.isDone()); |
441 |
+ |
assertEquals(1, task.runCount()); |
442 |
+ |
assertEquals(1, task.doneCount()); |
443 |
+ |
assertEquals(0, task.setCount()); |
444 |
+ |
assertEquals(0, task.setExceptionCount()); |
445 |
+ |
cancelled.countDown(); |
446 |
+ |
awaitTermination(t); |
447 |
+ |
assertEquals(1, task.setCount()); |
448 |
+ |
assertEquals(0, task.setExceptionCount()); |
449 |
+ |
tryToConfuseDoneTask(task); |
450 |
+ |
checkCancelled(task); |
451 |
+ |
} |
452 |
+ |
|
453 |
+ |
/** |
454 |
|
* cancel(true) interrupts a running task that subsequently throws |
455 |
|
*/ |
456 |
|
public void testCancelInterrupt_taskFails() { |
461 |
|
try { |
462 |
|
pleaseCancel.countDown(); |
463 |
|
delay(LONG_DELAY_MS); |
464 |
< |
} finally { throw new RuntimeException(); } |
464 |
> |
shouldThrow(); |
465 |
> |
} catch (Throwable t) { |
466 |
> |
assertTrue(t instanceof InterruptedException); |
467 |
> |
} |
468 |
> |
throw new RuntimeException(); |
469 |
|
}}); |
470 |
|
|
471 |
|
Thread t = newStartedThread(task); |
590 |
|
* CancellationException |
591 |
|
*/ |
592 |
|
public void testTimedGet_Cancellation() { |
593 |
< |
for (final boolean mayInterruptIfRunning : |
594 |
< |
new boolean[] { true, false }) { |
595 |
< |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
596 |
< |
final CountDownLatch cancelled = new CountDownLatch(1); |
597 |
< |
final PublicFutureTask task = |
598 |
< |
new PublicFutureTask(new CheckedCallable<Object>() { |
599 |
< |
public Object realCall() throws InterruptedException { |
600 |
< |
pleaseCancel.countDown(); |
601 |
< |
if (mayInterruptIfRunning) { |
602 |
< |
try { |
603 |
< |
delay(2*LONG_DELAY_MS); |
604 |
< |
} catch (InterruptedException success) {} |
605 |
< |
} else { |
606 |
< |
await(cancelled); |
607 |
< |
} |
608 |
< |
return two; |
609 |
< |
}}); |
593 |
> |
testTimedGet_Cancellation(false); |
594 |
> |
} |
595 |
> |
public void testTimedGet_Cancellation_interrupt() { |
596 |
> |
testTimedGet_Cancellation(true); |
597 |
> |
} |
598 |
> |
public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { |
599 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
600 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
601 |
> |
final Callable<Object> callable = |
602 |
> |
new CheckedCallable<Object>() { |
603 |
> |
public Object realCall() throws InterruptedException { |
604 |
> |
pleaseCancel.countDown(); |
605 |
> |
if (mayInterruptIfRunning) { |
606 |
> |
try { |
607 |
> |
delay(2*LONG_DELAY_MS); |
608 |
> |
} catch (InterruptedException success) {} |
609 |
> |
} else { |
610 |
> |
await(cancelled); |
611 |
> |
} |
612 |
> |
return two; |
613 |
> |
}}; |
614 |
> |
final PublicFutureTask task = new PublicFutureTask(callable); |
615 |
|
|
616 |
< |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
616 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
617 |
|
public void realRun() throws Exception { |
618 |
|
pleaseCancel.countDown(); |
619 |
|
task.get(); |
620 |
|
}}; |
621 |
< |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
621 |
> |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
622 |
|
public void realRun() throws Exception { |
623 |
|
pleaseCancel.countDown(); |
624 |
|
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
625 |
|
}}; |
626 |
< |
t1.start(); |
627 |
< |
t2.start(); |
628 |
< |
Thread t3 = newStartedThread(task); |
629 |
< |
await(pleaseCancel); |
630 |
< |
checkIsRunning(task); |
631 |
< |
task.cancel(mayInterruptIfRunning); |
632 |
< |
checkCancelled(task); |
633 |
< |
awaitTermination(t1); |
634 |
< |
awaitTermination(t2); |
635 |
< |
cancelled.countDown(); |
636 |
< |
awaitTermination(t3); |
637 |
< |
assertEquals(1, task.runCount()); |
638 |
< |
assertEquals(1, task.setCount()); |
639 |
< |
assertEquals(0, task.setExceptionCount()); |
640 |
< |
tryToConfuseDoneTask(task); |
641 |
< |
checkCancelled(task); |
578 |
< |
} |
626 |
> |
t1.start(); |
627 |
> |
t2.start(); |
628 |
> |
Thread t3 = newStartedThread(task); |
629 |
> |
await(pleaseCancel); |
630 |
> |
checkIsRunning(task); |
631 |
> |
task.cancel(mayInterruptIfRunning); |
632 |
> |
checkCancelled(task); |
633 |
> |
awaitTermination(t1); |
634 |
> |
awaitTermination(t2); |
635 |
> |
cancelled.countDown(); |
636 |
> |
awaitTermination(t3); |
637 |
> |
assertEquals(1, task.runCount()); |
638 |
> |
assertEquals(1, task.setCount()); |
639 |
> |
assertEquals(0, task.setExceptionCount()); |
640 |
> |
tryToConfuseDoneTask(task); |
641 |
> |
checkCancelled(task); |
642 |
|
} |
643 |
|
|
644 |
|
/** |