test/jdk/java/net/Socket/AsyncShutdown.java
changeset 55649 ad8e3b295615
parent 54216 f10ca228b22f
child 58679 9c3209ff7550
equal deleted inserted replaced
55648:ba72dac556c3 55649:ad8e3b295615
    27  * @run testng AsyncShutdown
    27  * @run testng AsyncShutdown
    28  * @summary Test shutdownInput/shutdownOutput with threads blocked in read/write
    28  * @summary Test shutdownInput/shutdownOutput with threads blocked in read/write
    29  */
    29  */
    30 
    30 
    31 import java.io.IOException;
    31 import java.io.IOException;
       
    32 import java.net.InetAddress;
       
    33 import java.net.InetSocketAddress;
    32 import java.net.ServerSocket;
    34 import java.net.ServerSocket;
    33 import java.net.Socket;
    35 import java.net.Socket;
    34 import java.net.SocketTimeoutException;
    36 import java.net.SocketTimeoutException;
    35 import java.util.concurrent.Executors;
    37 import java.util.concurrent.Executors;
    36 import java.util.concurrent.ScheduledExecutorService;
    38 import java.util.concurrent.ScheduledExecutorService;
   121     static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
   123     static void withConnection(ThrowingBiConsumer<Socket, Socket> consumer)
   122         throws IOException
   124         throws IOException
   123     {
   125     {
   124         Socket s1 = null;
   126         Socket s1 = null;
   125         Socket s2 = null;
   127         Socket s2 = null;
   126         try (ServerSocket ss = new ServerSocket(0)) {
   128         try (ServerSocket ss = createBoundServer()) {
   127             s1 = new Socket();
   129             s1 = new Socket();
   128             s1.connect(ss.getLocalSocketAddress());
   130             s1.connect(ss.getLocalSocketAddress());
   129             s2 = ss.accept();
   131             s2 = ss.accept();
   130             consumer.accept(s1, s2);
   132             consumer.accept(s1, s2);
   131         } finally {
   133         } finally {
   132             if (s1 != null) s1.close();
   134             if (s1 != null) s1.close();
   133             if (s2 != null) s2.close();
   135             if (s2 != null) s2.close();
   134         }
   136         }
   135     }
   137     }
   136 
   138 
       
   139     static ServerSocket createBoundServer() throws IOException {
       
   140         ServerSocket ss = new ServerSocket();
       
   141         InetAddress loopback = InetAddress.getLoopbackAddress();
       
   142         InetSocketAddress address = new InetSocketAddress(loopback, 0);
       
   143         ss.bind(address);
       
   144         return ss;
       
   145     }
       
   146 
   137 }
   147 }