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

Commit

Permalink
added validation to make sure max tries value is valid
Browse files Browse the repository at this point in the history
  • Loading branch information
elennick committed Jul 16, 2018
1 parent acc32d1 commit e6e4666
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public class RetryConfigBuilder {
= "Number of tries can only be specified once!";
public final static String CAN_ONLY_SPECIFY_CUSTOM_EXCEPTION_STRAT__ERROR_MSG
= "You cannot use built in exception logic and custom exception logic in the same config!";
public final static String MUST_SPECIFY_MAX_TRIES_ABOVE_0__ERROR_MSG
= "Cannot specify a maximum number of tries less than 1!";

public RetryConfigBuilder() {
this.config = new RetryConfig();
Expand Down Expand Up @@ -110,6 +112,10 @@ public RetryConfigBuilder retryOnCustomExceptionLogic(Function<Exception, Boolea
}

public RetryConfigBuilder withMaxNumberOfTries(int max) {
if (max < 1) {
throw new InvalidRetryConfigException(MUST_SPECIFY_MAX_TRIES_ABOVE_0__ERROR_MSG);
}

if (config.getMaxNumberOfTries() != null) {
throw new InvalidRetryConfigException(ALREADY_SPECIFIED_NUMBER_OF_TRIES__ERROR_MSG);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,21 @@ public void verifyNoMaxTriesThrowsException() {
}
}

@Test
public void verifyZeroMaxTriesThrowsException() {
try {
retryConfigBuilder
.withDelayBetweenTries(1, ChronoUnit.SECONDS)
.withExponentialBackoff()
.withMaxNumberOfTries(0)
.build();
fail("Expected InvalidRetryConfigException but one wasn't thrown!");
} catch (InvalidRetryConfigException e) {
assertThat(e.getMessage())
.isEqualTo(RetryConfigBuilder.MUST_SPECIFY_MAX_TRIES_ABOVE_0__ERROR_MSG);
}
}

@Test
public void verifyTwoExceptionStrategiesThrowsException_anyAndSpecific() {
try {
Expand Down

0 comments on commit e6e4666

Please sign in to comment.