1
1
/*
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.
3
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
4
*
5
5
* This code is free software; you can redistribute it and/or modify it
21
21
* questions.
22
22
*/
23
23
24
- /**
24
+ /*
25
25
* @test
26
26
* @bug 8284161
27
27
* @summary Stress test the HTTP protocol handler and HTTP server
44
44
import java .net .InetSocketAddress ;
45
45
import java .net .Proxy ;
46
46
import java .net .URL ;
47
+ import java .util .concurrent .ExecutorService ;
47
48
import java .util .concurrent .Executors ;
48
49
import java .util .concurrent .ThreadFactory ;
49
50
import java .util .concurrent .atomic .AtomicInteger ;
52
53
53
54
public class HttpALot {
54
55
56
+ private static final String HELLO = "Hello" ;
57
+
55
58
public static void main (String [] args ) throws Exception {
56
59
int requests = 25_000 ;
57
60
if (args .length > 0 ) {
@@ -65,12 +68,20 @@ public static void main(String[] args) throws Exception {
65
68
InetAddress lb = InetAddress .getLoopbackAddress ();
66
69
HttpServer server = HttpServer .create (new InetSocketAddress (lb , 0 ), 1024 );
67
70
ThreadFactory factory = Thread .ofVirtual ().factory ();
68
- server .setExecutor (Executors .newThreadPerTaskExecutor (factory ));
71
+ final ExecutorService serverExecutor = Executors .newThreadPerTaskExecutor (factory );
72
+ server .setExecutor (serverExecutor );
69
73
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
74
85
}
75
86
requestsHandled .incrementAndGet ();
76
87
});
@@ -85,15 +96,21 @@ public static void main(String[] args) throws Exception {
85
96
86
97
// go
87
98
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
+ }
93
110
}
111
+ } finally {
112
+ server .stop (1 );
94
113
}
95
- } finally {
96
- server .stop (1 );
97
114
}
98
115
99
116
if (requestsHandled .get () < requests ) {
0 commit comments