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. |
2 |
> |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
> |
* Expert Group and released to the public domain, as explained at |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
8 |
– |
import java.util.concurrent.*; |
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.List; |
10 |
> |
import java.util.concurrent.*; |
11 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
12 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
13 |
> |
import java.util.*; |
14 |
|
|
15 |
< |
public class ThreadPoolExecutorTest extends TestCase{ |
15 |
> |
public class ThreadPoolExecutorTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
16 |
– |
|
17 |
– |
|
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
21 |
|
} |
21 |
– |
|
22 |
– |
private static long SHORT_DELAY_MS = 100; |
23 |
– |
private static long MEDIUM_DELAY_MS = 1000; |
24 |
– |
private static long LONG_DELAY_MS = 10000; |
25 |
– |
|
26 |
– |
//---- testThread class to implement ThreadFactory for use in constructors |
27 |
– |
static class testThread implements ThreadFactory{ |
28 |
– |
public Thread newThread(Runnable r){ |
29 |
– |
return new Thread(r); |
30 |
– |
} |
31 |
– |
} |
22 |
|
|
23 |
< |
//---- testReject class to implement RejectedExecutionHandler for use in the constructors |
24 |
< |
static class testReject implements RejectedExecutionHandler{ |
25 |
< |
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){} |
23 |
> |
static class ExtendedTPE extends ThreadPoolExecutor { |
24 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
25 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
26 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
27 |
> |
|
28 |
> |
public ExtendedTPE() { |
29 |
> |
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
30 |
> |
} |
31 |
> |
protected void beforeExecute(Thread t, Runnable r) { |
32 |
> |
beforeCalled.countDown(); |
33 |
> |
} |
34 |
> |
protected void afterExecute(Runnable r, Throwable t) { |
35 |
> |
afterCalled.countDown(); |
36 |
> |
} |
37 |
> |
protected void terminated() { |
38 |
> |
terminatedCalled.countDown(); |
39 |
> |
} |
40 |
> |
|
41 |
> |
public boolean beforeCalled() { |
42 |
> |
return beforeCalled.getCount() == 0; |
43 |
> |
} |
44 |
> |
public boolean afterCalled() { |
45 |
> |
return afterCalled.getCount() == 0; |
46 |
> |
} |
47 |
> |
public boolean terminatedCalled() { |
48 |
> |
return terminatedCalled.getCount() == 0; |
49 |
> |
} |
50 |
|
} |
51 |
|
|
52 |
< |
public Runnable newRunnable(){ |
53 |
< |
return new Runnable(){ |
54 |
< |
public void run(){ |
55 |
< |
try{Thread.sleep(MEDIUM_DELAY_MS); |
56 |
< |
} catch(Exception e){ |
57 |
< |
} |
44 |
< |
} |
45 |
< |
}; |
52 |
> |
static class FailingThreadFactory implements ThreadFactory { |
53 |
> |
int calls = 0; |
54 |
> |
public Thread newThread(Runnable r) { |
55 |
> |
if (++calls > 1) return null; |
56 |
> |
return new Thread(r); |
57 |
> |
} |
58 |
|
} |
59 |
|
|
48 |
– |
|
60 |
|
/** |
61 |
< |
* Test to verify that execute successfully executes a runnable |
61 |
> |
* execute successfully executes a runnable |
62 |
|
*/ |
63 |
< |
public void testExecute(){ |
64 |
< |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
63 |
> |
public void testExecute() throws InterruptedException { |
64 |
> |
final ThreadPoolExecutor p = |
65 |
> |
new ThreadPoolExecutor(1, 1, |
66 |
> |
LONG_DELAY_MS, MILLISECONDS, |
67 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
68 |
> |
final CountDownLatch done = new CountDownLatch(1); |
69 |
> |
final Runnable task = new CheckedRunnable() { |
70 |
> |
public void realRun() { |
71 |
> |
done.countDown(); |
72 |
> |
}}; |
73 |
|
try { |
74 |
< |
one.execute(new Runnable(){ |
75 |
< |
public void run(){ |
57 |
< |
try{ |
58 |
< |
Thread.sleep(SHORT_DELAY_MS); |
59 |
< |
}catch(InterruptedException e){ |
60 |
< |
fail("unexpected exception"); |
61 |
< |
} |
62 |
< |
} |
63 |
< |
}); |
64 |
< |
Thread.sleep(SHORT_DELAY_MS * 2); |
65 |
< |
}catch(InterruptedException e){ |
66 |
< |
fail("unexpected exception"); |
74 |
> |
p.execute(task); |
75 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
76 |
|
} finally { |
77 |
< |
one.shutdown(); |
77 |
> |
joinPool(p); |
78 |
|
} |
79 |
|
} |
80 |
|
|
81 |
|
/** |
82 |
< |
* Test to verify getActiveCount gives correct values |
82 |
> |
* getActiveCount increases but doesn't overestimate, when a |
83 |
> |
* thread becomes active |
84 |
|
*/ |
85 |
< |
public void testGetActiveCount(){ |
86 |
< |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
85 |
> |
public void testGetActiveCount() throws InterruptedException { |
86 |
> |
final ThreadPoolExecutor p = |
87 |
> |
new ThreadPoolExecutor(2, 2, |
88 |
> |
LONG_DELAY_MS, MILLISECONDS, |
89 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
90 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
91 |
> |
final CountDownLatch done = new CountDownLatch(1); |
92 |
|
try { |
93 |
< |
assertEquals(0, two.getActiveCount()); |
94 |
< |
two.execute(newRunnable()); |
95 |
< |
try{Thread.sleep(10);}catch(Exception e){} |
96 |
< |
assertEquals(1, two.getActiveCount()); |
93 |
> |
assertEquals(0, p.getActiveCount()); |
94 |
> |
p.execute(new CheckedRunnable() { |
95 |
> |
public void realRun() throws InterruptedException { |
96 |
> |
threadStarted.countDown(); |
97 |
> |
assertEquals(1, p.getActiveCount()); |
98 |
> |
done.await(); |
99 |
> |
}}); |
100 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
101 |
> |
assertEquals(1, p.getActiveCount()); |
102 |
|
} finally { |
103 |
< |
two.shutdown(); |
103 |
> |
done.countDown(); |
104 |
> |
joinPool(p); |
105 |
|
} |
106 |
|
} |
107 |
< |
|
107 |
> |
|
108 |
> |
/** |
109 |
> |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
110 |
> |
*/ |
111 |
> |
public void testPrestartCoreThread() { |
112 |
> |
final ThreadPoolExecutor p = |
113 |
> |
new ThreadPoolExecutor(2, 2, |
114 |
> |
LONG_DELAY_MS, MILLISECONDS, |
115 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
116 |
> |
assertEquals(0, p.getPoolSize()); |
117 |
> |
assertTrue(p.prestartCoreThread()); |
118 |
> |
assertEquals(1, p.getPoolSize()); |
119 |
> |
assertTrue(p.prestartCoreThread()); |
120 |
> |
assertEquals(2, p.getPoolSize()); |
121 |
> |
assertFalse(p.prestartCoreThread()); |
122 |
> |
assertEquals(2, p.getPoolSize()); |
123 |
> |
joinPool(p); |
124 |
> |
} |
125 |
> |
|
126 |
> |
/** |
127 |
> |
* prestartAllCoreThreads starts all corePoolSize threads |
128 |
> |
*/ |
129 |
> |
public void testPrestartAllCoreThreads() { |
130 |
> |
final ThreadPoolExecutor p = |
131 |
> |
new ThreadPoolExecutor(2, 2, |
132 |
> |
LONG_DELAY_MS, MILLISECONDS, |
133 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
134 |
> |
assertEquals(0, p.getPoolSize()); |
135 |
> |
p.prestartAllCoreThreads(); |
136 |
> |
assertEquals(2, p.getPoolSize()); |
137 |
> |
p.prestartAllCoreThreads(); |
138 |
> |
assertEquals(2, p.getPoolSize()); |
139 |
> |
joinPool(p); |
140 |
> |
} |
141 |
> |
|
142 |
|
/** |
143 |
< |
* Test to verify getCompleteTaskCount gives correct values |
143 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
144 |
> |
* when tasks complete |
145 |
|
*/ |
146 |
< |
public void testGetCompletedTaskCount(){ |
147 |
< |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
146 |
> |
public void testGetCompletedTaskCount() throws InterruptedException { |
147 |
> |
final ThreadPoolExecutor p = |
148 |
> |
new ThreadPoolExecutor(2, 2, |
149 |
> |
LONG_DELAY_MS, MILLISECONDS, |
150 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
151 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
152 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
153 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
154 |
|
try { |
155 |
< |
assertEquals(0, two.getCompletedTaskCount()); |
156 |
< |
two.execute(newRunnable()); |
157 |
< |
try{Thread.sleep(2000);}catch(Exception e){} |
158 |
< |
assertEquals(1, two.getCompletedTaskCount()); |
155 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
156 |
> |
p.execute(new CheckedRunnable() { |
157 |
> |
public void realRun() throws InterruptedException { |
158 |
> |
threadStarted.countDown(); |
159 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
160 |
> |
threadProceed.await(); |
161 |
> |
threadDone.countDown(); |
162 |
> |
}}); |
163 |
> |
await(threadStarted); |
164 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
165 |
> |
threadProceed.countDown(); |
166 |
> |
threadDone.await(); |
167 |
> |
long startTime = System.nanoTime(); |
168 |
> |
while (p.getCompletedTaskCount() != 1) { |
169 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
170 |
> |
fail("timed out"); |
171 |
> |
Thread.yield(); |
172 |
> |
} |
173 |
|
} finally { |
174 |
< |
two.shutdown(); |
174 |
> |
joinPool(p); |
175 |
|
} |
176 |
|
} |
177 |
< |
|
177 |
> |
|
178 |
> |
/** |
179 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
180 |
> |
*/ |
181 |
> |
public void testGetCorePoolSize() { |
182 |
> |
final ThreadPoolExecutor p = |
183 |
> |
new ThreadPoolExecutor(1, 1, |
184 |
> |
LONG_DELAY_MS, MILLISECONDS, |
185 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
186 |
> |
assertEquals(1, p.getCorePoolSize()); |
187 |
> |
joinPool(p); |
188 |
> |
} |
189 |
> |
|
190 |
> |
/** |
191 |
> |
* getKeepAliveTime returns value given in constructor if not otherwise set |
192 |
> |
*/ |
193 |
> |
public void testGetKeepAliveTime() { |
194 |
> |
final ThreadPoolExecutor p = |
195 |
> |
new ThreadPoolExecutor(2, 2, |
196 |
> |
1000, MILLISECONDS, |
197 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
198 |
> |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
199 |
> |
joinPool(p); |
200 |
> |
} |
201 |
> |
|
202 |
> |
/** |
203 |
> |
* getThreadFactory returns factory in constructor if not set |
204 |
> |
*/ |
205 |
> |
public void testGetThreadFactory() { |
206 |
> |
ThreadFactory tf = new SimpleThreadFactory(); |
207 |
> |
final ThreadPoolExecutor p = |
208 |
> |
new ThreadPoolExecutor(1, 2, |
209 |
> |
LONG_DELAY_MS, MILLISECONDS, |
210 |
> |
new ArrayBlockingQueue<Runnable>(10), |
211 |
> |
tf, |
212 |
> |
new NoOpREHandler()); |
213 |
> |
assertSame(tf, p.getThreadFactory()); |
214 |
> |
joinPool(p); |
215 |
> |
} |
216 |
> |
|
217 |
> |
/** |
218 |
> |
* setThreadFactory sets the thread factory returned by getThreadFactory |
219 |
> |
*/ |
220 |
> |
public void testSetThreadFactory() { |
221 |
> |
final ThreadPoolExecutor p = |
222 |
> |
new ThreadPoolExecutor(1, 2, |
223 |
> |
LONG_DELAY_MS, MILLISECONDS, |
224 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
225 |
> |
ThreadFactory tf = new SimpleThreadFactory(); |
226 |
> |
p.setThreadFactory(tf); |
227 |
> |
assertSame(tf, p.getThreadFactory()); |
228 |
> |
joinPool(p); |
229 |
> |
} |
230 |
> |
|
231 |
|
/** |
232 |
< |
* Test to verify getCorePoolSize gives correct values |
232 |
> |
* setThreadFactory(null) throws NPE |
233 |
|
*/ |
234 |
< |
public void testGetCorePoolSize(){ |
235 |
< |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
234 |
> |
public void testSetThreadFactoryNull() { |
235 |
> |
final ThreadPoolExecutor p = |
236 |
> |
new ThreadPoolExecutor(1, 2, |
237 |
> |
LONG_DELAY_MS, MILLISECONDS, |
238 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
239 |
|
try { |
240 |
< |
assertEquals(1, one.getCorePoolSize()); |
240 |
> |
p.setThreadFactory(null); |
241 |
> |
shouldThrow(); |
242 |
> |
} catch (NullPointerException success) { |
243 |
|
} finally { |
244 |
< |
one.shutdown(); |
244 |
> |
joinPool(p); |
245 |
|
} |
246 |
|
} |
247 |
< |
|
247 |
> |
|
248 |
> |
/** |
249 |
> |
* getRejectedExecutionHandler returns handler in constructor if not set |
250 |
> |
*/ |
251 |
> |
public void testGetRejectedExecutionHandler() { |
252 |
> |
final RejectedExecutionHandler h = new NoOpREHandler(); |
253 |
> |
final ThreadPoolExecutor p = |
254 |
> |
new ThreadPoolExecutor(1, 2, |
255 |
> |
LONG_DELAY_MS, MILLISECONDS, |
256 |
> |
new ArrayBlockingQueue<Runnable>(10), |
257 |
> |
h); |
258 |
> |
assertSame(h, p.getRejectedExecutionHandler()); |
259 |
> |
joinPool(p); |
260 |
> |
} |
261 |
> |
|
262 |
> |
/** |
263 |
> |
* setRejectedExecutionHandler sets the handler returned by |
264 |
> |
* getRejectedExecutionHandler |
265 |
> |
*/ |
266 |
> |
public void testSetRejectedExecutionHandler() { |
267 |
> |
final ThreadPoolExecutor p = |
268 |
> |
new ThreadPoolExecutor(1, 2, |
269 |
> |
LONG_DELAY_MS, MILLISECONDS, |
270 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
271 |
> |
RejectedExecutionHandler h = new NoOpREHandler(); |
272 |
> |
p.setRejectedExecutionHandler(h); |
273 |
> |
assertSame(h, p.getRejectedExecutionHandler()); |
274 |
> |
joinPool(p); |
275 |
> |
} |
276 |
> |
|
277 |
|
/** |
278 |
< |
* Test to verify getKeepAliveTime gives correct values |
278 |
> |
* setRejectedExecutionHandler(null) throws NPE |
279 |
|
*/ |
280 |
< |
public void testGetKeepAliveTime(){ |
281 |
< |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
280 |
> |
public void testSetRejectedExecutionHandlerNull() { |
281 |
> |
final ThreadPoolExecutor p = |
282 |
> |
new ThreadPoolExecutor(1, 2, |
283 |
> |
LONG_DELAY_MS, MILLISECONDS, |
284 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
285 |
|
try { |
286 |
< |
assertEquals(1, two.getKeepAliveTime(TimeUnit.SECONDS)); |
286 |
> |
p.setRejectedExecutionHandler(null); |
287 |
> |
shouldThrow(); |
288 |
> |
} catch (NullPointerException success) { |
289 |
|
} finally { |
290 |
< |
two.shutdown(); |
290 |
> |
joinPool(p); |
291 |
|
} |
292 |
|
} |
293 |
< |
|
293 |
> |
|
294 |
|
/** |
295 |
< |
* Test to verify getLargestPoolSize gives correct values |
295 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
296 |
> |
* multiple threads active |
297 |
|
*/ |
298 |
< |
public void testGetLargestPoolSize(){ |
299 |
< |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
298 |
> |
public void testGetLargestPoolSize() throws InterruptedException { |
299 |
> |
final int THREADS = 3; |
300 |
> |
final ThreadPoolExecutor p = |
301 |
> |
new ThreadPoolExecutor(THREADS, THREADS, |
302 |
> |
LONG_DELAY_MS, MILLISECONDS, |
303 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
304 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
305 |
> |
final CountDownLatch done = new CountDownLatch(1); |
306 |
|
try { |
307 |
< |
assertEquals(0, two.getLargestPoolSize()); |
308 |
< |
two.execute(newRunnable()); |
309 |
< |
two.execute(newRunnable()); |
310 |
< |
try{Thread.sleep(SHORT_DELAY_MS);} catch(Exception e){} |
311 |
< |
assertEquals(2, two.getLargestPoolSize()); |
307 |
> |
assertEquals(0, p.getLargestPoolSize()); |
308 |
> |
for (int i = 0; i < THREADS; i++) |
309 |
> |
p.execute(new CheckedRunnable() { |
310 |
> |
public void realRun() throws InterruptedException { |
311 |
> |
threadsStarted.countDown(); |
312 |
> |
done.await(); |
313 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
314 |
> |
}}); |
315 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
316 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
317 |
|
} finally { |
318 |
< |
two.shutdown(); |
318 |
> |
done.countDown(); |
319 |
> |
joinPool(p); |
320 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
321 |
|
} |
322 |
|
} |
323 |
< |
|
323 |
> |
|
324 |
> |
/** |
325 |
> |
* getMaximumPoolSize returns value given in constructor if not |
326 |
> |
* otherwise set |
327 |
> |
*/ |
328 |
> |
public void testGetMaximumPoolSize() { |
329 |
> |
final ThreadPoolExecutor p = |
330 |
> |
new ThreadPoolExecutor(2, 3, |
331 |
> |
LONG_DELAY_MS, MILLISECONDS, |
332 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
333 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
334 |
> |
joinPool(p); |
335 |
> |
} |
336 |
> |
|
337 |
|
/** |
338 |
< |
* Test to verify getMaximumPoolSize gives correct values |
338 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
339 |
> |
* become active |
340 |
|
*/ |
341 |
< |
public void testGetMaximumPoolSize(){ |
342 |
< |
ThreadPoolExecutor two = new ThreadPoolExecutor(2, 2, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
341 |
> |
public void testGetPoolSize() throws InterruptedException { |
342 |
> |
final ThreadPoolExecutor p = |
343 |
> |
new ThreadPoolExecutor(1, 1, |
344 |
> |
LONG_DELAY_MS, MILLISECONDS, |
345 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
346 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
347 |
> |
final CountDownLatch done = new CountDownLatch(1); |
348 |
|
try { |
349 |
< |
assertEquals(2, two.getMaximumPoolSize()); |
349 |
> |
assertEquals(0, p.getPoolSize()); |
350 |
> |
p.execute(new CheckedRunnable() { |
351 |
> |
public void realRun() throws InterruptedException { |
352 |
> |
threadStarted.countDown(); |
353 |
> |
assertEquals(1, p.getPoolSize()); |
354 |
> |
done.await(); |
355 |
> |
}}); |
356 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
357 |
> |
assertEquals(1, p.getPoolSize()); |
358 |
|
} finally { |
359 |
< |
two.shutdown(); |
359 |
> |
done.countDown(); |
360 |
> |
joinPool(p); |
361 |
|
} |
362 |
|
} |
363 |
< |
|
363 |
> |
|
364 |
|
/** |
365 |
< |
* Test to verify getPoolSize gives correct values |
365 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
366 |
|
*/ |
367 |
< |
public void testGetPoolSize(){ |
368 |
< |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
369 |
< |
try { |
370 |
< |
assertEquals(0, one.getPoolSize()); |
371 |
< |
one.execute(newRunnable()); |
372 |
< |
assertEquals(1, one.getPoolSize()); |
367 |
> |
public void testGetTaskCount() throws InterruptedException { |
368 |
> |
final ThreadPoolExecutor p = |
369 |
> |
new ThreadPoolExecutor(1, 1, |
370 |
> |
LONG_DELAY_MS, MILLISECONDS, |
371 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
372 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
373 |
> |
final CountDownLatch done = new CountDownLatch(1); |
374 |
> |
try { |
375 |
> |
assertEquals(0, p.getTaskCount()); |
376 |
> |
p.execute(new CheckedRunnable() { |
377 |
> |
public void realRun() throws InterruptedException { |
378 |
> |
threadStarted.countDown(); |
379 |
> |
assertEquals(1, p.getTaskCount()); |
380 |
> |
done.await(); |
381 |
> |
}}); |
382 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
383 |
> |
assertEquals(1, p.getTaskCount()); |
384 |
|
} finally { |
385 |
< |
one.shutdown(); |
385 |
> |
done.countDown(); |
386 |
> |
joinPool(p); |
387 |
|
} |
388 |
|
} |
389 |
< |
|
389 |
> |
|
390 |
|
/** |
391 |
< |
* Test to verify getTaskCount gives correct values |
391 |
> |
* isShutdown is false before shutdown, true after |
392 |
|
*/ |
393 |
< |
public void testGetTaskCount(){ |
394 |
< |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
393 |
> |
public void testIsShutdown() { |
394 |
> |
final ThreadPoolExecutor p = |
395 |
> |
new ThreadPoolExecutor(1, 1, |
396 |
> |
LONG_DELAY_MS, MILLISECONDS, |
397 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
398 |
> |
assertFalse(p.isShutdown()); |
399 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
400 |
> |
assertTrue(p.isShutdown()); |
401 |
> |
joinPool(p); |
402 |
> |
} |
403 |
> |
|
404 |
> |
/** |
405 |
> |
* awaitTermination on a non-shutdown pool times out |
406 |
> |
*/ |
407 |
> |
public void testAwaitTermination_timesOut() throws InterruptedException { |
408 |
> |
final ThreadPoolExecutor p = |
409 |
> |
new ThreadPoolExecutor(1, 1, |
410 |
> |
LONG_DELAY_MS, MILLISECONDS, |
411 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
412 |
> |
assertFalse(p.isTerminated()); |
413 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
414 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
415 |
> |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
416 |
> |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
417 |
> |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
418 |
> |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
419 |
> |
long timeoutNanos = 999999L; |
420 |
> |
long startTime = System.nanoTime(); |
421 |
> |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
422 |
> |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
423 |
> |
assertFalse(p.isTerminated()); |
424 |
> |
startTime = System.nanoTime(); |
425 |
> |
long timeoutMillis = timeoutMillis(); |
426 |
> |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
427 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
428 |
> |
assertFalse(p.isTerminated()); |
429 |
> |
p.shutdown(); |
430 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
431 |
> |
assertTrue(p.isTerminated()); |
432 |
> |
} |
433 |
> |
|
434 |
> |
/** |
435 |
> |
* isTerminated is false before termination, true after |
436 |
> |
*/ |
437 |
> |
public void testIsTerminated() throws InterruptedException { |
438 |
> |
final ThreadPoolExecutor p = |
439 |
> |
new ThreadPoolExecutor(1, 1, |
440 |
> |
LONG_DELAY_MS, MILLISECONDS, |
441 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
442 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
443 |
> |
final CountDownLatch done = new CountDownLatch(1); |
444 |
> |
assertFalse(p.isTerminated()); |
445 |
|
try { |
446 |
< |
assertEquals(0, one.getTaskCount()); |
447 |
< |
for(int i = 0; i < 5; i++) |
448 |
< |
one.execute(newRunnable()); |
449 |
< |
try{Thread.sleep(SHORT_DELAY_MS);}catch(Exception e){} |
450 |
< |
assertEquals(5, one.getTaskCount()); |
446 |
> |
p.execute(new CheckedRunnable() { |
447 |
> |
public void realRun() throws InterruptedException { |
448 |
> |
assertFalse(p.isTerminated()); |
449 |
> |
threadStarted.countDown(); |
450 |
> |
done.await(); |
451 |
> |
}}); |
452 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
453 |
> |
assertFalse(p.isTerminating()); |
454 |
> |
done.countDown(); |
455 |
|
} finally { |
456 |
< |
one.shutdown(); |
456 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
457 |
|
} |
458 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
459 |
+ |
assertTrue(p.isTerminated()); |
460 |
|
} |
461 |
< |
|
461 |
> |
|
462 |
|
/** |
463 |
< |
* Test to verify isShutDown gives correct values |
463 |
> |
* isTerminating is not true when running or when terminated |
464 |
|
*/ |
465 |
< |
public void testIsShutdown(){ |
466 |
< |
|
467 |
< |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
465 |
> |
public void testIsTerminating() throws InterruptedException { |
466 |
> |
final ThreadPoolExecutor p = |
467 |
> |
new ThreadPoolExecutor(1, 1, |
468 |
> |
LONG_DELAY_MS, MILLISECONDS, |
469 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
470 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
471 |
> |
final CountDownLatch done = new CountDownLatch(1); |
472 |
|
try { |
473 |
< |
assertFalse(one.isShutdown()); |
473 |
> |
assertFalse(p.isTerminating()); |
474 |
> |
p.execute(new CheckedRunnable() { |
475 |
> |
public void realRun() throws InterruptedException { |
476 |
> |
assertFalse(p.isTerminating()); |
477 |
> |
threadStarted.countDown(); |
478 |
> |
done.await(); |
479 |
> |
}}); |
480 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
481 |
> |
assertFalse(p.isTerminating()); |
482 |
> |
done.countDown(); |
483 |
> |
} finally { |
484 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
485 |
|
} |
486 |
< |
finally { |
487 |
< |
one.shutdown(); |
486 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
487 |
> |
assertTrue(p.isTerminated()); |
488 |
> |
assertFalse(p.isTerminating()); |
489 |
> |
} |
490 |
> |
|
491 |
> |
/** |
492 |
> |
* getQueue returns the work queue, which contains queued tasks |
493 |
> |
*/ |
494 |
> |
public void testGetQueue() throws InterruptedException { |
495 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
496 |
> |
final ThreadPoolExecutor p = |
497 |
> |
new ThreadPoolExecutor(1, 1, |
498 |
> |
LONG_DELAY_MS, MILLISECONDS, |
499 |
> |
q); |
500 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
501 |
> |
final CountDownLatch done = new CountDownLatch(1); |
502 |
> |
try { |
503 |
> |
FutureTask[] tasks = new FutureTask[5]; |
504 |
> |
for (int i = 0; i < tasks.length; i++) { |
505 |
> |
Callable task = new CheckedCallable<Boolean>() { |
506 |
> |
public Boolean realCall() throws InterruptedException { |
507 |
> |
threadStarted.countDown(); |
508 |
> |
assertSame(q, p.getQueue()); |
509 |
> |
done.await(); |
510 |
> |
return Boolean.TRUE; |
511 |
> |
}}; |
512 |
> |
tasks[i] = new FutureTask(task); |
513 |
> |
p.execute(tasks[i]); |
514 |
> |
} |
515 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
516 |
> |
assertSame(q, p.getQueue()); |
517 |
> |
assertFalse(q.contains(tasks[0])); |
518 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
519 |
> |
assertEquals(tasks.length - 1, q.size()); |
520 |
> |
} finally { |
521 |
> |
done.countDown(); |
522 |
> |
joinPool(p); |
523 |
|
} |
196 |
– |
assertTrue(one.isShutdown()); |
524 |
|
} |
525 |
|
|
199 |
– |
|
526 |
|
/** |
527 |
< |
* Test to verify isTerminated gives correct values |
202 |
< |
* Makes sure termination does not take an innapropriate |
203 |
< |
* amount of time |
527 |
> |
* remove(task) removes queued task, and fails to remove active task |
528 |
|
*/ |
529 |
< |
public void testIsTerminated(){ |
530 |
< |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
529 |
> |
public void testRemove() throws InterruptedException { |
530 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
531 |
> |
final ThreadPoolExecutor p = |
532 |
> |
new ThreadPoolExecutor(1, 1, |
533 |
> |
LONG_DELAY_MS, MILLISECONDS, |
534 |
> |
q); |
535 |
> |
Runnable[] tasks = new Runnable[5]; |
536 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
537 |
> |
final CountDownLatch done = new CountDownLatch(1); |
538 |
|
try { |
539 |
< |
one.execute(newRunnable()); |
539 |
> |
for (int i = 0; i < tasks.length; i++) { |
540 |
> |
tasks[i] = new CheckedRunnable() { |
541 |
> |
public void realRun() throws InterruptedException { |
542 |
> |
threadStarted.countDown(); |
543 |
> |
done.await(); |
544 |
> |
}}; |
545 |
> |
p.execute(tasks[i]); |
546 |
> |
} |
547 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
548 |
> |
assertFalse(p.remove(tasks[0])); |
549 |
> |
assertTrue(q.contains(tasks[4])); |
550 |
> |
assertTrue(q.contains(tasks[3])); |
551 |
> |
assertTrue(p.remove(tasks[4])); |
552 |
> |
assertFalse(p.remove(tasks[4])); |
553 |
> |
assertFalse(q.contains(tasks[4])); |
554 |
> |
assertTrue(q.contains(tasks[3])); |
555 |
> |
assertTrue(p.remove(tasks[3])); |
556 |
> |
assertFalse(q.contains(tasks[3])); |
557 |
|
} finally { |
558 |
< |
one.shutdown(); |
558 |
> |
done.countDown(); |
559 |
> |
joinPool(p); |
560 |
|
} |
212 |
– |
boolean flag = false; |
213 |
– |
try{ |
214 |
– |
flag = one.awaitTermination(10, TimeUnit.SECONDS); |
215 |
– |
}catch(Exception e){} |
216 |
– |
assertTrue(one.isTerminated()); |
217 |
– |
if(!flag) |
218 |
– |
fail("ThreadPoolExecutor - thread pool did not terminate within suitable timeframe"); |
561 |
|
} |
562 |
|
|
563 |
|
/** |
564 |
< |
* Test to verify that purge correctly removes cancelled tasks |
223 |
< |
* from the queue |
564 |
> |
* purge removes cancelled tasks from the queue |
565 |
|
*/ |
566 |
< |
public void testPurge(){ |
567 |
< |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
566 |
> |
public void testPurge() throws InterruptedException { |
567 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
568 |
> |
final CountDownLatch done = new CountDownLatch(1); |
569 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
570 |
> |
final ThreadPoolExecutor p = |
571 |
> |
new ThreadPoolExecutor(1, 1, |
572 |
> |
LONG_DELAY_MS, MILLISECONDS, |
573 |
> |
q); |
574 |
> |
FutureTask[] tasks = new FutureTask[5]; |
575 |
|
try { |
576 |
< |
CancellableTask[] tasks = new CancellableTask[5]; |
577 |
< |
for(int i = 0; i < 5; i++){ |
578 |
< |
tasks[i] = new CancellableTask(newRunnable()); |
579 |
< |
one.execute(tasks[i]); |
576 |
> |
for (int i = 0; i < tasks.length; i++) { |
577 |
> |
Callable task = new CheckedCallable<Boolean>() { |
578 |
> |
public Boolean realCall() throws InterruptedException { |
579 |
> |
threadStarted.countDown(); |
580 |
> |
done.await(); |
581 |
> |
return Boolean.TRUE; |
582 |
> |
}}; |
583 |
> |
tasks[i] = new FutureTask(task); |
584 |
> |
p.execute(tasks[i]); |
585 |
|
} |
586 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
587 |
+ |
assertEquals(tasks.length, p.getTaskCount()); |
588 |
+ |
assertEquals(tasks.length - 1, q.size()); |
589 |
+ |
assertEquals(1L, p.getActiveCount()); |
590 |
+ |
assertEquals(0L, p.getCompletedTaskCount()); |
591 |
|
tasks[4].cancel(true); |
592 |
< |
tasks[3].cancel(true); |
593 |
< |
one.purge(); |
594 |
< |
long count = one.getTaskCount(); |
595 |
< |
assertTrue(count >= 2 && count < 5); |
592 |
> |
tasks[3].cancel(false); |
593 |
> |
p.purge(); |
594 |
> |
assertEquals(tasks.length - 3, q.size()); |
595 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
596 |
> |
p.purge(); // Nothing to do |
597 |
> |
assertEquals(tasks.length - 3, q.size()); |
598 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
599 |
|
} finally { |
600 |
< |
one.shutdown(); |
600 |
> |
done.countDown(); |
601 |
> |
joinPool(p); |
602 |
|
} |
603 |
|
} |
604 |
|
|
605 |
|
/** |
606 |
< |
* Test to verify shutDownNow returns a list |
245 |
< |
* containing the correct number of elements |
606 |
> |
* shutdownNow returns a list containing tasks that were not run |
607 |
|
*/ |
608 |
< |
public void testShutDownNow(){ |
609 |
< |
ThreadPoolExecutor one = new ThreadPoolExecutor(1, 1, 1, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
608 |
> |
public void testShutdownNow() { |
609 |
> |
final ThreadPoolExecutor p = |
610 |
> |
new ThreadPoolExecutor(1, 1, |
611 |
> |
LONG_DELAY_MS, MILLISECONDS, |
612 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
613 |
|
List l; |
614 |
|
try { |
615 |
< |
for(int i = 0; i < 5; i++) |
616 |
< |
one.execute(newRunnable()); |
615 |
> |
for (int i = 0; i < 5; i++) |
616 |
> |
p.execute(new MediumPossiblyInterruptedRunnable()); |
617 |
|
} |
618 |
|
finally { |
619 |
< |
l = one.shutdownNow(); |
619 |
> |
try { |
620 |
> |
l = p.shutdownNow(); |
621 |
> |
} catch (SecurityException ok) { return; } |
622 |
|
} |
623 |
< |
assertTrue(one.isShutdown()); |
624 |
< |
assertTrue(l.size() <= 4); |
623 |
> |
assertTrue(p.isShutdown()); |
624 |
> |
assertTrue(l.size() <= 4); |
625 |
|
} |
626 |
|
|
261 |
– |
|
262 |
– |
|
263 |
– |
|
264 |
– |
|
265 |
– |
|
627 |
|
// Exception Tests |
267 |
– |
|
628 |
|
|
629 |
< |
//---- Tests if corePoolSize argument is less than zero |
629 |
> |
/** |
630 |
> |
* Constructor throws if corePoolSize argument is less than zero |
631 |
> |
*/ |
632 |
|
public void testConstructor1() { |
633 |
< |
try{ |
634 |
< |
new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
635 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
636 |
< |
} |
637 |
< |
catch (IllegalArgumentException i){} |
633 |
> |
try { |
634 |
> |
new ThreadPoolExecutor(-1, 1, |
635 |
> |
LONG_DELAY_MS, MILLISECONDS, |
636 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
637 |
> |
shouldThrow(); |
638 |
> |
} catch (IllegalArgumentException success) {} |
639 |
|
} |
640 |
< |
|
641 |
< |
//---- Tests if maximumPoolSize is less than zero |
640 |
> |
|
641 |
> |
/** |
642 |
> |
* Constructor throws if maximumPoolSize is less than zero |
643 |
> |
*/ |
644 |
|
public void testConstructor2() { |
645 |
< |
try{ |
646 |
< |
new ThreadPoolExecutor(1,-1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
647 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
648 |
< |
} |
649 |
< |
catch (IllegalArgumentException i2){} |
645 |
> |
try { |
646 |
> |
new ThreadPoolExecutor(1, -1, |
647 |
> |
LONG_DELAY_MS, MILLISECONDS, |
648 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
649 |
> |
shouldThrow(); |
650 |
> |
} catch (IllegalArgumentException success) {} |
651 |
|
} |
652 |
< |
|
653 |
< |
//---- Tests if maximumPoolSize is equal to zero |
652 |
> |
|
653 |
> |
/** |
654 |
> |
* Constructor throws if maximumPoolSize is equal to zero |
655 |
> |
*/ |
656 |
|
public void testConstructor3() { |
657 |
< |
try{ |
658 |
< |
new ThreadPoolExecutor(1,0,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
659 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
660 |
< |
} |
661 |
< |
catch (IllegalArgumentException i3){} |
657 |
> |
try { |
658 |
> |
new ThreadPoolExecutor(1, 0, |
659 |
> |
LONG_DELAY_MS, MILLISECONDS, |
660 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
661 |
> |
shouldThrow(); |
662 |
> |
} catch (IllegalArgumentException success) {} |
663 |
|
} |
664 |
|
|
665 |
< |
//---- Tests if keepAliveTime is less than zero |
665 |
> |
/** |
666 |
> |
* Constructor throws if keepAliveTime is less than zero |
667 |
> |
*/ |
668 |
|
public void testConstructor4() { |
669 |
< |
try{ |
670 |
< |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
671 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
672 |
< |
} |
673 |
< |
catch (IllegalArgumentException i4){} |
669 |
> |
try { |
670 |
> |
new ThreadPoolExecutor(1, 2, |
671 |
> |
-1L, MILLISECONDS, |
672 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
673 |
> |
shouldThrow(); |
674 |
> |
} catch (IllegalArgumentException success) {} |
675 |
|
} |
676 |
|
|
677 |
< |
//---- Tests if corePoolSize is greater than the maximumPoolSize |
677 |
> |
/** |
678 |
> |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
679 |
> |
*/ |
680 |
|
public void testConstructor5() { |
681 |
< |
try{ |
682 |
< |
new ThreadPoolExecutor(2,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
683 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
684 |
< |
} |
685 |
< |
catch (IllegalArgumentException i5){} |
681 |
> |
try { |
682 |
> |
new ThreadPoolExecutor(2, 1, |
683 |
> |
LONG_DELAY_MS, MILLISECONDS, |
684 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
685 |
> |
shouldThrow(); |
686 |
> |
} catch (IllegalArgumentException success) {} |
687 |
|
} |
688 |
< |
|
689 |
< |
//---- Tests if workQueue is set to null |
690 |
< |
public void testNullPointerException() { |
691 |
< |
try{ |
692 |
< |
new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null); |
693 |
< |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
694 |
< |
} |
695 |
< |
catch (NullPointerException n){} |
688 |
> |
|
689 |
> |
/** |
690 |
> |
* Constructor throws if workQueue is set to null |
691 |
> |
*/ |
692 |
> |
public void testConstructorNullPointerException() { |
693 |
> |
try { |
694 |
> |
new ThreadPoolExecutor(1, 2, |
695 |
> |
LONG_DELAY_MS, MILLISECONDS, |
696 |
> |
(BlockingQueue) null); |
697 |
> |
shouldThrow(); |
698 |
> |
} catch (NullPointerException success) {} |
699 |
|
} |
322 |
– |
|
700 |
|
|
701 |
< |
|
702 |
< |
//---- Tests if corePoolSize argument is less than zero |
701 |
> |
/** |
702 |
> |
* Constructor throws if corePoolSize argument is less than zero |
703 |
> |
*/ |
704 |
|
public void testConstructor6() { |
705 |
< |
try{ |
706 |
< |
new ThreadPoolExecutor(-1,1,100L,TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread()); |
707 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
708 |
< |
}catch (IllegalArgumentException i6){} |
705 |
> |
try { |
706 |
> |
new ThreadPoolExecutor(-1, 1, |
707 |
> |
LONG_DELAY_MS, MILLISECONDS, |
708 |
> |
new ArrayBlockingQueue<Runnable>(10), |
709 |
> |
new SimpleThreadFactory()); |
710 |
> |
shouldThrow(); |
711 |
> |
} catch (IllegalArgumentException success) {} |
712 |
|
} |
713 |
< |
|
714 |
< |
//---- Tests if maximumPoolSize is less than zero |
713 |
> |
|
714 |
> |
/** |
715 |
> |
* Constructor throws if maximumPoolSize is less than zero |
716 |
> |
*/ |
717 |
|
public void testConstructor7() { |
718 |
< |
try{ |
719 |
< |
new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread()); |
720 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
721 |
< |
} |
722 |
< |
catch (IllegalArgumentException i7){} |
718 |
> |
try { |
719 |
> |
new ThreadPoolExecutor(1, -1, |
720 |
> |
LONG_DELAY_MS, MILLISECONDS, |
721 |
> |
new ArrayBlockingQueue<Runnable>(10), |
722 |
> |
new SimpleThreadFactory()); |
723 |
> |
shouldThrow(); |
724 |
> |
} catch (IllegalArgumentException success) {} |
725 |
|
} |
726 |
|
|
727 |
< |
//---- Tests if maximumPoolSize is equal to zero |
727 |
> |
/** |
728 |
> |
* Constructor throws if maximumPoolSize is equal to zero |
729 |
> |
*/ |
730 |
|
public void testConstructor8() { |
731 |
< |
try{ |
732 |
< |
new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread()); |
733 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
734 |
< |
} |
735 |
< |
catch (IllegalArgumentException i8){} |
731 |
> |
try { |
732 |
> |
new ThreadPoolExecutor(1, 0, |
733 |
> |
LONG_DELAY_MS, MILLISECONDS, |
734 |
> |
new ArrayBlockingQueue<Runnable>(10), |
735 |
> |
new SimpleThreadFactory()); |
736 |
> |
shouldThrow(); |
737 |
> |
} catch (IllegalArgumentException success) {} |
738 |
|
} |
739 |
|
|
740 |
< |
//---- Tests if keepAliveTime is less than zero |
740 |
> |
/** |
741 |
> |
* Constructor throws if keepAliveTime is less than zero |
742 |
> |
*/ |
743 |
|
public void testConstructor9() { |
744 |
< |
try{ |
745 |
< |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread()); |
746 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
747 |
< |
} |
748 |
< |
catch (IllegalArgumentException i9){} |
744 |
> |
try { |
745 |
> |
new ThreadPoolExecutor(1, 2, |
746 |
> |
-1L, MILLISECONDS, |
747 |
> |
new ArrayBlockingQueue<Runnable>(10), |
748 |
> |
new SimpleThreadFactory()); |
749 |
> |
shouldThrow(); |
750 |
> |
} catch (IllegalArgumentException success) {} |
751 |
|
} |
752 |
|
|
753 |
< |
//---- Tests if corePoolSize is greater than the maximumPoolSize |
753 |
> |
/** |
754 |
> |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
755 |
> |
*/ |
756 |
|
public void testConstructor10() { |
757 |
< |
try{ |
758 |
< |
new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread()); |
759 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
757 |
> |
try { |
758 |
> |
new ThreadPoolExecutor(2, 1, |
759 |
> |
LONG_DELAY_MS, MILLISECONDS, |
760 |
> |
new ArrayBlockingQueue<Runnable>(10), |
761 |
> |
new SimpleThreadFactory()); |
762 |
> |
shouldThrow(); |
763 |
> |
} catch (IllegalArgumentException success) {} |
764 |
> |
} |
765 |
> |
|
766 |
> |
/** |
767 |
> |
* Constructor throws if workQueue is set to null |
768 |
> |
*/ |
769 |
> |
public void testConstructorNullPointerException2() { |
770 |
> |
try { |
771 |
> |
new ThreadPoolExecutor(1, 2, |
772 |
> |
LONG_DELAY_MS, MILLISECONDS, |
773 |
> |
(BlockingQueue) null, |
774 |
> |
new SimpleThreadFactory()); |
775 |
> |
shouldThrow(); |
776 |
> |
} catch (NullPointerException success) {} |
777 |
> |
} |
778 |
> |
|
779 |
> |
/** |
780 |
> |
* Constructor throws if threadFactory is set to null |
781 |
> |
*/ |
782 |
> |
public void testConstructorNullPointerException3() { |
783 |
> |
try { |
784 |
> |
new ThreadPoolExecutor(1, 2, |
785 |
> |
LONG_DELAY_MS, MILLISECONDS, |
786 |
> |
new ArrayBlockingQueue<Runnable>(10), |
787 |
> |
(ThreadFactory) null); |
788 |
> |
shouldThrow(); |
789 |
> |
} catch (NullPointerException success) {} |
790 |
> |
} |
791 |
> |
|
792 |
> |
/** |
793 |
> |
* Constructor throws if corePoolSize argument is less than zero |
794 |
> |
*/ |
795 |
> |
public void testConstructor11() { |
796 |
> |
try { |
797 |
> |
new ThreadPoolExecutor(-1, 1, |
798 |
> |
LONG_DELAY_MS, MILLISECONDS, |
799 |
> |
new ArrayBlockingQueue<Runnable>(10), |
800 |
> |
new NoOpREHandler()); |
801 |
> |
shouldThrow(); |
802 |
> |
} catch (IllegalArgumentException success) {} |
803 |
> |
} |
804 |
> |
|
805 |
> |
/** |
806 |
> |
* Constructor throws if maximumPoolSize is less than zero |
807 |
> |
*/ |
808 |
> |
public void testConstructor12() { |
809 |
> |
try { |
810 |
> |
new ThreadPoolExecutor(1, -1, |
811 |
> |
LONG_DELAY_MS, MILLISECONDS, |
812 |
> |
new ArrayBlockingQueue<Runnable>(10), |
813 |
> |
new NoOpREHandler()); |
814 |
> |
shouldThrow(); |
815 |
> |
} catch (IllegalArgumentException success) {} |
816 |
> |
} |
817 |
> |
|
818 |
> |
/** |
819 |
> |
* Constructor throws if maximumPoolSize is equal to zero |
820 |
> |
*/ |
821 |
> |
public void testConstructor13() { |
822 |
> |
try { |
823 |
> |
new ThreadPoolExecutor(1, 0, |
824 |
> |
LONG_DELAY_MS, MILLISECONDS, |
825 |
> |
new ArrayBlockingQueue<Runnable>(10), |
826 |
> |
new NoOpREHandler()); |
827 |
> |
shouldThrow(); |
828 |
> |
} catch (IllegalArgumentException success) {} |
829 |
> |
} |
830 |
> |
|
831 |
> |
/** |
832 |
> |
* Constructor throws if keepAliveTime is less than zero |
833 |
> |
*/ |
834 |
> |
public void testConstructor14() { |
835 |
> |
try { |
836 |
> |
new ThreadPoolExecutor(1, 2, |
837 |
> |
-1L, MILLISECONDS, |
838 |
> |
new ArrayBlockingQueue<Runnable>(10), |
839 |
> |
new NoOpREHandler()); |
840 |
> |
shouldThrow(); |
841 |
> |
} catch (IllegalArgumentException success) {} |
842 |
> |
} |
843 |
> |
|
844 |
> |
/** |
845 |
> |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
846 |
> |
*/ |
847 |
> |
public void testConstructor15() { |
848 |
> |
try { |
849 |
> |
new ThreadPoolExecutor(2, 1, |
850 |
> |
LONG_DELAY_MS, MILLISECONDS, |
851 |
> |
new ArrayBlockingQueue<Runnable>(10), |
852 |
> |
new NoOpREHandler()); |
853 |
> |
shouldThrow(); |
854 |
> |
} catch (IllegalArgumentException success) {} |
855 |
> |
} |
856 |
> |
|
857 |
> |
/** |
858 |
> |
* Constructor throws if workQueue is set to null |
859 |
> |
*/ |
860 |
> |
public void testConstructorNullPointerException4() { |
861 |
> |
try { |
862 |
> |
new ThreadPoolExecutor(1, 2, |
863 |
> |
LONG_DELAY_MS, MILLISECONDS, |
864 |
> |
(BlockingQueue) null, |
865 |
> |
new NoOpREHandler()); |
866 |
> |
shouldThrow(); |
867 |
> |
} catch (NullPointerException success) {} |
868 |
> |
} |
869 |
> |
|
870 |
> |
/** |
871 |
> |
* Constructor throws if handler is set to null |
872 |
> |
*/ |
873 |
> |
public void testConstructorNullPointerException5() { |
874 |
> |
try { |
875 |
> |
new ThreadPoolExecutor(1, 2, |
876 |
> |
LONG_DELAY_MS, MILLISECONDS, |
877 |
> |
new ArrayBlockingQueue<Runnable>(10), |
878 |
> |
(RejectedExecutionHandler) null); |
879 |
> |
shouldThrow(); |
880 |
> |
} catch (NullPointerException success) {} |
881 |
> |
} |
882 |
> |
|
883 |
> |
/** |
884 |
> |
* Constructor throws if corePoolSize argument is less than zero |
885 |
> |
*/ |
886 |
> |
public void testConstructor16() { |
887 |
> |
try { |
888 |
> |
new ThreadPoolExecutor(-1, 1, |
889 |
> |
LONG_DELAY_MS, MILLISECONDS, |
890 |
> |
new ArrayBlockingQueue<Runnable>(10), |
891 |
> |
new SimpleThreadFactory(), |
892 |
> |
new NoOpREHandler()); |
893 |
> |
shouldThrow(); |
894 |
> |
} catch (IllegalArgumentException success) {} |
895 |
> |
} |
896 |
> |
|
897 |
> |
/** |
898 |
> |
* Constructor throws if maximumPoolSize is less than zero |
899 |
> |
*/ |
900 |
> |
public void testConstructor17() { |
901 |
> |
try { |
902 |
> |
new ThreadPoolExecutor(1, -1, |
903 |
> |
LONG_DELAY_MS, MILLISECONDS, |
904 |
> |
new ArrayBlockingQueue<Runnable>(10), |
905 |
> |
new SimpleThreadFactory(), |
906 |
> |
new NoOpREHandler()); |
907 |
> |
shouldThrow(); |
908 |
> |
} catch (IllegalArgumentException success) {} |
909 |
> |
} |
910 |
> |
|
911 |
> |
/** |
912 |
> |
* Constructor throws if maximumPoolSize is equal to zero |
913 |
> |
*/ |
914 |
> |
public void testConstructor18() { |
915 |
> |
try { |
916 |
> |
new ThreadPoolExecutor(1, 0, |
917 |
> |
LONG_DELAY_MS, MILLISECONDS, |
918 |
> |
new ArrayBlockingQueue<Runnable>(10), |
919 |
> |
new SimpleThreadFactory(), |
920 |
> |
new NoOpREHandler()); |
921 |
> |
shouldThrow(); |
922 |
> |
} catch (IllegalArgumentException success) {} |
923 |
> |
} |
924 |
> |
|
925 |
> |
/** |
926 |
> |
* Constructor throws if keepAliveTime is less than zero |
927 |
> |
*/ |
928 |
> |
public void testConstructor19() { |
929 |
> |
try { |
930 |
> |
new ThreadPoolExecutor(1, 2, |
931 |
> |
-1L, MILLISECONDS, |
932 |
> |
new ArrayBlockingQueue<Runnable>(10), |
933 |
> |
new SimpleThreadFactory(), |
934 |
> |
new NoOpREHandler()); |
935 |
> |
shouldThrow(); |
936 |
> |
} catch (IllegalArgumentException success) {} |
937 |
> |
} |
938 |
> |
|
939 |
> |
/** |
940 |
> |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
941 |
> |
*/ |
942 |
> |
public void testConstructor20() { |
943 |
> |
try { |
944 |
> |
new ThreadPoolExecutor(2, 1, |
945 |
> |
LONG_DELAY_MS, MILLISECONDS, |
946 |
> |
new ArrayBlockingQueue<Runnable>(10), |
947 |
> |
new SimpleThreadFactory(), |
948 |
> |
new NoOpREHandler()); |
949 |
> |
shouldThrow(); |
950 |
> |
} catch (IllegalArgumentException success) {} |
951 |
> |
} |
952 |
> |
|
953 |
> |
/** |
954 |
> |
* Constructor throws if workQueue is null |
955 |
> |
*/ |
956 |
> |
public void testConstructorNullPointerException6() { |
957 |
> |
try { |
958 |
> |
new ThreadPoolExecutor(1, 2, |
959 |
> |
LONG_DELAY_MS, MILLISECONDS, |
960 |
> |
(BlockingQueue) null, |
961 |
> |
new SimpleThreadFactory(), |
962 |
> |
new NoOpREHandler()); |
963 |
> |
shouldThrow(); |
964 |
> |
} catch (NullPointerException success) {} |
965 |
> |
} |
966 |
> |
|
967 |
> |
/** |
968 |
> |
* Constructor throws if handler is null |
969 |
> |
*/ |
970 |
> |
public void testConstructorNullPointerException7() { |
971 |
> |
try { |
972 |
> |
new ThreadPoolExecutor(1, 2, |
973 |
> |
LONG_DELAY_MS, MILLISECONDS, |
974 |
> |
new ArrayBlockingQueue<Runnable>(10), |
975 |
> |
new SimpleThreadFactory(), |
976 |
> |
(RejectedExecutionHandler) null); |
977 |
> |
shouldThrow(); |
978 |
> |
} catch (NullPointerException success) {} |
979 |
> |
} |
980 |
> |
|
981 |
> |
/** |
982 |
> |
* Constructor throws if ThreadFactory is null |
983 |
> |
*/ |
984 |
> |
public void testConstructorNullPointerException8() { |
985 |
> |
try { |
986 |
> |
new ThreadPoolExecutor(1, 2, |
987 |
> |
LONG_DELAY_MS, MILLISECONDS, |
988 |
> |
new ArrayBlockingQueue<Runnable>(10), |
989 |
> |
(ThreadFactory) null, |
990 |
> |
new NoOpREHandler()); |
991 |
> |
shouldThrow(); |
992 |
> |
} catch (NullPointerException success) {} |
993 |
> |
} |
994 |
> |
|
995 |
> |
/** |
996 |
> |
* get of submitted callable throws InterruptedException if interrupted |
997 |
> |
*/ |
998 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
999 |
> |
final ThreadPoolExecutor p = |
1000 |
> |
new ThreadPoolExecutor(1, 1, |
1001 |
> |
60, TimeUnit.SECONDS, |
1002 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1003 |
> |
|
1004 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1005 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1006 |
> |
try { |
1007 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1008 |
> |
public void realRun() throws Exception { |
1009 |
> |
Callable task = new CheckedCallable<Boolean>() { |
1010 |
> |
public Boolean realCall() throws InterruptedException { |
1011 |
> |
threadStarted.countDown(); |
1012 |
> |
done.await(); |
1013 |
> |
return Boolean.TRUE; |
1014 |
> |
}}; |
1015 |
> |
p.submit(task).get(); |
1016 |
> |
}}); |
1017 |
> |
|
1018 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1019 |
> |
t.interrupt(); |
1020 |
> |
awaitTermination(t, MEDIUM_DELAY_MS); |
1021 |
> |
} finally { |
1022 |
> |
done.countDown(); |
1023 |
> |
joinPool(p); |
1024 |
|
} |
366 |
– |
catch (IllegalArgumentException i10){} |
1025 |
|
} |
1026 |
|
|
1027 |
< |
//---- Tests if workQueue is set to null |
1028 |
< |
public void testNullPointerException2() { |
1029 |
< |
try{ |
1030 |
< |
new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread()); |
1031 |
< |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
1027 |
> |
/** |
1028 |
> |
* execute throws RejectedExecutionException if saturated. |
1029 |
> |
*/ |
1030 |
> |
public void testSaturatedExecute() { |
1031 |
> |
ThreadPoolExecutor p = |
1032 |
> |
new ThreadPoolExecutor(1, 1, |
1033 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1034 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1035 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1036 |
> |
try { |
1037 |
> |
Runnable task = new CheckedRunnable() { |
1038 |
> |
public void realRun() throws InterruptedException { |
1039 |
> |
done.await(); |
1040 |
> |
}}; |
1041 |
> |
for (int i = 0; i < 2; ++i) |
1042 |
> |
p.execute(task); |
1043 |
> |
for (int i = 0; i < 2; ++i) { |
1044 |
> |
try { |
1045 |
> |
p.execute(task); |
1046 |
> |
shouldThrow(); |
1047 |
> |
} catch (RejectedExecutionException success) {} |
1048 |
> |
assertTrue(p.getTaskCount() <= 2); |
1049 |
> |
} |
1050 |
> |
} finally { |
1051 |
> |
done.countDown(); |
1052 |
> |
joinPool(p); |
1053 |
|
} |
375 |
– |
catch (NullPointerException n2){} |
1054 |
|
} |
1055 |
|
|
1056 |
< |
//---- Tests if threadFactory is set to null |
1057 |
< |
public void testNullPointerException3() { |
1058 |
< |
try{ |
1059 |
< |
ThreadFactory f = null; |
1060 |
< |
new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
1061 |
< |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
1056 |
> |
/** |
1057 |
> |
* submit(runnable) throws RejectedExecutionException if saturated. |
1058 |
> |
*/ |
1059 |
> |
public void testSaturatedSubmitRunnable() { |
1060 |
> |
ThreadPoolExecutor p = |
1061 |
> |
new ThreadPoolExecutor(1, 1, |
1062 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1063 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1064 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1065 |
> |
try { |
1066 |
> |
Runnable task = new CheckedRunnable() { |
1067 |
> |
public void realRun() throws InterruptedException { |
1068 |
> |
done.await(); |
1069 |
> |
}}; |
1070 |
> |
for (int i = 0; i < 2; ++i) |
1071 |
> |
p.submit(task); |
1072 |
> |
for (int i = 0; i < 2; ++i) { |
1073 |
> |
try { |
1074 |
> |
p.execute(task); |
1075 |
> |
shouldThrow(); |
1076 |
> |
} catch (RejectedExecutionException success) {} |
1077 |
> |
assertTrue(p.getTaskCount() <= 2); |
1078 |
> |
} |
1079 |
> |
} finally { |
1080 |
> |
done.countDown(); |
1081 |
> |
joinPool(p); |
1082 |
|
} |
385 |
– |
catch (NullPointerException n3){} |
1083 |
|
} |
1084 |
< |
|
1085 |
< |
|
1086 |
< |
//---- Tests if corePoolSize argument is less than zero |
1087 |
< |
public void testConstructor11() { |
1088 |
< |
try{ |
1089 |
< |
new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject()); |
1090 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1084 |
> |
|
1085 |
> |
/** |
1086 |
> |
* submit(callable) throws RejectedExecutionException if saturated. |
1087 |
> |
*/ |
1088 |
> |
public void testSaturatedSubmitCallable() { |
1089 |
> |
ThreadPoolExecutor p = |
1090 |
> |
new ThreadPoolExecutor(1, 1, |
1091 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1092 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1093 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1094 |
> |
try { |
1095 |
> |
Runnable task = new CheckedRunnable() { |
1096 |
> |
public void realRun() throws InterruptedException { |
1097 |
> |
done.await(); |
1098 |
> |
}}; |
1099 |
> |
for (int i = 0; i < 2; ++i) |
1100 |
> |
p.submit(Executors.callable(task)); |
1101 |
> |
for (int i = 0; i < 2; ++i) { |
1102 |
> |
try { |
1103 |
> |
p.execute(task); |
1104 |
> |
shouldThrow(); |
1105 |
> |
} catch (RejectedExecutionException success) {} |
1106 |
> |
assertTrue(p.getTaskCount() <= 2); |
1107 |
> |
} |
1108 |
> |
} finally { |
1109 |
> |
done.countDown(); |
1110 |
> |
joinPool(p); |
1111 |
|
} |
395 |
– |
catch (IllegalArgumentException i11){} |
1112 |
|
} |
1113 |
|
|
1114 |
< |
//---- Tests if maximumPoolSize is less than zero |
1115 |
< |
public void testConstructor12() { |
1116 |
< |
try{ |
1117 |
< |
new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject()); |
1118 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1114 |
> |
/** |
1115 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
1116 |
> |
*/ |
1117 |
> |
public void testSaturatedExecute2() { |
1118 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1119 |
> |
final ThreadPoolExecutor p = |
1120 |
> |
new ThreadPoolExecutor(1, 1, |
1121 |
> |
LONG_DELAY_MS, |
1122 |
> |
MILLISECONDS, |
1123 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1124 |
> |
h); |
1125 |
> |
try { |
1126 |
> |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1127 |
> |
for (int i = 0; i < tasks.length; ++i) |
1128 |
> |
tasks[i] = new TrackedNoOpRunnable(); |
1129 |
> |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1130 |
> |
p.execute(mr); |
1131 |
> |
for (int i = 0; i < tasks.length; ++i) |
1132 |
> |
p.execute(tasks[i]); |
1133 |
> |
for (int i = 1; i < tasks.length; ++i) |
1134 |
> |
assertTrue(tasks[i].done); |
1135 |
> |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1136 |
> |
} finally { |
1137 |
> |
joinPool(p); |
1138 |
|
} |
404 |
– |
catch (IllegalArgumentException i12){} |
1139 |
|
} |
1140 |
|
|
1141 |
< |
//---- Tests if maximumPoolSize is equal to zero |
1142 |
< |
public void testConstructor13() { |
1143 |
< |
try{ |
1144 |
< |
new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject()); |
1145 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1141 |
> |
/** |
1142 |
> |
* executor using DiscardPolicy drops task if saturated. |
1143 |
> |
*/ |
1144 |
> |
public void testSaturatedExecute3() { |
1145 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1146 |
> |
final ThreadPoolExecutor p = |
1147 |
> |
new ThreadPoolExecutor(1, 1, |
1148 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1149 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1150 |
> |
h); |
1151 |
> |
try { |
1152 |
> |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1153 |
> |
for (int i = 0; i < tasks.length; ++i) |
1154 |
> |
tasks[i] = new TrackedNoOpRunnable(); |
1155 |
> |
p.execute(new TrackedLongRunnable()); |
1156 |
> |
for (TrackedNoOpRunnable task : tasks) |
1157 |
> |
p.execute(task); |
1158 |
> |
for (TrackedNoOpRunnable task : tasks) |
1159 |
> |
assertFalse(task.done); |
1160 |
> |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1161 |
> |
} finally { |
1162 |
> |
joinPool(p); |
1163 |
|
} |
413 |
– |
catch (IllegalArgumentException i13){} |
1164 |
|
} |
1165 |
|
|
1166 |
< |
//---- Tests if keepAliveTime is less than zero |
1167 |
< |
public void testConstructor14() { |
1168 |
< |
try{ |
1169 |
< |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject()); |
1170 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1166 |
> |
/** |
1167 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1168 |
> |
*/ |
1169 |
> |
public void testSaturatedExecute4() { |
1170 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1171 |
> |
final ThreadPoolExecutor p = |
1172 |
> |
new ThreadPoolExecutor(1, 1, |
1173 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1174 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1175 |
> |
h); |
1176 |
> |
try { |
1177 |
> |
p.execute(new TrackedLongRunnable()); |
1178 |
> |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1179 |
> |
p.execute(r2); |
1180 |
> |
assertTrue(p.getQueue().contains(r2)); |
1181 |
> |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1182 |
> |
p.execute(r3); |
1183 |
> |
assertFalse(p.getQueue().contains(r2)); |
1184 |
> |
assertTrue(p.getQueue().contains(r3)); |
1185 |
> |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1186 |
> |
} finally { |
1187 |
> |
joinPool(p); |
1188 |
|
} |
422 |
– |
catch (IllegalArgumentException i14){} |
1189 |
|
} |
1190 |
|
|
1191 |
< |
//---- Tests if corePoolSize is greater than the maximumPoolSize |
1192 |
< |
public void testConstructor15() { |
1193 |
< |
try{ |
1194 |
< |
new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testReject()); |
1195 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1191 |
> |
/** |
1192 |
> |
* execute throws RejectedExecutionException if shutdown |
1193 |
> |
*/ |
1194 |
> |
public void testRejectedExecutionExceptionOnShutdown() { |
1195 |
> |
ThreadPoolExecutor p = |
1196 |
> |
new ThreadPoolExecutor(1, 1, |
1197 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1198 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1199 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1200 |
> |
try { |
1201 |
> |
p.execute(new NoOpRunnable()); |
1202 |
> |
shouldThrow(); |
1203 |
> |
} catch (RejectedExecutionException success) {} |
1204 |
> |
|
1205 |
> |
joinPool(p); |
1206 |
> |
} |
1207 |
> |
|
1208 |
> |
/** |
1209 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
1210 |
> |
*/ |
1211 |
> |
public void testCallerRunsOnShutdown() { |
1212 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1213 |
> |
final ThreadPoolExecutor p = |
1214 |
> |
new ThreadPoolExecutor(1, 1, |
1215 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1216 |
> |
new ArrayBlockingQueue<Runnable>(1), h); |
1217 |
> |
|
1218 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1219 |
> |
try { |
1220 |
> |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1221 |
> |
p.execute(r); |
1222 |
> |
assertFalse(r.done); |
1223 |
> |
} finally { |
1224 |
> |
joinPool(p); |
1225 |
|
} |
431 |
– |
catch (IllegalArgumentException i15){} |
1226 |
|
} |
1227 |
|
|
1228 |
< |
//---- Tests if workQueue is set to null |
1229 |
< |
public void testNullPointerException4() { |
1230 |
< |
try{ |
1231 |
< |
new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testReject()); |
1232 |
< |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
1228 |
> |
/** |
1229 |
> |
* execute using DiscardPolicy drops task on shutdown |
1230 |
> |
*/ |
1231 |
> |
public void testDiscardOnShutdown() { |
1232 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1233 |
> |
ThreadPoolExecutor p = |
1234 |
> |
new ThreadPoolExecutor(1, 1, |
1235 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1236 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1237 |
> |
h); |
1238 |
> |
|
1239 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1240 |
> |
try { |
1241 |
> |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1242 |
> |
p.execute(r); |
1243 |
> |
assertFalse(r.done); |
1244 |
> |
} finally { |
1245 |
> |
joinPool(p); |
1246 |
|
} |
440 |
– |
catch (NullPointerException n4){} |
1247 |
|
} |
1248 |
|
|
1249 |
< |
//---- Tests if handler is set to null |
1250 |
< |
public void testNullPointerException5() { |
1251 |
< |
try{ |
1252 |
< |
RejectedExecutionHandler r = null; |
1253 |
< |
new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
1254 |
< |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
1249 |
> |
/** |
1250 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
1251 |
> |
*/ |
1252 |
> |
public void testDiscardOldestOnShutdown() { |
1253 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1254 |
> |
ThreadPoolExecutor p = |
1255 |
> |
new ThreadPoolExecutor(1, 1, |
1256 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1257 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1258 |
> |
h); |
1259 |
> |
|
1260 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1261 |
> |
try { |
1262 |
> |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1263 |
> |
p.execute(r); |
1264 |
> |
assertFalse(r.done); |
1265 |
> |
} finally { |
1266 |
> |
joinPool(p); |
1267 |
|
} |
450 |
– |
catch (NullPointerException n5){} |
1268 |
|
} |
1269 |
|
|
1270 |
< |
|
1271 |
< |
//---- Tests if corePoolSize argument is less than zero |
1272 |
< |
public void testConstructor16() { |
1273 |
< |
try{ |
1274 |
< |
new ThreadPoolExecutor(-1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject()); |
1275 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1270 |
> |
/** |
1271 |
> |
* execute(null) throws NPE |
1272 |
> |
*/ |
1273 |
> |
public void testExecuteNull() { |
1274 |
> |
ThreadPoolExecutor p = |
1275 |
> |
new ThreadPoolExecutor(1, 2, |
1276 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1277 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1278 |
> |
try { |
1279 |
> |
p.execute(null); |
1280 |
> |
shouldThrow(); |
1281 |
> |
} catch (NullPointerException success) {} |
1282 |
> |
|
1283 |
> |
joinPool(p); |
1284 |
> |
} |
1285 |
> |
|
1286 |
> |
/** |
1287 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
1288 |
> |
*/ |
1289 |
> |
public void testCorePoolSizeIllegalArgumentException() { |
1290 |
> |
ThreadPoolExecutor p = |
1291 |
> |
new ThreadPoolExecutor(1, 2, |
1292 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1293 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1294 |
> |
try { |
1295 |
> |
p.setCorePoolSize(-1); |
1296 |
> |
shouldThrow(); |
1297 |
> |
} catch (IllegalArgumentException success) { |
1298 |
> |
} finally { |
1299 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1300 |
|
} |
1301 |
< |
catch (IllegalArgumentException i16){} |
1301 |
> |
joinPool(p); |
1302 |
|
} |
1303 |
|
|
1304 |
< |
//---- Tests if maximumPoolSize is less than zero |
1305 |
< |
public void testConstructor17() { |
1306 |
< |
try{ |
1307 |
< |
new ThreadPoolExecutor(1,-1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject()); |
1308 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1304 |
> |
/** |
1305 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1306 |
> |
* given a value less the core pool size |
1307 |
> |
*/ |
1308 |
> |
public void testMaximumPoolSizeIllegalArgumentException() { |
1309 |
> |
ThreadPoolExecutor p = |
1310 |
> |
new ThreadPoolExecutor(2, 3, |
1311 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1312 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1313 |
> |
try { |
1314 |
> |
p.setMaximumPoolSize(1); |
1315 |
> |
shouldThrow(); |
1316 |
> |
} catch (IllegalArgumentException success) { |
1317 |
> |
} finally { |
1318 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1319 |
|
} |
1320 |
< |
catch (IllegalArgumentException i17){} |
1320 |
> |
joinPool(p); |
1321 |
|
} |
1322 |
|
|
1323 |
< |
//---- Tests if maximumPoolSize is equal to zero |
1324 |
< |
public void testConstructor18() { |
1325 |
< |
try{ |
1326 |
< |
new ThreadPoolExecutor(1,0,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject()); |
1327 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1323 |
> |
/** |
1324 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
1325 |
> |
* if given a negative value |
1326 |
> |
*/ |
1327 |
> |
public void testMaximumPoolSizeIllegalArgumentException2() { |
1328 |
> |
ThreadPoolExecutor p = |
1329 |
> |
new ThreadPoolExecutor(2, 3, |
1330 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1331 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1332 |
> |
try { |
1333 |
> |
p.setMaximumPoolSize(-1); |
1334 |
> |
shouldThrow(); |
1335 |
> |
} catch (IllegalArgumentException success) { |
1336 |
> |
} finally { |
1337 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1338 |
|
} |
1339 |
< |
catch (IllegalArgumentException i18){} |
1339 |
> |
joinPool(p); |
1340 |
|
} |
1341 |
|
|
1342 |
< |
//---- Tests if keepAliveTime is less than zero |
1343 |
< |
public void testConstructor19() { |
1344 |
< |
try{ |
1345 |
< |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject()); |
1346 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1342 |
> |
/** |
1343 |
> |
* setKeepAliveTime throws IllegalArgumentException |
1344 |
> |
* when given a negative value |
1345 |
> |
*/ |
1346 |
> |
public void testKeepAliveTimeIllegalArgumentException() { |
1347 |
> |
ThreadPoolExecutor p = |
1348 |
> |
new ThreadPoolExecutor(2, 3, |
1349 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1350 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1351 |
> |
try { |
1352 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1353 |
> |
shouldThrow(); |
1354 |
> |
} catch (IllegalArgumentException success) { |
1355 |
> |
} finally { |
1356 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1357 |
|
} |
1358 |
< |
catch (IllegalArgumentException i19){} |
1358 |
> |
joinPool(p); |
1359 |
|
} |
1360 |
|
|
1361 |
< |
//---- Tests if corePoolSize is greater than the maximumPoolSize |
1362 |
< |
public void testConstructor20() { |
1363 |
< |
try{ |
1364 |
< |
new ThreadPoolExecutor(2,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new testThread(),new testReject()); |
1365 |
< |
fail("ThreadPoolExecutor constructor should throw an IllegalArgumentException"); |
1361 |
> |
/** |
1362 |
> |
* terminated() is called on termination |
1363 |
> |
*/ |
1364 |
> |
public void testTerminated() { |
1365 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1366 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1367 |
> |
assertTrue(p.terminatedCalled()); |
1368 |
> |
joinPool(p); |
1369 |
> |
} |
1370 |
> |
|
1371 |
> |
/** |
1372 |
> |
* beforeExecute and afterExecute are called when executing task |
1373 |
> |
*/ |
1374 |
> |
public void testBeforeAfter() throws InterruptedException { |
1375 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1376 |
> |
try { |
1377 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1378 |
> |
final CheckedRunnable task = new CheckedRunnable() { |
1379 |
> |
public void realRun() { |
1380 |
> |
done.countDown(); |
1381 |
> |
}}; |
1382 |
> |
p.execute(task); |
1383 |
> |
await(p.afterCalled); |
1384 |
> |
assertEquals(0, done.getCount()); |
1385 |
> |
assertTrue(p.afterCalled()); |
1386 |
> |
assertTrue(p.beforeCalled()); |
1387 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1388 |
> |
} finally { |
1389 |
> |
joinPool(p); |
1390 |
> |
} |
1391 |
> |
} |
1392 |
> |
|
1393 |
> |
/** |
1394 |
> |
* completed submit of callable returns result |
1395 |
> |
*/ |
1396 |
> |
public void testSubmitCallable() throws Exception { |
1397 |
> |
ExecutorService e = |
1398 |
> |
new ThreadPoolExecutor(2, 2, |
1399 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1400 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1401 |
> |
try { |
1402 |
> |
Future<String> future = e.submit(new StringTask()); |
1403 |
> |
String result = future.get(); |
1404 |
> |
assertSame(TEST_STRING, result); |
1405 |
> |
} finally { |
1406 |
> |
joinPool(e); |
1407 |
> |
} |
1408 |
> |
} |
1409 |
> |
|
1410 |
> |
/** |
1411 |
> |
* completed submit of runnable returns successfully |
1412 |
> |
*/ |
1413 |
> |
public void testSubmitRunnable() throws Exception { |
1414 |
> |
ExecutorService e = |
1415 |
> |
new ThreadPoolExecutor(2, 2, |
1416 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1417 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1418 |
> |
try { |
1419 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
1420 |
> |
future.get(); |
1421 |
> |
assertTrue(future.isDone()); |
1422 |
> |
} finally { |
1423 |
> |
joinPool(e); |
1424 |
> |
} |
1425 |
> |
} |
1426 |
> |
|
1427 |
> |
/** |
1428 |
> |
* completed submit of (runnable, result) returns result |
1429 |
> |
*/ |
1430 |
> |
public void testSubmitRunnable2() throws Exception { |
1431 |
> |
ExecutorService e = |
1432 |
> |
new ThreadPoolExecutor(2, 2, |
1433 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1434 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1435 |
> |
try { |
1436 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1437 |
> |
String result = future.get(); |
1438 |
> |
assertSame(TEST_STRING, result); |
1439 |
> |
} finally { |
1440 |
> |
joinPool(e); |
1441 |
> |
} |
1442 |
> |
} |
1443 |
> |
|
1444 |
> |
/** |
1445 |
> |
* invokeAny(null) throws NPE |
1446 |
> |
*/ |
1447 |
> |
public void testInvokeAny1() throws Exception { |
1448 |
> |
ExecutorService e = |
1449 |
> |
new ThreadPoolExecutor(2, 2, |
1450 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1451 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1452 |
> |
try { |
1453 |
> |
e.invokeAny(null); |
1454 |
> |
shouldThrow(); |
1455 |
> |
} catch (NullPointerException success) { |
1456 |
> |
} finally { |
1457 |
> |
joinPool(e); |
1458 |
> |
} |
1459 |
> |
} |
1460 |
> |
|
1461 |
> |
/** |
1462 |
> |
* invokeAny(empty collection) throws IAE |
1463 |
> |
*/ |
1464 |
> |
public void testInvokeAny2() throws Exception { |
1465 |
> |
ExecutorService e = |
1466 |
> |
new ThreadPoolExecutor(2, 2, |
1467 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1468 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1469 |
> |
try { |
1470 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1471 |
> |
shouldThrow(); |
1472 |
> |
} catch (IllegalArgumentException success) { |
1473 |
> |
} finally { |
1474 |
> |
joinPool(e); |
1475 |
> |
} |
1476 |
> |
} |
1477 |
> |
|
1478 |
> |
/** |
1479 |
> |
* invokeAny(c) throws NPE if c has null elements |
1480 |
> |
*/ |
1481 |
> |
public void testInvokeAny3() throws Exception { |
1482 |
> |
final CountDownLatch latch = new CountDownLatch(1); |
1483 |
> |
final ExecutorService e = |
1484 |
> |
new ThreadPoolExecutor(2, 2, |
1485 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1486 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1487 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1488 |
> |
l.add(latchAwaitingStringTask(latch)); |
1489 |
> |
l.add(null); |
1490 |
> |
try { |
1491 |
> |
e.invokeAny(l); |
1492 |
> |
shouldThrow(); |
1493 |
> |
} catch (NullPointerException success) { |
1494 |
> |
} finally { |
1495 |
> |
latch.countDown(); |
1496 |
> |
joinPool(e); |
1497 |
> |
} |
1498 |
> |
} |
1499 |
> |
|
1500 |
> |
/** |
1501 |
> |
* invokeAny(c) throws ExecutionException if no task completes |
1502 |
> |
*/ |
1503 |
> |
public void testInvokeAny4() throws Exception { |
1504 |
> |
ExecutorService e = |
1505 |
> |
new ThreadPoolExecutor(2, 2, |
1506 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1507 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1508 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1509 |
> |
l.add(new NPETask()); |
1510 |
> |
try { |
1511 |
> |
e.invokeAny(l); |
1512 |
> |
shouldThrow(); |
1513 |
> |
} catch (ExecutionException success) { |
1514 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1515 |
> |
} finally { |
1516 |
> |
joinPool(e); |
1517 |
> |
} |
1518 |
> |
} |
1519 |
> |
|
1520 |
> |
/** |
1521 |
> |
* invokeAny(c) returns result of some task |
1522 |
> |
*/ |
1523 |
> |
public void testInvokeAny5() throws Exception { |
1524 |
> |
ExecutorService e = |
1525 |
> |
new ThreadPoolExecutor(2, 2, |
1526 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1527 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1528 |
> |
try { |
1529 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1530 |
> |
l.add(new StringTask()); |
1531 |
> |
l.add(new StringTask()); |
1532 |
> |
String result = e.invokeAny(l); |
1533 |
> |
assertSame(TEST_STRING, result); |
1534 |
> |
} finally { |
1535 |
> |
joinPool(e); |
1536 |
> |
} |
1537 |
> |
} |
1538 |
> |
|
1539 |
> |
/** |
1540 |
> |
* invokeAll(null) throws NPE |
1541 |
> |
*/ |
1542 |
> |
public void testInvokeAll1() throws Exception { |
1543 |
> |
ExecutorService e = |
1544 |
> |
new ThreadPoolExecutor(2, 2, |
1545 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1546 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1547 |
> |
try { |
1548 |
> |
e.invokeAll(null); |
1549 |
> |
shouldThrow(); |
1550 |
> |
} catch (NullPointerException success) { |
1551 |
> |
} finally { |
1552 |
> |
joinPool(e); |
1553 |
> |
} |
1554 |
> |
} |
1555 |
> |
|
1556 |
> |
/** |
1557 |
> |
* invokeAll(empty collection) returns empty collection |
1558 |
> |
*/ |
1559 |
> |
public void testInvokeAll2() throws InterruptedException { |
1560 |
> |
ExecutorService e = |
1561 |
> |
new ThreadPoolExecutor(2, 2, |
1562 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1563 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1564 |
> |
try { |
1565 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1566 |
> |
assertTrue(r.isEmpty()); |
1567 |
> |
} finally { |
1568 |
> |
joinPool(e); |
1569 |
> |
} |
1570 |
> |
} |
1571 |
> |
|
1572 |
> |
/** |
1573 |
> |
* invokeAll(c) throws NPE if c has null elements |
1574 |
> |
*/ |
1575 |
> |
public void testInvokeAll3() throws Exception { |
1576 |
> |
ExecutorService e = |
1577 |
> |
new ThreadPoolExecutor(2, 2, |
1578 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1579 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1580 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1581 |
> |
l.add(new StringTask()); |
1582 |
> |
l.add(null); |
1583 |
> |
try { |
1584 |
> |
e.invokeAll(l); |
1585 |
> |
shouldThrow(); |
1586 |
> |
} catch (NullPointerException success) { |
1587 |
> |
} finally { |
1588 |
> |
joinPool(e); |
1589 |
> |
} |
1590 |
> |
} |
1591 |
> |
|
1592 |
> |
/** |
1593 |
> |
* get of element of invokeAll(c) throws exception on failed task |
1594 |
> |
*/ |
1595 |
> |
public void testInvokeAll4() throws Exception { |
1596 |
> |
ExecutorService e = |
1597 |
> |
new ThreadPoolExecutor(2, 2, |
1598 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1599 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1600 |
> |
try { |
1601 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1602 |
> |
l.add(new NPETask()); |
1603 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1604 |
> |
assertEquals(1, futures.size()); |
1605 |
> |
try { |
1606 |
> |
futures.get(0).get(); |
1607 |
> |
shouldThrow(); |
1608 |
> |
} catch (ExecutionException success) { |
1609 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1610 |
> |
} |
1611 |
> |
} finally { |
1612 |
> |
joinPool(e); |
1613 |
> |
} |
1614 |
> |
} |
1615 |
> |
|
1616 |
> |
/** |
1617 |
> |
* invokeAll(c) returns results of all completed tasks |
1618 |
> |
*/ |
1619 |
> |
public void testInvokeAll5() throws Exception { |
1620 |
> |
ExecutorService e = |
1621 |
> |
new ThreadPoolExecutor(2, 2, |
1622 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1623 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1624 |
> |
try { |
1625 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1626 |
> |
l.add(new StringTask()); |
1627 |
> |
l.add(new StringTask()); |
1628 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1629 |
> |
assertEquals(2, futures.size()); |
1630 |
> |
for (Future<String> future : futures) |
1631 |
> |
assertSame(TEST_STRING, future.get()); |
1632 |
> |
} finally { |
1633 |
> |
joinPool(e); |
1634 |
> |
} |
1635 |
> |
} |
1636 |
> |
|
1637 |
> |
/** |
1638 |
> |
* timed invokeAny(null) throws NPE |
1639 |
> |
*/ |
1640 |
> |
public void testTimedInvokeAny1() throws Exception { |
1641 |
> |
ExecutorService e = |
1642 |
> |
new ThreadPoolExecutor(2, 2, |
1643 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1644 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1645 |
> |
try { |
1646 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1647 |
> |
shouldThrow(); |
1648 |
> |
} catch (NullPointerException success) { |
1649 |
> |
} finally { |
1650 |
> |
joinPool(e); |
1651 |
> |
} |
1652 |
> |
} |
1653 |
> |
|
1654 |
> |
/** |
1655 |
> |
* timed invokeAny(,,null) throws NPE |
1656 |
> |
*/ |
1657 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1658 |
> |
ExecutorService e = |
1659 |
> |
new ThreadPoolExecutor(2, 2, |
1660 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1661 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1662 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1663 |
> |
l.add(new StringTask()); |
1664 |
> |
try { |
1665 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1666 |
> |
shouldThrow(); |
1667 |
> |
} catch (NullPointerException success) { |
1668 |
> |
} finally { |
1669 |
> |
joinPool(e); |
1670 |
> |
} |
1671 |
> |
} |
1672 |
> |
|
1673 |
> |
/** |
1674 |
> |
* timed invokeAny(empty collection) throws IAE |
1675 |
> |
*/ |
1676 |
> |
public void testTimedInvokeAny2() throws Exception { |
1677 |
> |
ExecutorService e = |
1678 |
> |
new ThreadPoolExecutor(2, 2, |
1679 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1680 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1681 |
> |
try { |
1682 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1683 |
> |
shouldThrow(); |
1684 |
> |
} catch (IllegalArgumentException success) { |
1685 |
> |
} finally { |
1686 |
> |
joinPool(e); |
1687 |
> |
} |
1688 |
> |
} |
1689 |
> |
|
1690 |
> |
/** |
1691 |
> |
* timed invokeAny(c) throws NPE if c has null elements |
1692 |
> |
*/ |
1693 |
> |
public void testTimedInvokeAny3() throws Exception { |
1694 |
> |
final CountDownLatch latch = new CountDownLatch(1); |
1695 |
> |
final ExecutorService e = |
1696 |
> |
new ThreadPoolExecutor(2, 2, |
1697 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1698 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1699 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1700 |
> |
l.add(latchAwaitingStringTask(latch)); |
1701 |
> |
l.add(null); |
1702 |
> |
try { |
1703 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1704 |
> |
shouldThrow(); |
1705 |
> |
} catch (NullPointerException success) { |
1706 |
> |
} finally { |
1707 |
> |
latch.countDown(); |
1708 |
> |
joinPool(e); |
1709 |
> |
} |
1710 |
> |
} |
1711 |
> |
|
1712 |
> |
/** |
1713 |
> |
* timed invokeAny(c) throws ExecutionException if no task completes |
1714 |
> |
*/ |
1715 |
> |
public void testTimedInvokeAny4() throws Exception { |
1716 |
> |
ExecutorService e = |
1717 |
> |
new ThreadPoolExecutor(2, 2, |
1718 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1719 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1720 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1721 |
> |
l.add(new NPETask()); |
1722 |
> |
try { |
1723 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1724 |
> |
shouldThrow(); |
1725 |
> |
} catch (ExecutionException success) { |
1726 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1727 |
> |
} finally { |
1728 |
> |
joinPool(e); |
1729 |
|
} |
496 |
– |
catch (IllegalArgumentException i20){} |
1730 |
|
} |
1731 |
|
|
1732 |
< |
//---- Tests if workQueue is set to null |
1733 |
< |
public void testNullPointerException6() { |
1734 |
< |
try{ |
1735 |
< |
new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,null,new testThread(),new testReject()); |
1736 |
< |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
1732 |
> |
/** |
1733 |
> |
* timed invokeAny(c) returns result of some task |
1734 |
> |
*/ |
1735 |
> |
public void testTimedInvokeAny5() throws Exception { |
1736 |
> |
ExecutorService e = |
1737 |
> |
new ThreadPoolExecutor(2, 2, |
1738 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1739 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1740 |
> |
try { |
1741 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1742 |
> |
l.add(new StringTask()); |
1743 |
> |
l.add(new StringTask()); |
1744 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1745 |
> |
assertSame(TEST_STRING, result); |
1746 |
> |
} finally { |
1747 |
> |
joinPool(e); |
1748 |
|
} |
505 |
– |
catch (NullPointerException n6){} |
1749 |
|
} |
1750 |
|
|
1751 |
< |
//---- Tests if handler is set to null |
1752 |
< |
public void testNullPointerException7() { |
1753 |
< |
try{ |
1754 |
< |
RejectedExecutionHandler r = null; |
1755 |
< |
new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new testThread(),r); |
1756 |
< |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
1751 |
> |
/** |
1752 |
> |
* timed invokeAll(null) throws NPE |
1753 |
> |
*/ |
1754 |
> |
public void testTimedInvokeAll1() throws Exception { |
1755 |
> |
ExecutorService e = |
1756 |
> |
new ThreadPoolExecutor(2, 2, |
1757 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1758 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1759 |
> |
try { |
1760 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1761 |
> |
shouldThrow(); |
1762 |
> |
} catch (NullPointerException success) { |
1763 |
> |
} finally { |
1764 |
> |
joinPool(e); |
1765 |
|
} |
515 |
– |
catch (NullPointerException n7){} |
1766 |
|
} |
1767 |
|
|
1768 |
< |
//---- Tests if ThradFactory is set top null |
1769 |
< |
public void testNullPointerException8() { |
1770 |
< |
try{ |
1771 |
< |
ThreadFactory f = null; |
1772 |
< |
new ThreadPoolExecutor(1,2,100L,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new testReject()); |
1773 |
< |
fail("ThreadPoolExecutor constructor should throw a NullPointerException"); |
1768 |
> |
/** |
1769 |
> |
* timed invokeAll(,,null) throws NPE |
1770 |
> |
*/ |
1771 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1772 |
> |
ExecutorService e = |
1773 |
> |
new ThreadPoolExecutor(2, 2, |
1774 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1775 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1776 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1777 |
> |
l.add(new StringTask()); |
1778 |
> |
try { |
1779 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1780 |
> |
shouldThrow(); |
1781 |
> |
} catch (NullPointerException success) { |
1782 |
> |
} finally { |
1783 |
> |
joinPool(e); |
1784 |
|
} |
525 |
– |
catch (NullPointerException n8){} |
1785 |
|
} |
527 |
– |
|
1786 |
|
|
1787 |
|
/** |
1788 |
< |
* Test to verify execute will throw RejectedExcutionException |
531 |
< |
* ThreadPoolExecutor will throw one when more runnables are |
532 |
< |
* executed then will fit in the Queue. |
1788 |
> |
* timed invokeAll(empty collection) returns empty collection |
1789 |
|
*/ |
1790 |
< |
public void testRejectedExecutedException(){ |
1791 |
< |
ThreadPoolExecutor tpe = null; |
1792 |
< |
try{ |
1793 |
< |
tpe = new ThreadPoolExecutor(1,1,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1794 |
< |
}catch(Exception e){} |
1795 |
< |
tpe.shutdown(); |
1796 |
< |
try{ |
1797 |
< |
tpe.execute(new Runnable(){ |
1798 |
< |
public void run(){ |
1799 |
< |
try{ |
1800 |
< |
Thread.sleep(1000); |
545 |
< |
}catch(InterruptedException e){} |
546 |
< |
} |
547 |
< |
}); |
548 |
< |
fail("ThreadPoolExecutor - void execute(Runnable) should throw RejectedExecutionException"); |
549 |
< |
}catch(RejectedExecutionException sucess){} |
550 |
< |
|
551 |
< |
|
1790 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
1791 |
> |
ExecutorService e = |
1792 |
> |
new ThreadPoolExecutor(2, 2, |
1793 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1794 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1795 |
> |
try { |
1796 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1797 |
> |
assertTrue(r.isEmpty()); |
1798 |
> |
} finally { |
1799 |
> |
joinPool(e); |
1800 |
> |
} |
1801 |
|
} |
1802 |
< |
|
1802 |
> |
|
1803 |
|
/** |
1804 |
< |
* Test to verify setCorePoolSize will throw IllegalArgumentException |
556 |
< |
* when given a negative |
1804 |
> |
* timed invokeAll(c) throws NPE if c has null elements |
1805 |
|
*/ |
1806 |
< |
public void testIllegalArgumentException1(){ |
1807 |
< |
ThreadPoolExecutor tpe = null; |
1808 |
< |
try{ |
1809 |
< |
tpe = new ThreadPoolExecutor(1,2,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1810 |
< |
}catch(Exception e){} |
1811 |
< |
try{ |
1812 |
< |
tpe.setCorePoolSize(-1); |
1813 |
< |
fail("ThreadPoolExecutor - void setCorePoolSize(int) should throw IllegalArgumentException"); |
1814 |
< |
}catch(IllegalArgumentException sucess){ |
1806 |
> |
public void testTimedInvokeAll3() throws Exception { |
1807 |
> |
ExecutorService e = |
1808 |
> |
new ThreadPoolExecutor(2, 2, |
1809 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1810 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1811 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1812 |
> |
l.add(new StringTask()); |
1813 |
> |
l.add(null); |
1814 |
> |
try { |
1815 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1816 |
> |
shouldThrow(); |
1817 |
> |
} catch (NullPointerException success) { |
1818 |
|
} finally { |
1819 |
< |
tpe.shutdown(); |
1819 |
> |
joinPool(e); |
1820 |
|
} |
1821 |
< |
} |
1821 |
> |
} |
1822 |
|
|
572 |
– |
|
1823 |
|
/** |
1824 |
< |
* Test to verify setMaximumPoolSize(int) will throw IllegalArgumentException |
1825 |
< |
* if given a value less the it's actual core pool size |
1826 |
< |
*/ |
1827 |
< |
public void testIllegalArgumentException2(){ |
1828 |
< |
ThreadPoolExecutor tpe = null; |
1829 |
< |
try{ |
1830 |
< |
tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1831 |
< |
}catch(Exception e){} |
1832 |
< |
try{ |
1833 |
< |
tpe.setMaximumPoolSize(1); |
1834 |
< |
fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException"); |
1835 |
< |
}catch(IllegalArgumentException sucess){ |
1824 |
> |
* get of element of invokeAll(c) throws exception on failed task |
1825 |
> |
*/ |
1826 |
> |
public void testTimedInvokeAll4() throws Exception { |
1827 |
> |
ExecutorService e = |
1828 |
> |
new ThreadPoolExecutor(2, 2, |
1829 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1830 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1831 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1832 |
> |
l.add(new NPETask()); |
1833 |
> |
List<Future<String>> futures = |
1834 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1835 |
> |
assertEquals(1, futures.size()); |
1836 |
> |
try { |
1837 |
> |
futures.get(0).get(); |
1838 |
> |
shouldThrow(); |
1839 |
> |
} catch (ExecutionException success) { |
1840 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1841 |
|
} finally { |
1842 |
< |
tpe.shutdown(); |
1842 |
> |
joinPool(e); |
1843 |
|
} |
1844 |
|
} |
1845 |
< |
|
1845 |
> |
|
1846 |
|
/** |
1847 |
< |
* Test to verify that setMaximumPoolSize will throw IllegalArgumentException |
593 |
< |
* if given a negative number |
1847 |
> |
* timed invokeAll(c) returns results of all completed tasks |
1848 |
|
*/ |
1849 |
< |
public void testIllegalArgumentException2SP(){ |
1850 |
< |
ThreadPoolExecutor tpe = null; |
1851 |
< |
try{ |
1852 |
< |
tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1853 |
< |
}catch(Exception e){} |
1854 |
< |
try{ |
1855 |
< |
tpe.setMaximumPoolSize(-1); |
1856 |
< |
fail("ThreadPoolExecutor - void setMaximumPoolSize(int) should throw IllegalArgumentException"); |
1857 |
< |
}catch(IllegalArgumentException sucess){ |
1849 |
> |
public void testTimedInvokeAll5() throws Exception { |
1850 |
> |
ExecutorService e = |
1851 |
> |
new ThreadPoolExecutor(2, 2, |
1852 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1853 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1854 |
> |
try { |
1855 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1856 |
> |
l.add(new StringTask()); |
1857 |
> |
l.add(new StringTask()); |
1858 |
> |
List<Future<String>> futures = |
1859 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1860 |
> |
assertEquals(2, futures.size()); |
1861 |
> |
for (Future<String> future : futures) |
1862 |
> |
assertSame(TEST_STRING, future.get()); |
1863 |
|
} finally { |
1864 |
< |
tpe.shutdown(); |
1864 |
> |
joinPool(e); |
1865 |
|
} |
1866 |
|
} |
608 |
– |
|
1867 |
|
|
1868 |
|
/** |
1869 |
< |
* Test to verify setKeepAliveTime will throw IllegalArgumentException |
612 |
< |
* when given a negative value |
1869 |
> |
* timed invokeAll(c) cancels tasks not completed by timeout |
1870 |
|
*/ |
1871 |
< |
public void testIllegalArgumentException3(){ |
1872 |
< |
ThreadPoolExecutor tpe = null; |
1873 |
< |
try{ |
1874 |
< |
tpe = new ThreadPoolExecutor(2,3,100,TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1875 |
< |
}catch(Exception e){} |
1876 |
< |
|
1877 |
< |
try{ |
1878 |
< |
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
1879 |
< |
fail("ThreadPoolExecutor - void setKeepAliveTime(long, TimeUnit) should throw IllegalArgumentException"); |
1880 |
< |
}catch(IllegalArgumentException sucess){ |
1871 |
> |
public void testTimedInvokeAll6() throws Exception { |
1872 |
> |
ExecutorService e = |
1873 |
> |
new ThreadPoolExecutor(2, 2, |
1874 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1875 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1876 |
> |
try { |
1877 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1878 |
> |
l.add(new StringTask()); |
1879 |
> |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1880 |
> |
l.add(new StringTask()); |
1881 |
> |
List<Future<String>> futures = |
1882 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1883 |
> |
assertEquals(l.size(), futures.size()); |
1884 |
> |
for (Future future : futures) |
1885 |
> |
assertTrue(future.isDone()); |
1886 |
> |
assertFalse(futures.get(0).isCancelled()); |
1887 |
> |
assertTrue(futures.get(1).isCancelled()); |
1888 |
|
} finally { |
1889 |
< |
tpe.shutdown(); |
1889 |
> |
joinPool(e); |
1890 |
|
} |
1891 |
|
} |
1892 |
< |
|
1893 |
< |
|
1894 |
< |
|
1892 |
> |
|
1893 |
> |
/** |
1894 |
> |
* Execution continues if there is at least one thread even if |
1895 |
> |
* thread factory fails to create more |
1896 |
> |
*/ |
1897 |
> |
public void testFailingThreadFactory() throws InterruptedException { |
1898 |
> |
final ExecutorService e = |
1899 |
> |
new ThreadPoolExecutor(100, 100, |
1900 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1901 |
> |
new LinkedBlockingQueue<Runnable>(), |
1902 |
> |
new FailingThreadFactory()); |
1903 |
> |
try { |
1904 |
> |
final int TASKS = 100; |
1905 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1906 |
> |
for (int k = 0; k < TASKS; ++k) |
1907 |
> |
e.execute(new CheckedRunnable() { |
1908 |
> |
public void realRun() { |
1909 |
> |
done.countDown(); |
1910 |
> |
}}); |
1911 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1912 |
> |
} finally { |
1913 |
> |
joinPool(e); |
1914 |
> |
} |
1915 |
> |
} |
1916 |
> |
|
1917 |
> |
/** |
1918 |
> |
* allowsCoreThreadTimeOut is by default false. |
1919 |
> |
*/ |
1920 |
> |
public void testAllowsCoreThreadTimeOut() { |
1921 |
> |
final ThreadPoolExecutor p = |
1922 |
> |
new ThreadPoolExecutor(2, 2, |
1923 |
> |
1000, MILLISECONDS, |
1924 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1925 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1926 |
> |
joinPool(p); |
1927 |
> |
} |
1928 |
> |
|
1929 |
> |
/** |
1930 |
> |
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1931 |
> |
*/ |
1932 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1933 |
> |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1934 |
> |
final ThreadPoolExecutor p = |
1935 |
> |
new ThreadPoolExecutor(2, 10, |
1936 |
> |
coreThreadTimeOut, MILLISECONDS, |
1937 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1938 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1939 |
> |
try { |
1940 |
> |
p.allowCoreThreadTimeOut(true); |
1941 |
> |
p.execute(new CheckedRunnable() { |
1942 |
> |
public void realRun() { |
1943 |
> |
threadStarted.countDown(); |
1944 |
> |
assertEquals(1, p.getPoolSize()); |
1945 |
> |
}}); |
1946 |
> |
await(threadStarted); |
1947 |
> |
delay(coreThreadTimeOut); |
1948 |
> |
long startTime = System.nanoTime(); |
1949 |
> |
while (p.getPoolSize() > 0 |
1950 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1951 |
> |
Thread.yield(); |
1952 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1953 |
> |
assertEquals(0, p.getPoolSize()); |
1954 |
> |
} finally { |
1955 |
> |
joinPool(p); |
1956 |
> |
} |
1957 |
> |
} |
1958 |
> |
|
1959 |
> |
/** |
1960 |
> |
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1961 |
> |
*/ |
1962 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1963 |
> |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1964 |
> |
final ThreadPoolExecutor p = |
1965 |
> |
new ThreadPoolExecutor(2, 10, |
1966 |
> |
coreThreadTimeOut, MILLISECONDS, |
1967 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1968 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1969 |
> |
try { |
1970 |
> |
p.allowCoreThreadTimeOut(false); |
1971 |
> |
p.execute(new CheckedRunnable() { |
1972 |
> |
public void realRun() throws InterruptedException { |
1973 |
> |
threadStarted.countDown(); |
1974 |
> |
assertTrue(p.getPoolSize() >= 1); |
1975 |
> |
}}); |
1976 |
> |
delay(2 * coreThreadTimeOut); |
1977 |
> |
assertTrue(p.getPoolSize() >= 1); |
1978 |
> |
} finally { |
1979 |
> |
joinPool(p); |
1980 |
> |
} |
1981 |
> |
} |
1982 |
> |
|
1983 |
> |
/** |
1984 |
> |
* execute allows the same task to be submitted multiple times, even |
1985 |
> |
* if rejected |
1986 |
> |
*/ |
1987 |
> |
public void testRejectedRecycledTask() throws InterruptedException { |
1988 |
> |
final int nTasks = 1000; |
1989 |
> |
final CountDownLatch done = new CountDownLatch(nTasks); |
1990 |
> |
final Runnable recycledTask = new Runnable() { |
1991 |
> |
public void run() { |
1992 |
> |
done.countDown(); |
1993 |
> |
}}; |
1994 |
> |
final ThreadPoolExecutor p = |
1995 |
> |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
1996 |
> |
new ArrayBlockingQueue(30)); |
1997 |
> |
try { |
1998 |
> |
for (int i = 0; i < nTasks; ++i) { |
1999 |
> |
for (;;) { |
2000 |
> |
try { |
2001 |
> |
p.execute(recycledTask); |
2002 |
> |
break; |
2003 |
> |
} |
2004 |
> |
catch (RejectedExecutionException ignore) {} |
2005 |
> |
} |
2006 |
> |
} |
2007 |
> |
// enough time to run all tasks |
2008 |
> |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2009 |
> |
} finally { |
2010 |
> |
joinPool(p); |
2011 |
> |
} |
2012 |
> |
} |
2013 |
> |
|
2014 |
|
} |