Skip to content

JUnit5 JenkinsRule supports timeout#979

Draft
strangelookingnerd wants to merge 1 commit intojenkinsci:masterfrom
strangelookingnerd:junit5_timeout
Draft

JUnit5 JenkinsRule supports timeout#979
strangelookingnerd wants to merge 1 commit intojenkinsci:masterfrom
strangelookingnerd:junit5_timeout

Conversation

@strangelookingnerd
Copy link
Contributor

Trying to fix #977.

Draft for now, implementation is very rough.

Testing done

Automated testing will be an issue as it is tricky to make tests pass that have failed with a timeout 😄

Submitter checklist

  • Make sure you are opening from a topic/feature/bugfix branch (right side) and not your main branch!
  • Ensure that the pull request title represents the desired changelog entry
  • Please describe what you did
  • Link to relevant issues in GitHub or Jira
  • Link to relevant pull requests, esp. upstream and downstream changes
  • Ensure you have provided tests that demonstrate the feature works or the issue is fixed

@strangelookingnerd strangelookingnerd marked this pull request as draft June 5, 2025 13:14
class JenkinsRuleTimeout5Test extends JenkinsRuleTimeoutTestBase {

// this is the only way to make JenkinsRule#timeout work here
private static final String TIMEOUT = System.setProperty("jenkins.test.timeout", "30");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there a reason you aren't just going the way of setting the default junit test timeout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was trying to keep JenkinsRule#timeout and @WithTimeout functional as before. Which kind of works I think, but the outcome - as you can see - is a mess.

If we can simply ignore the above as a requirement, then yes, setting the default timeout is the easiest solution.
It could be based on jenkins.test.timeout property to keep that functional as well.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the built-in would be fine and a javadoc update to say not supported on JUnit 5 and to point to the new way instead

Copy link
Contributor Author

@strangelookingnerd strangelookingnerd Jun 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I gave it another go an hit a wall. Just pasting my findings here.

It appears that the global timeout defined via junit.jupiter.execution.timeout.default does handle lifecyle methods and tests, however this does not include anything happening in an Extension.
This is unfortunate as it will not allow us to cover JenkinsRule#before or JenkinsRule#after by these timeouts. This could be by-passed by wrapping these calls with assertTimeout or assertTmeoutPreemptively. Yet this comes with a caveat:

By default timeouts are not handled preemptively in JUnit5 (see https://junit.org/junit5/docs/current/user-guide/#writing-tests-declarative-timeouts-thread-mode). This means a test will report that it timed-out only if it at some point finishes. If execution hangs indefinitly for what ever reason, tests will do so as well.

I tried to by-pass that by setting the thread-mode as suggested in the documentation. This will then interrupt hanging threads, but also comes with side-effects for the behavior due to ThreadLocal usages as suggested by the documentation.

Not really what I was hoping for, yet there still may be some things I have not yet tried out.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

JUnit 5 tests don't apply a timeout

2 participants