--- jsr166/src/test/tck/JSR166TestCase.java 2009/07/31 23:53:23 1.34
+++ jsr166/src/test/tck/JSR166TestCase.java 2009/11/17 21:51:45 1.39
@@ -9,6 +9,7 @@
import junit.framework.*;
import java.util.*;
import java.util.concurrent.*;
+import static java.util.concurrent.TimeUnit.MILLISECONDS;
import java.io.*;
import java.security.*;
@@ -288,13 +289,8 @@ public class JSR166TestCase extends Test
* threadFail with message "should throw exception"
*/
public void threadShouldThrow() {
- try {
- threadFailed = true;
- fail("should throw exception");
- } catch (AssertionFailedError e) {
- e.printStackTrace();
- throw e;
- }
+ threadFailed = true;
+ fail("should throw exception");
}
/**
@@ -320,7 +316,7 @@ public class JSR166TestCase extends Test
public void joinPool(ExecutorService exec) {
try {
exec.shutdown();
- assertTrue(exec.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
+ assertTrue(exec.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
} catch (SecurityException ok) {
// Allowed in case test doesn't have privs
} catch (InterruptedException ie) {
@@ -343,6 +339,14 @@ public class JSR166TestCase extends Test
fail("Unexpected exception");
}
+ /**
+ * fail with message "Unexpected exception", with argument
+ */
+ public void unexpectedException(Throwable ex) {
+ ex.printStackTrace();
+ fail("Unexpected exception: " + ex);
+ }
+
/**
* The number of elements to place in collections, arrays, etc.
@@ -391,9 +395,67 @@ public class JSR166TestCase extends Test
public void refresh() {}
}
+ /**
+ * Sleep until the timeout has elapsed, or interrupted.
+ * Does NOT throw InterruptedException.
+ */
+ void sleepTillInterrupted(long timeoutMillis) {
+ try {
+ Thread.sleep(timeoutMillis);
+ } catch (InterruptedException wakeup) {
+ }
+ }
+
+ /**
+ * Returns a new started Thread running the given runnable.
+ */
+ Thread newStartedThread(Runnable runnable) {
+ Thread t = new Thread(runnable);
+ t.start();
+ return t;
+ }
// Some convenient Runnable classes
+ abstract class CheckedRunnable implements Runnable {
+ abstract void realRun() throws Throwable;
+
+ public final void run() {
+ try {
+ realRun();
+ } catch (Throwable t) {
+ threadUnexpectedException(t);
+ }
+ }
+ }
+
+ abstract class CheckedInterruptedRunnable implements Runnable {
+ abstract void realRun() throws Throwable;
+
+ public final void run() {
+ try {
+ realRun();
+ threadShouldThrow();
+ } catch (InterruptedException success) {
+ } catch (Throwable t) {
+ threadUnexpectedException(t);
+ }
+ }
+ }
+
+ abstract class CheckedCallable implements Callable {
+ abstract T realCall() throws Throwable;
+
+ public final T call() {
+ try {
+ return realCall();
+ } catch (Throwable t) {
+ threadUnexpectedException(t);
+ return null;
+ }
+ }
+ }
+
static class NoOpRunnable implements Runnable {
public void run() {}
}
@@ -416,111 +478,75 @@ public class JSR166TestCase extends Test
public Integer call() { return one; }
}
- class ShortRunnable implements Runnable {
- public void run() {
- try {
- Thread.sleep(SHORT_DELAY_MS);
- }
- catch (Exception e) {
- threadUnexpectedException(e);
- }
+ class ShortRunnable extends CheckedRunnable {
+ void realRun() throws Throwable {
+ Thread.sleep(SHORT_DELAY_MS);
}
}
- class ShortInterruptedRunnable implements Runnable {
- public void run() {
- try {
- Thread.sleep(SHORT_DELAY_MS);
- threadShouldThrow();
- }
- catch (InterruptedException success) {
- }
+ class ShortInterruptedRunnable extends CheckedInterruptedRunnable {
+ void realRun() throws InterruptedException {
+ Thread.sleep(SHORT_DELAY_MS);
}
}
- class SmallRunnable implements Runnable {
- public void run() {
- try {
- Thread.sleep(SMALL_DELAY_MS);
- }
- catch (Exception e) {
- threadUnexpectedException(e);
- }
+ class SmallRunnable extends CheckedRunnable {
+ void realRun() throws Throwable {
+ Thread.sleep(SMALL_DELAY_MS);
}
}
- class SmallPossiblyInterruptedRunnable implements Runnable {
- public void run() {
+ class SmallPossiblyInterruptedRunnable extends CheckedRunnable {
+ void realRun() {
try {
Thread.sleep(SMALL_DELAY_MS);
}
- catch (Exception e) {
+ catch (InterruptedException ok) {
}
}
}
- class SmallCallable implements Callable {
- public Object call() {
- try {
- Thread.sleep(SMALL_DELAY_MS);
- }
- catch (Exception e) {
- threadUnexpectedException(e);
- }
+ class SmallCallable extends CheckedCallable {
+ Object realCall() throws Throwable {
+ Thread.sleep(SMALL_DELAY_MS);
return Boolean.TRUE;
}
}
- class SmallInterruptedRunnable implements Runnable {
- public void run() {
- try {
- Thread.sleep(SMALL_DELAY_MS);
- threadShouldThrow();
- }
- catch (InterruptedException success) {
- }
+ class SmallInterruptedRunnable extends CheckedInterruptedRunnable {
+ void realRun() throws InterruptedException {
+ Thread.sleep(SMALL_DELAY_MS);
}
}
-
- class MediumRunnable implements Runnable {
- public void run() {
- try {
- Thread.sleep(MEDIUM_DELAY_MS);
- }
- catch (Exception e) {
- threadUnexpectedException(e);
- }
+ class MediumRunnable extends CheckedRunnable {
+ void realRun() throws Throwable {
+ Thread.sleep(MEDIUM_DELAY_MS);
}
}
- class MediumInterruptedRunnable implements Runnable {
- public void run() {
- try {
- Thread.sleep(MEDIUM_DELAY_MS);
- threadShouldThrow();
- }
- catch (InterruptedException success) {
- }
+ class MediumInterruptedRunnable extends CheckedInterruptedRunnable {
+ void realRun() throws InterruptedException {
+ Thread.sleep(MEDIUM_DELAY_MS);
}
}
- class MediumPossiblyInterruptedRunnable implements Runnable {
- public void run() {
+ class MediumPossiblyInterruptedRunnable extends CheckedRunnable {
+ void realRun() {
try {
Thread.sleep(MEDIUM_DELAY_MS);
}
- catch (InterruptedException success) {
+ catch (InterruptedException ok) {
}
}
}
- class LongPossiblyInterruptedRunnable implements Runnable {
- public void run() {
+ class LongPossiblyInterruptedRunnable extends CheckedRunnable {
+ void realRun() {
try {
Thread.sleep(LONG_DELAY_MS);
}
- catch (InterruptedException success) {
+ catch (InterruptedException ok) {
}
}
}
@@ -540,7 +566,7 @@ public class JSR166TestCase extends Test
try {
Thread.sleep(SMALL_DELAY_MS);
done = true;
- } catch (Exception e) {
+ } catch (InterruptedException ok) {
}
}
}
@@ -551,7 +577,7 @@ public class JSR166TestCase extends Test
try {
Thread.sleep(MEDIUM_DELAY_MS);
done = true;
- } catch (Exception e) {
+ } catch (InterruptedException ok) {
}
}
}
@@ -562,7 +588,7 @@ public class JSR166TestCase extends Test
try {
Thread.sleep(LONG_DELAY_MS);
done = true;
- } catch (Exception e) {
+ } catch (InterruptedException ok) {
}
}
}
@@ -580,7 +606,7 @@ public class JSR166TestCase extends Test
try {
Thread.sleep(SMALL_DELAY_MS);
done = true;
- } catch (Exception e) {
+ } catch (InterruptedException ok) {
}
return Boolean.TRUE;
}
@@ -591,8 +617,8 @@ public class JSR166TestCase extends Test
* For use as RejectedExecutionHandler in constructors
*/
static class NoOpREHandler implements RejectedExecutionHandler {
- public void rejectedExecution(Runnable r, ThreadPoolExecutor executor){}
+ public void rejectedExecution(Runnable r,
+ ThreadPoolExecutor executor) {}
}
-
}