8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out
authorxuelei
Tue, 12 Jan 2016 08:05:16 +0000
changeset 34948 9293584191b6
parent 34947 e937e10e46f7
child 34949 304424bbae03
8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out Reviewed-by: weijun, wetmore
jdk/test/javax/net/ssl/SSLSession/SessionCacheSizeTests.java
--- a/jdk/test/javax/net/ssl/SSLSession/SessionCacheSizeTests.java	Mon Jan 11 10:13:29 2016 -0800
+++ b/jdk/test/javax/net/ssl/SSLSession/SessionCacheSizeTests.java	Tue Jan 12 08:05:16 2016 +0000
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2014, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2016, 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
@@ -109,28 +109,34 @@
 
     void doServerSide(int serverPort, int serverConns) throws Exception {
 
-        SSLServerSocket sslServerSocket =
-            (SSLServerSocket) sslssf.createServerSocket(serverPort);
-        sslServerSocket.setSoTimeout(45000); // timeout to accept a connection
-        serverPorts[createdPorts++] = sslServerSocket.getLocalPort();
+        try (SSLServerSocket sslServerSocket =
+                (SSLServerSocket) sslssf.createServerSocket(serverPort)) {
+
+            // timeout to accept a connection
+            sslServerSocket.setSoTimeout(45000);
+
+            // make sure createdPorts++ is atomic
+            synchronized(serverPorts) {
+                serverPorts[createdPorts++] = sslServerSocket.getLocalPort();
 
-        /*
-         * Signal Client, we're ready for his connect.
-         */
-        if (createdPorts == serverPorts.length) {
-            serverReady = true;
-        }
-        int read = 0;
-        int nConnections = 0;
-        /*
-         * Divide the max connections among the available server ports.
-         * The use of more than one server port ensures creation of more
-         * than one session.
-         */
-        SSLSession sessions [] = new SSLSession [serverConns];
-        SSLSessionContext sessCtx = sslctx.getServerSessionContext();
+                /*
+                 * Signal Client, we're ready for his connect.
+                 */
+                if (createdPorts == serverPorts.length) {
+                    serverReady = true;
+                }
+            }
+            int read = 0;
+            int nConnections = 0;
 
-        try {
+            /*
+             * Divide the max connections among the available server ports.
+             * The use of more than one server port ensures creation of more
+             * than one session.
+             */
+            SSLSession sessions [] = new SSLSession [serverConns];
+            SSLSessionContext sessCtx = sslctx.getServerSessionContext();
+
             while (nConnections < serverConns) {
                 try (SSLSocket sslSocket =
                         (SSLSocket)sslServerSocket.accept()) {
@@ -144,8 +150,6 @@
                     nConnections++;
                 }
             }
-        } finally {
-            sslServerSocket.close();
         }
     }
 
@@ -271,8 +275,8 @@
      * Using four ports (one per each connection), we are able to create
      * alteast four sessions.
      */
-    volatile int serverPorts[] = new int[]{0, 0, 0, 0};
-    volatile int createdPorts = 0;
+    int serverPorts[] = new int[]{0, 0, 0, 0};  // MAX_ACTIVE_CONNECTIONS: 4
+    int createdPorts = 0;
     static SSLServerSocketFactory sslssf;
     static SSLSocketFactory sslsf;
     static SSLContext sslctx;