author | dfuchs |
Thu, 12 Sep 2019 15:46:11 +0100 | |
changeset 58108 | 5302477c8285 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 2005, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* |
|
25 |
* @test |
|
7022
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
26 |
* @bug 5045306 6356004 6993490 |
30820 | 27 |
* @modules java.base/sun.net.www |
44115
bb4e971bf5d4
8176195: Fix misc module dependencies in jdk_core tests
xiaofeya
parents:
30820
diff
changeset
|
28 |
* java.management |
2 | 29 |
* @library ../../httptest/ |
13788 | 30 |
* @build HttpCallback TestHttpServer HttpTransaction |
2 | 31 |
* @run main/othervm B5045306 |
32 |
* @summary Http keep-alive implementation is not efficient |
|
33 |
*/ |
|
34 |
||
35 |
import java.net.*; |
|
36 |
import java.io.*; |
|
37 |
import java.lang.management.*; |
|
38 |
||
39 |
/* Part 1: |
|
40 |
* The http client makes a connection to a URL whos content contains a lot of |
|
41 |
* data, more than can fit in the socket buffer. The client only reads |
|
42 |
* 1 byte of the data from the InputStream leaving behind more data than can |
|
43 |
* fit in the socket buffer. The client then makes a second call to the http |
|
44 |
* server. If the connection port used by the client is the same as for the |
|
45 |
* first call then that means that the connection is being reused. |
|
46 |
* |
|
47 |
* Part 2: |
|
48 |
* Test buggy webserver that sends less data than it specifies in its |
|
49 |
* Content-length header. |
|
50 |
*/ |
|
51 |
||
52 |
public class B5045306 |
|
53 |
{ |
|
54 |
static SimpleHttpTransaction httpTrans; |
|
13788 | 55 |
static TestHttpServer server; |
2 | 56 |
|
57 |
public static void main(String[] args) throws Exception { |
|
58 |
startHttpServer(); |
|
59 |
clientHttpCalls(); |
|
60 |
} |
|
61 |
||
62 |
public static void startHttpServer() { |
|
63 |
try { |
|
64 |
httpTrans = new SimpleHttpTransaction(); |
|
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
47216
diff
changeset
|
65 |
server = new TestHttpServer(httpTrans, 1, 10, InetAddress.getLocalHost(), 0); |
2 | 66 |
} catch (IOException e) { |
67 |
e.printStackTrace(); |
|
68 |
} |
|
69 |
} |
|
70 |
||
71 |
public static void clientHttpCalls() { |
|
72 |
try { |
|
73 |
System.out.println("http server listen on: " + server.getLocalPort()); |
|
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
47216
diff
changeset
|
74 |
String hostAddr = InetAddress.getLocalHost().getHostAddress(); |
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
47216
diff
changeset
|
75 |
if (hostAddr.indexOf(':') > -1) hostAddr = "[" + hostAddr + "]"; |
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
47216
diff
changeset
|
76 |
String baseURLStr = "http://" + hostAddr + ":" + server.getLocalPort() + "/"; |
2 | 77 |
|
78 |
URL bigDataURL = new URL (baseURLStr + "firstCall"); |
|
79 |
URL smallDataURL = new URL (baseURLStr + "secondCall"); |
|
80 |
||
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
47216
diff
changeset
|
81 |
HttpURLConnection uc = (HttpURLConnection)bigDataURL.openConnection(Proxy.NO_PROXY); |
2 | 82 |
|
83 |
//Only read 1 byte of response data and close the stream |
|
84 |
InputStream is = uc.getInputStream(); |
|
85 |
byte[] ba = new byte[1]; |
|
86 |
is.read(ba); |
|
87 |
is.close(); |
|
88 |
||
89 |
// Allow the KeepAliveStreamCleaner thread to read the data left behind and cache the connection. |
|
90 |
try { Thread.sleep(2000); } catch (Exception e) {} |
|
91 |
||
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
47216
diff
changeset
|
92 |
uc = (HttpURLConnection)smallDataURL.openConnection(Proxy.NO_PROXY); |
2 | 93 |
uc.getResponseCode(); |
94 |
||
95 |
if (SimpleHttpTransaction.failed) |
|
96 |
throw new RuntimeException("Failed: Initial Keep Alive Connection is not being reused"); |
|
97 |
||
98 |
// Part 2 |
|
99 |
URL part2Url = new URL (baseURLStr + "part2"); |
|
58108
5302477c8285
8230858: Replace wildcard address with loopback or local host in tests - part 23
dfuchs
parents:
47216
diff
changeset
|
100 |
uc = (HttpURLConnection)part2Url.openConnection(Proxy.NO_PROXY); |
2 | 101 |
is = uc.getInputStream(); |
102 |
is.close(); |
|
103 |
||
104 |
// Allow the KeepAliveStreamCleaner thread to try and read the data left behind and cache the connection. |
|
105 |
try { Thread.sleep(2000); } catch (Exception e) {} |
|
106 |
||
107 |
ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); |
|
108 |
if (threadMXBean.isThreadCpuTimeSupported()) { |
|
109 |
long[] threads = threadMXBean.getAllThreadIds(); |
|
110 |
ThreadInfo[] threadInfo = threadMXBean.getThreadInfo(threads); |
|
111 |
for (int i=0; i<threadInfo.length; i++) { |
|
112 |
if (threadInfo[i].getThreadName().equals("Keep-Alive-SocketCleaner")) { |
|
113 |
System.out.println("Found Keep-Alive-SocketCleaner thread"); |
|
114 |
long threadID = threadInfo[i].getThreadId(); |
|
115 |
long before = threadMXBean.getThreadCpuTime(threadID); |
|
116 |
try { Thread.sleep(2000); } catch (Exception e) {} |
|
117 |
long after = threadMXBean.getThreadCpuTime(threadID); |
|
118 |
||
119 |
if (before ==-1 || after == -1) |
|
120 |
break; // thread has died, OK |
|
121 |
||
122 |
// if Keep-Alive-SocketCleaner consumes more than 50% of cpu then we |
|
123 |
// can assume a recursive loop. |
|
124 |
long total = after - before; |
|
125 |
if (total >= 1000000000) // 1 second, or 1 billion nanoseconds |
|
126 |
throw new RuntimeException("Failed: possible recursive loop in Keep-Alive-SocketCleaner"); |
|
127 |
} |
|
128 |
} |
|
129 |
} |
|
130 |
||
131 |
} catch (IOException e) { |
|
132 |
e.printStackTrace(); |
|
133 |
} finally { |
|
134 |
server.terminate(); |
|
135 |
} |
|
136 |
} |
|
137 |
} |
|
138 |
||
139 |
class SimpleHttpTransaction implements HttpCallback |
|
140 |
{ |
|
141 |
static boolean failed = false; |
|
142 |
||
143 |
// Need to have enough data here that is too large for the socket buffer to hold. |
|
144 |
// Also http.KeepAlive.remainingData must be greater than this value, default is 256K. |
|
145 |
static final int RESPONSE_DATA_LENGTH = 128 * 1024; |
|
146 |
||
147 |
int port1; |
|
148 |
||
149 |
public void request(HttpTransaction trans) { |
|
150 |
try { |
|
151 |
String path = trans.getRequestURI().getPath(); |
|
152 |
if (path.equals("/firstCall")) { |
|
153 |
port1 = trans.channel().socket().getPort(); |
|
154 |
System.out.println("First connection on client port = " + port1); |
|
155 |
||
156 |
byte[] responseBody = new byte[RESPONSE_DATA_LENGTH]; |
|
157 |
for (int i=0; i<responseBody.length; i++) |
|
158 |
responseBody[i] = 0x41; |
|
159 |
trans.setResponseEntityBody (responseBody, responseBody.length); |
|
160 |
trans.sendResponse(200, "OK"); |
|
161 |
} else if (path.equals("/secondCall")) { |
|
162 |
int port2 = trans.channel().socket().getPort(); |
|
163 |
System.out.println("Second connection on client port = " + port2); |
|
164 |
||
165 |
if (port1 != port2) |
|
166 |
failed = true; |
|
167 |
||
168 |
trans.setResponseHeader ("Content-length", Integer.toString(0)); |
|
7022
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
169 |
|
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
170 |
/* Force the server to not respond for more that the timeout |
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
171 |
* set by the keepalive cleaner (5000 millis). This ensures the |
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
172 |
* timeout is correctly resets the default read timeout, |
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
173 |
* infinity. See 6993490. */ |
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
174 |
System.out.println("server sleeping..."); |
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
175 |
try {Thread.sleep(6000); } catch (InterruptedException e) {} |
1066bfde0f5e
6993490: SocketTimeoutException on HTTP keep-alive connections
chegar
parents:
5506
diff
changeset
|
176 |
|
2 | 177 |
trans.sendResponse(200, "OK"); |
178 |
} else if(path.equals("/part2")) { |
|
179 |
System.out.println("Call to /part2"); |
|
180 |
byte[] responseBody = new byte[RESPONSE_DATA_LENGTH]; |
|
181 |
for (int i=0; i<responseBody.length; i++) |
|
182 |
responseBody[i] = 0x41; |
|
183 |
trans.setResponseEntityBody (responseBody, responseBody.length); |
|
184 |
||
185 |
// override the Content-length header to be greater than the actual response body |
|
186 |
trans.setResponseHeader("Content-length", Integer.toString(responseBody.length+1)); |
|
187 |
trans.sendResponse(200, "OK"); |
|
188 |
||
189 |
// now close the socket |
|
190 |
trans.channel().socket().close(); |
|
191 |
} |
|
192 |
} catch (Exception e) { |
|
193 |
e.printStackTrace(); |
|
194 |
} |
|
195 |
} |
|
196 |
} |