From 06624126864eb5510fe53823e835e9af4d191ab0 Mon Sep 17 00:00:00 2001 From: Peter Shipton Date: Mon, 16 Nov 2020 12:08:32 -0500 Subject: [PATCH] Fix getUnixPID() for jdk11+ and stop waiting for tests after 10 min Issue https://github.com/eclipse/openj9/issues/11196 Issue https://github.com/eclipse/openj9/issues/11197 [ci skip] Signed-off-by: Peter Shipton --- .../cmdline_options_tester/src/Test.java | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/test/functional/cmdline_options_tester/src/Test.java b/test/functional/cmdline_options_tester/src/Test.java index e1568e6bd89..60ca57a9797 100644 --- a/test/functional/cmdline_options_tester/src/Test.java +++ b/test/functional/cmdline_options_tester/src/Test.java @@ -29,6 +29,7 @@ import java.io.PrintWriter; import java.io.Writer; import java.lang.reflect.Field; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.Collections; import java.util.List; @@ -73,6 +74,7 @@ class Test { static String archName = System.getProperty("os.arch"); static boolean isRiscv = archName.toLowerCase().contains("riscv"); + static boolean isJava8 = System.getProperty("java.specification.version").equals("1.8"); /** * Create a new test case with the given id. @@ -224,7 +226,10 @@ public boolean run(long defaultTimeout) { TestSuite.printErrorMessage("stderr timed out"); } - proc.waitFor(); + // Wait an additional 10 min after the regular timeout for the process to finish. + // It should finish because the ProcessKiller terminates it, but if it doesn't + // then don't wait forever. + proc.waitFor(_timeout + (10 * 60 * 1000), TimeUnit.MILLISECONDS); testTimer.stop(); timer = testTimer.getTimeSpent(); @@ -411,14 +416,24 @@ public void dumpVerboseOutput(boolean result) { } public static int getUnixPID(Process process) throws Exception { - Class cl = process.getClass(); - if (!cl.getName().equals("java.lang.UNIXProcess") || isRiscv) { + if (isRiscv) { return 0; } - Field field = cl.getDeclaredField("pid"); - field.setAccessible(true); - Object pidObject = field.get(process); - return ((Integer) pidObject).intValue(); + if (isJava8) { + Class cl = process.getClass(); + if (!cl.getName().equals("java.lang.UNIXProcess")) { + return 0; + } + Field field = cl.getDeclaredField("pid"); + field.setAccessible(true); + Object pidObject = field.get(process); + return ((Integer) pidObject).intValue(); + } else { + // if not Java 8 then it must be Java 11 or later + Method pidMethod = Process.class.getMethod("pid"); + Long pid = (Long)pidMethod.invoke(process); + return pid.intValue(); + } } private final class StreamMatcher extends Thread {