8210130: java/net/httpclient/UnknownBodyLengthTest.java failed
authordfuchs
Mon, 28 Jan 2019 15:24:41 +0000
changeset 53522 40eb23e0a8c5
parent 53521 41fa3e6f2785
child 53523 4c5184c56dc2
8210130: java/net/httpclient/UnknownBodyLengthTest.java failed Summary: The fix improves the test robustness by making it retry once in case of IO/Connect exception. Reviewed-by: chegar
test/jdk/java/net/httpclient/UnknownBodyLengthTest.java
--- a/test/jdk/java/net/httpclient/UnknownBodyLengthTest.java	Mon Jan 28 13:51:16 2019 +0000
+++ b/test/jdk/java/net/httpclient/UnknownBodyLengthTest.java	Mon Jan 28 15:24:41 2019 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -42,10 +42,14 @@
  * @bug 8207966
  * @library /test/lib
  * @build jdk.test.lib.net.SimpleSSLContext
- * @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain false
- * @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL false
- * @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain true
- * @run main/othervm -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL true
+ * @run main/othervm -Djdk.httpclient.enableAllMethodRetry
+ *                   -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain false
+ * @run main/othervm -Djdk.httpclient.enableAllMethodRetry
+ *                   -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL false
+ * @run main/othervm -Djdk.httpclient.enableAllMethodRetry
+ *                   -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest plain true
+ * @run main/othervm -Djdk.httpclient.enableAllMethodRetry
+ *                   -Djdk.tls.acknowledgeCloseNotify=true UnknownBodyLengthTest SSL true
  */
 
 public class UnknownBodyLengthTest {
@@ -84,46 +88,55 @@
                 throw new RuntimeException("error at position " + i);
     }
 
+    volatile boolean stopped;
+
     void server(final boolean withContentLength) {
         fillBuf(BUF);
         try {
-            Socket s = ss.accept();
-            s.setTcpNoDelay(true);
-            s.setSoLinger(true, 1);
-            System.out.println("Accepted: "+s.getRemoteSocketAddress());
-            System.out.println("Accepted: "+s);
-            OutputStream os = s.getOutputStream();
-            InputStream is = s.getInputStream();
-            boolean done = false;
-            byte[] buf = new byte[1024];
-            String rsp = "";
-            while (!done) {
-                int c = is.read(buf);
-                if (c < 0) break;
-                String s1 = new String(buf, 0, c, "ISO-8859-1");
-                rsp += s1;
-                done = rsp.endsWith("!#!#");
+            while (!stopped) {
+                try (Socket s = ss.accept()) {
+                    s.setTcpNoDelay(true);
+                    s.setSoLinger(true, 1);
+                    System.out.println("Accepted: " + s.getRemoteSocketAddress());
+                    System.out.println("Accepted: " + s);
+                    OutputStream os = s.getOutputStream();
+                    InputStream is = s.getInputStream();
+                    boolean done = false;
+                    byte[] buf = new byte[1024];
+                    String rsp = "";
+                    while (!done) {
+                        int c = is.read(buf);
+                        if (c < 0) break;
+                        String s1 = new String(buf, 0, c, "ISO-8859-1");
+                        rsp += s1;
+                        done = rsp.endsWith("!#!#");
+                    }
+                    String r = "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type:" +
+                            " text/xml; charset=UTF-8\r\n";
+                    os.write(r.getBytes());
+                    String chdr = "Content-Length: " + Integer.toString(BUF.length) +
+                            "\r\n";
+                    System.out.println(chdr);
+                    if (withContentLength)
+                        os.write(chdr.getBytes());
+                    os.write("\r\n".getBytes());
+                    os.write(BUF);
+                    if (is.available() > 0)
+                        is.read(buf);
+                    os.flush();
+                    os.close();
+                    s.shutdownOutput();
+                } catch(Exception e) {
+                    if (!stopped) {
+                        System.out.println("Unexpected server exception: " + e);
+                        e.printStackTrace();
+                    }
+                }
             }
-            String r = "HTTP/1.0 200 OK\r\nConnection: close\r\nContent-Type:" +
-                       " text/xml; charset=UTF-8\r\n";
-            os.write(r.getBytes());
-            String chdr = "Content-Length: " + Integer.toString(BUF.length) +
-                     "\r\n";
-            System.out.println(chdr);
-            if(withContentLength)
-                os.write(chdr.getBytes());
-            os.write("\r\n".getBytes());
-            os.write(BUF);
-            if (is.available() > 0)
-                is.read(buf);
-            os.flush();
-            os.close();
-            s.shutdownOutput();
-            s.close();
         } catch(final Throwable t) {
-          t.printStackTrace();
+            if (!stopped) t.printStackTrace();
         } finally {
-            try {ss.close(); } catch (Exception e) {}
+            try {stopped = true; ss.close(); } catch (Exception e) {}
         }
     }
 
@@ -154,11 +167,20 @@
         boolean ssl = args[0].equals("SSL");
         boolean fixedlen = args[1].equals("true");
         UnknownBodyLengthTest test = new UnknownBodyLengthTest(ssl);
-        test.run(ssl, fixedlen);
+        try {
+            test.run(ssl, fixedlen);
+        } finally {
+            test.stop();
+        }
     }
 
     public void run(boolean ssl, boolean fixedlen) throws Exception {
         new Thread(()->server(fixedlen)).start();
         client(ssl);
     }
+
+    public void stop() {
+        stopped = true;
+        try { ss.close(); } catch (Throwable t) { }
+    }
 }