diff --git a/src/main/java/org/cactoos/func/Retry.java b/src/main/java/org/cactoos/func/Retry.java index 9f586b0364..e64447c74d 100644 --- a/src/main/java/org/cactoos/func/Retry.java +++ b/src/main/java/org/cactoos/func/Retry.java @@ -125,7 +125,12 @@ public Y apply(final X input) throws Exception { error = ex; } if (!this.wait.isZero() && !this.wait.isNegative()) { - Thread.sleep(this.wait.toMillis()); + try { + Thread.sleep(this.wait.toMillis()); + } catch (final InterruptedException ex) { + error = ex; + break; + } } ++attempt; } diff --git a/src/main/java/org/cactoos/scalar/Retry.java b/src/main/java/org/cactoos/scalar/Retry.java index 777f2b242c..07bc725789 100644 --- a/src/main/java/org/cactoos/scalar/Retry.java +++ b/src/main/java/org/cactoos/scalar/Retry.java @@ -26,7 +26,6 @@ import java.time.Duration; import org.cactoos.Func; import org.cactoos.Scalar; -import org.cactoos.func.Retry; /** * Func that will try a few times before throwing an exception. diff --git a/src/test/java/org/cactoos/func/RetryTest.java b/src/test/java/org/cactoos/func/RetryTest.java index f6dfd020ad..a301be86f5 100644 --- a/src/test/java/org/cactoos/func/RetryTest.java +++ b/src/test/java/org/cactoos/func/RetryTest.java @@ -34,6 +34,9 @@ * @since 0.8 * @checkstyle JavadocMethodCheck (500 lines) * @checkstyle MagicNumberCheck (500 line) + * @todo #909:30min add a test to where an interrupt exception is thrown so + * that the catch and error assignment is verified. The exception should be + * thrown when Thread.sleep is called in Retry.java:129 */ @SuppressWarnings("PMD.AvoidDuplicateLiterals") public final class RetryTest { diff --git a/src/test/java/org/cactoos/scalar/RetryTest.java b/src/test/java/org/cactoos/scalar/RetryTest.java index 887d4e335d..6707be681b 100644 --- a/src/test/java/org/cactoos/scalar/RetryTest.java +++ b/src/test/java/org/cactoos/scalar/RetryTest.java @@ -31,6 +31,7 @@ import java.util.List; import java.util.concurrent.atomic.AtomicInteger; import org.hamcrest.Matchers; +import org.hamcrest.core.IsEqual; import org.junit.Test; import org.llorllale.cactoos.matchers.Assertion; import org.llorllale.cactoos.matchers.ScalarHasValue; @@ -67,7 +68,7 @@ public void runsScalarTwiceWithDefaults() throws Exception { final AtomicInteger tries = new AtomicInteger(0); new Assertion<>( "Should run twice with defaults", - () -> new RetryScalar<>( + () -> new Retry<>( () -> { // @checkstyle MagicNumberCheck (1 line) if (tries.getAndIncrement() <= 1) { @@ -76,7 +77,7 @@ public void runsScalarTwiceWithDefaults() throws Exception { return 0; } ).value(), - Matchers.equalTo(0) + new IsEqual<>(0) ).affirm(); } @@ -88,7 +89,7 @@ public void runsScalarMultipleTimesIgnoringNegativeDuration() final AtomicInteger tries = new AtomicInteger(0); new Assertion<>( "Should ignore negative duration", - () -> new RetryScalar<>( + () -> new Retry<>( () -> { if (tries.getAndIncrement() < times) { throw new IllegalArgumentException("Not yet"); @@ -99,7 +100,7 @@ public void runsScalarMultipleTimesIgnoringNegativeDuration() // @checkstyle MagicNumberCheck (1 line) Duration.of(-5, ChronoUnit.DAYS) ).value(), - Matchers.equalTo(0) + new IsEqual<>(0) ).affirm(); } @@ -111,7 +112,7 @@ public void runsScalarMultipleTimesWithWait() throws Exception { final long wait = 500; final AtomicInteger tries = new AtomicInteger(0); final List executions = new ArrayList<>(times); - new RetryScalar<>( + new Retry<>( () -> { if (tries.getAndIncrement() < times) { executions.add(Instant.now());