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); |
535 |
|
try { |
536 |
|
p.execute(new CheckedRunnable() { |
537 |
|
public void realRun() throws InterruptedException { |
538 |
– |
threadStarted.countDown(); |
538 |
|
assertFalse(p.isTerminated()); |
539 |
+ |
threadStarted.countDown(); |
540 |
|
done.await(); |
541 |
|
}}); |
542 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
543 |
+ |
assertFalse(p.isTerminating()); |
544 |
|
done.countDown(); |
545 |
|
} finally { |
546 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
560 |
|
assertFalse(p.isTerminating()); |
561 |
|
p.execute(new CheckedRunnable() { |
562 |
|
public void realRun() throws InterruptedException { |
562 |
– |
threadStarted.countDown(); |
563 |
|
assertFalse(p.isTerminating()); |
564 |
+ |
threadStarted.countDown(); |
565 |
|
done.await(); |
566 |
|
}}); |
567 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
657 |
|
long count = p.getTaskCount(); |
658 |
|
if (count >= 0 && count <= max) |
659 |
|
break; |
660 |
< |
Thread.sleep(1); |
660 |
> |
delay(1); |
661 |
|
} |
662 |
|
assertTrue(k < SMALL_DELAY_MS); |
663 |
|
} finally { |
722 |
|
public void testShutDown2() throws InterruptedException { |
723 |
|
CustomExecutor p = new CustomExecutor(1); |
724 |
|
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
725 |
+ |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
726 |
+ |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
727 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
728 |
|
for (int i = 0; i < tasks.length; i++) |
729 |
|
tasks[i] = p.schedule(new NoOpRunnable(), |
748 |
|
*/ |
749 |
|
public void testShutDown3() throws InterruptedException { |
750 |
|
CustomExecutor p = new CustomExecutor(1); |
751 |
+ |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
752 |
+ |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
753 |
|
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
754 |
+ |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
755 |
+ |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
756 |
|
ScheduledFuture task = |
757 |
|
p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
758 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
771 |
|
*/ |
772 |
|
public void testShutDown4() throws InterruptedException { |
773 |
|
CustomExecutor p = new CustomExecutor(1); |
767 |
– |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
774 |
|
final CountDownLatch counter = new CountDownLatch(2); |
775 |
|
try { |
776 |
+ |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
777 |
+ |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
778 |
+ |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
779 |
|
final Runnable r = new CheckedRunnable() { |
780 |
|
public void realRun() { |
781 |
|
counter.countDown(); |