ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.30
Committed: Wed Aug 25 00:07:03 2010 UTC (13 years, 8 months ago) by jsr166
Branch: MAIN
Changes since 1.29: +3 -3 lines
Log Message:
whitespace

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.13 * 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
5 jsr166 1.22 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import junit.framework.*;
10     import java.util.*;
11     import java.util.concurrent.*;
12 jsr166 1.25 import static java.util.concurrent.TimeUnit.MILLISECONDS;
13 dl 1.18 import java.util.concurrent.atomic.*;
14 dl 1.1
15 dl 1.4 public class ScheduledExecutorTest extends JSR166TestCase {
16 dl 1.1 public static void main(String[] args) {
17 jsr166 1.30 junit.textui.TestRunner.run(suite());
18 dl 1.1 }
19     public static Test suite() {
20 jsr166 1.26 return new TestSuite(ScheduledExecutorTest.class);
21 dl 1.1 }
22    
23    
24 dl 1.5 /**
25 dl 1.6 * execute successfully executes a runnable
26 dl 1.5 */
27 jsr166 1.25 public void testExecute() throws InterruptedException {
28     TrackedShortRunnable runnable =new TrackedShortRunnable();
29     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
30     p1.execute(runnable);
31     assertFalse(runnable.done);
32     Thread.sleep(SHORT_DELAY_MS);
33     try { p1.shutdown(); } catch (SecurityException ok) { return; }
34     Thread.sleep(MEDIUM_DELAY_MS);
35     assertTrue(runnable.done);
36     try { p1.shutdown(); } catch (SecurityException ok) { return; }
37     joinPool(p1);
38 dl 1.1 }
39    
40 dl 1.6
41 dl 1.5 /**
42 dl 1.6 * delayed schedule of callable successfully executes after delay
43 dl 1.5 */
44 jsr166 1.25 public void testSchedule1() throws Exception {
45     TrackedCallable callable = new TrackedCallable();
46     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
47     Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
48     assertFalse(callable.done);
49     Thread.sleep(MEDIUM_DELAY_MS);
50     assertTrue(callable.done);
51     assertEquals(Boolean.TRUE, f.get());
52     try { p1.shutdown(); } catch (SecurityException ok) { return; }
53     joinPool(p1);
54 dl 1.1 }
55    
56     /**
57 dl 1.6 * delayed schedule of runnable successfully executes after delay
58 dl 1.1 */
59 jsr166 1.25 public void testSchedule3() throws InterruptedException {
60     TrackedShortRunnable runnable = new TrackedShortRunnable();
61     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
62     p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS);
63     Thread.sleep(SHORT_DELAY_MS);
64     assertFalse(runnable.done);
65     Thread.sleep(MEDIUM_DELAY_MS);
66     assertTrue(runnable.done);
67     try { p1.shutdown(); } catch (SecurityException ok) { return; }
68     joinPool(p1);
69 dl 1.1 }
70 jsr166 1.22
71 dl 1.1 /**
72 dl 1.6 * scheduleAtFixedRate executes runnable after given initial delay
73 dl 1.1 */
74 jsr166 1.25 public void testSchedule4() throws InterruptedException {
75     TrackedShortRunnable runnable = new TrackedShortRunnable();
76     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
77     ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
78     assertFalse(runnable.done);
79     Thread.sleep(MEDIUM_DELAY_MS);
80     assertTrue(runnable.done);
81     h.cancel(true);
82     joinPool(p1);
83 dl 1.6 }
84    
85 dl 1.18 static class RunnableCounter implements Runnable {
86     AtomicInteger count = new AtomicInteger(0);
87     public void run() { count.getAndIncrement(); }
88     }
89    
90 dl 1.6 /**
91     * scheduleWithFixedDelay executes runnable after given initial delay
92     */
93 jsr166 1.25 public void testSchedule5() throws InterruptedException {
94     TrackedShortRunnable runnable = new TrackedShortRunnable();
95     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
96     ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS);
97     assertFalse(runnable.done);
98     Thread.sleep(MEDIUM_DELAY_MS);
99     assertTrue(runnable.done);
100     h.cancel(true);
101     joinPool(p1);
102 dl 1.1 }
103 jsr166 1.22
104 dl 1.6 /**
105 dl 1.18 * scheduleAtFixedRate executes series of tasks at given rate
106     */
107 jsr166 1.25 public void testFixedRateSequence() throws InterruptedException {
108     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
109     RunnableCounter counter = new RunnableCounter();
110     ScheduledFuture h =
111     p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS);
112     Thread.sleep(SMALL_DELAY_MS);
113     h.cancel(true);
114     int c = counter.count.get();
115     // By time scaling conventions, we must have at least
116     // an execution per SHORT delay, but no more than one SHORT more
117     assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
118     assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
119     joinPool(p1);
120 dl 1.18 }
121    
122     /**
123     * scheduleWithFixedDelay executes series of tasks with given period
124     */
125 jsr166 1.25 public void testFixedDelaySequence() throws InterruptedException {
126     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
127     RunnableCounter counter = new RunnableCounter();
128     ScheduledFuture h =
129     p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS);
130     Thread.sleep(SMALL_DELAY_MS);
131     h.cancel(true);
132     int c = counter.count.get();
133     assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
134     assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
135     joinPool(p1);
136 dl 1.18 }
137    
138    
139     /**
140 jsr166 1.30 * execute(null) throws NPE
141 dl 1.6 */
142 jsr166 1.25 public void testExecuteNull() throws InterruptedException {
143 dl 1.9 ScheduledThreadPoolExecutor se = null;
144 dl 1.6 try {
145 jsr166 1.26 se = new ScheduledThreadPoolExecutor(1);
146     se.execute(null);
147 dl 1.6 shouldThrow();
148 jsr166 1.26 } catch (NullPointerException success) {}
149 jsr166 1.22
150 jsr166 1.26 joinPool(se);
151 dl 1.6 }
152    
153     /**
154 jsr166 1.30 * schedule(null) throws NPE
155 dl 1.6 */
156 jsr166 1.25 public void testScheduleNull() throws InterruptedException {
157 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
158 jsr166 1.26 try {
159 dl 1.7 TrackedCallable callable = null;
160 jsr166 1.26 Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
161 dl 1.6 shouldThrow();
162 jsr166 1.26 } catch (NullPointerException success) {}
163     joinPool(se);
164 dl 1.6 }
165 jsr166 1.22
166 dl 1.1 /**
167 dl 1.6 * execute throws RejectedExecutionException if shutdown
168 dl 1.1 */
169 jsr166 1.25 public void testSchedule1_RejectedExecutionException() throws InterruptedException {
170 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
171 dl 1.5 try {
172 dl 1.1 se.shutdown();
173 dl 1.4 se.schedule(new NoOpRunnable(),
174 jsr166 1.25 MEDIUM_DELAY_MS, MILLISECONDS);
175 dl 1.5 shouldThrow();
176 jsr166 1.24 } catch (RejectedExecutionException success) {
177 dl 1.17 } catch (SecurityException ok) {
178 dl 1.4 }
179 jsr166 1.22
180 dl 1.4 joinPool(se);
181 dl 1.1 }
182    
183     /**
184 dl 1.6 * schedule throws RejectedExecutionException if shutdown
185 dl 1.1 */
186 jsr166 1.25 public void testSchedule2_RejectedExecutionException() throws InterruptedException {
187 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
188 dl 1.5 try {
189 dl 1.1 se.shutdown();
190 dl 1.4 se.schedule(new NoOpCallable(),
191 jsr166 1.25 MEDIUM_DELAY_MS, MILLISECONDS);
192 dl 1.5 shouldThrow();
193 jsr166 1.24 } catch (RejectedExecutionException success) {
194 dl 1.17 } catch (SecurityException ok) {
195 dl 1.4 }
196     joinPool(se);
197 dl 1.1 }
198    
199     /**
200 dl 1.6 * schedule callable throws RejectedExecutionException if shutdown
201 dl 1.1 */
202 jsr166 1.25 public void testSchedule3_RejectedExecutionException() throws InterruptedException {
203 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
204 dl 1.5 try {
205 dl 1.1 se.shutdown();
206 dl 1.4 se.schedule(new NoOpCallable(),
207 jsr166 1.25 MEDIUM_DELAY_MS, MILLISECONDS);
208 dl 1.5 shouldThrow();
209 jsr166 1.24 } catch (RejectedExecutionException success) {
210 dl 1.17 } catch (SecurityException ok) {
211     }
212 dl 1.4 joinPool(se);
213 dl 1.1 }
214    
215     /**
216 dl 1.6 * scheduleAtFixedRate throws RejectedExecutionException if shutdown
217 dl 1.1 */
218 jsr166 1.25 public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException {
219 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
220 dl 1.5 try {
221 dl 1.1 se.shutdown();
222 dl 1.4 se.scheduleAtFixedRate(new NoOpRunnable(),
223 jsr166 1.25 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
224 dl 1.5 shouldThrow();
225 jsr166 1.24 } catch (RejectedExecutionException success) {
226 dl 1.17 } catch (SecurityException ok) {
227 jsr166 1.22 }
228 dl 1.4 joinPool(se);
229 dl 1.1 }
230 jsr166 1.22
231 dl 1.1 /**
232 dl 1.6 * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
233 dl 1.1 */
234 jsr166 1.25 public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException {
235 dl 1.9 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
236 dl 1.5 try {
237 dl 1.1 se.shutdown();
238 dl 1.4 se.scheduleWithFixedDelay(new NoOpRunnable(),
239 jsr166 1.25 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
240 dl 1.5 shouldThrow();
241 jsr166 1.24 } catch (RejectedExecutionException success) {
242 dl 1.17 } catch (SecurityException ok) {
243 jsr166 1.22 }
244 dl 1.4 joinPool(se);
245 dl 1.1 }
246    
247     /**
248 dl 1.6 * getActiveCount increases but doesn't overestimate, when a
249     * thread becomes active
250 dl 1.2 */
251 jsr166 1.25 public void testGetActiveCount() throws InterruptedException {
252 dl 1.9 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
253 dl 1.5 assertEquals(0, p2.getActiveCount());
254     p2.execute(new SmallRunnable());
255 jsr166 1.25 Thread.sleep(SHORT_DELAY_MS);
256 dl 1.5 assertEquals(1, p2.getActiveCount());
257     joinPool(p2);
258 dl 1.2 }
259 jsr166 1.22
260 dl 1.2 /**
261 dl 1.6 * getCompletedTaskCount increases, but doesn't overestimate,
262     * when tasks complete
263 dl 1.2 */
264 jsr166 1.25 public void testGetCompletedTaskCount() throws InterruptedException {
265 dl 1.9 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
266 dl 1.5 assertEquals(0, p2.getCompletedTaskCount());
267     p2.execute(new SmallRunnable());
268 jsr166 1.25 Thread.sleep(MEDIUM_DELAY_MS);
269 dl 1.5 assertEquals(1, p2.getCompletedTaskCount());
270     joinPool(p2);
271 dl 1.2 }
272 jsr166 1.22
273 dl 1.2 /**
274 jsr166 1.22 * getCorePoolSize returns size given in constructor if not otherwise set
275 dl 1.2 */
276 jsr166 1.25 public void testGetCorePoolSize() throws InterruptedException {
277 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
278 dl 1.5 assertEquals(1, p1.getCorePoolSize());
279     joinPool(p1);
280 dl 1.2 }
281 jsr166 1.22
282 dl 1.2 /**
283 dl 1.6 * getLargestPoolSize increases, but doesn't overestimate, when
284     * multiple threads active
285 dl 1.2 */
286 jsr166 1.25 public void testGetLargestPoolSize() throws InterruptedException {
287 dl 1.9 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
288 dl 1.5 assertEquals(0, p2.getLargestPoolSize());
289     p2.execute(new SmallRunnable());
290     p2.execute(new SmallRunnable());
291 jsr166 1.25 Thread.sleep(SHORT_DELAY_MS);
292 dl 1.5 assertEquals(2, p2.getLargestPoolSize());
293     joinPool(p2);
294 dl 1.2 }
295 jsr166 1.22
296 dl 1.2 /**
297 dl 1.6 * getPoolSize increases, but doesn't overestimate, when threads
298     * become active
299 dl 1.2 */
300 jsr166 1.25 public void testGetPoolSize() throws InterruptedException {
301 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
302 dl 1.5 assertEquals(0, p1.getPoolSize());
303     p1.execute(new SmallRunnable());
304     assertEquals(1, p1.getPoolSize());
305     joinPool(p1);
306 dl 1.2 }
307 jsr166 1.22
308 dl 1.2 /**
309 dl 1.6 * getTaskCount increases, but doesn't overestimate, when tasks
310     * submitted
311 dl 1.2 */
312 jsr166 1.25 public void testGetTaskCount() throws InterruptedException {
313 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
314 dl 1.5 assertEquals(0, p1.getTaskCount());
315 jsr166 1.23 for (int i = 0; i < 5; i++)
316 dl 1.5 p1.execute(new SmallRunnable());
317 jsr166 1.25 Thread.sleep(SHORT_DELAY_MS);
318 dl 1.5 assertEquals(5, p1.getTaskCount());
319     joinPool(p1);
320 dl 1.2 }
321 dl 1.8
322 jsr166 1.22 /**
323 dl 1.8 * getThreadFactory returns factory in constructor if not set
324     */
325 jsr166 1.25 public void testGetThreadFactory() throws InterruptedException {
326 dl 1.8 ThreadFactory tf = new SimpleThreadFactory();
327 jsr166 1.26 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
328 dl 1.8 assertSame(tf, p.getThreadFactory());
329     joinPool(p);
330     }
331    
332 jsr166 1.22 /**
333 dl 1.8 * setThreadFactory sets the thread factory returned by getThreadFactory
334     */
335 jsr166 1.25 public void testSetThreadFactory() throws InterruptedException {
336 dl 1.8 ThreadFactory tf = new SimpleThreadFactory();
337 jsr166 1.26 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
338 dl 1.8 p.setThreadFactory(tf);
339     assertSame(tf, p.getThreadFactory());
340     joinPool(p);
341     }
342    
343 jsr166 1.22 /**
344 dl 1.8 * setThreadFactory(null) throws NPE
345     */
346 jsr166 1.25 public void testSetThreadFactoryNull() throws InterruptedException {
347 jsr166 1.26 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
348 dl 1.8 try {
349     p.setThreadFactory(null);
350     shouldThrow();
351     } catch (NullPointerException success) {
352     } finally {
353     joinPool(p);
354     }
355     }
356 jsr166 1.22
357 dl 1.2 /**
358 dl 1.6 * is isShutDown is false before shutdown, true after
359 dl 1.2 */
360 dl 1.5 public void testIsShutdown() {
361 jsr166 1.22
362 jsr166 1.26 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
363 dl 1.2 try {
364 dl 1.5 assertFalse(p1.isShutdown());
365 dl 1.2 }
366     finally {
367 jsr166 1.23 try { p1.shutdown(); } catch (SecurityException ok) { return; }
368 dl 1.2 }
369 jsr166 1.26 assertTrue(p1.isShutdown());
370 dl 1.2 }
371    
372 jsr166 1.22
373 dl 1.2 /**
374 dl 1.6 * isTerminated is false before termination, true after
375 dl 1.2 */
376 jsr166 1.25 public void testIsTerminated() throws InterruptedException {
377 jsr166 1.26 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
378 dl 1.2 try {
379 dl 1.5 p1.execute(new SmallRunnable());
380 dl 1.2 } finally {
381 jsr166 1.23 try { p1.shutdown(); } catch (SecurityException ok) { return; }
382 dl 1.2 }
383 jsr166 1.25 assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
384     assertTrue(p1.isTerminated());
385 dl 1.5 }
386    
387     /**
388 dl 1.6 * isTerminating is not true when running or when terminated
389 dl 1.5 */
390 jsr166 1.25 public void testIsTerminating() throws InterruptedException {
391 jsr166 1.26 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
392 dl 1.5 assertFalse(p1.isTerminating());
393     try {
394     p1.execute(new SmallRunnable());
395     assertFalse(p1.isTerminating());
396     } finally {
397 jsr166 1.23 try { p1.shutdown(); } catch (SecurityException ok) { return; }
398 dl 1.5 }
399 jsr166 1.25
400     assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
401     assertTrue(p1.isTerminated());
402     assertFalse(p1.isTerminating());
403 dl 1.2 }
404    
405     /**
406 dl 1.8 * getQueue returns the work queue, which contains queued tasks
407     */
408 jsr166 1.25 public void testGetQueue() throws InterruptedException {
409 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
410     ScheduledFuture[] tasks = new ScheduledFuture[5];
411 jsr166 1.24 for (int i = 0; i < 5; i++) {
412 jsr166 1.25 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
413 dl 1.8 }
414     try {
415     Thread.sleep(SHORT_DELAY_MS);
416     BlockingQueue<Runnable> q = p1.getQueue();
417     assertTrue(q.contains(tasks[4]));
418     assertFalse(q.contains(tasks[0]));
419     } finally {
420     joinPool(p1);
421     }
422     }
423    
424     /**
425     * remove(task) removes queued task, and fails to remove active task
426     */
427 jsr166 1.25 public void testRemove() throws InterruptedException {
428 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
429     ScheduledFuture[] tasks = new ScheduledFuture[5];
430 jsr166 1.24 for (int i = 0; i < 5; i++) {
431 jsr166 1.25 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
432 dl 1.8 }
433     try {
434     Thread.sleep(SHORT_DELAY_MS);
435     BlockingQueue<Runnable> q = p1.getQueue();
436     assertFalse(p1.remove((Runnable)tasks[0]));
437     assertTrue(q.contains((Runnable)tasks[4]));
438     assertTrue(q.contains((Runnable)tasks[3]));
439     assertTrue(p1.remove((Runnable)tasks[4]));
440     assertFalse(p1.remove((Runnable)tasks[4]));
441     assertFalse(q.contains((Runnable)tasks[4]));
442     assertTrue(q.contains((Runnable)tasks[3]));
443     assertTrue(p1.remove((Runnable)tasks[3]));
444     assertFalse(q.contains((Runnable)tasks[3]));
445     } finally {
446     joinPool(p1);
447     }
448     }
449    
450     /**
451     * purge removes cancelled tasks from the queue
452 dl 1.2 */
453 jsr166 1.25 public void testPurge() throws InterruptedException {
454 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
455     ScheduledFuture[] tasks = new ScheduledFuture[5];
456 jsr166 1.24 for (int i = 0; i < 5; i++) {
457 jsr166 1.25 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
458 dl 1.19 }
459     try {
460     int max = 5;
461     if (tasks[4].cancel(true)) --max;
462     if (tasks[3].cancel(true)) --max;
463     // There must eventually be an interference-free point at
464     // which purge will not fail. (At worst, when queue is empty.)
465     int k;
466     for (k = 0; k < SMALL_DELAY_MS; ++k) {
467     p1.purge();
468     long count = p1.getTaskCount();
469     if (count >= 0 && count <= max)
470     break;
471     Thread.sleep(1);
472     }
473     assertTrue(k < SMALL_DELAY_MS);
474     } finally {
475     joinPool(p1);
476 dl 1.2 }
477     }
478    
479     /**
480 dl 1.6 * shutDownNow returns a list containing tasks that were not run
481 dl 1.2 */
482 jsr166 1.25 public void testShutDownNow() throws InterruptedException {
483 jsr166 1.26 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
484 jsr166 1.23 for (int i = 0; i < 5; i++)
485 jsr166 1.25 p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
486 dl 1.17 List l;
487     try {
488     l = p1.shutdownNow();
489 jsr166 1.22 } catch (SecurityException ok) {
490 dl 1.17 return;
491     }
492 jsr166 1.26 assertTrue(p1.isShutdown());
493     assertTrue(l.size() > 0 && l.size() <= 5);
494 dl 1.5 joinPool(p1);
495 dl 1.2 }
496    
497 dl 1.5 /**
498 dl 1.6 * In default setting, shutdown cancels periodic but not delayed
499     * tasks at shutdown
500 dl 1.5 */
501 jsr166 1.25 public void testShutDown1() throws InterruptedException {
502     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
503     assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
504     assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
505 jsr166 1.22
506 jsr166 1.25 ScheduledFuture[] tasks = new ScheduledFuture[5];
507     for (int i = 0; i < 5; i++)
508     tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
509     try { p1.shutdown(); } catch (SecurityException ok) { return; }
510     BlockingQueue q = p1.getQueue();
511     for (Iterator it = q.iterator(); it.hasNext();) {
512     ScheduledFuture t = (ScheduledFuture)it.next();
513     assertFalse(t.isCancelled());
514     }
515     assertTrue(p1.isShutdown());
516     Thread.sleep(SMALL_DELAY_MS);
517     for (int i = 0; i < 5; ++i) {
518     assertTrue(tasks[i].isDone());
519     assertFalse(tasks[i].isCancelled());
520 dl 1.2 }
521     }
522    
523    
524 dl 1.5 /**
525 dl 1.6 * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
526     * delayed tasks are cancelled at shutdown
527 dl 1.5 */
528 jsr166 1.25 public void testShutDown2() throws InterruptedException {
529     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
530     p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
531     ScheduledFuture[] tasks = new ScheduledFuture[5];
532     for (int i = 0; i < 5; i++)
533     tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS);
534     try { p1.shutdown(); } catch (SecurityException ok) { return; }
535     assertTrue(p1.isShutdown());
536     BlockingQueue q = p1.getQueue();
537     assertTrue(q.isEmpty());
538     Thread.sleep(SMALL_DELAY_MS);
539     assertTrue(p1.isTerminated());
540 dl 1.2 }
541    
542    
543 dl 1.5 /**
544 dl 1.6 * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
545     * periodic tasks are not cancelled at shutdown
546 dl 1.5 */
547 jsr166 1.25 public void testShutDown3() throws InterruptedException {
548     ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
549     p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
550     ScheduledFuture task =
551     p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS);
552     try { p1.shutdown(); } catch (SecurityException ok) { return; }
553     assertTrue(p1.isShutdown());
554     BlockingQueue q = p1.getQueue();
555     assertTrue(q.isEmpty());
556     Thread.sleep(SHORT_DELAY_MS);
557     assertTrue(p1.isTerminated());
558 dl 1.2 }
559    
560 dl 1.5 /**
561 dl 1.6 * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
562     * periodic tasks are cancelled at shutdown
563 dl 1.5 */
564 jsr166 1.25 public void testShutDown4() throws InterruptedException {
565 dl 1.9 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
566 dl 1.2 try {
567 dl 1.5 p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
568 dl 1.9 ScheduledFuture task =
569 jsr166 1.25 p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
570 dl 1.2 assertFalse(task.isCancelled());
571 jsr166 1.23 try { p1.shutdown(); } catch (SecurityException ok) { return; }
572 dl 1.2 assertFalse(task.isCancelled());
573 dl 1.5 assertFalse(p1.isTerminated());
574     assertTrue(p1.isShutdown());
575 dl 1.2 Thread.sleep(SHORT_DELAY_MS);
576     assertFalse(task.isCancelled());
577 dl 1.16 assertTrue(task.cancel(true));
578 dl 1.15 assertTrue(task.isDone());
579 dl 1.2 Thread.sleep(SHORT_DELAY_MS);
580 dl 1.5 assertTrue(p1.isTerminated());
581 dl 1.2 }
582 jsr166 1.22 finally {
583 dl 1.17 joinPool(p1);
584 dl 1.2 }
585 dl 1.1 }
586    
587 dl 1.10 /**
588     * completed submit of callable returns result
589     */
590 jsr166 1.25 public void testSubmitCallable() throws Exception {
591 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
592     try {
593     Future<String> future = e.submit(new StringTask());
594     String result = future.get();
595     assertSame(TEST_STRING, result);
596     } finally {
597     joinPool(e);
598     }
599     }
600    
601     /**
602     * completed submit of runnable returns successfully
603     */
604 jsr166 1.25 public void testSubmitRunnable() throws Exception {
605 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
606     try {
607     Future<?> future = e.submit(new NoOpRunnable());
608     future.get();
609     assertTrue(future.isDone());
610     } finally {
611     joinPool(e);
612     }
613     }
614    
615     /**
616     * completed submit of (runnable, result) returns result
617     */
618 jsr166 1.25 public void testSubmitRunnable2() throws Exception {
619 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
620     try {
621     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
622     String result = future.get();
623     assertSame(TEST_STRING, result);
624     } finally {
625     joinPool(e);
626     }
627     }
628    
629     /**
630     * invokeAny(null) throws NPE
631     */
632 jsr166 1.25 public void testInvokeAny1() throws Exception {
633 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
634     try {
635     e.invokeAny(null);
636 jsr166 1.25 shouldThrow();
637 dl 1.10 } catch (NullPointerException success) {
638     } finally {
639     joinPool(e);
640     }
641     }
642    
643     /**
644     * invokeAny(empty collection) throws IAE
645     */
646 jsr166 1.25 public void testInvokeAny2() throws Exception {
647 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
648     try {
649     e.invokeAny(new ArrayList<Callable<String>>());
650 jsr166 1.25 shouldThrow();
651 dl 1.10 } catch (IllegalArgumentException success) {
652     } finally {
653     joinPool(e);
654     }
655     }
656    
657     /**
658     * invokeAny(c) throws NPE if c has null elements
659     */
660 jsr166 1.25 public void testInvokeAny3() throws Exception {
661 jsr166 1.29 CountDownLatch latch = new CountDownLatch(1);
662 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
663 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
664     l.add(latchAwaitingStringTask(latch));
665     l.add(null);
666 dl 1.10 try {
667     e.invokeAny(l);
668 jsr166 1.25 shouldThrow();
669 dl 1.10 } catch (NullPointerException success) {
670     } finally {
671 jsr166 1.25 latch.countDown();
672 dl 1.10 joinPool(e);
673     }
674     }
675    
676     /**
677     * invokeAny(c) throws ExecutionException if no task completes
678     */
679 jsr166 1.25 public void testInvokeAny4() throws Exception {
680 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
681 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
682     l.add(new NPETask());
683 dl 1.10 try {
684     e.invokeAny(l);
685 jsr166 1.25 shouldThrow();
686 dl 1.10 } catch (ExecutionException success) {
687 jsr166 1.25 assertTrue(success.getCause() instanceof NullPointerException);
688 dl 1.10 } finally {
689     joinPool(e);
690     }
691     }
692    
693     /**
694     * invokeAny(c) returns result of some task
695     */
696 jsr166 1.25 public void testInvokeAny5() throws Exception {
697 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
698     try {
699 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
700 dl 1.10 l.add(new StringTask());
701     l.add(new StringTask());
702     String result = e.invokeAny(l);
703     assertSame(TEST_STRING, result);
704     } finally {
705     joinPool(e);
706     }
707     }
708    
709     /**
710     * invokeAll(null) throws NPE
711     */
712 jsr166 1.25 public void testInvokeAll1() throws Exception {
713 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
714     try {
715     e.invokeAll(null);
716 jsr166 1.25 shouldThrow();
717 dl 1.10 } catch (NullPointerException success) {
718     } finally {
719     joinPool(e);
720     }
721     }
722    
723     /**
724     * invokeAll(empty collection) returns empty collection
725     */
726 jsr166 1.25 public void testInvokeAll2() throws Exception {
727 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
728     try {
729     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
730     assertTrue(r.isEmpty());
731     } finally {
732     joinPool(e);
733     }
734     }
735    
736     /**
737     * invokeAll(c) throws NPE if c has null elements
738     */
739 jsr166 1.25 public void testInvokeAll3() throws Exception {
740 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
741 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
742     l.add(new StringTask());
743     l.add(null);
744 dl 1.10 try {
745     e.invokeAll(l);
746 jsr166 1.25 shouldThrow();
747 dl 1.10 } catch (NullPointerException success) {
748     } finally {
749     joinPool(e);
750     }
751     }
752    
753     /**
754     * get of invokeAll(c) throws exception on failed task
755     */
756 jsr166 1.25 public void testInvokeAll4() throws Exception {
757 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
758 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
759     l.add(new NPETask());
760     List<Future<String>> futures = e.invokeAll(l);
761     assertEquals(1, futures.size());
762 dl 1.10 try {
763 jsr166 1.29 futures.get(0).get();
764 jsr166 1.25 shouldThrow();
765 jsr166 1.23 } catch (ExecutionException success) {
766 jsr166 1.25 assertTrue(success.getCause() instanceof NullPointerException);
767 dl 1.10 } finally {
768     joinPool(e);
769     }
770     }
771    
772     /**
773     * invokeAll(c) returns results of all completed tasks
774     */
775 jsr166 1.25 public void testInvokeAll5() throws Exception {
776 dl 1.10 ExecutorService e = new ScheduledThreadPoolExecutor(2);
777     try {
778 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
779 dl 1.10 l.add(new StringTask());
780     l.add(new StringTask());
781 jsr166 1.29 List<Future<String>> futures = e.invokeAll(l);
782     assertEquals(2, futures.size());
783     for (Future<String> future : futures)
784 jsr166 1.25 assertSame(TEST_STRING, future.get());
785 dl 1.10 } finally {
786     joinPool(e);
787     }
788     }
789    
790 dl 1.11 /**
791     * timed invokeAny(null) throws NPE
792     */
793 jsr166 1.25 public void testTimedInvokeAny1() throws Exception {
794 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
795     try {
796 jsr166 1.25 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
797     shouldThrow();
798 dl 1.11 } catch (NullPointerException success) {
799     } finally {
800     joinPool(e);
801     }
802     }
803    
804     /**
805     * timed invokeAny(,,null) throws NPE
806     */
807 jsr166 1.25 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
808 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
809 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
810     l.add(new StringTask());
811 dl 1.11 try {
812     e.invokeAny(l, MEDIUM_DELAY_MS, null);
813 jsr166 1.25 shouldThrow();
814 dl 1.11 } catch (NullPointerException success) {
815     } finally {
816     joinPool(e);
817     }
818     }
819    
820     /**
821     * timed invokeAny(empty collection) throws IAE
822     */
823 jsr166 1.25 public void testTimedInvokeAny2() throws Exception {
824 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
825     try {
826 jsr166 1.25 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
827     shouldThrow();
828 dl 1.11 } catch (IllegalArgumentException success) {
829     } finally {
830     joinPool(e);
831     }
832     }
833    
834     /**
835     * timed invokeAny(c) throws NPE if c has null elements
836     */
837 jsr166 1.25 public void testTimedInvokeAny3() throws Exception {
838 jsr166 1.29 CountDownLatch latch = new CountDownLatch(1);
839 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
840 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
841     l.add(latchAwaitingStringTask(latch));
842     l.add(null);
843 dl 1.11 try {
844 jsr166 1.25 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
845     shouldThrow();
846 dl 1.11 } catch (NullPointerException success) {
847     } finally {
848 jsr166 1.25 latch.countDown();
849 dl 1.11 joinPool(e);
850     }
851     }
852    
853     /**
854     * timed invokeAny(c) throws ExecutionException if no task completes
855     */
856 jsr166 1.25 public void testTimedInvokeAny4() throws Exception {
857 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
858 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
859     l.add(new NPETask());
860 dl 1.11 try {
861 jsr166 1.25 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
862     shouldThrow();
863 jsr166 1.23 } catch (ExecutionException success) {
864 jsr166 1.25 assertTrue(success.getCause() instanceof NullPointerException);
865 dl 1.11 } finally {
866     joinPool(e);
867     }
868     }
869    
870     /**
871     * timed invokeAny(c) returns result of some task
872     */
873 jsr166 1.25 public void testTimedInvokeAny5() throws Exception {
874 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
875     try {
876 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
877 dl 1.11 l.add(new StringTask());
878     l.add(new StringTask());
879 jsr166 1.25 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
880 dl 1.11 assertSame(TEST_STRING, result);
881     } finally {
882     joinPool(e);
883     }
884     }
885    
886     /**
887     * timed invokeAll(null) throws NPE
888     */
889 jsr166 1.25 public void testTimedInvokeAll1() throws Exception {
890 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
891     try {
892 jsr166 1.25 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
893     shouldThrow();
894 dl 1.11 } catch (NullPointerException success) {
895     } finally {
896     joinPool(e);
897     }
898     }
899    
900     /**
901     * timed invokeAll(,,null) throws NPE
902     */
903 jsr166 1.25 public void testTimedInvokeAllNullTimeUnit() throws Exception {
904 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
905 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
906     l.add(new StringTask());
907 dl 1.11 try {
908     e.invokeAll(l, MEDIUM_DELAY_MS, null);
909 jsr166 1.25 shouldThrow();
910 dl 1.11 } catch (NullPointerException success) {
911     } finally {
912     joinPool(e);
913     }
914     }
915    
916     /**
917     * timed invokeAll(empty collection) returns empty collection
918     */
919 jsr166 1.25 public void testTimedInvokeAll2() throws Exception {
920 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
921     try {
922 jsr166 1.25 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
923 dl 1.11 assertTrue(r.isEmpty());
924     } finally {
925     joinPool(e);
926     }
927     }
928    
929     /**
930     * timed invokeAll(c) throws NPE if c has null elements
931     */
932 jsr166 1.25 public void testTimedInvokeAll3() throws Exception {
933 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
934 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
935     l.add(new StringTask());
936     l.add(null);
937 dl 1.11 try {
938 jsr166 1.25 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
939     shouldThrow();
940 dl 1.11 } catch (NullPointerException success) {
941     } finally {
942     joinPool(e);
943     }
944     }
945    
946     /**
947     * get of element of invokeAll(c) throws exception on failed task
948     */
949 jsr166 1.25 public void testTimedInvokeAll4() throws Exception {
950 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
951 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
952     l.add(new NPETask());
953     List<Future<String>> futures =
954     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
955     assertEquals(1, futures.size());
956 dl 1.11 try {
957 jsr166 1.29 futures.get(0).get();
958 jsr166 1.25 shouldThrow();
959 jsr166 1.23 } catch (ExecutionException success) {
960 jsr166 1.25 assertTrue(success.getCause() instanceof NullPointerException);
961 dl 1.11 } finally {
962     joinPool(e);
963     }
964     }
965    
966     /**
967     * timed invokeAll(c) returns results of all completed tasks
968     */
969 jsr166 1.25 public void testTimedInvokeAll5() throws Exception {
970 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
971     try {
972 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
973 dl 1.11 l.add(new StringTask());
974     l.add(new StringTask());
975 jsr166 1.29 List<Future<String>> futures =
976     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
977     assertEquals(2, futures.size());
978     for (Future<String> future : futures)
979 jsr166 1.25 assertSame(TEST_STRING, future.get());
980 dl 1.11 } finally {
981     joinPool(e);
982     }
983     }
984    
985     /**
986     * timed invokeAll(c) cancels tasks not completed by timeout
987     */
988 jsr166 1.25 public void testTimedInvokeAll6() throws Exception {
989 dl 1.11 ExecutorService e = new ScheduledThreadPoolExecutor(2);
990     try {
991 jsr166 1.29 List<Callable<String>> l = new ArrayList<Callable<String>>();
992 dl 1.11 l.add(new StringTask());
993 dl 1.12 l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
994 dl 1.14 l.add(new StringTask());
995 jsr166 1.29 List<Future<String>> futures =
996     e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
997     assertEquals(3, futures.size());
998     Iterator<Future<String>> it = futures.iterator();
999 dl 1.11 Future<String> f1 = it.next();
1000     Future<String> f2 = it.next();
1001 dl 1.14 Future<String> f3 = it.next();
1002 dl 1.11 assertTrue(f1.isDone());
1003 dl 1.14 assertTrue(f2.isDone());
1004     assertTrue(f3.isDone());
1005 dl 1.11 assertFalse(f1.isCancelled());
1006     assertTrue(f2.isCancelled());
1007     } finally {
1008     joinPool(e);
1009     }
1010     }
1011    
1012 dl 1.10
1013 dl 1.1 }