746 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
747 |
|
*/ |
748 |
|
public void testShutdown_cancellation() throws Exception { |
749 |
< |
final int poolSize = 6; |
749 |
> |
final int poolSize = 4; |
750 |
|
final ScheduledThreadPoolExecutor p |
751 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
752 |
|
final BlockingQueue<Runnable> q = p.getQueue(); |
753 |
|
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
754 |
+ |
final long delay = rnd.nextInt(2); |
755 |
+ |
final int rounds = rnd.nextInt(1, 3); |
756 |
|
final boolean effectiveDelayedPolicy; |
757 |
|
final boolean effectivePeriodicPolicy; |
758 |
|
final boolean effectiveRemovePolicy; |
818 |
|
List<Future<?>> periodics = new ArrayList<>(); |
819 |
|
|
820 |
|
immediates.add(p.submit(task)); |
821 |
< |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
822 |
< |
for (int rounds : new int[] { 1, 2 }) { |
823 |
< |
periodics.add(p.scheduleAtFixedRate( |
824 |
< |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
825 |
< |
periodics.add(p.scheduleWithFixedDelay( |
824 |
< |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
825 |
< |
} |
821 |
> |
delayeds.add(p.schedule(task, delay, MILLISECONDS)); |
822 |
> |
periodics.add(p.scheduleAtFixedRate( |
823 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
824 |
> |
periodics.add(p.scheduleWithFixedDelay( |
825 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
826 |
|
|
827 |
|
await(poolBlocked); |
828 |
|
|
832 |
|
|
833 |
|
// Add second wave of tasks. |
834 |
|
immediates.add(p.submit(task)); |
835 |
< |
long delay_ms = effectiveDelayedPolicy ? 1 : LONG_DELAY_MS; |
836 |
< |
delayeds.add(p.schedule(task, delay_ms, MILLISECONDS)); |
837 |
< |
for (int rounds : new int[] { 1, 2 }) { |
838 |
< |
periodics.add(p.scheduleAtFixedRate( |
839 |
< |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
840 |
< |
periodics.add(p.scheduleWithFixedDelay( |
841 |
< |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
842 |
< |
} |
835 |
> |
delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); |
836 |
> |
periodics.add(p.scheduleAtFixedRate( |
837 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
838 |
> |
periodics.add(p.scheduleWithFixedDelay( |
839 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
840 |
|
|
841 |
|
assertEquals(poolSize, q.size()); |
842 |
|
assertEquals(poolSize, ran.get()); |
866 |
|
assertTrue(!effectiveDelayedPolicy |
867 |
|
^ q.contains(delayeds.get(1))); |
868 |
|
assertTrue(!effectivePeriodicPolicy |
869 |
< |
^ q.containsAll(periodics.subList(4, 8))); |
869 |
> |
^ q.containsAll(periodics.subList(2, 4))); |
870 |
|
|
871 |
|
immediates.forEach(f -> assertFalse(f.isDone())); |
872 |
|
|
886 |
|
} |
887 |
|
}); |
888 |
|
else { |
889 |
< |
periodics.subList(0, 4).forEach(f -> assertFalse(f.isDone())); |
890 |
< |
periodics.subList(4, 8).forEach(f -> assertTrue(f.isCancelled())); |
889 |
> |
periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); |
890 |
> |
periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); |
891 |
|
} |
892 |
|
|
893 |
|
unblock.countDown(); // Release all pool threads |
923 |
|
|
924 |
|
assertEquals(poolSize + 1 |
925 |
|
+ (effectiveDelayedPolicy ? 1 : 0) |
926 |
< |
+ (periodicTasksContinue ? 4 : 0), |
926 |
> |
+ (periodicTasksContinue ? 2 : 0), |
927 |
|
ran.get()); |
928 |
|
} |
929 |
|
|