test/java/net/Socket/FDClose.java

Print this page

        

@@ -22,19 +22,26 @@
  */
 
 /**
  * @test
  * @bug 4152799
+ * @run main/othervm -XX:+UseVMInterruptibleIO FDClose
  * @summary  test to see if interrupting a socket accept closes fd0
  */
+
+/* This test requires interruptible I/O to verify that the accept
+ * implementation does not impact on fd0. UseVMInterruptibleIO needs to
+ * be explicitly enabled since it is now disabled by default, see 6554406
+ */
+
 import java.net.*;
 import java.io.*;
 import java.util.*;
 
 public class FDClose {
 
-    static boolean isServerReady = false;
+    static volatile boolean isServerReady = false;
 
     public static void main(String[] args) throws Exception {
 
         Thread me = Thread.currentThread();
 

@@ -52,20 +59,32 @@
         readerThread.interrupt();
 
         // Wait another moment
         me.sleep(100);
 
+        try {
         // Check to see if fd0 is closed
         System.in.available();
+        } finally {
+           test.terminate();
     }
+    }
 
     public static class AReader implements Runnable {
+        volatile ServerSocket sock;
+
         public void run() {
             try {
-                ServerSocket sock = new ServerSocket(0);
+                sock = new ServerSocket(0);
                 isServerReady = true;
                 sock.accept();
             } catch (Exception e) {
             }
         }
+
+        void terminate() {
+            try {
+                if (sock != null) sock.close();
+            } catch (IOException e) {}
     }
+    }
 }