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 ns and us for config duration units #6654

Merged
merged 5 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ public Duration getDuration(String name) {
try {
long rawNumber = Long.parseLong(numberString.trim());
TimeUnit unit = getDurationUnit(unitString.trim());
return Duration.ofMillis(TimeUnit.MILLISECONDS.convert(rawNumber, unit));
return Duration.ofNanos(TimeUnit.NANOSECONDS.convert(rawNumber, unit));
} catch (NumberFormatException ex) {
throw new ConfigurationException(
"Invalid duration property "
Expand Down Expand Up @@ -256,6 +256,10 @@ private static List<String> filterBlanksAndNulls(String[] values) {
/** Returns the TimeUnit associated with a unit string. Defaults to milliseconds. */
private static TimeUnit getDurationUnit(String unitString) {
switch (unitString) {
case "us":
return TimeUnit.MICROSECONDS;
case "ns":
return TimeUnit.NANOSECONDS;
case "": // Fallthrough expected
case "ms":
return TimeUnit.MILLISECONDS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,24 @@ void invalidDuration() {
.hasMessage("Invalid duration property duration=9mm. Invalid duration string, found: mm");
}

@Test
void durationNegativeParsing() {
assertThat(
DefaultConfigProperties.createFromMap(Collections.singletonMap("duration", "-41"))
.getDuration("duration"))
.isEqualTo(Duration.ofMillis(-41));
}

@Test
void durationUnitParsing() {
assertThat(
DefaultConfigProperties.createFromMap(Collections.singletonMap("duration", "3ns"))
.getDuration("duration"))
.isEqualTo(Duration.ofNanos(3));
assertThat(
DefaultConfigProperties.createFromMap(Collections.singletonMap("duration", "2us"))
.getDuration("duration"))
.isEqualTo(Duration.ofNanos(2000));
assertThat(
DefaultConfigProperties.createFromMap(Collections.singletonMap("duration", "1"))
.getDuration("duration"))
Expand Down
Loading