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 |
|
*/ |
96 |
|
public void testSchedule1() throws Exception { |
97 |
|
CustomExecutor p = new CustomExecutor(1); |
98 |
< |
final long t0 = System.nanoTime(); |
102 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
98 |
> |
final long startTime = System.nanoTime(); |
99 |
|
final CountDownLatch done = new CountDownLatch(1); |
100 |
|
try { |
101 |
|
Callable task = new CheckedCallable<Boolean>() { |
102 |
|
public Boolean realCall() { |
103 |
|
done.countDown(); |
104 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
104 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
105 |
|
return Boolean.TRUE; |
106 |
|
}}; |
107 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
108 |
< |
assertEquals(Boolean.TRUE, f.get()); |
109 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
107 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
108 |
> |
assertSame(Boolean.TRUE, f.get()); |
109 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
110 |
|
assertTrue(done.await(0L, MILLISECONDS)); |
111 |
|
} finally { |
112 |
|
joinPool(p); |
118 |
|
*/ |
119 |
|
public void testSchedule3() throws Exception { |
120 |
|
CustomExecutor p = new CustomExecutor(1); |
121 |
< |
final long t0 = System.nanoTime(); |
126 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
121 |
> |
final long startTime = System.nanoTime(); |
122 |
|
final CountDownLatch done = new CountDownLatch(1); |
123 |
|
try { |
124 |
|
Runnable task = new CheckedRunnable() { |
125 |
|
public void realRun() { |
126 |
|
done.countDown(); |
127 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
127 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
128 |
|
}}; |
129 |
< |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
130 |
< |
assertNull(f.get()); |
131 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
132 |
< |
assertTrue(done.await(0L, MILLISECONDS)); |
129 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
130 |
> |
await(done); |
131 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
132 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
133 |
|
} finally { |
134 |
|
joinPool(p); |
135 |
|
} |
140 |
|
*/ |
141 |
|
public void testSchedule4() throws InterruptedException { |
142 |
|
CustomExecutor p = new CustomExecutor(1); |
143 |
< |
final long t0 = System.nanoTime(); |
149 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
143 |
> |
final long startTime = System.nanoTime(); |
144 |
|
final CountDownLatch done = new CountDownLatch(1); |
145 |
|
try { |
146 |
|
Runnable task = new CheckedRunnable() { |
147 |
|
public void realRun() { |
148 |
|
done.countDown(); |
149 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
149 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
150 |
|
}}; |
151 |
|
ScheduledFuture f = |
152 |
< |
p.scheduleAtFixedRate(task, SHORT_DELAY_MS, |
153 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
154 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
155 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
152 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
153 |
> |
LONG_DELAY_MS, MILLISECONDS); |
154 |
> |
await(done); |
155 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
156 |
|
f.cancel(true); |
157 |
|
} finally { |
158 |
|
joinPool(p); |
164 |
|
*/ |
165 |
|
public void testSchedule5() throws InterruptedException { |
166 |
|
CustomExecutor p = new CustomExecutor(1); |
167 |
< |
final long t0 = System.nanoTime(); |
174 |
< |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
167 |
> |
final long startTime = System.nanoTime(); |
168 |
|
final CountDownLatch done = new CountDownLatch(1); |
169 |
|
try { |
170 |
|
Runnable task = new CheckedRunnable() { |
171 |
|
public void realRun() { |
172 |
|
done.countDown(); |
173 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
173 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
174 |
|
}}; |
175 |
|
ScheduledFuture f = |
176 |
< |
p.scheduleWithFixedDelay(task, SHORT_DELAY_MS, |
177 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
178 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
179 |
< |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
176 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
177 |
> |
LONG_DELAY_MS, MILLISECONDS); |
178 |
> |
await(done); |
179 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
180 |
|
f.cancel(true); |
181 |
|
} finally { |
182 |
|
joinPool(p); |
222 |
|
joinPool(p); |
223 |
|
} |
224 |
|
|
232 |
– |
|
225 |
|
/** |
226 |
|
* execute(null) throws NPE |
227 |
|
*/ |
374 |
|
assertEquals(0, p.getCompletedTaskCount()); |
375 |
|
threadProceed.countDown(); |
376 |
|
threadDone.await(); |
377 |
< |
delay(SHORT_DELAY_MS); |
378 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
377 |
> |
long startTime = System.nanoTime(); |
378 |
> |
while (p.getCompletedTaskCount() != 1) { |
379 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
380 |
> |
fail("timed out"); |
381 |
> |
Thread.yield(); |
382 |
> |
} |
383 |
|
} finally { |
384 |
|
joinPool(p); |
385 |
|
} |
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 |
|
*/ |
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 |
< |
delay(1); |
656 |
< |
} |
662 |
< |
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 |
|
} |
683 |
– |
assertTrue(p.isShutdown()); |
684 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
685 |
– |
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 |
|
|
717 |
– |
|
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 |
|
|
744 |
– |
|
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()); |
760 |
– |
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)); |
765 |
< |
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 { |