10 |
|
import junit.framework.*; |
11 |
|
import java.util.*; |
12 |
|
import java.util.concurrent.*; |
13 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.math.BigInteger; |
15 |
|
import java.security.*; |
16 |
|
|
17 |
< |
public class AbstractExecutorServiceTest extends JSR166TestCase{ |
17 |
> |
public class AbstractExecutorServiceTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
|
junit.textui.TestRunner.run (suite()); |
20 |
|
} |
39 |
|
/** |
40 |
|
* execute(runnable) runs it to completion |
41 |
|
*/ |
42 |
< |
public void testExecuteRunnable() { |
43 |
< |
try { |
44 |
< |
ExecutorService e = new DirectExecutorService(); |
45 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
46 |
< |
assertFalse(task.done); |
47 |
< |
Future<?> future = e.submit(task); |
48 |
< |
future.get(); |
48 |
< |
assertTrue(task.done); |
49 |
< |
} |
50 |
< |
catch (ExecutionException ex) { |
51 |
< |
unexpectedException(); |
52 |
< |
} |
53 |
< |
catch (InterruptedException ex) { |
54 |
< |
unexpectedException(); |
55 |
< |
} |
42 |
> |
public void testExecuteRunnable() throws Exception { |
43 |
> |
ExecutorService e = new DirectExecutorService(); |
44 |
> |
TrackedShortRunnable task = new TrackedShortRunnable(); |
45 |
> |
assertFalse(task.done); |
46 |
> |
Future<?> future = e.submit(task); |
47 |
> |
future.get(); |
48 |
> |
assertTrue(task.done); |
49 |
|
} |
50 |
|
|
51 |
|
|
52 |
|
/** |
53 |
|
* Completed submit(callable) returns result |
54 |
|
*/ |
55 |
< |
public void testSubmitCallable() { |
56 |
< |
try { |
57 |
< |
ExecutorService e = new DirectExecutorService(); |
58 |
< |
Future<String> future = e.submit(new StringTask()); |
59 |
< |
String result = future.get(); |
67 |
< |
assertSame(TEST_STRING, result); |
68 |
< |
} |
69 |
< |
catch (ExecutionException ex) { |
70 |
< |
unexpectedException(); |
71 |
< |
} |
72 |
< |
catch (InterruptedException ex) { |
73 |
< |
unexpectedException(); |
74 |
< |
} |
55 |
> |
public void testSubmitCallable() throws Exception { |
56 |
> |
ExecutorService e = new DirectExecutorService(); |
57 |
> |
Future<String> future = e.submit(new StringTask()); |
58 |
> |
String result = future.get(); |
59 |
> |
assertSame(TEST_STRING, result); |
60 |
|
} |
61 |
|
|
62 |
|
/** |
63 |
|
* Completed submit(runnable) returns successfully |
64 |
|
*/ |
65 |
< |
public void testSubmitRunnable() { |
66 |
< |
try { |
67 |
< |
ExecutorService e = new DirectExecutorService(); |
68 |
< |
Future<?> future = e.submit(new NoOpRunnable()); |
69 |
< |
future.get(); |
85 |
< |
assertTrue(future.isDone()); |
86 |
< |
} |
87 |
< |
catch (ExecutionException ex) { |
88 |
< |
unexpectedException(); |
89 |
< |
} |
90 |
< |
catch (InterruptedException ex) { |
91 |
< |
unexpectedException(); |
92 |
< |
} |
65 |
> |
public void testSubmitRunnable() throws Exception { |
66 |
> |
ExecutorService e = new DirectExecutorService(); |
67 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
68 |
> |
future.get(); |
69 |
> |
assertTrue(future.isDone()); |
70 |
|
} |
71 |
|
|
72 |
|
/** |
73 |
|
* Completed submit(runnable, result) returns result |
74 |
|
*/ |
75 |
< |
public void testSubmitRunnable2() { |
76 |
< |
try { |
77 |
< |
ExecutorService e = new DirectExecutorService(); |
78 |
< |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
79 |
< |
String result = future.get(); |
103 |
< |
assertSame(TEST_STRING, result); |
104 |
< |
} |
105 |
< |
catch (ExecutionException ex) { |
106 |
< |
unexpectedException(); |
107 |
< |
} |
108 |
< |
catch (InterruptedException ex) { |
109 |
< |
unexpectedException(); |
110 |
< |
} |
75 |
> |
public void testSubmitRunnable2() throws Exception { |
76 |
> |
ExecutorService e = new DirectExecutorService(); |
77 |
> |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
78 |
> |
String result = future.get(); |
79 |
> |
assertSame(TEST_STRING, result); |
80 |
|
} |
81 |
|
|
82 |
|
|
83 |
|
/** |
84 |
|
* A submitted privileged action to completion |
85 |
|
*/ |
86 |
< |
public void testSubmitPrivilegedAction() { |
86 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
87 |
|
Policy savedPolicy = null; |
88 |
|
try { |
89 |
|
savedPolicy = Policy.getPolicy(); |
91 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
92 |
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
93 |
|
Policy.setPolicy(policy); |
94 |
< |
} catch(AccessControlException ok) { |
94 |
> |
} catch (AccessControlException ok) { |
95 |
|
return; |
96 |
|
} |
97 |
|
try { |
104 |
|
Object result = future.get(); |
105 |
|
assertSame(TEST_STRING, result); |
106 |
|
} |
138 |
– |
catch (ExecutionException ex) { |
139 |
– |
unexpectedException(); |
140 |
– |
} |
141 |
– |
catch (InterruptedException ex) { |
142 |
– |
unexpectedException(); |
143 |
– |
} |
107 |
|
finally { |
108 |
|
try { |
109 |
|
Policy.setPolicy(savedPolicy); |
110 |
< |
} catch(AccessControlException ok) { |
110 |
> |
} catch (AccessControlException ok) { |
111 |
|
return; |
112 |
|
} |
113 |
|
} |
116 |
|
/** |
117 |
|
* A submitted a privileged exception action runs to completion |
118 |
|
*/ |
119 |
< |
public void testSubmitPrivilegedExceptionAction() { |
119 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
120 |
|
Policy savedPolicy = null; |
121 |
|
try { |
122 |
|
savedPolicy = Policy.getPolicy(); |
124 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
125 |
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
126 |
|
Policy.setPolicy(policy); |
127 |
< |
} catch(AccessControlException ok) { |
127 |
> |
} catch (AccessControlException ok) { |
128 |
|
return; |
129 |
|
} |
130 |
|
|
138 |
|
Object result = future.get(); |
139 |
|
assertSame(TEST_STRING, result); |
140 |
|
} |
178 |
– |
catch (ExecutionException ex) { |
179 |
– |
unexpectedException(); |
180 |
– |
} |
181 |
– |
catch (InterruptedException ex) { |
182 |
– |
unexpectedException(); |
183 |
– |
} |
141 |
|
finally { |
142 |
|
Policy.setPolicy(savedPolicy); |
143 |
|
} |
146 |
|
/** |
147 |
|
* A submitted failed privileged exception action reports exception |
148 |
|
*/ |
149 |
< |
public void testSubmitFailedPrivilegedExceptionAction() { |
149 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
150 |
|
Policy savedPolicy = null; |
151 |
|
try { |
152 |
|
savedPolicy = Policy.getPolicy(); |
154 |
|
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
155 |
|
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
156 |
|
Policy.setPolicy(policy); |
157 |
< |
} catch(AccessControlException ok) { |
157 |
> |
} catch (AccessControlException ok) { |
158 |
|
return; |
159 |
|
} |
160 |
|
|
204 |
– |
|
161 |
|
try { |
162 |
|
ExecutorService e = new DirectExecutorService(); |
163 |
|
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
165 |
|
throw new IndexOutOfBoundsException(); |
166 |
|
}})); |
167 |
|
|
168 |
< |
Object result = future.get(); |
168 |
> |
future.get(); |
169 |
|
shouldThrow(); |
170 |
< |
} |
171 |
< |
catch (ExecutionException success) { |
216 |
< |
} |
217 |
< |
catch (InterruptedException ex) { |
218 |
< |
unexpectedException(); |
170 |
> |
} catch (ExecutionException success) { |
171 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
172 |
|
} |
173 |
|
finally { |
174 |
|
Policy.setPolicy(savedPolicy); |
181 |
|
public void testExecuteNullRunnable() { |
182 |
|
try { |
183 |
|
ExecutorService e = new DirectExecutorService(); |
184 |
< |
TrackedShortRunnable task = null; |
232 |
< |
Future<?> future = e.submit(task); |
184 |
> |
e.submit((Runnable) null); |
185 |
|
shouldThrow(); |
186 |
< |
} |
235 |
< |
catch (NullPointerException success) { |
236 |
< |
} |
237 |
< |
catch (Exception ex) { |
238 |
< |
unexpectedException(); |
239 |
< |
} |
186 |
> |
} catch (NullPointerException success) {} |
187 |
|
} |
188 |
|
|
189 |
|
|
193 |
|
public void testSubmitNullCallable() { |
194 |
|
try { |
195 |
|
ExecutorService e = new DirectExecutorService(); |
196 |
< |
StringTask t = null; |
250 |
< |
Future<String> future = e.submit(t); |
196 |
> |
e.submit((Callable) null); |
197 |
|
shouldThrow(); |
198 |
< |
} |
253 |
< |
catch (NullPointerException success) { |
254 |
< |
} |
255 |
< |
catch (Exception ex) { |
256 |
< |
unexpectedException(); |
257 |
< |
} |
198 |
> |
} catch (NullPointerException success) {} |
199 |
|
} |
200 |
|
|
201 |
|
/** |
203 |
|
* executor is saturated. |
204 |
|
*/ |
205 |
|
public void testExecute1() { |
206 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
206 |
> |
ThreadPoolExecutor p = |
207 |
> |
new ThreadPoolExecutor(1, 1, |
208 |
> |
60, TimeUnit.SECONDS, |
209 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
210 |
|
try { |
211 |
< |
|
268 |
< |
for(int i = 0; i < 5; ++i){ |
211 |
> |
for (int i = 0; i < 2; ++i) |
212 |
|
p.submit(new MediumRunnable()); |
213 |
+ |
for (int i = 0; i < 2; ++i) { |
214 |
+ |
try { |
215 |
+ |
p.submit(new MediumRunnable()); |
216 |
+ |
shouldThrow(); |
217 |
+ |
} catch (RejectedExecutionException success) {} |
218 |
|
} |
219 |
< |
shouldThrow(); |
220 |
< |
} catch(RejectedExecutionException success){} |
221 |
< |
joinPool(p); |
219 |
> |
} finally { |
220 |
> |
joinPool(p); |
221 |
> |
} |
222 |
|
} |
223 |
|
|
224 |
|
/** |
226 |
|
* if executor is saturated. |
227 |
|
*/ |
228 |
|
public void testExecute2() { |
229 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
229 |
> |
ThreadPoolExecutor p = |
230 |
> |
new ThreadPoolExecutor(1, 1, |
231 |
> |
60, TimeUnit.SECONDS, |
232 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
233 |
|
try { |
234 |
< |
for(int i = 0; i < 5; ++i) { |
235 |
< |
p.submit(new SmallCallable()); |
234 |
> |
for (int i = 0; i < 2; ++i) |
235 |
> |
p.submit(new MediumRunnable()); |
236 |
> |
for (int i = 0; i < 2; ++i) { |
237 |
> |
try { |
238 |
> |
p.submit(new SmallCallable()); |
239 |
> |
shouldThrow(); |
240 |
> |
} catch (RejectedExecutionException success) {} |
241 |
|
} |
242 |
< |
shouldThrow(); |
243 |
< |
} catch(RejectedExecutionException e){} |
244 |
< |
joinPool(p); |
242 |
> |
} finally { |
243 |
> |
joinPool(p); |
244 |
> |
} |
245 |
|
} |
246 |
|
|
247 |
|
|
249 |
|
* Blocking on submit(callable) throws InterruptedException if |
250 |
|
* caller interrupted. |
251 |
|
*/ |
252 |
< |
public void testInterruptedSubmit() { |
252 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
253 |
|
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
254 |
< |
Thread t = new Thread(new Runnable() { |
255 |
< |
public void run() { |
256 |
< |
try { |
257 |
< |
p.submit(new Callable<Object>() { |
258 |
< |
public Object call() { |
259 |
< |
try { |
260 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
261 |
< |
shouldThrow(); |
262 |
< |
} catch(InterruptedException e){ |
263 |
< |
} |
264 |
< |
return null; |
265 |
< |
} |
266 |
< |
}).get(); |
311 |
< |
} catch(InterruptedException success){ |
312 |
< |
} catch(Exception e) { |
313 |
< |
unexpectedException(); |
314 |
< |
} |
315 |
< |
|
316 |
< |
} |
317 |
< |
}); |
318 |
< |
try { |
319 |
< |
t.start(); |
320 |
< |
Thread.sleep(SHORT_DELAY_MS); |
321 |
< |
t.interrupt(); |
322 |
< |
} catch(Exception e){ |
323 |
< |
unexpectedException(); |
324 |
< |
} |
254 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
255 |
> |
public void realRun() throws Exception { |
256 |
> |
p.submit(new CheckedCallable<Object>() { |
257 |
> |
public Object realCall() |
258 |
> |
throws InterruptedException { |
259 |
> |
Thread.sleep(SMALL_DELAY_MS); |
260 |
> |
return null; |
261 |
> |
}}).get(); |
262 |
> |
}}); |
263 |
> |
|
264 |
> |
t.start(); |
265 |
> |
Thread.sleep(SHORT_DELAY_MS); |
266 |
> |
t.interrupt(); |
267 |
|
joinPool(p); |
268 |
|
} |
269 |
|
|
270 |
|
/** |
271 |
< |
* get of submitted callable throws Exception if callable |
271 |
> |
* get of submitted callable throws InterruptedException if callable |
272 |
|
* interrupted |
273 |
|
*/ |
274 |
< |
public void testSubmitIE() { |
275 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
276 |
< |
|
277 |
< |
final Callable c = new Callable() { |
278 |
< |
public Object call() { |
279 |
< |
try { |
280 |
< |
p.submit(new SmallCallable()).get(); |
281 |
< |
shouldThrow(); |
282 |
< |
} catch(InterruptedException e){} |
283 |
< |
catch(RejectedExecutionException e2){} |
284 |
< |
catch(ExecutionException e3){} |
285 |
< |
return Boolean.TRUE; |
286 |
< |
} |
287 |
< |
}; |
288 |
< |
|
347 |
< |
|
348 |
< |
|
349 |
< |
Thread t = new Thread(new Runnable() { |
350 |
< |
public void run() { |
351 |
< |
try { |
352 |
< |
c.call(); |
353 |
< |
} catch(Exception e){} |
354 |
< |
} |
355 |
< |
}); |
356 |
< |
try { |
357 |
< |
t.start(); |
358 |
< |
Thread.sleep(SHORT_DELAY_MS); |
359 |
< |
t.interrupt(); |
360 |
< |
t.join(); |
361 |
< |
} catch(InterruptedException e){ |
362 |
< |
unexpectedException(); |
363 |
< |
} |
364 |
< |
|
274 |
> |
public void testSubmitIE() throws InterruptedException { |
275 |
> |
final ThreadPoolExecutor p = |
276 |
> |
new ThreadPoolExecutor(1, 1, |
277 |
> |
60, TimeUnit.SECONDS, |
278 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
279 |
> |
|
280 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
281 |
> |
public void realRun() throws Exception { |
282 |
> |
p.submit(new SmallCallable()).get(); |
283 |
> |
}}); |
284 |
> |
|
285 |
> |
t.start(); |
286 |
> |
Thread.sleep(SHORT_DELAY_MS); |
287 |
> |
t.interrupt(); |
288 |
> |
t.join(); |
289 |
|
joinPool(p); |
290 |
|
} |
291 |
|
|
293 |
|
* get of submit(callable) throws ExecutionException if callable |
294 |
|
* throws exception |
295 |
|
*/ |
296 |
< |
public void testSubmitEE() { |
297 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
296 |
> |
public void testSubmitEE() throws InterruptedException { |
297 |
> |
ThreadPoolExecutor p = |
298 |
> |
new ThreadPoolExecutor(1, 1, |
299 |
> |
60, TimeUnit.SECONDS, |
300 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
301 |
|
|
302 |
< |
try { |
303 |
< |
Callable c = new Callable() { |
377 |
< |
public Object call() { |
378 |
< |
int i = 5/0; |
379 |
< |
return Boolean.TRUE; |
380 |
< |
} |
381 |
< |
}; |
382 |
< |
|
383 |
< |
for(int i =0; i < 5; i++){ |
384 |
< |
p.submit(c).get(); |
385 |
< |
} |
302 |
> |
Callable c = new Callable() { |
303 |
> |
public Object call() { return 5/0; }}; |
304 |
|
|
305 |
+ |
try { |
306 |
+ |
p.submit(c).get(); |
307 |
|
shouldThrow(); |
308 |
< |
} |
309 |
< |
catch(ExecutionException success){ |
390 |
< |
} catch(Exception e) { |
391 |
< |
unexpectedException(); |
308 |
> |
} catch (ExecutionException success) { |
309 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
310 |
|
} |
311 |
|
joinPool(p); |
312 |
|
} |
314 |
|
/** |
315 |
|
* invokeAny(null) throws NPE |
316 |
|
*/ |
317 |
< |
public void testInvokeAny1() { |
317 |
> |
public void testInvokeAny1() |
318 |
> |
throws InterruptedException, ExecutionException { |
319 |
|
ExecutorService e = new DirectExecutorService(); |
320 |
|
try { |
321 |
|
e.invokeAny(null); |
322 |
+ |
shouldThrow(); |
323 |
|
} catch (NullPointerException success) { |
404 |
– |
} catch(Exception ex) { |
405 |
– |
unexpectedException(); |
324 |
|
} finally { |
325 |
|
joinPool(e); |
326 |
|
} |
329 |
|
/** |
330 |
|
* invokeAny(empty collection) throws IAE |
331 |
|
*/ |
332 |
< |
public void testInvokeAny2() { |
332 |
> |
public void testInvokeAny2() |
333 |
> |
throws InterruptedException, ExecutionException { |
334 |
|
ExecutorService e = new DirectExecutorService(); |
335 |
|
try { |
336 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
337 |
+ |
shouldThrow(); |
338 |
|
} catch (IllegalArgumentException success) { |
419 |
– |
} catch(Exception ex) { |
420 |
– |
unexpectedException(); |
339 |
|
} finally { |
340 |
|
joinPool(e); |
341 |
|
} |
344 |
|
/** |
345 |
|
* invokeAny(c) throws NPE if c has null elements |
346 |
|
*/ |
347 |
< |
public void testInvokeAny3() { |
347 |
> |
public void testInvokeAny3() throws Exception { |
348 |
|
ExecutorService e = new DirectExecutorService(); |
349 |
+ |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
350 |
+ |
l.add(new Callable<Integer>() { |
351 |
+ |
public Integer call() { return 5/0; }}); |
352 |
+ |
l.add(null); |
353 |
|
try { |
432 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
433 |
– |
l.add(new StringTask()); |
434 |
– |
l.add(null); |
354 |
|
e.invokeAny(l); |
355 |
+ |
shouldThrow(); |
356 |
|
} catch (NullPointerException success) { |
437 |
– |
} catch(Exception ex) { |
438 |
– |
ex.printStackTrace(); |
439 |
– |
unexpectedException(); |
357 |
|
} finally { |
358 |
|
joinPool(e); |
359 |
|
} |
362 |
|
/** |
363 |
|
* invokeAny(c) throws ExecutionException if no task in c completes |
364 |
|
*/ |
365 |
< |
public void testInvokeAny4() { |
365 |
> |
public void testInvokeAny4() throws InterruptedException { |
366 |
|
ExecutorService e = new DirectExecutorService(); |
367 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
368 |
+ |
l.add(new NPETask()); |
369 |
|
try { |
451 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
452 |
– |
l.add(new NPETask()); |
370 |
|
e.invokeAny(l); |
371 |
< |
} catch(ExecutionException success) { |
372 |
< |
} catch(Exception ex) { |
373 |
< |
unexpectedException(); |
371 |
> |
shouldThrow(); |
372 |
> |
} catch (ExecutionException success) { |
373 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
374 |
|
} finally { |
375 |
|
joinPool(e); |
376 |
|
} |
379 |
|
/** |
380 |
|
* invokeAny(c) returns result of some task in c if at least one completes |
381 |
|
*/ |
382 |
< |
public void testInvokeAny5() { |
382 |
> |
public void testInvokeAny5() throws Exception { |
383 |
|
ExecutorService e = new DirectExecutorService(); |
384 |
|
try { |
385 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
385 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
386 |
|
l.add(new StringTask()); |
387 |
|
l.add(new StringTask()); |
388 |
|
String result = e.invokeAny(l); |
389 |
|
assertSame(TEST_STRING, result); |
473 |
– |
} catch (ExecutionException success) { |
474 |
– |
} catch(Exception ex) { |
475 |
– |
unexpectedException(); |
390 |
|
} finally { |
391 |
|
joinPool(e); |
392 |
|
} |
395 |
|
/** |
396 |
|
* invokeAll(null) throws NPE |
397 |
|
*/ |
398 |
< |
public void testInvokeAll1() { |
398 |
> |
public void testInvokeAll1() throws InterruptedException { |
399 |
|
ExecutorService e = new DirectExecutorService(); |
400 |
|
try { |
401 |
|
e.invokeAll(null); |
402 |
+ |
shouldThrow(); |
403 |
|
} catch (NullPointerException success) { |
489 |
– |
} catch(Exception ex) { |
490 |
– |
unexpectedException(); |
404 |
|
} finally { |
405 |
|
joinPool(e); |
406 |
|
} |
409 |
|
/** |
410 |
|
* invokeAll(empty collection) returns empty collection |
411 |
|
*/ |
412 |
< |
public void testInvokeAll2() { |
412 |
> |
public void testInvokeAll2() throws InterruptedException { |
413 |
|
ExecutorService e = new DirectExecutorService(); |
414 |
|
try { |
415 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
416 |
|
assertTrue(r.isEmpty()); |
504 |
– |
} catch(Exception ex) { |
505 |
– |
unexpectedException(); |
417 |
|
} finally { |
418 |
|
joinPool(e); |
419 |
|
} |
422 |
|
/** |
423 |
|
* invokeAll(c) throws NPE if c has null elements |
424 |
|
*/ |
425 |
< |
public void testInvokeAll3() { |
425 |
> |
public void testInvokeAll3() throws InterruptedException { |
426 |
|
ExecutorService e = new DirectExecutorService(); |
427 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
428 |
+ |
l.add(new StringTask()); |
429 |
+ |
l.add(null); |
430 |
|
try { |
517 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
518 |
– |
l.add(new StringTask()); |
519 |
– |
l.add(null); |
431 |
|
e.invokeAll(l); |
432 |
+ |
shouldThrow(); |
433 |
|
} catch (NullPointerException success) { |
522 |
– |
} catch(Exception ex) { |
523 |
– |
unexpectedException(); |
434 |
|
} finally { |
435 |
|
joinPool(e); |
436 |
|
} |
439 |
|
/** |
440 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
441 |
|
*/ |
442 |
< |
public void testInvokeAll4() { |
442 |
> |
public void testInvokeAll4() throws Exception { |
443 |
|
ExecutorService e = new DirectExecutorService(); |
444 |
|
try { |
445 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
445 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
446 |
|
l.add(new NPETask()); |
447 |
< |
List<Future<String>> result = e.invokeAll(l); |
448 |
< |
assertEquals(1, result.size()); |
449 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
450 |
< |
it.next().get(); |
451 |
< |
} catch(ExecutionException success) { |
452 |
< |
} catch(Exception ex) { |
453 |
< |
unexpectedException(); |
447 |
> |
List<Future<String>> futures = e.invokeAll(l); |
448 |
> |
assertEquals(1, futures.size()); |
449 |
> |
try { |
450 |
> |
futures.get(0).get(); |
451 |
> |
shouldThrow(); |
452 |
> |
} catch (ExecutionException success) { |
453 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
454 |
> |
} |
455 |
|
} finally { |
456 |
|
joinPool(e); |
457 |
|
} |
460 |
|
/** |
461 |
|
* invokeAll(c) returns results of all completed tasks in c |
462 |
|
*/ |
463 |
< |
public void testInvokeAll5() { |
463 |
> |
public void testInvokeAll5() throws Exception { |
464 |
|
ExecutorService e = new DirectExecutorService(); |
465 |
|
try { |
466 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
466 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
467 |
|
l.add(new StringTask()); |
468 |
|
l.add(new StringTask()); |
469 |
< |
List<Future<String>> result = e.invokeAll(l); |
470 |
< |
assertEquals(2, result.size()); |
471 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
472 |
< |
assertSame(TEST_STRING, it.next().get()); |
562 |
< |
} catch (ExecutionException success) { |
563 |
< |
} catch(Exception ex) { |
564 |
< |
unexpectedException(); |
469 |
> |
List<Future<String>> futures = e.invokeAll(l); |
470 |
> |
assertEquals(2, futures.size()); |
471 |
> |
for (Future<String> future : futures) |
472 |
> |
assertSame(TEST_STRING, future.get()); |
473 |
|
} finally { |
474 |
|
joinPool(e); |
475 |
|
} |
479 |
|
/** |
480 |
|
* timed invokeAny(null) throws NPE |
481 |
|
*/ |
482 |
< |
public void testTimedInvokeAny1() { |
482 |
> |
public void testTimedInvokeAny1() throws Exception { |
483 |
|
ExecutorService e = new DirectExecutorService(); |
484 |
|
try { |
485 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
485 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
486 |
> |
shouldThrow(); |
487 |
|
} catch (NullPointerException success) { |
579 |
– |
} catch(Exception ex) { |
580 |
– |
unexpectedException(); |
488 |
|
} finally { |
489 |
|
joinPool(e); |
490 |
|
} |
493 |
|
/** |
494 |
|
* timed invokeAny(null time unit) throws NPE |
495 |
|
*/ |
496 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
496 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
497 |
|
ExecutorService e = new DirectExecutorService(); |
498 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
499 |
+ |
l.add(new StringTask()); |
500 |
|
try { |
592 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
593 |
– |
l.add(new StringTask()); |
501 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
502 |
+ |
shouldThrow(); |
503 |
|
} catch (NullPointerException success) { |
596 |
– |
} catch(Exception ex) { |
597 |
– |
unexpectedException(); |
504 |
|
} finally { |
505 |
|
joinPool(e); |
506 |
|
} |
509 |
|
/** |
510 |
|
* timed invokeAny(empty collection) throws IAE |
511 |
|
*/ |
512 |
< |
public void testTimedInvokeAny2() { |
512 |
> |
public void testTimedInvokeAny2() throws Exception { |
513 |
|
ExecutorService e = new DirectExecutorService(); |
514 |
|
try { |
515 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
515 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
516 |
> |
shouldThrow(); |
517 |
|
} catch (IllegalArgumentException success) { |
611 |
– |
} catch(Exception ex) { |
612 |
– |
unexpectedException(); |
518 |
|
} finally { |
519 |
|
joinPool(e); |
520 |
|
} |
523 |
|
/** |
524 |
|
* timed invokeAny(c) throws NPE if c has null elements |
525 |
|
*/ |
526 |
< |
public void testTimedInvokeAny3() { |
526 |
> |
public void testTimedInvokeAny3() throws Exception { |
527 |
|
ExecutorService e = new DirectExecutorService(); |
528 |
+ |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
529 |
+ |
l.add(new Callable<Integer>() { |
530 |
+ |
public Integer call() { return 5/0; }}); |
531 |
+ |
l.add(null); |
532 |
|
try { |
533 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
534 |
< |
l.add(new StringTask()); |
626 |
< |
l.add(null); |
627 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
533 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
534 |
> |
shouldThrow(); |
535 |
|
} catch (NullPointerException success) { |
629 |
– |
} catch(Exception ex) { |
630 |
– |
ex.printStackTrace(); |
631 |
– |
unexpectedException(); |
536 |
|
} finally { |
537 |
|
joinPool(e); |
538 |
|
} |
541 |
|
/** |
542 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
543 |
|
*/ |
544 |
< |
public void testTimedInvokeAny4() { |
544 |
> |
public void testTimedInvokeAny4() throws Exception { |
545 |
|
ExecutorService e = new DirectExecutorService(); |
546 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
547 |
+ |
l.add(new NPETask()); |
548 |
|
try { |
549 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
550 |
< |
l.add(new NPETask()); |
551 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
552 |
< |
} catch(ExecutionException success) { |
647 |
< |
} catch(Exception ex) { |
648 |
< |
unexpectedException(); |
549 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
550 |
> |
shouldThrow(); |
551 |
> |
} catch (ExecutionException success) { |
552 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
553 |
|
} finally { |
554 |
|
joinPool(e); |
555 |
|
} |
558 |
|
/** |
559 |
|
* timed invokeAny(c) returns result of some task in c |
560 |
|
*/ |
561 |
< |
public void testTimedInvokeAny5() { |
561 |
> |
public void testTimedInvokeAny5() throws Exception { |
562 |
|
ExecutorService e = new DirectExecutorService(); |
563 |
|
try { |
564 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
564 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
565 |
|
l.add(new StringTask()); |
566 |
|
l.add(new StringTask()); |
567 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
567 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
568 |
|
assertSame(TEST_STRING, result); |
665 |
– |
} catch (ExecutionException success) { |
666 |
– |
} catch(Exception ex) { |
667 |
– |
unexpectedException(); |
569 |
|
} finally { |
570 |
|
joinPool(e); |
571 |
|
} |
574 |
|
/** |
575 |
|
* timed invokeAll(null) throws NPE |
576 |
|
*/ |
577 |
< |
public void testTimedInvokeAll1() { |
577 |
> |
public void testTimedInvokeAll1() throws InterruptedException { |
578 |
|
ExecutorService e = new DirectExecutorService(); |
579 |
|
try { |
580 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
580 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
581 |
> |
shouldThrow(); |
582 |
|
} catch (NullPointerException success) { |
681 |
– |
} catch(Exception ex) { |
682 |
– |
unexpectedException(); |
583 |
|
} finally { |
584 |
|
joinPool(e); |
585 |
|
} |
588 |
|
/** |
589 |
|
* timed invokeAll(null time unit) throws NPE |
590 |
|
*/ |
591 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
591 |
> |
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
592 |
|
ExecutorService e = new DirectExecutorService(); |
593 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
594 |
+ |
l.add(new StringTask()); |
595 |
|
try { |
694 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
695 |
– |
l.add(new StringTask()); |
596 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
597 |
+ |
shouldThrow(); |
598 |
|
} catch (NullPointerException success) { |
698 |
– |
} catch(Exception ex) { |
699 |
– |
unexpectedException(); |
599 |
|
} finally { |
600 |
|
joinPool(e); |
601 |
|
} |
604 |
|
/** |
605 |
|
* timed invokeAll(empty collection) returns empty collection |
606 |
|
*/ |
607 |
< |
public void testTimedInvokeAll2() { |
607 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
608 |
|
ExecutorService e = new DirectExecutorService(); |
609 |
|
try { |
610 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
610 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
611 |
|
assertTrue(r.isEmpty()); |
713 |
– |
} catch(Exception ex) { |
714 |
– |
unexpectedException(); |
612 |
|
} finally { |
613 |
|
joinPool(e); |
614 |
|
} |
617 |
|
/** |
618 |
|
* timed invokeAll(c) throws NPE if c has null elements |
619 |
|
*/ |
620 |
< |
public void testTimedInvokeAll3() { |
620 |
> |
public void testTimedInvokeAll3() throws InterruptedException { |
621 |
|
ExecutorService e = new DirectExecutorService(); |
622 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
623 |
+ |
l.add(new StringTask()); |
624 |
+ |
l.add(null); |
625 |
|
try { |
626 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
627 |
< |
l.add(new StringTask()); |
728 |
< |
l.add(null); |
729 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
626 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
627 |
> |
shouldThrow(); |
628 |
|
} catch (NullPointerException success) { |
731 |
– |
} catch(Exception ex) { |
732 |
– |
unexpectedException(); |
629 |
|
} finally { |
630 |
|
joinPool(e); |
631 |
|
} |
634 |
|
/** |
635 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
636 |
|
*/ |
637 |
< |
public void testTimedInvokeAll4() { |
637 |
> |
public void testTimedInvokeAll4() throws Exception { |
638 |
|
ExecutorService e = new DirectExecutorService(); |
639 |
|
try { |
640 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
640 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
641 |
|
l.add(new NPETask()); |
642 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
643 |
< |
assertEquals(1, result.size()); |
644 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
645 |
< |
it.next().get(); |
646 |
< |
} catch(ExecutionException success) { |
647 |
< |
} catch(Exception ex) { |
648 |
< |
unexpectedException(); |
642 |
> |
List<Future<String>> futures = |
643 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
644 |
> |
assertEquals(1, futures.size()); |
645 |
> |
try { |
646 |
> |
futures.get(0).get(); |
647 |
> |
shouldThrow(); |
648 |
> |
} catch (ExecutionException success) { |
649 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
650 |
> |
} |
651 |
|
} finally { |
652 |
|
joinPool(e); |
653 |
|
} |
656 |
|
/** |
657 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
658 |
|
*/ |
659 |
< |
public void testTimedInvokeAll5() { |
659 |
> |
public void testTimedInvokeAll5() throws Exception { |
660 |
|
ExecutorService e = new DirectExecutorService(); |
661 |
|
try { |
662 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
662 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
663 |
|
l.add(new StringTask()); |
664 |
|
l.add(new StringTask()); |
665 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
666 |
< |
assertEquals(2, result.size()); |
667 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
668 |
< |
assertSame(TEST_STRING, it.next().get()); |
669 |
< |
} catch (ExecutionException success) { |
772 |
< |
} catch(Exception ex) { |
773 |
< |
unexpectedException(); |
665 |
> |
List<Future<String>> futures = |
666 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
667 |
> |
assertEquals(2, futures.size()); |
668 |
> |
for (Future<String> future : futures) |
669 |
> |
assertSame(TEST_STRING, future.get()); |
670 |
|
} finally { |
671 |
|
joinPool(e); |
672 |
|
} |
675 |
|
/** |
676 |
|
* timed invokeAll cancels tasks not completed by timeout |
677 |
|
*/ |
678 |
< |
public void testTimedInvokeAll6() { |
678 |
> |
public void testTimedInvokeAll6() throws InterruptedException { |
679 |
|
ExecutorService e = new DirectExecutorService(); |
680 |
|
try { |
681 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
681 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
682 |
|
l.add(new StringTask()); |
683 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
684 |
|
l.add(new StringTask()); |
685 |
< |
List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
686 |
< |
assertEquals(3, result.size()); |
687 |
< |
Iterator<Future<String>> it = result.iterator(); |
685 |
> |
List<Future<String>> futures = |
686 |
> |
e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
687 |
> |
assertEquals(3, futures.size()); |
688 |
> |
Iterator<Future<String>> it = futures.iterator(); |
689 |
|
Future<String> f1 = it.next(); |
690 |
|
Future<String> f2 = it.next(); |
691 |
|
Future<String> f3 = it.next(); |
694 |
|
assertTrue(f2.isDone()); |
695 |
|
assertTrue(f3.isDone()); |
696 |
|
assertTrue(f3.isCancelled()); |
800 |
– |
} catch(Exception ex) { |
801 |
– |
unexpectedException(); |
697 |
|
} finally { |
698 |
|
joinPool(e); |
699 |
|
} |