6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.*; |
11 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
+ |
|
12 |
+ |
import java.util.ArrayList; |
13 |
+ |
import java.util.HashSet; |
14 |
+ |
import java.util.List; |
15 |
+ |
import java.util.concurrent.BlockingQueue; |
16 |
+ |
import java.util.concurrent.Callable; |
17 |
+ |
import java.util.concurrent.CancellationException; |
18 |
+ |
import java.util.concurrent.CountDownLatch; |
19 |
+ |
import java.util.concurrent.ExecutionException; |
20 |
+ |
import java.util.concurrent.Executors; |
21 |
+ |
import java.util.concurrent.ExecutorService; |
22 |
+ |
import java.util.concurrent.Future; |
23 |
+ |
import java.util.concurrent.RejectedExecutionException; |
24 |
+ |
import java.util.concurrent.ScheduledFuture; |
25 |
+ |
import java.util.concurrent.ScheduledThreadPoolExecutor; |
26 |
+ |
import java.util.concurrent.ThreadFactory; |
27 |
+ |
import java.util.concurrent.ThreadPoolExecutor; |
28 |
|
import java.util.concurrent.atomic.AtomicInteger; |
29 |
|
|
30 |
+ |
import junit.framework.Test; |
31 |
+ |
import junit.framework.TestSuite; |
32 |
+ |
|
33 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
34 |
|
public static void main(String[] args) { |
35 |
< |
junit.textui.TestRunner.run(suite()); |
35 |
> |
main(suite(), args); |
36 |
|
} |
37 |
|
public static Test suite() { |
38 |
|
return new TestSuite(ScheduledExecutorTest.class); |
164 |
|
long startTime = System.nanoTime(); |
165 |
|
int cycles = 10; |
166 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
167 |
< |
CheckedRunnable task = new CheckedRunnable() { |
167 |
> |
Runnable task = new CheckedRunnable() { |
168 |
|
public void realRun() { done.countDown(); }}; |
169 |
|
ScheduledFuture h = |
170 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
192 |
|
long startTime = System.nanoTime(); |
193 |
|
int cycles = 10; |
194 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
195 |
< |
CheckedRunnable task = new CheckedRunnable() { |
195 |
> |
Runnable task = new CheckedRunnable() { |
196 |
|
public void realRun() { done.countDown(); }}; |
197 |
|
ScheduledFuture h = |
198 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
653 |
|
} |
654 |
|
|
655 |
|
/** |
656 |
< |
* shutdownNow returns a list containing tasks that were not run |
656 |
> |
* shutdownNow returns a list containing tasks that were not run, |
657 |
> |
* and those tasks are drained from the queue |
658 |
|
*/ |
659 |
< |
public void testShutdownNow() { |
660 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
661 |
< |
for (int i = 0; i < 5; i++) |
662 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
663 |
< |
LONG_DELAY_MS, MILLISECONDS); |
664 |
< |
try { |
665 |
< |
List<Runnable> l = p.shutdownNow(); |
666 |
< |
assertTrue(p.isShutdown()); |
667 |
< |
assertEquals(5, l.size()); |
659 |
> |
public void testShutdownNow() throws InterruptedException { |
660 |
> |
final int poolSize = 2; |
661 |
> |
final int count = 5; |
662 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
663 |
> |
final ScheduledThreadPoolExecutor p = |
664 |
> |
new ScheduledThreadPoolExecutor(poolSize); |
665 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
666 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
667 |
> |
threadsStarted.countDown(); |
668 |
> |
try { |
669 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
670 |
> |
} catch (InterruptedException success) {} |
671 |
> |
ran.getAndIncrement(); |
672 |
> |
}}; |
673 |
> |
for (int i = 0; i < count; i++) |
674 |
> |
p.execute(waiter); |
675 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
676 |
> |
assertEquals(poolSize, p.getActiveCount()); |
677 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
678 |
> |
final List<Runnable> queuedTasks; |
679 |
> |
try { |
680 |
> |
queuedTasks = p.shutdownNow(); |
681 |
|
} catch (SecurityException ok) { |
682 |
< |
// Allowed in case test doesn't have privs |
651 |
< |
} finally { |
652 |
< |
joinPool(p); |
682 |
> |
return; // Allowed in case test doesn't have privs |
683 |
|
} |
684 |
+ |
assertTrue(p.isShutdown()); |
685 |
+ |
assertTrue(p.getQueue().isEmpty()); |
686 |
+ |
assertEquals(count - poolSize, queuedTasks.size()); |
687 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
688 |
+ |
assertTrue(p.isTerminated()); |
689 |
+ |
assertEquals(poolSize, ran.get()); |
690 |
+ |
assertEquals(poolSize, p.getCompletedTaskCount()); |
691 |
|
} |
692 |
|
|
693 |
|
/** |
694 |
< |
* In default setting, shutdown cancels periodic but not delayed |
695 |
< |
* tasks at shutdown |
694 |
> |
* shutdownNow returns a list containing tasks that were not run, |
695 |
> |
* and those tasks are drained from the queue |
696 |
|
*/ |
697 |
< |
public void testShutdown1() throws InterruptedException { |
697 |
> |
public void testShutdownNow_delayedTasks() throws InterruptedException { |
698 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
699 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
700 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
701 |
< |
|
702 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
703 |
< |
for (int i = 0; i < tasks.length; i++) |
704 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
705 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
706 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
707 |
< |
BlockingQueue<Runnable> q = p.getQueue(); |
708 |
< |
for (ScheduledFuture task : tasks) { |
709 |
< |
assertFalse(task.isDone()); |
710 |
< |
assertFalse(task.isCancelled()); |
711 |
< |
assertTrue(q.contains(task)); |
699 |
> |
List<ScheduledFuture> tasks = new ArrayList<>(); |
700 |
> |
for (int i = 0; i < 3; i++) { |
701 |
> |
Runnable r = new NoOpRunnable(); |
702 |
> |
tasks.add(p.schedule(r, 9, SECONDS)); |
703 |
> |
tasks.add(p.scheduleAtFixedRate(r, 9, 9, SECONDS)); |
704 |
> |
tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); |
705 |
> |
} |
706 |
> |
if (testImplementationDetails) |
707 |
> |
assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); |
708 |
> |
final List<Runnable> queuedTasks; |
709 |
> |
try { |
710 |
> |
queuedTasks = p.shutdownNow(); |
711 |
> |
} catch (SecurityException ok) { |
712 |
> |
return; // Allowed in case test doesn't have privs |
713 |
|
} |
714 |
|
assertTrue(p.isShutdown()); |
715 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
716 |
< |
assertTrue(p.isTerminated()); |
715 |
> |
assertTrue(p.getQueue().isEmpty()); |
716 |
> |
if (testImplementationDetails) |
717 |
> |
assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); |
718 |
> |
assertEquals(tasks.size(), queuedTasks.size()); |
719 |
|
for (ScheduledFuture task : tasks) { |
720 |
< |
assertTrue(task.isDone()); |
720 |
> |
assertFalse(task.isDone()); |
721 |
|
assertFalse(task.isCancelled()); |
722 |
|
} |
723 |
< |
} |
684 |
< |
|
685 |
< |
/** |
686 |
< |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
687 |
< |
* delayed tasks are cancelled at shutdown |
688 |
< |
*/ |
689 |
< |
public void testShutdown2() throws InterruptedException { |
690 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
691 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
692 |
< |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
693 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
694 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
695 |
< |
for (int i = 0; i < tasks.length; i++) |
696 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
697 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
698 |
< |
BlockingQueue q = p.getQueue(); |
699 |
< |
assertEquals(tasks.length, q.size()); |
700 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
701 |
< |
assertTrue(p.isShutdown()); |
702 |
< |
assertTrue(q.isEmpty()); |
703 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
723 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
724 |
|
assertTrue(p.isTerminated()); |
705 |
– |
for (ScheduledFuture task : tasks) { |
706 |
– |
assertTrue(task.isDone()); |
707 |
– |
assertTrue(task.isCancelled()); |
708 |
– |
} |
725 |
|
} |
726 |
|
|
727 |
|
/** |
728 |
< |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
729 |
< |
* periodic tasks are cancelled at shutdown |
730 |
< |
*/ |
731 |
< |
public void testShutdown3() throws InterruptedException { |
732 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
733 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
734 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
735 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
736 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
737 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
738 |
< |
long initialDelay = LONG_DELAY_MS; |
739 |
< |
ScheduledFuture task = |
740 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
741 |
< |
5, MILLISECONDS); |
728 |
> |
* By default, periodic tasks are cancelled at shutdown. |
729 |
> |
* By default, delayed tasks keep running after shutdown. |
730 |
> |
* Check that changing the default values work: |
731 |
> |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
732 |
> |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
733 |
> |
*/ |
734 |
> |
public void testShutdown_cancellation() throws Exception { |
735 |
> |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
736 |
> |
for (Boolean policy : allBooleans) |
737 |
> |
{ |
738 |
> |
final int poolSize = 2; |
739 |
> |
final ScheduledThreadPoolExecutor p |
740 |
> |
= new ScheduledThreadPoolExecutor(poolSize); |
741 |
> |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
742 |
> |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
743 |
> |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
744 |
> |
if (policy != null) { |
745 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
746 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
747 |
> |
p.setRemoveOnCancelPolicy(policy); |
748 |
> |
} |
749 |
> |
assertEquals(effectiveDelayedPolicy, |
750 |
> |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
751 |
> |
assertEquals(effectivePeriodicPolicy, |
752 |
> |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
753 |
> |
assertEquals(effectiveRemovePolicy, |
754 |
> |
p.getRemoveOnCancelPolicy()); |
755 |
> |
// Strategy: Wedge the pool with poolSize "blocker" threads |
756 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
757 |
> |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
758 |
> |
final CountDownLatch unblock = new CountDownLatch(1); |
759 |
> |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
760 |
> |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
761 |
> |
Runnable task = new CheckedRunnable() { public void realRun() |
762 |
> |
throws InterruptedException { |
763 |
> |
poolBlocked.countDown(); |
764 |
> |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
765 |
> |
ran.getAndIncrement(); |
766 |
> |
}}; |
767 |
> |
List<Future<?>> blockers = new ArrayList<>(); |
768 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
769 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
770 |
> |
for (int i = 0; i < poolSize; i++) |
771 |
> |
blockers.add(p.submit(task)); |
772 |
> |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
773 |
> |
|
774 |
> |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
775 |
> |
1, 1, MILLISECONDS)); |
776 |
> |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
777 |
> |
1, 1, MILLISECONDS)); |
778 |
> |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
779 |
> |
|
780 |
> |
assertTrue(p.getQueue().containsAll(periodics)); |
781 |
> |
assertTrue(p.getQueue().containsAll(delayeds)); |
782 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
783 |
|
assertTrue(p.isShutdown()); |
784 |
< |
assertTrue(p.getQueue().isEmpty()); |
785 |
< |
assertTrue(task.isDone()); |
786 |
< |
assertTrue(task.isCancelled()); |
787 |
< |
joinPool(p); |
788 |
< |
} |
789 |
< |
|
790 |
< |
/** |
791 |
< |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
792 |
< |
* periodic tasks are not cancelled at shutdown |
793 |
< |
*/ |
794 |
< |
public void testShutdown4() throws InterruptedException { |
795 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
796 |
< |
final CountDownLatch counter = new CountDownLatch(2); |
797 |
< |
try { |
798 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
799 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
800 |
< |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
801 |
< |
final Runnable r = new CheckedRunnable() { |
802 |
< |
public void realRun() { |
803 |
< |
counter.countDown(); |
804 |
< |
}}; |
805 |
< |
ScheduledFuture task = |
750 |
< |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
751 |
< |
assertFalse(task.isDone()); |
752 |
< |
assertFalse(task.isCancelled()); |
753 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
754 |
< |
assertFalse(task.isCancelled()); |
755 |
< |
assertFalse(p.isTerminated()); |
756 |
< |
assertTrue(p.isShutdown()); |
757 |
< |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
758 |
< |
assertFalse(task.isCancelled()); |
759 |
< |
assertTrue(task.cancel(false)); |
760 |
< |
assertTrue(task.isDone()); |
761 |
< |
assertTrue(task.isCancelled()); |
762 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
763 |
< |
assertTrue(p.isTerminated()); |
784 |
> |
assertFalse(p.isTerminated()); |
785 |
> |
for (Future<?> periodic : periodics) { |
786 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
787 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
788 |
> |
} |
789 |
> |
for (Future<?> delayed : delayeds) { |
790 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
791 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
792 |
> |
} |
793 |
> |
if (testImplementationDetails) { |
794 |
> |
assertEquals(effectivePeriodicPolicy, |
795 |
> |
p.getQueue().containsAll(periodics)); |
796 |
> |
assertEquals(effectiveDelayedPolicy, |
797 |
> |
p.getQueue().containsAll(delayeds)); |
798 |
> |
} |
799 |
> |
// Release all pool threads |
800 |
> |
unblock.countDown(); |
801 |
> |
|
802 |
> |
for (Future<?> delayed : delayeds) { |
803 |
> |
if (effectiveDelayedPolicy) { |
804 |
> |
assertNull(delayed.get()); |
805 |
> |
} |
806 |
|
} |
807 |
< |
finally { |
808 |
< |
joinPool(p); |
807 |
> |
if (effectivePeriodicPolicy) { |
808 |
> |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
809 |
> |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
810 |
> |
for (Future<?> periodic : periodics) { |
811 |
> |
assertTrue(periodic.cancel(false)); |
812 |
> |
assertTrue(periodic.isCancelled()); |
813 |
> |
assertTrue(periodic.isDone()); |
814 |
> |
} |
815 |
|
} |
816 |
< |
} |
816 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
817 |
> |
assertTrue(p.isTerminated()); |
818 |
> |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
819 |
> |
}} |
820 |
|
|
821 |
|
/** |
822 |
|
* completed submit of callable returns result |
1207 |
|
l.add(new StringTask()); |
1208 |
|
l.add(new StringTask()); |
1209 |
|
List<Future<String>> futures = |
1210 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1210 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1211 |
|
assertEquals(2, futures.size()); |
1212 |
|
for (Future<String> future : futures) |
1213 |
|
assertSame(TEST_STRING, future.get()); |
1222 |
|
public void testTimedInvokeAll6() throws Exception { |
1223 |
|
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1224 |
|
try { |
1225 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1226 |
< |
l.add(new StringTask()); |
1227 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1228 |
< |
l.add(new StringTask()); |
1229 |
< |
List<Future<String>> futures = |
1230 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1231 |
< |
assertEquals(l.size(), futures.size()); |
1232 |
< |
for (Future future : futures) |
1233 |
< |
assertTrue(future.isDone()); |
1234 |
< |
assertFalse(futures.get(0).isCancelled()); |
1235 |
< |
assertTrue(futures.get(1).isCancelled()); |
1225 |
> |
for (long timeout = timeoutMillis();;) { |
1226 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1227 |
> |
tasks.add(new StringTask("0")); |
1228 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1229 |
> |
tasks.add(new StringTask("2")); |
1230 |
> |
long startTime = System.nanoTime(); |
1231 |
> |
List<Future<String>> futures = |
1232 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1233 |
> |
assertEquals(tasks.size(), futures.size()); |
1234 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1235 |
> |
for (Future future : futures) |
1236 |
> |
assertTrue(future.isDone()); |
1237 |
> |
assertTrue(futures.get(1).isCancelled()); |
1238 |
> |
try { |
1239 |
> |
assertEquals("0", futures.get(0).get()); |
1240 |
> |
assertEquals("2", futures.get(2).get()); |
1241 |
> |
break; |
1242 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1243 |
> |
timeout *= 2; |
1244 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1245 |
> |
fail("expected exactly one task to be cancelled"); |
1246 |
> |
} |
1247 |
> |
} |
1248 |
|
} finally { |
1249 |
|
joinPool(e); |
1250 |
|
} |