11 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
|
13 |
|
import java.util.ArrayList; |
14 |
+ |
import java.util.Collection; |
15 |
+ |
import java.util.Collections; |
16 |
|
import java.util.HashSet; |
17 |
|
import java.util.List; |
18 |
|
import java.util.concurrent.BlockingQueue; |
75 |
|
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
76 |
|
assertSame(Boolean.TRUE, f.get()); |
77 |
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
78 |
< |
assertTrue(done.await(0L, MILLISECONDS)); |
78 |
> |
assertEquals(0L, done.getCount()); |
79 |
|
} |
80 |
|
} |
81 |
|
|
243 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
244 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
245 |
|
try { |
246 |
< |
TrackedCallable callable = null; |
247 |
< |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
246 |
> |
Future f = p.schedule((Callable)null, |
247 |
> |
randomTimeout(), randomTimeUnit()); |
248 |
|
shouldThrow(); |
249 |
|
} catch (NullPointerException success) {} |
250 |
|
} |
259 |
|
try { |
260 |
|
p.shutdown(); |
261 |
|
p.schedule(new NoOpRunnable(), |
262 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
262 |
> |
randomTimeout(), randomTimeUnit()); |
263 |
|
shouldThrow(); |
264 |
|
} catch (RejectedExecutionException success) { |
265 |
|
} catch (SecurityException ok) {} |
275 |
|
try { |
276 |
|
p.shutdown(); |
277 |
|
p.schedule(new NoOpCallable(), |
278 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
278 |
> |
randomTimeout(), randomTimeUnit()); |
279 |
|
shouldThrow(); |
280 |
|
} catch (RejectedExecutionException success) { |
281 |
|
} catch (SecurityException ok) {} |
291 |
|
try { |
292 |
|
p.shutdown(); |
293 |
|
p.schedule(new NoOpCallable(), |
294 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
294 |
> |
randomTimeout(), randomTimeUnit()); |
295 |
|
shouldThrow(); |
296 |
|
} catch (RejectedExecutionException success) { |
297 |
|
} catch (SecurityException ok) {} |
748 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
749 |
|
*/ |
750 |
|
public void testShutdown_cancellation() throws Exception { |
751 |
< |
final int poolSize = 6; |
751 |
> |
final int poolSize = 4; |
752 |
|
final ScheduledThreadPoolExecutor p |
753 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
754 |
|
final BlockingQueue<Runnable> q = p.getQueue(); |
755 |
|
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
756 |
+ |
final long delay = rnd.nextInt(2); |
757 |
+ |
final int rounds = rnd.nextInt(1, 3); |
758 |
|
final boolean effectiveDelayedPolicy; |
759 |
|
final boolean effectivePeriodicPolicy; |
760 |
|
final boolean effectiveRemovePolicy; |
820 |
|
List<Future<?>> periodics = new ArrayList<>(); |
821 |
|
|
822 |
|
immediates.add(p.submit(task)); |
823 |
< |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
824 |
< |
for (int rounds : new int[] { 1, 2 }) { |
825 |
< |
periodics.add(p.scheduleAtFixedRate( |
826 |
< |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
827 |
< |
periodics.add(p.scheduleWithFixedDelay( |
824 |
< |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
825 |
< |
} |
823 |
> |
delayeds.add(p.schedule(task, delay, MILLISECONDS)); |
824 |
> |
periodics.add(p.scheduleAtFixedRate( |
825 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
826 |
> |
periodics.add(p.scheduleWithFixedDelay( |
827 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
828 |
|
|
829 |
|
await(poolBlocked); |
830 |
|
|
831 |
|
assertEquals(poolSize, ran.get()); |
832 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
833 |
|
assertTrue(q.isEmpty()); |
834 |
|
|
835 |
|
// Add second wave of tasks. |
836 |
|
immediates.add(p.submit(task)); |
837 |
< |
long delay_ms = effectiveDelayedPolicy ? 1 : LONG_DELAY_MS; |
838 |
< |
delayeds.add(p.schedule(task, delay_ms, MILLISECONDS)); |
839 |
< |
for (int rounds : new int[] { 1, 2 }) { |
840 |
< |
periodics.add(p.scheduleAtFixedRate( |
841 |
< |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
839 |
< |
periodics.add(p.scheduleWithFixedDelay( |
840 |
< |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
841 |
< |
} |
837 |
> |
delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); |
838 |
> |
periodics.add(p.scheduleAtFixedRate( |
839 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
840 |
> |
periodics.add(p.scheduleWithFixedDelay( |
841 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
842 |
|
|
843 |
|
assertEquals(poolSize, q.size()); |
844 |
|
assertEquals(poolSize, ran.get()); |
868 |
|
assertTrue(!effectiveDelayedPolicy |
869 |
|
^ q.contains(delayeds.get(1))); |
870 |
|
assertTrue(!effectivePeriodicPolicy |
871 |
< |
^ q.containsAll(periodics.subList(4, 8))); |
871 |
> |
^ q.containsAll(periodics.subList(2, 4))); |
872 |
|
|
873 |
|
immediates.forEach(f -> assertFalse(f.isDone())); |
874 |
|
|
878 |
|
else |
879 |
|
assertTrue(delayeds.get(1).isCancelled()); |
880 |
|
|
881 |
< |
if (testImplementationDetails) { |
882 |
< |
if (effectivePeriodicPolicy) |
883 |
< |
periodics.forEach( |
884 |
< |
f -> { |
885 |
< |
assertFalse(f.isDone()); |
886 |
< |
if (!periodicTasksContinue) |
887 |
< |
assertTrue(f.cancel(false)); |
888 |
< |
}); |
889 |
< |
else { |
890 |
< |
periodics.subList(0, 4).forEach(f -> assertFalse(f.isDone())); |
891 |
< |
periodics.subList(4, 8).forEach(f -> assertTrue(f.isCancelled())); |
892 |
< |
} |
881 |
> |
if (effectivePeriodicPolicy) |
882 |
> |
periodics.forEach( |
883 |
> |
f -> { |
884 |
> |
assertFalse(f.isDone()); |
885 |
> |
if (!periodicTasksContinue) { |
886 |
> |
assertTrue(f.cancel(false)); |
887 |
> |
assertTrue(f.isCancelled()); |
888 |
> |
} |
889 |
> |
}); |
890 |
> |
else { |
891 |
> |
periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); |
892 |
> |
periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); |
893 |
|
} |
894 |
|
|
895 |
|
unblock.countDown(); // Release all pool threads |
925 |
|
|
926 |
|
assertEquals(poolSize + 1 |
927 |
|
+ (effectiveDelayedPolicy ? 1 : 0) |
928 |
< |
+ (periodicTasksContinue ? 4 : 0), |
928 |
> |
+ (periodicTasksContinue ? 2 : 0), |
929 |
|
ran.get()); |
930 |
|
} |
931 |
|
|
966 |
|
} |
967 |
|
|
968 |
|
/** |
969 |
< |
* invokeAny(null) throws NPE |
969 |
> |
* invokeAny(null) throws NullPointerException |
970 |
|
*/ |
971 |
|
public void testInvokeAny1() throws Exception { |
972 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
979 |
|
} |
980 |
|
|
981 |
|
/** |
982 |
< |
* invokeAny(empty collection) throws IAE |
982 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
983 |
|
*/ |
984 |
|
public void testInvokeAny2() throws Exception { |
985 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
992 |
|
} |
993 |
|
|
994 |
|
/** |
995 |
< |
* invokeAny(c) throws NPE if c has null elements |
995 |
> |
* invokeAny(c) throws NullPointerException if c has null elements |
996 |
|
*/ |
997 |
|
public void testInvokeAny3() throws Exception { |
998 |
|
CountDownLatch latch = new CountDownLatch(1); |
1054 |
|
} |
1055 |
|
|
1056 |
|
/** |
1057 |
< |
* invokeAll(empty collection) returns empty collection |
1057 |
> |
* invokeAll(empty collection) returns empty list |
1058 |
|
*/ |
1059 |
|
public void testInvokeAll2() throws Exception { |
1060 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1061 |
+ |
final Collection<Callable<String>> emptyCollection |
1062 |
+ |
= Collections.emptyList(); |
1063 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1064 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1064 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1065 |
|
assertTrue(r.isEmpty()); |
1066 |
|
} |
1067 |
|
} |
1124 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1125 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1126 |
|
try { |
1127 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1127 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1128 |
|
shouldThrow(); |
1129 |
|
} catch (NullPointerException success) {} |
1130 |
|
} |
1131 |
|
} |
1132 |
|
|
1133 |
|
/** |
1134 |
< |
* timed invokeAny(,,null) throws NPE |
1134 |
> |
* timed invokeAny(,,null) throws NullPointerException |
1135 |
|
*/ |
1136 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1137 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1139 |
|
List<Callable<String>> l = new ArrayList<>(); |
1140 |
|
l.add(new StringTask()); |
1141 |
|
try { |
1142 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1142 |
> |
e.invokeAny(l, randomTimeout(), null); |
1143 |
|
shouldThrow(); |
1144 |
|
} catch (NullPointerException success) {} |
1145 |
|
} |
1146 |
|
} |
1147 |
|
|
1148 |
|
/** |
1149 |
< |
* timed invokeAny(empty collection) throws IAE |
1149 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1150 |
|
*/ |
1151 |
|
public void testTimedInvokeAny2() throws Exception { |
1152 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1153 |
+ |
final Collection<Callable<String>> emptyCollection |
1154 |
+ |
= Collections.emptyList(); |
1155 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1156 |
|
try { |
1157 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1157 |
> |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
1158 |
|
shouldThrow(); |
1159 |
|
} catch (IllegalArgumentException success) {} |
1160 |
|
} |
1171 |
|
l.add(latchAwaitingStringTask(latch)); |
1172 |
|
l.add(null); |
1173 |
|
try { |
1174 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1174 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
1175 |
|
shouldThrow(); |
1176 |
|
} catch (NullPointerException success) {} |
1177 |
|
latch.countDown(); |
1220 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1221 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1222 |
|
try { |
1223 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1223 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1224 |
|
shouldThrow(); |
1225 |
|
} catch (NullPointerException success) {} |
1226 |
|
} |
1235 |
|
List<Callable<String>> l = new ArrayList<>(); |
1236 |
|
l.add(new StringTask()); |
1237 |
|
try { |
1238 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1238 |
> |
e.invokeAll(l, randomTimeout(), null); |
1239 |
|
shouldThrow(); |
1240 |
|
} catch (NullPointerException success) {} |
1241 |
|
} |
1242 |
|
} |
1243 |
|
|
1244 |
|
/** |
1245 |
< |
* timed invokeAll(empty collection) returns empty collection |
1245 |
> |
* timed invokeAll(empty collection) returns empty list |
1246 |
|
*/ |
1247 |
|
public void testTimedInvokeAll2() throws Exception { |
1248 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1249 |
+ |
final Collection<Callable<String>> emptyCollection |
1250 |
+ |
= Collections.emptyList(); |
1251 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1252 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1253 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1252 |
> |
List<Future<String>> r = |
1253 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1254 |
|
assertTrue(r.isEmpty()); |
1255 |
|
} |
1256 |
|
} |
1265 |
|
l.add(new StringTask()); |
1266 |
|
l.add(null); |
1267 |
|
try { |
1268 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1268 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1269 |
|
shouldThrow(); |
1270 |
|
} catch (NullPointerException success) {} |
1271 |
|
} |