diff options
author | 2016-12-08 19:53:41 +0000 | |
---|---|---|
committer | 2016-12-09 15:32:55 +0000 | |
commit | 25120df651ae7d0ac9e85cbbea54921d1f2c4ae4 (patch) | |
tree | f0938c8db71c6e4b2684befdb88fa6023cc85155 /src/java_tools/junitrunner/java/com/google/testing/junit/runner | |
parent | 38edc5dc9d1fd58ffcb3223a1c194e04eb066437 (diff) |
Initial code for Persistent Java Test Runner.
At this point this does nothing more than re-run the exact same test without having to re-start the test runner.
In future iterations the aim is to be able to re-run tests with modified code, without having to re-start the test runner.
To test out the WorkerTestStrategy simply use --test_strategy=experimental_worker for a test with bazel.
--
PiperOrigin-RevId: 141465929
MOS_MIGRATED_REVID=141465929
Diffstat (limited to 'src/java_tools/junitrunner/java/com/google/testing/junit/runner')
-rw-r--r-- | src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD | 1 | ||||
-rw-r--r-- | src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java | 53 |
2 files changed, 53 insertions, 1 deletions
diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD index 1699cb882b..71a715a85a 100644 --- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD +++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BUILD @@ -17,6 +17,7 @@ java_library( "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/model", "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/sharding", "//src/java_tools/junitrunner/java/com/google/testing/junit/runner/util", + "//src/main/protobuf:worker_protocol_java_proto", "//third_party:junit4", ], ) diff --git a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java index 6e9cf02188..7e3984138e 100644 --- a/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java +++ b/src/java_tools/junitrunner/java/com/google/testing/junit/runner/BazelTestRunner.java @@ -14,12 +14,16 @@ package com.google.testing.junit.runner; +import com.google.devtools.build.lib.worker.WorkerProtocol.WorkRequest; +import com.google.devtools.build.lib.worker.WorkerProtocol.WorkResponse; import com.google.testing.junit.runner.internal.StackTraces; import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.Config; import com.google.testing.junit.runner.junit4.JUnit4InstanceModules.SuiteClass; import com.google.testing.junit.runner.junit4.JUnit4Runner; import com.google.testing.junit.runner.model.AntXmlResultWriter; import com.google.testing.junit.runner.model.XmlResultWriter; +import java.io.ByteArrayOutputStream; +import java.io.IOException; import java.io.PrintStream; import java.text.DateFormat; import java.text.SimpleDateFormat; @@ -67,11 +71,15 @@ public class BazelTestRunner { * <li>All tests pass: exit code of 0</li> * </ul> */ - public static void main(String args[]) { + public static void main(String[] args) { PrintStream stderr = System.err; String suiteClassName = System.getProperty(TEST_SUITE_PROPERTY_NAME); + if (args.length >= 1 && args[args.length - 1].equals("--persistent_test_runner")) { + System.exit(runPersistentTestRunner(suiteClassName)); + } + if (!checkTestSuiteProperty(suiteClassName)) { System.exit(2); } @@ -131,6 +139,7 @@ public class BazelTestRunner { } } + // TODO(kush): Use a new classloader for the following instantiation. JUnit4Runner runner = JUnit4Bazel.builder() .suiteClass(new SuiteClass(suite)) @@ -140,6 +149,48 @@ public class BazelTestRunner { return runner.run().wasSuccessful() ? 0 : 1; } + private static int runPersistentTestRunner(String suiteClassName) { + PrintStream originalStdOut = System.out; + PrintStream originalStdErr = System.err; + + while (true) { + try { + WorkRequest request = WorkRequest.parseDelimitedFrom(System.in); + + if (request == null) { + break; + } + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos, true); + System.setOut(ps); + System.setErr(ps); + + String[] arguments = request.getArgumentsList().toArray(new String[0]); + int exitCode = -1; + try { + exitCode = runTestsInSuite(suiteClassName, arguments); + } finally { + System.setOut(originalStdOut); + System.setErr(originalStdErr); + } + + WorkResponse response = + WorkResponse + .newBuilder() + .setOutput(baos.toString()) + .setExitCode(exitCode) + .build(); + response.writeDelimitedTo(System.out); + System.out.flush(); + + } catch (IOException e) { + e.printStackTrace(); + return 1; + } + } + return 0; + } + private static Class<?> getTestClass(String name) { if (name == null) { return null; |