11package io.modelcontextprotocol.kotlin.sdk.integration
22
33import io.ktor.client.HttpClient
4- import io.ktor.server.application.ApplicationStopped
54import io.ktor.server.application.install
65import io.ktor.server.cio.CIOApplicationEngine
76import io.ktor.server.engine.EmbeddedServer
@@ -20,12 +19,12 @@ import io.modelcontextprotocol.kotlin.sdk.client.mcpWebSocketTransport
2019import io.modelcontextprotocol.kotlin.sdk.server.Server
2120import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
2221import io.modelcontextprotocol.kotlin.sdk.server.mcpWebSocket
22+ import kotlin.test.Test
23+ import kotlin.test.assertTrue
24+ import kotlin.time.Duration.Companion.seconds
2325import kotlinx.coroutines.Dispatchers
2426import kotlinx.coroutines.test.runTest
2527import kotlinx.coroutines.withContext
26- import kotlinx.coroutines.withTimeout
27- import kotlin.test.Test
28- import kotlin.test.assertTrue
2928import io.ktor.client.engine.cio.CIO as ClientCIO
3029import io.ktor.client.plugins.websocket.WebSockets as ClientWebSocket
3130import io.ktor.server.cio.CIO as ServerCIO
@@ -34,21 +33,19 @@ import io.ktor.server.websocket.WebSockets as ServerWebSockets
3433class WebSocketIntegrationTest {
3534
3635 @Test
37- fun `client should be able to connect to websocket server 2` () = runTest {
36+ fun `client should be able to connect to websocket server 2` () = runTest(timeout = 5 .seconds) {
3837 var server: EmbeddedServer <CIOApplicationEngine , CIOApplicationEngine .Configuration >? = null
3938 var client: Client ? = null
4039
4140 try {
4241 withContext(Dispatchers .Default ) {
43- withTimeout(1000 ) {
44- server = initServer()
45- val port = server.engine.resolvedConnectors().first().port
46- client = initClient(serverPort = port)
47- }
42+ server = initServer()
43+ val port = server.engine.resolvedConnectors().first().port
44+ client = initClient(serverPort = port)
4845 }
4946 } finally {
5047 client?.close()
51- server?.stop (1000 , 2000 )
48+ server?.stopSuspend (1000 , 2000 )
5249 }
5350 }
5451
@@ -60,24 +57,22 @@ class WebSocketIntegrationTest {
6057 * 3. Observe that Client A receives a response related to it.
6158 */
6259 @Test
63- fun `single websocket connection` () = runTest {
60+ fun `single websocket connection` () = runTest(timeout = 5 .seconds) {
6461 var server: EmbeddedServer <CIOApplicationEngine , CIOApplicationEngine .Configuration >? = null
6562 var client: Client ? = null
6663
6764 try {
6865 withContext(Dispatchers .Default ) {
69- withTimeout(1000 ) {
70- server = initServer()
71- val port = server.engine.resolvedConnectors().first().port
72- client = initClient(" Client A" , port)
73-
74- val promptA = getPrompt(client, " Client A" )
75- assertTrue { " Client A" in promptA }
76- }
66+ server = initServer()
67+ val port = server.engine.resolvedConnectors().first().port
68+ client = initClient(" Client A" , port)
69+
70+ val promptA = getPrompt(client, " Client A" )
71+ assertTrue { " Client A" in promptA }
7772 }
7873 } finally {
7974 client?.close()
80- server?.stop (1000 , 2000 )
75+ server?.stopSuspend (1000 , 2000 )
8176 }
8277 }
8378
@@ -90,32 +85,30 @@ class WebSocketIntegrationTest {
9085 * 4. Observe that Client B (connection #2) receives a response related to sessionId#1.
9186 */
9287 @Test
93- fun `multiple websocket connections` () = runTest {
88+ fun `multiple websocket connections` () = runTest(timeout = 5 .seconds) {
9489 var server: EmbeddedServer <CIOApplicationEngine , CIOApplicationEngine .Configuration >? = null
9590 var clientA: Client ? = null
9691 var clientB: Client ? = null
9792
9893 try {
9994 withContext(Dispatchers .Default ) {
100- withTimeout(1000 ) {
101- server = initServer()
102- val port = server.engine.resolvedConnectors().first().port
103- clientA = initClient(" Client A" , port)
104- clientB = initClient(" Client B" ,port)
105-
106- // Step 3: Send a prompt request from Client A
107- val promptA = getPrompt(clientA, " Client A" )
108- // Step 4: Send a prompt request from Client B
109- val promptB = getPrompt(clientB, " Client B" )
110-
111- assertTrue { " Client A" in promptA }
112- assertTrue { " Client B" in promptB }
113- }
95+ server = initServer()
96+ val port = server.engine.resolvedConnectors().first().port
97+ clientA = initClient(" Client A" , port)
98+ clientB = initClient(" Client B" , port)
99+
100+ // Step 3: Send a prompt request from Client A
101+ val promptA = getPrompt(clientA, " Client A" )
102+ // Step 4: Send a prompt request from Client B
103+ val promptB = getPrompt(clientB, " Client B" )
104+
105+ assertTrue { " Client A" in promptA }
106+ assertTrue { " Client B" in promptB }
114107 }
115108 } finally {
116109 clientA?.close()
117110 clientB?.close()
118- server?.stop (1000 , 2000 )
111+ server?.stopSuspend (1000 , 2000 )
119112 }
120113 }
121114
@@ -178,10 +171,6 @@ class WebSocketIntegrationTest {
178171 }
179172 }
180173
181- ktorServer.monitor.subscribe(ApplicationStopped ) {
182- println (" SD -- [T] ktor server has been stopped" )
183- }
184-
185174 return ktorServer.startSuspend(wait = false )
186175 }
187176
0 commit comments