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