author | dfuchs |
Wed, 15 May 2019 19:09:54 +0100 | |
changeset 54885 | e58e454c1409 |
parent 54787 | 7748aa47b4e2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
54787
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
2 |
* Copyright (c) 2002, 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 |
|
26 |
* @bug 4636628 |
|
27 |
* @summary HttpURLConnection duplicates HTTP GET requests when used with multiple threads |
|
28 |
*/ |
|
29 |
||
30 |
/* |
|
31 |
* This tests keep-alive behavior using chunkedinputstreams |
|
32 |
* It checks that keep-alive connections are used and also |
|
33 |
* that requests are not being repeated (due to errors) |
|
34 |
* |
|
35 |
* It also checks that the keepalive connections are closed eventually |
|
36 |
* because the test will not terminate if the connections |
|
37 |
* are not closed by the keep-alive timer. |
|
38 |
*/ |
|
39 |
||
40 |
import java.net.*; |
|
41 |
import java.io.*; |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
42 |
import java.time.Duration; |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
43 |
import java.util.Queue; |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
44 |
import java.util.concurrent.ConcurrentLinkedQueue; |
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
45 |
import java.util.concurrent.atomic.AtomicInteger; |
2 | 46 |
|
47 |
public class MultiThreadTest extends Thread { |
|
48 |
||
49 |
/* |
|
50 |
* Is debugging enabled - start with -d to enable. |
|
51 |
*/ |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
52 |
static boolean debug = true; // disable debug once stability proven |
2 | 53 |
|
54 |
static Object threadlock = new Object (); |
|
55 |
static int threadCounter = 0; |
|
56 |
||
57 |
static Object getLock() { return threadlock; } |
|
58 |
||
59 |
static void debug(String msg) { |
|
60 |
if (debug) |
|
61 |
System.out.println(msg); |
|
62 |
} |
|
63 |
||
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
64 |
static final AtomicInteger reqnum = new AtomicInteger(); |
2 | 65 |
|
66 |
void doRequest(String uri) throws Exception { |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
67 |
URL url = new URL(uri + "?foo="+reqnum.getAndIncrement()); |
2 | 68 |
HttpURLConnection http = (HttpURLConnection)url.openConnection(); |
69 |
InputStream in = http.getInputStream(); |
|
70 |
byte b[] = new byte[100]; |
|
71 |
int total = 0; |
|
72 |
int n; |
|
73 |
do { |
|
74 |
n = in.read(b); |
|
75 |
if (n > 0) total += n; |
|
76 |
} while (n > 0); |
|
77 |
debug ("client: read " + total + " bytes"); |
|
78 |
in.close(); |
|
79 |
http.disconnect(); |
|
80 |
} |
|
81 |
||
82 |
String uri; |
|
83 |
byte[] b; |
|
84 |
int requests; |
|
85 |
||
54787
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
86 |
MultiThreadTest(String authority, int requests) throws Exception { |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
87 |
uri = "http://" + authority + "/foo.html"; |
2 | 88 |
|
89 |
b = new byte [256]; |
|
90 |
this.requests = requests; |
|
91 |
||
92 |
synchronized (threadlock) { |
|
93 |
threadCounter ++; |
|
94 |
} |
|
95 |
} |
|
96 |
||
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
97 |
public void run() { |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
98 |
long start = System.nanoTime(); |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
99 |
|
2 | 100 |
try { |
101 |
for (int i=0; i<requests; i++) { |
|
102 |
doRequest (uri); |
|
103 |
} |
|
104 |
} catch (Exception e) { |
|
105 |
throw new RuntimeException (e.getMessage()); |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
106 |
} finally { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
107 |
synchronized (threadlock) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
108 |
threadCounter --; |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
109 |
if (threadCounter == 0) { |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
110 |
threadlock.notifyAll(); |
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
111 |
} |
2 | 112 |
} |
113 |
} |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
114 |
debug("client: end - " + Duration.ofNanos(System.nanoTime() - start)); |
2 | 115 |
} |
116 |
||
117 |
static int threads=5; |
|
118 |
||
119 |
public static void main(String args[]) throws Exception { |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
120 |
long start = System.nanoTime(); |
2 | 121 |
|
122 |
int x = 0, arg_len = args.length; |
|
123 |
int requests = 20; |
|
124 |
||
125 |
if (arg_len > 0 && args[0].equals("-d")) { |
|
126 |
debug = true; |
|
127 |
x = 1; |
|
128 |
arg_len --; |
|
129 |
} |
|
130 |
if (arg_len > 0) { |
|
131 |
threads = Integer.parseInt (args[x]); |
|
132 |
requests = Integer.parseInt (args[x+1]); |
|
133 |
} |
|
134 |
||
135 |
/* start the server */ |
|
54787
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
136 |
InetAddress loopback = InetAddress.getLoopbackAddress(); |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
137 |
ServerSocket ss = new ServerSocket(); |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
138 |
ss.bind(new InetSocketAddress(loopback, 0)); |
2 | 139 |
Server svr = new Server(ss); |
140 |
svr.start(); |
|
141 |
||
142 |
Object lock = MultiThreadTest.getLock(); |
|
143 |
synchronized (lock) { |
|
144 |
for (int i=0; i<threads; i++) { |
|
54787
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
145 |
MultiThreadTest t = new MultiThreadTest(svr.getAuthority(), requests); |
2 | 146 |
t.start (); |
147 |
} |
|
148 |
try { |
|
149 |
lock.wait(); |
|
150 |
} catch (InterruptedException e) {} |
|
151 |
} |
|
152 |
||
153 |
// shutdown server - we're done. |
|
154 |
svr.shutdown(); |
|
155 |
||
156 |
int cnt = svr.connectionCount(); |
|
157 |
MultiThreadTest.debug("Connections = " + cnt); |
|
158 |
int reqs = Worker.getRequests (); |
|
159 |
MultiThreadTest.debug("Requests = " + reqs); |
|
160 |
System.out.println ("Connection count = " + cnt + " Request count = " + reqs); |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
161 |
|
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
162 |
// We may have received traffic from something else than |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
163 |
// our client. We should only count those workers for which |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
164 |
// the expected header has been found. |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
165 |
int validConnections = 0; |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
166 |
for (Worker w : svr.workers()) { |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
167 |
if (w.headerFound) validConnections++; |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
168 |
} |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
169 |
|
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
170 |
if (validConnections > threads + 1 || validConnections == 0) { // could be less |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
171 |
throw new RuntimeException ("Expected " + threads + " connections: used " + validConnections); |
2 | 172 |
} |
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
173 |
|
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
174 |
// Sometimes the client drops a connection after a while and |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
175 |
// spawns a new one. Why this is happening is not clear, |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
176 |
// and JDK-8223783 is logged to follow up on this. For the sake |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
177 |
// of test stabilization we don't fail on `threads + 1` connections |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
178 |
// but log a warning instead. |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
179 |
if (validConnections == threads + 1) { |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
180 |
debug("WARNING: " + validConnections |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
181 |
+ " have been used, where only " + threads |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
182 |
+ " were expected!"); |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
183 |
} |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
184 |
|
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
185 |
if (validConnections != cnt) { |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
186 |
debug("WARNING: got " + (cnt - validConnections) + " unexpected connections!"); |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
187 |
} |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
188 |
if (validConnections == cnt && reqs != threads*requests) { |
2 | 189 |
throw new RuntimeException ("Expected "+ threads*requests+ " requests: got " +reqs); |
190 |
} |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
191 |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
192 |
for (Thread worker : svr.workers()) { |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
193 |
worker.join(60_000); |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
194 |
} |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
195 |
|
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
196 |
debug("main thread end - " + Duration.ofNanos(System.nanoTime() - start)); |
2 | 197 |
} |
198 |
} |
|
199 |
||
200 |
/* |
|
201 |
* Server thread to accept connection and create worker threads |
|
202 |
* to service each connection. |
|
203 |
*/ |
|
204 |
class Server extends Thread { |
|
205 |
ServerSocket ss; |
|
206 |
int connectionCount; |
|
207 |
boolean shutdown = false; |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
208 |
private final Queue<Worker> workers = new ConcurrentLinkedQueue<>(); |
2 | 209 |
|
210 |
Server(ServerSocket ss) { |
|
211 |
this.ss = ss; |
|
212 |
} |
|
213 |
||
54787
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
214 |
public String getAuthority() { |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
215 |
InetAddress address = ss.getInetAddress(); |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
216 |
String hostaddr = address.isAnyLocalAddress() |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
217 |
? "localhost" : address.getHostAddress(); |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
218 |
if (hostaddr.indexOf(':') > -1) { |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
219 |
hostaddr = "[" + hostaddr + "]"; |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
220 |
} |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
221 |
return hostaddr + ":" + ss.getLocalPort(); |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
222 |
} |
7748aa47b4e2
8223573: Replace wildcard address with loopback or local host in tests - part 4
dfuchs
parents:
51947
diff
changeset
|
223 |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
224 |
public Queue<Worker> workers() { |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
225 |
return workers; |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
226 |
} |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
227 |
|
2 | 228 |
public synchronized int connectionCount() { |
229 |
return connectionCount; |
|
230 |
} |
|
231 |
||
232 |
public synchronized void shutdown() { |
|
233 |
shutdown = true; |
|
234 |
} |
|
235 |
||
236 |
public void run() { |
|
237 |
try { |
|
238 |
ss.setSoTimeout(2000); |
|
239 |
||
240 |
for (;;) { |
|
241 |
Socket s; |
|
242 |
try { |
|
243 |
MultiThreadTest.debug("server: calling accept."); |
|
244 |
s = ss.accept(); |
|
245 |
MultiThreadTest.debug("server: return accept."); |
|
246 |
} catch (SocketTimeoutException te) { |
|
247 |
MultiThreadTest.debug("server: STE"); |
|
248 |
synchronized (this) { |
|
249 |
if (shutdown) { |
|
250 |
MultiThreadTest.debug("server: Shuting down."); |
|
251 |
return; |
|
252 |
} |
|
253 |
} |
|
254 |
continue; |
|
255 |
} |
|
256 |
||
257 |
int id; |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
258 |
Worker w; |
2 | 259 |
synchronized (this) { |
260 |
id = connectionCount++; |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
261 |
w = new Worker(s, id); |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
262 |
workers.add(w); |
2 | 263 |
} |
264 |
w.start(); |
|
265 |
MultiThreadTest.debug("server: Started worker " + id); |
|
266 |
} |
|
267 |
||
268 |
} catch (Exception e) { |
|
269 |
e.printStackTrace(); |
|
270 |
} finally { |
|
271 |
try { |
|
272 |
ss.close(); |
|
273 |
} catch (Exception e) { } |
|
274 |
} |
|
275 |
} |
|
276 |
} |
|
277 |
||
278 |
/* |
|
279 |
* Worker thread to service single connection - can service |
|
280 |
* multiple http requests on same connection. |
|
281 |
*/ |
|
282 |
class Worker extends Thread { |
|
283 |
Socket s; |
|
284 |
int id; |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
285 |
volatile boolean headerFound; |
2 | 286 |
|
287 |
Worker(Socket s, int id) { |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
288 |
super("Worker-" + id); |
2 | 289 |
this.s = s; |
290 |
this.id = id; |
|
291 |
} |
|
292 |
||
293 |
static int requests = 0; |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
294 |
static final Object rlock = new Object(); |
2 | 295 |
|
296 |
public static int getRequests () { |
|
297 |
synchronized (rlock) { |
|
298 |
return requests; |
|
299 |
} |
|
300 |
} |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
301 |
|
2 | 302 |
public static void incRequests () { |
303 |
synchronized (rlock) { |
|
304 |
requests++; |
|
305 |
} |
|
306 |
} |
|
307 |
||
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
308 |
int readUntil(InputStream in, StringBuilder headers, char[] seq) throws IOException { |
2 | 309 |
int i=0, count=0; |
310 |
while (true) { |
|
311 |
int c = in.read(); |
|
312 |
if (c == -1) |
|
313 |
return -1; |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
314 |
headers.append((char)c); |
2 | 315 |
count++; |
316 |
if (c == seq[i]) { |
|
317 |
i++; |
|
318 |
if (i == seq.length) |
|
319 |
return count; |
|
320 |
continue; |
|
321 |
} else { |
|
322 |
i = 0; |
|
323 |
} |
|
324 |
} |
|
325 |
} |
|
326 |
||
327 |
public void run() { |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
328 |
long start = System.nanoTime(); |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
329 |
|
2 | 330 |
try { |
331 |
int max = 400; |
|
332 |
byte b[] = new byte[1000]; |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
333 |
InputStream in = new BufferedInputStream(s.getInputStream()); |
2 | 334 |
// response to client |
335 |
PrintStream out = new PrintStream( |
|
336 |
new BufferedOutputStream( |
|
337 |
s.getOutputStream() )); |
|
338 |
||
339 |
for (;;) { |
|
340 |
||
341 |
// read entire request from client |
|
342 |
int n=0; |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
343 |
StringBuilder headers = new StringBuilder(); |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
344 |
n = readUntil(in, headers, new char[] {'\r','\n', '\r','\n'}); |
2 | 345 |
if (n <= 0) { |
346 |
MultiThreadTest.debug("worker: " + id + ": Shutdown"); |
|
347 |
s.close(); |
|
348 |
return; |
|
349 |
} |
|
54885
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
350 |
if (headers.toString().contains("/foo.html?foo=")) { |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
351 |
headerFound = true; |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
352 |
} else { |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
353 |
MultiThreadTest.debug("worker: " + id + ": Unexpected request received: " + headers); |
e58e454c1409
8223716: sun/net/www/http/HttpClient/MultiThreadTest.java should be more resilient to unexpected traffic
dfuchs
parents:
54787
diff
changeset
|
354 |
} |
2 | 355 |
|
356 |
MultiThreadTest.debug("worker " + id + |
|
357 |
": Read request from client " + |
|
358 |
"(" + n + " bytes)."); |
|
359 |
||
360 |
incRequests(); |
|
361 |
out.print("HTTP/1.1 200 OK\r\n"); |
|
362 |
out.print("Transfer-Encoding: chunked\r\n"); |
|
363 |
out.print("Content-Type: text/html\r\n"); |
|
364 |
out.print("Connection: Keep-Alive\r\n"); |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
365 |
out.print("Keep-Alive: timeout=15, max="+max+"\r\n"); |
2 | 366 |
out.print("\r\n"); |
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
367 |
out.print("6\r\nHello \r\n"); |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
368 |
out.print("5\r\nWorld\r\n"); |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
369 |
out.print("0\r\n\r\n"); |
2 | 370 |
out.flush(); |
371 |
||
372 |
if (--max == 0) { |
|
373 |
s.close(); |
|
374 |
return; |
|
375 |
} |
|
376 |
} |
|
377 |
||
378 |
} catch (Exception e) { |
|
379 |
e.printStackTrace(); |
|
380 |
} finally { |
|
381 |
try { |
|
382 |
s.close(); |
|
383 |
} catch (Exception e) { } |
|
51947
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
384 |
MultiThreadTest.debug("worker: " + id + " end - " + |
2d980757fd07
8211092: test/jdk/sun/net/www/http/HttpClient/MultiThreadTest.java fails intermittently when cleaning up
chegar
parents:
47216
diff
changeset
|
385 |
Duration.ofNanos(System.nanoTime() - start)); |
2 | 386 |
} |
387 |
} |
|
388 |
} |