fix(run): count every test when JUnit runs them in parallel (#6377)

With JUnit's parallel concurrent mode, OpenIDEA's JUnit runner could report fewer tests than finished: the listener is called from several worker threads, Reporter's counters were plain ints incremented without a lock and PlatformRunner's start times a HashMap. The counting methods are synchronized now and the start times a ConcurrentHashMap. build.py --test runs a 4000-invocation parallel sample and expects the exact summary.

Closes #6334

Co-authored-by: audichuang <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-11-27 14:37:21 +08:00
1 джерело 2a26c82357
коміт e6cb7df653
5 files changed
+34 -8

No files matched your search

+9 -1
Переглянути файл
@@ -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)):
Бінарний файл не відображається.
+3 -2
Переглянути файл
@@ -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<String, Long> started = new HashMap<>();
/** Written by the threads running the tests: JUnit may run them in parallel. */
private final Map<String, Long> started = new ConcurrentHashMap<>();
/** The tests and classes each thread is running, innermost first: its output belongs to the first. */
private final ThreadLocal<Deque<String>> owners = ThreadLocal.withInitial(ArrayDeque::new);
private final List<Output> outputs = new ArrayList<>();
+6 -5
Переглянути файл
@@ -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");
+16
Переглянути файл
@@ -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");
}
}