Skip to content

Commit 0c89e49

Browse files
committed
[GR-53665] SubprocessTest improvements
PullRequest: graal/17605
2 parents 4431412 + dcd3909 commit 0c89e49

File tree

1 file changed

+51
-6
lines changed
  • compiler/src/jdk.internal.vm.compiler.test/src/org/graalvm/compiler/core/test

1 file changed

+51
-6
lines changed

compiler/src/jdk.internal.vm.compiler.test/src/org/graalvm/compiler/core/test/SubprocessTest.java

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424
*/
2525
package org.graalvm.compiler.core.test;
2626

27+
import static org.graalvm.compiler.test.SubprocessUtil.getProcessCommandLine;
2728
import static org.graalvm.compiler.test.SubprocessUtil.getVMCommandLine;
2829
import static org.graalvm.compiler.test.SubprocessUtil.java;
2930
import static org.graalvm.compiler.test.SubprocessUtil.withoutDebuggerArguments;
3031

3132
import java.io.IOException;
3233
import java.util.Arrays;
34+
import java.util.LinkedList;
3335
import java.util.List;
3436
import java.util.function.Predicate;
3537

@@ -38,6 +40,23 @@
3840
import org.junit.Assume;
3941
import org.junit.Before;
4042

43+
/**
44+
* Utility class for executing Graal compiler tests in a subprocess. This can be useful for tests
45+
* that need special VM arguments or that produce textual output or a special process termination
46+
* status that need to be analyzed. The class to be executed may be the current class or any other
47+
* unit test class.
48+
* <p/>
49+
* If the test class contains multiple {@code @Test} methods, they will all be executed in the
50+
* subprocess, except when using one of the methods that take a {@code testSelector} argument. All
51+
* methods in this class take a {@link Runnable} argument. If the test class is the same as the
52+
* calling class, this runnable defines the operation to be executed in the subprocess. If the test
53+
* class is not the same as the calling class, the runnable is irrelevant and can be a nop.
54+
* <p/>
55+
* The subprocess will inherit any {@code -JUnitVerbose} flag (typically set through
56+
* {@code mx unittest --verbose}) from the parent process. If this flag is set, the standard output
57+
* of the child process will be echoed to the standard output of the parent process. If the child
58+
* process terminates with an error, its standard output will always be printed.
59+
*/
4160
public abstract class SubprocessTest extends GraalCompilerTest {
4261

4362
@Before
@@ -54,16 +73,24 @@ public void checkJavaAgent() {
5473
* {@link Subprocess} instance describing the process after its successful termination.
5574
*/
5675
public SubprocessUtil.Subprocess launchSubprocess(Runnable runnable, String... args) throws InterruptedException, IOException {
57-
return launchSubprocess(null, true, getClass(), runnable, args);
76+
return launchSubprocess(null, true, getClass(), null, runnable, args);
5877
}
5978

6079
public static SubprocessUtil.Subprocess launchSubprocess(Class<? extends GraalCompilerTest> testClass, Runnable runnable, String... args) throws InterruptedException, IOException {
61-
return launchSubprocess(null, true, testClass, runnable, args);
80+
return launchSubprocess(null, true, testClass, null, runnable, args);
81+
}
82+
83+
public void launchSubprocess(String testSelector, Runnable runnable, String... args) throws InterruptedException, IOException {
84+
launchSubprocess(null, true, getClass(), testSelector, runnable, args);
85+
}
86+
87+
public static SubprocessUtil.Subprocess launchSubprocess(Predicate<List<String>> testPredicate, boolean expectNormalExit, Class<? extends GraalCompilerTest> testClass,
88+
Runnable runnable, String... args) throws InterruptedException, IOException {
89+
return launchSubprocess(testPredicate, expectNormalExit, testClass, null, runnable, args);
6290
}
6391

64-
public static SubprocessUtil.Subprocess launchSubprocess(Predicate<List<String>> testPredicate, boolean expectNormalExit, Class<? extends GraalCompilerTest> testClass, Runnable runnable,
65-
String... args)
66-
throws InterruptedException, IOException {
92+
public static SubprocessUtil.Subprocess launchSubprocess(Predicate<List<String>> testPredicate, boolean expectNormalExit, Class<? extends GraalCompilerTest> testClass, String testSelector,
93+
Runnable runnable, String... args) throws InterruptedException, IOException {
6794
String recursionPropName = testClass.getSimpleName() + ".Subprocess";
6895
if (Boolean.getBoolean(recursionPropName)) {
6996
runnable.run();
@@ -77,7 +104,18 @@ public static SubprocessUtil.Subprocess launchSubprocess(Predicate<List<String>>
77104
if (verbose) {
78105
System.err.println(String.join(" ", vmArgs));
79106
}
80-
SubprocessUtil.Subprocess proc = java(vmArgs, "com.oracle.mxtool.junit.MxJUnitWrapper", testClass.getName());
107+
List<String> mainClassAndArgs = new LinkedList<>();
108+
mainClassAndArgs.add("com.oracle.mxtool.junit.MxJUnitWrapper");
109+
String testName = testClass.getName();
110+
if (testSelector != null) {
111+
testName += "#" + testSelector;
112+
}
113+
mainClassAndArgs.add(testName);
114+
boolean junitVerbose = getProcessCommandLine().contains("-JUnitVerbose");
115+
if (junitVerbose) {
116+
mainClassAndArgs.add("-JUnitVerbose");
117+
}
118+
SubprocessUtil.Subprocess proc = java(vmArgs, mainClassAndArgs);
81119
if (testPredicate != null) {
82120
assertTrue(testPredicate.test(proc.output), proc.toString() + " produced unexpected output:\n\n" + String.join("\n", proc.output));
83121
}
@@ -91,6 +129,13 @@ public static SubprocessUtil.Subprocess launchSubprocess(Predicate<List<String>>
91129
} else {
92130
assertTrue(proc.exitCode != 0, proc.toString() + " produced normal exit code " + proc.exitCode + ", but expected abnormal exit.");
93131
}
132+
if (junitVerbose) {
133+
System.out.println("--- subprocess output:");
134+
for (String line : proc.output) {
135+
System.out.println(line);
136+
}
137+
System.out.println("--- end subprocess output");
138+
}
94139
return proc;
95140
}
96141
}

0 commit comments

Comments
 (0)