2323
2424import jdk .httpclient .test .lib .http3 .Http3TestServer ;
2525import jdk .test .lib .net .SimpleSSLContext ;
26- import org .testng .ITestContext ;
27- import org .testng .ITestResult ;
28- import org .testng .SkipException ;
29- import org .testng .annotations .AfterTest ;
30- import org .testng .annotations .AfterClass ;
31- import org .testng .annotations .BeforeMethod ;
32- import org .testng .annotations .BeforeTest ;
33- import org .testng .annotations .DataProvider ;
3426
3527import javax .net .ssl .SSLContext ;
3628import java .io .BufferedReader ;
4941import java .net .http .HttpResponse .BodySubscriber ;
5042import java .nio .ByteBuffer ;
5143import java .nio .charset .StandardCharsets ;
52- import java .util .Arrays ;
5344import java .util .EnumSet ;
5445import java .util .List ;
5546import java .util .concurrent .CompletableFuture ;
7667import static java .net .http .HttpClient .Version .HTTP_3 ;
7768import static java .net .http .HttpOption .H3_DISCOVERY ;
7869import static java .nio .charset .StandardCharsets .UTF_8 ;
79- import static org .testng .Assert .assertEquals ;
80- import static org .testng .Assert .assertTrue ;
70+ import org .junit .jupiter .api .AfterAll ;
71+ import static org .junit .jupiter .api .Assertions .assertEquals ;
72+ import static org .junit .jupiter .api .Assertions .assertTrue ;
73+
74+ import org .junit .jupiter .api .Assumptions ;
75+ import org .junit .jupiter .api .BeforeAll ;
76+ import org .junit .jupiter .api .BeforeEach ;
77+ import org .junit .jupiter .api .extension .BeforeEachCallback ;
78+ import org .junit .jupiter .api .extension .ExtensionContext ;
79+ import org .junit .jupiter .api .extension .RegisterExtension ;
80+ import org .junit .jupiter .api .extension .TestWatcher ;
8181
8282public abstract class AbstractThrowingSubscribers implements HttpServerAdapters {
8383
84- SSLContext sslContext ;
85- HttpTestServer httpTestServer ; // HTTP/1.1 [ 4 servers ]
86- HttpTestServer httpsTestServer ; // HTTPS/1.1
87- HttpTestServer http2TestServer ; // HTTP/2 ( h2c )
88- HttpTestServer https2TestServer ; // HTTP/2 ( h2 )
89- HttpTestServer http3TestServer ; // HTTP/3 ( h3 )
90- String httpURI_fixed ;
91- String httpURI_chunk ;
92- String httpsURI_fixed ;
93- String httpsURI_chunk ;
94- String http2URI_fixed ;
95- String http2URI_chunk ;
96- String https2URI_fixed ;
97- String https2URI_chunk ;
98- String http3URI_fixed ;
99- String http3URI_chunk ;
100- String http3URI_head ;
84+ static SSLContext sslContext ;
85+ static HttpTestServer httpTestServer ; // HTTP/1.1 [ 4 servers ]
86+ static HttpTestServer httpsTestServer ; // HTTPS/1.1
87+ static HttpTestServer http2TestServer ; // HTTP/2 ( h2c )
88+ static HttpTestServer https2TestServer ; // HTTP/2 ( h2 )
89+ static HttpTestServer http3TestServer ; // HTTP/3 ( h3 )
90+ static String httpURI_fixed ;
91+ static String httpURI_chunk ;
92+ static String httpsURI_fixed ;
93+ static String httpsURI_chunk ;
94+ static String http2URI_fixed ;
95+ static String http2URI_chunk ;
96+ static String https2URI_fixed ;
97+ static String https2URI_chunk ;
98+ static String http3URI_fixed ;
99+ static String http3URI_chunk ;
100+ static String http3URI_head ;
101101
102102 static final int ITERATION_COUNT = 1 ;
103103 static final int REPEAT_RESPONSE = 3 ;
@@ -116,8 +116,34 @@ public static String now() {
116116 return String .format ("[%d s, %d ms, %d ns] " , secs , mill , nan );
117117 }
118118
119- final ReferenceTracker TRACKER = ReferenceTracker .INSTANCE ;
120- private volatile HttpClient sharedClient ;
119+ static final class TestStopper implements TestWatcher , BeforeEachCallback {
120+ final AtomicReference <String > failed = new AtomicReference <>();
121+ TestStopper () { }
122+ @ Override
123+ public void testFailed (ExtensionContext context , Throwable cause ) {
124+ if (stopAfterFirstFailure ()) {
125+ String msg = "Aborting due to: " + cause ;
126+ failed .compareAndSet (null , msg );
127+ FAILURES .putIfAbsent (context .getDisplayName (), cause );
128+ System .out .printf ("%nTEST FAILED: %s%s%n\t Aborting due to %s%n%n" ,
129+ now (), context .getDisplayName (), cause );
130+ System .err .printf ("%nTEST FAILED: %s%s%n\t Aborting due to %s%n%n" ,
131+ now (), context .getDisplayName (), cause );
132+ }
133+ }
134+
135+ @ Override
136+ public void beforeEach (ExtensionContext context ) {
137+ String msg = failed .get ();
138+ Assumptions .assumeTrue (msg == null , msg );
139+ }
140+ }
141+
142+ @ RegisterExtension
143+ static final TestStopper stopper = new TestStopper ();
144+
145+ static final ReferenceTracker TRACKER = ReferenceTracker .INSTANCE ;
146+ private static volatile HttpClient sharedClient ;
121147
122148 static class TestExecutor implements Executor {
123149 final AtomicLong tasks = new AtomicLong ();
@@ -143,21 +169,10 @@ public void execute(Runnable command) {
143169 }
144170 }
145171
146- protected boolean stopAfterFirstFailure () {
172+ protected static boolean stopAfterFirstFailure () {
147173 return Boolean .getBoolean ("jdk.internal.httpclient.debug" );
148174 }
149175
150- final AtomicReference <SkipException > skiptests = new AtomicReference <>();
151- void checkSkip () {
152- var skip = skiptests .get ();
153- if (skip != null ) throw skip ;
154- }
155- static String name (ITestResult result ) {
156- var params = result .getParameters ();
157- return result .getName ()
158- + (params == null ? "()" : Arrays .toString (result .getParameters ()));
159- }
160-
161176 static Version version (String uri ) {
162177 if (uri .contains ("/http1/" ) || uri .contains ("/https1/" ))
163178 return HTTP_1_1 ;
@@ -168,7 +183,7 @@ static Version version(String uri) {
168183 return null ;
169184 }
170185
171- HttpRequest .Builder newRequestBuilder (String uri ) {
186+ static HttpRequest .Builder newRequestBuilder (String uri ) {
172187 var builder = HttpRequest .newBuilder (URI .create (uri ));
173188 if (version (uri ) == HTTP_3 ) {
174189 builder .version (HTTP_3 );
@@ -177,7 +192,7 @@ HttpRequest.Builder newRequestBuilder(String uri) {
177192 return builder ;
178193 }
179194
180- HttpResponse <String > headRequest (HttpClient client )
195+ static HttpResponse <String > headRequest (HttpClient client )
181196 throws IOException , InterruptedException
182197 {
183198 System .out .println ("\n " + now () + "--- Sending HEAD request ----\n " );
@@ -186,26 +201,15 @@ HttpResponse<String> headRequest(HttpClient client)
186201 var request = newRequestBuilder (http3URI_head )
187202 .HEAD ().version (HTTP_2 ).build ();
188203 var response = client .send (request , BodyHandlers .ofString ());
189- assertEquals (response .statusCode (), 200 );
190- assertEquals (response .version (), HTTP_2 );
204+ assertEquals (200 , response .statusCode ());
205+ assertEquals (HTTP_2 , response .version ());
191206 System .out .println ("\n " + now () + "--- HEAD request succeeded ----\n " );
192207 System .err .println ("\n " + now () + "--- HEAD request succeeded ----\n " );
193208 return response ;
194209 }
195210
196- @ BeforeMethod
197- void beforeMethod (ITestContext context ) {
198- if (stopAfterFirstFailure () && context .getFailedTests ().size () > 0 ) {
199- if (skiptests .get () == null ) {
200- SkipException skip = new SkipException ("some tests failed" );
201- skip .setStackTrace (new StackTraceElement [0 ]);
202- skiptests .compareAndSet (null , skip );
203- }
204- }
205- }
206-
207- @ AfterClass
208- static final void printFailedTests (ITestContext context ) {
211+ @ AfterAll
212+ static final void printFailedTests () {
209213 out .println ("\n =========================" );
210214 try {
211215 // Exceptions should already have been added to FAILURES
@@ -230,7 +234,7 @@ static final void printFailedTests(ITestContext context) {
230234 }
231235 }
232236
233- private String [] uris () {
237+ private static String [] uris () {
234238 return new String [] {
235239 http3URI_fixed ,
236240 http3URI_chunk ,
@@ -245,10 +249,9 @@ private String[] uris() {
245249 };
246250 }
247251
248- static AtomicLong URICOUNT = new AtomicLong ();
252+ static final AtomicLong URICOUNT = new AtomicLong ();
249253
250- @ DataProvider (name = "sanity" )
251- public Object [][] sanity () {
254+ public static Object [][] sanity () {
252255 String [] uris = uris ();
253256 Object [][] result = new Object [uris .length * 2 ][];
254257 int i = 0 ;
@@ -261,11 +264,7 @@ public Object[][] sanity() {
261264 return result ;
262265 }
263266
264- @ DataProvider (name = "variants" )
265- public Object [][] variants (ITestContext context ) {
266- if (stopAfterFirstFailure () && context .getFailedTests ().size () > 0 ) {
267- return new Object [0 ][];
268- }
267+ public static Object [][] variants () {
269268 String [] uris = uris ();
270269 Object [][] result = new Object [uris .length * 2 * 2 ][];
271270 int i = 0 ;
@@ -282,21 +281,21 @@ public Object[][] variants(ITestContext context) {
282281 return result ;
283282 }
284283
285- private HttpClient makeNewClient () {
284+ private static HttpClient makeNewClient () {
286285 clientCount .incrementAndGet ();
287- HttpClient client = newClientBuilderForH3 ()
286+ HttpClient client = HttpServerAdapters . createClientBuilderForH3 ()
288287 .proxy (HttpClient .Builder .NO_PROXY )
289288 .executor (executor )
290289 .sslContext (sslContext )
291290 .build ();
292291 return TRACKER .track (client );
293292 }
294293
295- HttpClient newHttpClient (boolean share ) {
294+ static HttpClient newHttpClient (boolean share ) {
296295 if (!share ) return makeNewClient ();
297296 HttpClient shared = sharedClient ;
298297 if (shared != null ) return shared ;
299- synchronized (this ) {
298+ synchronized (AbstractThrowingSubscribers . class ) {
300299 shared = sharedClient ;
301300 if (shared == null ) {
302301 shared = sharedClient = makeNewClient ();
@@ -357,7 +356,7 @@ protected void testSanityImpl(String uri, boolean sameClient)
357356 HttpResponse <String > response = client .send (req , handler );
358357 String body = response .body ();
359358 Stream .of (body .split ("\n " )).forEach (u ->
360- assertEquals (URI .create (u ).getPath (), URI .create (uri2 ).getPath ()));
359+ assertEquals (URI .create (uri2 ).getPath (), URI .create (u ).getPath ()));
361360 if (!sameClient ) {
362361 // Wait for the client to be garbage collected.
363362 // we use the ReferenceTracker API rather than HttpClient::close here,
@@ -469,7 +468,6 @@ <T,U> void testThrowing(String name, String uri, boolean sameClient,
469468 boolean async , EnumSet <Where > excludes )
470469 throws Exception
471470 {
472- checkSkip ();
473471 out .printf ("%n%s%s%n" , now (), name );
474472 try {
475473 testThrowing (uri , sameClient , handlers , finisher , thrower , async , excludes );
@@ -551,7 +549,6 @@ private <T,U> void testThrowing(String uri, boolean sameClient,
551549 if (error != null ) throw error ;
552550 System .out .println (now () + "operation finished normally: " + tracker .getName ());
553551 System .err .println (now () + "operation finished normally: " + tracker .getName ());
554-
555552 }
556553 }
557554 }
@@ -819,8 +816,8 @@ public CompletionStage<T> getBody() {
819816 }
820817
821818
822- @ BeforeTest
823- public void setup () throws Exception {
819+ @ BeforeAll
820+ public static void setup () throws Exception {
824821 System .out .println (now () + "setup" );
825822 System .err .println (now () + "setup" );
826823
@@ -902,8 +899,8 @@ public void setup() throws Exception {
902899 System .err .println (now () + "setup done" );
903900 }
904901
905- @ AfterTest
906- public void teardown () throws Exception {
902+ @ AfterAll
903+ public static void teardown () throws Exception {
907904 System .out .println (now () + "teardown" );
908905 System .err .println (now () + "teardown" );
909906
0 commit comments