test/java/net/Socket/FDClose.java

Print this page




   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4152799

  27  * @summary  test to see if interrupting a socket accept closes fd0
  28  */






  29 import java.net.*;
  30 import java.io.*;
  31 import java.util.*;
  32 
  33 public class FDClose {
  34 
  35     static boolean isServerReady = false;
  36 
  37     public static void main(String[] args) throws Exception {
  38 
  39         Thread me = Thread.currentThread();
  40 
  41         // Put a thread waiting on SocketServer.Accept
  42         AReader test = new AReader();
  43         Thread readerThread = new Thread(test);
  44         readerThread.start();
  45 
  46         // wait for the server socket to be ready
  47         while (!isServerReady) {
  48             me.sleep(100);
  49         }
  50 
  51         // Interrupt the waiting thread
  52         readerThread.interrupt();
  53 
  54         // Wait another moment
  55         me.sleep(100);
  56 

  57         // Check to see if fd0 is closed
  58         System.in.available();


  59     }

  60 
  61     public static class AReader implements Runnable {


  62         public void run() {
  63             try {
  64                 ServerSocket sock = new ServerSocket(0);
  65                 isServerReady = true;
  66                 sock.accept();
  67             } catch (Exception e) {
  68             }
  69         }





  70     }

  71 }


   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  20  * CA 95054 USA or visit www.sun.com if you need additional information or
  21  * have any questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4152799
  27  * @run main/othervm -XX:+UseVMInterruptibleIO FDClose
  28  * @summary  test to see if interrupting a socket accept closes fd0
  29  */
  30 
  31 /* This test requires interruptible I/O to verify that the accept
  32  * implementation does not impact on fd0. UseVMInterruptibleIO needs to
  33  * be explicitly enabled since it is now disabled by default, see 6554406
  34  */
  35 
  36 import java.net.*;
  37 import java.io.*;
  38 import java.util.*;
  39 
  40 public class FDClose {
  41 
  42     static volatile boolean isServerReady = false;
  43 
  44     public static void main(String[] args) throws Exception {
  45 
  46         Thread me = Thread.currentThread();
  47 
  48         // Put a thread waiting on SocketServer.Accept
  49         AReader test = new AReader();
  50         Thread readerThread = new Thread(test);
  51         readerThread.start();
  52 
  53         // wait for the server socket to be ready
  54         while (!isServerReady) {
  55             me.sleep(100);
  56         }
  57 
  58         // Interrupt the waiting thread
  59         readerThread.interrupt();
  60 
  61         // Wait another moment
  62         me.sleep(100);
  63 
  64         try {
  65             // Check to see if fd0 is closed
  66             System.in.available();
  67         } finally {
  68            test.terminate();
  69         }
  70     }
  71 
  72     public static class AReader implements Runnable {
  73         volatile ServerSocket sock;
  74 
  75         public void run() {
  76             try {
  77                 sock = new ServerSocket(0);
  78                 isServerReady = true;
  79                 sock.accept();
  80             } catch (Exception e) {
  81             }
  82         }
  83 
  84         void terminate() {
  85             try {
  86                 if (sock != null) sock.close();
  87             } catch (IOException e) {}
  88         }
  89     }
  90 }