-
Notifications
You must be signed in to change notification settings - Fork 55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add specific error handling for client connect #64
base: main
Are you sure you want to change the base?
Changes from all commits
b522dee
52f61e1
36eafc3
8f87110
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ import io.modelcontextprotocol.kotlin.sdk.CreateMessageResult | |
import io.modelcontextprotocol.kotlin.sdk.EmptyJsonObject | ||
import io.modelcontextprotocol.kotlin.sdk.Implementation | ||
import InMemoryTransport | ||
import io.mockk.coEvery | ||
import io.mockk.spyk | ||
import io.modelcontextprotocol.kotlin.sdk.InitializeRequest | ||
import io.modelcontextprotocol.kotlin.sdk.InitializeResult | ||
import io.modelcontextprotocol.kotlin.sdk.JSONRPCMessage | ||
|
@@ -49,13 +51,13 @@ import kotlin.test.fail | |
class ClientTest { | ||
@Test | ||
fun `should initialize with matching protocol version`() = runTest { | ||
var initialied = false | ||
var initialised = false | ||
val clientTransport = object : AbstractTransport() { | ||
override suspend fun start() {} | ||
|
||
override suspend fun send(message: JSONRPCMessage) { | ||
if (message !is JSONRPCRequest) return | ||
initialied = true | ||
initialised = true | ||
val result = InitializeResult( | ||
protocolVersion = LATEST_PROTOCOL_VERSION, | ||
capabilities = ServerCapabilities(), | ||
|
@@ -90,7 +92,7 @@ class ClientTest { | |
) | ||
|
||
client.connect(clientTransport) | ||
assertTrue(initialied) | ||
assertTrue(initialised) | ||
} | ||
|
||
@Test | ||
|
@@ -189,6 +191,61 @@ class ClientTest { | |
assertTrue(closed) | ||
} | ||
|
||
@Test | ||
fun `should reject due to non cancellation exception`() = runTest { | ||
var closed = false | ||
val clientTransport = object : AbstractTransport() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i will look into this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hi @e5l ,I havent had much time to look into it but maybe we can have a separate issue to take a look at all tests? |
||
override suspend fun start() {} | ||
|
||
override suspend fun send(message: JSONRPCMessage) { | ||
if (message !is JSONRPCRequest) return | ||
check(message.method == Method.Defined.Initialize.value) | ||
|
||
val result = InitializeResult( | ||
protocolVersion = LATEST_PROTOCOL_VERSION, | ||
capabilities = ServerCapabilities(), | ||
serverInfo = Implementation( | ||
name = "test", | ||
version = "1.0" | ||
) | ||
) | ||
|
||
val response = JSONRPCResponse( | ||
id = message.id, | ||
result = result | ||
) | ||
|
||
_onMessage.invoke(response) | ||
} | ||
|
||
override suspend fun close() { | ||
closed = true | ||
} | ||
} | ||
|
||
val mockClient = spyk( | ||
Client( | ||
clientInfo = Implementation( | ||
name = "test client", | ||
version = "1.0" | ||
), | ||
options = ClientOptions() | ||
) | ||
) | ||
|
||
coEvery{ | ||
mockClient.request<InitializeResult>(any()) | ||
} throws IllegalStateException("Test error") | ||
|
||
val exception = assertFailsWith<IllegalStateException> { | ||
mockClient.connect(clientTransport) | ||
} | ||
|
||
assertEquals("Error connecting to transport: Test error", exception.message) | ||
|
||
assertTrue(closed) | ||
} | ||
|
||
@Test | ||
fun `should respect server capabilities`() = runTest { | ||
val serverOptions = ServerOptions( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we add test for the case when a Cancellation exception is thrown?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ribhavpahuja can i ask how will cancellation exception be thrown in this case? wanted to clarify before adding a test