Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

junit4->5 rewrites @Test(1000 /*ms*/) to @Timeout(1000 /*sec*/) which breaks test logic #450

Closed
vlsi opened this issue Jan 4, 2024 · 5 comments · Fixed by #451
Closed
Assignees
Labels
bug Something isn't working

Comments

@vlsi
Copy link

vlsi commented Jan 4, 2024

What version of OpenRewrite are you using?

latest.integration

JUnit4 uses milliseconds for the timeout (there's no way to configure the unit), and JUnit5 uses TimeUnit.SECONDS by default.

I believe this is a wrong test:

void annotationWithTimeout() {
//language=java
rewriteRun(
java(
"""
import org.junit.Test;
public class MyTest {
@Test(timeout = 500)
public void test() {
}
}
""",
"""
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
public class MyTest {
@Test
@Timeout(500)
public void test() {
}
}
"""
)
);

I think OpenRewrite should convert @Test(timeout = 5000) to @Timeout(5); and it should convert @Test(timeout = 500) to @Timeout(value = 500, unit = TimeUnit.MILLISECONDS).

@vlsi vlsi added the bug Something isn't working label Jan 4, 2024
@timtebeek
Copy link
Contributor

That's indeed a bug, and a nasty one at that as well. Thanks for pointing it out!

timtebeek added a commit that referenced this issue Jan 4, 2024
@vlsi
Copy link
Author

vlsi commented Jan 4, 2024

It might make sense to add a rewrite rule that would detect "too large timeouts" and suggest converting them to seconds.
For instance: imagine someone has @Timeout(1000). I am 100% sure they meant 1 second rather than 1000 seconds = 16.7 minutes.

As a workaround, users could type @Timeout(value = 17, unit = TimeUnit.MINUTES) or @Timeout(value = 1000, unit = TimeUnit.SECONDS).

So the rewrite rule could be @Timeout(6000) => @Timeout(60).

@timtebeek
Copy link
Contributor

Yes I indeed think a separate recipe is in order that detects and corrects such implausible values; See also #451 (comment)

@timtebeek timtebeek moved this to Ready to Review in OpenRewrite Jan 4, 2024
@timtebeek timtebeek self-assigned this Jan 4, 2024
timtebeek added a commit that referenced this issue Jan 4, 2024
@github-project-automation github-project-automation bot moved this from Ready to Review to Done in OpenRewrite Jan 4, 2024
@timtebeek
Copy link
Contributor

Should be fixed for the time being; would you want to log a separate issue for detecting implausible timeouts and simplifying existing timeouts?

@vlsi
Copy link
Author

vlsi commented Jan 4, 2024

I've filed #452

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

2 participants