Skip to content

Commit 2163b23

Browse files
authored
Refactor testClientConnectWithInvalidJson (#338)
simplification ## Breaking Changes <!-- Will users need to update their code or configurations? --> ## 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
1 parent 93c2341 commit 2163b23

File tree

1 file changed

+10
-19
lines changed

1 file changed

+10
-19
lines changed

kotlin-sdk-client/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/StreamableHttpClientTransportTest.kt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest
1919
import io.modelcontextprotocol.kotlin.sdk.RequestId
2020
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
2121
import kotlinx.coroutines.Dispatchers
22-
import kotlinx.coroutines.TimeoutCancellationException
2322
import kotlinx.coroutines.delay
2423
import kotlinx.coroutines.test.runTest
2524
import kotlinx.coroutines.withContext
@@ -30,9 +29,9 @@ import kotlinx.serialization.json.buildJsonObject
3029
import kotlin.test.Ignore
3130
import kotlin.test.Test
3231
import kotlin.test.assertEquals
32+
import kotlin.test.assertFailsWith
3333
import kotlin.test.assertNull
3434
import kotlin.test.assertTrue
35-
import kotlin.test.fail
3635
import kotlin.time.Duration.Companion.seconds
3736

3837
class StreamableHttpClientTransportTest {
@@ -405,26 +404,18 @@ class StreamableHttpClientTransportTest {
405404
),
406405
)
407406

408-
runCatching {
407+
try {
409408
// Real time-keeping is needed; otherwise Protocol will always throw TimeoutCancellationException in tests
410-
withContext(Dispatchers.Default.limitedParallelism(1)) {
411-
withTimeout(5.seconds) {
412-
client.connect(transport)
413-
}
414-
}
415-
}.onSuccess {
416-
fail("Expected client.connect to fail on invalid JSON response")
417-
}.onFailure { e ->
418-
when (e) {
419-
is TimeoutCancellationException -> fail("Client connect caused a hang", e)
420-
421-
is IllegalStateException -> {
422-
// Expected behavior: connect finishes and fails with an exception.
409+
assertFailsWith<IllegalStateException>(
410+
message = "Expected client.connect to fail on invalid JSON response",
411+
) {
412+
withContext(Dispatchers.Default.limitedParallelism(1)) {
413+
withTimeout(5.seconds) {
414+
client.connect(transport)
415+
}
423416
}
424-
425-
else -> fail("Unexpected exception during client.connect", e)
426417
}
427-
}.also {
418+
} finally {
428419
transport.close()
429420
}
430421
}

0 commit comments

Comments
 (0)