2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
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()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
23 |
|
} |
24 |
|
|
25 |
< |
/** |
25 |
> |
/** |
26 |
|
* A no-frills implementation of AbstractExecutorService, designed |
27 |
|
* to test the submit methods only. |
28 |
|
*/ |
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 |
84 |
> |
* A submitted privileged action runs to completion |
85 |
|
*/ |
86 |
< |
public void testSubmitPrivilegedAction() { |
87 |
< |
Policy savedPolicy = Policy.getPolicy(); |
88 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
89 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
90 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
122 |
< |
Policy.setPolicy(policy); |
123 |
< |
try { |
124 |
< |
ExecutorService e = new DirectExecutorService(); |
125 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
86 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
87 |
> |
Runnable r = new CheckedRunnable() { |
88 |
> |
public void realRun() throws Exception { |
89 |
> |
ExecutorService e = new DirectExecutorService(); |
90 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
91 |
|
public Object run() { |
92 |
|
return TEST_STRING; |
93 |
|
}})); |
94 |
|
|
95 |
< |
Object result = future.get(); |
96 |
< |
assertSame(TEST_STRING, result); |
97 |
< |
} |
98 |
< |
catch (ExecutionException ex) { |
99 |
< |
unexpectedException(); |
100 |
< |
} |
101 |
< |
catch (InterruptedException ex) { |
137 |
< |
unexpectedException(); |
138 |
< |
} |
139 |
< |
finally { |
140 |
< |
Policy.setPolicy(savedPolicy); |
141 |
< |
} |
95 |
> |
assertSame(TEST_STRING, future.get()); |
96 |
> |
}}; |
97 |
> |
|
98 |
> |
runWithPermissions(r, |
99 |
> |
new RuntimePermission("getClassLoader"), |
100 |
> |
new RuntimePermission("setContextClassLoader"), |
101 |
> |
new RuntimePermission("modifyThread")); |
102 |
|
} |
103 |
|
|
104 |
|
/** |
105 |
< |
* A submitted a privileged exception action runs to completion |
105 |
> |
* A submitted privileged exception action runs to completion |
106 |
|
*/ |
107 |
< |
public void testSubmitPrivilegedExceptionAction() { |
108 |
< |
Policy savedPolicy = Policy.getPolicy(); |
109 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
110 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
111 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
152 |
< |
Policy.setPolicy(policy); |
153 |
< |
try { |
154 |
< |
ExecutorService e = new DirectExecutorService(); |
155 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
107 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
108 |
> |
Runnable r = new CheckedRunnable() { |
109 |
> |
public void realRun() throws Exception { |
110 |
> |
ExecutorService e = new DirectExecutorService(); |
111 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
112 |
|
public Object run() { |
113 |
|
return TEST_STRING; |
114 |
|
}})); |
115 |
|
|
116 |
< |
Object result = future.get(); |
117 |
< |
assertSame(TEST_STRING, result); |
118 |
< |
} |
119 |
< |
catch (ExecutionException ex) { |
164 |
< |
unexpectedException(); |
165 |
< |
} |
166 |
< |
catch (InterruptedException ex) { |
167 |
< |
unexpectedException(); |
168 |
< |
} |
169 |
< |
finally { |
170 |
< |
Policy.setPolicy(savedPolicy); |
171 |
< |
} |
116 |
> |
assertSame(TEST_STRING, future.get()); |
117 |
> |
}}; |
118 |
> |
|
119 |
> |
runWithPermissions(r); |
120 |
|
} |
121 |
|
|
122 |
|
/** |
123 |
|
* A submitted failed privileged exception action reports exception |
124 |
|
*/ |
125 |
< |
public void testSubmitFailedPrivilegedExceptionAction() { |
126 |
< |
Policy savedPolicy = Policy.getPolicy(); |
127 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
128 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
129 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
182 |
< |
Policy.setPolicy(policy); |
183 |
< |
try { |
184 |
< |
ExecutorService e = new DirectExecutorService(); |
185 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
125 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
126 |
> |
Runnable r = new CheckedRunnable() { |
127 |
> |
public void realRun() throws Exception { |
128 |
> |
ExecutorService e = new DirectExecutorService(); |
129 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
130 |
|
public Object run() throws Exception { |
131 |
|
throw new IndexOutOfBoundsException(); |
132 |
|
}})); |
133 |
|
|
134 |
< |
Object result = future.get(); |
135 |
< |
shouldThrow(); |
136 |
< |
} |
137 |
< |
catch (ExecutionException success) { |
138 |
< |
} |
139 |
< |
catch (InterruptedException ex) { |
140 |
< |
unexpectedException(); |
141 |
< |
} |
198 |
< |
finally { |
199 |
< |
Policy.setPolicy(savedPolicy); |
200 |
< |
} |
134 |
> |
try { |
135 |
> |
future.get(); |
136 |
> |
shouldThrow(); |
137 |
> |
} catch (ExecutionException success) { |
138 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
139 |
> |
}}}; |
140 |
> |
|
141 |
> |
runWithPermissions(r); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
147 |
|
public void testExecuteNullRunnable() { |
148 |
|
try { |
149 |
|
ExecutorService e = new DirectExecutorService(); |
150 |
< |
TrackedShortRunnable task = null; |
210 |
< |
Future<?> future = e.submit(task); |
150 |
> |
e.submit((Runnable) null); |
151 |
|
shouldThrow(); |
152 |
< |
} |
213 |
< |
catch (NullPointerException success) { |
214 |
< |
} |
215 |
< |
catch (Exception ex) { |
216 |
< |
unexpectedException(); |
217 |
< |
} |
152 |
> |
} catch (NullPointerException success) {} |
153 |
|
} |
154 |
|
|
155 |
|
|
159 |
|
public void testSubmitNullCallable() { |
160 |
|
try { |
161 |
|
ExecutorService e = new DirectExecutorService(); |
162 |
< |
StringTask t = null; |
228 |
< |
Future<String> future = e.submit(t); |
162 |
> |
e.submit((Callable) null); |
163 |
|
shouldThrow(); |
164 |
< |
} |
231 |
< |
catch (NullPointerException success) { |
232 |
< |
} |
233 |
< |
catch (Exception ex) { |
234 |
< |
unexpectedException(); |
235 |
< |
} |
164 |
> |
} catch (NullPointerException success) {} |
165 |
|
} |
166 |
|
|
167 |
|
/** |
169 |
|
* executor is saturated. |
170 |
|
*/ |
171 |
|
public void testExecute1() { |
172 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
172 |
> |
ThreadPoolExecutor p = |
173 |
> |
new ThreadPoolExecutor(1, 1, |
174 |
> |
60, TimeUnit.SECONDS, |
175 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
176 |
|
try { |
177 |
< |
|
246 |
< |
for(int i = 0; i < 5; ++i){ |
177 |
> |
for (int i = 0; i < 2; ++i) |
178 |
|
p.submit(new MediumRunnable()); |
179 |
+ |
for (int i = 0; i < 2; ++i) { |
180 |
+ |
try { |
181 |
+ |
p.submit(new MediumRunnable()); |
182 |
+ |
shouldThrow(); |
183 |
+ |
} catch (RejectedExecutionException success) {} |
184 |
|
} |
185 |
< |
shouldThrow(); |
186 |
< |
} catch(RejectedExecutionException success){} |
187 |
< |
joinPool(p); |
185 |
> |
} finally { |
186 |
> |
joinPool(p); |
187 |
> |
} |
188 |
|
} |
189 |
|
|
190 |
|
/** |
192 |
|
* if executor is saturated. |
193 |
|
*/ |
194 |
|
public void testExecute2() { |
195 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, 60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
195 |
> |
ThreadPoolExecutor p = |
196 |
> |
new ThreadPoolExecutor(1, 1, |
197 |
> |
60, TimeUnit.SECONDS, |
198 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
199 |
|
try { |
200 |
< |
for(int i = 0; i < 5; ++i) { |
201 |
< |
p.submit(new SmallCallable()); |
200 |
> |
for (int i = 0; i < 2; ++i) |
201 |
> |
p.submit(new MediumRunnable()); |
202 |
> |
for (int i = 0; i < 2; ++i) { |
203 |
> |
try { |
204 |
> |
p.submit(new SmallCallable()); |
205 |
> |
shouldThrow(); |
206 |
> |
} catch (RejectedExecutionException success) {} |
207 |
|
} |
208 |
< |
shouldThrow(); |
209 |
< |
} catch(RejectedExecutionException e){} |
210 |
< |
joinPool(p); |
208 |
> |
} finally { |
209 |
> |
joinPool(p); |
210 |
> |
} |
211 |
|
} |
212 |
|
|
213 |
|
|
214 |
|
/** |
215 |
< |
* Blocking on submit(callable) throws InterruptedException if |
272 |
< |
* caller interrupted. |
215 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
216 |
|
*/ |
217 |
< |
public void testInterruptedSubmit() { |
218 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
219 |
< |
Thread t = new Thread(new Runnable() { |
220 |
< |
public void run() { |
221 |
< |
try { |
222 |
< |
p.submit(new Callable<Object>() { |
223 |
< |
public Object call() { |
224 |
< |
try { |
225 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
226 |
< |
shouldThrow(); |
227 |
< |
} catch(InterruptedException e){ |
285 |
< |
} |
286 |
< |
return null; |
287 |
< |
} |
288 |
< |
}).get(); |
289 |
< |
} catch(InterruptedException success){ |
290 |
< |
} catch(Exception e) { |
291 |
< |
unexpectedException(); |
292 |
< |
} |
293 |
< |
|
294 |
< |
} |
295 |
< |
}); |
217 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
218 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
219 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
220 |
> |
final ExecutorService p |
221 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
222 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
223 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
224 |
> |
public Void realCall() throws InterruptedException { |
225 |
> |
quittingTime.await(); |
226 |
> |
return null; |
227 |
> |
}}; |
228 |
|
try { |
229 |
+ |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
230 |
+ |
public void realRun() throws Exception { |
231 |
+ |
Future<Void> future = p.submit(awaiter); |
232 |
+ |
submitted.countDown(); |
233 |
+ |
future.get(); |
234 |
+ |
}}); |
235 |
|
t.start(); |
236 |
< |
Thread.sleep(SHORT_DELAY_MS); |
236 |
> |
submitted.await(); |
237 |
|
t.interrupt(); |
238 |
< |
} catch(Exception e){ |
239 |
< |
unexpectedException(); |
238 |
> |
t.join(); |
239 |
> |
} finally { |
240 |
> |
quittingTime.countDown(); |
241 |
> |
joinPool(p); |
242 |
|
} |
303 |
– |
joinPool(p); |
243 |
|
} |
244 |
|
|
245 |
|
/** |
246 |
< |
* get of submitted callable throws Exception if callable |
246 |
> |
* get of submitted callable throws InterruptedException if callable |
247 |
|
* interrupted |
248 |
|
*/ |
249 |
< |
public void testSubmitIE() { |
250 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
251 |
< |
|
252 |
< |
final Callable c = new Callable() { |
253 |
< |
public Object call() { |
254 |
< |
try { |
255 |
< |
p.submit(new SmallCallable()).get(); |
256 |
< |
shouldThrow(); |
257 |
< |
} catch(InterruptedException e){} |
258 |
< |
catch(RejectedExecutionException e2){} |
259 |
< |
catch(ExecutionException e3){} |
260 |
< |
return Boolean.TRUE; |
261 |
< |
} |
262 |
< |
}; |
263 |
< |
|
325 |
< |
|
326 |
< |
|
327 |
< |
Thread t = new Thread(new Runnable() { |
328 |
< |
public void run() { |
329 |
< |
try { |
330 |
< |
c.call(); |
331 |
< |
} catch(Exception e){} |
332 |
< |
} |
333 |
< |
}); |
334 |
< |
try { |
335 |
< |
t.start(); |
336 |
< |
Thread.sleep(SHORT_DELAY_MS); |
337 |
< |
t.interrupt(); |
338 |
< |
t.join(); |
339 |
< |
} catch(InterruptedException e){ |
340 |
< |
unexpectedException(); |
341 |
< |
} |
342 |
< |
|
249 |
> |
public void testSubmitIE() throws InterruptedException { |
250 |
> |
final ThreadPoolExecutor p = |
251 |
> |
new ThreadPoolExecutor(1, 1, |
252 |
> |
60, TimeUnit.SECONDS, |
253 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
254 |
> |
|
255 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
256 |
> |
public void realRun() throws Exception { |
257 |
> |
p.submit(new SmallCallable()).get(); |
258 |
> |
}}); |
259 |
> |
|
260 |
> |
t.start(); |
261 |
> |
Thread.sleep(SHORT_DELAY_MS); |
262 |
> |
t.interrupt(); |
263 |
> |
t.join(); |
264 |
|
joinPool(p); |
265 |
|
} |
266 |
|
|
268 |
|
* get of submit(callable) throws ExecutionException if callable |
269 |
|
* throws exception |
270 |
|
*/ |
271 |
< |
public void testSubmitEE() { |
272 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
271 |
> |
public void testSubmitEE() throws InterruptedException { |
272 |
> |
ThreadPoolExecutor p = |
273 |
> |
new ThreadPoolExecutor(1, 1, |
274 |
> |
60, TimeUnit.SECONDS, |
275 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
276 |
|
|
277 |
< |
try { |
278 |
< |
Callable c = new Callable() { |
355 |
< |
public Object call() { |
356 |
< |
int i = 5/0; |
357 |
< |
return Boolean.TRUE; |
358 |
< |
} |
359 |
< |
}; |
360 |
< |
|
361 |
< |
for(int i =0; i < 5; i++){ |
362 |
< |
p.submit(c).get(); |
363 |
< |
} |
277 |
> |
Callable c = new Callable() { |
278 |
> |
public Object call() { return 5/0; }}; |
279 |
|
|
280 |
+ |
try { |
281 |
+ |
p.submit(c).get(); |
282 |
|
shouldThrow(); |
283 |
< |
} |
284 |
< |
catch(ExecutionException success){ |
368 |
< |
} catch(Exception e) { |
369 |
< |
unexpectedException(); |
283 |
> |
} catch (ExecutionException success) { |
284 |
> |
assertTrue(success.getCause() instanceof ArithmeticException); |
285 |
|
} |
286 |
|
joinPool(p); |
287 |
|
} |
289 |
|
/** |
290 |
|
* invokeAny(null) throws NPE |
291 |
|
*/ |
292 |
< |
public void testInvokeAny1() { |
292 |
> |
public void testInvokeAny1() |
293 |
> |
throws InterruptedException, ExecutionException { |
294 |
|
ExecutorService e = new DirectExecutorService(); |
295 |
|
try { |
296 |
|
e.invokeAny(null); |
297 |
+ |
shouldThrow(); |
298 |
|
} catch (NullPointerException success) { |
382 |
– |
} catch(Exception ex) { |
383 |
– |
unexpectedException(); |
299 |
|
} finally { |
300 |
|
joinPool(e); |
301 |
|
} |
304 |
|
/** |
305 |
|
* invokeAny(empty collection) throws IAE |
306 |
|
*/ |
307 |
< |
public void testInvokeAny2() { |
307 |
> |
public void testInvokeAny2() |
308 |
> |
throws InterruptedException, ExecutionException { |
309 |
|
ExecutorService e = new DirectExecutorService(); |
310 |
|
try { |
311 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
312 |
+ |
shouldThrow(); |
313 |
|
} catch (IllegalArgumentException success) { |
397 |
– |
} catch(Exception ex) { |
398 |
– |
unexpectedException(); |
314 |
|
} finally { |
315 |
|
joinPool(e); |
316 |
|
} |
319 |
|
/** |
320 |
|
* invokeAny(c) throws NPE if c has null elements |
321 |
|
*/ |
322 |
< |
public void testInvokeAny3() { |
322 |
> |
public void testInvokeAny3() throws Exception { |
323 |
|
ExecutorService e = new DirectExecutorService(); |
324 |
+ |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
325 |
+ |
l.add(new Callable<Integer>() { |
326 |
+ |
public Integer call() { return 5/0; }}); |
327 |
+ |
l.add(null); |
328 |
|
try { |
410 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
411 |
– |
l.add(new StringTask()); |
412 |
– |
l.add(null); |
329 |
|
e.invokeAny(l); |
330 |
+ |
shouldThrow(); |
331 |
|
} catch (NullPointerException success) { |
415 |
– |
} catch(Exception ex) { |
416 |
– |
ex.printStackTrace(); |
417 |
– |
unexpectedException(); |
332 |
|
} finally { |
333 |
|
joinPool(e); |
334 |
|
} |
337 |
|
/** |
338 |
|
* invokeAny(c) throws ExecutionException if no task in c completes |
339 |
|
*/ |
340 |
< |
public void testInvokeAny4() { |
340 |
> |
public void testInvokeAny4() throws InterruptedException { |
341 |
|
ExecutorService e = new DirectExecutorService(); |
342 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
343 |
+ |
l.add(new NPETask()); |
344 |
|
try { |
429 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
430 |
– |
l.add(new NPETask()); |
345 |
|
e.invokeAny(l); |
346 |
< |
} catch(ExecutionException success) { |
347 |
< |
} catch(Exception ex) { |
348 |
< |
unexpectedException(); |
346 |
> |
shouldThrow(); |
347 |
> |
} catch (ExecutionException success) { |
348 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
349 |
|
} finally { |
350 |
|
joinPool(e); |
351 |
|
} |
354 |
|
/** |
355 |
|
* invokeAny(c) returns result of some task in c if at least one completes |
356 |
|
*/ |
357 |
< |
public void testInvokeAny5() { |
357 |
> |
public void testInvokeAny5() throws Exception { |
358 |
|
ExecutorService e = new DirectExecutorService(); |
359 |
|
try { |
360 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
360 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
361 |
|
l.add(new StringTask()); |
362 |
|
l.add(new StringTask()); |
363 |
|
String result = e.invokeAny(l); |
364 |
|
assertSame(TEST_STRING, result); |
451 |
– |
} catch (ExecutionException success) { |
452 |
– |
} catch(Exception ex) { |
453 |
– |
unexpectedException(); |
365 |
|
} finally { |
366 |
|
joinPool(e); |
367 |
|
} |
370 |
|
/** |
371 |
|
* invokeAll(null) throws NPE |
372 |
|
*/ |
373 |
< |
public void testInvokeAll1() { |
373 |
> |
public void testInvokeAll1() throws InterruptedException { |
374 |
|
ExecutorService e = new DirectExecutorService(); |
375 |
|
try { |
376 |
|
e.invokeAll(null); |
377 |
+ |
shouldThrow(); |
378 |
|
} catch (NullPointerException success) { |
467 |
– |
} catch(Exception ex) { |
468 |
– |
unexpectedException(); |
379 |
|
} finally { |
380 |
|
joinPool(e); |
381 |
|
} |
384 |
|
/** |
385 |
|
* invokeAll(empty collection) returns empty collection |
386 |
|
*/ |
387 |
< |
public void testInvokeAll2() { |
387 |
> |
public void testInvokeAll2() throws InterruptedException { |
388 |
|
ExecutorService e = new DirectExecutorService(); |
389 |
|
try { |
390 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
391 |
|
assertTrue(r.isEmpty()); |
482 |
– |
} catch(Exception ex) { |
483 |
– |
unexpectedException(); |
392 |
|
} finally { |
393 |
|
joinPool(e); |
394 |
|
} |
397 |
|
/** |
398 |
|
* invokeAll(c) throws NPE if c has null elements |
399 |
|
*/ |
400 |
< |
public void testInvokeAll3() { |
400 |
> |
public void testInvokeAll3() throws InterruptedException { |
401 |
|
ExecutorService e = new DirectExecutorService(); |
402 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
403 |
+ |
l.add(new StringTask()); |
404 |
+ |
l.add(null); |
405 |
|
try { |
495 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
496 |
– |
l.add(new StringTask()); |
497 |
– |
l.add(null); |
406 |
|
e.invokeAll(l); |
407 |
+ |
shouldThrow(); |
408 |
|
} catch (NullPointerException success) { |
500 |
– |
} catch(Exception ex) { |
501 |
– |
unexpectedException(); |
409 |
|
} finally { |
410 |
|
joinPool(e); |
411 |
|
} |
414 |
|
/** |
415 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
416 |
|
*/ |
417 |
< |
public void testInvokeAll4() { |
417 |
> |
public void testInvokeAll4() throws Exception { |
418 |
|
ExecutorService e = new DirectExecutorService(); |
419 |
|
try { |
420 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
420 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
421 |
|
l.add(new NPETask()); |
422 |
< |
List<Future<String>> result = e.invokeAll(l); |
423 |
< |
assertEquals(1, result.size()); |
424 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
425 |
< |
it.next().get(); |
426 |
< |
} catch(ExecutionException success) { |
427 |
< |
} catch(Exception ex) { |
428 |
< |
unexpectedException(); |
422 |
> |
List<Future<String>> futures = e.invokeAll(l); |
423 |
> |
assertEquals(1, futures.size()); |
424 |
> |
try { |
425 |
> |
futures.get(0).get(); |
426 |
> |
shouldThrow(); |
427 |
> |
} catch (ExecutionException success) { |
428 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
429 |
> |
} |
430 |
|
} finally { |
431 |
|
joinPool(e); |
432 |
|
} |
435 |
|
/** |
436 |
|
* invokeAll(c) returns results of all completed tasks in c |
437 |
|
*/ |
438 |
< |
public void testInvokeAll5() { |
438 |
> |
public void testInvokeAll5() throws Exception { |
439 |
|
ExecutorService e = new DirectExecutorService(); |
440 |
|
try { |
441 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
441 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
442 |
|
l.add(new StringTask()); |
443 |
|
l.add(new StringTask()); |
444 |
< |
List<Future<String>> result = e.invokeAll(l); |
445 |
< |
assertEquals(2, result.size()); |
446 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
447 |
< |
assertSame(TEST_STRING, it.next().get()); |
540 |
< |
} catch (ExecutionException success) { |
541 |
< |
} catch(Exception ex) { |
542 |
< |
unexpectedException(); |
444 |
> |
List<Future<String>> futures = e.invokeAll(l); |
445 |
> |
assertEquals(2, futures.size()); |
446 |
> |
for (Future<String> future : futures) |
447 |
> |
assertSame(TEST_STRING, future.get()); |
448 |
|
} finally { |
449 |
|
joinPool(e); |
450 |
|
} |
454 |
|
/** |
455 |
|
* timed invokeAny(null) throws NPE |
456 |
|
*/ |
457 |
< |
public void testTimedInvokeAny1() { |
457 |
> |
public void testTimedInvokeAny1() throws Exception { |
458 |
|
ExecutorService e = new DirectExecutorService(); |
459 |
|
try { |
460 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
460 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
461 |
> |
shouldThrow(); |
462 |
|
} catch (NullPointerException success) { |
557 |
– |
} catch(Exception ex) { |
558 |
– |
unexpectedException(); |
463 |
|
} finally { |
464 |
|
joinPool(e); |
465 |
|
} |
468 |
|
/** |
469 |
|
* timed invokeAny(null time unit) throws NPE |
470 |
|
*/ |
471 |
< |
public void testTimedInvokeAnyNullTimeUnit() { |
471 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
472 |
|
ExecutorService e = new DirectExecutorService(); |
473 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
474 |
+ |
l.add(new StringTask()); |
475 |
|
try { |
570 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
571 |
– |
l.add(new StringTask()); |
476 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
477 |
+ |
shouldThrow(); |
478 |
|
} catch (NullPointerException success) { |
574 |
– |
} catch(Exception ex) { |
575 |
– |
unexpectedException(); |
479 |
|
} finally { |
480 |
|
joinPool(e); |
481 |
|
} |
484 |
|
/** |
485 |
|
* timed invokeAny(empty collection) throws IAE |
486 |
|
*/ |
487 |
< |
public void testTimedInvokeAny2() { |
487 |
> |
public void testTimedInvokeAny2() throws Exception { |
488 |
|
ExecutorService e = new DirectExecutorService(); |
489 |
|
try { |
490 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
490 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
491 |
> |
shouldThrow(); |
492 |
|
} catch (IllegalArgumentException success) { |
589 |
– |
} catch(Exception ex) { |
590 |
– |
unexpectedException(); |
493 |
|
} finally { |
494 |
|
joinPool(e); |
495 |
|
} |
498 |
|
/** |
499 |
|
* timed invokeAny(c) throws NPE if c has null elements |
500 |
|
*/ |
501 |
< |
public void testTimedInvokeAny3() { |
501 |
> |
public void testTimedInvokeAny3() throws Exception { |
502 |
|
ExecutorService e = new DirectExecutorService(); |
503 |
+ |
List<Callable<Integer>> l = new ArrayList<Callable<Integer>>(); |
504 |
+ |
l.add(new Callable<Integer>() { |
505 |
+ |
public Integer call() { return 5/0; }}); |
506 |
+ |
l.add(null); |
507 |
|
try { |
508 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
509 |
< |
l.add(new StringTask()); |
604 |
< |
l.add(null); |
605 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
508 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
509 |
> |
shouldThrow(); |
510 |
|
} catch (NullPointerException success) { |
607 |
– |
} catch(Exception ex) { |
608 |
– |
ex.printStackTrace(); |
609 |
– |
unexpectedException(); |
511 |
|
} finally { |
512 |
|
joinPool(e); |
513 |
|
} |
516 |
|
/** |
517 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
518 |
|
*/ |
519 |
< |
public void testTimedInvokeAny4() { |
519 |
> |
public void testTimedInvokeAny4() throws Exception { |
520 |
|
ExecutorService e = new DirectExecutorService(); |
521 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
522 |
+ |
l.add(new NPETask()); |
523 |
|
try { |
524 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
525 |
< |
l.add(new NPETask()); |
526 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
527 |
< |
} catch(ExecutionException success) { |
625 |
< |
} catch(Exception ex) { |
626 |
< |
unexpectedException(); |
524 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
525 |
> |
shouldThrow(); |
526 |
> |
} catch (ExecutionException success) { |
527 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
528 |
|
} finally { |
529 |
|
joinPool(e); |
530 |
|
} |
533 |
|
/** |
534 |
|
* timed invokeAny(c) returns result of some task in c |
535 |
|
*/ |
536 |
< |
public void testTimedInvokeAny5() { |
536 |
> |
public void testTimedInvokeAny5() throws Exception { |
537 |
|
ExecutorService e = new DirectExecutorService(); |
538 |
|
try { |
539 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
539 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
540 |
|
l.add(new StringTask()); |
541 |
|
l.add(new StringTask()); |
542 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
542 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
543 |
|
assertSame(TEST_STRING, result); |
643 |
– |
} catch (ExecutionException success) { |
644 |
– |
} catch(Exception ex) { |
645 |
– |
unexpectedException(); |
544 |
|
} finally { |
545 |
|
joinPool(e); |
546 |
|
} |
549 |
|
/** |
550 |
|
* timed invokeAll(null) throws NPE |
551 |
|
*/ |
552 |
< |
public void testTimedInvokeAll1() { |
552 |
> |
public void testTimedInvokeAll1() throws InterruptedException { |
553 |
|
ExecutorService e = new DirectExecutorService(); |
554 |
|
try { |
555 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
555 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
556 |
> |
shouldThrow(); |
557 |
|
} catch (NullPointerException success) { |
659 |
– |
} catch(Exception ex) { |
660 |
– |
unexpectedException(); |
558 |
|
} finally { |
559 |
|
joinPool(e); |
560 |
|
} |
563 |
|
/** |
564 |
|
* timed invokeAll(null time unit) throws NPE |
565 |
|
*/ |
566 |
< |
public void testTimedInvokeAllNullTimeUnit() { |
566 |
> |
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
567 |
|
ExecutorService e = new DirectExecutorService(); |
568 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
569 |
+ |
l.add(new StringTask()); |
570 |
|
try { |
672 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
673 |
– |
l.add(new StringTask()); |
571 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
572 |
+ |
shouldThrow(); |
573 |
|
} catch (NullPointerException success) { |
676 |
– |
} catch(Exception ex) { |
677 |
– |
unexpectedException(); |
574 |
|
} finally { |
575 |
|
joinPool(e); |
576 |
|
} |
579 |
|
/** |
580 |
|
* timed invokeAll(empty collection) returns empty collection |
581 |
|
*/ |
582 |
< |
public void testTimedInvokeAll2() { |
582 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
583 |
|
ExecutorService e = new DirectExecutorService(); |
584 |
|
try { |
585 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
585 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
586 |
|
assertTrue(r.isEmpty()); |
691 |
– |
} catch(Exception ex) { |
692 |
– |
unexpectedException(); |
587 |
|
} finally { |
588 |
|
joinPool(e); |
589 |
|
} |
592 |
|
/** |
593 |
|
* timed invokeAll(c) throws NPE if c has null elements |
594 |
|
*/ |
595 |
< |
public void testTimedInvokeAll3() { |
595 |
> |
public void testTimedInvokeAll3() throws InterruptedException { |
596 |
|
ExecutorService e = new DirectExecutorService(); |
597 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
598 |
+ |
l.add(new StringTask()); |
599 |
+ |
l.add(null); |
600 |
|
try { |
601 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
602 |
< |
l.add(new StringTask()); |
706 |
< |
l.add(null); |
707 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
601 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
602 |
> |
shouldThrow(); |
603 |
|
} catch (NullPointerException success) { |
709 |
– |
} catch(Exception ex) { |
710 |
– |
unexpectedException(); |
604 |
|
} finally { |
605 |
|
joinPool(e); |
606 |
|
} |
609 |
|
/** |
610 |
|
* get of returned element of invokeAll(c) throws exception on failed task |
611 |
|
*/ |
612 |
< |
public void testTimedInvokeAll4() { |
612 |
> |
public void testTimedInvokeAll4() throws Exception { |
613 |
|
ExecutorService e = new DirectExecutorService(); |
614 |
|
try { |
615 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
615 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
616 |
|
l.add(new NPETask()); |
617 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
618 |
< |
assertEquals(1, result.size()); |
619 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
620 |
< |
it.next().get(); |
621 |
< |
} catch(ExecutionException success) { |
622 |
< |
} catch(Exception ex) { |
623 |
< |
unexpectedException(); |
617 |
> |
List<Future<String>> futures = |
618 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
619 |
> |
assertEquals(1, futures.size()); |
620 |
> |
try { |
621 |
> |
futures.get(0).get(); |
622 |
> |
shouldThrow(); |
623 |
> |
} catch (ExecutionException success) { |
624 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
625 |
> |
} |
626 |
|
} finally { |
627 |
|
joinPool(e); |
628 |
|
} |
631 |
|
/** |
632 |
|
* timed invokeAll(c) returns results of all completed tasks in c |
633 |
|
*/ |
634 |
< |
public void testTimedInvokeAll5() { |
634 |
> |
public void testTimedInvokeAll5() throws Exception { |
635 |
|
ExecutorService e = new DirectExecutorService(); |
636 |
|
try { |
637 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
637 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
638 |
|
l.add(new StringTask()); |
639 |
|
l.add(new StringTask()); |
640 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
641 |
< |
assertEquals(2, result.size()); |
642 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
643 |
< |
assertSame(TEST_STRING, it.next().get()); |
644 |
< |
} catch (ExecutionException success) { |
750 |
< |
} catch(Exception ex) { |
751 |
< |
unexpectedException(); |
640 |
> |
List<Future<String>> futures = |
641 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
642 |
> |
assertEquals(2, futures.size()); |
643 |
> |
for (Future<String> future : futures) |
644 |
> |
assertSame(TEST_STRING, future.get()); |
645 |
|
} finally { |
646 |
|
joinPool(e); |
647 |
|
} |
650 |
|
/** |
651 |
|
* timed invokeAll cancels tasks not completed by timeout |
652 |
|
*/ |
653 |
< |
public void testTimedInvokeAll6() { |
653 |
> |
public void testTimedInvokeAll6() throws InterruptedException { |
654 |
|
ExecutorService e = new DirectExecutorService(); |
655 |
|
try { |
656 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
656 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
657 |
|
l.add(new StringTask()); |
658 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
659 |
|
l.add(new StringTask()); |
660 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
661 |
< |
assertEquals(3, result.size()); |
662 |
< |
Iterator<Future<String>> it = result.iterator(); |
660 |
> |
List<Future<String>> futures = |
661 |
> |
e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
662 |
> |
assertEquals(3, futures.size()); |
663 |
> |
Iterator<Future<String>> it = futures.iterator(); |
664 |
|
Future<String> f1 = it.next(); |
665 |
|
Future<String> f2 = it.next(); |
666 |
|
Future<String> f3 = it.next(); |
669 |
|
assertTrue(f2.isDone()); |
670 |
|
assertTrue(f3.isDone()); |
671 |
|
assertTrue(f3.isCancelled()); |
778 |
– |
} catch(Exception ex) { |
779 |
– |
unexpectedException(); |
672 |
|
} finally { |
673 |
|
joinPool(e); |
674 |
|
} |