16 |
|
|
17 |
|
public class AbstractExecutorServiceTest extends JSR166TestCase { |
18 |
|
public static void main(String[] args) { |
19 |
< |
junit.textui.TestRunner.run (suite()); |
19 |
> |
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
22 |
|
return new TestSuite(AbstractExecutorServiceTest.class); |
29 |
|
static class DirectExecutorService extends AbstractExecutorService { |
30 |
|
public void execute(Runnable r) { r.run(); } |
31 |
|
public void shutdown() { shutdown = true; } |
32 |
< |
public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; } |
32 |
> |
public List<Runnable> shutdownNow() { |
33 |
> |
shutdown = true; |
34 |
> |
return Collections.EMPTY_LIST; |
35 |
> |
} |
36 |
|
public boolean isShutdown() { return shutdown; } |
37 |
|
public boolean isTerminated() { return isShutdown(); } |
38 |
< |
public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); } |
38 |
> |
public boolean awaitTermination(long timeout, TimeUnit unit) { |
39 |
> |
return isShutdown(); |
40 |
> |
} |
41 |
|
private volatile boolean shutdown = false; |
42 |
|
} |
43 |
|
|
86 |
|
|
87 |
|
|
88 |
|
/** |
89 |
< |
* A submitted privileged action to completion |
89 |
> |
* A submitted privileged action runs to completion |
90 |
|
*/ |
91 |
|
public void testSubmitPrivilegedAction() throws Exception { |
92 |
< |
Policy savedPolicy = null; |
93 |
< |
try { |
94 |
< |
savedPolicy = Policy.getPolicy(); |
95 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
91 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
92 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
93 |
< |
Policy.setPolicy(policy); |
94 |
< |
} catch (AccessControlException ok) { |
95 |
< |
return; |
96 |
< |
} |
97 |
< |
try { |
98 |
< |
ExecutorService e = new DirectExecutorService(); |
99 |
< |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
92 |
> |
Runnable r = new CheckedRunnable() { |
93 |
> |
public void realRun() throws Exception { |
94 |
> |
ExecutorService e = new DirectExecutorService(); |
95 |
> |
Future future = e.submit(Executors.callable(new PrivilegedAction() { |
96 |
|
public Object run() { |
97 |
|
return TEST_STRING; |
98 |
|
}})); |
99 |
|
|
100 |
< |
Object result = future.get(); |
101 |
< |
assertSame(TEST_STRING, result); |
102 |
< |
} |
103 |
< |
finally { |
104 |
< |
try { |
105 |
< |
Policy.setPolicy(savedPolicy); |
106 |
< |
} catch (AccessControlException ok) { |
111 |
< |
return; |
112 |
< |
} |
113 |
< |
} |
100 |
> |
assertSame(TEST_STRING, future.get()); |
101 |
> |
}}; |
102 |
> |
|
103 |
> |
runWithPermissions(r, |
104 |
> |
new RuntimePermission("getClassLoader"), |
105 |
> |
new RuntimePermission("setContextClassLoader"), |
106 |
> |
new RuntimePermission("modifyThread")); |
107 |
|
} |
108 |
|
|
109 |
|
/** |
110 |
< |
* A submitted a privileged exception action runs to completion |
110 |
> |
* A submitted privileged exception action runs to completion |
111 |
|
*/ |
112 |
|
public void testSubmitPrivilegedExceptionAction() throws Exception { |
113 |
< |
Policy savedPolicy = null; |
114 |
< |
try { |
115 |
< |
savedPolicy = Policy.getPolicy(); |
116 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
124 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
125 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
126 |
< |
Policy.setPolicy(policy); |
127 |
< |
} catch (AccessControlException ok) { |
128 |
< |
return; |
129 |
< |
} |
130 |
< |
|
131 |
< |
try { |
132 |
< |
ExecutorService e = new DirectExecutorService(); |
133 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
113 |
> |
Runnable r = new CheckedRunnable() { |
114 |
> |
public void realRun() throws Exception { |
115 |
> |
ExecutorService e = new DirectExecutorService(); |
116 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
117 |
|
public Object run() { |
118 |
|
return TEST_STRING; |
119 |
|
}})); |
120 |
|
|
121 |
< |
Object result = future.get(); |
122 |
< |
assertSame(TEST_STRING, result); |
123 |
< |
} |
124 |
< |
finally { |
142 |
< |
Policy.setPolicy(savedPolicy); |
143 |
< |
} |
121 |
> |
assertSame(TEST_STRING, future.get()); |
122 |
> |
}}; |
123 |
> |
|
124 |
> |
runWithPermissions(r); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
|
* A submitted failed privileged exception action reports exception |
129 |
|
*/ |
130 |
|
public void testSubmitFailedPrivilegedExceptionAction() throws Exception { |
131 |
< |
Policy savedPolicy = null; |
132 |
< |
try { |
133 |
< |
savedPolicy = Policy.getPolicy(); |
134 |
< |
AdjustablePolicy policy = new AdjustablePolicy(); |
154 |
< |
policy.addPermission(new RuntimePermission("getContextClassLoader")); |
155 |
< |
policy.addPermission(new RuntimePermission("setContextClassLoader")); |
156 |
< |
Policy.setPolicy(policy); |
157 |
< |
} catch (AccessControlException ok) { |
158 |
< |
return; |
159 |
< |
} |
160 |
< |
|
161 |
< |
try { |
162 |
< |
ExecutorService e = new DirectExecutorService(); |
163 |
< |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
131 |
> |
Runnable r = new CheckedRunnable() { |
132 |
> |
public void realRun() throws Exception { |
133 |
> |
ExecutorService e = new DirectExecutorService(); |
134 |
> |
Future future = e.submit(Executors.callable(new PrivilegedExceptionAction() { |
135 |
|
public Object run() throws Exception { |
136 |
|
throw new IndexOutOfBoundsException(); |
137 |
|
}})); |
138 |
|
|
139 |
< |
future.get(); |
140 |
< |
shouldThrow(); |
141 |
< |
} catch (ExecutionException success) { |
142 |
< |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
143 |
< |
} |
144 |
< |
finally { |
145 |
< |
Policy.setPolicy(savedPolicy); |
146 |
< |
} |
139 |
> |
try { |
140 |
> |
future.get(); |
141 |
> |
shouldThrow(); |
142 |
> |
} catch (ExecutionException success) { |
143 |
> |
assertTrue(success.getCause() instanceof IndexOutOfBoundsException); |
144 |
> |
}}}; |
145 |
> |
|
146 |
> |
runWithPermissions(r); |
147 |
|
} |
148 |
|
|
149 |
|
/** |
217 |
|
|
218 |
|
|
219 |
|
/** |
220 |
< |
* Blocking on submit(callable) throws InterruptedException if |
250 |
< |
* caller interrupted. |
220 |
> |
* submit(callable).get() throws InterruptedException if interrupted |
221 |
|
*/ |
222 |
|
public void testInterruptedSubmit() throws InterruptedException { |
223 |
< |
final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(10)); |
224 |
< |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
225 |
< |
public void realRun() throws Exception { |
226 |
< |
p.submit(new CheckedCallable<Object>() { |
227 |
< |
public Object realCall() |
228 |
< |
throws InterruptedException { |
229 |
< |
Thread.sleep(SMALL_DELAY_MS); |
230 |
< |
return null; |
231 |
< |
}}).get(); |
232 |
< |
}}); |
233 |
< |
|
234 |
< |
t.start(); |
235 |
< |
Thread.sleep(SHORT_DELAY_MS); |
236 |
< |
t.interrupt(); |
237 |
< |
joinPool(p); |
223 |
> |
final CountDownLatch submitted = new CountDownLatch(1); |
224 |
> |
final CountDownLatch quittingTime = new CountDownLatch(1); |
225 |
> |
final ExecutorService p |
226 |
> |
= new ThreadPoolExecutor(1,1,60, TimeUnit.SECONDS, |
227 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
228 |
> |
final Callable<Void> awaiter = new CheckedCallable<Void>() { |
229 |
> |
public Void realCall() throws InterruptedException { |
230 |
> |
quittingTime.await(); |
231 |
> |
return null; |
232 |
> |
}}; |
233 |
> |
try { |
234 |
> |
Thread t = new Thread(new CheckedInterruptedRunnable() { |
235 |
> |
public void realRun() throws Exception { |
236 |
> |
Future<Void> future = p.submit(awaiter); |
237 |
> |
submitted.countDown(); |
238 |
> |
future.get(); |
239 |
> |
}}); |
240 |
> |
t.start(); |
241 |
> |
submitted.await(); |
242 |
> |
t.interrupt(); |
243 |
> |
t.join(); |
244 |
> |
} finally { |
245 |
> |
quittingTime.countDown(); |
246 |
> |
joinPool(p); |
247 |
> |
} |
248 |
|
} |
249 |
|
|
250 |
|
/** |