Skip to content

Commit

Permalink
Allow garbage collecting executed runners
Browse files Browse the repository at this point in the history
RunnerTestDescriptors can now be garbage collected after they have been
executed because they are now removed from the list of children of the
engine descriptor.
  • Loading branch information
marcphilipp committed Dec 30, 2020
1 parent 8169f92 commit 90dd6fd
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import static org.junit.platform.engine.TestExecutionResult.successful;
import static org.junit.vintage.engine.descriptor.VintageTestDescriptor.ENGINE_ID;

import java.util.Iterator;
import java.util.Optional;

import org.apiguardian.api.API;
Expand Down Expand Up @@ -68,19 +69,17 @@ public void execute(ExecutionRequest request) {
EngineExecutionListener engineExecutionListener = request.getEngineExecutionListener();
VintageEngineDescriptor engineDescriptor = (VintageEngineDescriptor) request.getRootTestDescriptor();
engineExecutionListener.executionStarted(engineDescriptor);
RunnerExecutor runnerExecutor = new RunnerExecutor(engineExecutionListener,
engineDescriptor.getTestSourceProvider());
executeAllChildren(runnerExecutor, engineDescriptor);
executeAllChildren(engineDescriptor, engineExecutionListener);
engineExecutionListener.executionFinished(engineDescriptor, successful());
}

private void executeAllChildren(RunnerExecutor runnerExecutor, TestDescriptor engineDescriptor) {
// @formatter:off
engineDescriptor.getChildren()
.stream()
.map(RunnerTestDescriptor.class::cast)
.forEach(runnerExecutor::execute);
// @formatter:on
private void executeAllChildren(VintageEngineDescriptor engineDescriptor,
EngineExecutionListener engineExecutionListener) {
RunnerExecutor runnerExecutor = new RunnerExecutor(engineExecutionListener);
for (Iterator<TestDescriptor> iterator = engineDescriptor.getModifiableChildren().iterator(); iterator.hasNext();) {
runnerExecutor.execute((RunnerTestDescriptor) iterator.next());
iterator.remove();
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@

import static org.apiguardian.api.API.Status.INTERNAL;

import java.util.Set;

import org.apiguardian.api.API;
import org.junit.platform.engine.TestDescriptor;
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.descriptor.EngineDescriptor;

Expand All @@ -22,15 +25,12 @@
@API(status = INTERNAL, since = "5.6")
public class VintageEngineDescriptor extends EngineDescriptor {

private final TestSourceProvider testSourceProvider;

public VintageEngineDescriptor(UniqueId uniqueId, TestSourceProvider testSourceProvider) {
public VintageEngineDescriptor(UniqueId uniqueId) {
super(uniqueId, "JUnit Vintage");
this.testSourceProvider = testSourceProvider;
}

public TestSourceProvider getTestSourceProvider() {
return testSourceProvider;
public Set<TestDescriptor> getModifiableChildren() {
return children;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ class RunnerTestDescriptorPostProcessor {

private final UniqueIdReader uniqueIdReader = new UniqueIdReader();
private final UniqueIdStringifier uniqueIdStringifier = new UniqueIdStringifier();
private final TestSourceProvider testSourceProvider;

public RunnerTestDescriptorPostProcessor(TestSourceProvider testSourceProvider) {
this.testSourceProvider = testSourceProvider;
}
private final TestSourceProvider testSourceProvider = new TestSourceProvider();

void applyFiltersAndCreateDescendants(RunnerTestDescriptor runnerTestDescriptor) {
addChildrenRecursively(runnerTestDescriptor);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import org.junit.platform.engine.UniqueId;
import org.junit.platform.engine.support.discovery.EngineDiscoveryRequestResolver;
import org.junit.vintage.engine.descriptor.RunnerTestDescriptor;
import org.junit.vintage.engine.descriptor.TestSourceProvider;
import org.junit.vintage.engine.descriptor.VintageEngineDescriptor;

/**
Expand All @@ -39,16 +38,13 @@ public class VintageDiscoverer {
// @formatter:on

public VintageEngineDescriptor discover(EngineDiscoveryRequest discoveryRequest, UniqueId uniqueId) {
TestSourceProvider testSourceProvider = new TestSourceProvider();
VintageEngineDescriptor engineDescriptor = new VintageEngineDescriptor(uniqueId, testSourceProvider);
VintageEngineDescriptor engineDescriptor = new VintageEngineDescriptor(uniqueId);
resolver.resolve(discoveryRequest, engineDescriptor);
RunnerTestDescriptorPostProcessor postProcessor = new RunnerTestDescriptorPostProcessor(testSourceProvider);
// @formatter:off
engineDescriptor.getChildren().stream()
.filter(RunnerTestDescriptor.class::isInstance)
.map(RunnerTestDescriptor.class::cast)
.forEach(postProcessor::applyFiltersAndCreateDescendants);
// @formatter:on
RunnerTestDescriptorPostProcessor postProcessor = new RunnerTestDescriptorPostProcessor();
for (TestDescriptor testDescriptor : engineDescriptor.getChildren()) {
RunnerTestDescriptor runnerTestDescriptor = (RunnerTestDescriptor) testDescriptor;
postProcessor.applyFiltersAndCreateDescendants(runnerTestDescriptor);
}
return engineDescriptor;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,10 @@
public class RunnerExecutor {

private final EngineExecutionListener engineExecutionListener;
private final TestSourceProvider testSourceProvider;
private final TestSourceProvider testSourceProvider = new TestSourceProvider();

public RunnerExecutor(EngineExecutionListener engineExecutionListener, TestSourceProvider testSourceProvider) {
public RunnerExecutor(EngineExecutionListener engineExecutionListener) {
this.engineExecutionListener = engineExecutionListener;
this.testSourceProvider = testSourceProvider;
}

public void execute(RunnerTestDescriptor runnerTestDescriptor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder;
import org.junit.vintage.engine.VintageUniqueIdBuilder;
import org.junit.vintage.engine.descriptor.RunnerTestDescriptor;
import org.junit.vintage.engine.descriptor.TestSourceProvider;
import org.junit.vintage.engine.samples.junit4.IgnoredJUnit4TestCase;
import org.junit.vintage.engine.samples.junit4.IgnoredJUnit4TestCaseWithNotFilterableRunner;
import org.junit.vintage.engine.samples.junit4.NotFilterableRunner;
Expand Down Expand Up @@ -75,8 +74,7 @@ private void resolve(DiscoverySelector selector) {
mock(LauncherDiscoveryListener.class)).build();
TestDescriptor engineDescriptor = new VintageDiscoverer().discover(request, VintageUniqueIdBuilder.engineId());
var runnerTestDescriptor = (RunnerTestDescriptor) getOnlyElement(engineDescriptor.getChildren());
new RunnerTestDescriptorPostProcessor(new TestSourceProvider()).applyFiltersAndCreateDescendants(
runnerTestDescriptor);
new RunnerTestDescriptorPostProcessor().applyFiltersAndCreateDescendants(runnerTestDescriptor);
}

}

0 comments on commit 90dd6fd

Please sign in to comment.