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

Comparing jsr166/src/test/tck/JSR166TestCase.java (file contents):
Revision 1.53 by jsr166, Thu Sep 16 00:52:49 2010 UTC vs.
Revision 1.61 by jsr166, Mon Oct 11 03:54:10 2010 UTC

# Line 97 | Line 97 | public class JSR166TestCase extends Test
97          Boolean.getBoolean("jsr166.useSecurityManager");
98  
99      /**
100 +     * If true, report on stdout all "slow" tests, that is, ones that
101 +     * take more than profileThreshold milliseconds to execute.
102 +     */
103 +    private static final boolean profileTests =
104 +        Boolean.getBoolean("jsr166.profileTests");
105 +
106 +    /**
107 +     * The number of milliseconds that tests are permitted for
108 +     * execution without being reported, when profileTests is set.
109 +     */
110 +    private static final long profileThreshold =
111 +        Long.getLong("jsr166.profileThreshold", 100);
112 +
113 +    protected void runTest() throws Throwable {
114 +        if (profileTests)
115 +            runTestProfiled();
116 +        else
117 +            super.runTest();
118 +    }
119 +
120 +    protected void runTestProfiled() throws Throwable {
121 +        long t0 = System.nanoTime();
122 +        try {
123 +            super.runTest();
124 +        } finally {
125 +            long elapsedMillis =
126 +                (System.nanoTime() - t0) / (1000L * 1000L);
127 +            if (elapsedMillis >= profileThreshold)
128 +                System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
129 +        }
130 +    }
131 +    
132 +    /**
133       * Runs all JSR166 unit tests using junit.textui.TestRunner
134       */
135      public static void main(String[] args) {
# Line 105 | Line 138 | public class JSR166TestCase extends Test
138              Policy.setPolicy(permissivePolicy());
139              System.setSecurityManager(new SecurityManager());
140          }
141 <        int iters = 1;
142 <        if (args.length > 0)
110 <            iters = Integer.parseInt(args[0]);
141 >        int iters = (args.length == 0) ? 1 : Integer.parseInt(args[0]);
142 >
143          Test s = suite();
144          for (int i = 0; i < iters; ++i) {
145              junit.textui.TestRunner.run(s);
# Line 117 | Line 149 | public class JSR166TestCase extends Test
149          System.exit(0);
150      }
151  
152 +    public static TestSuite newTestSuite(Object... suiteOrClasses) {
153 +        TestSuite suite = new TestSuite();
154 +        for (Object suiteOrClass : suiteOrClasses) {
155 +            if (suiteOrClass instanceof TestSuite)
156 +                suite.addTest((TestSuite) suiteOrClass);
157 +            else if (suiteOrClass instanceof Class)
158 +                suite.addTest(new TestSuite((Class<?>) suiteOrClass));
159 +            else
160 +                throw new ClassCastException("not a test suite or class");
161 +        }
162 +        return suite;
163 +    }
164 +
165      /**
166 <     * Collects all JSR166 unit tests as one suite
166 >     * Collects all JSR166 unit tests as one suite.
167       */
168      public static Test suite() {
169 <        TestSuite suite = new TestSuite("JSR166 Unit Tests");
170 <
171 <        suite.addTest(new TestSuite(ForkJoinPoolTest.class));
172 <        suite.addTest(new TestSuite(ForkJoinTaskTest.class));
173 <        suite.addTest(new TestSuite(RecursiveActionTest.class));
174 <        suite.addTest(new TestSuite(RecursiveTaskTest.class));
175 <        suite.addTest(new TestSuite(LinkedTransferQueueTest.class));
176 <        suite.addTest(new TestSuite(PhaserTest.class));
177 <        suite.addTest(new TestSuite(ThreadLocalRandomTest.class));
178 <        suite.addTest(new TestSuite(AbstractExecutorServiceTest.class));
179 <        suite.addTest(new TestSuite(AbstractQueueTest.class));
180 <        suite.addTest(new TestSuite(AbstractQueuedSynchronizerTest.class));
181 <        suite.addTest(new TestSuite(AbstractQueuedLongSynchronizerTest.class));
182 <        suite.addTest(new TestSuite(ArrayBlockingQueueTest.class));
183 <        suite.addTest(new TestSuite(ArrayDequeTest.class));
184 <        suite.addTest(new TestSuite(AtomicBooleanTest.class));
185 <        suite.addTest(new TestSuite(AtomicIntegerArrayTest.class));
186 <        suite.addTest(new TestSuite(AtomicIntegerFieldUpdaterTest.class));
187 <        suite.addTest(new TestSuite(AtomicIntegerTest.class));
188 <        suite.addTest(new TestSuite(AtomicLongArrayTest.class));
189 <        suite.addTest(new TestSuite(AtomicLongFieldUpdaterTest.class));
190 <        suite.addTest(new TestSuite(AtomicLongTest.class));
191 <        suite.addTest(new TestSuite(AtomicMarkableReferenceTest.class));
192 <        suite.addTest(new TestSuite(AtomicReferenceArrayTest.class));
193 <        suite.addTest(new TestSuite(AtomicReferenceFieldUpdaterTest.class));
194 <        suite.addTest(new TestSuite(AtomicReferenceTest.class));
195 <        suite.addTest(new TestSuite(AtomicStampedReferenceTest.class));
196 <        suite.addTest(new TestSuite(ConcurrentHashMapTest.class));
197 <        suite.addTest(new TestSuite(ConcurrentLinkedDequeTest.class));
198 <        suite.addTest(new TestSuite(ConcurrentLinkedQueueTest.class));
199 <        suite.addTest(new TestSuite(ConcurrentSkipListMapTest.class));
200 <        suite.addTest(new TestSuite(ConcurrentSkipListSubMapTest.class));
201 <        suite.addTest(new TestSuite(ConcurrentSkipListSetTest.class));
202 <        suite.addTest(new TestSuite(ConcurrentSkipListSubSetTest.class));
203 <        suite.addTest(new TestSuite(CopyOnWriteArrayListTest.class));
204 <        suite.addTest(new TestSuite(CopyOnWriteArraySetTest.class));
205 <        suite.addTest(new TestSuite(CountDownLatchTest.class));
206 <        suite.addTest(new TestSuite(CyclicBarrierTest.class));
207 <        suite.addTest(new TestSuite(DelayQueueTest.class));
208 <        suite.addTest(new TestSuite(EntryTest.class));
209 <        suite.addTest(new TestSuite(ExchangerTest.class));
210 <        suite.addTest(new TestSuite(ExecutorsTest.class));
211 <        suite.addTest(new TestSuite(ExecutorCompletionServiceTest.class));
212 <        suite.addTest(new TestSuite(FutureTaskTest.class));
213 <        suite.addTest(new TestSuite(LinkedBlockingDequeTest.class));
214 <        suite.addTest(new TestSuite(LinkedBlockingQueueTest.class));
215 <        suite.addTest(new TestSuite(LinkedListTest.class));
216 <        suite.addTest(new TestSuite(LockSupportTest.class));
217 <        suite.addTest(new TestSuite(PriorityBlockingQueueTest.class));
218 <        suite.addTest(new TestSuite(PriorityQueueTest.class));
219 <        suite.addTest(new TestSuite(ReentrantLockTest.class));
220 <        suite.addTest(new TestSuite(ReentrantReadWriteLockTest.class));
221 <        suite.addTest(new TestSuite(ScheduledExecutorTest.class));
222 <        suite.addTest(new TestSuite(ScheduledExecutorSubclassTest.class));
223 <        suite.addTest(new TestSuite(SemaphoreTest.class));
224 <        suite.addTest(new TestSuite(SynchronousQueueTest.class));
225 <        suite.addTest(new TestSuite(SystemTest.class));
226 <        suite.addTest(new TestSuite(ThreadLocalTest.class));
227 <        suite.addTest(new TestSuite(ThreadPoolExecutorTest.class));
228 <        suite.addTest(new TestSuite(ThreadPoolExecutorSubclassTest.class));
229 <        suite.addTest(new TestSuite(ThreadTest.class));
230 <        suite.addTest(new TestSuite(TimeUnitTest.class));
231 <        suite.addTest(new TestSuite(TreeMapTest.class));
232 <        suite.addTest(new TestSuite(TreeSetTest.class));
233 <        suite.addTest(new TestSuite(TreeSubMapTest.class));
189 <        suite.addTest(new TestSuite(TreeSubSetTest.class));
190 <
191 <        return suite;
169 >        return newTestSuite(
170 >            ForkJoinPoolTest.suite(),
171 >            ForkJoinTaskTest.suite(),
172 >            RecursiveActionTest.suite(),
173 >            RecursiveTaskTest.suite(),
174 >            LinkedTransferQueueTest.suite(),
175 >            PhaserTest.suite(),
176 >            ThreadLocalRandomTest.suite(),
177 >            AbstractExecutorServiceTest.suite(),
178 >            AbstractQueueTest.suite(),
179 >            AbstractQueuedSynchronizerTest.suite(),
180 >            AbstractQueuedLongSynchronizerTest.suite(),
181 >            ArrayBlockingQueueTest.suite(),
182 >            ArrayDequeTest.suite(),
183 >            AtomicBooleanTest.suite(),
184 >            AtomicIntegerArrayTest.suite(),
185 >            AtomicIntegerFieldUpdaterTest.suite(),
186 >            AtomicIntegerTest.suite(),
187 >            AtomicLongArrayTest.suite(),
188 >            AtomicLongFieldUpdaterTest.suite(),
189 >            AtomicLongTest.suite(),
190 >            AtomicMarkableReferenceTest.suite(),
191 >            AtomicReferenceArrayTest.suite(),
192 >            AtomicReferenceFieldUpdaterTest.suite(),
193 >            AtomicReferenceTest.suite(),
194 >            AtomicStampedReferenceTest.suite(),
195 >            ConcurrentHashMapTest.suite(),
196 >            ConcurrentLinkedDequeTest.suite(),
197 >            ConcurrentLinkedQueueTest.suite(),
198 >            ConcurrentSkipListMapTest.suite(),
199 >            ConcurrentSkipListSubMapTest.suite(),
200 >            ConcurrentSkipListSetTest.suite(),
201 >            ConcurrentSkipListSubSetTest.suite(),
202 >            CopyOnWriteArrayListTest.suite(),
203 >            CopyOnWriteArraySetTest.suite(),
204 >            CountDownLatchTest.suite(),
205 >            CyclicBarrierTest.suite(),
206 >            DelayQueueTest.suite(),
207 >            EntryTest.suite(),
208 >            ExchangerTest.suite(),
209 >            ExecutorsTest.suite(),
210 >            ExecutorCompletionServiceTest.suite(),
211 >            FutureTaskTest.suite(),
212 >            LinkedBlockingDequeTest.suite(),
213 >            LinkedBlockingQueueTest.suite(),
214 >            LinkedListTest.suite(),
215 >            LockSupportTest.suite(),
216 >            PriorityBlockingQueueTest.suite(),
217 >            PriorityQueueTest.suite(),
218 >            ReentrantLockTest.suite(),
219 >            ReentrantReadWriteLockTest.suite(),
220 >            ScheduledExecutorTest.suite(),
221 >            ScheduledExecutorSubclassTest.suite(),
222 >            SemaphoreTest.suite(),
223 >            SynchronousQueueTest.suite(),
224 >            SystemTest.suite(),
225 >            ThreadLocalTest.suite(),
226 >            ThreadPoolExecutorTest.suite(),
227 >            ThreadPoolExecutorSubclassTest.suite(),
228 >            ThreadTest.suite(),
229 >            TimeUnitTest.suite(),
230 >            TreeMapTest.suite(),
231 >            TreeSetTest.suite(),
232 >            TreeSubMapTest.suite(),
233 >            TreeSubSetTest.suite());
234      }
235  
236  
# Line 251 | Line 293 | public class JSR166TestCase extends Test
293                  throw (RuntimeException) t;
294              else if (t instanceof Exception)
295                  throw (Exception) t;
296 <            else
297 <                throw new AssertionError(t);
296 >            else {
297 >                AssertionFailedError afe =
298 >                    new AssertionFailedError(t.toString());
299 >                afe.initCause(t);
300 >                throw afe;
301 >            }
302          }
303      }
304  
305      /**
306       * Just like fail(reason), but additionally recording (using
307 <     * threadRecordFailure) any AssertionError thrown, so that the current
308 <     * testcase will fail.
307 >     * threadRecordFailure) any AssertionFailedError thrown, so that
308 >     * the current testcase will fail.
309       */
310      public void threadFail(String reason) {
311          try {
312              fail(reason);
313 <        } catch (Throwable t) {
313 >        } catch (AssertionFailedError t) {
314              threadRecordFailure(t);
315              fail(reason);
316          }
# Line 272 | Line 318 | public class JSR166TestCase extends Test
318  
319      /**
320       * Just like assertTrue(b), but additionally recording (using
321 <     * threadRecordFailure) any AssertionError thrown, so that the current
322 <     * testcase will fail.
321 >     * threadRecordFailure) any AssertionFailedError thrown, so that
322 >     * the current testcase will fail.
323       */
324      public void threadAssertTrue(boolean b) {
325          try {
326              assertTrue(b);
327 <        } catch (AssertionError t) {
327 >        } catch (AssertionFailedError t) {
328              threadRecordFailure(t);
329              throw t;
330          }
# Line 286 | Line 332 | public class JSR166TestCase extends Test
332  
333      /**
334       * Just like assertFalse(b), but additionally recording (using
335 <     * threadRecordFailure) any AssertionError thrown, so that the
336 <     * current testcase will fail.
335 >     * threadRecordFailure) any AssertionFailedError thrown, so that
336 >     * the current testcase will fail.
337       */
338      public void threadAssertFalse(boolean b) {
339          try {
340              assertFalse(b);
341 <        } catch (AssertionError t) {
341 >        } catch (AssertionFailedError t) {
342              threadRecordFailure(t);
343              throw t;
344          }
# Line 300 | Line 346 | public class JSR166TestCase extends Test
346  
347      /**
348       * Just like assertNull(x), but additionally recording (using
349 <     * threadRecordFailure) any AssertionError thrown, so that the
350 <     * current testcase will fail.
349 >     * threadRecordFailure) any AssertionFailedError thrown, so that
350 >     * the current testcase will fail.
351       */
352      public void threadAssertNull(Object x) {
353          try {
354              assertNull(x);
355 <        } catch (AssertionError t) {
355 >        } catch (AssertionFailedError t) {
356              threadRecordFailure(t);
357              throw t;
358          }
# Line 314 | Line 360 | public class JSR166TestCase extends Test
360  
361      /**
362       * Just like assertEquals(x, y), but additionally recording (using
363 <     * threadRecordFailure) any AssertionError thrown, so that the
364 <     * current testcase will fail.
363 >     * threadRecordFailure) any AssertionFailedError thrown, so that
364 >     * the current testcase will fail.
365       */
366      public void threadAssertEquals(long x, long y) {
367          try {
368              assertEquals(x, y);
369 <        } catch (AssertionError t) {
369 >        } catch (AssertionFailedError t) {
370              threadRecordFailure(t);
371              throw t;
372          }
# Line 328 | Line 374 | public class JSR166TestCase extends Test
374  
375      /**
376       * Just like assertEquals(x, y), but additionally recording (using
377 <     * threadRecordFailure) any AssertionError thrown, so that the
378 <     * current testcase will fail.
377 >     * threadRecordFailure) any AssertionFailedError thrown, so that
378 >     * the current testcase will fail.
379       */
380      public void threadAssertEquals(Object x, Object y) {
381          try {
382              assertEquals(x, y);
383 <        } catch (AssertionError t) {
383 >        } catch (AssertionFailedError t) {
384              threadRecordFailure(t);
385              throw t;
386 +        } catch (Throwable t) {
387 +            threadUnexpectedException(t);
388          }
389      }
390  
391      /**
392       * Just like assertSame(x, y), but additionally recording (using
393 <     * threadRecordFailure) any AssertionError thrown, so that the
394 <     * current testcase will fail.
393 >     * threadRecordFailure) any AssertionFailedError thrown, so that
394 >     * the current testcase will fail.
395       */
396      public void threadAssertSame(Object x, Object y) {
397          try {
398              assertSame(x, y);
399 <        } catch (AssertionError t) {
399 >        } catch (AssertionFailedError t) {
400              threadRecordFailure(t);
401              throw t;
402          }
# Line 369 | Line 417 | public class JSR166TestCase extends Test
417      }
418  
419      /**
420 <     * Calls threadFail with message "Unexpected exception" + ex.
420 >     * Records the given exception using {@link #threadRecordFailure},
421 >     * then rethrows the exception, wrapping it in an
422 >     * AssertionFailedError if necessary.
423       */
424      public void threadUnexpectedException(Throwable t) {
425          threadRecordFailure(t);
426          t.printStackTrace();
377        // Rethrow, wrapping in an AssertionError if necessary
427          if (t instanceof RuntimeException)
428              throw (RuntimeException) t;
429          else if (t instanceof Error)
430              throw (Error) t;
431          else {
432 <            AssertionError ae = new AssertionError("unexpected exception: " + t);
432 >            AssertionFailedError afe =
433 >                new AssertionFailedError("unexpected exception: " + t);
434              t.initCause(t);
435 <            throw ae;
436 <        }            
435 >            throw afe;
436 >        }
437      }
438  
439      /**
# Line 392 | Line 442 | public class JSR166TestCase extends Test
442      public void joinPool(ExecutorService exec) {
443          try {
444              exec.shutdown();
445 <            assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
445 >            assertTrue("ExecutorService did not terminate in a timely manner",
446 >                       exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
447          } catch (SecurityException ok) {
448              // Allowed in case test doesn't have privs
449          } catch (InterruptedException ie) {
# Line 416 | Line 467 | public class JSR166TestCase extends Test
467      }
468  
469      /**
419     * Fails with message "Unexpected exception: " + ex.
420     */
421    public void unexpectedException(Throwable ex) {
422        ex.printStackTrace();
423        fail("Unexpected exception: " + ex);
424    }
425
426
427    /**
470       * The number of elements to place in collections, arrays, etc.
471       */
472      public static final int SIZE = 20;
# Line 535 | Line 577 | public class JSR166TestCase extends Test
577      }
578  
579      /**
580 <     * Sleep until the timeout has elapsed, or interrupted.
580 >     * Sleeps until the given time has elapsed.
581 >     * Throws AssertionFailedError if interrupted.
582 >     */
583 >    void sleep(long millis) {
584 >        try {
585 >            Thread.sleep(millis);
586 >        } catch (InterruptedException ie) {
587 >            AssertionFailedError afe =
588 >                new AssertionFailedError("Unexpected InterruptedException");
589 >            afe.initCause(ie);
590 >            throw afe;
591 >        }
592 >    }
593 >
594 >    /**
595 >     * Sleeps until the timeout has elapsed, or interrupted.
596       * Does <em>NOT</em> throw InterruptedException.
597       */
598      void sleepTillInterrupted(long timeoutMillis) {
# Line 545 | Line 602 | public class JSR166TestCase extends Test
602      }
603  
604      /**
605 <     * Returns a new started Thread running the given runnable.
605 >     * Returns a new started daemon Thread running the given runnable.
606       */
607      Thread newStartedThread(Runnable runnable) {
608          Thread t = new Thread(runnable);
609 +        t.setDaemon(true);
610          t.start();
611          return t;
612      }
613  
614 +    /**
615 +     * Waits for the specified time (in milliseconds) for the thread
616 +     * to terminate (using {@link Thread#join(long)}), else interrupts
617 +     * the thread (in the hope that it may terminate later) and fails.
618 +     */
619 +    void awaitTermination(Thread t, long timeoutMillis) {
620 +        try {
621 +            t.join(timeoutMillis);
622 +        } catch (InterruptedException ie) {
623 +            threadUnexpectedException(ie);
624 +        } finally {
625 +            if (t.isAlive()) {
626 +                t.interrupt();
627 +                fail("Test timed out");
628 +            }
629 +        }
630 +    }
631 +
632      // Some convenient Runnable classes
633  
634      public abstract class CheckedRunnable implements Runnable {
# Line 716 | Line 792 | public class JSR166TestCase extends Test
792          }
793      }
794  
719    public class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
720        protected void realRun() throws InterruptedException {
721            Thread.sleep(SMALL_DELAY_MS);
722        }
723    }
724
795      public class MediumRunnable extends CheckedRunnable {
796          protected void realRun() throws Throwable {
797              Thread.sleep(MEDIUM_DELAY_MS);
# Line 759 | Line 829 | public class JSR166TestCase extends Test
829          }
830      }
831  
832 +    public interface TrackedRunnable extends Runnable {
833 +        boolean isDone();
834 +    }
835 +
836 +    public static TrackedRunnable trackedRunnable(final long timeoutMillis) {
837 +        return new TrackedRunnable() {
838 +                private volatile boolean done = false;
839 +                public boolean isDone() { return done; }
840 +                public void run() {
841 +                    try {
842 +                        Thread.sleep(timeoutMillis);
843 +                        done = true;
844 +                    } catch (InterruptedException ok) {}
845 +                }
846 +            };
847 +    }
848 +
849      public static class TrackedShortRunnable implements Runnable {
850          public volatile boolean done = false;
851          public void run() {
852              try {
853 +                Thread.sleep(SHORT_DELAY_MS);
854 +                done = true;
855 +            } catch (InterruptedException ok) {}
856 +        }
857 +    }
858 +
859 +    public static class TrackedSmallRunnable implements Runnable {
860 +        public volatile boolean done = false;
861 +        public void run() {
862 +            try {
863                  Thread.sleep(SMALL_DELAY_MS);
864                  done = true;
865              } catch (InterruptedException ok) {}
# Line 846 | Line 943 | public class JSR166TestCase extends Test
943                                        ThreadPoolExecutor executor) {}
944      }
945  
946 +    /**
947 +     * A CyclicBarrier that fails with AssertionFailedErrors instead
948 +     * of throwing checked exceptions.
949 +     */
950 +    public class CheckedBarrier extends CyclicBarrier {
951 +        public CheckedBarrier(int parties) { super(parties); }
952 +
953 +        public int await() {
954 +            try {
955 +                return super.await();
956 +            } catch (Exception e) {
957 +                AssertionFailedError afe =
958 +                    new AssertionFailedError("Unexpected exception: " + e);
959 +                afe.initCause(e);
960 +                throw afe;
961 +            }
962 +        }
963 +    }
964 +
965   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines