--- jsr166/src/test/tck/JSR166TestCase.java 2011/05/06 11:22:07 1.76 +++ jsr166/src/test/tck/JSR166TestCase.java 2011/05/09 20:00:19 1.79 @@ -7,6 +7,10 @@ */ import junit.framework.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInputStream; +import java.io.ObjectOutputStream; import java.util.Arrays; import java.util.NoSuchElementException; import java.util.PropertyPermission; @@ -449,7 +453,7 @@ public class JSR166TestCase extends Test */ public static void delay(long ms) throws InterruptedException { long startTime = System.nanoTime(); - long ns = ms * 1000 * 1000; + long ns = ms * 1000 * 1000; for (;;) { if (ms > 0L) Thread.sleep(ms); @@ -457,7 +461,7 @@ public class JSR166TestCase extends Test Thread.yield(); long d = ns - (System.nanoTime() - startTime); if (d > 0L) - ms = d / (1000 * 1000); + ms = d / (1000 * 1000); else break; } @@ -478,6 +482,20 @@ public class JSR166TestCase extends Test } } + /** + * Checks that thread does not terminate within timeoutMillis + * milliseconds (that is, Thread.join times out). + */ + public void assertThreadJoinTimesOut(Thread thread, long timeoutMillis) { + try { + long startTime = System.nanoTime(); + thread.join(timeoutMillis); + assertTrue(thread.isAlive()); + assertTrue(millisElapsedSince(startTime) >= timeoutMillis); + } catch (InterruptedException ie) { + fail("Unexpected InterruptedException"); + } + } /** * Fails with message "should throw exception". @@ -830,10 +848,18 @@ public class JSR166TestCase extends Test public Runnable awaiter(final CountDownLatch latch) { return new CheckedRunnable() { public void realRun() throws InterruptedException { - latch.await(); + await(latch); }}; } + public void await(CountDownLatch latch) { + try { + assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS)); + } catch (Throwable t) { + threadUnexpectedException(t); + } + } + public static class NPETask implements Callable { public String call() { throw new NullPointerException(); } } @@ -1081,4 +1107,21 @@ public class JSR166TestCase extends Test } } + @SuppressWarnings("unchecked") + public T serialClone(T o) { + try { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + ObjectOutputStream oos = new ObjectOutputStream(bos); + oos.writeObject(o); + oos.flush(); + oos.close(); + ByteArrayInputStream bin = + new ByteArrayInputStream(bos.toByteArray()); + ObjectInputStream ois = new ObjectInputStream(bin); + return (T) ois.readObject(); + } catch (Throwable t) { + threadUnexpectedException(t); + return null; + } + } }