Skip to content

Commit

Permalink
spring-projectsGH-386: Restore interrupted thread status in provided …
Browse files Browse the repository at this point in the history
…backoff policies
  • Loading branch information
mlichtblau committed Oct 16, 2023
1 parent 6af7bc0 commit 7647607
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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
Expand Down Expand Up @@ -92,4 +93,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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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
Expand Down Expand Up @@ -65,4 +66,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();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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
Expand All @@ -40,4 +41,23 @@ 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();
}

}

0 comments on commit 7647607

Please sign in to comment.