459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
< |
* isShutDown is false before shutdown, true after |
462 |
> |
* isShutdown is false before shutdown, true after |
463 |
|
*/ |
464 |
|
public void testIsShutdown() { |
465 |
|
|
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
< |
* purge removes cancelled tasks from the queue |
590 |
> |
* purge eventually removes cancelled tasks from the queue |
591 |
|
*/ |
592 |
|
public void testPurge() throws InterruptedException { |
593 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
594 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
595 |
< |
for (int i = 0; i < tasks.length; i++) { |
596 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
597 |
< |
} |
595 |
> |
for (int i = 0; i < tasks.length; i++) |
596 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
597 |
> |
LONG_DELAY_MS, MILLISECONDS); |
598 |
|
try { |
599 |
|
int max = tasks.length; |
600 |
|
if (tasks[4].cancel(true)) --max; |
601 |
|
if (tasks[3].cancel(true)) --max; |
602 |
|
// There must eventually be an interference-free point at |
603 |
|
// which purge will not fail. (At worst, when queue is empty.) |
604 |
< |
int k; |
605 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
604 |
> |
long startTime = System.nanoTime(); |
605 |
> |
do { |
606 |
|
p.purge(); |
607 |
|
long count = p.getTaskCount(); |
608 |
< |
if (count >= 0 && count <= max) |
609 |
< |
break; |
610 |
< |
delay(1); |
611 |
< |
} |
612 |
< |
assertTrue(k < SMALL_DELAY_MS); |
608 |
> |
if (count == max) |
609 |
> |
return; |
610 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
611 |
> |
fail("Purge failed to remove cancelled tasks"); |
612 |
|
} finally { |
613 |
|
for (ScheduledFuture task : tasks) |
614 |
|
task.cancel(true); |
617 |
|
} |
618 |
|
|
619 |
|
/** |
620 |
< |
* shutDownNow returns a list containing tasks that were not run |
620 |
> |
* shutdownNow returns a list containing tasks that were not run |
621 |
|
*/ |
622 |
< |
public void testShutDownNow() throws InterruptedException { |
622 |
> |
public void testShutdownNow() { |
623 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
624 |
|
for (int i = 0; i < 5; i++) |
625 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
626 |
< |
List l; |
625 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
626 |
> |
LONG_DELAY_MS, MILLISECONDS); |
627 |
|
try { |
628 |
< |
l = p.shutdownNow(); |
628 |
> |
List<Runnable> l = p.shutdownNow(); |
629 |
> |
assertTrue(p.isShutdown()); |
630 |
> |
assertEquals(5, l.size()); |
631 |
|
} catch (SecurityException ok) { |
632 |
< |
return; |
632 |
> |
// Allowed in case test doesn't have privs |
633 |
> |
} finally { |
634 |
> |
joinPool(p); |
635 |
|
} |
633 |
– |
assertTrue(p.isShutdown()); |
634 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
635 |
– |
joinPool(p); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
|
* In default setting, shutdown cancels periodic but not delayed |
640 |
|
* tasks at shutdown |
641 |
|
*/ |
642 |
< |
public void testShutDown1() throws InterruptedException { |
642 |
> |
public void testShutdown1() throws InterruptedException { |
643 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
644 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
645 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
669 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
670 |
|
* delayed tasks are cancelled at shutdown |
671 |
|
*/ |
672 |
< |
public void testShutDown2() throws InterruptedException { |
672 |
> |
public void testShutdown2() throws InterruptedException { |
673 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
674 |
|
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
675 |
|
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
695 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
696 |
|
* periodic tasks are cancelled at shutdown |
697 |
|
*/ |
698 |
< |
public void testShutDown3() throws InterruptedException { |
698 |
> |
public void testShutdown3() throws InterruptedException { |
699 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
700 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
701 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
702 |
|
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
703 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
704 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
705 |
+ |
long initialDelay = LONG_DELAY_MS; |
706 |
|
ScheduledFuture task = |
707 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
707 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
708 |
> |
5, MILLISECONDS); |
709 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
710 |
|
assertTrue(p.isShutdown()); |
709 |
– |
BlockingQueue q = p.getQueue(); |
711 |
|
assertTrue(p.getQueue().isEmpty()); |
712 |
|
assertTrue(task.isDone()); |
713 |
|
assertTrue(task.isCancelled()); |
714 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
714 |
< |
assertTrue(p.isTerminated()); |
714 |
> |
joinPool(p); |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
719 |
|
* periodic tasks are not cancelled at shutdown |
720 |
|
*/ |
721 |
< |
public void testShutDown4() throws InterruptedException { |
721 |
> |
public void testShutdown4() throws InterruptedException { |
722 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
723 |
|
final CountDownLatch counter = new CountDownLatch(2); |
724 |
|
try { |