29 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
30 |
|
import java.util.concurrent.atomic.AtomicInteger; |
31 |
|
import java.util.concurrent.atomic.AtomicLong; |
32 |
+ |
import java.util.stream.Stream; |
33 |
|
|
34 |
|
import junit.framework.Test; |
35 |
|
import junit.framework.TestSuite; |
746 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
747 |
|
*/ |
748 |
|
public void testShutdown_cancellation() throws Exception { |
749 |
< |
final int poolSize = 2; |
749 |
> |
final int poolSize = 6; |
750 |
|
final ScheduledThreadPoolExecutor p |
751 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
752 |
+ |
final BlockingQueue<Runnable> q = p.getQueue(); |
753 |
|
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
754 |
|
final boolean effectiveDelayedPolicy; |
755 |
|
final boolean effectivePeriodicPolicy; |
779 |
|
assertEquals(effectiveRemovePolicy, |
780 |
|
p.getRemoveOnCancelPolicy()); |
781 |
|
|
782 |
< |
// Strategy: Wedge the pool with poolSize "blocker" threads |
782 |
> |
final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean(); |
783 |
> |
|
784 |
> |
// Strategy: Wedge the pool with one wave of "blocker" tasks, |
785 |
> |
// then add a second wave that waits in the queue until unblocked. |
786 |
|
final AtomicInteger ran = new AtomicInteger(0); |
787 |
|
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
788 |
|
final CountDownLatch unblock = new CountDownLatch(1); |
789 |
< |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
790 |
< |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
791 |
< |
Runnable task = new CheckedRunnable() { public void realRun() |
792 |
< |
throws InterruptedException { |
793 |
< |
poolBlocked.countDown(); |
794 |
< |
await(unblock); |
795 |
< |
ran.getAndIncrement(); |
796 |
< |
}}; |
797 |
< |
List<Future<?>> blockers = new ArrayList<>(); |
798 |
< |
List<Future<?>> periodics = new ArrayList<>(); |
799 |
< |
List<Future<?>> delayeds = new ArrayList<>(); |
800 |
< |
for (int i = 0; i < poolSize; i++) |
801 |
< |
blockers.add(p.submit(task)); |
802 |
< |
await(poolBlocked); |
789 |
> |
final RuntimeException exception = new RuntimeException(); |
790 |
> |
|
791 |
> |
class Task implements Runnable { |
792 |
> |
public void run() { |
793 |
> |
try { |
794 |
> |
ran.getAndIncrement(); |
795 |
> |
poolBlocked.countDown(); |
796 |
> |
await(unblock); |
797 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
798 |
> |
} |
799 |
> |
} |
800 |
> |
|
801 |
> |
class PeriodicTask extends Task { |
802 |
> |
PeriodicTask(int rounds) { this.rounds = rounds; } |
803 |
> |
int rounds; |
804 |
> |
public void run() { |
805 |
> |
if (--rounds == 0) super.run(); |
806 |
> |
// throw exception to surely terminate this periodic task, |
807 |
> |
// but in a separate execution and in a detectable way. |
808 |
> |
if (rounds == -1) throw exception; |
809 |
> |
} |
810 |
> |
} |
811 |
|
|
812 |
< |
periodics.add(p.scheduleAtFixedRate( |
813 |
< |
countDowner(periodicLatch1), 1, 1, MILLISECONDS)); |
814 |
< |
periodics.add(p.scheduleWithFixedDelay( |
815 |
< |
countDowner(periodicLatch2), 1, 1, MILLISECONDS)); |
812 |
> |
Runnable task = new Task(); |
813 |
> |
|
814 |
> |
List<Future<?>> immediates = new ArrayList<>(); |
815 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
816 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
817 |
> |
|
818 |
> |
immediates.add(p.submit(task)); |
819 |
|
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
820 |
+ |
for (int rounds : new int[] { 1, 2 }) { |
821 |
+ |
periodics.add(p.scheduleAtFixedRate( |
822 |
+ |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
823 |
+ |
periodics.add(p.scheduleWithFixedDelay( |
824 |
+ |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
825 |
+ |
} |
826 |
+ |
|
827 |
+ |
await(poolBlocked); |
828 |
+ |
|
829 |
+ |
assertEquals(poolSize, ran.get()); |
830 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
831 |
+ |
assertTrue(q.isEmpty()); |
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 |
+ |
} |
843 |
+ |
|
844 |
+ |
assertEquals(poolSize, q.size()); |
845 |
+ |
assertEquals(poolSize, ran.get()); |
846 |
+ |
|
847 |
+ |
immediates.forEach( |
848 |
+ |
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
849 |
+ |
|
850 |
+ |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
851 |
+ |
.forEach(f -> assertFalse(f.isDone())); |
852 |
|
|
805 |
– |
assertTrue(p.getQueue().containsAll(periodics)); |
806 |
– |
assertTrue(p.getQueue().containsAll(delayeds)); |
853 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
854 |
|
assertTrue(p.isShutdown()); |
855 |
+ |
assertTrue(p.isTerminating()); |
856 |
|
assertFalse(p.isTerminated()); |
810 |
– |
for (Future<?> periodic : periodics) { |
811 |
– |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
812 |
– |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
813 |
– |
} |
814 |
– |
for (Future<?> delayed : delayeds) { |
815 |
– |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
816 |
– |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
817 |
– |
} |
818 |
– |
if (testImplementationDetails) { |
819 |
– |
assertEquals(effectivePeriodicPolicy, |
820 |
– |
p.getQueue().containsAll(periodics)); |
821 |
– |
assertEquals(effectiveDelayedPolicy, |
822 |
– |
p.getQueue().containsAll(delayeds)); |
823 |
– |
} |
824 |
– |
unblock.countDown(); // Release all pool threads |
857 |
|
|
858 |
+ |
if (rnd.nextBoolean()) |
859 |
+ |
assertThrows( |
860 |
+ |
RejectedExecutionException.class, |
861 |
+ |
() -> p.submit(task), |
862 |
+ |
() -> p.schedule(task, 1, SECONDS), |
863 |
+ |
() -> p.scheduleAtFixedRate( |
864 |
+ |
new PeriodicTask(1), 1, 1, SECONDS), |
865 |
+ |
() -> p.scheduleWithFixedDelay( |
866 |
+ |
new PeriodicTask(2), 1, 1, SECONDS)); |
867 |
+ |
|
868 |
+ |
assertTrue(q.contains(immediates.get(1))); |
869 |
+ |
assertTrue(!effectiveDelayedPolicy |
870 |
+ |
^ q.contains(delayeds.get(1))); |
871 |
+ |
assertTrue(!effectivePeriodicPolicy |
872 |
+ |
^ q.containsAll(periodics.subList(4, 8))); |
873 |
+ |
|
874 |
+ |
immediates.forEach(f -> assertFalse(f.isDone())); |
875 |
+ |
|
876 |
+ |
assertFalse(delayeds.get(0).isDone()); |
877 |
|
if (effectiveDelayedPolicy) |
878 |
< |
for (Future<?> delayed : delayeds) assertNull(delayed.get()); |
879 |
< |
if (effectivePeriodicPolicy) { |
880 |
< |
await(periodicLatch1); |
881 |
< |
await(periodicLatch2); |
882 |
< |
for (Future<?> periodic : periodics) { |
883 |
< |
assertTrue(periodic.cancel(false)); |
884 |
< |
assertTrue(periodic.isCancelled()); |
885 |
< |
assertTrue(periodic.isDone()); |
886 |
< |
} |
878 |
> |
assertFalse(delayeds.get(1).isDone()); |
879 |
> |
else |
880 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
881 |
> |
|
882 |
> |
if (effectivePeriodicPolicy) |
883 |
> |
periodics.forEach( |
884 |
> |
f -> { |
885 |
> |
assertFalse(f.isDone()); |
886 |
> |
if (!periodicTasksContinue) { |
887 |
> |
assertTrue(f.cancel(false)); |
888 |
> |
assertTrue(f.isCancelled()); |
889 |
> |
} |
890 |
> |
}); |
891 |
> |
else { |
892 |
> |
periodics.subList(0, 4).forEach(f -> assertFalse(f.isDone())); |
893 |
> |
periodics.subList(4, 8).forEach(f -> assertTrue(f.isCancelled())); |
894 |
|
} |
895 |
< |
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
895 |
> |
|
896 |
> |
unblock.countDown(); // Release all pool threads |
897 |
> |
|
898 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
899 |
+ |
assertFalse(p.isTerminating()); |
900 |
|
assertTrue(p.isTerminated()); |
901 |
|
|
902 |
< |
for (Future<?> future : delayeds) { |
903 |
< |
assertTrue(effectiveDelayedPolicy ^ future.isCancelled()); |
904 |
< |
assertTrue(future.isDone()); |
905 |
< |
} |
906 |
< |
for (Future<?> future : periodics) |
907 |
< |
assertTrue(future.isCancelled()); |
908 |
< |
for (Future<?> future : blockers) |
909 |
< |
assertNull(future.get()); |
910 |
< |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
902 |
> |
assertTrue(q.isEmpty()); |
903 |
> |
|
904 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
905 |
> |
.forEach(f -> assertTrue(f.isDone())); |
906 |
> |
|
907 |
> |
for (Future<?> f : immediates) assertNull(f.get()); |
908 |
> |
|
909 |
> |
assertNull(delayeds.get(0).get()); |
910 |
> |
if (effectiveDelayedPolicy) |
911 |
> |
assertNull(delayeds.get(1).get()); |
912 |
> |
else |
913 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
914 |
> |
|
915 |
> |
if (periodicTasksContinue) |
916 |
> |
periodics.forEach( |
917 |
> |
f -> { |
918 |
> |
try { f.get(); } |
919 |
> |
catch (ExecutionException success) { |
920 |
> |
assertSame(exception, success.getCause()); |
921 |
> |
} |
922 |
> |
catch (Throwable fail) { threadUnexpectedException(fail); } |
923 |
> |
}); |
924 |
> |
else |
925 |
> |
periodics.forEach(f -> assertTrue(f.isCancelled())); |
926 |
> |
|
927 |
> |
assertEquals(poolSize + 1 |
928 |
> |
+ (effectiveDelayedPolicy ? 1 : 0) |
929 |
> |
+ (periodicTasksContinue ? 4 : 0), |
930 |
> |
ran.get()); |
931 |
|
} |
932 |
|
|
933 |
|
/** |