|
| 1 | +/* |
| 2 | + * Copyright 2026-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.server.transport; |
| 6 | + |
| 7 | +import java.lang.reflect.Field; |
| 8 | +import java.time.Duration; |
| 9 | +import java.util.Map; |
| 10 | +import java.util.concurrent.atomic.AtomicInteger; |
| 11 | + |
| 12 | +import io.modelcontextprotocol.spec.McpSchema; |
| 13 | +import io.modelcontextprotocol.spec.McpStreamableServerSession; |
| 14 | +import io.modelcontextprotocol.spec.json.gson.GsonMcpJsonMapper; |
| 15 | +import org.junit.jupiter.api.Test; |
| 16 | +import reactor.core.publisher.Mono; |
| 17 | + |
| 18 | +import static org.assertj.core.api.Assertions.assertThat; |
| 19 | + |
| 20 | +/** |
| 21 | + * Tests for keep-alive failure eviction in |
| 22 | + * {@link HttpServletStreamableServerTransportProvider}. |
| 23 | + */ |
| 24 | +class HttpServletStreamableServerTransportProviderTests { |
| 25 | + |
| 26 | + @Test |
| 27 | + void firstKeepAliveFailureDoesNotEvictSession() throws Exception { |
| 28 | + HttpServletStreamableServerTransportProvider provider = createProvider(); |
| 29 | + TrackingStreamableSession session = createSession("session-1"); |
| 30 | + putSession(provider, session); |
| 31 | + |
| 32 | + provider.handleKeepAliveFailure(session, new RuntimeException("ping failed")); |
| 33 | + |
| 34 | + assertThat(sessions(provider)).containsEntry("session-1", session); |
| 35 | + assertThat(keepAliveFailureCounts(provider)).containsEntry("session-1", 1); |
| 36 | + assertThat(session.closeCount()).isZero(); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + void repeatedKeepAliveFailuresEvictSession() throws Exception { |
| 41 | + HttpServletStreamableServerTransportProvider provider = createProvider(); |
| 42 | + TrackingStreamableSession session = createSession("session-1"); |
| 43 | + putSession(provider, session); |
| 44 | + |
| 45 | + provider.handleKeepAliveFailure(session, new RuntimeException("first failure")); |
| 46 | + provider.handleKeepAliveFailure(session, new RuntimeException("second failure")); |
| 47 | + provider.handleKeepAliveFailure(session, new RuntimeException("third failure")); |
| 48 | + |
| 49 | + assertThat(sessions(provider)).doesNotContainKey("session-1"); |
| 50 | + assertThat(keepAliveFailureCounts(provider)).doesNotContainKey("session-1"); |
| 51 | + assertThat(session.closeCount()).isOne(); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + void successfulKeepAliveResetsFailureCount() throws Exception { |
| 56 | + HttpServletStreamableServerTransportProvider provider = createProvider(); |
| 57 | + TrackingStreamableSession session = createSession("session-1"); |
| 58 | + putSession(provider, session); |
| 59 | + |
| 60 | + provider.handleKeepAliveFailure(session, new RuntimeException("first failure")); |
| 61 | + provider.handleKeepAliveFailure(session, new RuntimeException("second failure")); |
| 62 | + provider.resetKeepAliveFailures(session); |
| 63 | + provider.handleKeepAliveFailure(session, new RuntimeException("failure after success")); |
| 64 | + |
| 65 | + assertThat(sessions(provider)).containsEntry("session-1", session); |
| 66 | + assertThat(keepAliveFailureCounts(provider)).containsEntry("session-1", 1); |
| 67 | + assertThat(session.closeCount()).isZero(); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void successfulKeepAliveFromReplacedSessionDoesNotResetReplacementFailureCount() throws Exception { |
| 72 | + HttpServletStreamableServerTransportProvider provider = createProvider(); |
| 73 | + TrackingStreamableSession oldSession = createSession("session-1"); |
| 74 | + TrackingStreamableSession replacementSession = createSession("session-1"); |
| 75 | + putSession(provider, replacementSession); |
| 76 | + |
| 77 | + provider.handleKeepAliveFailure(replacementSession, new RuntimeException("replacement failure")); |
| 78 | + provider.resetKeepAliveFailures(oldSession); |
| 79 | + |
| 80 | + assertThat(sessions(provider)).containsEntry("session-1", replacementSession); |
| 81 | + assertThat(keepAliveFailureCounts(provider)).containsEntry("session-1", 1); |
| 82 | + assertThat(oldSession.closeCount()).isZero(); |
| 83 | + assertThat(replacementSession.closeCount()).isZero(); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void keepAliveFailureDoesNotCloseReplacedSession() throws Exception { |
| 88 | + HttpServletStreamableServerTransportProvider provider = createProvider(); |
| 89 | + TrackingStreamableSession oldSession = createSession("session-1"); |
| 90 | + TrackingStreamableSession replacementSession = createSession("session-1"); |
| 91 | + putSession(provider, replacementSession); |
| 92 | + |
| 93 | + provider.handleKeepAliveFailure(oldSession, new RuntimeException("first failure")); |
| 94 | + provider.handleKeepAliveFailure(oldSession, new RuntimeException("second failure")); |
| 95 | + provider.handleKeepAliveFailure(oldSession, new RuntimeException("third failure")); |
| 96 | + |
| 97 | + assertThat(sessions(provider)).containsEntry("session-1", replacementSession); |
| 98 | + assertThat(keepAliveFailureCounts(provider)).doesNotContainKey("session-1"); |
| 99 | + assertThat(oldSession.closeCount()).isZero(); |
| 100 | + assertThat(replacementSession.closeCount()).isZero(); |
| 101 | + } |
| 102 | + |
| 103 | + private HttpServletStreamableServerTransportProvider createProvider() { |
| 104 | + return HttpServletStreamableServerTransportProvider.builder().jsonMapper(new GsonMcpJsonMapper()).build(); |
| 105 | + } |
| 106 | + |
| 107 | + private TrackingStreamableSession createSession(String sessionId) { |
| 108 | + return new TrackingStreamableSession(sessionId); |
| 109 | + } |
| 110 | + |
| 111 | + private void putSession(HttpServletStreamableServerTransportProvider provider, TrackingStreamableSession session) |
| 112 | + throws Exception { |
| 113 | + sessions(provider).put(session.getId(), session); |
| 114 | + } |
| 115 | + |
| 116 | + @SuppressWarnings("unchecked") |
| 117 | + private Map<String, McpStreamableServerSession> sessions(HttpServletStreamableServerTransportProvider provider) |
| 118 | + throws Exception { |
| 119 | + Field field = HttpServletStreamableServerTransportProvider.class.getDeclaredField("sessions"); |
| 120 | + field.setAccessible(true); |
| 121 | + return (Map<String, McpStreamableServerSession>) field.get(provider); |
| 122 | + } |
| 123 | + |
| 124 | + @SuppressWarnings("unchecked") |
| 125 | + private Map<String, Integer> keepAliveFailureCounts(HttpServletStreamableServerTransportProvider provider) |
| 126 | + throws Exception { |
| 127 | + Field field = HttpServletStreamableServerTransportProvider.class.getDeclaredField("keepAliveFailureCounts"); |
| 128 | + field.setAccessible(true); |
| 129 | + return (Map<String, Integer>) field.get(provider); |
| 130 | + } |
| 131 | + |
| 132 | + private static class TrackingStreamableSession extends McpStreamableServerSession { |
| 133 | + |
| 134 | + private final AtomicInteger closeCount = new AtomicInteger(); |
| 135 | + |
| 136 | + TrackingStreamableSession(String id) { |
| 137 | + super(id, McpSchema.ClientCapabilities.builder().build(), |
| 138 | + new McpSchema.Implementation("test-client", "1.0.0"), Duration.ofSeconds(5), Map.of(), Map.of(), |
| 139 | + Mono::empty); |
| 140 | + } |
| 141 | + |
| 142 | + @Override |
| 143 | + public void close() { |
| 144 | + this.closeCount.incrementAndGet(); |
| 145 | + } |
| 146 | + |
| 147 | + int closeCount() { |
| 148 | + return this.closeCount.get(); |
| 149 | + } |
| 150 | + |
| 151 | + } |
| 152 | + |
| 153 | +} |
0 commit comments