0
    private CompletionStage<org.asynchttpclient.Response> executeWithRetries(Request request) {
    RetryConfig retryConfig = RetryConfig.<org.asynchttpclient.Response>custom()
            .maxAttempts(5)
            .intervalFunction(IntervalFunction
                    .ofExponentialBackoff(TimeUnit.SECONDS.toMillis(2), 1.2))
            .build();
    Retry retry = Retry.of("proxy-retry" , retryConfig);
    Supplier<CompletionStage<org.asynchttpclient.Response>> retryableSupplier = Retry.decorateCompletionStage(
            retry , Executors.newScheduledThreadPool(10),  () -> executeCall(request));

    return retryableSupplier.get();
}

I am using this method hoping that the executeCall would get retried at least 3 times when it throws an exception. The executeCall(request) method returns a CompletionStage.

When I try to unit test this piece of code , the number of invocations of the executeCall(request) method is just once (I am throwing an exception inside this method).

How do I ensure that it retries atleast 5 times (this is the default value)

lakhansoren
  • 162
  • 1
  • 1
  • 9

1 Answers1

0

Probably you throw exception in the Supplier, not inside the future returned by Supplier.get() code. I've tried the following code:

import java.util.concurrent.*;
import java.util.function.*;
import io.github.resilience4j.retry.*;

public class Main {
    private static final ScheduledExecutorService scheduledExecutorService =
            Executors.newScheduledThreadPool(10);

    public static void main(final String[] args) {
        RetryConfig retryConfig = RetryConfig.custom()
                .maxAttempts(5)
                .intervalFunction(
                        IntervalFunction.ofExponentialBackoff(10, 1.2))
                .build();

        Retry retry = Retry.of("proxy-retry", retryConfig);

        Supplier<CompletionStage<String>> supplier =
                () -> CompletableFuture.supplyAsync(() -> {
                    System.out.println("failing code");
                    throw new RuntimeException();
                });

        retry.executeCompletionStage(scheduledExecutorService, supplier);
    }
}

The output is:

failing code
failing code
failing code
failing code
failing code

as expected!

IgorZep
  • 96
  • 3