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
This commit is contained in:
Pavel Gorgulov 2025-10-23 20:06:40 +02:00 • committed by GitHub
commit 2163b23d2c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -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<IllegalStateException>(
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()
}
}