744 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
745 |
|
*/ |
746 |
|
public void testShutdown_cancellation() throws Exception { |
747 |
< |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
748 |
< |
for (Boolean policy : allBooleans) |
747 |
> |
final int UNSPECIFIED = 0, YES = 1, NO = 2; |
748 |
> |
for (int maybe : new int[] { UNSPECIFIED, YES, NO }) |
749 |
|
{ |
750 |
|
final int poolSize = 2; |
751 |
|
final ScheduledThreadPoolExecutor p |
752 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
753 |
< |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
754 |
< |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
755 |
< |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
756 |
< |
if (policy != null) { |
757 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
758 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
759 |
< |
p.setRemoveOnCancelPolicy(policy); |
753 |
> |
final boolean effectiveDelayedPolicy = (maybe != NO); |
754 |
> |
final boolean effectivePeriodicPolicy = (maybe == YES); |
755 |
> |
final boolean effectiveRemovePolicy = (maybe == YES); |
756 |
> |
if (maybe != UNSPECIFIED) { |
757 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(maybe == YES); |
758 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(maybe == YES); |
759 |
> |
p.setRemoveOnCancelPolicy(maybe == YES); |
760 |
|
} |
761 |
|
assertEquals(effectiveDelayedPolicy, |
762 |
|
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
783 |
|
blockers.add(p.submit(task)); |
784 |
|
await(poolBlocked); |
785 |
|
|
786 |
< |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
787 |
< |
1, 1, MILLISECONDS)); |
788 |
< |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
789 |
< |
1, 1, MILLISECONDS)); |
786 |
> |
periodics.add(p.scheduleAtFixedRate( |
787 |
> |
countDowner(periodicLatch1), 1, 1, MILLISECONDS)); |
788 |
> |
periodics.add(p.scheduleWithFixedDelay( |
789 |
> |
countDowner(periodicLatch2), 1, 1, MILLISECONDS)); |
790 |
|
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
791 |
|
|
792 |
|
assertTrue(p.getQueue().containsAll(periodics)); |
808 |
|
assertEquals(effectiveDelayedPolicy, |
809 |
|
p.getQueue().containsAll(delayeds)); |
810 |
|
} |
811 |
< |
// Release all pool threads |
812 |
< |
unblock.countDown(); |
811 |
> |
unblock.countDown(); // Release all pool threads |
812 |
|
|
813 |
< |
for (Future<?> delayed : delayeds) { |
814 |
< |
if (effectiveDelayedPolicy) { |
816 |
< |
assertNull(delayed.get()); |
817 |
< |
} |
818 |
< |
} |
813 |
> |
if (effectiveDelayedPolicy) |
814 |
> |
for (Future<?> delayed : delayeds) assertNull(delayed.get()); |
815 |
|
if (effectivePeriodicPolicy) { |
816 |
|
await(periodicLatch1); |
817 |
|
await(periodicLatch2); |
824 |
|
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
825 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
826 |
|
assertTrue(p.isTerminated()); |
827 |
+ |
|
828 |
+ |
for (Future<?> future : delayeds) { |
829 |
+ |
assertTrue(effectiveDelayedPolicy ^ future.isCancelled()); |
830 |
+ |
assertTrue(future.isDone()); |
831 |
+ |
} |
832 |
+ |
for (Future<?> future : periodics) |
833 |
+ |
assertTrue(future.isCancelled()); |
834 |
+ |
for (Future<?> future : blockers) |
835 |
+ |
assertNull(future.get()); |
836 |
|
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
837 |
|
}} |
838 |
|
|