Skip to content

Commit b66f0bf

Browse files
committed
Stop -> StopSuspend
1 parent 239fc97 commit b66f0bf

File tree

2 files changed

+58
-80
lines changed

2 files changed

+58
-80
lines changed

kotlin-sdk-test/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/SseIntegrationTest.kt

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package io.modelcontextprotocol.kotlin.sdk.integration
22

33
import io.ktor.client.HttpClient
44
import io.ktor.client.plugins.sse.SSE
5-
import io.ktor.server.application.ApplicationStopped
65
import io.ktor.server.application.install
76
import io.ktor.server.cio.CIOApplicationEngine
87
import io.ktor.server.engine.EmbeddedServer
@@ -21,33 +20,31 @@ import io.modelcontextprotocol.kotlin.sdk.client.mcpSseTransport
2120
import io.modelcontextprotocol.kotlin.sdk.server.Server
2221
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
2322
import io.modelcontextprotocol.kotlin.sdk.server.mcp
23+
import kotlin.test.Test
24+
import kotlin.test.assertTrue
25+
import kotlin.time.Duration.Companion.seconds
2426
import kotlinx.coroutines.Dispatchers
2527
import kotlinx.coroutines.test.runTest
2628
import kotlinx.coroutines.withContext
27-
import kotlinx.coroutines.withTimeout
28-
import kotlin.test.Test
29-
import kotlin.test.assertTrue
3029
import io.ktor.client.engine.cio.CIO as ClientCIO
3130
import io.ktor.server.cio.CIO as ServerCIO
3231
import io.ktor.server.sse.SSE as ServerSSE
3332

3433
class SseIntegrationTest {
3534
@Test
36-
fun `client should be able to connect to sse server`() = runTest {
35+
fun `client should be able to connect to sse server`() = runTest(timeout = 5.seconds) {
3736
var server: EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration>? = null
3837
var client: Client? = null
3938

4039
try {
4140
withContext(Dispatchers.Default) {
42-
withTimeout(1000) {
43-
server = initServer()
44-
val port = server.engine.resolvedConnectors().first().port
45-
client = initClient(serverPort=port)
46-
}
41+
server = initServer()
42+
val port = server.engine.resolvedConnectors().first().port
43+
client = initClient(serverPort = port)
4744
}
4845
} finally {
4946
client?.close()
50-
server?.stop(1000, 2000)
47+
server?.stopSuspend(1000, 2000)
5148
}
5249
}
5350

@@ -59,23 +56,21 @@ class SseIntegrationTest {
5956
* 3. Observe that Client A receives a response related to it.
6057
*/
6158
@Test
62-
fun `single sse connection`() = runTest {
59+
fun `single sse connection`() = runTest(timeout = 5.seconds) {
6360
var server: EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration>? = null
6461
var client: Client? = null
6562
try {
6663
withContext(Dispatchers.Default) {
67-
withTimeout(1000) {
68-
server = initServer()
69-
val port = server.engine.resolvedConnectors().first().port
70-
client = initClient("Client A", port)
71-
72-
val promptA = getPrompt(client, "Client A")
73-
assertTrue { "Client A" in promptA }
74-
}
64+
server = initServer()
65+
val port = server.engine.resolvedConnectors().first().port
66+
client = initClient("Client A", port)
67+
68+
val promptA = getPrompt(client, "Client A")
69+
assertTrue { "Client A" in promptA }
7570
}
7671
} finally {
7772
client?.close()
78-
server?.stop(1000, 2000)
73+
server?.stopSuspend(1000, 2000)
7974
}
8075
}
8176

@@ -88,33 +83,31 @@ class SseIntegrationTest {
8883
* 4. Observe that Client B (connection #2) receives a response related to sessionId#1.
8984
*/
9085
@Test
91-
fun `multiple sse connections`() = runTest {
86+
fun `multiple sse connections`() = runTest(timeout = 5.seconds) {
9287
var server: EmbeddedServer<CIOApplicationEngine, CIOApplicationEngine.Configuration>? = null
9388
var clientA: Client? = null
9489
var clientB: Client? = null
9590

9691
try {
9792
withContext(Dispatchers.Default) {
98-
withTimeout(1000) {
99-
server = initServer()
100-
val port = server.engine.resolvedConnectors().first().port
93+
server = initServer()
94+
val port = server.engine.resolvedConnectors().first().port
10195

102-
clientA = initClient("Client A", port)
103-
clientB = initClient("Client B", port)
96+
clientA = initClient("Client A", port)
97+
clientB = initClient("Client B", port)
10498

105-
// Step 3: Send a prompt request from Client A
106-
val promptA = getPrompt(clientA, "Client A")
107-
// Step 4: Send a prompt request from Client B
108-
val promptB = getPrompt(clientB, "Client B")
99+
// Step 3: Send a prompt request from Client A
100+
val promptA = getPrompt(clientA, "Client A")
101+
// Step 4: Send a prompt request from Client B
102+
val promptB = getPrompt(clientB, "Client B")
109103

110-
assertTrue { "Client A" in promptA }
111-
assertTrue { "Client B" in promptB }
112-
}
104+
assertTrue { "Client A" in promptA }
105+
assertTrue { "Client B" in promptB }
113106
}
114107
} finally {
115108
clientA?.close()
116109
clientB?.close()
117-
server?.stop(1000, 2000)
110+
server?.stopSuspend(1000, 2000)
118111
}
119112
}
120113

@@ -177,10 +170,6 @@ class SseIntegrationTest {
177170
}
178171
}
179172

180-
ktorServer.monitor.subscribe(ApplicationStopped) {
181-
println("SD -- [T] ktor server has been stopped")
182-
}
183-
184173
return ktorServer.startSuspend(wait = false)
185174
}
186175

kotlin-sdk-test/src/commonTest/kotlin/io/modelcontextprotocol/kotlin/sdk/integration/WebSocketIntegrationTest.kt

Lines changed: 30 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package io.modelcontextprotocol.kotlin.sdk.integration
22

33
import io.ktor.client.HttpClient
4-
import io.ktor.server.application.ApplicationStopped
54
import io.ktor.server.application.install
65
import io.ktor.server.cio.CIOApplicationEngine
76
import io.ktor.server.engine.EmbeddedServer
@@ -20,12 +19,12 @@ import io.modelcontextprotocol.kotlin.sdk.client.mcpWebSocketTransport
2019
import io.modelcontextprotocol.kotlin.sdk.server.Server
2120
import io.modelcontextprotocol.kotlin.sdk.server.ServerOptions
2221
import io.modelcontextprotocol.kotlin.sdk.server.mcpWebSocket
22+
import kotlin.test.Test
23+
import kotlin.test.assertTrue
24+
import kotlin.time.Duration.Companion.seconds
2325
import kotlinx.coroutines.Dispatchers
2426
import kotlinx.coroutines.test.runTest
2527
import kotlinx.coroutines.withContext
26-
import kotlinx.coroutines.withTimeout
27-
import kotlin.test.Test
28-
import kotlin.test.assertTrue
2928
import io.ktor.client.engine.cio.CIO as ClientCIO
3029
import io.ktor.client.plugins.websocket.WebSockets as ClientWebSocket
3130
import io.ktor.server.cio.CIO as ServerCIO
@@ -34,21 +33,19 @@ import io.ktor.server.websocket.WebSockets as ServerWebSockets
3433
class 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

Comments
 (0)