Skip to content

Commit 24fbb11

Browse files
committed
Use single Tomcat instance per test class
- Instead of running one Tomcat per test method, run one per test - Improves the testing time of Servlet-based tests Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
1 parent dc952a2 commit 24fbb11

6 files changed

Lines changed: 238 additions & 230 deletions

File tree

mcp-test/src/test/java/io/modelcontextprotocol/server/HttpServletSseIntegrationTests.java

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
import org.apache.catalina.LifecycleException;
2828
import org.apache.catalina.LifecycleState;
2929
import org.apache.catalina.startup.Tomcat;
30+
import org.junit.jupiter.api.AfterAll;
3031
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.BeforeAll;
3133
import org.junit.jupiter.api.BeforeEach;
3234
import org.junit.jupiter.api.Test;
3335
import org.junit.jupiter.api.Timeout;
@@ -44,14 +46,42 @@ class HttpServletSseIntegrationTests extends AbstractMcpClientServerIntegrationT
4446

4547
private static final String CUSTOM_MESSAGE_ENDPOINT = "/otherPath/mcp/message";
4648

47-
private HttpServletSseServerTransportProvider mcpServerTransportProvider;
49+
// Tomcat is started once for the whole class; each test swaps in its own transport
50+
private static final TomcatTestUtil.DelegatingServlet MCP_SERVLET = new TomcatTestUtil.DelegatingServlet();
51+
52+
private static Tomcat tomcat;
4853

49-
private Tomcat tomcat;
54+
private HttpServletSseServerTransportProvider mcpServerTransportProvider;
5055

5156
static Stream<Arguments> clientsForTesting() {
5257
return Stream.of(Arguments.of("httpclient"));
5358
}
5459

60+
@BeforeAll
61+
public static void beforeAll() {
62+
tomcat = TomcatTestUtil.createTomcatServer("", PORT, MCP_SERVLET);
63+
try {
64+
tomcat.start();
65+
assertThat(tomcat.getServer().getState()).isEqualTo(LifecycleState.STARTED);
66+
}
67+
catch (Exception e) {
68+
throw new RuntimeException("Failed to start Tomcat", e);
69+
}
70+
}
71+
72+
@AfterAll
73+
public static void afterAll() {
74+
if (tomcat != null) {
75+
try {
76+
tomcat.stop();
77+
tomcat.destroy();
78+
}
79+
catch (LifecycleException e) {
80+
throw new RuntimeException("Failed to stop Tomcat", e);
81+
}
82+
}
83+
}
84+
5585
@BeforeEach
5686
public void before() {
5787
// Create and configure the transport provider
@@ -61,15 +91,7 @@ public void before() {
6191
.sseEndpoint(CUSTOM_SSE_ENDPOINT)
6292
.maxRequestSize(MAX_REQUEST_SIZE)
6393
.build();
64-
65-
tomcat = TomcatTestUtil.createTomcatServer("", PORT, mcpServerTransportProvider);
66-
try {
67-
tomcat.start();
68-
assertThat(tomcat.getServer().getState()).isEqualTo(LifecycleState.STARTED);
69-
}
70-
catch (Exception e) {
71-
throw new RuntimeException("Failed to start Tomcat", e);
72-
}
94+
MCP_SERVLET.setDelegate(mcpServerTransportProvider);
7395

7496
clientBuilders
7597
.put("httpclient",
@@ -93,15 +115,6 @@ public void after() {
93115
if (mcpServerTransportProvider != null) {
94116
mcpServerTransportProvider.closeGracefully().block();
95117
}
96-
if (tomcat != null) {
97-
try {
98-
tomcat.stop();
99-
tomcat.destroy();
100-
}
101-
catch (LifecycleException e) {
102-
throw new RuntimeException("Failed to stop Tomcat", e);
103-
}
104-
}
105118
}
106119

107120
@Override

0 commit comments

Comments
 (0)