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/licenses/publicdomain |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
|
13 |
|
import java.math.BigInteger; |
14 |
|
import java.security.*; |
15 |
|
|
16 |
< |
public class ExecutorsTest extends JSR166TestCase{ |
16 |
> |
public class ExecutorsTest extends JSR166TestCase { |
17 |
|
public static void main(String[] args) { |
18 |
< |
junit.textui.TestRunner.run (suite()); |
18 |
> |
junit.textui.TestRunner.run (suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
21 |
|
return new TestSuite(ExecutorsTest.class); |
22 |
|
} |
23 |
|
|
23 |
– |
private static final String TEST_STRING = "a test string"; |
24 |
– |
|
25 |
– |
private static class StringTask implements Callable<String> { |
26 |
– |
public String call() { return TEST_STRING; } |
27 |
– |
} |
28 |
– |
|
29 |
– |
static class DirectExecutor implements Executor { |
30 |
– |
public void execute(Runnable r) { |
31 |
– |
r.run(); |
32 |
– |
} |
33 |
– |
} |
34 |
– |
|
24 |
|
static class TimedCallable<T> implements Callable<T> { |
25 |
< |
private final Executor exec; |
25 |
> |
private final ExecutorService exec; |
26 |
|
private final Callable<T> func; |
27 |
|
private final long msecs; |
28 |
< |
|
29 |
< |
TimedCallable(Executor exec, Callable<T> func, long msecs) { |
28 |
> |
|
29 |
> |
TimedCallable(ExecutorService exec, Callable<T> func, long msecs) { |
30 |
|
this.exec = exec; |
31 |
|
this.func = func; |
32 |
|
this.msecs = msecs; |
33 |
|
} |
34 |
< |
|
34 |
> |
|
35 |
|
public T call() throws Exception { |
36 |
< |
Future<T> ftask = Executors.execute(exec, func); |
36 |
> |
Future<T> ftask = exec.submit(func); |
37 |
|
try { |
38 |
|
return ftask.get(msecs, TimeUnit.MILLISECONDS); |
39 |
|
} finally { |
69 |
|
e.execute(new NoOpRunnable()); |
70 |
|
e.execute(new NoOpRunnable()); |
71 |
|
e.execute(new NoOpRunnable()); |
72 |
< |
e.shutdown(); |
72 |
> |
joinPool(e); |
73 |
|
} |
74 |
|
|
75 |
|
/** |
80 |
|
e.execute(new NoOpRunnable()); |
81 |
|
e.execute(new NoOpRunnable()); |
82 |
|
e.execute(new NoOpRunnable()); |
83 |
< |
e.shutdown(); |
83 |
> |
joinPool(e); |
84 |
|
} |
85 |
|
|
86 |
|
/** |
90 |
|
try { |
91 |
|
ExecutorService e = Executors.newCachedThreadPool(null); |
92 |
|
shouldThrow(); |
93 |
< |
} |
105 |
< |
catch(NullPointerException success) { |
106 |
< |
} |
93 |
> |
} catch (NullPointerException success) {} |
94 |
|
} |
95 |
|
|
96 |
|
|
102 |
|
e.execute(new NoOpRunnable()); |
103 |
|
e.execute(new NoOpRunnable()); |
104 |
|
e.execute(new NoOpRunnable()); |
105 |
< |
e.shutdown(); |
105 |
> |
joinPool(e); |
106 |
|
} |
107 |
|
|
108 |
|
/** |
113 |
|
e.execute(new NoOpRunnable()); |
114 |
|
e.execute(new NoOpRunnable()); |
115 |
|
e.execute(new NoOpRunnable()); |
116 |
< |
e.shutdown(); |
116 |
> |
joinPool(e); |
117 |
|
} |
118 |
|
|
119 |
|
/** |
123 |
|
try { |
124 |
|
ExecutorService e = Executors.newSingleThreadExecutor(null); |
125 |
|
shouldThrow(); |
126 |
< |
} |
127 |
< |
catch(NullPointerException success) { |
126 |
> |
} catch (NullPointerException success) {} |
127 |
> |
} |
128 |
> |
|
129 |
> |
/** |
130 |
> |
* A new SingleThreadExecutor cannot be casted to concrete implementation |
131 |
> |
*/ |
132 |
> |
public void testCastNewSingleThreadExecutor() { |
133 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(); |
134 |
> |
try { |
135 |
> |
ThreadPoolExecutor tpe = (ThreadPoolExecutor)e; |
136 |
> |
shouldThrow(); |
137 |
> |
} catch (ClassCastException success) { |
138 |
> |
} finally { |
139 |
> |
joinPool(e); |
140 |
|
} |
141 |
|
} |
142 |
|
|
143 |
+ |
|
144 |
|
/** |
145 |
|
* A new newFixedThreadPool can execute runnables |
146 |
|
*/ |
149 |
|
e.execute(new NoOpRunnable()); |
150 |
|
e.execute(new NoOpRunnable()); |
151 |
|
e.execute(new NoOpRunnable()); |
152 |
< |
e.shutdown(); |
152 |
> |
joinPool(e); |
153 |
|
} |
154 |
|
|
155 |
|
/** |
160 |
|
e.execute(new NoOpRunnable()); |
161 |
|
e.execute(new NoOpRunnable()); |
162 |
|
e.execute(new NoOpRunnable()); |
163 |
< |
e.shutdown(); |
163 |
> |
joinPool(e); |
164 |
|
} |
165 |
|
|
166 |
|
/** |
170 |
|
try { |
171 |
|
ExecutorService e = Executors.newFixedThreadPool(2, null); |
172 |
|
shouldThrow(); |
173 |
< |
} |
174 |
< |
catch(NullPointerException success) { |
175 |
< |
} |
173 |
> |
} catch (NullPointerException success) {} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
180 |
|
try { |
181 |
|
ExecutorService e = Executors.newFixedThreadPool(0); |
182 |
|
shouldThrow(); |
183 |
< |
} |
186 |
< |
catch(IllegalArgumentException success) { |
187 |
< |
} |
183 |
> |
} catch (IllegalArgumentException success) {} |
184 |
|
} |
185 |
|
|
186 |
+ |
|
187 |
|
/** |
188 |
< |
* execute of runnable runs it to completion |
188 |
> |
* An unconfigurable newFixedThreadPool can execute runnables |
189 |
|
*/ |
190 |
< |
public void testExecuteRunnable() { |
191 |
< |
try { |
192 |
< |
Executor e = new DirectExecutor(); |
193 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
194 |
< |
assertFalse(task.done); |
195 |
< |
Future<?> future = Executors.execute(e, task); |
199 |
< |
future.get(); |
200 |
< |
assertTrue(task.done); |
201 |
< |
} |
202 |
< |
catch (ExecutionException ex) { |
203 |
< |
unexpectedException(); |
204 |
< |
} |
205 |
< |
catch (InterruptedException ex) { |
206 |
< |
unexpectedException(); |
207 |
< |
} |
190 |
> |
public void testunconfigurableExecutorService() { |
191 |
> |
ExecutorService e = Executors.unconfigurableExecutorService(Executors.newFixedThreadPool(2)); |
192 |
> |
e.execute(new NoOpRunnable()); |
193 |
> |
e.execute(new NoOpRunnable()); |
194 |
> |
e.execute(new NoOpRunnable()); |
195 |
> |
joinPool(e); |
196 |
|
} |
197 |
|
|
198 |
|
/** |
199 |
< |
* invoke of a runnable runs it to completion |
199 |
> |
* unconfigurableExecutorService(null) throws NPE |
200 |
|
*/ |
201 |
< |
public void testInvokeRunnable() { |
201 |
> |
public void testunconfigurableExecutorServiceNPE() { |
202 |
|
try { |
203 |
< |
Executor e = new DirectExecutor(); |
204 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
205 |
< |
assertFalse(task.done); |
218 |
< |
Executors.invoke(e, task); |
219 |
< |
assertTrue(task.done); |
220 |
< |
} |
221 |
< |
catch (ExecutionException ex) { |
222 |
< |
unexpectedException(); |
223 |
< |
} |
224 |
< |
catch (InterruptedException ex) { |
225 |
< |
unexpectedException(); |
226 |
< |
} |
203 |
> |
ExecutorService e = Executors.unconfigurableExecutorService(null); |
204 |
> |
shouldThrow(); |
205 |
> |
} catch (NullPointerException success) {} |
206 |
|
} |
207 |
|
|
208 |
|
/** |
209 |
< |
* execute of a callable runs it to completion |
209 |
> |
* unconfigurableScheduledExecutorService(null) throws NPE |
210 |
|
*/ |
211 |
< |
public void testExecuteCallable() { |
211 |
> |
public void testunconfigurableScheduledExecutorServiceNPE() { |
212 |
|
try { |
213 |
< |
Executor e = new DirectExecutor(); |
214 |
< |
Future<String> future = Executors.execute(e, new StringTask()); |
215 |
< |
String result = future.get(); |
237 |
< |
assertSame(TEST_STRING, result); |
238 |
< |
} |
239 |
< |
catch (ExecutionException ex) { |
240 |
< |
unexpectedException(); |
241 |
< |
} |
242 |
< |
catch (InterruptedException ex) { |
243 |
< |
unexpectedException(); |
244 |
< |
} |
213 |
> |
ExecutorService e = Executors.unconfigurableScheduledExecutorService(null); |
214 |
> |
shouldThrow(); |
215 |
> |
} catch (NullPointerException success) {} |
216 |
|
} |
217 |
|
|
218 |
|
|
219 |
|
/** |
220 |
< |
* execute of a privileged action runs it to completion |
220 |
> |
* a newSingleThreadScheduledExecutor successfully runs delayed task |
221 |
|
*/ |
222 |
< |
public void testExecutePrivilegedAction() { |
223 |
< |
Policy savedPolicy = Policy.getPolicy(); |
224 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
225 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
226 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
227 |
< |
Policy.setPolicy(policy); |
228 |
< |
try { |
229 |
< |
Executor e = new DirectExecutor(); |
230 |
< |
Future future = Executors.execute(e, new PrivilegedAction() { |
260 |
< |
public Object run() { |
261 |
< |
return TEST_STRING; |
262 |
< |
}}); |
263 |
< |
|
264 |
< |
Object result = future.get(); |
265 |
< |
assertSame(TEST_STRING, result); |
266 |
< |
} |
267 |
< |
catch (ExecutionException ex) { |
268 |
< |
unexpectedException(); |
269 |
< |
} |
270 |
< |
catch (InterruptedException ex) { |
271 |
< |
unexpectedException(); |
272 |
< |
} |
273 |
< |
finally { |
274 |
< |
Policy.setPolicy(savedPolicy); |
275 |
< |
} |
222 |
> |
public void testNewSingleThreadScheduledExecutor() throws Exception { |
223 |
> |
TrackedCallable callable = new TrackedCallable(); |
224 |
> |
ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor(); |
225 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
226 |
> |
assertFalse(callable.done); |
227 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
228 |
> |
assertTrue(callable.done); |
229 |
> |
assertEquals(Boolean.TRUE, f.get()); |
230 |
> |
joinPool(p1); |
231 |
|
} |
232 |
|
|
233 |
|
/** |
234 |
< |
* execute of a privileged exception action runs it to completion |
234 |
> |
* a newScheduledThreadPool successfully runs delayed task |
235 |
|
*/ |
236 |
< |
public void testExecutePrivilegedExceptionAction() { |
237 |
< |
Policy savedPolicy = Policy.getPolicy(); |
238 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
239 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
240 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
241 |
< |
Policy.setPolicy(policy); |
242 |
< |
try { |
243 |
< |
Executor e = new DirectExecutor(); |
244 |
< |
Future future = Executors.execute(e, new PrivilegedExceptionAction() { |
245 |
< |
public Object run() { |
291 |
< |
return TEST_STRING; |
292 |
< |
}}); |
236 |
> |
public void testnewScheduledThreadPool() throws Exception { |
237 |
> |
TrackedCallable callable = new TrackedCallable(); |
238 |
> |
ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2); |
239 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
240 |
> |
assertFalse(callable.done); |
241 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
242 |
> |
assertTrue(callable.done); |
243 |
> |
assertEquals(Boolean.TRUE, f.get()); |
244 |
> |
joinPool(p1); |
245 |
> |
} |
246 |
|
|
247 |
< |
Object result = future.get(); |
248 |
< |
assertSame(TEST_STRING, result); |
249 |
< |
} |
250 |
< |
catch (ExecutionException ex) { |
251 |
< |
unexpectedException(); |
252 |
< |
} |
253 |
< |
catch (InterruptedException ex) { |
254 |
< |
unexpectedException(); |
255 |
< |
} |
256 |
< |
finally { |
257 |
< |
Policy.setPolicy(savedPolicy); |
258 |
< |
} |
247 |
> |
/** |
248 |
> |
* an unconfigurable newScheduledThreadPool successfully runs delayed task |
249 |
> |
*/ |
250 |
> |
public void testunconfigurableScheduledExecutorService() throws Exception { |
251 |
> |
TrackedCallable callable = new TrackedCallable(); |
252 |
> |
ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2)); |
253 |
> |
Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
254 |
> |
assertFalse(callable.done); |
255 |
> |
Thread.sleep(MEDIUM_DELAY_MS); |
256 |
> |
assertTrue(callable.done); |
257 |
> |
assertEquals(Boolean.TRUE, f.get()); |
258 |
> |
joinPool(p1); |
259 |
|
} |
260 |
|
|
261 |
|
/** |
262 |
< |
* execute of a failed privileged exception action reports exception |
262 |
> |
* timeouts from execute will time out if they compute too long. |
263 |
|
*/ |
264 |
< |
public void testExecuteFailedPrivilegedExceptionAction() { |
265 |
< |
Policy savedPolicy = Policy.getPolicy(); |
266 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
267 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
315 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
316 |
< |
Policy.setPolicy(policy); |
264 |
> |
public void testTimedCallable() throws Exception { |
265 |
> |
int N = 10000; |
266 |
> |
ExecutorService executor = Executors.newSingleThreadExecutor(); |
267 |
> |
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
268 |
|
try { |
269 |
< |
Executor e = new DirectExecutor(); |
319 |
< |
Future future = Executors.execute(e, new PrivilegedExceptionAction() { |
320 |
< |
public Object run() throws Exception { |
321 |
< |
throw new IndexOutOfBoundsException(); |
322 |
< |
}}); |
269 |
> |
long startTime = System.currentTimeMillis(); |
270 |
|
|
271 |
< |
Object result = future.get(); |
272 |
< |
shouldThrow(); |
273 |
< |
} |
274 |
< |
catch (ExecutionException success) { |
275 |
< |
} |
276 |
< |
catch (InterruptedException ex) { |
277 |
< |
unexpectedException(); |
271 |
> |
long i = 0; |
272 |
> |
while (tasks.size() < N) { |
273 |
> |
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
274 |
> |
i += 10; |
275 |
> |
} |
276 |
> |
|
277 |
> |
int iters = 0; |
278 |
> |
BigInteger sum = BigInteger.ZERO; |
279 |
> |
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
280 |
> |
try { |
281 |
> |
++iters; |
282 |
> |
sum = sum.add(it.next().call()); |
283 |
> |
} |
284 |
> |
catch (TimeoutException success) { |
285 |
> |
assertTrue(iters > 0); |
286 |
> |
return; |
287 |
> |
} |
288 |
> |
} |
289 |
> |
// if by chance we didn't ever time out, total time must be small |
290 |
> |
long elapsed = System.currentTimeMillis() - startTime; |
291 |
> |
assertTrue(elapsed < N); |
292 |
|
} |
293 |
|
finally { |
294 |
< |
Policy.setPolicy(savedPolicy); |
294 |
> |
joinPool(executor); |
295 |
|
} |
296 |
|
} |
297 |
|
|
298 |
+ |
|
299 |
|
/** |
300 |
< |
* invoke of a collable runs it to completion |
300 |
> |
* ThreadPoolExecutor using defaultThreadFactory has |
301 |
> |
* specified group, priority, daemon status, and name |
302 |
|
*/ |
303 |
< |
public void testInvokeCallable() { |
304 |
< |
try { |
305 |
< |
Executor e = new DirectExecutor(); |
306 |
< |
String result = Executors.invoke(e, new StringTask()); |
303 |
> |
public void testDefaultThreadFactory() throws Exception { |
304 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
305 |
> |
Runnable r = new Runnable() { |
306 |
> |
public void run() { |
307 |
> |
try { |
308 |
> |
Thread current = Thread.currentThread(); |
309 |
> |
threadAssertTrue(!current.isDaemon()); |
310 |
> |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
311 |
> |
ThreadGroup g = current.getThreadGroup(); |
312 |
> |
SecurityManager s = System.getSecurityManager(); |
313 |
> |
if (s != null) |
314 |
> |
threadAssertTrue(g == s.getThreadGroup()); |
315 |
> |
else |
316 |
> |
threadAssertTrue(g == egroup); |
317 |
> |
String name = current.getName(); |
318 |
> |
threadAssertTrue(name.endsWith("thread-1")); |
319 |
> |
} catch (SecurityException ok) { |
320 |
> |
// Also pass if not allowed to change setting |
321 |
> |
} |
322 |
> |
} |
323 |
> |
}; |
324 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
325 |
|
|
326 |
< |
assertSame(TEST_STRING, result); |
327 |
< |
} |
328 |
< |
catch (ExecutionException ex) { |
329 |
< |
unexpectedException(); |
326 |
> |
e.execute(r); |
327 |
> |
try { |
328 |
> |
e.shutdown(); |
329 |
> |
} catch (SecurityException ok) { |
330 |
|
} |
331 |
< |
catch (InterruptedException ex) { |
332 |
< |
unexpectedException(); |
331 |
> |
|
332 |
> |
try { |
333 |
> |
Thread.sleep(SHORT_DELAY_MS); |
334 |
> |
} finally { |
335 |
> |
joinPool(e); |
336 |
|
} |
337 |
|
} |
338 |
|
|
339 |
|
/** |
340 |
< |
* execute with null executor throws NPE |
340 |
> |
* ThreadPoolExecutor using privilegedThreadFactory has |
341 |
> |
* specified group, priority, daemon status, name, |
342 |
> |
* access control context and context class loader |
343 |
|
*/ |
344 |
< |
public void testNullExecuteRunnable() { |
344 |
> |
public void testPrivilegedThreadFactory() throws Exception { |
345 |
> |
Policy savedPolicy = null; |
346 |
|
try { |
347 |
< |
TrackedShortRunnable task = new TrackedShortRunnable(); |
348 |
< |
assertFalse(task.done); |
349 |
< |
Future<?> future = Executors.execute(null, task); |
350 |
< |
shouldThrow(); |
347 |
> |
savedPolicy = Policy.getPolicy(); |
348 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
349 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
350 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
351 |
> |
Policy.setPolicy(policy); |
352 |
> |
} catch (AccessControlException ok) { |
353 |
> |
return; |
354 |
|
} |
355 |
< |
catch (NullPointerException success) { |
355 |
> |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
356 |
> |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
357 |
> |
final AccessControlContext thisacc = AccessController.getContext(); |
358 |
> |
Runnable r = new Runnable() { |
359 |
> |
public void run() { |
360 |
> |
try { |
361 |
> |
Thread current = Thread.currentThread(); |
362 |
> |
threadAssertTrue(!current.isDaemon()); |
363 |
> |
threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY); |
364 |
> |
ThreadGroup g = current.getThreadGroup(); |
365 |
> |
SecurityManager s = System.getSecurityManager(); |
366 |
> |
if (s != null) |
367 |
> |
threadAssertTrue(g == s.getThreadGroup()); |
368 |
> |
else |
369 |
> |
threadAssertTrue(g == egroup); |
370 |
> |
String name = current.getName(); |
371 |
> |
threadAssertTrue(name.endsWith("thread-1")); |
372 |
> |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
373 |
> |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
374 |
> |
} catch (SecurityException ok) { |
375 |
> |
// Also pass if not allowed to change settings |
376 |
> |
} |
377 |
> |
} |
378 |
> |
}; |
379 |
> |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
380 |
> |
|
381 |
> |
Policy.setPolicy(savedPolicy); |
382 |
> |
e.execute(r); |
383 |
> |
try { |
384 |
> |
e.shutdown(); |
385 |
> |
} catch (SecurityException ok) { |
386 |
|
} |
387 |
< |
catch (Exception ex) { |
388 |
< |
unexpectedException(); |
387 |
> |
try { |
388 |
> |
Thread.sleep(SHORT_DELAY_MS); |
389 |
> |
} finally { |
390 |
> |
joinPool(e); |
391 |
|
} |
392 |
+ |
|
393 |
|
} |
394 |
|
|
395 |
< |
/** |
396 |
< |
* execute with a null runnable throws NPE |
397 |
< |
*/ |
398 |
< |
public void testExecuteNullRunnable() { |
399 |
< |
try { |
377 |
< |
Executor e = new DirectExecutor(); |
378 |
< |
TrackedShortRunnable task = null; |
379 |
< |
Future<?> future = Executors.execute(e, task); |
380 |
< |
shouldThrow(); |
381 |
< |
} |
382 |
< |
catch (NullPointerException success) { |
395 |
> |
void checkCCL() { |
396 |
> |
SecurityManager sm = System.getSecurityManager(); |
397 |
> |
if (sm != null) { |
398 |
> |
sm.checkPermission(new RuntimePermission("setContextClassLoader")); |
399 |
> |
sm.checkPermission(new RuntimePermission("getClassLoader")); |
400 |
|
} |
401 |
< |
catch (Exception ex) { |
402 |
< |
unexpectedException(); |
401 |
> |
} |
402 |
> |
|
403 |
> |
class CheckCCL implements Callable<Object> { |
404 |
> |
public Object call() { |
405 |
> |
checkCCL(); |
406 |
> |
return null; |
407 |
|
} |
408 |
|
} |
409 |
|
|
410 |
+ |
|
411 |
|
/** |
412 |
< |
* invoke of a null runnable throws NPE |
412 |
> |
* Without class loader permissions, creating |
413 |
> |
* privilegedCallableUsingCurrentClassLoader throws ACE |
414 |
|
*/ |
415 |
< |
public void testInvokeNullRunnable() { |
415 |
> |
public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() { |
416 |
> |
Policy savedPolicy = null; |
417 |
|
try { |
418 |
< |
Executor e = new DirectExecutor(); |
419 |
< |
TrackedShortRunnable task = null; |
420 |
< |
Executors.invoke(e, task); |
421 |
< |
shouldThrow(); |
422 |
< |
} |
399 |
< |
catch (NullPointerException success) { |
418 |
> |
savedPolicy = Policy.getPolicy(); |
419 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
420 |
> |
Policy.setPolicy(policy); |
421 |
> |
} catch (AccessControlException ok) { |
422 |
> |
return; |
423 |
|
} |
424 |
< |
catch (Exception ex) { |
425 |
< |
unexpectedException(); |
424 |
> |
|
425 |
> |
// Check if program still has too many permissions to run test |
426 |
> |
try { |
427 |
> |
checkCCL(); |
428 |
> |
// too many privileges to test; so return |
429 |
> |
Policy.setPolicy(savedPolicy); |
430 |
> |
return; |
431 |
> |
} catch (AccessControlException ok) { |
432 |
|
} |
404 |
– |
} |
433 |
|
|
406 |
– |
/** |
407 |
– |
* execute of a null callable throws NPE |
408 |
– |
*/ |
409 |
– |
public void testExecuteNullCallable() { |
434 |
|
try { |
435 |
< |
Executor e = new DirectExecutor(); |
412 |
< |
StringTask t = null; |
413 |
< |
Future<String> future = Executors.execute(e, t); |
435 |
> |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
436 |
|
shouldThrow(); |
437 |
< |
} |
438 |
< |
catch (NullPointerException success) { |
439 |
< |
} |
418 |
< |
catch (Exception ex) { |
419 |
< |
unexpectedException(); |
437 |
> |
} catch (AccessControlException success) { |
438 |
> |
} finally { |
439 |
> |
Policy.setPolicy(savedPolicy); |
440 |
|
} |
441 |
|
} |
442 |
|
|
443 |
|
/** |
444 |
< |
* invoke of a null callable throws NPE |
444 |
> |
* With class loader permissions, calling |
445 |
> |
* privilegedCallableUsingCurrentClassLoader does not throw ACE |
446 |
|
*/ |
447 |
< |
public void testInvokeNullCallable() { |
447 |
> |
public void testprivilegedCallableUsingCCLWithPrivs() throws Exception { |
448 |
> |
Policy savedPolicy = null; |
449 |
|
try { |
450 |
< |
Executor e = new DirectExecutor(); |
451 |
< |
StringTask t = null; |
452 |
< |
String result = Executors.invoke(e, t); |
453 |
< |
shouldThrow(); |
450 |
> |
savedPolicy = Policy.getPolicy(); |
451 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
452 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
453 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
454 |
> |
Policy.setPolicy(policy); |
455 |
> |
} catch (AccessControlException ok) { |
456 |
> |
return; |
457 |
|
} |
458 |
< |
catch (NullPointerException success) { |
458 |
> |
|
459 |
> |
try { |
460 |
> |
Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable()); |
461 |
> |
task.call(); |
462 |
|
} |
463 |
< |
catch (Exception ex) { |
464 |
< |
unexpectedException(); |
463 |
> |
finally { |
464 |
> |
Policy.setPolicy(savedPolicy); |
465 |
|
} |
466 |
|
} |
467 |
|
|
468 |
|
/** |
469 |
< |
* execute(Executor, Runnable) throws RejectedExecutionException |
442 |
< |
* if saturated. |
469 |
> |
* Without permissions, calling privilegedCallable throws ACE |
470 |
|
*/ |
471 |
< |
public void testExecute1() { |
472 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
471 |
> |
public void testprivilegedCallableWithNoPrivs() throws Exception { |
472 |
> |
Callable task; |
473 |
> |
Policy savedPolicy = null; |
474 |
> |
AdjustablePolicy policy = null; |
475 |
> |
AccessControlContext noprivAcc = null; |
476 |
> |
try { |
477 |
> |
savedPolicy = Policy.getPolicy(); |
478 |
> |
policy = new AdjustablePolicy(); |
479 |
> |
Policy.setPolicy(policy); |
480 |
> |
noprivAcc = AccessController.getContext(); |
481 |
> |
task = Executors.privilegedCallable(new CheckCCL()); |
482 |
> |
Policy.setPolicy(savedPolicy); |
483 |
> |
} catch (AccessControlException ok) { |
484 |
> |
return; // program has too few permissions to set up test |
485 |
> |
} |
486 |
> |
|
487 |
> |
// Make sure that program doesn't have too many permissions |
488 |
|
try { |
489 |
< |
|
490 |
< |
for(int i = 0; i < 5; ++i){ |
491 |
< |
Executors.execute(p, new MediumRunnable()); |
492 |
< |
} |
493 |
< |
shouldThrow(); |
494 |
< |
} catch(RejectedExecutionException success){} |
495 |
< |
joinPool(p); |
496 |
< |
} |
489 |
> |
AccessController.doPrivileged(new PrivilegedAction() { |
490 |
> |
public Object run() { |
491 |
> |
checkCCL(); |
492 |
> |
return null; |
493 |
> |
}}, noprivAcc); |
494 |
> |
// too many permssions; skip test |
495 |
> |
return; |
496 |
> |
} catch (AccessControlException ok) { |
497 |
> |
} |
498 |
|
|
456 |
– |
/** |
457 |
– |
* execute(Executor, Callable)throws RejectedExecutionException |
458 |
– |
* if saturated. |
459 |
– |
*/ |
460 |
– |
public void testExecute2() { |
461 |
– |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
499 |
|
try { |
500 |
< |
for(int i = 0; i < 5; ++i) { |
464 |
< |
Executors.execute(p, new SmallCallable()); |
465 |
< |
} |
500 |
> |
task.call(); |
501 |
|
shouldThrow(); |
502 |
< |
} catch(RejectedExecutionException e){} |
468 |
< |
joinPool(p); |
502 |
> |
} catch (AccessControlException success) {} |
503 |
|
} |
504 |
|
|
471 |
– |
|
505 |
|
/** |
506 |
< |
* invoke(Executor, Runnable) throws InterruptedException if |
474 |
< |
* caller interrupted. |
506 |
> |
* With permissions, calling privilegedCallable succeeds |
507 |
|
*/ |
508 |
< |
public void testInterruptedInvoke() { |
509 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
478 |
< |
Thread t = new Thread(new Runnable() { |
479 |
< |
public void run() { |
480 |
< |
try { |
481 |
< |
Executors.invoke(p,new Runnable() { |
482 |
< |
public void run() { |
483 |
< |
try { |
484 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
485 |
< |
shouldThrow(); |
486 |
< |
} catch(InterruptedException e){ |
487 |
< |
} |
488 |
< |
} |
489 |
< |
}); |
490 |
< |
} catch(InterruptedException success){ |
491 |
< |
} catch(Exception e) { |
492 |
< |
unexpectedException(); |
493 |
< |
} |
494 |
< |
|
495 |
< |
} |
496 |
< |
}); |
508 |
> |
public void testprivilegedCallableWithPrivs() throws Exception { |
509 |
> |
Policy savedPolicy = null; |
510 |
|
try { |
511 |
< |
t.start(); |
512 |
< |
Thread.sleep(SHORT_DELAY_MS); |
513 |
< |
t.interrupt(); |
514 |
< |
} catch(Exception e){ |
515 |
< |
unexpectedException(); |
511 |
> |
savedPolicy = Policy.getPolicy(); |
512 |
> |
AdjustablePolicy policy = new AdjustablePolicy(); |
513 |
> |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
514 |
> |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
515 |
> |
Policy.setPolicy(policy); |
516 |
> |
} catch (AccessControlException ok) { |
517 |
> |
return; |
518 |
|
} |
504 |
– |
joinPool(p); |
505 |
– |
} |
519 |
|
|
520 |
< |
/** |
508 |
< |
* invoke(Executor, Runnable) throws ExecutionException if |
509 |
< |
* runnable throws exception. |
510 |
< |
*/ |
511 |
< |
public void testInvoke3() { |
512 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
520 |
> |
Callable task = Executors.privilegedCallable(new CheckCCL()); |
521 |
|
try { |
522 |
< |
Runnable r = new Runnable() { |
523 |
< |
public void run() { |
524 |
< |
int i = 5/0; |
517 |
< |
} |
518 |
< |
}; |
519 |
< |
|
520 |
< |
for(int i =0; i < 5; i++){ |
521 |
< |
Executors.invoke(p,r); |
522 |
< |
} |
523 |
< |
|
524 |
< |
shouldThrow(); |
525 |
< |
} catch(ExecutionException success){ |
526 |
< |
} catch(Exception e){ |
527 |
< |
unexpectedException(); |
522 |
> |
task.call(); |
523 |
> |
} finally { |
524 |
> |
Policy.setPolicy(savedPolicy); |
525 |
|
} |
529 |
– |
joinPool(p); |
526 |
|
} |
527 |
|
|
532 |
– |
|
533 |
– |
|
528 |
|
/** |
529 |
< |
* invoke(Executor, Callable) throws InterruptedException if |
536 |
< |
* callable throws exception |
529 |
> |
* callable(Runnable) returns null when called |
530 |
|
*/ |
531 |
< |
public void testInvoke5() { |
532 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
533 |
< |
|
534 |
< |
final Callable c = new Callable() { |
542 |
< |
public Object call() { |
543 |
< |
try { |
544 |
< |
Executors.invoke(p, new SmallCallable()); |
545 |
< |
shouldThrow(); |
546 |
< |
} catch(InterruptedException e){} |
547 |
< |
catch(RejectedExecutionException e2){} |
548 |
< |
catch(ExecutionException e3){} |
549 |
< |
return Boolean.TRUE; |
550 |
< |
} |
551 |
< |
}; |
531 |
> |
public void testCallable1() throws Exception { |
532 |
> |
Callable c = Executors.callable(new NoOpRunnable()); |
533 |
> |
assertNull(c.call()); |
534 |
> |
} |
535 |
|
|
536 |
+ |
/** |
537 |
+ |
* callable(Runnable, result) returns result when called |
538 |
+ |
*/ |
539 |
+ |
public void testCallable2() throws Exception { |
540 |
+ |
Callable c = Executors.callable(new NoOpRunnable(), one); |
541 |
+ |
assertEquals(one, c.call()); |
542 |
+ |
} |
543 |
|
|
544 |
< |
|
545 |
< |
Thread t = new Thread(new Runnable() { |
546 |
< |
public void run() { |
547 |
< |
try { |
548 |
< |
c.call(); |
549 |
< |
} catch(Exception e){} |
550 |
< |
} |
561 |
< |
}); |
562 |
< |
try { |
563 |
< |
t.start(); |
564 |
< |
Thread.sleep(SHORT_DELAY_MS); |
565 |
< |
t.interrupt(); |
566 |
< |
t.join(); |
567 |
< |
} catch(InterruptedException e){ |
568 |
< |
unexpectedException(); |
569 |
< |
} |
570 |
< |
|
571 |
< |
joinPool(p); |
544 |
> |
/** |
545 |
> |
* callable(PrivilegedAction) returns its result when called |
546 |
> |
*/ |
547 |
> |
public void testCallable3() throws Exception { |
548 |
> |
Callable c = Executors.callable(new PrivilegedAction() { |
549 |
> |
public Object run() { return one; }}); |
550 |
> |
assertEquals(one, c.call()); |
551 |
|
} |
552 |
|
|
553 |
|
/** |
554 |
< |
* invoke(Executor, Callable) will throw ExecutionException |
576 |
< |
* if callable throws exception |
554 |
> |
* callable(PrivilegedExceptionAction) returns its result when called |
555 |
|
*/ |
556 |
< |
public void testInvoke6() { |
557 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
556 |
> |
public void testCallable4() throws Exception { |
557 |
> |
Callable c = Executors.callable(new PrivilegedExceptionAction() { |
558 |
> |
public Object run() { return one; }}); |
559 |
> |
assertEquals(one, c.call()); |
560 |
> |
} |
561 |
|
|
562 |
+ |
|
563 |
+ |
/** |
564 |
+ |
* callable(null Runnable) throws NPE |
565 |
+ |
*/ |
566 |
+ |
public void testCallableNPE1() { |
567 |
|
try { |
568 |
< |
Callable c = new Callable() { |
583 |
< |
public Object call() { |
584 |
< |
int i = 5/0; |
585 |
< |
return Boolean.TRUE; |
586 |
< |
} |
587 |
< |
}; |
588 |
< |
|
589 |
< |
for(int i =0; i < 5; i++){ |
590 |
< |
Executors.invoke(p,c); |
591 |
< |
} |
592 |
< |
|
568 |
> |
Callable c = Executors.callable((Runnable) null); |
569 |
|
shouldThrow(); |
570 |
< |
} |
595 |
< |
catch(ExecutionException success){ |
596 |
< |
} catch(Exception e) { |
597 |
< |
unexpectedException(); |
598 |
< |
} |
599 |
< |
joinPool(p); |
570 |
> |
} catch (NullPointerException success) {} |
571 |
|
} |
572 |
|
|
602 |
– |
|
603 |
– |
|
573 |
|
/** |
574 |
< |
* timeouts from execute will time out if they compute too long. |
574 |
> |
* callable(null, result) throws NPE |
575 |
|
*/ |
576 |
< |
public void testTimedCallable() { |
608 |
< |
int N = 10000; |
609 |
< |
ExecutorService executor = Executors.newSingleThreadExecutor(); |
610 |
< |
List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N); |
576 |
> |
public void testCallableNPE2() { |
577 |
|
try { |
578 |
< |
long startTime = System.currentTimeMillis(); |
579 |
< |
|
580 |
< |
long i = 0; |
615 |
< |
while (tasks.size() < N) { |
616 |
< |
tasks.add(new TimedCallable<BigInteger>(executor, new Fib(i), 1)); |
617 |
< |
i += 10; |
618 |
< |
} |
619 |
< |
|
620 |
< |
int iters = 0; |
621 |
< |
BigInteger sum = BigInteger.ZERO; |
622 |
< |
for (Iterator<Callable<BigInteger>> it = tasks.iterator(); it.hasNext();) { |
623 |
< |
try { |
624 |
< |
++iters; |
625 |
< |
sum = sum.add(it.next().call()); |
626 |
< |
} |
627 |
< |
catch (TimeoutException success) { |
628 |
< |
assertTrue(iters > 0); |
629 |
< |
return; |
630 |
< |
} |
631 |
< |
catch (Exception e) { |
632 |
< |
unexpectedException(); |
633 |
< |
} |
634 |
< |
} |
635 |
< |
// if by chance we didn't ever time out, total time must be small |
636 |
< |
long elapsed = System.currentTimeMillis() - startTime; |
637 |
< |
assertTrue(elapsed < N); |
638 |
< |
} |
639 |
< |
finally { |
640 |
< |
joinPool(executor); |
641 |
< |
} |
578 |
> |
Callable c = Executors.callable((Runnable) null, one); |
579 |
> |
shouldThrow(); |
580 |
> |
} catch (NullPointerException success) {} |
581 |
|
} |
582 |
|
|
644 |
– |
|
583 |
|
/** |
584 |
< |
* ThreadPoolExecutor using defaultThreadFactory has |
647 |
< |
* specified group, priority, daemon status, and name |
584 |
> |
* callable(null PrivilegedAction) throws NPE |
585 |
|
*/ |
586 |
< |
public void testDefaultThreadFactory() { |
650 |
< |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
651 |
< |
Runnable r = new Runnable() { |
652 |
< |
public void run() { |
653 |
< |
Thread current = Thread.currentThread(); |
654 |
< |
threadAssertTrue(!current.isDaemon()); |
655 |
< |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
656 |
< |
ThreadGroup g = current.getThreadGroup(); |
657 |
< |
SecurityManager s = System.getSecurityManager(); |
658 |
< |
if (s != null) |
659 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
660 |
< |
else |
661 |
< |
threadAssertTrue(g == egroup); |
662 |
< |
String name = current.getName(); |
663 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
664 |
< |
} |
665 |
< |
}; |
666 |
< |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory()); |
667 |
< |
|
668 |
< |
e.execute(r); |
669 |
< |
e.shutdown(); |
586 |
> |
public void testCallableNPE3() { |
587 |
|
try { |
588 |
< |
Thread.sleep(SHORT_DELAY_MS); |
589 |
< |
} catch (Exception eX) { |
590 |
< |
unexpectedException(); |
674 |
< |
} finally { |
675 |
< |
joinPool(e); |
676 |
< |
} |
588 |
> |
Callable c = Executors.callable((PrivilegedAction) null); |
589 |
> |
shouldThrow(); |
590 |
> |
} catch (NullPointerException success) {} |
591 |
|
} |
592 |
|
|
593 |
|
/** |
594 |
< |
* ThreadPoolExecutor using privilegedThreadFactory has |
681 |
< |
* specified group, priority, daemon status, name, |
682 |
< |
* access control context and context class loader |
594 |
> |
* callable(null PrivilegedExceptionAction) throws NPE |
595 |
|
*/ |
596 |
< |
public void testPrivilegedThreadFactory() { |
685 |
< |
Policy savedPolicy = Policy.getPolicy(); |
686 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
687 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
688 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
689 |
< |
Policy.setPolicy(policy); |
690 |
< |
final ThreadGroup egroup = Thread.currentThread().getThreadGroup(); |
691 |
< |
final ClassLoader thisccl = Thread.currentThread().getContextClassLoader(); |
692 |
< |
final AccessControlContext thisacc = AccessController.getContext(); |
693 |
< |
Runnable r = new Runnable() { |
694 |
< |
public void run() { |
695 |
< |
Thread current = Thread.currentThread(); |
696 |
< |
threadAssertTrue(!current.isDaemon()); |
697 |
< |
threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY); |
698 |
< |
ThreadGroup g = current.getThreadGroup(); |
699 |
< |
SecurityManager s = System.getSecurityManager(); |
700 |
< |
if (s != null) |
701 |
< |
threadAssertTrue(g == s.getThreadGroup()); |
702 |
< |
else |
703 |
< |
threadAssertTrue(g == egroup); |
704 |
< |
String name = current.getName(); |
705 |
< |
threadAssertTrue(name.endsWith("thread-1")); |
706 |
< |
threadAssertTrue(thisccl == current.getContextClassLoader()); |
707 |
< |
threadAssertTrue(thisacc.equals(AccessController.getContext())); |
708 |
< |
} |
709 |
< |
}; |
710 |
< |
ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory()); |
711 |
< |
|
712 |
< |
Policy.setPolicy(savedPolicy); |
713 |
< |
e.execute(r); |
714 |
< |
e.shutdown(); |
596 |
> |
public void testCallableNPE4() { |
597 |
|
try { |
598 |
< |
Thread.sleep(SHORT_DELAY_MS); |
599 |
< |
} catch (Exception ex) { |
600 |
< |
unexpectedException(); |
719 |
< |
} finally { |
720 |
< |
joinPool(e); |
721 |
< |
} |
722 |
< |
|
598 |
> |
Callable c = Executors.callable((PrivilegedExceptionAction) null); |
599 |
> |
shouldThrow(); |
600 |
> |
} catch (NullPointerException success) {} |
601 |
|
} |
602 |
|
|
603 |
+ |
|
604 |
|
} |