From 48fd97257cd4879e290a9d0f89fb27f7e0d988ac Mon Sep 17 00:00:00 2001 From: Slawomir Jaranowski Date: Wed, 18 Dec 2019 08:23:36 +0100 Subject: [PATCH] Issue #775: Added getter for waitInterval of RetryOnRetryEvent (#778) --- .../retry/event/RetryOnErrorEvent.java | 6 ++- .../retry/event/RetryOnIgnoredErrorEvent.java | 6 ++- .../retry/event/RetryOnRetryEvent.java | 15 ++++-- .../retry/event/RetryOnSuccessEvent.java | 6 ++- .../retry/event/RetryEventTest.java | 46 +++++++++++++++++++ 5 files changed, 70 insertions(+), 9 deletions(-) diff --git a/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnErrorEvent.java b/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnErrorEvent.java index 3a3ca32522..671acbcc28 100644 --- a/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnErrorEvent.java +++ b/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnErrorEvent.java @@ -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); } @@ -40,6 +42,6 @@ public String toString() { getCreationTime(), getName(), getNumberOfRetryAttempts(), - getLastThrowable() != null ? getLastThrowable().toString() : "null"); + getLastThrowable()); } } diff --git a/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnIgnoredErrorEvent.java b/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnIgnoredErrorEvent.java index 6cf788d984..f659f8369e 100644 --- a/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnIgnoredErrorEvent.java +++ b/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnIgnoredErrorEvent.java @@ -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. *

@@ -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); } @@ -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()); } } diff --git a/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnRetryEvent.java b/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnRetryEvent.java index 43c2b174ce..b3aeff66de 100644 --- a/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnRetryEvent.java +++ b/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnRetryEvent.java @@ -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()); } } diff --git a/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnSuccessEvent.java b/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnSuccessEvent.java index 386e0f430e..9491187966 100644 --- a/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnSuccessEvent.java +++ b/resilience4j-retry/src/main/java/io/github/resilience4j/retry/event/RetryOnSuccessEvent.java @@ -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); } @@ -40,6 +42,6 @@ public String toString() { getCreationTime(), getName(), getNumberOfRetryAttempts(), - getLastThrowable() != null ? getLastThrowable().toString() : "null"); + getLastThrowable()); } } diff --git a/resilience4j-retry/src/test/java/io/github/resilience4j/retry/event/RetryEventTest.java b/resilience4j-retry/src/test/java/io/github/resilience4j/retry/event/RetryEventTest.java index 95f49e08b1..90883c0791 100644 --- a/resilience4j-retry/src/test/java/io/github/resilience4j/retry/event/RetryEventTest.java +++ b/resilience4j-retry/src/test/java/io/github/resilience4j/retry/event/RetryEventTest.java @@ -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; @@ -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, @@ -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", @@ -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'."); + } + }