|
| 1 | +/* |
| 2 | + * Copyright 2026-2026 the original author or authors. |
| 3 | + */ |
| 4 | + |
| 5 | +package io.modelcontextprotocol.client.transport; |
| 6 | + |
| 7 | +import java.io.IOException; |
| 8 | +import java.net.InetSocketAddress; |
| 9 | +import java.net.URI; |
| 10 | +import java.net.http.HttpClient; |
| 11 | +import java.net.http.HttpRequest; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 14 | +import java.util.concurrent.CountDownLatch; |
| 15 | +import java.util.concurrent.ExecutorService; |
| 16 | +import java.util.concurrent.Executors; |
| 17 | +import java.util.concurrent.TimeUnit; |
| 18 | + |
| 19 | +import com.sun.net.httpserver.HttpServer; |
| 20 | +import org.junit.jupiter.api.AfterEach; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | +import reactor.core.Disposable; |
| 23 | +import reactor.core.publisher.Hooks; |
| 24 | + |
| 25 | +import static org.assertj.core.api.Assertions.assertThat; |
| 26 | + |
| 27 | +class ResponseBodyHandlersSendAsyncTests { |
| 28 | + |
| 29 | + private final ExecutorService executor = Executors.newCachedThreadPool(); |
| 30 | + |
| 31 | + private final CountDownLatch releaseResponse = new CountDownLatch(1); |
| 32 | + |
| 33 | + private final List<Throwable> dropped = new CopyOnWriteArrayList<>(); |
| 34 | + |
| 35 | + private HttpServer server; |
| 36 | + |
| 37 | + @AfterEach |
| 38 | + void tearDown() { |
| 39 | + Hooks.resetOnErrorDropped(); |
| 40 | + this.releaseResponse.countDown(); |
| 41 | + if (this.server != null) { |
| 42 | + this.server.stop(0); |
| 43 | + } |
| 44 | + this.executor.shutdownNow(); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void cancellingBeforeTheResponseArrivesDropsNoError() throws Exception { |
| 49 | + CountDownLatch requestReceived = new CountDownLatch(1); |
| 50 | + this.server = HttpServer.create(new InetSocketAddress("127.0.0.1", 0), 0); |
| 51 | + this.server.setExecutor(this.executor); |
| 52 | + this.server.createContext("/", exchange -> { |
| 53 | + // Never responds, so that the exchange is cancelled while awaiting headers. |
| 54 | + requestReceived.countDown(); |
| 55 | + try { |
| 56 | + this.releaseResponse.await(); |
| 57 | + } |
| 58 | + catch (InterruptedException e) { |
| 59 | + Thread.currentThread().interrupt(); |
| 60 | + } |
| 61 | + exchange.close(); |
| 62 | + }); |
| 63 | + this.server.start(); |
| 64 | + Hooks.onErrorDropped(this.dropped::add); |
| 65 | + |
| 66 | + HttpRequest request = HttpRequest |
| 67 | + .newBuilder(URI.create("http://127.0.0.1:" + this.server.getAddress().getPort() + "/")) |
| 68 | + .build(); |
| 69 | + Disposable exchange = ResponseBodyHandlers.sendAsync(HttpClient.newHttpClient(), request).subscribe(); |
| 70 | + assertThat(requestReceived.await(5, TimeUnit.SECONDS)).isTrue(); |
| 71 | + |
| 72 | + // The HttpClient fails the aborted exchange within cancel() itself. |
| 73 | + exchange.dispose(); |
| 74 | + |
| 75 | + assertThat(this.dropped).isEmpty(); |
| 76 | + } |
| 77 | + |
| 78 | +} |
0 commit comments