2525 * @test
2626 * @summary Verify that some special headers - such as User-Agent
2727 * can be specified by the caller.
28- * @bug 8203771 8218546
28+ * @bug 8203771 8218546 8297200
2929 * @library /test/lib /test/jdk/java/net/httpclient/lib
3030 * @build jdk.httpclient.test.lib.common.HttpServerAdapters
3131 * jdk.httpclient.test.lib.http2.Http2TestServer
4242import com .sun .net .httpserver .HttpServer ;
4343import com .sun .net .httpserver .HttpsConfigurator ;
4444import com .sun .net .httpserver .HttpsServer ;
45+ import jdk .internal .net .http .common .OperationTrackers .Tracker ;
4546import jdk .test .lib .net .SimpleSSLContext ;
4647import org .testng .ITestContext ;
4748import org .testng .ITestResult ;
@@ -300,13 +301,18 @@ static String userAgent() {
300301 static final Map <String , Function <URI ,String >> DEFAULTS = Map .of (
301302 "USER-AGENT" , u -> userAgent (), "HOST" , u -> u .getRawAuthority ());
302303
304+ static void throwIfNotNull (Throwable throwable ) throws Exception {
305+ if (throwable instanceof Exception ex ) throw ex ;
306+ if (throwable instanceof Error e ) throw e ;
307+ }
308+
303309 @ Test (dataProvider = "variants" )
304310 void test (String uriString ,
305311 String headerNameAndValue ,
306312 boolean sameClient )
307313 throws Exception
308314 {
309- out .println ("\n --- Starting " );
315+ out .println ("\n --- Starting test " + now () );
310316
311317 int index = headerNameAndValue .indexOf (":" );
312318 String name = headerNameAndValue .substring (0 , index );
@@ -318,10 +324,14 @@ void test(String uriString,
318324 String value = useDefault ? DEFAULTS .get (key ).apply (uri ) : v ;
319325
320326 HttpClient client = null ;
327+ Tracker tracker = null ;
328+ Throwable thrown = null ;
321329 for (int i =0 ; i < ITERATION_COUNT ; i ++) {
322330 try {
323- if (!sameClient || client == null )
331+ if (!sameClient || client == null ) {
324332 client = newHttpClient ("test" , sameClient );
333+ tracker = TRACKER .getTracker (client );
334+ }
325335
326336 HttpRequest .Builder requestBuilder = HttpRequest .newBuilder (uri );
327337 if (!useDefault ) {
@@ -361,14 +371,20 @@ void test(String uriString,
361371 assertEquals (resp .headers ().allValues ("X-" + key ).size (), 0 );
362372 }
363373 }
374+ } catch (Throwable x ) {
375+ thrown = x ;
364376 } finally {
365377 if (!sameClient ) {
366378 client = null ;
367379 System .gc ();
368- var error = TRACKER .check (500 );
369- if (error != null ) throw error ;
380+ var error = TRACKER .check (tracker , 500 );
381+ if (error != null ) {
382+ if (thrown != null ) error .addSuppressed (thrown );
383+ throw error ;
384+ }
370385 }
371386 }
387+ throwIfNotNull (thrown );
372388 }
373389 }
374390
@@ -378,11 +394,12 @@ void testHomeMadeIllegalHeader(String uriString,
378394 boolean sameClient )
379395 throws Exception
380396 {
381- out .println ("\n --- Starting " );
397+ out .println ("\n --- Starting testHomeMadeIllegalHeader " + now () );
382398 final URI uri = URI .create (uriString );
383399
384400 HttpClient client = newHttpClient ("testHomeMadeIllegalHeader" , sameClient );
385-
401+ Tracker tracker = TRACKER .getTracker (client );
402+ Throwable thrown = null ;
386403 try {
387404 // Test a request which contains an illegal header created
388405 HttpRequest req = new HttpRequest () {
@@ -429,19 +446,29 @@ public HttpHeaders headers() {
429446 } catch (IllegalArgumentException ee ) {
430447 out .println ("Got IAE as expected" );
431448 }
449+ } catch (Throwable x ) {
450+ thrown = x ;
432451 } finally {
433452 if (!sameClient ) {
434453 client = null ;
435454 System .gc ();
436- var error = TRACKER .check (500 );
437- if (error != null ) throw error ;
455+ var error = TRACKER .check (tracker , 500 );
456+ if (error != null ) {
457+ if (thrown != null ) error .addSuppressed (thrown );
458+ throw error ;
459+ }
438460 }
439461 }
462+ throwIfNotNull (thrown );
440463 }
441464
465+
466+
442467 @ Test (dataProvider = "variants" )
443- void testAsync (String uriString , String headerNameAndValue , boolean sameClient ) {
444- out .println ("\n --- Starting " );
468+ void testAsync (String uriString , String headerNameAndValue , boolean sameClient )
469+ throws Exception
470+ {
471+ out .println ("\n --- Starting testAsync " + now ());
445472 int index = headerNameAndValue .indexOf (":" );
446473 String name = headerNameAndValue .substring (0 , index );
447474 String v = headerNameAndValue .substring (index +1 ).trim ();
@@ -452,10 +479,14 @@ void testAsync(String uriString, String headerNameAndValue, boolean sameClient)
452479 String value = useDefault ? DEFAULTS .get (key ).apply (uri ) : v ;
453480
454481 HttpClient client = null ;
482+ Tracker tracker = null ;
483+ Throwable thrown = null ;
455484 for (int i =0 ; i < ITERATION_COUNT ; i ++) {
456485 try {
457- if (!sameClient || client == null )
486+ if (!sameClient || client == null ) {
458487 client = newHttpClient ("testAsync" , sameClient );
488+ tracker = TRACKER .getTracker (client );
489+ }
459490
460491 HttpRequest .Builder requestBuilder = HttpRequest .newBuilder (uri );
461492 if (!useDefault ) {
@@ -498,14 +529,20 @@ void testAsync(String uriString, String headerNameAndValue, boolean sameClient)
498529 }
499530 })
500531 .join ();
532+ } catch (Throwable x ) {
533+ thrown = x ;
501534 } finally {
502535 if (!sameClient ) {
503536 client = null ;
504537 System .gc ();
505- var error = TRACKER .check (500 );
506- if (error != null ) throw error ;
538+ var error = TRACKER .check (tracker , 500 );
539+ if (error != null ) {
540+ if (thrown != null ) error .addSuppressed (thrown );
541+ throw error ;
542+ }
507543 }
508544 }
545+ throwIfNotNull (thrown );
509546 }
510547 }
511548
@@ -516,6 +553,7 @@ static String serverAuthority(HttpTestServer server) {
516553
517554 @ BeforeTest
518555 public void setup () throws Exception {
556+ out .println ("--- Starting setup " + now ());
519557 sslContext = new SimpleSSLContext ().get ();
520558 if (sslContext == null )
521559 throw new AssertionError ("Unexpected null sslContext" );
@@ -545,13 +583,15 @@ public void setup() throws Exception {
545583
546584 @ AfterTest
547585 public void teardown () throws Exception {
586+ out .println ("\n --- Teardown " + now ());
548587 HttpClient shared = sharedClient ;
549588 String sharedClientName =
550589 shared == null ? null : shared .toString ();
551590 if (shared != null ) TRACKER .track (shared );
552591 shared = sharedClient = null ;
553592 Thread .sleep (100 );
554- AssertionError fail = TRACKER .check (500 );
593+ AssertionError fail = TRACKER .check (2500 );
594+ out .println ("--- Stopping servers " + now ());
555595 try {
556596 httpTestServer .stop ();
557597 httpsTestServer .stop ();
0 commit comments