From 90dd6fd28d27664b1d896e3b01655d84183ddc81 Mon Sep 17 00:00:00 2001 From: Marc Philipp Date: Fri, 11 Dec 2020 13:03:23 +0100 Subject: [PATCH] Allow garbage collecting executed runners 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. --- .../vintage/engine/VintageTestEngine.java | 19 +++++++++---------- .../descriptor/VintageEngineDescriptor.java | 12 ++++++------ .../RunnerTestDescriptorPostProcessor.java | 6 +----- .../engine/discovery/VintageDiscoverer.java | 16 ++++++---------- .../engine/execution/RunnerExecutor.java | 5 ++--- ...unnerTestDescriptorPostProcessorTests.java | 4 +--- 6 files changed, 25 insertions(+), 37 deletions(-) diff --git a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java index a4d8206ecaec..0b90e0d58ef8 100644 --- a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java +++ b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/VintageTestEngine.java @@ -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; @@ -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 iterator = engineDescriptor.getModifiableChildren().iterator(); iterator.hasNext();) { + runnerExecutor.execute((RunnerTestDescriptor) iterator.next()); + iterator.remove(); + } } } diff --git a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/descriptor/VintageEngineDescriptor.java b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/descriptor/VintageEngineDescriptor.java index 81b92ab251a5..23f09d7b0b14 100644 --- a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/descriptor/VintageEngineDescriptor.java +++ b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/descriptor/VintageEngineDescriptor.java @@ -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; @@ -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 getModifiableChildren() { + return children; } } diff --git a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/RunnerTestDescriptorPostProcessor.java b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/RunnerTestDescriptorPostProcessor.java index f1410fd13b2d..daa518e7f75a 100644 --- a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/RunnerTestDescriptorPostProcessor.java +++ b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/RunnerTestDescriptorPostProcessor.java @@ -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); diff --git a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/VintageDiscoverer.java b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/VintageDiscoverer.java index 5aa1136a8475..26422dec3484 100644 --- a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/VintageDiscoverer.java +++ b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/discovery/VintageDiscoverer.java @@ -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; /** @@ -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; } diff --git a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/execution/RunnerExecutor.java b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/execution/RunnerExecutor.java index daa638795f54..871a7f28383a 100644 --- a/junit-vintage-engine/src/main/java/org/junit/vintage/engine/execution/RunnerExecutor.java +++ b/junit-vintage-engine/src/main/java/org/junit/vintage/engine/execution/RunnerExecutor.java @@ -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) { diff --git a/junit-vintage-engine/src/test/java/org/junit/vintage/engine/discovery/RunnerTestDescriptorPostProcessorTests.java b/junit-vintage-engine/src/test/java/org/junit/vintage/engine/discovery/RunnerTestDescriptorPostProcessorTests.java index 1abeff30b9cc..22e841e6765a 100644 --- a/junit-vintage-engine/src/test/java/org/junit/vintage/engine/discovery/RunnerTestDescriptorPostProcessorTests.java +++ b/junit-vintage-engine/src/test/java/org/junit/vintage/engine/discovery/RunnerTestDescriptorPostProcessorTests.java @@ -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; @@ -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); } }