ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorTest.java
Revision: 1.14
Committed: Sun Dec 28 21:56:18 2003 UTC (20 years, 4 months ago) by dl
Branch: MAIN
Changes since 1.13: +5 -2 lines
Log Message:
Add tests for AQS extensions; adjust others for protected condition methods

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