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.*; |
203 |
|
RunnableCounter counter = new RunnableCounter(); |
204 |
|
ScheduledFuture h = |
205 |
|
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
206 |
< |
Thread.sleep(SMALL_DELAY_MS); |
206 |
> |
delay(SMALL_DELAY_MS); |
207 |
|
h.cancel(true); |
208 |
|
int c = counter.count.get(); |
209 |
|
// By time scaling conventions, we must have at least |
221 |
|
RunnableCounter counter = new RunnableCounter(); |
222 |
|
ScheduledFuture h = |
223 |
|
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
224 |
< |
Thread.sleep(SMALL_DELAY_MS); |
224 |
> |
delay(SMALL_DELAY_MS); |
225 |
|
h.cancel(true); |
226 |
|
int c = counter.count.get(); |
227 |
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
291 |
|
/** |
292 |
|
* schedule callable throws RejectedExecutionException if shutdown |
293 |
|
*/ |
294 |
< |
public void testSchedule3_RejectedExecutionException() { |
295 |
< |
CustomExecutor se = new CustomExecutor(1); |
296 |
< |
try { |
297 |
< |
se.shutdown(); |
298 |
< |
se.schedule(new NoOpCallable(), |
299 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
300 |
< |
shouldThrow(); |
301 |
< |
} catch (RejectedExecutionException success) { |
302 |
< |
} catch (SecurityException ok) { |
303 |
< |
} |
304 |
< |
joinPool(se); |
294 |
> |
public void testSchedule3_RejectedExecutionException() { |
295 |
> |
CustomExecutor se = new CustomExecutor(1); |
296 |
> |
try { |
297 |
> |
se.shutdown(); |
298 |
> |
se.schedule(new NoOpCallable(), |
299 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
300 |
> |
shouldThrow(); |
301 |
> |
} catch (RejectedExecutionException success) { |
302 |
> |
} catch (SecurityException ok) { |
303 |
> |
} |
304 |
> |
joinPool(se); |
305 |
|
} |
306 |
|
|
307 |
|
/** |
382 |
|
assertEquals(0, p.getCompletedTaskCount()); |
383 |
|
threadProceed.countDown(); |
384 |
|
threadDone.await(); |
385 |
< |
Thread.sleep(SHORT_DELAY_MS); |
385 |
> |
delay(SHORT_DELAY_MS); |
386 |
|
assertEquals(1, p.getCompletedTaskCount()); |
387 |
|
} finally { |
388 |
|
joinPool(p); |
642 |
|
public void testPurge() throws InterruptedException { |
643 |
|
CustomExecutor p = new CustomExecutor(1); |
644 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
645 |
< |
for (int i = 0; i < tasks.length; i++) { |
646 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
647 |
< |
} |
645 |
> |
for (int i = 0; i < tasks.length; i++) |
646 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
647 |
> |
LONG_DELAY_MS, MILLISECONDS); |
648 |
|
try { |
649 |
|
int max = tasks.length; |
650 |
|
if (tasks[4].cancel(true)) --max; |
651 |
|
if (tasks[3].cancel(true)) --max; |
652 |
|
// There must eventually be an interference-free point at |
653 |
|
// which purge will not fail. (At worst, when queue is empty.) |
654 |
< |
int k; |
655 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
654 |
> |
long startTime = System.nanoTime(); |
655 |
> |
do { |
656 |
|
p.purge(); |
657 |
|
long count = p.getTaskCount(); |
658 |
< |
if (count >= 0 && count <= max) |
659 |
< |
break; |
660 |
< |
Thread.sleep(1); |
661 |
< |
} |
662 |
< |
assertTrue(k < SMALL_DELAY_MS); |
658 |
> |
if (count == max) |
659 |
> |
return; |
660 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
661 |
> |
fail("Purge failed to remove cancelled tasks"); |
662 |
|
} finally { |
663 |
|
for (ScheduledFuture task : tasks) |
664 |
|
task.cancel(true); |
672 |
|
public void testShutDownNow() { |
673 |
|
CustomExecutor p = new CustomExecutor(1); |
674 |
|
for (int i = 0; i < 5; i++) |
675 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
676 |
< |
List l; |
675 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
676 |
> |
LONG_DELAY_MS, MILLISECONDS); |
677 |
|
try { |
678 |
< |
l = p.shutdownNow(); |
678 |
> |
List<Runnable> l = p.shutdownNow(); |
679 |
> |
assertTrue(p.isShutdown()); |
680 |
> |
assertEquals(5, l.size()); |
681 |
|
} catch (SecurityException ok) { |
682 |
< |
return; |
682 |
> |
// Allowed in case test doesn't have privs |
683 |
> |
} finally { |
684 |
> |
joinPool(p); |
685 |
|
} |
683 |
– |
assertTrue(p.isShutdown()); |
684 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
685 |
– |
joinPool(p); |
686 |
|
} |
687 |
|
|
688 |
|
/** |