diff --git a/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java b/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java index 50857eea6d5..8b36da03a60 100644 --- a/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java +++ b/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java @@ -26,7 +26,6 @@ import static java.lang.Long.parseLong; import static java.lang.System.getProperty; import static java.nio.file.Files.readAllBytes; import static java.util.Arrays.stream; -import static java.util.Collections.emptyList; import static java.util.stream.Collectors.joining; import static java.util.stream.Collectors.toList; import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder; @@ -80,6 +79,12 @@ public class TestInheritFD { String logPath = createTempFile("logging", LOG_SUFFIX).getName(); File commFile = createTempFile("communication", ".txt"); + if (isWindows() == false && lsofCommand().isPresent() == false) { + System.out.println("Could not find lsof like command"); + System.out.println("Exit test case as successful though it could not verify anything"); + return; + } + ProcessBuilder pb = createJavaProcessBuilder( "-Xlog:gc:\"" + logPath + "\"", "-Dtest.jdk=" + getProperty("test.jdk"), @@ -115,7 +120,7 @@ public class TestInheritFD { pb.inheritIO(); // in future, redirect information from third VM to first VM pb.start(); - if (getProperty("os.name").toLowerCase().contains("win") == false) { + if (!isWindows()) { System.out.println("(Second VM) Open file descriptors:\n" + outputContainingFilenames().stream().collect(joining("\n"))); } } @@ -129,7 +134,7 @@ public class TestInheritFD { long parentPid = parseLong(args[1]); fakeLeakyJVM(false); // for debugging of test case - if (getProperty("os.name").toLowerCase().contains("win")) { + if (isWindows()) { windows(logFile, parentPid); } else { Collection output = outputContainingFilenames(); @@ -163,9 +168,12 @@ public class TestInheritFD { } } - static Collection outputContainingFilenames() { - long pid = ProcessHandle.current().pid(); - Optional command = stream(new String[][]{ + static boolean isWindows() { + return getProperty("os.name").toLowerCase().contains("win"); + } + + static Optional lsofCommand() { + return stream(new String[][]{ {"/usr/bin/lsof", "-p"}, {"/usr/sbin/lsof", "-p"}, {"/bin/lsof", "-p"}, @@ -174,10 +182,14 @@ public class TestInheritFD { {"/usr/bin/pfiles", "-F"}}) // Solaris .filter(args -> new File(args[0]).exists()) .findFirst(); + } + + static Collection outputContainingFilenames() { + long pid = ProcessHandle.current().pid(); - System.out.println("using command: " + command.map((c) -> c[0] + " " + c[1]).orElse("")); - // if command can not be found return list without log file (some machines does not have "lsof" in the expected place) - return command.map(c -> run(c[0], c[1], "" + pid).collect(toList())).orElse(emptyList()); + String[] command = lsofCommand().orElseThrow(() -> new RuntimeException("lsof like command not found")); + System.out.println("using command: " + command[0] + " " + command[1]); + return run(command[0], command[1], "" + pid).collect(toList()); } static boolean findOpenLogFile(Collection fileNames) {