ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java
Revision: 1.17
Committed: Tue Dec 1 22:51:44 2009 UTC (14 years, 5 months ago) by jsr166
Branch: MAIN
Changes since 1.16: +49 -60 lines
Log Message:
various improvements for invokeAll and invokeAny tests

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