From c89b9516d9763cbb97689071dc4c8dc50090bf1e Mon Sep 17 00:00:00 2001 From: Marius Lichtblau Date: Thu, 19 Oct 2023 17:28:22 +0200 Subject: [PATCH] GH-386: Restore interrupted thread status Fixes https://github.com/spring-projects/spring-retry/issues/386 * Update author and copyright **Cherry-pick to `1.3.x`** --- .../backoff/ExponentialBackOffPolicy.java | 4 +++- .../retry/backoff/FixedBackOffPolicy.java | 4 +++- .../backoff/UniformRandomBackOffPolicy.java | 4 +++- .../ExponentialBackOffPolicyTests.java | 18 ++++++++++++++- .../backoff/FixedBackOffPolicyTests.java | 19 +++++++++++++++- .../UniformRandomBackOffPolicyTests.java | 22 ++++++++++++++++++- 6 files changed, 65 insertions(+), 6 deletions(-) diff --git a/src/main/java/org/springframework/retry/backoff/ExponentialBackOffPolicy.java b/src/main/java/org/springframework/retry/backoff/ExponentialBackOffPolicy.java index 194f7d46..bd1f71c3 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 @@ * @author Dave Syer * @author Gary Russell * @author Artem Bilan + * @author Marius Lichtblau */ @SuppressWarnings("serial") public class ExponentialBackOffPolicy implements SleepingBackOffPolicy { @@ -245,6 +246,7 @@ public void backOff(BackOffContext backOffContext) throws BackOffInterruptedExce this.sleeper.sleep(sleepTime); } catch (InterruptedException e) { + Thread.currentThread().interrupt(); throw new BackOffInterruptedException("Thread interrupted while sleeping", e); } } diff --git a/src/main/java/org/springframework/retry/backoff/FixedBackOffPolicy.java b/src/main/java/org/springframework/retry/backoff/FixedBackOffPolicy.java index db12bb9f..fc26986a 100644 --- a/src/main/java/org/springframework/retry/backoff/FixedBackOffPolicy.java +++ b/src/main/java/org/springframework/retry/backoff/FixedBackOffPolicy.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. @@ -31,6 +31,7 @@ * @author Rob Harrop * @author Dave Syer * @author Artem Bilan + * @author Marius Lichtblau */ public class FixedBackOffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy { @@ -97,6 +98,7 @@ protected void doBackOff() throws BackOffInterruptedException { 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 ef696d88..68249f7b 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 @@ * @author Rob Harrop * @author Dave Syer * @author Tomaz Fernandes + * @author Marius Lichtblau */ public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy { @@ -138,6 +139,7 @@ protected void doBackOff() throws BackOffInterruptedException { 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 ab63a4f4..294203da 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 @@ 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 void testMultiBackOff() { } } + @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 5ca7091d..f57c1e7b 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 @@ 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 void testManyBackOffCalls() { 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 1dd3095b..0ea18385 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 @@ 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 void testSetSleeper() { 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(); + } + }