ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ExecutorsTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ExecutorsTest.java (file contents):
Revision 1.2 by dl, Sun Sep 7 20:39:11 2003 UTC vs.
Revision 1.8 by dl, Sat Nov 1 18:37:02 2003 UTC

# Line 10 | Line 10 | 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 ExecutorsTest extends TestCase{
15 <    
15 > public class ExecutorsTest extends JSR166TestCase{
16      public static void main(String[] args) {
17          junit.textui.TestRunner.run (suite());  
18      }
19    
20
19      public static Test suite() {
20          return new TestSuite(ExecutorsTest.class);
21      }
22  
23 <    private static long SHORT_DELAY_MS = 100;
24 <    private static long MEDIUM_DELAY_MS = 1000;
25 <    private static long LONG_DELAY_MS = 10000;
26 <
27 <    class SleepRun implements Runnable {
28 <        public void run() {
29 <            try{
30 <                Thread.sleep(MEDIUM_DELAY_MS);
31 <            } catch(InterruptedException e){
32 <                fail("unexpected exception");
23 >    private static final String TEST_STRING = "a test string";
24 >
25 >    private static class StringTask implements Callable<String> {
26 >        public String call() { return TEST_STRING; }
27 >    }
28 >
29 >    static class DirectExecutor implements Executor {
30 >        public void execute(Runnable r) {
31 >            r.run();
32 >        }
33 >    }
34 >
35 >    static class TimedCallable<T> implements Callable<T> {
36 >        private final Executor exec;
37 >        private final Callable<T> func;
38 >        private final long msecs;
39 >        
40 >        TimedCallable(Executor exec, Callable<T> func, long msecs) {
41 >            this.exec = exec;
42 >            this.func = func;
43 >            this.msecs = msecs;
44 >        }
45 >        
46 >        public T call() throws Exception {
47 >            Future<T> ftask = Executors.execute(exec, func);
48 >            try {
49 >                return ftask.get(msecs, TimeUnit.MILLISECONDS);
50 >            } finally {
51 >                ftask.cancel(true);
52              }
53          }
54      }
38    
55  
56 <    class SleepCall implements Callable {
57 <        public Object call(){
58 <            try{
59 <                Thread.sleep(MEDIUM_DELAY_MS);
60 <            }catch(InterruptedException e){
61 <                fail("unexpected exception");
56 >
57 >    private static class Fib implements Callable<BigInteger> {
58 >        private final BigInteger n;
59 >        Fib(long n) {
60 >            if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
61 >            this.n = BigInteger.valueOf(n);
62 >        }
63 >        public BigInteger call() {
64 >            BigInteger f1 = BigInteger.ONE;
65 >            BigInteger f2 = f1;
66 >            for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
67 >                BigInteger t = f1.add(f2);
68 >                f1 = f2;
69 >                f2 = t;
70              }
71 <            return Boolean.TRUE;
71 >            return f1;
72 >        }
73 >    };
74 >
75 >    /**
76 >     * A newCachedThreadPool can execute runnables
77 >     */
78 >    public void testNewCachedThreadPool1() {
79 >        ExecutorService e = Executors.newCachedThreadPool();
80 >        e.execute(new NoOpRunnable());
81 >        e.execute(new NoOpRunnable());
82 >        e.execute(new NoOpRunnable());
83 >        e.shutdown();
84 >    }
85 >
86 >    /**
87 >     * A newCachedThreadPool with given ThreadFactory can execute runnables
88 >     */
89 >    public void testNewCachedThreadPool2() {
90 >        ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory());
91 >        e.execute(new NoOpRunnable());
92 >        e.execute(new NoOpRunnable());
93 >        e.execute(new NoOpRunnable());
94 >        e.shutdown();
95 >    }
96 >
97 >    /**
98 >     * A newCachedThreadPool with null ThreadFactory throws NPE
99 >     */
100 >    public void testNewCachedThreadPool3() {
101 >        try {
102 >            ExecutorService e = Executors.newCachedThreadPool(null);
103 >            shouldThrow();
104 >        }
105 >        catch(NullPointerException success) {
106          }
107      }
108  
109  
110 +    /**
111 +     * A new SingleThreadExecutor can execute runnables
112 +     */
113 +    public void testNewSingleThreadExecutor1() {
114 +        ExecutorService e = Executors.newSingleThreadExecutor();
115 +        e.execute(new NoOpRunnable());
116 +        e.execute(new NoOpRunnable());
117 +        e.execute(new NoOpRunnable());
118 +        e.shutdown();
119 +    }
120 +
121 +    /**
122 +     * A new SingleThreadExecutor with given ThreadFactory can execute runnables
123 +     */
124 +    public void testNewSingleThreadExecutor2() {
125 +        ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory());
126 +        e.execute(new NoOpRunnable());
127 +        e.execute(new NoOpRunnable());
128 +        e.execute(new NoOpRunnable());
129 +        e.shutdown();
130 +    }
131 +
132 +    /**
133 +     * A new SingleThreadExecutor with null ThreadFactory throws NPE
134 +     */
135 +    public void testNewSingleThreadExecutor3() {
136 +        try {
137 +            ExecutorService e = Executors.newSingleThreadExecutor(null);
138 +            shouldThrow();
139 +        }
140 +        catch(NullPointerException success) {
141 +        }
142 +    }
143 +
144 +    /**
145 +     * A new newFixedThreadPool can execute runnables
146 +     */
147 +    public void testNewFixedThreadPool1() {
148 +        ExecutorService e = Executors.newFixedThreadPool(2);
149 +        e.execute(new NoOpRunnable());
150 +        e.execute(new NoOpRunnable());
151 +        e.execute(new NoOpRunnable());
152 +        e.shutdown();
153 +    }
154 +
155 +    /**
156 +     * A new newFixedThreadPool with given ThreadFactory can execute runnables
157 +     */
158 +    public void testNewFixedThreadPool2() {
159 +        ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory());
160 +        e.execute(new NoOpRunnable());
161 +        e.execute(new NoOpRunnable());
162 +        e.execute(new NoOpRunnable());
163 +        e.shutdown();
164 +    }
165 +
166 +    /**
167 +     * A new newFixedThreadPool with null ThreadFactory throws NPE
168 +     */
169 +    public void testNewFixedThreadPool3() {
170 +        try {
171 +            ExecutorService e = Executors.newFixedThreadPool(2, null);
172 +            shouldThrow();
173 +        }
174 +        catch(NullPointerException success) {
175 +        }
176 +    }
177 +
178 +    /**
179 +     * A new newFixedThreadPool with 0 threads throws IAE
180 +     */
181 +    public void testNewFixedThreadPool4() {
182 +        try {
183 +            ExecutorService e = Executors.newFixedThreadPool(0);
184 +            shouldThrow();
185 +        }
186 +        catch(IllegalArgumentException success) {
187 +        }
188 +    }
189 +
190 +    /**
191 +     * execute of runnable runs it to completion
192 +     */
193 +    public void testExecuteRunnable() {
194 +        try {
195 +            Executor e = new DirectExecutor();
196 +            TrackedShortRunnable task = new TrackedShortRunnable();
197 +            assertFalse(task.done);
198 +            Future<String> future = Executors.execute(e, task, TEST_STRING);
199 +            String result = future.get();
200 +            assertTrue(task.done);
201 +            assertSame(TEST_STRING, result);
202 +        }
203 +        catch (ExecutionException ex) {
204 +            unexpectedException();
205 +        }
206 +        catch (InterruptedException ex) {
207 +            unexpectedException();
208 +        }
209 +    }
210 +
211 +    /**
212 +     * invoke of a runnable runs it to completion
213 +     */
214 +    public void testInvokeRunnable() {
215 +        try {
216 +            Executor e = new DirectExecutor();
217 +            TrackedShortRunnable task = new TrackedShortRunnable();
218 +            assertFalse(task.done);
219 +            Executors.invoke(e, task);
220 +            assertTrue(task.done);
221 +        }
222 +        catch (ExecutionException ex) {
223 +            unexpectedException();
224 +        }
225 +        catch (InterruptedException ex) {
226 +            unexpectedException();
227 +        }
228 +    }
229 +
230 +    /**
231 +     * execute of a callable runs it to completion
232 +     */
233 +    public void testExecuteCallable() {
234 +        try {
235 +            Executor e = new DirectExecutor();
236 +            Future<String> future = Executors.execute(e, new StringTask());
237 +            String result = future.get();
238 +            assertSame(TEST_STRING, result);
239 +        }
240 +        catch (ExecutionException ex) {
241 +            unexpectedException();
242 +        }
243 +        catch (InterruptedException ex) {
244 +            unexpectedException();
245 +        }
246 +    }
247 +
248 +
249 +    /**
250 +     * execute of a privileged action runs it to completion
251 +     */
252 +    public void testExecutePrivilegedAction() {
253 +        Policy savedPolicy = Policy.getPolicy();
254 +        AdjustablePolicy policy = new AdjustablePolicy();
255 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
256 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
257 +        Policy.setPolicy(policy);
258 +        try {
259 +            Executor e = new DirectExecutor();
260 +            Future future = Executors.execute(e, new PrivilegedAction() {
261 +                    public Object run() {
262 +                        return TEST_STRING;
263 +                    }});
264 +
265 +            Object result = future.get();
266 +            assertSame(TEST_STRING, result);
267 +        }
268 +        catch (ExecutionException ex) {
269 +            unexpectedException();
270 +        }
271 +        catch (InterruptedException ex) {
272 +            unexpectedException();
273 +        }
274 +        finally {
275 +            Policy.setPolicy(savedPolicy);
276 +        }
277 +    }
278 +
279 +    /**
280 +     * execute of a privileged exception action runs it to completion
281 +     */
282 +    public void testExecutePrivilegedExceptionAction() {
283 +        Policy savedPolicy = Policy.getPolicy();
284 +        AdjustablePolicy policy = new AdjustablePolicy();
285 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
286 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
287 +        Policy.setPolicy(policy);
288 +        try {
289 +            Executor e = new DirectExecutor();
290 +            Future future = Executors.execute(e, new PrivilegedExceptionAction() {
291 +                    public Object run() {
292 +                        return TEST_STRING;
293 +                    }});
294 +
295 +            Object result = future.get();
296 +            assertSame(TEST_STRING, result);
297 +        }
298 +        catch (ExecutionException ex) {
299 +            unexpectedException();
300 +        }
301 +        catch (InterruptedException ex) {
302 +            unexpectedException();
303 +        }
304 +        finally {
305 +            Policy.setPolicy(savedPolicy);
306 +        }
307 +    }
308 +
309 +    /**
310 +     * execute of a failed privileged exception action reports exception
311 +     */
312 +    public void testExecuteFailedPrivilegedExceptionAction() {
313 +        Policy savedPolicy = Policy.getPolicy();
314 +        AdjustablePolicy policy = new AdjustablePolicy();
315 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
316 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
317 +        Policy.setPolicy(policy);
318 +        try {
319 +            Executor e = new DirectExecutor();
320 +            Future future = Executors.execute(e, new PrivilegedExceptionAction() {
321 +                    public Object run() throws Exception {
322 +                        throw new IndexOutOfBoundsException();
323 +                    }});
324 +
325 +            Object result = future.get();
326 +            shouldThrow();
327 +        }
328 +        catch (ExecutionException success) {
329 +        }
330 +        catch (InterruptedException ex) {
331 +            unexpectedException();
332 +        }
333 +        finally {
334 +            Policy.setPolicy(savedPolicy);
335 +        }
336 +    }
337  
338      /**
339 <     *  Test to verify execute(Executor, Runnable) will throw
55 <     *  RejectedExecutionException Attempting to execute a runnable on
56 <     *  a full ThreadPool will cause such an exception here, up to 5
57 <     *  runnables are attempted on a pool capable on handling one
58 <     *  until it throws an exception
339 >     * invoke of a collable runs it to completion
340       */
341 <    public void testExecute1(){
342 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
343 <        try{
341 >    public void testInvokeCallable() {
342 >        try {
343 >            Executor e = new DirectExecutor();
344 >            String result = Executors.invoke(e, new StringTask());
345 >
346 >            assertSame(TEST_STRING, result);
347 >        }
348 >        catch (ExecutionException ex) {
349 >            unexpectedException();
350 >        }
351 >        catch (InterruptedException ex) {
352 >            unexpectedException();
353 >        }
354 >    }
355 >
356 >    /**
357 >     * execute with null executor throws NPE
358 >     */
359 >    public void testNullExecuteRunnable() {
360 >        try {
361 >            TrackedShortRunnable task = new TrackedShortRunnable();
362 >            assertFalse(task.done);
363 >            Future<String> future = Executors.execute(null, task, TEST_STRING);
364 >            shouldThrow();
365 >        }
366 >        catch (NullPointerException success) {
367 >        }
368 >        catch (Exception ex) {
369 >            unexpectedException();
370 >        }
371 >    }
372 >
373 >    /**
374 >     * execute with a null runnable throws NPE
375 >     */
376 >    public void testExecuteNullRunnable() {
377 >        try {
378 >            Executor e = new DirectExecutor();
379 >            TrackedShortRunnable task = null;
380 >            Future<String> future = Executors.execute(e, task, TEST_STRING);
381 >            shouldThrow();
382 >        }
383 >        catch (NullPointerException success) {
384 >        }
385 >        catch (Exception ex) {
386 >            unexpectedException();
387 >        }
388 >    }
389 >
390 >    /**
391 >     * invoke of a null runnable throws NPE
392 >     */
393 >    public void testInvokeNullRunnable() {
394 >        try {
395 >            Executor e = new DirectExecutor();
396 >            TrackedShortRunnable task = null;
397 >            Executors.invoke(e, task);
398 >            shouldThrow();
399 >        }
400 >        catch (NullPointerException success) {
401 >        }
402 >        catch (Exception ex) {
403 >            unexpectedException();
404 >        }
405 >    }
406 >
407 >    /**
408 >     * execute of a null callable throws NPE
409 >     */
410 >    public void testExecuteNullCallable() {
411 >        try {
412 >            Executor e = new DirectExecutor();
413 >            StringTask t = null;
414 >            Future<String> future = Executors.execute(e, t);
415 >            shouldThrow();
416 >        }
417 >        catch (NullPointerException success) {
418 >        }
419 >        catch (Exception ex) {
420 >            unexpectedException();
421 >        }
422 >    }
423 >
424 >    /**
425 >     * invoke of a null callable throws NPE
426 >     */
427 >    public void testInvokeNullCallable() {
428 >        try {
429 >            Executor e = new DirectExecutor();
430 >            StringTask t = null;
431 >            String result = Executors.invoke(e, t);
432 >            shouldThrow();
433 >        }
434 >        catch (NullPointerException success) {
435 >        }
436 >        catch (Exception ex) {
437 >            unexpectedException();
438 >        }
439 >    }
440 >
441 >    /**
442 >     *  execute(Executor, Runnable) throws RejectedExecutionException
443 >     *  if saturated.
444 >     */
445 >    public void testExecute1() {
446 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
447 >        try {
448              
449              for(int i = 0; i < 5; ++i){
450 <                Executors.execute(p, new SleepRun(), Boolean.TRUE);
450 >                Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
451              }
452 <            fail("should throw");
452 >            shouldThrow();
453          } catch(RejectedExecutionException success){}
454 <        p.shutdownNow();
454 >        joinPool(p);
455      }
456  
457      /**
458 <     *  Test to verify execute(Executor, Callable) will throw
459 <     *  RejectedExecutionException Attempting to execute a callable on
75 <     *  a full ThreadPool will cause such an exception here, up to 5
76 <     *  runnables are attempted on a pool capable on handling one
77 <     *  until it throws an exception
458 >     *  execute(Executor, Callable)throws RejectedExecutionException
459 >     *  if saturated.
460       */
461 <    public void testExecute2(){
462 <         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
463 <        try{
461 >    public void testExecute2() {
462 >         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
463 >        try {
464              for(int i = 0; i < 5; ++i) {
465 <                Executors.execute(p, new SleepCall());
465 >                Executors.execute(p, new SmallCallable());
466              }
467 <            fail("should throw");
468 <        }catch(RejectedExecutionException e){}
469 <        p.shutdownNow();
467 >            shouldThrow();
468 >        } catch(RejectedExecutionException e){}
469 >        joinPool(p);
470      }
471  
472  
473      /**
474 <     *  Test to verify invoke(Executor, Runnable) throws InterruptedException
475 <     *  A single use of invoke starts that will wait long enough
94 <     *  for the invoking thread to be interrupted
474 >     *  invoke(Executor, Runnable) throws InterruptedException if
475 >     *  caller interrupted.
476       */
477 <    public void testInvoke2(){
478 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
477 >    public void testInterruptedInvoke() {
478 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
479          Thread t = new Thread(new Runnable() {
480 <                public void run(){
481 <                    try{
482 <                        Executors.invoke(p,new Runnable(){
483 <                                public void run(){
484 <                                    try{
480 >                public void run() {
481 >                    try {
482 >                        Executors.invoke(p,new Runnable() {
483 >                                public void run() {
484 >                                    try {
485                                          Thread.sleep(MEDIUM_DELAY_MS);
486 <                                        fail("should throw");
487 <                                    }catch(InterruptedException e){
486 >                                        shouldThrow();
487 >                                    } catch(InterruptedException e){
488                                      }
489                                  }
490                              });
491                      } catch(InterruptedException success){
492                      } catch(Exception e) {
493 <                        fail("unexpected exception");
493 >                        unexpectedException();
494                      }
495                      
496                  }
497              });
498 <        try{
498 >        try {
499              t.start();
500              Thread.sleep(SHORT_DELAY_MS);
501              t.interrupt();
502 <        }catch(Exception e){
503 <            fail("unexpected exception");
502 >        } catch(Exception e){
503 >            unexpectedException();
504          }
505 <        p.shutdownNow();
505 >        joinPool(p);
506      }
507  
508      /**
509 <     *  Test to verify invoke(Executor, Runnable) will throw
510 <     *  ExecutionException An ExecutionException occurs when the
130 <     *  underlying Runnable throws an exception, here the
131 <     *  DivideByZeroException will cause an ExecutionException
509 >     *  invoke(Executor, Runnable) throws ExecutionException if
510 >     *  runnable throws exception.
511       */
512 <    public void testInvoke3(){
513 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
514 <        try{
515 <            Runnable r = new Runnable(){
516 <                    public void run(){
512 >    public void testInvoke3() {
513 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
514 >        try {
515 >            Runnable r = new Runnable() {
516 >                    public void run() {
517                          int i = 5/0;
518                      }
519                  };
# Line 143 | Line 522 | public class ExecutorsTest extends TestC
522                  Executors.invoke(p,r);
523              }
524              
525 <            fail("should throw");
525 >            shouldThrow();
526          } catch(ExecutionException success){
527          } catch(Exception e){
528 <            fail("should throw EE");
528 >            unexpectedException();
529          }
530 <        p.shutdownNow();
530 >        joinPool(p);
531      }
532  
533  
534  
535      /**
536 <     *  Test to verify invoke(Executor, Callable) throws
537 <     *  InterruptedException A single use of invoke starts that will
159 <     *  wait long enough for the invoking thread to be interrupted
536 >     *  invoke(Executor, Callable) throws InterruptedException if
537 >     *  callable throws exception
538       */
539 <    public void testInvoke5(){
540 <        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
539 >    public void testInvoke5() {
540 >        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
541          
542 <        final Callable c = new Callable(){
543 <                public Object call(){
544 <                    try{
545 <                        Executors.invoke(p, new SleepCall());
546 <                        fail("should throw");
547 <                    }catch(InterruptedException e){}
542 >        final Callable c = new Callable() {
543 >                public Object call() {
544 >                    try {
545 >                        Executors.invoke(p, new SmallCallable());
546 >                        shouldThrow();
547 >                    } catch(InterruptedException e){}
548                      catch(RejectedExecutionException e2){}
549                      catch(ExecutionException e3){}
550                      return Boolean.TRUE;
# Line 175 | Line 553 | public class ExecutorsTest extends TestC
553  
554  
555          
556 <        Thread t = new Thread(new Runnable(){
557 <                public void run(){
558 <                    try{
556 >        Thread t = new Thread(new Runnable() {
557 >                public void run() {
558 >                    try {
559                          c.call();
560 <                    }catch(Exception e){}
560 >                    } catch(Exception e){}
561                  }
562            });
563 <        try{
563 >        try {
564              t.start();
565              Thread.sleep(SHORT_DELAY_MS);
566              t.interrupt();
567              t.join();
568 <        }catch(InterruptedException e){
569 <            fail("unexpected exception");
568 >        } catch(InterruptedException e){
569 >            unexpectedException();
570          }
571          
572 <        p.shutdownNow();
572 >        joinPool(p);
573      }
574  
575      /**
576 <     *  Test to verify invoke(Executor, Callable) will throw ExecutionException
577 <     *  An ExecutionException occurs when the underlying Runnable throws
200 <     *  an exception, here the DivideByZeroException will cause an ExecutionException
576 >     *  invoke(Executor, Callable) will throw ExecutionException
577 >     *  if callable throws exception
578       */
579 <    public void testInvoke6(){
580 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,100L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
579 >    public void testInvoke6() {
580 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
581  
582 <        try{
583 <            Callable c = new Callable(){
584 <                    public Object call(){
582 >        try {
583 >            Callable c = new Callable() {
584 >                    public Object call() {
585                          int i = 5/0;
586                          return Boolean.TRUE;
587                      }
# Line 214 | Line 591 | public class ExecutorsTest extends TestC
591                  Executors.invoke(p,c);
592              }
593              
594 <            fail("should throw");
595 <        }catch(RejectedExecutionException e){}
596 <        catch(InterruptedException e2){}
597 <        catch(ExecutionException e3){}
598 <        p.shutdownNow();
222 <    }
223 <
224 <    public void testExecuteRunnable () {
225 <        try {
226 <            Executor e = new DirectExecutor();
227 <            Task task = new Task();
228 <
229 <            assertFalse("task should not be complete", task.isCompleted());
230 <
231 <            Future<String> future = Executors.execute(e, task, TEST_STRING);
232 <            String result = future.get();
233 <
234 <            assertTrue("task should be complete", task.isCompleted());
235 <            assertSame("should return test string", TEST_STRING, result);
236 <        }
237 <        catch (ExecutionException ex) {
238 <            fail("Unexpected exception");
239 <        }
240 <        catch (InterruptedException ex) {
241 <            fail("Unexpected exception");
242 <        }
243 <    }
244 <
245 <    public void testInvokeRunnable () {
246 <        try {
247 <            Executor e = new DirectExecutor();
248 <            Task task = new Task();
249 <
250 <            assertFalse("task should not be complete", task.isCompleted());
251 <
252 <            Executors.invoke(e, task);
253 <
254 <            assertTrue("task should be complete", task.isCompleted());
255 <        }
256 <        catch (ExecutionException ex) {
257 <            fail("Unexpected exception");
258 <        }
259 <        catch (InterruptedException ex) {
260 <            fail("Unexpected exception");
594 >            shouldThrow();
595 >        }
596 >        catch(ExecutionException success){
597 >        } catch(Exception e) {
598 >            unexpectedException();
599          }
600 +        joinPool(p);
601      }
602  
264    public void testExecuteCallable () {
265        try {
266            Executor e = new DirectExecutor();
267            Future<String> future = Executors.execute(e, new StringTask());
268            String result = future.get();
269
270            assertSame("should return test string", TEST_STRING, result);
271        }
272        catch (ExecutionException ex) {
273            fail("Unexpected exception");
274        }
275        catch (InterruptedException ex) {
276            fail("Unexpected exception");
277        }
278    }
603  
280    public void testInvokeCallable () {
281        try {
282            Executor e = new DirectExecutor();
283            String result = Executors.invoke(e, new StringTask());
284
285            assertSame("should return test string", TEST_STRING, result);
286        }
287        catch (ExecutionException ex) {
288            fail("Unexpected exception" );
289        }
290        catch (InterruptedException ex) {
291            fail("Unexpected exception");
292        }
293    }
294
295    private static final String TEST_STRING = "a test string";
296
297    private static class Task implements Runnable {
298        public void run() { completed = true; }
299        public boolean isCompleted() { return completed; }
300        public void reset() { completed = false; }
301        private boolean completed = false;
302    }
303
304    private static class StringTask implements Callable<String> {
305        public String call() { return TEST_STRING; }
306    }
307
308    static class DirectExecutor implements Executor {
309        public void execute(Runnable r) {
310            r.run();
311        }
312    }
604  
605      /**
606 <     * Check that timeouts from execute will time out if they compute
316 <     * too long.
606 >     *  timeouts from execute will time out if they compute too long.
607       */
318
608      public void testTimedCallable() {
609          int N = 10000;
610          ExecutorService executor = Executors.newSingleThreadExecutor();
# Line 341 | Line 630 | public class ExecutorsTest extends TestC
630                      return;
631                  }
632                  catch (Exception e) {
633 <                    fail("unexpected exception: " + e);
633 >                    unexpectedException();
634                  }
635              }
636              // if by chance we didn't ever time out, total time must be small
# Line 349 | Line 638 | public class ExecutorsTest extends TestC
638              assertTrue(elapsed < N);
639          }
640          finally {
641 <            executor.shutdownNow();
641 >            joinPool(executor);
642          }
643      }
644  
645      
646 <    static class TimedCallable<T> implements Callable<T> {
647 <        private final Executor exec;
648 <        private final Callable<T> func;
649 <        private final long msecs;
650 <        
651 <        TimedCallable(Executor exec, Callable<T> func, long msecs) {
652 <            this.exec = exec;
653 <            this.func = func;
654 <            this.msecs = msecs;
655 <        }
656 <        
657 <        public T call() throws Exception {
658 <            Future<T> ftask = Executors.execute(exec, func);
659 <            try {
660 <                return ftask.get(msecs, TimeUnit.MILLISECONDS);
661 <            } finally {
662 <                ftask.cancel(true);
663 <            }
664 <        }
646 >    /**
647 >     * ThreadPoolExecutor using defaultThreadFactory has
648 >     * specified group, priority, daemon status, and name
649 >     */
650 >    public void testDefaultThreadFactory() {
651 >        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
652 >        Runnable r = new Runnable() {
653 >                public void run() {
654 >                    Thread current = Thread.currentThread();
655 >                    threadAssertTrue(!current.isDaemon());
656 >                    threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
657 >                    ThreadGroup g = current.getThreadGroup();
658 >                    SecurityManager s = System.getSecurityManager();
659 >                    if (s != null)
660 >                        threadAssertTrue(g == s.getThreadGroup());
661 >                    else
662 >                        threadAssertTrue(g == egroup);
663 >                    String name = current.getName();
664 >                    threadAssertTrue(name.endsWith("thread-1"));
665 >                }
666 >            };
667 >        ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
668 >        
669 >        e.execute(r);
670 >        e.shutdown();
671 >        try {
672 >            Thread.sleep(SHORT_DELAY_MS);
673 >        } catch (Exception eX) {
674 >            unexpectedException();
675 >        } finally {
676 >            joinPool(e);
677 >        }
678      }
679  
680 +    /**
681 +     * ThreadPoolExecutor using privilegedThreadFactory has
682 +     * specified group, priority, daemon status, name,
683 +     * access control context and context class loader
684 +     */
685 +    public void testPrivilegedThreadFactory() {
686 +        Policy savedPolicy = Policy.getPolicy();
687 +        AdjustablePolicy policy = new AdjustablePolicy();
688 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
689 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
690 +        Policy.setPolicy(policy);
691 +        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
692 +        final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
693 +        final AccessControlContext thisacc = AccessController.getContext();
694 +        Runnable r = new Runnable() {
695 +                public void run() {
696 +                    Thread current = Thread.currentThread();
697 +                    threadAssertTrue(!current.isDaemon());
698 +                    threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
699 +                    ThreadGroup g = current.getThreadGroup();
700 +                    SecurityManager s = System.getSecurityManager();
701 +                    if (s != null)
702 +                        threadAssertTrue(g == s.getThreadGroup());
703 +                    else
704 +                        threadAssertTrue(g == egroup);
705 +                    String name = current.getName();
706 +                    threadAssertTrue(name.endsWith("thread-1"));
707 +                    threadAssertTrue(thisccl == current.getContextClassLoader());
708 +                    threadAssertTrue(thisacc.equals(AccessController.getContext()));
709 +                }
710 +            };
711 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
712 +        
713 +        Policy.setPolicy(savedPolicy);
714 +        e.execute(r);
715 +        e.shutdown();
716 +        try {
717 +            Thread.sleep(SHORT_DELAY_MS);
718 +        } catch (Exception ex) {
719 +            unexpectedException();
720 +        } finally {
721 +            joinPool(e);
722 +        }
723  
724 <    private static class Fib implements Callable<BigInteger> {
380 <        private final BigInteger n;
381 <        Fib(long n) {
382 <            if (n < 0) throw new IllegalArgumentException("need non-negative arg, but got " + n);
383 <            this.n = BigInteger.valueOf(n);
384 <        }
385 <        public BigInteger call() {
386 <            BigInteger f1 = BigInteger.ONE;
387 <            BigInteger f2 = f1;
388 <            for (BigInteger i = BigInteger.ZERO; i.compareTo(n) < 0; i = i.add(BigInteger.ONE)) {
389 <                BigInteger t = f1.add(f2);
390 <                f1 = f2;
391 <                f2 = t;
392 <            }
393 <            return f1;
394 <        }
395 <    };
396 <
397 <
724 >    }
725  
726   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines