-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
prevent arithmetic overflow and reduce allocations in time conversions
- Loading branch information
1 parent
589d3e6
commit d2fabf2
Showing
6 changed files
with
177 additions
and
46 deletions.
There are no files selected for viewing
This file contains 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
60 changes: 60 additions & 0 deletions
60
implementation/core/src/main/java/io/smallrye/faulttolerance/core/util/Durations.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package io.smallrye.faulttolerance.core.util; | ||
|
||
import java.time.temporal.ChronoUnit; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
public final class Durations { | ||
private static final long SECONDS_IN_HALF_DAY = ChronoUnit.HALF_DAYS.getDuration().getSeconds(); | ||
private static final long SECONDS_IN_WEEK = ChronoUnit.WEEKS.getDuration().getSeconds();; | ||
private static final long SECONDS_IN_MONTH = ChronoUnit.MONTHS.getDuration().getSeconds();; | ||
private static final long SECONDS_IN_YEAR = ChronoUnit.YEARS.getDuration().getSeconds();; | ||
|
||
private static final long MAX_HALF_DAYS = Long.MAX_VALUE / SECONDS_IN_HALF_DAY; | ||
private static final long MAX_WEEKS = Long.MAX_VALUE / SECONDS_IN_WEEK; | ||
private static final long MAX_MONTHS = Long.MAX_VALUE / SECONDS_IN_MONTH; | ||
private static final long MAX_YEARS = Long.MAX_VALUE / SECONDS_IN_YEAR; | ||
|
||
public static long timeInMillis(long value, ChronoUnit unit) { | ||
switch (unit) { | ||
case NANOS: | ||
return TimeUnit.NANOSECONDS.toMillis(value); | ||
case MICROS: | ||
return TimeUnit.MICROSECONDS.toMillis(value); | ||
case MILLIS: | ||
return value; | ||
case SECONDS: | ||
return TimeUnit.SECONDS.toMillis(value); | ||
case MINUTES: | ||
return TimeUnit.MINUTES.toMillis(value); | ||
case HOURS: | ||
return TimeUnit.HOURS.toMillis(value); | ||
case HALF_DAYS: | ||
return convert(value, MAX_HALF_DAYS, SECONDS_IN_HALF_DAY); | ||
case DAYS: | ||
return TimeUnit.DAYS.toMillis(value); | ||
case WEEKS: | ||
return convert(value, MAX_WEEKS, SECONDS_IN_WEEK); | ||
case MONTHS: | ||
return convert(value, MAX_MONTHS, SECONDS_IN_MONTH); | ||
case YEARS: | ||
return convert(value, MAX_YEARS, SECONDS_IN_YEAR); | ||
default: | ||
throw new IllegalArgumentException("Unsupported time unit: " + unit); | ||
} | ||
} | ||
|
||
private static long convert(long value, long maxInUnit, long secondsInUnit) { | ||
if (value == Long.MIN_VALUE) { | ||
return Long.MIN_VALUE; | ||
} | ||
|
||
boolean negative = value < 0; | ||
long abs = negative ? -value : value; | ||
if (abs > maxInUnit) { | ||
// `value * secondsInUnit` would overflow | ||
return negative ? Long.MIN_VALUE : Long.MAX_VALUE; | ||
} | ||
|
||
return TimeUnit.SECONDS.toMillis(value * secondsInUnit); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
implementation/core/src/test/java/io/smallrye/faulttolerance/core/util/DurationsTest.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package io.smallrye.faulttolerance.core.util; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Duration; | ||
import java.time.temporal.ChronoUnit; | ||
import java.util.List; | ||
import java.util.concurrent.ThreadLocalRandom; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
public class DurationsTest { | ||
@Test | ||
public void timeInMillis() { | ||
assertThat(Durations.timeInMillis(5_000_000, ChronoUnit.NANOS)).isEqualTo(5); | ||
assertThat(Durations.timeInMillis(5_000, ChronoUnit.MICROS)).isEqualTo(5); | ||
assertThat(Durations.timeInMillis(5, ChronoUnit.MILLIS)).isEqualTo(5); | ||
assertThat(Durations.timeInMillis(2, ChronoUnit.SECONDS)).isEqualTo(2000); | ||
assertThat(Durations.timeInMillis(2, ChronoUnit.MINUTES)).isEqualTo(120_000); | ||
assertThat(Durations.timeInMillis(3, ChronoUnit.HOURS)).isEqualTo(Duration.ofHours(3).toMillis()); | ||
assertThat(Durations.timeInMillis(2, ChronoUnit.HALF_DAYS)).isEqualTo(Duration.ofDays(1).toMillis()); | ||
assertThat(Durations.timeInMillis(8, ChronoUnit.HALF_DAYS)).isEqualTo(Duration.ofDays(4).toMillis()); | ||
assertThat(Durations.timeInMillis(365, ChronoUnit.DAYS)).isEqualTo(Duration.ofDays(365).toMillis()); | ||
assertThat(Durations.timeInMillis(7, ChronoUnit.WEEKS)).isEqualTo(Duration.ofDays(7 * 7).toMillis()); | ||
assertThat(Durations.timeInMillis(17, ChronoUnit.WEEKS)).isEqualTo(Duration.ofDays(17 * 7).toMillis()); | ||
assertThat(Durations.timeInMillis(12, ChronoUnit.MONTHS)).isEqualTo(ChronoUnit.YEARS.getDuration().toMillis()); | ||
assertThat(Durations.timeInMillis(24, ChronoUnit.MONTHS)).isEqualTo(2 * ChronoUnit.YEARS.getDuration().toMillis()); | ||
|
||
assertThat(Durations.timeInMillis(0, ChronoUnit.HALF_DAYS)).isEqualTo(0); | ||
assertThat(Durations.timeInMillis(4, ChronoUnit.HALF_DAYS)).isEqualTo(Duration.ofDays(2).toMillis()); | ||
assertThat(Durations.timeInMillis(-4, ChronoUnit.HALF_DAYS)).isEqualTo(Duration.ofDays(-2).toMillis()); | ||
assertThat(Durations.timeInMillis(0, ChronoUnit.DAYS)).isEqualTo(0); | ||
assertThat(Durations.timeInMillis(4, ChronoUnit.DAYS)).isEqualTo(Duration.ofDays(4).toMillis()); | ||
assertThat(Durations.timeInMillis(-4, ChronoUnit.DAYS)).isEqualTo(Duration.ofDays(-4).toMillis()); | ||
assertThat(Durations.timeInMillis(0, ChronoUnit.WEEKS)).isEqualTo(0); | ||
assertThat(Durations.timeInMillis(4, ChronoUnit.WEEKS)).isEqualTo(Duration.ofDays(4 * 7).toMillis()); | ||
assertThat(Durations.timeInMillis(-4, ChronoUnit.WEEKS)).isEqualTo(Duration.ofDays(-4 * 7).toMillis()); | ||
assertThat(Durations.timeInMillis(0, ChronoUnit.MONTHS)).isEqualTo(0); | ||
assertThat(Durations.timeInMillis(7, ChronoUnit.MONTHS)).isEqualTo(7 * ChronoUnit.MONTHS.getDuration().toMillis()); | ||
assertThat(Durations.timeInMillis(-7, ChronoUnit.MONTHS)).isEqualTo(-7 * ChronoUnit.MONTHS.getDuration().toMillis()); | ||
|
||
assertThat(Durations.timeInMillis(Long.MAX_VALUE, ChronoUnit.HOURS)).isEqualTo(Long.MAX_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MAX_VALUE, ChronoUnit.HALF_DAYS)).isEqualTo(Long.MAX_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MAX_VALUE, ChronoUnit.DAYS)).isEqualTo(Long.MAX_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MAX_VALUE, ChronoUnit.WEEKS)).isEqualTo(Long.MAX_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MAX_VALUE, ChronoUnit.MONTHS)).isEqualTo(Long.MAX_VALUE); | ||
|
||
assertThat(Durations.timeInMillis(Long.MAX_VALUE - 1, ChronoUnit.HOURS)).isEqualTo(Long.MAX_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MAX_VALUE - 1, ChronoUnit.HALF_DAYS)).isEqualTo(Long.MAX_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MAX_VALUE - 1, ChronoUnit.DAYS)).isEqualTo(Long.MAX_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MAX_VALUE - 1, ChronoUnit.WEEKS)).isEqualTo(Long.MAX_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MAX_VALUE - 1, ChronoUnit.MONTHS)).isEqualTo(Long.MAX_VALUE); | ||
|
||
assertThat(Durations.timeInMillis(Long.MIN_VALUE + 1, ChronoUnit.HOURS)).isEqualTo(Long.MIN_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MIN_VALUE + 1, ChronoUnit.HALF_DAYS)).isEqualTo(Long.MIN_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MIN_VALUE + 1, ChronoUnit.DAYS)).isEqualTo(Long.MIN_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MIN_VALUE + 1, ChronoUnit.WEEKS)).isEqualTo(Long.MIN_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MIN_VALUE + 1, ChronoUnit.MONTHS)).isEqualTo(Long.MIN_VALUE); | ||
|
||
assertThat(Durations.timeInMillis(Long.MIN_VALUE, ChronoUnit.HOURS)).isEqualTo(Long.MIN_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MIN_VALUE, ChronoUnit.HALF_DAYS)).isEqualTo(Long.MIN_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MIN_VALUE, ChronoUnit.DAYS)).isEqualTo(Long.MIN_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MIN_VALUE, ChronoUnit.WEEKS)).isEqualTo(Long.MIN_VALUE); | ||
assertThat(Durations.timeInMillis(Long.MIN_VALUE, ChronoUnit.MONTHS)).isEqualTo(Long.MIN_VALUE); | ||
} | ||
|
||
@Test | ||
public void timeInMillis_random() { | ||
for (ChronoUnit unit : List.of(ChronoUnit.NANOS, ChronoUnit.MICROS, ChronoUnit.MILLIS, ChronoUnit.SECONDS, | ||
ChronoUnit.MINUTES, ChronoUnit.HOURS, ChronoUnit.HALF_DAYS, ChronoUnit.DAYS)) { | ||
for (int i = 0; i < 1_000_000; i++) { | ||
long value = ThreadLocalRandom.current().nextLong(Integer.MIN_VALUE * 1000L, Integer.MAX_VALUE * 1000L); | ||
assertThat(Durations.timeInMillis(value, unit)) | ||
.isEqualTo(TimeUnit.MILLISECONDS.convert(Duration.of(value, unit))); | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.