test/jdk/java/net/ServerSocket/UnreferencedSockets.java
branchniosocketimpl-branch
changeset 57358 f0a1d9760c5e
parent 57340 9026cb618cdd
parent 54841 43439afaab4a
child 58679 9c3209ff7550
equal deleted inserted replaced
57355:ceb5c3fd71d2 57358:f0a1d9760c5e
    21  * questions.
    21  * questions.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
       
    26  * @library /test/lib
    26  * @modules java.management java.base/java.io:+open java.base/java.net:+open
    27  * @modules java.management java.base/java.io:+open java.base/java.net:+open
    27  * @run main/othervm UnreferencedSockets
    28  * @run main/othervm UnreferencedSockets
    28  * @run main/othervm -Djava.net.preferIPv4Stack=true UnreferencedSockets
    29  * @run main/othervm -Djava.net.preferIPv4Stack=true UnreferencedSockets
    29  * @run main/othervm -Djdk.net.usePlainSocketImpl UnreferencedSockets
    30  * @run main/othervm -Djdk.net.usePlainSocketImpl UnreferencedSockets
    30  * @summary Check that unreferenced sockets are closed
    31  * @summary Check that unreferenced sockets are closed
    37 import java.lang.management.OperatingSystemMXBean;
    38 import java.lang.management.OperatingSystemMXBean;
    38 import java.lang.ref.ReferenceQueue;
    39 import java.lang.ref.ReferenceQueue;
    39 import java.lang.ref.WeakReference;
    40 import java.lang.ref.WeakReference;
    40 import java.lang.reflect.Field;
    41 import java.lang.reflect.Field;
    41 import java.io.IOException;
    42 import java.io.IOException;
       
    43 import java.net.InetAddress;
    42 import java.net.ServerSocket;
    44 import java.net.ServerSocket;
    43 import java.net.Socket;
    45 import java.net.Socket;
    44 import java.net.SocketImpl;
    46 import java.net.SocketImpl;
    45 import java.nio.file.Files;
    47 import java.nio.file.Files;
    46 import java.nio.file.Path;
    48 import java.nio.file.Path;
    50 import java.util.Optional;
    52 import java.util.Optional;
    51 import java.util.concurrent.TimeUnit;
    53 import java.util.concurrent.TimeUnit;
    52 
    54 
    53 import com.sun.management.UnixOperatingSystemMXBean;
    55 import com.sun.management.UnixOperatingSystemMXBean;
    54 
    56 
       
    57 import jdk.test.lib.net.IPSupport;
       
    58 
    55 public class UnreferencedSockets {
    59 public class UnreferencedSockets {
    56 
    60 
    57     /**
    61     /**
    58      * The set of sockets we have to check up on.
    62      * The set of sockets we have to check up on.
    59      */
    63      */
    67     // Server to echo a stream
    71     // Server to echo a stream
    68     static class Server implements Runnable {
    72     static class Server implements Runnable {
    69 
    73 
    70         ServerSocket ss;
    74         ServerSocket ss;
    71 
    75 
    72         Server() throws IOException {
    76         Server(InetAddress address) throws IOException {
    73             ss = new ServerSocket(0);
    77             ss = new ServerSocket(0, 0, address);
    74             pendingSockets.add(new NamedWeak(ss, pendingQueue, "serverSocket"));
    78             pendingSockets.add(new NamedWeak(ss, pendingQueue, "serverSocket"));
    75             extractRefs(ss, "serverSocket");
    79             extractRefs(ss, "serverSocket");
    76         }
    80         }
    77 
    81 
    78         public int localPort() {
    82         public int localPort() {
    79             return ss.getLocalPort();
    83             return ss.getLocalPort();
    80         }
    84         }
    81 
       
    82 
    85 
    83         public void run() {
    86         public void run() {
    84             try {
    87             try {
    85                 Socket s = ss.accept();
    88                 Socket s = ss.accept();
    86                 pendingSockets.add(new NamedWeak(s, pendingQueue, "acceptedSocket"));
    89                 pendingSockets.add(new NamedWeak(s, pendingQueue, "acceptedSocket"));
   106             }
   109             }
   107         }
   110         }
   108     }
   111     }
   109 
   112 
   110     public static void main(String args[]) throws Exception {
   113     public static void main(String args[]) throws Exception {
   111 
   114         IPSupport.throwSkippedExceptionIfNonOperational();
       
   115         InetAddress lba = InetAddress.getLoopbackAddress();
   112         // Create and close a ServerSocket to warm up the FD count for side effects.
   116         // Create and close a ServerSocket to warm up the FD count for side effects.
   113         try (ServerSocket s = new ServerSocket(0)) {
   117         try (ServerSocket s = new ServerSocket(0, 0, lba)) {
   114             // no-op; close immediately
   118             // no-op; close immediately
   115             s.getLocalPort();   // no-op
   119             s.getLocalPort();   // no-op
   116         }
   120         }
   117 
   121 
   118         long fdCount0 = getFdCount();
   122         long fdCount0 = getFdCount();
   119         listProcFD();
   123         listProcFD();
   120 
   124 
   121         // start a server
   125         // start a server
   122         Server svr = new Server();
   126         Server svr = new Server(lba);
   123         Thread thr = new Thread(svr);
   127         Thread thr = new Thread(svr);
   124         thr.start();
   128         thr.start();
   125 
   129 
   126         Socket s = new Socket("localhost", svr.localPort());
   130         Socket s = new Socket(lba, svr.localPort());
   127         pendingSockets.add(new NamedWeak(s, pendingQueue, "clientSocket"));
   131         pendingSockets.add(new NamedWeak(s, pendingQueue, "clientSocket"));
   128         extractRefs(s, "clientSocket");
   132         extractRefs(s, "clientSocket");
   129 
   133 
   130         OutputStream out = s.getOutputStream();
   134         OutputStream out = s.getOutputStream();
   131         out.write('x');
   135         out.write('x');