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

File Contents

# User Rev Content
1 dl 1.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 jsr166 1.6 import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 dl 1.1 import java.util.concurrent.atomic.*;
12    
13     public class ScheduledExecutorSubclassTest extends JSR166TestCase {
14     public static void main(String[] args) {
15 jsr166 1.7 junit.textui.TestRunner.run (suite());
16 dl 1.1 }
17     public static Test suite() {
18 jsr166 1.7 return new TestSuite(ScheduledExecutorSubclassTest.class);
19 dl 1.1 }
20    
21 jsr166 1.2 static class CustomTask<V> implements RunnableScheduledFuture<V> {
22 dl 1.1 RunnableScheduledFuture<V> task;
23     volatile boolean ran;
24     CustomTask(RunnableScheduledFuture<V> t) { task = t; }
25     public boolean isPeriodic() { return task.isPeriodic(); }
26 jsr166 1.2 public void run() {
27 dl 1.1 ran = true;
28 jsr166 1.2 task.run();
29 dl 1.1 }
30     public long getDelay(TimeUnit unit) { return task.getDelay(unit); }
31     public int compareTo(Delayed t) {
32 jsr166 1.2 return task.compareTo(((CustomTask)t).task);
33 dl 1.1 }
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 jsr166 1.2
51 dl 1.1
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 jsr166 1.2 CustomExecutor(int corePoolSize, ThreadFactory threadFactory,
70 dl 1.1 RejectedExecutionHandler handler) {
71     super(corePoolSize, threadFactory, handler);
72     }
73 jsr166 1.2
74 dl 1.1 }
75 jsr166 1.2
76 dl 1.1
77    
78     /**
79     * execute successfully executes a runnable
80     */
81 jsr166 1.6 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 dl 1.1 }
93    
94    
95     /**
96     * delayed schedule of callable successfully executes after delay
97     */
98 jsr166 1.6 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 dl 1.1 }
109    
110     /**
111     * delayed schedule of runnable successfully executes after delay
112     */
113 jsr166 1.6 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 dl 1.1 }
124 jsr166 1.2
125 dl 1.1 /**
126     * scheduleAtFixedRate executes runnable after given initial delay
127     */
128 jsr166 1.6 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 dl 1.1 }
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 jsr166 1.6 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 dl 1.1 }
157 jsr166 1.2
158 dl 1.1 /**
159     * scheduleAtFixedRate executes series of tasks at given rate
160     */
161 jsr166 1.6 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 dl 1.1 }
175    
176     /**
177     * scheduleWithFixedDelay executes series of tasks with given period
178     */
179 jsr166 1.6 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 dl 1.1 }
191    
192    
193     /**
194     * execute (null) throws NPE
195     */
196 jsr166 1.6 public void testExecuteNull() throws InterruptedException {
197     CustomExecutor se = new CustomExecutor(1);
198 dl 1.1 try {
199 jsr166 1.7 se.execute(null);
200 dl 1.1 shouldThrow();
201 jsr166 1.7 } catch (NullPointerException success) {}
202     joinPool(se);
203 dl 1.1 }
204    
205     /**
206     * schedule (null) throws NPE
207     */
208 jsr166 1.6 public void testScheduleNull() throws InterruptedException {
209 dl 1.1 CustomExecutor se = new CustomExecutor(1);
210 jsr166 1.7 try {
211 dl 1.1 TrackedCallable callable = null;
212 jsr166 1.7 Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
213 dl 1.1 shouldThrow();
214 jsr166 1.7 } catch (NullPointerException success) {}
215     joinPool(se);
216 dl 1.1 }
217 jsr166 1.2
218 dl 1.1 /**
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 jsr166 1.6 MEDIUM_DELAY_MS, MILLISECONDS);
227 dl 1.1 shouldThrow();
228 jsr166 1.4 } catch (RejectedExecutionException success) {
229 dl 1.1 } catch (SecurityException ok) {
230     }
231 jsr166 1.2
232 dl 1.1 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 jsr166 1.6 MEDIUM_DELAY_MS, MILLISECONDS);
244 dl 1.1 shouldThrow();
245 jsr166 1.4 } catch (RejectedExecutionException success) {
246 dl 1.1 } 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 jsr166 1.6 se.shutdown();
258     se.schedule(new NoOpCallable(),
259     MEDIUM_DELAY_MS, MILLISECONDS);
260     shouldThrow();
261     } catch (RejectedExecutionException success) {
262     } catch (SecurityException ok) {
263     }
264 dl 1.1 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 jsr166 1.6 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
276 dl 1.1 shouldThrow();
277 jsr166 1.4 } catch (RejectedExecutionException success) {
278 dl 1.1 } catch (SecurityException ok) {
279 jsr166 1.2 }
280 dl 1.1 joinPool(se);
281     }
282 jsr166 1.2
283 dl 1.1 /**
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 jsr166 1.6 MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS);
292 dl 1.1 shouldThrow();
293 jsr166 1.4 } catch (RejectedExecutionException success) {
294 dl 1.1 } catch (SecurityException ok) {
295 jsr166 1.2 }
296 dl 1.1 joinPool(se);
297     }
298    
299     /**
300     * getActiveCount increases but doesn't overestimate, when a
301     * thread becomes active
302     */
303 jsr166 1.6 public void testGetActiveCount() throws InterruptedException {
304 dl 1.1 CustomExecutor p2 = new CustomExecutor(2);
305     assertEquals(0, p2.getActiveCount());
306     p2.execute(new SmallRunnable());
307 jsr166 1.6 Thread.sleep(SHORT_DELAY_MS);
308 dl 1.1 assertEquals(1, p2.getActiveCount());
309     joinPool(p2);
310     }
311 jsr166 1.2
312 dl 1.1 /**
313     * getCompletedTaskCount increases, but doesn't overestimate,
314     * when tasks complete
315     */
316 jsr166 1.9 public void testGetCompletedTaskCount() throws InterruptedException {
317 dl 1.1 CustomExecutor p2 = new CustomExecutor(2);
318     assertEquals(0, p2.getCompletedTaskCount());
319     p2.execute(new SmallRunnable());
320 jsr166 1.6 Thread.sleep(MEDIUM_DELAY_MS);
321 dl 1.1 assertEquals(1, p2.getCompletedTaskCount());
322     joinPool(p2);
323     }
324 jsr166 1.2
325 dl 1.1 /**
326 jsr166 1.2 * getCorePoolSize returns size given in constructor if not otherwise set
327 dl 1.1 */
328     public void testGetCorePoolSize() {
329     CustomExecutor p1 = new CustomExecutor(1);
330     assertEquals(1, p1.getCorePoolSize());
331     joinPool(p1);
332     }
333 jsr166 1.2
334 dl 1.1 /**
335     * getLargestPoolSize increases, but doesn't overestimate, when
336     * multiple threads active
337     */
338 jsr166 1.6 public void testGetLargestPoolSize() throws InterruptedException {
339 dl 1.1 CustomExecutor p2 = new CustomExecutor(2);
340     assertEquals(0, p2.getLargestPoolSize());
341     p2.execute(new SmallRunnable());
342     p2.execute(new SmallRunnable());
343 jsr166 1.6 Thread.sleep(SHORT_DELAY_MS);
344 dl 1.1 assertEquals(2, p2.getLargestPoolSize());
345     joinPool(p2);
346     }
347 jsr166 1.2
348 dl 1.1 /**
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 jsr166 1.2
360 dl 1.1 /**
361     * getTaskCount increases, but doesn't overestimate, when tasks
362     * submitted
363     */
364 jsr166 1.6 public void testGetTaskCount() throws InterruptedException {
365 dl 1.1 CustomExecutor p1 = new CustomExecutor(1);
366     assertEquals(0, p1.getTaskCount());
367 jsr166 1.3 for (int i = 0; i < 5; i++)
368 dl 1.1 p1.execute(new SmallRunnable());
369 jsr166 1.6 Thread.sleep(SHORT_DELAY_MS);
370 dl 1.1 assertEquals(5, p1.getTaskCount());
371     joinPool(p1);
372     }
373    
374 jsr166 1.2 /**
375 dl 1.1 * getThreadFactory returns factory in constructor if not set
376     */
377     public void testGetThreadFactory() {
378     ThreadFactory tf = new SimpleThreadFactory();
379 jsr166 1.7 CustomExecutor p = new CustomExecutor(1, tf);
380 dl 1.1 assertSame(tf, p.getThreadFactory());
381     joinPool(p);
382     }
383    
384 jsr166 1.2 /**
385 dl 1.1 * setThreadFactory sets the thread factory returned by getThreadFactory
386     */
387     public void testSetThreadFactory() {
388     ThreadFactory tf = new SimpleThreadFactory();
389 jsr166 1.7 CustomExecutor p = new CustomExecutor(1);
390 dl 1.1 p.setThreadFactory(tf);
391     assertSame(tf, p.getThreadFactory());
392     joinPool(p);
393     }
394    
395 jsr166 1.2 /**
396 dl 1.1 * setThreadFactory(null) throws NPE
397     */
398     public void testSetThreadFactoryNull() {
399 jsr166 1.7 CustomExecutor p = new CustomExecutor(1);
400 dl 1.1 try {
401     p.setThreadFactory(null);
402     shouldThrow();
403     } catch (NullPointerException success) {
404     } finally {
405     joinPool(p);
406     }
407     }
408 jsr166 1.2
409 dl 1.1 /**
410     * is isShutDown is false before shutdown, true after
411     */
412     public void testIsShutdown() {
413 jsr166 1.7 CustomExecutor p1 = new CustomExecutor(1);
414 dl 1.1 try {
415     assertFalse(p1.isShutdown());
416     }
417     finally {
418 jsr166 1.3 try { p1.shutdown(); } catch (SecurityException ok) { return; }
419 dl 1.1 }
420 jsr166 1.7 assertTrue(p1.isShutdown());
421 dl 1.1 }
422    
423 jsr166 1.2
424 dl 1.1 /**
425 jsr166 1.6 * isTerminated is false before termination, true after
426 dl 1.1 */
427 jsr166 1.6 public void testIsTerminated() throws InterruptedException {
428 jsr166 1.7 CustomExecutor p1 = new CustomExecutor(1);
429 dl 1.1 try {
430     p1.execute(new SmallRunnable());
431     } finally {
432 jsr166 1.3 try { p1.shutdown(); } catch (SecurityException ok) { return; }
433 dl 1.1 }
434 jsr166 1.6 assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
435     assertTrue(p1.isTerminated());
436 dl 1.1 }
437    
438     /**
439     * isTerminating is not true when running or when terminated
440     */
441 jsr166 1.6 public void testIsTerminating() throws InterruptedException {
442 jsr166 1.7 CustomExecutor p1 = new CustomExecutor(1);
443 dl 1.1 assertFalse(p1.isTerminating());
444     try {
445     p1.execute(new SmallRunnable());
446     assertFalse(p1.isTerminating());
447     } finally {
448 jsr166 1.3 try { p1.shutdown(); } catch (SecurityException ok) { return; }
449 dl 1.1 }
450 jsr166 1.6 assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
451     assertTrue(p1.isTerminated());
452     assertFalse(p1.isTerminating());
453 dl 1.1 }
454    
455     /**
456     * getQueue returns the work queue, which contains queued tasks
457     */
458 jsr166 1.6 public void testGetQueue() throws InterruptedException {
459 dl 1.1 CustomExecutor p1 = new CustomExecutor(1);
460     ScheduledFuture[] tasks = new ScheduledFuture[5];
461 jsr166 1.4 for (int i = 0; i < 5; i++) {
462 jsr166 1.6 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
463 dl 1.1 }
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 jsr166 1.6 public void testRemove() throws InterruptedException {
478 dl 1.1 CustomExecutor p1 = new CustomExecutor(1);
479     ScheduledFuture[] tasks = new ScheduledFuture[5];
480 jsr166 1.4 for (int i = 0; i < 5; i++) {
481 jsr166 1.6 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS);
482 dl 1.1 }
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 jsr166 1.6 public void testPurge() throws InterruptedException {
504 dl 1.1 CustomExecutor p1 = new CustomExecutor(1);
505     ScheduledFuture[] tasks = new ScheduledFuture[5];
506 jsr166 1.4 for (int i = 0; i < 5; i++) {
507 jsr166 1.6 tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
508 dl 1.1 }
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 jsr166 1.7 CustomExecutor p1 = new CustomExecutor(1);
534 jsr166 1.3 for (int i = 0; i < 5; i++)
535 jsr166 1.6 p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS);
536 dl 1.1 List l;
537     try {
538     l = p1.shutdownNow();
539 jsr166 1.2 } catch (SecurityException ok) {
540 dl 1.1 return;
541     }
542 jsr166 1.7 assertTrue(p1.isShutdown());
543     assertTrue(l.size() > 0 && l.size() <= 5);
544 dl 1.1 joinPool(p1);
545     }
546    
547     /**
548     * In default setting, shutdown cancels periodic but not delayed
549     * tasks at shutdown
550     */
551 jsr166 1.6 public void testShutDown1() throws InterruptedException {
552     CustomExecutor p1 = new CustomExecutor(1);
553     assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy());
554     assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy());
555 jsr166 1.2
556 jsr166 1.6 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 dl 1.1 }
571     }
572    
573    
574     /**
575     * If setExecuteExistingDelayedTasksAfterShutdownPolicy is false,
576     * delayed tasks are cancelled at shutdown
577     */
578 jsr166 1.6 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 dl 1.1 }
591    
592    
593     /**
594     * If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false,
595     * periodic tasks are not cancelled at shutdown
596     */
597 jsr166 1.6 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 dl 1.1 }
609    
610     /**
611     * if setContinueExistingPeriodicTasksAfterShutdownPolicy is true,
612     * periodic tasks are cancelled at shutdown
613     */
614 jsr166 1.6 public void testShutDown4() throws InterruptedException {
615 dl 1.1 CustomExecutor p1 = new CustomExecutor(1);
616     try {
617     p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true);
618     ScheduledFuture task =
619 jsr166 1.6 p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS);
620 dl 1.1 assertFalse(task.isCancelled());
621 jsr166 1.3 try { p1.shutdown(); } catch (SecurityException ok) { return; }
622 dl 1.1 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 jsr166 1.2 finally {
633 dl 1.1 joinPool(p1);
634     }
635     }
636    
637     /**
638     * completed submit of callable returns result
639     */
640 jsr166 1.6 public void testSubmitCallable() throws Exception {
641 dl 1.1 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 jsr166 1.6 public void testSubmitRunnable() throws Exception {
655 dl 1.1 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 jsr166 1.6 public void testSubmitRunnable2() throws Exception {
669 dl 1.1 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 jsr166 1.6 public void testInvokeAny1() throws Exception {
683 dl 1.1 ExecutorService e = new CustomExecutor(2);
684     try {
685     e.invokeAny(null);
686 jsr166 1.6 shouldThrow();
687 dl 1.1 } catch (NullPointerException success) {
688     } finally {
689     joinPool(e);
690     }
691     }
692    
693     /**
694     * invokeAny(empty collection) throws IAE
695     */
696 jsr166 1.6 public void testInvokeAny2() throws Exception {
697 dl 1.1 ExecutorService e = new CustomExecutor(2);
698     try {
699     e.invokeAny(new ArrayList<Callable<String>>());
700 jsr166 1.6 shouldThrow();
701 dl 1.1 } catch (IllegalArgumentException success) {
702     } finally {
703     joinPool(e);
704     }
705     }
706    
707     /**
708     * invokeAny(c) throws NPE if c has null elements
709     */
710 jsr166 1.6 public void testInvokeAny3() throws Exception {
711     final CountDownLatch latch = new CountDownLatch(1);
712 dl 1.1 ExecutorService e = new CustomExecutor(2);
713     try {
714     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
715 jsr166 1.6 l.add(new Callable<String>() {
716     public String call() {
717     try {
718     latch.await();
719 jsr166 1.8 } catch (InterruptedException quittingTime) {}
720 jsr166 1.6 return TEST_STRING;
721     }});
722 dl 1.1 l.add(null);
723     e.invokeAny(l);
724 jsr166 1.6 shouldThrow();
725 dl 1.1 } catch (NullPointerException success) {
726     } finally {
727 jsr166 1.6 latch.countDown();
728 dl 1.1 joinPool(e);
729     }
730     }
731    
732     /**
733     * invokeAny(c) throws ExecutionException if no task completes
734     */
735 jsr166 1.6 public void testInvokeAny4() throws Exception {
736 dl 1.1 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 jsr166 1.6 shouldThrow();
742 dl 1.1 } catch (ExecutionException success) {
743 jsr166 1.6 assertTrue(success.getCause() instanceof NullPointerException);
744 dl 1.1 } finally {
745     joinPool(e);
746     }
747     }
748    
749     /**
750     * invokeAny(c) returns result of some task
751     */
752 jsr166 1.6 public void testInvokeAny5() throws Exception {
753 dl 1.1 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 jsr166 1.6 public void testInvokeAll1() throws Exception {
769 dl 1.1 ExecutorService e = new CustomExecutor(2);
770     try {
771     e.invokeAll(null);
772 jsr166 1.6 shouldThrow();
773 dl 1.1 } catch (NullPointerException success) {
774     } finally {
775     joinPool(e);
776     }
777     }
778    
779     /**
780     * invokeAll(empty collection) returns empty collection
781     */
782 jsr166 1.6 public void testInvokeAll2() throws Exception {
783 dl 1.1 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 jsr166 1.6 public void testInvokeAll3() throws Exception {
796 dl 1.1 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 jsr166 1.6 shouldThrow();
803 dl 1.1 } catch (NullPointerException success) {
804     } finally {
805     joinPool(e);
806     }
807     }
808    
809     /**
810     * get of invokeAll(c) throws exception on failed task
811     */
812 jsr166 1.6 public void testInvokeAll4() throws Exception {
813 dl 1.1 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 jsr166 1.6 for (Future<String> future : result)
820     future.get();
821     shouldThrow();
822 jsr166 1.3 } catch (ExecutionException success) {
823 jsr166 1.6 assertTrue(success.getCause() instanceof NullPointerException);
824 dl 1.1 } finally {
825     joinPool(e);
826     }
827     }
828    
829     /**
830     * invokeAll(c) returns results of all completed tasks
831     */
832 jsr166 1.6 public void testInvokeAll5() throws Exception {
833 dl 1.1 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 jsr166 1.6 for (Future<String> future : result)
841     assertSame(TEST_STRING, future.get());
842 dl 1.1 } finally {
843     joinPool(e);
844     }
845     }
846    
847     /**
848     * timed invokeAny(null) throws NPE
849     */
850 jsr166 1.6 public void testTimedInvokeAny1() throws Exception {
851 dl 1.1 ExecutorService e = new CustomExecutor(2);
852     try {
853 jsr166 1.6 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
854     shouldThrow();
855 dl 1.1 } catch (NullPointerException success) {
856     } finally {
857     joinPool(e);
858     }
859     }
860    
861     /**
862     * timed invokeAny(,,null) throws NPE
863     */
864 jsr166 1.6 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
865 dl 1.1 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 jsr166 1.6 shouldThrow();
871 dl 1.1 } catch (NullPointerException success) {
872     } finally {
873     joinPool(e);
874     }
875     }
876    
877     /**
878     * timed invokeAny(empty collection) throws IAE
879     */
880 jsr166 1.6 public void testTimedInvokeAny2() throws Exception {
881 dl 1.1 ExecutorService e = new CustomExecutor(2);
882     try {
883 jsr166 1.6 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
884     shouldThrow();
885 dl 1.1 } 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 jsr166 1.6 public void testTimedInvokeAny3() throws Exception {
895     final CountDownLatch latch = new CountDownLatch(1);
896 dl 1.1 ExecutorService e = new CustomExecutor(2);
897     try {
898     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
899 jsr166 1.6 l.add(new Callable<String>() {
900     public String call() {
901     try {
902     latch.await();
903 jsr166 1.8 } catch (InterruptedException quittingTime) {}
904 jsr166 1.6 return TEST_STRING;
905     }});
906 dl 1.1 l.add(null);
907 jsr166 1.6 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
908     shouldThrow();
909 dl 1.1 } catch (NullPointerException success) {
910     } finally {
911 jsr166 1.6 latch.countDown();
912 dl 1.1 joinPool(e);
913     }
914     }
915    
916     /**
917     * timed invokeAny(c) throws ExecutionException if no task completes
918     */
919 jsr166 1.6 public void testTimedInvokeAny4() throws Exception {
920 dl 1.1 ExecutorService e = new CustomExecutor(2);
921     try {
922     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
923     l.add(new NPETask());
924 jsr166 1.6 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
925     shouldThrow();
926 jsr166 1.3 } catch (ExecutionException success) {
927 jsr166 1.6 assertTrue(success.getCause() instanceof NullPointerException);
928 dl 1.1 } finally {
929     joinPool(e);
930     }
931     }
932    
933     /**
934     * timed invokeAny(c) returns result of some task
935     */
936 jsr166 1.6 public void testTimedInvokeAny5() throws Exception {
937 dl 1.1 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 jsr166 1.6 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
943 dl 1.1 assertSame(TEST_STRING, result);
944     } finally {
945     joinPool(e);
946     }
947     }
948    
949     /**
950     * timed invokeAll(null) throws NPE
951     */
952 jsr166 1.6 public void testTimedInvokeAll1() throws Exception {
953 dl 1.1 ExecutorService e = new CustomExecutor(2);
954     try {
955 jsr166 1.6 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
956     shouldThrow();
957 dl 1.1 } catch (NullPointerException success) {
958     } finally {
959     joinPool(e);
960     }
961     }
962    
963     /**
964     * timed invokeAll(,,null) throws NPE
965     */
966 jsr166 1.6 public void testTimedInvokeAllNullTimeUnit() throws Exception {
967 dl 1.1 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 jsr166 1.6 shouldThrow();
973 dl 1.1 } catch (NullPointerException success) {
974     } finally {
975     joinPool(e);
976     }
977     }
978    
979     /**
980     * timed invokeAll(empty collection) returns empty collection
981     */
982 jsr166 1.6 public void testTimedInvokeAll2() throws Exception {
983 dl 1.1 ExecutorService e = new CustomExecutor(2);
984     try {
985 jsr166 1.6 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
986 dl 1.1 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 jsr166 1.6 public void testTimedInvokeAll3() throws Exception {
996 dl 1.1 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 jsr166 1.6 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1002     shouldThrow();
1003 dl 1.1 } 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 jsr166 1.6 public void testTimedInvokeAll4() throws Exception {
1013 dl 1.1 ExecutorService e = new CustomExecutor(2);
1014     try {
1015     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1016     l.add(new NPETask());
1017 jsr166 1.6 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1018 dl 1.1 assertEquals(1, result.size());
1019 jsr166 1.6 for (Future<String> future : result)
1020     future.get();
1021     shouldThrow();
1022 jsr166 1.3 } catch (ExecutionException success) {
1023 jsr166 1.6 assertTrue(success.getCause() instanceof NullPointerException);
1024 dl 1.1 } finally {
1025     joinPool(e);
1026     }
1027     }
1028    
1029     /**
1030     * timed invokeAll(c) returns results of all completed tasks
1031     */
1032 jsr166 1.6 public void testTimedInvokeAll5() throws Exception {
1033 dl 1.1 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 jsr166 1.6 List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1039 dl 1.1 assertEquals(2, result.size());
1040 jsr166 1.6 for (Future<String> future : result)
1041     assertSame(TEST_STRING, future.get());
1042 dl 1.1 } finally {
1043     joinPool(e);
1044     }
1045     }
1046    
1047     /**
1048     * timed invokeAll(c) cancels tasks not completed by timeout
1049     */
1050 jsr166 1.6 public void testTimedInvokeAll6() throws Exception {
1051 dl 1.1 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 jsr166 1.6 List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1058 dl 1.1 assertEquals(3, result.size());
1059 jsr166 1.2 Iterator<Future<String>> it = result.iterator();
1060 dl 1.1 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     }