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 |
+ |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
|
|
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
13 |
< |
import java.util.concurrent.*; |
14 |
< |
import java.math.BigInteger; |
15 |
< |
import java.security.*; |
11 |
> |
import java.security.PrivilegedAction; |
12 |
> |
import java.security.PrivilegedExceptionAction; |
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.Collections; |
15 |
> |
import java.util.List; |
16 |
> |
import java.util.concurrent.AbstractExecutorService; |
17 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
18 |
> |
import java.util.concurrent.Callable; |
19 |
> |
import java.util.concurrent.CountDownLatch; |
20 |
> |
import java.util.concurrent.ExecutionException; |
21 |
> |
import java.util.concurrent.Executors; |
22 |
> |
import java.util.concurrent.ExecutorService; |
23 |
> |
import java.util.concurrent.Future; |
24 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
25 |
> |
import java.util.concurrent.TimeUnit; |
26 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
27 |
> |
|
28 |
> |
import junit.framework.Test; |
29 |
> |
import junit.framework.TestSuite; |
30 |
|
|
31 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
32 |
|
public static void main(String[] args) { |
33 |
< |
junit.textui.TestRunner.run (suite()); |
33 |
> |
junit.textui.TestRunner.run(suite()); |
34 |
|
} |
35 |
|
public static Test suite() { |
36 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
43 |
|
static class DirectExecutorService extends AbstractExecutorService { |
44 |
|
public void execute(Runnable r) { r.run(); } |
45 |
|
public void shutdown() { shutdown = true; } |
46 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
46 |
> |
public List<Runnable> shutdownNow() { |
47 |
> |
shutdown = true; |
48 |
> |
return Collections.EMPTY_LIST; |
49 |
> |
} |
50 |
|
public boolean isShutdown() { return shutdown; } |
51 |
|
public boolean isTerminated() { return isShutdown(); } |
52 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
52 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
53 |
> |
return isShutdown(); |
54 |
> |
} |
55 |
|
private volatile boolean shutdown = false; |
56 |
|
} |
57 |
|
|
60 |
|
*/ |
61 |
|
public void testExecuteRunnable() throws Exception { |
62 |
|
ExecutorService e = new DirectExecutorService(); |
63 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
64 |
< |
assertFalse(task.done); |
65 |
< |
Future<?> future = e.submit(task); |
66 |
< |
future.get(); |
67 |
< |
assertTrue(task.done); |
63 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
64 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
65 |
> |
public void realRun() { |
66 |
> |
done.set(true); |
67 |
> |
}}); |
68 |
> |
assertNull(future.get()); |
69 |
> |
assertNull(future.get(0, MILLISECONDS)); |
70 |
> |
assertTrue(done.get()); |
71 |
> |
assertTrue(future.isDone()); |
72 |
> |
assertFalse(future.isCancelled()); |
73 |
|
} |
74 |
|
|
50 |
– |
|
75 |
|
/** |
76 |
|
* Completed submit(callable) returns result |
77 |
|
*/ |
102 |
|
assertSame(TEST_STRING, result); |
103 |
|
} |
104 |
|
|
81 |
– |
|
105 |
|
/** |
106 |
< |
* A submitted privileged action to completion |
106 |
> |
* A submitted privileged action runs to completion |
107 |
|
*/ |
108 |
|
public void testSubmitPrivilegedAction() throws Exception { |
109 |
< |
Policy savedPolicy = null; |
110 |
< |
try { |
111 |
< |
savedPolicy = Policy.getPolicy(); |
112 |
< |
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() { |
109 |
> |
Runnable r = new CheckedRunnable() { |
110 |
> |
public void realRun() throws Exception { |
111 |
> |
ExecutorService e = new DirectExecutorService(); |
112 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
113 |
|
public Object run() { |
114 |
|
return TEST_STRING; |
115 |
|
}})); |
116 |
|
|
117 |
< |
Object result = future.get(); |
118 |
< |
assertSame(TEST_STRING, result); |
119 |
< |
} |
120 |
< |
finally { |
121 |
< |
try { |
122 |
< |
Policy.setPolicy(savedPolicy); |
123 |
< |
} catch (AccessControlException ok) { |
110 |
< |
return; |
111 |
< |
} |
112 |
< |
} |
117 |
> |
assertSame(TEST_STRING, future.get()); |
118 |
> |
}}; |
119 |
> |
|
120 |
> |
runWithPermissions(r, |
121 |
> |
new RuntimePermission("getClassLoader"), |
122 |
> |
new RuntimePermission("setContextClassLoader"), |
123 |
> |
new RuntimePermission("modifyThread")); |
124 |
|
} |
125 |
|
|
126 |
|
/** |
127 |
< |
* A submitted a privileged exception action runs to completion |
127 |
> |
* A submitted privileged exception action runs to completion |
128 |
|
*/ |
129 |
|
public void testSubmitPrivilegedExceptionAction() throws Exception { |
130 |
< |
Policy savedPolicy = null; |
131 |
< |
try { |
132 |
< |
savedPolicy = Policy.getPolicy(); |
133 |
< |
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() { |
130 |
> |
Runnable r = new CheckedRunnable() { |
131 |
> |
public void realRun() throws Exception { |
132 |
> |
ExecutorService e = new DirectExecutorService(); |
133 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
134 |
|
public Object run() { |
135 |
|
return TEST_STRING; |
136 |
|
}})); |
137 |
|
|
138 |
< |
Object result = future.get(); |
139 |
< |
assertSame(TEST_STRING, result); |
140 |
< |
} |
141 |
< |
finally { |
141 |
< |
Policy.setPolicy(savedPolicy); |
142 |
< |
} |
138 |
> |
assertSame(TEST_STRING, future.get()); |
139 |
> |
}}; |
140 |
> |
|
141 |
> |
runWithPermissions(r); |
142 |
|
} |
143 |
|
|
144 |
|
/** |
145 |
|
* A submitted failed privileged exception action reports exception |
146 |
|
*/ |
147 |
|
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
148 |
< |
Policy savedPolicy = null; |
149 |
< |
try { |
150 |
< |
savedPolicy = Policy.getPolicy(); |
151 |
< |
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() { |
148 |
> |
Runnable r = new CheckedRunnable() { |
149 |
> |
public void realRun() throws Exception { |
150 |
> |
ExecutorService e = new DirectExecutorService(); |
151 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
152 |
|
public Object run() throws Exception { |
153 |
|
throw new IndexOutOfBoundsException(); |
154 |
|
}})); |
155 |
|
|
156 |
< |
future.get(); |
157 |
< |
shouldThrow(); |
158 |
< |
} catch (ExecutionException success) { |
159 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
160 |
< |
} |
161 |
< |
finally { |
162 |
< |
Policy.setPolicy(savedPolicy); |
163 |
< |
} |
156 |
> |
try { |
157 |
> |
future.get(); |
158 |
> |
shouldThrow(); |
159 |
> |
} catch (ExecutionException success) { |
160 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
161 |
> |
}}}; |
162 |
> |
|
163 |
> |
runWithPermissions(r); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
174 |
|
} catch (NullPointerException success) {} |
175 |
|
} |
176 |
|
|
188 |
– |
|
177 |
|
/** |
178 |
|
* submit(null callable) throws NPE |
179 |
|
*/ |
186 |
|
} |
187 |
|
|
188 |
|
/** |
189 |
< |
* 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. |
189 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
190 |
|
*/ |
191 |
< |
public void testExecute2() { |
192 |
< |
ThreadPoolExecutor p = |
193 |
< |
new ThreadPoolExecutor(1, 1, |
194 |
< |
60, TimeUnit.SECONDS, |
195 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
196 |
< |
try { |
197 |
< |
for (int i = 0; i < 2; ++i) |
198 |
< |
p.submit(new MediumRunnable()); |
199 |
< |
for (int i = 0; i < 2; ++i) { |
200 |
< |
try { |
201 |
< |
p.submit(new SmallCallable()); |
202 |
< |
shouldThrow(); |
203 |
< |
} catch (RejectedExecutionException success) {} |
204 |
< |
} |
191 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
192 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
193 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
194 |
> |
final ExecutorService p |
195 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
196 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
197 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
198 |
> |
public Void realCall() throws InterruptedException { |
199 |
> |
quittingTime.await(); |
200 |
> |
return null; |
201 |
> |
}}; |
202 |
> |
try { |
203 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
204 |
> |
public void realRun() throws Exception { |
205 |
> |
Future<Void> future = p.submit(awaiter); |
206 |
> |
submitted.countDown(); |
207 |
> |
future.get(); |
208 |
> |
}}); |
209 |
> |
t.start(); |
210 |
> |
submitted.await(); |
211 |
> |
t.interrupt(); |
212 |
> |
t.join(); |
213 |
|
} finally { |
214 |
+ |
quittingTime.countDown(); |
215 |
|
joinPool(p); |
216 |
|
} |
217 |
|
} |
218 |
|
|
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 |
– |
|
219 |
|
/** |
220 |
< |
* get of submit(callable) throws ExecutionException if callable |
221 |
< |
* throws exception |
220 |
> |
* get of submit(callable) throws ExecutionException if callable |
221 |
> |
* throws exception |
222 |
|
*/ |
223 |
|
public void testSubmitEE() throws InterruptedException { |
224 |
|
ThreadPoolExecutor p = |
227 |
|
new ArrayBlockingQueue<Runnable>(10)); |
228 |
|
|
229 |
|
Callable c = new Callable() { |
230 |
< |
public Object call() { return 5/0; }}; |
230 |
> |
public Object call() { throw new ArithmeticException(); }}; |
231 |
|
|
232 |
|
try { |
233 |
|
p.submit(c).get(); |
241 |
|
/** |
242 |
|
* invokeAny(null) throws NPE |
243 |
|
*/ |
244 |
< |
public void testInvokeAny1() |
317 |
< |
throws InterruptedException, ExecutionException { |
244 |
> |
public void testInvokeAny1() throws Exception { |
245 |
|
ExecutorService e = new DirectExecutorService(); |
246 |
|
try { |
247 |
|
e.invokeAny(null); |
255 |
|
/** |
256 |
|
* invokeAny(empty collection) throws IAE |
257 |
|
*/ |
258 |
< |
public void testInvokeAny2() |
332 |
< |
throws InterruptedException, ExecutionException { |
258 |
> |
public void testInvokeAny2() throws Exception { |
259 |
|
ExecutorService e = new DirectExecutorService(); |
260 |
|
try { |
261 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
270 |
|
* invokeAny(c) throws NPE if c has null elements |
271 |
|
*/ |
272 |
|
public void testInvokeAny3() throws Exception { |
347 |
– |
final CountDownLatch latch = new CountDownLatch(1); |
273 |
|
ExecutorService e = new DirectExecutorService(); |
274 |
+ |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
275 |
+ |
l.add(new Callable<Long>() { |
276 |
+ |
public Long call() { throw new ArithmeticException(); }}); |
277 |
+ |
l.add(null); |
278 |
|
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); |
279 |
|
e.invokeAny(l); |
280 |
|
shouldThrow(); |
281 |
|
} catch (NullPointerException success) { |
282 |
|
} finally { |
359 |
– |
latch.countDown(); |
283 |
|
joinPool(e); |
284 |
|
} |
285 |
|
} |
289 |
|
*/ |
290 |
|
public void testInvokeAny4() throws InterruptedException { |
291 |
|
ExecutorService e = new DirectExecutorService(); |
292 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
293 |
+ |
l.add(new NPETask()); |
294 |
|
try { |
370 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
371 |
– |
l.add(new NPETask()); |
295 |
|
e.invokeAny(l); |
296 |
|
shouldThrow(); |
297 |
|
} catch (ExecutionException success) { |
307 |
|
public void testInvokeAny5() throws Exception { |
308 |
|
ExecutorService e = new DirectExecutorService(); |
309 |
|
try { |
310 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
310 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
311 |
|
l.add(new StringTask()); |
312 |
|
l.add(new StringTask()); |
313 |
|
String result = e.invokeAny(l); |
349 |
|
*/ |
350 |
|
public void testInvokeAll3() throws InterruptedException { |
351 |
|
ExecutorService e = new DirectExecutorService(); |
352 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
353 |
+ |
l.add(new StringTask()); |
354 |
+ |
l.add(null); |
355 |
|
try { |
430 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
431 |
– |
l.add(new StringTask()); |
432 |
– |
l.add(null); |
356 |
|
e.invokeAll(l); |
357 |
|
shouldThrow(); |
358 |
|
} catch (NullPointerException success) { |
367 |
|
public void testInvokeAll4() throws Exception { |
368 |
|
ExecutorService e = new DirectExecutorService(); |
369 |
|
try { |
370 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
370 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
371 |
|
l.add(new NPETask()); |
372 |
< |
List<Future<String>> result = e.invokeAll(l); |
373 |
< |
assertEquals(1, result.size()); |
374 |
< |
for (Future<String> future : result) { |
375 |
< |
try { |
376 |
< |
future.get(); |
377 |
< |
shouldThrow(); |
378 |
< |
} catch (ExecutionException success) { |
456 |
< |
Throwable cause = success.getCause(); |
457 |
< |
assertTrue(cause instanceof NullPointerException); |
458 |
< |
} |
372 |
> |
List<Future<String>> futures = e.invokeAll(l); |
373 |
> |
assertEquals(1, futures.size()); |
374 |
> |
try { |
375 |
> |
futures.get(0).get(); |
376 |
> |
shouldThrow(); |
377 |
> |
} catch (ExecutionException success) { |
378 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
379 |
|
} |
380 |
|
} finally { |
381 |
|
joinPool(e); |
388 |
|
public void testInvokeAll5() throws Exception { |
389 |
|
ExecutorService e = new DirectExecutorService(); |
390 |
|
try { |
391 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
391 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
392 |
|
l.add(new StringTask()); |
393 |
|
l.add(new StringTask()); |
394 |
< |
List<Future<String>> result = e.invokeAll(l); |
395 |
< |
assertEquals(2, result.size()); |
396 |
< |
for (Future<String> future : result) |
394 |
> |
List<Future<String>> futures = e.invokeAll(l); |
395 |
> |
assertEquals(2, futures.size()); |
396 |
> |
for (Future<String> future : futures) |
397 |
|
assertSame(TEST_STRING, future.get()); |
398 |
|
} finally { |
399 |
|
joinPool(e); |
400 |
|
} |
401 |
|
} |
402 |
|
|
483 |
– |
|
403 |
|
/** |
404 |
|
* timed invokeAny(null) throws NPE |
405 |
|
*/ |
406 |
|
public void testTimedInvokeAny1() throws Exception { |
407 |
|
ExecutorService e = new DirectExecutorService(); |
408 |
|
try { |
409 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
409 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
410 |
|
shouldThrow(); |
411 |
|
} catch (NullPointerException success) { |
412 |
|
} finally { |
419 |
|
*/ |
420 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
421 |
|
ExecutorService e = new DirectExecutorService(); |
422 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
423 |
+ |
l.add(new StringTask()); |
424 |
|
try { |
504 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
505 |
– |
l.add(new StringTask()); |
425 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
426 |
|
shouldThrow(); |
427 |
|
} catch (NullPointerException success) { |
436 |
|
public void testTimedInvokeAny2() throws Exception { |
437 |
|
ExecutorService e = new DirectExecutorService(); |
438 |
|
try { |
439 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
439 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
440 |
|
shouldThrow(); |
441 |
|
} catch (IllegalArgumentException success) { |
442 |
|
} finally { |
448 |
|
* timed invokeAny(c) throws NPE if c has null elements |
449 |
|
*/ |
450 |
|
public void testTimedInvokeAny3() throws Exception { |
532 |
– |
final CountDownLatch latch = new CountDownLatch(1); |
451 |
|
ExecutorService e = new DirectExecutorService(); |
452 |
+ |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
453 |
+ |
l.add(new Callable<Long>() { |
454 |
+ |
public Long call() { throw new ArithmeticException(); }}); |
455 |
+ |
l.add(null); |
456 |
|
try { |
457 |
< |
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); |
457 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
458 |
|
shouldThrow(); |
459 |
|
} catch (NullPointerException success) { |
460 |
|
} finally { |
544 |
– |
latch.countDown(); |
461 |
|
joinPool(e); |
462 |
|
} |
463 |
|
} |
467 |
|
*/ |
468 |
|
public void testTimedInvokeAny4() throws Exception { |
469 |
|
ExecutorService e = new DirectExecutorService(); |
470 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
471 |
+ |
l.add(new NPETask()); |
472 |
|
try { |
473 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
556 |
< |
l.add(new NPETask()); |
557 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
473 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
474 |
|
shouldThrow(); |
475 |
|
} catch (ExecutionException success) { |
476 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
485 |
|
public void testTimedInvokeAny5() throws Exception { |
486 |
|
ExecutorService e = new DirectExecutorService(); |
487 |
|
try { |
488 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
488 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
489 |
|
l.add(new StringTask()); |
490 |
|
l.add(new StringTask()); |
491 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
491 |
> |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
492 |
|
assertSame(TEST_STRING, result); |
493 |
|
} finally { |
494 |
|
joinPool(e); |
501 |
|
public void testTimedInvokeAll1() throws InterruptedException { |
502 |
|
ExecutorService e = new DirectExecutorService(); |
503 |
|
try { |
504 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
504 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
505 |
|
shouldThrow(); |
506 |
|
} catch (NullPointerException success) { |
507 |
|
} finally { |
514 |
|
*/ |
515 |
|
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
516 |
|
ExecutorService e = new DirectExecutorService(); |
517 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
518 |
+ |
l.add(new StringTask()); |
519 |
|
try { |
602 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
603 |
– |
l.add(new StringTask()); |
520 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
521 |
|
shouldThrow(); |
522 |
|
} catch (NullPointerException success) { |
531 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
532 |
|
ExecutorService e = new DirectExecutorService(); |
533 |
|
try { |
534 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
534 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
535 |
|
assertTrue(r.isEmpty()); |
536 |
|
} finally { |
537 |
|
joinPool(e); |
543 |
|
*/ |
544 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
545 |
|
ExecutorService e = new DirectExecutorService(); |
546 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
547 |
+ |
l.add(new StringTask()); |
548 |
+ |
l.add(null); |
549 |
|
try { |
550 |
< |
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); |
550 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
551 |
|
shouldThrow(); |
552 |
|
} catch (NullPointerException success) { |
553 |
|
} finally { |
561 |
|
public void testTimedInvokeAll4() 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 NPETask()); |
566 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
567 |
< |
assertEquals(1, result.size()); |
568 |
< |
for (Future<String> future : result) { |
569 |
< |
try { |
570 |
< |
future.get(); |
571 |
< |
} catch (ExecutionException success) { |
572 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
573 |
< |
} |
566 |
> |
List<Future<String>> futures = |
567 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
568 |
> |
assertEquals(1, futures.size()); |
569 |
> |
try { |
570 |
> |
futures.get(0).get(); |
571 |
> |
shouldThrow(); |
572 |
> |
} catch (ExecutionException success) { |
573 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
574 |
|
} |
575 |
|
} finally { |
576 |
|
joinPool(e); |
583 |
|
public void testTimedInvokeAll5() throws Exception { |
584 |
|
ExecutorService e = new DirectExecutorService(); |
585 |
|
try { |
586 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
586 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
587 |
|
l.add(new StringTask()); |
588 |
|
l.add(new StringTask()); |
589 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS); |
590 |
< |
assertEquals(2, result.size()); |
591 |
< |
for (Future<String> future : result) |
589 |
> |
List<Future<String>> futures = |
590 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
591 |
> |
assertEquals(2, futures.size()); |
592 |
> |
for (Future<String> future : futures) |
593 |
|
assertSame(TEST_STRING, future.get()); |
594 |
|
} finally { |
595 |
|
joinPool(e); |
602 |
|
public void testTimedInvokeAll6() throws InterruptedException { |
603 |
|
ExecutorService e = new DirectExecutorService(); |
604 |
|
try { |
605 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
605 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
606 |
|
l.add(new StringTask()); |
607 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
607 |
> |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
608 |
|
l.add(new StringTask()); |
609 |
< |
List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, TimeUnit.MILLISECONDS); |
610 |
< |
assertEquals(3, result.size()); |
611 |
< |
Iterator<Future<String>> it = result.iterator(); |
612 |
< |
Future<String> f1 = it.next(); |
613 |
< |
Future<String> f2 = it.next(); |
614 |
< |
Future<String> f3 = it.next(); |
615 |
< |
assertTrue(f1.isDone()); |
616 |
< |
assertFalse(f1.isCancelled()); |
700 |
< |
assertTrue(f2.isDone()); |
701 |
< |
assertTrue(f3.isDone()); |
702 |
< |
assertTrue(f3.isCancelled()); |
609 |
> |
List<Future<String>> futures = |
610 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
611 |
> |
assertEquals(l.size(), futures.size()); |
612 |
> |
for (Future future : futures) |
613 |
> |
assertTrue(future.isDone()); |
614 |
> |
assertFalse(futures.get(0).isCancelled()); |
615 |
> |
assertFalse(futures.get(1).isCancelled()); |
616 |
> |
assertTrue(futures.get(2).isCancelled()); |
617 |
|
} finally { |
618 |
|
joinPool(e); |
619 |
|
} |