-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: Add RetryOnExceptionsExtension for test execution exception ha…
…ndling
- Loading branch information
Showing
3 changed files
with
77 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
16 changes: 16 additions & 0 deletions
16
backend/src/test/java/ai/dragon/test/junit/extension/retry/RetryOnExceptions.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package ai.dragon.test.junit.extension.retry; | ||
|
||
import org.junit.jupiter.api.extension.ExtendWith; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target(ElementType.METHOD) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@ExtendWith(RetryOnExceptionsExtension.class) | ||
public @interface RetryOnExceptions { | ||
int value() default 3; // Default retry count | ||
Class<? extends Throwable>[] onExceptions() default { Throwable.class }; // Exceptions to retry on | ||
} |
51 changes: 51 additions & 0 deletions
51
backend/src/test/java/ai/dragon/test/junit/extension/retry/RetryOnExceptionsExtension.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package ai.dragon.test.junit.extension.retry; | ||
|
||
import java.lang.reflect.Method; | ||
|
||
import org.junit.jupiter.api.extension.BeforeEachCallback; | ||
import org.junit.jupiter.api.extension.ExtensionContext; | ||
import org.junit.jupiter.api.extension.ExtensionContext.Store; | ||
import org.junit.jupiter.api.extension.TestExecutionExceptionHandler; | ||
|
||
public class RetryOnExceptionsExtension implements TestExecutionExceptionHandler, BeforeEachCallback { | ||
|
||
@Override | ||
public void handleTestExecutionException(ExtensionContext context, Throwable throwable) throws Throwable { | ||
Method testMethod = context.getRequiredTestMethod(); | ||
RetryOnExceptions retry = testMethod.getAnnotation(RetryOnExceptions.class); | ||
|
||
if (retry != null) { | ||
int maxRetries = retry.value(); | ||
Class<? extends Throwable>[] retryOnExceptions = retry.onExceptions(); | ||
Store store = context.getStore(ExtensionContext.Namespace.create(testMethod)); | ||
|
||
int currentRetries = store.getOrDefault("retries", Integer.class, 0); | ||
|
||
boolean shouldRetry = false; | ||
for (Class<? extends Throwable> retryOnException : retryOnExceptions) { | ||
if (retryOnException.isInstance(throwable)) { | ||
shouldRetry = true; | ||
break; | ||
} | ||
} | ||
|
||
if (shouldRetry && currentRetries < maxRetries) { | ||
store.put("retries", currentRetries + 1); | ||
context.getRequiredTestMethod().invoke(context.getRequiredTestInstance()); | ||
} else { | ||
throw throwable; | ||
} | ||
} else { | ||
throw throwable; | ||
} | ||
} | ||
|
||
@Override | ||
public void beforeEach(ExtensionContext context) throws Exception { | ||
Method testMethod = context.getRequiredTestMethod(); | ||
if (testMethod.isAnnotationPresent(RetryOnExceptions.class)) { | ||
Store store = context.getStore(ExtensionContext.Namespace.create(testMethod)); | ||
store.put("retries", 0); // Reset retry count before each test | ||
} | ||
} | ||
} |