23
23
24
24
/*
25
25
* @test
26
- * @bug 8342075
26
+ * @bug 8342075 8343855
27
27
* @library /test/lib /test/jdk/java/net/httpclient/lib
28
28
* @build jdk.httpclient.test.lib.http2.Http2TestServer jdk.test.lib.net.SimpleSSLContext
29
29
* @run testng/othervm -Djdk.internal.httpclient.debug=true
40
40
import java .net .http .HttpClient ;
41
41
import java .net .http .HttpHeaders ;
42
42
import java .net .http .HttpRequest ;
43
- import java .net .http .HttpRequest .BodyPublishers ;
44
43
import java .net .http .HttpResponse ;
45
44
import java .net .http .HttpResponse .BodyHandlers ;
46
45
import java .nio .charset .StandardCharsets ;
53
52
import javax .net .ssl .SSLContext ;
54
53
import javax .net .ssl .SSLSession ;
55
54
55
+ import jdk .httpclient .test .lib .common .HttpServerAdapters .HttpHeadOrGetHandler ;
56
56
import jdk .httpclient .test .lib .common .HttpServerAdapters .HttpTestServer ;
57
57
import jdk .httpclient .test .lib .http2 .BodyOutputStream ;
58
58
import jdk .httpclient .test .lib .http2 .Http2Handler ;
69
69
import org .testng .annotations .DataProvider ;
70
70
import org .testng .annotations .Test ;
71
71
72
+ import static java .util .concurrent .TimeUnit .NANOSECONDS ;
72
73
import static org .testng .Assert .assertEquals ;
73
74
import static org .testng .Assert .fail ;
74
75
@@ -92,6 +93,19 @@ public Object[][] variants() {
92
93
};
93
94
}
94
95
96
+ static void sleep (long wait ) throws InterruptedException {
97
+ if (wait <= 0 ) return ;
98
+ long remaining = Utils .adjustTimeout (wait );
99
+ long start = System .nanoTime ();
100
+ while (remaining > 0 ) {
101
+ Thread .sleep (remaining );
102
+ long end = System .nanoTime ();
103
+ remaining = remaining - NANOSECONDS .toMillis (end - start );
104
+ }
105
+ System .out .printf ("Waited %s ms%n" ,
106
+ NANOSECONDS .toMillis (System .nanoTime () - start ));
107
+ }
108
+
95
109
96
110
@ Test (dataProvider = "variants" )
97
111
void test (String uri ,
@@ -115,7 +129,7 @@ void test(String uri,
115
129
CompletableFuture <String > sent = new CompletableFuture <>();
116
130
responseSent .put (query , sent );
117
131
HttpRequest request = HttpRequest .newBuilder (uriWithQuery )
118
- .POST ( BodyPublishers . ofString ( "Hello there!" ) )
132
+ .GET ( )
119
133
.build ();
120
134
System .out .println ("\n Sending request:" + uriWithQuery );
121
135
final HttpClient cc = client ;
@@ -130,9 +144,9 @@ void test(String uri,
130
144
// we have to pull to get the exception, but slow enough
131
145
// so that DataFrames are buffered up to the point that
132
146
// the window is exceeded...
133
- int wait = uri .startsWith ("https://" ) ? 500 : 350 ;
147
+ long wait = uri .startsWith ("https://" ) ? 800 : 350 ;
134
148
try (InputStream is = response .body ()) {
135
- Thread . sleep (Utils . adjustTimeout ( wait ) );
149
+ sleep (wait );
136
150
is .readAllBytes ();
137
151
}
138
152
// we could fail here if we haven't waited long enough
@@ -174,7 +188,7 @@ void testAsync(String uri,
174
188
CompletableFuture <String > sent = new CompletableFuture <>();
175
189
responseSent .put (query , sent );
176
190
HttpRequest request = HttpRequest .newBuilder (uriWithQuery )
177
- .POST ( BodyPublishers . ofString ( "Hello there!" ) )
191
+ .GET ( )
178
192
.build ();
179
193
System .out .println ("\n Sending request:" + uriWithQuery );
180
194
final HttpClient cc = client ;
@@ -188,9 +202,9 @@ void testAsync(String uri,
188
202
assertEquals (key , label , "Unexpected key for " + query );
189
203
}
190
204
sent .join ();
191
- int wait = uri .startsWith ("https://" ) ? 600 : 300 ;
205
+ long wait = uri .startsWith ("https://" ) ? 800 : 350 ;
192
206
try (InputStream is = response .body ()) {
193
- Thread . sleep (Utils . adjustTimeout ( wait ) );
207
+ sleep (wait );
194
208
is .readAllBytes ();
195
209
}
196
210
// we could fail here if we haven't waited long enough
@@ -252,7 +266,9 @@ public void setup() throws Exception {
252
266
var https2TestServer = new Http2TestServer ("localhost" , true , sslContext );
253
267
https2TestServer .addHandler (new Http2TestHandler (), "/https2/" );
254
268
this .https2TestServer = HttpTestServer .of (https2TestServer );
269
+ this .https2TestServer .addHandler (new HttpHeadOrGetHandler (), "/https2/head/" );
255
270
https2URI = "https://" + this .https2TestServer .serverAuthority () + "/https2/x" ;
271
+ String h2Head = "https://" + this .https2TestServer .serverAuthority () + "/https2/head/z" ;
256
272
257
273
// Override the default exchange supplier with a custom one to enable
258
274
// particular test scenarios
@@ -261,6 +277,13 @@ public void setup() throws Exception {
261
277
262
278
this .http2TestServer .start ();
263
279
this .https2TestServer .start ();
280
+
281
+ // warmup to eliminate delay due to SSL class loading and initialization.
282
+ try (var client = HttpClient .newBuilder ().sslContext (sslContext ).build ()) {
283
+ var request = HttpRequest .newBuilder (URI .create (h2Head )).HEAD ().build ();
284
+ var resp = client .send (request , BodyHandlers .discarding ());
285
+ assertEquals (resp .statusCode (), 200 );
286
+ }
264
287
}
265
288
266
289
@ AfterTest
@@ -279,11 +302,19 @@ public void handle(Http2TestExchange t) throws IOException {
279
302
OutputStream os = t .getResponseBody ()) {
280
303
281
304
byte [] bytes = is .readAllBytes ();
282
- System .out .println ("Server " + t .getLocalAddress () + " received:\n "
283
- + t .getRequestURI () + ": " + new String (bytes , StandardCharsets .UTF_8 ));
305
+ if (bytes .length != 0 ) {
306
+ System .out .println ("Server " + t .getLocalAddress () + " received:\n "
307
+ + t .getRequestURI () + ": " + new String (bytes , StandardCharsets .UTF_8 ));
308
+ } else {
309
+ System .out .println ("No request body for " + t .getRequestMethod ());
310
+ }
311
+
284
312
t .getResponseHeaders ().setHeader ("X-Connection-Key" , t .getConnectionKey ());
285
313
286
- if (bytes .length == 0 ) bytes = "no request body!" .getBytes (StandardCharsets .UTF_8 );
314
+ if (bytes .length == 0 ) {
315
+ bytes = "no request body!"
316
+ .repeat (100 ).getBytes (StandardCharsets .UTF_8 );
317
+ }
287
318
int window = Integer .getInteger ("jdk.httpclient.windowsize" , 2 * 16 * 1024 );
288
319
final int maxChunkSize ;
289
320
if (t instanceof FCHttp2TestExchange fct ) {
@@ -307,13 +338,22 @@ public void handle(Http2TestExchange t) throws IOException {
307
338
// ignore and continue...
308
339
}
309
340
}
310
- ((BodyOutputStream ) os ).writeUncontrolled (resp , 0 , resp .length );
341
+ try {
342
+ ((BodyOutputStream ) os ).writeUncontrolled (resp , 0 , resp .length );
343
+ } catch (IOException x ) {
344
+ if (t instanceof FCHttp2TestExchange fct ) {
345
+ fct .conn .updateConnectionWindow (resp .length );
346
+ }
347
+ }
348
+ }
349
+ } finally {
350
+ if (t instanceof FCHttp2TestExchange fct ) {
351
+ fct .responseSent (query );
352
+ } else {
353
+ fail ("Exchange is not %s but %s"
354
+ .formatted (FCHttp2TestExchange .class .getName (), t .getClass ().getName ()));
311
355
}
312
356
}
313
- if (t instanceof FCHttp2TestExchange fct ) {
314
- fct .responseSent (query );
315
- } else fail ("Exchange is not %s but %s"
316
- .formatted (FCHttp2TestExchange .class .getName (), t .getClass ().getName ()));
317
357
}
318
358
}
319
359
0 commit comments