ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.6
Committed: Thu Sep 25 11:02:41 2003 UTC (20 years, 7 months ago) by dl
Branch: MAIN
Changes since 1.5: +87 -85 lines
Log Message:
improve tck javadocs; rename and add a few tests

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 * execute successfully executes a runnable
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 * delayed schedule of callable successfully executes after delay
72 */
73 public void testSchedule1() {
74 try {
75 MyCallable callable = new MyCallable();
76 ScheduledExecutor p1 = new ScheduledExecutor(1);
77 Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
78 assertFalse(callable.done);
79 Thread.sleep(MEDIUM_DELAY_MS);
80 assertTrue(callable.done);
81 assertEquals(Boolean.TRUE, f.get());
82 p1.shutdown();
83 joinPool(p1);
84 } catch(RejectedExecutionException e){}
85 catch(Exception e){
86 unexpectedException();
87 }
88 }
89
90 /**
91 * delayed schedule of runnable successfully executes after delay
92 */
93 public void testSchedule3() {
94 try {
95 MyRunnable runnable = new MyRunnable();
96 ScheduledExecutor p1 = new ScheduledExecutor(1);
97 p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
98 Thread.sleep(SHORT_DELAY_MS);
99 assertFalse(runnable.done);
100 Thread.sleep(MEDIUM_DELAY_MS);
101 assertTrue(runnable.done);
102 p1.shutdown();
103 joinPool(p1);
104 } catch(Exception e){
105 unexpectedException();
106 }
107 }
108
109 /**
110 * scheduleAtFixedRate executes runnable after given initial delay
111 */
112 public void testSchedule4() {
113 try {
114 MyRunnable runnable = new MyRunnable();
115 ScheduledExecutor p1 = new ScheduledExecutor(1);
116 ScheduledCancellable h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
117 assertFalse(runnable.done);
118 Thread.sleep(MEDIUM_DELAY_MS);
119 assertTrue(runnable.done);
120 h.cancel(true);
121 p1.shutdown();
122 joinPool(p1);
123 } catch(Exception e){
124 unexpectedException();
125 }
126 }
127
128 /**
129 * scheduleWithFixedDelay executes runnable after given initial delay
130 */
131 public void testSchedule5() {
132 try {
133 MyRunnable runnable = new MyRunnable();
134 ScheduledExecutor p1 = new ScheduledExecutor(1);
135 ScheduledCancellable h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
136 assertFalse(runnable.done);
137 Thread.sleep(MEDIUM_DELAY_MS);
138 assertTrue(runnable.done);
139 h.cancel(true);
140 p1.shutdown();
141 joinPool(p1);
142 } catch(Exception e){
143 unexpectedException();
144 }
145 }
146
147 /**
148 * execute (null) throws NPE
149 */
150 public void testExecuteNull() {
151 ScheduledExecutor se = null;
152 try {
153 se = new ScheduledExecutor(1);
154 se.execute(null);
155 shouldThrow();
156 } catch(NullPointerException success){}
157 catch(Exception e){
158 unexpectedException();
159 }
160
161 joinPool(se);
162 }
163
164 /**
165 * schedule (null) throws NPE
166 */
167 public void testScheduleNull() {
168 ScheduledExecutor se = new ScheduledExecutor(1);
169 try {
170 MyCallable callable = null;
171 Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
172 shouldThrow();
173 } catch(NullPointerException success){}
174 catch(Exception e){
175 unexpectedException();
176 }
177 joinPool(se);
178 }
179
180 /**
181 * execute throws RejectedExecutionException if shutdown
182 */
183 public void testSchedule1_RejectedExecutionException() {
184 ScheduledExecutor se = new ScheduledExecutor(1);
185 try {
186 se.shutdown();
187 se.schedule(new NoOpRunnable(),
188 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
189 shouldThrow();
190 } catch(RejectedExecutionException success){
191 }
192 joinPool(se);
193
194 }
195
196 /**
197 * schedule throws RejectedExecutionException if shutdown
198 */
199 public void testSchedule2_RejectedExecutionException() {
200 ScheduledExecutor se = new ScheduledExecutor(1);
201 try {
202 se.shutdown();
203 se.schedule(new NoOpCallable(),
204 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
205 shouldThrow();
206 } catch(RejectedExecutionException success){
207 }
208 joinPool(se);
209 }
210
211 /**
212 * schedule callable throws RejectedExecutionException if shutdown
213 */
214 public void testSchedule3_RejectedExecutionException() {
215 ScheduledExecutor se = new ScheduledExecutor(1);
216 try {
217 se.shutdown();
218 se.schedule(new NoOpCallable(),
219 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
220 shouldThrow();
221 } catch(RejectedExecutionException success){
222 }
223 joinPool(se);
224 }
225
226 /**
227 * scheduleAtFixedRate throws RejectedExecutionException if shutdown
228 */
229 public void testScheduleAtFixedRate1_RejectedExecutionException() {
230 ScheduledExecutor se = new ScheduledExecutor(1);
231 try {
232 se.shutdown();
233 se.scheduleAtFixedRate(new NoOpRunnable(),
234 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
235 shouldThrow();
236 } catch(RejectedExecutionException success){
237 }
238 joinPool(se);
239 }
240
241 /**
242 * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
243 */
244 public void testScheduleWithFixedDelay1_RejectedExecutionException() {
245 ScheduledExecutor se = new ScheduledExecutor(1);
246 try {
247 se.shutdown();
248 se.scheduleWithFixedDelay(new NoOpRunnable(),
249 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
250 shouldThrow();
251 } catch(RejectedExecutionException success){
252 }
253 joinPool(se);
254 }
255
256 /**
257 * getActiveCount increases but doesn't overestimate, when a
258 * thread becomes active
259 */
260 public void testGetActiveCount() {
261 ScheduledExecutor p2 = new ScheduledExecutor(2);
262 assertEquals(0, p2.getActiveCount());
263 p2.execute(new SmallRunnable());
264 try {
265 Thread.sleep(SHORT_DELAY_MS);
266 } catch(Exception e){
267 unexpectedException();
268 }
269 assertEquals(1, p2.getActiveCount());
270 joinPool(p2);
271 }
272
273 /**
274 * getCompletedTaskCount increases, but doesn't overestimate,
275 * when tasks complete
276 */
277 public void testGetCompletedTaskCount() {
278 ScheduledExecutor p2 = new ScheduledExecutor(2);
279 assertEquals(0, p2.getCompletedTaskCount());
280 p2.execute(new SmallRunnable());
281 try {
282 Thread.sleep(MEDIUM_DELAY_MS);
283 } catch(Exception e){
284 unexpectedException();
285 }
286 assertEquals(1, p2.getCompletedTaskCount());
287 joinPool(p2);
288 }
289
290 /**
291 * getCorePoolSize returns size given in constructor if not otherwise set
292 */
293 public void testGetCorePoolSize() {
294 ScheduledExecutor p1 = new ScheduledExecutor(1);
295 assertEquals(1, p1.getCorePoolSize());
296 joinPool(p1);
297 }
298
299 /**
300 * getLargestPoolSize increases, but doesn't overestimate, when
301 * multiple threads active
302 */
303 public void testGetLargestPoolSize() {
304 ScheduledExecutor p2 = new ScheduledExecutor(2);
305 assertEquals(0, p2.getLargestPoolSize());
306 p2.execute(new SmallRunnable());
307 p2.execute(new SmallRunnable());
308 try {
309 Thread.sleep(SHORT_DELAY_MS);
310 } catch(Exception e){
311 unexpectedException();
312 }
313 assertEquals(2, p2.getLargestPoolSize());
314 joinPool(p2);
315 }
316
317 /**
318 * getPoolSize increases, but doesn't overestimate, when threads
319 * become active
320 */
321 public void testGetPoolSize() {
322 ScheduledExecutor p1 = new ScheduledExecutor(1);
323 assertEquals(0, p1.getPoolSize());
324 p1.execute(new SmallRunnable());
325 assertEquals(1, p1.getPoolSize());
326 joinPool(p1);
327 }
328
329 /**
330 * getTaskCount increases, but doesn't overestimate, when tasks
331 * submitted
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 * is isShutDown is false before shutdown, true after
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 is false before termination, true after
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 is not true when running or when terminated
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 * purge removes cancelled tasks from the queue
404 */
405 public void testPurge() {
406 ScheduledExecutor p1 = new ScheduledExecutor(1);
407 ScheduledCancellable[] tasks = new ScheduledCancellable[5];
408 for(int i = 0; i < 5; i++){
409 tasks[i] = p1.schedule(new SmallRunnable(), 1, TimeUnit.MILLISECONDS);
410 }
411 int max = 5;
412 if (tasks[4].cancel(true)) --max;
413 if (tasks[3].cancel(true)) --max;
414 p1.purge();
415 long count = p1.getTaskCount();
416 assertTrue(count > 0 && count <= max);
417 joinPool(p1);
418 }
419
420 /**
421 * shutDownNow returns a list containing tasks that were not run
422 */
423 public void testShutDownNow() {
424 ScheduledExecutor p1 = new ScheduledExecutor(1);
425 for(int i = 0; i < 5; i++)
426 p1.schedule(new SmallRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
427 List l = p1.shutdownNow();
428 assertTrue(p1.isShutdown());
429 assertTrue(l.size() > 0 && l.size() <= 5);
430 joinPool(p1);
431 }
432
433 /**
434 * In default setting, shutdown cancels periodic but not delayed
435 * tasks at shutdown
436 */
437 public void testShutDown1() {
438 try {
439 ScheduledExecutor p1 = new ScheduledExecutor(1);
440 assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
441 assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
442
443 ScheduledCancellable[] tasks = new ScheduledCancellable[5];
444 for(int i = 0; i < 5; i++)
445 tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
446 p1.shutdown();
447 BlockingQueue q = p1.getQueue();
448 for (Iterator it = q.iterator(); it.hasNext();) {
449 ScheduledCancellable t = (ScheduledCancellable)it.next();
450 assertFalse(t.isCancelled());
451 }
452 assertTrue(p1.isShutdown());
453 Thread.sleep(SMALL_DELAY_MS);
454 for (int i = 0; i < 5; ++i) {
455 assertTrue(tasks[i].isDone());
456 assertFalse(tasks[i].isCancelled());
457 }
458
459 }
460 catch(Exception ex) {
461 unexpectedException();
462 }
463 }
464
465
466 /**
467 * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
468 * delayed tasks are cancelled at shutdown
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 * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
492 * periodic tasks are not cancelled at shutdown
493 */
494 public void testShutDown3() {
495 try {
496 ScheduledExecutor p1 = new ScheduledExecutor(1);
497 p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
498 ScheduledCancellable task =
499 p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
500 p1.shutdown();
501 assertTrue(p1.isShutdown());
502 BlockingQueue q = p1.getQueue();
503 assertTrue(q.isEmpty());
504 Thread.sleep(SHORT_DELAY_MS);
505 assertTrue(p1.isTerminated());
506 }
507 catch(Exception ex) {
508 unexpectedException();
509 }
510 }
511
512 /**
513 * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
514 * periodic tasks are cancelled at shutdown
515 */
516 public void testShutDown4() {
517 ScheduledExecutor p1 = new ScheduledExecutor(1);
518 try {
519 p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
520 ScheduledCancellable task =
521 p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
522 assertFalse(task.isCancelled());
523 p1.shutdown();
524 assertFalse(task.isCancelled());
525 assertFalse(p1.isTerminated());
526 assertTrue(p1.isShutdown());
527 Thread.sleep(SHORT_DELAY_MS);
528 assertFalse(task.isCancelled());
529 task.cancel(true);
530 assertTrue(task.isCancelled());
531 Thread.sleep(SHORT_DELAY_MS);
532 assertTrue(p1.isTerminated());
533 }
534 catch(Exception ex) {
535 unexpectedException();
536 }
537 finally {
538 p1.shutdownNow();
539 }
540 }
541
542 }