Skip to content

Commit

Permalink
Add constructor make timeout customisable
Browse files Browse the repository at this point in the history
  • Loading branch information
aahlenst committed May 23, 2023
1 parent e6091f7 commit 5aff957
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.springframework.retry.RetryContext;
import org.springframework.retry.RetryPolicy;
import org.springframework.retry.context.RetryContextSupport;
import org.springframework.util.Assert;

/**
* A {@link RetryPolicy} that allows a retry only if it hasn't timed out. The clock is
Expand All @@ -37,6 +38,21 @@ public class TimeoutRetryPolicy implements RetryPolicy {

private long timeout = DEFAULT_TIMEOUT;

/**
* Creates a new instance with the timeout set to {@link #DEFAULT_TIMEOUT}.
*/
public TimeoutRetryPolicy() {
this(DEFAULT_TIMEOUT);
}

/**
* Creates a new instance with a configurable timeout.
* @param timeout timeout in milliseconds
*/
public TimeoutRetryPolicy(long timeout) {
this.timeout = timeout;
}

/**
* Setter for timeout in milliseconds. Default is {@link #DEFAULT_TIMEOUT}.
* @param timeout how long to wait until a timeout
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.springframework.retry.RetryContext;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

public class TimeoutRetryPolicyTests {

Expand Down Expand Up @@ -57,4 +58,15 @@ public void testParent() {
assertThat(child.getParent()).isSameAs(context);
}

@Test
public void testConstructorWithCustomTimeout() throws Exception {
TimeoutRetryPolicy policy = new TimeoutRetryPolicy(100);
RetryContext context = policy.open(null);
policy.registerThrowable(context, new Exception());
assertThat(policy.canRetry(context)).isTrue();
Thread.sleep(200);
assertThat(policy.canRetry(context)).isFalse();
policy.close(context);
}

}

0 comments on commit 5aff957

Please sign in to comment.