author | michaelm |
Mon, 09 May 2016 10:28:24 +0100 | |
changeset 37816 | b06d70715a79 |
parent 36131 | 379db4b2f95d |
child 38745 | be430178b177 |
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 |
||
24 |
//package javaapplication16; |
|
25 |
||
26 |
import java.io.IOException; |
|
27 |
import java.net.http.HttpClient; |
|
28 |
import java.net.http.HttpRequest; |
|
29 |
import java.net.http.HttpResponse; |
|
30 |
import java.net.URI; |
|
31 |
import java.util.concurrent.CompletableFuture; |
|
32 |
||
33 |
/** |
|
34 |
* @test |
|
35 |
* @bug 8087112 |
|
36 |
* @build Server |
|
37 |
* @run main/othervm -Djava.net.HttpClient.log=all SplitResponse |
|
38 |
*/ |
|
39 |
||
40 |
/** |
|
41 |
* Similar test to QuickResponses except that each byte of the response |
|
42 |
* is sent in a separate packet, which tests the stability of the implementation |
|
43 |
* for receiving unusual packet sizes. |
|
44 |
*/ |
|
45 |
public class SplitResponse { |
|
46 |
||
47 |
static Server server; |
|
48 |
||
49 |
static String response(String body) { |
|
50 |
return "HTTP/1.1 200 OK\r\nConnection: Close\r\nContent-length: " |
|
51 |
+ Integer.toString(body.length()) |
|
52 |
+ "\r\n\r\n" + body; |
|
53 |
} |
|
54 |
||
55 |
static final String responses[] = { |
|
56 |
"Lorem ipsum", |
|
57 |
"dolor sit amet", |
|
58 |
"consectetur adipiscing elit, sed do eiusmod tempor", |
|
59 |
"quis nostrud exercitation ullamco", |
|
60 |
"laboris nisi", |
|
61 |
"ut", |
|
62 |
"aliquip ex ea commodo consequat." + |
|
63 |
"Duis aute irure dolor in reprehenderit in voluptate velit esse" + |
|
64 |
"cillum dolore eu fugiat nulla pariatur.", |
|
65 |
"Excepteur sint occaecat cupidatat non proident." |
|
66 |
}; |
|
67 |
||
68 |
public static void main(String[] args) throws Exception { |
|
69 |
server = new Server(0); |
|
70 |
URI uri = new URI(server.getURL()); |
|
37816
b06d70715a79
8155888: java/net/httpclient/QuickResponses.java intermittently failed with java.util.ConcurrentModificationException
michaelm
parents:
36131
diff
changeset
|
71 |
server.start(); |
36131 | 72 |
|
73 |
HttpRequest request; |
|
74 |
HttpResponse r; |
|
75 |
CompletableFuture<HttpResponse> cf1; |
|
76 |
||
77 |
for (int i=0; i<responses.length; i++) { |
|
78 |
cf1 = HttpRequest.create(uri) |
|
79 |
.GET() |
|
80 |
.responseAsync(); |
|
81 |
String body = responses[i]; |
|
82 |
||
83 |
Server.Connection c = server.activity(); |
|
84 |
sendSplitResponse(response(body), c); |
|
85 |
r = cf1.get(); |
|
86 |
if (r.statusCode()!= 200) |
|
87 |
throw new RuntimeException("Failed"); |
|
88 |
||
89 |
String rxbody = r.body(HttpResponse.asString()); |
|
90 |
System.out.println("received " + rxbody); |
|
91 |
if (!rxbody.equals(body)) |
|
92 |
throw new RuntimeException("Failed"); |
|
93 |
c.close(); |
|
94 |
} |
|
95 |
HttpClient.getDefault().executorService().shutdownNow(); |
|
96 |
System.out.println("OK"); |
|
97 |
} |
|
98 |
||
99 |
// send the response one byte at a time with a small delay between bytes |
|
100 |
// to ensure that each byte is read in a separate read |
|
101 |
static void sendSplitResponse(String s, Server.Connection conn) { |
|
102 |
System.out.println("Sending: "); |
|
103 |
Thread t = new Thread(() -> { |
|
104 |
try { |
|
105 |
int len = s.length(); |
|
106 |
for (int i = 0; i < len; i++) { |
|
107 |
String onechar = s.substring(i, i + 1); |
|
108 |
conn.send(onechar); |
|
109 |
Thread.sleep(30); |
|
110 |
} |
|
111 |
System.out.println("sent"); |
|
112 |
} catch (IOException | InterruptedException e) { |
|
113 |
} |
|
114 |
}); |
|
115 |
t.setDaemon(true); |
|
116 |
t.start(); |
|
117 |
} |
|
118 |
} |