ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.5
Committed: Wed Nov 18 16:51:26 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.4: +111 -240 lines
Log Message:
sync with ThreadPoolExecutorTest.java

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 jsr166 1.2 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9     import java.util.concurrent.*;
10     import java.util.concurrent.locks.*;
11    
12     import junit.framework.*;
13     import java.util.*;
14    
15     public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
16     public static void main(String[] args) {
17 jsr166 1.2 junit.textui.TestRunner.run (suite());
18 dl 1.1 }
19     public static Test suite() {
20     return new TestSuite(ThreadPoolExecutorTest.class);
21     }
22    
23     static class CustomTask<V> implements RunnableFuture<V> {
24     final Callable<V> callable;
25     final ReentrantLock lock = new ReentrantLock();
26     final Condition cond = lock.newCondition();
27     boolean done;
28     boolean cancelled;
29     V result;
30     Thread thread;
31     Exception exception;
32     CustomTask(Callable<V> c) { callable = c; }
33     CustomTask(final Runnable r, final V res) { callable = new Callable<V>() {
34 jsr166 1.2 public V call() throws Exception { r.run(); return res; }};
35 dl 1.1 }
36     public boolean isDone() {
37     lock.lock(); try { return done; } finally { lock.unlock() ; }
38     }
39     public boolean isCancelled() {
40     lock.lock(); try { return cancelled; } finally { lock.unlock() ; }
41     }
42     public boolean cancel(boolean mayInterrupt) {
43     lock.lock();
44     try {
45     if (!done) {
46     cancelled = true;
47     done = true;
48 jsr166 1.2 if (mayInterrupt && thread != null)
49 dl 1.1 thread.interrupt();
50     return true;
51     }
52     return false;
53     }
54     finally { lock.unlock() ; }
55     }
56     public void run() {
57     boolean runme;
58     lock.lock();
59     try {
60     runme = !done;
61     if (!runme)
62     thread = Thread.currentThread();
63     }
64     finally { lock.unlock() ; }
65     if (!runme) return;
66     V v = null;
67     Exception e = null;
68     try {
69     v = callable.call();
70     }
71 jsr166 1.3 catch (Exception ex) {
72 dl 1.1 e = ex;
73     }
74     lock.lock();
75     try {
76     result = v;
77     exception = e;
78     done = true;
79     thread = null;
80     cond.signalAll();
81     }
82     finally { lock.unlock(); }
83     }
84     public V get() throws InterruptedException, ExecutionException {
85     lock.lock();
86     try {
87 jsr166 1.2 while (!done)
88 dl 1.1 cond.await();
89     if (exception != null)
90     throw new ExecutionException(exception);
91     return result;
92     }
93     finally { lock.unlock(); }
94     }
95     public V get(long timeout, TimeUnit unit)
96 jsr166 1.4 throws InterruptedException, ExecutionException, TimeoutException {
97 dl 1.1 long nanos = unit.toNanos(timeout);
98     lock.lock();
99     try {
100     for (;;) {
101     if (done) break;
102     if (nanos < 0)
103     throw new TimeoutException();
104     nanos = cond.awaitNanos(nanos);
105     }
106     if (exception != null)
107     throw new ExecutionException(exception);
108     return result;
109     }
110     finally { lock.unlock(); }
111     }
112 jsr166 1.2 }
113    
114 dl 1.1
115     static class CustomTPE extends ThreadPoolExecutor {
116     protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
117     return new CustomTask<V>(c);
118     }
119     protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
120     return new CustomTask<V>(r, v);
121 jsr166 1.2 }
122    
123 dl 1.1 CustomTPE(int corePoolSize,
124     int maximumPoolSize,
125     long keepAliveTime,
126     TimeUnit unit,
127     BlockingQueue<Runnable> workQueue) {
128 jsr166 1.2 super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
129 dl 1.1 workQueue);
130     }
131     CustomTPE(int corePoolSize,
132     int maximumPoolSize,
133     long keepAliveTime,
134     TimeUnit unit,
135     BlockingQueue<Runnable> workQueue,
136     ThreadFactory threadFactory) {
137     super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
138     threadFactory);
139     }
140    
141     CustomTPE(int corePoolSize,
142     int maximumPoolSize,
143     long keepAliveTime,
144     TimeUnit unit,
145     BlockingQueue<Runnable> workQueue,
146     RejectedExecutionHandler handler) {
147     super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
148     handler);
149     }
150     CustomTPE(int corePoolSize,
151     int maximumPoolSize,
152     long keepAliveTime,
153     TimeUnit unit,
154     BlockingQueue<Runnable> workQueue,
155     ThreadFactory threadFactory,
156     RejectedExecutionHandler handler) {
157 jsr166 1.2 super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
158 dl 1.1 workQueue, threadFactory, handler);
159     }
160    
161     volatile boolean beforeCalled = false;
162     volatile boolean afterCalled = false;
163     volatile boolean terminatedCalled = false;
164     public CustomTPE() {
165     super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
166     }
167     protected void beforeExecute(Thread t, Runnable r) {
168     beforeCalled = true;
169     }
170     protected void afterExecute(Runnable r, Throwable t) {
171     afterCalled = true;
172     }
173     protected void terminated() {
174     terminatedCalled = true;
175     }
176 jsr166 1.2
177 dl 1.1 }
178    
179 jsr166 1.4 static class FailingThreadFactory implements ThreadFactory {
180 dl 1.1 int calls = 0;
181 jsr166 1.4 public Thread newThread(Runnable r) {
182 dl 1.1 if (++calls > 1) return null;
183     return new Thread(r);
184 jsr166 1.2 }
185 dl 1.1 }
186 jsr166 1.2
187 dl 1.1
188     /**
189     * execute successfully executes a runnable
190     */
191 jsr166 1.5 public void testExecute() throws InterruptedException {
192 dl 1.1 ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
193     try {
194 jsr166 1.5 p1.execute(new ShortRunnable());
195 dl 1.1 Thread.sleep(SMALL_DELAY_MS);
196 jsr166 1.5 } finally {
197     joinPool(p1);
198 jsr166 1.2 }
199 dl 1.1 }
200    
201     /**
202     * getActiveCount increases but doesn't overestimate, when a
203     * thread becomes active
204     */
205 jsr166 1.5 public void testGetActiveCount() throws InterruptedException {
206 dl 1.1 ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
207     assertEquals(0, p2.getActiveCount());
208     p2.execute(new MediumRunnable());
209 jsr166 1.5 Thread.sleep(SHORT_DELAY_MS);
210 dl 1.1 assertEquals(1, p2.getActiveCount());
211     joinPool(p2);
212     }
213    
214     /**
215     * prestartCoreThread starts a thread if under corePoolSize, else doesn't
216     */
217     public void testPrestartCoreThread() {
218     ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
219     assertEquals(0, p2.getPoolSize());
220     assertTrue(p2.prestartCoreThread());
221     assertEquals(1, p2.getPoolSize());
222     assertTrue(p2.prestartCoreThread());
223     assertEquals(2, p2.getPoolSize());
224     assertFalse(p2.prestartCoreThread());
225     assertEquals(2, p2.getPoolSize());
226     joinPool(p2);
227     }
228    
229     /**
230     * prestartAllCoreThreads starts all corePoolSize threads
231     */
232     public void testPrestartAllCoreThreads() {
233     ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
234     assertEquals(0, p2.getPoolSize());
235     p2.prestartAllCoreThreads();
236     assertEquals(2, p2.getPoolSize());
237     p2.prestartAllCoreThreads();
238     assertEquals(2, p2.getPoolSize());
239     joinPool(p2);
240     }
241 jsr166 1.2
242 dl 1.1 /**
243     * getCompletedTaskCount increases, but doesn't overestimate,
244     * when tasks complete
245     */
246 jsr166 1.5 public void testGetCompletedTaskCount() throws InterruptedException {
247 dl 1.1 ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
248     assertEquals(0, p2.getCompletedTaskCount());
249     p2.execute(new ShortRunnable());
250 jsr166 1.5 Thread.sleep(SMALL_DELAY_MS);
251 dl 1.1 assertEquals(1, p2.getCompletedTaskCount());
252 jsr166 1.3 try { p2.shutdown(); } catch (SecurityException ok) { return; }
253 dl 1.1 joinPool(p2);
254     }
255 jsr166 1.2
256 dl 1.1 /**
257     * getCorePoolSize returns size given in constructor if not otherwise set
258     */
259     public void testGetCorePoolSize() {
260     ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
261     assertEquals(1, p1.getCorePoolSize());
262     joinPool(p1);
263     }
264 jsr166 1.2
265 dl 1.1 /**
266     * getKeepAliveTime returns value given in constructor if not otherwise set
267     */
268     public void testGetKeepAliveTime() {
269     ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
270     assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
271     joinPool(p2);
272     }
273    
274    
275 jsr166 1.2 /**
276 dl 1.1 * getThreadFactory returns factory in constructor if not set
277     */
278     public void testGetThreadFactory() {
279     ThreadFactory tf = new SimpleThreadFactory();
280     ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
281     assertSame(tf, p.getThreadFactory());
282     joinPool(p);
283     }
284    
285 jsr166 1.2 /**
286 dl 1.1 * setThreadFactory sets the thread factory returned by getThreadFactory
287     */
288     public void testSetThreadFactory() {
289     ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
290     ThreadFactory tf = new SimpleThreadFactory();
291     p.setThreadFactory(tf);
292     assertSame(tf, p.getThreadFactory());
293     joinPool(p);
294     }
295    
296    
297 jsr166 1.2 /**
298 dl 1.1 * setThreadFactory(null) throws NPE
299     */
300     public void testSetThreadFactoryNull() {
301     ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
302     try {
303     p.setThreadFactory(null);
304     shouldThrow();
305     } catch (NullPointerException success) {
306     } finally {
307     joinPool(p);
308     }
309     }
310    
311 jsr166 1.2 /**
312 dl 1.1 * getRejectedExecutionHandler returns handler in constructor if not set
313     */
314     public void testGetRejectedExecutionHandler() {
315     RejectedExecutionHandler h = new NoOpREHandler();
316     ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
317     assertSame(h, p.getRejectedExecutionHandler());
318     joinPool(p);
319     }
320    
321 jsr166 1.2 /**
322 dl 1.1 * setRejectedExecutionHandler sets the handler returned by
323     * getRejectedExecutionHandler
324     */
325     public void testSetRejectedExecutionHandler() {
326     ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
327     RejectedExecutionHandler h = new NoOpREHandler();
328     p.setRejectedExecutionHandler(h);
329     assertSame(h, p.getRejectedExecutionHandler());
330     joinPool(p);
331     }
332    
333    
334 jsr166 1.2 /**
335 dl 1.1 * setRejectedExecutionHandler(null) throws NPE
336     */
337     public void testSetRejectedExecutionHandlerNull() {
338     ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
339     try {
340     p.setRejectedExecutionHandler(null);
341     shouldThrow();
342     } catch (NullPointerException success) {
343     } finally {
344     joinPool(p);
345     }
346     }
347    
348 jsr166 1.2
349 dl 1.1 /**
350     * getLargestPoolSize increases, but doesn't overestimate, when
351     * multiple threads active
352     */
353 jsr166 1.5 public void testGetLargestPoolSize() throws InterruptedException {
354 dl 1.1 ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
355 jsr166 1.5 assertEquals(0, p2.getLargestPoolSize());
356     p2.execute(new MediumRunnable());
357     p2.execute(new MediumRunnable());
358     Thread.sleep(SHORT_DELAY_MS);
359     assertEquals(2, p2.getLargestPoolSize());
360 dl 1.1 joinPool(p2);
361     }
362 jsr166 1.2
363 dl 1.1 /**
364     * getMaximumPoolSize returns value given in constructor if not
365     * otherwise set
366     */
367     public void testGetMaximumPoolSize() {
368     ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
369     assertEquals(2, p2.getMaximumPoolSize());
370     joinPool(p2);
371     }
372 jsr166 1.2
373 dl 1.1 /**
374     * getPoolSize increases, but doesn't overestimate, when threads
375     * become active
376     */
377     public void testGetPoolSize() {
378     ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
379     assertEquals(0, p1.getPoolSize());
380     p1.execute(new MediumRunnable());
381     assertEquals(1, p1.getPoolSize());
382     joinPool(p1);
383     }
384 jsr166 1.2
385 dl 1.1 /**
386     * getTaskCount increases, but doesn't overestimate, when tasks submitted
387     */
388 jsr166 1.5 public void testGetTaskCount() throws InterruptedException {
389 dl 1.1 ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
390 jsr166 1.5 assertEquals(0, p1.getTaskCount());
391     p1.execute(new MediumRunnable());
392     Thread.sleep(SHORT_DELAY_MS);
393     assertEquals(1, p1.getTaskCount());
394 dl 1.1 joinPool(p1);
395     }
396 jsr166 1.2
397 dl 1.1 /**
398     * isShutDown is false before shutdown, true after
399     */
400     public void testIsShutdown() {
401 jsr166 1.2
402 dl 1.1 ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
403     assertFalse(p1.isShutdown());
404 jsr166 1.3 try { p1.shutdown(); } catch (SecurityException ok) { return; }
405 dl 1.1 assertTrue(p1.isShutdown());
406     joinPool(p1);
407     }
408    
409 jsr166 1.2
410 dl 1.1 /**
411     * isTerminated is false before termination, true after
412     */
413 jsr166 1.5 public void testIsTerminated() throws InterruptedException {
414 dl 1.1 ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
415     assertFalse(p1.isTerminated());
416     try {
417     p1.execute(new MediumRunnable());
418     } finally {
419 jsr166 1.3 try { p1.shutdown(); } catch (SecurityException ok) { return; }
420 dl 1.1 }
421 jsr166 1.5 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
422     assertTrue(p1.isTerminated());
423 dl 1.1 }
424    
425     /**
426     * isTerminating is not true when running or when terminated
427     */
428 jsr166 1.5 public void testIsTerminating() throws InterruptedException {
429 dl 1.1 ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
430     assertFalse(p1.isTerminating());
431     try {
432     p1.execute(new SmallRunnable());
433     assertFalse(p1.isTerminating());
434     } finally {
435 jsr166 1.3 try { p1.shutdown(); } catch (SecurityException ok) { return; }
436 dl 1.1 }
437 jsr166 1.5 assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
438     assertTrue(p1.isTerminated());
439     assertFalse(p1.isTerminating());
440 dl 1.1 }
441    
442     /**
443     * getQueue returns the work queue, which contains queued tasks
444     */
445 jsr166 1.5 public void testGetQueue() throws InterruptedException {
446 dl 1.1 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
447     ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
448     FutureTask[] tasks = new FutureTask[5];
449 jsr166 1.4 for (int i = 0; i < 5; i++) {
450 dl 1.1 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
451     p1.execute(tasks[i]);
452     }
453     try {
454     Thread.sleep(SHORT_DELAY_MS);
455     BlockingQueue<Runnable> wq = p1.getQueue();
456     assertSame(q, wq);
457     assertFalse(wq.contains(tasks[0]));
458     assertTrue(wq.contains(tasks[4]));
459     for (int i = 1; i < 5; ++i)
460     tasks[i].cancel(true);
461     p1.shutdownNow();
462     } finally {
463     joinPool(p1);
464     }
465     }
466    
467     /**
468     * remove(task) removes queued task, and fails to remove active task
469     */
470 jsr166 1.5 public void testRemove() throws InterruptedException {
471 dl 1.1 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
472     ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
473     FutureTask[] tasks = new FutureTask[5];
474 jsr166 1.4 for (int i = 0; i < 5; i++) {
475 dl 1.1 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
476     p1.execute(tasks[i]);
477     }
478     try {
479     Thread.sleep(SHORT_DELAY_MS);
480     assertFalse(p1.remove(tasks[0]));
481     assertTrue(q.contains(tasks[4]));
482     assertTrue(q.contains(tasks[3]));
483     assertTrue(p1.remove(tasks[4]));
484     assertFalse(p1.remove(tasks[4]));
485     assertFalse(q.contains(tasks[4]));
486     assertTrue(q.contains(tasks[3]));
487     assertTrue(p1.remove(tasks[3]));
488     assertFalse(q.contains(tasks[3]));
489     } finally {
490     joinPool(p1);
491     }
492     }
493    
494     /**
495     * purge removes cancelled tasks from the queue
496     */
497     public void testPurge() {
498     ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
499     FutureTask[] tasks = new FutureTask[5];
500 jsr166 1.4 for (int i = 0; i < 5; i++) {
501 dl 1.1 tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
502     p1.execute(tasks[i]);
503     }
504     tasks[4].cancel(true);
505     tasks[3].cancel(true);
506     p1.purge();
507     long count = p1.getTaskCount();
508     assertTrue(count >= 2 && count < 5);
509     joinPool(p1);
510     }
511    
512     /**
513     * shutDownNow returns a list containing tasks that were not run
514     */
515     public void testShutDownNow() {
516     ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
517     List l;
518     try {
519 jsr166 1.3 for (int i = 0; i < 5; i++)
520 dl 1.1 p1.execute(new MediumPossiblyInterruptedRunnable());
521     }
522     finally {
523     try {
524     l = p1.shutdownNow();
525     } catch (SecurityException ok) { return; }
526 jsr166 1.2
527 dl 1.1 }
528     assertTrue(p1.isShutdown());
529     assertTrue(l.size() <= 4);
530     }
531    
532     // Exception Tests
533    
534 jsr166 1.2
535     /**
536     * Constructor throws if corePoolSize argument is less than zero
537 dl 1.1 */
538     public void testConstructor1() {
539     try {
540     new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
541     shouldThrow();
542 jsr166 1.5 } catch (IllegalArgumentException success) {}
543 dl 1.1 }
544 jsr166 1.2
545     /**
546     * Constructor throws if maximumPoolSize is less than zero
547 dl 1.1 */
548     public void testConstructor2() {
549     try {
550     new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
551     shouldThrow();
552 jsr166 1.5 } catch (IllegalArgumentException success) {}
553 dl 1.1 }
554 jsr166 1.2
555     /**
556     * Constructor throws if maximumPoolSize is equal to zero
557 dl 1.1 */
558     public void testConstructor3() {
559     try {
560     new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
561     shouldThrow();
562 jsr166 1.5 } catch (IllegalArgumentException success) {}
563 dl 1.1 }
564    
565 jsr166 1.2 /**
566     * Constructor throws if keepAliveTime is less than zero
567 dl 1.1 */
568     public void testConstructor4() {
569     try {
570     new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
571     shouldThrow();
572 jsr166 1.5 } catch (IllegalArgumentException success) {}
573 dl 1.1 }
574    
575 jsr166 1.2 /**
576     * Constructor throws if corePoolSize is greater than the maximumPoolSize
577 dl 1.1 */
578     public void testConstructor5() {
579     try {
580     new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
581     shouldThrow();
582 jsr166 1.5 } catch (IllegalArgumentException success) {}
583 dl 1.1 }
584 jsr166 1.2
585     /**
586     * Constructor throws if workQueue is set to null
587 dl 1.1 */
588     public void testConstructorNullPointerException() {
589     try {
590     new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
591     shouldThrow();
592 jsr166 1.5 } catch (NullPointerException success) {}
593 dl 1.1 }
594    
595 jsr166 1.2
596    
597     /**
598     * Constructor throws if corePoolSize argument is less than zero
599 dl 1.1 */
600     public void testConstructor6() {
601     try {
602     new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
603     shouldThrow();
604 jsr166 1.4 } catch (IllegalArgumentException success) {}
605 dl 1.1 }
606 jsr166 1.2
607     /**
608     * Constructor throws if maximumPoolSize is less than zero
609 dl 1.1 */
610     public void testConstructor7() {
611     try {
612     new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
613     shouldThrow();
614 jsr166 1.5 } catch (IllegalArgumentException success) {}
615 dl 1.1 }
616    
617 jsr166 1.2 /**
618     * Constructor throws if maximumPoolSize is equal to zero
619 dl 1.1 */
620     public void testConstructor8() {
621     try {
622     new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
623     shouldThrow();
624 jsr166 1.5 } catch (IllegalArgumentException success) {}
625 dl 1.1 }
626    
627 jsr166 1.2 /**
628     * Constructor throws if keepAliveTime is less than zero
629 dl 1.1 */
630     public void testConstructor9() {
631     try {
632     new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
633     shouldThrow();
634 jsr166 1.5 } catch (IllegalArgumentException success) {}
635 dl 1.1 }
636    
637 jsr166 1.2 /**
638     * Constructor throws if corePoolSize is greater than the maximumPoolSize
639 dl 1.1 */
640     public void testConstructor10() {
641     try {
642     new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
643     shouldThrow();
644 jsr166 1.5 } catch (IllegalArgumentException success) {}
645 dl 1.1 }
646    
647 jsr166 1.2 /**
648     * Constructor throws if workQueue is set to null
649 dl 1.1 */
650     public void testConstructorNullPointerException2() {
651     try {
652     new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
653     shouldThrow();
654 jsr166 1.5 } catch (NullPointerException success) {}
655 dl 1.1 }
656    
657 jsr166 1.2 /**
658     * Constructor throws if threadFactory is set to null
659 dl 1.1 */
660     public void testConstructorNullPointerException3() {
661     try {
662     ThreadFactory f = null;
663     new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
664     shouldThrow();
665 jsr166 1.5 } catch (NullPointerException success) {}
666 dl 1.1 }
667 jsr166 1.2
668    
669     /**
670     * Constructor throws if corePoolSize argument is less than zero
671 dl 1.1 */
672     public void testConstructor11() {
673     try {
674     new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
675     shouldThrow();
676 jsr166 1.5 } catch (IllegalArgumentException success) {}
677 dl 1.1 }
678    
679 jsr166 1.2 /**
680     * Constructor throws if maximumPoolSize is less than zero
681 dl 1.1 */
682     public void testConstructor12() {
683     try {
684     new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
685     shouldThrow();
686 jsr166 1.5 } catch (IllegalArgumentException success) {}
687 dl 1.1 }
688    
689 jsr166 1.2 /**
690     * Constructor throws if maximumPoolSize is equal to zero
691 dl 1.1 */
692     public void testConstructor13() {
693     try {
694     new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
695     shouldThrow();
696 jsr166 1.5 } catch (IllegalArgumentException success) {}
697 dl 1.1 }
698    
699 jsr166 1.2 /**
700     * Constructor throws if keepAliveTime is less than zero
701 dl 1.1 */
702     public void testConstructor14() {
703     try {
704     new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
705     shouldThrow();
706 jsr166 1.5 } catch (IllegalArgumentException success) {}
707 dl 1.1 }
708    
709 jsr166 1.2 /**
710     * Constructor throws if corePoolSize is greater than the maximumPoolSize
711 dl 1.1 */
712     public void testConstructor15() {
713     try {
714     new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
715     shouldThrow();
716 jsr166 1.5 } catch (IllegalArgumentException success) {}
717 dl 1.1 }
718    
719 jsr166 1.2 /**
720     * Constructor throws if workQueue is set to null
721 dl 1.1 */
722     public void testConstructorNullPointerException4() {
723     try {
724     new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
725     shouldThrow();
726 jsr166 1.5 } catch (NullPointerException success) {}
727 dl 1.1 }
728    
729 jsr166 1.2 /**
730     * Constructor throws if handler is set to null
731 dl 1.1 */
732     public void testConstructorNullPointerException5() {
733     try {
734     RejectedExecutionHandler r = null;
735     new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
736     shouldThrow();
737 jsr166 1.5 } catch (NullPointerException success) {}
738 dl 1.1 }
739    
740 jsr166 1.2
741     /**
742     * Constructor throws if corePoolSize argument is less than zero
743 dl 1.1 */
744     public void testConstructor16() {
745     try {
746     new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
747     shouldThrow();
748 jsr166 1.5 } catch (IllegalArgumentException success) {}
749 dl 1.1 }
750    
751 jsr166 1.2 /**
752     * Constructor throws if maximumPoolSize is less than zero
753 dl 1.1 */
754     public void testConstructor17() {
755     try {
756     new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
757     shouldThrow();
758 jsr166 1.5 } catch (IllegalArgumentException success) {}
759 dl 1.1 }
760    
761 jsr166 1.2 /**
762     * Constructor throws if maximumPoolSize is equal to zero
763 dl 1.1 */
764     public void testConstructor18() {
765     try {
766     new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
767     shouldThrow();
768 jsr166 1.5 } catch (IllegalArgumentException success) {}
769 dl 1.1 }
770    
771 jsr166 1.2 /**
772     * Constructor throws if keepAliveTime is less than zero
773 dl 1.1 */
774     public void testConstructor19() {
775     try {
776     new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
777     shouldThrow();
778 jsr166 1.5 } catch (IllegalArgumentException success) {}
779 dl 1.1 }
780    
781 jsr166 1.2 /**
782     * Constructor throws if corePoolSize is greater than the maximumPoolSize
783 dl 1.1 */
784     public void testConstructor20() {
785     try {
786     new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
787     shouldThrow();
788 jsr166 1.5 } catch (IllegalArgumentException success) {}
789 dl 1.1 }
790    
791 jsr166 1.2 /**
792     * Constructor throws if workQueue is set to null
793 dl 1.1 */
794     public void testConstructorNullPointerException6() {
795     try {
796     new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
797     shouldThrow();
798 jsr166 1.5 } catch (NullPointerException success) {}
799 dl 1.1 }
800    
801 jsr166 1.2 /**
802     * Constructor throws if handler is set to null
803 dl 1.1 */
804     public void testConstructorNullPointerException7() {
805     try {
806     RejectedExecutionHandler r = null;
807     new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
808     shouldThrow();
809 jsr166 1.5 } catch (NullPointerException success) {}
810 dl 1.1 }
811    
812 jsr166 1.2 /**
813     * Constructor throws if ThreadFactory is set top null
814 dl 1.1 */
815     public void testConstructorNullPointerException8() {
816     try {
817     ThreadFactory f = null;
818     new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
819     shouldThrow();
820 jsr166 1.5 } catch (NullPointerException success) {}
821 dl 1.1 }
822 jsr166 1.2
823 dl 1.1
824     /**
825     * execute throws RejectedExecutionException
826     * if saturated.
827     */
828     public void testSaturatedExecute() {
829     ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
830     try {
831 jsr166 1.2
832 jsr166 1.4 for (int i = 0; i < 5; ++i) {
833 dl 1.1 p.execute(new MediumRunnable());
834     }
835     shouldThrow();
836 jsr166 1.4 } catch (RejectedExecutionException success) {}
837 dl 1.1 joinPool(p);
838     }
839    
840     /**
841     * executor using CallerRunsPolicy runs task if saturated.
842     */
843     public void testSaturatedExecute2() {
844     RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
845     ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
846     try {
847 jsr166 1.2
848 dl 1.1 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
849 jsr166 1.4 for (int i = 0; i < 5; ++i) {
850 dl 1.1 tasks[i] = new TrackedNoOpRunnable();
851     }
852     TrackedLongRunnable mr = new TrackedLongRunnable();
853     p.execute(mr);
854 jsr166 1.4 for (int i = 0; i < 5; ++i) {
855 dl 1.1 p.execute(tasks[i]);
856     }
857 jsr166 1.3 for (int i = 1; i < 5; ++i) {
858 dl 1.1 assertTrue(tasks[i].done);
859     }
860 jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
861 dl 1.1 } finally {
862     joinPool(p);
863     }
864     }
865    
866     /**
867     * executor using DiscardPolicy drops task if saturated.
868     */
869     public void testSaturatedExecute3() {
870     RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
871     ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
872     try {
873 jsr166 1.2
874 dl 1.1 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
875 jsr166 1.4 for (int i = 0; i < 5; ++i) {
876 dl 1.1 tasks[i] = new TrackedNoOpRunnable();
877     }
878     p.execute(new TrackedLongRunnable());
879 jsr166 1.4 for (int i = 0; i < 5; ++i) {
880 dl 1.1 p.execute(tasks[i]);
881     }
882 jsr166 1.4 for (int i = 0; i < 5; ++i) {
883 dl 1.1 assertFalse(tasks[i].done);
884     }
885 jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
886 dl 1.1 } finally {
887     joinPool(p);
888     }
889     }
890    
891     /**
892     * executor using DiscardOldestPolicy drops oldest task if saturated.
893     */
894     public void testSaturatedExecute4() {
895     RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
896     ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
897     try {
898     p.execute(new TrackedLongRunnable());
899     TrackedLongRunnable r2 = new TrackedLongRunnable();
900     p.execute(r2);
901     assertTrue(p.getQueue().contains(r2));
902     TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
903     p.execute(r3);
904     assertFalse(p.getQueue().contains(r2));
905     assertTrue(p.getQueue().contains(r3));
906 jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
907 dl 1.1 } finally {
908     joinPool(p);
909     }
910     }
911    
912     /**
913     * execute throws RejectedExecutionException if shutdown
914     */
915     public void testRejectedExecutionExceptionOnShutdown() {
916 jsr166 1.2 ThreadPoolExecutor tpe =
917 dl 1.1 new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
918 jsr166 1.3 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
919 dl 1.1 try {
920     tpe.execute(new NoOpRunnable());
921     shouldThrow();
922 jsr166 1.4 } catch (RejectedExecutionException success) {}
923 jsr166 1.2
924 dl 1.1 joinPool(tpe);
925     }
926    
927     /**
928     * execute using CallerRunsPolicy drops task on shutdown
929     */
930     public void testCallerRunsOnShutdown() {
931     RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
932     ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
933    
934 jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
935 dl 1.1 try {
936     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
937     p.execute(r);
938     assertFalse(r.done);
939     } finally {
940     joinPool(p);
941     }
942     }
943    
944     /**
945     * execute using DiscardPolicy drops task on shutdown
946     */
947     public void testDiscardOnShutdown() {
948     RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
949     ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
950    
951 jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
952 dl 1.1 try {
953     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
954     p.execute(r);
955     assertFalse(r.done);
956     } finally {
957     joinPool(p);
958     }
959     }
960    
961    
962     /**
963     * execute using DiscardOldestPolicy drops task on shutdown
964     */
965     public void testDiscardOldestOnShutdown() {
966     RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
967     ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
968    
969 jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
970 dl 1.1 try {
971     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
972     p.execute(r);
973     assertFalse(r.done);
974     } finally {
975     joinPool(p);
976     }
977     }
978    
979    
980     /**
981     * execute (null) throws NPE
982     */
983     public void testExecuteNull() {
984     ThreadPoolExecutor tpe = null;
985     try {
986     tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
987     tpe.execute(null);
988     shouldThrow();
989 jsr166 1.4 } catch (NullPointerException success) {}
990 jsr166 1.2
991 dl 1.1 joinPool(tpe);
992     }
993 jsr166 1.2
994 dl 1.1 /**
995     * setCorePoolSize of negative value throws IllegalArgumentException
996     */
997     public void testCorePoolSizeIllegalArgumentException() {
998     ThreadPoolExecutor tpe = null;
999     try {
1000     tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1001 jsr166 1.4 } catch (Exception e) {}
1002 dl 1.1 try {
1003     tpe.setCorePoolSize(-1);
1004     shouldThrow();
1005 jsr166 1.4 } catch (IllegalArgumentException success) {
1006 dl 1.1 } finally {
1007 jsr166 1.3 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1008 dl 1.1 }
1009     joinPool(tpe);
1010 jsr166 1.2 }
1011 dl 1.1
1012     /**
1013     * setMaximumPoolSize(int) throws IllegalArgumentException if
1014     * given a value less the core pool size
1015 jsr166 1.2 */
1016 dl 1.1 public void testMaximumPoolSizeIllegalArgumentException() {
1017     ThreadPoolExecutor tpe = null;
1018     try {
1019     tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1020 jsr166 1.4 } catch (Exception e) {}
1021 dl 1.1 try {
1022     tpe.setMaximumPoolSize(1);
1023     shouldThrow();
1024 jsr166 1.4 } catch (IllegalArgumentException success) {
1025 dl 1.1 } finally {
1026 jsr166 1.3 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1027 dl 1.1 }
1028     joinPool(tpe);
1029     }
1030 jsr166 1.2
1031 dl 1.1 /**
1032     * setMaximumPoolSize throws IllegalArgumentException
1033     * if given a negative value
1034     */
1035     public void testMaximumPoolSizeIllegalArgumentException2() {
1036     ThreadPoolExecutor tpe = null;
1037     try {
1038     tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1039 jsr166 1.4 } catch (Exception e) {}
1040 dl 1.1 try {
1041     tpe.setMaximumPoolSize(-1);
1042     shouldThrow();
1043 jsr166 1.4 } catch (IllegalArgumentException success) {
1044 dl 1.1 } finally {
1045 jsr166 1.3 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1046 dl 1.1 }
1047     joinPool(tpe);
1048     }
1049 jsr166 1.2
1050 dl 1.1
1051     /**
1052     * setKeepAliveTime throws IllegalArgumentException
1053     * when given a negative value
1054     */
1055     public void testKeepAliveTimeIllegalArgumentException() {
1056     ThreadPoolExecutor tpe = null;
1057     try {
1058     tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1059 jsr166 1.4 } catch (Exception e) {}
1060 jsr166 1.2
1061 dl 1.1 try {
1062     tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
1063     shouldThrow();
1064 jsr166 1.4 } catch (IllegalArgumentException success) {
1065 dl 1.1 } finally {
1066 jsr166 1.3 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1067 dl 1.1 }
1068     joinPool(tpe);
1069     }
1070    
1071     /**
1072     * terminated() is called on termination
1073     */
1074     public void testTerminated() {
1075     CustomTPE tpe = new CustomTPE();
1076 jsr166 1.3 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1077 dl 1.1 assertTrue(tpe.terminatedCalled);
1078     joinPool(tpe);
1079     }
1080    
1081     /**
1082     * beforeExecute and afterExecute are called when executing task
1083     */
1084 jsr166 1.5 public void testBeforeAfter() throws InterruptedException {
1085 dl 1.1 CustomTPE tpe = new CustomTPE();
1086     try {
1087     TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1088     tpe.execute(r);
1089     Thread.sleep(SHORT_DELAY_MS);
1090     assertTrue(r.done);
1091     assertTrue(tpe.beforeCalled);
1092     assertTrue(tpe.afterCalled);
1093 jsr166 1.3 try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1094 dl 1.1 } finally {
1095     joinPool(tpe);
1096     }
1097     }
1098    
1099     /**
1100     * completed submit of callable returns result
1101     */
1102 jsr166 1.5 public void testSubmitCallable() throws Exception {
1103 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1104     try {
1105     Future<String> future = e.submit(new StringTask());
1106     String result = future.get();
1107     assertSame(TEST_STRING, result);
1108     } finally {
1109     joinPool(e);
1110     }
1111     }
1112    
1113     /**
1114     * completed submit of runnable returns successfully
1115     */
1116 jsr166 1.5 public void testSubmitRunnable() throws Exception {
1117 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1118     try {
1119     Future<?> future = e.submit(new NoOpRunnable());
1120     future.get();
1121     assertTrue(future.isDone());
1122     } finally {
1123     joinPool(e);
1124     }
1125     }
1126    
1127     /**
1128     * completed submit of (runnable, result) returns result
1129     */
1130 jsr166 1.5 public void testSubmitRunnable2() throws Exception {
1131 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1132     try {
1133     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1134     String result = future.get();
1135     assertSame(TEST_STRING, result);
1136     } finally {
1137     joinPool(e);
1138     }
1139     }
1140    
1141    
1142     /**
1143     * invokeAny(null) throws NPE
1144     */
1145 jsr166 1.5 public void testInvokeAny1() throws Exception {
1146 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1147     try {
1148     e.invokeAny(null);
1149 jsr166 1.5 shouldThrow();
1150 dl 1.1 } catch (NullPointerException success) {
1151     } finally {
1152     joinPool(e);
1153     }
1154     }
1155    
1156     /**
1157     * invokeAny(empty collection) throws IAE
1158     */
1159 jsr166 1.5 public void testInvokeAny2() throws Exception {
1160 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1161     try {
1162     e.invokeAny(new ArrayList<Callable<String>>());
1163 jsr166 1.5 shouldThrow();
1164 dl 1.1 } catch (IllegalArgumentException success) {
1165     } finally {
1166     joinPool(e);
1167     }
1168     }
1169    
1170     /**
1171     * invokeAny(c) throws NPE if c has null elements
1172     */
1173 jsr166 1.5 public void testInvokeAny3() throws Exception {
1174     final CountDownLatch latch = new CountDownLatch(1);
1175 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1176     try {
1177     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1178 jsr166 1.5 l.add(new CheckedCallable<String>() {
1179     public String realCall() throws InterruptedException {
1180     latch.await();
1181     return TEST_STRING;
1182     }});
1183 dl 1.1 l.add(null);
1184     e.invokeAny(l);
1185 jsr166 1.5 shouldThrow();
1186 dl 1.1 } catch (NullPointerException success) {
1187     } finally {
1188 jsr166 1.5 latch.countDown();
1189 dl 1.1 joinPool(e);
1190     }
1191     }
1192    
1193     /**
1194     * invokeAny(c) throws ExecutionException if no task completes
1195     */
1196 jsr166 1.5 public void testInvokeAny4() throws Exception {
1197 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1198     try {
1199     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1200     l.add(new NPETask());
1201     e.invokeAny(l);
1202 jsr166 1.5 shouldThrow();
1203 dl 1.1 } catch (ExecutionException success) {
1204     } finally {
1205     joinPool(e);
1206     }
1207     }
1208    
1209     /**
1210     * invokeAny(c) returns result of some task
1211     */
1212 jsr166 1.5 public void testInvokeAny5() throws Exception {
1213 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1214     try {
1215     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1216     l.add(new StringTask());
1217     l.add(new StringTask());
1218     String result = e.invokeAny(l);
1219     assertSame(TEST_STRING, result);
1220     } finally {
1221     joinPool(e);
1222     }
1223     }
1224    
1225     /**
1226     * invokeAll(null) throws NPE
1227     */
1228 jsr166 1.5 public void testInvokeAll1() throws Exception {
1229 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1230     try {
1231     e.invokeAll(null);
1232 jsr166 1.5 shouldThrow();
1233 dl 1.1 } catch (NullPointerException success) {
1234     } finally {
1235     joinPool(e);
1236     }
1237     }
1238    
1239     /**
1240     * invokeAll(empty collection) returns empty collection
1241     */
1242 jsr166 1.5 public void testInvokeAll2() throws Exception {
1243 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1244     try {
1245     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1246     assertTrue(r.isEmpty());
1247     } finally {
1248     joinPool(e);
1249     }
1250     }
1251    
1252     /**
1253     * invokeAll(c) throws NPE if c has null elements
1254     */
1255 jsr166 1.5 public void testInvokeAll3() throws Exception {
1256 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1257     try {
1258     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1259     l.add(new StringTask());
1260     l.add(null);
1261     e.invokeAll(l);
1262 jsr166 1.5 shouldThrow();
1263 dl 1.1 } catch (NullPointerException success) {
1264     } finally {
1265     joinPool(e);
1266     }
1267     }
1268    
1269     /**
1270     * get of element of invokeAll(c) throws exception on failed task
1271     */
1272 jsr166 1.5 public void testInvokeAll4() throws Exception {
1273 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1274     try {
1275     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1276     l.add(new NPETask());
1277     List<Future<String>> result = e.invokeAll(l);
1278     assertEquals(1, result.size());
1279 jsr166 1.2 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1280 dl 1.1 it.next().get();
1281 jsr166 1.5 shouldThrow();
1282 jsr166 1.3 } catch (ExecutionException success) {
1283 dl 1.1 } finally {
1284     joinPool(e);
1285     }
1286     }
1287    
1288     /**
1289     * invokeAll(c) returns results of all completed tasks
1290     */
1291 jsr166 1.5 public void testInvokeAll5() throws Exception {
1292 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1293     try {
1294     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1295     l.add(new StringTask());
1296     l.add(new StringTask());
1297     List<Future<String>> result = e.invokeAll(l);
1298     assertEquals(2, result.size());
1299 jsr166 1.2 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1300 dl 1.1 assertSame(TEST_STRING, it.next().get());
1301 jsr166 1.5 shouldThrow();
1302 dl 1.1 } catch (ExecutionException success) {
1303     } finally {
1304     joinPool(e);
1305     }
1306     }
1307    
1308    
1309    
1310     /**
1311     * timed invokeAny(null) throws NPE
1312     */
1313 jsr166 1.5 public void testTimedInvokeAny1() throws Exception {
1314 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1315     try {
1316     e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1317 jsr166 1.5 shouldThrow();
1318 dl 1.1 } catch (NullPointerException success) {
1319     } finally {
1320     joinPool(e);
1321     }
1322     }
1323    
1324     /**
1325     * timed invokeAny(,,null) throws NPE
1326     */
1327 jsr166 1.5 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1328 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1329     try {
1330     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1331     l.add(new StringTask());
1332     e.invokeAny(l, MEDIUM_DELAY_MS, null);
1333 jsr166 1.5 shouldThrow();
1334 dl 1.1 } catch (NullPointerException success) {
1335     } finally {
1336     joinPool(e);
1337     }
1338     }
1339    
1340     /**
1341     * timed invokeAny(empty collection) throws IAE
1342     */
1343 jsr166 1.5 public void testTimedInvokeAny2() throws Exception {
1344 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1345     try {
1346     e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1347 jsr166 1.5 shouldThrow();
1348 dl 1.1 } catch (IllegalArgumentException success) {
1349     } finally {
1350     joinPool(e);
1351     }
1352     }
1353    
1354     /**
1355     * timed invokeAny(c) throws NPE if c has null elements
1356     */
1357 jsr166 1.5 public void testTimedInvokeAny3() throws Exception {
1358 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1359     try {
1360     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1361     l.add(new StringTask());
1362     l.add(null);
1363     e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1364 jsr166 1.5 shouldThrow();
1365 dl 1.1 } catch (NullPointerException success) {
1366     } finally {
1367     joinPool(e);
1368     }
1369     }
1370    
1371     /**
1372     * timed invokeAny(c) throws ExecutionException if no task completes
1373     */
1374 jsr166 1.5 public void testTimedInvokeAny4() throws Exception {
1375 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1376     try {
1377     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1378     l.add(new NPETask());
1379     e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1380 jsr166 1.5 shouldThrow();
1381 jsr166 1.3 } catch (ExecutionException success) {
1382 dl 1.1 } finally {
1383     joinPool(e);
1384     }
1385     }
1386    
1387     /**
1388     * timed invokeAny(c) returns result of some task
1389     */
1390 jsr166 1.5 public void testTimedInvokeAny5() throws Exception {
1391 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1392     try {
1393     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1394     l.add(new StringTask());
1395     l.add(new StringTask());
1396     String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1397     assertSame(TEST_STRING, result);
1398 jsr166 1.5 shouldThrow();
1399 dl 1.1 } catch (ExecutionException success) {
1400     } finally {
1401     joinPool(e);
1402     }
1403     }
1404    
1405     /**
1406     * timed invokeAll(null) throws NPE
1407     */
1408 jsr166 1.5 public void testTimedInvokeAll1() throws Exception {
1409 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1410     try {
1411     e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1412 jsr166 1.5 shouldThrow();
1413 dl 1.1 } catch (NullPointerException success) {
1414     } finally {
1415     joinPool(e);
1416     }
1417     }
1418    
1419     /**
1420     * timed invokeAll(,,null) throws NPE
1421     */
1422 jsr166 1.5 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1423 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1424     try {
1425     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1426     l.add(new StringTask());
1427     e.invokeAll(l, MEDIUM_DELAY_MS, null);
1428 jsr166 1.5 shouldThrow();
1429 dl 1.1 } catch (NullPointerException success) {
1430     } finally {
1431     joinPool(e);
1432     }
1433     }
1434    
1435     /**
1436     * timed invokeAll(empty collection) returns empty collection
1437     */
1438 jsr166 1.5 public void testTimedInvokeAll2() throws Exception {
1439 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1440     try {
1441     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1442     assertTrue(r.isEmpty());
1443     } finally {
1444     joinPool(e);
1445     }
1446     }
1447    
1448     /**
1449     * timed invokeAll(c) throws NPE if c has null elements
1450     */
1451 jsr166 1.5 public void testTimedInvokeAll3() throws Exception {
1452 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1453     try {
1454     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1455     l.add(new StringTask());
1456     l.add(null);
1457     e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1458 jsr166 1.5 shouldThrow();
1459 dl 1.1 } catch (NullPointerException success) {
1460     } finally {
1461     joinPool(e);
1462     }
1463     }
1464    
1465     /**
1466     * get of element of invokeAll(c) throws exception on failed task
1467     */
1468 jsr166 1.5 public void testTimedInvokeAll4() throws Exception {
1469 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1470     try {
1471     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1472     l.add(new NPETask());
1473     List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1474     assertEquals(1, result.size());
1475 jsr166 1.2 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1476 dl 1.1 it.next().get();
1477 jsr166 1.5 shouldThrow();
1478 jsr166 1.3 } catch (ExecutionException success) {
1479 dl 1.1 } finally {
1480     joinPool(e);
1481     }
1482     }
1483    
1484     /**
1485     * timed invokeAll(c) returns results of all completed tasks
1486     */
1487 jsr166 1.5 public void testTimedInvokeAll5() throws Exception {
1488 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1489     try {
1490     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1491     l.add(new StringTask());
1492     l.add(new StringTask());
1493     List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1494     assertEquals(2, result.size());
1495 jsr166 1.2 for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1496 dl 1.1 assertSame(TEST_STRING, it.next().get());
1497 jsr166 1.5 shouldThrow();
1498 dl 1.1 } catch (ExecutionException success) {
1499     } finally {
1500     joinPool(e);
1501     }
1502     }
1503    
1504     /**
1505     * timed invokeAll(c) cancels tasks not completed by timeout
1506     */
1507 jsr166 1.5 public void testTimedInvokeAll6() throws Exception {
1508 dl 1.1 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1509     try {
1510     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1511     l.add(new StringTask());
1512     l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1513     l.add(new StringTask());
1514     List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1515     assertEquals(3, result.size());
1516 jsr166 1.2 Iterator<Future<String>> it = result.iterator();
1517 dl 1.1 Future<String> f1 = it.next();
1518     Future<String> f2 = it.next();
1519     Future<String> f3 = it.next();
1520     assertTrue(f1.isDone());
1521     assertTrue(f2.isDone());
1522     assertTrue(f3.isDone());
1523     assertFalse(f1.isCancelled());
1524     assertTrue(f2.isCancelled());
1525     } finally {
1526     joinPool(e);
1527     }
1528     }
1529    
1530     /**
1531     * Execution continues if there is at least one thread even if
1532     * thread factory fails to create more
1533     */
1534 jsr166 1.5 public void testFailingThreadFactory() throws InterruptedException {
1535 dl 1.1 ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1536     try {
1537     ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1538     for (int k = 0; k < 100; ++k) {
1539     e.execute(new NoOpRunnable());
1540     }
1541     Thread.sleep(LONG_DELAY_MS);
1542     } finally {
1543     joinPool(e);
1544     }
1545     }
1546    
1547     /**
1548     * allowsCoreThreadTimeOut is by default false.
1549     */
1550     public void testAllowsCoreThreadTimeOut() {
1551     ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1552     assertFalse(tpe.allowsCoreThreadTimeOut());
1553     joinPool(tpe);
1554     }
1555    
1556     /**
1557     * allowCoreThreadTimeOut(true) causes idle threads to time out
1558     */
1559 jsr166 1.5 public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1560 dl 1.1 ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1561     tpe.allowCoreThreadTimeOut(true);
1562     tpe.execute(new NoOpRunnable());
1563     try {
1564     Thread.sleep(MEDIUM_DELAY_MS);
1565     assertEquals(0, tpe.getPoolSize());
1566     } finally {
1567     joinPool(tpe);
1568     }
1569     }
1570    
1571     /**
1572     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1573     */
1574 jsr166 1.5 public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1575 dl 1.1 ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1576     tpe.allowCoreThreadTimeOut(false);
1577     tpe.execute(new NoOpRunnable());
1578     try {
1579     Thread.sleep(MEDIUM_DELAY_MS);
1580     assertTrue(tpe.getPoolSize() >= 1);
1581     } finally {
1582     joinPool(tpe);
1583     }
1584     }
1585    
1586     }