Skip to content

Commit 31ce9f0

Browse files
committed
fixup! Introduce Kotlin integration tests
Signed-off-by: Sergey Karpov <[email protected]>
1 parent 52511c8 commit 31ce9f0

File tree

1 file changed

+27
-23
lines changed
  • kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/utils

1 file changed

+27
-23
lines changed

kotlin-sdk-test/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/utils/Retry.kt

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import org.junit.jupiter.api.extension.ExtensionContext
55
import org.junit.jupiter.api.extension.InvocationInterceptor
66
import org.junit.jupiter.api.extension.InvocationInterceptor.Invocation
77
import org.junit.jupiter.api.extension.ReflectiveInvocationContext
8+
import org.opentest4j.TestAbortedException
89
import java.lang.annotation.Inherited
910
import java.lang.reflect.AnnotatedElement
1011
import 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

Comments
 (0)