Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit acc32d1

Browse files
committed
added more tests
1 parent 668f83b commit acc32d1

File tree

1 file changed

+59
-9
lines changed

1 file changed

+59
-9
lines changed

src/test/java/com/evanlennick/retry4j/CallExecutorTest_RetryOnCustomLogicTest.java

Lines changed: 59 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import com.evanlennick.retry4j.config.RetryConfigBuilder;
55
import com.evanlennick.retry4j.exception.RetriesExhaustedException;
66
import com.evanlennick.retry4j.exception.UnexpectedException;
7-
import org.junit.BeforeClass;
87
import org.junit.Test;
98

109
import java.time.Duration;
@@ -14,24 +13,54 @@
1413

1514
public class CallExecutorTest_RetryOnCustomLogicTest {
1615

17-
private static RetryConfig config;
16+
@Test
17+
public void verifyShouldRetryOnExceptionMessage() {
18+
RetryConfig config = new RetryConfigBuilder()
19+
.retryOnCustomExceptionLogic(ex -> ex.getMessage().contains("should retry!"))
20+
.withFixedBackoff()
21+
.withDelayBetweenTries(Duration.ofMillis(1))
22+
.withMaxNumberOfTries(3)
23+
.build();
1824

19-
@BeforeClass
20-
public static void setup() {
21-
config = new RetryConfigBuilder()
25+
try {
26+
new CallExecutor<>(config)
27+
.execute(() -> {
28+
throw new RuntimeException("should retry!");
29+
});
30+
fail();
31+
} catch (RetriesExhaustedException e) {
32+
assertThat(e.getStatus().getTotalTries()).isEqualTo(3);
33+
}
34+
}
35+
36+
@Test(expected = UnexpectedException.class)
37+
public void verifyShouldNotRetryOnExceptionMessage() {
38+
RetryConfig config = new RetryConfigBuilder()
2239
.retryOnCustomExceptionLogic(ex -> ex.getMessage().contains("should retry!"))
2340
.withFixedBackoff()
2441
.withDelayBetweenTries(Duration.ofMillis(1))
2542
.withMaxNumberOfTries(3)
2643
.build();
44+
45+
new CallExecutor<>(config)
46+
.execute(() -> {
47+
throw new RuntimeException("should NOT retry!");
48+
});
2749
}
2850

2951
@Test
30-
public void verifyShouldRetry() {
52+
public void verifyShouldRetryOnCustomException() {
53+
RetryConfig config = new RetryConfigBuilder()
54+
.retryOnCustomExceptionLogic(ex -> ((CustomTestException) ex).getSomeValue() > 0)
55+
.withFixedBackoff()
56+
.withDelayBetweenTries(Duration.ofMillis(1))
57+
.withMaxNumberOfTries(3)
58+
.build();
59+
3160
try {
3261
new CallExecutor<>(config)
3362
.execute(() -> {
34-
throw new RuntimeException("should retry!");
63+
throw new CustomTestException("should retry!", 100);
3564
});
3665
fail();
3766
} catch (RetriesExhaustedException e) {
@@ -40,10 +69,31 @@ public void verifyShouldRetry() {
4069
}
4170

4271
@Test(expected = UnexpectedException.class)
43-
public void verifyShouldNotRetry() {
72+
public void verifyShouldNotRetryOnCustomException() {
73+
RetryConfig config = new RetryConfigBuilder()
74+
.retryOnCustomExceptionLogic(ex -> ((CustomTestException) ex).getSomeValue() > 0)
75+
.withFixedBackoff()
76+
.withDelayBetweenTries(Duration.ofMillis(1))
77+
.withMaxNumberOfTries(3)
78+
.build();
79+
4480
new CallExecutor<>(config)
4581
.execute(() -> {
46-
throw new RuntimeException("should NOT retry!");
82+
throw new CustomTestException("test message", -100);
4783
});
4884
}
85+
86+
private class CustomTestException extends RuntimeException {
87+
88+
private int someValue;
89+
90+
public CustomTestException(String message, int someValue) {
91+
super(message);
92+
this.someValue = someValue;
93+
}
94+
95+
public int getSomeValue() {
96+
return someValue;
97+
}
98+
}
4999
}

0 commit comments

Comments
 (0)