8364786: Test java/net/vthread/HttpALot.java intermittently fails - 24999 handled, expected 25000

Reviewed-by: dfuchs, alanb, vyazici
This commit is contained in:
Jaikiran Pai 2025-08-09 02:00:58 +00:00
parent 8ad1fcc48a
commit f83454cd61

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -21,7 +21,7 @@
* questions.
*/
/**
/*
* @test
* @bug 8284161
* @summary Stress test the HTTP protocol handler and HTTP server
@ -44,6 +44,7 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;
@ -52,6 +53,8 @@ import jdk.test.lib.net.URIBuilder;
public class HttpALot {
private static final String HELLO = "Hello";
public static void main(String[] args) throws Exception {
int requests = 25_000;
if (args.length > 0) {
@ -65,12 +68,20 @@ public class HttpALot {
InetAddress lb = InetAddress.getLoopbackAddress();
HttpServer server = HttpServer.create(new InetSocketAddress(lb, 0), 1024);
ThreadFactory factory = Thread.ofVirtual().factory();
server.setExecutor(Executors.newThreadPerTaskExecutor(factory));
final ExecutorService serverExecutor = Executors.newThreadPerTaskExecutor(factory);
server.setExecutor(serverExecutor);
server.createContext("/hello", e -> {
byte[] response = "Hello".getBytes("UTF-8");
e.sendResponseHeaders(200, response.length);
try (OutputStream out = e.getResponseBody()) {
out.write(response);
try {
byte[] response = HELLO.getBytes("UTF-8");
e.sendResponseHeaders(200, response.length);
try (OutputStream out = e.getResponseBody()) {
out.write(response);
}
} catch (Throwable t) {
System.err.println("failed to handle request " + e.getRequestURI()
+ " due to: " + t);
t.printStackTrace();
throw t; // let it propagate
}
requestsHandled.incrementAndGet();
});
@ -85,15 +96,21 @@ public class HttpALot {
// go
server.start();
try {
factory = Thread.ofVirtual().name("fetcher-", 0).factory();
try (var executor = Executors.newThreadPerTaskExecutor(factory)) {
for (int i = 1; i <= requests; i++) {
executor.submit(() -> fetch(url)).get();
try (serverExecutor) {
try {
factory = Thread.ofVirtual().name("fetcher-", 0).factory();
try (var executor = Executors.newThreadPerTaskExecutor(factory)) {
for (int i = 1; i <= requests; i++) {
final String actual = executor.submit(() -> fetch(url)).get();
if (!HELLO.equals(actual)) {
throw new RuntimeException("unexpected response: \"" + actual
+ "\" for request " + i);
}
}
}
} finally {
server.stop(1);
}
} finally {
server.stop(1);
}
if (requestsHandled.get() < requests) {