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

Add unit to @Timeout #451

Merged
merged 1 commit into from
Jan 4, 2024
Merged

Add unit to @Timeout #451

merged 1 commit into from
Jan 4, 2024

Conversation

timtebeek
Copy link
Contributor

Fixes #450

Quickest way to fix this is to add an explicit unit, since any division might lead to rounding errors.

We could also explore simplifying the durations as we do for AssertJ

private J.MethodInvocation simplifyTimeUnits(J.MethodInvocation m, ExecutionContext ctx) {
Expression arg = m.getArguments().get(0);
Long argValue = SimplifyDurationCreationUnits.getConstantIntegralValue(arg);
if (argValue == null) {
return m;
}
List<Object> unitInfo = getUnitInfo(m.getSimpleName(), Math.toIntExact(argValue));
String methodName = (String) unitInfo.get(0);
int methodArg = (int) unitInfo.get(1);
if (!(m.getSimpleName().equals(methodName))) {
// update method invocation with new name and arg
String template = String.format("#{any()}.%s(%d)", methodName, methodArg);
return applyTemplate(ctx, m, template, m.getSelect());
}
return m;
}
private static List<Object> getUnitInfo(String name, int argValue) {
final int timeLength;
if (name.equals("hasSeconds") || name.equals("hasMinutes")) {
timeLength = 60;
} else if (name.equals("hasNanos") || name.equals("hasMillis")) {
timeLength = 1000;
} else if (name.equals("hasHours")) {
timeLength = 24;
} else {
return Arrays.asList(name, argValue);
}
if (argValue % timeLength == 0) {
String newName = METHOD_MAP.get(name);
return getUnitInfo(newName, argValue / timeLength);
} else {
// returning name, newArg
return Arrays.asList(name, argValue);
}
}

But that's probably best done as a separate followup recipe such that we also pick up any existing tests.

@timtebeek timtebeek self-assigned this Jan 4, 2024
@timtebeek timtebeek added the bug Something isn't working label Jan 4, 2024
@timtebeek timtebeek merged commit af84b02 into main Jan 4, 2024
1 check passed
@timtebeek timtebeek deleted the add-unit-to-timeout branch January 4, 2024 15:32
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 this pull request may close these issues.

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