Skip to content

Commit f7899b2

Browse files
committed
feat(test): adds findAvailablePort for test server
1 parent fab434c commit f7899b2

File tree

3 files changed

+34
-17
lines changed

3 files changed

+34
-17
lines changed

mcp/src/test/java/io/modelcontextprotocol/server/transport/HttpServletSseServerCustomContextPathTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44
package io.modelcontextprotocol.server.transport;
55

66
import com.fasterxml.jackson.databind.ObjectMapper;
7+
78
import io.modelcontextprotocol.client.McpClient;
89
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
910
import io.modelcontextprotocol.server.McpServer;
1011
import io.modelcontextprotocol.spec.McpSchema;
11-
import org.apache.catalina.Context;
1212
import org.apache.catalina.LifecycleException;
1313
import org.apache.catalina.LifecycleState;
1414
import org.apache.catalina.startup.Tomcat;
@@ -18,9 +18,9 @@
1818

1919
import static org.assertj.core.api.Assertions.assertThat;
2020

21-
public class HttpServletSseServerCustomContextPathTests {
21+
class HttpServletSseServerCustomContextPathTests {
2222

23-
private static final int PORT = 8195;
23+
private static final int PORT = TomcatTestUtil.findAvailablePort();
2424

2525
private static final String CUSTOM_CONTEXT_PATH = "/api/v1";
2626

@@ -49,7 +49,7 @@ public void before() {
4949

5050
try {
5151
tomcat.start();
52-
assertThat(tomcat.getServer().getState() == LifecycleState.STARTED);
52+
assertThat(tomcat.getServer().getState()).isEqualTo(LifecycleState.STARTED);
5353
}
5454
catch (Exception e) {
5555
throw new RuntimeException("Failed to start Tomcat", e);

mcp/src/test/java/io/modelcontextprotocol/server/transport/HttpServletSseServerTransportProviderIntegrationTests.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import java.util.function.Function;
1111

1212
import com.fasterxml.jackson.databind.ObjectMapper;
13+
1314
import io.modelcontextprotocol.client.McpClient;
1415
import io.modelcontextprotocol.client.transport.HttpClientSseClientTransport;
1516
import io.modelcontextprotocol.server.McpServer;
@@ -42,9 +43,9 @@
4243
import static org.awaitility.Awaitility.await;
4344
import static org.mockito.Mockito.mock;
4445

45-
public class HttpServletSseServerTransportProviderIntegrationTests {
46+
class HttpServletSseServerTransportProviderIntegrationTests {
4647

47-
private static final int PORT = 8185;
48+
private static final int PORT = TomcatTestUtil.findAvailablePort();
4849

4950
private static final String CUSTOM_SSE_ENDPOINT = "/somePath/sse";
5051

@@ -68,7 +69,7 @@ public void before() {
6869
tomcat = TomcatTestUtil.createTomcatServer("", PORT, mcpServerTransportProvider);
6970
try {
7071
tomcat.start();
71-
assertThat(tomcat.getServer().getState() == LifecycleState.STARTED);
72+
assertThat(tomcat.getServer().getState()).isEqualTo(LifecycleState.STARTED);
7273
}
7374
catch (Exception e) {
7475
throw new RuntimeException("Failed to start Tomcat", e);
@@ -127,7 +128,7 @@ void testCreateMessageWithoutSamplingCapabilities() {
127128
}
128129

129130
@Test
130-
void testCreateMessageSuccess() throws InterruptedException {
131+
void testCreateMessageSuccess() {
131132

132133
// Client
133134

@@ -185,8 +186,7 @@ void testCreateMessageSuccess() throws InterruptedException {
185186

186187
CallToolResult response = mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
187188

188-
assertThat(response).isNotNull();
189-
assertThat(response).isEqualTo(callResponse);
189+
assertThat(response).isNotNull().isEqualTo(callResponse);
190190

191191
mcpClient.close();
192192
mcpServer.close();
@@ -395,8 +395,7 @@ void testToolCallSuccess() {
395395

396396
CallToolResult response = mcpClient.callTool(new McpSchema.CallToolRequest("tool1", Map.of()));
397397

398-
assertThat(response).isNotNull();
399-
assertThat(response).isEqualTo(callResponse);
398+
assertThat(response).isNotNull().isEqualTo(callResponse);
400399

401400
mcpClient.close();
402401
mcpServer.close();

mcp/src/test/java/io/modelcontextprotocol/server/transport/TomcatTestUtil.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,23 @@
33
*/
44
package io.modelcontextprotocol.server.transport;
55

6-
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import java.io.IOException;
7+
import java.net.InetSocketAddress;
8+
import java.net.ServerSocket;
9+
710
import jakarta.servlet.Servlet;
811
import org.apache.catalina.Context;
9-
import org.apache.catalina.LifecycleState;
1012
import org.apache.catalina.startup.Tomcat;
1113

12-
import static org.junit.Assert.assertThat;
13-
1414
/**
1515
* @author Christian Tzolov
1616
*/
1717
public class TomcatTestUtil {
1818

19+
TomcatTestUtil() {
20+
// Prevent instantiation
21+
}
22+
1923
public static Tomcat createTomcatServer(String contextPath, int port, Servlet servlet) {
2024

2125
var tomcat = new Tomcat();
@@ -24,7 +28,6 @@ public static Tomcat createTomcatServer(String contextPath, int port, Servlet se
2428
String baseDir = System.getProperty("java.io.tmpdir");
2529
tomcat.setBaseDir(baseDir);
2630

27-
// Context context = tomcat.addContext("", baseDir);
2831
Context context = tomcat.addContext(contextPath, baseDir);
2932

3033
// Add transport servlet to Tomcat
@@ -42,4 +45,19 @@ public static Tomcat createTomcatServer(String contextPath, int port, Servlet se
4245
return tomcat;
4346
}
4447

48+
/**
49+
* Finds an available port on the local machine.
50+
* @return an available port number
51+
* @throws IllegalStateException if no available port can be found
52+
*/
53+
public static int findAvailablePort() {
54+
try (final ServerSocket socket = new ServerSocket()) {
55+
socket.bind(new InetSocketAddress(0));
56+
return socket.getLocalPort();
57+
}
58+
catch (final IOException e) {
59+
throw new IllegalStateException("Cannot bind to an available port!", e);
60+
}
61+
}
62+
4563
}

0 commit comments

Comments
 (0)