Rewrite keep-alive and session-sweeping tests with VirtualTimeScheduler
Some checks failed
Conformance Tests / Server Conformance (push) Has been cancelled
Conformance Tests / Client Conformance (push) Has been cancelled
Conformance Tests / Client Conformance-1 (push) Has been cancelled
Conformance Tests / Client Conformance-2 (push) Has been cancelled
Conformance Tests / Client Conformance-3 (push) Has been cancelled
Conformance Tests / Auth Conformance (push) Has been cancelled
Conformance Tests / Auth Conformance-1 (push) Has been cancelled
Conformance Tests / Auth Conformance-2 (push) Has been cancelled
Conformance Tests / Auth Conformance-3 (push) Has been cancelled
Conformance Tests / Auth Conformance-4 (push) Has been cancelled
Conformance Tests / Auth Conformance-5 (push) Has been cancelled
Conformance Tests / Auth Conformance-6 (push) Has been cancelled
Conformance Tests / Auth Conformance-7 (push) Has been cancelled
Conformance Tests / Auth Conformance-8 (push) Has been cancelled
Conformance Tests / Auth Conformance-9 (push) Has been cancelled
Conformance Tests / Auth Conformance-10 (push) Has been cancelled
Conformance Tests / Auth Conformance-11 (push) Has been cancelled
Conformance Tests / Auth Conformance-12 (push) Has been cancelled
Conformance Tests / Auth Conformance-13 (push) Has been cancelled
Publish Snapshot / Build branch (push) Has been cancelled

Signed-off-by: Daniel Garnier-Moiroux <git@garnier.wf>
This commit is contained in:
Daniel Garnier-Moiroux 2026-09-17 21:42:10 +02:00 • committed by Daniel Garnier-Moiroux
commit c7fef64f92

View file

@ -51,7 +51,9 @@ import org.junit.jupiter.params.provider.Arguments;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import reactor.core.publisher.Flux; import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono; import reactor.core.publisher.Mono;
import reactor.core.scheduler.Schedulers;
import reactor.test.StepVerifier; import reactor.test.StepVerifier;
import reactor.test.scheduler.VirtualTimeScheduler;
import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA; import static io.modelcontextprotocol.util.ToolsUtils.EMPTY_JSON_SCHEMA;
import static java.nio.charset.StandardCharsets.UTF_8; import static java.nio.charset.StandardCharsets.UTF_8;
@ -85,6 +87,8 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
// result in a session sweep // result in a session sweep
private final Duration SESSION_SWEEP_INTERVAL = KEEP_ALIVE_INTERVAL.multipliedBy(2); private final Duration SESSION_SWEEP_INTERVAL = KEEP_ALIVE_INTERVAL.multipliedBy(2);
private final HttpClient httpClient = HttpClient.newHttpClient();
@Override @Override
protected void awaitClientStreamEstablished() { protected void awaitClientStreamEstablished() {
var timeout = Duration.ofSeconds(5); var timeout = Duration.ofSeconds(5);
@ -209,7 +213,6 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
@Test @Test
void rejectsWhenBodyBytesExceedLimitWithoutContentLengthHeader() throws Exception { void rejectsWhenBodyBytesExceedLimitWithoutContentLengthHeader() throws Exception {
var httpClient = HttpClient.newHttpClient();
// A publisher with unknown content length forces chunked transfer encoding, // A publisher with unknown content length forces chunked transfer encoding,
// bypassing the Content-Length header check and exercising the body byte // bypassing the Content-Length header check and exercising the body byte
// count // count
@ -248,9 +251,8 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
} }
@Test @Test
void resumedStreamReceivesServerNotifications() throws Exception { void resumedStreamReceivesServerNotifications() {
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var httpClient = HttpClient.newHttpClient();
var sessionId = initializeSession(httpClient); var sessionId = initializeSession(httpClient);
@ -264,9 +266,8 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
} }
@Test @Test
void replacedListeningStreamIsClosed() throws Exception { void replacedListeningStreamIsClosed() {
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var httpClient = HttpClient.newHttpClient();
var sessionId = initializeSession(httpClient); var sessionId = initializeSession(httpClient);
@ -292,34 +293,34 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
} }
@Test @Test
void keepAliveSkipsSessionsWithoutListeningStream() throws Exception { void keepAliveSkipsSessionsWithoutListeningStream() {
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var httpClient = HttpClient.newHttpClient();
var keepAliveLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(KeepAliveScheduler.class); var keepAliveLogger = (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(KeepAliveScheduler.class);
ListAppender<ILoggingEvent> logAppender = new ListAppender<>(); ListAppender<ILoggingEvent> logAppender = new ListAppender<>();
logAppender.start(); logAppender.start();
keepAliveLogger.addAppender(logAppender); keepAliveLogger.addAppender(logAppender);
var virtualTime = VirtualTimeScheduler.getOrSet();
try { try {
// A client is free to never issue the GET request establishing a listening withTransportProvider(KEEP_ALIVE_INTERVAL, null);
// stream. Such a session has nothing to write a ping to, so it must not be
// pinged on every keep-alive interval.
initializeSession(httpClient); initializeSession(httpClient);
Thread.sleep(KEEP_ALIVE_INTERVAL.multipliedBy(3).toMillis()); // go through multiple keep-alive-rounds
virtualTime.advanceTimeBy(KEEP_ALIVE_INTERVAL.multipliedBy(3));
assertThat(logAppender.list).noneMatch(event -> event.getLevel() == Level.WARN); assertThat(logAppender.list).as("Should not display any Keep-Alive warning log")
.noneMatch(event -> event.getLevel() == Level.WARN);
} }
finally { finally {
keepAliveLogger.detachAppender(logAppender); keepAliveLogger.detachAppender(logAppender);
logAppender.stop(); logAppender.stop();
VirtualTimeScheduler.reset();
} }
} }
@Test @Test
void keepAlivePingsSessionsWithListeningStream() throws Exception { void keepAlivePingsSessionsWithListeningStream() {
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var httpClient = HttpClient.newHttpClient();
var sessionId = initializeSession(httpClient); var sessionId = initializeSession(httpClient);
var stream = openListeningStream(httpClient, sessionId, null); var stream = openListeningStream(httpClient, sessionId, null);
@ -331,9 +332,8 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
} }
@Test @Test
void unansweredKeepAlivePingReleasesTheStreamButKeepsTheSession() throws Exception { void unansweredKeepAlivePingReleasesTheStreamButKeepsTheSession() {
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var httpClient = HttpClient.newHttpClient();
var sessionId = initializeSession(httpClient); var sessionId = initializeSession(httpClient);
var stream = openListeningStream(httpClient, sessionId, null); var stream = openListeningStream(httpClient, sessionId, null);
@ -350,182 +350,163 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
} }
@Test @Test
void sessionEviction() throws Exception { void sessionEviction() {
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); var virtualTime = VirtualTimeScheduler.getOrSet();
var httpClient = HttpClient.newHttpClient(); try {
var sessionId = initializeSession(httpClient); withTransportProvider(null, SESSION_SWEEP_INTERVAL);
for (int i = 0; i < 4; i++) { var sessionId = initializeSession(httpClient);
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_ACCEPTED);
Thread.sleep(SESSION_SWEEP_INTERVAL.dividedBy(2).toMillis()); // One client request every sweep interval/2, the session remains active
// We do 5 total requests so the total time is > 2 sweep intervals, verifying
// that the sweeper does not reclaim a session
for (int i = 1; i < 6; i++) {
virtualTime.advanceTimeBy(SESSION_SWEEP_INTERVAL.dividedBy(2));
assertThat(postNotification(httpClient, sessionId)).as("Session should be active for request #%s", i)
.isEqualTo(HttpServletResponse.SC_ACCEPTED);
}
virtualTime.advanceTimeBy(SESSION_SWEEP_INTERVAL.multipliedBy(2));
assertThat(postNotification(httpClient, sessionId)).as("Session should be sweeped after SWEEP_INTERVAL * 2")
.isEqualTo(HttpServletResponse.SC_NOT_FOUND);
}
finally {
VirtualTimeScheduler.reset();
} }
// The session is active, so it's not swept
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_ACCEPTED);
// Wait for two sweep intervals to ensure the session has been swept
Thread.sleep(SESSION_SWEEP_INTERVAL.multipliedBy(2).toMillis());
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
} }
@Test @Test
void sessionHoldingAnOpenStreamIsNotEvicted() throws Exception { void sessionHoldingAnOpenStreamIsNotEvicted() {
mcpServerTransportProvider.closeGracefully().block(); var virtualTime = VirtualTimeScheduler.getOrSet();
mcpServerTransportProvider = HttpServletStreamableServerTransportProvider.builder() try {
.contextExtractor(TEST_CONTEXT_EXTRACTOR) withTransportProvider(null, SESSION_SWEEP_INTERVAL);
.mcpEndpoint(MESSAGE_ENDPOINT) var sessionId = initializeSession(httpClient);
// remove keepalive to ensure the SSE stream is not removed on ping failures openListeningStream(httpClient, sessionId, null);
.keepAliveInterval(null) awaitClientStreamEstablished();
.sessionSweepInterval(SESSION_SWEEP_INTERVAL)
.build();
MCP_SERVLET.setDelegate(mcpServerTransportProvider);
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var httpClient = HttpClient.newHttpClient();
var sessionId = initializeSession(httpClient); virtualTime.advanceTimeBy(SESSION_SWEEP_INTERVAL.multipliedBy(2));
openListeningStream(httpClient, sessionId, null); assertThat(postNotification(httpClient, sessionId))
awaitClientStreamEstablished(); .as("Session should not be sweeped when a GET SSE stream is open")
.isEqualTo(HttpServletResponse.SC_ACCEPTED);
// A client is allowed to hold a connection open without sending anything on it, }
// so this session is not idle however long it stays silent finally {
Thread.sleep(SESSION_SWEEP_INTERVAL.multipliedBy(2).toMillis()); VirtualTimeScheduler.reset();
}
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_ACCEPTED);
} }
@Test @Test
void sessionWithStreamIsNotEvicted() throws Exception { void sessionEvictionAfterReleasingStream() {
mcpServerTransportProvider.closeGracefully().block(); var virtualTime = VirtualTimeScheduler.getOrSet();
mcpServerTransportProvider = HttpServletStreamableServerTransportProvider.builder() try {
.contextExtractor(TEST_CONTEXT_EXTRACTOR) withTransportProvider(null, SESSION_SWEEP_INTERVAL);
.mcpEndpoint(MESSAGE_ENDPOINT) var sessionId = initializeSession(httpClient);
// remove keepalive to ensure the SSE stream is not removed on ping failures var clientStream = openListeningStream(httpClient, sessionId, null);
.keepAliveInterval(null) awaitClientStreamEstablished();
.sessionSweepInterval(SESSION_SWEEP_INTERVAL) // "prep" the client so we can close the stream by sending data: the
.build(); // subscription
MCP_SERVLET.setDelegate(mcpServerTransportProvider); // in the clientStream is only present when the client has received data
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build(); awaitNotification(clientStream.events());
var httpClient = HttpClient.newHttpClient(); virtualTime.advanceTimeBy(SESSION_SWEEP_INTERVAL.multipliedBy(2));
var sessionId = initializeSession(httpClient); assertThat(postNotification(httpClient, sessionId)).as("Session should be active when a stream is open")
openListeningStream(httpClient, sessionId, null); .isEqualTo(HttpServletResponse.SC_ACCEPTED);
// Wait for two sweep intervals to ensure the sweeper has run // Close the client stream
Thread.sleep(SESSION_SWEEP_INTERVAL.multipliedBy(2).toMillis()); clientStream.closeStream();
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_ACCEPTED);
await().atMost(Duration.ofSeconds(5))
.pollDelay(Duration.ZERO)
.pollInterval(Duration.ZERO)
.untilAsserted(() -> {
// send a message so the server realizes the client is gone might take
// a few tries before the internal buffer fills up and the connection
// errors
mcpServerTransportProvider.notifyClients(McpSchema.METHOD_NOTIFICATION_RESOURCES_LIST_CHANGED, null)
.block();
// eventually, the session should be sweepable
virtualTime.advanceTimeBy(SESSION_SWEEP_INTERVAL.multipliedBy(2));
assertThat(postNotification(httpClient, sessionId))
.as("Session should be sweeped after a stream is released")
.isEqualTo(HttpServletResponse.SC_NOT_FOUND);
});
}
finally {
VirtualTimeScheduler.reset();
}
} }
@Test @Test
void sessionEvictionAfterReleasingStream() throws Exception { void sessionIsNotEvictedWithoutSweepInterval() {
mcpServerTransportProvider.closeGracefully().block(); var virtualTime = VirtualTimeScheduler.getOrSet();
mcpServerTransportProvider = HttpServletStreamableServerTransportProvider.builder() try {
.contextExtractor(TEST_CONTEXT_EXTRACTOR) withTransportProvider(KEEP_ALIVE_INTERVAL, null);
.mcpEndpoint(MESSAGE_ENDPOINT) var sessionId = initializeSession(httpClient);
// remove keepalive to ensure the SSE stream is not removed on ping failures
.keepAliveInterval(null)
.sessionSweepInterval(SESSION_SWEEP_INTERVAL)
.build();
MCP_SERVLET.setDelegate(mcpServerTransportProvider);
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var httpClient = HttpClient.newHttpClient();
var sessionId = initializeSession(httpClient); virtualTime.advanceTimeBy(SESSION_SWEEP_INTERVAL.multipliedBy(10));
var clientStream = openListeningStream(httpClient, sessionId, null); assertThat(postNotification(httpClient, sessionId))
awaitClientStreamEstablished(); .as("Session should never be sweeped when there is no sessionSweepInterval")
// "prep" the client so we can close the stream by sending data: the subscription .isEqualTo(HttpServletResponse.SC_ACCEPTED);
// in the clientStream is only present when the client has received data }
awaitNotification(clientStream.events()); finally {
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_ACCEPTED); VirtualTimeScheduler.reset();
}
// Close the client stream
clientStream.closeStream();
await().atMost(Duration.ofSeconds(5))
.pollDelay(Duration.ZERO)
.pollInterval(SESSION_SWEEP_INTERVAL.multipliedBy(2))
.untilAsserted(() -> {
// send a message so the server realizes the client is gone
// might take a few tries before the internal buffer fills up and the
// connection errors
mcpServerTransportProvider.notifyClients(McpSchema.METHOD_NOTIFICATION_RESOURCES_LIST_CHANGED, null)
.block();
// eventually, the session should be sweepable
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
});
}
@Test
void sessionIsNotEvictedWithoutSweepInterval() throws Exception {
// Sweeping can be turned off: such a transport keeps its sessions until they are
// deleted or the server shuts down
mcpServerTransportProvider.closeGracefully().block();
mcpServerTransportProvider = HttpServletStreamableServerTransportProvider.builder()
.contextExtractor(TEST_CONTEXT_EXTRACTOR)
.mcpEndpoint(MESSAGE_ENDPOINT)
.keepAliveInterval(KEEP_ALIVE_INTERVAL)
.sessionSweepInterval(null)
.build();
MCP_SERVLET.setDelegate(mcpServerTransportProvider);
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
var httpClient = HttpClient.newHttpClient();
var sessionId = initializeSession(httpClient);
// The session holds no stream and stays silent for long enough that a scheduled
// sweeper would have evicted it several times over
Thread.sleep(SESSION_SWEEP_INTERVAL.multipliedBy(4).toMillis());
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_ACCEPTED);
} }
/** /**
* A client which aborts a tool call must not leave its session behind. * A client which aborts a tool call must not leave its session behind.
*/ */
@Test @Test
void sessionIsEvictedWhenTheClientAbortsAResponseStream() throws Exception { void sessionIsEvictedWhenTheClientAbortsAResponseStream() {
mcpServerTransportProvider.closeGracefully().block(); var virtualTime = VirtualTimeScheduler.getOrSet();
mcpServerTransportProvider = HttpServletStreamableServerTransportProvider.builder() try {
.contextExtractor(TEST_CONTEXT_EXTRACTOR) withTransportProvider(null, SESSION_SWEEP_INTERVAL);
.mcpEndpoint(MESSAGE_ENDPOINT) var toolEmissionInterval = Duration.ofMillis(10);
// remove keepalive, so only the response stream can keep the session alive prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0")
.keepAliveInterval(null) .capabilities(McpSchema.ServerCapabilities.builder().tools(true).build())
.sessionSweepInterval(SESSION_SWEEP_INTERVAL) .tools(McpServerFeatures.AsyncToolSpecification.builder()
.build(); .tool(McpSchema.Tool.builder("hangs", EMPTY_JSON_SCHEMA).description("never returns").build())
MCP_SERVLET.setDelegate(mcpServerTransportProvider); .callHandler((exchange, request) -> Flux
.interval(Duration.ZERO, toolEmissionInterval, Schedulers.newSingle("test-tool"))
.flatMap(tick -> exchange.loggingNotification(McpSchema.LoggingMessageNotification.builder()
.level(McpSchema.LoggingLevel.INFO)
.data("x".repeat(64 * 1024))
.build()))
.then(Mono.<McpSchema.CallToolResult>never()))
.build())
.build();
var sessionId = initializeSession(httpClient);
// A tool which never returns but keeps writing to its response stream. The // The POST opens a response SSE stream, which the session counts as an open
// payloads are large and frequent on purpose: writes to a connection whose peer // stream for as long as the call is in flight
// is gone keep succeeding until the socket buffer fills up, and that is the only var responseStream = postToolCall(httpClient, sessionId, "hangs");
// thing which can surface the disconnect here.
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0")
.capabilities(McpSchema.ServerCapabilities.builder().tools(true).build())
.tools(McpServerFeatures.AsyncToolSpecification.builder()
.tool(McpSchema.Tool.builder("hangs", EMPTY_JSON_SCHEMA).description("never returns").build())
.callHandler((exchange, request) -> Flux.interval(Duration.ofMillis(10))
.flatMap(tick -> exchange.loggingNotification(McpSchema.LoggingMessageNotification.builder()
.level(McpSchema.LoggingLevel.INFO)
.data("x".repeat(64 * 1024))
.build()))
.then(Mono.<McpSchema.CallToolResult>never()))
.build())
.build();
var httpClient = HttpClient.newHttpClient(); await().atMost(Duration.ofSeconds(5))
var sessionId = initializeSession(httpClient); .pollDelay(Duration.ZERO)
.pollInterval(Duration.ofMillis(10))
.untilAsserted(() -> {
// We do this in a loop for the wire-side of the connection - data has
// to be buffered then flushed
// The POST opens a response SSE stream, which the session counts as an open // Advance time so the tool emits some data
// stream for as long as the call is in flight virtualTime.advanceTimeBy(toolEmissionInterval);
var responseStream = postToolCall(httpClient, sessionId, "hangs"); // Verify the data is received
await().atMost(Duration.ofSeconds(5)) assertThat(responseStream.events()).anyMatch(line -> line.contains("notifications/message"));
.untilAsserted( });
() -> assertThat(responseStream.events()).anyMatch(line -> line.contains("notifications/message"))); virtualTime.advanceTimeBy(SESSION_SWEEP_INTERVAL.multipliedBy(2));
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_ACCEPTED); assertThat(postNotification(httpClient, sessionId)).as("Session with POST stream is active")
.isEqualTo(HttpServletResponse.SC_ACCEPTED);
// The client gives up on the call and disconnects // The client gives up on the call and disconnects
responseStream.closeStream(); responseStream.closeStream();
// Nothing is connected to the session anymore, so the sweeper must reclaim it. // Nothing is connected to the session anymore, so the sweeper must reclaim
// Probing only once: any request would count as activity and reset the clock. // it.
Thread.sleep(SESSION_SWEEP_INTERVAL.multipliedBy(2).toMillis()); // Probing only once: any request would count as activity and reset the clock.
virtualTime.advanceTimeBy(SESSION_SWEEP_INTERVAL.multipliedBy(5));
assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_NOT_FOUND); assertThat(postNotification(httpClient, sessionId)).isEqualTo(HttpServletResponse.SC_NOT_FOUND);
}
finally {
VirtualTimeScheduler.reset();
}
} }
/** /**
@ -559,7 +540,7 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
return new StreamResponse(clientFuture, events, streamRef); return new StreamResponse(clientFuture, events, streamRef);
} }
private int postNotification(HttpClient httpClient, String sessionId) throws Exception { private int postNotification(HttpClient httpClient, String sessionId) {
var notification = HttpRequest.newBuilder() var notification = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT)) .uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT))
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
@ -567,10 +548,15 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
.header(HttpHeaders.MCP_SESSION_ID, sessionId) .header(HttpHeaders.MCP_SESSION_ID, sessionId)
.POST(HttpRequest.BodyPublishers.ofString("{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}")) .POST(HttpRequest.BodyPublishers.ofString("{\"jsonrpc\":\"2.0\",\"method\":\"notifications/initialized\"}"))
.build(); .build();
return httpClient.send(notification, HttpResponse.BodyHandlers.ofString()).statusCode(); try {
return httpClient.send(notification, HttpResponse.BodyHandlers.ofString()).statusCode();
}
catch (IOException | InterruptedException e) {
return -1;
}
} }
private String initializeSession(HttpClient httpClient) throws Exception { private String initializeSession(HttpClient httpClient) {
var initialize = HttpRequest.newBuilder() var initialize = HttpRequest.newBuilder()
.uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT)) .uri(URI.create("http://localhost:" + PORT + MESSAGE_ENDPOINT))
.header("Content-Type", "application/json") .header("Content-Type", "application/json")
@ -581,9 +567,15 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
"clientInfo":{"name":"test-client","version":"1.0.0"}}}""")) "clientInfo":{"name":"test-client","version":"1.0.0"}}}"""))
.build(); .build();
var response = httpClient.send(initialize, HttpResponse.BodyHandlers.ofString()); HttpResponse<String> response = null;
try {
response = httpClient.send(initialize, HttpResponse.BodyHandlers.ofString());
}
catch (IOException | InterruptedException e) {
return null;
}
assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_OK); assertThat(response.statusCode()).isEqualTo(HttpServletResponse.SC_OK);
return response.headers().firstValue(HttpHeaders.MCP_SESSION_ID).orElseThrow(); return response.headers().firstValue(HttpHeaders.MCP_SESSION_ID).orElse(null);
} }
/** /**
@ -641,4 +633,17 @@ class HttpServletStreamableIntegrationTests extends AbstractMcpClientServerInteg
} }
} }
private void withTransportProvider(Duration keepAliveInterval, Duration sessionSweepInterval) {
mcpServerTransportProvider.closeGracefully().block();
mcpServerTransportProvider = HttpServletStreamableServerTransportProvider.builder()
.contextExtractor(TEST_CONTEXT_EXTRACTOR)
.mcpEndpoint(MESSAGE_ENDPOINT)
.keepAliveInterval(keepAliveInterval)
// no sweeping: the session must survive every keep-alive round
.sessionSweepInterval(sessionSweepInterval)
.build();
MCP_SERVLET.setDelegate(mcpServerTransportProvider);
prepareAsyncServerBuilder().serverInfo("test-server", "1.0.0").build();
}
} }