diff --git a/src/main/java/org/springframework/retry/backoff/ExponentialBackOffPolicy.java b/src/main/java/org/springframework/retry/backoff/ExponentialBackOffPolicy.java index 194f7d4..bd1f71c 100644 --- a/src/main/java/org/springframework/retry/backoff/ExponentialBackOffPolicy.java +++ b/src/main/java/org/springframework/retry/backoff/ExponentialBackOffPolicy.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 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. @@ -41,6 +41,7 @@ import org.springframework.util.ClassUtils; * @author Dave Syer * @author Gary Russell * @author Artem Bilan + * @author Marius Lichtblau */ @SuppressWarnings("serial") public class ExponentialBackOffPolicy implements SleepingBackOffPolicy { @@ -245,6 +246,7 @@ public class ExponentialBackOffPolicy implements SleepingBackOffPolicy { @@ -97,6 +98,7 @@ public class FixedBackOffPolicy extends StatelessBackOffPolicy implements Sleepi sleeper.sleep(this.backOffPeriod.get()); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); throw new BackOffInterruptedException("Thread interrupted while sleeping", e); } } diff --git a/src/main/java/org/springframework/retry/backoff/UniformRandomBackOffPolicy.java b/src/main/java/org/springframework/retry/backoff/UniformRandomBackOffPolicy.java index ef696d8..68249f7 100644 --- a/src/main/java/org/springframework/retry/backoff/UniformRandomBackOffPolicy.java +++ b/src/main/java/org/springframework/retry/backoff/UniformRandomBackOffPolicy.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 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. @@ -32,6 +32,7 @@ import org.springframework.util.Assert; * @author Rob Harrop * @author Dave Syer * @author Tomaz Fernandes + * @author Marius Lichtblau */ public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy { @@ -138,6 +139,7 @@ public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy this.sleeper.sleep(min + delta); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); throw new BackOffInterruptedException("Thread interrupted while sleeping", e); } } diff --git a/src/test/java/org/springframework/retry/backoff/ExponentialBackOffPolicyTests.java b/src/test/java/org/springframework/retry/backoff/ExponentialBackOffPolicyTests.java index ab63a4f..294203d 100644 --- a/src/test/java/org/springframework/retry/backoff/ExponentialBackOffPolicyTests.java +++ b/src/test/java/org/springframework/retry/backoff/ExponentialBackOffPolicyTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 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. @@ -19,11 +19,13 @@ package org.springframework.retry.backoff; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rob Harrop * @author Dave Syer * @author Gary Russell + * @author Marius Lichtblau */ public class ExponentialBackOffPolicyTests { @@ -92,4 +94,18 @@ public class ExponentialBackOffPolicyTests { } } + @Test + public void testInterruptedStatusIsRestored() { + ExponentialBackOffPolicy strategy = new ExponentialBackOffPolicy(); + strategy.setSleeper(new Sleeper() { + @Override + public void sleep(long backOffPeriod) throws InterruptedException { + throw new InterruptedException("foo"); + } + }); + BackOffContext context = strategy.start(null); + assertThatExceptionOfType(BackOffInterruptedException.class).isThrownBy(() -> strategy.backOff(context)); + assertThat(Thread.interrupted()).isTrue(); + } + } diff --git a/src/test/java/org/springframework/retry/backoff/FixedBackOffPolicyTests.java b/src/test/java/org/springframework/retry/backoff/FixedBackOffPolicyTests.java index 5ca7091..f57c1e7 100644 --- a/src/test/java/org/springframework/retry/backoff/FixedBackOffPolicyTests.java +++ b/src/test/java/org/springframework/retry/backoff/FixedBackOffPolicyTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 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. @@ -19,11 +19,13 @@ package org.springframework.retry.backoff; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Rob Harrop * @author Dave Syer * @author Gary Russell + * @author Marius Lichtblau * @since 2.1 */ public class FixedBackOffPolicyTests { @@ -65,4 +67,19 @@ public class FixedBackOffPolicyTests { assertThat(sleeper.getBackOffs().length).isEqualTo(10); } + @Test + public void testInterruptedStatusIsRestored() { + int backOffPeriod = 50; + FixedBackOffPolicy strategy = new FixedBackOffPolicy(); + strategy.setBackOffPeriod(backOffPeriod); + strategy.setSleeper(new Sleeper() { + @Override + public void sleep(long backOffPeriod) throws InterruptedException { + throw new InterruptedException("foo"); + } + }); + assertThatExceptionOfType(BackOffInterruptedException.class).isThrownBy(() -> strategy.backOff(null)); + assertThat(Thread.interrupted()).isTrue(); + } + } diff --git a/src/test/java/org/springframework/retry/backoff/UniformRandomBackOffPolicyTests.java b/src/test/java/org/springframework/retry/backoff/UniformRandomBackOffPolicyTests.java index 1dd3095..0ea1838 100644 --- a/src/test/java/org/springframework/retry/backoff/UniformRandomBackOffPolicyTests.java +++ b/src/test/java/org/springframework/retry/backoff/UniformRandomBackOffPolicyTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2006-2022 the original author or authors. + * Copyright 2006-2023 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. @@ -19,10 +19,12 @@ package org.springframework.retry.backoff; import org.junit.jupiter.api.Test; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; /** * @author Tomaz Fernandes * @author Gary Russell + * @author Marius Lichtblau * @since 1.3.2 */ public class UniformRandomBackOffPolicyTests { @@ -40,4 +42,22 @@ public class UniformRandomBackOffPolicyTests { assertThat(withSleeper.getMaxBackOffPeriod()).isEqualTo(maxBackOff); } + @Test + public void testInterruptedStatusIsRestored() { + UniformRandomBackOffPolicy backOffPolicy = new UniformRandomBackOffPolicy(); + int minBackOff = 1000; + int maxBackOff = 10000; + backOffPolicy.setMinBackOffPeriod(minBackOff); + backOffPolicy.setMaxBackOffPeriod(maxBackOff); + UniformRandomBackOffPolicy withSleeper = backOffPolicy.withSleeper(new Sleeper() { + @Override + public void sleep(long backOffPeriod) throws InterruptedException { + throw new InterruptedException("foo"); + } + }); + + assertThatExceptionOfType(BackOffInterruptedException.class).isThrownBy(() -> withSleeper.backOff(null)); + assertThat(Thread.interrupted()).isTrue(); + } + }