ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
Revision: 1.4
Committed: Fri Dec 19 20:38:55 2003 UTC (20 years, 5 months ago) by dl
Branch: MAIN
Changes since 1.3: +8 -68 lines
Log Message:
Adjust to API changes

File Contents

# Content
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.
6 */
7
8
9 import junit.framework.*;
10 import java.util.*;
11 import java.util.concurrent.*;
12 import java.math.BigInteger;
13 import java.security.*;
14
15 public class AbstractExecutorServiceTest extends JSR166TestCase{
16 public static void main(String[] args) {
17 junit.textui.TestRunner.run (suite());
18 }
19 public static Test suite() {
20 return new TestSuite(ExecutorsTest.class);
21 }
22
23 /**
24 * A no-frills implementation of AbstractExecutorService, designed
25 * to test the submit/invoke methods only.
26 */
27 static class DirectExecutorService extends AbstractExecutorService {
28 public void execute(Runnable r) { r.run(); }
29 public void shutdown() { shutdown = true; }
30 public List<Runnable> shutdownNow() { shutdown = true; return Collections.EMPTY_LIST; }
31 public boolean isShutdown() { return shutdown; }
32 public boolean isTerminated() { return isShutdown(); }
33 public boolean awaitTermination(long timeout, TimeUnit unit) { return isShutdown(); }
34 private volatile boolean shutdown = false;
35 }
36
37 private static final String TEST_STRING = "a test string";
38
39 private static class StringTask implements Callable<String> {
40 public String call() { return TEST_STRING; }
41 }
42
43 /**
44 * execute of runnable runs it to completion
45 */
46 public void testExecuteRunnable() {
47 try {
48 ExecutorService e = new DirectExecutorService();
49 TrackedShortRunnable task = new TrackedShortRunnable();
50 assertFalse(task.done);
51 Future<?> future = e.submit(task);
52 future.get();
53 assertTrue(task.done);
54 }
55 catch (ExecutionException ex) {
56 unexpectedException();
57 }
58 catch (InterruptedException ex) {
59 unexpectedException();
60 }
61 }
62
63
64 /**
65 * execute of a callable runs it to completion
66 */
67 public void testExecuteCallable() {
68 try {
69 ExecutorService e = new DirectExecutorService();
70 Future<String> future = e.submit(new StringTask());
71 String result = future.get();
72 assertSame(TEST_STRING, result);
73 }
74 catch (ExecutionException ex) {
75 unexpectedException();
76 }
77 catch (InterruptedException ex) {
78 unexpectedException();
79 }
80 }
81
82
83 /**
84 * execute of a privileged action runs it to completion
85 */
86 public void testExecutePrivilegedAction() {
87 Policy savedPolicy = Policy.getPolicy();
88 AdjustablePolicy policy = new AdjustablePolicy();
89 policy.addPermission(new RuntimePermission("getContextClassLoader"));
90 policy.addPermission(new RuntimePermission("setContextClassLoader"));
91 Policy.setPolicy(policy);
92 try {
93 ExecutorService e = new DirectExecutorService();
94 Future future = e.submit(new PrivilegedAction() {
95 public Object run() {
96 return TEST_STRING;
97 }});
98
99 Object result = future.get();
100 assertSame(TEST_STRING, result);
101 }
102 catch (ExecutionException ex) {
103 unexpectedException();
104 }
105 catch (InterruptedException ex) {
106 unexpectedException();
107 }
108 finally {
109 Policy.setPolicy(savedPolicy);
110 }
111 }
112
113 /**
114 * execute of a privileged exception action runs it to completion
115 */
116 public void testExecutePrivilegedExceptionAction() {
117 Policy savedPolicy = Policy.getPolicy();
118 AdjustablePolicy policy = new AdjustablePolicy();
119 policy.addPermission(new RuntimePermission("getContextClassLoader"));
120 policy.addPermission(new RuntimePermission("setContextClassLoader"));
121 Policy.setPolicy(policy);
122 try {
123 ExecutorService e = new DirectExecutorService();
124 Future future = e.submit(new PrivilegedExceptionAction() {
125 public Object run() {
126 return TEST_STRING;
127 }});
128
129 Object result = future.get();
130 assertSame(TEST_STRING, result);
131 }
132 catch (ExecutionException ex) {
133 unexpectedException();
134 }
135 catch (InterruptedException ex) {
136 unexpectedException();
137 }
138 finally {
139 Policy.setPolicy(savedPolicy);
140 }
141 }
142
143 /**
144 * execute of a failed privileged exception action reports exception
145 */
146 public void testExecuteFailedPrivilegedExceptionAction() {
147 Policy savedPolicy = Policy.getPolicy();
148 AdjustablePolicy policy = new AdjustablePolicy();
149 policy.addPermission(new RuntimePermission("getContextClassLoader"));
150 policy.addPermission(new RuntimePermission("setContextClassLoader"));
151 Policy.setPolicy(policy);
152 try {
153 ExecutorService e = new DirectExecutorService();
154 Future future = e.submit(new PrivilegedExceptionAction() {
155 public Object run() throws Exception {
156 throw new IndexOutOfBoundsException();
157 }});
158
159 Object result = future.get();
160 shouldThrow();
161 }
162 catch (ExecutionException success) {
163 }
164 catch (InterruptedException ex) {
165 unexpectedException();
166 }
167 finally {
168 Policy.setPolicy(savedPolicy);
169 }
170 }
171
172 /**
173 * invoke of a callable runs it to completion
174 */
175 public void testInvokeCallable() {
176 try {
177 ExecutorService e = new DirectExecutorService();
178 String result = e.invoke(new StringTask());
179
180 assertSame(TEST_STRING, result);
181 }
182 catch (ExecutionException ex) {
183 unexpectedException();
184 }
185 catch (InterruptedException ex) {
186 unexpectedException();
187 }
188 }
189
190 /**
191 * execute with a null runnable throws NPE
192 */
193 public void testExecuteNullRunnable() {
194 try {
195 ExecutorService e = new DirectExecutorService();
196 TrackedShortRunnable task = null;
197 Future<?> future = e.submit(task);
198 shouldThrow();
199 }
200 catch (NullPointerException success) {
201 }
202 catch (Exception ex) {
203 unexpectedException();
204 }
205 }
206
207
208 /**
209 * execute of a null callable throws NPE
210 */
211 public void testExecuteNullCallable() {
212 try {
213 ExecutorService e = new DirectExecutorService();
214 StringTask t = null;
215 Future<String> future = e.submit(t);
216 shouldThrow();
217 }
218 catch (NullPointerException success) {
219 }
220 catch (Exception ex) {
221 unexpectedException();
222 }
223 }
224
225 /**
226 * invoke of a null callable throws NPE
227 */
228 public void testInvokeNullCallable() {
229 try {
230 ExecutorService e = new DirectExecutorService();
231 StringTask t = null;
232 String result = e.invoke(t);
233 shouldThrow();
234 }
235 catch (NullPointerException success) {
236 }
237 catch (Exception ex) {
238 unexpectedException();
239 }
240 }
241
242 /**
243 * execute(Executor, Runnable) throws RejectedExecutionException
244 * if saturated.
245 */
246 public void testExecute1() {
247 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
248 try {
249
250 for(int i = 0; i < 5; ++i){
251 p.submit(new MediumRunnable());
252 }
253 shouldThrow();
254 } catch(RejectedExecutionException success){}
255 joinPool(p);
256 }
257
258 /**
259 * execute(Executor, Callable)throws RejectedExecutionException
260 * if saturated.
261 */
262 public void testExecute2() {
263 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
264 try {
265 for(int i = 0; i < 5; ++i) {
266 p.submit(new SmallCallable());
267 }
268 shouldThrow();
269 } catch(RejectedExecutionException e){}
270 joinPool(p);
271 }
272
273
274 /**
275 * invoke(Executor, Callable) throws InterruptedException if
276 * caller interrupted.
277 */
278 public void testInterruptedInvoke() {
279 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
280 Thread t = new Thread(new Runnable() {
281 public void run() {
282 try {
283 p.invoke(new Callable<Object>() {
284 public Object call() {
285 try {
286 Thread.sleep(MEDIUM_DELAY_MS);
287 shouldThrow();
288 } catch(InterruptedException e){
289 }
290 return null;
291 }
292 });
293 } catch(InterruptedException success){
294 } catch(Exception e) {
295 unexpectedException();
296 }
297
298 }
299 });
300 try {
301 t.start();
302 Thread.sleep(SHORT_DELAY_MS);
303 t.interrupt();
304 } catch(Exception e){
305 unexpectedException();
306 }
307 joinPool(p);
308 }
309
310 /**
311 * invoke(Executor, Callable) throws InterruptedException if
312 * callable throws exception
313 */
314 public void testInvoke5() {
315 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
316
317 final Callable c = new Callable() {
318 public Object call() {
319 try {
320 p.invoke(new SmallCallable());
321 shouldThrow();
322 } catch(InterruptedException e){}
323 catch(RejectedExecutionException e2){}
324 catch(ExecutionException e3){}
325 return Boolean.TRUE;
326 }
327 };
328
329
330
331 Thread t = new Thread(new Runnable() {
332 public void run() {
333 try {
334 c.call();
335 } catch(Exception e){}
336 }
337 });
338 try {
339 t.start();
340 Thread.sleep(SHORT_DELAY_MS);
341 t.interrupt();
342 t.join();
343 } catch(InterruptedException e){
344 unexpectedException();
345 }
346
347 joinPool(p);
348 }
349
350 /**
351 * invoke(Executor, Callable) will throw ExecutionException
352 * if callable throws exception
353 */
354 public void testInvoke6() {
355 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
356
357 try {
358 Callable c = new Callable() {
359 public Object call() {
360 int i = 5/0;
361 return Boolean.TRUE;
362 }
363 };
364
365 for(int i =0; i < 5; i++){
366 p.invoke(c);
367 }
368
369 shouldThrow();
370 }
371 catch(ExecutionException success){
372 } catch(Exception e) {
373 unexpectedException();
374 }
375 joinPool(p);
376 }
377
378 }