Skip to content

Commit

Permalink
Merge pull request #823 from askoog/timedoutexception
Browse files Browse the repository at this point in the history
Throw TestFailedOnTimeoutException instead of plain Exception on timeout. Fixes #771
  • Loading branch information
kcooney committed Feb 18, 2014
2 parents eb8b5ee + baf1ea0 commit 161f656
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 4 deletions.
3 changes: 2 additions & 1 deletion src/main/java/org/junit/internal/runners/MethodRoadie.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.junit.runner.notification.Failure;
import org.junit.runner.notification.RunNotifier;
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.TestTimedOutException;

/**
* @deprecated Included for backwards compatibility with JUnit 4.4. Will be
Expand Down Expand Up @@ -74,7 +75,7 @@ public Object call() throws Exception {
}
result.get(0, TimeUnit.MILLISECONDS); // throws the exception if one occurred during the invocation
} catch (TimeoutException e) {
addFailure(new Exception(String.format("test timed out after %d milliseconds", timeout)));
addFailure(new TestTimedOutException(timeout, TimeUnit.MILLISECONDS));
} catch (Exception e) {
addFailure(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import org.junit.runners.model.MultipleFailureException;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestTimedOutException;

public class FailOnTimeout extends Statement {
private final Statement fOriginalStatement;
Expand Down Expand Up @@ -68,8 +69,7 @@ private Throwable getResult(FutureTask<Throwable> task, Thread thread) {
private Exception createTimeoutException(Thread thread) {
StackTraceElement[] stackTrace = thread.getStackTrace();
final Thread stuckThread = fLookForStuckThread ? getStuckThread(thread) : null;
Exception currThreadException = new Exception(String.format(
"test timed out after %d %s", fTimeout, fTimeUnit.name().toLowerCase()));
Exception currThreadException = new TestTimedOutException(fTimeout, fTimeUnit);
if (stackTrace != null) {
currThreadException.setStackTrace(stackTrace);
thread.interrupt();
Expand Down
44 changes: 44 additions & 0 deletions src/main/java/org/junit/runners/model/TestTimedOutException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package org.junit.runners.model;

import java.util.concurrent.TimeUnit;

/**
* Exception thrown when a test fails on timeout.
*
* @since 4.12
*
*/
public class TestTimedOutException extends Exception {

private static final long serialVersionUID = 31935685163547539L;

private final TimeUnit fTimeUnit;
private final long fTimeout;

/**
* Creates exception with a standard message "test timed out after [timeout] [timeUnit]"
*
* @param timeout the amount of time passed before the test was interrupted
* @param timeUnit the time unit for the timeout value
*/
public TestTimedOutException(long timeout, TimeUnit timeUnit) {
super(String.format("test timed out after %d %s",
timeout, timeUnit.name().toLowerCase()));
fTimeUnit = timeUnit;
fTimeout = timeout;
}

/**
* Gets the time passed before the test was interrupted
*/
public long getTimeout() {
return fTimeout;
}

/**
* Gets the time unit for the timeout value
*/
public TimeUnit getTimeUnit() {
return fTimeUnit;
}
}
2 changes: 2 additions & 0 deletions src/test/java/org/junit/tests/AllTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.junit.tests.experimental.theories.runner.WithAutoGeneratedDataPoints;
import org.junit.tests.experimental.theories.runner.WithDataPointMethod;
import org.junit.tests.experimental.theories.runner.WithNamedDataPoints;
import org.junit.tests.internal.runners.statements.FailOnTimeoutTest;
import org.junit.tests.junit3compatibility.AllTestsTest;
import org.junit.tests.junit3compatibility.ClassRequestTest;
import org.junit.tests.junit3compatibility.ForwardCompatibilityTest;
Expand Down Expand Up @@ -200,6 +201,7 @@
CategoryFilterFactoryTest.class,
FrameworkFieldTest.class,
FrameworkMethodTest.class,
FailOnTimeoutTest.class,
JUnitCoreTest.class
})
public class AllTests {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,19 @@
import static java.lang.System.currentTimeMillis;
import static java.lang.Thread.sleep;
import static org.hamcrest.core.Is.is;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.Rule;
import org.junit.Test;
import org.junit.internal.runners.statements.FailOnTimeout;
import org.junit.rules.ExpectedException;
import org.junit.runners.model.Statement;
import org.junit.runners.model.TestTimedOutException;

/**
* @author Asaf Ary, Stefan Birkner
Expand All @@ -29,6 +33,12 @@ public class FailOnTimeoutTest {
private final FailOnTimeout failOnTimeout = new FailOnTimeout(statement,
TIMEOUT);

@Test
public void throwsTestTimedOutException() throws Throwable {
thrown.expect(TestTimedOutException.class);
evaluateWithWaitDuration(TIMEOUT + 50);
}

@Test
public void throwExceptionWithNiceMessageOnTimeout() throws Throwable {
thrown.expectMessage("test timed out after 100 milliseconds");
Expand All @@ -45,7 +55,7 @@ public void sendUpExceptionThrownByStatement() throws Throwable {
@Test
public void throwExceptionIfTheSecondCallToEvaluateNeedsTooMuchTime()
throws Throwable {
thrown.expect(Exception.class);
thrown.expect(TestTimedOutException.class);
evaluateWithWaitDuration(0);
evaluateWithWaitDuration(TIMEOUT + 50);
}
Expand All @@ -61,6 +71,18 @@ public void throwTimeoutExceptionOnSecondCallAlthoughFirstCallThrowsException()
evaluateWithWaitDuration(TIMEOUT + 50);
}

@Test
public void throwsExceptionWithTimeoutValueAndTimeUnitSet()
throws Throwable {
try {
evaluateWithWaitDuration(TIMEOUT + 50);
fail("No exception was thrown when test timed out");
} catch (TestTimedOutException e) {
assertEquals(TIMEOUT, e.getTimeout());
assertEquals(TimeUnit.MILLISECONDS, e.getTimeUnit());
}
}

private void evaluateWithException(Exception exception) throws Throwable {
statement.nextException = exception;
statement.waitDuration = 0;
Expand Down

0 comments on commit 161f656

Please sign in to comment.