Skip to content
Browse files

fixes for #544 and #545

  • Loading branch information...
1 parent ea96d2e commit b28fe09d1a54955bed2e4db5352c14fae195a418 @Tibor17 committed Nov 15, 2012
View
19 src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java
@@ -2,6 +2,8 @@
import org.junit.runners.model.Statement;
+import java.io.InterruptedIOException;
+import java.nio.channels.ClosedByInterruptException;
import java.util.concurrent.TimeUnit;
public class FailOnTimeout extends Statement {
@@ -29,6 +31,7 @@ public void evaluate() throws Throwable {
private StatementThread evaluateStatement() throws InterruptedException {
StatementThread thread = new StatementThread(fOriginalStatement);
+ thread.setDaemon(true);
thread.start();
fTimeUnit.timedJoin(thread, fTimeout);
if (!thread.fFinished) {
@@ -56,14 +59,14 @@ private void throwTimeoutException(StatementThread thread) throws Exception {
private static class StatementThread extends Thread {
private final Statement fStatement;
-
- private boolean fFinished = false;
-
- private Throwable fExceptionThrownByOriginalStatement = null;
-
- private StackTraceElement[] fRecordedStackTrace = null;
+ private volatile boolean fFinished;
+ private volatile Throwable fExceptionThrownByOriginalStatement;
+ private volatile StackTraceElement[] fRecordedStackTrace;
public StatementThread(Statement statement) {
+ fFinished = false;
+ fExceptionThrownByOriginalStatement = null;
+ fRecordedStackTrace = null;
fStatement = statement;
}
@@ -82,6 +85,10 @@ public void run() {
fFinished = true;
} catch (InterruptedException e) {
// don't log the InterruptedException
+ } catch (InterruptedIOException e) {
+ // don't log the InterruptedIOException
+ } catch (ClosedByInterruptException e) {
+ // don't log the ClosedByInterruptException
} catch (Throwable e) {
fExceptionThrownByOriginalStatement = e;
}
View
37 src/test/java/org/junit/tests/experimental/rules/TimeoutRuleTest.java
@@ -9,6 +9,13 @@
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
+import java.io.File;
+import java.io.IOException;
+import java.io.InterruptedIOException;
+import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.Random;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
@@ -49,6 +56,28 @@ public void run4() {
while (!run4done) {
}
}
+
+ @Test
+ public void run5() throws IOException {
+ logger.append("run5");
+ Random rnd = new Random();
+ byte[] data = new byte[1024];
+ while (true) {
+ File tmp = File.createTempFile("dummy", ".tmp");
+ tmp.deleteOnExit();
+ FileChannel in = new RandomAccessFile(tmp, "rw").getChannel();
+ rnd.nextBytes(data);
+ in.write(ByteBuffer.wrap(data));//Interrupted thread closes channel and throws ClosedByInterruptException.
+ in.close();
+ }
+ }
+
+ @Test
+ public void run6() throws InterruptedIOException {
+ logger.append("run6");
+ //Java IO throws InterruptedIOException on SUN machines.
+ throw new InterruptedIOException();
+ }
}
public static class HasGlobalLongTimeout extends AbstractTimeoutTest {
@@ -79,21 +108,25 @@ public void after() {
public void timeUnitTimeout() throws InterruptedException {
HasGlobalTimeUnitTimeout.logger.setLength(0);
Result result = JUnitCore.runClasses(HasGlobalTimeUnitTimeout.class);
- assertEquals(4, result.getFailureCount());
+ assertEquals(6, result.getFailureCount());
assertThat(HasGlobalTimeUnitTimeout.logger.toString(), containsString("run1"));
assertThat(HasGlobalTimeUnitTimeout.logger.toString(), containsString("run2"));
assertThat(HasGlobalTimeUnitTimeout.logger.toString(), containsString("run3"));
assertThat(HasGlobalTimeUnitTimeout.logger.toString(), containsString("run4"));
+ assertThat(HasGlobalTimeUnitTimeout.logger.toString(), containsString("run5"));
+ assertThat(HasGlobalTimeUnitTimeout.logger.toString(), containsString("run6"));
}
@Test
public void longTimeout() throws InterruptedException {
HasGlobalLongTimeout.logger.setLength(0);
Result result = JUnitCore.runClasses(HasGlobalLongTimeout.class);
- assertEquals(4, result.getFailureCount());
+ assertEquals(6, result.getFailureCount());
assertThat(HasGlobalLongTimeout.logger.toString(), containsString("run1"));
assertThat(HasGlobalLongTimeout.logger.toString(), containsString("run2"));
assertThat(HasGlobalLongTimeout.logger.toString(), containsString("run3"));
assertThat(HasGlobalLongTimeout.logger.toString(), containsString("run4"));
+ assertThat(HasGlobalLongTimeout.logger.toString(), containsString("run5"));
+ assertThat(HasGlobalLongTimeout.logger.toString(), containsString("run6"));
}
}

0 comments on commit b28fe09

Please sign in to comment.
Something went wrong with that request. Please try again.