ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.18
Committed: Tue Jan 20 20:30:08 2004 UTC (20 years, 4 months ago) by dl
Branch: MAIN
CVS Tags: JSR166_PFD
Changes since 1.17: +3 -0 lines
Log Message:
Catch security exception

File Contents

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