jdk/test/java/net/Socket/asyncClose/Socket_getInputStream_read.java
changeset 23604 5c29c65ae465
parent 5506 202f599c92aa
equal deleted inserted replaced
23603:c561991073ad 23604:5c29c65ae465
    25  * Tests that a thread blocked in Socket.getInputStream().read()
    25  * Tests that a thread blocked in Socket.getInputStream().read()
    26  * throws a SocketException if the socket is asynchronously closed.
    26  * throws a SocketException if the socket is asynchronously closed.
    27  */
    27  */
    28 import java.net.*;
    28 import java.net.*;
    29 import java.io.*;
    29 import java.io.*;
       
    30 import java.util.concurrent.CountDownLatch;
    30 
    31 
    31 public class Socket_getInputStream_read extends AsyncCloseTest implements Runnable {
    32 public class Socket_getInputStream_read extends AsyncCloseTest implements Runnable {
    32     Socket s;
    33     private final Socket s;
    33     int timeout = 0;
    34     private final int timeout;
       
    35     private final CountDownLatch latch;
    34 
    36 
    35     public Socket_getInputStream_read() {
    37     public Socket_getInputStream_read() {
       
    38         this(0);
    36     }
    39     }
    37 
    40 
    38     public Socket_getInputStream_read(int timeout) {
    41     public Socket_getInputStream_read(int timeout) {
    39         this.timeout = timeout;
    42         this.timeout = timeout;
       
    43         latch = new CountDownLatch(1);
       
    44         s = new Socket();
    40     }
    45     }
    41 
    46 
    42     public String description() {
    47     public String description() {
    43         String s = "Socket.getInputStream().read()";
    48         String s = "Socket.getInputStream().read()";
    44         if (timeout > 0) {
    49         if (timeout > 0) {
    46         }
    51         }
    47         return s;
    52         return s;
    48     }
    53     }
    49 
    54 
    50     public void run() {
    55     public void run() {
    51         InputStream in;
       
    52 
       
    53         try {
    56         try {
    54             in = s.getInputStream();
    57             InputStream in = s.getInputStream();
    55             if (timeout > 0) {
    58             if (timeout > 0) {
    56                 s.setSoTimeout(timeout);
    59                 s.setSoTimeout(timeout);
    57             }
    60             }
       
    61             latch.countDown();
       
    62             int n = in.read();
       
    63             failed("Socket.getInputStream().read() returned unexpectly!!");
       
    64         } catch (SocketException se) {
       
    65             if (latch.getCount() != 1) {
       
    66                 closed();
       
    67             }
    58         } catch (Exception e) {
    68         } catch (Exception e) {
    59             failed(e.getMessage());
    69             failed(e.getMessage());
    60             return;
    70         } finally {
    61         }
    71             if (latch.getCount() == 1) {
    62 
    72                 latch.countDown();
    63         try {
    73             }
    64             int n = in.read();
       
    65             failed("getInptuStream().read() returned unexpectly!!");
       
    66         } catch (SocketException se) {
       
    67             closed();
       
    68         } catch (Exception e) {
       
    69             failed(e.getMessage());
       
    70         }
    74         }
    71     }
    75     }
    72 
    76 
    73     public boolean go() throws Exception {
    77     public AsyncCloseTest go() {
       
    78         try {
       
    79             ServerSocket ss = new ServerSocket(0);
       
    80             InetAddress lh = InetAddress.getLocalHost();
       
    81             s.connect( new InetSocketAddress(lh, ss.getLocalPort()) );
       
    82             Socket s2 = ss.accept();
       
    83             Thread thr = new Thread(this);
       
    84             thr.start();
       
    85             latch.await();
       
    86             Thread.sleep(5000); //sleep, so Socket.getInputStream().read() can block
       
    87             s.close();
       
    88             thr.join();
    74 
    89 
    75         ServerSocket ss = new ServerSocket(0);
    90             if (isClosed()) {
    76 
    91                 return passed();
    77         InetAddress lh = InetAddress.getLocalHost();
    92             } else {
    78         s = new Socket();
    93                 return failed("Socket.getInputStream().read() wasn't preempted");
    79         s.connect( new InetSocketAddress(lh, ss.getLocalPort()) );
    94             }
    80 
    95         } catch (Exception x) {
    81         Socket s2 = ss.accept();
    96             failed(x.getMessage());
    82 
    97             throw new RuntimeException(x);
    83         Thread thr = new Thread(this);
       
    84         thr.start();
       
    85 
       
    86         Thread.currentThread().sleep(1000);
       
    87 
       
    88         s.close();
       
    89 
       
    90         Thread.currentThread().sleep(1000);
       
    91 
       
    92         if (isClosed()) {
       
    93             return true;
       
    94         } else {
       
    95             failed("getInputStream().read() wasn't preempted");
       
    96             return false;
       
    97         }
    98         }
    98 
       
    99     }
    99     }
   100 }
   100 }