1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import java.util.concurrent.*; |
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.List; |
11 |
|
|
|
12 |
dl |
1.3 |
public class ThreadPoolExecutorTest extends JSR166TestCase { |
13 |
dl |
1.1 |
public static void main(String[] args) { |
14 |
|
|
junit.textui.TestRunner.run (suite()); |
15 |
|
|
} |
16 |
|
|
public static Test suite() { |
17 |
|
|
return new TestSuite(ThreadPoolExecutorTest.class); |
18 |
|
|
} |
19 |
|
|
|
20 |
dl |
1.3 |
/** |
21 |
|
|
* For use as ThreadFactory in constructors |
22 |
|
|
*/ |
23 |
|
|
static class MyThreadFactory implements ThreadFactory{ |
24 |
dl |
1.1 |
public Thread newThread(Runnable r){ |
25 |
|
|
return new Thread(r); |
26 |
|
|
} |
27 |
|
|
} |
28 |
|
|
|
29 |
dl |
1.3 |
/** |
30 |
|
|
* For use as RejectedExecutionHandler in constructors |
31 |
|
|
*/ |
32 |
|
|
static class MyREHandler implements RejectedExecutionHandler{ |
33 |
dl |
1.1 |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
34 |
|
|
} |
35 |
|
|
|
36 |
|
|
/** |
37 |
dl |
1.3 |
* execute successfully executes a runnable |
38 |
dl |
1.1 |
*/ |
39 |
dl |
1.5 |
public void testExecute() { |
40 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
41 |
dl |
1.1 |
try { |
42 |
dl |
1.5 |
p1.execute(new Runnable() { |
43 |
|
|
public void run() { |
44 |
|
|
try { |
45 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
46 |
dl |
1.2 |
} catch(InterruptedException e){ |
47 |
dl |
1.5 |
threadUnexpectedException(); |
48 |
dl |
1.1 |
} |
49 |
|
|
} |
50 |
|
|
}); |
51 |
dl |
1.3 |
Thread.sleep(SMALL_DELAY_MS); |
52 |
dl |
1.2 |
} catch(InterruptedException e){ |
53 |
dl |
1.5 |
unexpectedException(); |
54 |
dl |
1.3 |
} |
55 |
dl |
1.5 |
joinPool(p1); |
56 |
dl |
1.1 |
} |
57 |
|
|
|
58 |
|
|
/** |
59 |
dl |
1.6 |
* getActiveCount increases but doesn't overestimate, when a |
60 |
|
|
* thread becomes active |
61 |
dl |
1.1 |
*/ |
62 |
dl |
1.5 |
public void testGetActiveCount() { |
63 |
|
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
64 |
|
|
assertEquals(0, p2.getActiveCount()); |
65 |
|
|
p2.execute(new MediumRunnable()); |
66 |
|
|
try { |
67 |
dl |
1.3 |
Thread.sleep(SHORT_DELAY_MS); |
68 |
|
|
} catch(Exception e){ |
69 |
dl |
1.5 |
unexpectedException(); |
70 |
dl |
1.1 |
} |
71 |
dl |
1.5 |
assertEquals(1, p2.getActiveCount()); |
72 |
|
|
joinPool(p2); |
73 |
dl |
1.1 |
} |
74 |
|
|
|
75 |
|
|
/** |
76 |
dl |
1.6 |
* getCompletedTaskCount increases, but doesn't overestimate, |
77 |
|
|
* when tasks complete |
78 |
dl |
1.1 |
*/ |
79 |
dl |
1.5 |
public void testGetCompletedTaskCount() { |
80 |
|
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
81 |
|
|
assertEquals(0, p2.getCompletedTaskCount()); |
82 |
|
|
p2.execute(new ShortRunnable()); |
83 |
|
|
try { |
84 |
dl |
1.3 |
Thread.sleep(MEDIUM_DELAY_MS); |
85 |
|
|
} catch(Exception e){ |
86 |
dl |
1.5 |
unexpectedException(); |
87 |
dl |
1.1 |
} |
88 |
dl |
1.5 |
assertEquals(1, p2.getCompletedTaskCount()); |
89 |
|
|
p2.shutdown(); |
90 |
|
|
joinPool(p2); |
91 |
dl |
1.1 |
} |
92 |
|
|
|
93 |
|
|
/** |
94 |
dl |
1.6 |
* getCorePoolSize returns size given in constructor if not otherwise set |
95 |
dl |
1.1 |
*/ |
96 |
dl |
1.5 |
public void testGetCorePoolSize() { |
97 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
98 |
|
|
assertEquals(1, p1.getCorePoolSize()); |
99 |
|
|
joinPool(p1); |
100 |
dl |
1.1 |
} |
101 |
|
|
|
102 |
|
|
/** |
103 |
dl |
1.6 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
104 |
dl |
1.1 |
*/ |
105 |
dl |
1.5 |
public void testGetKeepAliveTime() { |
106 |
|
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
107 |
|
|
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
108 |
|
|
joinPool(p2); |
109 |
dl |
1.1 |
} |
110 |
|
|
|
111 |
|
|
/** |
112 |
dl |
1.6 |
* getLargestPoolSize increases, but doesn't overestimate, when |
113 |
|
|
* multiple threads active |
114 |
dl |
1.1 |
*/ |
115 |
dl |
1.5 |
public void testGetLargestPoolSize() { |
116 |
|
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
117 |
dl |
1.1 |
try { |
118 |
dl |
1.5 |
assertEquals(0, p2.getLargestPoolSize()); |
119 |
|
|
p2.execute(new MediumRunnable()); |
120 |
|
|
p2.execute(new MediumRunnable()); |
121 |
dl |
1.3 |
Thread.sleep(SHORT_DELAY_MS); |
122 |
dl |
1.5 |
assertEquals(2, p2.getLargestPoolSize()); |
123 |
dl |
1.3 |
} catch(Exception e){ |
124 |
dl |
1.5 |
unexpectedException(); |
125 |
dl |
1.3 |
} |
126 |
dl |
1.5 |
joinPool(p2); |
127 |
dl |
1.1 |
} |
128 |
|
|
|
129 |
|
|
/** |
130 |
dl |
1.6 |
* getMaximumPoolSize returns value given in constructor if not |
131 |
|
|
* otherwise set |
132 |
dl |
1.1 |
*/ |
133 |
dl |
1.5 |
public void testGetMaximumPoolSize() { |
134 |
|
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
135 |
|
|
assertEquals(2, p2.getMaximumPoolSize()); |
136 |
|
|
joinPool(p2); |
137 |
dl |
1.1 |
} |
138 |
|
|
|
139 |
|
|
/** |
140 |
dl |
1.6 |
* getPoolSize increases, but doesn't overestimate, when threads |
141 |
|
|
* become active |
142 |
dl |
1.1 |
*/ |
143 |
dl |
1.5 |
public void testGetPoolSize() { |
144 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
145 |
|
|
assertEquals(0, p1.getPoolSize()); |
146 |
|
|
p1.execute(new MediumRunnable()); |
147 |
|
|
assertEquals(1, p1.getPoolSize()); |
148 |
|
|
joinPool(p1); |
149 |
dl |
1.1 |
} |
150 |
|
|
|
151 |
|
|
/** |
152 |
dl |
1.6 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
153 |
dl |
1.1 |
*/ |
154 |
dl |
1.5 |
public void testGetTaskCount() { |
155 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
156 |
dl |
1.1 |
try { |
157 |
dl |
1.5 |
assertEquals(0, p1.getTaskCount()); |
158 |
|
|
p1.execute(new MediumRunnable()); |
159 |
dl |
1.3 |
Thread.sleep(SHORT_DELAY_MS); |
160 |
dl |
1.5 |
assertEquals(1, p1.getTaskCount()); |
161 |
dl |
1.3 |
} catch(Exception e){ |
162 |
dl |
1.5 |
unexpectedException(); |
163 |
dl |
1.3 |
} |
164 |
dl |
1.5 |
joinPool(p1); |
165 |
dl |
1.1 |
} |
166 |
|
|
|
167 |
|
|
/** |
168 |
dl |
1.6 |
* isShutDown is false before shutdown, true after |
169 |
dl |
1.1 |
*/ |
170 |
dl |
1.5 |
public void testIsShutdown() { |
171 |
dl |
1.1 |
|
172 |
dl |
1.5 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
173 |
|
|
assertFalse(p1.isShutdown()); |
174 |
|
|
p1.shutdown(); |
175 |
|
|
assertTrue(p1.isShutdown()); |
176 |
|
|
joinPool(p1); |
177 |
dl |
1.1 |
} |
178 |
|
|
|
179 |
|
|
|
180 |
|
|
/** |
181 |
dl |
1.6 |
* isTerminated is false before termination, true after |
182 |
dl |
1.1 |
*/ |
183 |
dl |
1.5 |
public void testIsTerminated() { |
184 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
185 |
dl |
1.6 |
assertFalse(p1.isTerminated()); |
186 |
dl |
1.1 |
try { |
187 |
dl |
1.5 |
p1.execute(new MediumRunnable()); |
188 |
dl |
1.1 |
} finally { |
189 |
dl |
1.5 |
p1.shutdown(); |
190 |
dl |
1.1 |
} |
191 |
dl |
1.3 |
try { |
192 |
dl |
1.5 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
193 |
|
|
assertTrue(p1.isTerminated()); |
194 |
dl |
1.3 |
} catch(Exception e){ |
195 |
dl |
1.5 |
unexpectedException(); |
196 |
|
|
} |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
/** |
200 |
dl |
1.6 |
* isTerminating is not true when running or when terminated |
201 |
dl |
1.5 |
*/ |
202 |
|
|
public void testIsTerminating() { |
203 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
204 |
|
|
assertFalse(p1.isTerminating()); |
205 |
|
|
try { |
206 |
|
|
p1.execute(new SmallRunnable()); |
207 |
|
|
assertFalse(p1.isTerminating()); |
208 |
|
|
} finally { |
209 |
|
|
p1.shutdown(); |
210 |
|
|
} |
211 |
|
|
try { |
212 |
|
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
213 |
|
|
assertTrue(p1.isTerminated()); |
214 |
|
|
assertFalse(p1.isTerminating()); |
215 |
|
|
} catch(Exception e){ |
216 |
|
|
unexpectedException(); |
217 |
dl |
1.3 |
} |
218 |
dl |
1.1 |
} |
219 |
|
|
|
220 |
|
|
/** |
221 |
dl |
1.6 |
* purge removes cancelled tasks from the queue |
222 |
dl |
1.1 |
*/ |
223 |
dl |
1.5 |
public void testPurge() { |
224 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
225 |
dl |
1.3 |
CancellableTask[] tasks = new CancellableTask[5]; |
226 |
|
|
for(int i = 0; i < 5; i++){ |
227 |
|
|
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
228 |
dl |
1.5 |
p1.execute(tasks[i]); |
229 |
dl |
1.3 |
} |
230 |
|
|
tasks[4].cancel(true); |
231 |
|
|
tasks[3].cancel(true); |
232 |
dl |
1.5 |
p1.purge(); |
233 |
|
|
long count = p1.getTaskCount(); |
234 |
dl |
1.3 |
assertTrue(count >= 2 && count < 5); |
235 |
dl |
1.5 |
joinPool(p1); |
236 |
dl |
1.1 |
} |
237 |
|
|
|
238 |
|
|
/** |
239 |
dl |
1.6 |
* shutDownNow returns a list containing tasks that were not run |
240 |
dl |
1.1 |
*/ |
241 |
dl |
1.5 |
public void testShutDownNow() { |
242 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
243 |
dl |
1.1 |
List l; |
244 |
|
|
try { |
245 |
|
|
for(int i = 0; i < 5; i++) |
246 |
dl |
1.5 |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
247 |
dl |
1.1 |
} |
248 |
|
|
finally { |
249 |
dl |
1.5 |
l = p1.shutdownNow(); |
250 |
dl |
1.1 |
} |
251 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
252 |
dl |
1.1 |
assertTrue(l.size() <= 4); |
253 |
|
|
} |
254 |
|
|
|
255 |
|
|
// Exception Tests |
256 |
|
|
|
257 |
|
|
|
258 |
dl |
1.6 |
/** |
259 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
260 |
|
|
*/ |
261 |
dl |
1.1 |
public void testConstructor1() { |
262 |
dl |
1.5 |
try { |
263 |
dl |
1.3 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
264 |
dl |
1.5 |
shouldThrow(); |
265 |
dl |
1.1 |
} |
266 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
267 |
dl |
1.1 |
} |
268 |
|
|
|
269 |
dl |
1.6 |
/** |
270 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
271 |
|
|
*/ |
272 |
dl |
1.1 |
public void testConstructor2() { |
273 |
dl |
1.5 |
try { |
274 |
dl |
1.3 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
275 |
dl |
1.5 |
shouldThrow(); |
276 |
dl |
1.1 |
} |
277 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
278 |
dl |
1.1 |
} |
279 |
|
|
|
280 |
dl |
1.6 |
/** |
281 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
282 |
|
|
*/ |
283 |
dl |
1.1 |
public void testConstructor3() { |
284 |
dl |
1.5 |
try { |
285 |
dl |
1.3 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
286 |
dl |
1.5 |
shouldThrow(); |
287 |
dl |
1.1 |
} |
288 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
289 |
dl |
1.1 |
} |
290 |
|
|
|
291 |
dl |
1.6 |
/** |
292 |
|
|
* Constructor throws if keepAliveTime is less than zero |
293 |
|
|
*/ |
294 |
dl |
1.1 |
public void testConstructor4() { |
295 |
dl |
1.5 |
try { |
296 |
dl |
1.1 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
297 |
dl |
1.5 |
shouldThrow(); |
298 |
dl |
1.1 |
} |
299 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
300 |
dl |
1.1 |
} |
301 |
|
|
|
302 |
dl |
1.6 |
/** |
303 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
304 |
|
|
*/ |
305 |
dl |
1.1 |
public void testConstructor5() { |
306 |
dl |
1.5 |
try { |
307 |
dl |
1.3 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
308 |
dl |
1.5 |
shouldThrow(); |
309 |
dl |
1.1 |
} |
310 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
311 |
dl |
1.1 |
} |
312 |
|
|
|
313 |
dl |
1.6 |
/** |
314 |
|
|
* Constructor throws if workQueue is set to null |
315 |
|
|
*/ |
316 |
dl |
1.1 |
public void testNullPointerException() { |
317 |
dl |
1.5 |
try { |
318 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null); |
319 |
dl |
1.5 |
shouldThrow(); |
320 |
dl |
1.1 |
} |
321 |
dl |
1.3 |
catch (NullPointerException success){} |
322 |
dl |
1.1 |
} |
323 |
|
|
|
324 |
|
|
|
325 |
|
|
|
326 |
dl |
1.6 |
/** |
327 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
328 |
|
|
*/ |
329 |
dl |
1.1 |
public void testConstructor6() { |
330 |
dl |
1.5 |
try { |
331 |
dl |
1.3 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
332 |
dl |
1.5 |
shouldThrow(); |
333 |
dl |
1.3 |
} catch (IllegalArgumentException success){} |
334 |
dl |
1.1 |
} |
335 |
|
|
|
336 |
dl |
1.6 |
/** |
337 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
338 |
|
|
*/ |
339 |
dl |
1.1 |
public void testConstructor7() { |
340 |
dl |
1.5 |
try { |
341 |
dl |
1.3 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
342 |
dl |
1.5 |
shouldThrow(); |
343 |
dl |
1.1 |
} |
344 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
345 |
dl |
1.1 |
} |
346 |
|
|
|
347 |
dl |
1.6 |
/** |
348 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
349 |
|
|
*/ |
350 |
dl |
1.1 |
public void testConstructor8() { |
351 |
dl |
1.5 |
try { |
352 |
dl |
1.3 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
353 |
dl |
1.5 |
shouldThrow(); |
354 |
dl |
1.1 |
} |
355 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
356 |
dl |
1.1 |
} |
357 |
|
|
|
358 |
dl |
1.6 |
/** |
359 |
|
|
* Constructor throws if keepAliveTime is less than zero |
360 |
|
|
*/ |
361 |
dl |
1.1 |
public void testConstructor9() { |
362 |
dl |
1.5 |
try { |
363 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
364 |
dl |
1.5 |
shouldThrow(); |
365 |
dl |
1.1 |
} |
366 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
367 |
dl |
1.1 |
} |
368 |
|
|
|
369 |
dl |
1.6 |
/** |
370 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
371 |
|
|
*/ |
372 |
dl |
1.1 |
public void testConstructor10() { |
373 |
dl |
1.5 |
try { |
374 |
dl |
1.3 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory()); |
375 |
dl |
1.5 |
shouldThrow(); |
376 |
dl |
1.1 |
} |
377 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
378 |
dl |
1.1 |
} |
379 |
|
|
|
380 |
dl |
1.6 |
/** |
381 |
|
|
* Constructor throws if workQueue is set to null |
382 |
|
|
*/ |
383 |
dl |
1.1 |
public void testNullPointerException2() { |
384 |
dl |
1.5 |
try { |
385 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory()); |
386 |
dl |
1.5 |
shouldThrow(); |
387 |
dl |
1.1 |
} |
388 |
dl |
1.3 |
catch (NullPointerException success){} |
389 |
dl |
1.1 |
} |
390 |
|
|
|
391 |
dl |
1.6 |
/** |
392 |
|
|
* Constructor throws if threadFactory is set to null |
393 |
|
|
*/ |
394 |
dl |
1.1 |
public void testNullPointerException3() { |
395 |
dl |
1.5 |
try { |
396 |
dl |
1.1 |
ThreadFactory f = null; |
397 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
398 |
dl |
1.5 |
shouldThrow(); |
399 |
dl |
1.1 |
} |
400 |
dl |
1.3 |
catch (NullPointerException success){} |
401 |
dl |
1.1 |
} |
402 |
|
|
|
403 |
|
|
|
404 |
dl |
1.6 |
/** |
405 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
406 |
|
|
*/ |
407 |
dl |
1.1 |
public void testConstructor11() { |
408 |
dl |
1.5 |
try { |
409 |
dl |
1.3 |
new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
410 |
dl |
1.5 |
shouldThrow(); |
411 |
dl |
1.1 |
} |
412 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
413 |
dl |
1.1 |
} |
414 |
|
|
|
415 |
dl |
1.6 |
/** |
416 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
417 |
|
|
*/ |
418 |
dl |
1.1 |
public void testConstructor12() { |
419 |
dl |
1.5 |
try { |
420 |
dl |
1.3 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
421 |
dl |
1.5 |
shouldThrow(); |
422 |
dl |
1.1 |
} |
423 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
424 |
dl |
1.1 |
} |
425 |
|
|
|
426 |
dl |
1.6 |
/** |
427 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
428 |
|
|
*/ |
429 |
dl |
1.1 |
public void testConstructor13() { |
430 |
dl |
1.5 |
try { |
431 |
dl |
1.3 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
432 |
dl |
1.5 |
shouldThrow(); |
433 |
dl |
1.1 |
} |
434 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
435 |
dl |
1.1 |
} |
436 |
|
|
|
437 |
dl |
1.6 |
/** |
438 |
|
|
* Constructor throws if keepAliveTime is less than zero |
439 |
|
|
*/ |
440 |
dl |
1.1 |
public void testConstructor14() { |
441 |
dl |
1.5 |
try { |
442 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
443 |
dl |
1.5 |
shouldThrow(); |
444 |
dl |
1.1 |
} |
445 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
446 |
dl |
1.1 |
} |
447 |
|
|
|
448 |
dl |
1.6 |
/** |
449 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
450 |
|
|
*/ |
451 |
dl |
1.1 |
public void testConstructor15() { |
452 |
dl |
1.5 |
try { |
453 |
dl |
1.3 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyREHandler()); |
454 |
dl |
1.5 |
shouldThrow(); |
455 |
dl |
1.1 |
} |
456 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
457 |
dl |
1.1 |
} |
458 |
|
|
|
459 |
dl |
1.6 |
/** |
460 |
|
|
* Constructor throws if workQueue is set to null |
461 |
|
|
*/ |
462 |
dl |
1.1 |
public void testNullPointerException4() { |
463 |
dl |
1.5 |
try { |
464 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyREHandler()); |
465 |
dl |
1.5 |
shouldThrow(); |
466 |
dl |
1.1 |
} |
467 |
dl |
1.3 |
catch (NullPointerException success){} |
468 |
dl |
1.1 |
} |
469 |
|
|
|
470 |
dl |
1.6 |
/** |
471 |
|
|
* Constructor throws if handler is set to null |
472 |
|
|
*/ |
473 |
dl |
1.1 |
public void testNullPointerException5() { |
474 |
dl |
1.5 |
try { |
475 |
dl |
1.1 |
RejectedExecutionHandler r = null; |
476 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
477 |
dl |
1.5 |
shouldThrow(); |
478 |
dl |
1.1 |
} |
479 |
dl |
1.3 |
catch (NullPointerException success){} |
480 |
dl |
1.1 |
} |
481 |
|
|
|
482 |
|
|
|
483 |
dl |
1.6 |
/** |
484 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
485 |
|
|
*/ |
486 |
dl |
1.1 |
public void testConstructor16() { |
487 |
dl |
1.5 |
try { |
488 |
dl |
1.3 |
new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
489 |
dl |
1.5 |
shouldThrow(); |
490 |
dl |
1.1 |
} |
491 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
492 |
dl |
1.1 |
} |
493 |
|
|
|
494 |
dl |
1.6 |
/** |
495 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
496 |
|
|
*/ |
497 |
dl |
1.1 |
public void testConstructor17() { |
498 |
dl |
1.5 |
try { |
499 |
dl |
1.3 |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
500 |
dl |
1.5 |
shouldThrow(); |
501 |
dl |
1.1 |
} |
502 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
503 |
dl |
1.1 |
} |
504 |
|
|
|
505 |
dl |
1.6 |
/** |
506 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
507 |
|
|
*/ |
508 |
dl |
1.1 |
public void testConstructor18() { |
509 |
dl |
1.5 |
try { |
510 |
dl |
1.3 |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
511 |
dl |
1.5 |
shouldThrow(); |
512 |
dl |
1.1 |
} |
513 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
514 |
dl |
1.1 |
} |
515 |
|
|
|
516 |
dl |
1.6 |
/** |
517 |
|
|
* Constructor throws if keepAliveTime is less than zero |
518 |
|
|
*/ |
519 |
dl |
1.1 |
public void testConstructor19() { |
520 |
dl |
1.5 |
try { |
521 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
522 |
dl |
1.5 |
shouldThrow(); |
523 |
dl |
1.1 |
} |
524 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
525 |
dl |
1.1 |
} |
526 |
|
|
|
527 |
dl |
1.6 |
/** |
528 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
529 |
|
|
*/ |
530 |
dl |
1.1 |
public void testConstructor20() { |
531 |
dl |
1.5 |
try { |
532 |
dl |
1.3 |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),new MyREHandler()); |
533 |
dl |
1.5 |
shouldThrow(); |
534 |
dl |
1.1 |
} |
535 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
536 |
dl |
1.1 |
} |
537 |
|
|
|
538 |
dl |
1.6 |
/** |
539 |
|
|
* Constructor throws if workQueue is set to null |
540 |
|
|
*/ |
541 |
dl |
1.1 |
public void testNullPointerException6() { |
542 |
dl |
1.5 |
try { |
543 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new MyThreadFactory(),new MyREHandler()); |
544 |
dl |
1.5 |
shouldThrow(); |
545 |
dl |
1.1 |
} |
546 |
dl |
1.3 |
catch (NullPointerException success){} |
547 |
dl |
1.1 |
} |
548 |
|
|
|
549 |
dl |
1.6 |
/** |
550 |
|
|
* Constructor throws if handler is set to null |
551 |
|
|
*/ |
552 |
dl |
1.1 |
public void testNullPointerException7() { |
553 |
dl |
1.5 |
try { |
554 |
dl |
1.1 |
RejectedExecutionHandler r = null; |
555 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new MyThreadFactory(),r); |
556 |
dl |
1.5 |
shouldThrow(); |
557 |
dl |
1.1 |
} |
558 |
dl |
1.3 |
catch (NullPointerException success){} |
559 |
dl |
1.1 |
} |
560 |
|
|
|
561 |
dl |
1.6 |
/** |
562 |
|
|
* Constructor throws if ThreadFactory is set top null |
563 |
|
|
*/ |
564 |
dl |
1.1 |
public void testNullPointerException8() { |
565 |
dl |
1.5 |
try { |
566 |
dl |
1.1 |
ThreadFactory f = null; |
567 |
dl |
1.3 |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new MyREHandler()); |
568 |
dl |
1.5 |
shouldThrow(); |
569 |
dl |
1.1 |
} |
570 |
dl |
1.3 |
catch (NullPointerException successdn8){} |
571 |
dl |
1.1 |
} |
572 |
|
|
|
573 |
|
|
|
574 |
|
|
/** |
575 |
dl |
1.6 |
* execute throws RejectedExecutionException if shutdown |
576 |
dl |
1.1 |
*/ |
577 |
dl |
1.5 |
public void testRejectedExecutionException() { |
578 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
579 |
dl |
1.5 |
try { |
580 |
dl |
1.3 |
tpe = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
581 |
dl |
1.2 |
} catch(Exception e){} |
582 |
dl |
1.1 |
tpe.shutdown(); |
583 |
dl |
1.5 |
try { |
584 |
dl |
1.3 |
tpe.execute(new NoOpRunnable()); |
585 |
dl |
1.5 |
shouldThrow(); |
586 |
dl |
1.2 |
} catch(RejectedExecutionException success){} |
587 |
dl |
1.1 |
|
588 |
dl |
1.3 |
joinPool(tpe); |
589 |
dl |
1.1 |
} |
590 |
dl |
1.6 |
|
591 |
|
|
/** |
592 |
|
|
* execute throws RejectedExecutionException |
593 |
|
|
* if saturated. |
594 |
|
|
*/ |
595 |
|
|
public void testSaturatedExecute() { |
596 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
597 |
|
|
try { |
598 |
|
|
|
599 |
|
|
for(int i = 0; i < 5; ++i){ |
600 |
|
|
p.execute(new MediumRunnable()); |
601 |
|
|
} |
602 |
|
|
shouldThrow(); |
603 |
|
|
} catch(RejectedExecutionException success){} |
604 |
|
|
joinPool(p); |
605 |
|
|
} |
606 |
|
|
|
607 |
|
|
/** |
608 |
|
|
* execute (null) throws NPE |
609 |
|
|
*/ |
610 |
|
|
public void testExecuteNull() { |
611 |
|
|
ThreadPoolExecutor tpe = null; |
612 |
|
|
try { |
613 |
|
|
tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
614 |
|
|
tpe.execute(null); |
615 |
|
|
shouldThrow(); |
616 |
|
|
} catch(NullPointerException success){} |
617 |
|
|
|
618 |
|
|
joinPool(tpe); |
619 |
|
|
} |
620 |
dl |
1.1 |
|
621 |
|
|
/** |
622 |
dl |
1.6 |
* setCorePoolSize of netaitev value throws IllegalArgumentException |
623 |
dl |
1.1 |
*/ |
624 |
dl |
1.5 |
public void testCorePoolSizeIllegalArgumentException() { |
625 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
626 |
dl |
1.5 |
try { |
627 |
dl |
1.3 |
tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
628 |
dl |
1.2 |
} catch(Exception e){} |
629 |
dl |
1.5 |
try { |
630 |
dl |
1.1 |
tpe.setCorePoolSize(-1); |
631 |
dl |
1.5 |
shouldThrow(); |
632 |
dl |
1.2 |
} catch(IllegalArgumentException success){ |
633 |
dl |
1.1 |
} finally { |
634 |
|
|
tpe.shutdown(); |
635 |
|
|
} |
636 |
dl |
1.3 |
joinPool(tpe); |
637 |
dl |
1.1 |
} |
638 |
|
|
|
639 |
|
|
/** |
640 |
dl |
1.6 |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
641 |
|
|
* given a value less the core pool size |
642 |
dl |
1.1 |
*/ |
643 |
dl |
1.5 |
public void testMaximumPoolSizeIllegalArgumentException() { |
644 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
645 |
dl |
1.5 |
try { |
646 |
dl |
1.3 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
647 |
dl |
1.2 |
} catch(Exception e){} |
648 |
dl |
1.5 |
try { |
649 |
dl |
1.1 |
tpe.setMaximumPoolSize(1); |
650 |
dl |
1.5 |
shouldThrow(); |
651 |
dl |
1.2 |
} catch(IllegalArgumentException success){ |
652 |
dl |
1.1 |
} finally { |
653 |
|
|
tpe.shutdown(); |
654 |
|
|
} |
655 |
dl |
1.3 |
joinPool(tpe); |
656 |
dl |
1.1 |
} |
657 |
|
|
|
658 |
|
|
/** |
659 |
dl |
1.6 |
* setMaximumPoolSize throws IllegalArgumentException |
660 |
|
|
* if given a negative value |
661 |
dl |
1.1 |
*/ |
662 |
dl |
1.5 |
public void testMaximumPoolSizeIllegalArgumentException2() { |
663 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
664 |
dl |
1.5 |
try { |
665 |
dl |
1.3 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
666 |
dl |
1.2 |
} catch(Exception e){} |
667 |
dl |
1.5 |
try { |
668 |
dl |
1.1 |
tpe.setMaximumPoolSize(-1); |
669 |
dl |
1.5 |
shouldThrow(); |
670 |
dl |
1.2 |
} catch(IllegalArgumentException success){ |
671 |
dl |
1.1 |
} finally { |
672 |
|
|
tpe.shutdown(); |
673 |
|
|
} |
674 |
dl |
1.3 |
joinPool(tpe); |
675 |
dl |
1.1 |
} |
676 |
|
|
|
677 |
|
|
|
678 |
|
|
/** |
679 |
dl |
1.6 |
* setKeepAliveTime throws IllegalArgumentException |
680 |
dl |
1.1 |
* when given a negative value |
681 |
|
|
*/ |
682 |
dl |
1.5 |
public void testKeepAliveTimeIllegalArgumentException() { |
683 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
684 |
dl |
1.5 |
try { |
685 |
dl |
1.3 |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
686 |
dl |
1.2 |
} catch(Exception e){} |
687 |
dl |
1.1 |
|
688 |
dl |
1.5 |
try { |
689 |
dl |
1.1 |
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
690 |
dl |
1.5 |
shouldThrow(); |
691 |
dl |
1.2 |
} catch(IllegalArgumentException success){ |
692 |
dl |
1.1 |
} finally { |
693 |
|
|
tpe.shutdown(); |
694 |
|
|
} |
695 |
dl |
1.3 |
joinPool(tpe); |
696 |
dl |
1.1 |
} |
697 |
|
|
|
698 |
|
|
} |