ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorSubclassTest.java
Revision: 1.6
Committed: Fri Nov 20 22:58:48 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.5: +238 -366 lines
Log Message:
improve exception handling

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