30899
|
1 |
/*
|
|
2 |
* Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
|
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
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 |
import java.io.IOException;
|
|
25 |
import java.lang.InterruptedException;
|
|
26 |
import java.time.Duration;
|
|
27 |
import java.time.Instant;
|
|
28 |
import java.util.ArrayList;
|
|
29 |
import java.util.Arrays;
|
|
30 |
import java.util.List;
|
|
31 |
import java.util.concurrent.ArrayBlockingQueue;
|
|
32 |
import java.util.concurrent.CompletableFuture;
|
|
33 |
import java.util.concurrent.ConcurrentHashMap;
|
|
34 |
import java.util.concurrent.ExecutionException;
|
|
35 |
import java.util.stream.Collectors;
|
|
36 |
import jdk.testlibrary.Platform;
|
|
37 |
import org.testng.annotations.Test;
|
|
38 |
import org.testng.Assert;
|
|
39 |
import org.testng.TestNG;
|
|
40 |
|
|
41 |
/*
|
|
42 |
* @test
|
|
43 |
* @library /lib/testlibrary
|
|
44 |
* @summary Functions of Process.onExit and ProcessHandle.onExit
|
|
45 |
* @author Roger Riggs
|
|
46 |
*/
|
|
47 |
|
|
48 |
public class OnExitTest extends ProcessUtil {
|
|
49 |
|
|
50 |
@SuppressWarnings("raw_types")
|
|
51 |
public static void main(String[] args) {
|
|
52 |
Class<?>[] testclass = { OnExitTest.class};
|
|
53 |
TestNG testng = new TestNG();
|
|
54 |
testng.setTestClasses(testclass);
|
|
55 |
testng.run();
|
|
56 |
}
|
|
57 |
|
|
58 |
/**
|
|
59 |
* Basic test of exitValue and onExit.
|
|
60 |
*/
|
|
61 |
@Test
|
|
62 |
public static void test1() {
|
|
63 |
try {
|
|
64 |
int[] exitValues = {0, 1, 10};
|
|
65 |
for (int value : exitValues) {
|
|
66 |
Process p = JavaChild.spawn("exit", Integer.toString(value));
|
|
67 |
CompletableFuture<Process> future = p.onExit();
|
|
68 |
future.thenAccept( (ph) -> {
|
|
69 |
int actualExitValue = ph.exitValue();
|
|
70 |
printf(" javaChild done: %s, exitStatus: %d%n",
|
|
71 |
ph, actualExitValue);
|
|
72 |
Assert.assertEquals(actualExitValue, value, "actualExitValue incorrect");
|
|
73 |
Assert.assertEquals(ph, p, "Different Process passed to thenAccept");
|
|
74 |
});
|
|
75 |
|
|
76 |
Process h = future.get();
|
|
77 |
Assert.assertEquals(h, p);
|
|
78 |
Assert.assertEquals(p.exitValue(), value);
|
|
79 |
Assert.assertFalse(p.isAlive(), "Process should not be alive");
|
|
80 |
p.waitFor();
|
|
81 |
}
|
|
82 |
} catch (IOException | InterruptedException | ExecutionException ex) {
|
|
83 |
Assert.fail(ex.getMessage(), ex);
|
|
84 |
} finally {
|
|
85 |
destroyProcessTree(ProcessHandle.current());
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
/**
|
|
90 |
* Test of Completion handler when parent is killed.
|
|
91 |
* Spawn 1 child to spawn 3 children each with 2 children.
|
|
92 |
*/
|
|
93 |
@Test
|
|
94 |
public static void test2() {
|
|
95 |
try {
|
|
96 |
ConcurrentHashMap<ProcessHandle, ProcessHandle> processes = new ConcurrentHashMap<>();
|
|
97 |
List<ProcessHandle> children = getChildren(ProcessHandle.current());
|
|
98 |
children.forEach(ProcessUtil::printProcess);
|
|
99 |
Assert.assertEquals(children.size(), 0,
|
|
100 |
"Expected to start with zero children; " + children);
|
|
101 |
|
|
102 |
JavaChild proc = JavaChild.spawnJavaChild("stdin");
|
|
103 |
ProcessHandle procHandle = proc.toHandle();
|
|
104 |
printf(" spawned: %d%n", proc.getPid());
|
|
105 |
|
|
106 |
proc.forEachOutputLine((s) -> {
|
|
107 |
String[] split = s.trim().split(" ");
|
|
108 |
if (split.length == 3 && split[1].equals("spawn")) {
|
|
109 |
Long child = Long.valueOf(split[2]);
|
|
110 |
Long parent = Long.valueOf(split[0].split(":")[0]);
|
|
111 |
processes.put(ProcessHandle.of(child).get(), ProcessHandle.of(parent).get());
|
|
112 |
}
|
|
113 |
});
|
|
114 |
|
|
115 |
proc.sendAction("spawn", "3", "stdin");
|
|
116 |
|
|
117 |
proc.sendAction("child", "spawn", "2", "stdin");
|
|
118 |
|
|
119 |
// Poll until all 9 child processes exist or the timeout is reached
|
|
120 |
int expected = 9;
|
|
121 |
Instant endTimeout = Instant.now().plusSeconds(10L);
|
|
122 |
do {
|
|
123 |
Thread.sleep(200L);
|
|
124 |
printf(" subprocess count: %d, waiting for %d%n", processes.size(), expected);
|
|
125 |
} while (processes.size() < expected &&
|
|
126 |
Instant.now().isBefore(endTimeout));
|
|
127 |
|
|
128 |
children = getAllChildren(procHandle);
|
|
129 |
|
|
130 |
ArrayBlockingQueue<ProcessHandle> completions = new ArrayBlockingQueue<>(expected + 1);
|
|
131 |
Instant startTime = Instant.now();
|
|
132 |
// Create a future for each of the 9 children
|
|
133 |
processes.forEach( (p, parent) -> {
|
|
134 |
p.onExit().whenComplete((ph, ex) -> {
|
|
135 |
Duration elapsed = Duration.between(startTime, Instant.now());
|
|
136 |
completions.add(ph);
|
|
137 |
printf("whenComplete: pid: %s, exception: %s, thread: %s, elapsed: %s%n",
|
|
138 |
ph, ex, Thread.currentThread(), elapsed);
|
|
139 |
});
|
|
140 |
});
|
|
141 |
|
|
142 |
// Check that each of the spawned processes is included in the children
|
|
143 |
List<ProcessHandle> remaining = new ArrayList<>(children);
|
|
144 |
processes.forEach((p, parent) -> {
|
|
145 |
Assert.assertTrue(remaining.remove(p), "spawned process should have been in children");
|
|
146 |
});
|
|
147 |
|
|
148 |
// Remove Win32 system spawned conhost.exe processes
|
|
149 |
remaining.removeIf(ProcessUtil::isWindowsConsole);
|
|
150 |
|
|
151 |
remaining.forEach(p -> printProcess(p, "unexpected: "));
|
|
152 |
if (remaining.size() > 0) {
|
|
153 |
// Show full list for debugging
|
|
154 |
ProcessUtil.logTaskList();
|
|
155 |
}
|
|
156 |
|
|
157 |
proc.destroy(); // kill off the parent
|
|
158 |
proc.waitFor();
|
|
159 |
|
|
160 |
// Wait for all the processes to be completed
|
|
161 |
processes.forEach((p, parent) -> {
|
|
162 |
try {
|
|
163 |
p.onExit().get();
|
|
164 |
} catch (InterruptedException | ExecutionException ex) {
|
|
165 |
// ignore
|
|
166 |
}
|
|
167 |
});
|
|
168 |
|
|
169 |
// Verify that all 9 exit handlers were called
|
|
170 |
processes.forEach((p, parent) ->
|
|
171 |
Assert.assertTrue(completions.contains(p), "Child onExit not called: " + p
|
|
172 |
+ ", parent: " + parent
|
|
173 |
+ ": " + p.info()));
|
|
174 |
|
|
175 |
// Show the status of the original children
|
|
176 |
children.forEach(p -> printProcess(p, "after onExit:"));
|
|
177 |
|
|
178 |
Assert.assertEquals(proc.isAlive(), false, "destroyed process is alive:: %s%n" + proc);
|
|
179 |
|
|
180 |
List<ProcessHandle> children2 = getAllChildren(procHandle);
|
|
181 |
printf(" children2: %s%n", children2.toString());
|
|
182 |
Assert.assertEquals(children2.size(), 0, "After onExit, expected no children");
|
|
183 |
|
|
184 |
Assert.assertEquals(remaining.size(), 0, "Unaccounted for children");
|
|
185 |
|
|
186 |
} catch (IOException | InterruptedException ex) {
|
|
187 |
Assert.fail(ex.getMessage());
|
|
188 |
} finally {
|
|
189 |
destroyProcessTree(ProcessHandle.current());
|
|
190 |
}
|
|
191 |
}
|
|
192 |
|
|
193 |
}
|