Skip to content

Commit

Permalink
GH-386: Restore interrupted thread status
Browse files Browse the repository at this point in the history
Fixes #386

* Update author and copyright

**Cherry-pick to `1.3.x`**
  • Loading branch information
mlichtblau authored Oct 19, 2023
1 parent 6af7bc0 commit c89b951
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -41,6 +41,7 @@
* @author Dave Syer
* @author Gary Russell
* @author Artem Bilan
* @author Marius Lichtblau
*/
@SuppressWarnings("serial")
public class ExponentialBackOffPolicy implements SleepingBackOffPolicy<ExponentialBackOffPolicy> {
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -31,6 +31,7 @@
* @author Rob Harrop
* @author Dave Syer
* @author Artem Bilan
* @author Marius Lichtblau
*/
public class FixedBackOffPolicy extends StatelessBackOffPolicy implements SleepingBackOffPolicy<FixedBackOffPolicy> {

Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -32,6 +32,7 @@
* @author Rob Harrop
* @author Dave Syer
* @author Tomaz Fernandes
* @author Marius Lichtblau
*/
public class UniformRandomBackOffPolicy extends StatelessBackOffPolicy
implements SleepingBackOffPolicy<UniformRandomBackOffPolicy> {
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {

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

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand Down Expand Up @@ -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();
}

}
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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 {
Expand All @@ -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();
}

}

0 comments on commit c89b951

Please sign in to comment.