1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
– |
|
51 |
|
public class CustomExecutor extends ScheduledThreadPoolExecutor { |
52 |
|
|
53 |
|
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) { |
72 |
|
|
73 |
|
} |
74 |
|
|
76 |
– |
|
75 |
|
/** |
76 |
|
* execute successfully executes a runnable |
77 |
|
*/ |
90 |
|
} |
91 |
|
} |
92 |
|
|
95 |
– |
|
93 |
|
/** |
94 |
|
* delayed schedule of callable successfully executes after delay |
95 |
|
*/ |
200 |
|
RunnableCounter counter = new RunnableCounter(); |
201 |
|
ScheduledFuture h = |
202 |
|
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
203 |
< |
Thread.sleep(SMALL_DELAY_MS); |
203 |
> |
delay(SMALL_DELAY_MS); |
204 |
|
h.cancel(true); |
205 |
|
int c = counter.count.get(); |
206 |
|
// By time scaling conventions, we must have at least |
218 |
|
RunnableCounter counter = new RunnableCounter(); |
219 |
|
ScheduledFuture h = |
220 |
|
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
221 |
< |
Thread.sleep(SMALL_DELAY_MS); |
221 |
> |
delay(SMALL_DELAY_MS); |
222 |
|
h.cancel(true); |
223 |
|
int c = counter.count.get(); |
224 |
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
226 |
|
joinPool(p); |
227 |
|
} |
228 |
|
|
232 |
– |
|
229 |
|
/** |
230 |
|
* execute(null) throws NPE |
231 |
|
*/ |
287 |
|
/** |
288 |
|
* schedule callable throws RejectedExecutionException if shutdown |
289 |
|
*/ |
290 |
< |
public void testSchedule3_RejectedExecutionException() { |
291 |
< |
CustomExecutor se = new CustomExecutor(1); |
292 |
< |
try { |
293 |
< |
se.shutdown(); |
294 |
< |
se.schedule(new NoOpCallable(), |
295 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
296 |
< |
shouldThrow(); |
297 |
< |
} catch (RejectedExecutionException success) { |
298 |
< |
} catch (SecurityException ok) { |
299 |
< |
} |
300 |
< |
joinPool(se); |
290 |
> |
public void testSchedule3_RejectedExecutionException() { |
291 |
> |
CustomExecutor se = new CustomExecutor(1); |
292 |
> |
try { |
293 |
> |
se.shutdown(); |
294 |
> |
se.schedule(new NoOpCallable(), |
295 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
296 |
> |
shouldThrow(); |
297 |
> |
} catch (RejectedExecutionException success) { |
298 |
> |
} catch (SecurityException ok) { |
299 |
> |
} |
300 |
> |
joinPool(se); |
301 |
|
} |
302 |
|
|
303 |
|
/** |
378 |
|
assertEquals(0, p.getCompletedTaskCount()); |
379 |
|
threadProceed.countDown(); |
380 |
|
threadDone.await(); |
381 |
< |
Thread.sleep(SHORT_DELAY_MS); |
381 |
> |
delay(SHORT_DELAY_MS); |
382 |
|
assertEquals(1, p.getCompletedTaskCount()); |
383 |
|
} finally { |
384 |
|
joinPool(p); |
506 |
|
} |
507 |
|
|
508 |
|
/** |
509 |
< |
* isShutDown is false before shutdown, true after |
509 |
> |
* isShutdown is false before shutdown, true after |
510 |
|
*/ |
511 |
|
public void testIsShutdown() { |
512 |
|
CustomExecutor p = new CustomExecutor(1); |
519 |
|
assertTrue(p.isShutdown()); |
520 |
|
} |
521 |
|
|
526 |
– |
|
522 |
|
/** |
523 |
|
* isTerminated is false before termination, true after |
524 |
|
*/ |
530 |
|
try { |
531 |
|
p.execute(new CheckedRunnable() { |
532 |
|
public void realRun() throws InterruptedException { |
538 |
– |
threadStarted.countDown(); |
533 |
|
assertFalse(p.isTerminated()); |
534 |
+ |
threadStarted.countDown(); |
535 |
|
done.await(); |
536 |
|
}}); |
537 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
538 |
+ |
assertFalse(p.isTerminating()); |
539 |
|
done.countDown(); |
540 |
|
} finally { |
541 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
555 |
|
assertFalse(p.isTerminating()); |
556 |
|
p.execute(new CheckedRunnable() { |
557 |
|
public void realRun() throws InterruptedException { |
562 |
– |
threadStarted.countDown(); |
558 |
|
assertFalse(p.isTerminating()); |
559 |
+ |
threadStarted.countDown(); |
560 |
|
done.await(); |
561 |
|
}}); |
562 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
637 |
|
public void testPurge() throws InterruptedException { |
638 |
|
CustomExecutor p = new CustomExecutor(1); |
639 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
640 |
< |
for (int i = 0; i < tasks.length; i++) { |
641 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
642 |
< |
} |
640 |
> |
for (int i = 0; i < tasks.length; i++) |
641 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
642 |
> |
LONG_DELAY_MS, MILLISECONDS); |
643 |
|
try { |
644 |
|
int max = tasks.length; |
645 |
|
if (tasks[4].cancel(true)) --max; |
646 |
|
if (tasks[3].cancel(true)) --max; |
647 |
|
// There must eventually be an interference-free point at |
648 |
|
// which purge will not fail. (At worst, when queue is empty.) |
649 |
< |
int k; |
650 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
649 |
> |
long startTime = System.nanoTime(); |
650 |
> |
do { |
651 |
|
p.purge(); |
652 |
|
long count = p.getTaskCount(); |
653 |
< |
if (count >= 0 && count <= max) |
654 |
< |
break; |
655 |
< |
Thread.sleep(1); |
656 |
< |
} |
661 |
< |
assertTrue(k < SMALL_DELAY_MS); |
653 |
> |
if (count == max) |
654 |
> |
return; |
655 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
656 |
> |
fail("Purge failed to remove cancelled tasks"); |
657 |
|
} finally { |
658 |
|
for (ScheduledFuture task : tasks) |
659 |
|
task.cancel(true); |
662 |
|
} |
663 |
|
|
664 |
|
/** |
665 |
< |
* shutDownNow returns a list containing tasks that were not run |
665 |
> |
* shutdownNow returns a list containing tasks that were not run |
666 |
|
*/ |
667 |
< |
public void testShutDownNow() { |
667 |
> |
public void testShutdownNow() { |
668 |
|
CustomExecutor p = new CustomExecutor(1); |
669 |
|
for (int i = 0; i < 5; i++) |
670 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
671 |
< |
List l; |
670 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
671 |
> |
LONG_DELAY_MS, MILLISECONDS); |
672 |
|
try { |
673 |
< |
l = p.shutdownNow(); |
673 |
> |
List<Runnable> l = p.shutdownNow(); |
674 |
> |
assertTrue(p.isShutdown()); |
675 |
> |
assertEquals(5, l.size()); |
676 |
|
} catch (SecurityException ok) { |
677 |
< |
return; |
677 |
> |
// Allowed in case test doesn't have privs |
678 |
> |
} finally { |
679 |
> |
joinPool(p); |
680 |
|
} |
682 |
– |
assertTrue(p.isShutdown()); |
683 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
684 |
– |
joinPool(p); |
681 |
|
} |
682 |
|
|
683 |
|
/** |
684 |
|
* In default setting, shutdown cancels periodic but not delayed |
685 |
|
* tasks at shutdown |
686 |
|
*/ |
687 |
< |
public void testShutDown1() throws InterruptedException { |
687 |
> |
public void testShutdown1() throws InterruptedException { |
688 |
|
CustomExecutor p = new CustomExecutor(1); |
689 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
690 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
709 |
|
} |
710 |
|
} |
711 |
|
|
716 |
– |
|
712 |
|
/** |
713 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
714 |
|
* delayed tasks are cancelled at shutdown |
715 |
|
*/ |
716 |
< |
public void testShutDown2() throws InterruptedException { |
716 |
> |
public void testShutdown2() throws InterruptedException { |
717 |
|
CustomExecutor p = new CustomExecutor(1); |
718 |
|
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
719 |
|
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
735 |
|
} |
736 |
|
} |
737 |
|
|
743 |
– |
|
738 |
|
/** |
739 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
740 |
|
* periodic tasks are cancelled at shutdown |
741 |
|
*/ |
742 |
< |
public void testShutDown3() throws InterruptedException { |
742 |
> |
public void testShutdown3() throws InterruptedException { |
743 |
|
CustomExecutor p = new CustomExecutor(1); |
744 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
745 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
746 |
|
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
747 |
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
748 |
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
749 |
+ |
long initialDelay = LONG_DELAY_MS; |
750 |
|
ScheduledFuture task = |
751 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
751 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
752 |
> |
5, MILLISECONDS); |
753 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
754 |
|
assertTrue(p.isShutdown()); |
759 |
– |
BlockingQueue q = p.getQueue(); |
755 |
|
assertTrue(p.getQueue().isEmpty()); |
756 |
|
assertTrue(task.isDone()); |
757 |
|
assertTrue(task.isCancelled()); |
758 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
764 |
< |
assertTrue(p.isTerminated()); |
758 |
> |
joinPool(p); |
759 |
|
} |
760 |
|
|
761 |
|
/** |
762 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
763 |
|
* periodic tasks are not cancelled at shutdown |
764 |
|
*/ |
765 |
< |
public void testShutDown4() throws InterruptedException { |
765 |
> |
public void testShutdown4() throws InterruptedException { |
766 |
|
CustomExecutor p = new CustomExecutor(1); |
767 |
|
final CountDownLatch counter = new CountDownLatch(2); |
768 |
|
try { |