Skip to content

Commit

Permalink
Improve toString of ExponentialBackOff
Browse files Browse the repository at this point in the history
Closes gh-32061
  • Loading branch information
snicoll committed Jan 19, 2024
1 parent 7e51193 commit 9c6e559
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,6 +16,8 @@

package org.springframework.util.backoff;

import java.util.StringJoiner;

import org.springframework.util.Assert;

/**
Expand Down Expand Up @@ -212,6 +214,16 @@ private void checkMultiplier(double multiplier) {
"or equal to 1. A multiplier of 1 is equivalent to a fixed interval.");
}

@Override
public String toString() {
return new StringJoiner(", ", ExponentialBackOff.class.getSimpleName() + "{", "}")
.add("initialInterval=" + this.initialInterval)
.add("multiplier=" + this.multiplier)
.add("maxInterval=" + this.maxInterval)
.add("maxElapsedTime=" + this.maxElapsedTime)
.add("maxAttempts=" + this.maxAttempts)
.toString();
}

private class ExponentialBackOffExecution implements BackOffExecution {

Expand Down Expand Up @@ -255,11 +267,12 @@ private long multiplyInterval(long maxInterval) {

@Override
public String toString() {
StringBuilder sb = new StringBuilder("ExponentialBackOff{");
sb.append("currentInterval=").append(this.currentInterval < 0 ? "n/a" : this.currentInterval + "ms");
sb.append(", multiplier=").append(getMultiplier());
sb.append('}');
return sb.toString();
String currentIntervalDescription = this.currentInterval < 0 ? "n/a" : this.currentInterval + "ms";
return new StringJoiner(", ", ExponentialBackOffExecution.class.getSimpleName() + "{", "}")
.add("currentInterval=" + currentIntervalDescription)
.add("multiplier=" + getMultiplier())
.add("attempts=" + this.attempts)
.toString();
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -127,14 +127,14 @@ void maxIntervalReachedImmediately() {
}

@Test
void toStringContent() {
void executionToStringContent() {
ExponentialBackOff backOff = new ExponentialBackOff(2000L, 2.0);
BackOffExecution execution = backOff.start();
assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=n/a, multiplier=2.0}");
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=n/a, multiplier=2.0, attempts=0}");
execution.nextBackOff();
assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=2000ms, multiplier=2.0}");
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=2000ms, multiplier=2.0, attempts=1}");
execution.nextBackOff();
assertThat(execution.toString()).isEqualTo("ExponentialBackOff{currentInterval=4000ms, multiplier=2.0}");
assertThat(execution.toString()).isEqualTo("ExponentialBackOffExecution{currentInterval=4000ms, multiplier=2.0, attempts=2}");
}

@Test
Expand Down

0 comments on commit 9c6e559

Please sign in to comment.