@@ -19,7 +19,6 @@ import io.modelcontextprotocol.kotlin.sdk.JSONRPCRequest
1919import io.modelcontextprotocol.kotlin.sdk.RequestId
2020import io.modelcontextprotocol.kotlin.sdk.shared.McpJson
2121import kotlinx.coroutines.Dispatchers
22- import kotlinx.coroutines.TimeoutCancellationException
2322import kotlinx.coroutines.delay
2423import kotlinx.coroutines.test.runTest
2524import kotlinx.coroutines.withContext
@@ -30,9 +29,9 @@ import kotlinx.serialization.json.buildJsonObject
3029import kotlin.test.Ignore
3130import kotlin.test.Test
3231import kotlin.test.assertEquals
32+ import kotlin.test.assertFailsWith
3333import kotlin.test.assertNull
3434import kotlin.test.assertTrue
35- import kotlin.test.fail
3635import kotlin.time.Duration.Companion.seconds
3736
3837class 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