2121 * questions.
2222 */
2323
24- /*
25- * @test
26- * @bug 6270015
27- * @library /test/lib
28- * @build jdk.test.lib.Asserts
29- * jdk.test.lib.Utils
30- * jdk.test.lib.net.SimpleSSLContext
31- * jdk.test.lib.net.URIBuilder
32- * @run main/othervm Test12
33- * @run main/othervm -Djava.net.preferIPv6Addresses=true Test12
34- * @summary Light weight HTTP server
35- */
36-
3724import com .sun .net .httpserver .*;
3825
3926import java .nio .file .Files ;
4027import java .nio .file .Path ;
28+ import java .util .ArrayList ;
29+ import java .util .List ;
4130import java .util .concurrent .*;
4231import java .io .*;
4332import java .net .*;
4938import static jdk .test .lib .Asserts .assertFileContentsEqual ;
5039import static jdk .test .lib .Utils .createTempFileOfSize ;
5140
52- /* basic http/s connectivity test
53- * Tests:
54- * - same as Test1, but in parallel
41+ /*
42+ * @test
43+ * @bug 6270015 8359477
44+ * @summary Light weight HTTP server - basic http/s connectivity test, same as Test1,
45+ * but in parallel
46+ * @library /test/lib
47+ * @build jdk.test.lib.Asserts
48+ * jdk.test.lib.Utils
49+ * jdk.test.lib.net.SimpleSSLContext
50+ * jdk.test.lib.net.URIBuilder
51+ * @run main/othervm Test12
52+ * @run main/othervm -Djava.net.preferIPv6Addresses=true Test12
5553 */
56-
5754public class Test12 extends Test {
5855
5956 private static final String TEMP_FILE_PREFIX =
6057 HttpServer .class .getPackageName () + '-' + Test12 .class .getSimpleName () + '-' ;
6158
6259 static SSLContext ctx ;
6360
64- static boolean fail = false ;
65-
6661 public static void main (String [] args ) throws Exception {
6762 HttpServer s1 = null ;
6863 HttpsServer s2 = null ;
69- ExecutorService executor =null ;
7064 Path smallFilePath = createTempFileOfSize (TEMP_FILE_PREFIX , null , 23 );
7165 Path largeFilePath = createTempFileOfSize (TEMP_FILE_PREFIX , null , 2730088 );
66+ final ExecutorService executor = Executors .newCachedThreadPool ();
7267 try {
7368 System .out .print ("Test12: " );
7469 InetAddress loopback = InetAddress .getLoopbackAddress ();
@@ -80,7 +75,6 @@ public static void main (String[] args) throws Exception {
8075 HttpHandler h = new FileServerHandler (smallFilePath .getParent ().toString ());
8176 HttpContext c1 = s1 .createContext ("/" , h );
8277 HttpContext c2 = s2 .createContext ("/" , h );
83- executor = Executors .newCachedThreadPool ();
8478 s1 .setExecutor (executor );
8579 s2 .setExecutor (executor );
8680 ctx = new SimpleSSLContext ().get ();
@@ -90,7 +84,7 @@ public static void main (String[] args) throws Exception {
9084
9185 int port = s1 .getAddress ().getPort ();
9286 int httpsport = s2 .getAddress ().getPort ();
93- Runner r [] = new Runner [8 ];
87+ final Runner [] r = new Runner [8 ];
9488 r [0 ] = new Runner (true , "http" , port , smallFilePath );
9589 r [1 ] = new Runner (true , "http" , port , largeFilePath );
9690 r [2 ] = new Runner (true , "https" , httpsport , smallFilePath );
@@ -99,95 +93,83 @@ public static void main (String[] args) throws Exception {
9993 r [5 ] = new Runner (false , "http" , port , largeFilePath );
10094 r [6 ] = new Runner (false , "https" , httpsport , smallFilePath );
10195 r [7 ] = new Runner (false , "https" , httpsport , largeFilePath );
102- start (r );
103- join (r );
104- System .out .println ("OK" );
96+ // submit the tasks
97+ final List <Future <Void >> futures = new ArrayList <>();
98+ for (Runner runner : r ) {
99+ futures .add (executor .submit (runner ));
100+ }
101+ // wait for the tasks' completion
102+ for (Future <Void > f : futures ) {
103+ f .get ();
104+ }
105+ System .out .println ("All " + futures .size () + " tasks completed successfully" );
105106 } finally {
106- if (s1 != null )
107+ if (s1 != null ) {
107108 s1 .stop (0 );
108- if (s2 != null )
109+ }
110+ if (s2 != null ) {
109111 s2 .stop (0 );
110- if (executor != null )
111- executor .shutdown ();
112+ }
113+ executor .close ();
114+ // it's OK to delete these files since the server side handlers
115+ // serving these files have completed (guaranteed by the completion of Executor.close())
116+ System .out .println ("deleting " + smallFilePath );
112117 Files .delete (smallFilePath );
118+ System .out .println ("deleting " + largeFilePath );
113119 Files .delete (largeFilePath );
114120 }
115121 }
116122
117- static void start (Runner [] x ) {
118- for (int i =0 ; i <x .length ; i ++) {
119- x [i ].start ();
120- }
121- }
122-
123- static void join (Runner [] x ) {
124- for (int i =0 ; i <x .length ; i ++) {
125- try {
126- x [i ].join ();
127- } catch (InterruptedException e ) {}
128- }
129- }
130-
131-
132- static class Runner extends Thread {
123+ static class Runner implements Callable <Void > {
133124
134125 boolean fixedLen ;
135126 String protocol ;
136127 int port ;
137128 private final Path filePath ;
138129
139- Runner (boolean fixedLen , String protocol , int port , Path filePath ) {
130+ Runner (boolean fixedLen , String protocol , int port , Path filePath ) {
140131 this .fixedLen =fixedLen ;
141132 this .protocol =protocol ;
142133 this .port =port ;
143134 this .filePath = filePath ;
144135 }
145136
146- public void run () {
147- try {
148- URL url = URIBuilder .newBuilder ()
149- .scheme (protocol )
150- .loopback ()
151- .port (port )
152- .path ("/" + filePath .getFileName ())
153- .toURL ();
154- HttpURLConnection urlc = (HttpURLConnection ) url .openConnection (Proxy .NO_PROXY );
155- if (urlc instanceof HttpsURLConnection ) {
156- HttpsURLConnection urlcs = (HttpsURLConnection ) urlc ;
157- urlcs .setHostnameVerifier (new HostnameVerifier () {
158- public boolean verify (String s , SSLSession s1 ) {
159- return true ;
160- }
161- });
162- urlcs .setSSLSocketFactory (ctx .getSocketFactory ());
163- }
164- byte [] buf = new byte [4096 ];
165-
166- if (fixedLen ) {
167- urlc .setRequestProperty ("XFixed" , "yes" );
168- }
169- InputStream is = urlc .getInputStream ();
170- File temp = File .createTempFile ("Test1" , null );
171- temp .deleteOnExit ();
172- OutputStream fout = new BufferedOutputStream (new FileOutputStream (temp ));
173- int c , count = 0 ;
174- while ((c =is .read (buf )) != -1 ) {
175- count += c ;
176- fout .write (buf , 0 , c );
177- }
178- is .close ();
179- fout .close ();
180-
181- if (count != filePath .toFile ().length ()) {
182- throw new RuntimeException ("wrong amount of data returned" );
183- }
184- assertFileContentsEqual (filePath , temp .toPath ());
185- temp .delete ();
186- } catch (Exception e ) {
187- e .printStackTrace ();
188- fail = true ;
137+ @ Override
138+ public Void call () throws Exception {
139+ final URL url = URIBuilder .newBuilder ()
140+ .scheme (protocol )
141+ .loopback ()
142+ .port (port )
143+ .path ("/" + filePath .getFileName ())
144+ .toURL ();
145+ final HttpURLConnection urlc = (HttpURLConnection ) url .openConnection (Proxy .NO_PROXY );
146+ if (urlc instanceof HttpsURLConnection ) {
147+ HttpsURLConnection urlcs = (HttpsURLConnection ) urlc ;
148+ urlcs .setHostnameVerifier (new HostnameVerifier () {
149+ public boolean verify (String s , SSLSession s1 ) {
150+ return true ;
151+ }
152+ });
153+ urlcs .setSSLSocketFactory (ctx .getSocketFactory ());
189154 }
155+ if (fixedLen ) {
156+ urlc .setRequestProperty ("XFixed" , "yes" );
157+ }
158+ final Path temp = Files .createTempFile (Path .of ("." ), "Test12" , null );
159+ final long numReceived ;
160+ try (InputStream is = urlc .getInputStream ();
161+ OutputStream fout = new BufferedOutputStream (new FileOutputStream (temp .toFile ()))) {
162+ numReceived = is .transferTo (fout );
163+ }
164+ System .out .println ("received " + numReceived + " response bytes for " + url );
165+ final long expected = filePath .toFile ().length ();
166+ if (numReceived != expected ) {
167+ throw new RuntimeException ("expected " + expected + " bytes, but received "
168+ + numReceived );
169+ }
170+ assertFileContentsEqual (filePath , temp );
171+ Files .delete (temp );
172+ return null ;
190173 }
191174 }
192-
193175}
0 commit comments