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`**
This commit is contained in:
Marius Lichtblau
2023-10-19 17:28:22 +02:00
committed by GitHub
parent 6af7bc0eeb
commit c89b9516d9
6 changed files with 65 additions and 6 deletions

View File

@@ -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<ExponentialBackOffPolicy> {
@@ -245,6 +246,7 @@ public class ExponentialBackOffPolicy implements SleepingBackOffPolicy<Exponenti
this.sleeper.sleep(sleepTime);
}
catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new BackOffInterruptedException("Thread interrupted while sleeping", e);
}
}

View File

@@ -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 @@ import org.springframework.util.Assert;
* @author Rob Harrop
* @author Dave Syer
* @author Artem Bilan
* @author Marius Lichtblau
*/
public class FixedBackOffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy<FixedBackOffPolicy> {
@@ -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);
}
}

View File

@@ -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<UniformRandomBackOffPolicy> {
@@ -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);
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}

View File

@@ -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();
}
}