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.77 by jsr166, Fri May 6 17:26:29 2011 UTC vs.
Revision 1.80 by jsr166, Fri May 13 21:48:58 2011 UTC

# Line 7 | Line 7
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;
# Line 447 | Line 451 | public class JSR166TestCase extends Test
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 {
454 >    public static void delay(long millis) throws InterruptedException {
455          long startTime = System.nanoTime();
456 <        long ns = ms * 1000 * 1000;
456 >        long ns = millis * 1000 * 1000;
457          for (;;) {
458 <            if (ms > 0L)
459 <                Thread.sleep(ms);
458 >            if (millis > 0L)
459 >                Thread.sleep(millis);
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);
464 >                millis = d / (1000 * 1000);
465              else
466                  break;
467          }
# Line 478 | Line 482 | public class JSR166TestCase extends Test
482          }
483      }
484  
485 +    /**
486 +     * Checks that thread does not terminate within the given millisecond delay.
487 +     */
488 +    public void assertThreadStaysAlive(Thread thread, long millis) {
489 +        try {
490 +            // No need to optimize the failing case via Thread.join.
491 +            delay(millis);
492 +            assertTrue(thread.isAlive());
493 +        } catch (InterruptedException ie) {
494 +            fail("Unexpected InterruptedException");
495 +        }
496 +    }
497  
498      /**
499       * Fails with message "should throw exception".
# Line 830 | Line 846 | public class JSR166TestCase extends Test
846      public Runnable awaiter(final CountDownLatch latch) {
847          return new CheckedRunnable() {
848              public void realRun() throws InterruptedException {
849 <                latch.await();
849 >                await(latch);
850              }};
851      }
852  
853 +    public void await(CountDownLatch latch) {
854 +        try {
855 +            assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
856 +        } catch (Throwable t) {
857 +            threadUnexpectedException(t);
858 +        }
859 +    }
860 +
861      public static class NPETask implements Callable<String> {
862          public String call() { throw new NullPointerException(); }
863      }
# Line 1081 | Line 1105 | public class JSR166TestCase extends Test
1105          }
1106      }
1107  
1108 +    @SuppressWarnings("unchecked")
1109 +    public <T> T serialClone(T o) {
1110 +        try {
1111 +            ByteArrayOutputStream bos = new ByteArrayOutputStream();
1112 +            ObjectOutputStream oos = new ObjectOutputStream(bos);
1113 +            oos.writeObject(o);
1114 +            oos.flush();
1115 +            oos.close();
1116 +            ByteArrayInputStream bin =
1117 +                new ByteArrayInputStream(bos.toByteArray());
1118 +            ObjectInputStream ois = new ObjectInputStream(bin);
1119 +            return (T) ois.readObject();
1120 +        } catch (Throwable t) {
1121 +            threadUnexpectedException(t);
1122 +            return null;
1123 +        }
1124 +    }
1125   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines