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 |
|
} |
21 |
|
|
22 |
|
private static final String TEST_STRING = "a test string"; |
23 |
|
|
25 |
– |
private static class MyTask implements Runnable { |
26 |
– |
public void run() { completed = true; } |
27 |
– |
public boolean isCompleted() { return completed; } |
28 |
– |
public void reset() { completed = false; } |
29 |
– |
private boolean completed = false; |
30 |
– |
} |
31 |
– |
|
24 |
|
private static class StringTask implements Callable<String> { |
25 |
|
public String call() { return TEST_STRING; } |
26 |
|
} |
71 |
|
} |
72 |
|
}; |
73 |
|
|
74 |
+ |
/** |
75 |
+ |
* A newCachedThreadPool can execute runnables |
76 |
+ |
*/ |
77 |
+ |
public void testNewCachedThreadPool1() { |
78 |
+ |
ExecutorService e = Executors.newCachedThreadPool(); |
79 |
+ |
e.execute(new NoOpRunnable()); |
80 |
+ |
e.execute(new NoOpRunnable()); |
81 |
+ |
e.execute(new NoOpRunnable()); |
82 |
+ |
e.shutdown(); |
83 |
+ |
} |
84 |
+ |
|
85 |
+ |
/** |
86 |
+ |
* A newCachedThreadPool with given ThreadFactory can execute runnables |
87 |
+ |
*/ |
88 |
+ |
public void testNewCachedThreadPool2() { |
89 |
+ |
ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory()); |
90 |
+ |
e.execute(new NoOpRunnable()); |
91 |
+ |
e.execute(new NoOpRunnable()); |
92 |
+ |
e.execute(new NoOpRunnable()); |
93 |
+ |
e.shutdown(); |
94 |
+ |
} |
95 |
+ |
|
96 |
+ |
/** |
97 |
+ |
* A newCachedThreadPool with null ThreadFactory throws NPE |
98 |
+ |
*/ |
99 |
+ |
public void testNewCachedThreadPool3() { |
100 |
+ |
try { |
101 |
+ |
ExecutorService e = Executors.newCachedThreadPool(null); |
102 |
+ |
shouldThrow(); |
103 |
+ |
} |
104 |
+ |
catch(NullPointerException success) { |
105 |
+ |
} |
106 |
+ |
} |
107 |
+ |
|
108 |
+ |
|
109 |
+ |
/** |
110 |
+ |
* A new SingleThreadExecutor can execute runnables |
111 |
+ |
*/ |
112 |
+ |
public void testNewSingleThreadExecutor1() { |
113 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(); |
114 |
+ |
e.execute(new NoOpRunnable()); |
115 |
+ |
e.execute(new NoOpRunnable()); |
116 |
+ |
e.execute(new NoOpRunnable()); |
117 |
+ |
e.shutdown(); |
118 |
+ |
} |
119 |
+ |
|
120 |
+ |
/** |
121 |
+ |
* A new SingleThreadExecutor with given ThreadFactory can execute runnables |
122 |
+ |
*/ |
123 |
+ |
public void testNewSingleThreadExecutor2() { |
124 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory()); |
125 |
+ |
e.execute(new NoOpRunnable()); |
126 |
+ |
e.execute(new NoOpRunnable()); |
127 |
+ |
e.execute(new NoOpRunnable()); |
128 |
+ |
e.shutdown(); |
129 |
+ |
} |
130 |
+ |
|
131 |
+ |
/** |
132 |
+ |
* A new SingleThreadExecutor with null ThreadFactory throws NPE |
133 |
+ |
*/ |
134 |
+ |
public void testNewSingleThreadExecutor3() { |
135 |
+ |
try { |
136 |
+ |
ExecutorService e = Executors.newSingleThreadExecutor(null); |
137 |
+ |
shouldThrow(); |
138 |
+ |
} |
139 |
+ |
catch(NullPointerException success) { |
140 |
+ |
} |
141 |
+ |
} |
142 |
+ |
|
143 |
+ |
/** |
144 |
+ |
* A new newFixedThreadPool can execute runnables |
145 |
+ |
*/ |
146 |
+ |
public void testNewFixedThreadPool1() { |
147 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2); |
148 |
+ |
e.execute(new NoOpRunnable()); |
149 |
+ |
e.execute(new NoOpRunnable()); |
150 |
+ |
e.execute(new NoOpRunnable()); |
151 |
+ |
e.shutdown(); |
152 |
+ |
} |
153 |
+ |
|
154 |
+ |
/** |
155 |
+ |
* A new newFixedThreadPool with given ThreadFactory can execute runnables |
156 |
+ |
*/ |
157 |
+ |
public void testNewFixedThreadPool2() { |
158 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory()); |
159 |
+ |
e.execute(new NoOpRunnable()); |
160 |
+ |
e.execute(new NoOpRunnable()); |
161 |
+ |
e.execute(new NoOpRunnable()); |
162 |
+ |
e.shutdown(); |
163 |
+ |
} |
164 |
+ |
|
165 |
+ |
/** |
166 |
+ |
* A new newFixedThreadPool with null ThreadFactory throws NPE |
167 |
+ |
*/ |
168 |
+ |
public void testNewFixedThreadPool3() { |
169 |
+ |
try { |
170 |
+ |
ExecutorService e = Executors.newFixedThreadPool(2, null); |
171 |
+ |
shouldThrow(); |
172 |
+ |
} |
173 |
+ |
catch(NullPointerException success) { |
174 |
+ |
} |
175 |
+ |
} |
176 |
+ |
|
177 |
+ |
/** |
178 |
+ |
* A new newFixedThreadPool with 0 threads throws IAE |
179 |
+ |
*/ |
180 |
+ |
public void testNewFixedThreadPool4() { |
181 |
+ |
try { |
182 |
+ |
ExecutorService e = Executors.newFixedThreadPool(0); |
183 |
+ |
shouldThrow(); |
184 |
+ |
} |
185 |
+ |
catch(IllegalArgumentException success) { |
186 |
+ |
} |
187 |
+ |
} |
188 |
+ |
|
189 |
+ |
/** |
190 |
+ |
* execute of runnable runs it to completion |
191 |
+ |
*/ |
192 |
+ |
public void testExecuteRunnable () { |
193 |
+ |
try { |
194 |
+ |
Executor e = new DirectExecutor(); |
195 |
+ |
TrackedRunnable task = new TrackedRunnable(); |
196 |
+ |
assertFalse(task.done); |
197 |
+ |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
198 |
+ |
String result = future.get(); |
199 |
+ |
assertTrue(task.done); |
200 |
+ |
assertSame(TEST_STRING, result); |
201 |
+ |
} |
202 |
+ |
catch (ExecutionException ex) { |
203 |
+ |
unexpectedException(); |
204 |
+ |
} |
205 |
+ |
catch (InterruptedException ex) { |
206 |
+ |
unexpectedException(); |
207 |
+ |
} |
208 |
+ |
} |
209 |
+ |
|
210 |
+ |
/** |
211 |
+ |
* invoke of a runnable runs it to completion |
212 |
+ |
*/ |
213 |
+ |
public void testInvokeRunnable () { |
214 |
+ |
try { |
215 |
+ |
Executor e = new DirectExecutor(); |
216 |
+ |
TrackedRunnable task = new TrackedRunnable(); |
217 |
+ |
assertFalse(task.done); |
218 |
+ |
Executors.invoke(e, task); |
219 |
+ |
assertTrue(task.done); |
220 |
+ |
} |
221 |
+ |
catch (ExecutionException ex) { |
222 |
+ |
unexpectedException(); |
223 |
+ |
} |
224 |
+ |
catch (InterruptedException ex) { |
225 |
+ |
unexpectedException(); |
226 |
+ |
} |
227 |
+ |
} |
228 |
+ |
|
229 |
+ |
/** |
230 |
+ |
* execute of a callable runs it to completion |
231 |
+ |
*/ |
232 |
+ |
public void testExecuteCallable () { |
233 |
+ |
try { |
234 |
+ |
Executor e = new DirectExecutor(); |
235 |
+ |
Future<String> future = Executors.execute(e, new StringTask()); |
236 |
+ |
String result = future.get(); |
237 |
+ |
assertSame(TEST_STRING, result); |
238 |
+ |
} |
239 |
+ |
catch (ExecutionException ex) { |
240 |
+ |
unexpectedException(); |
241 |
+ |
} |
242 |
+ |
catch (InterruptedException ex) { |
243 |
+ |
unexpectedException(); |
244 |
+ |
} |
245 |
+ |
} |
246 |
+ |
|
247 |
+ |
/** |
248 |
+ |
* invoke of a collable runs it to completion |
249 |
+ |
*/ |
250 |
+ |
public void testInvokeCallable () { |
251 |
+ |
try { |
252 |
+ |
Executor e = new DirectExecutor(); |
253 |
+ |
String result = Executors.invoke(e, new StringTask()); |
254 |
+ |
|
255 |
+ |
assertSame(TEST_STRING, result); |
256 |
+ |
} |
257 |
+ |
catch (ExecutionException ex) { |
258 |
+ |
unexpectedException(); |
259 |
+ |
} |
260 |
+ |
catch (InterruptedException ex) { |
261 |
+ |
unexpectedException(); |
262 |
+ |
} |
263 |
+ |
} |
264 |
+ |
|
265 |
+ |
/** |
266 |
+ |
* execute with null executor throws NPE |
267 |
+ |
*/ |
268 |
+ |
public void testNullExecuteRunnable () { |
269 |
+ |
try { |
270 |
+ |
TrackedRunnable task = new TrackedRunnable(); |
271 |
+ |
assertFalse(task.done); |
272 |
+ |
Future<String> future = Executors.execute(null, task, TEST_STRING); |
273 |
+ |
shouldThrow(); |
274 |
+ |
} |
275 |
+ |
catch (NullPointerException success) { |
276 |
+ |
} |
277 |
+ |
catch (Exception ex) { |
278 |
+ |
unexpectedException(); |
279 |
+ |
} |
280 |
+ |
} |
281 |
|
|
282 |
+ |
/** |
283 |
+ |
* execute with a null runnable throws NPE |
284 |
+ |
*/ |
285 |
+ |
public void testExecuteNullRunnable() { |
286 |
+ |
try { |
287 |
+ |
Executor e = new DirectExecutor(); |
288 |
+ |
TrackedRunnable task = null; |
289 |
+ |
Future<String> future = Executors.execute(e, task, TEST_STRING); |
290 |
+ |
shouldThrow(); |
291 |
+ |
} |
292 |
+ |
catch (NullPointerException success) { |
293 |
+ |
} |
294 |
+ |
catch (Exception ex) { |
295 |
+ |
unexpectedException(); |
296 |
+ |
} |
297 |
+ |
} |
298 |
|
|
299 |
|
/** |
300 |
< |
* execute(Executor, Runnable) will throw |
301 |
< |
* RejectedExecutionException Attempting to execute a runnable on |
302 |
< |
* a full ThreadPool will cause such an exception here, up to 5 |
303 |
< |
* runnables are attempted on a pool capable on handling one |
304 |
< |
* until it throws an exception |
300 |
> |
* invoke of a null runnable throws NPE |
301 |
> |
*/ |
302 |
> |
public void testInvokeNullRunnable () { |
303 |
> |
try { |
304 |
> |
Executor e = new DirectExecutor(); |
305 |
> |
TrackedRunnable task = null; |
306 |
> |
Executors.invoke(e, task); |
307 |
> |
shouldThrow(); |
308 |
> |
} |
309 |
> |
catch (NullPointerException success) { |
310 |
> |
} |
311 |
> |
catch (Exception ex) { |
312 |
> |
unexpectedException(); |
313 |
> |
} |
314 |
> |
} |
315 |
> |
|
316 |
> |
/** |
317 |
> |
* execute of a null callable throws NPE |
318 |
> |
*/ |
319 |
> |
public void testExecuteNullCallable () { |
320 |
> |
try { |
321 |
> |
Executor e = new DirectExecutor(); |
322 |
> |
StringTask t = null; |
323 |
> |
Future<String> future = Executors.execute(e, t); |
324 |
> |
shouldThrow(); |
325 |
> |
} |
326 |
> |
catch (NullPointerException success) { |
327 |
> |
} |
328 |
> |
catch (Exception ex) { |
329 |
> |
unexpectedException(); |
330 |
> |
} |
331 |
> |
} |
332 |
> |
|
333 |
> |
/** |
334 |
> |
* invoke of a null callable throws NPE |
335 |
> |
*/ |
336 |
> |
public void testInvokeNullCallable () { |
337 |
> |
try { |
338 |
> |
Executor e = new DirectExecutor(); |
339 |
> |
StringTask t = null; |
340 |
> |
String result = Executors.invoke(e, t); |
341 |
> |
shouldThrow(); |
342 |
> |
} |
343 |
> |
catch (NullPointerException success) { |
344 |
> |
} |
345 |
> |
catch (Exception ex) { |
346 |
> |
unexpectedException(); |
347 |
> |
} |
348 |
> |
} |
349 |
> |
|
350 |
> |
/** |
351 |
> |
* execute(Executor, Runnable) throws RejectedExecutionException |
352 |
> |
* if saturated. |
353 |
|
*/ |
354 |
|
public void testExecute1() { |
355 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
< |
* execute(Executor, Callable) will throw |
368 |
< |
* 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 |
367 |
> |
* execute(Executor, Callable)throws RejectedExecutionException |
368 |
> |
* if saturated. |
369 |
|
*/ |
370 |
|
public void testExecute2() { |
371 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
380 |
|
|
381 |
|
|
382 |
|
/** |
383 |
< |
* invoke(Executor, Runnable) throws InterruptedException |
384 |
< |
* A single use of invoke starts that will wait long enough |
125 |
< |
* for the invoking thread to be interrupted |
383 |
> |
* invoke(Executor, Runnable) throws InterruptedException if |
384 |
> |
* caller interrupted. |
385 |
|
*/ |
386 |
< |
public void testInvoke2() { |
386 |
> |
public void testInterruptedInvoke() { |
387 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
388 |
|
Thread t = new Thread(new Runnable() { |
389 |
|
public void run() { |
415 |
|
} |
416 |
|
|
417 |
|
/** |
418 |
< |
* invoke(Executor, Runnable) will throw |
419 |
< |
* ExecutionException An ExecutionException occurs when the |
161 |
< |
* underlying Runnable throws an exception, here the |
162 |
< |
* DivideByZeroException will cause an ExecutionException |
418 |
> |
* invoke(Executor, Runnable) throws ExecutionException if |
419 |
> |
* runnable throws exception. |
420 |
|
*/ |
421 |
|
public void testInvoke3() { |
422 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
442 |
|
|
443 |
|
|
444 |
|
/** |
445 |
< |
* invoke(Executor, Callable) throws |
446 |
< |
* InterruptedException A single use of invoke starts that will |
190 |
< |
* wait long enough for the invoking thread to be interrupted |
445 |
> |
* invoke(Executor, Callable) throws InterruptedException if |
446 |
> |
* callable throws exception |
447 |
|
*/ |
448 |
|
public void testInvoke5() { |
449 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
482 |
|
} |
483 |
|
|
484 |
|
/** |
485 |
< |
* invoke(Executor, Callable) will throw ExecutionException |
486 |
< |
* An ExecutionException occurs when the underlying Runnable throws |
231 |
< |
* an exception, here the DivideByZeroException will cause an ExecutionException |
485 |
> |
* invoke(Executor, Callable) will throw ExecutionException |
486 |
> |
* if callable throws exception |
487 |
|
*/ |
488 |
|
public void testInvoke6() { |
489 |
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
501 |
|
} |
502 |
|
|
503 |
|
shouldThrow(); |
504 |
< |
} catch(RejectedExecutionException e){} |
505 |
< |
catch(InterruptedException e2){} |
506 |
< |
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) { |
504 |
> |
} |
505 |
> |
catch(ExecutionException success){ |
506 |
> |
} catch(Exception e) { |
507 |
|
unexpectedException(); |
508 |
|
} |
509 |
+ |
joinPool(p); |
510 |
|
} |
511 |
|
|
313 |
– |
/** |
314 |
– |
* |
315 |
– |
*/ |
316 |
– |
public void testInvokeCallable () { |
317 |
– |
try { |
318 |
– |
Executor e = new DirectExecutor(); |
319 |
– |
String result = Executors.invoke(e, new StringTask()); |
512 |
|
|
321 |
– |
assertSame(TEST_STRING, result); |
322 |
– |
} |
323 |
– |
catch (ExecutionException ex) { |
324 |
– |
unexpectedException(); |
325 |
– |
} |
326 |
– |
catch (InterruptedException ex) { |
327 |
– |
unexpectedException(); |
328 |
– |
} |
329 |
– |
} |
513 |
|
|
514 |
|
/** |
515 |
|
* timeouts from execute will time out if they compute too long. |