diff --git a/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java b/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java index 38d462ec9d3..7cea65e39aa 100644 --- a/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java +++ b/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java @@ -5,11 +5,16 @@ import static java.lang.management.ManagementFactory.getOperatingSystemMXBean; import static java.nio.file.Files.readAllBytes; import static java.nio.file.Files.readSymbolicLink; import static java.util.Arrays.stream; +import static java.util.Optional.empty; +import static java.util.Optional.of; import static java.util.stream.Collectors.joining; import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; import java.io.IOException; +import java.util.Optional; import com.sun.management.UnixOperatingSystemMXBean; @@ -124,6 +129,8 @@ public class TestInheritFD { long parentPid = parseLong(args[1]); long parentFDCount = parseLong(args[2]); + fakeLeakyJVM(false); + if(supportsUnixMXBean()){ long thisFDCount = unixNrFD(); System.out.println("This VM FD-count (" @@ -131,8 +138,8 @@ public class TestInheritFD { + ") should be strictly less than parent VM FD-count (" + parentFDCount + ") as log file should have been closed, HOWEVER, THIS CAN NOT BE RELIED" - + " ON as files in /proc and /sys is opened by the JVM"); - System.out.println(findOpenLogFile()?LEAKS_FD:RETAINS_FD); + + " ON as files in /proc and /sys are opened by the JVM"); + System.out.println(findOpenLogFile() ? LEAKS_FD : RETAINS_FD); } else if (getProperty("os.name").toLowerCase().contains("win")) { windows(logFile, parentPid); } else { @@ -151,11 +158,21 @@ public class TestInheritFD { return osBean.getOpenFileDescriptorCount(); } - static File linkTarget(File f) { + static Optional linkTargetName(File f) { try { - return readSymbolicLink(f.toPath()).toFile(); + return of(readSymbolicLink(f.toPath()).toFile().getName()); } catch (IOException e) { - return new File("Error: could not read symbolic link (last link can not be read as it points to the /proc/self/fd directory that has been closed): " + e.toString()); + return empty(); + } + } + + @SuppressWarnings("resource") + static void fakeLeakyJVM(boolean fake) { + if (fake) { + try { + new FileOutputStream("fakeLeakyJVM" + LOG_SUFFIX, false); + } catch (FileNotFoundException e) { + } } } @@ -163,12 +180,13 @@ public class TestInheritFD { File dir = new File("/proc/self/fd"); System.out.println("Open file descriptors:\n" + stream(dir.listFiles()) - .map(f -> "###" + f.getAbsolutePath() + " -> " + linkTarget(f)) + .map(f -> f.getAbsolutePath() + " maps to: " + linkTargetName(f).orElse("?")) .collect(joining("\n"))); return stream(dir.listFiles()) - .map(TestInheritFD::linkTarget) - .filter(f -> f.getName().endsWith(LOG_SUFFIX)) + .map(TestInheritFD::linkTargetName) + .flatMap(Optional::stream) + .filter(fileName -> fileName.endsWith(LOG_SUFFIX)) .findAny() .isPresent(); } @@ -177,6 +195,6 @@ public class TestInheritFD { System.out.println("waiting for pid: " + parentPid); ProcessHandle.of(parentPid).ifPresent(handle -> handle.onExit().join()); System.out.println("trying to rename file to the same name: " + f); - System.out.println(f.renameTo(f)?RETAINS_FD:LEAKS_FD); // this parts communicates a closed file descriptor by printing "VM RESULT => RETAINS FD" + System.out.println(f.renameTo(f) ? RETAINS_FD : LEAKS_FD); // this parts communicates a closed file descriptor by printing "VM RESULT => RETAINS FD" } } \ No newline at end of file