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