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

+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");