7 |
|
*/ |
8 |
|
|
9 |
|
import java.util.concurrent.*; |
10 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
|
import java.util.concurrent.atomic.*; |
12 |
|
import junit.framework.*; |
13 |
|
import java.util.*; |
14 |
|
|
15 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
25 |
|
volatile boolean afterCalled = false; |
26 |
|
volatile boolean terminatedCalled = false; |
27 |
|
public ExtendedTPE() { |
28 |
< |
super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); |
28 |
> |
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
29 |
|
} |
30 |
|
protected void beforeExecute(Thread t, Runnable r) { |
31 |
|
beforeCalled = true; |
48 |
|
|
49 |
|
|
50 |
|
/** |
51 |
< |
* execute successfully executes a runnable |
51 |
> |
* execute successfully executes a runnable |
52 |
|
*/ |
53 |
< |
public void testExecute() { |
54 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
53 |
> |
public void testExecute() throws InterruptedException { |
54 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
55 |
|
try { |
56 |
< |
p1.execute(new Runnable() { |
57 |
< |
public void run() { |
58 |
< |
try { |
59 |
< |
Thread.sleep(SHORT_DELAY_MS); |
59 |
< |
} catch (InterruptedException e) { |
60 |
< |
threadUnexpectedException(); |
61 |
< |
} |
62 |
< |
} |
63 |
< |
}); |
64 |
< |
Thread.sleep(SMALL_DELAY_MS); |
65 |
< |
} catch (InterruptedException e) { |
66 |
< |
unexpectedException(); |
56 |
> |
p1.execute(new ShortRunnable()); |
57 |
> |
Thread.sleep(SMALL_DELAY_MS); |
58 |
> |
} finally { |
59 |
> |
joinPool(p1); |
60 |
|
} |
68 |
– |
joinPool(p1); |
61 |
|
} |
62 |
|
|
63 |
|
/** |
64 |
< |
* getActiveCount increases but doesn't overestimate, when a |
65 |
< |
* thread becomes active |
64 |
> |
* getActiveCount increases but doesn't overestimate, when a |
65 |
> |
* thread becomes active |
66 |
|
*/ |
67 |
< |
public void testGetActiveCount() { |
68 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
67 |
> |
public void testGetActiveCount() throws InterruptedException { |
68 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
69 |
|
assertEquals(0, p2.getActiveCount()); |
70 |
|
p2.execute(new MediumRunnable()); |
71 |
< |
try { |
80 |
< |
Thread.sleep(SHORT_DELAY_MS); |
81 |
< |
} catch (Exception e) { |
82 |
< |
unexpectedException(); |
83 |
< |
} |
71 |
> |
Thread.sleep(SHORT_DELAY_MS); |
72 |
|
assertEquals(1, p2.getActiveCount()); |
73 |
|
joinPool(p2); |
74 |
|
} |
75 |
|
|
76 |
|
/** |
77 |
< |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
77 |
> |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
78 |
|
*/ |
79 |
|
public void testPrestartCoreThread() { |
80 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
80 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
81 |
|
assertEquals(0, p2.getPoolSize()); |
82 |
|
assertTrue(p2.prestartCoreThread()); |
83 |
|
assertEquals(1, p2.getPoolSize()); |
89 |
|
} |
90 |
|
|
91 |
|
/** |
92 |
< |
* prestartAllCoreThreads starts all corePoolSize threads |
92 |
> |
* prestartAllCoreThreads starts all corePoolSize threads |
93 |
|
*/ |
94 |
|
public void testPrestartAllCoreThreads() { |
95 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
95 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
96 |
|
assertEquals(0, p2.getPoolSize()); |
97 |
|
p2.prestartAllCoreThreads(); |
98 |
|
assertEquals(2, p2.getPoolSize()); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
106 |
< |
* when tasks complete |
105 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
106 |
> |
* when tasks complete |
107 |
|
*/ |
108 |
< |
public void testGetCompletedTaskCount() { |
109 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
108 |
> |
public void testGetCompletedTaskCount() throws InterruptedException { |
109 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
110 |
|
assertEquals(0, p2.getCompletedTaskCount()); |
111 |
|
p2.execute(new ShortRunnable()); |
112 |
< |
try { |
125 |
< |
Thread.sleep(SMALL_DELAY_MS); |
126 |
< |
} catch (Exception e) { |
127 |
< |
unexpectedException(); |
128 |
< |
} |
112 |
> |
Thread.sleep(SMALL_DELAY_MS); |
113 |
|
assertEquals(1, p2.getCompletedTaskCount()); |
114 |
|
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
115 |
|
joinPool(p2); |
116 |
|
} |
117 |
|
|
118 |
|
/** |
119 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
119 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
120 |
|
*/ |
121 |
|
public void testGetCorePoolSize() { |
122 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
122 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
123 |
|
assertEquals(1, p1.getCorePoolSize()); |
124 |
|
joinPool(p1); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* getKeepAliveTime returns value given in constructor if not otherwise set |
128 |
> |
* getKeepAliveTime returns value given in constructor if not otherwise set |
129 |
|
*/ |
130 |
|
public void testGetKeepAliveTime() { |
131 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
131 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
132 |
|
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
133 |
|
joinPool(p2); |
134 |
|
} |
139 |
|
*/ |
140 |
|
public void testGetThreadFactory() { |
141 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
142 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
142 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
143 |
|
assertSame(tf, p.getThreadFactory()); |
144 |
|
joinPool(p); |
145 |
|
} |
148 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
149 |
|
*/ |
150 |
|
public void testSetThreadFactory() { |
151 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
151 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
152 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
153 |
|
p.setThreadFactory(tf); |
154 |
|
assertSame(tf, p.getThreadFactory()); |
160 |
|
* setThreadFactory(null) throws NPE |
161 |
|
*/ |
162 |
|
public void testSetThreadFactoryNull() { |
163 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
163 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
164 |
|
try { |
165 |
|
p.setThreadFactory(null); |
166 |
|
shouldThrow(); |
175 |
|
*/ |
176 |
|
public void testGetRejectedExecutionHandler() { |
177 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
178 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
178 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
179 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
180 |
|
joinPool(p); |
181 |
|
} |
185 |
|
* getRejectedExecutionHandler |
186 |
|
*/ |
187 |
|
public void testSetRejectedExecutionHandler() { |
188 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
188 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
189 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
190 |
|
p.setRejectedExecutionHandler(h); |
191 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
197 |
|
* setRejectedExecutionHandler(null) throws NPE |
198 |
|
*/ |
199 |
|
public void testSetRejectedExecutionHandlerNull() { |
200 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
200 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
201 |
|
try { |
202 |
|
p.setRejectedExecutionHandler(null); |
203 |
|
shouldThrow(); |
209 |
|
|
210 |
|
|
211 |
|
/** |
212 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
213 |
< |
* multiple threads active |
212 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
213 |
> |
* multiple threads active |
214 |
|
*/ |
215 |
< |
public void testGetLargestPoolSize() { |
216 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
217 |
< |
try { |
218 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
219 |
< |
p2.execute(new MediumRunnable()); |
220 |
< |
p2.execute(new MediumRunnable()); |
221 |
< |
Thread.sleep(SHORT_DELAY_MS); |
238 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
239 |
< |
} catch (Exception e) { |
240 |
< |
unexpectedException(); |
241 |
< |
} |
215 |
> |
public void testGetLargestPoolSize() throws InterruptedException { |
216 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
217 |
> |
assertEquals(0, p2.getLargestPoolSize()); |
218 |
> |
p2.execute(new MediumRunnable()); |
219 |
> |
p2.execute(new MediumRunnable()); |
220 |
> |
Thread.sleep(SHORT_DELAY_MS); |
221 |
> |
assertEquals(2, p2.getLargestPoolSize()); |
222 |
|
joinPool(p2); |
223 |
|
} |
224 |
|
|
225 |
|
/** |
226 |
< |
* getMaximumPoolSize returns value given in constructor if not |
227 |
< |
* otherwise set |
226 |
> |
* getMaximumPoolSize returns value given in constructor if not |
227 |
> |
* otherwise set |
228 |
|
*/ |
229 |
|
public void testGetMaximumPoolSize() { |
230 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
230 |
> |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
231 |
|
assertEquals(2, p2.getMaximumPoolSize()); |
232 |
|
joinPool(p2); |
233 |
|
} |
234 |
|
|
235 |
|
/** |
236 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
237 |
< |
* become active |
236 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
237 |
> |
* become active |
238 |
|
*/ |
239 |
|
public void testGetPoolSize() { |
240 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
240 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
241 |
|
assertEquals(0, p1.getPoolSize()); |
242 |
|
p1.execute(new MediumRunnable()); |
243 |
|
assertEquals(1, p1.getPoolSize()); |
245 |
|
} |
246 |
|
|
247 |
|
/** |
248 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
248 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
249 |
|
*/ |
250 |
< |
public void testGetTaskCount() { |
251 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
252 |
< |
try { |
253 |
< |
assertEquals(0, p1.getTaskCount()); |
254 |
< |
p1.execute(new MediumRunnable()); |
255 |
< |
Thread.sleep(SHORT_DELAY_MS); |
276 |
< |
assertEquals(1, p1.getTaskCount()); |
277 |
< |
} catch (Exception e) { |
278 |
< |
unexpectedException(); |
279 |
< |
} |
250 |
> |
public void testGetTaskCount() throws InterruptedException { |
251 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
252 |
> |
assertEquals(0, p1.getTaskCount()); |
253 |
> |
p1.execute(new MediumRunnable()); |
254 |
> |
Thread.sleep(SHORT_DELAY_MS); |
255 |
> |
assertEquals(1, p1.getTaskCount()); |
256 |
|
joinPool(p1); |
257 |
|
} |
258 |
|
|
259 |
|
/** |
260 |
< |
* isShutDown is false before shutdown, true after |
260 |
> |
* isShutDown is false before shutdown, true after |
261 |
|
*/ |
262 |
|
public void testIsShutdown() { |
263 |
|
|
264 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
264 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
265 |
|
assertFalse(p1.isShutdown()); |
266 |
|
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
267 |
< |
assertTrue(p1.isShutdown()); |
267 |
> |
assertTrue(p1.isShutdown()); |
268 |
|
joinPool(p1); |
269 |
|
} |
270 |
|
|
271 |
|
|
272 |
|
/** |
273 |
< |
* isTerminated is false before termination, true after |
273 |
> |
* isTerminated is false before termination, true after |
274 |
|
*/ |
275 |
< |
public void testIsTerminated() { |
276 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
275 |
> |
public void testIsTerminated() throws InterruptedException { |
276 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
277 |
|
assertFalse(p1.isTerminated()); |
278 |
|
try { |
279 |
|
p1.execute(new MediumRunnable()); |
280 |
|
} finally { |
281 |
|
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
282 |
|
} |
283 |
< |
try { |
284 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
309 |
< |
assertTrue(p1.isTerminated()); |
310 |
< |
} catch (Exception e) { |
311 |
< |
unexpectedException(); |
312 |
< |
} |
283 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
284 |
> |
assertTrue(p1.isTerminated()); |
285 |
|
} |
286 |
|
|
287 |
|
/** |
288 |
< |
* isTerminating is not true when running or when terminated |
288 |
> |
* isTerminating is not true when running or when terminated |
289 |
|
*/ |
290 |
< |
public void testIsTerminating() { |
291 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
290 |
> |
public void testIsTerminating() throws InterruptedException { |
291 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
292 |
|
assertFalse(p1.isTerminating()); |
293 |
|
try { |
294 |
|
p1.execute(new SmallRunnable()); |
296 |
|
} finally { |
297 |
|
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
298 |
|
} |
299 |
< |
try { |
300 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
301 |
< |
assertTrue(p1.isTerminated()); |
330 |
< |
assertFalse(p1.isTerminating()); |
331 |
< |
} catch (Exception e) { |
332 |
< |
unexpectedException(); |
333 |
< |
} |
299 |
> |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
300 |
> |
assertTrue(p1.isTerminated()); |
301 |
> |
assertFalse(p1.isTerminating()); |
302 |
|
} |
303 |
|
|
304 |
|
/** |
305 |
|
* getQueue returns the work queue, which contains queued tasks |
306 |
|
*/ |
307 |
< |
public void testGetQueue() { |
307 |
> |
public void testGetQueue() throws InterruptedException { |
308 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
309 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
309 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
310 |
|
FutureTask[] tasks = new FutureTask[5]; |
311 |
|
for (int i = 0; i < 5; i++) { |
312 |
|
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
321 |
|
for (int i = 1; i < 5; ++i) |
322 |
|
tasks[i].cancel(true); |
323 |
|
p1.shutdownNow(); |
356 |
– |
} catch (Exception e) { |
357 |
– |
unexpectedException(); |
324 |
|
} finally { |
325 |
|
joinPool(p1); |
326 |
|
} |
329 |
|
/** |
330 |
|
* remove(task) removes queued task, and fails to remove active task |
331 |
|
*/ |
332 |
< |
public void testRemove() { |
332 |
> |
public void testRemove() throws InterruptedException { |
333 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
334 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
334 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
335 |
|
FutureTask[] tasks = new FutureTask[5]; |
336 |
|
for (int i = 0; i < 5; i++) { |
337 |
|
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
348 |
|
assertTrue(q.contains(tasks[3])); |
349 |
|
assertTrue(p1.remove(tasks[3])); |
350 |
|
assertFalse(q.contains(tasks[3])); |
385 |
– |
} catch (Exception e) { |
386 |
– |
unexpectedException(); |
351 |
|
} finally { |
352 |
|
joinPool(p1); |
353 |
|
} |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
< |
* purge removes cancelled tasks from the queue |
357 |
> |
* purge removes cancelled tasks from the queue |
358 |
|
*/ |
359 |
|
public void testPurge() { |
360 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
360 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
361 |
|
FutureTask[] tasks = new FutureTask[5]; |
362 |
|
for (int i = 0; i < 5; i++) { |
363 |
|
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
372 |
|
} |
373 |
|
|
374 |
|
/** |
375 |
< |
* shutDownNow returns a list containing tasks that were not run |
375 |
> |
* shutDownNow returns a list containing tasks that were not run |
376 |
|
*/ |
377 |
|
public void testShutDownNow() { |
378 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
378 |
> |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
379 |
|
List l; |
380 |
|
try { |
381 |
|
for (int i = 0; i < 5; i++) |
385 |
|
try { |
386 |
|
l = p1.shutdownNow(); |
387 |
|
} catch (SecurityException ok) { return; } |
424 |
– |
|
388 |
|
} |
389 |
< |
assertTrue(p1.isShutdown()); |
390 |
< |
assertTrue(l.size() <= 4); |
389 |
> |
assertTrue(p1.isShutdown()); |
390 |
> |
assertTrue(l.size() <= 4); |
391 |
|
} |
392 |
|
|
393 |
|
// Exception Tests |
398 |
|
*/ |
399 |
|
public void testConstructor1() { |
400 |
|
try { |
401 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
401 |
> |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
402 |
|
shouldThrow(); |
403 |
< |
} |
441 |
< |
catch (IllegalArgumentException success) {} |
403 |
> |
} catch (IllegalArgumentException success) {} |
404 |
|
} |
405 |
|
|
406 |
|
/** |
408 |
|
*/ |
409 |
|
public void testConstructor2() { |
410 |
|
try { |
411 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
411 |
> |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
412 |
|
shouldThrow(); |
413 |
< |
} |
452 |
< |
catch (IllegalArgumentException success) {} |
413 |
> |
} catch (IllegalArgumentException success) {} |
414 |
|
} |
415 |
|
|
416 |
|
/** |
418 |
|
*/ |
419 |
|
public void testConstructor3() { |
420 |
|
try { |
421 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
421 |
> |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
422 |
|
shouldThrow(); |
423 |
< |
} |
463 |
< |
catch (IllegalArgumentException success) {} |
423 |
> |
} catch (IllegalArgumentException success) {} |
424 |
|
} |
425 |
|
|
426 |
|
/** |
428 |
|
*/ |
429 |
|
public void testConstructor4() { |
430 |
|
try { |
431 |
< |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
431 |
> |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
432 |
|
shouldThrow(); |
433 |
< |
} |
474 |
< |
catch (IllegalArgumentException success) {} |
433 |
> |
} catch (IllegalArgumentException success) {} |
434 |
|
} |
435 |
|
|
436 |
|
/** |
438 |
|
*/ |
439 |
|
public void testConstructor5() { |
440 |
|
try { |
441 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
441 |
> |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
442 |
|
shouldThrow(); |
443 |
< |
} |
485 |
< |
catch (IllegalArgumentException success) {} |
443 |
> |
} catch (IllegalArgumentException success) {} |
444 |
|
} |
445 |
|
|
446 |
|
/** |
448 |
|
*/ |
449 |
|
public void testConstructorNullPointerException() { |
450 |
|
try { |
451 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); |
451 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
452 |
|
shouldThrow(); |
453 |
< |
} |
496 |
< |
catch (NullPointerException success) {} |
453 |
> |
} catch (NullPointerException success) {} |
454 |
|
} |
455 |
|
|
456 |
|
|
460 |
|
*/ |
461 |
|
public void testConstructor6() { |
462 |
|
try { |
463 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
463 |
> |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
464 |
|
shouldThrow(); |
465 |
|
} catch (IllegalArgumentException success) {} |
466 |
|
} |
470 |
|
*/ |
471 |
|
public void testConstructor7() { |
472 |
|
try { |
473 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
473 |
> |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
474 |
|
shouldThrow(); |
475 |
< |
} |
519 |
< |
catch (IllegalArgumentException success) {} |
475 |
> |
} catch (IllegalArgumentException success) {} |
476 |
|
} |
477 |
|
|
478 |
|
/** |
480 |
|
*/ |
481 |
|
public void testConstructor8() { |
482 |
|
try { |
483 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
483 |
> |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
484 |
|
shouldThrow(); |
485 |
< |
} |
530 |
< |
catch (IllegalArgumentException success) {} |
485 |
> |
} catch (IllegalArgumentException success) {} |
486 |
|
} |
487 |
|
|
488 |
|
/** |
490 |
|
*/ |
491 |
|
public void testConstructor9() { |
492 |
|
try { |
493 |
< |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
493 |
> |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
494 |
|
shouldThrow(); |
495 |
< |
} |
541 |
< |
catch (IllegalArgumentException success) {} |
495 |
> |
} catch (IllegalArgumentException success) {} |
496 |
|
} |
497 |
|
|
498 |
|
/** |
500 |
|
*/ |
501 |
|
public void testConstructor10() { |
502 |
|
try { |
503 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
503 |
> |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
504 |
|
shouldThrow(); |
505 |
< |
} |
552 |
< |
catch (IllegalArgumentException success) {} |
505 |
> |
} catch (IllegalArgumentException success) {} |
506 |
|
} |
507 |
|
|
508 |
|
/** |
510 |
|
*/ |
511 |
|
public void testConstructorNullPointerException2() { |
512 |
|
try { |
513 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); |
513 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
514 |
|
shouldThrow(); |
515 |
< |
} |
563 |
< |
catch (NullPointerException success) {} |
515 |
> |
} catch (NullPointerException success) {} |
516 |
|
} |
517 |
|
|
518 |
|
/** |
521 |
|
public void testConstructorNullPointerException3() { |
522 |
|
try { |
523 |
|
ThreadFactory f = null; |
524 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
524 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
525 |
|
shouldThrow(); |
526 |
< |
} |
575 |
< |
catch (NullPointerException success) {} |
526 |
> |
} catch (NullPointerException success) {} |
527 |
|
} |
528 |
|
|
529 |
|
|
532 |
|
*/ |
533 |
|
public void testConstructor11() { |
534 |
|
try { |
535 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
535 |
> |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
536 |
|
shouldThrow(); |
537 |
< |
} |
587 |
< |
catch (IllegalArgumentException success) {} |
537 |
> |
} catch (IllegalArgumentException success) {} |
538 |
|
} |
539 |
|
|
540 |
|
/** |
542 |
|
*/ |
543 |
|
public void testConstructor12() { |
544 |
|
try { |
545 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
545 |
> |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
546 |
|
shouldThrow(); |
547 |
< |
} |
598 |
< |
catch (IllegalArgumentException success) {} |
547 |
> |
} catch (IllegalArgumentException success) {} |
548 |
|
} |
549 |
|
|
550 |
|
/** |
552 |
|
*/ |
553 |
|
public void testConstructor13() { |
554 |
|
try { |
555 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
555 |
> |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
556 |
|
shouldThrow(); |
557 |
< |
} |
609 |
< |
catch (IllegalArgumentException success) {} |
557 |
> |
} catch (IllegalArgumentException success) {} |
558 |
|
} |
559 |
|
|
560 |
|
/** |
562 |
|
*/ |
563 |
|
public void testConstructor14() { |
564 |
|
try { |
565 |
< |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
565 |
> |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
566 |
|
shouldThrow(); |
567 |
< |
} |
620 |
< |
catch (IllegalArgumentException success) {} |
567 |
> |
} catch (IllegalArgumentException success) {} |
568 |
|
} |
569 |
|
|
570 |
|
/** |
572 |
|
*/ |
573 |
|
public void testConstructor15() { |
574 |
|
try { |
575 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
575 |
> |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
576 |
|
shouldThrow(); |
577 |
< |
} |
631 |
< |
catch (IllegalArgumentException success) {} |
577 |
> |
} catch (IllegalArgumentException success) {} |
578 |
|
} |
579 |
|
|
580 |
|
/** |
582 |
|
*/ |
583 |
|
public void testConstructorNullPointerException4() { |
584 |
|
try { |
585 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); |
585 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
586 |
|
shouldThrow(); |
587 |
< |
} |
642 |
< |
catch (NullPointerException success) {} |
587 |
> |
} catch (NullPointerException success) {} |
588 |
|
} |
589 |
|
|
590 |
|
/** |
593 |
|
public void testConstructorNullPointerException5() { |
594 |
|
try { |
595 |
|
RejectedExecutionHandler r = null; |
596 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
596 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
597 |
|
shouldThrow(); |
598 |
< |
} |
654 |
< |
catch (NullPointerException success) {} |
598 |
> |
} catch (NullPointerException success) {} |
599 |
|
} |
600 |
|
|
601 |
|
|
604 |
|
*/ |
605 |
|
public void testConstructor16() { |
606 |
|
try { |
607 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
607 |
> |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
608 |
|
shouldThrow(); |
609 |
< |
} |
666 |
< |
catch (IllegalArgumentException success) {} |
609 |
> |
} catch (IllegalArgumentException success) {} |
610 |
|
} |
611 |
|
|
612 |
|
/** |
614 |
|
*/ |
615 |
|
public void testConstructor17() { |
616 |
|
try { |
617 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
617 |
> |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
618 |
|
shouldThrow(); |
619 |
< |
} |
677 |
< |
catch (IllegalArgumentException success) {} |
619 |
> |
} catch (IllegalArgumentException success) {} |
620 |
|
} |
621 |
|
|
622 |
|
/** |
624 |
|
*/ |
625 |
|
public void testConstructor18() { |
626 |
|
try { |
627 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
627 |
> |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
628 |
|
shouldThrow(); |
629 |
< |
} |
688 |
< |
catch (IllegalArgumentException success) {} |
629 |
> |
} catch (IllegalArgumentException success) {} |
630 |
|
} |
631 |
|
|
632 |
|
/** |
634 |
|
*/ |
635 |
|
public void testConstructor19() { |
636 |
|
try { |
637 |
< |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
637 |
> |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
638 |
|
shouldThrow(); |
639 |
< |
} |
699 |
< |
catch (IllegalArgumentException success) {} |
639 |
> |
} catch (IllegalArgumentException success) {} |
640 |
|
} |
641 |
|
|
642 |
|
/** |
644 |
|
*/ |
645 |
|
public void testConstructor20() { |
646 |
|
try { |
647 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
647 |
> |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
648 |
|
shouldThrow(); |
649 |
< |
} |
710 |
< |
catch (IllegalArgumentException success) {} |
649 |
> |
} catch (IllegalArgumentException success) {} |
650 |
|
} |
651 |
|
|
652 |
|
/** |
653 |
< |
* Constructor throws if workQueue is set to null |
653 |
> |
* Constructor throws if workQueue is null |
654 |
|
*/ |
655 |
|
public void testConstructorNullPointerException6() { |
656 |
|
try { |
657 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
657 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
658 |
|
shouldThrow(); |
659 |
< |
} |
721 |
< |
catch (NullPointerException success) {} |
659 |
> |
} catch (NullPointerException success) {} |
660 |
|
} |
661 |
|
|
662 |
|
/** |
663 |
< |
* Constructor throws if handler is set to null |
663 |
> |
* Constructor throws if handler is null |
664 |
|
*/ |
665 |
|
public void testConstructorNullPointerException7() { |
666 |
|
try { |
667 |
|
RejectedExecutionHandler r = null; |
668 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
668 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
669 |
|
shouldThrow(); |
670 |
< |
} |
733 |
< |
catch (NullPointerException success) {} |
670 |
> |
} catch (NullPointerException success) {} |
671 |
|
} |
672 |
|
|
673 |
|
/** |
674 |
< |
* Constructor throws if ThreadFactory is set top null |
674 |
> |
* Constructor throws if ThreadFactory is null |
675 |
|
*/ |
676 |
|
public void testConstructorNullPointerException8() { |
677 |
|
try { |
678 |
|
ThreadFactory f = null; |
679 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
679 |
> |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
680 |
|
shouldThrow(); |
681 |
< |
} |
745 |
< |
catch (NullPointerException successdn8) {} |
681 |
> |
} catch (NullPointerException success) {} |
682 |
|
} |
683 |
|
|
684 |
|
|
685 |
|
/** |
686 |
< |
* execute throws RejectedExecutionException |
751 |
< |
* if saturated. |
686 |
> |
* execute throws RejectedExecutionException if saturated. |
687 |
|
*/ |
688 |
|
public void testSaturatedExecute() { |
689 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
689 |
> |
ThreadPoolExecutor p = |
690 |
> |
new ThreadPoolExecutor(1, 1, |
691 |
> |
LONG_DELAY_MS, MILLISECONDS, |
692 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
693 |
|
try { |
694 |
< |
|
757 |
< |
for (int i = 0; i < 5; ++i) { |
694 |
> |
for (int i = 0; i < 2; ++i) |
695 |
|
p.execute(new MediumRunnable()); |
696 |
+ |
for (int i = 0; i < 2; ++i) { |
697 |
+ |
try { |
698 |
+ |
p.execute(new MediumRunnable()); |
699 |
+ |
shouldThrow(); |
700 |
+ |
} catch (RejectedExecutionException success) {} |
701 |
|
} |
702 |
< |
shouldThrow(); |
703 |
< |
} catch (RejectedExecutionException success) {} |
704 |
< |
joinPool(p); |
702 |
> |
} finally { |
703 |
> |
joinPool(p); |
704 |
> |
} |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
708 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
709 |
|
*/ |
710 |
|
public void testSaturatedExecute2() { |
711 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
712 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
712 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
713 |
|
try { |
714 |
|
|
715 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
725 |
|
assertTrue(tasks[i].done); |
726 |
|
} |
727 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
786 |
– |
} catch (RejectedExecutionException ex) { |
787 |
– |
unexpectedException(); |
728 |
|
} finally { |
729 |
|
joinPool(p); |
730 |
|
} |
731 |
|
} |
732 |
|
|
733 |
|
/** |
734 |
< |
* executor using DiscardPolicy drops task if saturated. |
734 |
> |
* executor using DiscardPolicy drops task if saturated. |
735 |
|
*/ |
736 |
|
public void testSaturatedExecute3() { |
737 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
738 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
738 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
739 |
|
try { |
740 |
|
|
741 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
750 |
|
assertFalse(tasks[i].done); |
751 |
|
} |
752 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
813 |
– |
} catch (RejectedExecutionException ex) { |
814 |
– |
unexpectedException(); |
753 |
|
} finally { |
754 |
|
joinPool(p); |
755 |
|
} |
756 |
|
} |
757 |
|
|
758 |
|
/** |
759 |
< |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
759 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
760 |
|
*/ |
761 |
|
public void testSaturatedExecute4() { |
762 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
763 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
763 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
764 |
|
try { |
765 |
|
p.execute(new TrackedLongRunnable()); |
766 |
|
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
771 |
|
assertFalse(p.getQueue().contains(r2)); |
772 |
|
assertTrue(p.getQueue().contains(r3)); |
773 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
836 |
– |
} catch (RejectedExecutionException ex) { |
837 |
– |
unexpectedException(); |
774 |
|
} finally { |
775 |
|
joinPool(p); |
776 |
|
} |
777 |
|
} |
778 |
|
|
779 |
|
/** |
780 |
< |
* execute throws RejectedExecutionException if shutdown |
780 |
> |
* execute throws RejectedExecutionException if shutdown |
781 |
|
*/ |
782 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
783 |
|
ThreadPoolExecutor tpe = |
784 |
< |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
784 |
> |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
785 |
|
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
786 |
< |
try { |
787 |
< |
tpe.execute(new NoOpRunnable()); |
788 |
< |
shouldThrow(); |
789 |
< |
} catch (RejectedExecutionException success) {} |
786 |
> |
try { |
787 |
> |
tpe.execute(new NoOpRunnable()); |
788 |
> |
shouldThrow(); |
789 |
> |
} catch (RejectedExecutionException success) {} |
790 |
|
|
791 |
< |
joinPool(tpe); |
791 |
> |
joinPool(tpe); |
792 |
|
} |
793 |
|
|
794 |
|
/** |
795 |
< |
* execute using CallerRunsPolicy drops task on shutdown |
795 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
796 |
|
*/ |
797 |
|
public void testCallerRunsOnShutdown() { |
798 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
799 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
799 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
800 |
|
|
801 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
802 |
< |
try { |
802 |
> |
try { |
803 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
804 |
< |
p.execute(r); |
804 |
> |
p.execute(r); |
805 |
|
assertFalse(r.done); |
870 |
– |
} catch (RejectedExecutionException success) { |
871 |
– |
unexpectedException(); |
806 |
|
} finally { |
807 |
|
joinPool(p); |
808 |
|
} |
809 |
|
} |
810 |
|
|
811 |
|
/** |
812 |
< |
* execute using DiscardPolicy drops task on shutdown |
812 |
> |
* execute using DiscardPolicy drops task on shutdown |
813 |
|
*/ |
814 |
|
public void testDiscardOnShutdown() { |
815 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
816 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
816 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
817 |
|
|
818 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
819 |
< |
try { |
819 |
> |
try { |
820 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
821 |
< |
p.execute(r); |
821 |
> |
p.execute(r); |
822 |
|
assertFalse(r.done); |
889 |
– |
} catch (RejectedExecutionException success) { |
890 |
– |
unexpectedException(); |
823 |
|
} finally { |
824 |
|
joinPool(p); |
825 |
|
} |
827 |
|
|
828 |
|
|
829 |
|
/** |
830 |
< |
* execute using DiscardOldestPolicy drops task on shutdown |
830 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
831 |
|
*/ |
832 |
|
public void testDiscardOldestOnShutdown() { |
833 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
834 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
834 |
> |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
835 |
|
|
836 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
837 |
< |
try { |
837 |
> |
try { |
838 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
839 |
< |
p.execute(r); |
839 |
> |
p.execute(r); |
840 |
|
assertFalse(r.done); |
909 |
– |
} catch (RejectedExecutionException success) { |
910 |
– |
unexpectedException(); |
841 |
|
} finally { |
842 |
|
joinPool(p); |
843 |
|
} |
845 |
|
|
846 |
|
|
847 |
|
/** |
848 |
< |
* execute (null) throws NPE |
848 |
> |
* execute(null) throws NPE |
849 |
|
*/ |
850 |
|
public void testExecuteNull() { |
851 |
< |
ThreadPoolExecutor tpe = null; |
851 |
> |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
852 |
|
try { |
853 |
< |
tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
924 |
< |
tpe.execute(null); |
853 |
> |
tpe.execute(null); |
854 |
|
shouldThrow(); |
855 |
< |
} catch (NullPointerException success) {} |
855 |
> |
} catch (NullPointerException success) {} |
856 |
|
|
857 |
< |
joinPool(tpe); |
857 |
> |
joinPool(tpe); |
858 |
|
} |
859 |
|
|
860 |
|
/** |
861 |
< |
* setCorePoolSize of negative value throws IllegalArgumentException |
861 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
862 |
|
*/ |
863 |
|
public void testCorePoolSizeIllegalArgumentException() { |
864 |
< |
ThreadPoolExecutor tpe = null; |
865 |
< |
try { |
866 |
< |
tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
867 |
< |
} catch (Exception e) {} |
868 |
< |
try { |
869 |
< |
tpe.setCorePoolSize(-1); |
870 |
< |
shouldThrow(); |
871 |
< |
} catch (IllegalArgumentException success) { |
864 |
> |
ThreadPoolExecutor tpe = |
865 |
> |
new ThreadPoolExecutor(1, 2, |
866 |
> |
LONG_DELAY_MS, MILLISECONDS, |
867 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
868 |
> |
try { |
869 |
> |
tpe.setCorePoolSize(-1); |
870 |
> |
shouldThrow(); |
871 |
> |
} catch (IllegalArgumentException success) { |
872 |
|
} finally { |
873 |
|
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
874 |
|
} |
876 |
|
} |
877 |
|
|
878 |
|
/** |
879 |
< |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
880 |
< |
* given a value less the core pool size |
879 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
880 |
> |
* given a value less the core pool size |
881 |
|
*/ |
882 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
883 |
< |
ThreadPoolExecutor tpe = null; |
884 |
< |
try { |
885 |
< |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
886 |
< |
} catch (Exception e) {} |
883 |
> |
ThreadPoolExecutor tpe = |
884 |
> |
new ThreadPoolExecutor(2, 3, |
885 |
> |
LONG_DELAY_MS, MILLISECONDS, |
886 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
887 |
|
try { |
888 |
|
tpe.setMaximumPoolSize(1); |
889 |
|
shouldThrow(); |
895 |
|
} |
896 |
|
|
897 |
|
/** |
898 |
< |
* setMaximumPoolSize throws IllegalArgumentException |
899 |
< |
* if given a negative value |
898 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
899 |
> |
* if given a negative value |
900 |
|
*/ |
901 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
902 |
< |
ThreadPoolExecutor tpe = null; |
903 |
< |
try { |
904 |
< |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
905 |
< |
} catch (Exception e) {} |
902 |
> |
ThreadPoolExecutor tpe = |
903 |
> |
new ThreadPoolExecutor(2, 3, |
904 |
> |
LONG_DELAY_MS, MILLISECONDS, |
905 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
906 |
|
try { |
907 |
|
tpe.setMaximumPoolSize(-1); |
908 |
|
shouldThrow(); |
915 |
|
|
916 |
|
|
917 |
|
/** |
918 |
< |
* setKeepAliveTime throws IllegalArgumentException |
919 |
< |
* when given a negative value |
918 |
> |
* setKeepAliveTime throws IllegalArgumentException |
919 |
> |
* when given a negative value |
920 |
|
*/ |
921 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
922 |
< |
ThreadPoolExecutor tpe = null; |
922 |
> |
ThreadPoolExecutor tpe = |
923 |
> |
new ThreadPoolExecutor(2, 3, |
924 |
> |
LONG_DELAY_MS, MILLISECONDS, |
925 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
926 |
|
try { |
927 |
< |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
996 |
< |
} catch (Exception e) {} |
997 |
< |
|
998 |
< |
try { |
999 |
< |
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
927 |
> |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
928 |
|
shouldThrow(); |
929 |
|
} catch (IllegalArgumentException success) { |
930 |
|
} finally { |
946 |
|
/** |
947 |
|
* beforeExecute and afterExecute are called when executing task |
948 |
|
*/ |
949 |
< |
public void testBeforeAfter() { |
949 |
> |
public void testBeforeAfter() throws InterruptedException { |
950 |
|
ExtendedTPE tpe = new ExtendedTPE(); |
951 |
|
try { |
952 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
956 |
|
assertTrue(tpe.beforeCalled); |
957 |
|
assertTrue(tpe.afterCalled); |
958 |
|
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1031 |
– |
} |
1032 |
– |
catch (Exception ex) { |
1033 |
– |
unexpectedException(); |
959 |
|
} finally { |
960 |
|
joinPool(tpe); |
961 |
|
} |
964 |
|
/** |
965 |
|
* completed submit of callable returns result |
966 |
|
*/ |
967 |
< |
public void testSubmitCallable() { |
968 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
967 |
> |
public void testSubmitCallable() throws Exception { |
968 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
969 |
|
try { |
970 |
|
Future<String> future = e.submit(new StringTask()); |
971 |
|
String result = future.get(); |
972 |
|
assertSame(TEST_STRING, result); |
1048 |
– |
} |
1049 |
– |
catch (ExecutionException ex) { |
1050 |
– |
unexpectedException(); |
1051 |
– |
} |
1052 |
– |
catch (InterruptedException ex) { |
1053 |
– |
unexpectedException(); |
973 |
|
} finally { |
974 |
|
joinPool(e); |
975 |
|
} |
978 |
|
/** |
979 |
|
* completed submit of runnable returns successfully |
980 |
|
*/ |
981 |
< |
public void testSubmitRunnable() { |
982 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
981 |
> |
public void testSubmitRunnable() throws Exception { |
982 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
983 |
|
try { |
984 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
985 |
|
future.get(); |
986 |
|
assertTrue(future.isDone()); |
1068 |
– |
} |
1069 |
– |
catch (ExecutionException ex) { |
1070 |
– |
unexpectedException(); |
1071 |
– |
} |
1072 |
– |
catch (InterruptedException ex) { |
1073 |
– |
unexpectedException(); |
987 |
|
} finally { |
988 |
|
joinPool(e); |
989 |
|
} |
992 |
|
/** |
993 |
|
* completed submit of (runnable, result) returns result |
994 |
|
*/ |
995 |
< |
public void testSubmitRunnable2() { |
996 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
995 |
> |
public void testSubmitRunnable2() throws Exception { |
996 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
997 |
|
try { |
998 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
999 |
|
String result = future.get(); |
1000 |
|
assertSame(TEST_STRING, result); |
1088 |
– |
} |
1089 |
– |
catch (ExecutionException ex) { |
1090 |
– |
unexpectedException(); |
1091 |
– |
} |
1092 |
– |
catch (InterruptedException ex) { |
1093 |
– |
unexpectedException(); |
1001 |
|
} finally { |
1002 |
|
joinPool(e); |
1003 |
|
} |
1004 |
|
} |
1005 |
|
|
1006 |
|
|
1100 |
– |
|
1101 |
– |
|
1102 |
– |
|
1007 |
|
/** |
1008 |
|
* invokeAny(null) throws NPE |
1009 |
|
*/ |
1010 |
< |
public void testInvokeAny1() { |
1011 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1010 |
> |
public void testInvokeAny1() throws Exception { |
1011 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1012 |
|
try { |
1013 |
|
e.invokeAny(null); |
1014 |
+ |
shouldThrow(); |
1015 |
|
} catch (NullPointerException success) { |
1111 |
– |
} catch (Exception ex) { |
1112 |
– |
unexpectedException(); |
1016 |
|
} finally { |
1017 |
|
joinPool(e); |
1018 |
|
} |
1021 |
|
/** |
1022 |
|
* invokeAny(empty collection) throws IAE |
1023 |
|
*/ |
1024 |
< |
public void testInvokeAny2() { |
1025 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1024 |
> |
public void testInvokeAny2() throws Exception { |
1025 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1026 |
|
try { |
1027 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1028 |
+ |
shouldThrow(); |
1029 |
|
} catch (IllegalArgumentException success) { |
1126 |
– |
} catch (Exception ex) { |
1127 |
– |
unexpectedException(); |
1030 |
|
} finally { |
1031 |
|
joinPool(e); |
1032 |
|
} |
1035 |
|
/** |
1036 |
|
* invokeAny(c) throws NPE if c has null elements |
1037 |
|
*/ |
1038 |
< |
public void testInvokeAny3() { |
1039 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1038 |
> |
public void testInvokeAny3() throws Exception { |
1039 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1040 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1041 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1042 |
> |
l.add(latchAwaitingStringTask(latch)); |
1043 |
> |
l.add(null); |
1044 |
|
try { |
1139 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1140 |
– |
l.add(new StringTask()); |
1141 |
– |
l.add(null); |
1045 |
|
e.invokeAny(l); |
1046 |
+ |
shouldThrow(); |
1047 |
|
} catch (NullPointerException success) { |
1144 |
– |
} catch (Exception ex) { |
1145 |
– |
unexpectedException(); |
1048 |
|
} finally { |
1049 |
+ |
latch.countDown(); |
1050 |
|
joinPool(e); |
1051 |
|
} |
1052 |
|
} |
1054 |
|
/** |
1055 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1056 |
|
*/ |
1057 |
< |
public void testInvokeAny4() { |
1058 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1057 |
> |
public void testInvokeAny4() throws Exception { |
1058 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1059 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1060 |
> |
l.add(new NPETask()); |
1061 |
|
try { |
1157 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1158 |
– |
l.add(new NPETask()); |
1062 |
|
e.invokeAny(l); |
1063 |
+ |
shouldThrow(); |
1064 |
|
} catch (ExecutionException success) { |
1065 |
< |
} catch (Exception ex) { |
1162 |
< |
unexpectedException(); |
1065 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1066 |
|
} finally { |
1067 |
|
joinPool(e); |
1068 |
|
} |
1071 |
|
/** |
1072 |
|
* invokeAny(c) returns result of some task |
1073 |
|
*/ |
1074 |
< |
public void testInvokeAny5() { |
1075 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1074 |
> |
public void testInvokeAny5() throws Exception { |
1075 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1076 |
|
try { |
1077 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1077 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1078 |
|
l.add(new StringTask()); |
1079 |
|
l.add(new StringTask()); |
1080 |
|
String result = e.invokeAny(l); |
1081 |
|
assertSame(TEST_STRING, result); |
1179 |
– |
} catch (ExecutionException success) { |
1180 |
– |
} catch (Exception ex) { |
1181 |
– |
unexpectedException(); |
1082 |
|
} finally { |
1083 |
|
joinPool(e); |
1084 |
|
} |
1087 |
|
/** |
1088 |
|
* invokeAll(null) throws NPE |
1089 |
|
*/ |
1090 |
< |
public void testInvokeAll1() { |
1091 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1090 |
> |
public void testInvokeAll1() throws Exception { |
1091 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1092 |
|
try { |
1093 |
|
e.invokeAll(null); |
1094 |
+ |
shouldThrow(); |
1095 |
|
} catch (NullPointerException success) { |
1195 |
– |
} catch (Exception ex) { |
1196 |
– |
unexpectedException(); |
1096 |
|
} finally { |
1097 |
|
joinPool(e); |
1098 |
|
} |
1101 |
|
/** |
1102 |
|
* invokeAll(empty collection) returns empty collection |
1103 |
|
*/ |
1104 |
< |
public void testInvokeAll2() { |
1105 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1104 |
> |
public void testInvokeAll2() throws InterruptedException { |
1105 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1106 |
|
try { |
1107 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1108 |
|
assertTrue(r.isEmpty()); |
1210 |
– |
} catch (Exception ex) { |
1211 |
– |
unexpectedException(); |
1109 |
|
} finally { |
1110 |
|
joinPool(e); |
1111 |
|
} |
1114 |
|
/** |
1115 |
|
* invokeAll(c) throws NPE if c has null elements |
1116 |
|
*/ |
1117 |
< |
public void testInvokeAll3() { |
1118 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1117 |
> |
public void testInvokeAll3() throws Exception { |
1118 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1119 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1120 |
> |
l.add(new StringTask()); |
1121 |
> |
l.add(null); |
1122 |
|
try { |
1223 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1224 |
– |
l.add(new StringTask()); |
1225 |
– |
l.add(null); |
1123 |
|
e.invokeAll(l); |
1124 |
+ |
shouldThrow(); |
1125 |
|
} catch (NullPointerException success) { |
1228 |
– |
} catch (Exception ex) { |
1229 |
– |
unexpectedException(); |
1126 |
|
} finally { |
1127 |
|
joinPool(e); |
1128 |
|
} |
1131 |
|
/** |
1132 |
|
* get of element of invokeAll(c) throws exception on failed task |
1133 |
|
*/ |
1134 |
< |
public void testInvokeAll4() { |
1135 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1134 |
> |
public void testInvokeAll4() throws Exception { |
1135 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1136 |
|
try { |
1137 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1137 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1138 |
|
l.add(new NPETask()); |
1139 |
< |
List<Future<String>> result = e.invokeAll(l); |
1140 |
< |
assertEquals(1, result.size()); |
1141 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1142 |
< |
it.next().get(); |
1143 |
< |
} catch (ExecutionException success) { |
1144 |
< |
} catch (Exception ex) { |
1145 |
< |
unexpectedException(); |
1139 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1140 |
> |
assertEquals(1, futures.size()); |
1141 |
> |
try { |
1142 |
> |
futures.get(0).get(); |
1143 |
> |
shouldThrow(); |
1144 |
> |
} catch (ExecutionException success) { |
1145 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1146 |
> |
} |
1147 |
|
} finally { |
1148 |
|
joinPool(e); |
1149 |
|
} |
1152 |
|
/** |
1153 |
|
* invokeAll(c) returns results of all completed tasks |
1154 |
|
*/ |
1155 |
< |
public void testInvokeAll5() { |
1156 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1155 |
> |
public void testInvokeAll5() throws Exception { |
1156 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1157 |
|
try { |
1158 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1158 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1159 |
|
l.add(new StringTask()); |
1160 |
|
l.add(new StringTask()); |
1161 |
< |
List<Future<String>> result = e.invokeAll(l); |
1162 |
< |
assertEquals(2, result.size()); |
1163 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1164 |
< |
assertSame(TEST_STRING, it.next().get()); |
1268 |
< |
} catch (ExecutionException success) { |
1269 |
< |
} catch (Exception ex) { |
1270 |
< |
unexpectedException(); |
1161 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1162 |
> |
assertEquals(2, futures.size()); |
1163 |
> |
for (Future<String> future : futures) |
1164 |
> |
assertSame(TEST_STRING, future.get()); |
1165 |
|
} finally { |
1166 |
|
joinPool(e); |
1167 |
|
} |
1172 |
|
/** |
1173 |
|
* timed invokeAny(null) throws NPE |
1174 |
|
*/ |
1175 |
< |
public void testTimedInvokeAny1() { |
1176 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1175 |
> |
public void testTimedInvokeAny1() throws Exception { |
1176 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1177 |
|
try { |
1178 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1178 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1179 |
> |
shouldThrow(); |
1180 |
|
} catch (NullPointerException success) { |
1286 |
– |
} catch (Exception ex) { |
1287 |
– |
unexpectedException(); |
1181 |
|
} finally { |
1182 |
|
joinPool(e); |
1183 |
|
} |
1186 |
|
/** |
1187 |
|
* timed invokeAny(,,null) throws NPE |
1188 |
|
*/ |
1189 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
1190 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1189 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1190 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1191 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1192 |
> |
l.add(new StringTask()); |
1193 |
|
try { |
1299 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1300 |
– |
l.add(new StringTask()); |
1194 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1195 |
+ |
shouldThrow(); |
1196 |
|
} catch (NullPointerException success) { |
1303 |
– |
} catch (Exception ex) { |
1304 |
– |
unexpectedException(); |
1197 |
|
} finally { |
1198 |
|
joinPool(e); |
1199 |
|
} |
1202 |
|
/** |
1203 |
|
* timed invokeAny(empty collection) throws IAE |
1204 |
|
*/ |
1205 |
< |
public void testTimedInvokeAny2() { |
1206 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1205 |
> |
public void testTimedInvokeAny2() throws Exception { |
1206 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1207 |
|
try { |
1208 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1208 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1209 |
> |
shouldThrow(); |
1210 |
|
} catch (IllegalArgumentException success) { |
1318 |
– |
} catch (Exception ex) { |
1319 |
– |
unexpectedException(); |
1211 |
|
} finally { |
1212 |
|
joinPool(e); |
1213 |
|
} |
1216 |
|
/** |
1217 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1218 |
|
*/ |
1219 |
< |
public void testTimedInvokeAny3() { |
1220 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1219 |
> |
public void testTimedInvokeAny3() throws Exception { |
1220 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1221 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1222 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1223 |
> |
l.add(latchAwaitingStringTask(latch)); |
1224 |
> |
l.add(null); |
1225 |
|
try { |
1226 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1227 |
< |
l.add(new StringTask()); |
1333 |
< |
l.add(null); |
1334 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1226 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1227 |
> |
shouldThrow(); |
1228 |
|
} catch (NullPointerException success) { |
1336 |
– |
} catch (Exception ex) { |
1337 |
– |
ex.printStackTrace(); |
1338 |
– |
unexpectedException(); |
1229 |
|
} finally { |
1230 |
+ |
latch.countDown(); |
1231 |
|
joinPool(e); |
1232 |
|
} |
1233 |
|
} |
1235 |
|
/** |
1236 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1237 |
|
*/ |
1238 |
< |
public void testTimedInvokeAny4() { |
1239 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1238 |
> |
public void testTimedInvokeAny4() throws Exception { |
1239 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1240 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1241 |
> |
l.add(new NPETask()); |
1242 |
|
try { |
1243 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1244 |
< |
l.add(new NPETask()); |
1352 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1243 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1244 |
> |
shouldThrow(); |
1245 |
|
} catch (ExecutionException success) { |
1246 |
< |
} catch (Exception ex) { |
1355 |
< |
unexpectedException(); |
1246 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1247 |
|
} finally { |
1248 |
|
joinPool(e); |
1249 |
|
} |
1252 |
|
/** |
1253 |
|
* timed invokeAny(c) returns result of some task |
1254 |
|
*/ |
1255 |
< |
public void testTimedInvokeAny5() { |
1256 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1255 |
> |
public void testTimedInvokeAny5() throws Exception { |
1256 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1257 |
|
try { |
1258 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1258 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1259 |
|
l.add(new StringTask()); |
1260 |
|
l.add(new StringTask()); |
1261 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1261 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1262 |
|
assertSame(TEST_STRING, result); |
1372 |
– |
} catch (ExecutionException success) { |
1373 |
– |
} catch (Exception ex) { |
1374 |
– |
unexpectedException(); |
1263 |
|
} finally { |
1264 |
|
joinPool(e); |
1265 |
|
} |
1268 |
|
/** |
1269 |
|
* timed invokeAll(null) throws NPE |
1270 |
|
*/ |
1271 |
< |
public void testTimedInvokeAll1() { |
1272 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1271 |
> |
public void testTimedInvokeAll1() throws Exception { |
1272 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1273 |
|
try { |
1274 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1274 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1275 |
> |
shouldThrow(); |
1276 |
|
} catch (NullPointerException success) { |
1388 |
– |
} catch (Exception ex) { |
1389 |
– |
unexpectedException(); |
1277 |
|
} finally { |
1278 |
|
joinPool(e); |
1279 |
|
} |
1282 |
|
/** |
1283 |
|
* timed invokeAll(,,null) throws NPE |
1284 |
|
*/ |
1285 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
1286 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1285 |
> |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1286 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1287 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1288 |
> |
l.add(new StringTask()); |
1289 |
|
try { |
1401 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1402 |
– |
l.add(new StringTask()); |
1290 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1291 |
+ |
shouldThrow(); |
1292 |
|
} catch (NullPointerException success) { |
1405 |
– |
} catch (Exception ex) { |
1406 |
– |
unexpectedException(); |
1293 |
|
} finally { |
1294 |
|
joinPool(e); |
1295 |
|
} |
1298 |
|
/** |
1299 |
|
* timed invokeAll(empty collection) returns empty collection |
1300 |
|
*/ |
1301 |
< |
public void testTimedInvokeAll2() { |
1302 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1301 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
1302 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1303 |
|
try { |
1304 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1304 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1305 |
|
assertTrue(r.isEmpty()); |
1420 |
– |
} catch (Exception ex) { |
1421 |
– |
unexpectedException(); |
1306 |
|
} finally { |
1307 |
|
joinPool(e); |
1308 |
|
} |
1311 |
|
/** |
1312 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1313 |
|
*/ |
1314 |
< |
public void testTimedInvokeAll3() { |
1315 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1314 |
> |
public void testTimedInvokeAll3() throws Exception { |
1315 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1316 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1317 |
> |
l.add(new StringTask()); |
1318 |
> |
l.add(null); |
1319 |
|
try { |
1320 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1321 |
< |
l.add(new StringTask()); |
1435 |
< |
l.add(null); |
1436 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1320 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1321 |
> |
shouldThrow(); |
1322 |
|
} catch (NullPointerException success) { |
1438 |
– |
} catch (Exception ex) { |
1439 |
– |
unexpectedException(); |
1323 |
|
} finally { |
1324 |
|
joinPool(e); |
1325 |
|
} |
1328 |
|
/** |
1329 |
|
* get of element of invokeAll(c) throws exception on failed task |
1330 |
|
*/ |
1331 |
< |
public void testTimedInvokeAll4() { |
1332 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1331 |
> |
public void testTimedInvokeAll4() throws Exception { |
1332 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1333 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1334 |
> |
l.add(new NPETask()); |
1335 |
> |
List<Future<String>> futures = |
1336 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1337 |
> |
assertEquals(1, futures.size()); |
1338 |
|
try { |
1339 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1340 |
< |
l.add(new NPETask()); |
1453 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1454 |
< |
assertEquals(1, result.size()); |
1455 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1456 |
< |
it.next().get(); |
1339 |
> |
futures.get(0).get(); |
1340 |
> |
shouldThrow(); |
1341 |
|
} catch (ExecutionException success) { |
1342 |
< |
} catch (Exception ex) { |
1459 |
< |
unexpectedException(); |
1342 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1343 |
|
} finally { |
1344 |
|
joinPool(e); |
1345 |
|
} |
1348 |
|
/** |
1349 |
|
* timed invokeAll(c) returns results of all completed tasks |
1350 |
|
*/ |
1351 |
< |
public void testTimedInvokeAll5() { |
1352 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1351 |
> |
public void testTimedInvokeAll5() throws Exception { |
1352 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1353 |
|
try { |
1354 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1354 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1355 |
|
l.add(new StringTask()); |
1356 |
|
l.add(new StringTask()); |
1357 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
1358 |
< |
assertEquals(2, result.size()); |
1359 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1360 |
< |
assertSame(TEST_STRING, it.next().get()); |
1361 |
< |
} catch (ExecutionException success) { |
1479 |
< |
} catch (Exception ex) { |
1480 |
< |
unexpectedException(); |
1357 |
> |
List<Future<String>> futures = |
1358 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1359 |
> |
assertEquals(2, futures.size()); |
1360 |
> |
for (Future<String> future : futures) |
1361 |
> |
assertSame(TEST_STRING, future.get()); |
1362 |
|
} finally { |
1363 |
|
joinPool(e); |
1364 |
|
} |
1367 |
|
/** |
1368 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1369 |
|
*/ |
1370 |
< |
public void testTimedInvokeAll6() { |
1371 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1370 |
> |
public void testTimedInvokeAll6() throws Exception { |
1371 |
> |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1372 |
|
try { |
1373 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1373 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1374 |
|
l.add(new StringTask()); |
1375 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1376 |
|
l.add(new StringTask()); |
1377 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
1378 |
< |
assertEquals(3, result.size()); |
1379 |
< |
Iterator<Future<String>> it = result.iterator(); |
1377 |
> |
List<Future<String>> futures = |
1378 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1379 |
> |
assertEquals(3, futures.size()); |
1380 |
> |
Iterator<Future<String>> it = futures.iterator(); |
1381 |
|
Future<String> f1 = it.next(); |
1382 |
|
Future<String> f2 = it.next(); |
1383 |
|
Future<String> f3 = it.next(); |
1386 |
|
assertTrue(f3.isDone()); |
1387 |
|
assertFalse(f1.isCancelled()); |
1388 |
|
assertTrue(f2.isCancelled()); |
1507 |
– |
} catch (Exception ex) { |
1508 |
– |
unexpectedException(); |
1389 |
|
} finally { |
1390 |
|
joinPool(e); |
1391 |
|
} |
1395 |
|
* Execution continues if there is at least one thread even if |
1396 |
|
* thread factory fails to create more |
1397 |
|
*/ |
1398 |
< |
public void testFailingThreadFactory() { |
1399 |
< |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1398 |
> |
public void testFailingThreadFactory() throws InterruptedException { |
1399 |
> |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1400 |
|
try { |
1401 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1401 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1402 |
|
for (int k = 0; k < 100; ++k) { |
1403 |
|
e.execute(new NoOpRunnable()); |
1404 |
|
} |
1405 |
|
Thread.sleep(LONG_DELAY_MS); |
1526 |
– |
} catch (Exception ex) { |
1527 |
– |
unexpectedException(); |
1406 |
|
} finally { |
1407 |
|
joinPool(e); |
1408 |
|
} |
1412 |
|
* allowsCoreThreadTimeOut is by default false. |
1413 |
|
*/ |
1414 |
|
public void testAllowsCoreThreadTimeOut() { |
1415 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1415 |
> |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1416 |
|
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1417 |
|
joinPool(tpe); |
1418 |
|
} |
1420 |
|
/** |
1421 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1422 |
|
*/ |
1423 |
< |
public void testAllowCoreThreadTimeOut_true() { |
1424 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1423 |
> |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1424 |
> |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1425 |
|
tpe.allowCoreThreadTimeOut(true); |
1426 |
|
tpe.execute(new NoOpRunnable()); |
1427 |
|
try { |
1428 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
1429 |
|
assertEquals(0, tpe.getPoolSize()); |
1552 |
– |
} catch (InterruptedException e) { |
1553 |
– |
unexpectedException(); |
1430 |
|
} finally { |
1431 |
|
joinPool(tpe); |
1432 |
|
} |
1435 |
|
/** |
1436 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1437 |
|
*/ |
1438 |
< |
public void testAllowCoreThreadTimeOut_false() { |
1439 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1438 |
> |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1439 |
> |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1440 |
|
tpe.allowCoreThreadTimeOut(false); |
1441 |
|
tpe.execute(new NoOpRunnable()); |
1442 |
|
try { |
1443 |
|
Thread.sleep(MEDIUM_DELAY_MS); |
1444 |
|
assertTrue(tpe.getPoolSize() >= 1); |
1569 |
– |
} catch (InterruptedException e) { |
1570 |
– |
unexpectedException(); |
1445 |
|
} finally { |
1446 |
|
joinPool(tpe); |
1447 |
|
} |
1451 |
|
* execute allows the same task to be submitted multiple times, even |
1452 |
|
* if rejected |
1453 |
|
*/ |
1454 |
< |
public void testRejectedRecycledTask() { |
1454 |
> |
public void testRejectedRecycledTask() throws InterruptedException { |
1455 |
|
final int nTasks = 1000; |
1456 |
|
final AtomicInteger nRun = new AtomicInteger(0); |
1457 |
|
final Runnable recycledTask = new Runnable() { |
1474 |
|
} |
1475 |
|
Thread.sleep(5000); // enough time to run all tasks |
1476 |
|
assertEquals(nRun.get(), nTasks); |
1603 |
– |
} catch (Exception ex) { |
1604 |
– |
ex.printStackTrace(); |
1605 |
– |
unexpectedException(); |
1477 |
|
} finally { |
1478 |
|
p.shutdown(); |
1479 |
|
} |