test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java
changeset 50727 081b132c4dc0
parent 48534 12d9ff9e0a4b
equal deleted inserted replaced
50726:d7cf64ce3950 50727:081b132c4dc0
    22  */
    22  */
    23 
    23 
    24 import java.io.File;
    24 import java.io.File;
    25 import java.io.FileDescriptor;
    25 import java.io.FileDescriptor;
    26 import java.io.FileNotFoundException;
    26 import java.io.FileNotFoundException;
       
    27 import java.io.IOException;
    27 import java.io.RandomAccessFile;
    28 import java.io.RandomAccessFile;
    28 import java.lang.management.ManagementFactory;
    29 import java.lang.management.ManagementFactory;
    29 import java.lang.management.OperatingSystemMXBean;
    30 import java.lang.management.OperatingSystemMXBean;
    30 import java.lang.ref.Cleaner;
    31 import java.lang.ref.Cleaner;
    31 import java.lang.ref.Reference;
    32 import java.lang.ref.Reference;
    32 import java.lang.ref.ReferenceQueue;
    33 import java.lang.ref.ReferenceQueue;
    33 import java.lang.ref.WeakReference;
    34 import java.lang.ref.WeakReference;
    34 import java.lang.reflect.Field;
    35 import java.lang.reflect.Field;
       
    36 import java.nio.file.Path;
       
    37 import java.util.ArrayDeque;
    35 import java.util.HashSet;
    38 import java.util.HashSet;
    36 
    39 
    37 import com.sun.management.UnixOperatingSystemMXBean;
    40 import com.sun.management.UnixOperatingSystemMXBean;
    38 
    41 
       
    42 import jdk.test.lib.util.FileUtils;
       
    43 
    39 /**
    44 /**
    40  * @test
    45  * @test
    41  * @bug 8080225
    46  * @bug 8080225
       
    47  * @library /test/lib
       
    48  * @build jdk.test.lib.util.FileUtils UnreferencedRAFClosesFd
    42  * @modules java.base/java.io:open
    49  * @modules java.base/java.io:open
    43  * @summary Test to ensure that an unclosed and unreferenced RandomAccessFile closes the fd
    50  * @summary Test to ensure that an unclosed and unreferenced RandomAccessFile closes the fd
    44  * @run main/othervm UnreferencedRAFClosesFd
    51  * @run main/othervm UnreferencedRAFClosesFd
    45  */
    52  */
    46 public class UnreferencedRAFClosesFd {
    53 public class UnreferencedRAFClosesFd {
    52 
    59 
    53         File inFile= new File(System.getProperty("test.dir", "."), FILE_NAME);
    60         File inFile= new File(System.getProperty("test.dir", "."), FILE_NAME);
    54         inFile.createNewFile();
    61         inFile.createNewFile();
    55         inFile.deleteOnExit();
    62         inFile.deleteOnExit();
    56 
    63 
       
    64         FileUtils.listFileDescriptors(System.out);
    57         long fdCount0 = getFdCount();
    65         long fdCount0 = getFdCount();
    58         System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
       
    59 
    66 
    60         String name = inFile.getPath();
    67         String name = inFile.getPath();
    61         RandomAccessFile raf;
    68         RandomAccessFile raf;
    62         try {
    69         try {
    63             // raf is explicitly *not* closed to allow cleaner to work
    70             // raf is explicitly *not* closed to allow cleaner to work
    99         Reference.reachabilityFence(raf);
   106         Reference.reachabilityFence(raf);
   100         Reference.reachabilityFence(pending);
   107         Reference.reachabilityFence(pending);
   101 
   108 
   102         // Check the final count of open file descriptors
   109         // Check the final count of open file descriptors
   103         long fdCount = getFdCount();
   110         long fdCount = getFdCount();
   104         System.out.printf("final count of open file descriptors: %d%n", fdCount);
       
   105         if (fdCount != fdCount0) {
   111         if (fdCount != fdCount0) {
   106             throw new AssertionError("raw fd count wrong: expected: " + fdCount0
   112             System.out.printf("initial count of open file descriptors: %d%n", fdCount0);
   107                     + ", actual: " + fdCount);
   113             System.out.printf("final count of open file descriptors: %d%n", fdCount);
       
   114             FileUtils.listFileDescriptors(System.out);
   108         }
   115         }
   109     }
   116     }
   110 
   117 
   111 
   118 
   112     // Get the count of open file descriptors, or -1 if not available
   119     // Get the count of open file descriptors, or -1 if not available