|
| 1 | +package org.a2aproject.sdk.client.transport.rest; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 5 | + |
| 6 | +import java.io.BufferedReader; |
| 7 | +import java.io.IOException; |
| 8 | +import java.io.InputStreamReader; |
| 9 | +import java.net.InetAddress; |
| 10 | +import java.net.ServerSocket; |
| 11 | +import java.net.Socket; |
| 12 | +import java.nio.charset.StandardCharsets; |
| 13 | +import java.util.ArrayList; |
| 14 | +import java.util.List; |
| 15 | +import java.util.Locale; |
| 16 | +import java.util.Map; |
| 17 | +import java.util.concurrent.CompletableFuture; |
| 18 | +import java.util.concurrent.CountDownLatch; |
| 19 | +import java.util.concurrent.ExecutorService; |
| 20 | +import java.util.concurrent.Executors; |
| 21 | +import java.util.concurrent.Future; |
| 22 | +import java.util.concurrent.TimeUnit; |
| 23 | +import java.util.concurrent.atomic.AtomicReference; |
| 24 | +import java.util.function.Consumer; |
| 25 | + |
| 26 | +import org.a2aproject.sdk.client.http.A2AHttpClient; |
| 27 | +import org.a2aproject.sdk.client.http.A2AHttpResponse; |
| 28 | +import org.a2aproject.sdk.client.http.JdkA2AHttpClient; |
| 29 | +import org.a2aproject.sdk.client.http.ServerSentEvent; |
| 30 | +import org.a2aproject.sdk.spec.AgentCapabilities; |
| 31 | +import org.a2aproject.sdk.spec.AgentCard; |
| 32 | +import org.a2aproject.sdk.spec.AgentInterface; |
| 33 | +import org.a2aproject.sdk.spec.Message; |
| 34 | +import org.a2aproject.sdk.spec.MessageSendParams; |
| 35 | +import org.a2aproject.sdk.spec.StreamingEventKind; |
| 36 | +import org.a2aproject.sdk.spec.TaskIdParams; |
| 37 | +import org.a2aproject.sdk.spec.TextPart; |
| 38 | +import org.a2aproject.sdk.spec.TransportProtocol; |
| 39 | +import org.junit.jupiter.api.Test; |
| 40 | + |
| 41 | +class RestTransportCloseTest { |
| 42 | + |
| 43 | + private static final AgentInterface INTERFACE = new AgentInterface(TransportProtocol.HTTP_JSON.asString(), "http://localhost:4001"); |
| 44 | + private static final AgentCard CARD = AgentCard.builder().name("Test Agent").description("Test Agent") |
| 45 | + .version("1.0").supportedInterfaces(List.of(INTERFACE)) |
| 46 | + .capabilities(AgentCapabilities.builder().streaming(true).build()) |
| 47 | + .defaultInputModes(List.of("text")).defaultOutputModes(List.of("text")).skills(List.of()).build(); |
| 48 | + |
| 49 | + @Test |
| 50 | + void closeCancelsAllActiveStreamsAndIsIdempotent() throws Exception { |
| 51 | + PendingHttpClient http = new PendingHttpClient(); |
| 52 | + RestTransport transport = new RestTransport(http, CARD, INTERFACE, null); |
| 53 | + sendStreaming(transport, event -> {}); |
| 54 | + transport.subscribeToTask(new TaskIdParams("task-1234"), event -> {}, error -> {}, null); |
| 55 | + |
| 56 | + transport.close(); |
| 57 | + transport.close(); |
| 58 | + |
| 59 | + assertEquals(2, http.requests.size()); |
| 60 | + for (CountingFuture request : http.requests) { |
| 61 | + assertTrue(request.isCancelled(), "Closing REST must cancel each active HTTP stream"); |
| 62 | + assertEquals(1, request.cancellations, "Repeated close must not cancel a request again"); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void closeDoesNotCancelCompletedStreams() throws Exception { |
| 68 | + PendingHttpClient http = new PendingHttpClient(); |
| 69 | + RestTransport transport = new RestTransport(http, CARD, INTERFACE, null); |
| 70 | + sendStreaming(transport, event -> {}); |
| 71 | + CountingFuture request = http.requests.get(0); |
| 72 | + request.complete(null); |
| 73 | + |
| 74 | + transport.close(); |
| 75 | + |
| 76 | + assertEquals(0, request.cancellations, "Completed streams must be removed from the active requests"); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void closeDoesNotRetainStreamsCompletedDuringSetup() throws Exception { |
| 81 | + PendingHttpClient http = new PendingHttpClient(); |
| 82 | + RestTransport transport = new RestTransport(http, CARD, INTERFACE, null); |
| 83 | + http.duringSetup = () -> http.requests.get(0).complete(null); |
| 84 | + |
| 85 | + sendStreaming(transport, event -> {}); |
| 86 | + transport.close(); |
| 87 | + |
| 88 | + assertEquals(0, http.requests.get(0).cancellations, "Synchronous completion must not leave a tracked stream"); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + void closeDuringRequestSetupCancelsTheLateRegisteredStream() throws Exception { |
| 93 | + PendingHttpClient http = new PendingHttpClient(); |
| 94 | + RestTransport transport = new RestTransport(http, CARD, INTERFACE, null); |
| 95 | + http.duringSetup = () -> CompletableFuture.runAsync(transport::close).orTimeout(5, TimeUnit.SECONDS).join(); |
| 96 | + |
| 97 | + sendStreaming(transport, event -> {}); |
| 98 | + |
| 99 | + assertTrue(http.requests.get(0).isCancelled(), "Close must not lose a future returned after close began"); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + void closeReleasesTheRealJdkHttpConnection() throws Exception { |
| 104 | + ExecutorService executor = Executors.newSingleThreadExecutor(); |
| 105 | + AtomicReference<Socket> accepted = new AtomicReference<>(); |
| 106 | + try (ServerSocket server = new ServerSocket(0, 1, InetAddress.getByName("127.0.0.1"))) { |
| 107 | + server.setSoTimeout(10000); |
| 108 | + Future<Integer> connectionEnd = executor.submit(() -> { |
| 109 | + try (Socket socket = server.accept()) { |
| 110 | + accepted.set(socket); |
| 111 | + socket.setSoTimeout(10000); |
| 112 | + BufferedReader input = new BufferedReader(new InputStreamReader(socket.getInputStream(), StandardCharsets.UTF_8)); |
| 113 | + int contentLength = 0; |
| 114 | + for (String line; (line = input.readLine()) != null && !line.isEmpty();) { |
| 115 | + if (line.toLowerCase(Locale.ROOT).startsWith("content-length:")) { |
| 116 | + contentLength = Integer.parseInt(line.substring(line.indexOf(':') + 1).trim()); |
| 117 | + } |
| 118 | + } |
| 119 | + for (int i = 0; i < contentLength; i++) { |
| 120 | + if (input.read() == -1) { |
| 121 | + throw new IOException("Request ended before its body"); |
| 122 | + } |
| 123 | + } |
| 124 | + String event = "data: {\"task\":{\"id\":\"task-1234\",\"contextId\":\"context-1234\"," |
| 125 | + + "\"status\":{\"state\":\"TASK_STATE_WORKING\"}}}\n\n"; |
| 126 | + byte[] data = event.getBytes(StandardCharsets.UTF_8); |
| 127 | + String headers = "HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nTransfer-Encoding: chunked\r\n\r\n"; |
| 128 | + socket.getOutputStream().write(headers.getBytes(StandardCharsets.US_ASCII)); |
| 129 | + socket.getOutputStream().write((Integer.toHexString(data.length) + "\r\n").getBytes(StandardCharsets.US_ASCII)); |
| 130 | + socket.getOutputStream().write(data); |
| 131 | + socket.getOutputStream().write("\r\n".getBytes(StandardCharsets.US_ASCII)); |
| 132 | + socket.getOutputStream().flush(); |
| 133 | + // Keep the response open. EOF here proves the client released the upstream socket. |
| 134 | + return input.read(); |
| 135 | + } |
| 136 | + }); |
| 137 | + AgentInterface endpoint = new AgentInterface(TransportProtocol.HTTP_JSON.asString(), "http://127.0.0.1:" + server.getLocalPort()); |
| 138 | + RestTransport transport = new RestTransport(new JdkA2AHttpClient(), CARD, endpoint, null); |
| 139 | + CountDownLatch received = new CountDownLatch(1); |
| 140 | + try { |
| 141 | + sendStreaming(transport, event -> received.countDown()); |
| 142 | + assertTrue(received.await(5, TimeUnit.SECONDS), "The stream must be active before close"); |
| 143 | + |
| 144 | + transport.close(); |
| 145 | + |
| 146 | + assertEquals(-1, connectionEnd.get(5, TimeUnit.SECONDS), "Close must terminate the real HTTP connection"); |
| 147 | + } finally { |
| 148 | + transport.close(); |
| 149 | + Socket socket = accepted.get(); |
| 150 | + if (socket != null) { |
| 151 | + socket.close(); |
| 152 | + } |
| 153 | + } |
| 154 | + } finally { |
| 155 | + executor.shutdownNow(); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + private static void sendStreaming(RestTransport transport, Consumer<StreamingEventKind> consumer) |
| 160 | + throws Exception { |
| 161 | + Message message = Message.builder().role(Message.Role.ROLE_USER).messageId("message-1234") |
| 162 | + .parts(List.of(new TextPart("hello"))).build(); |
| 163 | + transport.sendMessageStreaming(MessageSendParams.builder().message(message).build(), consumer, error -> {}, null); |
| 164 | + } |
| 165 | + |
| 166 | + private static class CountingFuture extends CompletableFuture<Void> { |
| 167 | + private int cancellations; |
| 168 | + |
| 169 | + @Override |
| 170 | + public boolean cancel(boolean mayInterruptIfRunning) { |
| 171 | + cancellations++; |
| 172 | + return super.cancel(mayInterruptIfRunning); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + private static class PendingHttpClient extends JdkA2AHttpClient { |
| 177 | + private final List<CountingFuture> requests = new ArrayList<>(); |
| 178 | + private Runnable duringSetup = () -> {}; |
| 179 | + |
| 180 | + @Override |
| 181 | + public PostBuilder createPost() { |
| 182 | + return new A2AHttpClient.PostBuilder() { |
| 183 | + @Override |
| 184 | + public PostBuilder url(String url) { |
| 185 | + return this; |
| 186 | + } |
| 187 | + @Override |
| 188 | + public PostBuilder addHeaders(Map<String, String> headers) { |
| 189 | + return this; |
| 190 | + } |
| 191 | + @Override |
| 192 | + public PostBuilder addHeader(String name, String value) { |
| 193 | + return this; |
| 194 | + } |
| 195 | + @Override |
| 196 | + public PostBuilder body(String body) { |
| 197 | + return this; |
| 198 | + } |
| 199 | + @Override |
| 200 | + public A2AHttpResponse post() { |
| 201 | + throw new UnsupportedOperationException(); |
| 202 | + } |
| 203 | + @Override |
| 204 | + public CompletableFuture<Void> postAsyncSSE(Consumer<ServerSentEvent> messages, |
| 205 | + Consumer<Throwable> errors, Runnable complete) { |
| 206 | + CountingFuture request = new CountingFuture(); |
| 207 | + requests.add(request); |
| 208 | + duringSetup.run(); |
| 209 | + return request; |
| 210 | + } |
| 211 | + }; |
| 212 | + } |
| 213 | + } |
| 214 | +} |
0 commit comments