author | jboes |
Fri, 08 Nov 2019 11:15:16 +0000 | |
changeset 59029 | 3786a0962570 |
parent 58365 | 73950479184b |
permissions | -rw-r--r-- |
48083 | 1 |
/* |
49765 | 2 |
* Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved. |
48083 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
7 |
* published by the Free Software Foundation. |
|
8 |
* |
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
13 |
* accompanied this code). |
|
14 |
* |
|
15 |
* You should have received a copy of the GNU General Public License version |
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
18 |
* |
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
20 |
* or visit www.oracle.com if you need additional information or have any |
|
21 |
* questions. |
|
22 |
*/ |
|
23 |
||
24 |
/* |
|
25 |
* @test |
|
49765 | 26 |
* @modules java.net.http |
48083 | 27 |
* java.logging |
28 |
* jdk.httpserver |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
29 |
* @library /test/lib |
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
30 |
* @build jdk.test.lib.net.SimpleSSLContext |
48083 | 31 |
* @compile ../../../com/sun/net/httpserver/LogFilter.java |
32 |
* @compile ../../../com/sun/net/httpserver/EchoHandler.java |
|
33 |
* @compile ../../../com/sun/net/httpserver/FileServerHandler.java |
|
34 |
* @run main/othervm/timeout=40 ManyRequestsLegacy |
|
35 |
* @run main/othervm/timeout=40 -Dtest.insertDelay=true ManyRequestsLegacy |
|
36 |
* @run main/othervm/timeout=40 -Dtest.chunkSize=64 ManyRequestsLegacy |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
37 |
* @run main/othervm/timeout=40 -Dtest.insertDelay=true |
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
38 |
* -Dtest.chunkSize=64 ManyRequestsLegacy |
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
39 |
* @summary Send a large number of requests asynchronously using the legacy |
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
40 |
* URL.openConnection(), to help sanitize results of the test |
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
41 |
* ManyRequest.java. |
48083 | 42 |
*/ |
43 |
||
44 |
import javax.net.ssl.HttpsURLConnection; |
|
45 |
import javax.net.ssl.HostnameVerifier; |
|
46 |
import com.sun.net.httpserver.HttpsConfigurator; |
|
47 |
import com.sun.net.httpserver.HttpsParameters; |
|
48 |
import com.sun.net.httpserver.HttpsServer; |
|
49 |
import com.sun.net.httpserver.HttpExchange; |
|
50 |
import java.io.IOException; |
|
51 |
import java.io.InputStream; |
|
52 |
import java.io.OutputStream; |
|
53 |
import java.net.HttpURLConnection; |
|
49765 | 54 |
import java.net.InetAddress; |
48083 | 55 |
import java.net.URI; |
56 |
import java.net.URLConnection; |
|
57 |
import java.util.Optional; |
|
58 |
import java.util.concurrent.CompletableFuture; |
|
59 |
import java.util.stream.Collectors; |
|
60 |
import javax.net.ssl.SSLContext; |
|
61 |
import javax.net.ssl.SSLSession; |
|
49765 | 62 |
import java.net.http.HttpClient; |
63 |
import java.net.http.HttpClient.Version; |
|
64 |
import java.net.http.HttpHeaders; |
|
65 |
import java.net.http.HttpRequest; |
|
66 |
import java.net.http.HttpRequest.BodyPublishers; |
|
67 |
import java.net.http.HttpResponse; |
|
48083 | 68 |
import java.net.InetSocketAddress; |
69 |
import java.util.Arrays; |
|
70 |
import java.util.Formatter; |
|
71 |
import java.util.HashMap; |
|
72 |
import java.util.LinkedList; |
|
73 |
import java.util.Random; |
|
74 |
import java.util.logging.Logger; |
|
75 |
import java.util.logging.Level; |
|
52121
934969c63223
8211978: Move testlibrary/jdk/testlibrary/SimpleSSLContext.java and testkeys to network testlibrary
jjiang
parents:
49765
diff
changeset
|
76 |
import jdk.test.lib.net.SimpleSSLContext; |
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
52121
diff
changeset
|
77 |
import static java.net.Proxy.NO_PROXY; |
48083 | 78 |
|
79 |
public class ManyRequestsLegacy { |
|
80 |
||
81 |
volatile static int counter = 0; |
|
82 |
||
83 |
public static void main(String[] args) throws Exception { |
|
84 |
Logger logger = Logger.getLogger("com.sun.net.httpserver"); |
|
85 |
logger.setLevel(Level.ALL); |
|
86 |
logger.info("TEST"); |
|
87 |
System.out.println("Sending " + REQUESTS |
|
88 |
+ " requests; delay=" + INSERT_DELAY |
|
89 |
+ ", chunks=" + CHUNK_SIZE |
|
90 |
+ ", XFixed=" + XFIXED); |
|
91 |
SSLContext ctx = new SimpleSSLContext().get(); |
|
92 |
SSLContext.setDefault(ctx); |
|
93 |
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { |
|
94 |
public boolean verify(String hostname, SSLSession session) { |
|
95 |
return true; |
|
96 |
} |
|
97 |
}); |
|
49765 | 98 |
InetSocketAddress addr = new InetSocketAddress(InetAddress.getLoopbackAddress(), 0); |
48083 | 99 |
HttpsServer server = HttpsServer.create(addr, 0); |
100 |
server.setHttpsConfigurator(new Configurator(ctx)); |
|
101 |
||
102 |
LegacyHttpClient client = new LegacyHttpClient(); |
|
103 |
||
104 |
try { |
|
105 |
test(server, client); |
|
106 |
System.out.println("OK"); |
|
107 |
} finally { |
|
108 |
server.stop(0); |
|
109 |
} |
|
110 |
} |
|
111 |
||
112 |
//static final int REQUESTS = 1000; |
|
113 |
static final int REQUESTS = 20; |
|
114 |
static final boolean INSERT_DELAY = Boolean.getBoolean("test.insertDelay"); |
|
115 |
static final int CHUNK_SIZE = Math.max(0, |
|
116 |
Integer.parseInt(System.getProperty("test.chunkSize", "0"))); |
|
117 |
static final boolean XFIXED = Boolean.getBoolean("test.XFixed"); |
|
118 |
||
119 |
static class LegacyHttpClient { |
|
49765 | 120 |
static final class LegacyHttpResponse implements HttpResponse<byte[]> { |
48083 | 121 |
final HttpRequest request; |
122 |
final byte[] response; |
|
123 |
final int statusCode; |
|
124 |
public LegacyHttpResponse(HttpRequest request, int statusCode, byte[] response) { |
|
125 |
this.request = request; |
|
126 |
this.statusCode = statusCode; |
|
127 |
this.response = response; |
|
128 |
} |
|
129 |
private <T> T error() { |
|
130 |
throw new UnsupportedOperationException("Not supported yet."); |
|
131 |
} |
|
132 |
@Override |
|
133 |
public int statusCode() { return statusCode;} |
|
134 |
@Override |
|
135 |
public HttpRequest request() {return request;} |
|
136 |
@Override |
|
137 |
public Optional<HttpResponse<byte[]>> previousResponse() {return Optional.empty();} |
|
138 |
@Override |
|
139 |
public HttpHeaders headers() { return error(); } |
|
140 |
@Override |
|
141 |
public byte[] body() {return response;} |
|
142 |
@Override |
|
49765 | 143 |
public Optional<SSLSession> sslSession() { |
144 |
return Optional.empty(); // for now |
|
48083 | 145 |
} |
146 |
@Override |
|
147 |
public URI uri() { return request.uri();} |
|
148 |
@Override |
|
149 |
public HttpClient.Version version() { return Version.HTTP_1_1;} |
|
150 |
} |
|
151 |
||
152 |
private void debugCompleted(String tag, long startNanos, HttpRequest req) { |
|
153 |
System.err.println(tag + " elapsed " |
|
154 |
+ (System.nanoTime() - startNanos)/1000_000L |
|
155 |
+ " millis for " + req.method() |
|
156 |
+ " to " + req.uri()); |
|
157 |
} |
|
158 |
||
159 |
CompletableFuture<? extends HttpResponse<byte[]>> sendAsync(HttpRequest r, byte[] buf) { |
|
160 |
long start = System.nanoTime(); |
|
161 |
try { |
|
162 |
CompletableFuture<LegacyHttpResponse> cf = new CompletableFuture<>(); |
|
58365
73950479184b
8231504: Update networking tests to avoid implicit dependency on the system proxies
chegar
parents:
52121
diff
changeset
|
163 |
URLConnection urlc = r.uri().toURL().openConnection(NO_PROXY); |
48083 | 164 |
HttpURLConnection httpc = (HttpURLConnection)urlc; |
165 |
httpc.setRequestMethod(r.method()); |
|
166 |
for (String s : r.headers().map().keySet()) { |
|
167 |
httpc.setRequestProperty(s, r.headers().allValues(s) |
|
168 |
.stream().collect(Collectors.joining(","))); |
|
169 |
} |
|
170 |
httpc.setDoInput(true); |
|
171 |
if (buf != null) httpc.setDoOutput(true); |
|
172 |
Thread t = new Thread(() -> { |
|
173 |
try { |
|
174 |
if (buf != null) { |
|
175 |
try (OutputStream os = httpc.getOutputStream()) { |
|
176 |
os.write(buf); |
|
177 |
os.flush(); |
|
178 |
} |
|
179 |
} |
|
180 |
LegacyHttpResponse response = new LegacyHttpResponse(r, |
|
181 |
httpc.getResponseCode(),httpc.getInputStream().readAllBytes()); |
|
182 |
cf.complete(response); |
|
183 |
} catch(Throwable x) { |
|
184 |
cf.completeExceptionally(x); |
|
185 |
} |
|
186 |
}); |
|
187 |
t.start(); |
|
188 |
return cf.whenComplete((b,x) -> debugCompleted("ClientImpl (async)", start, r)); |
|
189 |
} catch(Throwable t) { |
|
190 |
debugCompleted("ClientImpl (async)", start, r); |
|
191 |
return CompletableFuture.failedFuture(t); |
|
192 |
} |
|
193 |
} |
|
194 |
} |
|
195 |
||
196 |
static class TestEchoHandler extends EchoHandler { |
|
197 |
final Random rand = new Random(); |
|
198 |
@Override |
|
199 |
public void handle(HttpExchange e) throws IOException { |
|
200 |
System.out.println("Server: received " + e.getRequestURI()); |
|
201 |
super.handle(e); |
|
202 |
} |
|
203 |
@Override |
|
204 |
protected void close(HttpExchange t, OutputStream os) throws IOException { |
|
205 |
if (INSERT_DELAY) { |
|
206 |
try { Thread.sleep(rand.nextInt(200)); } |
|
207 |
catch (InterruptedException e) {} |
|
208 |
} |
|
209 |
System.out.println("Server: close outbound: " + t.getRequestURI()); |
|
210 |
os.close(); |
|
211 |
} |
|
212 |
@Override |
|
213 |
protected void close(HttpExchange t, InputStream is) throws IOException { |
|
214 |
if (INSERT_DELAY) { |
|
215 |
try { Thread.sleep(rand.nextInt(200)); } |
|
216 |
catch (InterruptedException e) {} |
|
217 |
} |
|
218 |
System.out.println("Server: close inbound: " + t.getRequestURI()); |
|
219 |
is.close(); |
|
220 |
} |
|
221 |
} |
|
222 |
||
223 |
static void test(HttpsServer server, LegacyHttpClient client) throws Exception { |
|
224 |
int port = server.getAddress().getPort(); |
|
49765 | 225 |
URI baseURI = new URI("https://localhost:" + port + "/foo/x"); |
48083 | 226 |
server.createContext("/foo", new TestEchoHandler()); |
227 |
server.start(); |
|
228 |
||
229 |
RequestLimiter limiter = new RequestLimiter(40); |
|
230 |
Random rand = new Random(); |
|
231 |
CompletableFuture<?>[] results = new CompletableFuture<?>[REQUESTS]; |
|
232 |
HashMap<HttpRequest,byte[]> bodies = new HashMap<>(); |
|
233 |
||
234 |
for (int i=0; i<REQUESTS; i++) { |
|
235 |
byte[] buf = new byte[(i+1)*CHUNK_SIZE+i+1]; // different size bodies |
|
236 |
rand.nextBytes(buf); |
|
237 |
URI uri = new URI(baseURI.toString() + String.valueOf(i+1)); |
|
238 |
HttpRequest r = HttpRequest.newBuilder(uri) |
|
239 |
.header("XFixed", "true") |
|
49765 | 240 |
.POST(BodyPublishers.ofByteArray(buf)) |
48083 | 241 |
.build(); |
242 |
bodies.put(r, buf); |
|
243 |
||
244 |
results[i] = |
|
245 |
limiter.whenOkToSend() |
|
246 |
.thenCompose((v) -> { |
|
247 |
System.out.println("Client: sendAsync: " + r.uri()); |
|
248 |
return client.sendAsync(r, buf); |
|
249 |
}) |
|
250 |
.thenCompose((resp) -> { |
|
251 |
limiter.requestComplete(); |
|
252 |
if (resp.statusCode() != 200) { |
|
253 |
String s = "Expected 200, got: " + resp.statusCode(); |
|
254 |
System.out.println(s + " from " |
|
255 |
+ resp.request().uri().getPath()); |
|
256 |
return completedWithIOException(s); |
|
257 |
} else { |
|
258 |
counter++; |
|
259 |
System.out.println("Result (" + counter + ") from " |
|
260 |
+ resp.request().uri().getPath()); |
|
261 |
} |
|
262 |
return CompletableFuture.completedStage(resp.body()) |
|
263 |
.thenApply((b) -> new Pair<>(resp, b)); |
|
264 |
}) |
|
265 |
.thenAccept((pair) -> { |
|
266 |
HttpRequest request = pair.t.request(); |
|
267 |
byte[] requestBody = bodies.get(request); |
|
268 |
check(Arrays.equals(requestBody, pair.u), |
|
269 |
"bodies not equal:[" + bytesToHexString(requestBody) |
|
270 |
+ "] [" + bytesToHexString(pair.u) + "]"); |
|
271 |
||
272 |
}); |
|
273 |
} |
|
274 |
||
275 |
// wait for them all to complete and throw exception in case of error |
|
276 |
CompletableFuture.allOf(results).join(); |
|
277 |
} |
|
278 |
||
279 |
static <T> CompletableFuture<T> completedWithIOException(String message) { |
|
280 |
return CompletableFuture.failedFuture(new IOException(message)); |
|
281 |
} |
|
282 |
||
283 |
static String bytesToHexString(byte[] bytes) { |
|
284 |
if (bytes == null) |
|
285 |
return "null"; |
|
286 |
||
287 |
StringBuilder sb = new StringBuilder(bytes.length * 2); |
|
288 |
||
289 |
Formatter formatter = new Formatter(sb); |
|
290 |
for (byte b : bytes) { |
|
291 |
formatter.format("%02x", b); |
|
292 |
} |
|
293 |
||
294 |
return sb.toString(); |
|
295 |
} |
|
296 |
||
297 |
static final class Pair<T,U> { |
|
298 |
Pair(T t, U u) { |
|
299 |
this.t = t; this.u = u; |
|
300 |
} |
|
301 |
T t; |
|
302 |
U u; |
|
303 |
} |
|
304 |
||
305 |
/** |
|
306 |
* A simple limiter for controlling the number of requests to be run in |
|
307 |
* parallel whenOkToSend() is called which returns a CF<Void> that allows |
|
308 |
* each individual request to proceed, or block temporarily (blocking occurs |
|
309 |
* on the waiters list here. As each request actually completes |
|
310 |
* requestComplete() is called to notify this object, and allow some |
|
311 |
* requests to continue. |
|
312 |
*/ |
|
313 |
static class RequestLimiter { |
|
314 |
||
315 |
static final CompletableFuture<Void> COMPLETED_FUTURE = |
|
316 |
CompletableFuture.completedFuture(null); |
|
317 |
||
318 |
final int maxnumber; |
|
319 |
final LinkedList<CompletableFuture<Void>> waiters; |
|
320 |
int number; |
|
321 |
boolean blocked; |
|
322 |
||
323 |
RequestLimiter(int maximum) { |
|
324 |
waiters = new LinkedList<>(); |
|
325 |
maxnumber = maximum; |
|
326 |
} |
|
327 |
||
328 |
synchronized void requestComplete() { |
|
329 |
number--; |
|
330 |
// don't unblock until number of requests has halved. |
|
331 |
if ((blocked && number <= maxnumber / 2) || |
|
332 |
(!blocked && waiters.size() > 0)) { |
|
333 |
int toRelease = Math.min(maxnumber - number, waiters.size()); |
|
334 |
for (int i=0; i<toRelease; i++) { |
|
335 |
CompletableFuture<Void> f = waiters.remove(); |
|
336 |
number ++; |
|
337 |
f.complete(null); |
|
338 |
} |
|
339 |
blocked = number >= maxnumber; |
|
340 |
} |
|
341 |
} |
|
342 |
||
343 |
synchronized CompletableFuture<Void> whenOkToSend() { |
|
344 |
if (blocked || number + 1 >= maxnumber) { |
|
345 |
blocked = true; |
|
346 |
CompletableFuture<Void> r = new CompletableFuture<>(); |
|
347 |
waiters.add(r); |
|
348 |
return r; |
|
349 |
} else { |
|
350 |
number++; |
|
351 |
return COMPLETED_FUTURE; |
|
352 |
} |
|
353 |
} |
|
354 |
} |
|
355 |
||
356 |
static void check(boolean cond, Object... msg) { |
|
357 |
if (cond) |
|
358 |
return; |
|
359 |
StringBuilder sb = new StringBuilder(); |
|
360 |
for (Object o : msg) |
|
361 |
sb.append(o); |
|
362 |
throw new RuntimeException(sb.toString()); |
|
363 |
} |
|
364 |
||
365 |
static class Configurator extends HttpsConfigurator { |
|
366 |
public Configurator(SSLContext ctx) { |
|
367 |
super(ctx); |
|
368 |
} |
|
369 |
||
370 |
public void configure(HttpsParameters params) { |
|
371 |
params.setSSLParameters(getSSLContext().getSupportedSSLParameters()); |
|
372 |
} |
|
373 |
} |
|
374 |
} |