Use assertTimeoutPreemptively to preserve JUnit 4 timeout semantics - #2
Open
mattdepaula wants to merge 8 commits into
Open
Use assertTimeoutPreemptively to preserve JUnit 4 timeout semantics#2mattdepaula wants to merge 8 commits into
mattdepaula wants to merge 8 commits into
Conversation
…emantics @test(timeout=N) in JUnit 4 runs the test on a separate thread and preemptively aborts it via FutureTask.get(timeout). @timeout without threadMode defaults to SAME_THREAD, which only sends Thread.interrupt() — a non-interruptible test (infinite loop, deadlock) blocks the runner thread indefinitely. Replace the @timeout annotation with assertTimeoutPreemptively(Duration.ofMillis(N), () -> { body }), mirroring the existing @test(expected=...) → assertThrows pattern. assertTimeoutPreemptively runs the body on a separate thread and preemptively fails, preserving JUnit 4's preemptive-abort semantics. Fixes openrewrite#1073
…eptions with timeout" This reverts commit e4f1765.
|
did you intend to open this as an upstream contribution as an alternative to |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
UpdateTestAnnotationmigrated@Test(timeout=N)to@Timeout(N), which silently changes failure semantics:@Test(timeout=N)): test runs on a separate thread; runner unblocks after N ms viaFutureTask.get(timeout)— a hanging test fails and the suite continues@Timeout(N), defaultSAME_THREAD): sendsThread.interrupt()to the test thread; if the test never checks for interruption (infinite loop, deadlock), the runner thread blocks foreverThis PR replaces the
@Timeoutannotation withassertTimeoutPreemptively(Duration.ofMillis(N), () -> { body }), which runs the body on a separate thread and preemptively fails — preserving JUnit 4's original semantics. The pattern mirrors the existing@Test(expected=...)→assertThrows(...)body-wrap already implemented in this recipe.Edge case handled:
@Test(timeout=0)means "no timeout" in JUnit 4 (the default). The recipe skips theassertTimeoutPreemptivelywrapping whentimeout=0and migrates the annotation only.Upstream issue
openrewrite#1073
Behavior change
Before
After
Changes
UpdateTestAnnotation: replace@Timeoutemission with two-stepassertTimeoutPreemptivelybody-wrap (mirrorsassertThrowspattern); skip wrapping whentimeout=0UpdateTestAnnotationTest:annotationWithTimeout: updated expected output; added@Issue("#1073")annotationWithTimeoutAndStatements: new test — non-empty method body is wrapped correctlytimeoutZeroIsNoOpInJUnit4SoAnnotationOnlyMigrated: new test —timeout=0produces plain@Test, no body wrappingannotationWithTimeoutAndException: updated expected output (nestedassertThrowsinsideassertTimeoutPreemptively); added@Issue("#1073")Test plan
annotationWithTimeout— empty body, 500ms timeoutannotationWithTimeoutAndStatements— non-empty body, 500ms timeouttimeoutZeroIsNoOpInJUnit4SoAnnotationOnlyMigrated—timeout=0skips wrappingannotationWithTimeoutAndException— combinedexpected+timeout, correct nestingUpdateTestAnnotationTesttests continue to pass