Skip to content

Commit

Permalink
add ns and us for config duration units (#6654)
Browse files Browse the repository at this point in the history
Co-authored-by: Jack Berg <[email protected]>
  • Loading branch information
SylvainJuge and jack-berg authored Aug 20, 2024
1 parent 3e8092d commit d4b10a8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
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

0 comments on commit d4b10a8

Please sign in to comment.