diff --git a/src/main/java/org/jvnet/hudson/test/JenkinsRule.java b/src/main/java/org/jvnet/hudson/test/JenkinsRule.java index 75f2f4138..666c5651c 100644 --- a/src/main/java/org/jvnet/hudson/test/JenkinsRule.java +++ b/src/main/java/org/jvnet/hudson/test/JenkinsRule.java @@ -689,7 +689,15 @@ public Statement apply(final Statement base, final Description description) { // request has been made to not create the instance for this test method return base; } - Statement wrapped = new Statement() { + Statement wrappedBase; + final int testTimeout = getTestTimeoutOverride(description); + if (testTimeout <= 0) { + System.out.println("Test timeout disabled."); + wrappedBase = base; + } else { + wrappedBase = Timeout.seconds(testTimeout).apply(base, description); + } + return new Statement() { @Override public void evaluate() throws Throwable { testDescription = description; @@ -703,7 +711,12 @@ public void evaluate() throws Throwable { // so that test code has all the access to the system ACL.impersonate(ACL.SYSTEM); try { - base.evaluate(); + wrappedBase.evaluate(); + } catch (TestTimedOutException x) { + // withLookingForStuckThread does not work well; better to just have a full thread dump. + LOGGER.warning(String.format("Test timed out (after %d seconds).", testTimeout)); + dumpThreads(); + throw x; } catch (Throwable th) { testFailure = th; // allow the late attachment of a debugger in case of a failure. Useful @@ -737,26 +750,6 @@ public void evaluate() throws Throwable { } } }; - final int testTimeout = getTestTimeoutOverride(description); - if (testTimeout <= 0) { - System.out.println("Test timeout disabled."); - return wrapped; - } else { - final Statement timeoutStatement = Timeout.seconds(testTimeout).apply(wrapped, description); - return new Statement() { - @Override - public void evaluate() throws Throwable { - try { - timeoutStatement.evaluate(); - } catch (TestTimedOutException x) { - // withLookingForStuckThread does not work well; better to just have a full thread dump. - LOGGER.warning(String.format("Test timed out (after %d seconds).", testTimeout)); - dumpThreads(); - throw x; - } - } - }; - } } private int getTestTimeoutOverride(Description description) {