test/jdk/java/io/RandomAccessFile/UnreferencedRAFClosesFd.java
changeset 48224 be0df5ab3093
child 48534 12d9ff9e0a4b
equal deleted inserted replaced
48223:962b4324320c 48224:be0df5ab3093
       
     1 /*
       
     2  * Copyright (c) 2007, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
       
     4  *
       
     5  * This code is free software; you can redistribute it and/or modify it
       
     6  * under the terms of the GNU General Public License version 2 only, as
       
     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 Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
       
    20  * or visit www.oracle.com if you need additional information or have any
       
    21  * questions.
       
    22  */
       
    23 
       
    24 import java.io.File;
       
    25 import java.io.FileDescriptor;
       
    26 import java.io.FileOutputStream;
       
    27 import java.io.FileNotFoundException;
       
    28 import java.io.RandomAccessFile;
       
    29 import java.lang.ref.Cleaner;
       
    30 import java.lang.ref.Reference;
       
    31 import java.lang.ref.ReferenceQueue;
       
    32 import java.lang.ref.WeakReference;
       
    33 import java.lang.reflect.Field;
       
    34 import java.util.HashSet;
       
    35 
       
    36 /**
       
    37  * @test
       
    38  * @bug 8080225
       
    39  * @modules java.base/java.io:open
       
    40  * @summary Test to ensure that an unclosed and unreferenced RandomAccessFile closes the fd
       
    41  * @run main/othervm UnreferencedRAFClosesFd
       
    42  */
       
    43 public class UnreferencedRAFClosesFd {
       
    44 
       
    45     static final String FILE_NAME = "empty.txt";
       
    46 
       
    47     /* standalone interface */
       
    48     public static void main(String argv[]) throws Exception {
       
    49 
       
    50         File inFile= new File(System.getProperty("test.dir", "."), FILE_NAME);
       
    51         inFile.createNewFile();
       
    52         inFile.deleteOnExit();
       
    53 
       
    54         String name = inFile.getPath();
       
    55         RandomAccessFile raf;
       
    56         try {
       
    57             // raf is explicitly *not* closed to allow cleaner to work
       
    58             raf = new RandomAccessFile(name, "rw");
       
    59         } catch (FileNotFoundException e) {
       
    60             System.out.println("Unexpected exception " + e);
       
    61             throw(e);
       
    62         }
       
    63         FileDescriptor fd = raf.getFD();
       
    64 
       
    65         Field fdField = FileDescriptor.class.getDeclaredField("cleanup");
       
    66         fdField.setAccessible(true);
       
    67         Cleaner.Cleanable cleanup = (Cleaner.Cleanable)fdField.get(fd);
       
    68 
       
    69         // Prepare to wait for FOS, FD, Cleanup to be reclaimed
       
    70         ReferenceQueue<Object> queue = new ReferenceQueue<>();
       
    71         HashSet<Reference<?>> pending = new HashSet<>(3);
       
    72         pending.add(new WeakReference<>(cleanup, queue));
       
    73         pending.add(new WeakReference<>(raf, queue));
       
    74         pending.add(new WeakReference<>(fd, queue));
       
    75 
       
    76         Reference<?> r;
       
    77         while (((r = queue.remove(10L)) != null)
       
    78                 || !pending.isEmpty()) {
       
    79             System.out.printf("r: %s, pending: %d%n", r, pending.size());
       
    80             if (r != null) {
       
    81                 pending.remove(r);
       
    82             } else {
       
    83                 cleanup = null;
       
    84                 raf = null;
       
    85                 fd = null;
       
    86                 System.gc();  // attempt to reclaim the RAF, cleanup, and fd
       
    87             }
       
    88         }
       
    89 
       
    90         // Keep these variables in scope as gc roots
       
    91         Reference.reachabilityFence(cleanup);
       
    92         Reference.reachabilityFence(fd);
       
    93         Reference.reachabilityFence(raf);
       
    94         Reference.reachabilityFence(pending);
       
    95     }
       
    96 }