8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently
authorclanger
Mon, 16 Sep 2019 09:21:42 +0200
changeset 58143 b35771556cd0
parent 58142 24df796eef3d
child 58144 d003b3ef8b60
8230850: Test sun/tools/jcmd/TestProcessHelper.java fails intermittently Reviewed-by: stuefe, cjplummer, sgehwolf
test/jdk/sun/tools/jcmd/TestProcessHelper.java
--- a/test/jdk/sun/tools/jcmd/TestProcessHelper.java	Sun Sep 15 21:00:15 2019 -0400
+++ b/test/jdk/sun/tools/jcmd/TestProcessHelper.java	Mon Sep 16 09:21:42 2019 +0200
@@ -189,6 +189,26 @@
 
     private void checkMainClass(Process p, String expectedMainClass) {
         String mainClass = PROCESS_HELPER.getMainClass(Long.toString(p.pid()));
+        // getMainClass() may return null, e.g. due to timing issues.
+        // Attempt some limited retries.
+        if (mainClass == null) {
+            System.err.println("Main class returned by ProcessHelper was null.");
+            // sleep time doubles each round, altogether, wait no longer than 1 sec
+            final int MAX_RETRIES = 10;
+            int retrycount = 0;
+            long sleepms = 1;
+            while (retrycount < MAX_RETRIES && mainClass == null) {
+                System.err.println("Retry " + retrycount + ", sleeping for " + sleepms + "ms.");
+                try {
+                    Thread.sleep(sleepms);
+                } catch (InterruptedException e) {
+                    // ignore
+                }
+                mainClass = PROCESS_HELPER.getMainClass(Long.toString(p.pid()));
+                retrycount++;
+                sleepms *= 2;
+            }
+        }
         p.destroyForcibly();
         if (!expectedMainClass.equals(mainClass)) {
             throw new RuntimeException("Main class is wrong: " + mainClass);