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
* jdk.httpclient.test.lib.common.TestServerConfigurator
56
56
import javax .net .ssl .SSLContext ;
57
57
import javax .net .ssl .SSLSession ;
58
58
59
+ import jdk .httpclient .test .lib .common .HttpServerAdapters .HttpHeadOrGetHandler ;
59
60
import jdk .httpclient .test .lib .common .HttpServerAdapters .HttpTestServer ;
60
61
import jdk .httpclient .test .lib .http2 .BodyOutputStream ;
61
62
import jdk .httpclient .test .lib .http2 .Http2Handler ;
72
73
import org .testng .annotations .DataProvider ;
73
74
import org .testng .annotations .Test ;
74
75
76
+ import static java .util .concurrent .TimeUnit .NANOSECONDS ;
75
77
import static org .testng .Assert .assertEquals ;
76
78
import static org .testng .Assert .fail ;
77
79
@@ -95,6 +97,19 @@ public Object[][] variants() {
95
97
};
96
98
}
97
99
100
+ static void sleep (long wait ) throws InterruptedException {
101
+ if (wait <= 0 ) return ;
102
+ long remaining = Utils .adjustTimeout (wait );
103
+ long start = System .nanoTime ();
104
+ while (remaining > 0 ) {
105
+ Thread .sleep (remaining );
106
+ long end = System .nanoTime ();
107
+ remaining = remaining - NANOSECONDS .toMillis (end - start );
108
+ }
109
+ System .out .printf ("Waited %s ms%n" ,
110
+ NANOSECONDS .toMillis (System .nanoTime () - start ));
111
+ }
112
+
98
113
99
114
@ Test (dataProvider = "variants" )
100
115
void test (String uri ,
@@ -121,7 +136,7 @@ void test(String uri,
121
136
CompletableFuture <String > sent = new CompletableFuture <>();
122
137
responseSent .put (query , sent );
123
138
HttpRequest request = HttpRequest .newBuilder (uriWithQuery )
124
- .POST ( BodyPublishers . ofString ( "Hello there!" ) )
139
+ .GET ( )
125
140
.build ();
126
141
System .out .println ("\n Sending request:" + uriWithQuery );
127
142
final HttpClient cc = client ;
@@ -136,9 +151,9 @@ void test(String uri,
136
151
// we have to pull to get the exception, but slow enough
137
152
// so that DataFrames are buffered up to the point that
138
153
// the window is exceeded...
139
- int wait = uri .startsWith ("https://" ) ? 500 : 350 ;
154
+ long wait = uri .startsWith ("https://" ) ? 800 : 350 ;
140
155
try (InputStream is = response .body ()) {
141
- Thread . sleep (Utils . adjustTimeout ( wait ) );
156
+ sleep (wait );
142
157
is .readAllBytes ();
143
158
}
144
159
// we could fail here if we haven't waited long enough
@@ -187,7 +202,7 @@ void testAsync(String uri,
187
202
CompletableFuture <String > sent = new CompletableFuture <>();
188
203
responseSent .put (query , sent );
189
204
HttpRequest request = HttpRequest .newBuilder (uriWithQuery )
190
- .POST ( BodyPublishers . ofString ( "Hello there!" ) )
205
+ .GET ( )
191
206
.build ();
192
207
System .out .println ("\n Sending request:" + uriWithQuery );
193
208
final HttpClient cc = client ;
@@ -201,9 +216,9 @@ void testAsync(String uri,
201
216
assertEquals (key , label , "Unexpected key for " + query );
202
217
}
203
218
sent .join ();
204
- int wait = uri .startsWith ("https://" ) ? 600 : 300 ;
219
+ long wait = uri .startsWith ("https://" ) ? 800 : 350 ;
205
220
try (InputStream is = response .body ()) {
206
- Thread . sleep (Utils . adjustTimeout ( wait ) );
221
+ sleep (wait );
207
222
is .readAllBytes ();
208
223
}
209
224
// we could fail here if we haven't waited long enough
@@ -269,7 +284,9 @@ public void setup() throws Exception {
269
284
var https2TestServer = new Http2TestServer ("localhost" , true , sslContext );
270
285
https2TestServer .addHandler (new Http2TestHandler (), "/https2/" );
271
286
this .https2TestServer = HttpTestServer .of (https2TestServer );
287
+ this .https2TestServer .addHandler (new HttpHeadOrGetHandler (), "/https2/head/" );
272
288
https2URI = "https://" + this .https2TestServer .serverAuthority () + "/https2/x" ;
289
+ String h2Head = "https://" + this .https2TestServer .serverAuthority () + "/https2/head/z" ;
273
290
274
291
// Override the default exchange supplier with a custom one to enable
275
292
// particular test scenarios
@@ -278,6 +295,17 @@ public void setup() throws Exception {
278
295
279
296
this .http2TestServer .start ();
280
297
this .https2TestServer .start ();
298
+
299
+ // warmup to eliminate delay due to SSL class loading and initialization.
300
+ HttpClient client = HttpClient .newBuilder ().executor (Executors .newCachedThreadPool ()).sslContext (sslContext ).build ();
301
+ try {
302
+ var request = HttpRequest .newBuilder (URI .create (h2Head )).method ("HEAD" , BodyPublishers .noBody ()).build ();
303
+ var resp = client .send (request , BodyHandlers .discarding ());
304
+ assertEquals (resp .statusCode (), 200 );
305
+ } finally {
306
+ ExecutorService exec = (ExecutorService )client .executor ().get ();
307
+ exec .shutdownNow ();
308
+ }
281
309
}
282
310
283
311
@ AfterTest
@@ -296,11 +324,19 @@ public void handle(Http2TestExchange t) throws IOException {
296
324
OutputStream os = t .getResponseBody ()) {
297
325
298
326
byte [] bytes = is .readAllBytes ();
299
- System .out .println ("Server " + t .getLocalAddress () + " received:\n "
300
- + t .getRequestURI () + ": " + new String (bytes , StandardCharsets .UTF_8 ));
327
+ if (bytes .length != 0 ) {
328
+ System .out .println ("Server " + t .getLocalAddress () + " received:\n "
329
+ + t .getRequestURI () + ": " + new String (bytes , StandardCharsets .UTF_8 ));
330
+ } else {
331
+ System .out .println ("No request body for " + t .getRequestMethod ());
332
+ }
333
+
301
334
t .getResponseHeaders ().setHeader ("X-Connection-Key" , t .getConnectionKey ());
302
335
303
- if (bytes .length == 0 ) bytes = "no request body!" .getBytes (StandardCharsets .UTF_8 );
336
+ if (bytes .length == 0 ) {
337
+ bytes = "no request body!"
338
+ .repeat (100 ).getBytes (StandardCharsets .UTF_8 );
339
+ }
304
340
int window = Integer .getInteger ("jdk.httpclient.windowsize" , 2 * 16 * 1024 );
305
341
final int maxChunkSize ;
306
342
if (t instanceof FCHttp2TestExchange fct ) {
@@ -324,13 +360,22 @@ public void handle(Http2TestExchange t) throws IOException {
324
360
// ignore and continue...
325
361
}
326
362
}
327
- ((BodyOutputStream ) os ).writeUncontrolled (resp , 0 , resp .length );
363
+ try {
364
+ ((BodyOutputStream ) os ).writeUncontrolled (resp , 0 , resp .length );
365
+ } catch (IOException x ) {
366
+ if (t instanceof FCHttp2TestExchange fct ) {
367
+ fct .conn .updateConnectionWindow (resp .length );
368
+ }
369
+ }
370
+ }
371
+ } finally {
372
+ if (t instanceof FCHttp2TestExchange fct ) {
373
+ fct .responseSent (query );
374
+ } else {
375
+ fail ("Exchange is not %s but %s"
376
+ .formatted (FCHttp2TestExchange .class .getName (), t .getClass ().getName ()));
328
377
}
329
378
}
330
- if (t instanceof FCHttp2TestExchange fct ) {
331
- fct .responseSent (query );
332
- } else fail ("Exchange is not %s but %s"
333
- .formatted (FCHttp2TestExchange .class .getName (), t .getClass ().getName ()));
334
379
}
335
380
}
336
381
0 commit comments