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

Commit

Permalink
[#3] Making sure abortIf/retryIf are working as desired
Browse files Browse the repository at this point in the history
  • Loading branch information
nurkiewicz committed Jan 5, 2014
1 parent 4568f22 commit 3ab1090
Showing 1 changed file with 106 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public class RetryPolicyPredicatesTest extends AbstractRetryPolicyTest {
private RetryContext retryContextMock;

@Test
public void shouldAbortIfPredicateTrue() throws Exception {
public void shouldAbortIfAbortPredicateTrue() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().abortIf(t -> true);

Expand All @@ -29,6 +29,95 @@ public void shouldAbortIfPredicateTrue() throws Exception {
assertThat(shouldRetry).isFalse();
}

@Test
public void shouldRetryIfRetryPredicateTrue() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().retryIf(t -> true);

//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);

//then
assertThat(shouldRetry).isTrue();
}

@Test
public void shouldRetryIfBothPredicatesAbstainButClassShouldRetry() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().
retryIf(t -> false).
abortIf(t -> false);
given(retryContextMock.getLastThrowable()).willReturn(new RuntimeException());

//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);

//then
assertThat(shouldRetry).isTrue();
}

@Test
public void shouldAbortIfBothPredicatesAbstainButClassShouldAbort() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().
abortOn(NullPointerException.class).
retryIf(t -> false).
abortIf(t -> false);
given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException());

//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);

//then
assertThat(shouldRetry).isFalse();
}

@Test
public void shouldRetryIfPredicateTrueEvenIfClassShouldAbort() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().
abortOn(NullPointerException.class).
retryIf(t -> true);
given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException());

//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);

//then
assertThat(shouldRetry).isTrue();
}

@Test
public void shouldAbortIfPredicateTrueEvenIfClassShouldRetry() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().
retryOn(NullPointerException.class).
abortIf(t -> true);
given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException());

//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);

//then
assertThat(shouldRetry).isFalse();
}

@Test
public void whenAbortAndRetryPredicatesBothYieldTrueThenAbortWins() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().
retryOn(NullPointerException.class).
retryIf(t -> t.getMessage().contains("Foo")).
abortIf(t -> t.getMessage().contains("Foo"));
given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException("Foo"));

//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);

//then
assertThat(shouldRetry).isFalse();
}

@Test
public void shouldProceedIfPredicateFalseAndChildAccepts() throws Exception {
//given
Expand All @@ -54,6 +143,22 @@ public void shouldAbortIfPredicateFalseButShouldNotRetry() throws Exception {
assertThat(shouldRetry).isFalse();
}

@Test
public void shouldAbortIfPredicateTrueButShouldNotRetry() throws Exception {
//given
final RetryPolicy retryPolicy = new RetryPolicy().
retryIf(t -> true).
dontRetry();
given(retryContextMock.getLastThrowable()).willReturn(new NullPointerException());
given(retryContextMock.getRetryCount()).willReturn(1);

//when
final boolean shouldRetry = retryPolicy.shouldContinue(retryContextMock);

//then
assertThat(shouldRetry).isFalse();
}

@Test
public void shouldExamineExceptionAndDecide() throws Exception {
//given
Expand Down

0 comments on commit 3ab1090

Please # to comment.