|
6 | 6 |
|
7 | 7 | import java.io.ByteArrayInputStream; |
8 | 8 | import java.io.InputStream; |
| 9 | +import java.io.PipedInputStream; |
| 10 | +import java.io.PipedOutputStream; |
9 | 11 | import java.net.URI; |
10 | 12 | import java.net.http.HttpClient; |
11 | 13 | import java.net.http.HttpHeaders; |
12 | 14 | import java.net.http.HttpRequest; |
13 | 15 | import java.net.http.HttpResponse; |
| 16 | +import java.nio.charset.StandardCharsets; |
14 | 17 | import java.util.List; |
15 | 18 | import java.util.Map; |
| 19 | +import java.util.concurrent.BlockingQueue; |
16 | 20 | import java.util.concurrent.CompletableFuture; |
17 | 21 | import java.util.concurrent.CountDownLatch; |
| 22 | +import java.util.concurrent.LinkedBlockingQueue; |
18 | 23 | import java.util.concurrent.TimeUnit; |
19 | 24 | import java.util.concurrent.atomic.AtomicInteger; |
20 | 25 | import java.util.stream.Collectors; |
@@ -161,6 +166,146 @@ void concurrentSessionLoadsReuseInFlightSessionStreamOpen() throws Exception { |
161 | 166 | loads.get(1, TimeUnit.SECONDS); |
162 | 167 | } |
163 | 168 |
|
| 169 | + @Test |
| 170 | + void sessionNewResponseDoesNotBlockConnectionReaderWhileSessionStreamOpens() throws Exception { |
| 171 | + HttpClient httpClient = mock(HttpClient.class); |
| 172 | + PipedInputStream connectionStreamBody = new PipedInputStream(); |
| 173 | + PipedOutputStream connectionStreamWriter = new PipedOutputStream(connectionStreamBody); |
| 174 | + CompletableFuture<HttpResponse<InputStream>> sessionStreamResponse = new CompletableFuture<>(); |
| 175 | + BlockingQueue<AcpSchema.JSONRPCMessage> inboundMessages = new LinkedBlockingQueue<>(); |
| 176 | + |
| 177 | + when(httpClient.sendAsync(any(), any())).thenAnswer(invocation -> { |
| 178 | + HttpRequest request = invocation.getArgument(0); |
| 179 | + if ("POST".equals(request.method()) |
| 180 | + && request.headers().firstValue("Acp-Connection-Id").isEmpty()) { |
| 181 | + String initializeResponse = jsonMapper.writeValueAsString(AcpTestFixtures |
| 182 | + .createJsonRpcResponse("init-1", AcpTestFixtures.createInitializeResponse())); |
| 183 | + return CompletableFuture.completedFuture(response(200, |
| 184 | + Map.of("Content-Type", "application/json", "Acp-Connection-Id", "conn-1"), |
| 185 | + initializeResponse)); |
| 186 | + } |
| 187 | + if ("GET".equals(request.method()) |
| 188 | + && request.headers().firstValue("Acp-Session-Id").isEmpty()) { |
| 189 | + return CompletableFuture.completedFuture( |
| 190 | + response(200, Map.of("Content-Type", "text/event-stream"), connectionStreamBody)); |
| 191 | + } |
| 192 | + if ("GET".equals(request.method())) { |
| 193 | + return sessionStreamResponse; |
| 194 | + } |
| 195 | + return CompletableFuture.completedFuture(response(202, Map.of(), null)); |
| 196 | + }); |
| 197 | + |
| 198 | + StreamableHttpAcpClientTransport transport = new StreamableHttpAcpClientTransport( |
| 199 | + URI.create("https://localhost:8443/acp"), jsonMapper, httpClient); |
| 200 | + try { |
| 201 | + transport.connect(message -> message.doOnNext(inboundMessages::add).then(Mono.empty())).block(); |
| 202 | + transport.sendMessage(AcpTestFixtures.createJsonRpcRequest(AcpSchema.METHOD_INITIALIZE, "init-1", |
| 203 | + AcpTestFixtures.createInitializeRequest())) |
| 204 | + .block(); |
| 205 | + awaitResponse(inboundMessages, "init-1"); |
| 206 | + |
| 207 | + transport.sendMessage(AcpTestFixtures.createJsonRpcRequest(AcpSchema.METHOD_SESSION_NEW, "new-1", |
| 208 | + AcpTestFixtures.createNewSessionRequest("/workspace"))) |
| 209 | + .block(); |
| 210 | + transport.sendMessage(new AcpSchema.JSONRPCRequest(AcpSchema.JSONRPC_VERSION, "ping-1", |
| 211 | + "extension/ping", Map.of())) |
| 212 | + .block(); |
| 213 | + |
| 214 | + writeSse(connectionStreamWriter, |
| 215 | + AcpTestFixtures.createJsonRpcResponse("new-1", |
| 216 | + new AcpSchema.NewSessionResponse("sess-1", null, null))); |
| 217 | + writeSse(connectionStreamWriter, AcpTestFixtures.createJsonRpcResponse("ping-1", Map.of())); |
| 218 | + |
| 219 | + assertThat(awaitResponse(inboundMessages, "ping-1")).isNotNull(); |
| 220 | + assertThat(inboundMessages.stream() |
| 221 | + .filter(AcpSchema.JSONRPCResponse.class::isInstance) |
| 222 | + .map(AcpSchema.JSONRPCResponse.class::cast) |
| 223 | + .noneMatch(response -> "new-1".equals(response.id()))).isTrue(); |
| 224 | + |
| 225 | + sessionStreamResponse.complete(response(200, Map.of("Content-Type", "text/event-stream"), emptyBody())); |
| 226 | + assertThat(awaitResponse(inboundMessages, "new-1")).isNotNull(); |
| 227 | + } |
| 228 | + finally { |
| 229 | + connectionStreamWriter.close(); |
| 230 | + transport.close(); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + @Test |
| 235 | + void sessionNewErrorResponseIsDeliveredWithoutOpeningSessionStream() throws Exception { |
| 236 | + HttpClient httpClient = mock(HttpClient.class); |
| 237 | + PipedInputStream connectionStreamBody = new PipedInputStream(); |
| 238 | + PipedOutputStream connectionStreamWriter = new PipedOutputStream(connectionStreamBody); |
| 239 | + AtomicInteger sessionGetCount = new AtomicInteger(); |
| 240 | + BlockingQueue<AcpSchema.JSONRPCMessage> inboundMessages = new LinkedBlockingQueue<>(); |
| 241 | + |
| 242 | + when(httpClient.sendAsync(any(), any())).thenAnswer(invocation -> { |
| 243 | + HttpRequest request = invocation.getArgument(0); |
| 244 | + if ("POST".equals(request.method()) |
| 245 | + && request.headers().firstValue("Acp-Connection-Id").isEmpty()) { |
| 246 | + String initializeResponse = jsonMapper.writeValueAsString(AcpTestFixtures |
| 247 | + .createJsonRpcResponse("init-1", AcpTestFixtures.createInitializeResponse())); |
| 248 | + return CompletableFuture.completedFuture(response(200, |
| 249 | + Map.of("Content-Type", "application/json", "Acp-Connection-Id", "conn-1"), |
| 250 | + initializeResponse)); |
| 251 | + } |
| 252 | + if ("GET".equals(request.method()) |
| 253 | + && request.headers().firstValue("Acp-Session-Id").isEmpty()) { |
| 254 | + return CompletableFuture.completedFuture( |
| 255 | + response(200, Map.of("Content-Type", "text/event-stream"), connectionStreamBody)); |
| 256 | + } |
| 257 | + if ("GET".equals(request.method())) { |
| 258 | + sessionGetCount.incrementAndGet(); |
| 259 | + return CompletableFuture.completedFuture( |
| 260 | + response(200, Map.of("Content-Type", "text/event-stream"), emptyBody())); |
| 261 | + } |
| 262 | + return CompletableFuture.completedFuture(response(202, Map.of(), null)); |
| 263 | + }); |
| 264 | + |
| 265 | + StreamableHttpAcpClientTransport transport = new StreamableHttpAcpClientTransport( |
| 266 | + URI.create("https://localhost:8443/acp"), jsonMapper, httpClient); |
| 267 | + try { |
| 268 | + transport.connect(message -> message.doOnNext(inboundMessages::add).then(Mono.empty())).block(); |
| 269 | + transport.sendMessage(AcpTestFixtures.createJsonRpcRequest(AcpSchema.METHOD_INITIALIZE, "init-1", |
| 270 | + AcpTestFixtures.createInitializeRequest())) |
| 271 | + .block(); |
| 272 | + awaitResponse(inboundMessages, "init-1"); |
| 273 | + |
| 274 | + transport.sendMessage(AcpTestFixtures.createJsonRpcRequest(AcpSchema.METHOD_SESSION_NEW, "new-1", |
| 275 | + AcpTestFixtures.createNewSessionRequest("/workspace"))) |
| 276 | + .block(); |
| 277 | + writeSse(connectionStreamWriter, |
| 278 | + new AcpSchema.JSONRPCResponse(AcpSchema.JSONRPC_VERSION, "new-1", null, |
| 279 | + new AcpSchema.JSONRPCError(-32000, "agent rejected session", null))); |
| 280 | + |
| 281 | + AcpSchema.JSONRPCResponse response = awaitResponse(inboundMessages, "new-1"); |
| 282 | + assertThat(response.error()).isNotNull(); |
| 283 | + assertThat(response.error().message()).isEqualTo("agent rejected session"); |
| 284 | + assertThat(sessionGetCount).hasValue(0); |
| 285 | + } |
| 286 | + finally { |
| 287 | + connectionStreamWriter.close(); |
| 288 | + transport.close(); |
| 289 | + } |
| 290 | + } |
| 291 | + |
| 292 | + private void writeSse(PipedOutputStream writer, AcpSchema.JSONRPCMessage message) throws Exception { |
| 293 | + writer.write(("data: " + jsonMapper.writeValueAsString(message) + "\n\n").getBytes(StandardCharsets.UTF_8)); |
| 294 | + writer.flush(); |
| 295 | + } |
| 296 | + |
| 297 | + private AcpSchema.JSONRPCResponse awaitResponse(BlockingQueue<AcpSchema.JSONRPCMessage> messages, Object id) |
| 298 | + throws Exception { |
| 299 | + long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(2); |
| 300 | + while (System.nanoTime() < deadline) { |
| 301 | + AcpSchema.JSONRPCMessage message = messages.poll(50, TimeUnit.MILLISECONDS); |
| 302 | + if (message instanceof AcpSchema.JSONRPCResponse response && id.equals(response.id())) { |
| 303 | + return response; |
| 304 | + } |
| 305 | + } |
| 306 | + throw new AssertionError("Timed out waiting for response " + id); |
| 307 | + } |
| 308 | + |
164 | 309 | private InputStream emptyBody() { |
165 | 310 | return new ByteArrayInputStream(new byte[0]); |
166 | 311 | } |
|
0 commit comments