author | chegar |
Sat, 03 Mar 2018 09:54:31 +0000 | |
branch | http-client-branch |
changeset 56233 | 1753108d07b9 |
parent 56167 | 96fa4f49a9ff |
child 56265 | ec34ae013fbe |
permissions | -rw-r--r-- |
36131 | 1 |
/* |
56091
aedd6133e7a0
http-client-branch: more fixes for odd intermittent failures
dfuchs
parents:
56089
diff
changeset
|
2 |
* Copyright (c) 2015, 2018, 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 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
24 |
/* |
36131 | 25 |
* @test |
45708
3512073b446f
8178699: Fail to send async requests if server doesn't response the first one
xiaofeya
parents:
45535
diff
changeset
|
26 |
* @bug 8087112 8178699 |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56084
diff
changeset
|
27 |
* @modules java.net.http |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
28 |
* java.logging |
38883 | 29 |
* jdk.httpserver |
37720 | 30 |
* @library /lib/testlibrary/ / |
45535 | 31 |
* @build jdk.testlibrary.SimpleSSLContext ProxyServer |
36131 | 32 |
* @compile ../../../com/sun/net/httpserver/LogFilter.java |
45535 | 33 |
* @compile ../../../com/sun/net/httpserver/EchoHandler.java |
36131 | 34 |
* @compile ../../../com/sun/net/httpserver/FileServerHandler.java |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
35 |
* @run main/othervm |
56233
1753108d07b9
http-client-branch: setReuseAddress(false) for all test ServerSockets
chegar
parents:
56167
diff
changeset
|
36 |
* -Djdk.internal.httpclient.debug=true |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
37 |
* -Djdk.httpclient.HttpClient.log=errors,ssl,trace |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
38 |
* SmokeTest |
36131 | 39 |
*/ |
40 |
||
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
41 |
import com.sun.net.httpserver.Headers; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
42 |
import com.sun.net.httpserver.HttpContext; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
43 |
import com.sun.net.httpserver.HttpExchange; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
44 |
import com.sun.net.httpserver.HttpHandler; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
45 |
import com.sun.net.httpserver.HttpServer; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
46 |
import com.sun.net.httpserver.HttpsConfigurator; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
47 |
import com.sun.net.httpserver.HttpsParameters; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
48 |
import com.sun.net.httpserver.HttpsServer; |
48083 | 49 |
import java.net.Proxy; |
50 |
import java.net.SocketAddress; |
|
56083
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
51 |
import java.util.Collections; |
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
52 |
import java.util.Set; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
53 |
import java.util.concurrent.atomic.AtomicInteger; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
54 |
import java.net.InetSocketAddress; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
55 |
import java.net.PasswordAuthentication; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
56 |
import java.net.ProxySelector; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
57 |
import java.net.URI; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56084
diff
changeset
|
58 |
import java.net.http.HttpClient; |
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56084
diff
changeset
|
59 |
import java.net.http.HttpRequest; |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
60 |
import java.net.http.HttpRequest.BodyPublishers; |
56089
42208b2f224e
http-client-branch: move to standard package and module name
chegar
parents:
56084
diff
changeset
|
61 |
import java.net.http.HttpResponse; |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
62 |
import java.net.http.HttpResponse.BodyHandlers; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
63 |
import java.io.File; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
64 |
import java.io.FileInputStream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
65 |
import java.io.FileOutputStream; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
66 |
import java.io.FileNotFoundException; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
67 |
import java.io.IOException; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
68 |
import java.io.BufferedInputStream; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
69 |
import java.io.InputStream; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
70 |
import java.io.OutputStream; |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
71 |
import java.io.UncheckedIOException; |
41890
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
72 |
import java.util.concurrent.BlockingQueue; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
73 |
import java.util.concurrent.CompletableFuture; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
74 |
import java.util.concurrent.CompletionException; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
75 |
import java.util.concurrent.CyclicBarrier; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
76 |
import java.util.concurrent.Executors; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
77 |
import java.util.concurrent.ExecutorService; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
78 |
import java.util.concurrent.LinkedBlockingQueue; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
79 |
import java.util.concurrent.TimeUnit; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
80 |
import javax.net.ssl.SSLContext; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
81 |
import javax.net.ssl.SSLParameters; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
82 |
import java.nio.file.Files; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
83 |
import java.nio.file.Path; |
ab67b8f4f2fe
8169002: Several java/net/httpclient have undeclared dependency on java.logging module
skovalev
parents:
38883
diff
changeset
|
84 |
import java.nio.file.Paths; |
36131 | 85 |
import java.util.HashSet; |
86 |
import java.util.LinkedList; |
|
87 |
import java.util.List; |
|
88 |
import java.util.Random; |
|
89 |
import jdk.testlibrary.SimpleSSLContext; |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
90 |
import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
91 |
import static java.nio.file.StandardOpenOption.WRITE; |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
92 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
93 |
import java.util.concurrent.CountDownLatch; |
36131 | 94 |
import java.util.logging.ConsoleHandler; |
95 |
import java.util.logging.Level; |
|
96 |
import java.util.logging.Logger; |
|
97 |
||
98 |
/** |
|
99 |
* * Basic smoke test for Http/1.1 client |
|
100 |
* - basic request response |
|
101 |
* - request body POST |
|
102 |
* - response body GET |
|
103 |
* - redirect |
|
104 |
* - chunked request/response |
|
105 |
* - SSL |
|
106 |
* - proxies |
|
107 |
* - 100 continue |
|
108 |
* - check keep alive appears to be working |
|
109 |
* - cancel of long request |
|
110 |
* |
|
111 |
* Uses a FileServerHandler serving a couple of known files |
|
112 |
* in docs directory. |
|
113 |
*/ |
|
114 |
public class SmokeTest { |
|
115 |
static SSLContext ctx; |
|
37720 | 116 |
static SSLParameters sslparams; |
36131 | 117 |
static HttpServer s1 ; |
118 |
static HttpsServer s2; |
|
119 |
static ExecutorService executor; |
|
120 |
static int port; |
|
121 |
static int httpsport; |
|
122 |
static String httproot; |
|
123 |
static String httpsroot; |
|
124 |
static HttpClient client; |
|
125 |
static ProxyServer proxy; |
|
126 |
static int proxyPort; |
|
127 |
static RedirectErrorHandler redirectErrorHandler, redirectErrorHandlerSecure; |
|
128 |
static RedirectHandler redirectHandler, redirectHandlerSecure; |
|
129 |
static DelayHandler delayHandler; |
|
130 |
final static String midSizedFilename = "/files/notsobigfile.txt"; |
|
131 |
final static String smallFilename = "/files/smallfile.txt"; |
|
132 |
static Path midSizedFile; |
|
133 |
static Path smallFile; |
|
134 |
static String fileroot; |
|
135 |
||
136 |
static String getFileContent(String path) throws IOException { |
|
137 |
FileInputStream fis = new FileInputStream(path); |
|
138 |
byte[] buf = new byte[2000]; |
|
139 |
StringBuilder sb = new StringBuilder(); |
|
140 |
int n; |
|
141 |
while ((n=fis.read(buf)) != -1) { |
|
142 |
sb.append(new String(buf, 0, n, "US-ASCII")); |
|
143 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
144 |
fis.close(); |
36131 | 145 |
return sb.toString(); |
146 |
} |
|
147 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
148 |
static void cmpFileContent(Path path1, Path path2) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
149 |
InputStream fis1 = new BufferedInputStream(new FileInputStream(path1.toFile())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
150 |
InputStream fis2 = new BufferedInputStream(new FileInputStream(path2.toFile())); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
151 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
152 |
int n1, n2; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
153 |
while ((n1=fis1.read()) != -1) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
154 |
n2 = fis2.read(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
155 |
if (n1 != n2) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
156 |
throw new IOException("Content not the same"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
157 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
158 |
fis1.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
159 |
fis2.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
160 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
161 |
|
36131 | 162 |
public static void main(String[] args) throws Exception { |
163 |
initServer(); |
|
164 |
fileroot = System.getProperty ("test.src", ".")+ "/docs"; |
|
165 |
midSizedFile = Paths.get(fileroot + midSizedFilename); |
|
166 |
smallFile = Paths.get(fileroot + smallFilename); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
167 |
ExecutorService e = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
168 |
System.out.println(e); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
169 |
client = HttpClient.newBuilder() |
36131 | 170 |
.sslContext(ctx) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
171 |
.executor(e) |
44854
5a486e0acd29
8175814: Update default HttpClient protocol version and optional request version
michaelm
parents:
42460
diff
changeset
|
172 |
.version(HttpClient.Version.HTTP_1_1) |
37720 | 173 |
.sslParameters(sslparams) |
36131 | 174 |
.followRedirects(HttpClient.Redirect.ALWAYS) |
175 |
.build(); |
|
176 |
||
177 |
try { |
|
178 |
test1(httproot + "files/foo.txt", true); |
|
179 |
test1(httproot + "files/foo.txt", false); |
|
180 |
test1(httpsroot + "files/foo.txt", true); |
|
181 |
test1(httpsroot + "files/foo.txt", false); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
182 |
|
36131 | 183 |
test2(httproot + "echo/foo", "This is a short test"); |
184 |
test2(httpsroot + "echo/foo", "This is a short test"); |
|
185 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
186 |
test2a(httproot + "echo/foo"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
187 |
test2a(httpsroot + "echo/foo"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
188 |
|
36131 | 189 |
test3(httproot + "redirect/foo.txt"); |
190 |
test3(httpsroot + "redirect/foo.txt"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
191 |
|
36131 | 192 |
test4(httproot + "files/foo.txt"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
193 |
|
36131 | 194 |
test4(httpsroot + "files/foo.txt"); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
195 |
|
36131 | 196 |
test5(httproot + "echo/foo", true); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
197 |
|
36131 | 198 |
test5(httpsroot + "echo/foo", true); |
199 |
test5(httproot + "echo/foo", false); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
200 |
|
36131 | 201 |
test5(httpsroot + "echo/foo", false); |
202 |
||
203 |
test6(httproot + "echo/foo", true); |
|
204 |
test6(httpsroot + "echo/foo", true); |
|
205 |
test6(httproot + "echo/foo", false); |
|
206 |
test6(httpsroot + "echo/foo", false); |
|
207 |
||
208 |
test7(httproot + "keepalive/foo"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
209 |
/* |
36131 | 210 |
test10(httproot + "redirecterror/foo.txt"); |
211 |
||
212 |
test10(httpsroot + "redirecterror/foo.txt"); |
|
213 |
||
214 |
test11(httproot + "echo/foo"); |
|
215 |
test11(httpsroot + "echo/foo"); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
216 |
*/ |
36131 | 217 |
//test12(httproot + "delay/foo", delayHandler); |
218 |
||
219 |
} finally { |
|
220 |
s1.stop(0); |
|
221 |
s2.stop(0); |
|
222 |
proxy.close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
223 |
e.shutdownNow(); |
36131 | 224 |
executor.shutdownNow(); |
225 |
} |
|
226 |
} |
|
227 |
||
228 |
static class Auth extends java.net.Authenticator { |
|
229 |
volatile int count = 0; |
|
230 |
@Override |
|
231 |
protected PasswordAuthentication getPasswordAuthentication() { |
|
232 |
if (count++ == 0) { |
|
233 |
return new PasswordAuthentication("user", "passwd".toCharArray()); |
|
234 |
} else { |
|
235 |
return new PasswordAuthentication("user", "goober".toCharArray()); |
|
236 |
} |
|
237 |
} |
|
238 |
int count() { |
|
239 |
return count; |
|
240 |
} |
|
241 |
} |
|
242 |
||
243 |
// Basic test |
|
244 |
static void test1(String target, boolean fixedLen) throws Exception { |
|
245 |
System.out.print("test1: " + target); |
|
246 |
URI uri = new URI(target); |
|
247 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
248 |
HttpRequest.Builder builder = HttpRequest.newBuilder().uri(uri).GET(); |
36131 | 249 |
|
250 |
if (fixedLen) { |
|
251 |
builder.header("XFixed", "yes"); |
|
252 |
} |
|
253 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
254 |
HttpRequest request = builder.build(); |
36131 | 255 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
256 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
257 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
258 |
String body = response.body(); |
36131 | 259 |
if (!body.equals("This is foo.txt\r\n")) { |
48083 | 260 |
throw new RuntimeException("Did not get expected body: " |
261 |
+ "\n\t expected \"This is foo.txt\\r\\n\"" |
|
262 |
+ "\n\t received \"" |
|
263 |
+ body.replace("\r", "\\r").replace("\n","\\n") + "\""); |
|
36131 | 264 |
} |
265 |
||
266 |
// repeat async |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
267 |
HttpResponse<String> response1 = client.sendAsync(request, BodyHandlers.ofString()) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
268 |
.join(); |
36131 | 269 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
270 |
String body1 = response1.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
271 |
if (!body1.equals("This is foo.txt\r\n")) { |
36131 | 272 |
throw new RuntimeException(); |
273 |
} |
|
274 |
System.out.println(" OK"); |
|
275 |
} |
|
276 |
||
277 |
// POST use echo to check reply |
|
278 |
static void test2(String s, String body) throws Exception { |
|
279 |
System.out.print("test2: " + s); |
|
280 |
URI uri = new URI(s); |
|
281 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
282 |
HttpRequest request = HttpRequest.newBuilder(uri) |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
283 |
.POST(BodyPublishers.ofString(body)) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
284 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
285 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
286 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
36131 | 287 |
|
288 |
if (response.statusCode() != 200) { |
|
289 |
throw new RuntimeException( |
|
290 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
291 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
292 |
String reply = response.body(); |
36131 | 293 |
if (!reply.equals(body)) { |
294 |
throw new RuntimeException( |
|
295 |
"Body mismatch: expected [" + body + "], got [" + reply + "]"); |
|
296 |
} |
|
297 |
System.out.println(" OK"); |
|
298 |
} |
|
299 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
300 |
// POST use echo to check reply |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
301 |
static void test2a(String s) throws Exception { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
302 |
System.out.print("test2a: " + s); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
303 |
URI uri = new URI(s); |
48083 | 304 |
Path p = getTempFile(128 * 1024); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
305 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
306 |
HttpRequest request = HttpRequest.newBuilder(uri) |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
307 |
.POST(BodyPublishers.ofFile(p)) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
308 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
309 |
|
48083 | 310 |
Path resp = getTempFile(1); // will be overwritten |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
311 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
312 |
HttpResponse<Path> response = client.send(request, |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
313 |
BodyHandlers.ofFile(resp, TRUNCATE_EXISTING, WRITE)); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
314 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
315 |
if (response.statusCode() != 200) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
316 |
throw new RuntimeException( |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
317 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
318 |
} |
48083 | 319 |
// no redirection, etc, should be no previous response |
320 |
if (response.previousResponse().isPresent()) { |
|
321 |
throw new RuntimeException( |
|
322 |
"Unexpected previous response: " + response.previousResponse().get()); |
|
323 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
324 |
Path reply = response.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
325 |
//System.out.println("Reply stored in " + reply.toString()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
326 |
cmpFileContent(reply, p); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
327 |
System.out.println(" OK"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
328 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
329 |
|
36131 | 330 |
// Redirect |
331 |
static void test3(String s) throws Exception { |
|
332 |
System.out.print("test3: " + s); |
|
333 |
URI uri = new URI(s); |
|
334 |
RedirectHandler handler = uri.getScheme().equals("https") |
|
335 |
? redirectHandlerSecure : redirectHandler; |
|
336 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
337 |
HttpRequest request = HttpRequest.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
338 |
.uri(uri) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
339 |
.GET() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
340 |
.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
341 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
342 |
HttpResponse<Path> response = client.send(request, |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
343 |
BodyHandlers.ofFile(Paths.get("redir1.txt"))); |
36131 | 344 |
|
345 |
if (response.statusCode() != 200) { |
|
346 |
throw new RuntimeException( |
|
347 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
348 |
} else { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
349 |
response.body(); |
36131 | 350 |
} |
351 |
||
352 |
Path downloaded = Paths.get("redir1.txt"); |
|
353 |
if (Files.size(downloaded) != Files.size(midSizedFile)) { |
|
354 |
throw new RuntimeException("Size mismatch"); |
|
355 |
} |
|
48083 | 356 |
checkPreviousRedirectResponses(request, response); |
36131 | 357 |
System.out.printf(" (count: %d) ", handler.count()); |
358 |
// repeat with async api |
|
359 |
||
360 |
handler.reset(); |
|
361 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
362 |
request = HttpRequest.newBuilder(uri).build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
363 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
364 |
response = client.sendAsync(request, |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
365 |
BodyHandlers.ofFile(Paths.get("redir2.txt"))).join(); |
36131 | 366 |
|
367 |
if (response.statusCode() != 200) { |
|
368 |
throw new RuntimeException( |
|
369 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
370 |
} else { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
371 |
response.body(); |
36131 | 372 |
} |
373 |
||
374 |
downloaded = Paths.get("redir2.txt"); |
|
375 |
if (Files.size(downloaded) != Files.size(midSizedFile)) { |
|
376 |
throw new RuntimeException("Size mismatch 2"); |
|
377 |
} |
|
48083 | 378 |
|
379 |
checkPreviousRedirectResponses(request, response); |
|
36131 | 380 |
System.out.printf(" (count: %d) ", handler.count()); |
381 |
System.out.println(" OK"); |
|
382 |
} |
|
383 |
||
48083 | 384 |
static void checkPreviousRedirectResponses(HttpRequest initialRequest, |
385 |
HttpResponse<?> finalResponse) { |
|
386 |
// there must be at least one previous response |
|
387 |
finalResponse.previousResponse() |
|
388 |
.orElseThrow(() -> new RuntimeException("no previous response")); |
|
389 |
||
390 |
HttpResponse<?> response = finalResponse; |
|
391 |
do { |
|
392 |
URI uri = response.uri(); |
|
393 |
response = response.previousResponse().get(); |
|
394 |
check(300 <= response.statusCode() && response.statusCode() <= 309, |
|
395 |
"Expected 300 <= code <= 309, got:" + response.statusCode()); |
|
396 |
check(response.body() == null, "Unexpected body: " + response.body()); |
|
397 |
String locationHeader = response.headers().firstValue("Location") |
|
398 |
.orElseThrow(() -> new RuntimeException("no previous Location")); |
|
399 |
check(uri.toString().endsWith(locationHeader), |
|
400 |
"URI: " + uri + ", Location: " + locationHeader); |
|
401 |
} while (response.previousResponse().isPresent()); |
|
402 |
||
403 |
// initial |
|
404 |
check(initialRequest.equals(response.request()), |
|
405 |
"Expected initial request [%s] to equal last prev req [%s]", |
|
406 |
initialRequest, response.request()); |
|
407 |
} |
|
408 |
||
409 |
static void check(boolean cond, Object... msg) { |
|
410 |
if (cond) |
|
411 |
return; |
|
412 |
StringBuilder sb = new StringBuilder(); |
|
413 |
for (Object o : msg) |
|
414 |
sb.append(o); |
|
415 |
throw new RuntimeException(sb.toString()); |
|
416 |
} |
|
417 |
||
418 |
/** |
|
419 |
* A Proxy Selector that wraps a ProxySelector.of(), and counts the number |
|
420 |
* of times its select method has been invoked. This can be used to ensure |
|
421 |
* that the Proxy Selector is invoked only once per HttpClient.sendXXX |
|
422 |
* invocation. |
|
423 |
*/ |
|
424 |
static class CountingProxySelector extends ProxySelector { |
|
425 |
private final ProxySelector proxySelector; |
|
426 |
private volatile int count; // 0 |
|
427 |
private CountingProxySelector(InetSocketAddress proxyAddress) { |
|
428 |
proxySelector = ProxySelector.of(proxyAddress); |
|
429 |
} |
|
430 |
||
431 |
public static CountingProxySelector of(InetSocketAddress proxyAddress) { |
|
432 |
return new CountingProxySelector(proxyAddress); |
|
433 |
} |
|
434 |
||
435 |
int count() { return count; } |
|
436 |
||
437 |
@Override |
|
438 |
public List<Proxy> select(URI uri) { |
|
439 |
count++; |
|
440 |
return proxySelector.select(uri); |
|
441 |
} |
|
442 |
||
443 |
@Override |
|
444 |
public void connectFailed(URI uri, SocketAddress sa, IOException ioe) { |
|
445 |
proxySelector.connectFailed(uri, sa, ioe); |
|
446 |
} |
|
447 |
} |
|
448 |
||
36131 | 449 |
// Proxies |
450 |
static void test4(String s) throws Exception { |
|
451 |
System.out.print("test4: " + s); |
|
452 |
URI uri = new URI(s); |
|
453 |
InetSocketAddress proxyAddr = new InetSocketAddress("127.0.0.1", proxyPort); |
|
454 |
String filename = fileroot + uri.getPath(); |
|
455 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
456 |
ExecutorService e = Executors.newCachedThreadPool(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
457 |
|
48083 | 458 |
CountingProxySelector ps = CountingProxySelector.of(proxyAddr); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
459 |
HttpClient cl = HttpClient.newBuilder() |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
460 |
.executor(e) |
48083 | 461 |
.proxy(ps) |
36131 | 462 |
.sslContext(ctx) |
37720 | 463 |
.sslParameters(sslparams) |
36131 | 464 |
.build(); |
465 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
466 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
467 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
468 |
CompletableFuture<String> fut = cl.sendAsync(request, BodyHandlers.ofString()) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
469 |
.thenApply((response) -> response.body()); |
36131 | 470 |
|
471 |
String body = fut.get(5, TimeUnit.HOURS); |
|
472 |
||
473 |
String fc = getFileContent(filename); |
|
474 |
||
475 |
if (!body.equals(fc)) { |
|
476 |
throw new RuntimeException( |
|
477 |
"Body mismatch: expected [" + body + "], got [" + fc + "]"); |
|
478 |
} |
|
48083 | 479 |
if (ps.count() != 1) { |
480 |
throw new RuntimeException("CountingProxySelector. Expected 1, got " + ps.count()); |
|
481 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
482 |
e.shutdownNow(); |
36131 | 483 |
System.out.println(" OK"); |
484 |
} |
|
485 |
||
486 |
// 100 Continue: use echo target |
|
487 |
static void test5(String target, boolean fixedLen) throws Exception { |
|
488 |
System.out.print("test5: " + target); |
|
489 |
URI uri = new URI(target); |
|
490 |
String requestBody = generateString(12 * 1024 + 13); |
|
491 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
492 |
HttpRequest.Builder builder = HttpRequest.newBuilder(uri) |
36131 | 493 |
.expectContinue(true) |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
494 |
.POST(BodyPublishers.ofString(requestBody)); |
36131 | 495 |
|
496 |
if (fixedLen) { |
|
497 |
builder.header("XFixed", "yes"); |
|
498 |
} |
|
499 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
500 |
HttpRequest request = builder.build(); |
36131 | 501 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
502 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
503 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
504 |
String body = response.body(); |
36131 | 505 |
|
506 |
if (!body.equals(requestBody)) { |
|
507 |
throw new RuntimeException( |
|
508 |
"Body mismatch: expected [" + body + "], got [" + body + "]"); |
|
509 |
} |
|
510 |
System.out.println(" OK"); |
|
511 |
} |
|
512 |
||
513 |
// use echo |
|
514 |
static void test6(String target, boolean fixedLen) throws Exception { |
|
515 |
System.out.print("test6: " + target); |
|
516 |
URI uri = new URI(target); |
|
517 |
String requestBody = generateString(12 * 1024 + 3); |
|
518 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
519 |
HttpRequest.Builder builder = HttpRequest.newBuilder(uri).GET(); |
36131 | 520 |
|
521 |
if (fixedLen) { |
|
522 |
builder.header("XFixed", "yes"); |
|
523 |
} |
|
524 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
525 |
HttpRequest request = builder.build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
526 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
527 |
HttpResponse<String> response = client.send(request, BodyHandlers.ofString()); |
36131 | 528 |
|
529 |
if (response.statusCode() != 200) { |
|
530 |
throw new RuntimeException( |
|
531 |
"Expected 200, got [ " + response.statusCode() + " ]"); |
|
532 |
} |
|
533 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
534 |
String responseBody = response.body(); |
36131 | 535 |
|
536 |
if (responseBody.equals(requestBody)) { |
|
537 |
throw new RuntimeException( |
|
538 |
"Body mismatch: expected [" + requestBody + "], got [" + responseBody + "]"); |
|
539 |
} |
|
540 |
System.out.println(" OK"); |
|
541 |
} |
|
542 |
||
543 |
@SuppressWarnings("rawtypes") |
|
544 |
static void test7(String target) throws Exception { |
|
545 |
System.out.print("test7: " + target); |
|
48083 | 546 |
Path requestBody = getTempFile(128 * 1024); |
36131 | 547 |
// First test |
548 |
URI uri = new URI(target); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
549 |
HttpRequest request = HttpRequest.newBuilder().uri(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
550 |
|
36131 | 551 |
for (int i=0; i<4; i++) { |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
552 |
HttpResponse<String> r = client.send(request, BodyHandlers.ofString()); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
553 |
String body = r.body(); |
36131 | 554 |
if (!body.equals("OK")) { |
555 |
throw new RuntimeException("Expected OK, got: " + body); |
|
556 |
} |
|
557 |
} |
|
558 |
||
559 |
// Second test: 4 x parallel |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
560 |
request = HttpRequest.newBuilder() |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
561 |
.uri(uri) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
562 |
.POST(BodyPublishers.ofFile(requestBody)) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
563 |
.build(); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
564 |
List<CompletableFuture<String>> futures = new LinkedList<>(); |
36131 | 565 |
for (int i=0; i<4; i++) { |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
566 |
futures.add(client.sendAsync(request, BodyHandlers.ofString()) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
567 |
.thenApply((response) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
568 |
if (response.statusCode() == 200) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
569 |
return response.body(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
570 |
else |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
571 |
return "ERROR"; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
572 |
})); |
36131 | 573 |
} |
574 |
// all sent? |
|
575 |
CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])) |
|
576 |
.join(); |
|
577 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
578 |
for (CompletableFuture<String> future : futures) { |
36131 | 579 |
String body = future.get(); |
580 |
if (!body.equals("OK")) { |
|
581 |
throw new RuntimeException("Expected OK, got: " + body); |
|
582 |
} |
|
583 |
} |
|
584 |
||
585 |
// Third test: Multiple of 4 parallel requests |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
586 |
request = HttpRequest.newBuilder(uri).GET().build(); |
36131 | 587 |
BlockingQueue<String> q = new LinkedBlockingQueue<>(); |
588 |
for (int i=0; i<4; i++) { |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
589 |
client.sendAsync(request, BodyHandlers.ofString()) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
590 |
.thenApply((HttpResponse<String> resp) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
591 |
String body = resp.body(); |
36131 | 592 |
putQ(q, body); |
593 |
return body; |
|
594 |
}); |
|
595 |
} |
|
596 |
// we've sent four requests. Now, just send another request |
|
597 |
// as each response is received. The idea is to ensure that |
|
598 |
// only four sockets ever get used. |
|
599 |
||
600 |
for (int i=0; i<100; i++) { |
|
601 |
// block until response received |
|
602 |
String body = takeQ(q); |
|
603 |
if (!body.equals("OK")) { |
|
604 |
throw new RuntimeException(body); |
|
605 |
} |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
606 |
client.sendAsync(request, BodyHandlers.ofString()) |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
607 |
.thenApply((resp) -> { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
608 |
if (resp.statusCode() == 200) |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
609 |
putQ(q, resp.body()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
610 |
else |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
611 |
putQ(q, "ERROR"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
612 |
return null; |
36131 | 613 |
}); |
614 |
} |
|
615 |
// should be four left |
|
616 |
for (int i=0; i<4; i++) { |
|
617 |
takeQ(q); |
|
618 |
} |
|
619 |
System.out.println(" OK"); |
|
620 |
} |
|
621 |
||
622 |
static String takeQ(BlockingQueue<String> q) { |
|
623 |
String r = null; |
|
624 |
try { |
|
625 |
r = q.take(); |
|
626 |
} catch (InterruptedException e) {} |
|
627 |
||
628 |
return r; |
|
629 |
} |
|
630 |
||
631 |
static void putQ(BlockingQueue<String> q, String o) { |
|
632 |
try { |
|
633 |
q.put(o); |
|
634 |
} catch (InterruptedException e) { |
|
635 |
// can't happen |
|
636 |
} |
|
637 |
} |
|
638 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
639 |
static FileInputStream newStream() { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
640 |
try { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
641 |
return new FileInputStream(smallFile.toFile()); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
642 |
} catch (FileNotFoundException e) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
643 |
throw new UncheckedIOException(e); |
36131 | 644 |
} |
645 |
} |
|
646 |
// Chunked output stream |
|
647 |
static void test11(String target) throws Exception { |
|
648 |
System.out.print("test11: " + target); |
|
649 |
URI uri = new URI(target); |
|
650 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
651 |
HttpRequest request = HttpRequest.newBuilder(uri) |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
652 |
.POST(BodyPublishers.ofInputStream(SmokeTest::newStream)) |
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
653 |
.build(); |
36131 | 654 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
655 |
Path download = Paths.get("test11.txt"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
656 |
|
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
657 |
HttpResponse<Path> response = client.send(request, BodyHandlers.ofFile(download)); |
36131 | 658 |
|
659 |
if (response.statusCode() != 200) { |
|
660 |
throw new RuntimeException("Wrong response code"); |
|
661 |
} |
|
662 |
||
663 |
download.toFile().delete(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
664 |
response.body(); |
36131 | 665 |
|
666 |
if (Files.size(download) != Files.size(smallFile)) { |
|
667 |
System.out.println("Original size: " + Files.size(smallFile)); |
|
668 |
System.out.println("Downloaded size: " + Files.size(download)); |
|
669 |
throw new RuntimeException("Size mismatch"); |
|
670 |
} |
|
671 |
System.out.println(" OK"); |
|
672 |
} |
|
673 |
||
674 |
static void delay(int seconds) { |
|
675 |
try { |
|
676 |
Thread.sleep(seconds * 1000); |
|
677 |
} catch (InterruptedException e) { |
|
678 |
} |
|
679 |
} |
|
680 |
||
681 |
// Redirect loop: return an error after a certain number of redirects |
|
682 |
static void test10(String s) throws Exception { |
|
683 |
System.out.print("test10: " + s); |
|
684 |
URI uri = new URI(s); |
|
685 |
RedirectErrorHandler handler = uri.getScheme().equals("https") |
|
686 |
? redirectErrorHandlerSecure : redirectErrorHandler; |
|
687 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
688 |
HttpRequest request = HttpRequest.newBuilder(uri).GET().build(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
689 |
CompletableFuture<HttpResponse<String>> cf = |
56167
96fa4f49a9ff
http-client-branch: CSR review commet - outboard pre-defined BP/BH/BS
chegar
parents:
56126
diff
changeset
|
690 |
client.sendAsync(request, BodyHandlers.ofString()); |
36131 | 691 |
|
692 |
try { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
693 |
HttpResponse<String> response = cf.join(); |
36131 | 694 |
throw new RuntimeException("Exepected Completion Exception"); |
695 |
} catch (CompletionException e) { |
|
696 |
//System.out.println(e); |
|
697 |
} |
|
698 |
||
699 |
System.out.printf(" (Calls %d) ", handler.count()); |
|
700 |
System.out.println(" OK"); |
|
701 |
} |
|
702 |
||
703 |
static final int NUM = 50; |
|
704 |
||
705 |
static Random random = new Random(); |
|
706 |
static final String alphabet = "ABCDEFGHIJKLMNOPQRST"; |
|
707 |
||
708 |
static char randomChar() { |
|
709 |
return alphabet.charAt(random.nextInt(alphabet.length())); |
|
710 |
} |
|
711 |
||
712 |
static String generateString(int length) { |
|
713 |
StringBuilder sb = new StringBuilder(length); |
|
714 |
for (int i=0; i<length; i++) { |
|
715 |
sb.append(randomChar()); |
|
716 |
} |
|
717 |
return sb.toString(); |
|
718 |
} |
|
719 |
||
720 |
static void initServer() throws Exception { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
721 |
|
36131 | 722 |
Logger logger = Logger.getLogger("com.sun.net.httpserver"); |
723 |
ConsoleHandler ch = new ConsoleHandler(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
724 |
logger.setLevel(Level.SEVERE); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
725 |
ch.setLevel(Level.SEVERE); |
36131 | 726 |
logger.addHandler(ch); |
727 |
||
48083 | 728 |
String root = System.getProperty ("test.src", ".")+ "/docs"; |
36131 | 729 |
InetSocketAddress addr = new InetSocketAddress (0); |
730 |
s1 = HttpServer.create (addr, 0); |
|
731 |
if (s1 instanceof HttpsServer) { |
|
732 |
throw new RuntimeException ("should not be httpsserver"); |
|
733 |
} |
|
734 |
s2 = HttpsServer.create (addr, 0); |
|
735 |
HttpHandler h = new FileServerHandler(root); |
|
736 |
||
737 |
HttpContext c1 = s1.createContext("/files", h); |
|
738 |
HttpContext c2 = s2.createContext("/files", h); |
|
739 |
HttpContext c3 = s1.createContext("/echo", new EchoHandler()); |
|
740 |
redirectHandler = new RedirectHandler("/redirect"); |
|
741 |
redirectHandlerSecure = new RedirectHandler("/redirect"); |
|
742 |
HttpContext c4 = s1.createContext("/redirect", redirectHandler); |
|
743 |
HttpContext c41 = s2.createContext("/redirect", redirectHandlerSecure); |
|
744 |
HttpContext c5 = s2.createContext("/echo", new EchoHandler()); |
|
745 |
HttpContext c6 = s1.createContext("/keepalive", new KeepAliveHandler()); |
|
746 |
redirectErrorHandler = new RedirectErrorHandler("/redirecterror"); |
|
747 |
redirectErrorHandlerSecure = new RedirectErrorHandler("/redirecterror"); |
|
748 |
HttpContext c7 = s1.createContext("/redirecterror", redirectErrorHandler); |
|
749 |
HttpContext c71 = s2.createContext("/redirecterror", redirectErrorHandlerSecure); |
|
750 |
delayHandler = new DelayHandler(); |
|
751 |
HttpContext c8 = s1.createContext("/delay", delayHandler); |
|
752 |
HttpContext c81 = s2.createContext("/delay", delayHandler); |
|
753 |
||
754 |
executor = Executors.newCachedThreadPool(); |
|
755 |
s1.setExecutor(executor); |
|
756 |
s2.setExecutor(executor); |
|
757 |
ctx = new SimpleSSLContext().get(); |
|
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56091
diff
changeset
|
758 |
sslparams = ctx.getDefaultSSLParameters(); |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56091
diff
changeset
|
759 |
//sslparams.setProtocols(new String[]{"TLSv1.2"}); |
37720 | 760 |
s2.setHttpsConfigurator(new Configurator(ctx)); |
36131 | 761 |
s1.start(); |
762 |
s2.start(); |
|
763 |
||
764 |
port = s1.getAddress().getPort(); |
|
765 |
System.out.println("HTTP server port = " + port); |
|
766 |
httpsport = s2.getAddress().getPort(); |
|
767 |
System.out.println("HTTPS server port = " + httpsport); |
|
768 |
httproot = "http://127.0.0.1:" + port + "/"; |
|
769 |
httpsroot = "https://127.0.0.1:" + httpsport + "/"; |
|
770 |
||
771 |
proxy = new ProxyServer(0, false); |
|
772 |
proxyPort = proxy.getPort(); |
|
773 |
System.out.println("Proxy port = " + proxyPort); |
|
774 |
} |
|
775 |
||
48083 | 776 |
static class RedirectHandler implements HttpHandler { |
777 |
private final String root; |
|
778 |
private volatile int count = 0; |
|
779 |
||
780 |
RedirectHandler(String root) { |
|
781 |
this.root = root; |
|
782 |
} |
|
37720 | 783 |
|
48083 | 784 |
@Override |
785 |
public synchronized void handle(HttpExchange t) throws IOException { |
|
786 |
byte[] buf = new byte[2048]; |
|
787 |
try (InputStream is = t.getRequestBody()) { |
|
788 |
while (is.read(buf) != -1) ; |
|
789 |
} |
|
37720 | 790 |
|
48083 | 791 |
Headers responseHeaders = t.getResponseHeaders(); |
36131 | 792 |
|
48083 | 793 |
if (count++ < 1) { |
794 |
responseHeaders.add("Location", root + "/foo/" + count); |
|
795 |
} else { |
|
796 |
responseHeaders.add("Location", SmokeTest.midSizedFilename); |
|
797 |
} |
|
798 |
t.sendResponseHeaders(301, 64 * 1024); |
|
799 |
byte[] bb = new byte[1024]; |
|
800 |
OutputStream os = t.getResponseBody(); |
|
801 |
for (int i=0; i<64; i++) { |
|
802 |
os.write(bb); |
|
803 |
} |
|
804 |
os.close(); |
|
805 |
t.close(); |
|
806 |
} |
|
36131 | 807 |
|
48083 | 808 |
int count() { |
809 |
return count; |
|
810 |
} |
|
36131 | 811 |
|
48083 | 812 |
void reset() { |
813 |
count = 0; |
|
36131 | 814 |
} |
815 |
} |
|
816 |
||
48083 | 817 |
static class RedirectErrorHandler implements HttpHandler { |
818 |
private final String root; |
|
819 |
private volatile int count = 1; |
|
36131 | 820 |
|
48083 | 821 |
RedirectErrorHandler(String root) { |
822 |
this.root = root; |
|
36131 | 823 |
} |
824 |
||
48083 | 825 |
synchronized int count() { |
826 |
return count; |
|
827 |
} |
|
36131 | 828 |
|
48083 | 829 |
synchronized void increment() { |
830 |
count++; |
|
36131 | 831 |
} |
832 |
||
48083 | 833 |
@Override |
834 |
public synchronized void handle(HttpExchange t) throws IOException { |
|
835 |
try (InputStream is = t.getRequestBody()) { |
|
836 |
is.readAllBytes(); |
|
837 |
} |
|
36131 | 838 |
|
48083 | 839 |
Headers map = t.getResponseHeaders(); |
840 |
String redirect = root + "/foo/" + Integer.toString(count); |
|
841 |
increment(); |
|
842 |
map.add("Location", redirect); |
|
843 |
t.sendResponseHeaders(301, -1); |
|
844 |
t.close(); |
|
845 |
} |
|
36131 | 846 |
} |
847 |
||
48083 | 848 |
static class DelayHandler implements HttpHandler { |
849 |
||
850 |
CyclicBarrier bar1 = new CyclicBarrier(2); |
|
851 |
CyclicBarrier bar2 = new CyclicBarrier(2); |
|
852 |
CyclicBarrier bar3 = new CyclicBarrier(2); |
|
36131 | 853 |
|
48083 | 854 |
CyclicBarrier barrier1() { |
855 |
return bar1; |
|
856 |
} |
|
857 |
||
858 |
CyclicBarrier barrier2() { |
|
859 |
return bar2; |
|
36131 | 860 |
} |
861 |
||
48083 | 862 |
@Override |
863 |
public synchronized void handle(HttpExchange he) throws IOException { |
|
864 |
he.getRequestBody().readAllBytes(); |
|
865 |
try { |
|
866 |
bar1.await(); |
|
867 |
bar2.await(); |
|
868 |
} catch (Exception e) { } |
|
869 |
he.sendResponseHeaders(200, -1); // will probably fail |
|
870 |
he.close(); |
|
871 |
} |
|
36131 | 872 |
} |
873 |
||
48083 | 874 |
static class Configurator extends HttpsConfigurator { |
875 |
public Configurator(SSLContext ctx) { |
|
876 |
super(ctx); |
|
877 |
} |
|
36131 | 878 |
|
48083 | 879 |
public void configure (HttpsParameters params) { |
56126
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56091
diff
changeset
|
880 |
SSLParameters p = getSSLContext().getDefaultSSLParameters(); |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56091
diff
changeset
|
881 |
//p.setProtocols(new String[]{"TLSv1.2"}); |
86e628130926
http-client-branch: fixed TLS hostname checking issue, SSL session reuse, and changed HttpResponse to return SSLSession
michaelm
parents:
56091
diff
changeset
|
882 |
params.setSSLParameters (p); |
36131 | 883 |
} |
884 |
} |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
885 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
886 |
static Path getTempFile(int size) throws IOException { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
887 |
File f = File.createTempFile("test", "txt"); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
888 |
f.deleteOnExit(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
889 |
byte[] buf = new byte[2048]; |
48083 | 890 |
for (int i = 0; i < buf.length; i++) |
891 |
buf[i] = (byte) i; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
892 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
893 |
FileOutputStream fos = new FileOutputStream(f); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
894 |
while (size > 0) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
895 |
int amount = Math.min(size, buf.length); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
896 |
fos.write(buf, 0, amount); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
897 |
size -= amount; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
898 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
899 |
fos.close(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
900 |
return f.toPath(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
901 |
} |
36131 | 902 |
} |
903 |
||
904 |
// check for simple hardcoded sequence and use remote address |
|
905 |
// to check. |
|
906 |
// First 4 requests executed in sequence (should use same connection/address) |
|
907 |
// Next 4 requests parallel (should use different addresses) |
|
908 |
// Then send 4 requests in parallel x 100 times (same four addresses used all time) |
|
909 |
||
910 |
class KeepAliveHandler implements HttpHandler { |
|
56083
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
911 |
final AtomicInteger counter = new AtomicInteger(0); |
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
912 |
final AtomicInteger nparallel = new AtomicInteger(0); |
36131 | 913 |
|
56083
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
914 |
final Set<Integer> portSet = Collections.synchronizedSet(new HashSet<>()); |
36131 | 915 |
|
56083
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
916 |
final int[] ports = new int[8]; |
36131 | 917 |
|
918 |
void sleep(int n) { |
|
919 |
try { |
|
920 |
Thread.sleep(n); |
|
921 |
} catch (InterruptedException e) {} |
|
922 |
} |
|
923 |
||
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
924 |
synchronized void setPort(int index, int value) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
925 |
ports[index] = value; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
926 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
927 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
928 |
synchronized int getPort(int index) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
929 |
return ports[index]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
930 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
931 |
|
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
932 |
synchronized void getPorts(int[] dest, int from) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
933 |
dest[0] = ports[from+0]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
934 |
dest[1] = ports[from+1]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
935 |
dest[2] = ports[from+2]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
936 |
dest[3] = ports[from+3]; |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
937 |
} |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
938 |
|
56083
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
939 |
static final CountDownLatch latch = new CountDownLatch(4); |
56091
aedd6133e7a0
http-client-branch: more fixes for odd intermittent failures
dfuchs
parents:
56089
diff
changeset
|
940 |
static final CountDownLatch latch7 = new CountDownLatch(4); |
56083
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
941 |
static final CountDownLatch latch8 = new CountDownLatch(1); |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
942 |
|
36131 | 943 |
@Override |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
944 |
public void handle (HttpExchange t) |
36131 | 945 |
throws IOException |
946 |
{ |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
947 |
int np = nparallel.incrementAndGet(); |
36131 | 948 |
int remotePort = t.getRemoteAddress().getPort(); |
949 |
String result = "OK"; |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
950 |
int[] lports = new int[4]; |
36131 | 951 |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
952 |
int n = counter.getAndIncrement(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
953 |
|
36131 | 954 |
/// First test |
955 |
if (n < 4) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
956 |
setPort(n, remotePort); |
36131 | 957 |
} |
958 |
if (n == 3) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
959 |
getPorts(lports, 0); |
36131 | 960 |
// check all values in ports[] are the same |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
961 |
if (lports[0] != lports[1] || lports[2] != lports[3] |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
962 |
|| lports[0] != lports[2]) { |
36131 | 963 |
result = "Error " + Integer.toString(n); |
964 |
System.out.println(result); |
|
965 |
} |
|
966 |
} |
|
967 |
// Second test |
|
968 |
if (n >=4 && n < 8) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
969 |
// delay so that this connection doesn't get reused |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
970 |
// before all 4 requests sent |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
971 |
setPort(n, remotePort); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
972 |
latch.countDown(); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
973 |
try {latch.await();} catch (InterruptedException e) {} |
56091
aedd6133e7a0
http-client-branch: more fixes for odd intermittent failures
dfuchs
parents:
56089
diff
changeset
|
974 |
latch7.countDown(); |
36131 | 975 |
} |
976 |
if (n == 7) { |
|
56091
aedd6133e7a0
http-client-branch: more fixes for odd intermittent failures
dfuchs
parents:
56089
diff
changeset
|
977 |
// wait until all n <= 7 have called setPort(...) |
aedd6133e7a0
http-client-branch: more fixes for odd intermittent failures
dfuchs
parents:
56089
diff
changeset
|
978 |
try {latch7.await();} catch (InterruptedException e) {} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
979 |
getPorts(lports, 4); |
36131 | 980 |
// should be all different |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
981 |
if (lports[0] == lports[1] || lports[2] == lports[3] |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
982 |
|| lports[0] == lports[2]) { |
36131 | 983 |
result = "Error " + Integer.toString(n); |
984 |
System.out.println(result); |
|
985 |
} |
|
986 |
// setup for third test |
|
987 |
for (int i=0; i<4; i++) { |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
988 |
portSet.add(lports[i]); |
36131 | 989 |
} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
990 |
System.out.printf("Ports: %d, %d, %d, %d\n", lports[0], lports[1], lports[2], lports[3]); |
56083
32ff53628380
http-client-branch: fix race condition and MT-safety in SmokeTest
dfuchs
parents:
55973
diff
changeset
|
991 |
latch8.countDown(); |
36131 | 992 |
} |
993 |
// Third test |
|
994 |
if (n > 7) { |
|
56091
aedd6133e7a0
http-client-branch: more fixes for odd intermittent failures
dfuchs
parents:
56089
diff
changeset
|
995 |
// wait until all n == 7 has updated portSet |
56084
6146d87993db
http-client-branch: fix race condition and MT-safety in SmokeTest (fix the fix)
dfuchs
parents:
56083
diff
changeset
|
996 |
try {latch8.await();} catch (InterruptedException e) {} |
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
997 |
if (np > 4) { |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
998 |
System.err.println("XXX np = " + np); |
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
999 |
} |
36131 | 1000 |
// just check that port is one of the ones in portSet |
1001 |
if (!portSet.contains(remotePort)) { |
|
56091
aedd6133e7a0
http-client-branch: more fixes for odd intermittent failures
dfuchs
parents:
56089
diff
changeset
|
1002 |
System.out.println ("UNEXPECTED REMOTE PORT " |
aedd6133e7a0
http-client-branch: more fixes for odd intermittent failures
dfuchs
parents:
56089
diff
changeset
|
1003 |
+ remotePort + " not in " + portSet); |
36131 | 1004 |
result = "Error " + Integer.toString(n); |
1005 |
System.out.println(result); |
|
1006 |
} |
|
1007 |
} |
|
1008 |
byte[] buf = new byte[2048]; |
|
1009 |
||
1010 |
try (InputStream is = t.getRequestBody()) { |
|
1011 |
while (is.read(buf) != -1) ; |
|
1012 |
} |
|
1013 |
t.sendResponseHeaders(200, result.length()); |
|
1014 |
OutputStream o = t.getResponseBody(); |
|
1015 |
o.write(result.getBytes("US-ASCII")); |
|
1016 |
t.close(); |
|
42460
7133f144981a
8170648: Move java.net.http package out of Java SE to incubator namespace
michaelm
parents:
41890
diff
changeset
|
1017 |
nparallel.getAndDecrement(); |
36131 | 1018 |
} |
1019 |
} |