test/jdk/com/sun/net/httpserver/bugs/6725892/Test.java
changeset 55031 37a077319427
parent 54314 46cf212cdcca
--- a/test/jdk/com/sun/net/httpserver/bugs/6725892/Test.java	Fri May 24 15:36:48 2019 +0200
+++ b/test/jdk/com/sun/net/httpserver/bugs/6725892/Test.java	Fri May 24 15:34:14 2019 +0100
@@ -26,6 +26,7 @@
  * @bug 6725892
  * @library /test/lib
  * @run main/othervm -Dsun.net.httpserver.maxReqTime=2 Test
+ * @run main/othervm -Djava.net.preferIPv6Addresses=true -Dsun.net.httpserver.maxReqTime=2 Test
  * @summary
  */
 
@@ -49,36 +50,37 @@
 
     static class Handler implements HttpHandler {
 
-        public void handle (HttpExchange t)
+        public void handle(HttpExchange t)
             throws IOException
         {
             InputStream is = t.getRequestBody();
             InetSocketAddress rem = t.getRemoteAddress();
-            System.out.println ("Request from: " + rem);
+            System.out.println("Request from: " + rem);
             while (is.read () != -1) ;
             is.close();
             String requrl = t.getRequestURI().toString();
             OutputStream os = t.getResponseBody();
-            t.sendResponseHeaders (200, RESPONSE_BODY.length());
-            os.write (RESPONSE_BODY.getBytes());
+            t.sendResponseHeaders(200, RESPONSE_BODY.length());
+            os.write(RESPONSE_BODY.getBytes());
             t.close();
         }
     }
 
-    public static void main (String[] args) throws Exception {
+    public static void main(String[] args) throws Exception {
 
         ExecutorService exec = Executors.newCachedThreadPool();
 
+        InetAddress loopback = InetAddress.getLoopbackAddress();
         try {
-            InetSocketAddress addr = new InetSocketAddress (0);
-            s1 = HttpServer.create (addr, 100);
-            HttpHandler h = new Handler ();
-            HttpContext c1 = s1.createContext ("/", h);
+            InetSocketAddress addr = new InetSocketAddress(loopback, 0);
+            s1 = HttpServer.create(addr, 100);
+            HttpHandler h = new Handler();
+            HttpContext c1 = s1.createContext("/", h);
             s1.setExecutor(exec);
             s1.start();
 
             port = s1.getAddress().getPort();
-            System.out.println ("Server on port " + port);
+            System.out.println("Server on port " + port);
             url = URIBuilder.newBuilder()
                 .scheme("http")
                 .loopback()
@@ -89,14 +91,14 @@
             test1();
             test2();
             test3();
-            Thread.sleep (2000);
+            Thread.sleep(2000);
         } catch (Exception e) {
             e.printStackTrace();
-            System.out.println ("FAIL");
-            throw new RuntimeException ();
+            System.out.println("FAIL");
+            throw new RuntimeException();
         } finally {
             s1.stop(0);
-            System.out.println ("After Shutdown");
+            System.out.println("After Shutdown");
             exec.shutdown();
         }
     }
@@ -105,10 +107,10 @@
 
     static void test1() throws IOException {
         failed = false;
-        Socket s = new Socket (InetAddress.getLoopbackAddress(), port);
+        Socket s = new Socket(InetAddress.getLoopbackAddress(), port);
         InputStream is = s.getInputStream();
         // server should close connection after 2 seconds. We wait up to 10
-        s.setSoTimeout (10000);
+        s.setSoTimeout(10000);
         try {
             is.read();
         } catch (SocketTimeoutException e) {
@@ -116,36 +118,36 @@
         }
         s.close();
         if (failed) {
-            System.out.println ("test1: FAIL");
-            throw new RuntimeException ();
+            System.out.println("test1: FAIL");
+            throw new RuntimeException();
         } else {
-            System.out.println ("test1: OK");
+            System.out.println("test1: OK");
         }
     }
 
     // send request and don't read response. Check server closes connection
 
     static void test2() throws IOException {
-        HttpURLConnection urlc = (HttpURLConnection) url.openConnection();
-        urlc.setReadTimeout (20 * 1000);
+        HttpURLConnection urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
+        urlc.setReadTimeout(20 * 1000);
         InputStream is = urlc.getInputStream();
         // we won't read response and check if it times out
         // on server. If it timesout at client then there is a problem
         try {
-            Thread.sleep (10 * 1000);
+            Thread.sleep(10 * 1000);
             while (is.read() != -1) ;
         } catch (InterruptedException e) {
-            System.out.println (e);
-            System.out.println ("test2: FAIL");
-            throw new RuntimeException ("unexpected error");
+            System.out.println(e);
+            System.out.println("test2: FAIL");
+            throw new RuntimeException("unexpected error");
         } catch (SocketTimeoutException e1) {
-            System.out.println (e1);
-            System.out.println ("test2: FAIL");
-            throw new RuntimeException ("client timedout");
+            System.out.println(e1);
+            System.out.println("test2: FAIL");
+            throw new RuntimeException("client timedout");
         } finally {
             is.close();
         }
-        System.out.println ("test2: OK");
+        System.out.println("test2: OK");
     }
 
     // same as test2, but repeated with multiple connections
@@ -174,17 +176,17 @@
         }
 
         void fail(String msg) {
-            System.out.println (msg);
+            System.out.println(msg);
             failed = true;
         }
 
-        public void run () {
+        public void run() {
             HttpURLConnection urlc;
             InputStream is = null;
 
             try {
-                urlc = (HttpURLConnection) url.openConnection();
-                urlc.setReadTimeout (20 * 1000);
+                urlc = (HttpURLConnection) url.openConnection(Proxy.NO_PROXY);
+                urlc.setReadTimeout(20 * 1000);
                 urlc.setDoOutput(true);
             } catch (IOException e) {
                 fail("Worker: failed to connect to server");
@@ -193,17 +195,17 @@
             }
             try {
                 OutputStream os = urlc.getOutputStream();
-                os.write ("foo".getBytes());
+                os.write("foo".getBytes());
                 if (mode == Mode.REQUEST) {
-                    Thread.sleep (3000);
+                    Thread.sleep(3000);
                 }
                 os.close();
                 is = urlc.getInputStream();
                 if (mode == Mode.RESPONSE) {
-                    Thread.sleep (3000);
+                    Thread.sleep(3000);
                 }
-                if (!checkResponse (is, RESPONSE_BODY)) {
-                    fail ("Worker: response");
+                if (!checkResponse(is, RESPONSE_BODY)) {
+                    fail("Worker: response");
                 }
                 is.close();
                 return;
@@ -214,11 +216,11 @@
             } catch (IOException e2) {
                 switch (mode) {
                   case NORMAL:
-                    fail ("Worker: " + e2.getMessage());
+                    fail("Worker: " + e2.getMessage());
                     break;
                   case RESPONSE:
                     if (is == null) {
-                        fail ("Worker: " + e2.getMessage());
+                        fail("Worker: " + e2.getMessage());
                         break;
                     }
                   // default: is ok
@@ -233,12 +235,12 @@
 
     static void test3() throws Exception {
         failed = false;
-        CountDownLatch l = new CountDownLatch (NUM*3);
+        CountDownLatch l = new CountDownLatch(NUM*3);
         Worker[] workers = new Worker[NUM*3];
         for (int i=0; i<NUM; i++) {
-            workers[i*3] = new Worker (l, Worker.Mode.NORMAL);
-            workers[i*3+1] = new Worker (l, Worker.Mode.REQUEST);
-            workers[i*3+2] = new Worker (l, Worker.Mode.RESPONSE);
+            workers[i*3] = new Worker(l, Worker.Mode.NORMAL);
+            workers[i*3+1] = new Worker(l, Worker.Mode.REQUEST);
+            workers[i*3+2] = new Worker(l, Worker.Mode.RESPONSE);
             workers[i*3].start();
             workers[i*3+1].start();
             workers[i*3+2].start();
@@ -248,26 +250,26 @@
             workers[i].join();
         }
         if (failed) {
-            throw new RuntimeException ("test3: failed");
+            throw new RuntimeException("test3: failed");
         }
-        System.out.println ("test3: OK");
+        System.out.println("test3: OK");
     }
 
-    static boolean checkResponse (InputStream is, String resp) {
+    static boolean checkResponse(InputStream is, String resp) {
         try {
             ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            byte[] buf = new byte [64];
+            byte[] buf = new byte[64];
             int c;
             while ((c=is.read(buf)) != -1) {
-                bos.write (buf, 0, c);
+                bos.write(buf, 0, c);
             }
             bos.close();
             if (!bos.toString().equals(resp)) {
-                System.out.println ("Wrong response: " + bos.toString());
+                System.out.println("Wrong response: " + bos.toString());
                 return false;
             }
         } catch (IOException e) {
-            System.out.println (e);
+            System.out.println(e);
             return false;
         }
         return true;