retryIf with a predicate doesn
Opened this issue · 0 comments
priyo commented
I am trying to retryIf when an exceptions root cause is a particular exception but it always retrying even if there isn't a match. Here is a test case which isn't working for the negative case
import com.nurkiewicz.asyncretry.RetryContext;
import org.mockito.Mock;
import org.testng.annotations.Test;
import java.io.FileNotFoundException;
import java.net.SocketException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
public class RetryIfPredicatesTest extends AbstractRetryPolicyTest {
@Mock
private RetryContext retryContextMock;
private Throwable getRootCause(Throwable t) {
while (t != null) {
Throwable cause = t.getCause();
if (cause != null) {
t = cause;
} else {
break;
}
}
return t;
}
@Test
public void shouldRetryWhenRetryIfIsTrue() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().
retryIf(t -> SocketException.class.equals(getRootCause(t).getClass()));
given(retryContextMock.getLastThrowable()).willReturn(new RuntimeException(new SocketException()));
//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);
//then
assertThat(shouldRetry).isTrue();
}
@Test
public void shouldNotRetryWhenRetryIfIsFalse() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().
retryIf(t -> SocketException.class.equals(getRootCause(t).getClass()));
given(retryContextMock.getLastThrowable()).willReturn(new RuntimeException(new FileNotFoundException()));
//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);
//then
assertThat(shouldRetry).isFalse();
}
}