|
| 1 | +package io.modelcontextprotocol.kotlin.sdk.integration.utils |
| 2 | + |
| 3 | +import org.junit.jupiter.api.extension.ExtendWith |
| 4 | +import org.junit.jupiter.api.extension.ExtensionContext |
| 5 | +import org.junit.jupiter.api.extension.InvocationInterceptor |
| 6 | +import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation |
| 7 | +import org.junit.jupiter.api.extension.ReflectiveInvocationContext |
| 8 | +import java.lang.annotation.Inherited |
| 9 | +import java.lang.reflect.AnnotatedElement |
| 10 | +import java.lang.reflect.Method |
| 11 | +import java.util.* |
| 12 | + |
| 13 | +@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION) |
| 14 | +@Retention(AnnotationRetention.RUNTIME) |
| 15 | +@Inherited |
| 16 | +@ExtendWith(RetryExtension::class) |
| 17 | +annotation class Retry( |
| 18 | + val times: Int = 3, |
| 19 | + val delayMs: Long = 0, |
| 20 | +) |
| 21 | + |
| 22 | +class RetryExtension : InvocationInterceptor { |
| 23 | + override fun interceptTestMethod( |
| 24 | + invocation: Invocation<Void>, |
| 25 | + invocationContext: ReflectiveInvocationContext<Method>, |
| 26 | + extensionContext: ExtensionContext |
| 27 | + ) { |
| 28 | + executeWithRetry(invocation, extensionContext) |
| 29 | + } |
| 30 | + |
| 31 | + private fun resolveRetryAnnotation(extensionContext: ExtensionContext): Retry? { |
| 32 | + val methodAnn = extensionContext.testMethod.flatMap { findRetry(it) } |
| 33 | + if (methodAnn.isPresent) return methodAnn.get() |
| 34 | + val classAnn = extensionContext.testClass.flatMap { findRetry(it) } |
| 35 | + return classAnn.orElse(null) |
| 36 | + } |
| 37 | + |
| 38 | + private fun findRetry(element: AnnotatedElement): Optional<Retry> { |
| 39 | + return Optional.ofNullable(element.getAnnotation(Retry::class.java)) |
| 40 | + } |
| 41 | + |
| 42 | + private fun executeWithRetry(invocation: Invocation<Void>, extensionContext: ExtensionContext) { |
| 43 | + val retry = resolveRetryAnnotation(extensionContext) |
| 44 | + if (retry == null || retry.times <= 1) { |
| 45 | + // No retry requested or only one attempt |
| 46 | + invocation.proceed() |
| 47 | + return |
| 48 | + } |
| 49 | + |
| 50 | + var attempt = 1 |
| 51 | + var lastError: Throwable? = null |
| 52 | + val maxAttempts = retry.times |
| 53 | + val delay = retry.delayMs |
| 54 | + |
| 55 | + while (attempt <= maxAttempts) { |
| 56 | + try { |
| 57 | + if (attempt > 1) { |
| 58 | + println("[RetryExtension] Attempt $attempt/$maxAttempts for ${describeTest(extensionContext)}") |
| 59 | + } |
| 60 | + invocation.proceed() |
| 61 | + if (attempt > 1) { |
| 62 | + println("[RetryExtension] Succeeded on attempt $attempt for ${describeTest(extensionContext)}") |
| 63 | + } |
| 64 | + return |
| 65 | + } catch (t: Throwable) { |
| 66 | + lastError = t |
| 67 | + if (attempt >= maxAttempts) { |
| 68 | + println("[RetryExtension] Giving up after $attempt attempts for ${describeTest(extensionContext)}: ${t.message}") |
| 69 | + throw t |
| 70 | + } else { |
| 71 | + println("[RetryExtension] Failure on attempt $attempt for ${describeTest(extensionContext)}: ${t.message}") |
| 72 | + if (delay > 0) { |
| 73 | + try { |
| 74 | + Thread.sleep(delay) |
| 75 | + } catch (_: InterruptedException) { |
| 76 | + } |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + attempt++ |
| 81 | + } |
| 82 | + // Should not reach here; rethrow last error defensively if it happens |
| 83 | + lastError?.let { throw it } |
| 84 | + } |
| 85 | + |
| 86 | + private fun describeTest(ctx: ExtensionContext): String { |
| 87 | + val methodName = ctx.testMethod.map(Method::getName).orElse("<unknown>") |
| 88 | + val className = ctx.testClass.map { it.name }.orElse("<unknown>") |
| 89 | + return "$className#$methodName" |
| 90 | + } |
| 91 | +} |
0 commit comments