Extend test coverage of UDP sockets niosocketimpl-branch
authoralanb
Sun, 07 Apr 2019 09:39:59 +0100
branchniosocketimpl-branch
changeset 57309 dbd1beb994ab
parent 57308 f22d38b23756
child 57310 c1fad761a86e
Extend test coverage of UDP sockets
test/jdk/java/net/Socket/Timeouts.java
test/jdk/java/net/Socket/UdpSocket.java
--- a/test/jdk/java/net/Socket/Timeouts.java	Sat Apr 06 16:36:24 2019 +0100
+++ b/test/jdk/java/net/Socket/Timeouts.java	Sun Apr 07 09:39:59 2019 +0100
@@ -494,8 +494,8 @@
             Future<Socket> result1 = pool.submit(ss::accept);
             Future<Socket> result2 = pool.submit(ss::accept);
 
-            // establish connection after 3 seconds
-            scheduleConnect(ss.getLocalSocketAddress(), 3000);
+            // establish connection after 2 seconds
+            scheduleConnect(ss.getLocalSocketAddress(), 2000);
 
             // one task should have accepted the connection, the other should
             // have completed with SocketTimeoutException
@@ -515,7 +515,7 @@
             }
             assertTrue((s1 != null) ^ (s2 != null));
 
-            // should get here in 4 seconds, not 7 seconds
+            // should get here in 4 seconds, not 8 seconds
             int timeout = ss.getSoTimeout();
             checkDuration(start, timeout-100, timeout+2000);
         } finally {
--- a/test/jdk/java/net/Socket/UdpSocket.java	Sat Apr 06 16:36:24 2019 +0100
+++ b/test/jdk/java/net/Socket/UdpSocket.java	Sun Apr 07 09:39:59 2019 +0100
@@ -28,6 +28,7 @@
  */
 
 import java.io.IOException;
+import java.lang.ref.WeakReference;
 import java.net.InetAddress;
 import java.net.InetSocketAddress;
 import java.net.Socket;
@@ -37,7 +38,8 @@
 import java.security.Permission;
 import java.util.Arrays;
 import java.util.ArrayList;
-import java.util.List;
+import java.util.ArrayDeque;
+import java.util.Deque;
 
 import org.testng.annotations.Test;
 import static org.testng.Assert.*;
@@ -84,7 +86,7 @@
      * Test that the number of UDP sockets is limited when running with a
      * security manager.
      */
-    public void testMaxSockets() throws IOException {
+    public void testMaxSockets() throws Exception {
         int limit = Integer.getInteger("sun.net.maxDatagramSockets");
 
         // security manager grants all permissions
@@ -93,14 +95,14 @@
         };
 
         System.setSecurityManager(securityManager);
-        List<Socket> sockets = new ArrayList<>();
+        Deque<Socket> sockets = new ArrayDeque<>();
         try {
             // create the maximum number of sockets
             for (int i=0; i<limit; i++) {
-                sockets.add(newUdpSocket());
+                sockets.offer(newUdpSocket());
             }
 
-            // try to create another socket
+            // try to create another socket - should fail
             try {
                 Socket s = newUdpSocket();
                 s.close();
@@ -108,10 +110,21 @@
             } catch (IOException expected) { }
 
             // close one socket
-            sockets.get(0).close();
+            sockets.pop().close();
+
+            // try to create another socket - should succeed
+            Socket s = newUdpSocket();
 
-            // create another socket
-            Socket s = newUdpSocket();
+            // unreference the socket and wait for it to be closed by the cleaner
+            var ref = new WeakReference<>(s);
+            s = null;
+            while (ref.get() != null) {
+                System.gc();
+                Thread.sleep(100);
+            }
+
+            // try to create another socket - should succeed
+            s = newUdpSocket();
             s.close();
         } finally {
             closeAll(sockets);
@@ -123,9 +136,9 @@
         return new Socket(InetAddress.getLoopbackAddress(), 8000, false);
     }
 
-    private void closeAll(List<Socket> sockets) throws IOException {
-        for (Socket s : sockets) {
-            s.close();
-        }
+    private static void closeAll(Deque<Socket> sockets) throws IOException {
+        sockets.forEach(s -> {
+            try { s.close(); } catch (IOException ignore) { }
+        });
     }
 }