ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorSubclassTest.java
Revision: 1.10
Committed: Tue Dec 1 09:48:12 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.9: +0 -1 lines
Log Message:
whitespace

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