17 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
18 |
|
} |
19 |
|
|
20 |
+ |
static class ExtendedTPE extends ThreadPoolExecutor { |
21 |
+ |
volatile boolean beforeCalled = false; |
22 |
+ |
volatile boolean afterCalled = false; |
23 |
+ |
volatile boolean terminatedCalled = false; |
24 |
+ |
public ExtendedTPE() { |
25 |
+ |
super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); |
26 |
+ |
} |
27 |
+ |
protected void beforeExecute(Thread t, Runnable r) { |
28 |
+ |
beforeCalled = true; |
29 |
+ |
} |
30 |
+ |
protected void afterExecute(Runnable r, Throwable t) { |
31 |
+ |
afterCalled = true; |
32 |
+ |
} |
33 |
+ |
protected void terminated() { |
34 |
+ |
terminatedCalled = true; |
35 |
+ |
} |
36 |
+ |
} |
37 |
|
|
38 |
|
/** |
39 |
< |
* execute successfully executes a runnable |
39 |
> |
* execute successfully executes a runnable |
40 |
|
*/ |
41 |
|
public void testExecute() { |
42 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
42 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
43 |
|
try { |
44 |
|
p1.execute(new Runnable() { |
45 |
|
public void run() { |
62 |
|
* thread becomes active |
63 |
|
*/ |
64 |
|
public void testGetActiveCount() { |
65 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
65 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
66 |
|
assertEquals(0, p2.getActiveCount()); |
67 |
|
p2.execute(new MediumRunnable()); |
68 |
|
try { |
73 |
|
assertEquals(1, p2.getActiveCount()); |
74 |
|
joinPool(p2); |
75 |
|
} |
76 |
+ |
|
77 |
+ |
/** |
78 |
+ |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
79 |
+ |
*/ |
80 |
+ |
public void testPrestartCoreThread() { |
81 |
+ |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
82 |
+ |
assertEquals(0, p2.getPoolSize()); |
83 |
+ |
assertTrue(p2.prestartCoreThread()); |
84 |
+ |
assertEquals(1, p2.getPoolSize()); |
85 |
+ |
assertTrue(p2.prestartCoreThread()); |
86 |
+ |
assertEquals(2, p2.getPoolSize()); |
87 |
+ |
assertFalse(p2.prestartCoreThread()); |
88 |
+ |
assertEquals(2, p2.getPoolSize()); |
89 |
+ |
joinPool(p2); |
90 |
+ |
} |
91 |
+ |
|
92 |
+ |
/** |
93 |
+ |
* prestartAllCoreThreads starts all corePoolSize threads |
94 |
+ |
*/ |
95 |
+ |
public void testPrestartAllCoreThreads() { |
96 |
+ |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
97 |
+ |
assertEquals(0, p2.getPoolSize()); |
98 |
+ |
p2.prestartAllCoreThreads(); |
99 |
+ |
assertEquals(2, p2.getPoolSize()); |
100 |
+ |
p2.prestartAllCoreThreads(); |
101 |
+ |
assertEquals(2, p2.getPoolSize()); |
102 |
+ |
joinPool(p2); |
103 |
+ |
} |
104 |
|
|
105 |
|
/** |
106 |
|
* getCompletedTaskCount increases, but doesn't overestimate, |
107 |
|
* when tasks complete |
108 |
|
*/ |
109 |
|
public void testGetCompletedTaskCount() { |
110 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
110 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
111 |
|
assertEquals(0, p2.getCompletedTaskCount()); |
112 |
|
p2.execute(new ShortRunnable()); |
113 |
|
try { |
114 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
114 |
> |
Thread.sleep(SMALL_DELAY_MS); |
115 |
|
} catch(Exception e){ |
116 |
|
unexpectedException(); |
117 |
|
} |
124 |
|
* getCorePoolSize returns size given in constructor if not otherwise set |
125 |
|
*/ |
126 |
|
public void testGetCorePoolSize() { |
127 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
127 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
128 |
|
assertEquals(1, p1.getCorePoolSize()); |
129 |
|
joinPool(p1); |
130 |
|
} |
137 |
|
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
138 |
|
joinPool(p2); |
139 |
|
} |
140 |
+ |
|
141 |
+ |
|
142 |
+ |
/** |
143 |
+ |
* getThreadFactory returns factory in constructor if not set |
144 |
+ |
*/ |
145 |
+ |
public void testGetThreadFactory() { |
146 |
+ |
ThreadFactory tf = new SimpleThreadFactory(); |
147 |
+ |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
148 |
+ |
assertSame(tf, p.getThreadFactory()); |
149 |
+ |
p.shutdown(); |
150 |
+ |
joinPool(p); |
151 |
+ |
} |
152 |
+ |
|
153 |
+ |
/** |
154 |
+ |
* setThreadFactory sets the thread factory returned by getThreadFactory |
155 |
+ |
*/ |
156 |
+ |
public void testSetThreadFactory() { |
157 |
+ |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
158 |
+ |
ThreadFactory tf = new SimpleThreadFactory(); |
159 |
+ |
p.setThreadFactory(tf); |
160 |
+ |
assertSame(tf, p.getThreadFactory()); |
161 |
+ |
p.shutdown(); |
162 |
+ |
joinPool(p); |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
|
166 |
+ |
/** |
167 |
+ |
* setThreadFactory(null) throws NPE |
168 |
+ |
*/ |
169 |
+ |
public void testSetThreadFactoryNull() { |
170 |
+ |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
171 |
+ |
try { |
172 |
+ |
p.setThreadFactory(null); |
173 |
+ |
shouldThrow(); |
174 |
+ |
} catch (NullPointerException success) { |
175 |
+ |
} finally { |
176 |
+ |
joinPool(p); |
177 |
+ |
} |
178 |
+ |
} |
179 |
+ |
|
180 |
|
|
181 |
|
/** |
182 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
183 |
|
* multiple threads active |
184 |
|
*/ |
185 |
|
public void testGetLargestPoolSize() { |
186 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
186 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
187 |
|
try { |
188 |
|
assertEquals(0, p2.getLargestPoolSize()); |
189 |
|
p2.execute(new MediumRunnable()); |
201 |
|
* otherwise set |
202 |
|
*/ |
203 |
|
public void testGetMaximumPoolSize() { |
204 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
204 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
205 |
|
assertEquals(2, p2.getMaximumPoolSize()); |
206 |
|
joinPool(p2); |
207 |
|
} |
211 |
|
* become active |
212 |
|
*/ |
213 |
|
public void testGetPoolSize() { |
214 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
214 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
215 |
|
assertEquals(0, p1.getPoolSize()); |
216 |
|
p1.execute(new MediumRunnable()); |
217 |
|
assertEquals(1, p1.getPoolSize()); |
219 |
|
} |
220 |
|
|
221 |
|
/** |
222 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
222 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
223 |
|
*/ |
224 |
|
public void testGetTaskCount() { |
225 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
225 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
226 |
|
try { |
227 |
|
assertEquals(0, p1.getTaskCount()); |
228 |
|
p1.execute(new MediumRunnable()); |
239 |
|
*/ |
240 |
|
public void testIsShutdown() { |
241 |
|
|
242 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
242 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
243 |
|
assertFalse(p1.isShutdown()); |
244 |
|
p1.shutdown(); |
245 |
|
assertTrue(p1.isShutdown()); |
251 |
|
* isTerminated is false before termination, true after |
252 |
|
*/ |
253 |
|
public void testIsTerminated() { |
254 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
254 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
255 |
|
assertFalse(p1.isTerminated()); |
256 |
|
try { |
257 |
|
p1.execute(new MediumRunnable()); |
270 |
|
* isTerminating is not true when running or when terminated |
271 |
|
*/ |
272 |
|
public void testIsTerminating() { |
273 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
273 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
274 |
|
assertFalse(p1.isTerminating()); |
275 |
|
try { |
276 |
|
p1.execute(new SmallRunnable()); |
288 |
|
} |
289 |
|
|
290 |
|
/** |
291 |
+ |
* getQueue returns the work queue, which contains queued tasks |
292 |
+ |
*/ |
293 |
+ |
public void testGetQueue() { |
294 |
+ |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
295 |
+ |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
296 |
+ |
CancellableTask[] tasks = new CancellableTask[5]; |
297 |
+ |
for(int i = 0; i < 5; i++){ |
298 |
+ |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
299 |
+ |
p1.execute(tasks[i]); |
300 |
+ |
} |
301 |
+ |
try { |
302 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
303 |
+ |
BlockingQueue<Runnable> wq = p1.getQueue(); |
304 |
+ |
assertSame(q, wq); |
305 |
+ |
assertFalse(wq.contains(tasks[0])); |
306 |
+ |
assertTrue(wq.contains(tasks[4])); |
307 |
+ |
p1.shutdownNow(); |
308 |
+ |
} catch(Exception e) { |
309 |
+ |
unexpectedException(); |
310 |
+ |
} finally { |
311 |
+ |
joinPool(p1); |
312 |
+ |
} |
313 |
+ |
} |
314 |
+ |
|
315 |
+ |
/** |
316 |
+ |
* remove(task) removes queued task, and fails to remove active task |
317 |
+ |
*/ |
318 |
+ |
public void testRemove() { |
319 |
+ |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
320 |
+ |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
321 |
+ |
CancellableTask[] tasks = new CancellableTask[5]; |
322 |
+ |
for(int i = 0; i < 5; i++){ |
323 |
+ |
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
324 |
+ |
p1.execute(tasks[i]); |
325 |
+ |
} |
326 |
+ |
try { |
327 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
328 |
+ |
assertFalse(p1.remove(tasks[0])); |
329 |
+ |
assertTrue(q.contains(tasks[4])); |
330 |
+ |
assertTrue(q.contains(tasks[3])); |
331 |
+ |
assertTrue(p1.remove(tasks[4])); |
332 |
+ |
assertFalse(p1.remove(tasks[4])); |
333 |
+ |
assertFalse(q.contains(tasks[4])); |
334 |
+ |
assertTrue(q.contains(tasks[3])); |
335 |
+ |
assertTrue(p1.remove(tasks[3])); |
336 |
+ |
assertFalse(q.contains(tasks[3])); |
337 |
+ |
p1.shutdownNow(); |
338 |
+ |
} catch(Exception e) { |
339 |
+ |
unexpectedException(); |
340 |
+ |
} finally { |
341 |
+ |
joinPool(p1); |
342 |
+ |
} |
343 |
+ |
} |
344 |
+ |
|
345 |
+ |
/** |
346 |
|
* purge removes cancelled tasks from the queue |
347 |
|
*/ |
348 |
|
public void testPurge() { |
349 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
349 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
350 |
|
CancellableTask[] tasks = new CancellableTask[5]; |
351 |
|
for(int i = 0; i < 5; i++){ |
352 |
|
tasks[i] = new CancellableTask(new MediumPossiblyInterruptedRunnable()); |
357 |
|
p1.purge(); |
358 |
|
long count = p1.getTaskCount(); |
359 |
|
assertTrue(count >= 2 && count < 5); |
360 |
+ |
p1.shutdownNow(); |
361 |
|
joinPool(p1); |
362 |
|
} |
363 |
|
|
365 |
|
* shutDownNow returns a list containing tasks that were not run |
366 |
|
*/ |
367 |
|
public void testShutDownNow() { |
368 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
368 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
369 |
|
List l; |
370 |
|
try { |
371 |
|
for(int i = 0; i < 5; i++) |
439 |
|
/** |
440 |
|
* Constructor throws if workQueue is set to null |
441 |
|
*/ |
442 |
< |
public void testNullPointerException() { |
442 |
> |
public void testConstructorNullPointerException() { |
443 |
|
try { |
444 |
< |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null); |
444 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); |
445 |
|
shouldThrow(); |
446 |
|
} |
447 |
|
catch (NullPointerException success){} |
464 |
|
*/ |
465 |
|
public void testConstructor7() { |
466 |
|
try { |
467 |
< |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
467 |
> |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
468 |
|
shouldThrow(); |
469 |
|
} |
470 |
|
catch (IllegalArgumentException success){} |
475 |
|
*/ |
476 |
|
public void testConstructor8() { |
477 |
|
try { |
478 |
< |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
478 |
> |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
479 |
|
shouldThrow(); |
480 |
|
} |
481 |
|
catch (IllegalArgumentException success){} |
497 |
|
*/ |
498 |
|
public void testConstructor10() { |
499 |
|
try { |
500 |
< |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
500 |
> |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
501 |
|
shouldThrow(); |
502 |
|
} |
503 |
|
catch (IllegalArgumentException success){} |
506 |
|
/** |
507 |
|
* Constructor throws if workQueue is set to null |
508 |
|
*/ |
509 |
< |
public void testNullPointerException2() { |
509 |
> |
public void testConstructorNullPointerException2() { |
510 |
|
try { |
511 |
< |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); |
511 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); |
512 |
|
shouldThrow(); |
513 |
|
} |
514 |
|
catch (NullPointerException success){} |
517 |
|
/** |
518 |
|
* Constructor throws if threadFactory is set to null |
519 |
|
*/ |
520 |
< |
public void testNullPointerException3() { |
520 |
> |
public void testConstructorNullPointerException3() { |
521 |
|
try { |
522 |
|
ThreadFactory f = null; |
523 |
< |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
523 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
524 |
|
shouldThrow(); |
525 |
|
} |
526 |
|
catch (NullPointerException success){} |
532 |
|
*/ |
533 |
|
public void testConstructor11() { |
534 |
|
try { |
535 |
< |
new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
535 |
> |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
536 |
|
shouldThrow(); |
537 |
|
} |
538 |
|
catch (IllegalArgumentException success){} |
543 |
|
*/ |
544 |
|
public void testConstructor12() { |
545 |
|
try { |
546 |
< |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
546 |
> |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
547 |
|
shouldThrow(); |
548 |
|
} |
549 |
|
catch (IllegalArgumentException success){} |
554 |
|
*/ |
555 |
|
public void testConstructor13() { |
556 |
|
try { |
557 |
< |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
557 |
> |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
558 |
|
shouldThrow(); |
559 |
|
} |
560 |
|
catch (IllegalArgumentException success){} |
576 |
|
*/ |
577 |
|
public void testConstructor15() { |
578 |
|
try { |
579 |
< |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
579 |
> |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
580 |
|
shouldThrow(); |
581 |
|
} |
582 |
|
catch (IllegalArgumentException success){} |
585 |
|
/** |
586 |
|
* Constructor throws if workQueue is set to null |
587 |
|
*/ |
588 |
< |
public void testNullPointerException4() { |
588 |
> |
public void testConstructorNullPointerException4() { |
589 |
|
try { |
590 |
< |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); |
590 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); |
591 |
|
shouldThrow(); |
592 |
|
} |
593 |
|
catch (NullPointerException success){} |
596 |
|
/** |
597 |
|
* Constructor throws if handler is set to null |
598 |
|
*/ |
599 |
< |
public void testNullPointerException5() { |
599 |
> |
public void testConstructorNullPointerException5() { |
600 |
|
try { |
601 |
|
RejectedExecutionHandler r = null; |
602 |
< |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
602 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
603 |
|
shouldThrow(); |
604 |
|
} |
605 |
|
catch (NullPointerException success){} |
611 |
|
*/ |
612 |
|
public void testConstructor16() { |
613 |
|
try { |
614 |
< |
new ThreadPoolExecutor(-1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
614 |
> |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
615 |
|
shouldThrow(); |
616 |
|
} |
617 |
|
catch (IllegalArgumentException success){} |
622 |
|
*/ |
623 |
|
public void testConstructor17() { |
624 |
|
try { |
625 |
< |
new ThreadPoolExecutor(1,-1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
625 |
> |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
626 |
|
shouldThrow(); |
627 |
|
} |
628 |
|
catch (IllegalArgumentException success){} |
633 |
|
*/ |
634 |
|
public void testConstructor18() { |
635 |
|
try { |
636 |
< |
new ThreadPoolExecutor(1,0,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
636 |
> |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
637 |
|
shouldThrow(); |
638 |
|
} |
639 |
|
catch (IllegalArgumentException success){} |
655 |
|
*/ |
656 |
|
public void testConstructor20() { |
657 |
|
try { |
658 |
< |
new ThreadPoolExecutor(2,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
658 |
> |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
659 |
|
shouldThrow(); |
660 |
|
} |
661 |
|
catch (IllegalArgumentException success){} |
664 |
|
/** |
665 |
|
* Constructor throws if workQueue is set to null |
666 |
|
*/ |
667 |
< |
public void testNullPointerException6() { |
667 |
> |
public void testConstructorNullPointerException6() { |
668 |
|
try { |
669 |
< |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
669 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
670 |
|
shouldThrow(); |
671 |
|
} |
672 |
|
catch (NullPointerException success){} |
675 |
|
/** |
676 |
|
* Constructor throws if handler is set to null |
677 |
|
*/ |
678 |
< |
public void testNullPointerException7() { |
678 |
> |
public void testConstructorNullPointerException7() { |
679 |
|
try { |
680 |
|
RejectedExecutionHandler r = null; |
681 |
< |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
681 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
682 |
|
shouldThrow(); |
683 |
|
} |
684 |
|
catch (NullPointerException success){} |
687 |
|
/** |
688 |
|
* Constructor throws if ThreadFactory is set top null |
689 |
|
*/ |
690 |
< |
public void testNullPointerException8() { |
690 |
> |
public void testConstructorNullPointerException8() { |
691 |
|
try { |
692 |
|
ThreadFactory f = null; |
693 |
< |
new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
693 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
694 |
|
shouldThrow(); |
695 |
|
} |
696 |
|
catch (NullPointerException successdn8){} |
698 |
|
|
699 |
|
|
700 |
|
/** |
701 |
< |
* execute throws RejectedExecutionException if shutdown |
701 |
> |
* execute throws RejectedExecutionException |
702 |
> |
* if saturated. |
703 |
|
*/ |
704 |
< |
public void testRejectedExecutionException() { |
705 |
< |
ThreadPoolExecutor tpe = null; |
704 |
> |
public void testSaturatedExecute() { |
705 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
706 |
|
try { |
707 |
< |
tpe = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
708 |
< |
} catch(Exception e){} |
707 |
> |
|
708 |
> |
for(int i = 0; i < 5; ++i){ |
709 |
> |
p.execute(new MediumRunnable()); |
710 |
> |
} |
711 |
> |
shouldThrow(); |
712 |
> |
} catch(RejectedExecutionException success){} |
713 |
> |
joinPool(p); |
714 |
> |
} |
715 |
> |
|
716 |
> |
/** |
717 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
718 |
> |
*/ |
719 |
> |
public void testSaturatedExecute2() { |
720 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
721 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
722 |
> |
try { |
723 |
> |
|
724 |
> |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
725 |
> |
for(int i = 0; i < 5; ++i){ |
726 |
> |
tasks[i] = new TrackedNoOpRunnable(); |
727 |
> |
} |
728 |
> |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
729 |
> |
p.execute(mr); |
730 |
> |
for(int i = 0; i < 5; ++i){ |
731 |
> |
p.execute(tasks[i]); |
732 |
> |
} |
733 |
> |
for(int i = 1; i < 5; ++i) { |
734 |
> |
assertTrue(tasks[i].done); |
735 |
> |
} |
736 |
> |
p.shutdownNow(); |
737 |
> |
} catch(RejectedExecutionException ex){ |
738 |
> |
unexpectedException(); |
739 |
> |
} finally { |
740 |
> |
joinPool(p); |
741 |
> |
} |
742 |
> |
} |
743 |
> |
|
744 |
> |
/** |
745 |
> |
* executor using DiscardPolicy drops task if saturated. |
746 |
> |
*/ |
747 |
> |
public void testSaturatedExecute3() { |
748 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
749 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
750 |
> |
try { |
751 |
> |
|
752 |
> |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
753 |
> |
for(int i = 0; i < 5; ++i){ |
754 |
> |
tasks[i] = new TrackedNoOpRunnable(); |
755 |
> |
} |
756 |
> |
p.execute(new TrackedLongRunnable()); |
757 |
> |
for(int i = 0; i < 5; ++i){ |
758 |
> |
p.execute(tasks[i]); |
759 |
> |
} |
760 |
> |
for(int i = 0; i < 5; ++i){ |
761 |
> |
assertFalse(tasks[i].done); |
762 |
> |
} |
763 |
> |
p.shutdownNow(); |
764 |
> |
} catch(RejectedExecutionException ex){ |
765 |
> |
unexpectedException(); |
766 |
> |
} finally { |
767 |
> |
joinPool(p); |
768 |
> |
} |
769 |
> |
} |
770 |
> |
|
771 |
> |
/** |
772 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
773 |
> |
*/ |
774 |
> |
public void testSaturatedExecute4() { |
775 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
776 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
777 |
> |
try { |
778 |
> |
p.execute(new TrackedLongRunnable()); |
779 |
> |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
780 |
> |
p.execute(r2); |
781 |
> |
assertTrue(p.getQueue().contains(r2)); |
782 |
> |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
783 |
> |
p.execute(r3); |
784 |
> |
assertFalse(p.getQueue().contains(r2)); |
785 |
> |
assertTrue(p.getQueue().contains(r3)); |
786 |
> |
p.shutdownNow(); |
787 |
> |
} catch(RejectedExecutionException ex){ |
788 |
> |
unexpectedException(); |
789 |
> |
} finally { |
790 |
> |
joinPool(p); |
791 |
> |
} |
792 |
> |
} |
793 |
> |
|
794 |
> |
/** |
795 |
> |
* execute throws RejectedExecutionException if shutdown |
796 |
> |
*/ |
797 |
> |
public void testRejectedExecutionExceptionOnShutdown() { |
798 |
> |
ThreadPoolExecutor tpe = |
799 |
> |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
800 |
|
tpe.shutdown(); |
801 |
|
try { |
802 |
|
tpe.execute(new NoOpRunnable()); |
807 |
|
} |
808 |
|
|
809 |
|
/** |
810 |
< |
* execute throws RejectedExecutionException |
578 |
< |
* if saturated. |
810 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
811 |
|
*/ |
812 |
< |
public void testSaturatedExecute() { |
813 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
814 |
< |
try { |
815 |
< |
|
816 |
< |
for(int i = 0; i < 5; ++i){ |
817 |
< |
p.execute(new MediumRunnable()); |
818 |
< |
} |
819 |
< |
shouldThrow(); |
820 |
< |
} catch(RejectedExecutionException success){} |
821 |
< |
joinPool(p); |
812 |
> |
public void testCallerRunsOnShutdown() { |
813 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
814 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
815 |
> |
|
816 |
> |
p.shutdown(); |
817 |
> |
try { |
818 |
> |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
819 |
> |
p.execute(r); |
820 |
> |
assertFalse(r.done); |
821 |
> |
} catch(RejectedExecutionException success){ |
822 |
> |
unexpectedException(); |
823 |
> |
} finally { |
824 |
> |
joinPool(p); |
825 |
> |
} |
826 |
> |
} |
827 |
> |
|
828 |
> |
/** |
829 |
> |
* execute using DiscardPolicy drops task on shutdown |
830 |
> |
*/ |
831 |
> |
public void testDiscardOnShutdown() { |
832 |
> |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
833 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
834 |
> |
|
835 |
> |
p.shutdown(); |
836 |
> |
try { |
837 |
> |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
838 |
> |
p.execute(r); |
839 |
> |
assertFalse(r.done); |
840 |
> |
} catch(RejectedExecutionException success){ |
841 |
> |
unexpectedException(); |
842 |
> |
} finally { |
843 |
> |
joinPool(p); |
844 |
> |
} |
845 |
|
} |
846 |
|
|
847 |
+ |
|
848 |
+ |
/** |
849 |
+ |
* execute using DiscardOldestPolicy drops task on shutdown |
850 |
+ |
*/ |
851 |
+ |
public void testDiscardOldestOnShutdown() { |
852 |
+ |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
853 |
+ |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
854 |
+ |
|
855 |
+ |
p.shutdown(); |
856 |
+ |
try { |
857 |
+ |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
858 |
+ |
p.execute(r); |
859 |
+ |
assertFalse(r.done); |
860 |
+ |
} catch(RejectedExecutionException success){ |
861 |
+ |
unexpectedException(); |
862 |
+ |
} finally { |
863 |
+ |
joinPool(p); |
864 |
+ |
} |
865 |
+ |
} |
866 |
+ |
|
867 |
+ |
|
868 |
|
/** |
869 |
|
* execute (null) throws NPE |
870 |
|
*/ |
871 |
|
public void testExecuteNull() { |
872 |
|
ThreadPoolExecutor tpe = null; |
873 |
|
try { |
874 |
< |
tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
874 |
> |
tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
875 |
|
tpe.execute(null); |
876 |
|
shouldThrow(); |
877 |
|
} catch(NullPointerException success){} |
880 |
|
} |
881 |
|
|
882 |
|
/** |
883 |
< |
* setCorePoolSize of netaitev value throws IllegalArgumentException |
883 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
884 |
|
*/ |
885 |
|
public void testCorePoolSizeIllegalArgumentException() { |
886 |
|
ThreadPoolExecutor tpe = null; |
887 |
|
try { |
888 |
< |
tpe = new ThreadPoolExecutor(1,2,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
888 |
> |
tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
889 |
|
} catch(Exception e){} |
890 |
|
try { |
891 |
|
tpe.setCorePoolSize(-1); |
904 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
905 |
|
ThreadPoolExecutor tpe = null; |
906 |
|
try { |
907 |
< |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
907 |
> |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
908 |
|
} catch(Exception e){} |
909 |
|
try { |
910 |
|
tpe.setMaximumPoolSize(1); |
923 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
924 |
|
ThreadPoolExecutor tpe = null; |
925 |
|
try { |
926 |
< |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
926 |
> |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
927 |
|
} catch(Exception e){} |
928 |
|
try { |
929 |
|
tpe.setMaximumPoolSize(-1); |
943 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
944 |
|
ThreadPoolExecutor tpe = null; |
945 |
|
try { |
946 |
< |
tpe = new ThreadPoolExecutor(2,3,SHORT_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
946 |
> |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
947 |
|
} catch(Exception e){} |
948 |
|
|
949 |
|
try { |
955 |
|
} |
956 |
|
joinPool(tpe); |
957 |
|
} |
958 |
< |
|
958 |
> |
|
959 |
> |
/** |
960 |
> |
* terminated() is called on termination |
961 |
> |
*/ |
962 |
> |
public void testTerminated() { |
963 |
> |
ExtendedTPE tpe = new ExtendedTPE(); |
964 |
> |
tpe.shutdown(); |
965 |
> |
assertTrue(tpe.terminatedCalled); |
966 |
> |
joinPool(tpe); |
967 |
> |
} |
968 |
> |
|
969 |
> |
/** |
970 |
> |
* beforeExecute and afterExecute are called when executing task |
971 |
> |
*/ |
972 |
> |
public void testBeforeAfter() { |
973 |
> |
ExtendedTPE tpe = new ExtendedTPE(); |
974 |
> |
try { |
975 |
> |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
976 |
> |
tpe.execute(r); |
977 |
> |
Thread.sleep(SHORT_DELAY_MS); |
978 |
> |
assertTrue(r.done); |
979 |
> |
assertTrue(tpe.beforeCalled); |
980 |
> |
assertTrue(tpe.afterCalled); |
981 |
> |
tpe.shutdown(); |
982 |
> |
} |
983 |
> |
catch(Exception ex) { |
984 |
> |
unexpectedException(); |
985 |
> |
} finally { |
986 |
> |
joinPool(tpe); |
987 |
> |
} |
988 |
> |
} |
989 |
|
} |