Skip to content
This repository has been archived by the owner on Nov 5, 2022. It is now read-only.

Commit

Permalink
Make Unreliables stop calling lambda after timeout occurs (#5)
Browse files Browse the repository at this point in the history
* Make Unreliables stop calling lambda after timeout occurs

* Tiny test error message fix.
  • Loading branch information
greenbird-chr authored and rnorth committed Apr 28, 2019
1 parent e55130e commit 18c189b
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
See [Releases](https://github.com/rnorth/duct-tape/releases)
See [Releases](https://github.com/rnorth/duct-tape/releases)
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.rnorth.ducttape.Preconditions.check;

Expand Down Expand Up @@ -34,9 +35,10 @@ public static <T> T retryUntilSuccess(final int timeout, @NotNull final TimeUnit
final int[] attempt = {0};
final Exception[] lastException = {null};

final AtomicBoolean doContinue = new AtomicBoolean(true);
try {
return Timeouts.getWithTimeout(timeout, timeUnit, () -> {
while (true) {
while (doContinue.get()) {
try {
return lambda.call();
} catch (Exception e) {
Expand All @@ -45,13 +47,16 @@ public static <T> T retryUntilSuccess(final int timeout, @NotNull final TimeUnit
lastException[0] = e;
}
}
return null;
});
} catch (org.rnorth.ducttape.TimeoutException e) {
if (lastException[0] != null) {
throw new org.rnorth.ducttape.TimeoutException("Timeout waiting for result with exception", lastException[0]);
} else {
throw new org.rnorth.ducttape.TimeoutException(e);
}
} finally {
doContinue.set(false);
}
}

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/rnorth/ducttape/unreliables/UnreliablesTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import org.rnorth.ducttape.TimeoutException;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.rnorth.visibleassertions.VisibleAssertions.assertEquals;
import static org.rnorth.visibleassertions.VisibleAssertions.assertFalse;
import static org.rnorth.visibleassertions.VisibleAssertions.assertThrows;
import static org.rnorth.visibleassertions.VisibleAssertions.fail;

Expand Down Expand Up @@ -115,6 +117,24 @@ public void testRetryUntilSuccessFailsWhenOutsideTimeoutWindow() throws Exceptio
}
}

@Test
public void testRetryUntilSuccessLambdaStopsBeingCalledAfterTimeout() throws Exception {
AtomicBoolean lambdaCalled = new AtomicBoolean();
try {
Unreliables.retryUntilSuccess(200, TimeUnit.MILLISECONDS, () -> {
lambdaCalled.set(true);
throw new RuntimeException();
});
fail("Expected TimeoutException on retryUntilSuccess that always fails.");
} catch (TimeoutException e) {
// ok
Thread.sleep(200); // give worker thread time to stop calling the lambda
lambdaCalled.set(false);
Thread.sleep(200); // give worker thread time to call the lambda if it is still running
assertFalse("Lambda should stop being executed when retryUntilSuccess times out.", lambdaCalled.get());
}
}

@Test
public void testRetryUntilSuccessFailsWhenOutsideTimeoutWindowAndCapturesException() throws Exception {
try {
Expand Down

0 comments on commit 18c189b

Please sign in to comment.