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