Consistently implement toString() in BackOff strategies

Closes gh-35120
This commit is contained in:
Sam Brannen 2025-06-27 16:10:55 +02:00
parent d97288a74e
commit 98b29b5e37
4 changed files with 32 additions and 11 deletions

View File

@ -255,7 +255,7 @@ public class ExponentialBackOff implements BackOff {
@Override
public String toString() {
return new StringJoiner(", ", ExponentialBackOff.class.getSimpleName() + "{", "}")
return new StringJoiner(", ", "ExponentialBackOff[", "]")
.add("initialInterval=" + this.initialInterval)
.add("jitter=" + this.jitter)
.add("multiplier=" + this.multiplier)
@ -316,7 +316,7 @@ public class ExponentialBackOff implements BackOff {
@Override
public String toString() {
String currentIntervalDescription = this.currentInterval < 0 ? "n/a" : this.currentInterval + "ms";
return new StringJoiner(", ", ExponentialBackOffExecution.class.getSimpleName() + "{", "}")
return new StringJoiner(", ", "ExponentialBackOffExecution[", "]")
.add("currentInterval=" + currentIntervalDescription)
.add("multiplier=" + getMultiplier())
.add("attempts=" + this.attempts)

View File

@ -119,6 +119,13 @@ public class FixedBackOff implements BackOff {
return new FixedBackOffExecution();
}
@Override
public String toString() {
String attemptValue = (this.maxAttempts == Long.MAX_VALUE ? "unlimited" :
String.valueOf(FixedBackOff.this.maxAttempts));
return "FixedBackOff[interval=" + this.interval +
", maxAttempts=" + attemptValue + ']';
}
private class FixedBackOffExecution implements BackOffExecution {
@ -139,10 +146,10 @@ public class FixedBackOff implements BackOff {
public String toString() {
String attemptValue = (FixedBackOff.this.maxAttempts == Long.MAX_VALUE ?
"unlimited" : String.valueOf(FixedBackOff.this.maxAttempts));
return "FixedBackOff{interval=" + FixedBackOff.this.interval +
return "FixedBackOffExecution[interval=" + FixedBackOff.this.interval +
", currentAttempts=" + this.currentAttempts +
", maxAttempts=" + attemptValue +
'}';
']';
}
}

View File

@ -32,6 +32,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* Tests for {@link ExponentialBackOff}.
*
* @author Stephane Nicoll
* @author Sam Brannen
*/
class ExponentialBackOffTests {
@ -128,14 +129,24 @@ class ExponentialBackOffTests {
}
@Test
void executionToStringContent() {
void toStringContent() {
ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
assertThat(backOff).asString()
.isEqualTo("""
ExponentialBackOff[\
initialInterval=2000, \
jitter=0, \
multiplier=2.0, \
maxInterval=30000, \
maxElapsedTime=%d, \
maxAttempts=%d]""", Long.MAX_VALUE, Integer.MAX_VALUE);
BackOffExecution execution = backOff.start();
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=n/a, multiplier=2.0, attempts=0}");
assertThat(execution).asString().isEqualTo("ExponentialBackOffExecution[currentInterval=n/a, multiplier=2.0, attempts=0]");
execution.nextBackOff();
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=2000ms, multiplier=2.0, attempts=1}");
assertThat(execution).asString().isEqualTo("ExponentialBackOffExecution[currentInterval=2000ms, multiplier=2.0, attempts=1]");
execution.nextBackOff();
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=4000ms, multiplier=2.0, attempts=2}");
assertThat(execution).asString().isEqualTo("ExponentialBackOffExecution[currentInterval=4000ms, multiplier=2.0, attempts=2]");
}
@Test

View File

@ -27,6 +27,7 @@ import static org.assertj.core.api.Assertions.assertThat;
* Tests for {@link FixedBackOff}.
*
* @author Stephane Nicoll
* @author Sam Brannen
*/
class FixedBackOffTests {
@ -86,12 +87,14 @@ class FixedBackOffTests {
@Test
void toStringContent() {
FixedBackOff backOff = new FixedBackOff(200L, 10);
assertThat(backOff).asString().isEqualTo("FixedBackOff[interval=200, maxAttempts=10]");
BackOffExecution execution = backOff.start();
assertThat(execution.toString()).isEqualTo("FixedBackOff{interval=200, currentAttempts=0, maxAttempts=10}");
assertThat(execution).asString().isEqualTo("FixedBackOffExecution[interval=200, currentAttempts=0, maxAttempts=10]");
execution.nextBackOff();
assertThat(execution.toString()).isEqualTo("FixedBackOff{interval=200, currentAttempts=1, maxAttempts=10}");
assertThat(execution).asString().isEqualTo("FixedBackOffExecution[interval=200, currentAttempts=1, maxAttempts=10]");
execution.nextBackOff();
assertThat(execution.toString()).isEqualTo("FixedBackOff{interval=200, currentAttempts=2, maxAttempts=10}");
assertThat(execution).asString().isEqualTo("FixedBackOffExecution[interval=200, currentAttempts=2, maxAttempts=10]");
}
}