Skip to content

Commit 40e9693

Browse files
committed
Add constructor make timeout customisable
1 parent e6091f7 commit 40e9693

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

src/main/java/org/springframework/retry/policy/TimeoutRetryPolicy.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,22 @@ public class TimeoutRetryPolicy implements RetryPolicy {
3535
*/
3636
public static final long DEFAULT_TIMEOUT = 1000;
3737

38-
private long timeout = DEFAULT_TIMEOUT;
38+
private long timeout;
39+
40+
/**
41+
* Creates a new instance with the timeout set to {@link #DEFAULT_TIMEOUT}.
42+
*/
43+
public TimeoutRetryPolicy() {
44+
this(DEFAULT_TIMEOUT);
45+
}
46+
47+
/**
48+
* Creates a new instance with a configurable timeout.
49+
* @param timeout timeout in milliseconds
50+
*/
51+
public TimeoutRetryPolicy(long timeout) {
52+
this.timeout = timeout;
53+
}
3954

4055
/**
4156
* Setter for timeout in milliseconds. Default is {@link #DEFAULT_TIMEOUT}.

src/test/java/org/springframework/retry/policy/TimeoutRetryPolicyTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import org.springframework.retry.RetryContext;
2222

2323
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2425

2526
public class TimeoutRetryPolicyTests {
2627

@@ -57,4 +58,15 @@ public void testParent() {
5758
assertThat(child.getParent()).isSameAs(context);
5859
}
5960

61+
@Test
62+
public void testConstructorWithCustomTimeout() throws Exception {
63+
TimeoutRetryPolicy policy = new TimeoutRetryPolicy(100);
64+
RetryContext context = policy.open(null);
65+
policy.registerThrowable(context, new Exception());
66+
assertThat(policy.canRetry(context)).isTrue();
67+
Thread.sleep(200);
68+
assertThat(policy.canRetry(context)).isFalse();
69+
policy.close(context);
70+
}
71+
6072
}

0 commit comments

Comments
 (0)