ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ScheduledExecutorSubclassTest.java
Revision: 1.1
Committed: Fri May 20 16:30:17 2005 UTC (19 years ago) by dl
Branch: MAIN
Log Message:
Add tests for new protected extenion methods

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