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