author | rriggs |
Thu, 07 Jan 2016 10:10:06 -0500 | |
changeset 34891 | dc0cab2e79cd |
parent 33648 | 9564031a20e0 |
child 36237 | 963d1115678b |
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.time.Duration; |
|
26 |
import java.time.Instant; |
|
27 |
import java.util.ArrayList; |
|
28 |
import java.util.List; |
|
29 |
import java.util.concurrent.CompletableFuture; |
|
30 |
import java.util.concurrent.ConcurrentHashMap; |
|
31 |
import java.util.concurrent.ExecutionException; |
|
31537
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
32 |
|
34891 | 33 |
import jdk.test.lib.Utils; |
31537
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
34 |
|
30899 | 35 |
import org.testng.annotations.Test; |
36 |
import org.testng.Assert; |
|
37 |
import org.testng.TestNG; |
|
38 |
||
39 |
/* |
|
40 |
* @test |
|
34891 | 41 |
* @library /test/lib/share/classes |
42 |
* @build jdk.test.lib.Platform jdk.test.lib.Utils |
|
43 |
* @run testng OnExitTest |
|
30899 | 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() { |
|
31537
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
95 |
ProcessHandle procHandle = null; |
30899 | 96 |
try { |
97 |
ConcurrentHashMap<ProcessHandle, ProcessHandle> processes = new ConcurrentHashMap<>(); |
|
98 |
List<ProcessHandle> children = getChildren(ProcessHandle.current()); |
|
99 |
children.forEach(ProcessUtil::printProcess); |
|
100 |
||
101 |
JavaChild proc = JavaChild.spawnJavaChild("stdin"); |
|
31537
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
102 |
procHandle = proc.toHandle(); |
30899 | 103 |
printf(" spawned: %d%n", proc.getPid()); |
104 |
||
105 |
proc.forEachOutputLine((s) -> { |
|
106 |
String[] split = s.trim().split(" "); |
|
107 |
if (split.length == 3 && split[1].equals("spawn")) { |
|
108 |
Long child = Long.valueOf(split[2]); |
|
109 |
Long parent = Long.valueOf(split[0].split(":")[0]); |
|
110 |
processes.put(ProcessHandle.of(child).get(), ProcessHandle.of(parent).get()); |
|
111 |
} |
|
112 |
}); |
|
113 |
||
114 |
proc.sendAction("spawn", "3", "stdin"); |
|
115 |
||
116 |
proc.sendAction("child", "spawn", "2", "stdin"); |
|
117 |
||
118 |
// Poll until all 9 child processes exist or the timeout is reached |
|
119 |
int expected = 9; |
|
31537
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
120 |
long timeout = Utils.adjustTimeout(10L); |
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
121 |
Instant endTimeout = Instant.now().plusSeconds(timeout); |
30899 | 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 |
||
32209
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31537
diff
changeset
|
128 |
if (processes.size() < expected) { |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31537
diff
changeset
|
129 |
printf("WARNING: not all children have been started. Can't complete test.%n"); |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31537
diff
changeset
|
130 |
printf(" You can try to increase the timeout or%n"); |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31537
diff
changeset
|
131 |
printf(" you can try to use a faster VM (i.e. not a debug version).%n"); |
24bb680a1609
8131168: Refactor ProcessHandleImpl_*.c and add implememtation for AIX
simonis
parents:
31537
diff
changeset
|
132 |
} |
33648
9564031a20e0
8138566: (Process) java.lang.Process.allChildren specification clarification
rriggs
parents:
32657
diff
changeset
|
133 |
children = getDescendants(procHandle); |
30899 | 134 |
|
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
135 |
ConcurrentHashMap<ProcessHandle, CompletableFuture<ProcessHandle>> completions = |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
136 |
new ConcurrentHashMap<>(); |
30899 | 137 |
Instant startTime = Instant.now(); |
138 |
// Create a future for each of the 9 children |
|
139 |
processes.forEach( (p, parent) -> { |
|
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
140 |
CompletableFuture<ProcessHandle> cf = p.onExit().whenComplete((ph, ex) -> { |
30899 | 141 |
Duration elapsed = Duration.between(startTime, Instant.now()); |
142 |
printf("whenComplete: pid: %s, exception: %s, thread: %s, elapsed: %s%n", |
|
143 |
ph, ex, Thread.currentThread(), elapsed); |
|
144 |
}); |
|
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
145 |
completions.put(p, cf); |
30899 | 146 |
}); |
147 |
||
148 |
// Check that each of the spawned processes is included in the children |
|
149 |
List<ProcessHandle> remaining = new ArrayList<>(children); |
|
150 |
processes.forEach((p, parent) -> { |
|
151 |
Assert.assertTrue(remaining.remove(p), "spawned process should have been in children"); |
|
152 |
}); |
|
153 |
||
154 |
// Remove Win32 system spawned conhost.exe processes |
|
155 |
remaining.removeIf(ProcessUtil::isWindowsConsole); |
|
156 |
||
157 |
remaining.forEach(p -> printProcess(p, "unexpected: ")); |
|
158 |
if (remaining.size() > 0) { |
|
159 |
// Show full list for debugging |
|
160 |
ProcessUtil.logTaskList(); |
|
161 |
} |
|
162 |
||
163 |
proc.destroy(); // kill off the parent |
|
164 |
proc.waitFor(); |
|
165 |
||
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
166 |
// Wait for all the processes and corresponding onExit CF to be completed |
30899 | 167 |
processes.forEach((p, parent) -> { |
168 |
try { |
|
169 |
p.onExit().get(); |
|
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
170 |
completions.get(p).join(); |
30899 | 171 |
} catch (InterruptedException | ExecutionException ex) { |
172 |
// ignore |
|
173 |
} |
|
174 |
}); |
|
175 |
||
31532
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
176 |
// 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
|
177 |
processes.forEach((p, parent) -> { |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
178 |
ProcessHandle value = completions.get(p).getNow(null); |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
179 |
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
|
180 |
+ ", actual: " + value |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
181 |
+ ": " + p.info()); |
c1e1214cebd3
8085981: java/lang/ProcessHandle/OnExitTest.java: AssertionError: Child onExit not called
rriggs
parents:
31182
diff
changeset
|
182 |
}); |
30899 | 183 |
|
184 |
// Show the status of the original children |
|
185 |
children.forEach(p -> printProcess(p, "after onExit:")); |
|
186 |
||
187 |
Assert.assertEquals(proc.isAlive(), false, "destroyed process is alive:: %s%n" + proc); |
|
188 |
} catch (IOException | InterruptedException ex) { |
|
189 |
Assert.fail(ex.getMessage()); |
|
190 |
} finally { |
|
31537
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
191 |
if (procHandle != null) { |
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
192 |
destroyProcessTree(procHandle); |
704fd18b5be1
8130296: [TESTBUG] java/lang/ProcessHandle/OnExitTest - Unaccounted for children expected [0] but found [1]
rriggs
parents:
31532
diff
changeset
|
193 |
} |
30899 | 194 |
} |
195 |
} |
|
196 |
||
197 |
} |