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 |
|
*/ |
16 |
|
|
17 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
29 |
|
static class DirectExecutorService extends AbstractExecutorService { |
30 |
|
public void execute(Runnable r) { r.run(); } |
31 |
|
public void shutdown() { shutdown = true; } |
32 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
32 |
> |
public List<Runnable> shutdownNow() { |
33 |
> |
shutdown = true; |
34 |
> |
return Collections.EMPTY_LIST; |
35 |
> |
} |
36 |
|
public boolean isShutdown() { return shutdown; } |
37 |
|
public boolean isTerminated() { return isShutdown(); } |
38 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
38 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
39 |
> |
return isShutdown(); |
40 |
> |
} |
41 |
|
private volatile boolean shutdown = false; |
42 |
|
} |
43 |
|
|
86 |
|
|
87 |
|
|
88 |
|
/** |
89 |
< |
* A submitted privileged action to completion |
89 |
> |
* A submitted privileged action runs to completion |
90 |
|
*/ |
91 |
|
public void testSubmitPrivilegedAction() throws Exception { |
92 |
< |
Policy savedPolicy = null; |
93 |
< |
try { |
94 |
< |
savedPolicy = Policy.getPolicy(); |
95 |
< |
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() { |
92 |
> |
Runnable r = new CheckedRunnable() { |
93 |
> |
public void realRun() throws Exception { |
94 |
> |
ExecutorService e = new DirectExecutorService(); |
95 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
96 |
|
public Object run() { |
97 |
|
return TEST_STRING; |
98 |
|
}})); |
99 |
|
|
100 |
< |
Object result = future.get(); |
101 |
< |
assertSame(TEST_STRING, result); |
102 |
< |
} |
103 |
< |
finally { |
104 |
< |
try { |
105 |
< |
Policy.setPolicy(savedPolicy); |
106 |
< |
} catch (AccessControlException ok) { |
111 |
< |
return; |
112 |
< |
} |
113 |
< |
} |
100 |
> |
assertSame(TEST_STRING, future.get()); |
101 |
> |
}}; |
102 |
> |
|
103 |
> |
runWithPermissions(r, |
104 |
> |
new RuntimePermission("getClassLoader"), |
105 |
> |
new RuntimePermission("setContextClassLoader"), |
106 |
> |
new RuntimePermission("modifyThread")); |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* A submitted a privileged exception action runs to completion |
110 |
> |
* A submitted privileged exception action runs to completion |
111 |
|
*/ |
112 |
|
public void testSubmitPrivilegedExceptionAction() throws Exception { |
113 |
< |
Policy savedPolicy = null; |
114 |
< |
try { |
115 |
< |
savedPolicy = Policy.getPolicy(); |
116 |
< |
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() { |
113 |
> |
Runnable r = new CheckedRunnable() { |
114 |
> |
public void realRun() throws Exception { |
115 |
> |
ExecutorService e = new DirectExecutorService(); |
116 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
117 |
|
public Object run() { |
118 |
|
return TEST_STRING; |
119 |
|
}})); |
120 |
|
|
121 |
< |
Object result = future.get(); |
122 |
< |
assertSame(TEST_STRING, result); |
123 |
< |
} |
124 |
< |
finally { |
142 |
< |
Policy.setPolicy(savedPolicy); |
143 |
< |
} |
121 |
> |
assertSame(TEST_STRING, future.get()); |
122 |
> |
}}; |
123 |
> |
|
124 |
> |
runWithPermissions(r); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
|
* A submitted failed privileged exception action reports exception |
129 |
|
*/ |
130 |
|
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
131 |
< |
Policy savedPolicy = null; |
132 |
< |
try { |
133 |
< |
savedPolicy = Policy.getPolicy(); |
134 |
< |
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() { |
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() throws Exception { |
136 |
|
throw new IndexOutOfBoundsException(); |
137 |
|
}})); |
138 |
|
|
139 |
< |
future.get(); |
140 |
< |
shouldThrow(); |
141 |
< |
} catch (ExecutionException success) { |
142 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
143 |
< |
} |
144 |
< |
finally { |
145 |
< |
Policy.setPolicy(savedPolicy); |
146 |
< |
} |
139 |
> |
try { |
140 |
> |
future.get(); |
141 |
> |
shouldThrow(); |
142 |
> |
} catch (ExecutionException success) { |
143 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
144 |
> |
}}}; |
145 |
> |
|
146 |
> |
runWithPermissions(r); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
170 |
|
} |
171 |
|
|
172 |
|
/** |
173 |
< |
* submit(runnable) throws RejectedExecutionException if |
203 |
< |
* executor is saturated. |
173 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
174 |
|
*/ |
175 |
< |
public void testExecute1() { |
176 |
< |
ThreadPoolExecutor p = |
177 |
< |
new ThreadPoolExecutor(1, 1, |
178 |
< |
60, TimeUnit.SECONDS, |
179 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
180 |
< |
try { |
181 |
< |
for (int i = 0; i < 2; ++i) |
182 |
< |
p.submit(new MediumRunnable()); |
183 |
< |
for (int i = 0; i < 2; ++i) { |
184 |
< |
try { |
185 |
< |
p.submit(new MediumRunnable()); |
186 |
< |
shouldThrow(); |
187 |
< |
} catch (RejectedExecutionException success) {} |
188 |
< |
} |
189 |
< |
} finally { |
190 |
< |
joinPool(p); |
191 |
< |
} |
192 |
< |
} |
193 |
< |
|
194 |
< |
/** |
195 |
< |
* submit(callable) throws RejectedExecutionException |
196 |
< |
* 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 |
< |
} |
175 |
> |
public void testInterruptedSubmit() throws InterruptedException { |
176 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
177 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
178 |
> |
final ExecutorService p |
179 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
180 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
181 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
182 |
> |
public Void realCall() throws InterruptedException { |
183 |
> |
quittingTime.await(); |
184 |
> |
return null; |
185 |
> |
}}; |
186 |
> |
try { |
187 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
188 |
> |
public void realRun() throws Exception { |
189 |
> |
Future<Void> future = p.submit(awaiter); |
190 |
> |
submitted.countDown(); |
191 |
> |
future.get(); |
192 |
> |
}}); |
193 |
> |
t.start(); |
194 |
> |
submitted.await(); |
195 |
> |
t.interrupt(); |
196 |
> |
t.join(); |
197 |
|
} finally { |
198 |
+ |
quittingTime.countDown(); |
199 |
|
joinPool(p); |
200 |
|
} |
201 |
|
} |
202 |
|
|
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 |
– |
|
270 |
– |
/** |
271 |
– |
* get of submitted callable throws InterruptedException if callable |
272 |
– |
* 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 |
– |
|
203 |
|
/** |
204 |
< |
* get of submit(callable) throws ExecutionException if callable |
205 |
< |
* throws exception |
204 |
> |
* get of submit(callable) throws ExecutionException if callable |
205 |
> |
* throws exception |
206 |
|
*/ |
207 |
|
public void testSubmitEE() throws InterruptedException { |
208 |
|
ThreadPoolExecutor p = |
225 |
|
/** |
226 |
|
* invokeAny(null) throws NPE |
227 |
|
*/ |
228 |
< |
public void testInvokeAny1() |
318 |
< |
throws InterruptedException, ExecutionException { |
228 |
> |
public void testInvokeAny1() throws Exception { |
229 |
|
ExecutorService e = new DirectExecutorService(); |
230 |
|
try { |
231 |
|
e.invokeAny(null); |
239 |
|
/** |
240 |
|
* invokeAny(empty collection) throws IAE |
241 |
|
*/ |
242 |
< |
public void testInvokeAny2() |
333 |
< |
throws InterruptedException, ExecutionException { |
242 |
> |
public void testInvokeAny2() throws Exception { |
243 |
|
ExecutorService e = new DirectExecutorService(); |
244 |
|
try { |
245 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
589 |
|
try { |
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>> futures = |
595 |
< |
e.invokeAll(l, SMALL_DELAY_MS, MILLISECONDS); |
595 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
596 |
|
assertEquals(3, futures.size()); |
597 |
|
Iterator<Future<String>> it = futures.iterator(); |
598 |
|
Future<String> f1 = it.next(); |
601 |
|
assertTrue(f1.isDone()); |
602 |
|
assertFalse(f1.isCancelled()); |
603 |
|
assertTrue(f2.isDone()); |
604 |
+ |
assertFalse(f2.isCancelled()); |
605 |
|
assertTrue(f3.isDone()); |
606 |
|
assertTrue(f3.isCancelled()); |
607 |
|
} finally { |