More cleanup and test improvements niosocketimpl-branch
authoralanb
Mon, 29 Apr 2019 09:03:06 +0100
branchniosocketimpl-branch
changeset 57341 733e9746d615
parent 57340 9026cb618cdd
child 57342 5b512573ccb8
More cleanup and test improvements
src/java.base/share/classes/java/net/ServerSocket.java
src/java.base/share/classes/java/net/Socket.java
test/jdk/ProblemList.txt
test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java
test/jdk/java/net/Socket/Timeouts.java
test/jdk/java/net/Socket/UdpSocket.java
test/jdk/java/net/SocketImpl/BadUsages.java
test/jdk/sun/security/ssl/SSLSocketImpl/NewSocketMethods.java
--- a/src/java.base/share/classes/java/net/ServerSocket.java	Fri Apr 26 11:31:38 2019 +0100
+++ b/src/java.base/share/classes/java/net/ServerSocket.java	Mon Apr 29 09:03:06 2019 +0100
@@ -689,15 +689,11 @@
     public void close() throws IOException {
         closeLock.lock();
         try {
-            if (!closed) {
-                try {
-                    SocketImpl impl = this.impl;
-                    if (impl != null)
-                        impl.close();
-                } finally {
-                    closed = true;
-                }
-            }
+            if (closed)
+                return;
+            closed = true;
+            if (created)
+                impl.close();
         } finally {
             closeLock.unlock();
         }
--- a/src/java.base/share/classes/java/net/Socket.java	Fri Apr 26 11:31:38 2019 +0100
+++ b/src/java.base/share/classes/java/net/Socket.java	Mon Apr 29 09:03:06 2019 +0100
@@ -1578,15 +1578,11 @@
     public void close() throws IOException {
         closeLock.lock();
         try {
-            if (!closed) {
-                try {
-                    SocketImpl impl = this.impl;
-                    if (impl != null)
-                        impl.close();
-                } finally {
-                    closed = true;
-                }
-            }
+            if (closed)
+                return;
+            closed = true;
+            if (created)
+                impl.close();
         } finally {
             closeLock.unlock();
         }
--- a/test/jdk/ProblemList.txt	Fri Apr 26 11:31:38 2019 +0100
+++ b/test/jdk/ProblemList.txt	Mon Apr 29 09:03:06 2019 +0100
@@ -728,8 +728,6 @@
 sun/security/pkcs11/tls/TestPremaster.java                      8204203 windows-all
 sun/security/tools/keytool/NssTest.java                         8204203 windows-all
 
-sun/security/ssl/SSLSocketImpl/NewSocketMethods.java            8221487 solaris-all
-
 ############################################################################
 
 # jdk_sound
--- a/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java	Fri Apr 26 11:31:38 2019 +0100
+++ b/test/jdk/java/net/ServerSocket/AcceptCauseFileDescriptorLeak.java	Mon Apr 29 09:03:06 2019 +0100
@@ -38,7 +38,7 @@
  *        jdk.test.lib.process.*
  *        AcceptCauseFileDescriptorLeak
  * @run main/othervm AcceptCauseFileDescriptorLeak root
- * @run main/othervm -Djdk.net.usePlainSocketImpl=true AcceptCauseFileDescriptorLeak root
+ * @run main/othervm -Djdk.net.usePlainSocketImpl AcceptCauseFileDescriptorLeak root
  */
 
 import java.io.IOException;
--- a/test/jdk/java/net/Socket/Timeouts.java	Fri Apr 26 11:31:38 2019 +0100
+++ b/test/jdk/java/net/Socket/Timeouts.java	Mon Apr 29 09:03:06 2019 +0100
@@ -23,6 +23,7 @@
 
 /*
  * @test
+ * @bug 8221481
  * @library /test/lib
  * @build jdk.test.lib.Utils
  * @run testng/timeout=180 Timeouts
@@ -34,6 +35,8 @@
 import java.io.InputStream;
 import java.io.OutputStream;
 import java.net.ConnectException;
+import java.net.InetAddress;
+import java.net.InetSocketAddress;
 import java.net.ServerSocket;
 import java.net.Socket;
 import java.net.SocketAddress;
@@ -57,7 +60,7 @@
      * Test timed connect where connection is established
      */
     public void testTimedConnect1() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             try (Socket s = new Socket()) {
                 s.connect(ss.getLocalSocketAddress(), 2000);
             }
@@ -80,7 +83,7 @@
      * Test connect with a timeout of Integer.MAX_VALUE
      */
     public void testTimedConnect3() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             try (Socket s = new Socket()) {
                 s.connect(ss.getLocalSocketAddress(), Integer.MAX_VALUE);
             }
@@ -91,7 +94,7 @@
      * Test connect with a negative timeout.
      */
     public void testTimedConnect4() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             try (Socket s = new Socket()) {
                 try {
                     s.connect(ss.getLocalSocketAddress(), -1);
@@ -287,7 +290,7 @@
     public void testTimedAccept1() throws IOException {
         Socket s1 = null;
         Socket s2 = null;
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             s1 = new Socket();
             s1.connect(ss.getLocalSocketAddress());
             ss.setSoTimeout(30*1000);
@@ -302,7 +305,7 @@
      * Test timed accept where a connection is established after a short delay
      */
     public void testTimedAccept2() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(30*1000);
             scheduleConnect(ss.getLocalSocketAddress(), 2000);
             Socket s = ss.accept();
@@ -314,7 +317,7 @@
      * Test timed accept where the accept times out
      */
     public void testTimedAccept3() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(2000);
             long startMillis = millisTime();
             try {
@@ -333,7 +336,7 @@
      * previous accept timed out.
      */
     public void testTimedAccept4() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(2000);
             try {
                 Socket s = ss.accept();
@@ -353,7 +356,7 @@
      * accept timed out
      */
     public void testTimedAccept5() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(2000);
             try {
                 Socket s = ss.accept();
@@ -374,7 +377,7 @@
      * accept timed out and after a short delay
      */
     public void testTimedAccept6() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(2000);
             try {
                 Socket s = ss.accept();
@@ -392,7 +395,7 @@
      * Test async close of a timed accept
      */
     public void testTimedAccept7() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(30*1000);
             long delay = 2000;
             scheduleClose(ss, delay);
@@ -410,7 +413,7 @@
      * Test timed accept with the thread interrupt status set.
      */
     public void testTimedAccept8() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(2000);
             Thread.currentThread().interrupt();
             long startMillis = millisTime();
@@ -433,7 +436,7 @@
      * Test interrupt of thread blocked in timed accept.
      */
     public void testTimedAccept9() throws IOException {
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(4000);
             // interrupt thread after 1 second
             Future<?> interrupter = scheduleInterrupt(Thread.currentThread(), 1000);
@@ -459,7 +462,7 @@
      */
     public void testTimedAccept10() throws Exception {
         ExecutorService pool = Executors.newFixedThreadPool(2);
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(4000);
 
             long startMillis = millisTime();
@@ -486,7 +489,7 @@
      */
     public void testTimedAccept11() throws Exception {
         ExecutorService pool = Executors.newFixedThreadPool(2);
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             ss.setSoTimeout(4000);
 
             long startMillis = millisTime();
@@ -543,6 +546,19 @@
         }
     }
 
+    /**
+     * Returns a ServerSocket bound to a port on the loopback address
+     */
+    static ServerSocket boundServerSocket() throws IOException {
+        var loopback = InetAddress.getLoopbackAddress();
+        ServerSocket ss = new ServerSocket();
+        ss.bind(new InetSocketAddress(loopback, 0));
+        return ss;
+    }
+
+    /**
+     * An operation that accepts two arguments and may throw IOException
+     */
     interface ThrowingBiConsumer<T, U> {
         void accept(T t, U u) throws IOException;
     }
@@ -555,7 +571,7 @@
     {
         Socket s1 = null;
         Socket s2 = null;
-        try (ServerSocket ss = new ServerSocket(0)) {
+        try (ServerSocket ss = boundServerSocket()) {
             s1 = new Socket();
             s1.connect(ss.getLocalSocketAddress());
             s2 = ss.accept();
--- a/test/jdk/java/net/Socket/UdpSocket.java	Fri Apr 26 11:31:38 2019 +0100
+++ b/test/jdk/java/net/Socket/UdpSocket.java	Mon Apr 29 09:03:06 2019 +0100
@@ -136,7 +136,7 @@
         return new Socket(InetAddress.getLoopbackAddress(), 8000, false);
     }
 
-    private static void closeAll(Deque<Socket> sockets) throws IOException {
+    private void closeAll(Deque<Socket> sockets) throws IOException {
         Socket s;
         while ((s = sockets.poll()) != null) {
             s.close();
--- a/test/jdk/java/net/SocketImpl/BadUsages.java	Fri Apr 26 11:31:38 2019 +0100
+++ b/test/jdk/java/net/SocketImpl/BadUsages.java	Mon Apr 29 09:03:06 2019 +0100
@@ -118,13 +118,14 @@
      * Test connect when already connected.
      */
     public void testConnect4() throws IOException {
-        try (var ss = new ServerSocket(0);
+        try (var ss = new ServerSocket();
              var impl = new PlatformSocketImpl(false)) {
+            var loopback = InetAddress.getLoopbackAddress();
+            ss.bind(new InetSocketAddress(loopback, 0));
             impl.create(true);
-            String host = ss.getInetAddress().getHostAddress();
             int port = ss.getLocalPort();
-            impl.connect(host, port);
-            expectThrows(IOException.class, () -> impl.connect(host, port));
+            impl.connect(loopback, port);
+            expectThrows(IOException.class, () -> impl.connect(loopback, port));
         }
     }
 
@@ -166,11 +167,12 @@
      * Test bind when connected.
      */
     public void testBind3() throws IOException {
-        try (var ss = new ServerSocket(0);
+        try (var ss = new ServerSocket();
              var impl = new PlatformSocketImpl(false)) {
+            var loopback = InetAddress.getLoopbackAddress();
+            ss.bind(new InetSocketAddress(loopback, 0));
             impl.create(true);
             impl.connect(ss.getLocalSocketAddress(), 0);
-            var loopback = InetAddress.getLoopbackAddress();
             expectThrows(IOException.class, () -> impl.bind(loopback, 0));
         }
     }
--- a/test/jdk/sun/security/ssl/SSLSocketImpl/NewSocketMethods.java	Fri Apr 26 11:31:38 2019 +0100
+++ b/test/jdk/sun/security/ssl/SSLSocketImpl/NewSocketMethods.java	Mon Apr 29 09:03:06 2019 +0100
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2001, 2011, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2001, 2019, 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
@@ -197,14 +197,19 @@
             /**
              * test some new methods of java.net.Socket added to merlin.
              */
-            socket.setTrafficClass(8);
-            socket.setReuseAddress(true);
-            System.out.println("Client getTrafficClass(): "
-                        + socket.getTrafficClass());
             System.out.println("Client isInputShutdown() "
                         + socket.isInputShutdown());
+            socket.setReuseAddress(true);
             System.out.println("Client getReuseAddress(): "
                         + socket.getReuseAddress());
+
+            // Solaris does not support set/get of IPV6_TCLASS when connected
+            if (!"SunOS".equals(System.getProperty("os.name"))) {
+                socket.setTrafficClass(8);
+                System.out.println("Client getTrafficClass(): "
+                        + socket.getTrafficClass());
+            }
+
             os.write(237);
             os.flush();
             System.out.println("Client read: " + is.read());