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