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.21 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.26 by jsr166, Sat Nov 21 02:33:20 2009 UTC

# Line 10 | Line 10
10   import junit.framework.*;
11   import java.util.*;
12   import java.util.concurrent.*;
13 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
14   import java.math.BigInteger;
15   import java.security.*;
16  
17 < public class ExecutorsTest extends JSR166TestCase{
17 > public class ExecutorsTest extends JSR166TestCase {
18      public static void main(String[] args) {
19          junit.textui.TestRunner.run (suite());
20      }
# Line 35 | Line 36 | public class ExecutorsTest extends JSR16
36          public T call() throws Exception {
37              Future<T> ftask = exec.submit(func);
38              try {
39 <                return ftask.get(msecs, TimeUnit.MILLISECONDS);
39 >                return ftask.get(msecs, MILLISECONDS);
40              } finally {
41                  ftask.cancel(true);
42              }
# Line 90 | Line 91 | public class ExecutorsTest extends JSR16
91          try {
92              ExecutorService e = Executors.newCachedThreadPool(null);
93              shouldThrow();
94 <        }
94 <        catch (NullPointerException success) {
95 <        }
94 >        } catch (NullPointerException success) {}
95      }
96  
97  
# Line 125 | Line 124 | public class ExecutorsTest extends JSR16
124          try {
125              ExecutorService e = Executors.newSingleThreadExecutor(null);
126              shouldThrow();
127 <        }
129 <        catch (NullPointerException success) {
130 <        }
127 >        } catch (NullPointerException success) {}
128      }
129  
130      /**
# Line 137 | Line 134 | public class ExecutorsTest extends JSR16
134          ExecutorService e = Executors.newSingleThreadExecutor();
135          try {
136              ThreadPoolExecutor tpe = (ThreadPoolExecutor)e;
137 +            shouldThrow();
138          } catch (ClassCastException success) {
139          } finally {
140              joinPool(e);
# Line 173 | Line 171 | public class ExecutorsTest extends JSR16
171          try {
172              ExecutorService e = Executors.newFixedThreadPool(2, null);
173              shouldThrow();
174 <        }
177 <        catch (NullPointerException success) {
178 <        }
174 >        } catch (NullPointerException success) {}
175      }
176  
177      /**
# Line 185 | Line 181 | public class ExecutorsTest extends JSR16
181          try {
182              ExecutorService e = Executors.newFixedThreadPool(0);
183              shouldThrow();
184 <        }
189 <        catch (IllegalArgumentException success) {
190 <        }
184 >        } catch (IllegalArgumentException success) {}
185      }
186  
187  
# Line 208 | Line 202 | public class ExecutorsTest extends JSR16
202      public void testunconfigurableExecutorServiceNPE() {
203          try {
204              ExecutorService e = Executors.unconfigurableExecutorService(null);
205 <        }
206 <        catch (NullPointerException success) {
213 <        }
205 >            shouldThrow();
206 >        } catch (NullPointerException success) {}
207      }
208  
209      /**
# Line 219 | Line 212 | public class ExecutorsTest extends JSR16
212      public void testunconfigurableScheduledExecutorServiceNPE() {
213          try {
214              ExecutorService e = Executors.unconfigurableScheduledExecutorService(null);
215 <        }
216 <        catch (NullPointerException success) {
224 <        }
215 >            shouldThrow();
216 >        } catch (NullPointerException success) {}
217      }
218  
219  
220      /**
221       * a newSingleThreadScheduledExecutor successfully runs delayed task
222       */
223 <    public void testNewSingleThreadScheduledExecutor() {
224 <        try {
225 <            TrackedCallable callable = new TrackedCallable();
226 <            ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor();
227 <            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
228 <            assertFalse(callable.done);
229 <            Thread.sleep(MEDIUM_DELAY_MS);
230 <            assertTrue(callable.done);
231 <            assertEquals(Boolean.TRUE, f.get());
240 <            joinPool(p1);
241 <        } catch (RejectedExecutionException e){}
242 <        catch (Exception e){
243 <            e.printStackTrace();
244 <            unexpectedException();
245 <        }
223 >    public void testNewSingleThreadScheduledExecutor() throws Exception {
224 >        TrackedCallable callable = new TrackedCallable();
225 >        ScheduledExecutorService p1 = Executors.newSingleThreadScheduledExecutor();
226 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
227 >        assertFalse(callable.done);
228 >        Thread.sleep(MEDIUM_DELAY_MS);
229 >        assertTrue(callable.done);
230 >        assertEquals(Boolean.TRUE, f.get());
231 >        joinPool(p1);
232      }
233  
234      /**
235       * a newScheduledThreadPool successfully runs delayed task
236       */
237 <    public void testnewScheduledThreadPool() {
238 <        try {
239 <            TrackedCallable callable = new TrackedCallable();
240 <            ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
241 <            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
242 <            assertFalse(callable.done);
243 <            Thread.sleep(MEDIUM_DELAY_MS);
244 <            assertTrue(callable.done);
245 <            assertEquals(Boolean.TRUE, f.get());
260 <            joinPool(p1);
261 <        } catch (RejectedExecutionException e){}
262 <        catch (Exception e){
263 <            e.printStackTrace();
264 <            unexpectedException();
265 <        }
237 >    public void testnewScheduledThreadPool() throws Exception {
238 >        TrackedCallable callable = new TrackedCallable();
239 >        ScheduledExecutorService p1 = Executors.newScheduledThreadPool(2);
240 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
241 >        assertFalse(callable.done);
242 >        Thread.sleep(MEDIUM_DELAY_MS);
243 >        assertTrue(callable.done);
244 >        assertEquals(Boolean.TRUE, f.get());
245 >        joinPool(p1);
246      }
247  
248      /**
249 <     * an unconfigurable  newScheduledThreadPool successfully runs delayed task
250 <     */
251 <    public void testunconfigurableScheduledExecutorService() {
252 <        try {
253 <            TrackedCallable callable = new TrackedCallable();
254 <            ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
255 <            Future f = p1.schedule(callable, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
256 <            assertFalse(callable.done);
257 <            Thread.sleep(MEDIUM_DELAY_MS);
258 <            assertTrue(callable.done);
259 <            assertEquals(Boolean.TRUE, f.get());
280 <            joinPool(p1);
281 <        } catch (RejectedExecutionException e){}
282 <        catch (Exception e){
283 <            e.printStackTrace();
284 <            unexpectedException();
285 <        }
249 >     * an unconfigurable newScheduledThreadPool successfully runs delayed task
250 >     */
251 >    public void testunconfigurableScheduledExecutorService() throws Exception {
252 >        TrackedCallable callable = new TrackedCallable();
253 >        ScheduledExecutorService p1 = Executors.unconfigurableScheduledExecutorService(Executors.newScheduledThreadPool(2));
254 >        Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS);
255 >        assertFalse(callable.done);
256 >        Thread.sleep(MEDIUM_DELAY_MS);
257 >        assertTrue(callable.done);
258 >        assertEquals(Boolean.TRUE, f.get());
259 >        joinPool(p1);
260      }
261  
262      /**
263       *  timeouts from execute will time out if they compute too long.
264       */
265 <    public void testTimedCallable() {
265 >    public void testTimedCallable() throws Exception {
266          int N = 10000;
267          ExecutorService executor = Executors.newSingleThreadExecutor();
268          List<Callable<BigInteger>> tasks = new ArrayList<Callable<BigInteger>>(N);
# Line 312 | Line 286 | public class ExecutorsTest extends JSR16
286                      assertTrue(iters > 0);
287                      return;
288                  }
315                catch (Exception e) {
316                    unexpectedException();
317                }
289              }
290              // if by chance we didn't ever time out, total time must be small
291              long elapsed = System.currentTimeMillis() - startTime;
# Line 330 | Line 301 | public class ExecutorsTest extends JSR16
301       * ThreadPoolExecutor using defaultThreadFactory has
302       * specified group, priority, daemon status, and name
303       */
304 <    public void testDefaultThreadFactory() {
304 >    public void testDefaultThreadFactory() throws Exception {
305          final ThreadGroup egroup = Thread.currentThread().getThreadGroup();
306          Runnable r = new Runnable() {
307                  public void run() {
308 <                    try {
309 <                        Thread current = Thread.currentThread();
310 <                        threadAssertTrue(!current.isDaemon());
311 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
312 <                        ThreadGroup g = current.getThreadGroup();
313 <                        SecurityManager s = System.getSecurityManager();
314 <                        if (s != null)
315 <                            threadAssertTrue(g == s.getThreadGroup());
316 <                        else
317 <                            threadAssertTrue(g == egroup);
318 <                        String name = current.getName();
319 <                        threadAssertTrue(name.endsWith("thread-1"));
320 <                    } catch (SecurityException ok) {
321 <                        // Also pass if not allowed to change setting
322 <                    }
308 >                    try {
309 >                        Thread current = Thread.currentThread();
310 >                        threadAssertTrue(!current.isDaemon());
311 >                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
312 >                        ThreadGroup g = current.getThreadGroup();
313 >                        SecurityManager s = System.getSecurityManager();
314 >                        if (s != null)
315 >                            threadAssertTrue(g == s.getThreadGroup());
316 >                        else
317 >                            threadAssertTrue(g == egroup);
318 >                        String name = current.getName();
319 >                        threadAssertTrue(name.endsWith("thread-1"));
320 >                    } catch (SecurityException ok) {
321 >                        // Also pass if not allowed to change setting
322 >                    }
323                  }
324              };
325          ExecutorService e = Executors.newSingleThreadExecutor(Executors.defaultThreadFactory());
# Line 361 | Line 332 | public class ExecutorsTest extends JSR16
332  
333          try {
334              Thread.sleep(SHORT_DELAY_MS);
364        } catch (Exception eX) {
365            unexpectedException();
335          } finally {
336              joinPool(e);
337          }
# Line 373 | Line 342 | public class ExecutorsTest extends JSR16
342       * specified group, priority, daemon status, name,
343       * access control context and context class loader
344       */
345 <    public void testPrivilegedThreadFactory() {
345 >    public void testPrivilegedThreadFactory() throws Exception {
346          Policy savedPolicy = null;
347          try {
348              savedPolicy = Policy.getPolicy();
# Line 389 | Line 358 | public class ExecutorsTest extends JSR16
358          final AccessControlContext thisacc = AccessController.getContext();
359          Runnable r = new Runnable() {
360                  public void run() {
361 <                    try {
362 <                        Thread current = Thread.currentThread();
363 <                        threadAssertTrue(!current.isDaemon());
364 <                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
365 <                        ThreadGroup g = current.getThreadGroup();
366 <                        SecurityManager s = System.getSecurityManager();
367 <                        if (s != null)
368 <                            threadAssertTrue(g == s.getThreadGroup());
369 <                        else
370 <                            threadAssertTrue(g == egroup);
371 <                        String name = current.getName();
372 <                        threadAssertTrue(name.endsWith("thread-1"));
373 <                        threadAssertTrue(thisccl == current.getContextClassLoader());
374 <                        threadAssertTrue(thisacc.equals(AccessController.getContext()));
375 <                    } catch (SecurityException ok) {
376 <                        // Also pass if not allowed to change settings
377 <                    }
361 >                    try {
362 >                        Thread current = Thread.currentThread();
363 >                        threadAssertTrue(!current.isDaemon());
364 >                        threadAssertTrue(current.getPriority() <= Thread.NORM_PRIORITY);
365 >                        ThreadGroup g = current.getThreadGroup();
366 >                        SecurityManager s = System.getSecurityManager();
367 >                        if (s != null)
368 >                            threadAssertTrue(g == s.getThreadGroup());
369 >                        else
370 >                            threadAssertTrue(g == egroup);
371 >                        String name = current.getName();
372 >                        threadAssertTrue(name.endsWith("thread-1"));
373 >                        threadAssertTrue(thisccl == current.getContextClassLoader());
374 >                        threadAssertTrue(thisacc.equals(AccessController.getContext()));
375 >                    } catch (SecurityException ok) {
376 >                        // Also pass if not allowed to change settings
377 >                    }
378                  }
379              };
380          ExecutorService e = Executors.newSingleThreadExecutor(Executors.privilegedThreadFactory());
# Line 418 | Line 387 | public class ExecutorsTest extends JSR16
387          }
388          try {
389              Thread.sleep(SHORT_DELAY_MS);
421        } catch (Exception ex) {
422            unexpectedException();
390          } finally {
391              joinPool(e);
392          }
# Line 447 | Line 414 | public class ExecutorsTest extends JSR16
414       * privilegedCallableUsingCurrentClassLoader throws ACE
415       */
416      public void testCreatePrivilegedCallableUsingCCLWithNoPrivs() {
417 <        Policy savedPolicy = null;
417 >        Policy savedPolicy = null;
418          try {
419              savedPolicy = Policy.getPolicy();
420              AdjustablePolicy policy = new AdjustablePolicy();
# Line 469 | Line 436 | public class ExecutorsTest extends JSR16
436              Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
437              shouldThrow();
438          } catch (AccessControlException success) {
439 <        } catch (Exception ex) {
473 <            unexpectedException();
474 <        }
475 <        finally {
439 >        } finally {
440              Policy.setPolicy(savedPolicy);
441          }
442      }
# Line 481 | Line 445 | public class ExecutorsTest extends JSR16
445       * With class loader permissions, calling
446       * privilegedCallableUsingCurrentClassLoader does not throw ACE
447       */
448 <    public void testprivilegedCallableUsingCCLWithPrivs() {
449 <        Policy savedPolicy = null;
448 >    public void testprivilegedCallableUsingCCLWithPrivs() throws Exception {
449 >        Policy savedPolicy = null;
450          try {
451              savedPolicy = Policy.getPolicy();
452              AdjustablePolicy policy = new AdjustablePolicy();
# Line 496 | Line 460 | public class ExecutorsTest extends JSR16
460          try {
461              Callable task = Executors.privilegedCallableUsingCurrentClassLoader(new NoOpCallable());
462              task.call();
499        } catch (Exception ex) {
500            unexpectedException();
463          }
464          finally {
465              Policy.setPolicy(savedPolicy);
# Line 507 | Line 469 | public class ExecutorsTest extends JSR16
469      /**
470       * Without permissions, calling privilegedCallable throws ACE
471       */
472 <    public void testprivilegedCallableWithNoPrivs() {
472 >    public void testprivilegedCallableWithNoPrivs() throws Exception {
473          Callable task;
474          Policy savedPolicy = null;
475          AdjustablePolicy policy = null;
# Line 538 | Line 500 | public class ExecutorsTest extends JSR16
500          try {
501              task.call();
502              shouldThrow();
503 <        } catch (AccessControlException success) {
542 <        } catch (Exception ex) {
543 <            unexpectedException();
544 <        }
503 >        } catch (AccessControlException success) {}
504      }
505  
506      /**
507       * With permissions, calling privilegedCallable succeeds
508       */
509 <    public void testprivilegedCallableWithPrivs() {
510 <        Policy savedPolicy = null;
509 >    public void testprivilegedCallableWithPrivs() throws Exception {
510 >        Policy savedPolicy = null;
511          try {
512              savedPolicy = Policy.getPolicy();
513              AdjustablePolicy policy = new AdjustablePolicy();
# Line 562 | Line 521 | public class ExecutorsTest extends JSR16
521          Callable task = Executors.privilegedCallable(new CheckCCL());
522          try {
523              task.call();
565        } catch (Exception ex) {
566            unexpectedException();
524          } finally {
525              Policy.setPolicy(savedPolicy);
526          }
# Line 572 | Line 529 | public class ExecutorsTest extends JSR16
529      /**
530       * callable(Runnable) returns null when called
531       */
532 <    public void testCallable1() {
533 <        try {
534 <            Callable c = Executors.callable(new NoOpRunnable());
578 <            assertNull(c.call());
579 <        } catch (Exception ex) {
580 <            unexpectedException();
581 <        }
582 <
532 >    public void testCallable1() throws Exception {
533 >        Callable c = Executors.callable(new NoOpRunnable());
534 >        assertNull(c.call());
535      }
536  
537      /**
538       * callable(Runnable, result) returns result when called
539       */
540 <    public void testCallable2() {
541 <        try {
542 <            Callable c = Executors.callable(new NoOpRunnable(), one);
591 <            assertEquals(one, c.call());
592 <        } catch (Exception ex) {
593 <            unexpectedException();
594 <        }
540 >    public void testCallable2() throws Exception {
541 >        Callable c = Executors.callable(new NoOpRunnable(), one);
542 >        assertEquals(one, c.call());
543      }
544  
545      /**
546       * callable(PrivilegedAction) returns its result when called
547       */
548 <    public void testCallable3() {
549 <        try {
550 <            Callable c = Executors.callable(new PrivilegedAction() {
603 <                    public Object run() { return one; }});
548 >    public void testCallable3() throws Exception {
549 >        Callable c = Executors.callable(new PrivilegedAction() {
550 >                public Object run() { return one; }});
551          assertEquals(one, c.call());
605        } catch (Exception ex) {
606            unexpectedException();
607        }
552      }
553  
554      /**
555       * callable(PrivilegedExceptionAction) returns its result when called
556       */
557 <    public void testCallable4() {
558 <        try {
559 <            Callable c = Executors.callable(new PrivilegedExceptionAction() {
560 <                    public Object run() { return one; }});
617 <            assertEquals(one, c.call());
618 <        } catch (Exception ex) {
619 <            unexpectedException();
620 <        }
557 >    public void testCallable4() throws Exception {
558 >        Callable c = Executors.callable(new PrivilegedExceptionAction() {
559 >                public Object run() { return one; }});
560 >        assertEquals(one, c.call());
561      }
562  
563  
# Line 626 | Line 566 | public class ExecutorsTest extends JSR16
566       */
567      public void testCallableNPE1() {
568          try {
569 <            Runnable r = null;
570 <            Callable c = Executors.callable(r);
571 <        } catch (NullPointerException success) {
632 <        }
569 >            Callable c = Executors.callable((Runnable) null);
570 >            shouldThrow();
571 >        } catch (NullPointerException success) {}
572      }
573  
574      /**
# Line 637 | Line 576 | public class ExecutorsTest extends JSR16
576       */
577      public void testCallableNPE2() {
578          try {
579 <            Runnable r = null;
580 <            Callable c = Executors.callable(r, one);
581 <        } catch (NullPointerException success) {
643 <        }
579 >            Callable c = Executors.callable((Runnable) null, one);
580 >            shouldThrow();
581 >        } catch (NullPointerException success) {}
582      }
583  
584      /**
# Line 648 | Line 586 | public class ExecutorsTest extends JSR16
586       */
587      public void testCallableNPE3() {
588          try {
589 <            PrivilegedAction r = null;
590 <            Callable c = Executors.callable(r);
591 <        } catch (NullPointerException success) {
654 <        }
589 >            Callable c = Executors.callable((PrivilegedAction) null);
590 >            shouldThrow();
591 >        } catch (NullPointerException success) {}
592      }
593  
594      /**
# Line 659 | Line 596 | public class ExecutorsTest extends JSR16
596       */
597      public void testCallableNPE4() {
598          try {
599 <            PrivilegedExceptionAction r = null;
600 <            Callable c = Executors.callable(r);
601 <        } catch (NullPointerException success) {
665 <        }
599 >            Callable c = Executors.callable((PrivilegedExceptionAction) null);
600 >            shouldThrow();
601 >        } catch (NullPointerException success) {}
602      }
603  
604  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines