Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 31 additions & 14 deletions test/jdk/java/net/vthread/HttpALot.java
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -21,7 +21,7 @@
* questions.
*/

/**
/*
* @test
* @bug 8284161
* @summary Stress test the HTTP protocol handler and HTTP server
Expand All @@ -44,6 +44,7 @@
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;
Expand All @@ -52,6 +53,8 @@

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) {
Expand All @@ -65,12 +68,20 @@ public static void main(String[] args) throws Exception {
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();
});
Expand All @@ -85,15 +96,21 @@ public static void main(String[] args) throws Exception {

// 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) {
Expand Down