8159501: ShortRSAKey512.java intermittently times out
authorasmotrak
Fri, 17 Jun 2016 19:45:06 -0700
changeset 39063 500967b1216c
parent 39062 93c6e022db74
child 39064 45cbf2937973
8159501: ShortRSAKey512.java intermittently times out Reviewed-by: xuelei
jdk/test/javax/net/ssl/TLSv12/ShortRSAKey512.java
--- a/jdk/test/javax/net/ssl/TLSv12/ShortRSAKey512.java	Fri Jun 17 14:37:00 2016 -0700
+++ b/jdk/test/javax/net/ssl/TLSv12/ShortRSAKey512.java	Fri Jun 17 19:45:06 2016 -0700
@@ -126,7 +126,7 @@
     /*
      * Turn on SSL debugging?
      */
-    static boolean debug = false;
+    static boolean debug = true;
 
     /*
      * Define the server side of the test.
@@ -138,24 +138,26 @@
         SSLContext context = generateSSLContext(null, targetCertStr,
                                             targetPrivateKey);
         SSLServerSocketFactory sslssf = context.getServerSocketFactory();
-        SSLServerSocket sslServerSocket =
-            (SSLServerSocket)sslssf.createServerSocket(serverPort);
-        serverPort = sslServerSocket.getLocalPort();
+        try (SSLServerSocket sslServerSocket =
+                (SSLServerSocket) sslssf.createServerSocket(serverPort)) {
 
-        /*
-         * Signal Client, we're ready for his connect.
-         */
-        serverReady = true;
+            serverPort = sslServerSocket.getLocalPort();
+            System.out.println("Start server on port " + serverPort);
 
-        SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept();
-        InputStream sslIS = sslSocket.getInputStream();
-        OutputStream sslOS = sslSocket.getOutputStream();
+            /*
+            * Signal Client, we're ready for his connect.
+            */
+            serverReady = true;
 
-        sslIS.read();
-        sslOS.write('A');
-        sslOS.flush();
+            try (SSLSocket sslSocket = (SSLSocket)sslServerSocket.accept()) {
+                InputStream sslIS = sslSocket.getInputStream();
+                OutputStream sslOS = sslSocket.getOutputStream();
 
-        sslSocket.close();
+                sslIS.read();
+                sslOS.write('A');
+                sslOS.flush();
+            }
+        }
     }
 
     /*
@@ -176,24 +178,24 @@
         SSLContext context = generateSSLContext(trustedCertStr, null, null);
         SSLSocketFactory sslsf = context.getSocketFactory();
 
-        SSLSocket sslSocket =
-            (SSLSocket)sslsf.createSocket("localhost", serverPort);
+        System.out.println("Client connects to port " + serverPort);
+        try (SSLSocket sslSocket =
+                (SSLSocket) sslsf.createSocket("localhost", serverPort)) {
 
-        // enable TLSv1.2 only
-        sslSocket.setEnabledProtocols(new String[] {"TLSv1.2"});
+            // enable TLSv1.2 only
+            sslSocket.setEnabledProtocols(new String[] {"TLSv1.2"});
 
-        // enable a block cipher
-        sslSocket.setEnabledCipherSuites(
-            new String[] {"TLS_DHE_RSA_WITH_AES_128_CBC_SHA"});
+            // enable a block cipher
+            sslSocket.setEnabledCipherSuites(
+                new String[] {"TLS_DHE_RSA_WITH_AES_128_CBC_SHA"});
 
-        InputStream sslIS = sslSocket.getInputStream();
-        OutputStream sslOS = sslSocket.getOutputStream();
+            InputStream sslIS = sslSocket.getInputStream();
+            OutputStream sslOS = sslSocket.getOutputStream();
 
-        sslOS.write('B');
-        sslOS.flush();
-        sslIS.read();
-
-        sslSocket.close();
+            sslOS.write('B');
+            sslOS.flush();
+            sslIS.read();
+        }
     }
 
     /*
@@ -309,16 +311,12 @@
      * Fork off the other side, then do your work.
      */
     ShortRSAKey512() throws Exception {
-        try {
-            if (separateServerThread) {
-                startServer(true);
-                startClient(false);
-            } else {
-                startClient(true);
-                startServer(false);
-            }
-        } catch (Exception e) {
-            // swallow for now.  Show later
+        if (separateServerThread) {
+            startServer(true);
+            startClient(false);
+        } else {
+            startClient(true);
+            startServer(false);
         }
 
         /*
@@ -336,16 +334,13 @@
          */
         Exception local;
         Exception remote;
-        String whichRemote;
 
         if (separateServerThread) {
             remote = serverException;
             local = clientException;
-            whichRemote = "server";
         } else {
             remote = clientException;
             local = serverException;
-            whichRemote = "client";
         }
 
         /*
@@ -353,9 +348,6 @@
          * print the remote side Exception
          */
         if ((local != null) && (remote != null)) {
-            System.out.println(whichRemote + " also threw:");
-            remote.printStackTrace();
-            System.out.println();
             throw local;
         }
 
@@ -368,7 +360,7 @@
         }
     }
 
-    void startServer(boolean newThread) throws Exception {
+    void startServer(boolean newThread) {
         if (newThread) {
             serverThread = new Thread() {
                 public void run() {
@@ -381,11 +373,13 @@
                          * Release the client, if not active already...
                          */
                         System.err.println("Server died...");
+                        e.printStackTrace(System.err);
                         serverReady = true;
                         serverException = e;
                     }
                 }
             };
+            serverThread.setDaemon(true);
             serverThread.start();
         } else {
             try {
@@ -398,7 +392,7 @@
         }
     }
 
-    void startClient(boolean newThread) throws Exception {
+    void startClient(boolean newThread) {
         if (newThread) {
             clientThread = new Thread() {
                 public void run() {
@@ -409,10 +403,12 @@
                          * Our client thread just died.
                          */
                         System.err.println("Client died...");
+                        e.printStackTrace(System.err);
                         clientException = e;
                     }
                 }
             };
+            clientThread.setDaemon(true);
             clientThread.start();
         } else {
             try {