From 2163b23d2c5d3ac4a8064acda37523eab5610e8f Mon Sep 17 00:00:00 2001 From: Pavel Gorgulov Date: Thu, 23 Oct 2025 20:06:40 +0200 Subject: [PATCH] Refactor testClientConnectWithInvalidJson (#338) simplification ## Breaking Changes ## Types of changes - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to change) - [ ] Documentation update - [x] Refactoring ## Checklist - [x] I have read the [MCP Documentation](https://modelcontextprotocol.io) - [x] My code follows the repository's style guidelines - [x] New and existing tests pass locally - [x] I have added appropriate error handling - [ ] I have added or updated documentation as needed --- .../StreamableHttpClientTransportTest.kt | 29 +++++++------------ 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/kotlin-sdk-client/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransportTest.kt b/kotlin-sdk-client/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransportTest.kt index 4c97272..a72bd85 100644 --- a/kotlin-sdk-client/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransportTest.kt +++ b/kotlin-sdk-client/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransportTest.kt @@ -19,7 +19,6 @@ import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest import io.modelcontextprotocol.kotlin.sdk.RequestId import io.modelcontextprotocol.kotlin.sdk.shared.McpJson import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.TimeoutCancellationException import kotlinx.coroutines.delay import kotlinx.coroutines.test.runTest import kotlinx.coroutines.withContext @@ -30,9 +29,9 @@ import kotlinx.serialization.json.buildJsonObject import kotlin.test.Ignore import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertFailsWith import kotlin.test.assertNull import kotlin.test.assertTrue -import kotlin.test.fail import kotlin.time.Duration.Companion.seconds class StreamableHttpClientTransportTest { @@ -405,26 +404,18 @@ class StreamableHttpClientTransportTest { ), ) - runCatching { + try { // Real time-keeping is needed; otherwise Protocol will always throw TimeoutCancellationException in tests - withContext(Dispatchers.Default.limitedParallelism(1)) { - withTimeout(5.seconds) { - client.connect(transport) + assertFailsWith( + message = "Expected client.connect to fail on invalid JSON response", + ) { + withContext(Dispatchers.Default.limitedParallelism(1)) { + withTimeout(5.seconds) { + client.connect(transport) + } } } - }.onSuccess { - fail("Expected client.connect to fail on invalid JSON response") - }.onFailure { e -> - when (e) { - is TimeoutCancellationException -> fail("Client connect caused a hang", e) - - is IllegalStateException -> { - // Expected behavior: connect finishes and fails with an exception. - } - - else -> fail("Unexpected exception during client.connect", e) - } - }.also { + } finally { transport.close() } }