ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.5
Committed: Sat Sep 20 18:20:08 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.4: +208 -170 lines
Log Message:
Documentation scaffolding

File Contents

# User Rev Content
1 dl 1.1 /*
2     * Written by members of JCP JSR-166 Expert Group and released to the
3     * public domain. Use, modify, and redistribute this code in any way
4     * without acknowledgement. Other contributors include Andrew Wright,
5     * Jeffrey Hayes, Pat Fischer, Mike Judd.
6     */
7    
8     import junit.framework.*;
9     import java.util.*;
10     import java.util.concurrent.*;
11    
12 dl 1.4 public class ScheduledExecutorTest extends JSR166TestCase {
13 dl 1.1 public static void main(String[] args) {
14     junit.textui.TestRunner.run (suite());
15     }
16     public static Test suite() {
17     return new TestSuite(ScheduledExecutorTest.class);
18     }
19    
20     static class MyRunnable implements Runnable {
21     volatile boolean done = false;
22 dl 1.5 public void run() {
23     try {
24 dl 1.4 Thread.sleep(SMALL_DELAY_MS);
25 dl 1.1 done = true;
26 dl 1.2 } catch(Exception e){
27     }
28 dl 1.1 }
29     }
30    
31     static class MyCallable implements Callable {
32     volatile boolean done = false;
33 dl 1.5 public Object call() {
34     try {
35 dl 1.4 Thread.sleep(SMALL_DELAY_MS);
36 dl 1.1 done = true;
37 dl 1.5 } catch(Exception e){
38     }
39 dl 1.1 return Boolean.TRUE;
40     }
41     }
42    
43 dl 1.5 /**
44     *
45     */
46     public void testExecute() {
47     try {
48 dl 1.1 MyRunnable runnable =new MyRunnable();
49 dl 1.5 ScheduledExecutor p1 = new ScheduledExecutor(1);
50     p1.execute(runnable);
51 dl 1.4 assertFalse(runnable.done);
52     Thread.sleep(SHORT_DELAY_MS);
53 dl 1.5 p1.shutdown();
54     try {
55 dl 1.2 Thread.sleep(MEDIUM_DELAY_MS);
56     } catch(InterruptedException e){
57 dl 1.5 unexpectedException();
58 dl 1.2 }
59 dl 1.1 assertTrue(runnable.done);
60 dl 1.5 p1.shutdown();
61     joinPool(p1);
62 dl 1.1 }
63     catch(Exception e){
64 dl 1.5 unexpectedException();
65 dl 1.1 }
66 dl 1.4
67 dl 1.1 }
68    
69 dl 1.5 /**
70     *
71     */
72     public void testSchedule1() {
73     try {
74 dl 1.1 MyCallable callable = new MyCallable();
75 dl 1.5 ScheduledExecutor p1 = new ScheduledExecutor(1);
76     Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
77 dl 1.4 assertFalse(callable.done);
78 dl 1.2 Thread.sleep(MEDIUM_DELAY_MS);
79 dl 1.1 assertTrue(callable.done);
80     assertEquals(Boolean.TRUE, f.get());
81 dl 1.5 p1.shutdown();
82     joinPool(p1);
83     } catch(RejectedExecutionException e){}
84 dl 1.2 catch(Exception e){
85 dl 1.5 unexpectedException();
86 dl 1.2 }
87 dl 1.1 }
88    
89     /**
90 dl 1.5 *
91 dl 1.1 */
92 dl 1.5 public void testSchedule3() {
93     try {
94 dl 1.1 MyRunnable runnable = new MyRunnable();
95 dl 1.5 ScheduledExecutor p1 = new ScheduledExecutor(1);
96     p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
97 dl 1.4 Thread.sleep(SHORT_DELAY_MS);
98     assertFalse(runnable.done);
99 dl 1.2 Thread.sleep(MEDIUM_DELAY_MS);
100 dl 1.1 assertTrue(runnable.done);
101 dl 1.5 p1.shutdown();
102     joinPool(p1);
103 dl 1.1 } catch(Exception e){
104 dl 1.5 unexpectedException();
105 dl 1.1 }
106     }
107    
108     /**
109 dl 1.5 *
110 dl 1.1 */
111 dl 1.5 public void testSchedule4() {
112     try {
113 dl 1.1 MyRunnable runnable = new MyRunnable();
114 dl 1.5 ScheduledExecutor p1 = new ScheduledExecutor(1);
115     p1.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
116 dl 1.4 assertFalse(runnable.done);
117 dl 1.2 Thread.sleep(MEDIUM_DELAY_MS);
118 dl 1.1 assertTrue(runnable.done);
119 dl 1.5 p1.shutdown();
120     joinPool(p1);
121 dl 1.1 } catch(Exception e){
122 dl 1.5 unexpectedException();
123 dl 1.1 }
124     }
125    
126    
127     // exception tests
128    
129     /**
130 dl 1.4 * schedule(Runnable, long) throws RejectedExecutionException
131 dl 1.1 * This occurs on an attempt to schedule a task on a shutdown executor
132     */
133 dl 1.5 public void testSchedule1_RejectedExecutionException() {
134 dl 1.4 ScheduledExecutor se = new ScheduledExecutor(1);
135 dl 1.5 try {
136 dl 1.1 se.shutdown();
137 dl 1.4 se.schedule(new NoOpRunnable(),
138     MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
139 dl 1.5 shouldThrow();
140 dl 1.4 } catch(RejectedExecutionException success){
141     }
142     joinPool(se);
143    
144 dl 1.1 }
145    
146     /**
147 dl 1.4 * schedule(Callable, long, TimeUnit) throws RejectedExecutionException
148 dl 1.1 * This occurs on an attempt to schedule a task on a shutdown executor
149     */
150 dl 1.5 public void testSchedule2_RejectedExecutionException() {
151 dl 1.4 ScheduledExecutor se = new ScheduledExecutor(1);
152 dl 1.5 try {
153 dl 1.1 se.shutdown();
154 dl 1.4 se.schedule(new NoOpCallable(),
155     MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
156 dl 1.5 shouldThrow();
157 dl 1.4 } catch(RejectedExecutionException success){
158     }
159     joinPool(se);
160 dl 1.1 }
161    
162     /**
163 dl 1.4 * schedule(Callable, long) throws RejectedExecutionException
164 dl 1.1 * This occurs on an attempt to schedule a task on a shutdown executor
165     */
166 dl 1.5 public void testSchedule3_RejectedExecutionException() {
167 dl 1.4 ScheduledExecutor se = new ScheduledExecutor(1);
168 dl 1.5 try {
169 dl 1.1 se.shutdown();
170 dl 1.4 se.schedule(new NoOpCallable(),
171     MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
172 dl 1.5 shouldThrow();
173 dl 1.4 } catch(RejectedExecutionException success){
174     }
175     joinPool(se);
176 dl 1.1 }
177    
178     /**
179 dl 1.4 * scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws
180 dl 1.1 * RejectedExecutionException.
181     * This occurs on an attempt to schedule a task on a shutdown executor
182     */
183 dl 1.5 public void testScheduleAtFixedRate1_RejectedExecutionException() {
184 dl 1.4 ScheduledExecutor se = new ScheduledExecutor(1);
185 dl 1.5 try {
186 dl 1.1 se.shutdown();
187 dl 1.4 se.scheduleAtFixedRate(new NoOpRunnable(),
188     MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
189 dl 1.5 shouldThrow();
190 dl 1.4 } catch(RejectedExecutionException success){
191     }
192     joinPool(se);
193 dl 1.1 }
194    
195     /**
196 dl 1.4 * scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws
197 dl 1.1 * RejectedExecutionException.
198     * This occurs on an attempt to schedule a task on a shutdown executor
199     */
200 dl 1.5 public void testScheduleAtFixedRate2_RejectedExecutionException() {
201 dl 1.4 ScheduledExecutor se = new ScheduledExecutor(1);
202 dl 1.5 try {
203 dl 1.1 se.shutdown();
204 dl 1.4 se.scheduleAtFixedRate(new NoOpRunnable(),
205     1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
206 dl 1.5 shouldThrow();
207 dl 1.4 } catch(RejectedExecutionException success){
208     }
209     joinPool(se);
210 dl 1.1 }
211    
212     /**
213 dl 1.4 * scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws
214 dl 1.1 * RejectedExecutionException.
215     * This occurs on an attempt to schedule a task on a shutdown executor
216     */
217 dl 1.5 public void testScheduleWithFixedDelay1_RejectedExecutionException() {
218 dl 1.4 ScheduledExecutor se = new ScheduledExecutor(1);
219 dl 1.5 try {
220 dl 1.1 se.shutdown();
221 dl 1.4 se.scheduleWithFixedDelay(new NoOpRunnable(),
222     MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
223 dl 1.5 shouldThrow();
224 dl 1.4 } catch(RejectedExecutionException success){
225     }
226     joinPool(se);
227 dl 1.1 }
228    
229     /**
230 dl 1.4 * scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws
231 dl 1.1 * RejectedExecutionException.
232     * This occurs on an attempt to schedule a task on a shutdown executor
233     */
234 dl 1.5 public void testScheduleWithFixedDelay2_RejectedExecutionException() {
235 dl 1.4 ScheduledExecutor se = new ScheduledExecutor(1);
236 dl 1.5 try {
237 dl 1.1 se.shutdown();
238 dl 1.4 se.scheduleWithFixedDelay(new NoOpRunnable(),
239     1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
240 dl 1.5 shouldThrow();
241 dl 1.4 } catch(RejectedExecutionException success){
242     }
243     joinPool(se);
244 dl 1.1 }
245    
246     /**
247 dl 1.4 * execute throws RejectedExecutionException
248 dl 1.1 * This occurs on an attempt to schedule a task on a shutdown executor
249     */
250 dl 1.5 public void testExecute_RejectedExecutionException() {
251 dl 1.4 ScheduledExecutor se = new ScheduledExecutor(1);
252 dl 1.5 try {
253 dl 1.1 se.shutdown();
254 dl 1.4 se.execute(new NoOpRunnable());
255 dl 1.5 shouldThrow();
256 dl 1.4 } catch(RejectedExecutionException success){
257     }
258     joinPool(se);
259 dl 1.2 }
260    
261     /**
262 dl 1.4 * getActiveCount gives correct values
263 dl 1.2 */
264 dl 1.5 public void testGetActiveCount() {
265     ScheduledExecutor p2 = new ScheduledExecutor(2);
266     assertEquals(0, p2.getActiveCount());
267     p2.execute(new SmallRunnable());
268     try {
269 dl 1.4 Thread.sleep(SHORT_DELAY_MS);
270     } catch(Exception e){
271 dl 1.5 unexpectedException();
272 dl 1.2 }
273 dl 1.5 assertEquals(1, p2.getActiveCount());
274     joinPool(p2);
275 dl 1.2 }
276    
277     /**
278 dl 1.4 * getCompleteTaskCount gives correct values
279 dl 1.2 */
280 dl 1.5 public void testGetCompletedTaskCount() {
281     ScheduledExecutor p2 = new ScheduledExecutor(2);
282     assertEquals(0, p2.getCompletedTaskCount());
283     p2.execute(new SmallRunnable());
284     try {
285 dl 1.4 Thread.sleep(MEDIUM_DELAY_MS);
286     } catch(Exception e){
287 dl 1.5 unexpectedException();
288 dl 1.2 }
289 dl 1.5 assertEquals(1, p2.getCompletedTaskCount());
290     joinPool(p2);
291 dl 1.2 }
292    
293     /**
294 dl 1.4 * getCorePoolSize gives correct values
295 dl 1.2 */
296 dl 1.5 public void testGetCorePoolSize() {
297     ScheduledExecutor p1 = new ScheduledExecutor(1);
298     assertEquals(1, p1.getCorePoolSize());
299     joinPool(p1);
300 dl 1.2 }
301    
302     /**
303 dl 1.4 * getLargestPoolSize gives correct values
304 dl 1.2 */
305 dl 1.5 public void testGetLargestPoolSize() {
306     ScheduledExecutor p2 = new ScheduledExecutor(2);
307     assertEquals(0, p2.getLargestPoolSize());
308     p2.execute(new SmallRunnable());
309     p2.execute(new SmallRunnable());
310     try {
311 dl 1.4 Thread.sleep(SHORT_DELAY_MS);
312     } catch(Exception e){
313 dl 1.5 unexpectedException();
314 dl 1.2 }
315 dl 1.5 assertEquals(2, p2.getLargestPoolSize());
316     joinPool(p2);
317 dl 1.2 }
318    
319     /**
320 dl 1.4 * getPoolSize gives correct values
321 dl 1.2 */
322 dl 1.5 public void testGetPoolSize() {
323     ScheduledExecutor p1 = new ScheduledExecutor(1);
324     assertEquals(0, p1.getPoolSize());
325     p1.execute(new SmallRunnable());
326     assertEquals(1, p1.getPoolSize());
327     joinPool(p1);
328 dl 1.2 }
329    
330     /**
331 dl 1.4 * getTaskCount gives correct values
332 dl 1.2 */
333 dl 1.5 public void testGetTaskCount() {
334     ScheduledExecutor p1 = new ScheduledExecutor(1);
335     assertEquals(0, p1.getTaskCount());
336 dl 1.4 for(int i = 0; i < 5; i++)
337 dl 1.5 p1.execute(new SmallRunnable());
338     try {
339 dl 1.4 Thread.sleep(SHORT_DELAY_MS);
340     } catch(Exception e){
341 dl 1.5 unexpectedException();
342 dl 1.2 }
343 dl 1.5 assertEquals(5, p1.getTaskCount());
344     joinPool(p1);
345 dl 1.2 }
346    
347     /**
348 dl 1.4 * isShutDown gives correct values
349 dl 1.2 */
350 dl 1.5 public void testIsShutdown() {
351 dl 1.2
352 dl 1.5 ScheduledExecutor p1 = new ScheduledExecutor(1);
353 dl 1.2 try {
354 dl 1.5 assertFalse(p1.isShutdown());
355 dl 1.2 }
356     finally {
357 dl 1.5 p1.shutdown();
358 dl 1.2 }
359 dl 1.5 assertTrue(p1.isShutdown());
360 dl 1.2 }
361    
362    
363     /**
364 dl 1.5 * isTerminated gives correct values
365 dl 1.2 */
366 dl 1.5 public void testIsTerminated() {
367     ScheduledExecutor p1 = new ScheduledExecutor(1);
368 dl 1.2 try {
369 dl 1.5 p1.execute(new SmallRunnable());
370 dl 1.2 } finally {
371 dl 1.5 p1.shutdown();
372 dl 1.2 }
373 dl 1.4 try {
374 dl 1.5 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
375     assertTrue(p1.isTerminated());
376 dl 1.2 } catch(Exception e){
377 dl 1.5 unexpectedException();
378     }
379     }
380    
381     /**
382     * isTerminating gives correct values
383     */
384     public void testIsTerminating() {
385     ScheduledExecutor p1 = new ScheduledExecutor(1);
386     assertFalse(p1.isTerminating());
387     try {
388     p1.execute(new SmallRunnable());
389     assertFalse(p1.isTerminating());
390     } finally {
391     p1.shutdown();
392     }
393     try {
394     assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
395     assertTrue(p1.isTerminated());
396     assertFalse(p1.isTerminating());
397     } catch(Exception e){
398     unexpectedException();
399 dl 1.2 }
400     }
401    
402     /**
403 dl 1.4 * that purge correctly removes cancelled tasks
404 dl 1.2 * from the queue
405     */
406 dl 1.5 public void testPurge() {
407     ScheduledExecutor p1 = new ScheduledExecutor(1);
408 dl 1.4 ScheduledCancellable[] tasks = new ScheduledCancellable[5];
409     for(int i = 0; i < 5; i++){
410 dl 1.5 tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS);
411 dl 1.2 }
412 dl 1.4 int max = 5;
413     if (tasks[4].cancel(true)) --max;
414     if (tasks[3].cancel(true)) --max;
415 dl 1.5 p1.purge();
416     long count = p1.getTaskCount();
417 dl 1.4 assertTrue(count > 0 && count <= max);
418 dl 1.5 joinPool(p1);
419 dl 1.2 }
420    
421     /**
422 dl 1.4 * shutDownNow returns a list
423 dl 1.2 * containing the correct number of elements
424     */
425 dl 1.5 public void testShutDownNow() {
426     ScheduledExecutor p1 = new ScheduledExecutor(1);
427 dl 1.2 for(int i = 0; i < 5; i++)
428 dl 1.5 p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
429     List l = p1.shutdownNow();
430     assertTrue(p1.isShutdown());
431 dl 1.2 assertTrue(l.size() > 0 && l.size() <= 5);
432 dl 1.5 joinPool(p1);
433 dl 1.2 }
434    
435 dl 1.5 /**
436     *
437     */
438     public void testShutDown1() {
439 dl 1.2 try {
440 dl 1.5 ScheduledExecutor p1 = new ScheduledExecutor(1);
441     assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
442     assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
443 dl 1.2
444     ScheduledCancellable[] tasks = new ScheduledCancellable[5];
445     for(int i = 0; i < 5; i++)
446 dl 1.5 tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
447     p1.shutdown();
448     BlockingQueue q = p1.getQueue();
449 dl 1.2 for (Iterator it = q.iterator(); it.hasNext();) {
450     ScheduledCancellable t = (ScheduledCancellable)it.next();
451     assertFalse(t.isCancelled());
452     }
453 dl 1.5 assertTrue(p1.isShutdown());
454 dl 1.4 Thread.sleep(SMALL_DELAY_MS);
455 dl 1.2 for (int i = 0; i < 5; ++i) {
456     assertTrue(tasks[i].isDone());
457     assertFalse(tasks[i].isCancelled());
458     }
459    
460     }
461     catch(Exception ex) {
462 dl 1.5 unexpectedException();
463 dl 1.2 }
464     }
465    
466    
467 dl 1.5 /**
468     *
469     */
470     public void testShutDown2() {
471 dl 1.2 try {
472 dl 1.5 ScheduledExecutor p1 = new ScheduledExecutor(1);
473     p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
474 dl 1.2 ScheduledCancellable[] tasks = new ScheduledCancellable[5];
475     for(int i = 0; i < 5; i++)
476 dl 1.5 tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
477     p1.shutdown();
478     assertTrue(p1.isShutdown());
479     BlockingQueue q = p1.getQueue();
480 dl 1.2 assertTrue(q.isEmpty());
481 dl 1.4 Thread.sleep(SMALL_DELAY_MS);
482 dl 1.5 assertTrue(p1.isTerminated());
483 dl 1.2 }
484     catch(Exception ex) {
485 dl 1.5 unexpectedException();
486 dl 1.2 }
487     }
488    
489    
490 dl 1.5 /**
491     *
492     */
493     public void testShutDown3() {
494 dl 1.2 try {
495 dl 1.5 ScheduledExecutor p1 = new ScheduledExecutor(1);
496     p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
497 dl 1.2 ScheduledCancellable task =
498 dl 1.5 p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
499     p1.shutdown();
500     assertTrue(p1.isShutdown());
501     BlockingQueue q = p1.getQueue();
502 dl 1.2 assertTrue(q.isEmpty());
503     Thread.sleep(SHORT_DELAY_MS);
504 dl 1.5 assertTrue(p1.isTerminated());
505 dl 1.2 }
506     catch(Exception ex) {
507 dl 1.5 unexpectedException();
508 dl 1.2 }
509     }
510    
511 dl 1.5 /**
512     *
513     */
514     public void testShutDown4() {
515     ScheduledExecutor p1 = new ScheduledExecutor(1);
516 dl 1.2 try {
517 dl 1.5 p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
518 dl 1.2 ScheduledCancellable task =
519 dl 1.5 p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
520 dl 1.2 assertFalse(task.isCancelled());
521 dl 1.5 p1.shutdown();
522 dl 1.2 assertFalse(task.isCancelled());
523 dl 1.5 assertFalse(p1.isTerminated());
524     assertTrue(p1.isShutdown());
525 dl 1.2 Thread.sleep(SHORT_DELAY_MS);
526     assertFalse(task.isCancelled());
527     task.cancel(true);
528     assertTrue(task.isCancelled());
529     Thread.sleep(SHORT_DELAY_MS);
530 dl 1.5 assertTrue(p1.isTerminated());
531 dl 1.2 }
532     catch(Exception ex) {
533 dl 1.5 unexpectedException();
534 dl 1.2 }
535     finally {
536 dl 1.5 p1.shutdownNow();
537 dl 1.2 }
538 dl 1.1 }
539    
540     }