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.8 by dl, Sat Nov 1 18:37:02 2003 UTC vs.
Revision 1.10 by tim, Wed Dec 10 01:51:12 2003 UTC

# Line 14 | Line 14 | 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);
21 <    }
22 <
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 <        }
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 187 | Line 175 | public class ExecutorsTest extends JSR16
175          }
176      }
177  
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     * invoke of a collable runs it to completion
340     */
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 MediumRunnable(), Boolean.TRUE);
451            }
452            shouldThrow();
453        } catch(RejectedExecutionException success){}
454        joinPool(p);
455    }
456
457    /**
458     *  execute(Executor, Callable)throws RejectedExecutionException
459     *  if saturated.
460     */
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 SmallCallable());
466            }
467            shouldThrow();
468        } catch(RejectedExecutionException e){}
469        joinPool(p);
470    }
471
472
473    /**
474     *  invoke(Executor, Runnable) throws InterruptedException if
475     *  caller interrupted.
476     */
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 {
485                                        Thread.sleep(MEDIUM_DELAY_MS);
486                                        shouldThrow();
487                                    } catch(InterruptedException e){
488                                    }
489                                }
490                            });
491                    } catch(InterruptedException success){
492                    } catch(Exception e) {
493                        unexpectedException();
494                    }
495                    
496                }
497            });
498        try {
499            t.start();
500            Thread.sleep(SHORT_DELAY_MS);
501            t.interrupt();
502        } catch(Exception e){
503            unexpectedException();
504        }
505        joinPool(p);
506    }
507
508    /**
509     *  invoke(Executor, Runnable) throws ExecutionException if
510     *  runnable throws exception.
511     */
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                };
520            
521            for(int i =0; i < 5; i++){
522                Executors.invoke(p,r);
523            }
524            
525            shouldThrow();
526        } catch(ExecutionException success){
527        } catch(Exception e){
528            unexpectedException();
529        }
530        joinPool(p);
531    }
532
533
534
535    /**
536     *  invoke(Executor, Callable) throws InterruptedException if
537     *  callable throws exception
538     */
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 SmallCallable());
546                        shouldThrow();
547                    } catch(InterruptedException e){}
548                    catch(RejectedExecutionException e2){}
549                    catch(ExecutionException e3){}
550                    return Boolean.TRUE;
551                }
552            };
553
554
555        
556        Thread t = new Thread(new Runnable() {
557                public void run() {
558                    try {
559                        c.call();
560                    } catch(Exception e){}
561                }
562          });
563        try {
564            t.start();
565            Thread.sleep(SHORT_DELAY_MS);
566            t.interrupt();
567            t.join();
568        } catch(InterruptedException e){
569            unexpectedException();
570        }
571        
572        joinPool(p);
573    }
574
575    /**
576     *  invoke(Executor, Callable) will throw ExecutionException
577     *  if callable throws exception
578     */
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() {
585                        int i = 5/0;
586                        return Boolean.TRUE;
587                    }
588                };
589            
590            for(int i =0; i < 5; i++){
591                Executors.invoke(p,c);
592            }
593            
594            shouldThrow();
595        }
596        catch(ExecutionException success){
597        } catch(Exception e) {
598            unexpectedException();
599        }
600        joinPool(p);
601    }
602
603
178  
179      /**
180       *  timeouts from execute will time out if they compute too long.
# Line 648 | Line 222 | public class ExecutorsTest extends JSR16
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 <        }
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      /**
# Line 683 | Line 257 | public class ExecutorsTest extends JSR16
257       * access control context and context class loader
258       */
259      public void testPrivilegedThreadFactory() {
260 <        Policy savedPolicy = Policy.getPolicy();
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();
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 <        }
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  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines