aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/java_tools
diff options
context:
space:
mode:
authorGravatar Kush Chakraborty <kush@google.com>2017-03-23 22:51:54 +0000
committerGravatar Yue Gan <yueg@google.com>2017-03-24 12:19:19 +0000
commit4443123db935fafd6f51f9c1f03bba1bc6538a22 (patch)
tree4a6b193856f1917a3195f2f189acc9e904d07180 /src/java_tools
parentea862bac70646d519388955a71d6189a0f01459e (diff)
2nd attempt to split classpaths, and load test classes in a separate classloader. Currently this funcionality is hidden behind the "experimental_testrunner" flag.
Original description (from commit 786cfa2ed980e278c42ee474408844f7e3720385): Separate the classpaths of the TestRunner with the test target, and use a separate Classloader to load the test target's classes. This enables a clean separation of the classes of the TestRunner with the target under test. This is achieved with the following steps: 1. Start the test runner with only the bare bones classpaths to the Test Runner's classes which are used by the system ClassLoader. 2. Have all the classpaths required to load the test target's classes in a TEST_TARGET_CLASSPATH environment variable exported by the stub script. 3. Use a new classloader to load all the test target's classes using the paths in TEST_TARGET_CLASSPATH. This additionally enables the persistent test runner (currently experimental), to reload all the target's classes for every subsequent test run, so it can pick up any changes to the classes in between runs. The persistent test runner can be used by adding the argument --test_strategy=experimental_worker to the bazel test command (and having the tag "experimental_testrunner" in the java_test rule). Tested this against: 1. gerrit/gerrit-common:client_tests: Dismal avg. improvement of 580ms to 557ms (just 23ms) 2. intellij/intellij/base:unit_tests: Somewhat modest avg. improvement 1661ms to 913ms (748 ms) -- PiperOrigin-RevId: 151065529 MOS_MIGRATED_REVID=151065529
Diffstat (limited to 'src/java_tools')
-rw-r--r--src/java_tools/junitrunner/java/com/google/testing/junit/runner/ExperimentalTestRunner.java91
1 files changed, 76 insertions, 15 deletions
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/ExperimentalTestRunner.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/ExperimentalTestRunner.java
index e5455259be..a2eb17b9cd 100644
--- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/ExperimentalTestRunner.java
+++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/ExperimentalTestRunner.java
@@ -23,14 +23,16 @@ import com.google.testing.junit.runner.junit4.JUnit4Runner;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.TimeUnit;
/**
- * For now the same as {@link BazelTestRunner} but intended to be a testbed to try out new features,
- * without breeaking existing tests.
+ * Like {@link BazelTestRunner} but runs the tests in their own classloader.
*/
public class ExperimentalTestRunner {
/**
@@ -39,6 +41,9 @@ public class ExperimentalTestRunner {
*/
static final String TEST_SUITE_PROPERTY_NAME = "bazel.test_suite";
+ private static URL[] classpaths = null;
+ private static URLClassLoader targetClassLoader;
+
private ExperimentalTestRunner() {
// utility class; should not be instantiated
}
@@ -118,7 +123,7 @@ public class ExperimentalTestRunner {
}
private static int runTestsInSuite(String suiteClassName, String[] args) {
- Class<?> suite = getTestClass(suiteClassName);
+ Class<?> suite = getTargetSuiteClass(suiteClassName);
if (suite == null) {
// No class found corresponding to the system property passed in from Bazel
@@ -128,16 +133,33 @@ public class ExperimentalTestRunner {
}
}
- // TODO(kush): Use a new classloader for the following instantiation.
JUnit4Runner runner =
JUnit4Bazel.builder()
.suiteClass(new SuiteClass(suite))
.config(new Config(args))
.build()
.runner();
- return runner.run().wasSuccessful() ? 0 : 1;
+
+ // Some frameworks such as Mockito use the Thread's context classloader.
+ Thread.currentThread().setContextClassLoader(targetClassLoader);
+
+ int result = 1;
+ try {
+ result = runner.run().wasSuccessful() ? 0 : 1;
+ } catch (RuntimeException e) {
+ System.err.println("Test run failed with exception");
+ e.printStackTrace();
+ }
+ return result;
}
+ /**
+ * Run in a loop awaiting instructions for the next test run.
+ *
+ * @param suiteClassName name of the class which is passed on to JUnit to determine the test suite
+ * @return 0 when we encounter an EOF from input, or non-zero values if we encounter an
+ * unrecoverable error.
+ */
private static int runPersistentTestRunner(String suiteClassName) {
PrintStream originalStdOut = System.out;
PrintStream originalStdErr = System.err;
@@ -149,11 +171,10 @@ public class ExperimentalTestRunner {
if (request == null) {
break;
}
- ByteArrayOutputStream baos = new ByteArrayOutputStream();
- PrintStream ps = new PrintStream(baos, true);
- System.setOut(ps);
- System.setErr(ps);
-
+ ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+ PrintStream printStream = new PrintStream(outputStream, true);
+ System.setOut(printStream);
+ System.setErr(printStream);
String[] arguments = request.getArgumentsList().toArray(new String[0]);
int exitCode = -1;
try {
@@ -164,7 +185,11 @@ public class ExperimentalTestRunner {
}
WorkResponse response =
- WorkResponse.newBuilder().setOutput(baos.toString()).setExitCode(exitCode).build();
+ WorkResponse
+ .newBuilder()
+ .setOutput(outputStream.toString())
+ .setExitCode(exitCode)
+ .build();
response.writeDelimitedTo(System.out);
System.out.flush();
@@ -176,19 +201,55 @@ public class ExperimentalTestRunner {
return 0;
}
- private static Class<?> getTestClass(String name) {
- if (name == null) {
+ /**
+ * Get the actual Test Suite class corresponding to the given name.
+ */
+ private static Class<?> getTargetSuiteClass(String suiteClassName) {
+ if (suiteClassName == null) {
return null;
}
try {
- return Class.forName(name);
- } catch (ClassNotFoundException e) {
+ targetClassLoader = new URLClassLoader(getClasspaths());
+ Class<?> targetSuiteClass = targetClassLoader.loadClass(suiteClassName);
+ System.out.printf(
+ "Running test suites for class: %s, created by classLoader: %s%n",
+ targetSuiteClass, targetSuiteClass.getClassLoader());
+ return targetSuiteClass;
+ } catch (ClassNotFoundException | MalformedURLException e) {
+ System.err.println("Exception in loading class:" + e.getMessage());
return null;
}
}
/**
+ * Used to get the classpaths which should be used to load the classes of the test target.
+ *
+ * @throws MalformedURLException when we are unable to create a given classpath.
+ * @return array of URLs containing the classpaths or null if classpaths could not be located.
+ */
+ private static URL[] getClasspaths() throws MalformedURLException {
+ // TODO(kush): WARNING THIS DOES NOT RELOAD CLASSPATHS FOR EVERY TEST RUN. b/34712039
+ if (classpaths != null) {
+ return classpaths;
+ }
+ String testTargetsClaspaths = System.getenv("TEST_TARGET_CLASSPATH");
+ if (testTargetsClaspaths == null || testTargetsClaspaths.isEmpty()) {
+ throw new IllegalStateException(
+ "Target's classpath not present in TEST_TARGET_CLASSPATH environment variable");
+ }
+
+ String[] targetClassPaths = testTargetsClaspaths.split(":");
+
+ classpaths = new URL[targetClassPaths.length];
+ String workingDir = System.getProperty("user.dir");
+ for (int index = 0; index < targetClassPaths.length; index++) {
+ classpaths[index] = new URL("file://" + workingDir + "/" + targetClassPaths[index]);
+ }
+ return classpaths;
+ }
+
+ /**
* Prints out stack traces if the JVM does not exit quickly. This can help detect shutdown hooks
* that are preventing the JVM from exiting quickly.
*