ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.18
Committed: Wed Jan 21 01:47:07 2004 UTC (20 years, 3 months ago) by dl
Branch: MAIN
Changes since 1.17: +51 -4 lines
Log Message:
Add/fix tests

File Contents

# Content
1 /*
2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
5 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 * Pat Fisher, Mike Judd.
7 */
8
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
12 import java.util.concurrent.atomic.*;
13
14 public class ScheduledExecutorTest extends JSR166TestCase {
15 public static void main(String[] args) {
16 junit.textui.TestRunner.run (suite());
17 }
18 public static Test suite() {
19 return new TestSuite(ScheduledExecutorTest.class);
20 }
21
22
23 /**
24 * execute successfully executes a runnable
25 */
26 public void testExecute() {
27 try {
28 TrackedShortRunnable runnable =new TrackedShortRunnable();
29 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
30 p1.execute(runnable);
31 assertFalse(runnable.done);
32 Thread.sleep(SHORT_DELAY_MS);
33 try { p1.shutdown(); } catch(SecurityException ok) { return; }
34 try {
35 Thread.sleep(MEDIUM_DELAY_MS);
36 } catch(InterruptedException e){
37 unexpectedException();
38 }
39 assertTrue(runnable.done);
40 try { p1.shutdown(); } catch(SecurityException ok) { return; }
41 joinPool(p1);
42 }
43 catch(Exception e){
44 unexpectedException();
45 }
46
47 }
48
49
50 /**
51 * delayed schedule of callable successfully executes after delay
52 */
53 public void testSchedule1() {
54 try {
55 TrackedCallable callable = new TrackedCallable();
56 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
57 Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
58 assertFalse(callable.done);
59 Thread.sleep(MEDIUM_DELAY_MS);
60 assertTrue(callable.done);
61 assertEquals(Boolean.TRUE, f.get());
62 try { p1.shutdown(); } catch(SecurityException ok) { return; }
63 joinPool(p1);
64 } catch(RejectedExecutionException e){}
65 catch(Exception e){
66 e.printStackTrace();
67 unexpectedException();
68 }
69 }
70
71 /**
72 * delayed schedule of runnable successfully executes after delay
73 */
74 public void testSchedule3() {
75 try {
76 TrackedShortRunnable runnable = new TrackedShortRunnable();
77 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
78 p1.schedule(runnable, SMALL_DELAY_MS, TimeUnit.MILLISECONDS);
79 Thread.sleep(SHORT_DELAY_MS);
80 assertFalse(runnable.done);
81 Thread.sleep(MEDIUM_DELAY_MS);
82 assertTrue(runnable.done);
83 try { p1.shutdown(); } catch(SecurityException ok) { return; }
84 joinPool(p1);
85 } catch(Exception e){
86 unexpectedException();
87 }
88 }
89
90 /**
91 * scheduleAtFixedRate executes runnable after given initial delay
92 */
93 public void testSchedule4() {
94 try {
95 TrackedShortRunnable runnable = new TrackedShortRunnable();
96 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
97 ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
98 assertFalse(runnable.done);
99 Thread.sleep(MEDIUM_DELAY_MS);
100 assertTrue(runnable.done);
101 h.cancel(true);
102 joinPool(p1);
103 } catch(Exception e){
104 unexpectedException();
105 }
106 }
107
108 static class RunnableCounter implements Runnable {
109 AtomicInteger count = new AtomicInteger(0);
110 public void run() { count.getAndIncrement(); }
111 }
112
113 /**
114 * scheduleWithFixedDelay executes runnable after given initial delay
115 */
116 public void testSchedule5() {
117 try {
118 TrackedShortRunnable runnable = new TrackedShortRunnable();
119 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
120 ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
121 assertFalse(runnable.done);
122 Thread.sleep(MEDIUM_DELAY_MS);
123 assertTrue(runnable.done);
124 h.cancel(true);
125 joinPool(p1);
126 } catch(Exception e){
127 unexpectedException();
128 }
129 }
130
131 /**
132 * scheduleAtFixedRate executes series of tasks at given rate
133 */
134 public void testFixedRateSequence() {
135 try {
136 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
137 RunnableCounter counter = new RunnableCounter();
138 ScheduledFuture h =
139 p1.scheduleAtFixedRate(counter, 0, 1, TimeUnit.MILLISECONDS);
140 Thread.sleep(SMALL_DELAY_MS);
141 h.cancel(true);
142 int c = counter.count.get();
143 // By time scaling conventions, we must have at least
144 // an execution per SHORT delay, but no more than one SHORT more
145 assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
146 assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
147 assertTrue(h.isDone());
148 joinPool(p1);
149 } catch(Exception e){
150 unexpectedException();
151 }
152 }
153
154 /**
155 * scheduleWithFixedDelay executes series of tasks with given period
156 */
157 public void testFixedDelaySequence() {
158 try {
159 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
160 RunnableCounter counter = new RunnableCounter();
161 ScheduledFuture h =
162 p1.scheduleWithFixedDelay(counter, 0, 1, TimeUnit.MILLISECONDS);
163 Thread.sleep(SMALL_DELAY_MS);
164 h.cancel(true);
165 int c = counter.count.get();
166 assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS);
167 assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS);
168 assertTrue(h.isDone());
169 joinPool(p1);
170 } catch(Exception e){
171 unexpectedException();
172 }
173 }
174
175
176 /**
177 * execute (null) throws NPE
178 */
179 public void testExecuteNull() {
180 ScheduledThreadPoolExecutor se = null;
181 try {
182 se = new ScheduledThreadPoolExecutor(1);
183 se.execute(null);
184 shouldThrow();
185 } catch(NullPointerException success){}
186 catch(Exception e){
187 unexpectedException();
188 }
189
190 joinPool(se);
191 }
192
193 /**
194 * schedule (null) throws NPE
195 */
196 public void testScheduleNull() {
197 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
198 try {
199 TrackedCallable callable = null;
200 Future f = se.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
201 shouldThrow();
202 } catch(NullPointerException success){}
203 catch(Exception e){
204 unexpectedException();
205 }
206 joinPool(se);
207 }
208
209 /**
210 * execute throws RejectedExecutionException if shutdown
211 */
212 public void testSchedule1_RejectedExecutionException() {
213 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
214 try {
215 se.shutdown();
216 se.schedule(new NoOpRunnable(),
217 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
218 shouldThrow();
219 } catch(RejectedExecutionException success){
220 } catch (SecurityException ok) {
221 }
222
223 joinPool(se);
224
225 }
226
227 /**
228 * schedule throws RejectedExecutionException if shutdown
229 */
230 public void testSchedule2_RejectedExecutionException() {
231 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
232 try {
233 se.shutdown();
234 se.schedule(new NoOpCallable(),
235 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
236 shouldThrow();
237 } catch(RejectedExecutionException success){
238 } catch (SecurityException ok) {
239 }
240 joinPool(se);
241 }
242
243 /**
244 * schedule callable throws RejectedExecutionException if shutdown
245 */
246 public void testSchedule3_RejectedExecutionException() {
247 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
248 try {
249 se.shutdown();
250 se.schedule(new NoOpCallable(),
251 MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
252 shouldThrow();
253 } catch(RejectedExecutionException success){
254 } catch (SecurityException ok) {
255 }
256 joinPool(se);
257 }
258
259 /**
260 * scheduleAtFixedRate throws RejectedExecutionException if shutdown
261 */
262 public void testScheduleAtFixedRate1_RejectedExecutionException() {
263 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
264 try {
265 se.shutdown();
266 se.scheduleAtFixedRate(new NoOpRunnable(),
267 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
268 shouldThrow();
269 } catch(RejectedExecutionException success){
270 } catch (SecurityException ok) {
271 }
272 joinPool(se);
273 }
274
275 /**
276 * scheduleWithFixedDelay throws RejectedExecutionException if shutdown
277 */
278 public void testScheduleWithFixedDelay1_RejectedExecutionException() {
279 ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1);
280 try {
281 se.shutdown();
282 se.scheduleWithFixedDelay(new NoOpRunnable(),
283 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
284 shouldThrow();
285 } catch(RejectedExecutionException success){
286 } catch (SecurityException ok) {
287 }
288 joinPool(se);
289 }
290
291 /**
292 * getActiveCount increases but doesn't overestimate, when a
293 * thread becomes active
294 */
295 public void testGetActiveCount() {
296 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
297 assertEquals(0, p2.getActiveCount());
298 p2.execute(new SmallRunnable());
299 try {
300 Thread.sleep(SHORT_DELAY_MS);
301 } catch(Exception e){
302 unexpectedException();
303 }
304 assertEquals(1, p2.getActiveCount());
305 joinPool(p2);
306 }
307
308 /**
309 * getCompletedTaskCount increases, but doesn't overestimate,
310 * when tasks complete
311 */
312 public void testGetCompletedTaskCount() {
313 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
314 assertEquals(0, p2.getCompletedTaskCount());
315 p2.execute(new SmallRunnable());
316 try {
317 Thread.sleep(MEDIUM_DELAY_MS);
318 } catch(Exception e){
319 unexpectedException();
320 }
321 assertEquals(1, p2.getCompletedTaskCount());
322 joinPool(p2);
323 }
324
325 /**
326 * getCorePoolSize returns size given in constructor if not otherwise set
327 */
328 public void testGetCorePoolSize() {
329 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
330 assertEquals(1, p1.getCorePoolSize());
331 joinPool(p1);
332 }
333
334 /**
335 * getLargestPoolSize increases, but doesn't overestimate, when
336 * multiple threads active
337 */
338 public void testGetLargestPoolSize() {
339 ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2);
340 assertEquals(0, p2.getLargestPoolSize());
341 p2.execute(new SmallRunnable());
342 p2.execute(new SmallRunnable());
343 try {
344 Thread.sleep(SHORT_DELAY_MS);
345 } catch(Exception e){
346 unexpectedException();
347 }
348 assertEquals(2, p2.getLargestPoolSize());
349 joinPool(p2);
350 }
351
352 /**
353 * getPoolSize increases, but doesn't overestimate, when threads
354 * become active
355 */
356 public void testGetPoolSize() {
357 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
358 assertEquals(0, p1.getPoolSize());
359 p1.execute(new SmallRunnable());
360 assertEquals(1, p1.getPoolSize());
361 joinPool(p1);
362 }
363
364 /**
365 * getTaskCount increases, but doesn't overestimate, when tasks
366 * submitted
367 */
368 public void testGetTaskCount() {
369 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
370 assertEquals(0, p1.getTaskCount());
371 for(int i = 0; i < 5; i++)
372 p1.execute(new SmallRunnable());
373 try {
374 Thread.sleep(SHORT_DELAY_MS);
375 } catch(Exception e){
376 unexpectedException();
377 }
378 assertEquals(5, p1.getTaskCount());
379 joinPool(p1);
380 }
381
382 /**
383 * getThreadFactory returns factory in constructor if not set
384 */
385 public void testGetThreadFactory() {
386 ThreadFactory tf = new SimpleThreadFactory();
387 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf);
388 assertSame(tf, p.getThreadFactory());
389 joinPool(p);
390 }
391
392 /**
393 * setThreadFactory sets the thread factory returned by getThreadFactory
394 */
395 public void testSetThreadFactory() {
396 ThreadFactory tf = new SimpleThreadFactory();
397 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
398 p.setThreadFactory(tf);
399 assertSame(tf, p.getThreadFactory());
400 joinPool(p);
401 }
402
403 /**
404 * setThreadFactory(null) throws NPE
405 */
406 public void testSetThreadFactoryNull() {
407 ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1);
408 try {
409 p.setThreadFactory(null);
410 shouldThrow();
411 } catch (NullPointerException success) {
412 } finally {
413 joinPool(p);
414 }
415 }
416
417 /**
418 * is isShutDown is false before shutdown, true after
419 */
420 public void testIsShutdown() {
421
422 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
423 try {
424 assertFalse(p1.isShutdown());
425 }
426 finally {
427 try { p1.shutdown(); } catch(SecurityException ok) { return; }
428 }
429 assertTrue(p1.isShutdown());
430 }
431
432
433 /**
434 * isTerminated is false before termination, true after
435 */
436 public void testIsTerminated() {
437 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
438 try {
439 p1.execute(new SmallRunnable());
440 } finally {
441 try { p1.shutdown(); } catch(SecurityException ok) { return; }
442 }
443 try {
444 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
445 assertTrue(p1.isTerminated());
446 } catch(Exception e){
447 unexpectedException();
448 }
449 }
450
451 /**
452 * isTerminating is not true when running or when terminated
453 */
454 public void testIsTerminating() {
455 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
456 assertFalse(p1.isTerminating());
457 try {
458 p1.execute(new SmallRunnable());
459 assertFalse(p1.isTerminating());
460 } finally {
461 try { p1.shutdown(); } catch(SecurityException ok) { return; }
462 }
463 try {
464 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
465 assertTrue(p1.isTerminated());
466 assertFalse(p1.isTerminating());
467 } catch(Exception e){
468 unexpectedException();
469 }
470 }
471
472 /**
473 * getQueue returns the work queue, which contains queued tasks
474 */
475 public void testGetQueue() {
476 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
477 ScheduledFuture[] tasks = new ScheduledFuture[5];
478 for(int i = 0; i < 5; i++){
479 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
480 }
481 try {
482 Thread.sleep(SHORT_DELAY_MS);
483 BlockingQueue<Runnable> q = p1.getQueue();
484 assertTrue(q.contains(tasks[4]));
485 assertFalse(q.contains(tasks[0]));
486 } catch(Exception e) {
487 unexpectedException();
488 } finally {
489 joinPool(p1);
490 }
491 }
492
493 /**
494 * remove(task) removes queued task, and fails to remove active task
495 */
496 public void testRemove() {
497 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
498 ScheduledFuture[] tasks = new ScheduledFuture[5];
499 for(int i = 0; i < 5; i++){
500 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
501 }
502 try {
503 Thread.sleep(SHORT_DELAY_MS);
504 BlockingQueue<Runnable> q = p1.getQueue();
505 assertFalse(p1.remove((Runnable)tasks[0]));
506 assertTrue(q.contains((Runnable)tasks[4]));
507 assertTrue(q.contains((Runnable)tasks[3]));
508 assertTrue(p1.remove((Runnable)tasks[4]));
509 assertFalse(p1.remove((Runnable)tasks[4]));
510 assertFalse(q.contains((Runnable)tasks[4]));
511 assertTrue(q.contains((Runnable)tasks[3]));
512 assertTrue(p1.remove((Runnable)tasks[3]));
513 assertFalse(q.contains((Runnable)tasks[3]));
514 } catch(Exception e) {
515 unexpectedException();
516 } finally {
517 joinPool(p1);
518 }
519 }
520
521 /**
522 * purge removes cancelled tasks from the queue
523 */
524 public void testPurge() {
525 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
526 ScheduledFuture[] tasks = new ScheduledFuture[5];
527 for(int i = 0; i < 5; i++){
528 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, TimeUnit.MILLISECONDS);
529 }
530 int max = 5;
531 if (tasks[4].cancel(true)) --max;
532 if (tasks[3].cancel(true)) --max;
533 p1.purge();
534 long count = p1.getTaskCount();
535 assertTrue(count > 0 && count <= max);
536 joinPool(p1);
537 }
538
539 /**
540 * shutDownNow returns a list containing tasks that were not run
541 */
542 public void testShutDownNow() {
543 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
544 for(int i = 0; i < 5; i++)
545 p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
546 List l;
547 try {
548 l = p1.shutdownNow();
549 } catch (SecurityException ok) {
550 return;
551 }
552 assertTrue(p1.isShutdown());
553 assertTrue(l.size() > 0 && l.size() <= 5);
554 joinPool(p1);
555 }
556
557 /**
558 * In default setting, shutdown cancels periodic but not delayed
559 * tasks at shutdown
560 */
561 public void testShutDown1() {
562 try {
563 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
564 assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
565 assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
566
567 ScheduledFuture[] tasks = new ScheduledFuture[5];
568 for(int i = 0; i < 5; i++)
569 tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
570 try { p1.shutdown(); } catch(SecurityException ok) { return; }
571 BlockingQueue q = p1.getQueue();
572 for (Iterator it = q.iterator(); it.hasNext();) {
573 ScheduledFuture t = (ScheduledFuture)it.next();
574 assertFalse(t.isCancelled());
575 }
576 assertTrue(p1.isShutdown());
577 Thread.sleep(SMALL_DELAY_MS);
578 for (int i = 0; i < 5; ++i) {
579 assertTrue(tasks[i].isDone());
580 assertFalse(tasks[i].isCancelled());
581 }
582
583 }
584 catch(Exception ex) {
585 unexpectedException();
586 }
587 }
588
589
590 /**
591 * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
592 * delayed tasks are cancelled at shutdown
593 */
594 public void testShutDown2() {
595 try {
596 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
597 p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
598 ScheduledFuture[] tasks = new ScheduledFuture[5];
599 for(int i = 0; i < 5; i++)
600 tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
601 try { p1.shutdown(); } catch(SecurityException ok) { return; }
602 assertTrue(p1.isShutdown());
603 BlockingQueue q = p1.getQueue();
604 assertTrue(q.isEmpty());
605 Thread.sleep(SMALL_DELAY_MS);
606 assertTrue(p1.isTerminated());
607 }
608 catch(Exception ex) {
609 unexpectedException();
610 }
611 }
612
613
614 /**
615 * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
616 * periodic tasks are not cancelled at shutdown
617 */
618 public void testShutDown3() {
619 try {
620 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
621 p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
622 ScheduledFuture task =
623 p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, TimeUnit.MILLISECONDS);
624 try { p1.shutdown(); } catch(SecurityException ok) { return; }
625 assertTrue(p1.isShutdown());
626 BlockingQueue q = p1.getQueue();
627 assertTrue(q.isEmpty());
628 Thread.sleep(SHORT_DELAY_MS);
629 assertTrue(p1.isTerminated());
630 }
631 catch(Exception ex) {
632 unexpectedException();
633 }
634 }
635
636 /**
637 * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
638 * periodic tasks are cancelled at shutdown
639 */
640 public void testShutDown4() {
641 ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1);
642 try {
643 p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
644 ScheduledFuture task =
645 p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, TimeUnit.MILLISECONDS);
646 assertFalse(task.isCancelled());
647 try { p1.shutdown(); } catch(SecurityException ok) { return; }
648 assertFalse(task.isCancelled());
649 assertFalse(p1.isTerminated());
650 assertTrue(p1.isShutdown());
651 Thread.sleep(SHORT_DELAY_MS);
652 assertFalse(task.isCancelled());
653 assertTrue(task.cancel(true));
654 assertTrue(task.isDone());
655 Thread.sleep(SHORT_DELAY_MS);
656 assertTrue(p1.isTerminated());
657 }
658 catch(Exception ex) {
659 unexpectedException();
660 }
661 finally {
662 joinPool(p1);
663 }
664 }
665
666 /**
667 * completed submit of callable returns result
668 */
669 public void testSubmitCallable() {
670 ExecutorService e = new ScheduledThreadPoolExecutor(2);
671 try {
672 Future<String> future = e.submit(new StringTask());
673 String result = future.get();
674 assertSame(TEST_STRING, result);
675 }
676 catch (ExecutionException ex) {
677 unexpectedException();
678 }
679 catch (InterruptedException ex) {
680 unexpectedException();
681 } finally {
682 joinPool(e);
683 }
684 }
685
686 /**
687 * completed submit of runnable returns successfully
688 */
689 public void testSubmitRunnable() {
690 ExecutorService e = new ScheduledThreadPoolExecutor(2);
691 try {
692 Future<?> future = e.submit(new NoOpRunnable());
693 future.get();
694 assertTrue(future.isDone());
695 }
696 catch (ExecutionException ex) {
697 unexpectedException();
698 }
699 catch (InterruptedException ex) {
700 unexpectedException();
701 } finally {
702 joinPool(e);
703 }
704 }
705
706 /**
707 * completed submit of (runnable, result) returns result
708 */
709 public void testSubmitRunnable2() {
710 ExecutorService e = new ScheduledThreadPoolExecutor(2);
711 try {
712 Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
713 String result = future.get();
714 assertSame(TEST_STRING, result);
715 }
716 catch (ExecutionException ex) {
717 unexpectedException();
718 }
719 catch (InterruptedException ex) {
720 unexpectedException();
721 } finally {
722 joinPool(e);
723 }
724 }
725
726 /**
727 * invokeAny(null) throws NPE
728 */
729 public void testInvokeAny1() {
730 ExecutorService e = new ScheduledThreadPoolExecutor(2);
731 try {
732 e.invokeAny(null);
733 } catch (NullPointerException success) {
734 } catch(Exception ex) {
735 unexpectedException();
736 } finally {
737 joinPool(e);
738 }
739 }
740
741 /**
742 * invokeAny(empty collection) throws IAE
743 */
744 public void testInvokeAny2() {
745 ExecutorService e = new ScheduledThreadPoolExecutor(2);
746 try {
747 e.invokeAny(new ArrayList<Callable<String>>());
748 } catch (IllegalArgumentException success) {
749 } catch(Exception ex) {
750 unexpectedException();
751 } finally {
752 joinPool(e);
753 }
754 }
755
756 /**
757 * invokeAny(c) throws NPE if c has null elements
758 */
759 public void testInvokeAny3() {
760 ExecutorService e = new ScheduledThreadPoolExecutor(2);
761 try {
762 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
763 l.add(new StringTask());
764 l.add(null);
765 e.invokeAny(l);
766 } catch (NullPointerException success) {
767 } catch(Exception ex) {
768 unexpectedException();
769 } finally {
770 joinPool(e);
771 }
772 }
773
774 /**
775 * invokeAny(c) throws ExecutionException if no task completes
776 */
777 public void testInvokeAny4() {
778 ExecutorService e = new ScheduledThreadPoolExecutor(2);
779 try {
780 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
781 l.add(new NPETask());
782 e.invokeAny(l);
783 } catch (ExecutionException success) {
784 } catch(Exception ex) {
785 unexpectedException();
786 } finally {
787 joinPool(e);
788 }
789 }
790
791 /**
792 * invokeAny(c) returns result of some task
793 */
794 public void testInvokeAny5() {
795 ExecutorService e = new ScheduledThreadPoolExecutor(2);
796 try {
797 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
798 l.add(new StringTask());
799 l.add(new StringTask());
800 String result = e.invokeAny(l);
801 assertSame(TEST_STRING, result);
802 } catch (ExecutionException success) {
803 } catch(Exception ex) {
804 unexpectedException();
805 } finally {
806 joinPool(e);
807 }
808 }
809
810 /**
811 * invokeAll(null) throws NPE
812 */
813 public void testInvokeAll1() {
814 ExecutorService e = new ScheduledThreadPoolExecutor(2);
815 try {
816 e.invokeAll(null);
817 } catch (NullPointerException success) {
818 } catch(Exception ex) {
819 unexpectedException();
820 } finally {
821 joinPool(e);
822 }
823 }
824
825 /**
826 * invokeAll(empty collection) returns empty collection
827 */
828 public void testInvokeAll2() {
829 ExecutorService e = new ScheduledThreadPoolExecutor(2);
830 try {
831 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
832 assertTrue(r.isEmpty());
833 } catch(Exception ex) {
834 unexpectedException();
835 } finally {
836 joinPool(e);
837 }
838 }
839
840 /**
841 * invokeAll(c) throws NPE if c has null elements
842 */
843 public void testInvokeAll3() {
844 ExecutorService e = new ScheduledThreadPoolExecutor(2);
845 try {
846 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
847 l.add(new StringTask());
848 l.add(null);
849 e.invokeAll(l);
850 } catch (NullPointerException success) {
851 } catch(Exception ex) {
852 unexpectedException();
853 } finally {
854 joinPool(e);
855 }
856 }
857
858 /**
859 * get of invokeAll(c) throws exception on failed task
860 */
861 public void testInvokeAll4() {
862 ExecutorService e = new ScheduledThreadPoolExecutor(2);
863 try {
864 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
865 l.add(new NPETask());
866 List<Future<String>> result = e.invokeAll(l);
867 assertEquals(1, result.size());
868 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
869 it.next().get();
870 } catch(ExecutionException success) {
871 } catch(Exception ex) {
872 unexpectedException();
873 } finally {
874 joinPool(e);
875 }
876 }
877
878 /**
879 * invokeAll(c) returns results of all completed tasks
880 */
881 public void testInvokeAll5() {
882 ExecutorService e = new ScheduledThreadPoolExecutor(2);
883 try {
884 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
885 l.add(new StringTask());
886 l.add(new StringTask());
887 List<Future<String>> result = e.invokeAll(l);
888 assertEquals(2, result.size());
889 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
890 assertSame(TEST_STRING, it.next().get());
891 } catch (ExecutionException success) {
892 } catch(Exception ex) {
893 unexpectedException();
894 } finally {
895 joinPool(e);
896 }
897 }
898
899 /**
900 * timed invokeAny(null) throws NPE
901 */
902 public void testTimedInvokeAny1() {
903 ExecutorService e = new ScheduledThreadPoolExecutor(2);
904 try {
905 e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
906 } catch (NullPointerException success) {
907 } catch(Exception ex) {
908 unexpectedException();
909 } finally {
910 joinPool(e);
911 }
912 }
913
914 /**
915 * timed invokeAny(,,null) throws NPE
916 */
917 public void testTimedInvokeAnyNullTimeUnit() {
918 ExecutorService e = new ScheduledThreadPoolExecutor(2);
919 try {
920 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
921 l.add(new StringTask());
922 e.invokeAny(l, MEDIUM_DELAY_MS, null);
923 } catch (NullPointerException success) {
924 } catch(Exception ex) {
925 unexpectedException();
926 } finally {
927 joinPool(e);
928 }
929 }
930
931 /**
932 * timed invokeAny(empty collection) throws IAE
933 */
934 public void testTimedInvokeAny2() {
935 ExecutorService e = new ScheduledThreadPoolExecutor(2);
936 try {
937 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
938 } catch (IllegalArgumentException success) {
939 } catch(Exception ex) {
940 unexpectedException();
941 } finally {
942 joinPool(e);
943 }
944 }
945
946 /**
947 * timed invokeAny(c) throws NPE if c has null elements
948 */
949 public void testTimedInvokeAny3() {
950 ExecutorService e = new ScheduledThreadPoolExecutor(2);
951 try {
952 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
953 l.add(new StringTask());
954 l.add(null);
955 e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
956 } catch (NullPointerException success) {
957 } catch(Exception ex) {
958 ex.printStackTrace();
959 unexpectedException();
960 } finally {
961 joinPool(e);
962 }
963 }
964
965 /**
966 * timed invokeAny(c) throws ExecutionException if no task completes
967 */
968 public void testTimedInvokeAny4() {
969 ExecutorService e = new ScheduledThreadPoolExecutor(2);
970 try {
971 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
972 l.add(new NPETask());
973 e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
974 } catch(ExecutionException success) {
975 } catch(Exception ex) {
976 unexpectedException();
977 } finally {
978 joinPool(e);
979 }
980 }
981
982 /**
983 * timed invokeAny(c) returns result of some task
984 */
985 public void testTimedInvokeAny5() {
986 ExecutorService e = new ScheduledThreadPoolExecutor(2);
987 try {
988 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
989 l.add(new StringTask());
990 l.add(new StringTask());
991 String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
992 assertSame(TEST_STRING, result);
993 } catch (ExecutionException success) {
994 } catch(Exception ex) {
995 unexpectedException();
996 } finally {
997 joinPool(e);
998 }
999 }
1000
1001 /**
1002 * timed invokeAll(null) throws NPE
1003 */
1004 public void testTimedInvokeAll1() {
1005 ExecutorService e = new ScheduledThreadPoolExecutor(2);
1006 try {
1007 e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1008 } catch (NullPointerException success) {
1009 } catch(Exception ex) {
1010 unexpectedException();
1011 } finally {
1012 joinPool(e);
1013 }
1014 }
1015
1016 /**
1017 * timed invokeAll(,,null) throws NPE
1018 */
1019 public void testTimedInvokeAllNullTimeUnit() {
1020 ExecutorService e = new ScheduledThreadPoolExecutor(2);
1021 try {
1022 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1023 l.add(new StringTask());
1024 e.invokeAll(l, MEDIUM_DELAY_MS, null);
1025 } catch (NullPointerException success) {
1026 } catch(Exception ex) {
1027 unexpectedException();
1028 } finally {
1029 joinPool(e);
1030 }
1031 }
1032
1033 /**
1034 * timed invokeAll(empty collection) returns empty collection
1035 */
1036 public void testTimedInvokeAll2() {
1037 ExecutorService e = new ScheduledThreadPoolExecutor(2);
1038 try {
1039 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1040 assertTrue(r.isEmpty());
1041 } catch(Exception ex) {
1042 unexpectedException();
1043 } finally {
1044 joinPool(e);
1045 }
1046 }
1047
1048 /**
1049 * timed invokeAll(c) throws NPE if c has null elements
1050 */
1051 public void testTimedInvokeAll3() {
1052 ExecutorService e = new ScheduledThreadPoolExecutor(2);
1053 try {
1054 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1055 l.add(new StringTask());
1056 l.add(null);
1057 e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1058 } catch (NullPointerException success) {
1059 } catch(Exception ex) {
1060 unexpectedException();
1061 } finally {
1062 joinPool(e);
1063 }
1064 }
1065
1066 /**
1067 * get of element of invokeAll(c) throws exception on failed task
1068 */
1069 public void testTimedInvokeAll4() {
1070 ExecutorService e = new ScheduledThreadPoolExecutor(2);
1071 try {
1072 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1073 l.add(new NPETask());
1074 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1075 assertEquals(1, result.size());
1076 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1077 it.next().get();
1078 } catch(ExecutionException success) {
1079 } catch(Exception ex) {
1080 unexpectedException();
1081 } finally {
1082 joinPool(e);
1083 }
1084 }
1085
1086 /**
1087 * timed invokeAll(c) returns results of all completed tasks
1088 */
1089 public void testTimedInvokeAll5() {
1090 ExecutorService e = new ScheduledThreadPoolExecutor(2);
1091 try {
1092 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1093 l.add(new StringTask());
1094 l.add(new StringTask());
1095 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1096 assertEquals(2, result.size());
1097 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1098 assertSame(TEST_STRING, it.next().get());
1099 } catch (ExecutionException success) {
1100 } catch(Exception ex) {
1101 unexpectedException();
1102 } finally {
1103 joinPool(e);
1104 }
1105 }
1106
1107 /**
1108 * timed invokeAll(c) cancels tasks not completed by timeout
1109 */
1110 public void testTimedInvokeAll6() {
1111 ExecutorService e = new ScheduledThreadPoolExecutor(2);
1112 try {
1113 ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1114 l.add(new StringTask());
1115 l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1116 l.add(new StringTask());
1117 List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1118 assertEquals(3, result.size());
1119 Iterator<Future<String>> it = result.iterator();
1120 Future<String> f1 = it.next();
1121 Future<String> f2 = it.next();
1122 Future<String> f3 = it.next();
1123 assertTrue(f1.isDone());
1124 assertTrue(f2.isDone());
1125 assertTrue(f3.isDone());
1126 assertFalse(f1.isCancelled());
1127 assertTrue(f2.isCancelled());
1128 } catch(Exception ex) {
1129 unexpectedException();
1130 } finally {
1131 joinPool(e);
1132 }
1133 }
1134
1135
1136 }