8218197: [failurehandler] parent processes shouldn't be killed till their children are handle

Reviewed-by: dholmes, kbarrett
diff --git a/test/failure_handler/src/share/classes/jdk/test/failurehandler/HtmlSection.java b/test/failure_handler/src/share/classes/jdk/test/failurehandler/HtmlSection.java
index d20b133..833f1c6 100644
--- a/test/failure_handler/src/share/classes/jdk/test/failurehandler/HtmlSection.java
+++ b/test/failure_handler/src/share/classes/jdk/test/failurehandler/HtmlSection.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -153,13 +153,11 @@
     public HtmlSection createChildren(String[] sections) {
         int i = 0;
         int n = sections.length;
-        HtmlSection current = rootSection;
-        if (current != null) {
-            for (; i < n && current.child != null;
-                    ++i, current = current.child) {
-                if (!sections[i].equals(current.child.name)) {
-                    break;
-                }
+        HtmlSection current = this;
+        for (; i < n && current.child != null;
+                ++i, current = current.child) {
+            if (!sections[i].equals(current.child.name)) {
+                break;
             }
         }
         for (; i < n; ++i) {
diff --git a/test/failure_handler/src/share/classes/jdk/test/failurehandler/ToolKit.java b/test/failure_handler/src/share/classes/jdk/test/failurehandler/ToolKit.java
index beeca05..58c55b0 100644
--- a/test/failure_handler/src/share/classes/jdk/test/failurehandler/ToolKit.java
+++ b/test/failure_handler/src/share/classes/jdk/test/failurehandler/ToolKit.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -31,6 +31,7 @@
 import java.util.LinkedList;
 import java.util.List;
 import java.util.Queue;
+import java.util.Deque;
 
 public class ToolKit implements EnvironmentInfoGatherer, ProcessInfoGatherer {
     private final List<ActionSet> actions = new ArrayList<>();
@@ -52,20 +53,27 @@
 
     @Override
     public void gatherProcessInfo(HtmlSection section, long pid) {
-        Queue<Long> pids = new LinkedList<>();
-        pids.add(pid);
-        for (Long p = pids.poll(); p != null; p = pids.poll()) {
-            HtmlSection pidSection = section.createChildren("" + p);
-            List<Long> children = helper.getChildren(pidSection, p);
+        // as some of actions can kill a process, we need to get children of all
+        // test process first, and run the actions starting from the leaves
+        // and going up by the process tree
+        Deque<Long> orderedPids = new LinkedList<>();
+        Queue<Long> testPids = new LinkedList<>();
+        testPids.add(pid);
+        HtmlSection ptreeSection = section.createChildren("test_processes");
+        for (Long p = testPids.poll(); p != null; p = testPids.poll()) {
+            orderedPids.addFirst(p);
+            List<Long> children = helper.getChildren(ptreeSection, p);
             if (!children.isEmpty()) {
-                HtmlSection s = pidSection.createChildren("children");
+                HtmlSection s = ptreeSection.createChildren("" + p);
                 for (Long c : children) {
                     s.link(section, c.toString(), c.toString());
                 }
-                pids.addAll(children);
+                testPids.addAll(children);
             }
+        }
+        for (Long p : orderedPids) {
             for (ActionSet set : actions) {
-                set.gatherProcessInfo(pidSection, p);
+                set.gatherProcessInfo(section, p);
             }
         }
     }
diff --git a/test/failure_handler/src/share/classes/jdk/test/failurehandler/action/ActionHelper.java b/test/failure_handler/src/share/classes/jdk/test/failurehandler/action/ActionHelper.java
index 8b2d275..035838b 100644
--- a/test/failure_handler/src/share/classes/jdk/test/failurehandler/action/ActionHelper.java
+++ b/test/failure_handler/src/share/classes/jdk/test/failurehandler/action/ActionHelper.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -64,7 +64,7 @@
     public ActionHelper(Path workDir, String prefix, Properties properties,
                         Path... jdks) throws InvalidValueException {
         this.workDir = workDir.toAbsolutePath();
-        getChildren = new PatternAction("children",
+        getChildren = new PatternAction(null,
                 Utils.prependPrefix(prefix, "getChildren"), properties);
         ValueHandler.apply(this, properties, prefix);
         String[] pathStrings = System.getenv("PATH").split(File.pathSeparator);
diff --git a/test/failure_handler/src/share/classes/jdk/test/failurehandler/action/PatternAction.java b/test/failure_handler/src/share/classes/jdk/test/failurehandler/action/PatternAction.java
index fbcaaa7..62ae9d7 100644
--- a/test/failure_handler/src/share/classes/jdk/test/failurehandler/action/PatternAction.java
+++ b/test/failure_handler/src/share/classes/jdk/test/failurehandler/action/PatternAction.java
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2015, 2019, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -44,7 +44,7 @@
 
     public PatternAction(String name, String id, Properties properties)
             throws InvalidValueException {
-        action = new SimpleAction(("pattern." + name), id, properties);
+        action = new SimpleAction(name != null ? ("pattern." + name) : "pattern", id, properties);
         ValueHandler.apply(this, properties, id);
         originalArgs = action.args.clone();
     }