Skip to content
This repository has been archived by the owner on Mar 30, 2019. It is now read-only.

retryIf with a predicate doesn #12

Open
priyo opened this issue Mar 29, 2019 · 0 comments
Open

retryIf with a predicate doesn #12

priyo opened this issue Mar 29, 2019 · 0 comments

Comments

@priyo
Copy link

priyo commented Mar 29, 2019

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();
}

}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant