ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/AbstractExecutorServiceTest.java
Revision: 1.2
Committed: Wed Dec 10 02:24:00 2003 UTC (20 years, 5 months ago) by tim
Branch: MAIN
Changes since 1.1: +1 -1 lines
Log Message:
Raw List returned by shutdownNow is really List<Runnable>.

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 * invoke of a runnable runs it to completion
65 */
66 public void testInvokeRunnable() {
67 try {
68 ExecutorService e = new DirectExecutorService();
69 TrackedShortRunnable task = new TrackedShortRunnable();
70 assertFalse(task.done);
71 e.invoke(task);
72 assertTrue(task.done);
73 }
74 catch (ExecutionException ex) {
75 unexpectedException();
76 }
77 catch (InterruptedException ex) {
78 unexpectedException();
79 }
80 }
81
82 /**
83 * execute of a callable runs it to completion
84 */
85 public void testExecuteCallable() {
86 try {
87 ExecutorService e = new DirectExecutorService();
88 Future<String> future = e.submit(new StringTask());
89 String result = future.get();
90 assertSame(TEST_STRING, result);
91 }
92 catch (ExecutionException ex) {
93 unexpectedException();
94 }
95 catch (InterruptedException ex) {
96 unexpectedException();
97 }
98 }
99
100
101 /**
102 * execute of a privileged action runs it to completion
103 */
104 public void testExecutePrivilegedAction() {
105 Policy savedPolicy = Policy.getPolicy();
106 AdjustablePolicy policy = new AdjustablePolicy();
107 policy.addPermission(new RuntimePermission("getContextClassLoader"));
108 policy.addPermission(new RuntimePermission("setContextClassLoader"));
109 Policy.setPolicy(policy);
110 try {
111 ExecutorService e = new DirectExecutorService();
112 Future future = e.submit(new PrivilegedAction() {
113 public Object run() {
114 return TEST_STRING;
115 }});
116
117 Object result = future.get();
118 assertSame(TEST_STRING, result);
119 }
120 catch (ExecutionException ex) {
121 unexpectedException();
122 }
123 catch (InterruptedException ex) {
124 unexpectedException();
125 }
126 finally {
127 Policy.setPolicy(savedPolicy);
128 }
129 }
130
131 /**
132 * execute of a privileged exception action runs it to completion
133 */
134 public void testExecutePrivilegedExceptionAction() {
135 Policy savedPolicy = Policy.getPolicy();
136 AdjustablePolicy policy = new AdjustablePolicy();
137 policy.addPermission(new RuntimePermission("getContextClassLoader"));
138 policy.addPermission(new RuntimePermission("setContextClassLoader"));
139 Policy.setPolicy(policy);
140 try {
141 ExecutorService e = new DirectExecutorService();
142 Future future = e.submit(new PrivilegedExceptionAction() {
143 public Object run() {
144 return TEST_STRING;
145 }});
146
147 Object result = future.get();
148 assertSame(TEST_STRING, result);
149 }
150 catch (ExecutionException ex) {
151 unexpectedException();
152 }
153 catch (InterruptedException ex) {
154 unexpectedException();
155 }
156 finally {
157 Policy.setPolicy(savedPolicy);
158 }
159 }
160
161 /**
162 * execute of a failed privileged exception action reports exception
163 */
164 public void testExecuteFailedPrivilegedExceptionAction() {
165 Policy savedPolicy = Policy.getPolicy();
166 AdjustablePolicy policy = new AdjustablePolicy();
167 policy.addPermission(new RuntimePermission("getContextClassLoader"));
168 policy.addPermission(new RuntimePermission("setContextClassLoader"));
169 Policy.setPolicy(policy);
170 try {
171 ExecutorService e = new DirectExecutorService();
172 Future future = e.submit(new PrivilegedExceptionAction() {
173 public Object run() throws Exception {
174 throw new IndexOutOfBoundsException();
175 }});
176
177 Object result = future.get();
178 shouldThrow();
179 }
180 catch (ExecutionException success) {
181 }
182 catch (InterruptedException ex) {
183 unexpectedException();
184 }
185 finally {
186 Policy.setPolicy(savedPolicy);
187 }
188 }
189
190 /**
191 * invoke of a collable runs it to completion
192 */
193 public void testInvokeCallable() {
194 try {
195 ExecutorService e = new DirectExecutorService();
196 String result = e.invoke(new StringTask());
197
198 assertSame(TEST_STRING, result);
199 }
200 catch (ExecutionException ex) {
201 unexpectedException();
202 }
203 catch (InterruptedException ex) {
204 unexpectedException();
205 }
206 }
207
208 /**
209 * execute with a null runnable throws NPE
210 */
211 public void testExecuteNullRunnable() {
212 try {
213 ExecutorService e = new DirectExecutorService();
214 TrackedShortRunnable task = null;
215 Future<?> future = e.submit(task);
216 shouldThrow();
217 }
218 catch (NullPointerException success) {
219 }
220 catch (Exception ex) {
221 unexpectedException();
222 }
223 }
224
225 /**
226 * invoke of a null runnable throws NPE
227 */
228 public void testInvokeNullRunnable() {
229 try {
230 ExecutorService e = new DirectExecutorService();
231 TrackedShortRunnable task = null;
232 e.invoke(task);
233 shouldThrow();
234 }
235 catch (NullPointerException success) {
236 }
237 catch (Exception ex) {
238 unexpectedException();
239 }
240 }
241
242 /**
243 * execute of a null callable throws NPE
244 */
245 public void testExecuteNullCallable() {
246 try {
247 ExecutorService e = new DirectExecutorService();
248 StringTask t = null;
249 Future<String> future = e.submit(t);
250 shouldThrow();
251 }
252 catch (NullPointerException success) {
253 }
254 catch (Exception ex) {
255 unexpectedException();
256 }
257 }
258
259 /**
260 * invoke of a null callable throws NPE
261 */
262 public void testInvokeNullCallable() {
263 try {
264 ExecutorService e = new DirectExecutorService();
265 StringTask t = null;
266 String result = e.invoke(t);
267 shouldThrow();
268 }
269 catch (NullPointerException success) {
270 }
271 catch (Exception ex) {
272 unexpectedException();
273 }
274 }
275
276 /**
277 * execute(Executor, Runnable) throws RejectedExecutionException
278 * if saturated.
279 */
280 public void testExecute1() {
281 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
282 try {
283
284 for(int i = 0; i < 5; ++i){
285 p.submit(new MediumRunnable());
286 }
287 shouldThrow();
288 } catch(RejectedExecutionException success){}
289 joinPool(p);
290 }
291
292 /**
293 * execute(Executor, Callable)throws RejectedExecutionException
294 * if saturated.
295 */
296 public void testExecute2() {
297 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
298 try {
299 for(int i = 0; i < 5; ++i) {
300 p.submit(new SmallCallable());
301 }
302 shouldThrow();
303 } catch(RejectedExecutionException e){}
304 joinPool(p);
305 }
306
307
308 /**
309 * invoke(Executor, Runnable) throws InterruptedException if
310 * caller interrupted.
311 */
312 public void testInterruptedInvoke() {
313 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
314 Thread t = new Thread(new Runnable() {
315 public void run() {
316 try {
317 p.invoke(new Runnable() {
318 public void run() {
319 try {
320 Thread.sleep(MEDIUM_DELAY_MS);
321 shouldThrow();
322 } catch(InterruptedException e){
323 }
324 }
325 });
326 } catch(InterruptedException success){
327 } catch(Exception e) {
328 unexpectedException();
329 }
330
331 }
332 });
333 try {
334 t.start();
335 Thread.sleep(SHORT_DELAY_MS);
336 t.interrupt();
337 } catch(Exception e){
338 unexpectedException();
339 }
340 joinPool(p);
341 }
342
343 /**
344 * invoke(Executor, Runnable) throws ExecutionException if
345 * runnable throws exception.
346 */
347 public void testInvoke3() {
348 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
349 try {
350 Runnable r = new Runnable() {
351 public void run() {
352 int i = 5/0;
353 }
354 };
355
356 for(int i =0; i < 5; i++){
357 p.invoke(r);
358 }
359
360 shouldThrow();
361 } catch(ExecutionException success){
362 } catch(Exception e){
363 unexpectedException();
364 }
365 joinPool(p);
366 }
367
368
369
370 /**
371 * invoke(Executor, Callable) throws InterruptedException if
372 * callable throws exception
373 */
374 public void testInvoke5() {
375 final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
376
377 final Callable c = new Callable() {
378 public Object call() {
379 try {
380 p.invoke(new SmallCallable());
381 shouldThrow();
382 } catch(InterruptedException e){}
383 catch(RejectedExecutionException e2){}
384 catch(ExecutionException e3){}
385 return Boolean.TRUE;
386 }
387 };
388
389
390
391 Thread t = new Thread(new Runnable() {
392 public void run() {
393 try {
394 c.call();
395 } catch(Exception e){}
396 }
397 });
398 try {
399 t.start();
400 Thread.sleep(SHORT_DELAY_MS);
401 t.interrupt();
402 t.join();
403 } catch(InterruptedException e){
404 unexpectedException();
405 }
406
407 joinPool(p);
408 }
409
410 /**
411 * invoke(Executor, Callable) will throw ExecutionException
412 * if callable throws exception
413 */
414 public void testInvoke6() {
415 ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
416
417 try {
418 Callable c = new Callable() {
419 public Object call() {
420 int i = 5/0;
421 return Boolean.TRUE;
422 }
423 };
424
425 for(int i =0; i < 5; i++){
426 p.invoke(c);
427 }
428
429 shouldThrow();
430 }
431 catch(ExecutionException success){
432 } catch(Exception e) {
433 unexpectedException();
434 }
435 joinPool(p);
436 }
437
438 }