Skip to content

Commit f980793

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

File tree

8 files changed

+66
-14
lines changed

8 files changed

+66
-14
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright 2025 - 2025 the original author or authors.
3+
*/
4+
package io.modelcontextprotocol.server;
5+
6+
import java.io.IOException;
7+
import java.net.InetSocketAddress;
8+
import java.net.ServerSocket;
9+
10+
public class TestUtil {
11+
12+
TestUtil() {
13+
// Prevent instantiation
14+
}
15+
16+
/**
17+
* Finds an available port on the local machine.
18+
* @return an available port number
19+
* @throws IllegalStateException if no available port can be found
20+
*/
21+
public static int findAvailablePort() {
22+
try (final ServerSocket socket = new ServerSocket()) {
23+
socket.bind(new InetSocketAddress(0));
24+
return socket.getLocalPort();
25+
}
26+
catch (final IOException e) {
27+
throw new IllegalStateException("Cannot bind to an available port!", e);
28+
}
29+
}
30+
31+
}

mcp-spring/mcp-spring-webflux/src/test/java/io/modelcontextprotocol/server/WebFluxSseMcpAsyncServerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@Timeout(15) // Giving extra time beyond the client timeout
2424
class WebFluxSseMcpAsyncServerTests extends AbstractMcpAsyncServerTests {
2525

26-
private static final int PORT = 8181;
26+
private static final int PORT = TestUtil.findAvailablePort();
2727

2828
private static final String MESSAGE_ENDPOINT = "/mcp/message";
2929

mcp-spring/mcp-spring-webflux/src/test/java/io/modelcontextprotocol/server/WebFluxSseMcpSyncServerTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
@Timeout(15) // Giving extra time beyond the client timeout
2424
class WebFluxSseMcpSyncServerTests extends AbstractMcpSyncServerTests {
2525

26-
private static final int PORT = 8182;
26+
private static final int PORT = TestUtil.findAvailablePort();
2727

2828
private static final String MESSAGE_ENDPOINT = "/mcp/message";
2929

mcp-spring/mcp-spring-webmvc/src/test/java/io/modelcontextprotocol/server/TomcatTestUtil.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
*/
44
package io.modelcontextprotocol.server;
55

6+
import java.io.IOException;
7+
import java.net.InetSocketAddress;
8+
import java.net.ServerSocket;
9+
610
import org.apache.catalina.Context;
711
import org.apache.catalina.startup.Tomcat;
812

@@ -14,10 +18,14 @@
1418
*/
1519
public class TomcatTestUtil {
1620

21+
TomcatTestUtil() {
22+
// Prevent instantiation
23+
}
24+
1725
public record TomcatServer(Tomcat tomcat, AnnotationConfigWebApplicationContext appContext) {
1826
}
1927

20-
public TomcatServer createTomcatServer(String contextPath, int port, Class<?> componentClass) {
28+
public static TomcatServer createTomcatServer(String contextPath, int port, Class<?> componentClass) {
2129

2230
// Set up Tomcat first
2331
var tomcat = new Tomcat();
@@ -57,4 +65,19 @@ public TomcatServer createTomcatServer(String contextPath, int port, Class<?> co
5765
return new TomcatServer(tomcat, appContext);
5866
}
5967

68+
/**
69+
* Finds an available port on the local machine.
70+
* @return an available port number
71+
* @throws IllegalStateException if no available port can be found
72+
*/
73+
public static int findAvailablePort() {
74+
try (final ServerSocket socket = new ServerSocket()) {
75+
socket.bind(new InetSocketAddress(0));
76+
return socket.getLocalPort();
77+
}
78+
catch (final IOException e) {
79+
throw new IllegalStateException("Cannot bind to an available port!", e);
80+
}
81+
}
82+
6083
}

mcp-spring/mcp-spring-webmvc/src/test/java/io/modelcontextprotocol/server/WebMvcSseAsyncServerTransportTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class WebMvcSseAsyncServerTransportTests extends AbstractMcpAsyncServerTests {
2525

2626
private static final String MESSAGE_ENDPOINT = "/mcp/message";
2727

28-
private static final int PORT = 8181;
28+
private static final int PORT = TomcatTestUtil.findAvailablePort();
2929

3030
private Tomcat tomcat;
3131

@@ -73,7 +73,6 @@ protected McpServerTransportProvider createMcpTransportProvider() {
7373

7474
// Create DispatcherServlet with our Spring context
7575
DispatcherServlet dispatcherServlet = new DispatcherServlet(appContext);
76-
// dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
7776

7877
// Add servlet to Tomcat and get the wrapper
7978
var wrapper = Tomcat.addServlet(context, "dispatcherServlet", dispatcherServlet);

mcp-spring/mcp-spring-webmvc/src/test/java/io/modelcontextprotocol/server/WebMvcSseCustomContextPathTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@
2222

2323
import static org.assertj.core.api.Assertions.assertThat;
2424

25-
public class WebMvcSseCustomContextPathTests {
25+
class WebMvcSseCustomContextPathTests {
2626

2727
private static final String CUSTOM_CONTEXT_PATH = "/app/1";
2828

29-
private static final int PORT = 8183;
29+
private static final int PORT = TomcatTestUtil.findAvailablePort();
3030

3131
private static final String MESSAGE_ENDPOINT = "/mcp/message";
3232

@@ -39,11 +39,11 @@ public class WebMvcSseCustomContextPathTests {
3939
@BeforeEach
4040
public void before() {
4141

42-
tomcatServer = new TomcatTestUtil().createTomcatServer(CUSTOM_CONTEXT_PATH, PORT, TestConfig.class);
42+
tomcatServer = TomcatTestUtil.createTomcatServer(CUSTOM_CONTEXT_PATH, PORT, TestConfig.class);
4343

4444
try {
4545
tomcatServer.tomcat().start();
46-
assertThat(tomcatServer.tomcat().getServer().getState() == LifecycleState.STARTED);
46+
assertThat(tomcatServer.tomcat().getServer().getState()).isEqualTo(LifecycleState.STARTED);
4747
}
4848
catch (Exception e) {
4949
throw new RuntimeException("Failed to start Tomcat", e);

mcp-spring/mcp-spring-webmvc/src/test/java/io/modelcontextprotocol/server/WebMvcSseIntegrationTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@
4646

4747
class WebMvcSseIntegrationTests {
4848

49-
private static final int PORT = 8183;
49+
private static final int PORT = TomcatTestUtil.findAvailablePort();
5050

5151
private static final String MESSAGE_ENDPOINT = "/mcp/message";
5252

@@ -75,7 +75,7 @@ public RouterFunction<ServerResponse> routerFunction(WebMvcSseServerTransportPro
7575
@BeforeEach
7676
public void before() {
7777

78-
tomcatServer = new TomcatTestUtil().createTomcatServer("", PORT, TestConfig.class);
78+
tomcatServer = TomcatTestUtil.createTomcatServer("", PORT, TestConfig.class);
7979

8080
try {
8181
tomcatServer.tomcat().start();
@@ -142,7 +142,7 @@ void testCreateMessageWithoutSamplingCapabilities() {
142142
}
143143

144144
@Test
145-
void testCreateMessageSuccess() throws InterruptedException {
145+
void testCreateMessageSuccess() {
146146

147147
// Client
148148

mcp-spring/mcp-spring-webmvc/src/test/java/io/modelcontextprotocol/server/WebMvcSseSyncServerTransportTests.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class WebMvcSseSyncServerTransportTests extends AbstractMcpSyncServerTests {
2424

2525
private static final String MESSAGE_ENDPOINT = "/mcp/message";
2626

27-
private static final int PORT = 8181;
27+
private static final int PORT = TomcatTestUtil.findAvailablePort();
2828

2929
private Tomcat tomcat;
3030

@@ -72,7 +72,6 @@ protected WebMvcSseServerTransportProvider createMcpTransportProvider() {
7272

7373
// Create DispatcherServlet with our Spring context
7474
DispatcherServlet dispatcherServlet = new DispatcherServlet(appContext);
75-
// dispatcherServlet.setThrowExceptionIfNoHandlerFound(true);
7675

7776
// Add servlet to Tomcat and get the wrapper
7877
var wrapper = Tomcat.addServlet(context, "dispatcherServlet", dispatcherServlet);

0 commit comments

Comments
 (0)