Skip to content

Commit

Permalink
Ensure the randomize function always returns a number greater than 1.0 (
Browse files Browse the repository at this point in the history
resilience4j#2152)

* Ensure the randomize function always returns a number greater than 1.0

* Update randomizationFactor validation to allow 1.0

* Add that 1.0f also passes in the shouldPassPositiveRandomizationFactor test
  • Loading branch information
JoosJuliet authored Apr 25, 2024
1 parent dc9505a commit 01be4a1
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,9 @@ static double randomize(final double current, final double randomizationFactor)
final double delta = randomizationFactor * current;
final double min = current - delta;
final double max = current + delta;
final double randomizedValue = min + (Math.random() * (max - min + 1));

return (min + (Math.random() * (max - min + 1)));
return Math.max(1.0, randomizedValue);
}

static void checkInterval(long intervalMillis) {
Expand All @@ -251,7 +252,7 @@ static void checkInterval(long intervalMillis) {
}

static void checkRandomizationFactor(double randomizationFactor) {
if (randomizationFactor < 0.0 || randomizationFactor >= 1.0) {
if (randomizationFactor < 0.0 || randomizationFactor > 1.0) {
throw new IllegalArgumentException(
"Illegal argument randomizationFactor: " + randomizationFactor);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ public void shouldRejectOutOfBoundsRandomizationFactor() {
public void shouldPassPositiveRandomizationFactor() {
final Duration duration = Duration.ofMillis(100);
final float multiplier = 1.5f;
final List<Float> correctFactors = List.of(0.0f, 0.25f, 0.5f, 0.75f, 0.1f);
final List<Float> correctFactors = List.of(0.0f, 0.1f, 0.25f, 0.5f, 0.75f, 1.0f);

correctFactors.forEach(v -> IntervalFunction.ofRandomized(duration, v));
correctFactors
Expand Down

0 comments on commit 01be4a1

Please sign in to comment.