author | aeubanks |
Wed, 27 Mar 2019 09:06:43 -0700 | |
changeset 54314 | 46cf212cdcca |
parent 47216 | 71c04702a3d5 |
child 55309 | 8081b181bba8 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14342
8435a30053c1
7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents:
10075
diff
changeset
|
2 |
* Copyright (c) 2006, 2011, 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 6373555 |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
27 |
* @library /test/lib |
2 | 28 |
* @summary HTTP Server failing to answer client requests |
29 |
*/ |
|
30 |
||
31 |
import java.net.*; |
|
32 |
import java.io.*; |
|
33 |
import java.util.*; |
|
34 |
import com.sun.net.httpserver.*; |
|
35 |
import java.util.concurrent.*; |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
36 |
import jdk.test.lib.net.URIBuilder; |
2 | 37 |
|
38 |
public class B6373555 { |
|
39 |
||
40 |
private static int s_received = 0; |
|
41 |
private static int sent = 0; |
|
42 |
||
43 |
private static int received = 0; |
|
44 |
private static int port; |
|
45 |
||
46 |
private static volatile boolean error = false; |
|
47 |
static HttpServer httpServer; |
|
48 |
static ExecutorService pool, execs; |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
49 |
static int NUM = 1000; |
2 | 50 |
|
51 |
public static void main(String[] args) throws Exception { |
|
52 |
try { |
|
53 |
if (args.length > 0) { |
|
54 |
NUM = Integer.parseInt (args[0]); |
|
55 |
} |
|
56 |
execs = Executors.newFixedThreadPool(5); |
|
57 |
httpServer = createHttpServer(execs); |
|
58 |
port = httpServer.getAddress().getPort(); |
|
59 |
pool = Executors.newFixedThreadPool(10); |
|
60 |
httpServer.start(); |
|
61 |
for (int i=0; i < NUM; i++) { |
|
62 |
pool.execute(new Client()); |
|
63 |
if (error) { |
|
64 |
throw new Exception ("error in test"); |
|
65 |
} |
|
66 |
} |
|
67 |
System.out.println("Main thread waiting"); |
|
68 |
pool.shutdown(); |
|
69 |
long latest = System.currentTimeMillis() + 200 * 1000; |
|
70 |
while (System.currentTimeMillis() < latest) { |
|
71 |
if (pool.awaitTermination(2000L, TimeUnit.MILLISECONDS)) { |
|
72 |
System.out.println("Main thread done!"); |
|
73 |
return; |
|
74 |
} |
|
75 |
if (error) { |
|
76 |
throw new Exception ("error in test"); |
|
77 |
} |
|
78 |
} |
|
79 |
throw new Exception ("error in test: timed out"); |
|
80 |
} finally { |
|
81 |
httpServer.stop(0); |
|
82 |
pool.shutdownNow(); |
|
83 |
execs.shutdownNow(); |
|
84 |
} |
|
85 |
} |
|
86 |
||
87 |
public static class Client implements Runnable { |
|
88 |
||
89 |
byte[] getBuf () { |
|
90 |
byte[] buf = new byte [5200]; |
|
91 |
for (int i=0; i< 5200; i++) { |
|
92 |
buf [i] = (byte)i; |
|
93 |
} |
|
94 |
return buf; |
|
95 |
} |
|
96 |
||
97 |
public void run() { |
|
98 |
try { |
|
99 |
Thread.sleep(10); |
|
100 |
byte[] buf = getBuf(); |
|
54314
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
101 |
URL url = URIBuilder.newBuilder() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
102 |
.scheme("http") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
103 |
.loopback() |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
104 |
.port(port) |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
105 |
.path("/test") |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
106 |
.toURLUnchecked(); |
46cf212cdcca
8220575: Replace hardcoded 127.0.0.1 in URLs with new URI builder
aeubanks
parents:
47216
diff
changeset
|
107 |
System.out.println("URL: " + url); |
2 | 108 |
HttpURLConnection con = (HttpURLConnection)url.openConnection(); |
109 |
con.setDoOutput(true); |
|
110 |
con.setDoInput(true); |
|
111 |
con.setRequestMethod("POST"); |
|
112 |
con.setRequestProperty( |
|
113 |
"Content-Type", |
|
114 |
"Multipart/Related; type=\"application/xop+xml\"; boundary=\"----=_Part_0_6251267.1128549570165\"; start-info=\"text/xml\""); |
|
115 |
OutputStream out = con.getOutputStream(); |
|
116 |
out.write(buf); |
|
117 |
out.close(); |
|
118 |
InputStream in = con.getInputStream(); |
|
119 |
byte[] newBuf = readFully(in); |
|
120 |
in.close(); |
|
121 |
if (buf.length != newBuf.length) { |
|
122 |
System.out.println("Doesn't match"); |
|
123 |
error = true; |
|
124 |
} |
|
125 |
} |
|
126 |
catch(Exception e) { |
|
6115
7c523cf2bc8a
6969395: TEST_BUG: Tests in java/net sun/net problems
chegar
parents:
5506
diff
changeset
|
127 |
e.printStackTrace(); |
2 | 128 |
System.out.print ("."); |
129 |
error = true; |
|
130 |
} |
|
131 |
} |
|
132 |
} |
|
133 |
||
134 |
private static byte[] readFully(InputStream istream) throws IOException { |
|
135 |
ByteArrayOutputStream bout = new ByteArrayOutputStream(); |
|
136 |
byte[] buf = new byte[1024]; |
|
137 |
int num = 0; |
|
138 |
||
139 |
if (istream != null) { |
|
140 |
while ((num = istream.read(buf)) != -1) { |
|
141 |
bout.write(buf, 0, num); |
|
142 |
} |
|
143 |
} |
|
144 |
byte[] ret = bout.toByteArray(); |
|
145 |
return ret; |
|
146 |
} |
|
147 |
||
148 |
||
149 |
private static HttpServer createHttpServer(ExecutorService execs) |
|
150 |
throws Exception { |
|
151 |
InetSocketAddress inetAddress = new InetSocketAddress(0); |
|
10075
c8d9bac189cc
7058832: com/sun/net/httpserver/bugs/B6373555.java failing intermittently
michaelm
parents:
7668
diff
changeset
|
152 |
HttpServer testServer = HttpServer.create(inetAddress, 15); |
2 | 153 |
testServer.setExecutor(execs); |
154 |
HttpContext context = testServer.createContext("/test"); |
|
155 |
context.setHandler(new HttpHandler() { |
|
156 |
public void handle(HttpExchange msg) { |
|
157 |
try { |
|
158 |
String method = msg.getRequestMethod(); |
|
159 |
if (method.equals("POST")) { |
|
160 |
InputStream is = msg.getRequestBody(); |
|
161 |
byte[] buf = readFully(is); |
|
162 |
is.close(); |
|
163 |
writePostReply(msg, buf); |
|
164 |
} else { |
|
165 |
System.out.println("****** METHOD not handled ***** "+method); |
|
166 |
System.out.println("Received="+s_received); |
|
167 |
} |
|
168 |
} |
|
169 |
catch(Exception e) { |
|
170 |
e.printStackTrace(); |
|
171 |
} |
|
172 |
finally { |
|
173 |
msg.close(); |
|
174 |
} |
|
175 |
} |
|
176 |
} |
|
177 |
); |
|
178 |
return testServer; |
|
179 |
} |
|
180 |
||
181 |
private static void writePostReply(HttpExchange msg, byte[] buf) |
|
182 |
throws Exception { |
|
183 |
msg.getResponseHeaders().add("Content-Type", "text/xml"); |
|
184 |
msg.sendResponseHeaders(200, buf.length); |
|
185 |
OutputStream out = msg.getResponseBody(); |
|
186 |
out.write(buf); |
|
187 |
out.close(); |
|
188 |
} |
|
189 |
||
190 |
} |