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