From 7cb47163c1ffda2116dc104133259055b6290118 Mon Sep 17 00:00:00 2001 From: Konstantin Pavlov <1517853+kpavlov@users.noreply.github.com> Date: Mon, 8 Dec 2025 13:19:44 +0200 Subject: [PATCH] Fix flaky StdioClientTransportErrorHandlingTest Use non-empty buffer for reliability. --- .../StdioClientTransportErrorHandlingTest.kt | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/kotlin-sdk-client/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/stdio/StdioClientTransportErrorHandlingTest.kt b/kotlin-sdk-client/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/stdio/StdioClientTransportErrorHandlingTest.kt index 43a8b04f..da187411 100644 --- a/kotlin-sdk-client/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/stdio/StdioClientTransportErrorHandlingTest.kt +++ b/kotlin-sdk-client/src/jvmTest/kotlin/io/modelcontextprotocol/kotlin/sdk/client/stdio/StdioClientTransportErrorHandlingTest.kt @@ -41,8 +41,9 @@ class StdioClientTransportErrorHandlingTest { val stderrBuffer = Buffer() // Empty stderr = immediate EOF - val inputBuffer = Buffer() - inputBuffer.writeString("""data: {"jsonrpc":"2.0","method":"ping","id":1}\n\n""") + val inputBuffer = createNonEmptyBuffer { + """data: {"jsonrpc":"2.0","method":"ping","id":1}\n\n""" + } val outputBuffer = Buffer() transport = StdioClientTransport( @@ -66,8 +67,9 @@ class StdioClientTransportErrorHandlingTest { @Test fun `should call onClose exactly once on error scenarios`() = runTest { - val stderrBuffer = Buffer() - stderrBuffer.write("FATAL: critical error\n".encodeToByteArray()) + val stderrBuffer = createNonEmptyBuffer { + "FATAL: critical error\n" + } val inputBuffer = Buffer() val outputBuffer = Buffer() @@ -149,8 +151,9 @@ class StdioClientTransportErrorHandlingTest { fun `Send should handle exceptions`(throwable: Throwable, shouldWrap: Boolean, expectedCode: Int?) = runTest { val sendChannel: Channel = mockk(relaxed = true) + val stdin = createNonEmptyBuffer { "id: 1\ndata:\n" } transport = StdioClientTransport( - input = Buffer(), + input = stdin, output = Buffer(), sendChannel = sendChannel, ) @@ -173,4 +176,10 @@ class StdioClientTransportErrorHandlingTest { exception shouldBeSameInstanceAs throwable } } + + fun createNonEmptyBuffer(block: () -> String): Buffer { + val buffer = Buffer() + buffer.writeString(block()) + return buffer + } }