ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.4
Committed: Sun Sep 14 20:42:40 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.3: +144 -200 lines
Log Message:
New base class JSR166TestCase

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