Add -Djdk.net.usePlainSocketImpl to important tests niosocketimpl-branch
authoralanb
Sat, 06 Apr 2019 16:36:24 +0100
branchniosocketimpl-branch
changeset 57308 f22d38b23756
parent 57305 b2c5d70eecf6
child 57309 dbd1beb994ab
Add -Djdk.net.usePlainSocketImpl to important tests
test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java
test/jdk/java/net/ServerSocket/UnreferencedSockets.java
test/jdk/java/net/Socket/ConnectionReset.java
test/jdk/java/net/Socket/Timeouts.java
test/jdk/java/net/Socket/UdpSocket.java
test/jdk/java/net/Socket/asyncClose/AsyncClose.java
test/jdk/java/net/ipv6tests/TcpTest.java
--- a/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java	Fri Apr 05 12:07:38 2019 +0100
+++ b/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java	Sat Apr 06 16:36:24 2019 +0100
@@ -38,6 +38,7 @@
  *        jdk.test.lib.process.*
  *        AcceptCauseFileDescriptorLeak
  * @run main/othervm AcceptCauseFileDescriptorLeak root
+ * @run main/othervm -Djdk.net.usePlainSocketImpl=true AcceptCauseFileDescriptorLeak root
  */
 
 import java.io.IOException;
--- a/test/jdk/java/net/ServerSocket/UnreferencedSockets.java	Fri Apr 05 12:07:38 2019 +0100
+++ b/test/jdk/java/net/ServerSocket/UnreferencedSockets.java	Sat Apr 06 16:36:24 2019 +0100
@@ -26,6 +26,7 @@
  * @modules java.management java.base/java.io:+open java.base/java.net:+open
  * @run main/othervm UnreferencedSockets
  * @run main/othervm -Djava.net.preferIPv4Stack=true UnreferencedSockets
+ * @run main/othervm -Djdk.net.usePlainSocketImpl UnreferencedSockets
  * @summary Check that unreferenced sockets are closed
  */
 
--- a/test/jdk/java/net/Socket/ConnectionReset.java	Fri Apr 05 12:07:38 2019 +0100
+++ b/test/jdk/java/net/Socket/ConnectionReset.java	Sat Apr 06 16:36:24 2019 +0100
@@ -25,6 +25,7 @@
  * @test
  * @requires os.family != "solaris"
  * @run testng ConnectionReset
+ * @run testng/othervm -Djdk.net.usePlainSocketImpl ConnectionReset
  * @summary Test behavior of read and available when a connection is reset
  */
 
--- a/test/jdk/java/net/Socket/Timeouts.java	Fri Apr 05 12:07:38 2019 +0100
+++ b/test/jdk/java/net/Socket/Timeouts.java	Sat Apr 06 16:36:24 2019 +0100
@@ -25,7 +25,7 @@
  * @test
  * @library /test/lib
  * @build jdk.test.lib.Utils
- * @run testng Timeouts
+ * @run testng/timeout=180 Timeouts
  * @summary Test Socket timeouts
  */
 
@@ -131,10 +131,14 @@
     public void testTimedRead3() throws IOException {
         withConnection((s1, s2) -> {
             s2.setSoTimeout(2000);
+            long start = System.currentTimeMillis();
             try {
                 s2.getInputStream().read();
                 assertTrue(false);
-            } catch (SocketTimeoutException expected) { }
+            } catch (SocketTimeoutException expected) {
+                int timeout = s2.getSoTimeout();
+                checkDuration(start, timeout-100, timeout+2000);
+            }
         });
     }
 
@@ -312,11 +316,15 @@
     public void testTimedAccept3() throws IOException {
         try (ServerSocket ss = new ServerSocket(0)) {
             ss.setSoTimeout(2000);
+            long start = System.currentTimeMillis();
             try {
                 Socket s = ss.accept();
                 s.close();
                 assertTrue(false);
-            } catch (SocketTimeoutException expected) { }
+            } catch (SocketTimeoutException expected) {
+                int timeout = ss.getSoTimeout();
+                checkDuration(start, timeout-100, timeout+2000);
+            }
         }
     }
 
@@ -386,11 +394,15 @@
     public void testTimedAccept7() throws IOException {
         try (ServerSocket ss = new ServerSocket(0)) {
             ss.setSoTimeout(30*1000);
-            scheduleClose(ss, 2000);
+            long delay = 2000;
+            scheduleClose(ss, delay);
+            long start = System.currentTimeMillis();
             try {
                 ss.accept().close();
                 assertTrue(false);
-            } catch (SocketException expected) { }
+            } catch (SocketException expected) {
+                checkDuration(start, delay-100, delay+2000);
+            }
         }
     }
 
@@ -407,8 +419,9 @@
                 s.close();
                 assertTrue(false);
             } catch (SocketTimeoutException expected) {
-                // accept should have blocked for 2000ms
-                assertTrue((System.currentTimeMillis() - start) > 1800);
+                // accept should have blocked for 2 seconds
+                int timeout = ss.getSoTimeout();
+                checkDuration(start, timeout-100, timeout+2000);
                 assertTrue(Thread.currentThread().isInterrupted());
             } finally {
                 Thread.interrupted(); // clear interrupt status
@@ -422,16 +435,17 @@
     public void testTimedAccept9() throws IOException {
         try (ServerSocket ss = new ServerSocket(0)) {
             ss.setSoTimeout(4000);
-            // interrupt thread after 1000ms
+            // interrupt thread after 1 second
             Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 1000);
             long start = System.currentTimeMillis();
             try {
-                Socket s = ss.accept();   // should block for 4000
+                Socket s = ss.accept();   // should block for 4 seconds
                 s.close();
                 assertTrue(false);
             } catch (SocketTimeoutException expected) {
-                // accept should have blocked for 4000ms
-                assertTrue((System.currentTimeMillis() - start) > 3800);
+                // accept should have blocked for 4 seconds
+                int timeout = ss.getSoTimeout();
+                checkDuration(start, timeout-100, timeout+2000);
                 assertTrue(Thread.currentThread().isInterrupted());
             } finally {
                 interrupter.cancel(true);
@@ -459,8 +473,9 @@
             e = expectThrows(ExecutionException.class, result2::get);
             assertTrue(e.getCause() instanceof SocketTimeoutException);
 
-            // both tasks should completed in a little over 4000ms
-            assertTrue((System.currentTimeMillis() - start) < 5000);
+            // should get here in 4 seconds, not 8 seconds
+            int timeout = ss.getSoTimeout();
+            checkDuration(start, timeout-100, timeout+2000);
         } finally {
             pool.shutdown();
         }
@@ -479,7 +494,8 @@
             Future<Socket> result1 = pool.submit(ss::accept);
             Future<Socket> result2 = pool.submit(ss::accept);
 
-            scheduleConnect(ss.getLocalSocketAddress(), 1500);
+            // establish connection after 3 seconds
+            scheduleConnect(ss.getLocalSocketAddress(), 3000);
 
             // one task should have accepted the connection, the other should
             // have completed with SocketTimeoutException
@@ -499,9 +515,9 @@
             }
             assertTrue((s1 != null) ^ (s2 != null));
 
-            // both tasks should completed in a little over 4000ms
-            long duration = System.currentTimeMillis() - start;
-            assertTrue(duration > 3800 && duration < 5000);
+            // should get here in 4 seconds, not 7 seconds
+            int timeout = ss.getSoTimeout();
+            checkDuration(start, timeout-100, timeout+2000);
         } finally {
             pool.shutdown();
         }
@@ -613,4 +629,17 @@
             executor.shutdown();
         }
     }
+
+    /**
+     * Check the duration of a task
+     * @param start start time, in milliseconds
+     * @param min minimum expected duration, in milliseconds
+     * @param max maximum expected duration, in milliseconds
+     * @return the duration (now - start), in milliseconds
+     */
+    private static long checkDuration(long start, long min, long max) {
+        long duration = System.currentTimeMillis() - start;
+        assertTrue(duration >= min && duration <= max);
+        return duration;
+    }
 }
--- a/test/jdk/java/net/Socket/UdpSocket.java	Fri Apr 05 12:07:38 2019 +0100
+++ b/test/jdk/java/net/Socket/UdpSocket.java	Sat Apr 06 16:36:24 2019 +0100
@@ -23,7 +23,7 @@
 
 /**
  * @test
- * @run main UdpSocket
+ * @run testng/othervm -Dsun.net.maxDatagramSockets=32 UdpSocket
  * @summary Basic test for a Socket to a UDP socket
  */
 
@@ -34,13 +34,23 @@
 import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.DatagramChannel;
+import java.security.Permission;
 import java.util.Arrays;
+import java.util.ArrayList;
+import java.util.List;
 
+import org.testng.annotations.Test;
+import static org.testng.Assert.*;
+
+@Test
 public class UdpSocket {
 
-    static final String MESSAGE = "hello";
+    /**
+     * Test using the Socket API to send/receive datagrams
+     */
+    public void testSendReceive() throws IOException {
+        final String MESSAGE = "hello";
 
-    public static void main(String[] args) throws IOException {
         try (DatagramChannel dc = DatagramChannel.open()) {
             var loopback = InetAddress.getLoopbackAddress();
             dc.bind(new InetSocketAddress(loopback, 0));
@@ -56,8 +66,7 @@
                 var buf = ByteBuffer.allocate(100);
                 SocketAddress remote = dc.receive(buf);
                 buf.flip();
-                if (buf.remaining() != MESSAGE.length())
-                    throw new RuntimeException("Unexpected size");
+                assertTrue(buf.remaining() == MESSAGE.length(), "Unexpected size");
 
                 // echo the datagram
                 dc.send(buf, remote);
@@ -65,11 +74,58 @@
                 // receive datagram with the socket input stream
                 byte[] array2 = new byte[100];
                 int n = s.getInputStream().read(array2);
-                if (n != MESSAGE.length())
-                    throw new RuntimeException("Unexpected size");
-                if (!Arrays.equals(array1, 0, n, array2, 0, n))
-                    throw new RuntimeException("Unexpected contents");
+                assertTrue(n == MESSAGE.length(), "Unexpected size");
+                assertTrue(Arrays.equals(array1, 0, n, array2, 0, n), "Unexpected contents");
             }
         }
     }
+
+    /**
+     * Test that the number of UDP sockets is limited when running with a
+     * security manager.
+     */
+    public void testMaxSockets() throws IOException {
+        int limit = Integer.getInteger("sun.net.maxDatagramSockets");
+
+        // security manager grants all permissions
+        var securityManager = new SecurityManager() {
+            @Override public void checkPermission(Permission perm) { }
+        };
+
+        System.setSecurityManager(securityManager);
+        List<Socket> sockets = new ArrayList<>();
+        try {
+            // create the maximum number of sockets
+            for (int i=0; i<limit; i++) {
+                sockets.add(newUdpSocket());
+            }
+
+            // try to create another socket
+            try {
+                Socket s = newUdpSocket();
+                s.close();
+                assertTrue(false);
+            } catch (IOException expected) { }
+
+            // close one socket
+            sockets.get(0).close();
+
+            // create another socket
+            Socket s = newUdpSocket();
+            s.close();
+        } finally {
+            closeAll(sockets);
+            System.setSecurityManager(null);
+        }
+    }
+
+    private Socket newUdpSocket() throws IOException {
+        return new Socket(InetAddress.getLoopbackAddress(), 8000, false);
+    }
+
+    private void closeAll(List<Socket> sockets) throws IOException {
+        for (Socket s : sockets) {
+            s.close();
+        }
+    }
 }
--- a/test/jdk/java/net/Socket/asyncClose/AsyncClose.java	Fri Apr 05 12:07:38 2019 +0100
+++ b/test/jdk/java/net/Socket/asyncClose/AsyncClose.java	Sat Apr 06 16:36:24 2019 +0100
@@ -33,6 +33,7 @@
  *          cause any thread blocked on the socket to throw a SocketException.
  * @run main AsyncClose
  * @run main/othervm -Djava.net.preferIPv4Stack=true AsyncClose
+ * @run main/othervm -Djdk.net.usePlainSocketImpl AsyncClose
  */
 
 public class AsyncClose {
--- a/test/jdk/java/net/ipv6tests/TcpTest.java	Fri Apr 05 12:07:38 2019 +0100
+++ b/test/jdk/java/net/ipv6tests/TcpTest.java	Sat Apr 06 16:36:24 2019 +0100
@@ -29,7 +29,8 @@
  * @library /test/lib
  * @build jdk.test.lib.NetworkConfiguration
  *        jdk.test.lib.Platform
- * @run main TcpTest -d
+ * @run main TcpTest
+ * @run main/othervm -Djdk.net.usePlainSocketImpl TcpTest
  */
 
 import java.net.*;