Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/httpclient/AbstractConnectTimeout.java
41149 views
1
/*
2
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
import java.net.ConnectException;
25
import java.net.InetSocketAddress;
26
import java.net.NoRouteToHostException;
27
import java.net.ProxySelector;
28
import java.net.URI;
29
import java.net.http.HttpClient;
30
import java.net.http.HttpClient.Version;
31
import java.net.http.HttpConnectTimeoutException;
32
import java.net.http.HttpRequest;
33
import java.net.http.HttpRequest.BodyPublishers;
34
import java.net.http.HttpResponse;
35
import java.net.http.HttpResponse.BodyHandlers;
36
import java.nio.channels.UnresolvedAddressException;
37
import java.time.Duration;
38
import java.util.ArrayList;
39
import java.util.Arrays;
40
import java.util.List;
41
import java.util.concurrent.CompletionException;
42
import org.testng.annotations.DataProvider;
43
import static java.lang.System.out;
44
import static java.net.http.HttpClient.Builder.NO_PROXY;
45
import static java.net.http.HttpClient.Version.HTTP_1_1;
46
import static java.net.http.HttpClient.Version.HTTP_2;
47
import static java.time.Duration.*;
48
import static java.util.concurrent.TimeUnit.NANOSECONDS;
49
import static org.testng.Assert.fail;
50
51
public abstract class AbstractConnectTimeout {
52
53
static final Duration NO_DURATION = null;
54
55
static List<List<Duration>> TIMEOUTS = List.of(
56
// connectTimeout HttpRequest timeout
57
Arrays.asList( NO_DURATION, ofSeconds(1) ),
58
Arrays.asList( NO_DURATION, ofMillis(100) ),
59
Arrays.asList( NO_DURATION, ofNanos(99) ),
60
Arrays.asList( NO_DURATION, ofNanos(1) ),
61
62
Arrays.asList( ofSeconds(1), NO_DURATION ),
63
Arrays.asList( ofMillis(100), NO_DURATION ),
64
Arrays.asList( ofNanos(99), NO_DURATION ),
65
Arrays.asList( ofNanos(1), NO_DURATION ),
66
67
Arrays.asList( ofSeconds(1), ofMinutes(1) ),
68
Arrays.asList( ofMillis(100), ofMinutes(1) ),
69
Arrays.asList( ofNanos(99), ofMinutes(1) ),
70
Arrays.asList( ofNanos(1), ofMinutes(1) )
71
);
72
73
static final List<String> METHODS = List.of("GET", "POST");
74
static final List<Version> VERSIONS = List.of(HTTP_2, HTTP_1_1);
75
static final List<String> SCHEMES = List.of("https", "http");
76
77
@DataProvider(name = "variants")
78
public Object[][] variants() {
79
List<Object[]> l = new ArrayList<>();
80
for (List<Duration> timeouts : TIMEOUTS) {
81
Duration connectTimeout = timeouts.get(0);
82
Duration requestTimeout = timeouts.get(1);
83
for (String method: METHODS) {
84
for (String scheme : SCHEMES) {
85
for (Version requestVersion : VERSIONS) {
86
l.add(new Object[] {requestVersion, scheme, method, connectTimeout, requestTimeout});
87
}}}}
88
return l.stream().toArray(Object[][]::new);
89
}
90
91
static final ProxySelector EXAMPLE_DOT_COM_PROXY = ProxySelector.of(
92
InetSocketAddress.createUnresolved("example.com", 8080));
93
94
//@Test(dataProvider = "variants")
95
protected void timeoutNoProxySync(Version requestVersion,
96
String scheme,
97
String method,
98
Duration connectTimeout,
99
Duration requestTimeout)
100
throws Exception
101
{
102
timeoutSync(requestVersion, scheme, method, connectTimeout, requestTimeout, NO_PROXY);
103
}
104
105
//@Test(dataProvider = "variants")
106
protected void timeoutWithProxySync(Version requestVersion,
107
String scheme,
108
String method,
109
Duration connectTimeout,
110
Duration requestTimeout)
111
throws Exception
112
{
113
timeoutSync(requestVersion, scheme, method, connectTimeout, requestTimeout, EXAMPLE_DOT_COM_PROXY);
114
}
115
116
private void timeoutSync(Version requestVersion,
117
String scheme,
118
String method,
119
Duration connectTimeout,
120
Duration requestTimeout,
121
ProxySelector proxy)
122
throws Exception
123
{
124
out.printf("%ntimeoutSync(requestVersion=%s, scheme=%s, method=%s,"
125
+ " connectTimeout=%s, requestTimeout=%s, proxy=%s)%n",
126
requestVersion, scheme, method, connectTimeout, requestTimeout, proxy);
127
128
HttpClient client = newClient(connectTimeout, proxy);
129
HttpRequest request = newRequest(scheme, requestVersion, method, requestTimeout);
130
131
for (int i = 0; i < 2; i++) {
132
out.printf("iteration %d%n", i);
133
long startTime = System.nanoTime();
134
try {
135
HttpResponse<?> resp = client.send(request, BodyHandlers.ofString());
136
printResponse(resp);
137
fail("Unexpected response: " + resp);
138
} catch (HttpConnectTimeoutException expected) { // blocking thread-specific exception
139
long elapsedTime = NANOSECONDS.toMillis(System.nanoTime() - startTime);
140
out.printf("Client: received in %d millis%n", elapsedTime);
141
assertExceptionTypeAndCause(expected.getCause());
142
} catch (ConnectException e) {
143
long elapsedTime = NANOSECONDS.toMillis(System.nanoTime() - startTime);
144
out.printf("Client: received in %d millis%n", elapsedTime);
145
Throwable t = e.getCause().getCause(); // blocking thread-specific exception
146
if (!isAcceptableCause(t)) { // tolerate only NRTHE or UAE
147
e.printStackTrace(out);
148
fail("Unexpected exception:" + e);
149
} else {
150
out.printf("Caught ConnectException with "
151
+ " cause: %s - skipping%n", t.getCause());
152
}
153
}
154
}
155
}
156
157
//@Test(dataProvider = "variants")
158
protected void timeoutNoProxyAsync(Version requestVersion,
159
String scheme,
160
String method,
161
Duration connectTimeout,
162
Duration requestTimeout) {
163
timeoutAsync(requestVersion, scheme, method, connectTimeout, requestTimeout, NO_PROXY);
164
}
165
166
//@Test(dataProvider = "variants")
167
protected void timeoutWithProxyAsync(Version requestVersion,
168
String scheme,
169
String method,
170
Duration connectTimeout,
171
Duration requestTimeout) {
172
timeoutAsync(requestVersion, scheme, method, connectTimeout, requestTimeout, EXAMPLE_DOT_COM_PROXY);
173
}
174
175
private void timeoutAsync(Version requestVersion,
176
String scheme,
177
String method,
178
Duration connectTimeout,
179
Duration requestTimeout,
180
ProxySelector proxy) {
181
out.printf("%ntimeoutAsync(requestVersion=%s, scheme=%s, method=%s, "
182
+ "connectTimeout=%s, requestTimeout=%s, proxy=%s)%n",
183
requestVersion, scheme, method, connectTimeout, requestTimeout, proxy);
184
185
HttpClient client = newClient(connectTimeout, proxy);
186
HttpRequest request = newRequest(scheme, requestVersion, method, requestTimeout);
187
for (int i = 0; i < 2; i++) {
188
out.printf("iteration %d%n", i);
189
long startTime = System.nanoTime();
190
try {
191
HttpResponse<?> resp = client.sendAsync(request, BodyHandlers.ofString()).join();
192
printResponse(resp);
193
fail("Unexpected response: " + resp);
194
} catch (CompletionException e) {
195
long elapsedTime = NANOSECONDS.toMillis(System.nanoTime() - startTime);
196
out.printf("Client: received in %d millis%n", elapsedTime);
197
Throwable t = e.getCause();
198
if (t instanceof ConnectException && isAcceptableCause(t.getCause())) {
199
// tolerate only NRTHE and UAE
200
out.printf("Caught ConnectException with "
201
+ "cause: %s - skipping%n", t.getCause());
202
} else {
203
assertExceptionTypeAndCause(t);
204
}
205
}
206
}
207
}
208
209
static boolean isAcceptableCause(Throwable cause) {
210
if (cause instanceof NoRouteToHostException) return true;
211
if (cause instanceof UnresolvedAddressException) return true;
212
return false;
213
}
214
215
static HttpClient newClient(Duration connectTimeout, ProxySelector proxy) {
216
HttpClient.Builder builder = HttpClient.newBuilder().proxy(proxy);
217
if (connectTimeout != NO_DURATION)
218
builder.connectTimeout(connectTimeout);
219
return builder.build();
220
}
221
222
static HttpRequest newRequest(String scheme,
223
Version reqVersion,
224
String method,
225
Duration requestTimeout) {
226
// Resolvable address. Most tested environments just ignore the TCP SYN,
227
// or occasionally return ICMP no route to host
228
URI uri = URI.create(scheme +"://example.com:81/");
229
HttpRequest.Builder reqBuilder = HttpRequest.newBuilder(uri);
230
reqBuilder = reqBuilder.version(reqVersion);
231
switch (method) {
232
case "GET" : reqBuilder.GET(); break;
233
case "POST" : reqBuilder.POST(BodyPublishers.noBody()); break;
234
default: throw new AssertionError("Unknown method:" + method);
235
}
236
if (requestTimeout != NO_DURATION)
237
reqBuilder.timeout(requestTimeout);
238
return reqBuilder.build();
239
}
240
241
static void assertExceptionTypeAndCause(Throwable t) {
242
if (!(t instanceof HttpConnectTimeoutException)) {
243
t.printStackTrace(out);
244
fail("Expected HttpConnectTimeoutException, got:" + t);
245
}
246
Throwable connEx = t.getCause();
247
if (!(connEx instanceof ConnectException)) {
248
t.printStackTrace(out);
249
fail("Expected ConnectException cause in:" + connEx);
250
}
251
out.printf("Caught expected HttpConnectTimeoutException with ConnectException"
252
+ " cause: %n%s%n%s%n", t, connEx);
253
final String EXPECTED_MESSAGE = "HTTP connect timed out"; // impl dependent
254
if (!connEx.getMessage().equals(EXPECTED_MESSAGE))
255
fail("Expected: \"" + EXPECTED_MESSAGE + "\", got: \"" + connEx.getMessage() + "\"");
256
257
}
258
259
static void printResponse(HttpResponse<?> response) {
260
out.println("Unexpected response: " + response);
261
out.println("Headers: " + response.headers());
262
out.println("Body: " + response.body());
263
}
264
}
265
266