From 130652ea92f5bb14a41feabbd37f110018efdb25 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Mon, 23 Nov 2020 09:35:02 +0100 Subject: [PATCH] Ensure JenkinsRule#after() is called even when timeout is reached --- .../org/jvnet/hudson/test/JenkinsRule.java | 37 ++++++++----------- 1 file changed, 15 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/jvnet/hudson/test/JenkinsRule.java b/src/main/java/org/jvnet/hudson/test/JenkinsRule.java index 37c375ab9..85eb37c60 100644 --- a/src/main/java/org/jvnet/hudson/test/JenkinsRule.java +++ b/src/main/java/org/jvnet/hudson/test/JenkinsRule.java @@ -584,7 +584,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; @@ -598,7 +606,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 @@ -632,26 +645,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) {