test/jdk/java/nio/channels/FileChannel/CleanerTest.java
changeset 47327 8cb132b3a016
child 50796 1f1eb24facdd
equal deleted inserted replaced
47326:00f9fe99736e 47327:8cb132b3a016
       
     1 /*
       
     2  * Copyright (c) 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 /* @test
       
    25  * @bug 8147615
       
    26  * @summary Test whether an unreferenced FileChannel is actually cleaned
       
    27  * @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris") | (os.family == "aix")
       
    28  * @modules java.management
       
    29  * @run main/othervm CleanerTest
       
    30  */
       
    31 
       
    32 import com.sun.management.UnixOperatingSystemMXBean;
       
    33 import java.lang.management.ManagementFactory;
       
    34 import java.lang.management.OperatingSystemMXBean;
       
    35 import java.lang.ref.PhantomReference;
       
    36 import java.lang.ref.Reference;
       
    37 import java.lang.ref.ReferenceQueue;
       
    38 import java.nio.channels.FileChannel;
       
    39 import java.nio.file.Files;
       
    40 import java.nio.file.Path;
       
    41 import java.nio.file.Paths;
       
    42 import java.nio.file.StandardOpenOption;
       
    43 
       
    44 public class CleanerTest {
       
    45     public static void main(String[] args) throws Throwable {
       
    46         OperatingSystemMXBean mxBean =
       
    47             ManagementFactory.getOperatingSystemMXBean();
       
    48         UnixOperatingSystemMXBean unixMxBean = null;
       
    49         if (mxBean instanceof UnixOperatingSystemMXBean) {
       
    50             unixMxBean = (UnixOperatingSystemMXBean)mxBean;
       
    51         } else {
       
    52             System.out.println("Non-Unix system: skipping test.");
       
    53             return;
       
    54         }
       
    55 
       
    56         Path path = Paths.get(System.getProperty("test.dir", "."), "junk");
       
    57         try {
       
    58             FileChannel fc = FileChannel.open(path, StandardOpenOption.CREATE,
       
    59                 StandardOpenOption.READ, StandardOpenOption.WRITE);
       
    60 
       
    61             ReferenceQueue refQueue = new ReferenceQueue();
       
    62             Reference fcRef = new PhantomReference(fc, refQueue);
       
    63 
       
    64             long fdCount0 = unixMxBean.getOpenFileDescriptorCount();
       
    65             fc = null;
       
    66 
       
    67             // Perform repeated GCs until the reference has been enqueued.
       
    68             do {
       
    69                 Thread.sleep(1);
       
    70                 System.gc();
       
    71             } while (refQueue.poll() == null);
       
    72 
       
    73             // Loop until the open file descriptor count has been decremented.
       
    74             while (unixMxBean.getOpenFileDescriptorCount() > fdCount0 - 1) {
       
    75                 Thread.sleep(1);
       
    76             }
       
    77 
       
    78             long fdCount = unixMxBean.getOpenFileDescriptorCount();
       
    79             if (fdCount != fdCount0 - 1) {
       
    80                 throw new RuntimeException("FD count expected " +
       
    81                     (fdCount0 - 1) + "; actual " + fdCount);
       
    82             }
       
    83         } finally {
       
    84             Files.delete(path);
       
    85         }
       
    86     }
       
    87 }