diff --git a/test/jdk/java/net/httpclient/CancelRequestTest.java b/test/jdk/java/net/httpclient/CancelRequestTest.java index 32ca1337162..c7a39a35379 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(); }