Path: blob/master/test/jdk/java/nio/channels/FileLock/FileLockGC.java
41154 views
/*1* Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.IOException;25import java.io.RandomAccessFile;26import java.lang.ref.Reference;27import java.lang.ref.WeakReference;28import java.nio.channels.FileLock;29import java.nio.channels.OverlappingFileLockException;30import java.nio.file.Files;31import java.nio.file.Path;32import jdk.test.lib.util.FileUtils;3334/*35* @test36* @bug 816625337* @summary Verify that OverlappingFileLockException is thrown when expected.38* @library .. /test/lib39* @build jdk.test.lib.util.FileUtils40* @run main/othervm FileLockGC41*/42public class FileLockGC {43public enum TestType {44NO_GC_NO_RELEASE(true),45// A hypothetical 'GC_THEN_RELEASE' case is infeasible46RELEASE(false),47RELEASE_THEN_GC(false),48GC(true);4950private final boolean exceptionExpected;5152TestType(boolean exceptionExpected) {53this.exceptionExpected = exceptionExpected;54}5556boolean exceptionExpected() {57return exceptionExpected;58}59}6061public static void main(String[] args) throws Exception {62final File f = new File(System.getProperty("test.dir", ".")63+ File.separator + "junk.txt");64final Path p = f.toPath();65int failures = 0;6667for (TestType t : TestType.values()) {68try {69if (!testFileLockGC(f, t)) {70failures++;71}72} finally {73FileUtils.deleteFileIfExistsWithRetry(p);74}75}7677if (failures != 0) {78throw new RuntimeException("Test had " + failures + " failure(s)");79}80}8182private static boolean testFileLockGC(File f, TestType type)83throws InterruptedException, IOException {84System.out.printf("Test %s starting%n", type.toString());8586final RandomAccessFile raf1 = new RandomAccessFile(f, "rw");8788FileLock lock1 = raf1.getChannel().tryLock();89WeakReference<FileLock> ref1 = new WeakReference(lock1);9091switch (type) {92case GC:93lock1 = null;94System.gc();95break;96case RELEASE:97lock1.release();98break;99case RELEASE_THEN_GC:100lock1.release();101lock1 = null;102System.gc();103break;104default: // NO_GC_NO_RELEASE105// lock1 is neither collected nor released106break;107}108109final RandomAccessFile raf2 = new RandomAccessFile(f, "rw");110111boolean success = true;112FileLock lock2 = null;113try {114lock2 = raf2.getChannel().tryLock();115if (type.exceptionExpected()) {116System.err.printf117("No expected OverlappingFileLockException for test %s%n",118type.toString());119success = false;120}121} catch (OverlappingFileLockException ofe) {122if (!type.exceptionExpected()) {123System.err.printf124("Unexpected OverlappingFileLockException for test %s%n",125type.toString());126success = false;127}128} finally {129if (lock1 != null) {130lock1.release();131}132if (lock2 != null) {133lock2.release();134}135raf2.close();136raf1.close();137System.out.printf("Test %s finished%n", type.toString());138}139140return success;141}142}143144145