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