Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix asynchronous @BeforeRetry #1071

Merged
merged 1 commit into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ private CompletionStage<V> afterDelay(InvocationContext<CompletionStage<V>> ctx,
}
}

if (beforeRetry != null) {
if (beforeRetry != null && attempt > 0) {
try {
beforeRetry.accept(new FailureContext(lastFailure, ctx));
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.smallrye.faulttolerance.async.compstage.retry.beforeretry;

import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.concurrent.CompletableFuture.failedFuture;

import java.io.IOException;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.atomic.AtomicInteger;

import jakarta.enterprise.context.ApplicationScoped;

import org.eclipse.microprofile.faulttolerance.Asynchronous;
import org.eclipse.microprofile.faulttolerance.Fallback;
import org.eclipse.microprofile.faulttolerance.Retry;

import io.smallrye.faulttolerance.api.BeforeRetry;

@ApplicationScoped
public class AsyncHelloService {
static final AtomicInteger COUNTER = new AtomicInteger(0);
static final AtomicInteger BEFORE_RETRY_COUNTER = new AtomicInteger(0);

@Asynchronous
@Retry(maxRetries = 2)
@BeforeRetry(methodName = "beforeRetry")
@Fallback(fallbackMethod = "fallback")
public CompletionStage<String> hello() {
COUNTER.incrementAndGet();
return failedFuture(new IOException("Simulated IO error"));
}

private void beforeRetry() {
BEFORE_RETRY_COUNTER.incrementAndGet();
}

private CompletionStage<String> fallback() {
return completedFuture("Fallback");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.smallrye.faulttolerance.async.compstage.retry.beforeretry;

import static org.assertj.core.api.Assertions.assertThat;

import java.util.concurrent.ExecutionException;

import org.junit.jupiter.api.Test;

import io.smallrye.faulttolerance.util.FaultToleranceBasicTest;

@FaultToleranceBasicTest
public class AsynchronousCompletionStageRetryTest {
@Test
public void testAsyncBeforeRetry(AsyncHelloService helloService) throws InterruptedException, ExecutionException {
assertThat(helloService.hello().toCompletableFuture().get()).isEqualTo("Fallback");
assertThat(AsyncHelloService.COUNTER.get()).isEqualTo(3);
assertThat(AsyncHelloService.BEFORE_RETRY_COUNTER.get()).isEqualTo(2);
}
}