author | dfuchs |
Mon, 05 Feb 2018 15:51:09 +0000 | |
branch | http-client-branch |
changeset 56070 | 66a9c3185028 |
parent 55973 | 4d9b002587db |
child 56076 | 9a2855e0a796 |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
48083 | 2 |
* Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. |
36131 | 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 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
24 |
import jdk.incubator.http.HttpClient; |
48083 | 25 |
import jdk.incubator.http.HttpClient.Version; |
26 |
import jdk.incubator.http.HttpHeaders; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
27 |
import jdk.incubator.http.HttpRequest; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
28 |
import jdk.incubator.http.HttpResponse; |
48083 | 29 |
import jdk.testlibrary.SimpleSSLContext; |
30 |
||
31 |
import javax.net.ServerSocketFactory; |
|
32 |
import javax.net.ssl.SSLContext; |
|
33 |
import javax.net.ssl.SSLException; |
|
34 |
import javax.net.ssl.SSLServerSocketFactory; |
|
35 |
import java.io.IOException; |
|
36 |
import java.net.SocketException; |
|
36131 | 37 |
import java.net.URI; |
48083 | 38 |
import java.nio.ByteBuffer; |
39 |
import java.util.List; |
|
36131 | 40 |
import java.util.concurrent.CompletableFuture; |
48083 | 41 |
import java.util.concurrent.CompletionException; |
42 |
import java.util.concurrent.CompletionStage; |
|
43 |
import java.util.concurrent.ExecutionException; |
|
44 |
import java.util.concurrent.Flow; |
|
45 |
import java.util.concurrent.atomic.AtomicBoolean; |
|
46 |
import java.util.concurrent.atomic.AtomicInteger; |
|
47 |
||
48 |
import jdk.incubator.http.HttpResponse.BodyHandler; |
|
49 |
import jdk.incubator.http.HttpResponse.BodySubscriber; |
|
50 |
||
51 |
import static java.lang.String.format; |
|
52 |
import static java.lang.System.out; |
|
36131 | 53 |
|
54 |
/** |
|
55 |
* @test |
|
56 |
* @bug 8087112 |
|
48083 | 57 |
* @library /lib/testlibrary |
58 |
* @build jdk.testlibrary.SimpleSSLContext |
|
59 |
* @build MockServer |
|
60 |
* @run main/othervm CancelledResponse |
|
61 |
* @run main/othervm CancelledResponse SSL |
|
36131 | 62 |
*/ |
63 |
||
64 |
/** |
|
48083 | 65 |
* Similar test to SplitResponse except that the client will cancel the response |
66 |
* before receiving it fully. |
|
36131 | 67 |
*/ |
48083 | 68 |
public class CancelledResponse { |
36131 | 69 |
|
48083 | 70 |
static String response(String body, boolean serverKeepalive) { |
71 |
StringBuilder sb = new StringBuilder(); |
|
72 |
sb.append("HTTP/1.1 200 OK\r\n"); |
|
73 |
if (!serverKeepalive) |
|
74 |
sb.append("Connection: Close\r\n"); |
|
36131 | 75 |
|
48083 | 76 |
sb.append("Content-length: ").append(body.length()).append("\r\n"); |
77 |
sb.append("\r\n"); |
|
78 |
sb.append(body); |
|
79 |
return sb.toString(); |
|
36131 | 80 |
} |
81 |
||
82 |
static final String responses[] = { |
|
48083 | 83 |
"Lorem ipsum dolor sit amet consectetur adipiscing elit,", |
84 |
"sed do eiusmod tempor quis nostrud exercitation ullamco laboris nisi ut", |
|
85 |
"aliquip ex ea commodo consequat." |
|
36131 | 86 |
}; |
87 |
||
48083 | 88 |
final ServerSocketFactory factory; |
89 |
final SSLContext context; |
|
90 |
final boolean useSSL; |
|
91 |
CancelledResponse(boolean useSSL) throws IOException { |
|
92 |
this.useSSL = useSSL; |
|
93 |
context = new SimpleSSLContext().get(); |
|
94 |
SSLContext.setDefault(context); |
|
95 |
factory = useSSL ? SSLServerSocketFactory.getDefault() |
|
96 |
: ServerSocketFactory.getDefault(); |
|
97 |
} |
|
98 |
||
99 |
public HttpClient newHttpClient() { |
|
100 |
HttpClient client; |
|
101 |
if (useSSL) { |
|
102 |
client = HttpClient.newBuilder() |
|
103 |
.sslContext(context) |
|
104 |
.build(); |
|
105 |
} else { |
|
106 |
client = HttpClient.newHttpClient(); |
|
107 |
} |
|
108 |
return client; |
|
109 |
} |
|
110 |
||
36131 | 111 |
public static void main(String[] args) throws Exception { |
48083 | 112 |
boolean useSSL = false; |
113 |
if (args != null && args.length == 1) { |
|
114 |
useSSL = "SSL".equals(args[0]); |
|
115 |
} |
|
116 |
CancelledResponse sp = new CancelledResponse(useSSL); |
|
117 |
||
118 |
for (Version version : Version.values()) { |
|
119 |
for (boolean serverKeepalive : new boolean[]{ true, false }) { |
|
120 |
// Note: the mock server doesn't support Keep-Alive, but |
|
121 |
// pretending that it might exercises code paths in and out of |
|
122 |
// the connection pool, and retry logic |
|
123 |
for (boolean async : new boolean[]{ true, false }) { |
|
124 |
sp.test(version, serverKeepalive, async); |
|
125 |
} |
|
126 |
} |
|
127 |
} |
|
128 |
} |
|
129 |
||
130 |
static class CancelException extends IOException { |
|
131 |
} |
|
132 |
||
133 |
// @Test |
|
134 |
void test(Version version, boolean serverKeepalive, boolean async) |
|
135 |
throws Exception |
|
136 |
{ |
|
137 |
out.println(format("*** version %s, serverKeepAlive: %s, async: %s ***", |
|
138 |
version, serverKeepalive, async)); |
|
139 |
MockServer server = new MockServer(0, factory); |
|
36131 | 140 |
URI uri = new URI(server.getURL()); |
48083 | 141 |
out.println("server is: " + uri); |
37816
b06d70715a79
8155888: java/net/httpclient/QuickResponses.java intermittently failed with java.util.ConcurrentModificationException
michaelm
parents:
36131
diff
changeset
|
142 |
server.start(); |
36131 | 143 |
|
48083 | 144 |
HttpClient client = newHttpClient(); |
145 |
HttpRequest request = HttpRequest.newBuilder(uri).version(version).build(); |
|
146 |
try { |
|
147 |
for (int i = 0; i < responses.length; i++) { |
|
148 |
HttpResponse<String> r = null; |
|
149 |
CompletableFuture<HttpResponse<String>> cf1; |
|
150 |
CancelException expected = null; |
|
151 |
AtomicBoolean cancelled = new AtomicBoolean(); |
|
36131 | 152 |
|
48083 | 153 |
out.println("----- iteration " + i + " -----"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
154 |
String body = responses[i]; |
48083 | 155 |
Thread t = sendSplitResponse(response(body, serverKeepalive), server, cancelled); |
36131 | 156 |
|
48083 | 157 |
try { |
158 |
if (async) { |
|
159 |
out.println("send async: " + request); |
|
160 |
cf1 = client.sendAsync(request, asString(body, cancelled)); |
|
161 |
r = cf1.get(); |
|
162 |
} else { // sync |
|
163 |
out.println("send sync: " + request); |
|
164 |
r = client.send(request, asString(body, cancelled)); |
|
165 |
} |
|
166 |
} catch (CancelException c1) { |
|
167 |
System.out.println("Got expected exception: " + c1); |
|
168 |
expected = c1; |
|
169 |
} catch (IOException | ExecutionException | CompletionException c2) { |
|
170 |
Throwable c = c2; |
|
171 |
while (c != null && !(c instanceof CancelException)) { |
|
172 |
c = c.getCause(); |
|
173 |
} |
|
174 |
if (c instanceof CancelException) { |
|
175 |
System.out.println("Got expected exception: " + c); |
|
176 |
expected = (CancelException)c; |
|
177 |
} else throw c2; |
|
178 |
} |
|
179 |
if (r != null) { |
|
180 |
if (r.statusCode() != 200) |
|
181 |
throw new RuntimeException("Failed"); |
|
36131 | 182 |
|
48083 | 183 |
String rxbody = r.body(); |
184 |
out.println("received " + rxbody); |
|
185 |
if (!rxbody.equals(body)) |
|
186 |
throw new RuntimeException(format("Expected:%s, got:%s", body, rxbody)); |
|
187 |
} |
|
188 |
t.join(); |
|
189 |
conn.close(); |
|
190 |
if (expected == null) { |
|
191 |
throw new RuntimeException("Expected exception not raised for " |
|
192 |
+ i + " cancelled=" + cancelled.get()); |
|
193 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
194 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
38745
diff
changeset
|
195 |
} finally { |
48083 | 196 |
server.close(); |
36131 | 197 |
} |
198 |
System.out.println("OK"); |
|
199 |
} |
|
200 |
||
48083 | 201 |
static class CancellingSubscriber implements BodySubscriber<String> { |
202 |
private final String expected; |
|
203 |
private final CompletableFuture<String> result; |
|
204 |
private Flow.Subscription subscription; |
|
205 |
final AtomicInteger index = new AtomicInteger(); |
|
206 |
final AtomicBoolean cancelled; |
|
207 |
CancellingSubscriber(String expected, AtomicBoolean cancelled) { |
|
208 |
this.cancelled = cancelled; |
|
209 |
this.expected = expected; |
|
210 |
result = new CompletableFuture<>(); |
|
211 |
} |
|
212 |
||
213 |
@Override |
|
214 |
public CompletionStage<String> getBody() { |
|
215 |
return result; |
|
216 |
} |
|
217 |
||
218 |
@Override |
|
219 |
public void onSubscribe(Flow.Subscription subscription) { |
|
220 |
this.subscription = subscription; |
|
221 |
subscription.request(1); |
|
222 |
} |
|
223 |
||
224 |
@Override |
|
225 |
public void onNext(List<ByteBuffer> item) { |
|
226 |
//if (result.isDone()) |
|
227 |
for (ByteBuffer b : item) { |
|
228 |
while (b.hasRemaining() && !result.isDone()) { |
|
229 |
int i = index.getAndIncrement(); |
|
230 |
char at = expected.charAt(i); |
|
231 |
byte[] data = new byte[b.remaining()]; |
|
232 |
b.get(data); // we know that the server writes 1 char |
|
233 |
String s = new String(data); |
|
234 |
char c = s.charAt(0); |
|
235 |
if (c != at) { |
|
236 |
Throwable x = new IllegalStateException("char at " |
|
237 |
+ i + " is '" + c + "' expected '" |
|
238 |
+ at + "' for \"" + expected +"\""); |
|
239 |
out.println("unexpected char received, cancelling"); |
|
240 |
subscription.cancel(); |
|
241 |
result.completeExceptionally(x); |
|
242 |
return; |
|
243 |
} |
|
244 |
} |
|
245 |
} |
|
246 |
if (index.get() > 0 && !result.isDone()) { |
|
247 |
// we should complete the result here, but let's |
|
248 |
// see if we get something back... |
|
249 |
out.println("Cancelling subscription after reading " + index.get()); |
|
250 |
cancelled.set(true); |
|
251 |
subscription.cancel(); |
|
252 |
result.completeExceptionally(new CancelException()); |
|
253 |
return; |
|
254 |
} |
|
255 |
if (!result.isDone()) { |
|
256 |
out.println("requesting 1 more"); |
|
257 |
subscription.request(1); |
|
258 |
} |
|
259 |
} |
|
260 |
||
261 |
@Override |
|
262 |
public void onError(Throwable throwable) { |
|
263 |
result.completeExceptionally(throwable); |
|
264 |
} |
|
265 |
||
266 |
@Override |
|
267 |
public void onComplete() { |
|
268 |
int len = index.get(); |
|
269 |
if (len == expected.length()) { |
|
270 |
result.complete(expected); |
|
271 |
} else { |
|
272 |
Throwable x = new IllegalStateException("received only " |
|
273 |
+ len + " chars, expected " + expected.length() |
|
274 |
+ " for \"" + expected +"\""); |
|
275 |
result.completeExceptionally(x); |
|
276 |
} |
|
277 |
} |
|
278 |
} |
|
279 |
||
280 |
static class CancellingHandler implements BodyHandler<String> { |
|
281 |
final String expected; |
|
282 |
final AtomicBoolean cancelled; |
|
283 |
CancellingHandler(String expected, AtomicBoolean cancelled) { |
|
284 |
this.expected = expected; |
|
285 |
this.cancelled = cancelled; |
|
286 |
} |
|
287 |
@Override |
|
288 |
public BodySubscriber<String> apply(int statusCode, HttpHeaders responseHeaders) { |
|
289 |
assert !cancelled.get(); |
|
290 |
return new CancellingSubscriber(expected, cancelled); |
|
291 |
} |
|
292 |
} |
|
293 |
||
294 |
BodyHandler<String> asString(String expected, AtomicBoolean cancelled) { |
|
295 |
return new CancellingHandler(expected, cancelled); |
|
296 |
} |
|
297 |
||
298 |
// required for cleanup |
|
299 |
volatile MockServer.Connection conn; |
|
300 |
||
301 |
// Sends the response, mostly, one byte at a time with a small delay |
|
302 |
// between bytes, to encourage that each byte is read in a separate read |
|
303 |
Thread sendSplitResponse(String s, MockServer server, AtomicBoolean cancelled) { |
|
36131 | 304 |
System.out.println("Sending: "); |
305 |
Thread t = new Thread(() -> { |
|
48083 | 306 |
System.out.println("Waiting for server to receive headers"); |
307 |
conn = server.activity(); |
|
308 |
System.out.println("Start sending response"); |
|
309 |
int sent = 0; |
|
36131 | 310 |
try { |
311 |
int len = s.length(); |
|
48083 | 312 |
out.println("sending " + s); |
36131 | 313 |
for (int i = 0; i < len; i++) { |
314 |
String onechar = s.substring(i, i + 1); |
|
315 |
conn.send(onechar); |
|
48083 | 316 |
sent++; |
317 |
Thread.sleep(10); |
|
36131 | 318 |
} |
48083 | 319 |
out.println("sent " + s); |
320 |
} catch (SSLException | SocketException x) { |
|
321 |
// if SSL then we might get a "Broken Pipe", otherwise |
|
322 |
// a "Socket closed". |
|
323 |
boolean expected = cancelled.get(); |
|
324 |
if (sent > 0 && expected) { |
|
325 |
System.out.println("Connection closed by peer as expected: " + x); |
|
326 |
return; |
|
327 |
} else { |
|
328 |
System.out.println("Unexpected exception (sent=" |
|
329 |
+ sent + ", cancelled=" + expected + "): " + x); |
|
330 |
throw new RuntimeException(x); |
|
331 |
} |
|
36131 | 332 |
} catch (IOException | InterruptedException e) { |
48083 | 333 |
throw new RuntimeException(e); |
36131 | 334 |
} |
335 |
}); |
|
336 |
t.setDaemon(true); |
|
337 |
t.start(); |
|
48083 | 338 |
return t; |
36131 | 339 |
} |
340 |
} |