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:
parent
93c2341210
commit
2163b23d2c
1 changed files with 10 additions and 19 deletions
|
|
@ -19,7 +19,6 @@ import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest
|
||||||
import io.modelcontextprotocol.kotlin.sdk.RequestId
|
import io.modelcontextprotocol.kotlin.sdk.RequestId
|
||||||
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
|
import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
|
||||||
import kotlinx.coroutines.Dispatchers
|
import kotlinx.coroutines.Dispatchers
|
||||||
import kotlinx.coroutines.TimeoutCancellationException
|
|
||||||
import kotlinx.coroutines.delay
|
import kotlinx.coroutines.delay
|
||||||
import kotlinx.coroutines.test.runTest
|
import kotlinx.coroutines.test.runTest
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
|
|
@ -30,9 +29,9 @@ import kotlinx.serialization.json.buildJsonObject
|
||||||
import kotlin.test.Ignore
|
import kotlin.test.Ignore
|
||||||
import kotlin.test.Test
|
import kotlin.test.Test
|
||||||
import kotlin.test.assertEquals
|
import kotlin.test.assertEquals
|
||||||
|
import kotlin.test.assertFailsWith
|
||||||
import kotlin.test.assertNull
|
import kotlin.test.assertNull
|
||||||
import kotlin.test.assertTrue
|
import kotlin.test.assertTrue
|
||||||
import kotlin.test.fail
|
|
||||||
import kotlin.time.Duration.Companion.seconds
|
import kotlin.time.Duration.Companion.seconds
|
||||||
|
|
||||||
class StreamableHttpClientTransportTest {
|
class StreamableHttpClientTransportTest {
|
||||||
|
|
@ -405,26 +404,18 @@ class StreamableHttpClientTransportTest {
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
runCatching {
|
try {
|
||||||
// Real time-keeping is needed; otherwise Protocol will always throw TimeoutCancellationException in tests
|
// Real time-keeping is needed; otherwise Protocol will always throw TimeoutCancellationException in tests
|
||||||
withContext(Dispatchers.Default.limitedParallelism(1)) {
|
assertFailsWith<IllegalStateException>(
|
||||||
withTimeout(5.seconds) {
|
message = "Expected client.connect to fail on invalid JSON response",
|
||||||
client.connect(transport)
|
) {
|
||||||
|
withContext(Dispatchers.Default.limitedParallelism(1)) {
|
||||||
|
withTimeout(5.seconds) {
|
||||||
|
client.connect(transport)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}.onSuccess {
|
} finally {
|
||||||
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 {
|
|
||||||
transport.close()
|
transport.close()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue