8205054: Could not find "lsof" on test machine
authorlkorinth
Mon, 18 Jun 2018 20:43:42 -0400
changeset 50615 9ba6f5dfbe56
parent 50614 3810c9a2efa1
child 50616 c9e7dc7976ae
8205054: Could not find "lsof" on test machine Reviewed-by: dholmes, mikael, goetz
test/hotspot/jtreg/runtime/8176717/TestInheritFD.java
--- a/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java	Tue Jun 19 08:06:35 2018 +0800
+++ b/test/hotspot/jtreg/runtime/8176717/TestInheritFD.java	Mon Jun 18 20:43:42 2018 -0400
@@ -1,21 +1,3 @@
-import static java.io.File.createTempFile;
-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.stream.Collectors.joining;
-import static java.util.stream.Collectors.toList;
-import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder;
-
-import java.io.BufferedReader;
-import java.io.File;
-import java.io.FileNotFoundException;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.util.Collection;
-import java.util.stream.Stream;
-
 /*
  * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
@@ -39,6 +21,26 @@
  * questions.
  */
 
+import static java.io.File.createTempFile;
+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.stream.Collectors.joining;
+import static java.util.stream.Collectors.toList;
+import static jdk.test.lib.process.ProcessTools.createJavaProcessBuilder;
+import static jdk.test.lib.Platform.isWindows;
+
+import java.io.BufferedReader;
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.util.Collection;
+import java.util.Optional;
+import java.util.stream.Stream;
+
 /*
  * @test TestInheritFD
  * @bug 8176717 8176809
@@ -78,6 +80,12 @@
         String logPath = createTempFile("logging", LOG_SUFFIX).getName();
         File commFile = createTempFile("communication", ".txt");
 
+        if (!isWindows() && !lsofCommand().isPresent()) {
+            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"),
@@ -113,7 +121,7 @@
             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")));
             }
         }
@@ -127,7 +135,7 @@
                 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<String> output = outputContainingFilenames();
@@ -161,19 +169,24 @@
         }
     }
 
+    static Optional<String[]> lsofCommandCache = stream(new String[][]{
+            {"/usr/bin/lsof", "-p"},
+            {"/usr/sbin/lsof", "-p"},
+            {"/bin/lsof", "-p"},
+            {"/sbin/lsof", "-p"},
+            {"/usr/local/bin/lsof", "-p"},
+            {"/usr/bin/pfiles", "-F"}}) // Solaris
+        .filter(args -> new File(args[0]).exists())
+        .findFirst();
+
+    static Optional<String[]> lsofCommand() {
+        return lsofCommandCache;
+    }
+
     static Collection<String> outputContainingFilenames() {
         long pid = ProcessHandle.current().pid();
-        String[] command = stream(new String[][]{
-                {"/usr/bin/lsof", "-p"},
-                {"/usr/sbin/lsof", "-p"},
-                {"/bin/lsof", "-p"},
-                {"/sbin/lsof", "-p"},
-                {"/usr/local/bin/lsof", "-p"},
-                {"/usr/bin/pfiles", "-F"}}) // Solaris
-            .filter(args -> new File(args[0]).exists())
-            .findFirst()
-            .orElseThrow(() -> new RuntimeException("could not find lsof-like command"));
 
+        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());
     }
@@ -191,4 +204,5 @@
         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"
     }
-}
\ No newline at end of file
+}
+