12 |
|
import java.math.BigInteger; |
13 |
|
|
14 |
|
public class ExecutorsTest extends JSR166TestCase{ |
15 |
– |
|
15 |
|
public static void main(String[] args) { |
16 |
|
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
78 |
|
} |
79 |
|
}; |
80 |
|
|
81 |
+ |
/** |
82 |
+ |
* For use as ThreadFactory in constructors |
83 |
+ |
*/ |
84 |
+ |
static class MyThreadFactory implements ThreadFactory{ |
85 |
+ |
public Thread newThread(Runnable r){ |
86 |
+ |
return new Thread(r); |
87 |
+ |
} |
88 |
+ |
} |
89 |
+ |
|
90 |
+ |
/** |
91 |
+ |
* A newCachedThreadPool can execute runnables |
92 |
+ |
*/ |
93 |
+ |
public void testNewCachedThreadPool1() { |
94 |
+ |
ExecutorService e = Executors.newCachedThreadPool(); |
95 |
+ |
e.execute(new NoOpRunnable()); |
96 |
+ |
e.execute(new NoOpRunnable()); |
97 |
+ |
e.execute(new NoOpRunnable()); |
98 |
+ |
e.shutdown(); |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
/** |
102 |
+ |
* A newCachedThreadPool with given ThreadFactory can execute runnables |
103 |
+ |
*/ |
104 |
+ |
public void testNewCachedThreadPool2() { |
105 |
+ |
ExecutorService e = Executors.newCachedThreadPool(new MyThreadFactory()); |
106 |
+ |
e.execute(new NoOpRunnable()); |
107 |
+ |
e.execute(new NoOpRunnable()); |
108 |
+ |
e.execute(new NoOpRunnable()); |
109 |
+ |
e.shutdown(); |
110 |
+ |
} |
111 |
+ |
|
112 |
+ |
/** |
113 |
+ |
* A newCachedThreadPool with null ThreadFactory throws NPE |
114 |
+ |
*/ |
115 |
+ |
public void testNewCachedThreadPool3() { |
116 |
+ |
try { |
117 |
+ |
ExecutorService e = Executors.newCachedThreadPool(null); |
118 |
+ |
shouldThrow(); |
119 |
+ |
} |
120 |
+ |
catch(NullPointerException success) { |
121 |
+ |
} |
122 |
+ |
} |
123 |
+ |
|
124 |
+ |
|
125 |
+ |
/** |
126 |
+ |
* A new SingleThreadExecutor can execute runnables |
127 |
+ |
*/ |
128 |
+ |
public void testNewSingleThreadExecutor1() { |
129 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(); |
130 |
+ |
e.execute(new NoOpRunnable()); |
131 |
+ |
e.execute(new NoOpRunnable()); |
132 |
+ |
e.execute(new NoOpRunnable()); |
133 |
+ |
e.shutdown(); |
134 |
+ |
} |
135 |
+ |
|
136 |
+ |
/** |
137 |
+ |
* A new SingleThreadExecutor with given ThreadFactory can execute runnables |
138 |
+ |
*/ |
139 |
+ |
public void testNewSingleThreadExecutor2() { |
140 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(new MyThreadFactory()); |
141 |
+ |
e.execute(new NoOpRunnable()); |
142 |
+ |
e.execute(new NoOpRunnable()); |
143 |
+ |
e.execute(new NoOpRunnable()); |
144 |
+ |
e.shutdown(); |
145 |
+ |
} |
146 |
+ |
|
147 |
+ |
/** |
148 |
+ |
* A new SingleThreadExecutor with null ThreadFactory throws NPE |
149 |
+ |
*/ |
150 |
+ |
public void testNewSingleThreadExecutor3() { |
151 |
+ |
try { |
152 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(null); |
153 |
+ |
shouldThrow(); |
154 |
+ |
} |
155 |
+ |
catch(NullPointerException success) { |
156 |
+ |
} |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
/** |
160 |
+ |
* A new newFixedThreadPool can execute runnables |
161 |
+ |
*/ |
162 |
+ |
public void testNewFixedThreadPool1() { |
163 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2); |
164 |
+ |
e.execute(new NoOpRunnable()); |
165 |
+ |
e.execute(new NoOpRunnable()); |
166 |
+ |
e.execute(new NoOpRunnable()); |
167 |
+ |
e.shutdown(); |
168 |
+ |
} |
169 |
+ |
|
170 |
+ |
/** |
171 |
+ |
* A new newFixedThreadPool with given ThreadFactory can execute runnables |
172 |
+ |
*/ |
173 |
+ |
public void testNewFixedThreadPool2() { |
174 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2, new MyThreadFactory()); |
175 |
+ |
e.execute(new NoOpRunnable()); |
176 |
+ |
e.execute(new NoOpRunnable()); |
177 |
+ |
e.execute(new NoOpRunnable()); |
178 |
+ |
e.shutdown(); |
179 |
+ |
} |
180 |
+ |
|
181 |
+ |
/** |
182 |
+ |
* A new newFixedThreadPool with null ThreadFactory throws NPE |
183 |
+ |
*/ |
184 |
+ |
public void testNewFixedThreadPool3() { |
185 |
+ |
try { |
186 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2, null); |
187 |
+ |
shouldThrow(); |
188 |
+ |
} |
189 |
+ |
catch(NullPointerException success) { |
190 |
+ |
} |
191 |
+ |
} |
192 |
+ |
|
193 |
+ |
/** |
194 |
+ |
* A new newFixedThreadPool with 0 threads throws IAE |
195 |
+ |
*/ |
196 |
+ |
public void testNewFixedThreadPool4() { |
197 |
+ |
try { |
198 |
+ |
ExecutorService e = Executors.newFixedThreadPool(0); |
199 |
+ |
shouldThrow(); |
200 |
+ |
} |
201 |
+ |
catch(IllegalArgumentException success) { |
202 |
+ |
} |
203 |
+ |
} |
204 |
+ |
|
205 |
+ |
/** |
206 |
+ |
* execute of runnable runs it to completion |
207 |
+ |
*/ |
208 |
+ |
public void testExecuteRunnable () { |
209 |
+ |
try { |
210 |
+ |
Executor e = new DirectExecutor(); |
211 |
+ |
MyTask task = new MyTask(); |
212 |
+ |
assertFalse(task.isCompleted()); |
213 |
+ |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
214 |
+ |
String result = future.get(); |
215 |
+ |
assertTrue(task.isCompleted()); |
216 |
+ |
assertSame(TEST_STRING, result); |
217 |
+ |
} |
218 |
+ |
catch (ExecutionException ex) { |
219 |
+ |
unexpectedException(); |
220 |
+ |
} |
221 |
+ |
catch (InterruptedException ex) { |
222 |
+ |
unexpectedException(); |
223 |
+ |
} |
224 |
+ |
} |
225 |
+ |
|
226 |
+ |
/** |
227 |
+ |
* invoke of a runnable runs it to completion |
228 |
+ |
*/ |
229 |
+ |
public void testInvokeRunnable () { |
230 |
+ |
try { |
231 |
+ |
Executor e = new DirectExecutor(); |
232 |
+ |
MyTask task = new MyTask(); |
233 |
+ |
assertFalse(task.isCompleted()); |
234 |
+ |
Executors.invoke(e, task); |
235 |
+ |
assertTrue(task.isCompleted()); |
236 |
+ |
} |
237 |
+ |
catch (ExecutionException ex) { |
238 |
+ |
unexpectedException(); |
239 |
+ |
} |
240 |
+ |
catch (InterruptedException ex) { |
241 |
+ |
unexpectedException(); |
242 |
+ |
} |
243 |
+ |
} |
244 |
+ |
|
245 |
+ |
/** |
246 |
+ |
* execute of a callable runs it to completion |
247 |
+ |
*/ |
248 |
+ |
public void testExecuteCallable () { |
249 |
+ |
try { |
250 |
+ |
Executor e = new DirectExecutor(); |
251 |
+ |
Future<String> future = Executors.execute(e, new StringTask()); |
252 |
+ |
String result = future.get(); |
253 |
+ |
assertSame(TEST_STRING, result); |
254 |
+ |
} |
255 |
+ |
catch (ExecutionException ex) { |
256 |
+ |
unexpectedException(); |
257 |
+ |
} |
258 |
+ |
catch (InterruptedException ex) { |
259 |
+ |
unexpectedException(); |
260 |
+ |
} |
261 |
+ |
} |
262 |
+ |
|
263 |
+ |
/** |
264 |
+ |
* invoke of a collable runs it to completion |
265 |
+ |
*/ |
266 |
+ |
public void testInvokeCallable () { |
267 |
+ |
try { |
268 |
+ |
Executor e = new DirectExecutor(); |
269 |
+ |
String result = Executors.invoke(e, new StringTask()); |
270 |
+ |
|
271 |
+ |
assertSame(TEST_STRING, result); |
272 |
+ |
} |
273 |
+ |
catch (ExecutionException ex) { |
274 |
+ |
unexpectedException(); |
275 |
+ |
} |
276 |
+ |
catch (InterruptedException ex) { |
277 |
+ |
unexpectedException(); |
278 |
+ |
} |
279 |
+ |
} |
280 |
|
|
281 |
+ |
/** |
282 |
+ |
* execute with null executor throws NPE |
283 |
+ |
*/ |
284 |
+ |
public void testNullExecuteRunnable () { |
285 |
+ |
try { |
286 |
+ |
MyTask task = new MyTask(); |
287 |
+ |
assertFalse(task.isCompleted()); |
288 |
+ |
Future<String> future = Executors.execute(null, task, TEST_STRING); |
289 |
+ |
shouldThrow(); |
290 |
+ |
} |
291 |
+ |
catch (NullPointerException success) { |
292 |
+ |
} |
293 |
+ |
catch (Exception ex) { |
294 |
+ |
unexpectedException(); |
295 |
+ |
} |
296 |
+ |
} |
297 |
|
|
298 |
|
/** |
299 |
< |
* execute(Executor, Runnable) will throw |
300 |
< |
* RejectedExecutionException Attempting to execute a runnable on |
301 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
302 |
< |
* runnables are attempted on a pool capable on handling one |
303 |
< |
* until it throws an exception |
299 |
> |
* execute with a null runnable throws NPE |
300 |
> |
*/ |
301 |
> |
public void testExecuteNullRunnable() { |
302 |
> |
try { |
303 |
> |
Executor e = new DirectExecutor(); |
304 |
> |
MyTask task = null; |
305 |
> |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
306 |
> |
shouldThrow(); |
307 |
> |
} |
308 |
> |
catch (NullPointerException success) { |
309 |
> |
} |
310 |
> |
catch (Exception ex) { |
311 |
> |
unexpectedException(); |
312 |
> |
} |
313 |
> |
} |
314 |
> |
|
315 |
> |
/** |
316 |
> |
* invoke of a null runnable throws NPE |
317 |
> |
*/ |
318 |
> |
public void testInvokeNullRunnable () { |
319 |
> |
try { |
320 |
> |
Executor e = new DirectExecutor(); |
321 |
> |
MyTask task = null; |
322 |
> |
Executors.invoke(e, task); |
323 |
> |
shouldThrow(); |
324 |
> |
} |
325 |
> |
catch (NullPointerException success) { |
326 |
> |
} |
327 |
> |
catch (Exception ex) { |
328 |
> |
unexpectedException(); |
329 |
> |
} |
330 |
> |
} |
331 |
> |
|
332 |
> |
/** |
333 |
> |
* execute of a null callable throws NPE |
334 |
> |
*/ |
335 |
> |
public void testExecuteNullCallable () { |
336 |
> |
try { |
337 |
> |
Executor e = new DirectExecutor(); |
338 |
> |
StringTask t = null; |
339 |
> |
Future<String> future = Executors.execute(e, t); |
340 |
> |
shouldThrow(); |
341 |
> |
} |
342 |
> |
catch (NullPointerException success) { |
343 |
> |
} |
344 |
> |
catch (Exception ex) { |
345 |
> |
unexpectedException(); |
346 |
> |
} |
347 |
> |
} |
348 |
> |
|
349 |
> |
/** |
350 |
> |
* invoke of a null callable throws NPE |
351 |
> |
*/ |
352 |
> |
public void testInvokeNullCallable () { |
353 |
> |
try { |
354 |
> |
Executor e = new DirectExecutor(); |
355 |
> |
StringTask t = null; |
356 |
> |
String result = Executors.invoke(e, t); |
357 |
> |
shouldThrow(); |
358 |
> |
} |
359 |
> |
catch (NullPointerException success) { |
360 |
> |
} |
361 |
> |
catch (Exception ex) { |
362 |
> |
unexpectedException(); |
363 |
> |
} |
364 |
> |
} |
365 |
> |
|
366 |
> |
/** |
367 |
> |
* execute(Executor, Runnable) throws RejectedExecutionException |
368 |
> |
* if saturated. |
369 |
|
*/ |
370 |
|
public void testExecute1() { |
371 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
380 |
|
} |
381 |
|
|
382 |
|
/** |
383 |
< |
* execute(Executor, Callable) will throw |
384 |
< |
* RejectedExecutionException Attempting to execute a callable on |
106 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
107 |
< |
* runnables are attempted on a pool capable on handling one |
108 |
< |
* until it throws an exception |
383 |
> |
* execute(Executor, Callable)throws RejectedExecutionException |
384 |
> |
* if saturated. |
385 |
|
*/ |
386 |
|
public void testExecute2() { |
387 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
396 |
|
|
397 |
|
|
398 |
|
/** |
399 |
< |
* invoke(Executor, Runnable) throws InterruptedException |
400 |
< |
* A single use of invoke starts that will wait long enough |
125 |
< |
* for the invoking thread to be interrupted |
399 |
> |
* invoke(Executor, Runnable) throws InterruptedException if |
400 |
> |
* caller interrupted. |
401 |
|
*/ |
402 |
< |
public void testInvoke2() { |
402 |
> |
public void testInterruptedInvoke() { |
403 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
404 |
|
Thread t = new Thread(new Runnable() { |
405 |
|
public void run() { |
431 |
|
} |
432 |
|
|
433 |
|
/** |
434 |
< |
* invoke(Executor, Runnable) will throw |
435 |
< |
* ExecutionException An ExecutionException occurs when the |
161 |
< |
* underlying Runnable throws an exception, here the |
162 |
< |
* DivideByZeroException will cause an ExecutionException |
434 |
> |
* invoke(Executor, Runnable) throws ExecutionException if |
435 |
> |
* runnable throws exception. |
436 |
|
*/ |
437 |
|
public void testInvoke3() { |
438 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
458 |
|
|
459 |
|
|
460 |
|
/** |
461 |
< |
* invoke(Executor, Callable) throws |
462 |
< |
* InterruptedException A single use of invoke starts that will |
190 |
< |
* wait long enough for the invoking thread to be interrupted |
461 |
> |
* invoke(Executor, Callable) throws InterruptedException if |
462 |
> |
* callable throws exception |
463 |
|
*/ |
464 |
|
public void testInvoke5() { |
465 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
498 |
|
} |
499 |
|
|
500 |
|
/** |
501 |
< |
* invoke(Executor, Callable) will throw ExecutionException |
502 |
< |
* An ExecutionException occurs when the underlying Runnable throws |
231 |
< |
* an exception, here the DivideByZeroException will cause an ExecutionException |
501 |
> |
* invoke(Executor, Callable) will throw ExecutionException |
502 |
> |
* if callable throws exception |
503 |
|
*/ |
504 |
|
public void testInvoke6() { |
505 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
517 |
|
} |
518 |
|
|
519 |
|
shouldThrow(); |
520 |
< |
} catch(RejectedExecutionException e){} |
521 |
< |
catch(InterruptedException e2){} |
522 |
< |
catch(ExecutionException e3){} |
252 |
< |
joinPool(p); |
253 |
< |
} |
254 |
< |
|
255 |
< |
/** |
256 |
< |
* |
257 |
< |
*/ |
258 |
< |
public void testExecuteRunnable () { |
259 |
< |
try { |
260 |
< |
Executor e = new DirectExecutor(); |
261 |
< |
MyTask task = new MyTask(); |
262 |
< |
assertFalse(task.isCompleted()); |
263 |
< |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
264 |
< |
String result = future.get(); |
265 |
< |
assertTrue(task.isCompleted()); |
266 |
< |
assertSame(TEST_STRING, result); |
267 |
< |
} |
268 |
< |
catch (ExecutionException ex) { |
269 |
< |
unexpectedException(); |
270 |
< |
} |
271 |
< |
catch (InterruptedException ex) { |
272 |
< |
unexpectedException(); |
273 |
< |
} |
274 |
< |
} |
275 |
< |
|
276 |
< |
/** |
277 |
< |
* |
278 |
< |
*/ |
279 |
< |
public void testInvokeRunnable () { |
280 |
< |
try { |
281 |
< |
Executor e = new DirectExecutor(); |
282 |
< |
MyTask task = new MyTask(); |
283 |
< |
assertFalse(task.isCompleted()); |
284 |
< |
Executors.invoke(e, task); |
285 |
< |
assertTrue(task.isCompleted()); |
286 |
< |
} |
287 |
< |
catch (ExecutionException ex) { |
288 |
< |
unexpectedException(); |
289 |
< |
} |
290 |
< |
catch (InterruptedException ex) { |
291 |
< |
unexpectedException(); |
292 |
< |
} |
293 |
< |
} |
294 |
< |
|
295 |
< |
/** |
296 |
< |
* |
297 |
< |
*/ |
298 |
< |
public void testExecuteCallable () { |
299 |
< |
try { |
300 |
< |
Executor e = new DirectExecutor(); |
301 |
< |
Future<String> future = Executors.execute(e, new StringTask()); |
302 |
< |
String result = future.get(); |
303 |
< |
assertSame(TEST_STRING, result); |
304 |
< |
} |
305 |
< |
catch (ExecutionException ex) { |
306 |
< |
unexpectedException(); |
307 |
< |
} |
308 |
< |
catch (InterruptedException ex) { |
520 |
> |
} |
521 |
> |
catch(ExecutionException success){ |
522 |
> |
} catch(Exception e) { |
523 |
|
unexpectedException(); |
524 |
|
} |
525 |
+ |
joinPool(p); |
526 |
|
} |
527 |
|
|
313 |
– |
/** |
314 |
– |
* |
315 |
– |
*/ |
316 |
– |
public void testInvokeCallable () { |
317 |
– |
try { |
318 |
– |
Executor e = new DirectExecutor(); |
319 |
– |
String result = Executors.invoke(e, new StringTask()); |
528 |
|
|
321 |
– |
assertSame(TEST_STRING, result); |
322 |
– |
} |
323 |
– |
catch (ExecutionException ex) { |
324 |
– |
unexpectedException(); |
325 |
– |
} |
326 |
– |
catch (InterruptedException ex) { |
327 |
– |
unexpectedException(); |
328 |
– |
} |
329 |
– |
} |
529 |
|
|
530 |
|
/** |
531 |
|
* timeouts from execute will time out if they compute too long. |