11/*
2- * Copyright (c) 2020, 2023 , Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2020, 2025 , Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2121 * questions.
2222 */
2323
24- /**
24+ /*
2525 * @test
2626 * @bug 8284161
2727 * @summary Stress test the HTTP protocol handler and HTTP server
4444import java .net .InetSocketAddress ;
4545import java .net .Proxy ;
4646import java .net .URL ;
47+ import java .util .concurrent .ExecutorService ;
4748import java .util .concurrent .Executors ;
4849import java .util .concurrent .ThreadFactory ;
4950import java .util .concurrent .atomic .AtomicInteger ;
5253
5354public class HttpALot {
5455
56+ private static final String HELLO = "Hello" ;
57+
5558 public static void main (String [] args ) throws Exception {
5659 int requests = 25_000 ;
5760 if (args .length > 0 ) {
@@ -65,12 +68,20 @@ public static void main(String[] args) throws Exception {
6568 InetAddress lb = InetAddress .getLoopbackAddress ();
6669 HttpServer server = HttpServer .create (new InetSocketAddress (lb , 0 ), 1024 );
6770 ThreadFactory factory = Thread .ofVirtual ().factory ();
68- server .setExecutor (Executors .newThreadPerTaskExecutor (factory ));
71+ final ExecutorService serverExecutor = Executors .newThreadPerTaskExecutor (factory );
72+ server .setExecutor (serverExecutor );
6973 server .createContext ("/hello" , e -> {
70- byte [] response = "Hello" .getBytes ("UTF-8" );
71- e .sendResponseHeaders (200 , response .length );
72- try (OutputStream out = e .getResponseBody ()) {
73- out .write (response );
74+ try {
75+ byte [] response = HELLO .getBytes ("UTF-8" );
76+ e .sendResponseHeaders (200 , response .length );
77+ try (OutputStream out = e .getResponseBody ()) {
78+ out .write (response );
79+ }
80+ } catch (Throwable t ) {
81+ System .err .println ("failed to handle request " + e .getRequestURI ()
82+ + " due to: " + t );
83+ t .printStackTrace ();
84+ throw t ; // let it propagate
7485 }
7586 requestsHandled .incrementAndGet ();
7687 });
@@ -85,15 +96,21 @@ public static void main(String[] args) throws Exception {
8596
8697 // go
8798 server .start ();
88- try {
89- factory = Thread .ofVirtual ().name ("fetcher-" , 0 ).factory ();
90- try (var executor = Executors .newThreadPerTaskExecutor (factory )) {
91- for (int i = 1 ; i <= requests ; i ++) {
92- executor .submit (() -> fetch (url )).get ();
99+ try (serverExecutor ) {
100+ try {
101+ factory = Thread .ofVirtual ().name ("fetcher-" , 0 ).factory ();
102+ try (var executor = Executors .newThreadPerTaskExecutor (factory )) {
103+ for (int i = 1 ; i <= requests ; i ++) {
104+ final String actual = executor .submit (() -> fetch (url )).get ();
105+ if (!HELLO .equals (actual )) {
106+ throw new RuntimeException ("unexpected response: \" " + actual
107+ + "\" for request " + i );
108+ }
109+ }
93110 }
111+ } finally {
112+ server .stop (1 );
94113 }
95- } finally {
96- server .stop (1 );
97114 }
98115
99116 if (requestsHandled .get () < requests ) {
0 commit comments