8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use
authorasmotrak
Tue, 14 Jun 2016 10:27:23 -0700
changeset 38952 ca428fb526d4
parent 38951 077499b63957
child 38953 2cd777b7c655
8159038: javax/net/ssl/SSLSession/SessionCacheSizeTests.java failed with java.net.SocketException: Address already in use Reviewed-by: xuelei
jdk/test/javax/net/ssl/SSLSession/SessionCacheSizeTests.java
--- a/jdk/test/javax/net/ssl/SSLSession/SessionCacheSizeTests.java	Tue Jun 14 09:03:12 2016 -0700
+++ b/jdk/test/javax/net/ssl/SSLSession/SessionCacheSizeTests.java	Tue Jun 14 10:27:23 2016 -0700
@@ -106,17 +106,21 @@
      */
     static int MAX_ACTIVE_CONNECTIONS = 4;
 
-    void doServerSide(int serverPort, int serverConns) throws Exception {
+    static final int FREE_PORT = 0;
 
+    void doServerSide(int serverConns) throws Exception {
         try (SSLServerSocket sslServerSocket =
-                (SSLServerSocket) sslssf.createServerSocket(serverPort)) {
+                (SSLServerSocket) sslssf.createServerSocket(FREE_PORT)) {
 
             // timeout to accept a connection
             sslServerSocket.setSoTimeout(45000);
 
             // make sure createdPorts++ is atomic
             synchronized(serverPorts) {
-                serverPorts[createdPorts++] = sslServerSocket.getLocalPort();
+                int serverPort = sslServerSocket.getLocalPort();
+                System.out.printf("server #%d started on port %d%n",
+                        createdPorts, serverPort);
+                serverPorts[createdPorts++] = serverPort;
 
                 /*
                  * Signal Client, we're ready for his connect.
@@ -173,11 +177,54 @@
         SSLSessionContext sessCtx = sslctx.getClientSessionContext();
         sessCtx.setSessionTimeout(0); // no limit
 
-        while (nConnections < (MAX_ACTIVE_CONNECTIONS - 1)) {
-            // divide the connections among the available server ports
+        try {
+            while (nConnections < (MAX_ACTIVE_CONNECTIONS - 1)) {
+                // divide the connections among the available server ports
+                int serverPort = serverPorts [nConnections % (serverPorts.length)];
+                System.out.printf("client #%d connects to port %d%n",
+                        nConnections, serverPort);
+                sslSockets[nConnections] = (SSLSocket) sslsf.
+                            createSocket("localhost",
+                            serverPort);
+                InputStream sslIS = sslSockets[nConnections].getInputStream();
+                OutputStream sslOS = sslSockets[nConnections].getOutputStream();
+                sslOS.write(237);
+                sslOS.flush();
+                int read = sslIS.read();
+                SSLSession sess = sslSockets[nConnections].getSession();
+                if (!sessions.contains(sess))
+                    sessions.add(sess);
+                nConnections++;
+            }
+            System.out.println("Current cacheSize is set to: " +
+                                    sessCtx.getSessionCacheSize());
+            System.out.println();
+            System.out.println("Currently cached Sessions......");
+            System.out.println("============================================"
+                                    + "============================");
+            System.out.println("Session                                     "
+                                    + "      Session-last-accessTime");
+            System.out.println("============================================"
+                                    + "============================");
+            checkCachedSessions(sessCtx, nConnections);
+            // Change session cache size
+            sessCtx.setSessionCacheSize(2);
+            System.out.println("Session cache size changed to: "
+                            + sessCtx.getSessionCacheSize());
+            System.out.println();
+            checkCachedSessions(sessCtx, nConnections);
+
+            // Test the effect of increasing the cache size
+            sessCtx.setSessionCacheSize(3);
+            System.out.println("Session cache size changed to: "
+                            + sessCtx.getSessionCacheSize());
+            // create a new session
+            int serverPort = serverPorts [nConnections % (serverPorts.length)];
+            System.out.printf("client #%d connects to port %d%n",
+                    nConnections, serverPort);
             sslSockets[nConnections] = (SSLSocket) sslsf.
-                        createSocket("localhost",
-                        serverPorts [nConnections % (serverPorts.length)]);
+                            createSocket("localhost",
+                            serverPort);
             InputStream sslIS = sslSockets[nConnections].getInputStream();
             OutputStream sslOS = sslSockets[nConnections].getOutputStream();
             sslOS.write(237);
@@ -187,48 +234,15 @@
             if (!sessions.contains(sess))
                 sessions.add(sess);
             nConnections++;
-        }
-        System.out.println("Current cacheSize is set to: " +
-                                 sessCtx.getSessionCacheSize());
-        System.out.println();
-        System.out.println("Currently cached Sessions......");
-        System.out.println("============================================"
-                                + "============================");
-        System.out.println("Session                                     "
-                                + "      Session-last-accessTime");
-        System.out.println("============================================"
-                                + "============================");
-        checkCachedSessions(sessCtx, nConnections);
-        // Change session cache size
-        sessCtx.setSessionCacheSize(2);
-        System.out.println("Session cache size changed to: "
-                        + sessCtx.getSessionCacheSize());
-        System.out.println();
-        checkCachedSessions(sessCtx, nConnections);
 
-        // Test the effect of increasing the cache size
-        sessCtx.setSessionCacheSize(3);
-        System.out.println("Session cache size changed to: "
-                        + sessCtx.getSessionCacheSize());
-        // create a new session
-        sslSockets[nConnections] = (SSLSocket) sslsf.
-                        createSocket("localhost",
-                        serverPorts [nConnections % (serverPorts.length)]);
-        InputStream sslIS = sslSockets[nConnections].getInputStream();
-        OutputStream sslOS = sslSockets[nConnections].getOutputStream();
-        sslOS.write(237);
-        sslOS.flush();
-        int read = sslIS.read();
-        SSLSession sess = sslSockets[nConnections].getSession();
-        if (!sessions.contains(sess))
-            sessions.add(sess);
-        nConnections++;
-
-        // test the number of sessions cached against the cache size
-        checkCachedSessions(sessCtx, nConnections);
-
-        for (int i = 0; i < nConnections; i++) {
-            sslSockets[i].close();
+            // test the number of sessions cached against the cache size
+            checkCachedSessions(sessCtx, nConnections);
+        } finally {
+            for (int i = 0; i < nConnections; i++) {
+                if (sslSockets[i] != null) {
+                    sslSockets[i].close();
+                }
+            }
         }
         System.out.println("Session cache size tests passed");
     }
@@ -302,7 +316,9 @@
         sslctx = SSLContext.getInstance("TLS");
         KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
         KeyStore ks = KeyStore.getInstance("JKS");
-        ks.load(new FileInputStream(keyFilename), passwd.toCharArray());
+        try (FileInputStream fis = new FileInputStream(keyFilename)) {
+            ks.load(fis, passwd.toCharArray());
+        }
         kmf.init(ks, passwd.toCharArray());
         sslctx.init(kmf.getKeyManagers(), null, null);
         sslssf = (SSLServerSocketFactory) sslctx.getServerSocketFactory();
@@ -343,19 +359,21 @@
                 for (int i = 0; i < serverPorts.length; i++) {
                     // distribute remaining connections among the
                     // available ports
-                    if (i < remainingConns)
-                        startServer(serverPorts[i], (serverConns + 1), true);
-                    else
-                        startServer(serverPorts[i], serverConns, true);
+                    if (i < remainingConns) {
+                        startServer(serverConns + 1, true);
+                    } else {
+                        startServer(serverConns, true);
+                    }
                 }
                 startClient(false);
             } else {
                 startClient(true);
                 for (int i = 0; i < serverPorts.length; i++) {
-                    if (i < remainingConns)
-                        startServer(serverPorts[i], (serverConns + 1), false);
-                    else
-                        startServer(serverPorts[i], serverConns, false);
+                    if (i < remainingConns) {
+                        startServer(serverConns + 1, false);
+                    } else {
+                        startServer(serverConns, false);
+                    }
                 }
             }
         } catch (Exception e) {
@@ -420,13 +438,12 @@
         // Fall-through: no exception to throw!
     }
 
-    void startServer(final int port, final int nConns,
-                        boolean newThread) throws Exception {
+    void startServer(final int nConns, boolean newThread) throws Exception {
         if (newThread) {
             serverThread = new Thread() {
                 public void run() {
                     try {
-                        doServerSide(port, nConns);
+                        doServerSide(nConns);
                     } catch (Exception e) {
                         /*
                          * Our server thread just died.
@@ -443,7 +460,7 @@
             serverThread.start();
         } else {
             try {
-                doServerSide(port, nConns);
+                doServerSide(nConns);
             } catch (Exception e) {
                 serverException = e;
             } finally {