author | rriggs |
Tue, 07 Jul 2015 21:25:05 -0400 | |
changeset 31532 | c1e1214cebd3 |
parent 31182 | 07956922e5d8 |
child 31537 | 704fd18b5be1 |
permissions | -rw-r--r-- |
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.List; |
|
30 |
import java.util.concurrent.ArrayBlockingQueue; |
|
31 |
import java.util.concurrent.CompletableFuture; |
|
32 |
import java.util.concurrent.ConcurrentHashMap; |
|
33 |
import java.util.concurrent.ExecutionException; |
|
34 |
import org.testng.annotations.Test; |
|
35 |
import org.testng.Assert; |
|
36 |
import org.testng.TestNG; |
|
37 |
||
38 |
/* |
|
39 |
* @test |
|
40 |
* @summary Functions of Process.onExit and ProcessHandle.onExit |
|
41 |
* @author Roger Riggs |
|
42 |
*/ |
|
43 |
||
44 |
public class OnExitTest extends ProcessUtil { |
|
45 |
||
46 |
@SuppressWarnings("raw_types") |
|
47 |
public static void main(String[] args) { |
|
48 |
Class<?>[] testclass = { OnExitTest.class}; |
|
49 |
TestNG testng = new TestNG(); |
|
50 |
testng.setTestClasses(testclass); |
|
51 |
testng.run(); |
|
52 |
} |
|
53 |
||
54 |
/** |
|
55 |
* Basic test of exitValue and onExit. |
|
56 |
*/ |
|
57 |
@Test |
|
58 |
public static void test1() { |
|
59 |
try { |
|
60 |
int[] exitValues = {0, 1, 10}; |
|
61 |
for (int value : exitValues) { |
|
62 |
Process p = JavaChild.spawn("exit", Integer.toString(value)); |
|
63 |
CompletableFuture<Process> future = p.onExit(); |
|
64 |
future.thenAccept( (ph) -> { |
|
65 |
int actualExitValue = ph.exitValue(); |
|
66 |
printf(" javaChild done: %s, exitStatus: %d%n", |
|
67 |
ph, actualExitValue); |
|
68 |
Assert.assertEquals(actualExitValue, value, "actualExitValue incorrect"); |
|
69 |
Assert.assertEquals(ph, p, "Different Process passed to thenAccept"); |
|
70 |
}); |
|
71 |
||
72 |
Process h = future.get(); |
|
73 |
Assert.assertEquals(h, p); |
|
74 |
Assert.assertEquals(p.exitValue(), value); |
|
75 |
Assert.assertFalse(p.isAlive(), "Process should not be alive"); |
|
76 |
p.waitFor(); |
|
77 |
} |
|
78 |
} catch (IOException | InterruptedException | ExecutionException ex) { |
|
79 |
Assert.fail(ex.getMessage(), ex); |
|
80 |
} finally { |
|
81 |
destroyProcessTree(ProcessHandle.current()); |
|
82 |
} |
|
83 |
} |
|
84 |
||
85 |
/** |
|
86 |
* Test of Completion handler when parent is killed. |
|
87 |
* Spawn 1 child to spawn 3 children each with 2 children. |
|
88 |
*/ |
|
89 |
@Test |
|
90 |
public static void test2() { |
|
91 |
try { |
|
92 |
ConcurrentHashMap<ProcessHandle, ProcessHandle> processes = new ConcurrentHashMap<>(); |
|
93 |
List<ProcessHandle> children = getChildren(ProcessHandle.current()); |
|
94 |
children.forEach(ProcessUtil::printProcess); |
|
95 |
Assert.assertEquals(children.size(), 0, |
|
96 |
"Expected to start with zero children; " + children); |
|
97 |
||
98 |
JavaChild proc = JavaChild.spawnJavaChild("stdin"); |
|
99 |
ProcessHandle procHandle = proc.toHandle(); |
|
100 |
printf(" spawned: %d%n", proc.getPid()); |
|
101 |
||
102 |
proc.forEachOutputLine((s) -> { |
|
103 |
String[] split = s.trim().split(" "); |
|
104 |
if (split.length == 3 && split[1].equals("spawn")) { |
|
105 |
Long child = Long.valueOf(split[2]); |
|
106 |
Long parent = Long.valueOf(split[0].split(":")[0]); |
|
107 |
processes.put(ProcessHandle.of(child).get(), ProcessHandle.of(parent).get()); |
|
108 |
} |
|
109 |
}); |
|
110 |
||
111 |
proc.sendAction("spawn", "3", "stdin"); |
|
112 |
||
113 |
proc.sendAction("child", "spawn", "2", "stdin"); |
|
114 |
||
115 |
// Poll until all 9 child processes exist or the timeout is reached |
|
116 |
int expected = 9; |
|
117 |
Instant endTimeout = Instant.now().plusSeconds(10L); |
|
118 |
do { |
|
119 |
Thread.sleep(200L); |
|
120 |
printf(" subprocess count: %d, waiting for %d%n", processes.size(), expected); |
|
121 |
} while (processes.size() < expected && |
|
122 |
Instant.now().isBefore(endTimeout)); |
|
123 |
||
124 |
children = getAllChildren(procHandle); |
|
125 |
||
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
126 |
ConcurrentHashMap<ProcessHandle, CompletableFuture<ProcessHandle>> completions = |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
127 |
new ConcurrentHashMap<>(); |
30899 | 128 |
Instant startTime = Instant.now(); |
129 |
// Create a future for each of the 9 children |
|
130 |
processes.forEach( (p, parent) -> { |
|
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
131 |
CompletableFuture<ProcessHandle> cf = p.onExit().whenComplete((ph, ex) -> { |
30899 | 132 |
Duration elapsed = Duration.between(startTime, Instant.now()); |
133 |
printf("whenComplete: pid: %s, exception: %s, thread: %s, elapsed: %s%n", |
|
134 |
ph, ex, Thread.currentThread(), elapsed); |
|
135 |
}); |
|
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
136 |
completions.put(p, cf); |
30899 | 137 |
}); |
138 |
||
139 |
// Check that each of the spawned processes is included in the children |
|
140 |
List<ProcessHandle> remaining = new ArrayList<>(children); |
|
141 |
processes.forEach((p, parent) -> { |
|
142 |
Assert.assertTrue(remaining.remove(p), "spawned process should have been in children"); |
|
143 |
}); |
|
144 |
||
145 |
// Remove Win32 system spawned conhost.exe processes |
|
146 |
remaining.removeIf(ProcessUtil::isWindowsConsole); |
|
147 |
||
148 |
remaining.forEach(p -> printProcess(p, "unexpected: ")); |
|
149 |
if (remaining.size() > 0) { |
|
150 |
// Show full list for debugging |
|
151 |
ProcessUtil.logTaskList(); |
|
152 |
} |
|
153 |
||
154 |
proc.destroy(); // kill off the parent |
|
155 |
proc.waitFor(); |
|
156 |
||
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
157 |
// Wait for all the processes and corresponding onExit CF to be completed |
30899 | 158 |
processes.forEach((p, parent) -> { |
159 |
try { |
|
160 |
p.onExit().get(); |
|
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
161 |
completions.get(p).join(); |
30899 | 162 |
} catch (InterruptedException | ExecutionException ex) { |
163 |
// ignore |
|
164 |
} |
|
165 |
}); |
|
166 |
||
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
167 |
// Verify that all 9 exit handlers were called with the correct ProcessHandle |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
168 |
processes.forEach((p, parent) -> { |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
169 |
ProcessHandle value = completions.get(p).getNow(null); |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
170 |
Assert.assertEquals(p, value, "onExit.get value expected: " + p |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
171 |
+ ", actual: " + value |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
172 |
+ ": " + p.info()); |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
173 |
}); |
30899 | 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 |
} |