@@ -5,6 +5,7 @@ import org.junit.jupiter.api.extension.ExtensionContext
55import org.junit.jupiter.api.extension.InvocationInterceptor
66import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation
77import org.junit.jupiter.api.extension.ReflectiveInvocationContext
8+ import org.opentest4j.TestAbortedException
89import java.lang.annotation.Inherited
910import java.lang.reflect.AnnotatedElement
1011import java.lang.reflect.Method
@@ -46,39 +47,42 @@ class RetryExtension : InvocationInterceptor {
4647 return
4748 }
4849
49- var attempt = 1
50- var lastError: Throwable ? = null
5150 val maxAttempts = retry.times
5251 val delay = retry.delayMs
52+ var lastError: Throwable ? = null
5353
54- while (attempt <= maxAttempts) {
55- try {
56- if (attempt > 1 ) {
57- println (" [Retry] Attempt $attempt /$maxAttempts for ${describeTest(extensionContext)} " )
54+ for (attempt in 1 .. maxAttempts) {
55+ if (attempt > 1 && delay > 0 ) {
56+ try {
57+ Thread .sleep(delay)
58+ } catch (_: InterruptedException ) {
59+ Thread .currentThread().interrupt()
60+ break
5861 }
59- invocation.proceed()
60- if (attempt > 1 ) {
61- println (" [Retry] Succeeded on attempt $attempt for ${describeTest(extensionContext)} " )
62+ }
63+
64+ try {
65+ if (attempt == 1 ) {
66+ invocation.proceed()
67+ } else {
68+ val instance = extensionContext.requiredTestInstance
69+ val testMethod = extensionContext.requiredTestMethod
70+ testMethod.isAccessible = true
71+ testMethod.invoke(instance)
6272 }
6373 return
6474 } catch (t: Throwable ) {
65- lastError = t
66- if (attempt >= maxAttempts) {
67- println (" [Retry] Giving up after $attempt attempts for ${describeTest(extensionContext)} : ${t.message} " )
68- throw t
69- } else {
70- println (" [Retry] Failure on attempt $attempt for ${describeTest(extensionContext)} : ${t.message} " )
71- if (delay > 0 ) {
72- try {
73- Thread .sleep(delay)
74- } catch (_: InterruptedException ) {
75- }
76- }
75+ if (t is TestAbortedException ) throw t
76+ lastError = if (t is java.lang.reflect.InvocationTargetException ) t.targetException ? : t else t
77+ if (attempt == maxAttempts) {
78+ println (" [Retry] Giving up after $attempt attempts for ${describeTest(extensionContext)} : ${lastError.message} " )
79+ throw lastError
7780 }
81+ println (" [Retry] Failure on attempt $attempt /${maxAttempts} for ${describeTest(extensionContext)} : ${lastError.message} " )
7882 }
79- attempt++
8083 }
81- lastError?.let { throw it }
84+
85+ throw lastError ? : IllegalStateException (" Unexpected state in retry logic" )
8286 }
8387
8488 private fun describeTest (ctx : ExtensionContext ): String {
0 commit comments