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; |
73 |
|
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
74 |
|
assertSame(Boolean.TRUE, f.get()); |
75 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
76 |
< |
assertTrue(done.await(0L, MILLISECONDS)); |
76 |
> |
assertEquals(0L, done.getCount()); |
77 |
|
} |
78 |
|
} |
79 |
|
|
241 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
242 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
243 |
|
try { |
244 |
< |
TrackedCallable callable = null; |
245 |
< |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
244 |
> |
Future f = p.schedule((Callable)null, |
245 |
> |
randomTimeout(), randomTimeUnit()); |
246 |
|
shouldThrow(); |
247 |
|
} catch (NullPointerException success) {} |
248 |
|
} |
746 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
747 |
|
*/ |
748 |
|
public void testShutdown_cancellation() throws Exception { |
749 |
< |
final int poolSize = 2; |
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; |
781 |
|
assertEquals(effectiveRemovePolicy, |
782 |
|
p.getRemoveOnCancelPolicy()); |
783 |
|
|
784 |
< |
// Strategy: Wedge the pool with poolSize "blocker" threads |
784 |
> |
final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean(); |
785 |
> |
|
786 |
> |
// Strategy: Wedge the pool with one wave of "blocker" tasks, |
787 |
> |
// then add a second wave that waits in the queue until unblocked. |
788 |
|
final AtomicInteger ran = new AtomicInteger(0); |
789 |
|
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
790 |
|
final CountDownLatch unblock = new CountDownLatch(1); |
791 |
< |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
792 |
< |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
793 |
< |
Runnable task = new CheckedRunnable() { public void realRun() |
794 |
< |
throws InterruptedException { |
795 |
< |
poolBlocked.countDown(); |
796 |
< |
await(unblock); |
797 |
< |
ran.getAndIncrement(); |
798 |
< |
}}; |
799 |
< |
List<Future<?>> blockers = new ArrayList<>(); |
800 |
< |
List<Future<?>> periodics = new ArrayList<>(); |
801 |
< |
List<Future<?>> delayeds = new ArrayList<>(); |
802 |
< |
for (int i = 0; i < poolSize; i++) |
803 |
< |
blockers.add(p.submit(task)); |
791 |
> |
final RuntimeException exception = new RuntimeException(); |
792 |
> |
|
793 |
> |
class Task implements Runnable { |
794 |
> |
public void run() { |
795 |
> |
try { |
796 |
> |
ran.getAndIncrement(); |
797 |
> |
poolBlocked.countDown(); |
798 |
> |
await(unblock); |
799 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
800 |
> |
} |
801 |
> |
} |
802 |
> |
|
803 |
> |
class PeriodicTask extends Task { |
804 |
> |
PeriodicTask(int rounds) { this.rounds = rounds; } |
805 |
> |
int rounds; |
806 |
> |
public void run() { |
807 |
> |
if (--rounds == 0) super.run(); |
808 |
> |
// throw exception to surely terminate this periodic task, |
809 |
> |
// but in a separate execution and in a detectable way. |
810 |
> |
if (rounds == -1) throw exception; |
811 |
> |
} |
812 |
> |
} |
813 |
> |
|
814 |
> |
Runnable task = new Task(); |
815 |
> |
|
816 |
> |
List<Future<?>> immediates = new ArrayList<>(); |
817 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
818 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
819 |
> |
|
820 |
> |
immediates.add(p.submit(task)); |
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 |
|
|
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 |
+ |
delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); |
836 |
|
periodics.add(p.scheduleAtFixedRate( |
837 |
< |
countDowner(periodicLatch1), 1, 1, MILLISECONDS)); |
837 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
838 |
|
periodics.add(p.scheduleWithFixedDelay( |
839 |
< |
countDowner(periodicLatch2), 1, 1, MILLISECONDS)); |
840 |
< |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
839 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
840 |
> |
|
841 |
> |
assertEquals(poolSize, q.size()); |
842 |
> |
assertEquals(poolSize, ran.get()); |
843 |
> |
|
844 |
> |
immediates.forEach( |
845 |
> |
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
846 |
> |
|
847 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
848 |
> |
.forEach(f -> assertFalse(f.isDone())); |
849 |
|
|
805 |
– |
assertTrue(p.getQueue().containsAll(periodics)); |
806 |
– |
assertTrue(p.getQueue().containsAll(delayeds)); |
850 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
851 |
|
assertTrue(p.isShutdown()); |
852 |
+ |
assertTrue(p.isTerminating()); |
853 |
|
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 |
854 |
|
|
855 |
+ |
if (rnd.nextBoolean()) |
856 |
+ |
assertThrows( |
857 |
+ |
RejectedExecutionException.class, |
858 |
+ |
() -> p.submit(task), |
859 |
+ |
() -> p.schedule(task, 1, SECONDS), |
860 |
+ |
() -> p.scheduleAtFixedRate( |
861 |
+ |
new PeriodicTask(1), 1, 1, SECONDS), |
862 |
+ |
() -> p.scheduleWithFixedDelay( |
863 |
+ |
new PeriodicTask(2), 1, 1, SECONDS)); |
864 |
+ |
|
865 |
+ |
assertTrue(q.contains(immediates.get(1))); |
866 |
+ |
assertTrue(!effectiveDelayedPolicy |
867 |
+ |
^ q.contains(delayeds.get(1))); |
868 |
+ |
assertTrue(!effectivePeriodicPolicy |
869 |
+ |
^ q.containsAll(periodics.subList(2, 4))); |
870 |
+ |
|
871 |
+ |
immediates.forEach(f -> assertFalse(f.isDone())); |
872 |
+ |
|
873 |
+ |
assertFalse(delayeds.get(0).isDone()); |
874 |
|
if (effectiveDelayedPolicy) |
875 |
< |
for (Future<?> delayed : delayeds) assertNull(delayed.get()); |
876 |
< |
if (effectivePeriodicPolicy) { |
877 |
< |
await(periodicLatch1); |
878 |
< |
await(periodicLatch2); |
879 |
< |
for (Future<?> periodic : periodics) { |
880 |
< |
assertTrue(periodic.cancel(false)); |
881 |
< |
assertTrue(periodic.isCancelled()); |
882 |
< |
assertTrue(periodic.isDone()); |
883 |
< |
} |
875 |
> |
assertFalse(delayeds.get(1).isDone()); |
876 |
> |
else |
877 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
878 |
> |
|
879 |
> |
if (effectivePeriodicPolicy) |
880 |
> |
periodics.forEach( |
881 |
> |
f -> { |
882 |
> |
assertFalse(f.isDone()); |
883 |
> |
if (!periodicTasksContinue) { |
884 |
> |
assertTrue(f.cancel(false)); |
885 |
> |
assertTrue(f.isCancelled()); |
886 |
> |
} |
887 |
> |
}); |
888 |
> |
else { |
889 |
> |
periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); |
890 |
> |
periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); |
891 |
|
} |
892 |
< |
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
892 |
> |
|
893 |
> |
unblock.countDown(); // Release all pool threads |
894 |
> |
|
895 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
896 |
+ |
assertFalse(p.isTerminating()); |
897 |
|
assertTrue(p.isTerminated()); |
898 |
|
|
899 |
< |
for (Future<?> future : delayeds) { |
900 |
< |
assertTrue(effectiveDelayedPolicy ^ future.isCancelled()); |
901 |
< |
assertTrue(future.isDone()); |
902 |
< |
} |
903 |
< |
for (Future<?> future : periodics) |
904 |
< |
assertTrue(future.isCancelled()); |
905 |
< |
for (Future<?> future : blockers) |
906 |
< |
assertNull(future.get()); |
907 |
< |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
899 |
> |
assertTrue(q.isEmpty()); |
900 |
> |
|
901 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
902 |
> |
.forEach(f -> assertTrue(f.isDone())); |
903 |
> |
|
904 |
> |
for (Future<?> f : immediates) assertNull(f.get()); |
905 |
> |
|
906 |
> |
assertNull(delayeds.get(0).get()); |
907 |
> |
if (effectiveDelayedPolicy) |
908 |
> |
assertNull(delayeds.get(1).get()); |
909 |
> |
else |
910 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
911 |
> |
|
912 |
> |
if (periodicTasksContinue) |
913 |
> |
periodics.forEach( |
914 |
> |
f -> { |
915 |
> |
try { f.get(); } |
916 |
> |
catch (ExecutionException success) { |
917 |
> |
assertSame(exception, success.getCause()); |
918 |
> |
} |
919 |
> |
catch (Throwable fail) { threadUnexpectedException(fail); } |
920 |
> |
}); |
921 |
> |
else |
922 |
> |
periodics.forEach(f -> assertTrue(f.isCancelled())); |
923 |
> |
|
924 |
> |
assertEquals(poolSize + 1 |
925 |
> |
+ (effectiveDelayedPolicy ? 1 : 0) |
926 |
> |
+ (periodicTasksContinue ? 2 : 0), |
927 |
> |
ran.get()); |
928 |
|
} |
929 |
|
|
930 |
|
/** |