Skip to content

Commit d97288a

Browse files
committed
Improve Javadoc and tests for BackOff strategies
1 parent 28f9adf commit d97288a

File tree

5 files changed

+43
-30
lines changed

5 files changed

+43
-30
lines changed

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
package org.springframework.util.backoff;
1818

1919
/**
20-
* Provide a {@link BackOffExecution} that indicates the rate at which
21-
* an operation should be retried.
20+
* Strategy interface for providing a {@link BackOffExecution} that indicates the
21+
* rate at which an operation should be retried.
2222
*
2323
* <p>Users of this interface are expected to use it like this:
2424
*
2525
* <pre class="code">
26-
* BackOffExecution exec = backOff.start();
26+
* BackOffExecution execution = backOff.start();
2727
*
2828
* // In the operation recovery/retry loop:
29-
* long waitInterval = exec.nextBackOff();
29+
* long waitInterval = execution.nextBackOff();
3030
* if (waitInterval == BackOffExecution.STOP) {
3131
* // do not retry operation
3232
* }
@@ -35,12 +35,14 @@
3535
* // retry operation
3636
* }</pre>
3737
*
38-
* Once the underlying operation has completed successfully,
39-
* the execution instance can be discarded.
38+
* <p>Once the underlying operation has completed successfully, the execution
39+
* instance can be discarded.
4040
*
4141
* @author Stephane Nicoll
4242
* @since 4.1
4343
* @see BackOffExecution
44+
* @see FixedBackOff
45+
* @see ExponentialBackOff
4446
*/
4547
@FunctionalInterface
4648
public interface BackOff {

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,10 @@
1717
package org.springframework.util.backoff;
1818

1919
/**
20-
* Represent a particular back-off execution.
20+
* <p>A {@code BackOffExecution} is effectively an executable instance of a given
21+
* {@link BackOff} strategy.
2122
*
22-
* <p>Implementations do not need to be thread safe.
23+
* <p>Implementations may be stateful but do not need to be thread-safe.
2324
*
2425
* @author Stephane Nicoll
2526
* @since 4.1
@@ -29,7 +30,7 @@
2930
public interface BackOffExecution {
3031

3132
/**
32-
* Return value of {@link #nextBackOff()} that indicates that the operation
33+
* Return value of {@link #nextBackOff()} which indicates that the operation
3334
* should not be retried.
3435
*/
3536
long STOP = -1;

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

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public class ExponentialBackOff implements BackOff {
6666
public static final long DEFAULT_INITIAL_INTERVAL = 2000L;
6767

6868
/**
69-
* The default jitter range for each interval: {@value} ms.
69+
* The default jitter value for each interval: {@value} ms.
7070
* @since 7.0
7171
*/
7272
public static final long DEFAULT_JITTER = 0;
@@ -121,7 +121,7 @@ public ExponentialBackOff() {
121121
/**
122122
* Create an instance with the supplied settings.
123123
* @param initialInterval the initial interval in milliseconds
124-
* @param multiplier the multiplier (should be greater than or equal to 1)
124+
* @param multiplier the multiplier (must be greater than or equal to 1)
125125
*/
126126
public ExponentialBackOff(long initialInterval, double multiplier) {
127127
checkMultiplier(multiplier);
@@ -131,7 +131,8 @@ public ExponentialBackOff(long initialInterval, double multiplier) {
131131

132132

133133
/**
134-
* Set the initial interval in milliseconds.
134+
* Set the initial interval.
135+
* @param initialInterval the initial interval in milliseconds
135136
*/
136137
public void setInitialInterval(long initialInterval) {
137138
this.initialInterval = initialInterval;
@@ -145,20 +146,22 @@ public long getInitialInterval() {
145146
}
146147

147148
/**
148-
* Set the jitter range (ms) to apply for each interval, leading to random
149-
* milliseconds within the range to be subtracted or added, resulting in a
150-
* value between {@code interval - jitter} and {@code interval + jitter}
151-
* but never below {@code initialInterval} or above {@code maxInterval}.
152-
* If a multiplier is specified, it applies to the jitter range as well.
149+
* Set the jitter value to apply for each interval, leading to random
150+
* milliseconds to be subtracted or added and resulting in a value between
151+
* {@code interval - jitter} and {@code interval + jitter} but never below
152+
* {@code initialInterval} or above {@code maxInterval}.
153+
* <p>If a {@code multiplier} is specified, it is applied to the jitter value
154+
* as well.
155+
* @param jitter the jitter value in milliseconds
153156
* @since 7.0
154157
*/
155158
public void setJitter(long jitter) {
156-
Assert.isTrue(jitter >= 0, () -> "Invalid jitter '" + jitter + "': Must be >=0.");
159+
Assert.isTrue(jitter >= 0, () -> "Invalid jitter '" + jitter + "': must be >= 0.");
157160
this.jitter = jitter;
158161
}
159162

160163
/**
161-
* Return the jitter range to apply for each interval.
164+
* Return the jitter value to apply for each interval in milliseconds.
162165
* @since 7.0
163166
*/
164167
public long getJitter() {
@@ -169,6 +172,7 @@ public long getJitter() {
169172
* Set the value to multiply the current interval by for each attempt.
170173
* <p>This applies to the {@linkplain #setInitialInterval initial interval}
171174
* as well as the {@linkplain #setJitter jitter range}.
175+
* @param multiplier the multiplier (must be greater than or equal to 1)
172176
*/
173177
public void setMultiplier(double multiplier) {
174178
checkMultiplier(multiplier);

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,28 +92,29 @@ void maxAttemptsReached() {
9292
}
9393

9494
@Test
95-
void startReturnDifferentInstances() {
95+
void startReturnsDifferentInstances() {
9696
ExponentialBackOff backOff = new ExponentialBackOff();
9797
backOff.setInitialInterval(2000L);
9898
backOff.setMultiplier(2.0);
9999
backOff.setMaxElapsedTime(4000L);
100100

101-
BackOffExecution execution = backOff.start();
101+
BackOffExecution execution1 = backOff.start();
102102
BackOffExecution execution2 = backOff.start();
103103

104-
assertThat(execution.nextBackOff()).isEqualTo(2000L);
104+
assertThat(execution1).isNotSameAs(execution2);
105+
106+
assertThat(execution1.nextBackOff()).isEqualTo(2000L);
105107
assertThat(execution2.nextBackOff()).isEqualTo(2000L);
106-
assertThat(execution.nextBackOff()).isEqualTo(4000L);
108+
assertThat(execution1.nextBackOff()).isEqualTo(4000L);
107109
assertThat(execution2.nextBackOff()).isEqualTo(4000L);
108-
assertThat(execution.nextBackOff()).isEqualTo(BackOffExecution.STOP);
110+
assertThat(execution1.nextBackOff()).isEqualTo(BackOffExecution.STOP);
109111
assertThat(execution2.nextBackOff()).isEqualTo(BackOffExecution.STOP);
110112
}
111113

112114
@Test
113115
void invalidInterval() {
114116
ExponentialBackOff backOff = new ExponentialBackOff();
115-
assertThatIllegalArgumentException().isThrownBy(() ->
116-
backOff.setMultiplier(0.9));
117+
assertThatIllegalArgumentException().isThrownBy(() -> backOff.setMultiplier(0.9));
117118
}
118119

119120
@Test

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import static org.assertj.core.api.Assertions.assertThat;
2525

2626
/**
27+
* Tests for {@link FixedBackOff}.
28+
*
2729
* @author Stephane Nicoll
2830
*/
2931
class FixedBackOffTests {
@@ -54,14 +56,17 @@ void maxAttemptsReached() {
5456
}
5557

5658
@Test
57-
void startReturnDifferentInstances() {
59+
void startReturnsDifferentInstances() {
5860
FixedBackOff backOff = new FixedBackOff(100L, 1);
59-
BackOffExecution execution = backOff.start();
61+
62+
BackOffExecution execution1 = backOff.start();
6063
BackOffExecution execution2 = backOff.start();
6164

62-
assertThat(execution.nextBackOff()).isEqualTo(100L);
65+
assertThat(execution1).isNotSameAs(execution2);
66+
67+
assertThat(execution1.nextBackOff()).isEqualTo(100L);
6368
assertThat(execution2.nextBackOff()).isEqualTo(100L);
64-
assertThat(execution.nextBackOff()).isEqualTo(BackOffExecution.STOP);
69+
assertThat(execution1.nextBackOff()).isEqualTo(BackOffExecution.STOP);
6570
assertThat(execution2.nextBackOff()).isEqualTo(BackOffExecution.STOP);
6671
}
6772

0 commit comments

Comments
 (0)