mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-22 14:09:25 +00:00
Implement toString() in RetryPolicy and RetryExecution implementations
Closes gh-35029
This commit is contained in:
+58
-1
@@ -25,9 +25,11 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
|
||||
import static org.mockito.Mockito.mock;
|
||||
|
||||
/**
|
||||
* Tests for {@link MaxRetryAttemptsPolicy}.
|
||||
* Tests for {@link MaxRetryAttemptsPolicy} and its {@link RetryExecution}.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Sam Brannen
|
||||
* @since 7.0
|
||||
*/
|
||||
class MaxRetryAttemptsPolicyTests {
|
||||
|
||||
@@ -44,14 +46,69 @@ class MaxRetryAttemptsPolicyTests {
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isTrue();
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isTrue();
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isTrue();
|
||||
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isFalse();
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void customMaxRetryAttempts() {
|
||||
// given
|
||||
MaxRetryAttemptsPolicy retryPolicy = new MaxRetryAttemptsPolicy(2);
|
||||
Throwable throwable = mock();
|
||||
|
||||
// when
|
||||
RetryExecution retryExecution = retryPolicy.start();
|
||||
|
||||
// then
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isTrue();
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isTrue();
|
||||
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isFalse();
|
||||
assertThat(retryExecution.shouldRetry(throwable)).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void invalidMaxRetryAttempts() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new MaxRetryAttemptsPolicy(0))
|
||||
.withMessage("Max retry attempts must be greater than zero");
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> new MaxRetryAttemptsPolicy(-1))
|
||||
.withMessage("Max retry attempts must be greater than zero");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringImplementations() {
|
||||
MaxRetryAttemptsPolicy policy1 = new MaxRetryAttemptsPolicy();
|
||||
MaxRetryAttemptsPolicy policy2 = new MaxRetryAttemptsPolicy(1);
|
||||
|
||||
assertThat(policy1).asString().isEqualTo("MaxRetryAttemptsPolicy[maxRetryAttempts=3]");
|
||||
assertThat(policy2).asString().isEqualTo("MaxRetryAttemptsPolicy[maxRetryAttempts=1]");
|
||||
|
||||
RetryExecution retryExecution = policy1.start();
|
||||
assertThat(retryExecution).asString()
|
||||
.isEqualTo("MaxRetryAttemptsPolicyExecution[retryAttempts=0, maxRetryAttempts=3]");
|
||||
|
||||
assertThat(retryExecution.shouldRetry(mock())).isTrue();
|
||||
assertThat(retryExecution).asString()
|
||||
.isEqualTo("MaxRetryAttemptsPolicyExecution[retryAttempts=1, maxRetryAttempts=3]");
|
||||
|
||||
assertThat(retryExecution.shouldRetry(mock())).isTrue();
|
||||
assertThat(retryExecution).asString()
|
||||
.isEqualTo("MaxRetryAttemptsPolicyExecution[retryAttempts=2, maxRetryAttempts=3]");
|
||||
|
||||
assertThat(retryExecution.shouldRetry(mock())).isTrue();
|
||||
assertThat(retryExecution).asString()
|
||||
.isEqualTo("MaxRetryAttemptsPolicyExecution[retryAttempts=3, maxRetryAttempts=3]");
|
||||
|
||||
assertThat(retryExecution.shouldRetry(mock())).isFalse();
|
||||
assertThat(retryExecution).asString()
|
||||
.isEqualTo("MaxRetryAttemptsPolicyExecution[retryAttempts=4, maxRetryAttempts=3]");
|
||||
|
||||
assertThat(retryExecution.shouldRetry(mock())).isFalse();
|
||||
assertThat(retryExecution).asString()
|
||||
.isEqualTo("MaxRetryAttemptsPolicyExecution[retryAttempts=5, maxRetryAttempts=3]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+20
-1
@@ -20,12 +20,17 @@ import java.time.Duration;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import org.springframework.core.retry.RetryExecution;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
|
||||
/**
|
||||
* Tests for {@link MaxRetryDurationPolicy}.
|
||||
* Tests for {@link MaxRetryDurationPolicy} and its {@link RetryExecution}.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Sam Brannen
|
||||
* @since 7.0
|
||||
*/
|
||||
class MaxRetryDurationPolicyTests {
|
||||
|
||||
@@ -36,4 +41,18 @@ class MaxRetryDurationPolicyTests {
|
||||
.withMessage("Max retry duration must be positive");
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringImplementations() {
|
||||
MaxRetryDurationPolicy policy1 = new MaxRetryDurationPolicy();
|
||||
MaxRetryDurationPolicy policy2 = new MaxRetryDurationPolicy(Duration.ofSeconds(1));
|
||||
|
||||
assertThat(policy1).asString().isEqualTo("MaxRetryDurationPolicy[maxRetryDuration=3000ms]");
|
||||
assertThat(policy2).asString().isEqualTo("MaxRetryDurationPolicy[maxRetryDuration=1000ms]");
|
||||
|
||||
assertThat(policy1.start()).asString()
|
||||
.matches("MaxRetryDurationPolicyExecution\\[retryStartTime=.+, maxRetryDuration=3000ms\\]");
|
||||
assertThat(policy2.start()).asString()
|
||||
.matches("MaxRetryDurationPolicyExecution\\[retryStartTime=.+, maxRetryDuration=1000ms\\]");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
+28
-10
@@ -25,28 +25,46 @@ import org.springframework.core.retry.RetryExecution;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for {@link PredicateRetryPolicy}.
|
||||
* Tests for {@link PredicateRetryPolicy} and its {@link RetryExecution}.
|
||||
*
|
||||
* @author Mahmoud Ben Hassine
|
||||
* @author Sam Brannen
|
||||
* @since 7.0
|
||||
*/
|
||||
class PredicateRetryPolicyTests {
|
||||
|
||||
@Test
|
||||
void predicateRetryPolicy() {
|
||||
// given
|
||||
class MyException extends Exception {
|
||||
@java.io.Serial
|
||||
private static final long serialVersionUID = 1L;
|
||||
}
|
||||
Predicate<Throwable> predicate = MyException.class::isInstance;
|
||||
Predicate<Throwable> predicate = NumberFormatException.class::isInstance;
|
||||
PredicateRetryPolicy retryPolicy = new PredicateRetryPolicy(predicate);
|
||||
|
||||
// when
|
||||
RetryExecution retryExecution = retryPolicy.start();
|
||||
|
||||
// then
|
||||
assertThat(retryExecution.shouldRetry(new MyException())).isTrue();
|
||||
assertThat(retryExecution.shouldRetry(new NumberFormatException())).isTrue();
|
||||
assertThat(retryExecution.shouldRetry(new IllegalStateException())).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
void toStringImplementations() {
|
||||
PredicateRetryPolicy policy1 = new PredicateRetryPolicy(NumberFormatException.class::isInstance);
|
||||
PredicateRetryPolicy policy2 = new PredicateRetryPolicy(new NumberFormatExceptionMatcher());
|
||||
|
||||
assertThat(policy1).asString().matches("PredicateRetryPolicy\\[predicate=PredicateRetryPolicyTests.+?Lambda.+?\\]");
|
||||
assertThat(policy2).asString().isEqualTo("PredicateRetryPolicy[predicate=NumberFormatExceptionMatcher]");
|
||||
|
||||
assertThat(policy1.start()).asString()
|
||||
.matches("PredicateRetryPolicyExecution\\[predicate=PredicateRetryPolicyTests.+?Lambda.+?\\]");
|
||||
assertThat(policy2.start()).asString()
|
||||
.isEqualTo("PredicateRetryPolicyExecution[predicate=NumberFormatExceptionMatcher]");
|
||||
}
|
||||
|
||||
|
||||
private static class NumberFormatExceptionMatcher implements Predicate<Throwable> {
|
||||
|
||||
@Override
|
||||
public boolean test(Throwable throwable) {
|
||||
return (throwable instanceof NumberFormatException);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user