From 17eb133fe2d004d425308216be8366a2351f703e Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Fri, 24 Oct 2025 16:52:56 +0200 Subject: [PATCH 1/3] backport a74ebd048ae569296619c112c23169c46b571863 --- .../net/httpclient/CancelRequestTest.java | 51 +++++++++++++------ 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/test/jdk/java/net/httpclient/CancelRequestTest.java b/test/jdk/java/net/httpclient/CancelRequestTest.java index aaec8d52f7b..32ca1337162 100644 --- a/test/jdk/java/net/httpclient/CancelRequestTest.java +++ b/test/jdk/java/net/httpclient/CancelRequestTest.java @@ -86,6 +86,7 @@ import static java.lang.System.arraycopy; import static java.lang.System.out; +import static java.lang.System.err; import static java.net.http.HttpClient.Version.HTTP_1_1; import static java.net.http.HttpClient.Version.HTTP_2; import static java.nio.charset.StandardCharsets.UTF_8; @@ -356,14 +357,22 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup } catch (ExecutionException x) { assertEquals(response.isDone(), true); Throwable wrapped = x.getCause(); - assertTrue(CancellationException.class.isAssignableFrom(wrapped.getClass())); - Throwable cause = wrapped.getCause(); - out.println("CancellationException cause: " + x); - assertTrue(IOException.class.isAssignableFrom(cause.getClass())); - if (cause instanceof HttpConnectTimeoutException) { + Throwable cause = wrapped; + if (mayInterruptIfRunning) { + assertTrue(CancellationException.class.isAssignableFrom(wrapped.getClass()), + "Unexpected exception: " + wrapped); + cause = wrapped.getCause(); + out.println("CancellationException cause: " + x); + if (cause instanceof HttpConnectTimeoutException) { + cause.printStackTrace(out); + throw new RuntimeException("Unexpected timeout exception", cause); + } + } + if (!IOException.class.isInstance(cause)) { + out.println("Unexpected cause: " + cause.getClass()); cause.printStackTrace(out); - throw new RuntimeException("Unexpected timeout exception", cause); } + assertTrue(IOException.class.isAssignableFrom(cause.getClass())); if (mayInterruptIfRunning) { out.println("Got expected exception: " + wrapped); out.println("\tcause: " + cause); @@ -381,7 +390,7 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup assertEquals(cf2.isCancelled(), false); assertEquals(latch.getCount(), 0); - var error = TRACKER.check(tracker, 200, + var error = TRACKER.check(tracker, 1000, (t) -> t.getOutstandingOperations() > 0 || t.getOutstandingSubscribers() > 0, "subscribers for testGetSendAsync(%s)\n\t step [%s]".formatted(req.uri(), i), false); @@ -493,7 +502,7 @@ public Iterator iterator() { assertEquals(cf2.isCancelled(), false); assertEquals(latch.getCount(), 0); - var error = TRACKER.check(tracker, 200, + var error = TRACKER.check(tracker, 1000, (t) -> t.getOutstandingOperations() > 0 || t.getOutstandingSubscribers() > 0, "subscribers for testPostSendAsync(%s)\n\t step [%s]".formatted(req.uri(), i), false); @@ -515,9 +524,11 @@ public void testPostInterrupt(String uri, boolean sameClient) Thread main = Thread.currentThread(); CompletableFuture interruptingThread = new CompletableFuture<>(); + var uriStr = uri + "/post/req=" + i; Runnable interrupt = () -> { Thread current = Thread.currentThread(); - out.printf("%s Interrupting main from: %s (%s)", now(), current, uri); + out.printf("%s Interrupting main from: %s (%s)%n", now(), current, uriStr); + err.printf("%s Interrupting main from: %s (%s)%n", now(), current, uriStr); interruptingThread.complete(current); main.interrupt(); }; @@ -528,36 +539,44 @@ public void testPostInterrupt(String uri, boolean sameClient) return List.of(BODY.getBytes(UTF_8)).iterator(); }; - HttpRequest req = HttpRequest.newBuilder(URI.create(uri)) + HttpRequest req = HttpRequest.newBuilder(URI.create(uriStr)) .POST(HttpRequest.BodyPublishers.ofByteArrays(iterable)) .build(); String body = null; Exception failed = null; try { + out.println("Sending: " + uriStr); body = client.send(req, BodyHandlers.ofString()).body(); } catch (Exception x) { failed = x; } - + out.println(uriStr + ": got result or exception"); if (failed instanceof InterruptedException) { - out.println("Got expected exception: " + failed); + out.println(uriStr + ": Got expected exception: " + failed); } else if (failed instanceof IOException) { + out.println(uriStr + ": got IOException: " + failed); // that could be OK if the main thread was interrupted // from the main thread: the interrupt status could have // been caught by writing to the socket from the main // thread. - if (interruptingThread.get() == main) { - out.println("Accepting IOException: " + failed); + if (interruptingThread.isDone() && interruptingThread.get() == main) { + out.println(uriStr + ": Accepting IOException: " + failed); failed.printStackTrace(out); } else { + out.println(uriStr + ": unexpected exception: " + failed); throw failed; } } else if (failed != null) { - assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); + out.println(uriStr + ": unexpected exception: " + failed); throw failed; + } else { + assert failed == null; + out.println(uriStr + ": got body: " + body); + assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); } + out.println("next iteration"); - var error = TRACKER.check(tracker, 200, + var error = TRACKER.check(tracker, 1000, (t) -> t.getOutstandingOperations() > 0 || t.getOutstandingSubscribers() > 0, "subscribers for testPostInterrupt(%s)\n\t step [%s]".formatted(req.uri(), i), false); From 0cabc09344259cd64c81948a62053a2b60e6e401 Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Sun, 26 Oct 2025 19:55:18 +0100 Subject: [PATCH 2/3] backport 4f864faf428c8171be975a79db5bc2bc145f8805 --- .../net/httpclient/CancelRequestTest.java | 59 +++++++++---------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/test/jdk/java/net/httpclient/CancelRequestTest.java b/test/jdk/java/net/httpclient/CancelRequestTest.java index 32ca1337162..7851b112498 100644 --- a/test/jdk/java/net/httpclient/CancelRequestTest.java +++ b/test/jdk/java/net/httpclient/CancelRequestTest.java @@ -35,9 +35,6 @@ */ // * -Dseed=3582896013206826205L // * -Dseed=5784221742235559231L -import com.sun.net.httpserver.HttpServer; -import com.sun.net.httpserver.HttpsConfigurator; -import com.sun.net.httpserver.HttpsServer; import jdk.internal.net.http.common.OperationTrackers.Tracker; import jdk.test.lib.RandomFactory; import jdk.test.lib.net.SimpleSSLContext; @@ -56,8 +53,6 @@ import java.io.InputStream; import java.io.OutputStream; import java.lang.ref.Reference; -import java.net.InetAddress; -import java.net.InetSocketAddress; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpConnectTimeoutException; @@ -80,17 +75,15 @@ import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicReference; import java.util.stream.Collectors; -import java.util.stream.Stream; import jdk.httpclient.test.lib.common.HttpServerAdapters; -import jdk.httpclient.test.lib.http2.Http2TestServer; -import static java.lang.System.arraycopy; import static java.lang.System.out; import static java.lang.System.err; import static java.net.http.HttpClient.Version.HTTP_1_1; import static java.net.http.HttpClient.Version.HTTP_2; import static java.nio.charset.StandardCharsets.UTF_8; import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; public class CancelRequestTest implements HttpServerAdapters { @@ -179,19 +172,19 @@ void beforeMethod(ITestContext context) { } @AfterClass - static final void printFailedTests(ITestContext context) { + static void printFailedTests(ITestContext context) { out.println("\n========================="); var failed = context.getFailedTests().getAllResults().stream() - .collect(Collectors.toMap(r -> name(r), ITestResult::getThrowable)); + .collect(Collectors.toMap(CancelRequestTest::name, ITestResult::getThrowable)); FAILURES.putAll(failed); try { out.printf("%n%sCreated %d servers and %d clients%n", now(), serverCount.get(), clientCount.get()); if (FAILURES.isEmpty()) return; out.println("Failed tests: "); - FAILURES.entrySet().forEach((e) -> { - out.printf("\t%s: %s%n", e.getKey(), e.getValue()); - e.getValue().printStackTrace(out); + FAILURES.forEach((key, value) -> { + out.printf("\t%s: %s%n", key, value); + value.printStackTrace(out); }); if (tasksFailed) { System.out.println("WARNING: Some tasks failed"); @@ -327,7 +320,7 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup out.println("cf2 after cancel: " + cf2); try { String body = cf2.get().body(); - assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); + assertEquals(body, String.join("", BODY.split("\\|"))); throw new AssertionError("Expected CancellationException not received"); } catch (ExecutionException x) { out.println("Got expected exception: " + x); @@ -348,14 +341,14 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup // completed yet - so wait for it here... try { String body = response.get().body(); - assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); + assertEquals(body, String.join("", BODY.split("\\|"))); if (mayInterruptIfRunning) { // well actually - this could happen... In which case we'll need to // increase the latency in the server handler... throw new AssertionError("Expected Exception not received"); } } catch (ExecutionException x) { - assertEquals(response.isDone(), true); + assertTrue(response.isDone()); Throwable wrapped = x.getCause(); Throwable cause = wrapped; if (mayInterruptIfRunning) { @@ -383,11 +376,11 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup } } - assertEquals(response.isDone(), true); - assertEquals(response.isCancelled(), false); + assertTrue(response.isDone()); + assertFalse(response.isCancelled()); assertEquals(cf1.isCancelled(), hasCancellationException); - assertEquals(cf2.isDone(), true); - assertEquals(cf2.isCancelled(), false); + assertTrue(cf2.isDone()); + assertFalse(cf2.isCancelled()); assertEquals(latch.getCount(), 0); var error = TRACKER.check(tracker, 1000, @@ -397,6 +390,8 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup Reference.reachabilityFence(client); if (error != null) throw error; } + assert client != null; + if (!sameClient) client.close(); } @Test(dataProvider = "asyncurls") @@ -413,7 +408,7 @@ public void testPostSendAsync(String uri, boolean sameClient, boolean mayInterru CompletableFuture> cancelFuture = new CompletableFuture<>(); - Iterable iterable = new Iterable() { + Iterable iterable = new Iterable<>() { @Override public Iterator iterator() { // this is dangerous @@ -448,7 +443,7 @@ public Iterator iterator() { out.println("cf2 after cancel: " + cf2); try { String body = cf2.get().body(); - assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); + assertEquals(body, String.join("", BODY.split("\\|"))); throw new AssertionError("Expected CancellationException not received"); } catch (ExecutionException x) { out.println("Got expected exception: " + x); @@ -469,14 +464,14 @@ public Iterator iterator() { // completed yet - so wait for it here... try { String body = response.get().body(); - assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); + assertEquals(body, String.join("", BODY.split("\\|"))); if (mayInterruptIfRunning) { // well actually - this could happen... In which case we'll need to // increase the latency in the server handler... throw new AssertionError("Expected Exception not received"); } } catch (ExecutionException x) { - assertEquals(response.isDone(), true); + assertTrue(response.isDone()); Throwable wrapped = x.getCause(); assertTrue(CancellationException.class.isAssignableFrom(wrapped.getClass())); Throwable cause = wrapped.getCause(); @@ -495,11 +490,11 @@ public Iterator iterator() { } } - assertEquals(response.isDone(), true); - assertEquals(response.isCancelled(), false); + assertTrue(response.isDone()); + assertFalse(response.isCancelled()); assertEquals(cf1.isCancelled(), hasCancellationException); - assertEquals(cf2.isDone(), true); - assertEquals(cf2.isCancelled(), false); + assertTrue(cf2.isDone()); + assertFalse(cf2.isCancelled()); assertEquals(latch.getCount(), 0); var error = TRACKER.check(tracker, 1000, @@ -509,6 +504,8 @@ public Iterator iterator() { Reference.reachabilityFence(client); if (error != null) throw error; } + assert client != null; + if (!sameClient) client.close(); } @Test(dataProvider = "urls") @@ -572,17 +569,19 @@ public void testPostInterrupt(String uri, boolean sameClient) } else { assert failed == null; out.println(uriStr + ": got body: " + body); - assertEquals(body, Stream.of(BODY.split("\\|")).collect(Collectors.joining())); + assertEquals(body, String.join("", BODY.split("\\|"))); } out.println("next iteration"); - var error = TRACKER.check(tracker, 1000, + var error = TRACKER.check(tracker, 2000, (t) -> t.getOutstandingOperations() > 0 || t.getOutstandingSubscribers() > 0, "subscribers for testPostInterrupt(%s)\n\t step [%s]".formatted(req.uri(), i), false); Reference.reachabilityFence(client); if (error != null) throw error; } + assert client != null; + if (!sameClient) client.close(); } From 4b6806f6cdbff1387961f25273d301762fd8e9e9 Mon Sep 17 00:00:00 2001 From: Goetz Lindenmaier Date: Sun, 26 Oct 2025 20:05:28 +0100 Subject: [PATCH 3/3] HttpClient not ASutoClosable in 17 --- test/jdk/java/net/httpclient/CancelRequestTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/jdk/java/net/httpclient/CancelRequestTest.java b/test/jdk/java/net/httpclient/CancelRequestTest.java index 7851b112498..c7a39a35379 100644 --- a/test/jdk/java/net/httpclient/CancelRequestTest.java +++ b/test/jdk/java/net/httpclient/CancelRequestTest.java @@ -391,7 +391,7 @@ public void testGetSendAsync(String uri, boolean sameClient, boolean mayInterrup if (error != null) throw error; } assert client != null; - if (!sameClient) client.close(); + //if (!sameClient) client.close(); } @Test(dataProvider = "asyncurls") @@ -505,7 +505,7 @@ public Iterator iterator() { if (error != null) throw error; } assert client != null; - if (!sameClient) client.close(); + //if (!sameClient) client.close(); } @Test(dataProvider = "urls") @@ -581,7 +581,7 @@ public void testPostInterrupt(String uri, boolean sameClient) if (error != null) throw error; } assert client != null; - if (!sameClient) client.close(); + //if (!sameClient) client.close(); }