1 |
|
/* |
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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
|
9 |
|
import junit.framework.*; |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
< |
import java.math.BigInteger; |
12 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
13 |
> |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
14 |
|
import java.security.*; |
15 |
|
|
16 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
28 |
|
static class DirectExecutorService extends AbstractExecutorService { |
29 |
|
public void execute(Runnable r) { r.run(); } |
30 |
|
public void shutdown() { shutdown = true; } |
31 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
31 |
> |
public List<Runnable> shutdownNow() { |
32 |
> |
shutdown = true; |
33 |
> |
return Collections.EMPTY_LIST; |
34 |
> |
} |
35 |
|
public boolean isShutdown() { return shutdown; } |
36 |
|
public boolean isTerminated() { return isShutdown(); } |
37 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
37 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
38 |
> |
return isShutdown(); |
39 |
> |
} |
40 |
|
private volatile boolean shutdown = false; |
41 |
|
} |
42 |
|
|
45 |
|
*/ |
46 |
|
public void testExecuteRunnable() throws Exception { |
47 |
|
ExecutorService e = new DirectExecutorService(); |
48 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
49 |
< |
assertFalse(task.done); |
50 |
< |
Future<?> future = e.submit(task); |
51 |
< |
future.get(); |
52 |
< |
assertTrue(task.done); |
48 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
49 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
50 |
> |
public void realRun() { |
51 |
> |
done.set(true); |
52 |
> |
}}); |
53 |
> |
assertNull(future.get()); |
54 |
> |
assertNull(future.get(0, MILLISECONDS)); |
55 |
> |
assertTrue(done.get()); |
56 |
> |
assertTrue(future.isDone()); |
57 |
> |
assertFalse(future.isCancelled()); |
58 |
|
} |
59 |
|
|
50 |
– |
|
60 |
|
/** |
61 |
|
* Completed submit(callable) returns result |
62 |
|
*/ |
87 |
|
assertSame(TEST_STRING, result); |
88 |
|
} |
89 |
|
|
81 |
– |
|
90 |
|
/** |
91 |
< |
* A submitted privileged action to completion |
91 |
> |
* A submitted privileged action runs to completion |
92 |
|
*/ |
93 |
|
public void testSubmitPrivilegedAction() throws Exception { |
94 |
< |
Policy savedPolicy = null; |
95 |
< |
try { |
96 |
< |
savedPolicy = Policy.getPolicy(); |
97 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
90 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
91 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
92 |
< |
Policy.setPolicy(policy); |
93 |
< |
} catch (AccessControlException ok) { |
94 |
< |
return; |
95 |
< |
} |
96 |
< |
try { |
97 |
< |
ExecutorService e = new DirectExecutorService(); |
98 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
94 |
> |
Runnable r = new CheckedRunnable() { |
95 |
> |
public void realRun() throws Exception { |
96 |
> |
ExecutorService e = new DirectExecutorService(); |
97 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
98 |
|
public Object run() { |
99 |
|
return TEST_STRING; |
100 |
|
}})); |
101 |
|
|
102 |
< |
Object result = future.get(); |
103 |
< |
assertSame(TEST_STRING, result); |
104 |
< |
} |
105 |
< |
finally { |
106 |
< |
try { |
107 |
< |
Policy.setPolicy(savedPolicy); |
108 |
< |
} catch (AccessControlException ok) { |
110 |
< |
return; |
111 |
< |
} |
112 |
< |
} |
102 |
> |
assertSame(TEST_STRING, future.get()); |
103 |
> |
}}; |
104 |
> |
|
105 |
> |
runWithPermissions(r, |
106 |
> |
new RuntimePermission("getClassLoader"), |
107 |
> |
new RuntimePermission("setContextClassLoader"), |
108 |
> |
new RuntimePermission("modifyThread")); |
109 |
|
} |
110 |
|
|
111 |
|
/** |
112 |
< |
* A submitted a privileged exception action runs to completion |
112 |
> |
* A submitted privileged exception action runs to completion |
113 |
|
*/ |
114 |
|
public void testSubmitPrivilegedExceptionAction() throws Exception { |
115 |
< |
Policy savedPolicy = null; |
116 |
< |
try { |
117 |
< |
savedPolicy = Policy.getPolicy(); |
118 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
123 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
124 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
125 |
< |
Policy.setPolicy(policy); |
126 |
< |
} catch (AccessControlException ok) { |
127 |
< |
return; |
128 |
< |
} |
129 |
< |
|
130 |
< |
try { |
131 |
< |
ExecutorService e = new DirectExecutorService(); |
132 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
115 |
> |
Runnable r = new CheckedRunnable() { |
116 |
> |
public void realRun() throws Exception { |
117 |
> |
ExecutorService e = new DirectExecutorService(); |
118 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
119 |
|
public Object run() { |
120 |
|
return TEST_STRING; |
121 |
|
}})); |
122 |
|
|
123 |
< |
Object result = future.get(); |
124 |
< |
assertSame(TEST_STRING, result); |
125 |
< |
} |
126 |
< |
finally { |
141 |
< |
Policy.setPolicy(savedPolicy); |
142 |
< |
} |
123 |
> |
assertSame(TEST_STRING, future.get()); |
124 |
> |
}}; |
125 |
> |
|
126 |
> |
runWithPermissions(r); |
127 |
|
} |
128 |
|
|
129 |
|
/** |
130 |
|
* A submitted failed privileged exception action reports exception |
131 |
|
*/ |
132 |
|
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
133 |
< |
Policy savedPolicy = null; |
134 |
< |
try { |
135 |
< |
savedPolicy = Policy.getPolicy(); |
136 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
153 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
154 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
155 |
< |
Policy.setPolicy(policy); |
156 |
< |
} catch (AccessControlException ok) { |
157 |
< |
return; |
158 |
< |
} |
159 |
< |
|
160 |
< |
try { |
161 |
< |
ExecutorService e = new DirectExecutorService(); |
162 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
133 |
> |
Runnable r = new CheckedRunnable() { |
134 |
> |
public void realRun() throws Exception { |
135 |
> |
ExecutorService e = new DirectExecutorService(); |
136 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
137 |
|
public Object run() throws Exception { |
138 |
|
throw new IndexOutOfBoundsException(); |
139 |
|
}})); |
140 |
|
|
141 |
< |
future.get(); |
142 |
< |
shouldThrow(); |
143 |
< |
} catch (ExecutionException success) { |
144 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
145 |
< |
} |
146 |
< |
finally { |
147 |
< |
Policy.setPolicy(savedPolicy); |
148 |
< |
} |
141 |
> |
try { |
142 |
> |
future.get(); |
143 |
> |
shouldThrow(); |
144 |
> |
} catch (ExecutionException success) { |
145 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
146 |
> |
}}}; |
147 |
> |
|
148 |
> |
runWithPermissions(r); |
149 |
|
} |
150 |
|
|
151 |
|
/** |
159 |
|
} catch (NullPointerException success) {} |
160 |
|
} |
161 |
|
|
188 |
– |
|
162 |
|
/** |
163 |
|
* submit(null callable) throws NPE |
164 |
|
*/ |
171 |
|
} |
172 |
|
|
173 |
|
/** |
174 |
< |
* submit(runnable) throws RejectedExecutionException if |
202 |
< |
* executor is saturated. |
203 |
< |
*/ |
204 |
< |
public void testExecute1() { |
205 |
< |
ThreadPoolExecutor p = |
206 |
< |
new ThreadPoolExecutor(1, 1, |
207 |
< |
60, TimeUnit.SECONDS, |
208 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
209 |
< |
try { |
210 |
< |
for (int i = 0; i < 2; ++i) |
211 |
< |
p.submit(new MediumRunnable()); |
212 |
< |
for (int i = 0; i < 2; ++i) { |
213 |
< |
try { |
214 |
< |
p.submit(new MediumRunnable()); |
215 |
< |
shouldThrow(); |
216 |
< |
} catch (RejectedExecutionException success) {} |
217 |
< |
} |
218 |
< |
} finally { |
219 |
< |
joinPool(p); |
220 |
< |
} |
221 |
< |
} |
222 |
< |
|
223 |
< |
/** |
224 |
< |
* submit(callable) throws RejectedExecutionException |
225 |
< |
* if executor is saturated. |
174 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
175 |
|
*/ |
176 |
< |
public void testExecute2() { |
177 |
< |
ThreadPoolExecutor p = |
178 |
< |
new ThreadPoolExecutor(1, 1, |
179 |
< |
60, TimeUnit.SECONDS, |
180 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
181 |
< |
try { |
182 |
< |
for (int i = 0; i < 2; ++i) |
183 |
< |
p.submit(new MediumRunnable()); |
184 |
< |
for (int i = 0; i < 2; ++i) { |
185 |
< |
try { |
186 |
< |
p.submit(new SmallCallable()); |
187 |
< |
shouldThrow(); |
188 |
< |
} catch (RejectedExecutionException success) {} |
189 |
< |
} |
176 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
177 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
178 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
179 |
> |
final ExecutorService p |
180 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
181 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
182 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
183 |
> |
public Void realCall() throws InterruptedException { |
184 |
> |
quittingTime.await(); |
185 |
> |
return null; |
186 |
> |
}}; |
187 |
> |
try { |
188 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
189 |
> |
public void realRun() throws Exception { |
190 |
> |
Future<Void> future = p.submit(awaiter); |
191 |
> |
submitted.countDown(); |
192 |
> |
future.get(); |
193 |
> |
}}); |
194 |
> |
t.start(); |
195 |
> |
submitted.await(); |
196 |
> |
t.interrupt(); |
197 |
> |
t.join(); |
198 |
|
} finally { |
199 |
+ |
quittingTime.countDown(); |
200 |
|
joinPool(p); |
201 |
|
} |
202 |
|
} |
203 |
|
|
246 |
– |
|
247 |
– |
/** |
248 |
– |
* Blocking on submit(callable) throws InterruptedException if |
249 |
– |
* caller interrupted. |
250 |
– |
*/ |
251 |
– |
public void testInterruptedSubmit() throws InterruptedException { |
252 |
– |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
253 |
– |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
254 |
– |
public void realRun() throws Exception { |
255 |
– |
p.submit(new CheckedCallable<Object>() { |
256 |
– |
public Object realCall() |
257 |
– |
throws InterruptedException { |
258 |
– |
Thread.sleep(SMALL_DELAY_MS); |
259 |
– |
return null; |
260 |
– |
}}).get(); |
261 |
– |
}}); |
262 |
– |
|
263 |
– |
t.start(); |
264 |
– |
Thread.sleep(SHORT_DELAY_MS); |
265 |
– |
t.interrupt(); |
266 |
– |
joinPool(p); |
267 |
– |
} |
268 |
– |
|
269 |
– |
/** |
270 |
– |
* get of submitted callable throws InterruptedException if callable |
271 |
– |
* interrupted |
272 |
– |
*/ |
273 |
– |
public void testSubmitIE() throws InterruptedException { |
274 |
– |
final ThreadPoolExecutor p = |
275 |
– |
new ThreadPoolExecutor(1, 1, |
276 |
– |
60, TimeUnit.SECONDS, |
277 |
– |
new ArrayBlockingQueue<Runnable>(10)); |
278 |
– |
|
279 |
– |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
280 |
– |
public void realRun() throws Exception { |
281 |
– |
p.submit(new SmallCallable()).get(); |
282 |
– |
}}); |
283 |
– |
|
284 |
– |
t.start(); |
285 |
– |
Thread.sleep(SHORT_DELAY_MS); |
286 |
– |
t.interrupt(); |
287 |
– |
t.join(); |
288 |
– |
joinPool(p); |
289 |
– |
} |
290 |
– |
|
204 |
|
/** |
205 |
< |
* get of submit(callable) throws ExecutionException if callable |
206 |
< |
* throws exception |
205 |
> |
* get of submit(callable) throws ExecutionException if callable |
206 |
> |
* throws exception |
207 |
|
*/ |
208 |
|
public void testSubmitEE() throws InterruptedException { |
209 |
|
ThreadPoolExecutor p = |
212 |
|
new ArrayBlockingQueue<Runnable>(10)); |
213 |
|
|
214 |
|
Callable c = new Callable() { |
215 |
< |
public Object call() { return 5/0; }}; |
215 |
> |
public Object call() { throw new ArithmeticException(); }}; |
216 |
|
|
217 |
|
try { |
218 |
|
p.submit(c).get(); |
226 |
|
/** |
227 |
|
* invokeAny(null) throws NPE |
228 |
|
*/ |
229 |
< |
public void testInvokeAny1() |
317 |
< |
throws InterruptedException, ExecutionException { |
229 |
> |
public void testInvokeAny1() throws Exception { |
230 |
|
ExecutorService e = new DirectExecutorService(); |
231 |
|
try { |
232 |
|
e.invokeAny(null); |
240 |
|
/** |
241 |
|
* invokeAny(empty collection) throws IAE |
242 |
|
*/ |
243 |
< |
public void testInvokeAny2() |
332 |
< |
throws InterruptedException, ExecutionException { |
243 |
> |
public void testInvokeAny2() throws Exception { |
244 |
|
ExecutorService e = new DirectExecutorService(); |
245 |
|
try { |
246 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
255 |
|
* invokeAny(c) throws NPE if c has null elements |
256 |
|
*/ |
257 |
|
public void testInvokeAny3() throws Exception { |
347 |
– |
final CountDownLatch latch = new CountDownLatch(1); |
258 |
|
ExecutorService e = new DirectExecutorService(); |
259 |
+ |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
260 |
+ |
l.add(new Callable<Long>() { |
261 |
+ |
public Long call() { throw new ArithmeticException(); }}); |
262 |
+ |
l.add(null); |
263 |
|
try { |
350 |
– |
ArrayList<Callable<Integer>> l |
351 |
– |
= new ArrayList<Callable<Integer>>(); |
352 |
– |
l.add(new Callable<Integer>() { |
353 |
– |
public Integer call() { return 5/0; }}); |
354 |
– |
l.add(null); |
264 |
|
e.invokeAny(l); |
265 |
|
shouldThrow(); |
266 |
|
} catch (NullPointerException success) { |
267 |
|
} finally { |
359 |
– |
latch.countDown(); |
268 |
|
joinPool(e); |
269 |
|
} |
270 |
|
} |
274 |
|
*/ |
275 |
|
public void testInvokeAny4() throws InterruptedException { |
276 |
|
ExecutorService e = new DirectExecutorService(); |
277 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
278 |
+ |
l.add(new NPETask()); |
279 |
|
try { |
370 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
371 |
– |
l.add(new NPETask()); |
280 |
|
e.invokeAny(l); |
281 |
|
shouldThrow(); |
282 |
|
} catch (ExecutionException success) { |
292 |
|
public void testInvokeAny5() throws Exception { |
293 |
|
ExecutorService e = new DirectExecutorService(); |
294 |
|
try { |
295 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
295 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
296 |
|
l.add(new StringTask()); |
297 |
|
l.add(new StringTask()); |
298 |
|
String result = e.invokeAny(l); |
334 |
|
*/ |
335 |
|
public void testInvokeAll3() throws InterruptedException { |
336 |
|
ExecutorService e = new DirectExecutorService(); |
337 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
338 |
+ |
l.add(new StringTask()); |
339 |
+ |
l.add(null); |
340 |
|
try { |
430 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
431 |
– |
l.add(new StringTask()); |
432 |
– |
l.add(null); |
341 |
|
e.invokeAll(l); |
342 |
|
shouldThrow(); |
343 |
|
} catch (NullPointerException success) { |
352 |
|
public void testInvokeAll4() throws Exception { |
353 |
|
ExecutorService e = new DirectExecutorService(); |
354 |
|
try { |
355 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
355 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
356 |
|
l.add(new NPETask()); |
357 |
< |
List<Future<String>> result = e.invokeAll(l); |
358 |
< |
assertEquals(1, result.size()); |
359 |
< |
for (Future<String> future : result) { |
360 |
< |
try { |
361 |
< |
future.get(); |
362 |
< |
shouldThrow(); |
363 |
< |
} catch (ExecutionException success) { |
456 |
< |
Throwable cause = success.getCause(); |
457 |
< |
assertTrue(cause instanceof NullPointerException); |
458 |
< |
} |
357 |
> |
List<Future<String>> futures = e.invokeAll(l); |
358 |
> |
assertEquals(1, futures.size()); |
359 |
> |
try { |
360 |
> |
futures.get(0).get(); |
361 |
> |
shouldThrow(); |
362 |
> |
} catch (ExecutionException success) { |
363 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
364 |
|
} |
365 |
|
} finally { |
366 |
|
joinPool(e); |
373 |
|
public void testInvokeAll5() throws Exception { |
374 |
|
ExecutorService e = new DirectExecutorService(); |
375 |
|
try { |
376 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
376 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
377 |
|
l.add(new StringTask()); |
378 |
|
l.add(new StringTask()); |
379 |
< |
List<Future<String>> result = e.invokeAll(l); |
380 |
< |
assertEquals(2, result.size()); |
381 |
< |
for (Future<String> future : result) |
379 |
> |
List<Future<String>> futures = e.invokeAll(l); |
380 |
> |
assertEquals(2, futures.size()); |
381 |
> |
for (Future<String> future : futures) |
382 |
|
assertSame(TEST_STRING, future.get()); |
383 |
|
} finally { |
384 |
|
joinPool(e); |
385 |
|
} |
386 |
|
} |
387 |
|
|
483 |
– |
|
388 |
|
/** |
389 |
|
* timed invokeAny(null) throws NPE |
390 |
|
*/ |
391 |
|
public void testTimedInvokeAny1() throws Exception { |
392 |
|
ExecutorService e = new DirectExecutorService(); |
393 |
|
try { |
394 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
394 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
395 |
|
shouldThrow(); |
396 |
|
} catch (NullPointerException success) { |
397 |
|
} finally { |
404 |
|
*/ |
405 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
406 |
|
ExecutorService e = new DirectExecutorService(); |
407 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
408 |
+ |
l.add(new StringTask()); |
409 |
|
try { |
504 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
505 |
– |
l.add(new StringTask()); |
410 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
411 |
|
shouldThrow(); |
412 |
|
} catch (NullPointerException success) { |
421 |
|
public void testTimedInvokeAny2() throws Exception { |
422 |
|
ExecutorService e = new DirectExecutorService(); |
423 |
|
try { |
424 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
424 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
425 |
|
shouldThrow(); |
426 |
|
} catch (IllegalArgumentException success) { |
427 |
|
} finally { |
433 |
|
* timed invokeAny(c) throws NPE if c has null elements |
434 |
|
*/ |
435 |
|
public void testTimedInvokeAny3() throws Exception { |
532 |
– |
final CountDownLatch latch = new CountDownLatch(1); |
436 |
|
ExecutorService e = new DirectExecutorService(); |
437 |
+ |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
438 |
+ |
l.add(new Callable<Long>() { |
439 |
+ |
public Long call() { throw new ArithmeticException(); }}); |
440 |
+ |
l.add(null); |
441 |
|
try { |
442 |
< |
ArrayList<Callable<Integer>> l |
536 |
< |
= new ArrayList<Callable<Integer>>(); |
537 |
< |
l.add(new Callable<Integer>() { |
538 |
< |
public Integer call() { return 5/0; }}); |
539 |
< |
l.add(null); |
540 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
442 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
443 |
|
shouldThrow(); |
444 |
|
} catch (NullPointerException success) { |
445 |
|
} finally { |
544 |
– |
latch.countDown(); |
446 |
|
joinPool(e); |
447 |
|
} |
448 |
|
} |
452 |
|
*/ |
453 |
|
public void testTimedInvokeAny4() throws Exception { |
454 |
|
ExecutorService e = new DirectExecutorService(); |
455 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
456 |
+ |
l.add(new NPETask()); |
457 |
|
try { |
458 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
556 |
< |
l.add(new NPETask()); |
557 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
458 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
459 |
|
shouldThrow(); |
460 |
|
} catch (ExecutionException success) { |
461 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
470 |
|
public void testTimedInvokeAny5() throws Exception { |
471 |
|
ExecutorService e = new DirectExecutorService(); |
472 |
|
try { |
473 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
473 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
474 |
|
l.add(new StringTask()); |
475 |
|
l.add(new StringTask()); |
476 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
476 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
477 |
|
assertSame(TEST_STRING, result); |
478 |
|
} finally { |
479 |
|
joinPool(e); |
486 |
|
public void testTimedInvokeAll1() throws InterruptedException { |
487 |
|
ExecutorService e = new DirectExecutorService(); |
488 |
|
try { |
489 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
489 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
490 |
|
shouldThrow(); |
491 |
|
} catch (NullPointerException success) { |
492 |
|
} finally { |
499 |
|
*/ |
500 |
|
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
501 |
|
ExecutorService e = new DirectExecutorService(); |
502 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
503 |
+ |
l.add(new StringTask()); |
504 |
|
try { |
602 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
603 |
– |
l.add(new StringTask()); |
505 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
506 |
|
shouldThrow(); |
507 |
|
} catch (NullPointerException success) { |
516 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
517 |
|
ExecutorService e = new DirectExecutorService(); |
518 |
|
try { |
519 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
519 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
520 |
|
assertTrue(r.isEmpty()); |
521 |
|
} finally { |
522 |
|
joinPool(e); |
528 |
|
*/ |
529 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
530 |
|
ExecutorService e = new DirectExecutorService(); |
531 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
532 |
+ |
l.add(new StringTask()); |
533 |
+ |
l.add(null); |
534 |
|
try { |
535 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
632 |
< |
l.add(new StringTask()); |
633 |
< |
l.add(null); |
634 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
535 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
536 |
|
shouldThrow(); |
537 |
|
} catch (NullPointerException success) { |
538 |
|
} finally { |
546 |
|
public void testTimedInvokeAll4() throws Exception { |
547 |
|
ExecutorService e = new DirectExecutorService(); |
548 |
|
try { |
549 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
549 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
550 |
|
l.add(new NPETask()); |
551 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
552 |
< |
assertEquals(1, result.size()); |
553 |
< |
for (Future<String> future : result) { |
554 |
< |
try { |
555 |
< |
future.get(); |
556 |
< |
} catch (ExecutionException success) { |
557 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
558 |
< |
} |
551 |
> |
List<Future<String>> futures = |
552 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
553 |
> |
assertEquals(1, futures.size()); |
554 |
> |
try { |
555 |
> |
futures.get(0).get(); |
556 |
> |
shouldThrow(); |
557 |
> |
} catch (ExecutionException success) { |
558 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
559 |
|
} |
560 |
|
} finally { |
561 |
|
joinPool(e); |
568 |
|
public void testTimedInvokeAll5() throws Exception { |
569 |
|
ExecutorService e = new DirectExecutorService(); |
570 |
|
try { |
571 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
571 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
572 |
|
l.add(new StringTask()); |
573 |
|
l.add(new StringTask()); |
574 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
575 |
< |
assertEquals(2, result.size()); |
576 |
< |
for (Future<String> future : result) |
574 |
> |
List<Future<String>> futures = |
575 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
576 |
> |
assertEquals(2, futures.size()); |
577 |
> |
for (Future<String> future : futures) |
578 |
|
assertSame(TEST_STRING, future.get()); |
579 |
|
} finally { |
580 |
|
joinPool(e); |
587 |
|
public void testTimedInvokeAll6() throws InterruptedException { |
588 |
|
ExecutorService e = new DirectExecutorService(); |
589 |
|
try { |
590 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
590 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
591 |
|
l.add(new StringTask()); |
592 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
592 |
> |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
593 |
|
l.add(new StringTask()); |
594 |
< |
List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
595 |
< |
assertEquals(3, result.size()); |
596 |
< |
Iterator<Future<String>> it = result.iterator(); |
597 |
< |
Future<String> f1 = it.next(); |
598 |
< |
Future<String> f2 = it.next(); |
599 |
< |
Future<String> f3 = it.next(); |
600 |
< |
assertTrue(f1.isDone()); |
601 |
< |
assertFalse(f1.isCancelled()); |
700 |
< |
assertTrue(f2.isDone()); |
701 |
< |
assertTrue(f3.isDone()); |
702 |
< |
assertTrue(f3.isCancelled()); |
594 |
> |
List<Future<String>> futures = |
595 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
596 |
> |
assertEquals(l.size(), futures.size()); |
597 |
> |
for (Future future : futures) |
598 |
> |
assertTrue(future.isDone()); |
599 |
> |
assertFalse(futures.get(0).isCancelled()); |
600 |
> |
assertFalse(futures.get(1).isCancelled()); |
601 |
> |
assertTrue(futures.get(2).isCancelled()); |
602 |
|
} finally { |
603 |
|
joinPool(e); |
604 |
|
} |