Skip to content

Commit

Permalink
Merge pull request #1218 from Vampire/thread-safe-result-collectors
Browse files Browse the repository at this point in the history
Make the result collectors thread-safe
  • Loading branch information
hcoles committed May 30, 2023
2 parents 54e3760 + b71680b commit c189175
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -168,11 +169,11 @@ private static Container createNewContainer() {
return new UnContainer() {
@Override
public List<TestResult> execute(final TestUnit group) {
final List<TestResult> results = new ArrayList<>();
final Collection<TestResult> results = new ConcurrentLinkedDeque<>();
final ExitingResultCollector rc = new ExitingResultCollector(
new ConcreteResultCollector(results));
group.execute(rc);
return results;
return new ArrayList<>(results);
}
};
}
Expand Down
5 changes: 5 additions & 0 deletions pitest/src/main/java/org/pitest/testapi/ResultCollector.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
*/
package org.pitest.testapi;

/**
* All implementations of this interface should be thread-safe, as the methods can be
* called from various threads and also from parallel running tests, depending on the used
* test engine.
*/
public interface ResultCollector {

void notifyEnd(Description description, Throwable t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@
* <p>At least one of the {@code executionStarted} methods needs to be overwritten
* by subclasses as the default implementations call each other for backwards compatibility
* and thus would cause a stack overflow error if called.
*
* <p>All implementations of this interface should be thread-safe, as the methods can be
* called from various threads and also from parallel running tests, depending on the used
* test engine.
*/
public interface TestUnitExecutionListener {
default void executionStarted(Description description) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@
import org.pitest.testapi.Description;
import org.pitest.testapi.ResultCollector;

import java.util.concurrent.atomic.AtomicBoolean;

public class ExitingResultCollector implements ResultCollector {

private final ResultCollector child;
private boolean hadFailure = false;
private final AtomicBoolean hadFailure = new AtomicBoolean(false);

public ExitingResultCollector(final ResultCollector child) {
this.child = child;
Expand All @@ -38,14 +40,14 @@ public void notifyStart(final Description description) {

@Override
public boolean shouldExit() {
return this.hadFailure;
return this.hadFailure.get();
}

@Override
public void notifyEnd(final Description description, final Throwable t) {
this.child.notifyEnd(description, t);
if (t != null) {
this.hadFailure = true;
this.hadFailure.set(true);
}

}
Expand All @@ -55,4 +57,4 @@ public void notifyEnd(final Description description) {
this.child.notifyEnd(description);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public final class ConcreteResultCollector implements ResultCollector {

private final Collection<TestResult> feedback;

/**
* @param feedback the collection where to collect test results; this collection should be thread-safe
* as it can be called from various threads and also from parallel running tests,
* depending on the used test engine.
*/
public ConcreteResultCollector(final Collection<TestResult> feedback) {
this.feedback = feedback;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@
package org.pitest.testapi.execute.containers;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentLinkedDeque;

import org.pitest.testapi.TestResult;
import org.pitest.testapi.TestUnit;
Expand All @@ -26,10 +28,10 @@ public class UnContainer implements Container {

@Override
public List<TestResult> execute(final TestUnit group) {
final List<TestResult> results = new ArrayList<>(12);
final Collection<TestResult> results = new ConcurrentLinkedDeque<>();
final ConcreteResultCollector rc = new ConcreteResultCollector(results);
group.execute(rc);
return results;
return new ArrayList<>(results);
}

}

0 comments on commit c189175

Please sign in to comment.