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 |
– |
|
10 |
– |
import junit.framework.*; |
11 |
– |
import java.util.*; |
12 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.math.BigInteger; |
11 |
< |
import java.security.*; |
10 |
> |
|
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.CancellationException; |
20 |
> |
import java.util.concurrent.CountDownLatch; |
21 |
> |
import java.util.concurrent.ExecutionException; |
22 |
> |
import java.util.concurrent.Executors; |
23 |
> |
import java.util.concurrent.ExecutorService; |
24 |
> |
import java.util.concurrent.Future; |
25 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
26 |
> |
import java.util.concurrent.TimeUnit; |
27 |
> |
import java.util.concurrent.atomic.AtomicBoolean; |
28 |
> |
|
29 |
> |
import junit.framework.Test; |
30 |
> |
import junit.framework.TestSuite; |
31 |
|
|
32 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
33 |
|
public static void main(String[] args) { |
34 |
< |
junit.textui.TestRunner.run (suite()); |
34 |
> |
main(suite(), args); |
35 |
|
} |
36 |
|
public static Test suite() { |
37 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
44 |
|
static class DirectExecutorService extends AbstractExecutorService { |
45 |
|
public void execute(Runnable r) { r.run(); } |
46 |
|
public void shutdown() { shutdown = true; } |
47 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
47 |
> |
public List<Runnable> shutdownNow() { |
48 |
> |
shutdown = true; |
49 |
> |
return Collections.EMPTY_LIST; |
50 |
> |
} |
51 |
|
public boolean isShutdown() { return shutdown; } |
52 |
|
public boolean isTerminated() { return isShutdown(); } |
53 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
53 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
54 |
> |
return isShutdown(); |
55 |
> |
} |
56 |
|
private volatile boolean shutdown = false; |
57 |
|
} |
58 |
|
|
61 |
|
*/ |
62 |
|
public void testExecuteRunnable() throws Exception { |
63 |
|
ExecutorService e = new DirectExecutorService(); |
64 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
65 |
< |
assertFalse(task.done); |
66 |
< |
Future<?> future = e.submit(task); |
67 |
< |
future.get(); |
68 |
< |
assertTrue(task.done); |
64 |
> |
final AtomicBoolean done = new AtomicBoolean(false); |
65 |
> |
Future<?> future = e.submit(new CheckedRunnable() { |
66 |
> |
public void realRun() { |
67 |
> |
done.set(true); |
68 |
> |
}}); |
69 |
> |
assertNull(future.get()); |
70 |
> |
assertNull(future.get(0, MILLISECONDS)); |
71 |
> |
assertTrue(done.get()); |
72 |
> |
assertTrue(future.isDone()); |
73 |
> |
assertFalse(future.isCancelled()); |
74 |
|
} |
75 |
|
|
51 |
– |
|
76 |
|
/** |
77 |
|
* Completed submit(callable) returns result |
78 |
|
*/ |
103 |
|
assertSame(TEST_STRING, result); |
104 |
|
} |
105 |
|
|
82 |
– |
|
106 |
|
/** |
107 |
< |
* A submitted privileged action to completion |
107 |
> |
* A submitted privileged action runs to completion |
108 |
|
*/ |
109 |
|
public void testSubmitPrivilegedAction() throws Exception { |
110 |
< |
Policy savedPolicy = null; |
111 |
< |
try { |
112 |
< |
savedPolicy = Policy.getPolicy(); |
113 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
91 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
92 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
93 |
< |
Policy.setPolicy(policy); |
94 |
< |
} catch (AccessControlException ok) { |
95 |
< |
return; |
96 |
< |
} |
97 |
< |
try { |
98 |
< |
ExecutorService e = new DirectExecutorService(); |
99 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
110 |
> |
Runnable r = new CheckedRunnable() { |
111 |
> |
public void realRun() throws Exception { |
112 |
> |
ExecutorService e = new DirectExecutorService(); |
113 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
114 |
|
public Object run() { |
115 |
|
return TEST_STRING; |
116 |
|
}})); |
117 |
|
|
118 |
< |
Object result = future.get(); |
119 |
< |
assertSame(TEST_STRING, result); |
120 |
< |
} |
121 |
< |
finally { |
122 |
< |
try { |
123 |
< |
Policy.setPolicy(savedPolicy); |
124 |
< |
} catch (AccessControlException ok) { |
111 |
< |
return; |
112 |
< |
} |
113 |
< |
} |
118 |
> |
assertSame(TEST_STRING, future.get()); |
119 |
> |
}}; |
120 |
> |
|
121 |
> |
runWithPermissions(r, |
122 |
> |
new RuntimePermission("getClassLoader"), |
123 |
> |
new RuntimePermission("setContextClassLoader"), |
124 |
> |
new RuntimePermission("modifyThread")); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* A submitted a privileged exception action runs to completion |
128 |
> |
* A submitted privileged exception action runs to completion |
129 |
|
*/ |
130 |
|
public void testSubmitPrivilegedExceptionAction() throws Exception { |
131 |
< |
Policy savedPolicy = null; |
132 |
< |
try { |
133 |
< |
savedPolicy = Policy.getPolicy(); |
134 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
124 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
125 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
126 |
< |
Policy.setPolicy(policy); |
127 |
< |
} catch (AccessControlException ok) { |
128 |
< |
return; |
129 |
< |
} |
130 |
< |
|
131 |
< |
try { |
132 |
< |
ExecutorService e = new DirectExecutorService(); |
133 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
131 |
> |
Runnable r = new CheckedRunnable() { |
132 |
> |
public void realRun() throws Exception { |
133 |
> |
ExecutorService e = new DirectExecutorService(); |
134 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
135 |
|
public Object run() { |
136 |
|
return TEST_STRING; |
137 |
|
}})); |
138 |
|
|
139 |
< |
Object result = future.get(); |
140 |
< |
assertSame(TEST_STRING, result); |
141 |
< |
} |
142 |
< |
finally { |
142 |
< |
Policy.setPolicy(savedPolicy); |
143 |
< |
} |
139 |
> |
assertSame(TEST_STRING, future.get()); |
140 |
> |
}}; |
141 |
> |
|
142 |
> |
runWithPermissions(r); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
|
* A submitted failed privileged exception action reports exception |
147 |
|
*/ |
148 |
|
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
149 |
< |
Policy savedPolicy = null; |
150 |
< |
try { |
151 |
< |
savedPolicy = Policy.getPolicy(); |
152 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
154 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
155 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
156 |
< |
Policy.setPolicy(policy); |
157 |
< |
} catch (AccessControlException ok) { |
158 |
< |
return; |
159 |
< |
} |
160 |
< |
|
161 |
< |
try { |
162 |
< |
ExecutorService e = new DirectExecutorService(); |
163 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
149 |
> |
Runnable r = new CheckedRunnable() { |
150 |
> |
public void realRun() throws Exception { |
151 |
> |
ExecutorService e = new DirectExecutorService(); |
152 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
153 |
|
public Object run() throws Exception { |
154 |
|
throw new IndexOutOfBoundsException(); |
155 |
|
}})); |
156 |
|
|
157 |
< |
future.get(); |
158 |
< |
shouldThrow(); |
159 |
< |
} catch (ExecutionException success) { |
160 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
161 |
< |
} |
162 |
< |
finally { |
163 |
< |
Policy.setPolicy(savedPolicy); |
164 |
< |
} |
157 |
> |
try { |
158 |
> |
future.get(); |
159 |
> |
shouldThrow(); |
160 |
> |
} catch (ExecutionException success) { |
161 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
162 |
> |
}}}; |
163 |
> |
|
164 |
> |
runWithPermissions(r); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
|
* execute(null runnable) throws NPE |
169 |
|
*/ |
170 |
|
public void testExecuteNullRunnable() { |
171 |
+ |
ExecutorService e = new DirectExecutorService(); |
172 |
|
try { |
183 |
– |
ExecutorService e = new DirectExecutorService(); |
173 |
|
e.submit((Runnable) null); |
174 |
|
shouldThrow(); |
175 |
|
} catch (NullPointerException success) {} |
176 |
|
} |
177 |
|
|
189 |
– |
|
178 |
|
/** |
179 |
|
* submit(null callable) throws NPE |
180 |
|
*/ |
181 |
|
public void testSubmitNullCallable() { |
182 |
+ |
ExecutorService e = new DirectExecutorService(); |
183 |
|
try { |
195 |
– |
ExecutorService e = new DirectExecutorService(); |
184 |
|
e.submit((Callable) null); |
185 |
|
shouldThrow(); |
186 |
|
} catch (NullPointerException success) {} |
187 |
|
} |
188 |
|
|
189 |
|
/** |
190 |
< |
* submit(runnable) throws RejectedExecutionException if |
203 |
< |
* executor is saturated. |
190 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
191 |
|
*/ |
192 |
< |
public void testExecute1() { |
193 |
< |
ThreadPoolExecutor p = |
194 |
< |
new ThreadPoolExecutor(1, 1, |
195 |
< |
60, TimeUnit.SECONDS, |
196 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
197 |
< |
try { |
198 |
< |
for (int i = 0; i < 2; ++i) |
199 |
< |
p.submit(new MediumRunnable()); |
200 |
< |
for (int i = 0; i < 2; ++i) { |
201 |
< |
try { |
202 |
< |
p.submit(new MediumRunnable()); |
203 |
< |
shouldThrow(); |
204 |
< |
} catch (RejectedExecutionException success) {} |
205 |
< |
} |
206 |
< |
} finally { |
207 |
< |
joinPool(p); |
208 |
< |
} |
209 |
< |
} |
210 |
< |
|
211 |
< |
/** |
212 |
< |
* submit(callable) throws RejectedExecutionException |
213 |
< |
* if executor is saturated. |
227 |
< |
*/ |
228 |
< |
public void testExecute2() { |
229 |
< |
ThreadPoolExecutor p = |
230 |
< |
new ThreadPoolExecutor(1, 1, |
231 |
< |
60, TimeUnit.SECONDS, |
232 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
233 |
< |
try { |
234 |
< |
for (int i = 0; i < 2; ++i) |
235 |
< |
p.submit(new MediumRunnable()); |
236 |
< |
for (int i = 0; i < 2; ++i) { |
237 |
< |
try { |
238 |
< |
p.submit(new SmallCallable()); |
239 |
< |
shouldThrow(); |
240 |
< |
} catch (RejectedExecutionException success) {} |
241 |
< |
} |
192 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
193 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
194 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
195 |
> |
final ExecutorService p |
196 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
197 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
198 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
199 |
> |
public Void realCall() throws InterruptedException { |
200 |
> |
quittingTime.await(); |
201 |
> |
return null; |
202 |
> |
}}; |
203 |
> |
try { |
204 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
205 |
> |
public void realRun() throws Exception { |
206 |
> |
Future<Void> future = p.submit(awaiter); |
207 |
> |
submitted.countDown(); |
208 |
> |
future.get(); |
209 |
> |
}}); |
210 |
> |
t.start(); |
211 |
> |
submitted.await(); |
212 |
> |
t.interrupt(); |
213 |
> |
t.join(); |
214 |
|
} finally { |
215 |
+ |
quittingTime.countDown(); |
216 |
|
joinPool(p); |
217 |
|
} |
218 |
|
} |
219 |
|
|
247 |
– |
|
248 |
– |
/** |
249 |
– |
* Blocking on submit(callable) throws InterruptedException if |
250 |
– |
* caller interrupted. |
251 |
– |
*/ |
252 |
– |
public void testInterruptedSubmit() throws InterruptedException { |
253 |
– |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
254 |
– |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
255 |
– |
public void realRun() throws Exception { |
256 |
– |
p.submit(new CheckedCallable<Object>() { |
257 |
– |
public Object realCall() |
258 |
– |
throws InterruptedException { |
259 |
– |
Thread.sleep(SMALL_DELAY_MS); |
260 |
– |
return null; |
261 |
– |
}}).get(); |
262 |
– |
}}); |
263 |
– |
|
264 |
– |
t.start(); |
265 |
– |
Thread.sleep(SHORT_DELAY_MS); |
266 |
– |
t.interrupt(); |
267 |
– |
joinPool(p); |
268 |
– |
} |
269 |
– |
|
220 |
|
/** |
221 |
< |
* get of submitted callable throws InterruptedException if callable |
222 |
< |
* interrupted |
273 |
< |
*/ |
274 |
< |
public void testSubmitIE() throws InterruptedException { |
275 |
< |
final ThreadPoolExecutor p = |
276 |
< |
new ThreadPoolExecutor(1, 1, |
277 |
< |
60, TimeUnit.SECONDS, |
278 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
279 |
< |
|
280 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
281 |
< |
public void realRun() throws Exception { |
282 |
< |
p.submit(new SmallCallable()).get(); |
283 |
< |
}}); |
284 |
< |
|
285 |
< |
t.start(); |
286 |
< |
Thread.sleep(SHORT_DELAY_MS); |
287 |
< |
t.interrupt(); |
288 |
< |
t.join(); |
289 |
< |
joinPool(p); |
290 |
< |
} |
291 |
< |
|
292 |
< |
/** |
293 |
< |
* get of submit(callable) throws ExecutionException if callable |
294 |
< |
* throws exception |
221 |
> |
* get of submit(callable) throws ExecutionException if callable |
222 |
> |
* throws exception |
223 |
|
*/ |
224 |
|
public void testSubmitEE() throws InterruptedException { |
225 |
|
ThreadPoolExecutor p = |
228 |
|
new ArrayBlockingQueue<Runnable>(10)); |
229 |
|
|
230 |
|
Callable c = new Callable() { |
231 |
< |
public Object call() { return 5/0; }}; |
231 |
> |
public Object call() { throw new ArithmeticException(); }}; |
232 |
|
|
233 |
|
try { |
234 |
|
p.submit(c).get(); |
242 |
|
/** |
243 |
|
* invokeAny(null) throws NPE |
244 |
|
*/ |
245 |
< |
public void testInvokeAny1() |
318 |
< |
throws InterruptedException, ExecutionException { |
245 |
> |
public void testInvokeAny1() throws Exception { |
246 |
|
ExecutorService e = new DirectExecutorService(); |
247 |
|
try { |
248 |
|
e.invokeAny(null); |
256 |
|
/** |
257 |
|
* invokeAny(empty collection) throws IAE |
258 |
|
*/ |
259 |
< |
public void testInvokeAny2() |
333 |
< |
throws InterruptedException, ExecutionException { |
259 |
> |
public void testInvokeAny2() throws Exception { |
260 |
|
ExecutorService e = new DirectExecutorService(); |
261 |
|
try { |
262 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
271 |
|
* invokeAny(c) throws NPE if c has null elements |
272 |
|
*/ |
273 |
|
public void testInvokeAny3() throws Exception { |
348 |
– |
final CountDownLatch latch = new CountDownLatch(1); |
274 |
|
ExecutorService e = new DirectExecutorService(); |
275 |
+ |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
276 |
+ |
l.add(new Callable<Long>() { |
277 |
+ |
public Long call() { throw new ArithmeticException(); }}); |
278 |
+ |
l.add(null); |
279 |
|
try { |
351 |
– |
ArrayList<Callable<Integer>> l |
352 |
– |
= new ArrayList<Callable<Integer>>(); |
353 |
– |
l.add(new Callable<Integer>() { |
354 |
– |
public Integer call() { return 5/0; }}); |
355 |
– |
l.add(null); |
280 |
|
e.invokeAny(l); |
281 |
|
shouldThrow(); |
282 |
|
} catch (NullPointerException success) { |
283 |
|
} finally { |
360 |
– |
latch.countDown(); |
284 |
|
joinPool(e); |
285 |
|
} |
286 |
|
} |
290 |
|
*/ |
291 |
|
public void testInvokeAny4() throws InterruptedException { |
292 |
|
ExecutorService e = new DirectExecutorService(); |
293 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
294 |
+ |
l.add(new NPETask()); |
295 |
|
try { |
371 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
372 |
– |
l.add(new NPETask()); |
296 |
|
e.invokeAny(l); |
297 |
|
shouldThrow(); |
298 |
|
} catch (ExecutionException success) { |
308 |
|
public void testInvokeAny5() throws Exception { |
309 |
|
ExecutorService e = new DirectExecutorService(); |
310 |
|
try { |
311 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
311 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
312 |
|
l.add(new StringTask()); |
313 |
|
l.add(new StringTask()); |
314 |
|
String result = e.invokeAny(l); |
350 |
|
*/ |
351 |
|
public void testInvokeAll3() throws InterruptedException { |
352 |
|
ExecutorService e = new DirectExecutorService(); |
353 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
354 |
+ |
l.add(new StringTask()); |
355 |
+ |
l.add(null); |
356 |
|
try { |
431 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
432 |
– |
l.add(new StringTask()); |
433 |
– |
l.add(null); |
357 |
|
e.invokeAll(l); |
358 |
|
shouldThrow(); |
359 |
|
} catch (NullPointerException success) { |
368 |
|
public void testInvokeAll4() throws Exception { |
369 |
|
ExecutorService e = new DirectExecutorService(); |
370 |
|
try { |
371 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
371 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
372 |
|
l.add(new NPETask()); |
373 |
< |
List<Future<String>> result = e.invokeAll(l); |
374 |
< |
assertEquals(1, result.size()); |
375 |
< |
for (Future<String> future : result) { |
376 |
< |
try { |
377 |
< |
future.get(); |
378 |
< |
shouldThrow(); |
379 |
< |
} catch (ExecutionException success) { |
457 |
< |
Throwable cause = success.getCause(); |
458 |
< |
assertTrue(cause instanceof NullPointerException); |
459 |
< |
} |
373 |
> |
List<Future<String>> futures = e.invokeAll(l); |
374 |
> |
assertEquals(1, futures.size()); |
375 |
> |
try { |
376 |
> |
futures.get(0).get(); |
377 |
> |
shouldThrow(); |
378 |
> |
} catch (ExecutionException success) { |
379 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
380 |
|
} |
381 |
|
} finally { |
382 |
|
joinPool(e); |
389 |
|
public void testInvokeAll5() throws Exception { |
390 |
|
ExecutorService e = new DirectExecutorService(); |
391 |
|
try { |
392 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
392 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
393 |
|
l.add(new StringTask()); |
394 |
|
l.add(new StringTask()); |
395 |
< |
List<Future<String>> result = e.invokeAll(l); |
396 |
< |
assertEquals(2, result.size()); |
397 |
< |
for (Future<String> future : result) |
395 |
> |
List<Future<String>> futures = e.invokeAll(l); |
396 |
> |
assertEquals(2, futures.size()); |
397 |
> |
for (Future<String> future : futures) |
398 |
|
assertSame(TEST_STRING, future.get()); |
399 |
|
} finally { |
400 |
|
joinPool(e); |
401 |
|
} |
402 |
|
} |
403 |
|
|
484 |
– |
|
404 |
|
/** |
405 |
|
* timed invokeAny(null) throws NPE |
406 |
|
*/ |
420 |
|
*/ |
421 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
422 |
|
ExecutorService e = new DirectExecutorService(); |
423 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
424 |
+ |
l.add(new StringTask()); |
425 |
|
try { |
505 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
506 |
– |
l.add(new StringTask()); |
426 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
427 |
|
shouldThrow(); |
428 |
|
} catch (NullPointerException success) { |
449 |
|
* timed invokeAny(c) throws NPE if c has null elements |
450 |
|
*/ |
451 |
|
public void testTimedInvokeAny3() throws Exception { |
533 |
– |
final CountDownLatch latch = new CountDownLatch(1); |
452 |
|
ExecutorService e = new DirectExecutorService(); |
453 |
+ |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
454 |
+ |
l.add(new Callable<Long>() { |
455 |
+ |
public Long call() { throw new ArithmeticException(); }}); |
456 |
+ |
l.add(null); |
457 |
|
try { |
536 |
– |
ArrayList<Callable<Integer>> l |
537 |
– |
= new ArrayList<Callable<Integer>>(); |
538 |
– |
l.add(new Callable<Integer>() { |
539 |
– |
public Integer call() { return 5/0; }}); |
540 |
– |
l.add(null); |
458 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
459 |
|
shouldThrow(); |
460 |
|
} catch (NullPointerException success) { |
461 |
|
} finally { |
545 |
– |
latch.countDown(); |
462 |
|
joinPool(e); |
463 |
|
} |
464 |
|
} |
468 |
|
*/ |
469 |
|
public void testTimedInvokeAny4() throws Exception { |
470 |
|
ExecutorService e = new DirectExecutorService(); |
471 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
472 |
+ |
l.add(new NPETask()); |
473 |
|
try { |
556 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
557 |
– |
l.add(new NPETask()); |
474 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
475 |
|
shouldThrow(); |
476 |
|
} catch (ExecutionException success) { |
486 |
|
public void testTimedInvokeAny5() throws Exception { |
487 |
|
ExecutorService e = new DirectExecutorService(); |
488 |
|
try { |
489 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
489 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
490 |
|
l.add(new StringTask()); |
491 |
|
l.add(new StringTask()); |
492 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
515 |
|
*/ |
516 |
|
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
517 |
|
ExecutorService e = new DirectExecutorService(); |
518 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
519 |
+ |
l.add(new StringTask()); |
520 |
|
try { |
603 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
604 |
– |
l.add(new StringTask()); |
521 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
522 |
|
shouldThrow(); |
523 |
|
} catch (NullPointerException success) { |
544 |
|
*/ |
545 |
|
public void testTimedInvokeAll3() throws InterruptedException { |
546 |
|
ExecutorService e = new DirectExecutorService(); |
547 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
548 |
+ |
l.add(new StringTask()); |
549 |
+ |
l.add(null); |
550 |
|
try { |
632 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
633 |
– |
l.add(new StringTask()); |
634 |
– |
l.add(null); |
551 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
552 |
|
shouldThrow(); |
553 |
|
} catch (NullPointerException success) { |
562 |
|
public void testTimedInvokeAll4() throws Exception { |
563 |
|
ExecutorService e = new DirectExecutorService(); |
564 |
|
try { |
565 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
565 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
566 |
|
l.add(new NPETask()); |
567 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
568 |
< |
assertEquals(1, result.size()); |
569 |
< |
for (Future<String> future : result) { |
570 |
< |
try { |
571 |
< |
future.get(); |
572 |
< |
} catch (ExecutionException success) { |
573 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
574 |
< |
} |
567 |
> |
List<Future<String>> futures = |
568 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
569 |
> |
assertEquals(1, futures.size()); |
570 |
> |
try { |
571 |
> |
futures.get(0).get(); |
572 |
> |
shouldThrow(); |
573 |
> |
} catch (ExecutionException success) { |
574 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
575 |
|
} |
576 |
|
} finally { |
577 |
|
joinPool(e); |
584 |
|
public void testTimedInvokeAll5() throws Exception { |
585 |
|
ExecutorService e = new DirectExecutorService(); |
586 |
|
try { |
587 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
587 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
588 |
|
l.add(new StringTask()); |
589 |
|
l.add(new StringTask()); |
590 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
591 |
< |
assertEquals(2, result.size()); |
592 |
< |
for (Future<String> future : result) |
590 |
> |
List<Future<String>> futures = |
591 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
592 |
> |
assertEquals(2, futures.size()); |
593 |
> |
for (Future<String> future : futures) |
594 |
|
assertSame(TEST_STRING, future.get()); |
595 |
|
} finally { |
596 |
|
joinPool(e); |
600 |
|
/** |
601 |
|
* timed invokeAll cancels tasks not completed by timeout |
602 |
|
*/ |
603 |
< |
public void testTimedInvokeAll6() throws InterruptedException { |
603 |
> |
public void testTimedInvokeAll6() throws Exception { |
604 |
|
ExecutorService e = new DirectExecutorService(); |
605 |
|
try { |
606 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
607 |
< |
l.add(new StringTask()); |
608 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
609 |
< |
l.add(new StringTask()); |
610 |
< |
List<Future<String>> result = e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
611 |
< |
assertEquals(3, result.size()); |
612 |
< |
Iterator<Future<String>> it = result.iterator(); |
613 |
< |
Future<String> f1 = it.next(); |
614 |
< |
Future<String> f2 = it.next(); |
615 |
< |
Future<String> f3 = it.next(); |
616 |
< |
assertTrue(f1.isDone()); |
617 |
< |
assertFalse(f1.isCancelled()); |
618 |
< |
assertTrue(f2.isDone()); |
619 |
< |
assertTrue(f3.isDone()); |
620 |
< |
assertTrue(f3.isCancelled()); |
606 |
> |
long timeout = timeoutMillis(); |
607 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
608 |
> |
tasks.add(new StringTask("0")); |
609 |
> |
tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout), |
610 |
> |
TEST_STRING)); |
611 |
> |
tasks.add(new StringTask("2")); |
612 |
> |
long startTime = System.nanoTime(); |
613 |
> |
List<Future<String>> futures = |
614 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
615 |
> |
assertEquals(tasks.size(), futures.size()); |
616 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
617 |
> |
for (Future future : futures) |
618 |
> |
assertTrue(future.isDone()); |
619 |
> |
assertEquals("0", futures.get(0).get()); |
620 |
> |
assertEquals(TEST_STRING, futures.get(1).get()); |
621 |
> |
assertTrue(futures.get(2).isCancelled()); |
622 |
|
} finally { |
623 |
|
joinPool(e); |
624 |
|
} |