Skip to content

Commit

Permalink
Issue ReactiveX#775: Added getter for waitInterval of RetryOnRetryEve…
Browse files Browse the repository at this point in the history
  • Loading branch information
slawekjaranowski authored and RobWin committed Dec 18, 2019
1 parent 1268dd5 commit 48fd972
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
*/
package io.github.resilience4j.retry.event;

import io.github.resilience4j.core.lang.Nullable;

/**
* A RetryEvent which informs that a call has been retried, but still failed, such that the the
* maximum number of attempts has been reached. It will not be retried any more.
*/
public class RetryOnErrorEvent extends AbstractRetryEvent {

public RetryOnErrorEvent(String name, int numberOfAttempts, Throwable lastThrowable) {
public RetryOnErrorEvent(String name, int numberOfAttempts, @Nullable Throwable lastThrowable) {
super(name, numberOfAttempts, lastThrowable);
}

Expand All @@ -40,6 +42,6 @@ public String toString() {
getCreationTime(),
getName(),
getNumberOfRetryAttempts(),
getLastThrowable() != null ? getLastThrowable().toString() : "null");
getLastThrowable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package io.github.resilience4j.retry.event;

import io.github.resilience4j.core.lang.Nullable;

/**
* A RetryEvent which informs that an error has been ignored. It will not be retried.
* <p>
Expand All @@ -26,7 +28,7 @@
*/
public class RetryOnIgnoredErrorEvent extends AbstractRetryEvent {

public RetryOnIgnoredErrorEvent(String name, Throwable lastThrowable) {
public RetryOnIgnoredErrorEvent(String name, @Nullable Throwable lastThrowable) {
super(name, 0, lastThrowable);
}

Expand All @@ -40,6 +42,6 @@ public String toString() {
return String.format("%s: Retry '%s' recorded an error which has been ignored: '%s'.",
getCreationTime(),
getName(),
getLastThrowable() != null ? getLastThrowable().toString() : "null");
getLastThrowable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,23 @@ public Type getEventType() {
return Type.RETRY;
}

/**
* Returns the interval used to wait before next retry.
*
* @return the wait interval
*/
public Duration getWaitInterval() {
return waitInterval;
}

@Override
public String toString() {
return String.format(
"%s: Retry '%s', waiting %s until attempt #%d. Last attempt failed with exception %s",
"%s: Retry '%s', waiting %s until attempt '%d'. Last attempt failed with exception '%s'.",
getCreationTime(),
getName(),
waitInterval,
getWaitInterval(),
getNumberOfRetryAttempts(),
getLastThrowable() != null ? getLastThrowable().toString() : "null");
getLastThrowable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,15 @@
*/
package io.github.resilience4j.retry.event;

import io.github.resilience4j.core.lang.Nullable;

/**
* A RetryEvent which informs that a call has been retried and a retry was successful. This event is
* not published when a call was successful without a retry attempt.
*/
public class RetryOnSuccessEvent extends AbstractRetryEvent {

public RetryOnSuccessEvent(String name, int currentNumOfAttempts, Throwable lastThrowable) {
public RetryOnSuccessEvent(String name, int currentNumOfAttempts, @Nullable Throwable lastThrowable) {
super(name, currentNumOfAttempts, lastThrowable);
}

Expand All @@ -40,6 +42,6 @@ public String toString() {
getCreationTime(),
getName(),
getNumberOfRetryAttempts(),
getLastThrowable() != null ? getLastThrowable().toString() : "null");
getLastThrowable());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Test;

import java.io.IOException;
import java.time.Duration;

import static io.github.resilience4j.retry.event.RetryEvent.Type;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -39,6 +40,14 @@ public void testRetryOnErrorEvent() {
"Retry 'test' recorded a failed retry attempt. Number of retry attempts: '2'. Giving up. Last exception was: 'java.io.IOException: Bla'.");
}

@Test
public void testRetryOnErrorEventWithNullLastThrowable() {
RetryOnErrorEvent retryOnErrorEvent = new RetryOnErrorEvent("test", 2, null);
assertThat(retryOnErrorEvent.getLastThrowable()).isNull();
assertThat(retryOnErrorEvent.toString()).contains(
"Retry 'test' recorded a failed retry attempt. Number of retry attempts: '2'. Giving up. Last exception was: 'null'.");
}

@Test
public void testRetryOnSuccessEvent() {
RetryOnSuccessEvent retryOnSuccessEvent = new RetryOnSuccessEvent("test", 2,
Expand All @@ -51,6 +60,14 @@ public void testRetryOnSuccessEvent() {
"Retry 'test' recorded a successful retry attempt. Number of retry attempts: '2', Last exception was: 'java.io.IOException: Bla'.");
}

@Test
public void testRetryOnSuccessEventWithNullLastThrowable() {
RetryOnSuccessEvent retryOnSuccessEvent = new RetryOnSuccessEvent("test", 2, null);
assertThat(retryOnSuccessEvent.getLastThrowable()).isNull();
assertThat(retryOnSuccessEvent.toString()).contains(
"Retry 'test' recorded a successful retry attempt. Number of retry attempts: '2', Last exception was: 'null'.");
}

@Test
public void testRetryOnIgnoredErrorEvent() {
RetryOnIgnoredErrorEvent retryOnIgnoredErrorEvent = new RetryOnIgnoredErrorEvent("test",
Expand All @@ -63,4 +80,33 @@ public void testRetryOnIgnoredErrorEvent() {
"Retry 'test' recorded an error which has been ignored: 'java.io.IOException: Bla'.");
}

@Test
public void testRetryOnIgnoredErrorEventWithNullLastThrowable() {
RetryOnIgnoredErrorEvent retryOnIgnoredErrorEvent = new RetryOnIgnoredErrorEvent("test", null);
assertThat(retryOnIgnoredErrorEvent.getLastThrowable()).isNull();
assertThat(retryOnIgnoredErrorEvent.toString()).contains(
"Retry 'test' recorded an error which has been ignored: 'null'.");
}

@Test
public void testRetryOnRetryEvent() {
RetryOnRetryEvent retryOnRetryEvent = new RetryOnRetryEvent("test", 2,
new IOException("Bla"), 1234L);
assertThat(retryOnRetryEvent.getName()).isEqualTo("test");
assertThat(retryOnRetryEvent.getNumberOfRetryAttempts()).isEqualTo(2);
assertThat(retryOnRetryEvent.getEventType()).isEqualTo(Type.RETRY);
assertThat(retryOnRetryEvent.getLastThrowable()).isInstanceOf(IOException.class);
assertThat(retryOnRetryEvent.getWaitInterval()).isEqualTo(Duration.ofMillis(1234L));
assertThat(retryOnRetryEvent.toString()).contains(
"Retry 'test', waiting PT1.234S until attempt '2'. Last attempt failed with exception 'java.io.IOException: Bla'.");
}

@Test
public void testRetryOnRetryEventWithNullLastThrowable() {
RetryOnRetryEvent retryOnRetryEvent = new RetryOnRetryEvent("test", 2, null, 500L);
assertThat(retryOnRetryEvent.getLastThrowable()).isNull();
assertThat(retryOnRetryEvent.toString()).contains(
"Retry 'test', waiting PT0.5S until attempt '2'. Last attempt failed with exception 'null'.");
}

}

0 comments on commit 48fd972

Please sign in to comment.