36131
|
1 |
/*
|
|
2 |
* Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
|
|
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
|
|
26 |
* @bug 8087112
|
38883
|
27 |
* @modules java.httpclient
|
|
28 |
* jdk.httpserver
|
37720
|
29 |
* @library /lib/testlibrary/ /
|
|
30 |
* @build jdk.testlibrary.SimpleSSLContext EchoHandler
|
36131
|
31 |
* @compile ../../../com/sun/net/httpserver/LogFilter.java
|
|
32 |
* @compile ../../../com/sun/net/httpserver/FileServerHandler.java
|
37720
|
33 |
* @run main/othervm/timeout=40 -Djava.net.http.HttpClient.log=ssl ManyRequests
|
36131
|
34 |
* @summary Send a large number of requests asynchronously
|
|
35 |
*/
|
|
36 |
|
|
37 |
//package javaapplication16;
|
|
38 |
|
37720
|
39 |
import com.sun.net.httpserver.*;
|
36131
|
40 |
import java.io.IOException;
|
|
41 |
import java.io.UncheckedIOException;
|
|
42 |
import java.net.http.HttpClient;
|
|
43 |
import java.net.http.HttpRequest;
|
|
44 |
import java.net.http.HttpResponse;
|
|
45 |
import java.net.InetSocketAddress;
|
|
46 |
import java.net.URI;
|
|
47 |
import java.util.Arrays;
|
|
48 |
import java.util.HashMap;
|
|
49 |
import java.util.LinkedList;
|
|
50 |
import java.util.Random;
|
37720
|
51 |
import java.util.logging.*;
|
36131
|
52 |
import java.util.concurrent.CompletableFuture;
|
37720
|
53 |
import javax.net.ssl.*;
|
36131
|
54 |
import jdk.testlibrary.SimpleSSLContext;
|
|
55 |
|
|
56 |
public class ManyRequests {
|
|
57 |
|
37720
|
58 |
volatile static int counter = 0;
|
|
59 |
|
36131
|
60 |
public static void main(String[] args) throws Exception {
|
37720
|
61 |
Logger logger = Logger.getLogger("com.sun.net.httpserver");
|
|
62 |
logger.setLevel(Level.ALL);
|
|
63 |
logger.info("TEST");
|
|
64 |
|
36131
|
65 |
SSLContext ctx = new SimpleSSLContext().get();
|
|
66 |
|
|
67 |
InetSocketAddress addr = new InetSocketAddress(0);
|
|
68 |
HttpsServer server = HttpsServer.create(addr, 0);
|
37720
|
69 |
server.setHttpsConfigurator(new Configurator(ctx));
|
36131
|
70 |
|
|
71 |
HttpClient client = HttpClient.create()
|
|
72 |
.sslContext(ctx)
|
|
73 |
.build();
|
|
74 |
try {
|
|
75 |
test(server, client);
|
|
76 |
System.out.println("OK");
|
|
77 |
} finally {
|
|
78 |
server.stop(0);
|
|
79 |
client.executorService().shutdownNow();
|
|
80 |
}
|
|
81 |
}
|
|
82 |
|
37720
|
83 |
//static final int REQUESTS = 1000;
|
|
84 |
static final int REQUESTS = 20;
|
36131
|
85 |
|
|
86 |
static void test(HttpsServer server, HttpClient client) throws Exception {
|
|
87 |
int port = server.getAddress().getPort();
|
|
88 |
URI uri = new URI("https://127.0.0.1:" + port + "/foo/x");
|
|
89 |
server.createContext("/foo", new EchoHandler());
|
|
90 |
server.start();
|
|
91 |
|
|
92 |
RequestLimiter limiter = new RequestLimiter(40);
|
|
93 |
Random rand = new Random();
|
|
94 |
CompletableFuture<Void>[] results = new CompletableFuture[REQUESTS];
|
|
95 |
HashMap<HttpRequest,byte[]> bodies = new HashMap<>();
|
|
96 |
|
|
97 |
for (int i=0; i<REQUESTS; i++) {
|
|
98 |
byte[] buf = new byte[i+1]; // different size bodies
|
|
99 |
rand.nextBytes(buf);
|
|
100 |
HttpRequest r = client.request(uri)
|
|
101 |
.body(HttpRequest.fromByteArray(buf))
|
|
102 |
.POST();
|
|
103 |
bodies.put(r, buf);
|
|
104 |
|
|
105 |
results[i] =
|
|
106 |
limiter.whenOkToSend()
|
|
107 |
.thenCompose((v) -> r.responseAsync())
|
|
108 |
.thenCompose((resp) -> {
|
|
109 |
limiter.requestComplete();
|
|
110 |
if (resp.statusCode() != 200) {
|
|
111 |
resp.bodyAsync(HttpResponse.ignoreBody());
|
|
112 |
String s = "Expected 200, got: " + resp.statusCode();
|
|
113 |
return completedWithIOException(s);
|
37720
|
114 |
} else {
|
|
115 |
counter++;
|
|
116 |
System.out.println("Result from " + counter);
|
36131
|
117 |
}
|
|
118 |
return resp.bodyAsync(HttpResponse.asByteArray())
|
|
119 |
.thenApply((b) -> new Pair<>(resp, b));
|
|
120 |
})
|
|
121 |
.thenAccept((pair) -> {
|
|
122 |
HttpRequest request = pair.t.request();
|
|
123 |
byte[] requestBody = bodies.get(request);
|
|
124 |
check(Arrays.equals(requestBody, pair.u),
|
|
125 |
"bodies not equal");
|
|
126 |
|
|
127 |
});
|
|
128 |
}
|
37720
|
129 |
|
36131
|
130 |
// wait for them all to complete and throw exception in case of error
|
37720
|
131 |
//try {
|
|
132 |
CompletableFuture.allOf(results).join();
|
|
133 |
//} catch (Exception e) {
|
|
134 |
//e.printStackTrace();
|
|
135 |
//throw e;
|
|
136 |
//}
|
36131
|
137 |
}
|
|
138 |
|
|
139 |
static <T> CompletableFuture<T> completedWithIOException(String message) {
|
37720
|
140 |
return CompletableFuture.failedFuture(new IOException(message));
|
36131
|
141 |
}
|
|
142 |
|
|
143 |
static final class Pair<T,U> {
|
|
144 |
Pair(T t, U u) {
|
|
145 |
this.t = t; this.u = u;
|
|
146 |
}
|
|
147 |
T t;
|
|
148 |
U u;
|
|
149 |
}
|
|
150 |
|
|
151 |
/**
|
|
152 |
* A simple limiter for controlling the number of requests to be run in
|
|
153 |
* parallel whenOkToSend() is called which returns a CF<Void> that allows
|
|
154 |
* each individual request to proceed, or block temporarily (blocking occurs
|
|
155 |
* on the waiters list here. As each request actually completes
|
|
156 |
* requestComplete() is called to notify this object, and allow some
|
|
157 |
* requests to continue.
|
|
158 |
*/
|
|
159 |
static class RequestLimiter {
|
|
160 |
|
|
161 |
static final CompletableFuture<Void> COMPLETED_FUTURE =
|
|
162 |
CompletableFuture.completedFuture(null);
|
|
163 |
|
|
164 |
final int maxnumber;
|
|
165 |
final LinkedList<CompletableFuture<Void>> waiters;
|
|
166 |
int number;
|
|
167 |
boolean blocked;
|
|
168 |
|
|
169 |
RequestLimiter(int maximum) {
|
|
170 |
waiters = new LinkedList<>();
|
|
171 |
maxnumber = maximum;
|
|
172 |
}
|
|
173 |
|
|
174 |
synchronized void requestComplete() {
|
|
175 |
number--;
|
|
176 |
// don't unblock until number of requests has halved.
|
|
177 |
if ((blocked && number <= maxnumber / 2) ||
|
|
178 |
(!blocked && waiters.size() > 0)) {
|
|
179 |
int toRelease = Math.min(maxnumber - number, waiters.size());
|
|
180 |
for (int i=0; i<toRelease; i++) {
|
|
181 |
CompletableFuture<Void> f = waiters.remove();
|
|
182 |
number ++;
|
|
183 |
f.complete(null);
|
|
184 |
}
|
|
185 |
blocked = number >= maxnumber;
|
|
186 |
}
|
|
187 |
}
|
|
188 |
|
|
189 |
synchronized CompletableFuture<Void> whenOkToSend() {
|
|
190 |
if (blocked || number + 1 >= maxnumber) {
|
|
191 |
blocked = true;
|
|
192 |
CompletableFuture<Void> r = new CompletableFuture<>();
|
|
193 |
waiters.add(r);
|
|
194 |
return r;
|
|
195 |
} else {
|
|
196 |
number++;
|
|
197 |
return COMPLETED_FUTURE;
|
|
198 |
}
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
static void check(boolean cond, Object... msg) {
|
|
203 |
if (cond)
|
|
204 |
return;
|
|
205 |
StringBuilder sb = new StringBuilder();
|
|
206 |
for (Object o : msg)
|
|
207 |
sb.append(o);
|
|
208 |
throw new RuntimeException(sb.toString());
|
|
209 |
}
|
|
210 |
}
|
37720
|
211 |
|
|
212 |
class Configurator extends HttpsConfigurator {
|
|
213 |
public Configurator(SSLContext ctx) {
|
|
214 |
super(ctx);
|
|
215 |
}
|
|
216 |
|
|
217 |
public void configure (HttpsParameters params) {
|
|
218 |
params.setSSLParameters (getSSLContext().getSupportedSSLParameters());
|
|
219 |
}
|
|
220 |
}
|
|
221 |
|