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