test/jdk/java/nio/channels/FileChannel/CleanerTest.java
author bpb
Mon, 30 Apr 2018 13:40:39 -0700
changeset 49930 3aaaa5370999
parent 47327 8cb132b3a016
child 50796 1f1eb24facdd
permissions -rw-r--r--
8202284: FileChannel and FileOutpuStream variants of AtomicAppend should fail silently on macOS >= 10.13 Reviewed-by: chegar
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
47327
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     1
/*
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     2
 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     4
 *
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     7
 * published by the Free Software Foundation.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     8
 *
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    13
 * accompanied this code).
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    14
 *
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    18
 *
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    21
 * questions.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    22
 */
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    23
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    24
/* @test
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    25
 * @bug 8147615
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    26
 * @summary Test whether an unreferenced FileChannel is actually cleaned
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    27
 * @requires (os.family == "linux") | (os.family == "mac") | (os.family == "solaris") | (os.family == "aix")
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    28
 * @modules java.management
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    29
 * @run main/othervm CleanerTest
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    30
 */
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    31
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    32
import com.sun.management.UnixOperatingSystemMXBean;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    33
import java.lang.management.ManagementFactory;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    34
import java.lang.management.OperatingSystemMXBean;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    35
import java.lang.ref.PhantomReference;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    36
import java.lang.ref.Reference;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    37
import java.lang.ref.ReferenceQueue;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    38
import java.nio.channels.FileChannel;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    39
import java.nio.file.Files;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    40
import java.nio.file.Path;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    41
import java.nio.file.Paths;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    42
import java.nio.file.StandardOpenOption;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    43
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    44
public class CleanerTest {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    45
    public static void main(String[] args) throws Throwable {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    46
        OperatingSystemMXBean mxBean =
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    47
            ManagementFactory.getOperatingSystemMXBean();
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    48
        UnixOperatingSystemMXBean unixMxBean = null;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    49
        if (mxBean instanceof UnixOperatingSystemMXBean) {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    50
            unixMxBean = (UnixOperatingSystemMXBean)mxBean;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    51
        } else {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    52
            System.out.println("Non-Unix system: skipping test.");
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    53
            return;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    54
        }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    55
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    56
        Path path = Paths.get(System.getProperty("test.dir", "."), "junk");
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    57
        try {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    58
            FileChannel fc = FileChannel.open(path, StandardOpenOption.CREATE,
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    59
                StandardOpenOption.READ, StandardOpenOption.WRITE);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    60
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    61
            ReferenceQueue refQueue = new ReferenceQueue();
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    62
            Reference fcRef = new PhantomReference(fc, refQueue);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    63
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    64
            long fdCount0 = unixMxBean.getOpenFileDescriptorCount();
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    65
            fc = null;
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    66
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    67
            // Perform repeated GCs until the reference has been enqueued.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    68
            do {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    69
                Thread.sleep(1);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    70
                System.gc();
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    71
            } while (refQueue.poll() == null);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    72
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    73
            // Loop until the open file descriptor count has been decremented.
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    74
            while (unixMxBean.getOpenFileDescriptorCount() > fdCount0 - 1) {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    75
                Thread.sleep(1);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    76
            }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    77
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    78
            long fdCount = unixMxBean.getOpenFileDescriptorCount();
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    79
            if (fdCount != fdCount0 - 1) {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    80
                throw new RuntimeException("FD count expected " +
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    81
                    (fdCount0 - 1) + "; actual " + fdCount);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    82
            }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    83
        } finally {
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    84
            Files.delete(path);
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    85
        }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    86
    }
8cb132b3a016 8147615: (fc) FileChannelImpl has no finalizer
bpb
parents:
diff changeset
    87
}