GH-234: UniRndBackOffPolicy: Propagate maxPeriod

Fixes https://github.com/spring-projects/spring-retry/issues/234

Currently `UniformRandomBackOffPolicy` loses `maxBackOffPeriod` when `withSleeper` is used

* Propagate `maxBackOffPeriod` into a new `UniformRandomBackOffPolicy` instance produced
by the `withSleeper(Sleeper)` API
This commit is contained in:
Tomaz Fernandes
2021-03-16 11:59:18 -03:00
committed by GitHub
parent 65377ea778
commit 5bdad2fe54
2 changed files with 45 additions and 1 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2014 the original author or authors.
* Copyright 2006-2021 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.
@@ -28,6 +28,7 @@ import java.util.Random;
*
* @author Rob Harrop
* @author Dave Syer
* @author Tomaz Fernandes
*/
public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy
implements SleepingBackOffPolicy<UniformRandomBackOffPolicy> {
@@ -53,6 +54,7 @@ public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy
public UniformRandomBackOffPolicy withSleeper(Sleeper sleeper) {
UniformRandomBackOffPolicy res = new UniformRandomBackOffPolicy();
res.setMinBackOffPeriod(minBackOffPeriod);
res.setMaxBackOffPeriod(maxBackOffPeriod);
res.setSleeper(sleeper);
return res;
}

View File

@@ -0,0 +1,42 @@
/*
* Copyright 2021 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.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.retry.backoff;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
/**
* @author Tomaz Fernandes
* @since 1.3.2
*/
public class UniformRandomBackOffPolicyTest {
@Test
public void testSetSleeper() {
UniformRandomBackOffPolicy backOffPolicy = new UniformRandomBackOffPolicy();
int minBackOff = 1000;
int maxBackOff = 10000;
backOffPolicy.setMinBackOffPeriod(minBackOff);
backOffPolicy.setMaxBackOffPeriod(maxBackOff);
UniformRandomBackOffPolicy withSleeper = backOffPolicy.withSleeper(new DummySleeper());
assertEquals(minBackOff, withSleeper.getMinBackOffPeriod());
assertEquals(maxBackOff, withSleeper.getMaxBackOffPeriod());
}
}