From 06943519f841b3cb0fb3ef07348921e7528d0a02 Mon Sep 17 00:00:00 2001 From: "emanuel.trandafir" Date: Sat, 14 Oct 2023 13:06:31 +0200 Subject: [PATCH 1/4] BAEL-7001: retry RetryCompletableFuture --- .../retry/RetryCompletableFuture.java | 63 +++++++++ .../retry/RetryCompletableFutureUnitTest.java | 125 ++++++++++++++++++ 2 files changed, 188 insertions(+) create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java create mode 100644 core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java new file mode 100644 index 0000000000..41f1309311 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java @@ -0,0 +1,63 @@ +package com.baeldung.concurrent.completablefuture.retry; + +import java.util.concurrent.CompletableFuture; +import java.util.function.Function; +import java.util.function.Supplier; + +public class RetryCompletableFuture { + public static CompletableFuture retryTask(Supplier supplier, int maxRetries) { + Supplier retryableSupplier = retryFunction(supplier, maxRetries); + return CompletableFuture.supplyAsync(retryableSupplier); + } + + static Supplier retryFunction(Supplier supplier, int maxRetries) { + return () -> { + int retries = 0; + while (retries < maxRetries) { + try { + return supplier.get(); + } catch (Exception e) { + retries++; + } + } + throw new IllegalStateException(String.format("Task failed after %s attempts", maxRetries)); + }; + } + + public static CompletableFuture retryUnsafe(Supplier supplier, int maxRetries) { + CompletableFuture cf = CompletableFuture.supplyAsync(supplier); + sleep(100l); + for (int i = 0; i < maxRetries; i++) { + cf = cf.exceptionally(__ -> supplier.get()); + } + return cf; + } + + public static CompletableFuture retryNesting(Supplier supplier, int maxRetries) { + CompletableFuture cf = CompletableFuture.supplyAsync(supplier); + sleep(100); + for (int i = 0; i < maxRetries; i++) { + cf = cf.thenApply(CompletableFuture::completedFuture) + .exceptionally(__ -> CompletableFuture.supplyAsync(supplier)) + .thenCompose(Function.identity()); + } + return cf; + } + + public static CompletableFuture retryExceptionallyAsync(Supplier supplier, int maxRetries) { + CompletableFuture cf = CompletableFuture.supplyAsync(supplier); + sleep(100); + for (int i = 0; i < maxRetries; i++) { + cf = cf.exceptionallyAsync(__ -> supplier.get()); + } + return cf; + } + + private static void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } +} diff --git a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java new file mode 100644 index 0000000000..b48039d4a6 --- /dev/null +++ b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java @@ -0,0 +1,125 @@ +package com.baeldung.concurrent.completablefuture.retry; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionException; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Supplier; + +import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.*; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class RetryCompletableFutureUnitTest { + private AtomicInteger retriesCounter = new AtomicInteger(0); + + @BeforeEach + void beforeEach() { + retriesCounter.set(0); + } + + @Test + void whenRetryingTask_thenReturnsCorrectlyAfterFourInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); + + CompletableFuture result = retryTask(codeToRun, 10); + + assertThat(result.join()) + .isEqualTo(100); + assertThat(retriesCounter) + .hasValue(4); + } + + @Test + void whenRetryingTask_thenThrowsExceptionAfterThreeInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); + + CompletableFuture result = retryTask(codeToRun, 3); + + assertThatThrownBy(result::join) + .isInstanceOf(CompletionException.class) + .hasMessageContaining("IllegalStateException: Task failed after 3 attempts"); + } + + @Test + void whenRetryingExceptionally_thenReturnsCorrectlyAfterFourInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); + + CompletableFuture result = retryUnsafe(codeToRun, 10); + + assertThat(result.join()) + .isEqualTo(100); + assertThat(retriesCounter) + .hasValue(4); + } + + @Test + void whenRetryingExceptionally_thenThrowsExceptionAfterThreeInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); + + CompletableFuture result = retryUnsafe(codeToRun, 3); + + assertThatThrownBy(result::join) + .isInstanceOf(CompletionException.class) + .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); + } + + @Test + void whenRetryingExceptionallyAsync_thenReturnsCorrectlyAfterFourInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); + + CompletableFuture result = retryExceptionallyAsync(codeToRun, 10); + + assertThat(result.join()) + .isEqualTo(100); + assertThat(retriesCounter) + .hasValue(4); + } + + @Test + void whenRetryingExceptionallyAsync_thenThrowsExceptionAfterThreeInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); + + CompletableFuture result = retryExceptionallyAsync(codeToRun, 3); + + assertThatThrownBy(result::join) + .isInstanceOf(CompletionException.class) + .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); + } + + @Test + void whenRetryingNesting_thenReturnsCorrectlyAfterFourInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); + + CompletableFuture result = retryNesting(codeToRun, 10); + + assertThat(result.join()) + .isEqualTo(100); + assertThat(retriesCounter) + .hasValue(4); + } + + @Test + void whenRetryingNesting_thenThrowsExceptionAfterThreeInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); + + CompletableFuture result = retryNesting(codeToRun, 3); + + assertThatThrownBy(result::join) + .isInstanceOf(CompletionException.class) + .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); + } + + int failFourTimesThenReturn(int returnValue) { + int retryNr = retriesCounter.get(); + System.out.println(String.format("invocation: %s, thread: %s", retryNr, Thread.currentThread().getName())); + if (retryNr < 4) { + retriesCounter.set(retryNr + 1); + throw new RuntimeException(String.format("task failed for %s time(s)", retryNr)); + } + return returnValue; + } + +} From 210eaf2c0b77fb1bd366cbd2040d5ac0376d81de Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Mon, 23 Oct 2023 18:07:43 +0200 Subject: [PATCH 2/4] BAEL-7001: fixed formatting --- .../retry/RetryCompletableFuture.java | 100 +++++----- .../retry/RetryCompletableFutureUnitTest.java | 176 +++++++++--------- 2 files changed, 139 insertions(+), 137 deletions(-) diff --git a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java index 41f1309311..a3df6b3624 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/main/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFuture.java @@ -5,59 +5,59 @@ import java.util.function.Function; import java.util.function.Supplier; public class RetryCompletableFuture { - public static CompletableFuture retryTask(Supplier supplier, int maxRetries) { - Supplier retryableSupplier = retryFunction(supplier, maxRetries); - return CompletableFuture.supplyAsync(retryableSupplier); - } + public static CompletableFuture retryTask(Supplier supplier, int maxRetries) { + Supplier retryableSupplier = retryFunction(supplier, maxRetries); + return CompletableFuture.supplyAsync(retryableSupplier); + } - static Supplier retryFunction(Supplier supplier, int maxRetries) { - return () -> { - int retries = 0; - while (retries < maxRetries) { - try { - return supplier.get(); - } catch (Exception e) { - retries++; - } - } - throw new IllegalStateException(String.format("Task failed after %s attempts", maxRetries)); - }; - } + static Supplier retryFunction(Supplier supplier, int maxRetries) { + return () -> { + int retries = 0; + while (retries < maxRetries) { + try { + return supplier.get(); + } catch (Exception e) { + retries++; + } + } + throw new IllegalStateException(String.format("Task failed after %s attempts", maxRetries)); + }; + } - public static CompletableFuture retryUnsafe(Supplier supplier, int maxRetries) { - CompletableFuture cf = CompletableFuture.supplyAsync(supplier); - sleep(100l); - for (int i = 0; i < maxRetries; i++) { - cf = cf.exceptionally(__ -> supplier.get()); - } - return cf; - } + public static CompletableFuture retryUnsafe(Supplier supplier, int maxRetries) { + CompletableFuture cf = CompletableFuture.supplyAsync(supplier); + sleep(100l); + for (int i = 0; i < maxRetries; i++) { + cf = cf.exceptionally(__ -> supplier.get()); + } + return cf; + } - public static CompletableFuture retryNesting(Supplier supplier, int maxRetries) { - CompletableFuture cf = CompletableFuture.supplyAsync(supplier); - sleep(100); - for (int i = 0; i < maxRetries; i++) { - cf = cf.thenApply(CompletableFuture::completedFuture) - .exceptionally(__ -> CompletableFuture.supplyAsync(supplier)) - .thenCompose(Function.identity()); - } - return cf; - } + public static CompletableFuture retryNesting(Supplier supplier, int maxRetries) { + CompletableFuture cf = CompletableFuture.supplyAsync(supplier); + sleep(100); + for (int i = 0; i < maxRetries; i++) { + cf = cf.thenApply(CompletableFuture::completedFuture) + .exceptionally(__ -> CompletableFuture.supplyAsync(supplier)) + .thenCompose(Function.identity()); + } + return cf; + } - public static CompletableFuture retryExceptionallyAsync(Supplier supplier, int maxRetries) { - CompletableFuture cf = CompletableFuture.supplyAsync(supplier); - sleep(100); - for (int i = 0; i < maxRetries; i++) { - cf = cf.exceptionallyAsync(__ -> supplier.get()); - } - return cf; - } + public static CompletableFuture retryExceptionallyAsync(Supplier supplier, int maxRetries) { + CompletableFuture cf = CompletableFuture.supplyAsync(supplier); + sleep(100); + for (int i = 0; i < maxRetries; i++) { + cf = cf.exceptionallyAsync(__ -> supplier.get()); + } + return cf; + } - private static void sleep(long millis) { - try { - Thread.sleep(millis); - } catch (InterruptedException e) { - throw new RuntimeException(e); - } - } + private static void sleep(long millis) { + try { + Thread.sleep(millis); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } } diff --git a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java index b48039d4a6..ce847fe751 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java @@ -1,125 +1,127 @@ package com.baeldung.concurrent.completablefuture.retry; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; +import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.retryExceptionallyAsync; +import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.retryNesting; +import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.retryTask; +import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.retryUnsafe; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import java.util.concurrent.CompletableFuture; import java.util.concurrent.CompletionException; import java.util.concurrent.atomic.AtomicInteger; import java.util.function.Supplier; -import static com.baeldung.concurrent.completablefuture.retry.RetryCompletableFuture.*; -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.assertThatThrownBy; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; class RetryCompletableFutureUnitTest { - private AtomicInteger retriesCounter = new AtomicInteger(0); + private AtomicInteger retriesCounter = new AtomicInteger(0); - @BeforeEach - void beforeEach() { - retriesCounter.set(0); - } + @BeforeEach + void beforeEach() { + retriesCounter.set(0); + } - @Test - void whenRetryingTask_thenReturnsCorrectlyAfterFourInvocations() { - Supplier codeToRun = () -> failFourTimesThenReturn(100); + @Test + void whenRetryingTask_thenReturnsCorrectlyAfterFourInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); - CompletableFuture result = retryTask(codeToRun, 10); + CompletableFuture result = retryTask(codeToRun, 10); - assertThat(result.join()) - .isEqualTo(100); - assertThat(retriesCounter) - .hasValue(4); - } + assertThat(result.join()) + .isEqualTo(100); + assertThat(retriesCounter) + .hasValue(4); + } - @Test - void whenRetryingTask_thenThrowsExceptionAfterThreeInvocations() { - Supplier codeToRun = () -> failFourTimesThenReturn(100); + @Test + void whenRetryingTask_thenThrowsExceptionAfterThreeInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); - CompletableFuture result = retryTask(codeToRun, 3); + CompletableFuture result = retryTask(codeToRun, 3); - assertThatThrownBy(result::join) - .isInstanceOf(CompletionException.class) - .hasMessageContaining("IllegalStateException: Task failed after 3 attempts"); - } + assertThatThrownBy(result::join).isInstanceOf(CompletionException.class) + .hasMessageContaining("IllegalStateException: Task failed after 3 attempts"); + } - @Test - void whenRetryingExceptionally_thenReturnsCorrectlyAfterFourInvocations() { - Supplier codeToRun = () -> failFourTimesThenReturn(100); + @Test + void whenRetryingExceptionally_thenReturnsCorrectlyAfterFourInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); - CompletableFuture result = retryUnsafe(codeToRun, 10); + CompletableFuture result = retryUnsafe(codeToRun, 10); - assertThat(result.join()) - .isEqualTo(100); - assertThat(retriesCounter) - .hasValue(4); - } + assertThat(result.join()) + .isEqualTo(100); + assertThat(retriesCounter) + .hasValue(4); + } - @Test - void whenRetryingExceptionally_thenThrowsExceptionAfterThreeInvocations() { - Supplier codeToRun = () -> failFourTimesThenReturn(100); + @Test + void whenRetryingExceptionally_thenThrowsExceptionAfterThreeInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); - CompletableFuture result = retryUnsafe(codeToRun, 3); + CompletableFuture result = retryUnsafe(codeToRun, 3); - assertThatThrownBy(result::join) - .isInstanceOf(CompletionException.class) - .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); - } + assertThatThrownBy(result::join) + .isInstanceOf(CompletionException.class) + .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); + } - @Test - void whenRetryingExceptionallyAsync_thenReturnsCorrectlyAfterFourInvocations() { - Supplier codeToRun = () -> failFourTimesThenReturn(100); + @Test + void whenRetryingExceptionallyAsync_thenReturnsCorrectlyAfterFourInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); - CompletableFuture result = retryExceptionallyAsync(codeToRun, 10); + CompletableFuture result = retryExceptionallyAsync(codeToRun, 10); - assertThat(result.join()) - .isEqualTo(100); - assertThat(retriesCounter) - .hasValue(4); - } + assertThat(result.join()) + .isEqualTo(100); + assertThat(retriesCounter) + .hasValue(4); + } - @Test - void whenRetryingExceptionallyAsync_thenThrowsExceptionAfterThreeInvocations() { - Supplier codeToRun = () -> failFourTimesThenReturn(100); + @Test + void whenRetryingExceptionallyAsync_thenThrowsExceptionAfterThreeInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); - CompletableFuture result = retryExceptionallyAsync(codeToRun, 3); + CompletableFuture result = retryExceptionallyAsync(codeToRun, 3); - assertThatThrownBy(result::join) - .isInstanceOf(CompletionException.class) - .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); - } + assertThatThrownBy(result::join) + .isInstanceOf(CompletionException.class) + .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); + } - @Test - void whenRetryingNesting_thenReturnsCorrectlyAfterFourInvocations() { - Supplier codeToRun = () -> failFourTimesThenReturn(100); + @Test + void whenRetryingNesting_thenReturnsCorrectlyAfterFourInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); - CompletableFuture result = retryNesting(codeToRun, 10); + CompletableFuture result = retryNesting(codeToRun, 10); - assertThat(result.join()) - .isEqualTo(100); - assertThat(retriesCounter) - .hasValue(4); - } + assertThat(result.join()) + .isEqualTo(100); + assertThat(retriesCounter) + .hasValue(4); + } - @Test - void whenRetryingNesting_thenThrowsExceptionAfterThreeInvocations() { - Supplier codeToRun = () -> failFourTimesThenReturn(100); + @Test + void whenRetryingNesting_thenThrowsExceptionAfterThreeInvocations() { + Supplier codeToRun = () -> failFourTimesThenReturn(100); - CompletableFuture result = retryNesting(codeToRun, 3); + CompletableFuture result = retryNesting(codeToRun, 3); - assertThatThrownBy(result::join) - .isInstanceOf(CompletionException.class) - .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); - } + assertThatThrownBy(result::join) + .isInstanceOf(CompletionException.class) + .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); + } - int failFourTimesThenReturn(int returnValue) { - int retryNr = retriesCounter.get(); - System.out.println(String.format("invocation: %s, thread: %s", retryNr, Thread.currentThread().getName())); - if (retryNr < 4) { - retriesCounter.set(retryNr + 1); - throw new RuntimeException(String.format("task failed for %s time(s)", retryNr)); - } - return returnValue; - } + int failFourTimesThenReturn(int returnValue) { + int retryNr = retriesCounter.get(); + System.out.println(String.format("invocation: %s, thread: %s", retryNr, Thread.currentThread().getName())); + if (retryNr < 4) { + retriesCounter.set(retryNr + 1); + throw new RuntimeException(String.format("task failed for %s time(s)", retryNr)); + } + return returnValue; + } } From 5b4fc0d6f3753e8855d44da343dfba749118d123 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Mon, 23 Oct 2023 18:13:35 +0200 Subject: [PATCH 3/4] BAEL-7001: small fix --- .../retry/RetryCompletableFutureUnitTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java index ce847fe751..7f1ea41f92 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java @@ -41,7 +41,8 @@ class RetryCompletableFutureUnitTest { CompletableFuture result = retryTask(codeToRun, 3); - assertThatThrownBy(result::join).isInstanceOf(CompletionException.class) + assertThatThrownBy(result::join) + .isInstanceOf(CompletionException.class) .hasMessageContaining("IllegalStateException: Task failed after 3 attempts"); } From f5bb9479b455c4a2e4faebeeabeb95bcc7244690 Mon Sep 17 00:00:00 2001 From: emanueltrandafir1993 Date: Wed, 25 Oct 2023 19:53:50 +0200 Subject: [PATCH 4/4] MKT-7001: formatting --- .../retry/RetryCompletableFutureUnitTest.java | 32 +++++++------------ 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java index 7f1ea41f92..ea49f0fa08 100644 --- a/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java +++ b/core-java-modules/core-java-concurrency-basic-3/src/test/java/com/baeldung/concurrent/completablefuture/retry/RetryCompletableFutureUnitTest.java @@ -29,10 +29,8 @@ class RetryCompletableFutureUnitTest { CompletableFuture result = retryTask(codeToRun, 10); - assertThat(result.join()) - .isEqualTo(100); - assertThat(retriesCounter) - .hasValue(4); + assertThat(result.join()).isEqualTo(100); + assertThat(retriesCounter).hasValue(4); } @Test @@ -52,10 +50,8 @@ class RetryCompletableFutureUnitTest { CompletableFuture result = retryUnsafe(codeToRun, 10); - assertThat(result.join()) - .isEqualTo(100); - assertThat(retriesCounter) - .hasValue(4); + assertThat(result.join()).isEqualTo(100); + assertThat(retriesCounter).hasValue(4); } @Test @@ -65,8 +61,8 @@ class RetryCompletableFutureUnitTest { CompletableFuture result = retryUnsafe(codeToRun, 3); assertThatThrownBy(result::join) - .isInstanceOf(CompletionException.class) - .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); + .isInstanceOf(CompletionException.class) + .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); } @Test @@ -75,10 +71,8 @@ class RetryCompletableFutureUnitTest { CompletableFuture result = retryExceptionallyAsync(codeToRun, 10); - assertThat(result.join()) - .isEqualTo(100); - assertThat(retriesCounter) - .hasValue(4); + assertThat(result.join()).isEqualTo(100); + assertThat(retriesCounter).hasValue(4); } @Test @@ -88,7 +82,7 @@ class RetryCompletableFutureUnitTest { CompletableFuture result = retryExceptionallyAsync(codeToRun, 3); assertThatThrownBy(result::join) - .isInstanceOf(CompletionException.class) + .isInstanceOf(CompletionException.class) .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); } @@ -98,10 +92,8 @@ class RetryCompletableFutureUnitTest { CompletableFuture result = retryNesting(codeToRun, 10); - assertThat(result.join()) - .isEqualTo(100); - assertThat(retriesCounter) - .hasValue(4); + assertThat(result.join()).isEqualTo(100); + assertThat(retriesCounter).hasValue(4); } @Test @@ -111,7 +103,7 @@ class RetryCompletableFutureUnitTest { CompletableFuture result = retryNesting(codeToRun, 3); assertThatThrownBy(result::join) - .isInstanceOf(CompletionException.class) + .isInstanceOf(CompletionException.class) .hasMessageContaining("RuntimeException: task failed for 3 time(s)"); }