diff --git a/java/junit-runner/build.py b/java/junit-runner/build.py index ec6a47d81..64cd0e9b9 100644 --- a/java/junit-runner/build.py +++ b/java/junit-runner/build.py @@ -40,7 +40,7 @@ with tempfile.TemporaryDirectory() as scratch: sample = Path(scratch) / "sample" cp = os.pathsep.join(jars) subprocess.run([tool("javac"), "-encoding", "UTF-8", "-cp", cp, "-d", str(sample), - str(root / "tests" / "SampleTest.java"), str(root / "tests" / "Junit4SampleTest.java")], check=True) + *(str(root / "tests" / t) for t in ("SampleTest.java", "Junit4SampleTest.java", "ParallelSampleTest.java"))], check=True) run = subprocess.run([tool("java"), "-cp", os.pathsep.join([str(classes), str(sample), cp]), "ai.openidea.junit.Runner", "sample.SampleTest"], capture_output=True) out = run.stdout.decode("utf-8") @@ -66,6 +66,14 @@ with tempfile.TemporaryDirectory() as scratch: method = subprocess.run([tool("java"), "-cp", os.pathsep.join([str(classes), str(sample), cp]), "ai.openidea.junit.Runner", "sample.SampleTest#parameterized"], capture_output=True) assert method.stdout.decode().count("testStarted") == 2, method.stdout + # Tests JUnit runs in parallel report from several threads at once: none may be lost. + parallel = subprocess.run([tool("java"), "-Djunit.jupiter.execution.parallel.enabled=true", + "-Djunit.jupiter.execution.parallel.mode.default=concurrent", + "-cp", os.pathsep.join([str(classes), str(sample), cp]), + "ai.openidea.junit.Runner", "sample.ParallelSampleTest"], capture_output=True) + summary = parallel.stdout.decode().splitlines()[-1] + assert summary == "4000 tests completed, 2000 failed", summary + assert parallel.returncode == 1, parallel.returncode # JUnit 4 runs on JUnitCore, with the JUnit Platform on the classpath (no Vintage engine) and without it. junit4 = [j for j in jars if "/junit/junit/" in j or "hamcrest" in j] for classpath in (cp, os.pathsep.join(junit4)): diff --git a/java/junit-runner/openidea-junit-runner.jar b/java/junit-runner/openidea-junit-runner.jar index 1a18960b4..201d25952 100644 Binary files a/java/junit-runner/openidea-junit-runner.jar and b/java/junit-runner/openidea-junit-runner.jar differ diff --git a/java/junit-runner/src/ai/openidea/junit/PlatformRunner.java b/java/junit-runner/src/ai/openidea/junit/PlatformRunner.java index 1d4e2d845..aaa4c8bd8 100644 --- a/java/junit-runner/src/ai/openidea/junit/PlatformRunner.java +++ b/java/junit-runner/src/ai/openidea/junit/PlatformRunner.java @@ -9,10 +9,10 @@ import java.nio.charset.StandardCharsets; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Deque; -import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; import org.junit.platform.engine.DiscoverySelector; import org.junit.platform.engine.TestExecutionResult; import org.junit.platform.engine.TestSource; @@ -33,7 +33,8 @@ import org.junit.platform.launcher.core.LauncherFactory; */ final class PlatformRunner implements TestExecutionListener { private final Reporter report; - private final Map started = new HashMap<>(); + /** Written by the threads running the tests: JUnit may run them in parallel. */ + private final Map started = new ConcurrentHashMap<>(); /** The tests and classes each thread is running, innermost first: its output belongs to the first. */ private final ThreadLocal> owners = ThreadLocal.withInitial(ArrayDeque::new); private final List outputs = new ArrayList<>(); diff --git a/java/junit-runner/src/ai/openidea/junit/Reporter.java b/java/junit-runner/src/ai/openidea/junit/Reporter.java index 52b696ec3..ecdf0a047 100644 --- a/java/junit-runner/src/ai/openidea/junit/Reporter.java +++ b/java/junit-runner/src/ai/openidea/junit/Reporter.java @@ -6,7 +6,8 @@ import java.io.StringWriter; /** * Prints tests as the TeamCity service messages of OpenIDEA's Gradle test init script - * ({@code gradleTests.gradle}) and counts them for the summary line. + * ({@code gradleTests.gradle}) and counts them for the summary line. Parallel tests report from + * several threads at once. */ final class Reporter { private final PrintStream out; @@ -40,19 +41,19 @@ final class Reporter { } } - void failed(String own, Throwable error) { + synchronized void failed(String own, Throwable error) { failed++; StringWriter trace = new StringWriter(); if (error != null) error.printStackTrace(new PrintWriter(trace)); message("testFailed", own + attr("message", error == null ? "" : error.toString()) + attr("details", withoutRunner(trace.toString()))); } - void skipped(String own, String reason) { + synchronized void skipped(String own, String reason) { skipped++; message("testIgnored", own + (reason == null ? "" : attr("message", reason))); } - void finished(String own, long duration) { + synchronized void finished(String own, long duration) { tests++; message("testFinished", own + attr("duration", Long.toString(duration))); } @@ -64,7 +65,7 @@ final class Reporter { finished(own, 0); } - String summary() { + synchronized String summary() { StringBuilder summary = new StringBuilder().append(tests).append(tests == 1 ? " test" : " tests"); summary.append(" completed"); if (failed > 0) summary.append(", ").append(failed).append(" failed"); diff --git a/java/junit-runner/tests/ParallelSampleTest.java b/java/junit-runner/tests/ParallelSampleTest.java new file mode 100644 index 000000000..81e3474e7 --- /dev/null +++ b/java/junit-runner/tests/ParallelSampleTest.java @@ -0,0 +1,16 @@ +package sample; + +import static org.junit.jupiter.api.Assertions.fail; + +import org.junit.jupiter.api.RepeatedTest; + +/** Many tests finishing at once when JUnit runs them in parallel. */ +class ParallelSampleTest { + @RepeatedTest(2000) + void passes() {} + + @RepeatedTest(2000) + void fails() { + fail("no"); + } +}