9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
+ |
import java.util.concurrent.atomic.*; |
13 |
|
|
14 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
15 |
|
public static void main(String[] args) { |
30 |
|
p1.execute(runnable); |
31 |
|
assertFalse(runnable.done); |
32 |
|
Thread.sleep(SHORT_DELAY_MS); |
33 |
< |
p1.shutdown(); |
33 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
34 |
|
try { |
35 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
36 |
|
} catch(InterruptedException e){ |
37 |
|
unexpectedException(); |
38 |
|
} |
39 |
|
assertTrue(runnable.done); |
40 |
< |
p1.shutdown(); |
40 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
41 |
|
joinPool(p1); |
42 |
|
} |
43 |
|
catch(Exception e){ |
59 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
60 |
|
assertTrue(callable.done); |
61 |
|
assertEquals(Boolean.TRUE, f.get()); |
62 |
< |
p1.shutdown(); |
62 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
63 |
|
joinPool(p1); |
64 |
|
} catch(RejectedExecutionException e){} |
65 |
|
catch(Exception e){ |
80 |
|
assertFalse(runnable.done); |
81 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
82 |
|
assertTrue(runnable.done); |
83 |
< |
p1.shutdown(); |
83 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
84 |
|
joinPool(p1); |
85 |
|
} catch(Exception e){ |
86 |
|
unexpectedException(); |
99 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
100 |
|
assertTrue(runnable.done); |
101 |
|
h.cancel(true); |
101 |
– |
p1.shutdown(); |
102 |
|
joinPool(p1); |
103 |
|
} catch(Exception e){ |
104 |
|
unexpectedException(); |
105 |
|
} |
106 |
|
} |
107 |
|
|
108 |
+ |
static class RunnableCounter implements Runnable { |
109 |
+ |
AtomicInteger count = new AtomicInteger(0); |
110 |
+ |
public void run() { count.getAndIncrement(); } |
111 |
+ |
} |
112 |
+ |
|
113 |
|
/** |
114 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
115 |
|
*/ |
122 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
123 |
|
assertTrue(runnable.done); |
124 |
|
h.cancel(true); |
120 |
– |
p1.shutdown(); |
125 |
|
joinPool(p1); |
126 |
|
} catch(Exception e){ |
127 |
|
unexpectedException(); |
129 |
|
} |
130 |
|
|
131 |
|
/** |
132 |
+ |
* scheduleAtFixedRate executes series of tasks at given rate |
133 |
+ |
*/ |
134 |
+ |
public void testFixedRateSequence() { |
135 |
+ |
try { |
136 |
+ |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
137 |
+ |
RunnableCounter counter = new RunnableCounter(); |
138 |
+ |
ScheduledFuture h = |
139 |
+ |
p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS); |
140 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
141 |
+ |
h.cancel(true); |
142 |
+ |
int c = counter.count.get(); |
143 |
+ |
// By time scaling conventions, we must have at least |
144 |
+ |
// an execution per SHORT delay, but no more than one SHORT more |
145 |
+ |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
146 |
+ |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
147 |
+ |
assertTrue(h.isDone()); |
148 |
+ |
joinPool(p1); |
149 |
+ |
} catch(Exception e){ |
150 |
+ |
unexpectedException(); |
151 |
+ |
} |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
/** |
155 |
+ |
* scheduleWithFixedDelay executes series of tasks with given period |
156 |
+ |
*/ |
157 |
+ |
public void testFixedDelaySequence() { |
158 |
+ |
try { |
159 |
+ |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
160 |
+ |
RunnableCounter counter = new RunnableCounter(); |
161 |
+ |
ScheduledFuture h = |
162 |
+ |
p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS); |
163 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
164 |
+ |
h.cancel(true); |
165 |
+ |
int c = counter.count.get(); |
166 |
+ |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
167 |
+ |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
168 |
+ |
assertTrue(h.isDone()); |
169 |
+ |
joinPool(p1); |
170 |
+ |
} catch(Exception e){ |
171 |
+ |
unexpectedException(); |
172 |
+ |
} |
173 |
+ |
} |
174 |
+ |
|
175 |
+ |
|
176 |
+ |
/** |
177 |
|
* execute (null) throws NPE |
178 |
|
*/ |
179 |
|
public void testExecuteNull() { |
217 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
218 |
|
shouldThrow(); |
219 |
|
} catch(RejectedExecutionException success){ |
220 |
+ |
} catch (SecurityException ok) { |
221 |
|
} |
222 |
+ |
|
223 |
|
joinPool(se); |
224 |
|
|
225 |
|
} |
235 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
236 |
|
shouldThrow(); |
237 |
|
} catch(RejectedExecutionException success){ |
238 |
+ |
} catch (SecurityException ok) { |
239 |
|
} |
240 |
|
joinPool(se); |
241 |
|
} |
251 |
|
MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
252 |
|
shouldThrow(); |
253 |
|
} catch(RejectedExecutionException success){ |
254 |
< |
} |
254 |
> |
} catch (SecurityException ok) { |
255 |
> |
} |
256 |
|
joinPool(se); |
257 |
|
} |
258 |
|
|
267 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
268 |
|
shouldThrow(); |
269 |
|
} catch(RejectedExecutionException success){ |
270 |
+ |
} catch (SecurityException ok) { |
271 |
|
} |
272 |
|
joinPool(se); |
273 |
|
} |
283 |
|
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
284 |
|
shouldThrow(); |
285 |
|
} catch(RejectedExecutionException success){ |
286 |
+ |
} catch (SecurityException ok) { |
287 |
|
} |
288 |
|
joinPool(se); |
289 |
|
} |
386 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
387 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
388 |
|
assertSame(tf, p.getThreadFactory()); |
334 |
– |
p.shutdown(); |
389 |
|
joinPool(p); |
390 |
|
} |
391 |
|
|
397 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
398 |
|
p.setThreadFactory(tf); |
399 |
|
assertSame(tf, p.getThreadFactory()); |
346 |
– |
p.shutdown(); |
400 |
|
joinPool(p); |
401 |
|
} |
402 |
|
|
424 |
|
assertFalse(p1.isShutdown()); |
425 |
|
} |
426 |
|
finally { |
427 |
< |
p1.shutdown(); |
427 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
428 |
|
} |
429 |
|
assertTrue(p1.isShutdown()); |
430 |
|
} |
438 |
|
try { |
439 |
|
p1.execute(new SmallRunnable()); |
440 |
|
} finally { |
441 |
< |
p1.shutdown(); |
441 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
442 |
|
} |
443 |
|
try { |
444 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
458 |
|
p1.execute(new SmallRunnable()); |
459 |
|
assertFalse(p1.isTerminating()); |
460 |
|
} finally { |
461 |
< |
p1.shutdown(); |
461 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
462 |
|
} |
463 |
|
try { |
464 |
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
483 |
|
BlockingQueue<Runnable> q = p1.getQueue(); |
484 |
|
assertTrue(q.contains(tasks[4])); |
485 |
|
assertFalse(q.contains(tasks[0])); |
433 |
– |
p1.shutdownNow(); |
486 |
|
} catch(Exception e) { |
487 |
|
unexpectedException(); |
488 |
|
} finally { |
511 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
512 |
|
assertTrue(p1.remove((Runnable)tasks[3])); |
513 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
462 |
– |
p1.shutdownNow(); |
514 |
|
} catch(Exception e) { |
515 |
|
unexpectedException(); |
516 |
|
} finally { |
543 |
|
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
544 |
|
for(int i = 0; i < 5; i++) |
545 |
|
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
546 |
< |
List l = p1.shutdownNow(); |
546 |
> |
List l; |
547 |
> |
try { |
548 |
> |
l = p1.shutdownNow(); |
549 |
> |
} catch (SecurityException ok) { |
550 |
> |
return; |
551 |
> |
} |
552 |
|
assertTrue(p1.isShutdown()); |
553 |
|
assertTrue(l.size() > 0 && l.size() <= 5); |
554 |
|
joinPool(p1); |
567 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
568 |
|
for(int i = 0; i < 5; i++) |
569 |
|
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
570 |
< |
p1.shutdown(); |
570 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
571 |
|
BlockingQueue q = p1.getQueue(); |
572 |
|
for (Iterator it = q.iterator(); it.hasNext();) { |
573 |
|
ScheduledFuture t = (ScheduledFuture)it.next(); |
598 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
599 |
|
for(int i = 0; i < 5; i++) |
600 |
|
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
601 |
< |
p1.shutdown(); |
601 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
602 |
|
assertTrue(p1.isShutdown()); |
603 |
|
BlockingQueue q = p1.getQueue(); |
604 |
|
assertTrue(q.isEmpty()); |
621 |
|
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
622 |
|
ScheduledFuture task = |
623 |
|
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS); |
624 |
< |
p1.shutdown(); |
624 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
625 |
|
assertTrue(p1.isShutdown()); |
626 |
|
BlockingQueue q = p1.getQueue(); |
627 |
|
assertTrue(q.isEmpty()); |
644 |
|
ScheduledFuture task = |
645 |
|
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS); |
646 |
|
assertFalse(task.isCancelled()); |
647 |
< |
p1.shutdown(); |
647 |
> |
try { p1.shutdown(); } catch(SecurityException ok) { return; } |
648 |
|
assertFalse(task.isCancelled()); |
649 |
|
assertFalse(p1.isTerminated()); |
650 |
|
assertTrue(p1.isShutdown()); |
658 |
|
catch(Exception ex) { |
659 |
|
unexpectedException(); |
660 |
|
} |
661 |
< |
finally { |
662 |
< |
p1.shutdownNow(); |
661 |
> |
finally { |
662 |
> |
joinPool(p1); |
663 |
|
} |
664 |
|
} |
665 |
|
|
723 |
|
} |
724 |
|
} |
725 |
|
|
670 |
– |
|
671 |
– |
|
672 |
– |
|
673 |
– |
|
726 |
|
/** |
727 |
|
* invokeAny(null) throws NPE |
728 |
|
*/ |