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; |
26 |
|
import java.util.concurrent.ScheduledFuture; |
27 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
28 |
|
import java.util.concurrent.ThreadFactory; |
29 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
32 |
|
import java.util.concurrent.atomic.AtomicInteger; |
33 |
|
import java.util.concurrent.atomic.AtomicLong; |
34 |
+ |
import java.util.stream.Stream; |
35 |
|
|
36 |
|
import junit.framework.Test; |
37 |
|
import junit.framework.TestSuite; |
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 |
|
|
224 |
|
} |
225 |
|
|
226 |
|
/** |
227 |
< |
* execute(null) throws NPE |
227 |
> |
* Submitting null tasks throws NullPointerException |
228 |
|
*/ |
229 |
< |
public void testExecuteNull() throws InterruptedException { |
229 |
> |
public void testNullTaskSubmission() { |
230 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
231 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
232 |
< |
try { |
229 |
< |
p.execute(null); |
230 |
< |
shouldThrow(); |
231 |
< |
} catch (NullPointerException success) {} |
232 |
> |
assertNullTaskSubmissionThrowsNullPointerException(p); |
233 |
|
} |
234 |
|
} |
235 |
|
|
236 |
|
/** |
237 |
< |
* schedule(null) throws NPE |
237 |
> |
* Submitted tasks are rejected when shutdown |
238 |
|
*/ |
239 |
< |
public void testScheduleNull() throws InterruptedException { |
239 |
> |
public void testSubmittedTasksRejectedWhenShutdown() throws InterruptedException { |
240 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
241 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
242 |
< |
try { |
243 |
< |
TrackedCallable callable = null; |
244 |
< |
Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
245 |
< |
shouldThrow(); |
246 |
< |
} catch (NullPointerException success) {} |
247 |
< |
} |
248 |
< |
} |
241 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
242 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(p.getCorePoolSize()); |
243 |
> |
final CountDownLatch done = new CountDownLatch(1); |
244 |
> |
final Runnable r = () -> { |
245 |
> |
threadsStarted.countDown(); |
246 |
> |
for (;;) { |
247 |
> |
try { |
248 |
> |
done.await(); |
249 |
> |
return; |
250 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
251 |
> |
}}; |
252 |
> |
final Callable<Boolean> c = () -> { |
253 |
> |
threadsStarted.countDown(); |
254 |
> |
for (;;) { |
255 |
> |
try { |
256 |
> |
done.await(); |
257 |
> |
return Boolean.TRUE; |
258 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
259 |
> |
}}; |
260 |
|
|
261 |
< |
/** |
262 |
< |
* execute throws RejectedExecutionException if shutdown |
263 |
< |
*/ |
264 |
< |
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
265 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
266 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
267 |
< |
try { |
268 |
< |
p.shutdown(); |
269 |
< |
p.schedule(new NoOpRunnable(), |
258 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
259 |
< |
shouldThrow(); |
260 |
< |
} catch (RejectedExecutionException success) { |
261 |
< |
} catch (SecurityException ok) {} |
262 |
< |
} |
263 |
< |
} |
261 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
262 |
> |
for (int i = p.getCorePoolSize(); i--> 0; ) { |
263 |
> |
switch (rnd.nextInt(4)) { |
264 |
> |
case 0: p.execute(r); break; |
265 |
> |
case 1: assertFalse(p.submit(r).isDone()); break; |
266 |
> |
case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; |
267 |
> |
case 3: assertFalse(p.submit(c).isDone()); break; |
268 |
> |
} |
269 |
> |
} |
270 |
|
|
271 |
< |
/** |
272 |
< |
* schedule throws RejectedExecutionException if shutdown |
267 |
< |
*/ |
268 |
< |
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
269 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
270 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
271 |
< |
try { |
272 |
< |
p.shutdown(); |
273 |
< |
p.schedule(new NoOpCallable(), |
274 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
275 |
< |
shouldThrow(); |
276 |
< |
} catch (RejectedExecutionException success) { |
277 |
< |
} catch (SecurityException ok) {} |
278 |
< |
} |
279 |
< |
} |
271 |
> |
// ScheduledThreadPoolExecutor has an unbounded queue, so never saturated. |
272 |
> |
await(threadsStarted); |
273 |
|
|
274 |
< |
/** |
275 |
< |
* schedule callable throws RejectedExecutionException if shutdown |
276 |
< |
*/ |
284 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
285 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
286 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
287 |
< |
try { |
274 |
> |
if (rnd.nextBoolean()) |
275 |
> |
p.shutdownNow(); |
276 |
> |
else |
277 |
|
p.shutdown(); |
278 |
< |
p.schedule(new NoOpCallable(), |
279 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
280 |
< |
shouldThrow(); |
292 |
< |
} catch (RejectedExecutionException success) { |
293 |
< |
} catch (SecurityException ok) {} |
294 |
< |
} |
295 |
< |
} |
278 |
> |
// Pool is shutdown, but not yet terminated |
279 |
> |
assertTaskSubmissionsAreRejected(p); |
280 |
> |
assertFalse(p.isTerminated()); |
281 |
|
|
282 |
< |
/** |
283 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
299 |
< |
*/ |
300 |
< |
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
301 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
302 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
303 |
< |
try { |
304 |
< |
p.shutdown(); |
305 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), |
306 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
307 |
< |
shouldThrow(); |
308 |
< |
} catch (RejectedExecutionException success) { |
309 |
< |
} catch (SecurityException ok) {} |
310 |
< |
} |
311 |
< |
} |
282 |
> |
done.countDown(); // release blocking tasks |
283 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
284 |
|
|
285 |
< |
/** |
314 |
< |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
315 |
< |
*/ |
316 |
< |
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
317 |
< |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
318 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
319 |
< |
try { |
320 |
< |
p.shutdown(); |
321 |
< |
p.scheduleWithFixedDelay(new NoOpRunnable(), |
322 |
< |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
323 |
< |
shouldThrow(); |
324 |
< |
} catch (RejectedExecutionException success) { |
325 |
< |
} catch (SecurityException ok) {} |
285 |
> |
assertTaskSubmissionsAreRejected(p); |
286 |
|
} |
287 |
+ |
assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount()); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
607 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
608 |
|
try (PoolCleaner cleaner = cleaner(p, releaser)) { |
609 |
|
for (int i = 0; i < tasks.length; i++) |
610 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
610 |
> |
tasks[i] = p.schedule(possiblyInterruptedRunnable(SMALL_DELAY_MS), |
611 |
|
LONG_DELAY_MS, MILLISECONDS); |
612 |
|
int max = tasks.length; |
613 |
|
if (tasks[4].cancel(true)) --max; |
704 |
|
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
705 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
706 |
|
*/ |
707 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
708 |
|
public void testShutdown_cancellation() throws Exception { |
709 |
< |
final int UNSPECIFIED = 0, YES = 1, NO = 2; |
748 |
< |
for (int maybe : new int[] { UNSPECIFIED, YES, NO }) |
749 |
< |
{ |
750 |
< |
final int poolSize = 2; |
709 |
> |
final int poolSize = 4; |
710 |
|
final ScheduledThreadPoolExecutor p |
711 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
712 |
< |
final boolean effectiveDelayedPolicy = (maybe != NO); |
713 |
< |
final boolean effectivePeriodicPolicy = (maybe == YES); |
714 |
< |
final boolean effectiveRemovePolicy = (maybe == YES); |
715 |
< |
if (maybe != UNSPECIFIED) { |
716 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(maybe == YES); |
717 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(maybe == YES); |
718 |
< |
p.setRemoveOnCancelPolicy(maybe == YES); |
719 |
< |
} |
712 |
> |
final BlockingQueue<Runnable> q = p.getQueue(); |
713 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
714 |
> |
final long delay = rnd.nextInt(2); |
715 |
> |
final int rounds = rnd.nextInt(1, 3); |
716 |
> |
final boolean effectiveDelayedPolicy; |
717 |
> |
final boolean effectivePeriodicPolicy; |
718 |
> |
final boolean effectiveRemovePolicy; |
719 |
> |
|
720 |
> |
if (rnd.nextBoolean()) |
721 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy( |
722 |
> |
effectiveDelayedPolicy = rnd.nextBoolean()); |
723 |
> |
else |
724 |
> |
effectiveDelayedPolicy = true; |
725 |
|
assertEquals(effectiveDelayedPolicy, |
726 |
|
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
727 |
+ |
|
728 |
+ |
if (rnd.nextBoolean()) |
729 |
+ |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy( |
730 |
+ |
effectivePeriodicPolicy = rnd.nextBoolean()); |
731 |
+ |
else |
732 |
+ |
effectivePeriodicPolicy = false; |
733 |
|
assertEquals(effectivePeriodicPolicy, |
734 |
|
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
735 |
+ |
|
736 |
+ |
if (rnd.nextBoolean()) |
737 |
+ |
p.setRemoveOnCancelPolicy( |
738 |
+ |
effectiveRemovePolicy = rnd.nextBoolean()); |
739 |
+ |
else |
740 |
+ |
effectiveRemovePolicy = false; |
741 |
|
assertEquals(effectiveRemovePolicy, |
742 |
|
p.getRemoveOnCancelPolicy()); |
743 |
< |
// Strategy: Wedge the pool with poolSize "blocker" threads |
743 |
> |
|
744 |
> |
final boolean periodicTasksContinue = effectivePeriodicPolicy && rnd.nextBoolean(); |
745 |
> |
|
746 |
> |
// Strategy: Wedge the pool with one wave of "blocker" tasks, |
747 |
> |
// then add a second wave that waits in the queue until unblocked. |
748 |
|
final AtomicInteger ran = new AtomicInteger(0); |
749 |
|
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
750 |
|
final CountDownLatch unblock = new CountDownLatch(1); |
751 |
< |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
752 |
< |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
753 |
< |
Runnable task = new CheckedRunnable() { public void realRun() |
754 |
< |
throws InterruptedException { |
755 |
< |
poolBlocked.countDown(); |
756 |
< |
await(unblock); |
757 |
< |
ran.getAndIncrement(); |
758 |
< |
}}; |
759 |
< |
List<Future<?>> blockers = new ArrayList<>(); |
760 |
< |
List<Future<?>> periodics = new ArrayList<>(); |
761 |
< |
List<Future<?>> delayeds = new ArrayList<>(); |
762 |
< |
for (int i = 0; i < poolSize; i++) |
763 |
< |
blockers.add(p.submit(task)); |
751 |
> |
final RuntimeException exception = new RuntimeException(); |
752 |
> |
|
753 |
> |
class Task implements Runnable { |
754 |
> |
public void run() { |
755 |
> |
try { |
756 |
> |
ran.getAndIncrement(); |
757 |
> |
poolBlocked.countDown(); |
758 |
> |
await(unblock); |
759 |
> |
} catch (Throwable fail) { threadUnexpectedException(fail); } |
760 |
> |
} |
761 |
> |
} |
762 |
> |
|
763 |
> |
class PeriodicTask extends Task { |
764 |
> |
PeriodicTask(int rounds) { this.rounds = rounds; } |
765 |
> |
int rounds; |
766 |
> |
public void run() { |
767 |
> |
if (--rounds == 0) super.run(); |
768 |
> |
// throw exception to surely terminate this periodic task, |
769 |
> |
// but in a separate execution and in a detectable way. |
770 |
> |
if (rounds == -1) throw exception; |
771 |
> |
} |
772 |
> |
} |
773 |
> |
|
774 |
> |
Runnable task = new Task(); |
775 |
> |
|
776 |
> |
List<Future<?>> immediates = new ArrayList<>(); |
777 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
778 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
779 |
> |
|
780 |
> |
immediates.add(p.submit(task)); |
781 |
> |
delayeds.add(p.schedule(task, delay, MILLISECONDS)); |
782 |
> |
periodics.add(p.scheduleAtFixedRate( |
783 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
784 |
> |
periodics.add(p.scheduleWithFixedDelay( |
785 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
786 |
> |
|
787 |
|
await(poolBlocked); |
788 |
|
|
789 |
+ |
assertEquals(poolSize, ran.get()); |
790 |
+ |
assertEquals(poolSize, p.getActiveCount()); |
791 |
+ |
assertTrue(q.isEmpty()); |
792 |
+ |
|
793 |
+ |
// Add second wave of tasks. |
794 |
+ |
immediates.add(p.submit(task)); |
795 |
+ |
delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); |
796 |
|
periodics.add(p.scheduleAtFixedRate( |
797 |
< |
countDowner(periodicLatch1), 1, 1, MILLISECONDS)); |
797 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
798 |
|
periodics.add(p.scheduleWithFixedDelay( |
799 |
< |
countDowner(periodicLatch2), 1, 1, MILLISECONDS)); |
800 |
< |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
799 |
> |
new PeriodicTask(rounds), delay, 1, MILLISECONDS)); |
800 |
> |
|
801 |
> |
assertEquals(poolSize, q.size()); |
802 |
> |
assertEquals(poolSize, ran.get()); |
803 |
> |
|
804 |
> |
immediates.forEach( |
805 |
> |
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
806 |
> |
|
807 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream) |
808 |
> |
.forEach(f -> assertFalse(f.isDone())); |
809 |
|
|
792 |
– |
assertTrue(p.getQueue().containsAll(periodics)); |
793 |
– |
assertTrue(p.getQueue().containsAll(delayeds)); |
810 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
811 |
|
assertTrue(p.isShutdown()); |
812 |
+ |
assertTrue(p.isTerminating()); |
813 |
|
assertFalse(p.isTerminated()); |
797 |
– |
for (Future<?> periodic : periodics) { |
798 |
– |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
799 |
– |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
800 |
– |
} |
801 |
– |
for (Future<?> delayed : delayeds) { |
802 |
– |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
803 |
– |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
804 |
– |
} |
805 |
– |
if (testImplementationDetails) { |
806 |
– |
assertEquals(effectivePeriodicPolicy, |
807 |
– |
p.getQueue().containsAll(periodics)); |
808 |
– |
assertEquals(effectiveDelayedPolicy, |
809 |
– |
p.getQueue().containsAll(delayeds)); |
810 |
– |
} |
811 |
– |
unblock.countDown(); // Release all pool threads |
814 |
|
|
815 |
+ |
if (rnd.nextBoolean()) |
816 |
+ |
assertThrows( |
817 |
+ |
RejectedExecutionException.class, |
818 |
+ |
() -> p.submit(task), |
819 |
+ |
() -> p.schedule(task, 1, SECONDS), |
820 |
+ |
() -> p.scheduleAtFixedRate( |
821 |
+ |
new PeriodicTask(1), 1, 1, SECONDS), |
822 |
+ |
() -> p.scheduleWithFixedDelay( |
823 |
+ |
new PeriodicTask(2), 1, 1, SECONDS)); |
824 |
+ |
|
825 |
+ |
assertTrue(q.contains(immediates.get(1))); |
826 |
+ |
assertTrue(!effectiveDelayedPolicy |
827 |
+ |
^ q.contains(delayeds.get(1))); |
828 |
+ |
assertTrue(!effectivePeriodicPolicy |
829 |
+ |
^ q.containsAll(periodics.subList(2, 4))); |
830 |
+ |
|
831 |
+ |
immediates.forEach(f -> assertFalse(f.isDone())); |
832 |
+ |
|
833 |
+ |
assertFalse(delayeds.get(0).isDone()); |
834 |
|
if (effectiveDelayedPolicy) |
835 |
< |
for (Future<?> delayed : delayeds) assertNull(delayed.get()); |
836 |
< |
if (effectivePeriodicPolicy) { |
837 |
< |
await(periodicLatch1); |
838 |
< |
await(periodicLatch2); |
839 |
< |
for (Future<?> periodic : periodics) { |
840 |
< |
assertTrue(periodic.cancel(false)); |
841 |
< |
assertTrue(periodic.isCancelled()); |
842 |
< |
assertTrue(periodic.isDone()); |
843 |
< |
} |
835 |
> |
assertFalse(delayeds.get(1).isDone()); |
836 |
> |
else |
837 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
838 |
> |
|
839 |
> |
if (effectivePeriodicPolicy) |
840 |
> |
periodics.forEach( |
841 |
> |
f -> { |
842 |
> |
assertFalse(f.isDone()); |
843 |
> |
if (!periodicTasksContinue) { |
844 |
> |
assertTrue(f.cancel(false)); |
845 |
> |
assertTrue(f.isCancelled()); |
846 |
> |
} |
847 |
> |
}); |
848 |
> |
else { |
849 |
> |
periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); |
850 |
> |
periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); |
851 |
|
} |
852 |
< |
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
852 |
> |
|
853 |
> |
unblock.countDown(); // Release all pool threads |
854 |
> |
|
855 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
856 |
+ |
assertFalse(p.isTerminating()); |
857 |
|
assertTrue(p.isTerminated()); |
858 |
|
|
859 |
< |
for (Future<?> future : delayeds) { |
860 |
< |
assertTrue(effectiveDelayedPolicy ^ future.isCancelled()); |
861 |
< |
assertTrue(future.isDone()); |
862 |
< |
} |
863 |
< |
for (Future<?> future : periodics) |
864 |
< |
assertTrue(future.isCancelled()); |
865 |
< |
for (Future<?> future : blockers) |
866 |
< |
assertNull(future.get()); |
867 |
< |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
868 |
< |
}} |
859 |
> |
assertTrue(q.isEmpty()); |
860 |
> |
|
861 |
> |
Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream) |
862 |
> |
.forEach(f -> assertTrue(f.isDone())); |
863 |
> |
|
864 |
> |
for (Future<?> f : immediates) assertNull(f.get()); |
865 |
> |
|
866 |
> |
assertNull(delayeds.get(0).get()); |
867 |
> |
if (effectiveDelayedPolicy) |
868 |
> |
assertNull(delayeds.get(1).get()); |
869 |
> |
else |
870 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
871 |
> |
|
872 |
> |
if (periodicTasksContinue) |
873 |
> |
periodics.forEach( |
874 |
> |
f -> { |
875 |
> |
try { f.get(); } |
876 |
> |
catch (ExecutionException success) { |
877 |
> |
assertSame(exception, success.getCause()); |
878 |
> |
} |
879 |
> |
catch (Throwable fail) { threadUnexpectedException(fail); } |
880 |
> |
}); |
881 |
> |
else |
882 |
> |
periodics.forEach(f -> assertTrue(f.isCancelled())); |
883 |
> |
|
884 |
> |
assertEquals(poolSize + 1 |
885 |
> |
+ (effectiveDelayedPolicy ? 1 : 0) |
886 |
> |
+ (periodicTasksContinue ? 2 : 0), |
887 |
> |
ran.get()); |
888 |
> |
} |
889 |
|
|
890 |
|
/** |
891 |
|
* completed submit of callable returns result |
924 |
|
} |
925 |
|
|
926 |
|
/** |
927 |
< |
* invokeAny(null) throws NPE |
927 |
> |
* invokeAny(null) throws NullPointerException |
928 |
|
*/ |
929 |
|
public void testInvokeAny1() throws Exception { |
930 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
937 |
|
} |
938 |
|
|
939 |
|
/** |
940 |
< |
* invokeAny(empty collection) throws IAE |
940 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
941 |
|
*/ |
942 |
|
public void testInvokeAny2() throws Exception { |
943 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
950 |
|
} |
951 |
|
|
952 |
|
/** |
953 |
< |
* invokeAny(c) throws NPE if c has null elements |
953 |
> |
* invokeAny(c) throws NullPointerException if c has null elements |
954 |
|
*/ |
955 |
|
public void testInvokeAny3() throws Exception { |
956 |
|
CountDownLatch latch = new CountDownLatch(1); |
1012 |
|
} |
1013 |
|
|
1014 |
|
/** |
1015 |
< |
* invokeAll(empty collection) returns empty collection |
1015 |
> |
* invokeAll(empty collection) returns empty list |
1016 |
|
*/ |
1017 |
|
public void testInvokeAll2() throws Exception { |
1018 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1019 |
+ |
final Collection<Callable<String>> emptyCollection |
1020 |
+ |
= Collections.emptyList(); |
1021 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1022 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1022 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1023 |
|
assertTrue(r.isEmpty()); |
1024 |
|
} |
1025 |
|
} |
1082 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1083 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1084 |
|
try { |
1085 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1085 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1086 |
|
shouldThrow(); |
1087 |
|
} catch (NullPointerException success) {} |
1088 |
|
} |
1089 |
|
} |
1090 |
|
|
1091 |
|
/** |
1092 |
< |
* timed invokeAny(,,null) throws NPE |
1092 |
> |
* timed invokeAny(,,null) throws NullPointerException |
1093 |
|
*/ |
1094 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1095 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1097 |
|
List<Callable<String>> l = new ArrayList<>(); |
1098 |
|
l.add(new StringTask()); |
1099 |
|
try { |
1100 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1100 |
> |
e.invokeAny(l, randomTimeout(), null); |
1101 |
|
shouldThrow(); |
1102 |
|
} catch (NullPointerException success) {} |
1103 |
|
} |
1104 |
|
} |
1105 |
|
|
1106 |
|
/** |
1107 |
< |
* timed invokeAny(empty collection) throws IAE |
1107 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1108 |
|
*/ |
1109 |
|
public void testTimedInvokeAny2() throws Exception { |
1110 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1111 |
+ |
final Collection<Callable<String>> emptyCollection |
1112 |
+ |
= Collections.emptyList(); |
1113 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1114 |
|
try { |
1115 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1115 |
> |
e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); |
1116 |
|
shouldThrow(); |
1117 |
|
} catch (IllegalArgumentException success) {} |
1118 |
|
} |
1129 |
|
l.add(latchAwaitingStringTask(latch)); |
1130 |
|
l.add(null); |
1131 |
|
try { |
1132 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1132 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
1133 |
|
shouldThrow(); |
1134 |
|
} catch (NullPointerException success) {} |
1135 |
|
latch.countDown(); |
1178 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1179 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1180 |
|
try { |
1181 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1181 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1182 |
|
shouldThrow(); |
1183 |
|
} catch (NullPointerException success) {} |
1184 |
|
} |
1193 |
|
List<Callable<String>> l = new ArrayList<>(); |
1194 |
|
l.add(new StringTask()); |
1195 |
|
try { |
1196 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1196 |
> |
e.invokeAll(l, randomTimeout(), null); |
1197 |
|
shouldThrow(); |
1198 |
|
} catch (NullPointerException success) {} |
1199 |
|
} |
1200 |
|
} |
1201 |
|
|
1202 |
|
/** |
1203 |
< |
* timed invokeAll(empty collection) returns empty collection |
1203 |
> |
* timed invokeAll(empty collection) returns empty list |
1204 |
|
*/ |
1205 |
|
public void testTimedInvokeAll2() throws Exception { |
1206 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1207 |
+ |
final Collection<Callable<String>> emptyCollection |
1208 |
+ |
= Collections.emptyList(); |
1209 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1210 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1211 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
1210 |
> |
List<Future<String>> r = |
1211 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1212 |
|
assertTrue(r.isEmpty()); |
1213 |
|
} |
1214 |
|
} |
1223 |
|
l.add(new StringTask()); |
1224 |
|
l.add(null); |
1225 |
|
try { |
1226 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1226 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1227 |
|
shouldThrow(); |
1228 |
|
} catch (NullPointerException success) {} |
1229 |
|
} |
1309 |
|
* one-shot task from executing. |
1310 |
|
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1311 |
|
*/ |
1312 |
+ |
@SuppressWarnings("FutureReturnValueIgnored") |
1313 |
|
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1314 |
|
final CountDownLatch delayedDone = new CountDownLatch(1); |
1315 |
|
final CountDownLatch immediateDone = new CountDownLatch(1); |
1316 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1317 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
1318 |
< |
final Runnable immediate = new Runnable() { public void run() { |
1261 |
< |
immediateDone.countDown(); |
1262 |
< |
}}; |
1263 |
< |
final Runnable delayed = new Runnable() { public void run() { |
1318 |
> |
final Runnable delayed = () -> { |
1319 |
|
delayedDone.countDown(); |
1320 |
< |
p.submit(immediate); |
1321 |
< |
}}; |
1320 |
> |
p.submit(() -> immediateDone.countDown()); |
1321 |
> |
}; |
1322 |
|
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1323 |
|
await(delayedDone); |
1324 |
|
await(immediateDone); |