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