Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit e6e4666

Browse files
committed
added validation to make sure max tries value is valid
1 parent acc32d1 commit e6e4666

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/main/java/com/evanlennick/retry4j/config/RetryConfigBuilder.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ public class RetryConfigBuilder {
3636
= "Number of tries can only be specified once!";
3737
public final static String CAN_ONLY_SPECIFY_CUSTOM_EXCEPTION_STRAT__ERROR_MSG
3838
= "You cannot use built in exception logic and custom exception logic in the same config!";
39+
public final static String MUST_SPECIFY_MAX_TRIES_ABOVE_0__ERROR_MSG
40+
= "Cannot specify a maximum number of tries less than 1!";
3941

4042
public RetryConfigBuilder() {
4143
this.config = new RetryConfig();
@@ -110,6 +112,10 @@ public RetryConfigBuilder retryOnCustomExceptionLogic(Function<Exception, Boolea
110112
}
111113

112114
public RetryConfigBuilder withMaxNumberOfTries(int max) {
115+
if (max < 1) {
116+
throw new InvalidRetryConfigException(MUST_SPECIFY_MAX_TRIES_ABOVE_0__ERROR_MSG);
117+
}
118+
113119
if (config.getMaxNumberOfTries() != null) {
114120
throw new InvalidRetryConfigException(ALREADY_SPECIFIED_NUMBER_OF_TRIES__ERROR_MSG);
115121
}

src/test/java/com/evanlennick/retry4j/config/RetryConfigBuilderTest_WithValidationTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,21 @@ public void verifyNoMaxTriesThrowsException() {
130130
}
131131
}
132132

133+
@Test
134+
public void verifyZeroMaxTriesThrowsException() {
135+
try {
136+
retryConfigBuilder
137+
.withDelayBetweenTries(1, ChronoUnit.SECONDS)
138+
.withExponentialBackoff()
139+
.withMaxNumberOfTries(0)
140+
.build();
141+
fail("Expected InvalidRetryConfigException but one wasn't thrown!");
142+
} catch (InvalidRetryConfigException e) {
143+
assertThat(e.getMessage())
144+
.isEqualTo(RetryConfigBuilder.MUST_SPECIFY_MAX_TRIES_ABOVE_0__ERROR_MSG);
145+
}
146+
}
147+
133148
@Test
134149
public void verifyTwoExceptionStrategiesThrowsException_anyAndSpecific() {
135150
try {

0 commit comments

Comments
 (0)