Skip to content

Commit 00d5574

Browse files
authored
Precondition checks for constant wait, max attempts stop, and max delay stop (#14)
1 parent 02ad0f3 commit 00d5574

13 files changed

+59
-14
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
### Added
55
- Constant Wait strategy
66

7+
### Changed
8+
- Precondition checks for constant wait, max attempts stop, and max delay since first attempt stop
9+
710
## [1.0.0] - 2016-08-29
811
### Added
912
- Initial implementation

src/main/java/org/joeyb/retry/ConstantWait.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ class ConstantWait<V> implements Wait<V> {
1212
private final long waitTime;
1313

1414
ConstantWait(long waitTime) {
15+
if (waitTime < 0) {
16+
throw new IllegalArgumentException("waitTime must be greater than 0");
17+
}
18+
1519
this.waitTime = waitTime;
1620
}
1721

src/main/java/org/joeyb/retry/MaxAttemptsStop.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ class MaxAttemptsStop<V> implements Stop<V> {
1313
private final long maxAttempts;
1414

1515
MaxAttemptsStop(long maxAttempts) {
16+
if (maxAttempts < 1) {
17+
throw new IllegalArgumentException("maxAttempts must be greater than 1");
18+
}
19+
1620
this.maxAttempts = maxAttempts;
1721
}
1822

src/main/java/org/joeyb/retry/MaxDelaySinceFirstAttemptStop.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ public class MaxDelaySinceFirstAttemptStop<V> implements Stop<V> {
1313
private final long maxDelaySinceFirstAttempt;
1414

1515
MaxDelaySinceFirstAttemptStop(long maxDelay) {
16+
if (maxDelay < 0) {
17+
throw new IllegalArgumentException("maxDelay must be greater than 0");
18+
}
19+
1620
this.maxDelaySinceFirstAttempt = maxDelay;
1721
}
1822

src/test/java/org/joeyb/retry/CompositeAndStopTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CompositeAndStopTests {
1313
public void compositeAndStop() {
1414
Attempt<Long> attempt = Attempts.exception(
1515
ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE),
16-
ThreadLocalRandom.current().nextLong(),
16+
ThreadLocalRandom.current().nextLong(Long.MAX_VALUE),
1717
new RuntimeException());
1818
Stop<Long> falseStop = a -> false;
1919
Stop<Long> trueStop = a -> true;

src/test/java/org/joeyb/retry/CompositeOrStopTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class CompositeOrStopTests {
1313
public void compositeOrStop() {
1414
Attempt<Long> attempt = Attempts.exception(
1515
ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE),
16-
ThreadLocalRandom.current().nextLong(),
16+
ThreadLocalRandom.current().nextLong(Long.MAX_VALUE),
1717
new RuntimeException());
1818
Stop<Long> falseStop = a -> false;
1919
Stop<Long> trueStop = a -> true;
Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.joeyb.retry;
22

33
import static org.assertj.core.api.Java6Assertions.assertThat;
4+
import static org.assertj.core.api.Java6Assertions.assertThatThrownBy;
45

56
import org.junit.Test;
67

@@ -10,15 +11,23 @@ public class ConstantWaitTests {
1011

1112
@Test
1213
public void constantWait() {
13-
long waitTime = ThreadLocalRandom.current().nextLong();
14+
long waitTime = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
1415

1516
Attempt<Long> attempt = Attempts.exception(
1617
ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE),
17-
ThreadLocalRandom.current().nextLong(),
18+
ThreadLocalRandom.current().nextLong(Long.MAX_VALUE),
1819
new RuntimeException());
1920

2021
Wait<Long> constantWait = new ConstantWait<>(waitTime);
2122

2223
assertThat(constantWait.waitTime(attempt)).isEqualTo(waitTime);
2324
}
25+
26+
@Test
27+
public void throwsIfWaitTimeIsLessThanZero() {
28+
long waitTime = -ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
29+
30+
assertThatThrownBy(() -> new ConstantWait<>(waitTime))
31+
.isInstanceOf(IllegalArgumentException.class);
32+
}
2433
}

src/test/java/org/joeyb/retry/MaxAttemptsStopTests.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.joeyb.retry;
22

33
import static org.assertj.core.api.Java6Assertions.assertThat;
4+
import static org.assertj.core.api.Java6Assertions.assertThatThrownBy;
45

56
import org.junit.Test;
67

@@ -14,17 +15,17 @@ public void maxAttemptsStop() {
1415

1516
Attempt<Long> attemptBeforeMax = Attempts.exception(
1617
maxAttempts - 1,
17-
ThreadLocalRandom.current().nextLong(),
18+
ThreadLocalRandom.current().nextLong(Long.MAX_VALUE),
1819
new RuntimeException());
1920

2021
Attempt<Long> attemptAtMax = Attempts.exception(
2122
maxAttempts,
22-
ThreadLocalRandom.current().nextLong(),
23+
ThreadLocalRandom.current().nextLong(Long.MAX_VALUE),
2324
new RuntimeException());
2425

2526
Attempt<Long> attemptAfterMax = Attempts.exception(
2627
maxAttempts + 1,
27-
ThreadLocalRandom.current().nextLong(),
28+
ThreadLocalRandom.current().nextLong(Long.MAX_VALUE),
2829
new RuntimeException());
2930

3031
MaxAttemptsStop<Long> stop = new MaxAttemptsStop<>(maxAttempts);
@@ -34,4 +35,15 @@ public void maxAttemptsStop() {
3435
assertThat(stop.stop(attemptAfterMax)).isTrue();
3536
assertThat(stop.maxAttempts()).isEqualTo(maxAttempts);
3637
}
38+
39+
@Test
40+
public void throwsIfMaxAttemptsIsLessThanOne() {
41+
long maxAttempts = -ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE);
42+
43+
assertThatThrownBy(() -> new MaxAttemptsStop<>(maxAttempts))
44+
.isInstanceOf(IllegalArgumentException.class);
45+
46+
assertThatThrownBy(() -> new MaxAttemptsStop<>(0))
47+
.isInstanceOf(IllegalArgumentException.class);
48+
}
3749
}

src/test/java/org/joeyb/retry/MaxDelaySinceFirstAttemptStopTests.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.joeyb.retry;
22

33
import static org.assertj.core.api.Java6Assertions.assertThat;
4+
import static org.assertj.core.api.Java6Assertions.assertThatThrownBy;
45

56
import org.junit.Test;
67

@@ -34,4 +35,12 @@ public void maxDelaySinceFirstAttemptStop() {
3435
assertThat(stop.stop(attemptAfterMax)).isTrue();
3536
assertThat(stop.maxDelaySinceFirstAttempt()).isEqualTo(maxDelay);
3637
}
38+
39+
@Test
40+
public void throwsIfMaxDelayIsLessThanZero() {
41+
long maxDelay = -ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
42+
43+
assertThatThrownBy(() -> new MaxDelaySinceFirstAttemptStop<>(maxDelay))
44+
.isInstanceOf(IllegalArgumentException.class);
45+
}
3746
}

src/test/java/org/joeyb/retry/NeverStopTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class NeverStopTests {
1212
public void neverStop() {
1313
Attempt<Long> attempt = Attempts.exception(
1414
ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE),
15-
ThreadLocalRandom.current().nextLong(),
15+
ThreadLocalRandom.current().nextLong(Long.MAX_VALUE),
1616
new RuntimeException());
1717

1818
Stop<Long> stop = new NeverStop<>();

src/test/java/org/joeyb/retry/NoWaitTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class NoWaitTests {
1212
public void noWait() {
1313
Attempt<Long> attempt = Attempts.exception(
1414
ThreadLocalRandom.current().nextLong(1, Long.MAX_VALUE),
15-
ThreadLocalRandom.current().nextLong(),
15+
ThreadLocalRandom.current().nextLong(Long.MAX_VALUE),
1616
new RuntimeException());
1717

1818
Wait<Long> noWait = new NoWait<>();

src/test/java/org/joeyb/retry/RetryTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public class RetryTests {
1717
public void eventuallySuccessfulCallableRetriesExpectedNumberOfTimesBeforeSucceeding() {
1818
int attemptsBeforeSuccess = ThreadLocalRandom.current().nextInt(10, 100);
1919
long expectedResult = ThreadLocalRandom.current().nextLong();
20-
long waitTime = ThreadLocalRandom.current().nextLong();
20+
long waitTime = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
2121

2222
MemoizingAccept<Long> memoizingAccept = new MemoizingAccept<>(Accepts.any());
2323
MemoizingBlock memoizingBlock = new MemoizingBlock();
@@ -81,7 +81,7 @@ public void callableThatDoesNotSucceedBeforeStopThrowsRetryException() {
8181
int attemptsBeforeSuccess = ThreadLocalRandom.current().nextInt(10, 100);
8282
long expectedResult = ThreadLocalRandom.current().nextLong();
8383
long maxAttempts = attemptsBeforeSuccess - 1;
84-
long waitTime = ThreadLocalRandom.current().nextLong();
84+
long waitTime = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
8585

8686
MemoizingAccept<Long> memoizingAccept = new MemoizingAccept<>(Accepts.any());
8787
MemoizingBlock memoizingBlock = new MemoizingBlock();
@@ -129,7 +129,7 @@ public void builderBuildWithoutAdditionalConfig() {
129129
@Test
130130
public void builderAcceptBlockStopAndWaitMethodsSetGivenImplementations() {
131131
int attemptsBeforeSuccess = ThreadLocalRandom.current().nextInt(10, 100);
132-
long waitTime = ThreadLocalRandom.current().nextLong();
132+
long waitTime = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
133133

134134
MemoizingAccept<Long> memoizingAccept = new MemoizingAccept<>(Accepts.any());
135135
MemoizingBlock memoizingBlock = new MemoizingBlock();
@@ -169,7 +169,7 @@ public void builderAcceptNonNullResult() {
169169

170170
@Test
171171
public void builderConstantWait() {
172-
long waitTime = ThreadLocalRandom.current().nextLong();
172+
long waitTime = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
173173

174174
Retry<Long> retry = Retry.<Long>newBuilder()
175175
.constantWait(waitTime)

src/test/java/org/joeyb/retry/WaitsTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public void waitsOnlyHasPrivateConstructor() {
1616

1717
@Test
1818
public void constant() {
19-
long waitTime = ThreadLocalRandom.current().nextLong();
19+
long waitTime = ThreadLocalRandom.current().nextLong(Long.MAX_VALUE);
2020

2121
Wait<Long> wait = Waits.constant(waitTime);
2222

0 commit comments

Comments
 (0)