1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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/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.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 |
< |
public class AbstractExecutorServiceTest extends JSR166TestCase{ |
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(ExecutorsTest.class); |
37 |
> |
return new TestSuite(AbstractExecutorServiceTest.class); |
38 |
|
} |
39 |
|
|
40 |
< |
/** |
40 |
> |
/** |
41 |
|
* A no-frills implementation of AbstractExecutorService, designed |
42 |
< |
* to test the submit/invoke methods only. |
42 |
> |
* to test the submit methods only. |
43 |
|
*/ |
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 |
|
|
59 |
< |
private static final String TEST_STRING = "a test string"; |
60 |
< |
|
61 |
< |
private static class StringTask implements Callable<String> { |
62 |
< |
public String call() { return TEST_STRING; } |
59 |
> |
/** |
60 |
> |
* execute(runnable) runs it to completion |
61 |
> |
*/ |
62 |
> |
public void testExecuteRunnable() throws Exception { |
63 |
> |
ExecutorService e = new DirectExecutorService(); |
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 |
|
|
76 |
|
/** |
77 |
< |
* execute of runnable runs it to completion |
77 |
> |
* Completed submit(callable) returns result |
78 |
|
*/ |
79 |
< |
public void testExecuteRunnable() { |
80 |
< |
try { |
81 |
< |
ExecutorService e = new DirectExecutorService(); |
82 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
83 |
< |
assertFalse(task.done); |
51 |
< |
Future<?> future = e.submit(task); |
52 |
< |
future.get(); |
53 |
< |
assertTrue(task.done); |
54 |
< |
} |
55 |
< |
catch (ExecutionException ex) { |
56 |
< |
unexpectedException(); |
57 |
< |
} |
58 |
< |
catch (InterruptedException ex) { |
59 |
< |
unexpectedException(); |
60 |
< |
} |
79 |
> |
public void testSubmitCallable() throws Exception { |
80 |
> |
ExecutorService e = new DirectExecutorService(); |
81 |
> |
Future<String> future = e.submit(new StringTask()); |
82 |
> |
String result = future.get(); |
83 |
> |
assertSame(TEST_STRING, result); |
84 |
|
} |
85 |
|
|
63 |
– |
|
86 |
|
/** |
87 |
< |
* execute of a callable runs it to completion |
87 |
> |
* Completed submit(runnable) returns successfully |
88 |
|
*/ |
89 |
< |
public void testExecuteCallable() { |
90 |
< |
try { |
91 |
< |
ExecutorService e = new DirectExecutorService(); |
92 |
< |
Future<String> future = e.submit(new StringTask()); |
93 |
< |
String result = future.get(); |
72 |
< |
assertSame(TEST_STRING, result); |
73 |
< |
} |
74 |
< |
catch (ExecutionException ex) { |
75 |
< |
unexpectedException(); |
76 |
< |
} |
77 |
< |
catch (InterruptedException ex) { |
78 |
< |
unexpectedException(); |
79 |
< |
} |
89 |
> |
public void testSubmitRunnable() throws Exception { |
90 |
> |
ExecutorService e = new DirectExecutorService(); |
91 |
> |
Future<?> future = e.submit(new NoOpRunnable()); |
92 |
> |
future.get(); |
93 |
> |
assertTrue(future.isDone()); |
94 |
|
} |
95 |
|
|
96 |
+ |
/** |
97 |
+ |
* Completed submit(runnable, result) returns result |
98 |
+ |
*/ |
99 |
+ |
public void testSubmitRunnable2() throws Exception { |
100 |
+ |
ExecutorService e = new DirectExecutorService(); |
101 |
+ |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
102 |
+ |
String result = future.get(); |
103 |
+ |
assertSame(TEST_STRING, result); |
104 |
+ |
} |
105 |
|
|
106 |
|
/** |
107 |
< |
* execute of a privileged action runs it to completion |
107 |
> |
* A submitted privileged action runs to completion |
108 |
|
*/ |
109 |
< |
public void testExecutePrivilegedAction() { |
110 |
< |
Policy savedPolicy = Policy.getPolicy(); |
111 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
112 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
113 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
91 |
< |
Policy.setPolicy(policy); |
92 |
< |
try { |
93 |
< |
ExecutorService e = new DirectExecutorService(); |
94 |
< |
Future future = e.submit(new PrivilegedAction() { |
109 |
> |
public void testSubmitPrivilegedAction() throws Exception { |
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 |
< |
}}); |
116 |
> |
}})); |
117 |
|
|
118 |
< |
Object result = future.get(); |
119 |
< |
assertSame(TEST_STRING, result); |
120 |
< |
} |
121 |
< |
catch (ExecutionException ex) { |
122 |
< |
unexpectedException(); |
123 |
< |
} |
124 |
< |
catch (InterruptedException ex) { |
106 |
< |
unexpectedException(); |
107 |
< |
} |
108 |
< |
finally { |
109 |
< |
Policy.setPolicy(savedPolicy); |
110 |
< |
} |
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 |
< |
* execute of a privileged exception action runs it to completion |
128 |
> |
* A submitted privileged exception action runs to completion |
129 |
|
*/ |
130 |
< |
public void testExecutePrivilegedExceptionAction() { |
131 |
< |
Policy savedPolicy = Policy.getPolicy(); |
132 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
133 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
134 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
121 |
< |
Policy.setPolicy(policy); |
122 |
< |
try { |
123 |
< |
ExecutorService e = new DirectExecutorService(); |
124 |
< |
Future future = e.submit(new PrivilegedExceptionAction() { |
130 |
> |
public void testSubmitPrivilegedExceptionAction() throws Exception { |
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 |
< |
}}); |
137 |
> |
}})); |
138 |
|
|
139 |
< |
Object result = future.get(); |
140 |
< |
assertSame(TEST_STRING, result); |
141 |
< |
} |
142 |
< |
catch (ExecutionException ex) { |
133 |
< |
unexpectedException(); |
134 |
< |
} |
135 |
< |
catch (InterruptedException ex) { |
136 |
< |
unexpectedException(); |
137 |
< |
} |
138 |
< |
finally { |
139 |
< |
Policy.setPolicy(savedPolicy); |
140 |
< |
} |
139 |
> |
assertSame(TEST_STRING, future.get()); |
140 |
> |
}}; |
141 |
> |
|
142 |
> |
runWithPermissions(r); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
< |
* execute of a failed privileged exception action reports exception |
146 |
> |
* A submitted failed privileged exception action reports exception |
147 |
|
*/ |
148 |
< |
public void testExecuteFailedPrivilegedExceptionAction() { |
149 |
< |
Policy savedPolicy = Policy.getPolicy(); |
150 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
151 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
152 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
151 |
< |
Policy.setPolicy(policy); |
152 |
< |
try { |
153 |
< |
ExecutorService e = new DirectExecutorService(); |
154 |
< |
Future future = e.submit(new PrivilegedExceptionAction() { |
148 |
> |
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
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 |
< |
}}); |
155 |
> |
}})); |
156 |
|
|
157 |
< |
Object result = future.get(); |
158 |
< |
shouldThrow(); |
159 |
< |
} |
160 |
< |
catch (ExecutionException success) { |
161 |
< |
} |
162 |
< |
catch (InterruptedException ex) { |
163 |
< |
unexpectedException(); |
164 |
< |
} |
167 |
< |
finally { |
168 |
< |
Policy.setPolicy(savedPolicy); |
169 |
< |
} |
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 |
< |
* invoke of a callable runs it to completion |
168 |
> |
* execute(null runnable) throws NPE |
169 |
|
*/ |
170 |
< |
public void testInvokeCallable() { |
170 |
> |
public void testExecuteNullRunnable() { |
171 |
> |
ExecutorService e = new DirectExecutorService(); |
172 |
|
try { |
173 |
< |
ExecutorService e = new DirectExecutorService(); |
174 |
< |
String result = e.invoke(new StringTask()); |
175 |
< |
|
180 |
< |
assertSame(TEST_STRING, result); |
181 |
< |
} |
182 |
< |
catch (ExecutionException ex) { |
183 |
< |
unexpectedException(); |
184 |
< |
} |
185 |
< |
catch (InterruptedException ex) { |
186 |
< |
unexpectedException(); |
187 |
< |
} |
173 |
> |
e.submit((Runnable) null); |
174 |
> |
shouldThrow(); |
175 |
> |
} catch (NullPointerException success) {} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* execute with a null runnable throws NPE |
179 |
> |
* submit(null callable) throws NPE |
180 |
|
*/ |
181 |
< |
public void testExecuteNullRunnable() { |
181 |
> |
public void testSubmitNullCallable() { |
182 |
> |
ExecutorService e = new DirectExecutorService(); |
183 |
|
try { |
184 |
< |
ExecutorService e = new DirectExecutorService(); |
196 |
< |
TrackedShortRunnable task = null; |
197 |
< |
Future<?> future = e.submit(task); |
184 |
> |
e.submit((Callable) null); |
185 |
|
shouldThrow(); |
186 |
< |
} |
187 |
< |
catch (NullPointerException success) { |
188 |
< |
} |
189 |
< |
catch (Exception ex) { |
190 |
< |
unexpectedException(); |
186 |
> |
} catch (NullPointerException success) {} |
187 |
> |
} |
188 |
> |
|
189 |
> |
/** |
190 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
191 |
> |
*/ |
192 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
193 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
194 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
195 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
196 |
> |
public Void realCall() throws InterruptedException { |
197 |
> |
assertTrue(quittingTime.await(2*LONG_DELAY_MS, MILLISECONDS)); |
198 |
> |
return null; |
199 |
> |
}}; |
200 |
> |
final ExecutorService p |
201 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
202 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
203 |
> |
try (PoolCleaner cleaner = cleaner(p, quittingTime)) { |
204 |
> |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
205 |
> |
public void realRun() throws Exception { |
206 |
> |
Future<Void> future = p.submit(awaiter); |
207 |
> |
submitted.countDown(); |
208 |
> |
future.get(); |
209 |
> |
}}); |
210 |
> |
|
211 |
> |
await(submitted); |
212 |
> |
t.interrupt(); |
213 |
> |
awaitTermination(t); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
+ |
/** |
218 |
+ |
* get of submit(callable) throws ExecutionException if callable |
219 |
+ |
* throws exception |
220 |
+ |
*/ |
221 |
+ |
public void testSubmitEE() throws InterruptedException { |
222 |
+ |
final ThreadPoolExecutor p = |
223 |
+ |
new ThreadPoolExecutor(1, 1, |
224 |
+ |
60, TimeUnit.SECONDS, |
225 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
226 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
227 |
+ |
Callable c = new Callable() { |
228 |
+ |
public Object call() { throw new ArithmeticException(); }}; |
229 |
+ |
try { |
230 |
+ |
p.submit(c).get(); |
231 |
+ |
shouldThrow(); |
232 |
+ |
} catch (ExecutionException success) { |
233 |
+ |
assertTrue(success.getCause() instanceof ArithmeticException); |
234 |
+ |
} |
235 |
+ |
} |
236 |
+ |
} |
237 |
|
|
238 |
|
/** |
239 |
< |
* execute of a null callable throws NPE |
239 |
> |
* invokeAny(null) throws NPE |
240 |
|
*/ |
241 |
< |
public void testExecuteNullCallable() { |
242 |
< |
try { |
243 |
< |
ExecutorService e = new DirectExecutorService(); |
244 |
< |
StringTask t = null; |
245 |
< |
Future<String> future = e.submit(t); |
246 |
< |
shouldThrow(); |
241 |
> |
public void testInvokeAny1() throws Exception { |
242 |
> |
final ExecutorService e = new DirectExecutorService(); |
243 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
244 |
> |
try { |
245 |
> |
e.invokeAny(null); |
246 |
> |
shouldThrow(); |
247 |
> |
} catch (NullPointerException success) {} |
248 |
|
} |
249 |
< |
catch (NullPointerException success) { |
249 |
> |
} |
250 |
> |
|
251 |
> |
/** |
252 |
> |
* invokeAny(empty collection) throws IAE |
253 |
> |
*/ |
254 |
> |
public void testInvokeAny2() throws Exception { |
255 |
> |
final ExecutorService e = new DirectExecutorService(); |
256 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
257 |
> |
try { |
258 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
259 |
> |
shouldThrow(); |
260 |
> |
} catch (IllegalArgumentException success) {} |
261 |
|
} |
262 |
< |
catch (Exception ex) { |
263 |
< |
unexpectedException(); |
262 |
> |
} |
263 |
> |
|
264 |
> |
/** |
265 |
> |
* invokeAny(c) throws NPE if c has null elements |
266 |
> |
*/ |
267 |
> |
public void testInvokeAny3() throws Exception { |
268 |
> |
final ExecutorService e = new DirectExecutorService(); |
269 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
270 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
271 |
> |
l.add(new Callable<Long>() { |
272 |
> |
public Long call() { throw new ArithmeticException(); }}); |
273 |
> |
l.add(null); |
274 |
> |
try { |
275 |
> |
e.invokeAny(l); |
276 |
> |
shouldThrow(); |
277 |
> |
} catch (NullPointerException success) {} |
278 |
|
} |
279 |
|
} |
280 |
|
|
281 |
|
/** |
282 |
< |
* invoke of a null callable throws NPE |
282 |
> |
* invokeAny(c) throws ExecutionException if no task in c completes |
283 |
|
*/ |
284 |
< |
public void testInvokeNullCallable() { |
285 |
< |
try { |
286 |
< |
ExecutorService e = new DirectExecutorService(); |
287 |
< |
StringTask t = null; |
288 |
< |
String result = e.invoke(t); |
289 |
< |
shouldThrow(); |
284 |
> |
public void testInvokeAny4() throws InterruptedException { |
285 |
> |
final ExecutorService e = new DirectExecutorService(); |
286 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
287 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
288 |
> |
l.add(new NPETask()); |
289 |
> |
try { |
290 |
> |
e.invokeAny(l); |
291 |
> |
shouldThrow(); |
292 |
> |
} catch (ExecutionException success) { |
293 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
294 |
> |
} |
295 |
|
} |
296 |
< |
catch (NullPointerException success) { |
296 |
> |
} |
297 |
> |
|
298 |
> |
/** |
299 |
> |
* invokeAny(c) returns result of some task in c if at least one completes |
300 |
> |
*/ |
301 |
> |
public void testInvokeAny5() throws Exception { |
302 |
> |
final ExecutorService e = new DirectExecutorService(); |
303 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
304 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
305 |
> |
l.add(new StringTask()); |
306 |
> |
l.add(new StringTask()); |
307 |
> |
String result = e.invokeAny(l); |
308 |
> |
assertSame(TEST_STRING, result); |
309 |
|
} |
310 |
< |
catch (Exception ex) { |
311 |
< |
unexpectedException(); |
310 |
> |
} |
311 |
> |
|
312 |
> |
/** |
313 |
> |
* invokeAll(null) throws NPE |
314 |
> |
*/ |
315 |
> |
public void testInvokeAll1() throws InterruptedException { |
316 |
> |
final ExecutorService e = new DirectExecutorService(); |
317 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
318 |
> |
try { |
319 |
> |
e.invokeAll(null); |
320 |
> |
shouldThrow(); |
321 |
> |
} catch (NullPointerException success) {} |
322 |
|
} |
323 |
|
} |
324 |
|
|
325 |
|
/** |
326 |
< |
* execute(Executor, Runnable) throws RejectedExecutionException |
244 |
< |
* if saturated. |
326 |
> |
* invokeAll(empty collection) returns empty collection |
327 |
|
*/ |
328 |
< |
public void testExecute1() { |
329 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
330 |
< |
try { |
328 |
> |
public void testInvokeAll2() throws InterruptedException { |
329 |
> |
final ExecutorService e = new DirectExecutorService(); |
330 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
331 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
332 |
> |
assertTrue(r.isEmpty()); |
333 |
> |
} |
334 |
> |
} |
335 |
|
|
336 |
< |
for(int i = 0; i < 5; ++i){ |
337 |
< |
p.submit(new MediumRunnable()); |
338 |
< |
} |
339 |
< |
shouldThrow(); |
340 |
< |
} catch(RejectedExecutionException success){} |
341 |
< |
joinPool(p); |
336 |
> |
/** |
337 |
> |
* invokeAll(c) throws NPE if c has null elements |
338 |
> |
*/ |
339 |
> |
public void testInvokeAll3() throws InterruptedException { |
340 |
> |
final ExecutorService e = new DirectExecutorService(); |
341 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
342 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
343 |
> |
l.add(new StringTask()); |
344 |
> |
l.add(null); |
345 |
> |
try { |
346 |
> |
e.invokeAll(l); |
347 |
> |
shouldThrow(); |
348 |
> |
} catch (NullPointerException success) {} |
349 |
> |
} |
350 |
|
} |
351 |
|
|
352 |
|
/** |
353 |
< |
* execute(Executor, Callable)throws RejectedExecutionException |
260 |
< |
* if saturated. |
353 |
> |
* get of returned element of invokeAll(c) throws exception on failed task |
354 |
|
*/ |
355 |
< |
public void testExecute2() { |
356 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
357 |
< |
try { |
358 |
< |
for(int i = 0; i < 5; ++i) { |
359 |
< |
p.submit(new SmallCallable()); |
355 |
> |
public void testInvokeAll4() throws Exception { |
356 |
> |
final ExecutorService e = new DirectExecutorService(); |
357 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
358 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
359 |
> |
l.add(new NPETask()); |
360 |
> |
List<Future<String>> futures = e.invokeAll(l); |
361 |
> |
assertEquals(1, futures.size()); |
362 |
> |
try { |
363 |
> |
futures.get(0).get(); |
364 |
> |
shouldThrow(); |
365 |
> |
} catch (ExecutionException success) { |
366 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
367 |
|
} |
368 |
< |
shouldThrow(); |
269 |
< |
} catch(RejectedExecutionException e){} |
270 |
< |
joinPool(p); |
368 |
> |
} |
369 |
|
} |
370 |
|
|
371 |
+ |
/** |
372 |
+ |
* invokeAll(c) returns results of all completed tasks in c |
373 |
+ |
*/ |
374 |
+ |
public void testInvokeAll5() throws Exception { |
375 |
+ |
final ExecutorService e = new DirectExecutorService(); |
376 |
+ |
try (PoolCleaner cleaner = cleaner(e)) { |
377 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
378 |
+ |
l.add(new StringTask()); |
379 |
+ |
l.add(new StringTask()); |
380 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
381 |
+ |
assertEquals(2, futures.size()); |
382 |
+ |
for (Future<String> future : futures) |
383 |
+ |
assertSame(TEST_STRING, future.get()); |
384 |
+ |
} |
385 |
+ |
} |
386 |
|
|
387 |
|
/** |
388 |
< |
* invoke(Executor, Callable) throws InterruptedException if |
276 |
< |
* caller interrupted. |
388 |
> |
* timed invokeAny(null) throws NPE |
389 |
|
*/ |
390 |
< |
public void testInterruptedInvoke() { |
391 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
392 |
< |
Thread t = new Thread(new Runnable() { |
393 |
< |
public void run() { |
394 |
< |
try { |
395 |
< |
p.invoke(new Callable<Object>() { |
396 |
< |
public Object call() { |
397 |
< |
try { |
398 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
287 |
< |
shouldThrow(); |
288 |
< |
} catch(InterruptedException e){ |
289 |
< |
} |
290 |
< |
return null; |
291 |
< |
} |
292 |
< |
}); |
293 |
< |
} catch(InterruptedException success){ |
294 |
< |
} catch(Exception e) { |
295 |
< |
unexpectedException(); |
296 |
< |
} |
390 |
> |
public void testTimedInvokeAny1() throws Exception { |
391 |
> |
final ExecutorService e = new DirectExecutorService(); |
392 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
393 |
> |
try { |
394 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
395 |
> |
shouldThrow(); |
396 |
> |
} catch (NullPointerException success) {} |
397 |
> |
} |
398 |
> |
} |
399 |
|
|
400 |
< |
} |
401 |
< |
}); |
402 |
< |
try { |
403 |
< |
t.start(); |
404 |
< |
Thread.sleep(SHORT_DELAY_MS); |
405 |
< |
t.interrupt(); |
406 |
< |
} catch(Exception e){ |
407 |
< |
unexpectedException(); |
400 |
> |
/** |
401 |
> |
* timed invokeAny(null time unit) throws NPE |
402 |
> |
*/ |
403 |
> |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
404 |
> |
final ExecutorService e = new DirectExecutorService(); |
405 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
406 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
407 |
> |
l.add(new StringTask()); |
408 |
> |
try { |
409 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
410 |
> |
shouldThrow(); |
411 |
> |
} catch (NullPointerException success) {} |
412 |
|
} |
307 |
– |
joinPool(p); |
413 |
|
} |
414 |
|
|
415 |
|
/** |
416 |
< |
* invoke(Executor, Callable) throws InterruptedException if |
312 |
< |
* callable throws exception |
416 |
> |
* timed invokeAny(empty collection) throws IAE |
417 |
|
*/ |
418 |
< |
public void testInvoke5() { |
419 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
418 |
> |
public void testTimedInvokeAny2() throws Exception { |
419 |
> |
final ExecutorService e = new DirectExecutorService(); |
420 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
421 |
> |
try { |
422 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
423 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
424 |
> |
shouldThrow(); |
425 |
> |
} catch (IllegalArgumentException success) {} |
426 |
> |
} |
427 |
> |
} |
428 |
|
|
429 |
< |
final Callable c = new Callable() { |
430 |
< |
public Object call() { |
431 |
< |
try { |
432 |
< |
p.invoke(new SmallCallable()); |
433 |
< |
shouldThrow(); |
434 |
< |
} catch(InterruptedException e){} |
435 |
< |
catch(RejectedExecutionException e2){} |
436 |
< |
catch(ExecutionException e3){} |
437 |
< |
return Boolean.TRUE; |
438 |
< |
} |
439 |
< |
}; |
429 |
> |
/** |
430 |
> |
* timed invokeAny(c) throws NPE if c has null elements |
431 |
> |
*/ |
432 |
> |
public void testTimedInvokeAny3() throws Exception { |
433 |
> |
final ExecutorService e = new DirectExecutorService(); |
434 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
435 |
> |
List<Callable<Long>> l = new ArrayList<Callable<Long>>(); |
436 |
> |
l.add(new Callable<Long>() { |
437 |
> |
public Long call() { throw new ArithmeticException(); }}); |
438 |
> |
l.add(null); |
439 |
> |
try { |
440 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
441 |
> |
shouldThrow(); |
442 |
> |
} catch (NullPointerException success) {} |
443 |
> |
} |
444 |
> |
} |
445 |
|
|
446 |
+ |
/** |
447 |
+ |
* timed invokeAny(c) throws ExecutionException if no task completes |
448 |
+ |
*/ |
449 |
+ |
public void testTimedInvokeAny4() throws Exception { |
450 |
+ |
final ExecutorService e = new DirectExecutorService(); |
451 |
+ |
try (PoolCleaner cleaner = cleaner(e)) { |
452 |
+ |
long startTime = System.nanoTime(); |
453 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
454 |
+ |
l.add(new NPETask()); |
455 |
+ |
try { |
456 |
+ |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
457 |
+ |
shouldThrow(); |
458 |
+ |
} catch (ExecutionException success) { |
459 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
460 |
+ |
} |
461 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
462 |
+ |
} |
463 |
+ |
} |
464 |
|
|
465 |
+ |
/** |
466 |
+ |
* timed invokeAny(c) returns result of some task in c |
467 |
+ |
*/ |
468 |
+ |
public void testTimedInvokeAny5() throws Exception { |
469 |
+ |
final ExecutorService e = new DirectExecutorService(); |
470 |
+ |
try (PoolCleaner cleaner = cleaner(e)) { |
471 |
+ |
long startTime = System.nanoTime(); |
472 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
473 |
+ |
l.add(new StringTask()); |
474 |
+ |
l.add(new StringTask()); |
475 |
+ |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
476 |
+ |
assertSame(TEST_STRING, result); |
477 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
478 |
+ |
} |
479 |
+ |
} |
480 |
|
|
481 |
< |
Thread t = new Thread(new Runnable() { |
482 |
< |
public void run() { |
483 |
< |
try { |
484 |
< |
c.call(); |
485 |
< |
} catch(Exception e){} |
486 |
< |
} |
487 |
< |
}); |
488 |
< |
try { |
489 |
< |
t.start(); |
490 |
< |
Thread.sleep(SHORT_DELAY_MS); |
341 |
< |
t.interrupt(); |
342 |
< |
t.join(); |
343 |
< |
} catch(InterruptedException e){ |
344 |
< |
unexpectedException(); |
481 |
> |
/** |
482 |
> |
* timed invokeAll(null) throws NPE |
483 |
> |
*/ |
484 |
> |
public void testTimedInvokeAll1() throws InterruptedException { |
485 |
> |
final ExecutorService e = new DirectExecutorService(); |
486 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
487 |
> |
try { |
488 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
489 |
> |
shouldThrow(); |
490 |
> |
} catch (NullPointerException success) {} |
491 |
|
} |
492 |
+ |
} |
493 |
|
|
494 |
< |
joinPool(p); |
494 |
> |
/** |
495 |
> |
* timed invokeAll(null time unit) throws NPE |
496 |
> |
*/ |
497 |
> |
public void testTimedInvokeAllNullTimeUnit() throws InterruptedException { |
498 |
> |
final ExecutorService e = new DirectExecutorService(); |
499 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
500 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
501 |
> |
l.add(new StringTask()); |
502 |
> |
try { |
503 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
504 |
> |
shouldThrow(); |
505 |
> |
} catch (NullPointerException success) {} |
506 |
> |
} |
507 |
|
} |
508 |
|
|
509 |
|
/** |
510 |
< |
* invoke(Executor, Callable) will throw ExecutionException |
352 |
< |
* if callable throws exception |
510 |
> |
* timed invokeAll(empty collection) returns empty collection |
511 |
|
*/ |
512 |
< |
public void testInvoke6() { |
513 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
512 |
> |
public void testTimedInvokeAll2() throws InterruptedException { |
513 |
> |
final ExecutorService e = new DirectExecutorService(); |
514 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
515 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
516 |
> |
assertTrue(r.isEmpty()); |
517 |
> |
} |
518 |
> |
} |
519 |
|
|
520 |
< |
try { |
521 |
< |
Callable c = new Callable() { |
522 |
< |
public Object call() { |
523 |
< |
int i = 5/0; |
524 |
< |
return Boolean.TRUE; |
525 |
< |
} |
526 |
< |
}; |
520 |
> |
/** |
521 |
> |
* timed invokeAll(c) throws NPE if c has null elements |
522 |
> |
*/ |
523 |
> |
public void testTimedInvokeAll3() throws InterruptedException { |
524 |
> |
final ExecutorService e = new DirectExecutorService(); |
525 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
526 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
527 |
> |
l.add(new StringTask()); |
528 |
> |
l.add(null); |
529 |
> |
try { |
530 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
531 |
> |
shouldThrow(); |
532 |
> |
} catch (NullPointerException success) {} |
533 |
> |
} |
534 |
> |
} |
535 |
|
|
536 |
< |
for(int i =0; i < 5; i++){ |
537 |
< |
p.invoke(c); |
536 |
> |
/** |
537 |
> |
* get of returned element of invokeAll(c) throws exception on failed task |
538 |
> |
*/ |
539 |
> |
public void testTimedInvokeAll4() throws Exception { |
540 |
> |
final ExecutorService e = new DirectExecutorService(); |
541 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
542 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
543 |
> |
l.add(new NPETask()); |
544 |
> |
List<Future<String>> futures = |
545 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
546 |
> |
assertEquals(1, futures.size()); |
547 |
> |
try { |
548 |
> |
futures.get(0).get(); |
549 |
> |
shouldThrow(); |
550 |
> |
} catch (ExecutionException success) { |
551 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
552 |
|
} |
553 |
+ |
} |
554 |
+ |
} |
555 |
|
|
556 |
< |
shouldThrow(); |
556 |
> |
/** |
557 |
> |
* timed invokeAll(c) returns results of all completed tasks in c |
558 |
> |
*/ |
559 |
> |
public void testTimedInvokeAll5() throws Exception { |
560 |
> |
final ExecutorService e = new DirectExecutorService(); |
561 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
562 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
563 |
> |
l.add(new StringTask()); |
564 |
> |
l.add(new StringTask()); |
565 |
> |
List<Future<String>> futures = |
566 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
567 |
> |
assertEquals(2, futures.size()); |
568 |
> |
for (Future<String> future : futures) |
569 |
> |
assertSame(TEST_STRING, future.get()); |
570 |
|
} |
571 |
< |
catch(ExecutionException success){ |
572 |
< |
} catch(Exception e) { |
573 |
< |
unexpectedException(); |
571 |
> |
} |
572 |
> |
|
573 |
> |
/** |
574 |
> |
* timed invokeAll cancels tasks not completed by timeout |
575 |
> |
*/ |
576 |
> |
public void testTimedInvokeAll6() throws Exception { |
577 |
> |
final ExecutorService e = new DirectExecutorService(); |
578 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
579 |
> |
for (long timeout = timeoutMillis();;) { |
580 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
581 |
> |
tasks.add(new StringTask("0")); |
582 |
> |
tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout), |
583 |
> |
TEST_STRING)); |
584 |
> |
tasks.add(new StringTask("2")); |
585 |
> |
long startTime = System.nanoTime(); |
586 |
> |
List<Future<String>> futures = |
587 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
588 |
> |
assertEquals(tasks.size(), futures.size()); |
589 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
590 |
> |
for (Future future : futures) |
591 |
> |
assertTrue(future.isDone()); |
592 |
> |
try { |
593 |
> |
assertEquals("0", futures.get(0).get()); |
594 |
> |
assertEquals(TEST_STRING, futures.get(1).get()); |
595 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
596 |
> |
// unusual delay before starting second task |
597 |
> |
timeout *= 2; |
598 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
599 |
> |
fail("expected exactly one task to be cancelled"); |
600 |
> |
continue; |
601 |
> |
} |
602 |
> |
assertTrue(futures.get(2).isCancelled()); |
603 |
> |
break; |
604 |
> |
} |
605 |
|
} |
375 |
– |
joinPool(p); |
606 |
|
} |
607 |
|
|
608 |
|
} |