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

# Content
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 public class ScheduledExecutorTest extends JSR166TestCase {
13 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 Thread.sleep(SMALL_DELAY_MS);
25 done = true;
26 } catch(Exception e){
27 }
28 }
29 }
30
31 static class MyCallable implements Callable {
32 volatile boolean done = false;
33 public Object call(){
34 try{
35 Thread.sleep(SMALL_DELAY_MS);
36 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 assertFalse(runnable.done);
48 Thread.sleep(SHORT_DELAY_MS);
49 one.shutdown();
50 try{
51 Thread.sleep(MEDIUM_DELAY_MS);
52 } catch(InterruptedException e){
53 fail("unexpected exception");
54 }
55 assertTrue(runnable.done);
56 one.shutdown();
57 joinPool(one);
58 }
59 catch(Exception e){
60 fail("unexpected exception");
61 }
62
63 }
64
65 public void testSchedule1(){
66 try{
67 MyCallable callable = new MyCallable();
68 ScheduledExecutor one = new ScheduledExecutor(1);
69 Future f = one.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
70 assertFalse(callable.done);
71 Thread.sleep(MEDIUM_DELAY_MS);
72 assertTrue(callable.done);
73 assertEquals(Boolean.TRUE, f.get());
74 one.shutdown();
75 joinPool(one);
76 }catch(RejectedExecutionException e){}
77 catch(Exception e){
78 fail("unexpected exception");
79 }
80 }
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 one.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
90 Thread.sleep(SHORT_DELAY_MS);
91 assertFalse(runnable.done);
92 Thread.sleep(MEDIUM_DELAY_MS);
93 assertTrue(runnable.done);
94 one.shutdown();
95 joinPool(one);
96 } 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 one.schedule(runnable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
109 assertFalse(runnable.done);
110 Thread.sleep(MEDIUM_DELAY_MS);
111 assertTrue(runnable.done);
112 one.shutdown();
113 joinPool(one);
114 } catch(Exception e){
115 fail("unexpected exception");
116 }
117 }
118
119
120 // exception tests
121
122 /**
123 * schedule(Runnable, long) throws RejectedExecutionException
124 * This occurs on an attempt to schedule a task on a shutdown executor
125 */
126 public void testSchedule1_RejectedExecutionException(){
127 ScheduledExecutor se = new ScheduledExecutor(1);
128 try{
129 se.shutdown();
130 se.schedule(new NoOpRunnable(),
131 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
132 fail("shoud throw");
133 } catch(RejectedExecutionException success){
134 }
135 joinPool(se);
136
137 }
138
139 /**
140 * schedule(Callable, long, TimeUnit) throws RejectedExecutionException
141 * This occurs on an attempt to schedule a task on a shutdown executor
142 */
143 public void testSchedule2_RejectedExecutionException(){
144 ScheduledExecutor se = new ScheduledExecutor(1);
145 try{
146 se.shutdown();
147 se.schedule(new NoOpCallable(),
148 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
149 fail("should throw");
150 } catch(RejectedExecutionException success){
151 }
152 joinPool(se);
153 }
154
155 /**
156 * schedule(Callable, long) throws RejectedExecutionException
157 * This occurs on an attempt to schedule a task on a shutdown executor
158 */
159 public void testSchedule3_RejectedExecutionException(){
160 ScheduledExecutor se = new ScheduledExecutor(1);
161 try{
162 se.shutdown();
163 se.schedule(new NoOpCallable(),
164 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
165 fail("should throw");
166 } catch(RejectedExecutionException success){
167 }
168 joinPool(se);
169 }
170
171 /**
172 * scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws
173 * RejectedExecutionException.
174 * This occurs on an attempt to schedule a task on a shutdown executor
175 */
176 public void testScheduleAtFixedRate1_RejectedExecutionException(){
177 ScheduledExecutor se = new ScheduledExecutor(1);
178 try{
179 se.shutdown();
180 se.scheduleAtFixedRate(new NoOpRunnable(),
181 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
182 fail("should throw");
183 } catch(RejectedExecutionException success){
184 }
185 joinPool(se);
186 }
187
188 /**
189 * scheduleAtFixedRate(Runnable, long, long, TimeUnit) throws
190 * RejectedExecutionException.
191 * This occurs on an attempt to schedule a task on a shutdown executor
192 */
193 public void testScheduleAtFixedRate2_RejectedExecutionException(){
194 ScheduledExecutor se = new ScheduledExecutor(1);
195 try{
196 se.shutdown();
197 se.scheduleAtFixedRate(new NoOpRunnable(),
198 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
199 fail("should throw");
200 } catch(RejectedExecutionException success){
201 }
202 joinPool(se);
203 }
204
205 /**
206 * scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws
207 * RejectedExecutionException.
208 * This occurs on an attempt to schedule a task on a shutdown executor
209 */
210 public void testScheduleWithFixedDelay1_RejectedExecutionException(){
211 ScheduledExecutor se = new ScheduledExecutor(1);
212 try{
213 se.shutdown();
214 se.scheduleWithFixedDelay(new NoOpRunnable(),
215 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
216 fail("should throw");
217 } catch(RejectedExecutionException success){
218 }
219 joinPool(se);
220 }
221
222 /**
223 * scheduleWithFixedDelay(Runnable, long, long, TimeUnit) throws
224 * RejectedExecutionException.
225 * This occurs on an attempt to schedule a task on a shutdown executor
226 */
227 public void testScheduleWithFixedDelay2_RejectedExecutionException(){
228 ScheduledExecutor se = new ScheduledExecutor(1);
229 try{
230 se.shutdown();
231 se.scheduleWithFixedDelay(new NoOpRunnable(),
232 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
233 fail("should throw");
234 } catch(RejectedExecutionException success){
235 }
236 joinPool(se);
237 }
238
239 /**
240 * execute throws RejectedExecutionException
241 * This occurs on an attempt to schedule a task on a shutdown executor
242 */
243 public void testExecute_RejectedExecutionException(){
244 ScheduledExecutor se = new ScheduledExecutor(1);
245 try{
246 se.shutdown();
247 se.execute(new NoOpRunnable());
248 fail("should throw");
249 } catch(RejectedExecutionException success){
250 }
251 joinPool(se);
252 }
253
254 /**
255 * getActiveCount gives correct values
256 */
257 public void testGetActiveCount(){
258 ScheduledExecutor two = new ScheduledExecutor(2);
259 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 }
266 assertEquals(1, two.getActiveCount());
267 joinPool(two);
268 }
269
270 /**
271 * getCompleteTaskCount gives correct values
272 */
273 public void testGetCompletedTaskCount(){
274 ScheduledExecutor two = new ScheduledExecutor(2);
275 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 }
282 assertEquals(1, two.getCompletedTaskCount());
283 joinPool(two);
284 }
285
286 /**
287 * getCorePoolSize gives correct values
288 */
289 public void testGetCorePoolSize(){
290 ScheduledExecutor one = new ScheduledExecutor(1);
291 assertEquals(1, one.getCorePoolSize());
292 joinPool(one);
293 }
294
295 /**
296 * getLargestPoolSize gives correct values
297 */
298 public void testGetLargestPoolSize(){
299 ScheduledExecutor two = new ScheduledExecutor(2);
300 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 }
308 assertEquals(2, two.getLargestPoolSize());
309 joinPool(two);
310 }
311
312 /**
313 * getPoolSize gives correct values
314 */
315 public void testGetPoolSize(){
316 ScheduledExecutor one = new ScheduledExecutor(1);
317 assertEquals(0, one.getPoolSize());
318 one.execute(new SmallRunnable());
319 assertEquals(1, one.getPoolSize());
320 joinPool(one);
321 }
322
323 /**
324 * getTaskCount gives correct values
325 */
326 public void testGetTaskCount(){
327 ScheduledExecutor one = new ScheduledExecutor(1);
328 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 }
336 assertEquals(5, one.getTaskCount());
337 joinPool(one);
338 }
339
340 /**
341 * isShutDown gives correct values
342 */
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 * isTerminated gives correct values
358 * 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 one.execute(new SmallRunnable());
365 } finally {
366 one.shutdown();
367 }
368 try {
369 assertTrue(one.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
370 assertTrue(one.isTerminated());
371 } catch(Exception e){
372 fail("unexpected exception");
373 }
374 }
375
376 /**
377 * that purge correctly removes cancelled tasks
378 * from the queue
379 */
380 public void testPurge(){
381 ScheduledExecutor one = new ScheduledExecutor(1);
382 ScheduledCancellable[] tasks = new ScheduledCancellable[5];
383 for(int i = 0; i < 5; i++){
384 tasks[i] = one.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS);
385 }
386 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 }
394
395 /**
396 * shutDownNow returns a list
397 * 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 one.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
403 List l = one.shutdownNow();
404 assertTrue(one.isShutdown());
405 assertTrue(l.size() > 0 && l.size() <= 5);
406 joinPool(one);
407 }
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 tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
418 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 Thread.sleep(SMALL_DELAY_MS);
426 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 tasks[i] = one.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
445 one.shutdown();
446 assertTrue(one.isShutdown());
447 BlockingQueue q = one.getQueue();
448 assertTrue(q.isEmpty());
449 Thread.sleep(SMALL_DELAY_MS);
450 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 one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
464 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 one.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
482 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 }
501
502 }