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

Doug Lea
ViewVC Help
Powered by ViewVC 1.0.8