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