changeset 11655:931c1185bb1b

8146387: Test SSLSession/SessionCacheSizeTests socket accept timed out Reviewed-by: weijun, wetmore
author igerasim
date Tue, 26 Apr 2016 20:31:59 +0300
parents 2f6f19966859
children 7dd4682b4208
files test/sun/security/ssl/javax/net/ssl/NewAPIs/SessionCacheSizeTests.java
diffstat 1 files changed, 29 insertions(+), 25 deletions(-) [+]
line wrap: on
line diff
--- a/test/sun/security/ssl/javax/net/ssl/NewAPIs/SessionCacheSizeTests.java	Tue Apr 26 20:31:58 2016 +0300
+++ b/test/sun/security/ssl/javax/net/ssl/NewAPIs/SessionCacheSizeTests.java	Tue Apr 26 20:31:59 2016 +0300
@@ -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
@@ -108,28 +108,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)) {
 
-        /*
-         * 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();
+            // timeout to accept a connection
+            sslServerSocket.setSoTimeout(45000);
 
-        try {
+            // 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();
+
             while (nConnections < serverConns) {
                 try (SSLSocket sslSocket =
                         (SSLSocket)sslServerSocket.accept()) {
@@ -143,8 +149,6 @@
                     nConnections++;
                 }
             }
-        } finally {
-            sslServerSocket.close();
         }
     }
 
@@ -270,8 +274,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;