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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8