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.5 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.10 by tim, Wed Dec 10 01:51:12 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 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 <        }
20 >        return new TestSuite(ExecutorsTest.class);
21      }
22  
23      static class TimedCallable<T> implements Callable<T> {
24 <        private final Executor exec;
24 >        private final ExecutorService exec;
25          private final Callable<T> func;
26          private final long msecs;
27          
28 <        TimedCallable(Executor exec, Callable<T> func, long msecs) {
28 >        TimedCallable(ExecutorService exec, Callable<T> func, long msecs) {
29              this.exec = exec;
30              this.func = func;
31              this.msecs = msecs;
32          }
33          
34          public T call() throws Exception {
35 <            Future<T> ftask = Executors.execute(exec, func);
35 >            Future<T> ftask = exec.submit(func);
36              try {
37                  return ftask.get(msecs, TimeUnit.MILLISECONDS);
38              } finally {
# Line 79 | Line 61 | public class ExecutorsTest extends JSR16
61      };
62  
63      /**
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        }  
88    }
89
90    /**
64       * A newCachedThreadPool can execute runnables
65       */
66      public void testNewCachedThreadPool1() {
# Line 102 | Line 75 | public class ExecutorsTest extends JSR16
75       * A newCachedThreadPool with given ThreadFactory can execute runnables
76       */
77      public void testNewCachedThreadPool2() {
78 <        ExecutorService e = Executors.newCachedThreadPool(new MyThreadFactory());
78 >        ExecutorService e = Executors.newCachedThreadPool(new SimpleThreadFactory());
79          e.execute(new NoOpRunnable());
80          e.execute(new NoOpRunnable());
81          e.execute(new NoOpRunnable());
# Line 137 | Line 110 | public class ExecutorsTest extends JSR16
110       * A new SingleThreadExecutor with given ThreadFactory can execute runnables
111       */
112      public void testNewSingleThreadExecutor2() {
113 <        ExecutorService e = Executors.newSingleThreadExecutor(new MyThreadFactory());
113 >        ExecutorService e = Executors.newSingleThreadExecutor(new SimpleThreadFactory());
114          e.execute(new NoOpRunnable());
115          e.execute(new NoOpRunnable());
116          e.execute(new NoOpRunnable());
# Line 171 | Line 144 | public class ExecutorsTest extends JSR16
144       * A new newFixedThreadPool with given ThreadFactory can execute runnables
145       */
146      public void testNewFixedThreadPool2() {
147 <        ExecutorService e = Executors.newFixedThreadPool(2, new MyThreadFactory());
147 >        ExecutorService e = Executors.newFixedThreadPool(2, new SimpleThreadFactory());
148          e.execute(new NoOpRunnable());
149          e.execute(new NoOpRunnable());
150          e.execute(new NoOpRunnable());
# Line 202 | Line 175 | public class ExecutorsTest extends JSR16
175          }
176      }
177  
205    /**
206     * execute of runnable runs it to completion
207     */
208    public void testExecuteRunnable () {
209        try {
210            Executor e = new DirectExecutor();
211            MyTask task = new MyTask();
212            assertFalse(task.isCompleted());
213            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        }
224    }
225
226    /**
227     * invoke of a runnable runs it to completion
228     */
229    public void testInvokeRunnable () {
230        try {
231            Executor e = new DirectExecutor();
232            MyTask task = new MyTask();
233            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        }
243    }
244
245    /**
246     * execute of a callable runs it to completion
247     */
248    public void testExecuteCallable () {
249        try {
250            Executor e = new DirectExecutor();
251            Future<String> future = Executors.execute(e, new StringTask());
252            String result = future.get();
253            assertSame(TEST_STRING, result);
254        }
255        catch (ExecutionException ex) {
256            unexpectedException();
257        }
258        catch (InterruptedException ex) {
259            unexpectedException();
260        }
261    }
262
263    /**
264     * invoke of a collable runs it to completion
265     */
266    public void testInvokeCallable () {
267        try {
268            Executor e = new DirectExecutor();
269            String result = Executors.invoke(e, new StringTask());
270
271            assertSame(TEST_STRING, result);
272        }
273        catch (ExecutionException ex) {
274            unexpectedException();
275        }
276        catch (InterruptedException ex) {
277            unexpectedException();
278        }
279    }
280
281    /**
282     * execute with null executor throws NPE
283     */
284    public void testNullExecuteRunnable () {
285        try {
286            MyTask task = new MyTask();
287            assertFalse(task.isCompleted());
288            Future<String> future = Executors.execute(null, task, TEST_STRING);
289            shouldThrow();
290        }
291        catch (NullPointerException success) {
292        }
293        catch (Exception ex) {
294            unexpectedException();
295        }
296    }
297
298    /**
299     * execute with a null runnable throws NPE
300     */
301    public void testExecuteNullRunnable() {
302        try {
303            Executor e = new DirectExecutor();
304            MyTask task = null;
305            Future<String> future = Executors.execute(e, task, TEST_STRING);
306            shouldThrow();
307        }
308        catch (NullPointerException success) {
309        }
310        catch (Exception ex) {
311            unexpectedException();
312        }
313    }
314
315    /**
316     * invoke of a null runnable throws NPE
317     */
318    public void testInvokeNullRunnable () {
319        try {
320            Executor e = new DirectExecutor();
321            MyTask task = null;
322            Executors.invoke(e, task);
323            shouldThrow();
324        }
325        catch (NullPointerException success) {
326        }
327        catch (Exception ex) {
328            unexpectedException();
329        }
330    }
331
332    /**
333     * execute of a null callable throws NPE
334     */
335    public void testExecuteNullCallable () {
336        try {
337            Executor e = new DirectExecutor();
338            StringTask t = null;
339            Future<String> future = Executors.execute(e, t);
340            shouldThrow();
341        }
342        catch (NullPointerException success) {
343        }
344        catch (Exception ex) {
345            unexpectedException();
346        }
347    }
348
349    /**
350     * invoke of a null callable throws NPE
351     */
352    public void testInvokeNullCallable () {
353        try {
354            Executor e = new DirectExecutor();
355            StringTask t = null;
356            String result = Executors.invoke(e, t);
357            shouldThrow();
358        }
359        catch (NullPointerException success) {
360        }
361        catch (Exception ex) {
362            unexpectedException();
363        }
364    }
365
366    /**
367     *  execute(Executor, Runnable) throws RejectedExecutionException
368     *  if saturated.
369     */
370    public void testExecute1() {
371        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
372        try {
373            
374            for(int i = 0; i < 5; ++i){
375                Executors.execute(p, new MediumRunnable(), Boolean.TRUE);
376            }
377            shouldThrow();
378        } catch(RejectedExecutionException success){}
379        joinPool(p);
380    }
381
382    /**
383     *  execute(Executor, Callable)throws RejectedExecutionException
384     *  if saturated.
385     */
386    public void testExecute2() {
387         ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
388        try {
389            for(int i = 0; i < 5; ++i) {
390                Executors.execute(p, new SmallCallable());
391            }
392            shouldThrow();
393        } catch(RejectedExecutionException e){}
394        joinPool(p);
395    }
396
397
398    /**
399     *  invoke(Executor, Runnable) throws InterruptedException if
400     *  caller interrupted.
401     */
402    public void testInterruptedInvoke() {
403        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
404        Thread t = new Thread(new Runnable() {
405                public void run() {
406                    try {
407                        Executors.invoke(p,new Runnable() {
408                                public void run() {
409                                    try {
410                                        Thread.sleep(MEDIUM_DELAY_MS);
411                                        shouldThrow();
412                                    } catch(InterruptedException e){
413                                    }
414                                }
415                            });
416                    } catch(InterruptedException success){
417                    } catch(Exception e) {
418                        unexpectedException();
419                    }
420                    
421                }
422            });
423        try {
424            t.start();
425            Thread.sleep(SHORT_DELAY_MS);
426            t.interrupt();
427        } catch(Exception e){
428            unexpectedException();
429        }
430        joinPool(p);
431    }
432
433    /**
434     *  invoke(Executor, Runnable) throws ExecutionException if
435     *  runnable throws exception.
436     */
437    public void testInvoke3() {
438        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
439        try {
440            Runnable r = new Runnable() {
441                    public void run() {
442                        int i = 5/0;
443                    }
444                };
445            
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);
456    }
457
458
459
460    /**
461     *  invoke(Executor, Callable) throws InterruptedException if
462     *  callable throws exception
463     */
464    public void testInvoke5() {
465        final ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
466        
467        final Callable c = new Callable() {
468                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            };
478
479
480        
481        Thread t = new Thread(new Runnable() {
482                public void run() {
483                    try {
484                        c.call();
485                    } catch(Exception e){}
486                }
487          });
488        try {
489            t.start();
490            Thread.sleep(SHORT_DELAY_MS);
491            t.interrupt();
492            t.join();
493        } catch(InterruptedException e){
494            unexpectedException();
495        }
496        
497        joinPool(p);
498    }
499
500    /**
501     *  invoke(Executor, Callable) will throw ExecutionException
502     *  if callable throws exception
503     */
504    public void testInvoke6() {
505        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1,SHORT_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
506
507        try {
508            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            
519            shouldThrow();
520        }
521        catch(ExecutionException success){
522        } catch(Exception e) {
523            unexpectedException();
524        }
525        joinPool(p);
526    }
527
528
178  
179      /**
180       *  timeouts from execute will time out if they compute too long.
# Line 568 | Line 217 | public class ExecutorsTest extends JSR16
217      }
218  
219      
220 +    /**
221 +     * ThreadPoolExecutor using defaultThreadFactory has
222 +     * specified group, priority, daemon status, and name
223 +     */
224 +    public void testDefaultThreadFactory() {
225 +        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
226 +        Runnable r = new Runnable() {
227 +                public void run() {
228 +                    Thread current = Thread.currentThread();
229 +                    threadAssertTrue(!current.isDaemon());
230 +                    threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
231 +                    ThreadGroup g = current.getThreadGroup();
232 +                    SecurityManager s = System.getSecurityManager();
233 +                    if (s != null)
234 +                        threadAssertTrue(g == s.getThreadGroup());
235 +                    else
236 +                        threadAssertTrue(g == egroup);
237 +                    String name = current.getName();
238 +                    threadAssertTrue(name.endsWith("thread-1"));
239 +                }
240 +            };
241 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
242 +        
243 +        e.execute(r);
244 +        e.shutdown();
245 +        try {
246 +            Thread.sleep(SHORT_DELAY_MS);
247 +        } catch (Exception eX) {
248 +            unexpectedException();
249 +        } finally {
250 +            joinPool(e);
251 +        }
252 +    }
253 +
254 +    /**
255 +     * ThreadPoolExecutor using privilegedThreadFactory has
256 +     * specified group, priority, daemon status, name,
257 +     * access control context and context class loader
258 +     */
259 +    public void testPrivilegedThreadFactory() {
260 +        Policy savedPolicy = Policy.getPolicy();
261 +        AdjustablePolicy policy = new AdjustablePolicy();
262 +        policy.addPermission(new RuntimePermission("getContextClassLoader"));
263 +        policy.addPermission(new RuntimePermission("setContextClassLoader"));
264 +        Policy.setPolicy(policy);
265 +        final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
266 +        final ClassLoader thisccl = Thread.currentThread().getContextClassLoader();
267 +        final AccessControlContext thisacc = AccessController.getContext();
268 +        Runnable r = new Runnable() {
269 +                public void run() {
270 +                    Thread current = Thread.currentThread();
271 +                    threadAssertTrue(!current.isDaemon());
272 +                    threadAssertTrue(current.getPriority() == Thread.NORM_PRIORITY);
273 +                    ThreadGroup g = current.getThreadGroup();
274 +                    SecurityManager s = System.getSecurityManager();
275 +                    if (s != null)
276 +                        threadAssertTrue(g == s.getThreadGroup());
277 +                    else
278 +                        threadAssertTrue(g == egroup);
279 +                    String name = current.getName();
280 +                    threadAssertTrue(name.endsWith("thread-1"));
281 +                    threadAssertTrue(thisccl == current.getContextClassLoader());
282 +                    threadAssertTrue(thisacc.equals(AccessController.getContext()));
283 +                }
284 +            };
285 +        ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
286 +        
287 +        Policy.setPolicy(savedPolicy);
288 +        e.execute(r);
289 +        e.shutdown();
290 +        try {
291 +            Thread.sleep(SHORT_DELAY_MS);
292 +        } catch (Exception ex) {
293 +            unexpectedException();
294 +        } finally {
295 +            joinPool(e);
296 +        }
297  
298 +    }
299  
300   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines