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

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