1 |
|
/* |
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 |
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 java.util.concurrent.locks.*; |
9 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
< |
import junit.framework.*; |
13 |
< |
import java.util.*; |
12 |
> |
import java.util.ArrayList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.Callable; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.ExecutionException; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.Future; |
22 |
> |
import java.util.concurrent.FutureTask; |
23 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
24 |
> |
import java.util.concurrent.RejectedExecutionException; |
25 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
26 |
> |
import java.util.concurrent.RunnableFuture; |
27 |
> |
import java.util.concurrent.SynchronousQueue; |
28 |
> |
import java.util.concurrent.ThreadFactory; |
29 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
30 |
> |
import java.util.concurrent.TimeoutException; |
31 |
> |
import java.util.concurrent.TimeUnit; |
32 |
> |
import java.util.concurrent.locks.Condition; |
33 |
> |
import java.util.concurrent.locks.ReentrantLock; |
34 |
> |
|
35 |
> |
import junit.framework.Test; |
36 |
> |
import junit.framework.TestSuite; |
37 |
|
|
38 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
39 |
|
public static void main(String[] args) { |
40 |
< |
junit.textui.TestRunner.run (suite()); |
40 |
> |
main(suite(), args); |
41 |
|
} |
42 |
|
public static Test suite() { |
43 |
< |
return new TestSuite(ThreadPoolExecutorTest.class); |
43 |
> |
return new TestSuite(ThreadPoolExecutorSubclassTest.class); |
44 |
|
} |
45 |
|
|
46 |
|
static class CustomTask<V> implements RunnableFuture<V> { |
52 |
|
V result; |
53 |
|
Thread thread; |
54 |
|
Exception exception; |
55 |
< |
CustomTask(Callable<V> c) { callable = c; } |
56 |
< |
CustomTask(final Runnable r, final V res) { callable = new Callable<V>() { |
57 |
< |
public V call() throws Exception { r.run(); return res; }}; |
55 |
> |
CustomTask(Callable<V> c) { |
56 |
> |
if (c == null) throw new NullPointerException(); |
57 |
> |
callable = c; |
58 |
> |
} |
59 |
> |
CustomTask(final Runnable r, final V res) { |
60 |
> |
if (r == null) throw new NullPointerException(); |
61 |
> |
callable = new Callable<V>() { |
62 |
> |
public V call() throws Exception { r.run(); return res; }}; |
63 |
|
} |
64 |
|
public boolean isDone() { |
65 |
|
lock.lock(); try { return done; } finally { lock.unlock() ; } |
82 |
|
finally { lock.unlock() ; } |
83 |
|
} |
84 |
|
public void run() { |
57 |
– |
boolean runme; |
85 |
|
lock.lock(); |
86 |
|
try { |
87 |
< |
runme = !done; |
88 |
< |
if (!runme) |
89 |
< |
thread = Thread.currentThread(); |
87 |
> |
if (done) |
88 |
> |
return; |
89 |
> |
thread = Thread.currentThread(); |
90 |
|
} |
91 |
|
finally { lock.unlock() ; } |
65 |
– |
if (!runme) return; |
92 |
|
V v = null; |
93 |
|
Exception e = null; |
94 |
|
try { |
137 |
|
} |
138 |
|
} |
139 |
|
|
114 |
– |
|
140 |
|
static class CustomTPE extends ThreadPoolExecutor { |
141 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
142 |
|
return new CustomTask<V>(c); |
183 |
|
workQueue, threadFactory, handler); |
184 |
|
} |
185 |
|
|
186 |
< |
volatile boolean beforeCalled = false; |
187 |
< |
volatile boolean afterCalled = false; |
188 |
< |
volatile boolean terminatedCalled = false; |
186 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
187 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
188 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
189 |
> |
|
190 |
|
public CustomTPE() { |
191 |
< |
super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); |
191 |
> |
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
192 |
|
} |
193 |
|
protected void beforeExecute(Thread t, Runnable r) { |
194 |
< |
beforeCalled = true; |
194 |
> |
beforeCalled.countDown(); |
195 |
|
} |
196 |
|
protected void afterExecute(Runnable r, Throwable t) { |
197 |
< |
afterCalled = true; |
197 |
> |
afterCalled.countDown(); |
198 |
|
} |
199 |
|
protected void terminated() { |
200 |
< |
terminatedCalled = true; |
200 |
> |
terminatedCalled.countDown(); |
201 |
|
} |
202 |
|
|
203 |
+ |
public boolean beforeCalled() { |
204 |
+ |
return beforeCalled.getCount() == 0; |
205 |
+ |
} |
206 |
+ |
public boolean afterCalled() { |
207 |
+ |
return afterCalled.getCount() == 0; |
208 |
+ |
} |
209 |
+ |
public boolean terminatedCalled() { |
210 |
+ |
return terminatedCalled.getCount() == 0; |
211 |
+ |
} |
212 |
|
} |
213 |
|
|
214 |
|
static class FailingThreadFactory implements ThreadFactory { |
219 |
|
} |
220 |
|
} |
221 |
|
|
187 |
– |
|
222 |
|
/** |
223 |
< |
* execute successfully executes a runnable |
223 |
> |
* execute successfully executes a runnable |
224 |
|
*/ |
225 |
< |
public void testExecute() { |
226 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
225 |
> |
public void testExecute() throws InterruptedException { |
226 |
> |
final ThreadPoolExecutor p = |
227 |
> |
new CustomTPE(1, 1, |
228 |
> |
LONG_DELAY_MS, MILLISECONDS, |
229 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
230 |
> |
final CountDownLatch done = new CountDownLatch(1); |
231 |
> |
final Runnable task = new CheckedRunnable() { |
232 |
> |
public void realRun() { |
233 |
> |
done.countDown(); |
234 |
> |
}}; |
235 |
|
try { |
236 |
< |
p1.execute(new Runnable() { |
237 |
< |
public void run() { |
238 |
< |
try { |
239 |
< |
Thread.sleep(SHORT_DELAY_MS); |
198 |
< |
} catch (InterruptedException e) { |
199 |
< |
threadUnexpectedException(); |
200 |
< |
} |
201 |
< |
} |
202 |
< |
}); |
203 |
< |
Thread.sleep(SMALL_DELAY_MS); |
204 |
< |
} catch (InterruptedException e) { |
205 |
< |
unexpectedException(); |
236 |
> |
p.execute(task); |
237 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
238 |
> |
} finally { |
239 |
> |
joinPool(p); |
240 |
|
} |
207 |
– |
joinPool(p1); |
241 |
|
} |
242 |
|
|
243 |
|
/** |
244 |
< |
* getActiveCount increases but doesn't overestimate, when a |
245 |
< |
* thread becomes active |
244 |
> |
* getActiveCount increases but doesn't overestimate, when a |
245 |
> |
* thread becomes active |
246 |
|
*/ |
247 |
< |
public void testGetActiveCount() { |
248 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
249 |
< |
assertEquals(0, p2.getActiveCount()); |
250 |
< |
p2.execute(new MediumRunnable()); |
251 |
< |
try { |
252 |
< |
Thread.sleep(SHORT_DELAY_MS); |
253 |
< |
} catch (Exception e) { |
254 |
< |
unexpectedException(); |
247 |
> |
public void testGetActiveCount() throws InterruptedException { |
248 |
> |
final ThreadPoolExecutor p = |
249 |
> |
new CustomTPE(2, 2, |
250 |
> |
LONG_DELAY_MS, MILLISECONDS, |
251 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
252 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
253 |
> |
final CountDownLatch done = new CountDownLatch(1); |
254 |
> |
try { |
255 |
> |
assertEquals(0, p.getActiveCount()); |
256 |
> |
p.execute(new CheckedRunnable() { |
257 |
> |
public void realRun() throws InterruptedException { |
258 |
> |
threadStarted.countDown(); |
259 |
> |
assertEquals(1, p.getActiveCount()); |
260 |
> |
done.await(); |
261 |
> |
}}); |
262 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
263 |
> |
assertEquals(1, p.getActiveCount()); |
264 |
> |
} finally { |
265 |
> |
done.countDown(); |
266 |
> |
joinPool(p); |
267 |
|
} |
223 |
– |
assertEquals(1, p2.getActiveCount()); |
224 |
– |
joinPool(p2); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
< |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
271 |
> |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
272 |
|
*/ |
273 |
|
public void testPrestartCoreThread() { |
274 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
275 |
< |
assertEquals(0, p2.getPoolSize()); |
276 |
< |
assertTrue(p2.prestartCoreThread()); |
277 |
< |
assertEquals(1, p2.getPoolSize()); |
278 |
< |
assertTrue(p2.prestartCoreThread()); |
279 |
< |
assertEquals(2, p2.getPoolSize()); |
280 |
< |
assertFalse(p2.prestartCoreThread()); |
281 |
< |
assertEquals(2, p2.getPoolSize()); |
282 |
< |
joinPool(p2); |
274 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
275 |
> |
assertEquals(0, p.getPoolSize()); |
276 |
> |
assertTrue(p.prestartCoreThread()); |
277 |
> |
assertEquals(1, p.getPoolSize()); |
278 |
> |
assertTrue(p.prestartCoreThread()); |
279 |
> |
assertEquals(2, p.getPoolSize()); |
280 |
> |
assertFalse(p.prestartCoreThread()); |
281 |
> |
assertEquals(2, p.getPoolSize()); |
282 |
> |
joinPool(p); |
283 |
|
} |
284 |
|
|
285 |
|
/** |
286 |
< |
* prestartAllCoreThreads starts all corePoolSize threads |
286 |
> |
* prestartAllCoreThreads starts all corePoolSize threads |
287 |
|
*/ |
288 |
|
public void testPrestartAllCoreThreads() { |
289 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
290 |
< |
assertEquals(0, p2.getPoolSize()); |
291 |
< |
p2.prestartAllCoreThreads(); |
292 |
< |
assertEquals(2, p2.getPoolSize()); |
293 |
< |
p2.prestartAllCoreThreads(); |
294 |
< |
assertEquals(2, p2.getPoolSize()); |
295 |
< |
joinPool(p2); |
289 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
290 |
> |
assertEquals(0, p.getPoolSize()); |
291 |
> |
p.prestartAllCoreThreads(); |
292 |
> |
assertEquals(2, p.getPoolSize()); |
293 |
> |
p.prestartAllCoreThreads(); |
294 |
> |
assertEquals(2, p.getPoolSize()); |
295 |
> |
joinPool(p); |
296 |
|
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
300 |
< |
* when tasks complete |
299 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
300 |
> |
* when tasks complete |
301 |
|
*/ |
302 |
< |
public void testGetCompletedTaskCount() { |
303 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
304 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
305 |
< |
p2.execute(new ShortRunnable()); |
306 |
< |
try { |
307 |
< |
Thread.sleep(SMALL_DELAY_MS); |
308 |
< |
} catch (Exception e) { |
309 |
< |
unexpectedException(); |
302 |
> |
public void testGetCompletedTaskCount() throws InterruptedException { |
303 |
> |
final ThreadPoolExecutor p = |
304 |
> |
new CustomTPE(2, 2, |
305 |
> |
LONG_DELAY_MS, MILLISECONDS, |
306 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
307 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
308 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
309 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
310 |
> |
try { |
311 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
312 |
> |
p.execute(new CheckedRunnable() { |
313 |
> |
public void realRun() throws InterruptedException { |
314 |
> |
threadStarted.countDown(); |
315 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
316 |
> |
threadProceed.await(); |
317 |
> |
threadDone.countDown(); |
318 |
> |
}}); |
319 |
> |
await(threadStarted); |
320 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
321 |
> |
threadProceed.countDown(); |
322 |
> |
threadDone.await(); |
323 |
> |
long startTime = System.nanoTime(); |
324 |
> |
while (p.getCompletedTaskCount() != 1) { |
325 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
326 |
> |
fail("timed out"); |
327 |
> |
Thread.yield(); |
328 |
> |
} |
329 |
> |
} finally { |
330 |
> |
joinPool(p); |
331 |
|
} |
268 |
– |
assertEquals(1, p2.getCompletedTaskCount()); |
269 |
– |
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
270 |
– |
joinPool(p2); |
332 |
|
} |
333 |
|
|
334 |
|
/** |
335 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
335 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
336 |
|
*/ |
337 |
|
public void testGetCorePoolSize() { |
338 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
339 |
< |
assertEquals(1, p1.getCorePoolSize()); |
340 |
< |
joinPool(p1); |
338 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
339 |
> |
assertEquals(1, p.getCorePoolSize()); |
340 |
> |
joinPool(p); |
341 |
|
} |
342 |
|
|
343 |
|
/** |
344 |
< |
* getKeepAliveTime returns value given in constructor if not otherwise set |
344 |
> |
* getKeepAliveTime returns value given in constructor if not otherwise set |
345 |
|
*/ |
346 |
|
public void testGetKeepAliveTime() { |
347 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
348 |
< |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
349 |
< |
joinPool(p2); |
347 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
348 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
349 |
> |
joinPool(p); |
350 |
|
} |
351 |
|
|
291 |
– |
|
352 |
|
/** |
353 |
|
* getThreadFactory returns factory in constructor if not set |
354 |
|
*/ |
355 |
|
public void testGetThreadFactory() { |
356 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
357 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
357 |
> |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
358 |
|
assertSame(tf, p.getThreadFactory()); |
359 |
|
joinPool(p); |
360 |
|
} |
363 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
364 |
|
*/ |
365 |
|
public void testSetThreadFactory() { |
366 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
366 |
> |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
367 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
368 |
|
p.setThreadFactory(tf); |
369 |
|
assertSame(tf, p.getThreadFactory()); |
370 |
|
joinPool(p); |
371 |
|
} |
372 |
|
|
313 |
– |
|
373 |
|
/** |
374 |
|
* setThreadFactory(null) throws NPE |
375 |
|
*/ |
376 |
|
public void testSetThreadFactoryNull() { |
377 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
377 |
> |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
378 |
|
try { |
379 |
|
p.setThreadFactory(null); |
380 |
|
shouldThrow(); |
389 |
|
*/ |
390 |
|
public void testGetRejectedExecutionHandler() { |
391 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
392 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
392 |
> |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
393 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
394 |
|
joinPool(p); |
395 |
|
} |
399 |
|
* getRejectedExecutionHandler |
400 |
|
*/ |
401 |
|
public void testSetRejectedExecutionHandler() { |
402 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
402 |
> |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
403 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
404 |
|
p.setRejectedExecutionHandler(h); |
405 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
406 |
|
joinPool(p); |
407 |
|
} |
408 |
|
|
350 |
– |
|
409 |
|
/** |
410 |
|
* setRejectedExecutionHandler(null) throws NPE |
411 |
|
*/ |
412 |
|
public void testSetRejectedExecutionHandlerNull() { |
413 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
413 |
> |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
414 |
|
try { |
415 |
|
p.setRejectedExecutionHandler(null); |
416 |
|
shouldThrow(); |
420 |
|
} |
421 |
|
} |
422 |
|
|
365 |
– |
|
423 |
|
/** |
424 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
425 |
< |
* multiple threads active |
424 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
425 |
> |
* multiple threads active |
426 |
|
*/ |
427 |
< |
public void testGetLargestPoolSize() { |
428 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
429 |
< |
try { |
430 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
431 |
< |
p2.execute(new MediumRunnable()); |
432 |
< |
p2.execute(new MediumRunnable()); |
433 |
< |
Thread.sleep(SHORT_DELAY_MS); |
434 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
435 |
< |
} catch (Exception e) { |
436 |
< |
unexpectedException(); |
427 |
> |
public void testGetLargestPoolSize() throws InterruptedException { |
428 |
> |
final int THREADS = 3; |
429 |
> |
final ThreadPoolExecutor p = |
430 |
> |
new CustomTPE(THREADS, THREADS, |
431 |
> |
LONG_DELAY_MS, MILLISECONDS, |
432 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
433 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
434 |
> |
final CountDownLatch done = new CountDownLatch(1); |
435 |
> |
try { |
436 |
> |
assertEquals(0, p.getLargestPoolSize()); |
437 |
> |
for (int i = 0; i < THREADS; i++) |
438 |
> |
p.execute(new CheckedRunnable() { |
439 |
> |
public void realRun() throws InterruptedException { |
440 |
> |
threadsStarted.countDown(); |
441 |
> |
done.await(); |
442 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
443 |
> |
}}); |
444 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
445 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
446 |
> |
} finally { |
447 |
> |
done.countDown(); |
448 |
> |
joinPool(p); |
449 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
450 |
|
} |
381 |
– |
joinPool(p2); |
451 |
|
} |
452 |
|
|
453 |
|
/** |
454 |
< |
* getMaximumPoolSize returns value given in constructor if not |
455 |
< |
* otherwise set |
454 |
> |
* getMaximumPoolSize returns value given in constructor if not |
455 |
> |
* otherwise set |
456 |
|
*/ |
457 |
|
public void testGetMaximumPoolSize() { |
458 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
459 |
< |
assertEquals(2, p2.getMaximumPoolSize()); |
460 |
< |
joinPool(p2); |
458 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
459 |
> |
assertEquals(2, p.getMaximumPoolSize()); |
460 |
> |
joinPool(p); |
461 |
|
} |
462 |
|
|
463 |
|
/** |
464 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
465 |
< |
* become active |
464 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
465 |
> |
* become active |
466 |
|
*/ |
467 |
< |
public void testGetPoolSize() { |
468 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
469 |
< |
assertEquals(0, p1.getPoolSize()); |
470 |
< |
p1.execute(new MediumRunnable()); |
471 |
< |
assertEquals(1, p1.getPoolSize()); |
472 |
< |
joinPool(p1); |
467 |
> |
public void testGetPoolSize() throws InterruptedException { |
468 |
> |
final ThreadPoolExecutor p = |
469 |
> |
new CustomTPE(1, 1, |
470 |
> |
LONG_DELAY_MS, MILLISECONDS, |
471 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
472 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
473 |
> |
final CountDownLatch done = new CountDownLatch(1); |
474 |
> |
try { |
475 |
> |
assertEquals(0, p.getPoolSize()); |
476 |
> |
p.execute(new CheckedRunnable() { |
477 |
> |
public void realRun() throws InterruptedException { |
478 |
> |
threadStarted.countDown(); |
479 |
> |
assertEquals(1, p.getPoolSize()); |
480 |
> |
done.await(); |
481 |
> |
}}); |
482 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
483 |
> |
assertEquals(1, p.getPoolSize()); |
484 |
> |
} finally { |
485 |
> |
done.countDown(); |
486 |
> |
joinPool(p); |
487 |
> |
} |
488 |
|
} |
489 |
|
|
490 |
|
/** |
491 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
491 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
492 |
|
*/ |
493 |
< |
public void testGetTaskCount() { |
494 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
495 |
< |
try { |
496 |
< |
assertEquals(0, p1.getTaskCount()); |
497 |
< |
p1.execute(new MediumRunnable()); |
498 |
< |
Thread.sleep(SHORT_DELAY_MS); |
499 |
< |
assertEquals(1, p1.getTaskCount()); |
500 |
< |
} catch (Exception e) { |
501 |
< |
unexpectedException(); |
493 |
> |
public void testGetTaskCount() throws InterruptedException { |
494 |
> |
final ThreadPoolExecutor p = |
495 |
> |
new CustomTPE(1, 1, |
496 |
> |
LONG_DELAY_MS, MILLISECONDS, |
497 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
498 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
499 |
> |
final CountDownLatch done = new CountDownLatch(1); |
500 |
> |
try { |
501 |
> |
assertEquals(0, p.getTaskCount()); |
502 |
> |
p.execute(new CheckedRunnable() { |
503 |
> |
public void realRun() throws InterruptedException { |
504 |
> |
threadStarted.countDown(); |
505 |
> |
assertEquals(1, p.getTaskCount()); |
506 |
> |
done.await(); |
507 |
> |
}}); |
508 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
509 |
> |
assertEquals(1, p.getTaskCount()); |
510 |
> |
} finally { |
511 |
> |
done.countDown(); |
512 |
> |
joinPool(p); |
513 |
|
} |
419 |
– |
joinPool(p1); |
514 |
|
} |
515 |
|
|
516 |
|
/** |
517 |
< |
* isShutDown is false before shutdown, true after |
517 |
> |
* isShutdown is false before shutdown, true after |
518 |
|
*/ |
519 |
|
public void testIsShutdown() { |
520 |
|
|
521 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
522 |
< |
assertFalse(p1.isShutdown()); |
523 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
524 |
< |
assertTrue(p1.isShutdown()); |
525 |
< |
joinPool(p1); |
521 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
522 |
> |
assertFalse(p.isShutdown()); |
523 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
524 |
> |
assertTrue(p.isShutdown()); |
525 |
> |
joinPool(p); |
526 |
|
} |
527 |
|
|
434 |
– |
|
528 |
|
/** |
529 |
< |
* isTerminated is false before termination, true after |
529 |
> |
* isTerminated is false before termination, true after |
530 |
|
*/ |
531 |
< |
public void testIsTerminated() { |
532 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
533 |
< |
assertFalse(p1.isTerminated()); |
534 |
< |
try { |
535 |
< |
p1.execute(new MediumRunnable()); |
536 |
< |
} finally { |
537 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
538 |
< |
} |
539 |
< |
try { |
540 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
541 |
< |
assertTrue(p1.isTerminated()); |
542 |
< |
} catch (Exception e) { |
543 |
< |
unexpectedException(); |
544 |
< |
} |
531 |
> |
public void testIsTerminated() throws InterruptedException { |
532 |
> |
final ThreadPoolExecutor p = |
533 |
> |
new CustomTPE(1, 1, |
534 |
> |
LONG_DELAY_MS, MILLISECONDS, |
535 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
536 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
537 |
> |
final CountDownLatch done = new CountDownLatch(1); |
538 |
> |
try { |
539 |
> |
assertFalse(p.isTerminating()); |
540 |
> |
p.execute(new CheckedRunnable() { |
541 |
> |
public void realRun() throws InterruptedException { |
542 |
> |
assertFalse(p.isTerminating()); |
543 |
> |
threadStarted.countDown(); |
544 |
> |
done.await(); |
545 |
> |
}}); |
546 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
547 |
> |
assertFalse(p.isTerminating()); |
548 |
> |
done.countDown(); |
549 |
> |
} finally { |
550 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
551 |
> |
} |
552 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
553 |
> |
assertTrue(p.isTerminated()); |
554 |
> |
assertFalse(p.isTerminating()); |
555 |
|
} |
556 |
|
|
557 |
|
/** |
558 |
< |
* isTerminating is not true when running or when terminated |
559 |
< |
*/ |
560 |
< |
public void testIsTerminating() { |
561 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
562 |
< |
assertFalse(p1.isTerminating()); |
563 |
< |
try { |
564 |
< |
p1.execute(new SmallRunnable()); |
565 |
< |
assertFalse(p1.isTerminating()); |
566 |
< |
} finally { |
567 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
568 |
< |
} |
569 |
< |
try { |
570 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
571 |
< |
assertTrue(p1.isTerminated()); |
572 |
< |
assertFalse(p1.isTerminating()); |
573 |
< |
} catch (Exception e) { |
574 |
< |
unexpectedException(); |
575 |
< |
} |
558 |
> |
* isTerminating is not true when running or when terminated |
559 |
> |
*/ |
560 |
> |
public void testIsTerminating() throws InterruptedException { |
561 |
> |
final ThreadPoolExecutor p = |
562 |
> |
new CustomTPE(1, 1, |
563 |
> |
LONG_DELAY_MS, MILLISECONDS, |
564 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
565 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
566 |
> |
final CountDownLatch done = new CountDownLatch(1); |
567 |
> |
try { |
568 |
> |
assertFalse(p.isTerminating()); |
569 |
> |
p.execute(new CheckedRunnable() { |
570 |
> |
public void realRun() throws InterruptedException { |
571 |
> |
assertFalse(p.isTerminating()); |
572 |
> |
threadStarted.countDown(); |
573 |
> |
done.await(); |
574 |
> |
}}); |
575 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
576 |
> |
assertFalse(p.isTerminating()); |
577 |
> |
done.countDown(); |
578 |
> |
} finally { |
579 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
580 |
> |
} |
581 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
582 |
> |
assertTrue(p.isTerminated()); |
583 |
> |
assertFalse(p.isTerminating()); |
584 |
|
} |
585 |
|
|
586 |
|
/** |
587 |
|
* getQueue returns the work queue, which contains queued tasks |
588 |
|
*/ |
589 |
< |
public void testGetQueue() { |
590 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
591 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
592 |
< |
FutureTask[] tasks = new FutureTask[5]; |
593 |
< |
for (int i = 0; i < 5; i++) { |
594 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
595 |
< |
p1.execute(tasks[i]); |
596 |
< |
} |
597 |
< |
try { |
598 |
< |
Thread.sleep(SHORT_DELAY_MS); |
599 |
< |
BlockingQueue<Runnable> wq = p1.getQueue(); |
600 |
< |
assertSame(q, wq); |
601 |
< |
assertFalse(wq.contains(tasks[0])); |
602 |
< |
assertTrue(wq.contains(tasks[4])); |
603 |
< |
for (int i = 1; i < 5; ++i) |
604 |
< |
tasks[i].cancel(true); |
605 |
< |
p1.shutdownNow(); |
606 |
< |
} catch (Exception e) { |
607 |
< |
unexpectedException(); |
589 |
> |
public void testGetQueue() throws InterruptedException { |
590 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
591 |
> |
final ThreadPoolExecutor p = |
592 |
> |
new CustomTPE(1, 1, |
593 |
> |
LONG_DELAY_MS, MILLISECONDS, |
594 |
> |
q); |
595 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
596 |
> |
final CountDownLatch done = new CountDownLatch(1); |
597 |
> |
try { |
598 |
> |
FutureTask[] tasks = new FutureTask[5]; |
599 |
> |
for (int i = 0; i < tasks.length; i++) { |
600 |
> |
Callable task = new CheckedCallable<Boolean>() { |
601 |
> |
public Boolean realCall() throws InterruptedException { |
602 |
> |
threadStarted.countDown(); |
603 |
> |
assertSame(q, p.getQueue()); |
604 |
> |
done.await(); |
605 |
> |
return Boolean.TRUE; |
606 |
> |
}}; |
607 |
> |
tasks[i] = new FutureTask(task); |
608 |
> |
p.execute(tasks[i]); |
609 |
> |
} |
610 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
611 |
> |
assertSame(q, p.getQueue()); |
612 |
> |
assertFalse(q.contains(tasks[0])); |
613 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
614 |
> |
assertEquals(tasks.length - 1, q.size()); |
615 |
|
} finally { |
616 |
< |
joinPool(p1); |
616 |
> |
done.countDown(); |
617 |
> |
joinPool(p); |
618 |
|
} |
619 |
|
} |
620 |
|
|
621 |
|
/** |
622 |
|
* remove(task) removes queued task, and fails to remove active task |
623 |
|
*/ |
624 |
< |
public void testRemove() { |
624 |
> |
public void testRemove() throws InterruptedException { |
625 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
626 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
627 |
< |
FutureTask[] tasks = new FutureTask[5]; |
628 |
< |
for (int i = 0; i < 5; i++) { |
629 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
630 |
< |
p1.execute(tasks[i]); |
631 |
< |
} |
632 |
< |
try { |
633 |
< |
Thread.sleep(SHORT_DELAY_MS); |
634 |
< |
assertFalse(p1.remove(tasks[0])); |
626 |
> |
final ThreadPoolExecutor p = |
627 |
> |
new CustomTPE(1, 1, |
628 |
> |
LONG_DELAY_MS, MILLISECONDS, |
629 |
> |
q); |
630 |
> |
Runnable[] tasks = new Runnable[6]; |
631 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
632 |
> |
final CountDownLatch done = new CountDownLatch(1); |
633 |
> |
try { |
634 |
> |
for (int i = 0; i < tasks.length; i++) { |
635 |
> |
tasks[i] = new CheckedRunnable() { |
636 |
> |
public void realRun() throws InterruptedException { |
637 |
> |
threadStarted.countDown(); |
638 |
> |
done.await(); |
639 |
> |
}}; |
640 |
> |
p.execute(tasks[i]); |
641 |
> |
} |
642 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
643 |
> |
assertFalse(p.remove(tasks[0])); |
644 |
|
assertTrue(q.contains(tasks[4])); |
645 |
|
assertTrue(q.contains(tasks[3])); |
646 |
< |
assertTrue(p1.remove(tasks[4])); |
647 |
< |
assertFalse(p1.remove(tasks[4])); |
646 |
> |
assertTrue(p.remove(tasks[4])); |
647 |
> |
assertFalse(p.remove(tasks[4])); |
648 |
|
assertFalse(q.contains(tasks[4])); |
649 |
|
assertTrue(q.contains(tasks[3])); |
650 |
< |
assertTrue(p1.remove(tasks[3])); |
650 |
> |
assertTrue(p.remove(tasks[3])); |
651 |
|
assertFalse(q.contains(tasks[3])); |
524 |
– |
} catch (Exception e) { |
525 |
– |
unexpectedException(); |
652 |
|
} finally { |
653 |
< |
joinPool(p1); |
653 |
> |
done.countDown(); |
654 |
> |
joinPool(p); |
655 |
|
} |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
< |
* purge removes cancelled tasks from the queue |
659 |
> |
* purge removes cancelled tasks from the queue |
660 |
|
*/ |
661 |
< |
public void testPurge() { |
662 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
661 |
> |
public void testPurge() throws InterruptedException { |
662 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
663 |
> |
final CountDownLatch done = new CountDownLatch(1); |
664 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
665 |
> |
final ThreadPoolExecutor p = |
666 |
> |
new CustomTPE(1, 1, |
667 |
> |
LONG_DELAY_MS, MILLISECONDS, |
668 |
> |
q); |
669 |
|
FutureTask[] tasks = new FutureTask[5]; |
670 |
< |
for (int i = 0; i < 5; i++) { |
671 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
672 |
< |
p1.execute(tasks[i]); |
670 |
> |
try { |
671 |
> |
for (int i = 0; i < tasks.length; i++) { |
672 |
> |
Callable task = new CheckedCallable<Boolean>() { |
673 |
> |
public Boolean realCall() throws InterruptedException { |
674 |
> |
threadStarted.countDown(); |
675 |
> |
done.await(); |
676 |
> |
return Boolean.TRUE; |
677 |
> |
}}; |
678 |
> |
tasks[i] = new FutureTask(task); |
679 |
> |
p.execute(tasks[i]); |
680 |
> |
} |
681 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
682 |
> |
assertEquals(tasks.length, p.getTaskCount()); |
683 |
> |
assertEquals(tasks.length - 1, q.size()); |
684 |
> |
assertEquals(1L, p.getActiveCount()); |
685 |
> |
assertEquals(0L, p.getCompletedTaskCount()); |
686 |
> |
tasks[4].cancel(true); |
687 |
> |
tasks[3].cancel(false); |
688 |
> |
p.purge(); |
689 |
> |
assertEquals(tasks.length - 3, q.size()); |
690 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
691 |
> |
p.purge(); // Nothing to do |
692 |
> |
assertEquals(tasks.length - 3, q.size()); |
693 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
694 |
> |
} finally { |
695 |
> |
done.countDown(); |
696 |
> |
joinPool(p); |
697 |
|
} |
541 |
– |
tasks[4].cancel(true); |
542 |
– |
tasks[3].cancel(true); |
543 |
– |
p1.purge(); |
544 |
– |
long count = p1.getTaskCount(); |
545 |
– |
assertTrue(count >= 2 && count < 5); |
546 |
– |
joinPool(p1); |
698 |
|
} |
699 |
|
|
700 |
|
/** |
701 |
< |
* shutDownNow returns a list containing tasks that were not run |
701 |
> |
* shutdownNow returns a list containing tasks that were not run |
702 |
|
*/ |
703 |
< |
public void testShutDownNow() { |
704 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
703 |
> |
public void testShutdownNow() { |
704 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
705 |
|
List l; |
706 |
|
try { |
707 |
|
for (int i = 0; i < 5; i++) |
708 |
< |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
708 |
> |
p.execute(new MediumPossiblyInterruptedRunnable()); |
709 |
|
} |
710 |
|
finally { |
711 |
|
try { |
712 |
< |
l = p1.shutdownNow(); |
712 |
> |
l = p.shutdownNow(); |
713 |
|
} catch (SecurityException ok) { return; } |
563 |
– |
|
714 |
|
} |
715 |
< |
assertTrue(p1.isShutdown()); |
716 |
< |
assertTrue(l.size() <= 4); |
715 |
> |
assertTrue(p.isShutdown()); |
716 |
> |
assertTrue(l.size() <= 4); |
717 |
|
} |
718 |
|
|
719 |
|
// Exception Tests |
720 |
|
|
571 |
– |
|
721 |
|
/** |
722 |
|
* Constructor throws if corePoolSize argument is less than zero |
723 |
|
*/ |
724 |
|
public void testConstructor1() { |
725 |
|
try { |
726 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
726 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
727 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
728 |
|
shouldThrow(); |
729 |
< |
} |
580 |
< |
catch (IllegalArgumentException success) {} |
729 |
> |
} catch (IllegalArgumentException success) {} |
730 |
|
} |
731 |
|
|
732 |
|
/** |
734 |
|
*/ |
735 |
|
public void testConstructor2() { |
736 |
|
try { |
737 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
737 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
738 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
739 |
|
shouldThrow(); |
740 |
< |
} |
591 |
< |
catch (IllegalArgumentException success) {} |
740 |
> |
} catch (IllegalArgumentException success) {} |
741 |
|
} |
742 |
|
|
743 |
|
/** |
745 |
|
*/ |
746 |
|
public void testConstructor3() { |
747 |
|
try { |
748 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
748 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
749 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
750 |
|
shouldThrow(); |
751 |
< |
} |
602 |
< |
catch (IllegalArgumentException success) {} |
751 |
> |
} catch (IllegalArgumentException success) {} |
752 |
|
} |
753 |
|
|
754 |
|
/** |
756 |
|
*/ |
757 |
|
public void testConstructor4() { |
758 |
|
try { |
759 |
< |
new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
759 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
760 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
761 |
|
shouldThrow(); |
762 |
< |
} |
613 |
< |
catch (IllegalArgumentException success) {} |
762 |
> |
} catch (IllegalArgumentException success) {} |
763 |
|
} |
764 |
|
|
765 |
|
/** |
767 |
|
*/ |
768 |
|
public void testConstructor5() { |
769 |
|
try { |
770 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
770 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
771 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
772 |
|
shouldThrow(); |
773 |
< |
} |
624 |
< |
catch (IllegalArgumentException success) {} |
773 |
> |
} catch (IllegalArgumentException success) {} |
774 |
|
} |
775 |
|
|
776 |
|
/** |
778 |
|
*/ |
779 |
|
public void testConstructorNullPointerException() { |
780 |
|
try { |
781 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); |
781 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
782 |
|
shouldThrow(); |
783 |
< |
} |
635 |
< |
catch (NullPointerException success) {} |
783 |
> |
} catch (NullPointerException success) {} |
784 |
|
} |
785 |
|
|
638 |
– |
|
639 |
– |
|
786 |
|
/** |
787 |
|
* Constructor throws if corePoolSize argument is less than zero |
788 |
|
*/ |
789 |
|
public void testConstructor6() { |
790 |
|
try { |
791 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
791 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
792 |
> |
new ArrayBlockingQueue<Runnable>(10), |
793 |
> |
new SimpleThreadFactory()); |
794 |
|
shouldThrow(); |
795 |
|
} catch (IllegalArgumentException success) {} |
796 |
|
} |
800 |
|
*/ |
801 |
|
public void testConstructor7() { |
802 |
|
try { |
803 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
803 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
804 |
> |
new ArrayBlockingQueue<Runnable>(10), |
805 |
> |
new SimpleThreadFactory()); |
806 |
|
shouldThrow(); |
807 |
< |
} |
658 |
< |
catch (IllegalArgumentException success) {} |
807 |
> |
} catch (IllegalArgumentException success) {} |
808 |
|
} |
809 |
|
|
810 |
|
/** |
812 |
|
*/ |
813 |
|
public void testConstructor8() { |
814 |
|
try { |
815 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
815 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
816 |
> |
new ArrayBlockingQueue<Runnable>(10), |
817 |
> |
new SimpleThreadFactory()); |
818 |
|
shouldThrow(); |
819 |
< |
} |
669 |
< |
catch (IllegalArgumentException success) {} |
819 |
> |
} catch (IllegalArgumentException success) {} |
820 |
|
} |
821 |
|
|
822 |
|
/** |
824 |
|
*/ |
825 |
|
public void testConstructor9() { |
826 |
|
try { |
827 |
< |
new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
827 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
828 |
> |
new ArrayBlockingQueue<Runnable>(10), |
829 |
> |
new SimpleThreadFactory()); |
830 |
|
shouldThrow(); |
831 |
< |
} |
680 |
< |
catch (IllegalArgumentException success) {} |
831 |
> |
} catch (IllegalArgumentException success) {} |
832 |
|
} |
833 |
|
|
834 |
|
/** |
836 |
|
*/ |
837 |
|
public void testConstructor10() { |
838 |
|
try { |
839 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
839 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
840 |
> |
new ArrayBlockingQueue<Runnable>(10), |
841 |
> |
new SimpleThreadFactory()); |
842 |
|
shouldThrow(); |
843 |
< |
} |
691 |
< |
catch (IllegalArgumentException success) {} |
843 |
> |
} catch (IllegalArgumentException success) {} |
844 |
|
} |
845 |
|
|
846 |
|
/** |
848 |
|
*/ |
849 |
|
public void testConstructorNullPointerException2() { |
850 |
|
try { |
851 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); |
851 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
852 |
|
shouldThrow(); |
853 |
< |
} |
702 |
< |
catch (NullPointerException success) {} |
853 |
> |
} catch (NullPointerException success) {} |
854 |
|
} |
855 |
|
|
856 |
|
/** |
858 |
|
*/ |
859 |
|
public void testConstructorNullPointerException3() { |
860 |
|
try { |
861 |
< |
ThreadFactory f = null; |
862 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
861 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
862 |
> |
new ArrayBlockingQueue<Runnable>(10), |
863 |
> |
(ThreadFactory) null); |
864 |
|
shouldThrow(); |
865 |
< |
} |
714 |
< |
catch (NullPointerException success) {} |
865 |
> |
} catch (NullPointerException success) {} |
866 |
|
} |
867 |
|
|
717 |
– |
|
868 |
|
/** |
869 |
|
* Constructor throws if corePoolSize argument is less than zero |
870 |
|
*/ |
871 |
|
public void testConstructor11() { |
872 |
|
try { |
873 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
873 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
874 |
> |
new ArrayBlockingQueue<Runnable>(10), |
875 |
> |
new NoOpREHandler()); |
876 |
|
shouldThrow(); |
877 |
< |
} |
726 |
< |
catch (IllegalArgumentException success) {} |
877 |
> |
} catch (IllegalArgumentException success) {} |
878 |
|
} |
879 |
|
|
880 |
|
/** |
882 |
|
*/ |
883 |
|
public void testConstructor12() { |
884 |
|
try { |
885 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
885 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
886 |
> |
new ArrayBlockingQueue<Runnable>(10), |
887 |
> |
new NoOpREHandler()); |
888 |
|
shouldThrow(); |
889 |
< |
} |
737 |
< |
catch (IllegalArgumentException success) {} |
889 |
> |
} catch (IllegalArgumentException success) {} |
890 |
|
} |
891 |
|
|
892 |
|
/** |
894 |
|
*/ |
895 |
|
public void testConstructor13() { |
896 |
|
try { |
897 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
897 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
898 |
> |
new ArrayBlockingQueue<Runnable>(10), |
899 |
> |
new NoOpREHandler()); |
900 |
|
shouldThrow(); |
901 |
< |
} |
748 |
< |
catch (IllegalArgumentException success) {} |
901 |
> |
} catch (IllegalArgumentException success) {} |
902 |
|
} |
903 |
|
|
904 |
|
/** |
906 |
|
*/ |
907 |
|
public void testConstructor14() { |
908 |
|
try { |
909 |
< |
new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
909 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
910 |
> |
new ArrayBlockingQueue<Runnable>(10), |
911 |
> |
new NoOpREHandler()); |
912 |
|
shouldThrow(); |
913 |
< |
} |
759 |
< |
catch (IllegalArgumentException success) {} |
913 |
> |
} catch (IllegalArgumentException success) {} |
914 |
|
} |
915 |
|
|
916 |
|
/** |
918 |
|
*/ |
919 |
|
public void testConstructor15() { |
920 |
|
try { |
921 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
921 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
922 |
> |
new ArrayBlockingQueue<Runnable>(10), |
923 |
> |
new NoOpREHandler()); |
924 |
|
shouldThrow(); |
925 |
< |
} |
770 |
< |
catch (IllegalArgumentException success) {} |
925 |
> |
} catch (IllegalArgumentException success) {} |
926 |
|
} |
927 |
|
|
928 |
|
/** |
930 |
|
*/ |
931 |
|
public void testConstructorNullPointerException4() { |
932 |
|
try { |
933 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); |
933 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
934 |
> |
null, |
935 |
> |
new NoOpREHandler()); |
936 |
|
shouldThrow(); |
937 |
< |
} |
781 |
< |
catch (NullPointerException success) {} |
937 |
> |
} catch (NullPointerException success) {} |
938 |
|
} |
939 |
|
|
940 |
|
/** |
942 |
|
*/ |
943 |
|
public void testConstructorNullPointerException5() { |
944 |
|
try { |
945 |
< |
RejectedExecutionHandler r = null; |
946 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
945 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
946 |
> |
new ArrayBlockingQueue<Runnable>(10), |
947 |
> |
(RejectedExecutionHandler) null); |
948 |
|
shouldThrow(); |
949 |
< |
} |
793 |
< |
catch (NullPointerException success) {} |
949 |
> |
} catch (NullPointerException success) {} |
950 |
|
} |
951 |
|
|
796 |
– |
|
952 |
|
/** |
953 |
|
* Constructor throws if corePoolSize argument is less than zero |
954 |
|
*/ |
955 |
|
public void testConstructor16() { |
956 |
|
try { |
957 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
957 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
958 |
> |
new ArrayBlockingQueue<Runnable>(10), |
959 |
> |
new SimpleThreadFactory(), |
960 |
> |
new NoOpREHandler()); |
961 |
|
shouldThrow(); |
962 |
< |
} |
805 |
< |
catch (IllegalArgumentException success) {} |
962 |
> |
} catch (IllegalArgumentException success) {} |
963 |
|
} |
964 |
|
|
965 |
|
/** |
967 |
|
*/ |
968 |
|
public void testConstructor17() { |
969 |
|
try { |
970 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
970 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
971 |
> |
new ArrayBlockingQueue<Runnable>(10), |
972 |
> |
new SimpleThreadFactory(), |
973 |
> |
new NoOpREHandler()); |
974 |
|
shouldThrow(); |
975 |
< |
} |
816 |
< |
catch (IllegalArgumentException success) {} |
975 |
> |
} catch (IllegalArgumentException success) {} |
976 |
|
} |
977 |
|
|
978 |
|
/** |
980 |
|
*/ |
981 |
|
public void testConstructor18() { |
982 |
|
try { |
983 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
983 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
984 |
> |
new ArrayBlockingQueue<Runnable>(10), |
985 |
> |
new SimpleThreadFactory(), |
986 |
> |
new NoOpREHandler()); |
987 |
|
shouldThrow(); |
988 |
< |
} |
827 |
< |
catch (IllegalArgumentException success) {} |
988 |
> |
} catch (IllegalArgumentException success) {} |
989 |
|
} |
990 |
|
|
991 |
|
/** |
993 |
|
*/ |
994 |
|
public void testConstructor19() { |
995 |
|
try { |
996 |
< |
new CustomTPE(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
996 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
997 |
> |
new ArrayBlockingQueue<Runnable>(10), |
998 |
> |
new SimpleThreadFactory(), |
999 |
> |
new NoOpREHandler()); |
1000 |
|
shouldThrow(); |
1001 |
< |
} |
838 |
< |
catch (IllegalArgumentException success) {} |
1001 |
> |
} catch (IllegalArgumentException success) {} |
1002 |
|
} |
1003 |
|
|
1004 |
|
/** |
1006 |
|
*/ |
1007 |
|
public void testConstructor20() { |
1008 |
|
try { |
1009 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1009 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1010 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1011 |
> |
new SimpleThreadFactory(), |
1012 |
> |
new NoOpREHandler()); |
1013 |
|
shouldThrow(); |
1014 |
< |
} |
849 |
< |
catch (IllegalArgumentException success) {} |
1014 |
> |
} catch (IllegalArgumentException success) {} |
1015 |
|
} |
1016 |
|
|
1017 |
|
/** |
1018 |
< |
* Constructor throws if workQueue is set to null |
1018 |
> |
* Constructor throws if workQueue is null |
1019 |
|
*/ |
1020 |
|
public void testConstructorNullPointerException6() { |
1021 |
|
try { |
1022 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1022 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1023 |
> |
null, |
1024 |
> |
new SimpleThreadFactory(), |
1025 |
> |
new NoOpREHandler()); |
1026 |
|
shouldThrow(); |
1027 |
< |
} |
860 |
< |
catch (NullPointerException success) {} |
1027 |
> |
} catch (NullPointerException success) {} |
1028 |
|
} |
1029 |
|
|
1030 |
|
/** |
1031 |
< |
* Constructor throws if handler is set to null |
1031 |
> |
* Constructor throws if handler is null |
1032 |
|
*/ |
1033 |
|
public void testConstructorNullPointerException7() { |
1034 |
|
try { |
1035 |
< |
RejectedExecutionHandler r = null; |
1036 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1035 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1036 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1037 |
> |
new SimpleThreadFactory(), |
1038 |
> |
(RejectedExecutionHandler) null); |
1039 |
|
shouldThrow(); |
1040 |
< |
} |
872 |
< |
catch (NullPointerException success) {} |
1040 |
> |
} catch (NullPointerException success) {} |
1041 |
|
} |
1042 |
|
|
1043 |
|
/** |
1044 |
< |
* Constructor throws if ThreadFactory is set top null |
1044 |
> |
* Constructor throws if ThreadFactory is null |
1045 |
|
*/ |
1046 |
|
public void testConstructorNullPointerException8() { |
1047 |
|
try { |
1048 |
< |
ThreadFactory f = null; |
1049 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
1048 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1049 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1050 |
> |
(ThreadFactory) null, |
1051 |
> |
new NoOpREHandler()); |
1052 |
|
shouldThrow(); |
1053 |
< |
} |
884 |
< |
catch (NullPointerException successdn8) {} |
1053 |
> |
} catch (NullPointerException success) {} |
1054 |
|
} |
1055 |
|
|
887 |
– |
|
1056 |
|
/** |
1057 |
< |
* execute throws RejectedExecutionException |
890 |
< |
* if saturated. |
1057 |
> |
* execute throws RejectedExecutionException if saturated. |
1058 |
|
*/ |
1059 |
|
public void testSaturatedExecute() { |
1060 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1061 |
< |
try { |
1062 |
< |
|
1063 |
< |
for (int i = 0; i < 5; ++i) { |
1064 |
< |
p.execute(new MediumRunnable()); |
1060 |
> |
ThreadPoolExecutor p = |
1061 |
> |
new CustomTPE(1, 1, |
1062 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1063 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1064 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1065 |
> |
try { |
1066 |
> |
Runnable task = new CheckedRunnable() { |
1067 |
> |
public void realRun() throws InterruptedException { |
1068 |
> |
done.await(); |
1069 |
> |
}}; |
1070 |
> |
for (int i = 0; i < 2; ++i) |
1071 |
> |
p.execute(task); |
1072 |
> |
for (int i = 0; i < 2; ++i) { |
1073 |
> |
try { |
1074 |
> |
p.execute(task); |
1075 |
> |
shouldThrow(); |
1076 |
> |
} catch (RejectedExecutionException success) {} |
1077 |
> |
assertTrue(p.getTaskCount() <= 2); |
1078 |
|
} |
1079 |
< |
shouldThrow(); |
1080 |
< |
} catch (RejectedExecutionException success) {} |
1081 |
< |
joinPool(p); |
1079 |
> |
} finally { |
1080 |
> |
done.countDown(); |
1081 |
> |
joinPool(p); |
1082 |
> |
} |
1083 |
|
} |
1084 |
|
|
1085 |
|
/** |
1086 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1086 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
1087 |
|
*/ |
1088 |
|
public void testSaturatedExecute2() { |
1089 |
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1090 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1090 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, |
1091 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1092 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1093 |
> |
h); |
1094 |
|
try { |
911 |
– |
|
1095 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1096 |
< |
for (int i = 0; i < 5; ++i) { |
1096 |
> |
for (int i = 0; i < tasks.length; ++i) |
1097 |
|
tasks[i] = new TrackedNoOpRunnable(); |
915 |
– |
} |
1098 |
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1099 |
|
p.execute(mr); |
1100 |
< |
for (int i = 0; i < 5; ++i) { |
1100 |
> |
for (int i = 0; i < tasks.length; ++i) |
1101 |
|
p.execute(tasks[i]); |
1102 |
< |
} |
921 |
< |
for (int i = 1; i < 5; ++i) { |
1102 |
> |
for (int i = 1; i < tasks.length; ++i) |
1103 |
|
assertTrue(tasks[i].done); |
923 |
– |
} |
1104 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
925 |
– |
} catch (RejectedExecutionException ex) { |
926 |
– |
unexpectedException(); |
1105 |
|
} finally { |
1106 |
|
joinPool(p); |
1107 |
|
} |
1108 |
|
} |
1109 |
|
|
1110 |
|
/** |
1111 |
< |
* executor using DiscardPolicy drops task if saturated. |
1111 |
> |
* executor using DiscardPolicy drops task if saturated. |
1112 |
|
*/ |
1113 |
|
public void testSaturatedExecute3() { |
1114 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1115 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1115 |
> |
ThreadPoolExecutor p = |
1116 |
> |
new CustomTPE(1, 1, |
1117 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1118 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1119 |
> |
h); |
1120 |
|
try { |
939 |
– |
|
1121 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1122 |
< |
for (int i = 0; i < 5; ++i) { |
1122 |
> |
for (int i = 0; i < tasks.length; ++i) |
1123 |
|
tasks[i] = new TrackedNoOpRunnable(); |
943 |
– |
} |
1124 |
|
p.execute(new TrackedLongRunnable()); |
1125 |
< |
for (int i = 0; i < 5; ++i) { |
1126 |
< |
p.execute(tasks[i]); |
1127 |
< |
} |
1128 |
< |
for (int i = 0; i < 5; ++i) { |
949 |
< |
assertFalse(tasks[i].done); |
950 |
< |
} |
1125 |
> |
for (TrackedNoOpRunnable task : tasks) |
1126 |
> |
p.execute(task); |
1127 |
> |
for (TrackedNoOpRunnable task : tasks) |
1128 |
> |
assertFalse(task.done); |
1129 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
952 |
– |
} catch (RejectedExecutionException ex) { |
953 |
– |
unexpectedException(); |
1130 |
|
} finally { |
1131 |
|
joinPool(p); |
1132 |
|
} |
1133 |
|
} |
1134 |
|
|
1135 |
|
/** |
1136 |
< |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1136 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1137 |
|
*/ |
1138 |
|
public void testSaturatedExecute4() { |
1139 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1140 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1140 |
> |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1141 |
|
try { |
1142 |
|
p.execute(new TrackedLongRunnable()); |
1143 |
|
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1148 |
|
assertFalse(p.getQueue().contains(r2)); |
1149 |
|
assertTrue(p.getQueue().contains(r3)); |
1150 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
975 |
– |
} catch (RejectedExecutionException ex) { |
976 |
– |
unexpectedException(); |
1151 |
|
} finally { |
1152 |
|
joinPool(p); |
1153 |
|
} |
1154 |
|
} |
1155 |
|
|
1156 |
|
/** |
1157 |
< |
* execute throws RejectedExecutionException if shutdown |
1157 |
> |
* execute throws RejectedExecutionException if shutdown |
1158 |
|
*/ |
1159 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1160 |
< |
ThreadPoolExecutor tpe = |
1161 |
< |
new CustomTPE(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1162 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1163 |
< |
try { |
1164 |
< |
tpe.execute(new NoOpRunnable()); |
1165 |
< |
shouldThrow(); |
1166 |
< |
} catch (RejectedExecutionException success) {} |
1160 |
> |
ThreadPoolExecutor p = |
1161 |
> |
new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1162 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1163 |
> |
try { |
1164 |
> |
p.execute(new NoOpRunnable()); |
1165 |
> |
shouldThrow(); |
1166 |
> |
} catch (RejectedExecutionException success) {} |
1167 |
|
|
1168 |
< |
joinPool(tpe); |
1168 |
> |
joinPool(p); |
1169 |
|
} |
1170 |
|
|
1171 |
|
/** |
1172 |
< |
* execute using CallerRunsPolicy drops task on shutdown |
1172 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
1173 |
|
*/ |
1174 |
|
public void testCallerRunsOnShutdown() { |
1175 |
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1176 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1176 |
> |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1177 |
|
|
1178 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1179 |
< |
try { |
1179 |
> |
try { |
1180 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1181 |
< |
p.execute(r); |
1181 |
> |
p.execute(r); |
1182 |
|
assertFalse(r.done); |
1009 |
– |
} catch (RejectedExecutionException success) { |
1010 |
– |
unexpectedException(); |
1183 |
|
} finally { |
1184 |
|
joinPool(p); |
1185 |
|
} |
1186 |
|
} |
1187 |
|
|
1188 |
|
/** |
1189 |
< |
* execute using DiscardPolicy drops task on shutdown |
1189 |
> |
* execute using DiscardPolicy drops task on shutdown |
1190 |
|
*/ |
1191 |
|
public void testDiscardOnShutdown() { |
1192 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1193 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1193 |
> |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1194 |
|
|
1195 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1196 |
< |
try { |
1196 |
> |
try { |
1197 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1198 |
< |
p.execute(r); |
1198 |
> |
p.execute(r); |
1199 |
|
assertFalse(r.done); |
1028 |
– |
} catch (RejectedExecutionException success) { |
1029 |
– |
unexpectedException(); |
1200 |
|
} finally { |
1201 |
|
joinPool(p); |
1202 |
|
} |
1203 |
|
} |
1204 |
|
|
1035 |
– |
|
1205 |
|
/** |
1206 |
< |
* execute using DiscardOldestPolicy drops task on shutdown |
1206 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
1207 |
|
*/ |
1208 |
|
public void testDiscardOldestOnShutdown() { |
1209 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1210 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1210 |
> |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1211 |
|
|
1212 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1213 |
< |
try { |
1213 |
> |
try { |
1214 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1215 |
< |
p.execute(r); |
1215 |
> |
p.execute(r); |
1216 |
|
assertFalse(r.done); |
1048 |
– |
} catch (RejectedExecutionException success) { |
1049 |
– |
unexpectedException(); |
1217 |
|
} finally { |
1218 |
|
joinPool(p); |
1219 |
|
} |
1220 |
|
} |
1221 |
|
|
1055 |
– |
|
1222 |
|
/** |
1223 |
< |
* execute (null) throws NPE |
1223 |
> |
* execute(null) throws NPE |
1224 |
|
*/ |
1225 |
|
public void testExecuteNull() { |
1226 |
< |
ThreadPoolExecutor tpe = null; |
1226 |
> |
ThreadPoolExecutor p = |
1227 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1228 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1229 |
|
try { |
1230 |
< |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1063 |
< |
tpe.execute(null); |
1230 |
> |
p.execute(null); |
1231 |
|
shouldThrow(); |
1232 |
< |
} catch (NullPointerException success) {} |
1232 |
> |
} catch (NullPointerException success) {} |
1233 |
|
|
1234 |
< |
joinPool(tpe); |
1234 |
> |
joinPool(p); |
1235 |
|
} |
1236 |
|
|
1237 |
|
/** |
1238 |
< |
* setCorePoolSize of negative value throws IllegalArgumentException |
1238 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
1239 |
|
*/ |
1240 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1241 |
< |
ThreadPoolExecutor tpe = null; |
1242 |
< |
try { |
1243 |
< |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1244 |
< |
} catch (Exception e) {} |
1245 |
< |
try { |
1246 |
< |
tpe.setCorePoolSize(-1); |
1080 |
< |
shouldThrow(); |
1081 |
< |
} catch (IllegalArgumentException success) { |
1241 |
> |
ThreadPoolExecutor p = |
1242 |
> |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1243 |
> |
try { |
1244 |
> |
p.setCorePoolSize(-1); |
1245 |
> |
shouldThrow(); |
1246 |
> |
} catch (IllegalArgumentException success) { |
1247 |
|
} finally { |
1248 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1248 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1249 |
|
} |
1250 |
< |
joinPool(tpe); |
1250 |
> |
joinPool(p); |
1251 |
|
} |
1252 |
|
|
1253 |
|
/** |
1254 |
< |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1255 |
< |
* given a value less the core pool size |
1254 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException |
1255 |
> |
* if given a value less the core pool size |
1256 |
|
*/ |
1257 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1258 |
< |
ThreadPoolExecutor tpe = null; |
1259 |
< |
try { |
1095 |
< |
tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1096 |
< |
} catch (Exception e) {} |
1258 |
> |
ThreadPoolExecutor p = |
1259 |
> |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1260 |
|
try { |
1261 |
< |
tpe.setMaximumPoolSize(1); |
1261 |
> |
p.setMaximumPoolSize(1); |
1262 |
|
shouldThrow(); |
1263 |
|
} catch (IllegalArgumentException success) { |
1264 |
|
} finally { |
1265 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1265 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1266 |
|
} |
1267 |
< |
joinPool(tpe); |
1267 |
> |
joinPool(p); |
1268 |
|
} |
1269 |
|
|
1270 |
|
/** |
1271 |
< |
* setMaximumPoolSize throws IllegalArgumentException |
1272 |
< |
* if given a negative value |
1271 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
1272 |
> |
* if given a negative value |
1273 |
|
*/ |
1274 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1275 |
< |
ThreadPoolExecutor tpe = null; |
1275 |
> |
ThreadPoolExecutor p = |
1276 |
> |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1277 |
|
try { |
1278 |
< |
tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1115 |
< |
} catch (Exception e) {} |
1116 |
< |
try { |
1117 |
< |
tpe.setMaximumPoolSize(-1); |
1278 |
> |
p.setMaximumPoolSize(-1); |
1279 |
|
shouldThrow(); |
1280 |
|
} catch (IllegalArgumentException success) { |
1281 |
|
} finally { |
1282 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1282 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1283 |
|
} |
1284 |
< |
joinPool(tpe); |
1284 |
> |
joinPool(p); |
1285 |
|
} |
1286 |
|
|
1126 |
– |
|
1287 |
|
/** |
1288 |
< |
* setKeepAliveTime throws IllegalArgumentException |
1289 |
< |
* when given a negative value |
1288 |
> |
* setKeepAliveTime throws IllegalArgumentException |
1289 |
> |
* when given a negative value |
1290 |
|
*/ |
1291 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1292 |
< |
ThreadPoolExecutor tpe = null; |
1293 |
< |
try { |
1134 |
< |
tpe = new CustomTPE(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1135 |
< |
} catch (Exception e) {} |
1292 |
> |
ThreadPoolExecutor p = |
1293 |
> |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1294 |
|
|
1295 |
< |
try { |
1296 |
< |
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
1295 |
> |
try { |
1296 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1297 |
|
shouldThrow(); |
1298 |
|
} catch (IllegalArgumentException success) { |
1299 |
|
} finally { |
1300 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1300 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1301 |
|
} |
1302 |
< |
joinPool(tpe); |
1302 |
> |
joinPool(p); |
1303 |
|
} |
1304 |
|
|
1305 |
|
/** |
1306 |
|
* terminated() is called on termination |
1307 |
|
*/ |
1308 |
|
public void testTerminated() { |
1309 |
< |
CustomTPE tpe = new CustomTPE(); |
1310 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1311 |
< |
assertTrue(tpe.terminatedCalled); |
1312 |
< |
joinPool(tpe); |
1309 |
> |
CustomTPE p = new CustomTPE(); |
1310 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1311 |
> |
assertTrue(p.terminatedCalled()); |
1312 |
> |
joinPool(p); |
1313 |
|
} |
1314 |
|
|
1315 |
|
/** |
1316 |
|
* beforeExecute and afterExecute are called when executing task |
1317 |
|
*/ |
1318 |
< |
public void testBeforeAfter() { |
1319 |
< |
CustomTPE tpe = new CustomTPE(); |
1318 |
> |
public void testBeforeAfter() throws InterruptedException { |
1319 |
> |
CustomTPE p = new CustomTPE(); |
1320 |
|
try { |
1321 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1322 |
< |
tpe.execute(r); |
1323 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1324 |
< |
assertTrue(r.done); |
1325 |
< |
assertTrue(tpe.beforeCalled); |
1326 |
< |
assertTrue(tpe.afterCalled); |
1327 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1328 |
< |
} |
1329 |
< |
catch (Exception ex) { |
1330 |
< |
unexpectedException(); |
1321 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1322 |
> |
p.execute(new CheckedRunnable() { |
1323 |
> |
public void realRun() { |
1324 |
> |
done.countDown(); |
1325 |
> |
}}); |
1326 |
> |
await(p.afterCalled); |
1327 |
> |
assertEquals(0, done.getCount()); |
1328 |
> |
assertTrue(p.afterCalled()); |
1329 |
> |
assertTrue(p.beforeCalled()); |
1330 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1331 |
|
} finally { |
1332 |
< |
joinPool(tpe); |
1332 |
> |
joinPool(p); |
1333 |
|
} |
1334 |
|
} |
1335 |
|
|
1336 |
|
/** |
1337 |
|
* completed submit of callable returns result |
1338 |
|
*/ |
1339 |
< |
public void testSubmitCallable() { |
1340 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1339 |
> |
public void testSubmitCallable() throws Exception { |
1340 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1341 |
|
try { |
1342 |
|
Future<String> future = e.submit(new StringTask()); |
1343 |
|
String result = future.get(); |
1344 |
|
assertSame(TEST_STRING, result); |
1187 |
– |
} |
1188 |
– |
catch (ExecutionException ex) { |
1189 |
– |
unexpectedException(); |
1190 |
– |
} |
1191 |
– |
catch (InterruptedException ex) { |
1192 |
– |
unexpectedException(); |
1345 |
|
} finally { |
1346 |
|
joinPool(e); |
1347 |
|
} |
1350 |
|
/** |
1351 |
|
* completed submit of runnable returns successfully |
1352 |
|
*/ |
1353 |
< |
public void testSubmitRunnable() { |
1354 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1353 |
> |
public void testSubmitRunnable() throws Exception { |
1354 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1355 |
|
try { |
1356 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1357 |
|
future.get(); |
1358 |
|
assertTrue(future.isDone()); |
1207 |
– |
} |
1208 |
– |
catch (ExecutionException ex) { |
1209 |
– |
unexpectedException(); |
1210 |
– |
} |
1211 |
– |
catch (InterruptedException ex) { |
1212 |
– |
unexpectedException(); |
1359 |
|
} finally { |
1360 |
|
joinPool(e); |
1361 |
|
} |
1364 |
|
/** |
1365 |
|
* completed submit of (runnable, result) returns result |
1366 |
|
*/ |
1367 |
< |
public void testSubmitRunnable2() { |
1368 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1367 |
> |
public void testSubmitRunnable2() throws Exception { |
1368 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1369 |
|
try { |
1370 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1371 |
|
String result = future.get(); |
1372 |
|
assertSame(TEST_STRING, result); |
1227 |
– |
} |
1228 |
– |
catch (ExecutionException ex) { |
1229 |
– |
unexpectedException(); |
1230 |
– |
} |
1231 |
– |
catch (InterruptedException ex) { |
1232 |
– |
unexpectedException(); |
1373 |
|
} finally { |
1374 |
|
joinPool(e); |
1375 |
|
} |
1376 |
|
} |
1377 |
|
|
1238 |
– |
|
1239 |
– |
|
1240 |
– |
|
1241 |
– |
|
1378 |
|
/** |
1379 |
|
* invokeAny(null) throws NPE |
1380 |
|
*/ |
1381 |
< |
public void testInvokeAny1() { |
1382 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1381 |
> |
public void testInvokeAny1() throws Exception { |
1382 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1383 |
|
try { |
1384 |
|
e.invokeAny(null); |
1385 |
+ |
shouldThrow(); |
1386 |
|
} catch (NullPointerException success) { |
1250 |
– |
} catch (Exception ex) { |
1251 |
– |
unexpectedException(); |
1387 |
|
} finally { |
1388 |
|
joinPool(e); |
1389 |
|
} |
1392 |
|
/** |
1393 |
|
* invokeAny(empty collection) throws IAE |
1394 |
|
*/ |
1395 |
< |
public void testInvokeAny2() { |
1396 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1395 |
> |
public void testInvokeAny2() throws Exception { |
1396 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1397 |
|
try { |
1398 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1399 |
+ |
shouldThrow(); |
1400 |
|
} catch (IllegalArgumentException success) { |
1265 |
– |
} catch (Exception ex) { |
1266 |
– |
unexpectedException(); |
1401 |
|
} finally { |
1402 |
|
joinPool(e); |
1403 |
|
} |
1406 |
|
/** |
1407 |
|
* invokeAny(c) throws NPE if c has null elements |
1408 |
|
*/ |
1409 |
< |
public void testInvokeAny3() { |
1410 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1409 |
> |
public void testInvokeAny3() throws Exception { |
1410 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1411 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1412 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1413 |
> |
l.add(latchAwaitingStringTask(latch)); |
1414 |
> |
l.add(null); |
1415 |
|
try { |
1278 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1279 |
– |
l.add(new StringTask()); |
1280 |
– |
l.add(null); |
1416 |
|
e.invokeAny(l); |
1417 |
+ |
shouldThrow(); |
1418 |
|
} catch (NullPointerException success) { |
1283 |
– |
} catch (Exception ex) { |
1284 |
– |
unexpectedException(); |
1419 |
|
} finally { |
1420 |
+ |
latch.countDown(); |
1421 |
|
joinPool(e); |
1422 |
|
} |
1423 |
|
} |
1425 |
|
/** |
1426 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1427 |
|
*/ |
1428 |
< |
public void testInvokeAny4() { |
1429 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1428 |
> |
public void testInvokeAny4() throws Exception { |
1429 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1430 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1431 |
> |
l.add(new NPETask()); |
1432 |
|
try { |
1296 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1297 |
– |
l.add(new NPETask()); |
1433 |
|
e.invokeAny(l); |
1434 |
+ |
shouldThrow(); |
1435 |
|
} catch (ExecutionException success) { |
1436 |
< |
} catch (Exception ex) { |
1301 |
< |
unexpectedException(); |
1436 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1437 |
|
} finally { |
1438 |
|
joinPool(e); |
1439 |
|
} |
1442 |
|
/** |
1443 |
|
* invokeAny(c) returns result of some task |
1444 |
|
*/ |
1445 |
< |
public void testInvokeAny5() { |
1446 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1445 |
> |
public void testInvokeAny5() throws Exception { |
1446 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1447 |
|
try { |
1448 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1448 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1449 |
|
l.add(new StringTask()); |
1450 |
|
l.add(new StringTask()); |
1451 |
|
String result = e.invokeAny(l); |
1452 |
|
assertSame(TEST_STRING, result); |
1318 |
– |
} catch (ExecutionException success) { |
1319 |
– |
} catch (Exception ex) { |
1320 |
– |
unexpectedException(); |
1453 |
|
} finally { |
1454 |
|
joinPool(e); |
1455 |
|
} |
1458 |
|
/** |
1459 |
|
* invokeAll(null) throws NPE |
1460 |
|
*/ |
1461 |
< |
public void testInvokeAll1() { |
1462 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1461 |
> |
public void testInvokeAll1() throws Exception { |
1462 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1463 |
|
try { |
1464 |
|
e.invokeAll(null); |
1465 |
+ |
shouldThrow(); |
1466 |
|
} catch (NullPointerException success) { |
1334 |
– |
} catch (Exception ex) { |
1335 |
– |
unexpectedException(); |
1467 |
|
} finally { |
1468 |
|
joinPool(e); |
1469 |
|
} |
1472 |
|
/** |
1473 |
|
* invokeAll(empty collection) returns empty collection |
1474 |
|
*/ |
1475 |
< |
public void testInvokeAll2() { |
1476 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1475 |
> |
public void testInvokeAll2() throws Exception { |
1476 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1477 |
|
try { |
1478 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1479 |
|
assertTrue(r.isEmpty()); |
1349 |
– |
} catch (Exception ex) { |
1350 |
– |
unexpectedException(); |
1480 |
|
} finally { |
1481 |
|
joinPool(e); |
1482 |
|
} |
1485 |
|
/** |
1486 |
|
* invokeAll(c) throws NPE if c has null elements |
1487 |
|
*/ |
1488 |
< |
public void testInvokeAll3() { |
1489 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1488 |
> |
public void testInvokeAll3() throws Exception { |
1489 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1490 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1491 |
> |
l.add(new StringTask()); |
1492 |
> |
l.add(null); |
1493 |
|
try { |
1362 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1363 |
– |
l.add(new StringTask()); |
1364 |
– |
l.add(null); |
1494 |
|
e.invokeAll(l); |
1495 |
+ |
shouldThrow(); |
1496 |
|
} catch (NullPointerException success) { |
1367 |
– |
} catch (Exception ex) { |
1368 |
– |
unexpectedException(); |
1497 |
|
} finally { |
1498 |
|
joinPool(e); |
1499 |
|
} |
1502 |
|
/** |
1503 |
|
* get of element of invokeAll(c) throws exception on failed task |
1504 |
|
*/ |
1505 |
< |
public void testInvokeAll4() { |
1506 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1505 |
> |
public void testInvokeAll4() throws Exception { |
1506 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1507 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1508 |
> |
l.add(new NPETask()); |
1509 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1510 |
> |
assertEquals(1, futures.size()); |
1511 |
|
try { |
1512 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1513 |
< |
l.add(new NPETask()); |
1382 |
< |
List<Future<String>> result = e.invokeAll(l); |
1383 |
< |
assertEquals(1, result.size()); |
1384 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1385 |
< |
it.next().get(); |
1512 |
> |
futures.get(0).get(); |
1513 |
> |
shouldThrow(); |
1514 |
|
} catch (ExecutionException success) { |
1515 |
< |
} catch (Exception ex) { |
1388 |
< |
unexpectedException(); |
1515 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1516 |
|
} finally { |
1517 |
|
joinPool(e); |
1518 |
|
} |
1521 |
|
/** |
1522 |
|
* invokeAll(c) returns results of all completed tasks |
1523 |
|
*/ |
1524 |
< |
public void testInvokeAll5() { |
1525 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1524 |
> |
public void testInvokeAll5() throws Exception { |
1525 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1526 |
|
try { |
1527 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1527 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1528 |
|
l.add(new StringTask()); |
1529 |
|
l.add(new StringTask()); |
1530 |
< |
List<Future<String>> result = e.invokeAll(l); |
1531 |
< |
assertEquals(2, result.size()); |
1532 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1533 |
< |
assertSame(TEST_STRING, it.next().get()); |
1407 |
< |
} catch (ExecutionException success) { |
1408 |
< |
} catch (Exception ex) { |
1409 |
< |
unexpectedException(); |
1530 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1531 |
> |
assertEquals(2, futures.size()); |
1532 |
> |
for (Future<String> future : futures) |
1533 |
> |
assertSame(TEST_STRING, future.get()); |
1534 |
|
} finally { |
1535 |
|
joinPool(e); |
1536 |
|
} |
1537 |
|
} |
1538 |
|
|
1415 |
– |
|
1416 |
– |
|
1539 |
|
/** |
1540 |
|
* timed invokeAny(null) throws NPE |
1541 |
|
*/ |
1542 |
< |
public void testTimedInvokeAny1() { |
1543 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1542 |
> |
public void testTimedInvokeAny1() throws Exception { |
1543 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1544 |
|
try { |
1545 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1545 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1546 |
> |
shouldThrow(); |
1547 |
|
} catch (NullPointerException success) { |
1425 |
– |
} catch (Exception ex) { |
1426 |
– |
unexpectedException(); |
1548 |
|
} finally { |
1549 |
|
joinPool(e); |
1550 |
|
} |
1553 |
|
/** |
1554 |
|
* timed invokeAny(,,null) throws NPE |
1555 |
|
*/ |
1556 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
1557 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1556 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1557 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1558 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1559 |
> |
l.add(new StringTask()); |
1560 |
|
try { |
1438 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1439 |
– |
l.add(new StringTask()); |
1561 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1562 |
+ |
shouldThrow(); |
1563 |
|
} catch (NullPointerException success) { |
1442 |
– |
} catch (Exception ex) { |
1443 |
– |
unexpectedException(); |
1564 |
|
} finally { |
1565 |
|
joinPool(e); |
1566 |
|
} |
1569 |
|
/** |
1570 |
|
* timed invokeAny(empty collection) throws IAE |
1571 |
|
*/ |
1572 |
< |
public void testTimedInvokeAny2() { |
1573 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1572 |
> |
public void testTimedInvokeAny2() throws Exception { |
1573 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1574 |
|
try { |
1575 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1575 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1576 |
> |
shouldThrow(); |
1577 |
|
} catch (IllegalArgumentException success) { |
1457 |
– |
} catch (Exception ex) { |
1458 |
– |
unexpectedException(); |
1578 |
|
} finally { |
1579 |
|
joinPool(e); |
1580 |
|
} |
1583 |
|
/** |
1584 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1585 |
|
*/ |
1586 |
< |
public void testTimedInvokeAny3() { |
1587 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1586 |
> |
public void testTimedInvokeAny3() throws Exception { |
1587 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1588 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1589 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1590 |
> |
l.add(latchAwaitingStringTask(latch)); |
1591 |
> |
l.add(null); |
1592 |
|
try { |
1593 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1594 |
< |
l.add(new StringTask()); |
1472 |
< |
l.add(null); |
1473 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1593 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1594 |
> |
shouldThrow(); |
1595 |
|
} catch (NullPointerException success) { |
1475 |
– |
} catch (Exception ex) { |
1476 |
– |
ex.printStackTrace(); |
1477 |
– |
unexpectedException(); |
1596 |
|
} finally { |
1597 |
+ |
latch.countDown(); |
1598 |
|
joinPool(e); |
1599 |
|
} |
1600 |
|
} |
1602 |
|
/** |
1603 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1604 |
|
*/ |
1605 |
< |
public void testTimedInvokeAny4() { |
1606 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1605 |
> |
public void testTimedInvokeAny4() throws Exception { |
1606 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1607 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1608 |
> |
l.add(new NPETask()); |
1609 |
|
try { |
1610 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1611 |
< |
l.add(new NPETask()); |
1491 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1610 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1611 |
> |
shouldThrow(); |
1612 |
|
} catch (ExecutionException success) { |
1613 |
< |
} catch (Exception ex) { |
1494 |
< |
unexpectedException(); |
1613 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1614 |
|
} finally { |
1615 |
|
joinPool(e); |
1616 |
|
} |
1619 |
|
/** |
1620 |
|
* timed invokeAny(c) returns result of some task |
1621 |
|
*/ |
1622 |
< |
public void testTimedInvokeAny5() { |
1623 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1622 |
> |
public void testTimedInvokeAny5() throws Exception { |
1623 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1624 |
|
try { |
1625 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1625 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1626 |
|
l.add(new StringTask()); |
1627 |
|
l.add(new StringTask()); |
1628 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1628 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1629 |
|
assertSame(TEST_STRING, result); |
1511 |
– |
} catch (ExecutionException success) { |
1512 |
– |
} catch (Exception ex) { |
1513 |
– |
unexpectedException(); |
1630 |
|
} finally { |
1631 |
|
joinPool(e); |
1632 |
|
} |
1635 |
|
/** |
1636 |
|
* timed invokeAll(null) throws NPE |
1637 |
|
*/ |
1638 |
< |
public void testTimedInvokeAll1() { |
1639 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1638 |
> |
public void testTimedInvokeAll1() throws Exception { |
1639 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1640 |
|
try { |
1641 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1641 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1642 |
> |
shouldThrow(); |
1643 |
|
} catch (NullPointerException success) { |
1527 |
– |
} catch (Exception ex) { |
1528 |
– |
unexpectedException(); |
1644 |
|
} finally { |
1645 |
|
joinPool(e); |
1646 |
|
} |
1649 |
|
/** |
1650 |
|
* timed invokeAll(,,null) throws NPE |
1651 |
|
*/ |
1652 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
1653 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1652 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1653 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1654 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1655 |
> |
l.add(new StringTask()); |
1656 |
|
try { |
1540 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1541 |
– |
l.add(new StringTask()); |
1657 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1658 |
+ |
shouldThrow(); |
1659 |
|
} catch (NullPointerException success) { |
1544 |
– |
} catch (Exception ex) { |
1545 |
– |
unexpectedException(); |
1660 |
|
} finally { |
1661 |
|
joinPool(e); |
1662 |
|
} |
1665 |
|
/** |
1666 |
|
* timed invokeAll(empty collection) returns empty collection |
1667 |
|
*/ |
1668 |
< |
public void testTimedInvokeAll2() { |
1669 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1668 |
> |
public void testTimedInvokeAll2() throws Exception { |
1669 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1670 |
|
try { |
1671 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1671 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1672 |
|
assertTrue(r.isEmpty()); |
1559 |
– |
} catch (Exception ex) { |
1560 |
– |
unexpectedException(); |
1673 |
|
} finally { |
1674 |
|
joinPool(e); |
1675 |
|
} |
1678 |
|
/** |
1679 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1680 |
|
*/ |
1681 |
< |
public void testTimedInvokeAll3() { |
1682 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1681 |
> |
public void testTimedInvokeAll3() throws Exception { |
1682 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1683 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1684 |
> |
l.add(new StringTask()); |
1685 |
> |
l.add(null); |
1686 |
|
try { |
1687 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1688 |
< |
l.add(new StringTask()); |
1574 |
< |
l.add(null); |
1575 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1687 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1688 |
> |
shouldThrow(); |
1689 |
|
} catch (NullPointerException success) { |
1577 |
– |
} catch (Exception ex) { |
1578 |
– |
unexpectedException(); |
1690 |
|
} finally { |
1691 |
|
joinPool(e); |
1692 |
|
} |
1695 |
|
/** |
1696 |
|
* get of element of invokeAll(c) throws exception on failed task |
1697 |
|
*/ |
1698 |
< |
public void testTimedInvokeAll4() { |
1699 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1698 |
> |
public void testTimedInvokeAll4() throws Exception { |
1699 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1700 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1701 |
> |
l.add(new NPETask()); |
1702 |
> |
List<Future<String>> futures = |
1703 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1704 |
> |
assertEquals(1, futures.size()); |
1705 |
|
try { |
1706 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1707 |
< |
l.add(new NPETask()); |
1592 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1593 |
< |
assertEquals(1, result.size()); |
1594 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1595 |
< |
it.next().get(); |
1706 |
> |
futures.get(0).get(); |
1707 |
> |
shouldThrow(); |
1708 |
|
} catch (ExecutionException success) { |
1709 |
< |
} catch (Exception ex) { |
1598 |
< |
unexpectedException(); |
1709 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1710 |
|
} finally { |
1711 |
|
joinPool(e); |
1712 |
|
} |
1715 |
|
/** |
1716 |
|
* timed invokeAll(c) returns results of all completed tasks |
1717 |
|
*/ |
1718 |
< |
public void testTimedInvokeAll5() { |
1719 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1718 |
> |
public void testTimedInvokeAll5() throws Exception { |
1719 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1720 |
|
try { |
1721 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1721 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1722 |
|
l.add(new StringTask()); |
1723 |
|
l.add(new StringTask()); |
1724 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1725 |
< |
assertEquals(2, result.size()); |
1726 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1727 |
< |
assertSame(TEST_STRING, it.next().get()); |
1728 |
< |
} catch (ExecutionException success) { |
1618 |
< |
} catch (Exception ex) { |
1619 |
< |
unexpectedException(); |
1724 |
> |
List<Future<String>> futures = |
1725 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1726 |
> |
assertEquals(2, futures.size()); |
1727 |
> |
for (Future<String> future : futures) |
1728 |
> |
assertSame(TEST_STRING, future.get()); |
1729 |
|
} finally { |
1730 |
|
joinPool(e); |
1731 |
|
} |
1734 |
|
/** |
1735 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1736 |
|
*/ |
1737 |
< |
public void testTimedInvokeAll6() { |
1738 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1737 |
> |
public void testTimedInvokeAll6() throws Exception { |
1738 |
> |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1739 |
|
try { |
1740 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1740 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1741 |
|
l.add(new StringTask()); |
1742 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1743 |
|
l.add(new StringTask()); |
1744 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1745 |
< |
assertEquals(3, result.size()); |
1746 |
< |
Iterator<Future<String>> it = result.iterator(); |
1747 |
< |
Future<String> f1 = it.next(); |
1748 |
< |
Future<String> f2 = it.next(); |
1749 |
< |
Future<String> f3 = it.next(); |
1750 |
< |
assertTrue(f1.isDone()); |
1642 |
< |
assertTrue(f2.isDone()); |
1643 |
< |
assertTrue(f3.isDone()); |
1644 |
< |
assertFalse(f1.isCancelled()); |
1645 |
< |
assertTrue(f2.isCancelled()); |
1646 |
< |
} catch (Exception ex) { |
1647 |
< |
unexpectedException(); |
1744 |
> |
List<Future<String>> futures = |
1745 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1746 |
> |
assertEquals(l.size(), futures.size()); |
1747 |
> |
for (Future future : futures) |
1748 |
> |
assertTrue(future.isDone()); |
1749 |
> |
assertFalse(futures.get(0).isCancelled()); |
1750 |
> |
assertTrue(futures.get(1).isCancelled()); |
1751 |
|
} finally { |
1752 |
|
joinPool(e); |
1753 |
|
} |
1757 |
|
* Execution continues if there is at least one thread even if |
1758 |
|
* thread factory fails to create more |
1759 |
|
*/ |
1760 |
< |
public void testFailingThreadFactory() { |
1761 |
< |
ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1762 |
< |
try { |
1763 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1764 |
< |
for (int k = 0; k < 100; ++k) { |
1765 |
< |
e.execute(new NoOpRunnable()); |
1766 |
< |
} |
1767 |
< |
Thread.sleep(LONG_DELAY_MS); |
1768 |
< |
} catch (Exception ex) { |
1769 |
< |
unexpectedException(); |
1760 |
> |
public void testFailingThreadFactory() throws InterruptedException { |
1761 |
> |
final ExecutorService e = |
1762 |
> |
new CustomTPE(100, 100, |
1763 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1764 |
> |
new LinkedBlockingQueue<Runnable>(), |
1765 |
> |
new FailingThreadFactory()); |
1766 |
> |
try { |
1767 |
> |
final int TASKS = 100; |
1768 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1769 |
> |
for (int k = 0; k < TASKS; ++k) |
1770 |
> |
e.execute(new CheckedRunnable() { |
1771 |
> |
public void realRun() { |
1772 |
> |
done.countDown(); |
1773 |
> |
}}); |
1774 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1775 |
|
} finally { |
1776 |
|
joinPool(e); |
1777 |
|
} |
1781 |
|
* allowsCoreThreadTimeOut is by default false. |
1782 |
|
*/ |
1783 |
|
public void testAllowsCoreThreadTimeOut() { |
1784 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1785 |
< |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1786 |
< |
joinPool(tpe); |
1784 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1785 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1786 |
> |
joinPool(p); |
1787 |
|
} |
1788 |
|
|
1789 |
|
/** |
1790 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1791 |
|
*/ |
1792 |
< |
public void testAllowCoreThreadTimeOut_true() { |
1793 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1794 |
< |
tpe.allowCoreThreadTimeOut(true); |
1795 |
< |
tpe.execute(new NoOpRunnable()); |
1796 |
< |
try { |
1797 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1798 |
< |
assertEquals(0, tpe.getPoolSize()); |
1799 |
< |
} catch (InterruptedException e) { |
1800 |
< |
unexpectedException(); |
1792 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1793 |
> |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1794 |
> |
final ThreadPoolExecutor p = |
1795 |
> |
new CustomTPE(2, 10, |
1796 |
> |
coreThreadTimeOut, MILLISECONDS, |
1797 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1798 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1799 |
> |
try { |
1800 |
> |
p.allowCoreThreadTimeOut(true); |
1801 |
> |
p.execute(new CheckedRunnable() { |
1802 |
> |
public void realRun() throws InterruptedException { |
1803 |
> |
threadStarted.countDown(); |
1804 |
> |
assertEquals(1, p.getPoolSize()); |
1805 |
> |
}}); |
1806 |
> |
await(threadStarted); |
1807 |
> |
delay(coreThreadTimeOut); |
1808 |
> |
long startTime = System.nanoTime(); |
1809 |
> |
while (p.getPoolSize() > 0 |
1810 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1811 |
> |
Thread.yield(); |
1812 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1813 |
> |
assertEquals(0, p.getPoolSize()); |
1814 |
|
} finally { |
1815 |
< |
joinPool(tpe); |
1815 |
> |
joinPool(p); |
1816 |
|
} |
1817 |
|
} |
1818 |
|
|
1819 |
|
/** |
1820 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1821 |
|
*/ |
1822 |
< |
public void testAllowCoreThreadTimeOut_false() { |
1823 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1824 |
< |
tpe.allowCoreThreadTimeOut(false); |
1825 |
< |
tpe.execute(new NoOpRunnable()); |
1826 |
< |
try { |
1827 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1828 |
< |
assertTrue(tpe.getPoolSize() >= 1); |
1829 |
< |
} catch (InterruptedException e) { |
1830 |
< |
unexpectedException(); |
1822 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1823 |
> |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1824 |
> |
final ThreadPoolExecutor p = |
1825 |
> |
new CustomTPE(2, 10, |
1826 |
> |
coreThreadTimeOut, MILLISECONDS, |
1827 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1828 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1829 |
> |
try { |
1830 |
> |
p.allowCoreThreadTimeOut(false); |
1831 |
> |
p.execute(new CheckedRunnable() { |
1832 |
> |
public void realRun() throws InterruptedException { |
1833 |
> |
threadStarted.countDown(); |
1834 |
> |
assertTrue(p.getPoolSize() >= 1); |
1835 |
> |
}}); |
1836 |
> |
delay(2 * coreThreadTimeOut); |
1837 |
> |
assertTrue(p.getPoolSize() >= 1); |
1838 |
|
} finally { |
1839 |
< |
joinPool(tpe); |
1839 |
> |
joinPool(p); |
1840 |
|
} |
1841 |
|
} |
1842 |
|
|