Skip to content

Commit 0e4f5a3

Browse files
committed
Check arguments in DelayPolicy implementations
1 parent 944be8e commit 0e4f5a3

File tree

6 files changed

+23
-7
lines changed

6 files changed

+23
-7
lines changed

src/main/java/net/tascalate/concurrent/delays/BoundedMaxDelayPolicy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public BoundedMaxDelayPolicy(DelayPolicy target, long maxDelayMillis) {
4343

4444
public BoundedMaxDelayPolicy(DelayPolicy target, Duration maxDelay) {
4545
super(target);
46+
if (!DelayPolicy.isValid(maxDelay)) {
47+
throw new IllegalArgumentException("MaxDelay must be positive but was: " + maxDelay);
48+
}
4649
this.maxDelay = maxDelay;
4750
}
4851

src/main/java/net/tascalate/concurrent/delays/BoundedMinDelayPolicy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@ public BoundedMinDelayPolicy(DelayPolicy target, long minDelayMillis) {
4242

4343
public BoundedMinDelayPolicy(DelayPolicy target, Duration minDelay) {
4444
super(target);
45+
if (!DelayPolicy.isValid(minDelay)) {
46+
throw new IllegalArgumentException("MinDelay must be positive but was: " + minDelay);
47+
}
4548
this.minDelay = minDelay;
4649
}
4750

src/main/java/net/tascalate/concurrent/delays/DurationCalcs.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ static Duration safeTransform(Duration duration, LongBinaryOperator isConversion
3232
}
3333
}
3434
// return max possible value if doesn't fit
35-
return Duration.of(Long.MAX_VALUE, TIME_DIMENSIONS[count - 1]);
35+
return MAX_DURATION;
3636

3737
}
3838

@@ -75,4 +75,5 @@ private static boolean toBoolean(long v) {
7575

7676
private static final Duration MAX_BY_NANOS = Duration.ofNanos(Long.MAX_VALUE);
7777
private static final Duration MAX_BY_MILLIS = Duration.ofMillis(Long.MAX_VALUE);
78+
private static final Duration MAX_DURATION = Duration.ofSeconds(Long.MAX_VALUE).withNanos(999_999_999);
7879
}

src/main/java/net/tascalate/concurrent/delays/ExponentialDelayPolicy.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ public ExponentialDelayPolicy(Duration initialDelay, double multiplier) {
4343
if (!DelayPolicy.isValid(initialDelay)) {
4444
throw new IllegalArgumentException("Initial delay must be positive but was: " + initialDelay);
4545
}
46+
if (multiplier <= 0) {
47+
throw new IllegalArgumentException("Multiplier must be a positive number but was: " + multiplier);
48+
}
4649
this.initialDelay = initialDelay;
4750
this.multiplier = multiplier;
4851
}

src/main/java/net/tascalate/concurrent/delays/ProportionalRandomDelayPolicy.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,29 +44,29 @@ public ProportionalRandomDelayPolicy(DelayPolicy target, Random random) {
4444

4545
public ProportionalRandomDelayPolicy(DelayPolicy target, double multiplier) {
4646
super(target);
47-
if (multiplier <= 0 || multiplier >= 1) {
48-
throw new IllegalArgumentException("Multiplier should be within (0..1) exclusively");
47+
if (multiplier <= 0) {
48+
throw new IllegalArgumentException("Multiplier must be a positive number but was: " + multiplier);
4949
}
5050
this.multiplier = multiplier;
5151
}
5252

5353
public ProportionalRandomDelayPolicy(DelayPolicy target, double multiplier, Random random) {
5454
super(target, random);
55-
if (multiplier <= 0 || multiplier >= 1) {
56-
throw new IllegalArgumentException("Multiplier should be within (0..1) exclusively");
55+
if (multiplier <= 0) {
56+
throw new IllegalArgumentException("Multiplier must be a positive number but was: " + multiplier);
5757
}
5858
this.multiplier = multiplier;
5959
}
6060

6161
@Override
6262
long addRandomJitter(long initialDelay, double randomizer, int dimIdx) {
6363
double randomMultiplier = (1 - 2 * randomizer) * multiplier;
64-
return (long) (initialDelay * (1 + randomMultiplier));
64+
return Math.max(0, (long) (initialDelay * (1 + randomMultiplier)));
6565
}
6666

6767
@Override
6868
boolean checkBounds(long initialDelay, double randomizer, int dimIdx) {
6969
double randomMultiplier = (1 - 2 * randomizer) * multiplier;
70-
return Long.MAX_VALUE / initialDelay > randomMultiplier + 1;
70+
return Long.MAX_VALUE / initialDelay > 1 + randomMultiplier;
7171
}
7272
}

src/main/java/net/tascalate/concurrent/delays/UniformRandomDelayPolicy.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ public UniformRandomDelayPolicy(DelayPolicy target, long range) {
4949

5050
public UniformRandomDelayPolicy(DelayPolicy target, Duration range) {
5151
super(target);
52+
if (!DelayPolicy.isValid(range)) {
53+
throw new IllegalArgumentException("Range must be positive but was: " + range);
54+
}
5255
this.range = range;
5356
}
5457

@@ -58,6 +61,9 @@ public UniformRandomDelayPolicy(DelayPolicy target, long range, Random random) {
5861

5962
public UniformRandomDelayPolicy(DelayPolicy target, Duration range, Random random) {
6063
super(target, random);
64+
if (!DelayPolicy.isValid(range)) {
65+
throw new IllegalArgumentException("Range must be positive but was: " + range);
66+
}
6167
this.range = range;
6268
}
6369

0 commit comments

Comments
 (0)