--- jsr166/src/test/tck/JSR166TestCase.java 2010/11/21 19:04:45 1.70
+++ jsr166/src/test/tck/JSR166TestCase.java 2011/05/29 14:18:52 1.85
@@ -1,14 +1,22 @@
/*
* Written by Doug Lea with assistance from members of JCP JSR-166
* Expert Group and released to the public domain, as explained at
- * http://creativecommons.org/licenses/publicdomain
+ * http://creativecommons.org/publicdomain/zero/1.0/
* Other contributors include Andrew Wright, Jeffrey Hayes,
* Pat Fisher, Mike Judd.
*/
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.Date;
+import java.util.NoSuchElementException;
import java.util.PropertyPermission;
import java.util.concurrent.*;
+import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicReference;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.NANOSECONDS;
@@ -252,7 +260,6 @@ public class JSR166TestCase extends Test
return 50;
}
-
/**
* Sets delays as multiples of SHORT_DELAY.
*/
@@ -260,7 +267,23 @@ public class JSR166TestCase extends Test
SHORT_DELAY_MS = getShortDelay();
SMALL_DELAY_MS = SHORT_DELAY_MS * 5;
MEDIUM_DELAY_MS = SHORT_DELAY_MS * 10;
- LONG_DELAY_MS = SHORT_DELAY_MS * 50;
+ LONG_DELAY_MS = SHORT_DELAY_MS * 200;
+ }
+
+ /**
+ * Returns a timeout in milliseconds to be used in tests that
+ * verify that operations block or time out.
+ */
+ long timeoutMillis() {
+ return SHORT_DELAY_MS / 4;
+ }
+
+ /**
+ * Returns a new Date instance representing a time delayMillis
+ * milliseconds in the future.
+ */
+ Date delayedDate(long delayMillis) {
+ return new Date(System.currentTimeMillis() + delayMillis);
}
/**
@@ -284,9 +307,13 @@ public class JSR166TestCase extends Test
}
/**
+ * Extra checks that get done for all test cases.
+ *
* Triggers test case failure if any thread assertions have failed,
* by rethrowing, in the test harness thread, any exception recorded
* earlier by threadRecordFailure.
+ *
+ * Triggers test case failure if interrupt status is set in the main thread.
*/
public void tearDown() throws Exception {
Throwable t = threadFailure.getAndSet(null);
@@ -304,6 +331,9 @@ public class JSR166TestCase extends Test
throw afe;
}
}
+
+ if (Thread.interrupted())
+ throw new AssertionFailedError("interrupt status set in main thread");
}
/**
@@ -435,19 +465,40 @@ public class JSR166TestCase extends Test
else {
AssertionFailedError afe =
new AssertionFailedError("unexpected exception: " + t);
- t.initCause(t);
+ afe.initCause(t);
throw afe;
}
}
/**
+ * Delays, via Thread.sleep, for the given millisecond delay, but
+ * if the sleep is shorter than specified, may re-sleep or yield
+ * until time elapses.
+ */
+ static void delay(long millis) throws InterruptedException {
+ long startTime = System.nanoTime();
+ long ns = millis * 1000 * 1000;
+ for (;;) {
+ if (millis > 0L)
+ Thread.sleep(millis);
+ else // too short to sleep
+ Thread.yield();
+ long d = ns - (System.nanoTime() - startTime);
+ if (d > 0L)
+ millis = d / (1000 * 1000);
+ else
+ break;
+ }
+ }
+
+ /**
* Waits out termination of a thread pool or fails doing so.
*/
- public void joinPool(ExecutorService exec) {
+ void joinPool(ExecutorService exec) {
try {
exec.shutdown();
assertTrue("ExecutorService did not terminate in a timely manner",
- exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
+ exec.awaitTermination(2 * LONG_DELAY_MS, MILLISECONDS));
} catch (SecurityException ok) {
// Allowed in case test doesn't have privs
} catch (InterruptedException ie) {
@@ -455,6 +506,49 @@ public class JSR166TestCase extends Test
}
}
+ /**
+ * Checks that thread does not terminate within the default
+ * millisecond delay of {@code timeoutMillis()}.
+ */
+ void assertThreadStaysAlive(Thread thread) {
+ assertThreadStaysAlive(thread, timeoutMillis());
+ }
+
+ /**
+ * Checks that thread does not terminate within the given millisecond delay.
+ */
+ void assertThreadStaysAlive(Thread thread, long millis) {
+ try {
+ // No need to optimize the failing case via Thread.join.
+ delay(millis);
+ assertTrue(thread.isAlive());
+ } catch (InterruptedException ie) {
+ fail("Unexpected InterruptedException");
+ }
+ }
+
+ /**
+ * Checks that future.get times out, with the default timeout of
+ * {@code timeoutMillis()}.
+ */
+ void assertFutureTimesOut(Future future) {
+ assertFutureTimesOut(future, timeoutMillis());
+ }
+
+ /**
+ * Checks that future.get times out, with the given millisecond timeout.
+ */
+ void assertFutureTimesOut(Future future, long timeoutMillis) {
+ long startTime = System.nanoTime();
+ try {
+ future.get(timeoutMillis, MILLISECONDS);
+ shouldThrow();
+ } catch (TimeoutException success) {
+ } catch (Exception e) {
+ threadUnexpectedException(e);
+ } finally { future.cancel(true); }
+ assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
+ }
/**
* Fails with message "should throw exception".
@@ -586,7 +680,7 @@ public class JSR166TestCase extends Test
*/
void sleep(long millis) {
try {
- Thread.sleep(millis);
+ delay(millis);
} catch (InterruptedException ie) {
AssertionFailedError afe =
new AssertionFailedError("Unexpected InterruptedException");
@@ -596,16 +690,6 @@ public class JSR166TestCase extends Test
}
/**
- * Sleeps until the timeout has elapsed, or interrupted.
- * Does NOT throw InterruptedException.
- */
- void sleepTillInterrupted(long timeoutMillis) {
- try {
- Thread.sleep(timeoutMillis);
- } catch (InterruptedException wakeup) {}
- }
-
- /**
* Waits up to the specified number of milliseconds for the given
* thread to enter a wait state: BLOCKED, WAITING, or TIMED_WAITING.
*/
@@ -629,6 +713,14 @@ public class JSR166TestCase extends Test
}
/**
+ * Waits up to LONG_DELAY_MS for the given thread to enter a wait
+ * state: BLOCKED, WAITING, or TIMED_WAITING.
+ */
+ void waitForThreadToEnterWaitState(Thread thread) {
+ waitForThreadToEnterWaitState(thread, LONG_DELAY_MS);
+ }
+
+ /**
* Returns the number of milliseconds since time given by
* startNanoTime, which must have been previously returned from a
* call to {@link System.nanoTime()}.
@@ -658,13 +750,22 @@ public class JSR166TestCase extends Test
} catch (InterruptedException ie) {
threadUnexpectedException(ie);
} finally {
- if (t.isAlive()) {
+ if (t.getState() != Thread.State.TERMINATED) {
t.interrupt();
fail("Test timed out");
}
}
}
+ /**
+ * Waits for LONG_DELAY_MS milliseconds for the thread to
+ * terminate (using {@link Thread#join(long)}), else interrupts
+ * the thread (in the hope that it may terminate later) and fails.
+ */
+ void awaitTermination(Thread t) {
+ awaitTermination(t, LONG_DELAY_MS);
+ }
+
// Some convenient Runnable classes
public abstract class CheckedRunnable implements Runnable {
@@ -727,6 +828,7 @@ public class JSR166TestCase extends Test
realRun();
threadShouldThrow("InterruptedException");
} catch (InterruptedException success) {
+ threadAssertFalse(Thread.interrupted());
} catch (Throwable t) {
threadUnexpectedException(t);
}
@@ -756,6 +858,7 @@ public class JSR166TestCase extends Test
threadShouldThrow("InterruptedException");
return result;
} catch (InterruptedException success) {
+ threadAssertFalse(Thread.interrupted());
} catch (Throwable t) {
threadUnexpectedException(t);
}
@@ -787,6 +890,40 @@ public class JSR166TestCase extends Test
}};
}
+ public Runnable awaiter(final CountDownLatch latch) {
+ return new CheckedRunnable() {
+ public void realRun() throws InterruptedException {
+ await(latch);
+ }};
+ }
+
+ public void await(CountDownLatch latch) {
+ try {
+ assertTrue(latch.await(LONG_DELAY_MS, MILLISECONDS));
+ } catch (Throwable t) {
+ threadUnexpectedException(t);
+ }
+ }
+
+// /**
+// * Spin-waits up to LONG_DELAY_MS until flag becomes true.
+// */
+// public void await(AtomicBoolean flag) {
+// await(flag, LONG_DELAY_MS);
+// }
+
+// /**
+// * Spin-waits up to the specified timeout until flag becomes true.
+// */
+// public void await(AtomicBoolean flag, long timeoutMillis) {
+// long startTime = System.nanoTime();
+// while (!flag.get()) {
+// if (millisElapsedSince(startTime) > timeoutMillis)
+// throw new AssertionFailedError("timed out");
+// Thread.yield();
+// }
+// }
+
public static class NPETask implements Callable {
public String call() { throw new NullPointerException(); }
}
@@ -797,46 +934,46 @@ public class JSR166TestCase extends Test
public class ShortRunnable extends CheckedRunnable {
protected void realRun() throws Throwable {
- Thread.sleep(SHORT_DELAY_MS);
+ delay(SHORT_DELAY_MS);
}
}
public class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
protected void realRun() throws InterruptedException {
- Thread.sleep(SHORT_DELAY_MS);
+ delay(SHORT_DELAY_MS);
}
}
public class SmallRunnable extends CheckedRunnable {
protected void realRun() throws Throwable {
- Thread.sleep(SMALL_DELAY_MS);
+ delay(SMALL_DELAY_MS);
}
}
public class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
protected void realRun() {
try {
- Thread.sleep(SMALL_DELAY_MS);
+ delay(SMALL_DELAY_MS);
} catch (InterruptedException ok) {}
}
}
public class SmallCallable extends CheckedCallable {
protected Object realCall() throws InterruptedException {
- Thread.sleep(SMALL_DELAY_MS);
+ delay(SMALL_DELAY_MS);
return Boolean.TRUE;
}
}
public class MediumRunnable extends CheckedRunnable {
protected void realRun() throws Throwable {
- Thread.sleep(MEDIUM_DELAY_MS);
+ delay(MEDIUM_DELAY_MS);
}
}
public class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
protected void realRun() throws InterruptedException {
- Thread.sleep(MEDIUM_DELAY_MS);
+ delay(MEDIUM_DELAY_MS);
}
}
@@ -844,7 +981,7 @@ public class JSR166TestCase extends Test
return new CheckedRunnable() {
protected void realRun() {
try {
- Thread.sleep(timeoutMillis);
+ delay(timeoutMillis);
} catch (InterruptedException ok) {}
}};
}
@@ -852,7 +989,7 @@ public class JSR166TestCase extends Test
public class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
protected void realRun() {
try {
- Thread.sleep(MEDIUM_DELAY_MS);
+ delay(MEDIUM_DELAY_MS);
} catch (InterruptedException ok) {}
}
}
@@ -860,7 +997,7 @@ public class JSR166TestCase extends Test
public class LongPossiblyInterruptedRunnable extends CheckedRunnable {
protected void realRun() {
try {
- Thread.sleep(LONG_DELAY_MS);
+ delay(LONG_DELAY_MS);
} catch (InterruptedException ok) {}
}
}
@@ -884,7 +1021,7 @@ public class JSR166TestCase extends Test
public boolean isDone() { return done; }
public void run() {
try {
- Thread.sleep(timeoutMillis);
+ delay(timeoutMillis);
done = true;
} catch (InterruptedException ok) {}
}
@@ -895,7 +1032,7 @@ public class JSR166TestCase extends Test
public volatile boolean done = false;
public void run() {
try {
- Thread.sleep(SHORT_DELAY_MS);
+ delay(SHORT_DELAY_MS);
done = true;
} catch (InterruptedException ok) {}
}
@@ -905,7 +1042,7 @@ public class JSR166TestCase extends Test
public volatile boolean done = false;
public void run() {
try {
- Thread.sleep(SMALL_DELAY_MS);
+ delay(SMALL_DELAY_MS);
done = true;
} catch (InterruptedException ok) {}
}
@@ -915,7 +1052,7 @@ public class JSR166TestCase extends Test
public volatile boolean done = false;
public void run() {
try {
- Thread.sleep(MEDIUM_DELAY_MS);
+ delay(MEDIUM_DELAY_MS);
done = true;
} catch (InterruptedException ok) {}
}
@@ -925,7 +1062,7 @@ public class JSR166TestCase extends Test
public volatile boolean done = false;
public void run() {
try {
- Thread.sleep(LONG_DELAY_MS);
+ delay(LONG_DELAY_MS);
done = true;
} catch (InterruptedException ok) {}
}
@@ -942,7 +1079,7 @@ public class JSR166TestCase extends Test
public volatile boolean done = false;
public Object call() {
try {
- Thread.sleep(SMALL_DELAY_MS);
+ delay(SMALL_DELAY_MS);
done = true;
} catch (InterruptedException ok) {}
return Boolean.TRUE;
@@ -1007,4 +1144,48 @@ public class JSR166TestCase extends Test
}
}
+ void checkEmpty(BlockingQueue q) {
+ try {
+ assertTrue(q.isEmpty());
+ assertEquals(0, q.size());
+ assertNull(q.peek());
+ assertNull(q.poll());
+ assertNull(q.poll(0, MILLISECONDS));
+ assertEquals(q.toString(), "[]");
+ assertTrue(Arrays.equals(q.toArray(), new Object[0]));
+ assertFalse(q.iterator().hasNext());
+ try {
+ q.element();
+ shouldThrow();
+ } catch (NoSuchElementException success) {}
+ try {
+ q.iterator().next();
+ shouldThrow();
+ } catch (NoSuchElementException success) {}
+ try {
+ q.remove();
+ shouldThrow();
+ } catch (NoSuchElementException success) {}
+ } catch (InterruptedException ie) {
+ threadUnexpectedException(ie);
+ }
+ }
+
+ @SuppressWarnings("unchecked")
+ 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;
+ }
+ }
}