[cvs] / jsr166 / src / test / tck / ThreadPoolExecutorSubclassTest.java Repository:
ViewVC logotype

Annotation of /jsr166/src/test/tck/ThreadPoolExecutorSubclassTest.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.59 - (view) (download)

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 : jsr166 1.25 * http://creativecommons.org/publicdomain/zero/1.0/
5 : jsr166 1.2 * Other contributors include Andrew Wright, Jeffrey Hayes,
6 :     * Pat Fisher, Mike Judd.
7 : dl 1.1 */
8 :    
9 : jsr166 1.9 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 : jsr166 1.36 import static java.util.concurrent.TimeUnit.SECONDS;
11 : jsr166 1.33
12 :     import java.util.ArrayList;
13 :     import java.util.List;
14 :     import java.util.concurrent.ArrayBlockingQueue;
15 :     import java.util.concurrent.BlockingQueue;
16 :     import java.util.concurrent.Callable;
17 : jsr166 1.39 import java.util.concurrent.CancellationException;
18 : jsr166 1.33 import java.util.concurrent.CountDownLatch;
19 :     import java.util.concurrent.ExecutionException;
20 :     import java.util.concurrent.Executors;
21 :     import java.util.concurrent.ExecutorService;
22 :     import java.util.concurrent.Future;
23 :     import java.util.concurrent.FutureTask;
24 :     import java.util.concurrent.LinkedBlockingQueue;
25 :     import java.util.concurrent.RejectedExecutionException;
26 :     import java.util.concurrent.RejectedExecutionHandler;
27 :     import java.util.concurrent.RunnableFuture;
28 :     import java.util.concurrent.SynchronousQueue;
29 :     import java.util.concurrent.ThreadFactory;
30 :     import java.util.concurrent.ThreadPoolExecutor;
31 :     import java.util.concurrent.TimeoutException;
32 :     import java.util.concurrent.TimeUnit;
33 : jsr166 1.41 import java.util.concurrent.atomic.AtomicInteger;
34 : jsr166 1.31 import java.util.concurrent.locks.Condition;
35 :     import java.util.concurrent.locks.ReentrantLock;
36 : jsr166 1.33
37 :     import junit.framework.Test;
38 :     import junit.framework.TestSuite;
39 : dl 1.1
40 :     public class ThreadPoolExecutorSubclassTest extends JSR166TestCase {
41 :     public static void main(String[] args) {
42 : jsr166 1.35 main(suite(), args);
43 : dl 1.1 }
44 :     public static Test suite() {
45 : jsr166 1.6 return new TestSuite(ThreadPoolExecutorSubclassTest.class);
46 : dl 1.1 }
47 :    
48 :     static class CustomTask<V> implements RunnableFuture<V> {
49 :     final Callable<V> callable;
50 :     final ReentrantLock lock = new ReentrantLock();
51 :     final Condition cond = lock.newCondition();
52 :     boolean done;
53 :     boolean cancelled;
54 :     V result;
55 :     Thread thread;
56 :     Exception exception;
57 : jsr166 1.6 CustomTask(Callable<V> c) {
58 :     if (c == null) throw new NullPointerException();
59 :     callable = c;
60 :     }
61 :     CustomTask(final Runnable r, final V res) {
62 :     if (r == null) throw new NullPointerException();
63 :     callable = new Callable<V>() {
64 : jsr166 1.34 public V call() throws Exception { r.run(); return res; }};
65 : dl 1.1 }
66 :     public boolean isDone() {
67 :     lock.lock(); try { return done; } finally { lock.unlock() ; }
68 :     }
69 :     public boolean isCancelled() {
70 :     lock.lock(); try { return cancelled; } finally { lock.unlock() ; }
71 :     }
72 :     public boolean cancel(boolean mayInterrupt) {
73 :     lock.lock();
74 :     try {
75 :     if (!done) {
76 :     cancelled = true;
77 :     done = true;
78 : jsr166 1.2 if (mayInterrupt && thread != null)
79 : dl 1.1 thread.interrupt();
80 :     return true;
81 :     }
82 :     return false;
83 :     }
84 :     finally { lock.unlock() ; }
85 :     }
86 :     public void run() {
87 :     lock.lock();
88 :     try {
89 : jsr166 1.22 if (done)
90 :     return;
91 :     thread = Thread.currentThread();
92 : dl 1.1 }
93 :     finally { lock.unlock() ; }
94 :     V v = null;
95 :     Exception e = null;
96 :     try {
97 :     v = callable.call();
98 :     }
99 : jsr166 1.3 catch (Exception ex) {
100 : dl 1.1 e = ex;
101 :     }
102 :     lock.lock();
103 :     try {
104 : jsr166 1.44 if (!done) {
105 :     result = v;
106 :     exception = e;
107 :     done = true;
108 :     thread = null;
109 :     cond.signalAll();
110 :     }
111 : dl 1.1 }
112 :     finally { lock.unlock(); }
113 :     }
114 :     public V get() throws InterruptedException, ExecutionException {
115 :     lock.lock();
116 :     try {
117 : jsr166 1.2 while (!done)
118 : dl 1.1 cond.await();
119 : jsr166 1.44 if (cancelled)
120 :     throw new CancellationException();
121 : dl 1.1 if (exception != null)
122 :     throw new ExecutionException(exception);
123 :     return result;
124 :     }
125 :     finally { lock.unlock(); }
126 :     }
127 :     public V get(long timeout, TimeUnit unit)
128 : jsr166 1.4 throws InterruptedException, ExecutionException, TimeoutException {
129 : dl 1.1 long nanos = unit.toNanos(timeout);
130 :     lock.lock();
131 :     try {
132 : jsr166 1.44 while (!done) {
133 : jsr166 1.47 if (nanos <= 0L)
134 : dl 1.1 throw new TimeoutException();
135 :     nanos = cond.awaitNanos(nanos);
136 :     }
137 : jsr166 1.44 if (cancelled)
138 :     throw new CancellationException();
139 : dl 1.1 if (exception != null)
140 :     throw new ExecutionException(exception);
141 :     return result;
142 :     }
143 :     finally { lock.unlock(); }
144 :     }
145 : jsr166 1.2 }
146 :    
147 : dl 1.1 static class CustomTPE extends ThreadPoolExecutor {
148 :     protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) {
149 :     return new CustomTask<V>(c);
150 :     }
151 :     protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) {
152 :     return new CustomTask<V>(r, v);
153 : jsr166 1.2 }
154 :    
155 : dl 1.1 CustomTPE(int corePoolSize,
156 :     int maximumPoolSize,
157 :     long keepAliveTime,
158 :     TimeUnit unit,
159 :     BlockingQueue<Runnable> workQueue) {
160 : jsr166 1.2 super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
161 : dl 1.1 workQueue);
162 :     }
163 :     CustomTPE(int corePoolSize,
164 :     int maximumPoolSize,
165 :     long keepAliveTime,
166 :     TimeUnit unit,
167 :     BlockingQueue<Runnable> workQueue,
168 :     ThreadFactory threadFactory) {
169 :     super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
170 :     threadFactory);
171 :     }
172 :    
173 :     CustomTPE(int corePoolSize,
174 :     int maximumPoolSize,
175 :     long keepAliveTime,
176 :     TimeUnit unit,
177 :     BlockingQueue<Runnable> workQueue,
178 :     RejectedExecutionHandler handler) {
179 :     super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue,
180 :     handler);
181 :     }
182 :     CustomTPE(int corePoolSize,
183 :     int maximumPoolSize,
184 :     long keepAliveTime,
185 :     TimeUnit unit,
186 :     BlockingQueue<Runnable> workQueue,
187 :     ThreadFactory threadFactory,
188 :     RejectedExecutionHandler handler) {
189 : jsr166 1.2 super(corePoolSize, maximumPoolSize, keepAliveTime, unit,
190 : dl 1.1 workQueue, threadFactory, handler);
191 :     }
192 :    
193 : jsr166 1.30 final CountDownLatch beforeCalled = new CountDownLatch(1);
194 :     final CountDownLatch afterCalled = new CountDownLatch(1);
195 :     final CountDownLatch terminatedCalled = new CountDownLatch(1);
196 :    
197 : dl 1.1 public CustomTPE() {
198 : jsr166 1.9 super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
199 : dl 1.1 }
200 :     protected void beforeExecute(Thread t, Runnable r) {
201 : jsr166 1.30 beforeCalled.countDown();
202 : dl 1.1 }
203 :     protected void afterExecute(Runnable r, Throwable t) {
204 : jsr166 1.30 afterCalled.countDown();
205 : dl 1.1 }
206 :     protected void terminated() {
207 : jsr166 1.30 terminatedCalled.countDown();
208 : dl 1.1 }
209 : jsr166 1.2
210 : jsr166 1.30 public boolean beforeCalled() {
211 :     return beforeCalled.getCount() == 0;
212 :     }
213 :     public boolean afterCalled() {
214 :     return afterCalled.getCount() == 0;
215 :     }
216 :     public boolean terminatedCalled() {
217 :     return terminatedCalled.getCount() == 0;
218 :     }
219 : dl 1.1 }
220 :    
221 : jsr166 1.4 static class FailingThreadFactory implements ThreadFactory {
222 : dl 1.1 int calls = 0;
223 : jsr166 1.4 public Thread newThread(Runnable r) {
224 : dl 1.1 if (++calls > 1) return null;
225 :     return new Thread(r);
226 : jsr166 1.2 }
227 : dl 1.1 }
228 : jsr166 1.2
229 : dl 1.1 /**
230 : jsr166 1.19 * execute successfully executes a runnable
231 : dl 1.1 */
232 : jsr166 1.5 public void testExecute() throws InterruptedException {
233 : jsr166 1.50 final ThreadPoolExecutor p =
234 :     new CustomTPE(1, 1,
235 :     2 * LONG_DELAY_MS, MILLISECONDS,
236 :     new ArrayBlockingQueue<Runnable>(10));
237 : jsr166 1.51 try (PoolCleaner cleaner = cleaner(p)) {
238 : jsr166 1.48 final CountDownLatch done = new CountDownLatch(1);
239 :     final Runnable task = new CheckedRunnable() {
240 : jsr166 1.50 public void realRun() { done.countDown(); }};
241 : jsr166 1.21 p.execute(task);
242 : jsr166 1.48 assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
243 : jsr166 1.2 }
244 : dl 1.1 }
245 :    
246 :     /**
247 : jsr166 1.19 * getActiveCount increases but doesn't overestimate, when a
248 :     * thread becomes active
249 : dl 1.1 */
250 : jsr166 1.5 public void testGetActiveCount() throws InterruptedException {
251 : jsr166 1.21 final ThreadPoolExecutor p =
252 :     new CustomTPE(2, 2,
253 :     LONG_DELAY_MS, MILLISECONDS,
254 :     new ArrayBlockingQueue<Runnable>(10));
255 :     final CountDownLatch threadStarted = new CountDownLatch(1);
256 :     final CountDownLatch done = new CountDownLatch(1);
257 : jsr166 1.51 try (PoolCleaner cleaner = cleaner(p)) {
258 : jsr166 1.21 assertEquals(0, p.getActiveCount());
259 :     p.execute(new CheckedRunnable() {
260 :     public void realRun() throws InterruptedException {
261 :     threadStarted.countDown();
262 :     assertEquals(1, p.getActiveCount());
263 :     done.await();
264 :     }});
265 :     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
266 :     assertEquals(1, p.getActiveCount());
267 :     done.countDown();
268 :     }
269 : dl 1.1 }
270 :    
271 :     /**
272 : jsr166 1.19 * prestartCoreThread starts a thread if under corePoolSize, else doesn't
273 : dl 1.1 */
274 :     public void testPrestartCoreThread() {
275 : jsr166 1.52 ThreadPoolExecutor p =
276 :     new CustomTPE(2, 6,
277 :     LONG_DELAY_MS, MILLISECONDS,
278 :     new ArrayBlockingQueue<Runnable>(10));
279 :     try (PoolCleaner cleaner = cleaner(p)) {
280 :     assertEquals(0, p.getPoolSize());
281 :     assertTrue(p.prestartCoreThread());
282 :     assertEquals(1, p.getPoolSize());
283 :     assertTrue(p.prestartCoreThread());
284 :     assertEquals(2, p.getPoolSize());
285 :     assertFalse(p.prestartCoreThread());
286 :     assertEquals(2, p.getPoolSize());
287 :     p.setCorePoolSize(4);
288 :     assertTrue(p.prestartCoreThread());
289 :     assertEquals(3, p.getPoolSize());
290 :     assertTrue(p.prestartCoreThread());
291 :     assertEquals(4, p.getPoolSize());
292 :     assertFalse(p.prestartCoreThread());
293 :     assertEquals(4, p.getPoolSize());
294 :     }
295 : dl 1.1 }
296 :    
297 :     /**
298 : jsr166 1.19 * prestartAllCoreThreads starts all corePoolSize threads
299 : dl 1.1 */
300 :     public void testPrestartAllCoreThreads() {
301 : jsr166 1.53 ThreadPoolExecutor p =
302 :     new CustomTPE(2, 6,
303 :     LONG_DELAY_MS, MILLISECONDS,
304 :     new ArrayBlockingQueue<Runnable>(10));
305 :     try (PoolCleaner cleaner = cleaner(p)) {
306 :     assertEquals(0, p.getPoolSize());
307 :     p.prestartAllCoreThreads();
308 :     assertEquals(2, p.getPoolSize());
309 :     p.prestartAllCoreThreads();
310 :     assertEquals(2, p.getPoolSize());
311 :     p.setCorePoolSize(4);
312 :     p.prestartAllCoreThreads();
313 :     assertEquals(4, p.getPoolSize());
314 :     p.prestartAllCoreThreads();
315 :     assertEquals(4, p.getPoolSize());
316 :     }
317 : dl 1.1 }
318 : jsr166 1.2
319 : dl 1.1 /**
320 : jsr166 1.19 * getCompletedTaskCount increases, but doesn't overestimate,
321 :     * when tasks complete
322 : dl 1.1 */
323 : jsr166 1.5 public void testGetCompletedTaskCount() throws InterruptedException {
324 : jsr166 1.21 final ThreadPoolExecutor p =
325 :     new CustomTPE(2, 2,
326 :     LONG_DELAY_MS, MILLISECONDS,
327 :     new ArrayBlockingQueue<Runnable>(10));
328 : jsr166 1.54 try (PoolCleaner cleaner = cleaner(p)) {
329 :     final CountDownLatch threadStarted = new CountDownLatch(1);
330 :     final CountDownLatch threadProceed = new CountDownLatch(1);
331 :     final CountDownLatch threadDone = new CountDownLatch(1);
332 : jsr166 1.21 assertEquals(0, p.getCompletedTaskCount());
333 :     p.execute(new CheckedRunnable() {
334 :     public void realRun() throws InterruptedException {
335 :     threadStarted.countDown();
336 :     assertEquals(0, p.getCompletedTaskCount());
337 :     threadProceed.await();
338 :     threadDone.countDown();
339 :     }});
340 : jsr166 1.30 await(threadStarted);
341 : jsr166 1.21 assertEquals(0, p.getCompletedTaskCount());
342 :     threadProceed.countDown();
343 :     threadDone.await();
344 : jsr166 1.30 long startTime = System.nanoTime();
345 :     while (p.getCompletedTaskCount() != 1) {
346 :     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
347 :     fail("timed out");
348 :     Thread.yield();
349 :     }
350 : jsr166 1.21 }
351 : dl 1.1 }
352 : jsr166 1.2
353 : dl 1.1 /**
354 : jsr166 1.19 * getCorePoolSize returns size given in constructor if not otherwise set
355 : dl 1.1 */
356 :     public void testGetCorePoolSize() {
357 : jsr166 1.59 final ThreadPoolExecutor p =
358 : jsr166 1.55 new CustomTPE(1, 1,
359 :     LONG_DELAY_MS, MILLISECONDS,
360 :     new ArrayBlockingQueue<Runnable>(10));
361 :     try (PoolCleaner cleaner = cleaner(p)) {
362 :     assertEquals(1, p.getCorePoolSize());
363 :     }
364 : dl 1.1 }
365 : jsr166 1.2
366 : dl 1.1 /**
367 : jsr166 1.19 * getKeepAliveTime returns value given in constructor if not otherwise set
368 : dl 1.1 */
369 :     public void testGetKeepAliveTime() {
370 : jsr166 1.59 final ThreadPoolExecutor p =
371 : jsr166 1.56 new CustomTPE(2, 2,
372 :     1000, MILLISECONDS,
373 :     new ArrayBlockingQueue<Runnable>(10));
374 :     try (PoolCleaner cleaner = cleaner(p)) {
375 :     assertEquals(1, p.getKeepAliveTime(SECONDS));
376 :     }
377 : dl 1.1 }
378 :    
379 : jsr166 1.2 /**
380 : dl 1.1 * getThreadFactory returns factory in constructor if not set
381 :     */
382 :     public void testGetThreadFactory() {
383 : jsr166 1.59 final ThreadFactory threadFactory = new SimpleThreadFactory();
384 :     final ThreadPoolExecutor p =
385 : jsr166 1.57 new CustomTPE(1, 2,
386 :     LONG_DELAY_MS, MILLISECONDS,
387 :     new ArrayBlockingQueue<Runnable>(10),
388 :     threadFactory,
389 :     new NoOpREHandler());
390 :     try (PoolCleaner cleaner = cleaner(p)) {
391 :     assertSame(threadFactory, p.getThreadFactory());
392 :     }
393 : dl 1.1 }
394 :    
395 : jsr166 1.2 /**
396 : dl 1.1 * setThreadFactory sets the thread factory returned by getThreadFactory
397 :     */
398 :     public void testSetThreadFactory() {
399 : jsr166 1.59 final ThreadPoolExecutor p =
400 : jsr166 1.57 new CustomTPE(1, 2,
401 :     LONG_DELAY_MS, MILLISECONDS,
402 :     new ArrayBlockingQueue<Runnable>(10));
403 :     try (PoolCleaner cleaner = cleaner(p)) {
404 :     ThreadFactory threadFactory = new SimpleThreadFactory();
405 :     p.setThreadFactory(threadFactory);
406 :     assertSame(threadFactory, p.getThreadFactory());
407 :     }
408 : dl 1.1 }
409 :    
410 : jsr166 1.2 /**
411 : dl 1.1 * setThreadFactory(null) throws NPE
412 :     */
413 :     public void testSetThreadFactoryNull() {
414 : jsr166 1.59 final ThreadPoolExecutor p =
415 : jsr166 1.58 new CustomTPE(1, 2,
416 :     LONG_DELAY_MS, MILLISECONDS,
417 :     new ArrayBlockingQueue<Runnable>(10));
418 :     try (PoolCleaner cleaner = cleaner(p)) {
419 :     try {
420 :     p.setThreadFactory(null);
421 :     shouldThrow();
422 :     } catch (NullPointerException success) {}
423 : dl 1.1 }
424 :     }
425 :    
426 : jsr166 1.2 /**
427 : dl 1.1 * getRejectedExecutionHandler returns handler in constructor if not set
428 :     */
429 :     public void testGetRejectedExecutionHandler() {
430 : jsr166 1.59 final RejectedExecutionHandler h = new NoOpREHandler();
431 :     final ThreadPoolExecutor p =
432 :     new CustomTPE(1, 2,
433 :     LONG_DELAY_MS, MILLISECONDS,
434 :     new ArrayBlockingQueue<Runnable>(10),
435 :     h);
436 :     try (PoolCleaner cleaner = cleaner(p)) {
437 :     assertSame(h, p.getRejectedExecutionHandler());
438 :     }
439 : dl 1.1 }
440 :    
441 : jsr166 1.2 /**
442 : dl 1.1 * setRejectedExecutionHandler sets the handler returned by
443 :     * getRejectedExecutionHandler
444 :     */
445 :     public void testSetRejectedExecutionHandler() {
446 : jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447 : dl 1.1 RejectedExecutionHandler h = new NoOpREHandler();
448 :     p.setRejectedExecutionHandler(h);
449 :     assertSame(h, p.getRejectedExecutionHandler());
450 :     joinPool(p);
451 :     }
452 :    
453 : jsr166 1.2 /**
454 : dl 1.1 * setRejectedExecutionHandler(null) throws NPE
455 :     */
456 :     public void testSetRejectedExecutionHandlerNull() {
457 : jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
458 : dl 1.1 try {
459 :     p.setRejectedExecutionHandler(null);
460 :     shouldThrow();
461 :     } catch (NullPointerException success) {
462 :     } finally {
463 :     joinPool(p);
464 :     }
465 :     }
466 :    
467 :     /**
468 : jsr166 1.19 * getLargestPoolSize increases, but doesn't overestimate, when
469 :     * multiple threads active
470 : dl 1.1 */
471 : jsr166 1.5 public void testGetLargestPoolSize() throws InterruptedException {
472 : jsr166 1.21 final int THREADS = 3;
473 :     final ThreadPoolExecutor p =
474 :     new CustomTPE(THREADS, THREADS,
475 :     LONG_DELAY_MS, MILLISECONDS,
476 :     new ArrayBlockingQueue<Runnable>(10));
477 :     final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
478 :     final CountDownLatch done = new CountDownLatch(1);
479 :     try {
480 :     assertEquals(0, p.getLargestPoolSize());
481 :     for (int i = 0; i < THREADS; i++)
482 :     p.execute(new CheckedRunnable() {
483 :     public void realRun() throws InterruptedException {
484 :     threadsStarted.countDown();
485 :     done.await();
486 :     assertEquals(THREADS, p.getLargestPoolSize());
487 :     }});
488 :     assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
489 :     assertEquals(THREADS, p.getLargestPoolSize());
490 :     } finally {
491 :     done.countDown();
492 :     joinPool(p);
493 :     assertEquals(THREADS, p.getLargestPoolSize());
494 :     }
495 : dl 1.1 }
496 : jsr166 1.2
497 : dl 1.1 /**
498 : jsr166 1.19 * getMaximumPoolSize returns value given in constructor if not
499 :     * otherwise set
500 : dl 1.1 */
501 :     public void testGetMaximumPoolSize() {
502 : jsr166 1.21 ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
503 :     assertEquals(2, p.getMaximumPoolSize());
504 :     joinPool(p);
505 : dl 1.1 }
506 : jsr166 1.2
507 : dl 1.1 /**
508 : jsr166 1.19 * getPoolSize increases, but doesn't overestimate, when threads
509 :     * become active
510 : dl 1.1 */
511 : jsr166 1.21 public void testGetPoolSize() throws InterruptedException {
512 :     final ThreadPoolExecutor p =
513 :     new CustomTPE(1, 1,
514 :     LONG_DELAY_MS, MILLISECONDS,
515 :     new ArrayBlockingQueue<Runnable>(10));
516 :     final CountDownLatch threadStarted = new CountDownLatch(1);
517 :     final CountDownLatch done = new CountDownLatch(1);
518 :     try {
519 :     assertEquals(0, p.getPoolSize());
520 :     p.execute(new CheckedRunnable() {
521 :     public void realRun() throws InterruptedException {
522 :     threadStarted.countDown();
523 :     assertEquals(1, p.getPoolSize());
524 :     done.await();
525 :     }});
526 :     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
527 :     assertEquals(1, p.getPoolSize());
528 :     } finally {
529 :     done.countDown();
530 :     joinPool(p);
531 :     }
532 : dl 1.1 }
533 : jsr166 1.2
534 : dl 1.1 /**
535 : jsr166 1.19 * getTaskCount increases, but doesn't overestimate, when tasks submitted
536 : dl 1.1 */
537 : jsr166 1.5 public void testGetTaskCount() throws InterruptedException {
538 : jsr166 1.21 final ThreadPoolExecutor p =
539 :     new CustomTPE(1, 1,
540 :     LONG_DELAY_MS, MILLISECONDS,
541 :     new ArrayBlockingQueue<Runnable>(10));
542 :     final CountDownLatch threadStarted = new CountDownLatch(1);
543 :     final CountDownLatch done = new CountDownLatch(1);
544 :     try {
545 :     assertEquals(0, p.getTaskCount());
546 :     p.execute(new CheckedRunnable() {
547 :     public void realRun() throws InterruptedException {
548 :     threadStarted.countDown();
549 :     assertEquals(1, p.getTaskCount());
550 :     done.await();
551 :     }});
552 :     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
553 :     assertEquals(1, p.getTaskCount());
554 :     } finally {
555 :     done.countDown();
556 :     joinPool(p);
557 :     }
558 : dl 1.1 }
559 : jsr166 1.2
560 : dl 1.1 /**
561 : jsr166 1.28 * isShutdown is false before shutdown, true after
562 : dl 1.1 */
563 :     public void testIsShutdown() {
564 : jsr166 1.2
565 : jsr166 1.21 ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
566 :     assertFalse(p.isShutdown());
567 :     try { p.shutdown(); } catch (SecurityException ok) { return; }
568 :     assertTrue(p.isShutdown());
569 :     joinPool(p);
570 : dl 1.1 }
571 :    
572 :     /**
573 : jsr166 1.19 * isTerminated is false before termination, true after
574 : dl 1.1 */
575 : jsr166 1.5 public void testIsTerminated() throws InterruptedException {
576 : jsr166 1.21 final ThreadPoolExecutor p =
577 :     new CustomTPE(1, 1,
578 :     LONG_DELAY_MS, MILLISECONDS,
579 :     new ArrayBlockingQueue<Runnable>(10));
580 :     final CountDownLatch threadStarted = new CountDownLatch(1);
581 :     final CountDownLatch done = new CountDownLatch(1);
582 :     try {
583 :     assertFalse(p.isTerminating());
584 :     p.execute(new CheckedRunnable() {
585 :     public void realRun() throws InterruptedException {
586 : jsr166 1.24 assertFalse(p.isTerminating());
587 : jsr166 1.21 threadStarted.countDown();
588 :     done.await();
589 :     }});
590 :     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
591 :     assertFalse(p.isTerminating());
592 :     done.countDown();
593 :     } finally {
594 :     try { p.shutdown(); } catch (SecurityException ok) { return; }
595 :     }
596 :     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
597 :     assertTrue(p.isTerminated());
598 :     assertFalse(p.isTerminating());
599 : dl 1.1 }
600 :    
601 :     /**
602 : jsr166 1.19 * isTerminating is not true when running or when terminated
603 : dl 1.1 */
604 : jsr166 1.5 public void testIsTerminating() throws InterruptedException {
605 : jsr166 1.21 final ThreadPoolExecutor p =
606 :     new CustomTPE(1, 1,
607 :     LONG_DELAY_MS, MILLISECONDS,
608 :     new ArrayBlockingQueue<Runnable>(10));
609 :     final CountDownLatch threadStarted = new CountDownLatch(1);
610 :     final CountDownLatch done = new CountDownLatch(1);
611 :     try {
612 :     assertFalse(p.isTerminating());
613 :     p.execute(new CheckedRunnable() {
614 :     public void realRun() throws InterruptedException {
615 : jsr166 1.23 assertFalse(p.isTerminating());
616 : jsr166 1.21 threadStarted.countDown();
617 :     done.await();
618 :     }});
619 :     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
620 :     assertFalse(p.isTerminating());
621 :     done.countDown();
622 :     } finally {
623 :     try { p.shutdown(); } catch (SecurityException ok) { return; }
624 :     }
625 :     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
626 :     assertTrue(p.isTerminated());
627 :     assertFalse(p.isTerminating());
628 : dl 1.1 }
629 :    
630 :     /**
631 :     * getQueue returns the work queue, which contains queued tasks
632 :     */
633 : jsr166 1.5 public void testGetQueue() throws InterruptedException {
634 : jsr166 1.21 final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
635 :     final ThreadPoolExecutor p =
636 :     new CustomTPE(1, 1,
637 :     LONG_DELAY_MS, MILLISECONDS,
638 :     q);
639 :     final CountDownLatch threadStarted = new CountDownLatch(1);
640 :     final CountDownLatch done = new CountDownLatch(1);
641 :     try {
642 :     FutureTask[] tasks = new FutureTask[5];
643 :     for (int i = 0; i < tasks.length; i++) {
644 :     Callable task = new CheckedCallable<Boolean>() {
645 :     public Boolean realCall() throws InterruptedException {
646 :     threadStarted.countDown();
647 :     assertSame(q, p.getQueue());
648 :     done.await();
649 :     return Boolean.TRUE;
650 :     }};
651 :     tasks[i] = new FutureTask(task);
652 :     p.execute(tasks[i]);
653 :     }
654 :     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
655 :     assertSame(q, p.getQueue());
656 :     assertFalse(q.contains(tasks[0]));
657 :     assertTrue(q.contains(tasks[tasks.length - 1]));
658 :     assertEquals(tasks.length - 1, q.size());
659 : dl 1.1 } finally {
660 : jsr166 1.21 done.countDown();
661 :     joinPool(p);
662 : dl 1.1 }
663 :     }
664 :    
665 :     /**
666 :     * remove(task) removes queued task, and fails to remove active task
667 :     */
668 : jsr166 1.5 public void testRemove() throws InterruptedException {
669 : dl 1.1 BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
670 : jsr166 1.21 final ThreadPoolExecutor p =
671 :     new CustomTPE(1, 1,
672 :     LONG_DELAY_MS, MILLISECONDS,
673 :     q);
674 :     Runnable[] tasks = new Runnable[6];
675 :     final CountDownLatch threadStarted = new CountDownLatch(1);
676 :     final CountDownLatch done = new CountDownLatch(1);
677 :     try {
678 :     for (int i = 0; i < tasks.length; i++) {
679 :     tasks[i] = new CheckedRunnable() {
680 :     public void realRun() throws InterruptedException {
681 :     threadStarted.countDown();
682 :     done.await();
683 :     }};
684 :     p.execute(tasks[i]);
685 :     }
686 :     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
687 :     assertFalse(p.remove(tasks[0]));
688 : dl 1.1 assertTrue(q.contains(tasks[4]));
689 :     assertTrue(q.contains(tasks[3]));
690 : jsr166 1.21 assertTrue(p.remove(tasks[4]));
691 :     assertFalse(p.remove(tasks[4]));
692 : dl 1.1 assertFalse(q.contains(tasks[4]));
693 :     assertTrue(q.contains(tasks[3]));
694 : jsr166 1.21 assertTrue(p.remove(tasks[3]));
695 : dl 1.1 assertFalse(q.contains(tasks[3]));
696 :     } finally {
697 : jsr166 1.21 done.countDown();
698 :     joinPool(p);
699 : dl 1.1 }
700 :     }
701 :    
702 :     /**
703 : jsr166 1.19 * purge removes cancelled tasks from the queue
704 : dl 1.1 */
705 : jsr166 1.21 public void testPurge() throws InterruptedException {
706 :     final CountDownLatch threadStarted = new CountDownLatch(1);
707 :     final CountDownLatch done = new CountDownLatch(1);
708 :     final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
709 :     final ThreadPoolExecutor p =
710 :     new CustomTPE(1, 1,
711 :     LONG_DELAY_MS, MILLISECONDS,
712 :     q);
713 : dl 1.1 FutureTask[] tasks = new FutureTask[5];
714 : jsr166 1.21 try {
715 :     for (int i = 0; i < tasks.length; i++) {
716 :     Callable task = new CheckedCallable<Boolean>() {
717 :     public Boolean realCall() throws InterruptedException {
718 :     threadStarted.countDown();
719 :     done.await();
720 :     return Boolean.TRUE;
721 :     }};
722 :     tasks[i] = new FutureTask(task);
723 :     p.execute(tasks[i]);
724 :     }
725 :     assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
726 :     assertEquals(tasks.length, p.getTaskCount());
727 :     assertEquals(tasks.length - 1, q.size());
728 :     assertEquals(1L, p.getActiveCount());
729 :     assertEquals(0L, p.getCompletedTaskCount());
730 :     tasks[4].cancel(true);
731 :     tasks[3].cancel(false);
732 :     p.purge();
733 :     assertEquals(tasks.length - 3, q.size());
734 :     assertEquals(tasks.length - 2, p.getTaskCount());
735 :     p.purge(); // Nothing to do
736 :     assertEquals(tasks.length - 3, q.size());
737 :     assertEquals(tasks.length - 2, p.getTaskCount());
738 :     } finally {
739 :     done.countDown();
740 :     joinPool(p);
741 :     }
742 : dl 1.1 }
743 :    
744 :     /**
745 : jsr166 1.40 * shutdownNow returns a list containing tasks that were not run,
746 :     * and those tasks are drained from the queue
747 : dl 1.1 */
748 : jsr166 1.41 public void testShutdownNow() throws InterruptedException {
749 :     final int poolSize = 2;
750 :     final int count = 5;
751 :     final AtomicInteger ran = new AtomicInteger(0);
752 :     ThreadPoolExecutor p =
753 :     new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS,
754 :     new ArrayBlockingQueue<Runnable>(10));
755 :     CountDownLatch threadsStarted = new CountDownLatch(poolSize);
756 : jsr166 1.43 Runnable waiter = new CheckedRunnable() { public void realRun() {
757 : jsr166 1.41 threadsStarted.countDown();
758 : dl 1.1 try {
759 : jsr166 1.41 MILLISECONDS.sleep(2 * LONG_DELAY_MS);
760 :     } catch (InterruptedException success) {}
761 :     ran.getAndIncrement();
762 :     }};
763 :     for (int i = 0; i < count; i++)
764 :     p.execute(waiter);
765 :     assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
766 : jsr166 1.42 assertEquals(poolSize, p.getActiveCount());
767 :     assertEquals(0, p.getCompletedTaskCount());
768 : jsr166 1.41 final List<Runnable> queuedTasks;
769 :     try {
770 :     queuedTasks = p.shutdownNow();
771 :     } catch (SecurityException ok) {
772 :     return; // Allowed in case test doesn't have privs
773 : dl 1.1 }
774 : jsr166 1.21 assertTrue(p.isShutdown());
775 : jsr166 1.40 assertTrue(p.getQueue().isEmpty());
776 : jsr166 1.41 assertEquals(count - poolSize, queuedTasks.size());
777 :     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
778 :     assertTrue(p.isTerminated());
779 :     assertEquals(poolSize, ran.get());
780 : jsr166 1.42 assertEquals(poolSize, p.getCompletedTaskCount());
781 : dl 1.1 }
782 :    
783 :     // Exception Tests
784 :    
785 : jsr166 1.2 /**
786 :     * Constructor throws if corePoolSize argument is less than zero
787 : dl 1.1 */
788 :     public void testConstructor1() {
789 :     try {
790 : jsr166 1.36 new CustomTPE(-1, 1, 1L, SECONDS,
791 :     new ArrayBlockingQueue<Runnable>(10));
792 : dl 1.1 shouldThrow();
793 : jsr166 1.5 } catch (IllegalArgumentException success) {}
794 : dl 1.1 }
795 : jsr166 1.2
796 :     /**
797 :     * Constructor throws if maximumPoolSize is less than zero
798 : dl 1.1 */
799 :     public void testConstructor2() {
800 :     try {
801 : jsr166 1.36 new CustomTPE(1, -1, 1L, SECONDS,
802 :     new ArrayBlockingQueue<Runnable>(10));
803 : dl 1.1 shouldThrow();
804 : jsr166 1.5 } catch (IllegalArgumentException success) {}
805 : dl 1.1 }
806 : jsr166 1.2
807 :     /**
808 :     * Constructor throws if maximumPoolSize is equal to zero
809 : dl 1.1 */
810 :     public void testConstructor3() {
811 :     try {
812 : jsr166 1.36 new CustomTPE(1, 0, 1L, SECONDS,
813 :     new ArrayBlockingQueue<Runnable>(10));
814 : dl 1.1 shouldThrow();
815 : jsr166 1.5 } catch (IllegalArgumentException success) {}
816 : dl 1.1 }
817 :    
818 : jsr166 1.2 /**
819 :     * Constructor throws if keepAliveTime is less than zero
820 : dl 1.1 */
821 :     public void testConstructor4() {
822 :     try {
823 : jsr166 1.36 new CustomTPE(1, 2, -1L, SECONDS,
824 :     new ArrayBlockingQueue<Runnable>(10));
825 : dl 1.1 shouldThrow();
826 : jsr166 1.5 } catch (IllegalArgumentException success) {}
827 : dl 1.1 }
828 :    
829 : jsr166 1.2 /**
830 :     * Constructor throws if corePoolSize is greater than the maximumPoolSize
831 : dl 1.1 */
832 :     public void testConstructor5() {
833 :     try {
834 : jsr166 1.36 new CustomTPE(2, 1, 1L, SECONDS,
835 :     new ArrayBlockingQueue<Runnable>(10));
836 : dl 1.1 shouldThrow();
837 : jsr166 1.5 } catch (IllegalArgumentException success) {}
838 : dl 1.1 }
839 : jsr166 1.2
840 :     /**
841 :     * Constructor throws if workQueue is set to null
842 : dl 1.1 */
843 :     public void testConstructorNullPointerException() {
844 :     try {
845 : jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS, null);
846 : dl 1.1 shouldThrow();
847 : jsr166 1.5 } catch (NullPointerException success) {}
848 : dl 1.1 }
849 :    
850 : jsr166 1.2 /**
851 :     * Constructor throws if corePoolSize argument is less than zero
852 : dl 1.1 */
853 :     public void testConstructor6() {
854 :     try {
855 : jsr166 1.36 new CustomTPE(-1, 1, 1L, SECONDS,
856 :     new ArrayBlockingQueue<Runnable>(10),
857 :     new SimpleThreadFactory());
858 : dl 1.1 shouldThrow();
859 : jsr166 1.4 } catch (IllegalArgumentException success) {}
860 : dl 1.1 }
861 : jsr166 1.2
862 :     /**
863 :     * Constructor throws if maximumPoolSize is less than zero
864 : dl 1.1 */
865 :     public void testConstructor7() {
866 :     try {
867 : jsr166 1.36 new CustomTPE(1,-1, 1L, SECONDS,
868 :     new ArrayBlockingQueue<Runnable>(10),
869 :     new SimpleThreadFactory());
870 : dl 1.1 shouldThrow();
871 : jsr166 1.5 } catch (IllegalArgumentException success) {}
872 : dl 1.1 }
873 :    
874 : jsr166 1.2 /**
875 :     * Constructor throws if maximumPoolSize is equal to zero
876 : dl 1.1 */
877 :     public void testConstructor8() {
878 :     try {
879 : jsr166 1.36 new CustomTPE(1, 0, 1L, SECONDS,
880 :     new ArrayBlockingQueue<Runnable>(10),
881 :     new SimpleThreadFactory());
882 : dl 1.1 shouldThrow();
883 : jsr166 1.5 } catch (IllegalArgumentException success) {}
884 : dl 1.1 }
885 :    
886 : jsr166 1.2 /**
887 :     * Constructor throws if keepAliveTime is less than zero
888 : dl 1.1 */
889 :     public void testConstructor9() {
890 :     try {
891 : jsr166 1.36 new CustomTPE(1, 2, -1L, SECONDS,
892 :     new ArrayBlockingQueue<Runnable>(10),
893 :     new SimpleThreadFactory());
894 : dl 1.1 shouldThrow();
895 : jsr166 1.5 } catch (IllegalArgumentException success) {}
896 : dl 1.1 }
897 :    
898 : jsr166 1.2 /**
899 :     * Constructor throws if corePoolSize is greater than the maximumPoolSize
900 : dl 1.1 */
901 :     public void testConstructor10() {
902 :     try {
903 : jsr166 1.36 new CustomTPE(2, 1, 1L, SECONDS,
904 :     new ArrayBlockingQueue<Runnable>(10),
905 :     new SimpleThreadFactory());
906 : dl 1.1 shouldThrow();
907 : jsr166 1.5 } catch (IllegalArgumentException success) {}
908 : dl 1.1 }
909 :    
910 : jsr166 1.2 /**
911 :     * Constructor throws if workQueue is set to null
912 : dl 1.1 */
913 :     public void testConstructorNullPointerException2() {
914 :     try {
915 : jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory());
916 : dl 1.1 shouldThrow();
917 : jsr166 1.5 } catch (NullPointerException success) {}
918 : dl 1.1 }
919 :    
920 : jsr166 1.2 /**
921 :     * Constructor throws if threadFactory is set to null
922 : dl 1.1 */
923 :     public void testConstructorNullPointerException3() {
924 :     try {
925 : jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
926 :     new ArrayBlockingQueue<Runnable>(10),
927 :     (ThreadFactory) null);
928 : dl 1.1 shouldThrow();
929 : jsr166 1.5 } catch (NullPointerException success) {}
930 : dl 1.1 }
931 : jsr166 1.2
932 :     /**
933 :     * Constructor throws if corePoolSize argument is less than zero
934 : dl 1.1 */
935 :     public void testConstructor11() {
936 :     try {
937 : jsr166 1.36 new CustomTPE(-1, 1, 1L, SECONDS,
938 :     new ArrayBlockingQueue<Runnable>(10),
939 :     new NoOpREHandler());
940 : dl 1.1 shouldThrow();
941 : jsr166 1.5 } catch (IllegalArgumentException success) {}
942 : dl 1.1 }
943 :    
944 : jsr166 1.2 /**
945 :     * Constructor throws if maximumPoolSize is less than zero
946 : dl 1.1 */
947 :     public void testConstructor12() {
948 :     try {
949 : jsr166 1.36 new CustomTPE(1, -1, 1L, SECONDS,
950 :     new ArrayBlockingQueue<Runnable>(10),
951 :     new NoOpREHandler());
952 : dl 1.1 shouldThrow();
953 : jsr166 1.5 } catch (IllegalArgumentException success) {}
954 : dl 1.1 }
955 :    
956 : jsr166 1.2 /**
957 :     * Constructor throws if maximumPoolSize is equal to zero
958 : dl 1.1 */
959 :     public void testConstructor13() {
960 :     try {
961 : jsr166 1.36 new CustomTPE(1, 0, 1L, SECONDS,
962 :     new ArrayBlockingQueue<Runnable>(10),
963 :     new NoOpREHandler());
964 : dl 1.1 shouldThrow();
965 : jsr166 1.5 } catch (IllegalArgumentException success) {}
966 : dl 1.1 }
967 :    
968 : jsr166 1.2 /**
969 :     * Constructor throws if keepAliveTime is less than zero
970 : dl 1.1 */
971 :     public void testConstructor14() {
972 :     try {
973 : jsr166 1.36 new CustomTPE(1, 2, -1L, SECONDS,
974 :     new ArrayBlockingQueue<Runnable>(10),
975 :     new NoOpREHandler());
976 : dl 1.1 shouldThrow();
977 : jsr166 1.5 } catch (IllegalArgumentException success) {}
978 : dl 1.1 }
979 :    
980 : jsr166 1.2 /**
981 :     * Constructor throws if corePoolSize is greater than the maximumPoolSize
982 : dl 1.1 */
983 :     public void testConstructor15() {
984 :     try {
985 : jsr166 1.36 new CustomTPE(2, 1, 1L, SECONDS,
986 :     new ArrayBlockingQueue<Runnable>(10),
987 :     new NoOpREHandler());
988 : dl 1.1 shouldThrow();
989 : jsr166 1.5 } catch (IllegalArgumentException success) {}
990 : dl 1.1 }
991 :    
992 : jsr166 1.2 /**
993 :     * Constructor throws if workQueue is set to null
994 : dl 1.1 */
995 :     public void testConstructorNullPointerException4() {
996 :     try {
997 : jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
998 :     null,
999 :     new NoOpREHandler());
1000 : dl 1.1 shouldThrow();
1001 : jsr166 1.5 } catch (NullPointerException success) {}
1002 : dl 1.1 }
1003 :    
1004 : jsr166 1.2 /**
1005 :     * Constructor throws if handler is set to null
1006 : dl 1.1 */
1007 :     public void testConstructorNullPointerException5() {
1008 :     try {
1009 : jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1010 :     new ArrayBlockingQueue<Runnable>(10),
1011 :     (RejectedExecutionHandler) null);
1012 : dl 1.1 shouldThrow();
1013 : jsr166 1.5 } catch (NullPointerException success) {}
1014 : dl 1.1 }
1015 :    
1016 : jsr166 1.2 /**
1017 :     * Constructor throws if corePoolSize argument is less than zero
1018 : dl 1.1 */
1019 :     public void testConstructor16() {
1020 :     try {
1021 : jsr166 1.36 new CustomTPE(-1, 1, 1L, SECONDS,
1022 :     new ArrayBlockingQueue<Runnable>(10),
1023 :     new SimpleThreadFactory(),
1024 :     new NoOpREHandler());
1025 : dl 1.1 shouldThrow();
1026 : jsr166 1.5 } catch (IllegalArgumentException success) {}
1027 : dl 1.1 }
1028 :    
1029 : jsr166 1.2 /**
1030 :     * Constructor throws if maximumPoolSize is less than zero
1031 : dl 1.1 */
1032 :     public void testConstructor17() {
1033 :     try {
1034 : jsr166 1.36 new CustomTPE(1, -1, 1L, SECONDS,
1035 :     new ArrayBlockingQueue<Runnable>(10),
1036 :     new SimpleThreadFactory(),
1037 :     new NoOpREHandler());
1038 : dl 1.1 shouldThrow();
1039 : jsr166 1.5 } catch (IllegalArgumentException success) {}
1040 : dl 1.1 }
1041 :    
1042 : jsr166 1.2 /**
1043 :     * Constructor throws if maximumPoolSize is equal to zero
1044 : dl 1.1 */
1045 :     public void testConstructor18() {
1046 :     try {
1047 : jsr166 1.36 new CustomTPE(1, 0, 1L, SECONDS,
1048 :     new ArrayBlockingQueue<Runnable>(10),
1049 :     new SimpleThreadFactory(),
1050 :     new NoOpREHandler());
1051 : dl 1.1 shouldThrow();
1052 : jsr166 1.5 } catch (IllegalArgumentException success) {}
1053 : dl 1.1 }
1054 :    
1055 : jsr166 1.2 /**
1056 :     * Constructor throws if keepAliveTime is less than zero
1057 : dl 1.1 */
1058 :     public void testConstructor19() {
1059 :     try {
1060 : jsr166 1.36 new CustomTPE(1, 2, -1L, SECONDS,
1061 :     new ArrayBlockingQueue<Runnable>(10),
1062 :     new SimpleThreadFactory(),
1063 :     new NoOpREHandler());
1064 : dl 1.1 shouldThrow();
1065 : jsr166 1.5 } catch (IllegalArgumentException success) {}
1066 : dl 1.1 }
1067 :    
1068 : jsr166 1.2 /**
1069 :     * Constructor throws if corePoolSize is greater than the maximumPoolSize
1070 : dl 1.1 */
1071 :     public void testConstructor20() {
1072 :     try {
1073 : jsr166 1.36 new CustomTPE(2, 1, 1L, SECONDS,
1074 :     new ArrayBlockingQueue<Runnable>(10),
1075 :     new SimpleThreadFactory(),
1076 :     new NoOpREHandler());
1077 : dl 1.1 shouldThrow();
1078 : jsr166 1.5 } catch (IllegalArgumentException success) {}
1079 : dl 1.1 }
1080 :    
1081 : jsr166 1.2 /**
1082 : jsr166 1.20 * Constructor throws if workQueue is null
1083 : dl 1.1 */
1084 :     public void testConstructorNullPointerException6() {
1085 :     try {
1086 : jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1087 :     null,
1088 :     new SimpleThreadFactory(),
1089 :     new NoOpREHandler());
1090 : dl 1.1 shouldThrow();
1091 : jsr166 1.5 } catch (NullPointerException success) {}
1092 : dl 1.1 }
1093 :    
1094 : jsr166 1.2 /**
1095 : jsr166 1.20 * Constructor throws if handler is null
1096 : dl 1.1 */
1097 :     public void testConstructorNullPointerException7() {
1098 :     try {
1099 : jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1100 :     new ArrayBlockingQueue<Runnable>(10),
1101 :     new SimpleThreadFactory(),
1102 :     (RejectedExecutionHandler) null);
1103 : dl 1.1 shouldThrow();
1104 : jsr166 1.5 } catch (NullPointerException success) {}
1105 : dl 1.1 }
1106 :    
1107 : jsr166 1.2 /**
1108 : jsr166 1.20 * Constructor throws if ThreadFactory is null
1109 : dl 1.1 */
1110 :     public void testConstructorNullPointerException8() {
1111 :     try {
1112 : jsr166 1.36 new CustomTPE(1, 2, 1L, SECONDS,
1113 : jsr166 1.21 new ArrayBlockingQueue<Runnable>(10),
1114 :     (ThreadFactory) null,
1115 :     new NoOpREHandler());
1116 : dl 1.1 shouldThrow();
1117 : jsr166 1.5 } catch (NullPointerException success) {}
1118 : dl 1.1 }
1119 : jsr166 1.2
1120 : dl 1.1 /**
1121 : jsr166 1.19 * execute throws RejectedExecutionException if saturated.
1122 : dl 1.1 */
1123 :     public void testSaturatedExecute() {
1124 : jsr166 1.21 ThreadPoolExecutor p =
1125 :     new CustomTPE(1, 1,
1126 :     LONG_DELAY_MS, MILLISECONDS,
1127 :     new ArrayBlockingQueue<Runnable>(1));
1128 :     final CountDownLatch done = new CountDownLatch(1);
1129 :     try {
1130 :     Runnable task = new CheckedRunnable() {
1131 :     public void realRun() throws InterruptedException {
1132 :     done.await();
1133 :     }};
1134 :     for (int i = 0; i < 2; ++i)
1135 :     p.execute(task);
1136 :     for (int i = 0; i < 2; ++i) {
1137 :     try {
1138 :     p.execute(task);
1139 :     shouldThrow();
1140 :     } catch (RejectedExecutionException success) {}
1141 :     assertTrue(p.getTaskCount() <= 2);
1142 : dl 1.1 }
1143 : jsr166 1.21 } finally {
1144 :     done.countDown();
1145 :     joinPool(p);
1146 :     }
1147 : dl 1.1 }
1148 :    
1149 :     /**
1150 : jsr166 1.19 * executor using CallerRunsPolicy runs task if saturated.
1151 : dl 1.1 */
1152 :     public void testSaturatedExecute2() {
1153 :     RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1154 : jsr166 1.21 ThreadPoolExecutor p = new CustomTPE(1, 1,
1155 :     LONG_DELAY_MS, MILLISECONDS,
1156 :     new ArrayBlockingQueue<Runnable>(1),
1157 :     h);
1158 : dl 1.1 try {
1159 :     TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1160 : jsr166 1.21 for (int i = 0; i < tasks.length; ++i)
1161 : dl 1.1 tasks[i] = new TrackedNoOpRunnable();
1162 :     TrackedLongRunnable mr = new TrackedLongRunnable();
1163 :     p.execute(mr);
1164 : jsr166 1.21 for (int i = 0; i < tasks.length; ++i)
1165 : dl 1.1 p.execute(tasks[i]);
1166 : jsr166 1.21 for (int i = 1; i < tasks.length; ++i)
1167 : dl 1.1 assertTrue(tasks[i].done);
1168 : jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1169 : dl 1.1 } finally {
1170 :     joinPool(p);
1171 :     }
1172 :     }
1173 :    
1174 :     /**
1175 : jsr166 1.19 * executor using DiscardPolicy drops task if saturated.
1176 : dl 1.1 */
1177 :     public void testSaturatedExecute3() {
1178 :     RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1179 : jsr166 1.21 ThreadPoolExecutor p =
1180 :     new CustomTPE(1, 1,
1181 :     LONG_DELAY_MS, MILLISECONDS,
1182 :     new ArrayBlockingQueue<Runnable>(1),
1183 :     h);
1184 : dl 1.1 try {
1185 :     TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1186 : jsr166 1.21 for (int i = 0; i < tasks.length; ++i)
1187 : dl 1.1 tasks[i] = new TrackedNoOpRunnable();
1188 :     p.execute(new TrackedLongRunnable());
1189 : jsr166 1.21 for (TrackedNoOpRunnable task : tasks)
1190 :     p.execute(task);
1191 :     for (TrackedNoOpRunnable task : tasks)
1192 :     assertFalse(task.done);
1193 : jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1194 : dl 1.1 } finally {
1195 :     joinPool(p);
1196 :     }
1197 :     }
1198 :    
1199 :     /**
1200 : jsr166 1.19 * executor using DiscardOldestPolicy drops oldest task if saturated.
1201 : dl 1.1 */
1202 :     public void testSaturatedExecute4() {
1203 :     RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1204 : jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1205 : dl 1.1 try {
1206 :     p.execute(new TrackedLongRunnable());
1207 :     TrackedLongRunnable r2 = new TrackedLongRunnable();
1208 :     p.execute(r2);
1209 :     assertTrue(p.getQueue().contains(r2));
1210 :     TrackedNoOpRunnable r3 = new TrackedNoOpRunnable();
1211 :     p.execute(r3);
1212 :     assertFalse(p.getQueue().contains(r2));
1213 :     assertTrue(p.getQueue().contains(r3));
1214 : jsr166 1.3 try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1215 : dl 1.1 } finally {
1216 :     joinPool(p);
1217 :     }
1218 :     }
1219 :    
1220 :     /**
1221 : jsr166 1.19 * execute throws RejectedExecutionException if shutdown
1222 : dl 1.1 */
1223 :     public void testRejectedExecutionExceptionOnShutdown() {
1224 : jsr166 1.21 ThreadPoolExecutor p =
1225 : jsr166 1.9 new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1226 : jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1227 : jsr166 1.8 try {
1228 : jsr166 1.21 p.execute(new NoOpRunnable());
1229 : jsr166 1.8 shouldThrow();
1230 :     } catch (RejectedExecutionException success) {}
1231 : jsr166 1.2
1232 : jsr166 1.21 joinPool(p);
1233 : dl 1.1 }
1234 :    
1235 :     /**
1236 : jsr166 1.19 * execute using CallerRunsPolicy drops task on shutdown
1237 : dl 1.1 */
1238 :     public void testCallerRunsOnShutdown() {
1239 :     RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy();
1240 : jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1241 : dl 1.1
1242 : jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
1243 : jsr166 1.8 try {
1244 : dl 1.1 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1245 : jsr166 1.8 p.execute(r);
1246 : dl 1.1 assertFalse(r.done);
1247 :     } finally {
1248 :     joinPool(p);
1249 :     }
1250 :     }
1251 :    
1252 :     /**
1253 : jsr166 1.19 * execute using DiscardPolicy drops task on shutdown
1254 : dl 1.1 */
1255 :     public void testDiscardOnShutdown() {
1256 :     RejectedExecutionHandler h = new CustomTPE.DiscardPolicy();
1257 : jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1258 : dl 1.1
1259 : jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
1260 : jsr166 1.8 try {
1261 : dl 1.1 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1262 : jsr166 1.8 p.execute(r);
1263 : dl 1.1 assertFalse(r.done);
1264 :     } finally {
1265 :     joinPool(p);
1266 :     }
1267 :     }
1268 :    
1269 :     /**
1270 : jsr166 1.19 * execute using DiscardOldestPolicy drops task on shutdown
1271 : dl 1.1 */
1272 :     public void testDiscardOldestOnShutdown() {
1273 :     RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy();
1274 : jsr166 1.9 ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1275 : dl 1.1
1276 : jsr166 1.3 try { p.shutdown(); } catch (SecurityException ok) { return; }
1277 : jsr166 1.8 try {
1278 : dl 1.1 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1279 : jsr166 1.8 p.execute(r);
1280 : dl 1.1 assertFalse(r.done);
1281 :     } finally {
1282 :     joinPool(p);
1283 :     }
1284 :     }
1285 :    
1286 :     /**
1287 : jsr166 1.18 * execute(null) throws NPE
1288 : dl 1.1 */
1289 :     public void testExecuteNull() {
1290 : jsr166 1.37 ThreadPoolExecutor p =
1291 :     new CustomTPE(1, 2, 1L, SECONDS,
1292 :     new ArrayBlockingQueue<Runnable>(10));
1293 : dl 1.1 try {
1294 : jsr166 1.21 p.execute(null);
1295 : dl 1.1 shouldThrow();
1296 : jsr166 1.8 } catch (NullPointerException success) {}
1297 : jsr166 1.2
1298 : jsr166 1.21 joinPool(p);
1299 : dl 1.1 }
1300 : jsr166 1.2
1301 : dl 1.1 /**
1302 : jsr166 1.19 * setCorePoolSize of negative value throws IllegalArgumentException
1303 : dl 1.1 */
1304 :     public void testCorePoolSizeIllegalArgumentException() {
1305 : jsr166 1.21 ThreadPoolExecutor p =
1306 : jsr166 1.9 new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1307 : jsr166 1.8 try {
1308 : jsr166 1.21 p.setCorePoolSize(-1);
1309 : jsr166 1.8 shouldThrow();
1310 :     } catch (IllegalArgumentException success) {
1311 : dl 1.1 } finally {
1312 : jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1313 : dl 1.1 }
1314 : jsr166 1.21 joinPool(p);
1315 : jsr166 1.2 }
1316 : dl 1.1
1317 :     /**
1318 : jsr166 1.19 * setMaximumPoolSize(int) throws IllegalArgumentException
1319 :     * if given a value less the core pool size
1320 : jsr166 1.2 */
1321 : dl 1.1 public void testMaximumPoolSizeIllegalArgumentException() {
1322 : jsr166 1.21 ThreadPoolExecutor p =
1323 : jsr166 1.10 new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1324 : dl 1.1 try {
1325 : jsr166 1.21 p.setMaximumPoolSize(1);
1326 : dl 1.1 shouldThrow();
1327 : jsr166 1.4 } catch (IllegalArgumentException success) {
1328 : dl 1.1 } finally {
1329 : jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1330 : dl 1.1 }
1331 : jsr166 1.21 joinPool(p);
1332 : dl 1.1 }
1333 : jsr166 1.2
1334 : dl 1.1 /**
1335 : jsr166 1.19 * setMaximumPoolSize throws IllegalArgumentException
1336 :     * if given a negative value
1337 : dl 1.1 */
1338 :     public void testMaximumPoolSizeIllegalArgumentException2() {
1339 : jsr166 1.21 ThreadPoolExecutor p =
1340 : jsr166 1.10 new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1341 : dl 1.1 try {
1342 : jsr166 1.21 p.setMaximumPoolSize(-1);
1343 : dl 1.1 shouldThrow();
1344 : jsr166 1.4 } catch (IllegalArgumentException success) {
1345 : dl 1.1 } finally {
1346 : jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1347 : dl 1.1 }
1348 : jsr166 1.21 joinPool(p);
1349 : dl 1.1 }
1350 : jsr166 1.2
1351 : dl 1.1 /**
1352 : jsr166 1.19 * setKeepAliveTime throws IllegalArgumentException
1353 :     * when given a negative value
1354 : dl 1.1 */
1355 :     public void testKeepAliveTimeIllegalArgumentException() {
1356 : jsr166 1.21 ThreadPoolExecutor p =
1357 : jsr166 1.10 new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1358 : jsr166 1.2
1359 : jsr166 1.8 try {
1360 : jsr166 1.21 p.setKeepAliveTime(-1,MILLISECONDS);
1361 : dl 1.1 shouldThrow();
1362 : jsr166 1.4 } catch (IllegalArgumentException success) {
1363 : dl 1.1 } finally {
1364 : jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1365 : dl 1.1 }
1366 : jsr166 1.21 joinPool(p);
1367 : dl 1.1 }
1368 :    
1369 :     /**
1370 :     * terminated() is called on termination
1371 :     */
1372 :     public void testTerminated() {
1373 : jsr166 1.21 CustomTPE p = new CustomTPE();
1374 :     try { p.shutdown(); } catch (SecurityException ok) { return; }
1375 : jsr166 1.30 assertTrue(p.terminatedCalled());
1376 : jsr166 1.21 joinPool(p);
1377 : dl 1.1 }
1378 :    
1379 :     /**
1380 :     * beforeExecute and afterExecute are called when executing task
1381 :     */
1382 : jsr166 1.5 public void testBeforeAfter() throws InterruptedException {
1383 : jsr166 1.21 CustomTPE p = new CustomTPE();
1384 : dl 1.1 try {
1385 : jsr166 1.30 final CountDownLatch done = new CountDownLatch(1);
1386 : jsr166 1.32 p.execute(new CheckedRunnable() {
1387 : jsr166 1.30 public void realRun() {
1388 :     done.countDown();
1389 : jsr166 1.32 }});
1390 : jsr166 1.30 await(p.afterCalled);
1391 :     assertEquals(0, done.getCount());
1392 :     assertTrue(p.afterCalled());
1393 :     assertTrue(p.beforeCalled());
1394 : jsr166 1.21 try { p.shutdown(); } catch (SecurityException ok) { return; }
1395 : dl 1.1 } finally {
1396 : jsr166 1.21 joinPool(p);
1397 : dl 1.1 }
1398 :     }
1399 :    
1400 :     /**
1401 :     * completed submit of callable returns result
1402 :     */
1403 : jsr166 1.5 public void testSubmitCallable() throws Exception {
1404 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1405 : dl 1.1 try {
1406 :     Future<String> future = e.submit(new StringTask());
1407 :     String result = future.get();
1408 :     assertSame(TEST_STRING, result);
1409 :     } finally {
1410 :     joinPool(e);
1411 :     }
1412 :     }
1413 :    
1414 :     /**
1415 :     * completed submit of runnable returns successfully
1416 :     */
1417 : jsr166 1.5 public void testSubmitRunnable() throws Exception {
1418 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1419 : dl 1.1 try {
1420 :     Future<?> future = e.submit(new NoOpRunnable());
1421 :     future.get();
1422 :     assertTrue(future.isDone());
1423 :     } finally {
1424 :     joinPool(e);
1425 :     }
1426 :     }
1427 :    
1428 :     /**
1429 :     * completed submit of (runnable, result) returns result
1430 :     */
1431 : jsr166 1.5 public void testSubmitRunnable2() 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 :     Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1435 :     String result = future.get();
1436 :     assertSame(TEST_STRING, result);
1437 :     } finally {
1438 :     joinPool(e);
1439 :     }
1440 :     }
1441 :    
1442 :     /**
1443 :     * invokeAny(null) throws NPE
1444 :     */
1445 : jsr166 1.5 public void testInvokeAny1() throws Exception {
1446 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1447 : dl 1.1 try {
1448 :     e.invokeAny(null);
1449 : jsr166 1.5 shouldThrow();
1450 : dl 1.1 } catch (NullPointerException success) {
1451 :     } finally {
1452 :     joinPool(e);
1453 :     }
1454 :     }
1455 :    
1456 :     /**
1457 :     * invokeAny(empty collection) throws IAE
1458 :     */
1459 : jsr166 1.5 public void testInvokeAny2() throws Exception {
1460 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1461 : dl 1.1 try {
1462 :     e.invokeAny(new ArrayList<Callable<String>>());
1463 : jsr166 1.5 shouldThrow();
1464 : dl 1.1 } catch (IllegalArgumentException success) {
1465 :     } finally {
1466 :     joinPool(e);
1467 :     }
1468 :     }
1469 :    
1470 :     /**
1471 :     * invokeAny(c) throws NPE if c has null elements
1472 :     */
1473 : jsr166 1.5 public void testInvokeAny3() throws Exception {
1474 : jsr166 1.17 CountDownLatch latch = new CountDownLatch(1);
1475 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1476 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1477 :     l.add(latchAwaitingStringTask(latch));
1478 :     l.add(null);
1479 : dl 1.1 try {
1480 :     e.invokeAny(l);
1481 : jsr166 1.5 shouldThrow();
1482 : dl 1.1 } catch (NullPointerException success) {
1483 :     } finally {
1484 : jsr166 1.5 latch.countDown();
1485 : dl 1.1 joinPool(e);
1486 :     }
1487 :     }
1488 :    
1489 :     /**
1490 :     * invokeAny(c) throws ExecutionException if no task completes
1491 :     */
1492 : jsr166 1.5 public void testInvokeAny4() throws Exception {
1493 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1494 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1495 :     l.add(new NPETask());
1496 : dl 1.1 try {
1497 :     e.invokeAny(l);
1498 : jsr166 1.5 shouldThrow();
1499 : dl 1.1 } catch (ExecutionException success) {
1500 : jsr166 1.13 assertTrue(success.getCause() instanceof NullPointerException);
1501 : dl 1.1 } finally {
1502 :     joinPool(e);
1503 :     }
1504 :     }
1505 :    
1506 :     /**
1507 :     * invokeAny(c) returns result of some task
1508 :     */
1509 : jsr166 1.5 public void testInvokeAny5() throws Exception {
1510 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1511 : dl 1.1 try {
1512 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1513 : dl 1.1 l.add(new StringTask());
1514 :     l.add(new StringTask());
1515 :     String result = e.invokeAny(l);
1516 :     assertSame(TEST_STRING, result);
1517 :     } finally {
1518 :     joinPool(e);
1519 :     }
1520 :     }
1521 :    
1522 :     /**
1523 :     * invokeAll(null) throws NPE
1524 :     */
1525 : jsr166 1.5 public void testInvokeAll1() throws Exception {
1526 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1527 : dl 1.1 try {
1528 :     e.invokeAll(null);
1529 : jsr166 1.5 shouldThrow();
1530 : dl 1.1 } catch (NullPointerException success) {
1531 :     } finally {
1532 :     joinPool(e);
1533 :     }
1534 :     }
1535 :    
1536 :     /**
1537 :     * invokeAll(empty collection) returns empty collection
1538 :     */
1539 : jsr166 1.5 public void testInvokeAll2() throws Exception {
1540 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1541 : dl 1.1 try {
1542 :     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1543 :     assertTrue(r.isEmpty());
1544 :     } finally {
1545 :     joinPool(e);
1546 :     }
1547 :     }
1548 :    
1549 :     /**
1550 :     * invokeAll(c) throws NPE if c has null elements
1551 :     */
1552 : jsr166 1.5 public void testInvokeAll3() throws Exception {
1553 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1554 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1555 :     l.add(new StringTask());
1556 :     l.add(null);
1557 : dl 1.1 try {
1558 :     e.invokeAll(l);
1559 : jsr166 1.5 shouldThrow();
1560 : dl 1.1 } catch (NullPointerException success) {
1561 :     } finally {
1562 :     joinPool(e);
1563 :     }
1564 :     }
1565 :    
1566 :     /**
1567 :     * get of element of invokeAll(c) throws exception on failed task
1568 :     */
1569 : jsr166 1.5 public void testInvokeAll4() throws Exception {
1570 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1571 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1572 :     l.add(new NPETask());
1573 :     List<Future<String>> futures = e.invokeAll(l);
1574 :     assertEquals(1, futures.size());
1575 : dl 1.1 try {
1576 : jsr166 1.17 futures.get(0).get();
1577 : jsr166 1.5 shouldThrow();
1578 : jsr166 1.3 } catch (ExecutionException success) {
1579 : jsr166 1.15 assertTrue(success.getCause() instanceof NullPointerException);
1580 : dl 1.1 } finally {
1581 :     joinPool(e);
1582 :     }
1583 :     }
1584 :    
1585 :     /**
1586 :     * invokeAll(c) returns results of all completed tasks
1587 :     */
1588 : jsr166 1.5 public void testInvokeAll5() throws Exception {
1589 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1590 : dl 1.1 try {
1591 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1592 : dl 1.1 l.add(new StringTask());
1593 :     l.add(new StringTask());
1594 : jsr166 1.17 List<Future<String>> futures = e.invokeAll(l);
1595 :     assertEquals(2, futures.size());
1596 :     for (Future<String> future : futures)
1597 : jsr166 1.6 assertSame(TEST_STRING, future.get());
1598 : dl 1.1 } finally {
1599 :     joinPool(e);
1600 :     }
1601 :     }
1602 :    
1603 :     /**
1604 :     * timed invokeAny(null) throws NPE
1605 :     */
1606 : jsr166 1.5 public void testTimedInvokeAny1() throws Exception {
1607 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1608 : dl 1.1 try {
1609 : jsr166 1.9 e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1610 : jsr166 1.5 shouldThrow();
1611 : dl 1.1 } catch (NullPointerException success) {
1612 :     } finally {
1613 :     joinPool(e);
1614 :     }
1615 :     }
1616 :    
1617 :     /**
1618 :     * timed invokeAny(,,null) throws NPE
1619 :     */
1620 : jsr166 1.5 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1621 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1622 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1623 :     l.add(new StringTask());
1624 : dl 1.1 try {
1625 :     e.invokeAny(l, MEDIUM_DELAY_MS, null);
1626 : jsr166 1.5 shouldThrow();
1627 : dl 1.1 } catch (NullPointerException success) {
1628 :     } finally {
1629 :     joinPool(e);
1630 :     }
1631 :     }
1632 :    
1633 :     /**
1634 :     * timed invokeAny(empty collection) throws IAE
1635 :     */
1636 : jsr166 1.5 public void testTimedInvokeAny2() throws Exception {
1637 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1638 : dl 1.1 try {
1639 : jsr166 1.9 e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1640 : jsr166 1.5 shouldThrow();
1641 : dl 1.1 } catch (IllegalArgumentException success) {
1642 :     } finally {
1643 :     joinPool(e);
1644 :     }
1645 :     }
1646 :    
1647 :     /**
1648 :     * timed invokeAny(c) throws NPE if c has null elements
1649 :     */
1650 : jsr166 1.5 public void testTimedInvokeAny3() throws Exception {
1651 : jsr166 1.17 CountDownLatch latch = new CountDownLatch(1);
1652 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1653 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1654 :     l.add(latchAwaitingStringTask(latch));
1655 :     l.add(null);
1656 : dl 1.1 try {
1657 : jsr166 1.9 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1658 : jsr166 1.5 shouldThrow();
1659 : dl 1.1 } catch (NullPointerException success) {
1660 :     } finally {
1661 : jsr166 1.12 latch.countDown();
1662 : dl 1.1 joinPool(e);
1663 :     }
1664 :     }
1665 :    
1666 :     /**
1667 :     * timed invokeAny(c) throws ExecutionException if no task completes
1668 :     */
1669 : jsr166 1.5 public void testTimedInvokeAny4() throws Exception {
1670 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1671 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1672 :     l.add(new NPETask());
1673 : dl 1.1 try {
1674 : jsr166 1.9 e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1675 : jsr166 1.5 shouldThrow();
1676 : jsr166 1.3 } catch (ExecutionException success) {
1677 : jsr166 1.13 assertTrue(success.getCause() instanceof NullPointerException);
1678 : dl 1.1 } finally {
1679 :     joinPool(e);
1680 :     }
1681 :     }
1682 :    
1683 :     /**
1684 :     * timed invokeAny(c) returns result of some task
1685 :     */
1686 : jsr166 1.5 public void testTimedInvokeAny5() throws Exception {
1687 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1688 : dl 1.1 try {
1689 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1690 : dl 1.1 l.add(new StringTask());
1691 :     l.add(new StringTask());
1692 : jsr166 1.9 String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1693 : dl 1.1 assertSame(TEST_STRING, result);
1694 :     } finally {
1695 :     joinPool(e);
1696 :     }
1697 :     }
1698 :    
1699 :     /**
1700 :     * timed invokeAll(null) throws NPE
1701 :     */
1702 : jsr166 1.5 public void testTimedInvokeAll1() throws Exception {
1703 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1704 : dl 1.1 try {
1705 : jsr166 1.9 e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1706 : jsr166 1.5 shouldThrow();
1707 : dl 1.1 } catch (NullPointerException success) {
1708 :     } finally {
1709 :     joinPool(e);
1710 :     }
1711 :     }
1712 :    
1713 :     /**
1714 :     * timed invokeAll(,,null) throws NPE
1715 :     */
1716 : jsr166 1.5 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1717 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1718 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1719 :     l.add(new StringTask());
1720 : dl 1.1 try {
1721 :     e.invokeAll(l, MEDIUM_DELAY_MS, null);
1722 : jsr166 1.5 shouldThrow();
1723 : dl 1.1 } catch (NullPointerException success) {
1724 :     } finally {
1725 :     joinPool(e);
1726 :     }
1727 :     }
1728 :    
1729 :     /**
1730 :     * timed invokeAll(empty collection) returns empty collection
1731 :     */
1732 : jsr166 1.5 public void testTimedInvokeAll2() throws Exception {
1733 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1734 : dl 1.1 try {
1735 : jsr166 1.9 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1736 : dl 1.1 assertTrue(r.isEmpty());
1737 :     } finally {
1738 :     joinPool(e);
1739 :     }
1740 :     }
1741 :    
1742 :     /**
1743 :     * timed invokeAll(c) throws NPE if c has null elements
1744 :     */
1745 : jsr166 1.5 public void testTimedInvokeAll3() throws Exception {
1746 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1747 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1748 :     l.add(new StringTask());
1749 :     l.add(null);
1750 : dl 1.1 try {
1751 : jsr166 1.9 e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1752 : jsr166 1.5 shouldThrow();
1753 : dl 1.1 } catch (NullPointerException success) {
1754 :     } finally {
1755 :     joinPool(e);
1756 :     }
1757 :     }
1758 :    
1759 :     /**
1760 :     * get of element of invokeAll(c) throws exception on failed task
1761 :     */
1762 : jsr166 1.5 public void testTimedInvokeAll4() throws Exception {
1763 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1764 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1765 :     l.add(new NPETask());
1766 :     List<Future<String>> futures =
1767 :     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1768 :     assertEquals(1, futures.size());
1769 : dl 1.1 try {
1770 : jsr166 1.17 futures.get(0).get();
1771 : jsr166 1.5 shouldThrow();
1772 : jsr166 1.3 } catch (ExecutionException success) {
1773 : jsr166 1.6 assertTrue(success.getCause() instanceof NullPointerException);
1774 : dl 1.1 } finally {
1775 :     joinPool(e);
1776 :     }
1777 :     }
1778 :    
1779 :     /**
1780 :     * timed invokeAll(c) returns results of all completed tasks
1781 :     */
1782 : jsr166 1.5 public void testTimedInvokeAll5() throws Exception {
1783 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1784 : dl 1.1 try {
1785 : jsr166 1.17 List<Callable<String>> l = new ArrayList<Callable<String>>();
1786 : dl 1.1 l.add(new StringTask());
1787 :     l.add(new StringTask());
1788 : jsr166 1.17 List<Future<String>> futures =
1789 : jsr166 1.45 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1790 : jsr166 1.17 assertEquals(2, futures.size());
1791 :     for (Future<String> future : futures)
1792 : jsr166 1.6 assertSame(TEST_STRING, future.get());
1793 : dl 1.1 } finally {
1794 :     joinPool(e);
1795 :     }
1796 :     }
1797 :    
1798 :     /**
1799 :     * timed invokeAll(c) cancels tasks not completed by timeout
1800 :     */
1801 : jsr166 1.5 public void testTimedInvokeAll6() throws Exception {
1802 : jsr166 1.9 ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1803 : dl 1.1 try {
1804 : jsr166 1.39 for (long timeout = timeoutMillis();;) {
1805 :     List<Callable<String>> tasks = new ArrayList<>();
1806 :     tasks.add(new StringTask("0"));
1807 :     tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1808 :     tasks.add(new StringTask("2"));
1809 :     long startTime = System.nanoTime();
1810 :     List<Future<String>> futures =
1811 :     e.invokeAll(tasks, timeout, MILLISECONDS);
1812 :     assertEquals(tasks.size(), futures.size());
1813 :     assertTrue(millisElapsedSince(startTime) >= timeout);
1814 :     for (Future future : futures)
1815 :     assertTrue(future.isDone());
1816 :     assertTrue(futures.get(1).isCancelled());
1817 :     try {
1818 :     assertEquals("0", futures.get(0).get());
1819 :     assertEquals("2", futures.get(2).get());
1820 :     break;
1821 :     } catch (CancellationException retryWithLongerTimeout) {
1822 :     timeout *= 2;
1823 :     if (timeout >= LONG_DELAY_MS / 2)
1824 :     fail("expected exactly one task to be cancelled");
1825 :     }
1826 :     }
1827 : dl 1.1 } finally {
1828 :     joinPool(e);
1829 :     }
1830 :     }
1831 :    
1832 :     /**
1833 :     * Execution continues if there is at least one thread even if
1834 :     * thread factory fails to create more
1835 :     */
1836 : jsr166 1.5 public void testFailingThreadFactory() throws InterruptedException {
1837 : jsr166 1.21 final ExecutorService e =
1838 :     new CustomTPE(100, 100,
1839 :     LONG_DELAY_MS, MILLISECONDS,
1840 :     new LinkedBlockingQueue<Runnable>(),
1841 :     new FailingThreadFactory());
1842 :     try {
1843 :     final int TASKS = 100;
1844 :     final CountDownLatch done = new CountDownLatch(TASKS);
1845 :     for (int k = 0; k < TASKS; ++k)
1846 :     e.execute(new CheckedRunnable() {
1847 :     public void realRun() {
1848 :     done.countDown();
1849 :     }});
1850 :     assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1851 : dl 1.1 } finally {
1852 :     joinPool(e);
1853 :     }
1854 :     }
1855 :    
1856 :     /**
1857 :     * allowsCoreThreadTimeOut is by default false.
1858 :     */
1859 :     public void testAllowsCoreThreadTimeOut() {
1860 : jsr166 1.21 ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1861 :     assertFalse(p.allowsCoreThreadTimeOut());
1862 :     joinPool(p);
1863 : dl 1.1 }
1864 :    
1865 :     /**
1866 :     * allowCoreThreadTimeOut(true) causes idle threads to time out
1867 :     */
1868 : jsr166 1.21 public void testAllowCoreThreadTimeOut_true() throws Exception {
1869 : jsr166 1.39 long keepAliveTime = timeoutMillis();
1870 : jsr166 1.21 final ThreadPoolExecutor p =
1871 :     new CustomTPE(2, 10,
1872 : jsr166 1.39 keepAliveTime, MILLISECONDS,
1873 : jsr166 1.21 new ArrayBlockingQueue<Runnable>(10));
1874 :     final CountDownLatch threadStarted = new CountDownLatch(1);
1875 :     try {
1876 :     p.allowCoreThreadTimeOut(true);
1877 :     p.execute(new CheckedRunnable() {
1878 : jsr166 1.39 public void realRun() {
1879 : jsr166 1.21 threadStarted.countDown();
1880 :     assertEquals(1, p.getPoolSize());
1881 :     }});
1882 : jsr166 1.29 await(threadStarted);
1883 : jsr166 1.39 delay(keepAliveTime);
1884 : jsr166 1.29 long startTime = System.nanoTime();
1885 :     while (p.getPoolSize() > 0
1886 :     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1887 :     Thread.yield();
1888 :     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1889 : jsr166 1.21 assertEquals(0, p.getPoolSize());
1890 : dl 1.1 } finally {
1891 : jsr166 1.21 joinPool(p);
1892 : dl 1.1 }
1893 :     }
1894 :    
1895 :     /**
1896 :     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1897 :     */
1898 : jsr166 1.21 public void testAllowCoreThreadTimeOut_false() throws Exception {
1899 : jsr166 1.39 long keepAliveTime = timeoutMillis();
1900 : jsr166 1.21 final ThreadPoolExecutor p =
1901 :     new CustomTPE(2, 10,
1902 : jsr166 1.39 keepAliveTime, MILLISECONDS,
1903 : jsr166 1.21 new ArrayBlockingQueue<Runnable>(10));
1904 :     final CountDownLatch threadStarted = new CountDownLatch(1);
1905 :     try {
1906 :     p.allowCoreThreadTimeOut(false);
1907 :     p.execute(new CheckedRunnable() {
1908 :     public void realRun() throws InterruptedException {
1909 :     threadStarted.countDown();
1910 :     assertTrue(p.getPoolSize() >= 1);
1911 :     }});
1912 : jsr166 1.39 delay(2 * keepAliveTime);
1913 : jsr166 1.21 assertTrue(p.getPoolSize() >= 1);
1914 : dl 1.1 } finally {
1915 : jsr166 1.21 joinPool(p);
1916 : dl 1.1 }
1917 :     }
1918 :    
1919 : jsr166 1.44 /**
1920 :     * get(cancelled task) throws CancellationException
1921 :     * (in part, a test of CustomTPE itself)
1922 :     */
1923 :     public void testGet_cancelled() throws Exception {
1924 :     final ExecutorService e =
1925 :     new CustomTPE(1, 1,
1926 :     LONG_DELAY_MS, MILLISECONDS,
1927 :     new LinkedBlockingQueue<Runnable>());
1928 :     try {
1929 :     final CountDownLatch blockerStarted = new CountDownLatch(1);
1930 :     final CountDownLatch done = new CountDownLatch(1);
1931 :     final List<Future<?>> futures = new ArrayList<>();
1932 :     for (int i = 0; i < 2; i++) {
1933 :     Runnable r = new CheckedRunnable() { public void realRun()
1934 :     throws Throwable {
1935 :     blockerStarted.countDown();
1936 :     assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
1937 :     }};
1938 :     futures.add(e.submit(r));
1939 :     }
1940 :     assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
1941 :     for (Future<?> future : futures) future.cancel(false);
1942 :     for (Future<?> future : futures) {
1943 :     try {
1944 :     future.get();
1945 :     shouldThrow();
1946 :     } catch (CancellationException success) {}
1947 :     try {
1948 :     future.get(LONG_DELAY_MS, MILLISECONDS);
1949 :     shouldThrow();
1950 :     } catch (CancellationException success) {}
1951 :     assertTrue(future.isCancelled());
1952 :     assertTrue(future.isDone());
1953 :     }
1954 :     done.countDown();
1955 :     } finally {
1956 :     joinPool(e);
1957 :     }
1958 :     }
1959 :    
1960 : dl 1.1 }

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8