18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.testlibrary; 25 26 import static jdk.testlibrary.Asserts.assertTrue; 27 28 import java.io.IOException; 29 import java.net.InetAddress; 30 import java.net.ServerSocket; 31 import java.net.UnknownHostException; 32 import java.util.ArrayList; 33 import java.util.List; 34 import java.util.Arrays; 35 import java.util.Collections; 36 import java.util.regex.Pattern; 37 import java.util.regex.Matcher; 38 39 /** 40 * Common library for various test helper functions. 41 */ 42 public final class Utils { 43 44 /** 45 * Returns the sequence used by operating system to separate lines. 46 */ 47 public static final String NEW_LINE = System.getProperty("line.separator"); 48 49 /** 50 * Returns the value of 'test.vm.opts'system property. 51 */ 52 public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim(); 53 54 /** 55 * Returns the value of 'test.java.opts'system property. 56 */ 57 public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim(); 58 59 60 /** 61 * Returns the value of 'test.timeout.factor' system property 62 * converted to {@code double}. 63 */ 64 public static final double TIMEOUT_FACTOR; 65 static { 66 String toFactor = System.getProperty("test.timeout.factor", "1.0"); 67 TIMEOUT_FACTOR = Double.parseDouble(toFactor); 68 } 69 70 private Utils() { 71 // Private constructor to prevent class instantiation 72 } 73 74 /** 75 * Returns the list of VM options. 76 * 77 * @return List of VM options 78 */ 79 public static List<String> getVmOptions() { 80 return Arrays.asList(safeSplitString(VM_OPTIONS)); 81 } 82 83 /** 84 * Returns the list of VM options with -J prefix. 85 * 86 * @return The list of VM options with -J prefix 87 */ 88 public static List<String> getForwardVmOptions() { 89 String[] opts = safeSplitString(VM_OPTIONS); 241 output = ProcessTools.executeProcess(jcmdLauncher.getCommand()); 242 output.shouldHaveExitValue(0); 243 244 // Search for a line starting with numbers (pid), follwed by the key. 245 Pattern pattern = Pattern.compile("([0-9]+)\\s.*(" + key + ").*\\r?\\n"); 246 Matcher matcher = pattern.matcher(output.getStdout()); 247 248 int pid = -1; 249 if (matcher.find()) { 250 pid = Integer.parseInt(matcher.group(1)); 251 System.out.println("findJvmPid.pid: " + pid); 252 if (matcher.find()) { 253 throw new Exception("Found multiple JVM pids for key: " + key); 254 } 255 } 256 return pid; 257 } catch (Throwable t) { 258 System.out.println(String.format("Utils.findJvmPid(%s) failed: %s", key, t)); 259 throw t; 260 } 261 } 262 } | 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 package jdk.testlibrary; 25 26 import static jdk.testlibrary.Asserts.assertTrue; 27 28 import java.io.IOException; 29 import java.net.InetAddress; 30 import java.net.ServerSocket; 31 import java.net.UnknownHostException; 32 import java.util.ArrayList; 33 import java.util.List; 34 import java.util.Arrays; 35 import java.util.Collections; 36 import java.util.regex.Pattern; 37 import java.util.regex.Matcher; 38 import java.util.concurrent.TimeUnit; 39 40 /** 41 * Common library for various test helper functions. 42 */ 43 public final class Utils { 44 45 /** 46 * Returns the sequence used by operating system to separate lines. 47 */ 48 public static final String NEW_LINE = System.getProperty("line.separator"); 49 50 /** 51 * Returns the value of 'test.vm.opts'system property. 52 */ 53 public static final String VM_OPTIONS = System.getProperty("test.vm.opts", "").trim(); 54 55 /** 56 * Returns the value of 'test.java.opts'system property. 57 */ 58 public static final String JAVA_OPTIONS = System.getProperty("test.java.opts", "").trim(); 59 60 61 /** 62 * Returns the value of 'test.timeout.factor' system property 63 * converted to {@code double}. 64 */ 65 public static final double TIMEOUT_FACTOR; 66 static { 67 String toFactor = System.getProperty("test.timeout.factor", "1.0"); 68 TIMEOUT_FACTOR = Double.parseDouble(toFactor); 69 } 70 71 /** 72 * Returns the value of JTREG default test timeout in milliseconds 73 * converted to {@code long}. 74 */ 75 public static final long DEFAULT_TEST_TIMEOUT = TimeUnit.SECONDS.toMillis(120); 76 77 private Utils() { 78 // Private constructor to prevent class instantiation 79 } 80 81 /** 82 * Returns the list of VM options. 83 * 84 * @return List of VM options 85 */ 86 public static List<String> getVmOptions() { 87 return Arrays.asList(safeSplitString(VM_OPTIONS)); 88 } 89 90 /** 91 * Returns the list of VM options with -J prefix. 92 * 93 * @return The list of VM options with -J prefix 94 */ 95 public static List<String> getForwardVmOptions() { 96 String[] opts = safeSplitString(VM_OPTIONS); 248 output = ProcessTools.executeProcess(jcmdLauncher.getCommand()); 249 output.shouldHaveExitValue(0); 250 251 // Search for a line starting with numbers (pid), follwed by the key. 252 Pattern pattern = Pattern.compile("([0-9]+)\\s.*(" + key + ").*\\r?\\n"); 253 Matcher matcher = pattern.matcher(output.getStdout()); 254 255 int pid = -1; 256 if (matcher.find()) { 257 pid = Integer.parseInt(matcher.group(1)); 258 System.out.println("findJvmPid.pid: " + pid); 259 if (matcher.find()) { 260 throw new Exception("Found multiple JVM pids for key: " + key); 261 } 262 } 263 return pid; 264 } catch (Throwable t) { 265 System.out.println(String.format("Utils.findJvmPid(%s) failed: %s", key, t)); 266 throw t; 267 } 268 } 269 270 /** 271 * Adjusts the provided timeout value for the TIMEOUT_FACTOR 272 * @param tOut the timeout value to be adjusted 273 * @return The timeout value adjusted for the value of "test.timeout.factor" 274 * system property 275 */ 276 public static long adjustTimeout(long tOut) { 277 return Math.round(tOut * Utils.TIMEOUT_FACTOR); 278 } 279 } |