Skip to content

Commit cb03c52

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

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

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

Lines changed: 9 additions & 4 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

@@ -20,6 +23,7 @@ class StdioClientTransportErrorHandlingTest {
2023
// Empty stderr = immediate EOF
2124

2225
val inputBuffer = Buffer()
26+
inputBuffer.writeString("""data: {"jsonrpc":"2.0","method":"ping","id":1}\n\n""")
2327
val outputBuffer = Buffer()
2428

2529
val transport = StdioClientTransport(
@@ -28,16 +32,17 @@ class StdioClientTransportErrorHandlingTest {
2832
error = stderrBuffer,
2933
)
3034

31-
var closeCalled = false
32-
transport.onClose { closeCalled = true }
35+
val closeCalled = atomic(false)
36+
transport.onClose { closeCalled.update { true } }
3337

3438
transport.start()
35-
delay(100.milliseconds)
39+
delay(200.milliseconds)
3640

3741
// Stderr EOF should not close transport
38-
closeCalled.shouldBeFalse()
42+
closeCalled.value shouldBe false
3943

4044
transport.close()
45+
closeCalled.value shouldBe true
4146
}
4247

4348
@Test

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)