Skip to content

Commit 98b29b5

Browse files
committed
Consistently implement toString() in BackOff strategies
Closes gh-35120
1 parent d97288a commit 98b29b5

File tree

4 files changed

+32
-11
lines changed

4 files changed

+32
-11
lines changed

spring-core/src/main/java/org/springframework/util/backoff/ExponentialBackOff.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,7 +255,7 @@ public BackOffExecution start() {
255255

256256
@Override
257257
public String toString() {
258-
return new StringJoiner(", ", ExponentialBackOff.class.getSimpleName() + "{", "}")
258+
return new StringJoiner(", ", "ExponentialBackOff[", "]")
259259
.add("initialInterval=" + this.initialInterval)
260260
.add("jitter=" + this.jitter)
261261
.add("multiplier=" + this.multiplier)
@@ -316,7 +316,7 @@ private long applyJitter(long interval) {
316316
@Override
317317
public String toString() {
318318
String currentIntervalDescription = this.currentInterval < 0 ? "n/a" : this.currentInterval + "ms";
319-
return new StringJoiner(", ", ExponentialBackOffExecution.class.getSimpleName() + "{", "}")
319+
return new StringJoiner(", ", "ExponentialBackOffExecution[", "]")
320320
.add("currentInterval=" + currentIntervalDescription)
321321
.add("multiplier=" + getMultiplier())
322322
.add("attempts=" + this.attempts)

spring-core/src/main/java/org/springframework/util/backoff/FixedBackOff.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ public BackOffExecution start() {
119119
return new FixedBackOffExecution();
120120
}
121121

122+
@Override
123+
public String toString() {
124+
String attemptValue = (this.maxAttempts == Long.MAX_VALUE ? "unlimited" :
125+
String.valueOf(FixedBackOff.this.maxAttempts));
126+
return "FixedBackOff[interval=" + this.interval +
127+
", maxAttempts=" + attemptValue + ']';
128+
}
122129

123130
private class FixedBackOffExecution implements BackOffExecution {
124131

@@ -139,10 +146,10 @@ public long nextBackOff() {
139146
public String toString() {
140147
String attemptValue = (FixedBackOff.this.maxAttempts == Long.MAX_VALUE ?
141148
"unlimited" : String.valueOf(FixedBackOff.this.maxAttempts));
142-
return "FixedBackOff{interval=" + FixedBackOff.this.interval +
149+
return "FixedBackOffExecution[interval=" + FixedBackOff.this.interval +
143150
", currentAttempts=" + this.currentAttempts +
144151
", maxAttempts=" + attemptValue +
145-
'}';
152+
']';
146153
}
147154
}
148155

spring-core/src/test/java/org/springframework/util/ExponentialBackOffTests.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
* Tests for {@link ExponentialBackOff}.
3333
*
3434
* @author Stephane Nicoll
35+
* @author Sam Brannen
3536
*/
3637
class ExponentialBackOffTests {
3738

@@ -128,14 +129,24 @@ void maxIntervalReachedImmediately() {
128129
}
129130

130131
@Test
131-
void executionToStringContent() {
132+
void toStringContent() {
132133
ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
134+
assertThat(backOff).asString()
135+
.isEqualTo("""
136+
ExponentialBackOff[\
137+
initialInterval=2000, \
138+
jitter=0, \
139+
multiplier=2.0, \
140+
maxInterval=30000, \
141+
maxElapsedTime=%d, \
142+
maxAttempts=%d]""", Long.MAX_VALUE, Integer.MAX_VALUE);
143+
133144
BackOffExecution execution = backOff.start();
134-
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=n/a, multiplier=2.0, attempts=0}");
145+
assertThat(execution).asString().isEqualTo("ExponentialBackOffExecution[currentInterval=n/a, multiplier=2.0, attempts=0]");
135146
execution.nextBackOff();
136-
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=2000ms, multiplier=2.0, attempts=1}");
147+
assertThat(execution).asString().isEqualTo("ExponentialBackOffExecution[currentInterval=2000ms, multiplier=2.0, attempts=1]");
137148
execution.nextBackOff();
138-
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=4000ms, multiplier=2.0, attempts=2}");
149+
assertThat(execution).asString().isEqualTo("ExponentialBackOffExecution[currentInterval=4000ms, multiplier=2.0, attempts=2]");
139150
}
140151

141152
@Test

spring-core/src/test/java/org/springframework/util/FixedBackOffTests.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
* Tests for {@link FixedBackOff}.
2828
*
2929
* @author Stephane Nicoll
30+
* @author Sam Brannen
3031
*/
3132
class FixedBackOffTests {
3233

@@ -86,12 +87,14 @@ void liveUpdate() {
8687
@Test
8788
void toStringContent() {
8889
FixedBackOff backOff = new FixedBackOff(200L, 10);
90+
assertThat(backOff).asString().isEqualTo("FixedBackOff[interval=200, maxAttempts=10]");
91+
8992
BackOffExecution execution = backOff.start();
90-
assertThat(execution.toString()).isEqualTo("FixedBackOff{interval=200, currentAttempts=0, maxAttempts=10}");
93+
assertThat(execution).asString().isEqualTo("FixedBackOffExecution[interval=200, currentAttempts=0, maxAttempts=10]");
9194
execution.nextBackOff();
92-
assertThat(execution.toString()).isEqualTo("FixedBackOff{interval=200, currentAttempts=1, maxAttempts=10}");
95+
assertThat(execution).asString().isEqualTo("FixedBackOffExecution[interval=200, currentAttempts=1, maxAttempts=10]");
9396
execution.nextBackOff();
94-
assertThat(execution.toString()).isEqualTo("FixedBackOff{interval=200, currentAttempts=2, maxAttempts=10}");
97+
assertThat(execution).asString().isEqualTo("FixedBackOffExecution[interval=200, currentAttempts=2, maxAttempts=10]");
9598
}
9699

97100
}

0 commit comments

Comments
 (0)