Skip to content

Commit b486d61

Browse files
committed
Refactor StdioClientTransport tests
1 parent 214cc52 commit b486d61

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

kotlin-sdk-client/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/stdio/StdioClientTransportErrorHandlingTest.kt

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@ package io.modelcontextprotocol.kotlin.sdk.client.stdio
33
import io.kotest.matchers.booleans.shouldBeFalse
44
import io.kotest.matchers.shouldBe
55
import io.modelcontextprotocol.kotlin.sdk.client.StdioClientTransport
6+
import kotlinx.atomicfu.atomic
7+
import kotlinx.atomicfu.update
68
import kotlinx.coroutines.delay
79
import kotlinx.coroutines.test.runTest
810
import kotlinx.io.Buffer
11+
import kotlinx.io.writeString
912
import kotlin.test.Test
1013
import kotlin.time.Duration.Companion.milliseconds
1114

@@ -14,30 +17,34 @@ import kotlin.time.Duration.Companion.milliseconds
1417
*/
1518
class StdioClientTransportErrorHandlingTest {
1619

20+
private lateinit var transport: StdioClientTransport
21+
1722
@Test
1823
fun `should continue on stderr EOF`() = runTest {
1924
val stderrBuffer = Buffer()
2025
// Empty stderr = immediate EOF
2126

2227
val inputBuffer = Buffer()
28+
inputBuffer.writeString("""data: {"jsonrpc":"2.0","method":"ping","id":1}\n\n""")
2329
val outputBuffer = Buffer()
2430

25-
val transport = StdioClientTransport(
31+
transport = StdioClientTransport(
2632
input = inputBuffer,
2733
output = outputBuffer,
2834
error = stderrBuffer,
2935
)
3036

31-
var closeCalled = false
32-
transport.onClose { closeCalled = true }
37+
val closeCalled = atomic(false)
38+
transport.onClose { closeCalled.update { true } }
3339

3440
transport.start()
35-
delay(100.milliseconds)
41+
delay(200.milliseconds)
3642

3743
// Stderr EOF should not close transport
38-
closeCalled.shouldBeFalse()
44+
closeCalled.value shouldBe false
3945

4046
transport.close()
47+
closeCalled.value shouldBe true
4148
}
4249

4350
@Test
@@ -50,7 +57,7 @@ class StdioClientTransportErrorHandlingTest {
5057

5158
var closeCallCount = 0
5259

53-
val transport = StdioClientTransport(
60+
transport = StdioClientTransport(
5461
input = inputBuffer,
5562
output = outputBuffer,
5663
error = stderrBuffer,
@@ -73,7 +80,7 @@ class StdioClientTransportErrorHandlingTest {
7380
val inputBuffer = Buffer()
7481
val outputBuffer = Buffer()
7582

76-
val transport = StdioClientTransport(
83+
transport = StdioClientTransport(
7784
input = inputBuffer,
7885
output = outputBuffer,
7986
)

kotlin-sdk-client/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/stdio/StdioClientTransportLifecycleTest.kt

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,23 +9,27 @@ import io.modelcontextprotocol.kotlin.sdk.types.toJSON
99
import kotlinx.coroutines.delay
1010
import kotlinx.coroutines.test.runTest
1111
import kotlinx.io.Buffer
12+
import kotlin.test.BeforeTest
1213
import kotlin.test.Test
1314
import kotlin.time.Duration.Companion.milliseconds
1415

1516
class StdioClientTransportLifecycleTest {
1617

18+
private lateinit var transport: StdioClientTransport
19+
20+
@BeforeTest
21+
fun beforeEach() {
22+
transport = createTransport()
23+
}
24+
1725
@Test
1826
fun `should throw when started twice`() = runTest {
19-
val transport = createTransport()
20-
2127
transport.start()
2228

2329
val exception = shouldThrow<IllegalStateException> {
2430
transport.start()
2531
}
2632
exception.message shouldContain "already started"
27-
28-
transport.close()
2933
}
3034

3135
@Test

0 commit comments

Comments
 (0)