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