Skip to content

Commit

Permalink
(#909) refactorings according to review II
Browse files Browse the repository at this point in the history
  • Loading branch information
longstone committed Mar 24, 2019
1 parent fdc8ff1 commit fc1cbb2
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 7 deletions.
7 changes: 6 additions & 1 deletion src/main/java/org/cactoos/func/Retry.java
Expand Up @@ -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;
}
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/cactoos/scalar/Retry.java
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/org/cactoos/func/RetryTest.java
Expand Up @@ -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 {
Expand Down
11 changes: 6 additions & 5 deletions src/test/java/org/cactoos/scalar/RetryTest.java
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -76,7 +77,7 @@ public void runsScalarTwiceWithDefaults() throws Exception {
return 0;
}
).value(),
Matchers.equalTo(0)
new IsEqual<>(0)
).affirm();
}

Expand All @@ -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");
Expand All @@ -99,7 +100,7 @@ public void runsScalarMultipleTimesIgnoringNegativeDuration()
// @checkstyle MagicNumberCheck (1 line)
Duration.of(-5, ChronoUnit.DAYS)
).value(),
Matchers.equalTo(0)
new IsEqual<>(0)
).affirm();
}

Expand All @@ -111,7 +112,7 @@ public void runsScalarMultipleTimesWithWait() throws Exception {
final long wait = 500;
final AtomicInteger tries = new AtomicInteger(0);
final List<Instant> executions = new ArrayList<>(times);
new RetryScalar<>(
new Retry<>(
() -> {
if (tries.getAndIncrement() < times) {
executions.add(Instant.now());
Expand Down

1 comment on commit fc1cbb2

@0pdd
Copy link
Collaborator

@0pdd 0pdd commented on fc1cbb2 Mar 29, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Puzzle 909-fb64001d discovered in src/test/java/org/cactoos/func/RetryTest.java and submitted as #1088. Please, remember that the puzzle was not necessarily added in this particular commit. Maybe it was added earlier, but we discovered it only now.

Please sign in to comment.