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.57 by jsr166, Mon Oct 4 05:45:19 2010 UTC vs.
Revision 1.79 by jsr166, Mon May 9 20:00:19 2011 UTC

# Line 1 | Line 1
1   /*
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4 < * http://creativecommons.org/licenses/publicdomain
4 > * http://creativecommons.org/publicdomain/zero/1.0/
5   * Other contributors include Andrew Wright, Jeffrey Hayes,
6   * Pat Fisher, Mike Judd.
7   */
8  
9   import junit.framework.*;
10 + import java.io.ByteArrayInputStream;
11 + import java.io.ByteArrayOutputStream;
12 + import java.io.ObjectInputStream;
13 + import java.io.ObjectOutputStream;
14 + import java.util.Arrays;
15 + import java.util.NoSuchElementException;
16   import java.util.PropertyPermission;
17   import java.util.concurrent.*;
18   import java.util.concurrent.atomic.AtomicReference;
19   import static java.util.concurrent.TimeUnit.MILLISECONDS;
20 + import static java.util.concurrent.TimeUnit.NANOSECONDS;
21   import java.security.CodeSource;
22   import java.security.Permission;
23   import java.security.PermissionCollection;
# Line 96 | Line 103 | public class JSR166TestCase extends Test
103      private static final boolean useSecurityManager =
104          Boolean.getBoolean("jsr166.useSecurityManager");
105  
106 +    protected static final boolean expensiveTests =
107 +        Boolean.getBoolean("jsr166.expensiveTests");
108 +
109 +    /**
110 +     * If true, report on stdout all "slow" tests, that is, ones that
111 +     * take more than profileThreshold milliseconds to execute.
112 +     */
113 +    private static final boolean profileTests =
114 +        Boolean.getBoolean("jsr166.profileTests");
115 +
116 +    /**
117 +     * The number of milliseconds that tests are permitted for
118 +     * execution without being reported, when profileTests is set.
119 +     */
120 +    private static final long profileThreshold =
121 +        Long.getLong("jsr166.profileThreshold", 100);
122 +
123 +    protected void runTest() throws Throwable {
124 +        if (profileTests)
125 +            runTestProfiled();
126 +        else
127 +            super.runTest();
128 +    }
129 +
130 +    protected void runTestProfiled() throws Throwable {
131 +        long t0 = System.nanoTime();
132 +        try {
133 +            super.runTest();
134 +        } finally {
135 +            long elapsedMillis =
136 +                (System.nanoTime() - t0) / (1000L * 1000L);
137 +            if (elapsedMillis >= profileThreshold)
138 +                System.out.printf("%n%s: %d%n", toString(), elapsedMillis);
139 +        }
140 +    }
141 +
142      /**
143       * Runs all JSR166 unit tests using junit.textui.TestRunner
144       */
# Line 116 | Line 159 | public class JSR166TestCase extends Test
159          System.exit(0);
160      }
161  
162 +    public static TestSuite newTestSuite(Object... suiteOrClasses) {
163 +        TestSuite suite = new TestSuite();
164 +        for (Object suiteOrClass : suiteOrClasses) {
165 +            if (suiteOrClass instanceof TestSuite)
166 +                suite.addTest((TestSuite) suiteOrClass);
167 +            else if (suiteOrClass instanceof Class)
168 +                suite.addTest(new TestSuite((Class<?>) suiteOrClass));
169 +            else
170 +                throw new ClassCastException("not a test suite or class");
171 +        }
172 +        return suite;
173 +    }
174 +
175      /**
176 <     * Collects all JSR166 unit tests as one suite
176 >     * Collects all JSR166 unit tests as one suite.
177       */
178      public static Test suite() {
179 <        TestSuite suite = new TestSuite("JSR166 Unit Tests");
180 <
181 <        suite.addTest(ForkJoinPoolTest.suite());
182 <        suite.addTest(ForkJoinTaskTest.suite());
183 <        suite.addTest(RecursiveActionTest.suite());
184 <        suite.addTest(RecursiveTaskTest.suite());
185 <        suite.addTest(LinkedTransferQueueTest.suite());
186 <        suite.addTest(PhaserTest.suite());
187 <        suite.addTest(ThreadLocalRandomTest.suite());
188 <        suite.addTest(AbstractExecutorServiceTest.suite());
189 <        suite.addTest(AbstractQueueTest.suite());
190 <        suite.addTest(AbstractQueuedSynchronizerTest.suite());
191 <        suite.addTest(AbstractQueuedLongSynchronizerTest.suite());
192 <        suite.addTest(ArrayBlockingQueueTest.suite());
193 <        suite.addTest(ArrayDequeTest.suite());
194 <        suite.addTest(AtomicBooleanTest.suite());
195 <        suite.addTest(AtomicIntegerArrayTest.suite());
196 <        suite.addTest(AtomicIntegerFieldUpdaterTest.suite());
197 <        suite.addTest(AtomicIntegerTest.suite());
198 <        suite.addTest(AtomicLongArrayTest.suite());
199 <        suite.addTest(AtomicLongFieldUpdaterTest.suite());
200 <        suite.addTest(AtomicLongTest.suite());
201 <        suite.addTest(AtomicMarkableReferenceTest.suite());
202 <        suite.addTest(AtomicReferenceArrayTest.suite());
203 <        suite.addTest(AtomicReferenceFieldUpdaterTest.suite());
204 <        suite.addTest(AtomicReferenceTest.suite());
205 <        suite.addTest(AtomicStampedReferenceTest.suite());
206 <        suite.addTest(ConcurrentHashMapTest.suite());
207 <        suite.addTest(ConcurrentLinkedDequeTest.suite());
208 <        suite.addTest(ConcurrentLinkedQueueTest.suite());
209 <        suite.addTest(ConcurrentSkipListMapTest.suite());
210 <        suite.addTest(ConcurrentSkipListSubMapTest.suite());
211 <        suite.addTest(ConcurrentSkipListSetTest.suite());
212 <        suite.addTest(ConcurrentSkipListSubSetTest.suite());
213 <        suite.addTest(CopyOnWriteArrayListTest.suite());
214 <        suite.addTest(CopyOnWriteArraySetTest.suite());
215 <        suite.addTest(CountDownLatchTest.suite());
216 <        suite.addTest(CyclicBarrierTest.suite());
217 <        suite.addTest(DelayQueueTest.suite());
218 <        suite.addTest(EntryTest.suite());
219 <        suite.addTest(ExchangerTest.suite());
220 <        suite.addTest(ExecutorsTest.suite());
221 <        suite.addTest(ExecutorCompletionServiceTest.suite());
222 <        suite.addTest(FutureTaskTest.suite());
223 <        suite.addTest(LinkedBlockingDequeTest.suite());
224 <        suite.addTest(LinkedBlockingQueueTest.suite());
225 <        suite.addTest(LinkedListTest.suite());
226 <        suite.addTest(LockSupportTest.suite());
227 <        suite.addTest(PriorityBlockingQueueTest.suite());
228 <        suite.addTest(PriorityQueueTest.suite());
229 <        suite.addTest(ReentrantLockTest.suite());
230 <        suite.addTest(ReentrantReadWriteLockTest.suite());
231 <        suite.addTest(ScheduledExecutorTest.suite());
232 <        suite.addTest(ScheduledExecutorSubclassTest.suite());
233 <        suite.addTest(SemaphoreTest.suite());
234 <        suite.addTest(SynchronousQueueTest.suite());
235 <        suite.addTest(SystemTest.suite());
236 <        suite.addTest(ThreadLocalTest.suite());
237 <        suite.addTest(ThreadPoolExecutorTest.suite());
238 <        suite.addTest(ThreadPoolExecutorSubclassTest.suite());
239 <        suite.addTest(ThreadTest.suite());
240 <        suite.addTest(TimeUnitTest.suite());
241 <        suite.addTest(TreeMapTest.suite());
242 <        suite.addTest(TreeSetTest.suite());
243 <        suite.addTest(TreeSubMapTest.suite());
188 <        suite.addTest(TreeSubSetTest.suite());
189 <
190 <        return suite;
179 >        return newTestSuite(
180 >            ForkJoinPoolTest.suite(),
181 >            ForkJoinTaskTest.suite(),
182 >            RecursiveActionTest.suite(),
183 >            RecursiveTaskTest.suite(),
184 >            LinkedTransferQueueTest.suite(),
185 >            PhaserTest.suite(),
186 >            ThreadLocalRandomTest.suite(),
187 >            AbstractExecutorServiceTest.suite(),
188 >            AbstractQueueTest.suite(),
189 >            AbstractQueuedSynchronizerTest.suite(),
190 >            AbstractQueuedLongSynchronizerTest.suite(),
191 >            ArrayBlockingQueueTest.suite(),
192 >            ArrayDequeTest.suite(),
193 >            AtomicBooleanTest.suite(),
194 >            AtomicIntegerArrayTest.suite(),
195 >            AtomicIntegerFieldUpdaterTest.suite(),
196 >            AtomicIntegerTest.suite(),
197 >            AtomicLongArrayTest.suite(),
198 >            AtomicLongFieldUpdaterTest.suite(),
199 >            AtomicLongTest.suite(),
200 >            AtomicMarkableReferenceTest.suite(),
201 >            AtomicReferenceArrayTest.suite(),
202 >            AtomicReferenceFieldUpdaterTest.suite(),
203 >            AtomicReferenceTest.suite(),
204 >            AtomicStampedReferenceTest.suite(),
205 >            ConcurrentHashMapTest.suite(),
206 >            ConcurrentLinkedDequeTest.suite(),
207 >            ConcurrentLinkedQueueTest.suite(),
208 >            ConcurrentSkipListMapTest.suite(),
209 >            ConcurrentSkipListSubMapTest.suite(),
210 >            ConcurrentSkipListSetTest.suite(),
211 >            ConcurrentSkipListSubSetTest.suite(),
212 >            CopyOnWriteArrayListTest.suite(),
213 >            CopyOnWriteArraySetTest.suite(),
214 >            CountDownLatchTest.suite(),
215 >            CyclicBarrierTest.suite(),
216 >            DelayQueueTest.suite(),
217 >            EntryTest.suite(),
218 >            ExchangerTest.suite(),
219 >            ExecutorsTest.suite(),
220 >            ExecutorCompletionServiceTest.suite(),
221 >            FutureTaskTest.suite(),
222 >            LinkedBlockingDequeTest.suite(),
223 >            LinkedBlockingQueueTest.suite(),
224 >            LinkedListTest.suite(),
225 >            LockSupportTest.suite(),
226 >            PriorityBlockingQueueTest.suite(),
227 >            PriorityQueueTest.suite(),
228 >            ReentrantLockTest.suite(),
229 >            ReentrantReadWriteLockTest.suite(),
230 >            ScheduledExecutorTest.suite(),
231 >            ScheduledExecutorSubclassTest.suite(),
232 >            SemaphoreTest.suite(),
233 >            SynchronousQueueTest.suite(),
234 >            SystemTest.suite(),
235 >            ThreadLocalTest.suite(),
236 >            ThreadPoolExecutorTest.suite(),
237 >            ThreadPoolExecutorSubclassTest.suite(),
238 >            ThreadTest.suite(),
239 >            TimeUnitTest.suite(),
240 >            TreeMapTest.suite(),
241 >            TreeSetTest.suite(),
242 >            TreeSubMapTest.suite(),
243 >            TreeSubSetTest.suite());
244      }
245  
246  
# Line 213 | Line 266 | public class JSR166TestCase extends Test
266          SHORT_DELAY_MS = getShortDelay();
267          SMALL_DELAY_MS  = SHORT_DELAY_MS * 5;
268          MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
269 <        LONG_DELAY_MS   = SHORT_DELAY_MS * 50;
269 >        LONG_DELAY_MS   = SHORT_DELAY_MS * 200;
270      }
271  
272      /**
# Line 242 | Line 295 | public class JSR166TestCase extends Test
295       * earlier by threadRecordFailure.
296       */
297      public void tearDown() throws Exception {
298 <        Throwable t = threadFailure.get();
298 >        Throwable t = threadFailure.getAndSet(null);
299          if (t != null) {
300              if (t instanceof Error)
301                  throw (Error) t;
# Line 394 | Line 447 | public class JSR166TestCase extends Test
447      }
448  
449      /**
450 +     * Delays, via Thread.sleep for the given millisecond delay, but
451 +     * if the sleep is shorter than specified, may re-sleep or yield
452 +     * until time elapses.
453 +     */
454 +    public static void delay(long ms) throws InterruptedException {
455 +        long startTime = System.nanoTime();
456 +        long ns = ms * 1000 * 1000;
457 +        for (;;) {
458 +            if (ms > 0L)
459 +                Thread.sleep(ms);
460 +            else // too short to sleep
461 +                Thread.yield();
462 +            long d = ns - (System.nanoTime() - startTime);
463 +            if (d > 0L)
464 +                ms = d / (1000 * 1000);
465 +            else
466 +                break;
467 +        }
468 +    }
469 +
470 +    /**
471       * Waits out termination of a thread pool or fails doing so.
472       */
473      public void joinPool(ExecutorService exec) {
474          try {
475              exec.shutdown();
476              assertTrue("ExecutorService did not terminate in a timely manner",
477 <                       exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
477 >                       exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
478          } catch (SecurityException ok) {
479              // Allowed in case test doesn't have privs
480          } catch (InterruptedException ie) {
# Line 408 | Line 482 | public class JSR166TestCase extends Test
482          }
483      }
484  
485 +    /**
486 +     * Checks that thread does not terminate within timeoutMillis
487 +     * milliseconds (that is, Thread.join times out).
488 +     */
489 +    public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) {
490 +        try {
491 +            long startTime = System.nanoTime();
492 +            thread.join(timeoutMillis);
493 +            assertTrue(thread.isAlive());
494 +            assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
495 +        } catch (InterruptedException ie) {
496 +            fail("Unexpected InterruptedException");
497 +        }
498 +    }
499  
500      /**
501       * Fails with message "should throw exception".
# Line 534 | Line 622 | public class JSR166TestCase extends Test
622      }
623  
624      /**
625 <     * Sleep until the timeout has elapsed, or interrupted.
625 >     * Sleeps until the given time has elapsed.
626 >     * Throws AssertionFailedError if interrupted.
627 >     */
628 >    void sleep(long millis) {
629 >        try {
630 >            delay(millis);
631 >        } catch (InterruptedException ie) {
632 >            AssertionFailedError afe =
633 >                new AssertionFailedError("Unexpected InterruptedException");
634 >            afe.initCause(ie);
635 >            throw afe;
636 >        }
637 >    }
638 >
639 >    /**
640 >     * Sleeps until the timeout has elapsed, or interrupted.
641       * Does <em>NOT</em> throw InterruptedException.
642       */
643      void sleepTillInterrupted(long timeoutMillis) {
# Line 544 | Line 647 | public class JSR166TestCase extends Test
647      }
648  
649      /**
650 <     * Returns a new started Thread running the given runnable.
650 >     * Waits up to the specified number of milliseconds for the given
651 >     * thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
652 >     */
653 >    void waitForThreadToEnterWaitState(Thread thread, long timeoutMillis) {
654 >        long timeoutNanos = timeoutMillis * 1000L * 1000L;
655 >        long t0 = System.nanoTime();
656 >        for (;;) {
657 >            Thread.State s = thread.getState();
658 >            if (s == Thread.State.BLOCKED ||
659 >                s == Thread.State.WAITING ||
660 >                s == Thread.State.TIMED_WAITING)
661 >                return;
662 >            else if (s == Thread.State.TERMINATED)
663 >                fail("Unexpected thread termination");
664 >            else if (System.nanoTime() - t0 > timeoutNanos) {
665 >                threadAssertTrue(thread.isAlive());
666 >                return;
667 >            }
668 >            Thread.yield();
669 >        }
670 >    }
671 >
672 >    /**
673 >     * Waits up to LONG_DELAY_MS for the given thread to enter a wait
674 >     * state: BLOCKED, WAITING, or TIMED_WAITING.
675 >     */
676 >    void waitForThreadToEnterWaitState(Thread thread) {
677 >        waitForThreadToEnterWaitState(thread, LONG_DELAY_MS);
678 >    }
679 >
680 >    /**
681 >     * Returns the number of milliseconds since time given by
682 >     * startNanoTime, which must have been previously returned from a
683 >     * call to {@link System.nanoTime()}.
684 >     */
685 >    long millisElapsedSince(long startNanoTime) {
686 >        return NANOSECONDS.toMillis(System.nanoTime() - startNanoTime);
687 >    }
688 >
689 >    /**
690 >     * Returns a new started daemon Thread running the given runnable.
691       */
692      Thread newStartedThread(Runnable runnable) {
693          Thread t = new Thread(runnable);
694 +        t.setDaemon(true);
695          t.start();
696          return t;
697      }
698  
699 +    /**
700 +     * Waits for the specified time (in milliseconds) for the thread
701 +     * to terminate (using {@link Thread#join(long)}), else interrupts
702 +     * the thread (in the hope that it may terminate later) and fails.
703 +     */
704 +    void awaitTermination(Thread t, long timeoutMillis) {
705 +        try {
706 +            t.join(timeoutMillis);
707 +        } catch (InterruptedException ie) {
708 +            threadUnexpectedException(ie);
709 +        } finally {
710 +            if (t.isAlive()) {
711 +                t.interrupt();
712 +                fail("Test timed out");
713 +            }
714 +        }
715 +    }
716 +
717 +    /**
718 +     * Waits for LONG_DELAY_MS milliseconds for the thread to
719 +     * terminate (using {@link Thread#join(long)}), else interrupts
720 +     * the thread (in the hope that it may terminate later) and fails.
721 +     */
722 +    void awaitTermination(Thread t) {
723 +        awaitTermination(t, LONG_DELAY_MS);
724 +    }
725 +
726      // Some convenient Runnable classes
727  
728      public abstract class CheckedRunnable implements Runnable {
# Line 666 | Line 837 | public class JSR166TestCase extends Test
837  
838      public Callable<String> latchAwaitingStringTask(final CountDownLatch latch) {
839          return new CheckedCallable<String>() {
840 <            public String realCall() {
840 >            protected String realCall() {
841                  try {
842                      latch.await();
843                  } catch (InterruptedException quittingTime) {}
# Line 674 | Line 845 | public class JSR166TestCase extends Test
845              }};
846      }
847  
848 +    public Runnable awaiter(final CountDownLatch latch) {
849 +        return new CheckedRunnable() {
850 +            public void realRun() throws InterruptedException {
851 +                await(latch);
852 +            }};
853 +    }
854 +
855 +    public void await(CountDownLatch latch) {
856 +        try {
857 +            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
858 +        } catch (Throwable t) {
859 +            threadUnexpectedException(t);
860 +        }
861 +    }
862 +
863      public static class NPETask implements Callable<String> {
864          public String call() { throw new NullPointerException(); }
865      }
# Line 684 | Line 870 | public class JSR166TestCase extends Test
870  
871      public class ShortRunnable extends CheckedRunnable {
872          protected void realRun() throws Throwable {
873 <            Thread.sleep(SHORT_DELAY_MS);
873 >            delay(SHORT_DELAY_MS);
874          }
875      }
876  
877      public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
878          protected void realRun() throws InterruptedException {
879 <            Thread.sleep(SHORT_DELAY_MS);
879 >            delay(SHORT_DELAY_MS);
880          }
881      }
882  
883      public class SmallRunnable extends CheckedRunnable {
884          protected void realRun() throws Throwable {
885 <            Thread.sleep(SMALL_DELAY_MS);
885 >            delay(SMALL_DELAY_MS);
886          }
887      }
888  
889      public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
890          protected void realRun() {
891              try {
892 <                Thread.sleep(SMALL_DELAY_MS);
892 >                delay(SMALL_DELAY_MS);
893              } catch (InterruptedException ok) {}
894          }
895      }
896  
897      public class SmallCallable extends CheckedCallable {
898          protected Object realCall() throws InterruptedException {
899 <            Thread.sleep(SMALL_DELAY_MS);
899 >            delay(SMALL_DELAY_MS);
900              return Boolean.TRUE;
901          }
902      }
903  
904      public class MediumRunnable extends CheckedRunnable {
905          protected void realRun() throws Throwable {
906 <            Thread.sleep(MEDIUM_DELAY_MS);
906 >            delay(MEDIUM_DELAY_MS);
907          }
908      }
909  
910      public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
911          protected void realRun() throws InterruptedException {
912 <            Thread.sleep(MEDIUM_DELAY_MS);
912 >            delay(MEDIUM_DELAY_MS);
913          }
914      }
915  
916 +    public Runnable possiblyInterruptedRunnable(final long timeoutMillis) {
917 +        return new CheckedRunnable() {
918 +            protected void realRun() {
919 +                try {
920 +                    delay(timeoutMillis);
921 +                } catch (InterruptedException ok) {}
922 +            }};
923 +    }
924 +
925      public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
926          protected void realRun() {
927              try {
928 <                Thread.sleep(MEDIUM_DELAY_MS);
928 >                delay(MEDIUM_DELAY_MS);
929              } catch (InterruptedException ok) {}
930          }
931      }
# Line 738 | Line 933 | public class JSR166TestCase extends Test
933      public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
934          protected void realRun() {
935              try {
936 <                Thread.sleep(LONG_DELAY_MS);
936 >                delay(LONG_DELAY_MS);
937              } catch (InterruptedException ok) {}
938          }
939      }
# Line 752 | Line 947 | public class JSR166TestCase extends Test
947          }
948      }
949  
950 +    public interface TrackedRunnable extends Runnable {
951 +        boolean isDone();
952 +    }
953 +
954 +    public static TrackedRunnable trackedRunnable(final long timeoutMillis) {
955 +        return new TrackedRunnable() {
956 +                private volatile boolean done = false;
957 +                public boolean isDone() { return done; }
958 +                public void run() {
959 +                    try {
960 +                        delay(timeoutMillis);
961 +                        done = true;
962 +                    } catch (InterruptedException ok) {}
963 +                }
964 +            };
965 +    }
966 +
967      public static class TrackedShortRunnable implements Runnable {
968          public volatile boolean done = false;
969          public void run() {
970              try {
971 <                Thread.sleep(SMALL_DELAY_MS);
971 >                delay(SHORT_DELAY_MS);
972 >                done = true;
973 >            } catch (InterruptedException ok) {}
974 >        }
975 >    }
976 >
977 >    public static class TrackedSmallRunnable implements Runnable {
978 >        public volatile boolean done = false;
979 >        public void run() {
980 >            try {
981 >                delay(SMALL_DELAY_MS);
982                  done = true;
983              } catch (InterruptedException ok) {}
984          }
# Line 766 | Line 988 | public class JSR166TestCase extends Test
988          public volatile boolean done = false;
989          public void run() {
990              try {
991 <                Thread.sleep(MEDIUM_DELAY_MS);
991 >                delay(MEDIUM_DELAY_MS);
992                  done = true;
993              } catch (InterruptedException ok) {}
994          }
# Line 776 | Line 998 | public class JSR166TestCase extends Test
998          public volatile boolean done = false;
999          public void run() {
1000              try {
1001 <                Thread.sleep(LONG_DELAY_MS);
1001 >                delay(LONG_DELAY_MS);
1002                  done = true;
1003              } catch (InterruptedException ok) {}
1004          }
# Line 793 | Line 1015 | public class JSR166TestCase extends Test
1015          public volatile boolean done = false;
1016          public Object call() {
1017              try {
1018 <                Thread.sleep(SMALL_DELAY_MS);
1018 >                delay(SMALL_DELAY_MS);
1019                  done = true;
1020              } catch (InterruptedException ok) {}
1021              return Boolean.TRUE;
# Line 839 | Line 1061 | public class JSR166TestCase extends Test
1061                                        ThreadPoolExecutor executor) {}
1062      }
1063  
1064 +    /**
1065 +     * A CyclicBarrier that fails with AssertionFailedErrors instead
1066 +     * of throwing checked exceptions.
1067 +     */
1068 +    public class CheckedBarrier extends CyclicBarrier {
1069 +        public CheckedBarrier(int parties) { super(parties); }
1070 +
1071 +        public int await() {
1072 +            try {
1073 +                return super.await();
1074 +            } catch (Exception e) {
1075 +                AssertionFailedError afe =
1076 +                    new AssertionFailedError("Unexpected exception: " + e);
1077 +                afe.initCause(e);
1078 +                throw afe;
1079 +            }
1080 +        }
1081 +    }
1082 +
1083 +    public void checkEmpty(BlockingQueue q) {
1084 +        try {
1085 +            assertTrue(q.isEmpty());
1086 +            assertEquals(0, q.size());
1087 +            assertNull(q.peek());
1088 +            assertNull(q.poll());
1089 +            assertNull(q.poll(0, MILLISECONDS));
1090 +            assertEquals(q.toString(), "[]");
1091 +            assertTrue(Arrays.equals(q.toArray(), new Object[0]));
1092 +            assertFalse(q.iterator().hasNext());
1093 +            try {
1094 +                q.element();
1095 +                shouldThrow();
1096 +            } catch (NoSuchElementException success) {}
1097 +            try {
1098 +                q.iterator().next();
1099 +                shouldThrow();
1100 +            } catch (NoSuchElementException success) {}
1101 +            try {
1102 +                q.remove();
1103 +                shouldThrow();
1104 +            } catch (NoSuchElementException success) {}
1105 +        } catch (InterruptedException ie) {
1106 +            threadUnexpectedException(ie);
1107 +        }
1108 +    }
1109 +
1110 +    @SuppressWarnings("unchecked")
1111 +    public <T> T serialClone(T o) {
1112 +        try {
1113 +            ByteArrayOutputStream bos = new ByteArrayOutputStream();
1114 +            ObjectOutputStream oos = new ObjectOutputStream(bos);
1115 +            oos.writeObject(o);
1116 +            oos.flush();
1117 +            oos.close();
1118 +            ByteArrayInputStream bin =
1119 +                new ByteArrayInputStream(bos.toByteArray());
1120 +            ObjectInputStream ois = new ObjectInputStream(bin);
1121 +            return (T) ois.readObject();
1122 +        } catch (Throwable t) {
1123 +            threadUnexpectedException(t);
1124 +            return null;
1125 +        }
1126 +    }
1127   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines