< prev index next >

test/lib/testlibrary/jdk/testlibrary/Utils.java

Print this page
rev 17382 : imported patch 8182565


 200         return port;
 201     }
 202 
 203     /**
 204      * Returns the name of the local host.
 205      *
 206      * @return The host name
 207      * @throws UnknownHostException if IP address of a host could not be determined
 208      */
 209     public static String getHostname() throws UnknownHostException {
 210         InetAddress inetAddress = InetAddress.getLocalHost();
 211         String hostName = inetAddress.getHostName();
 212 
 213         assertTrue((hostName != null && !hostName.isEmpty()),
 214                 "Cannot get hostname");
 215 
 216         return hostName;
 217     }
 218 
 219     /**
 220      * Uses "jcmd -l" to search for a jvm pid. This function will wait
 221      * forever (until jtreg timeout) for the pid to be found.
 222      * @param key Regular expression to search for
 223      * @return The found pid.
 224      */
 225     public static int waitForJvmPid(String key) throws Throwable {
 226         final long iterationSleepMillis = 250;
 227         System.out.println("waitForJvmPid: Waiting for key '" + key + "'");
 228         System.out.flush();
 229         while (true) {
 230             int pid = tryFindJvmPid(key);
 231             if (pid >= 0) {
 232                 return pid;
 233             }
 234             Thread.sleep(iterationSleepMillis);
 235         }
 236     }
 237 
 238     /**
 239      * Searches for a jvm pid in the output from "jcmd -l".
 240      *
 241      * Example output from jcmd is:
 242      * 12498 sun.tools.jcmd.JCmd -l
 243      * 12254 /tmp/jdk8/tl/jdk/JTwork/classes/com/sun/tools/attach/Application.jar
 244      *
 245      * @param key A regular expression to search for.
 246      * @return The found pid, or -1 if Enot found.
 247      * @throws Exception If multiple matching jvms are found.
 248      */
 249     public static int tryFindJvmPid(String key) throws Throwable {
 250         OutputAnalyzer output = null;
 251         try {
 252             JDKToolLauncher jcmdLauncher = JDKToolLauncher.create("jcmd");
 253             jcmdLauncher.addToolArg("-l");
 254             output = ProcessTools.executeProcess(jcmdLauncher.getCommand());
 255             output.shouldHaveExitValue(0);
 256 
 257             // Search for a line starting with numbers (pid), follwed by the key.
 258             Pattern pattern = Pattern.compile("([0-9]+)\\s.*(" + key + ").*\\r?\\n");
 259             Matcher matcher = pattern.matcher(output.getStdout());
 260 
 261             int pid = -1;
 262             if (matcher.find()) {
 263                 pid = Integer.parseInt(matcher.group(1));
 264                 System.out.println("findJvmPid.pid: " + pid);
 265                 if (matcher.find()) {
 266                     throw new Exception("Found multiple JVM pids for key: " + key);
 267                 }
 268             }
 269             return pid;
 270         } catch (Throwable t) {
 271             System.out.println(String.format("Utils.findJvmPid(%s) failed: %s", key, t));
 272             throw t;
 273         }
 274     }
 275 
 276     /**
 277      * Adjusts the provided timeout value for the TIMEOUT_FACTOR
 278      * @param tOut the timeout value to be adjusted
 279      * @return The timeout value adjusted for the value of "test.timeout.factor"
 280      *         system property
 281      */
 282     public static long adjustTimeout(long tOut) {
 283         return Math.round(tOut * Utils.TIMEOUT_FACTOR);
 284     }
 285 
 286     /**
 287      * Wait for condition to be true
 288      *
 289      * @param condition, a condition to wait for
 290      */
 291     public static final void waitForCondition(BooleanSupplier condition) {
 292         waitForCondition(condition, -1L, 100L);
 293     }
 294 
 295     /**
 296      * Wait until timeout for condition to be true




 200         return port;
 201     }
 202 
 203     /**
 204      * Returns the name of the local host.
 205      *
 206      * @return The host name
 207      * @throws UnknownHostException if IP address of a host could not be determined
 208      */
 209     public static String getHostname() throws UnknownHostException {
 210         InetAddress inetAddress = InetAddress.getLocalHost();
 211         String hostName = inetAddress.getHostName();
 212 
 213         assertTrue((hostName != null && !hostName.isEmpty()),
 214                 "Cannot get hostname");
 215 
 216         return hostName;
 217     }
 218 
 219     /**

























































 220      * Adjusts the provided timeout value for the TIMEOUT_FACTOR
 221      * @param tOut the timeout value to be adjusted
 222      * @return The timeout value adjusted for the value of "test.timeout.factor"
 223      *         system property
 224      */
 225     public static long adjustTimeout(long tOut) {
 226         return Math.round(tOut * Utils.TIMEOUT_FACTOR);
 227     }
 228 
 229     /**
 230      * Wait for condition to be true
 231      *
 232      * @param condition, a condition to wait for
 233      */
 234     public static final void waitForCondition(BooleanSupplier condition) {
 235         waitForCondition(condition, -1L, 100L);
 236     }
 237 
 238     /**
 239      * Wait until timeout for condition to be true


< prev index next >