Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Create a single LauncherSession for invocations of JUnitPlatformProvider#863

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Create a single LauncherSession for invocations of JUnitPlatformProvider
Prior to this commit, `JUnitPlatformProvider` created multiple`LauncherSessions` for some executions, for example when `forkTestSet`was `null` or when rerunning failed tests. This caused registered`LauncherSessionListeners` registered via `ServiceLoader` on the JUnitPlatform to be called multiple times. As they are intended to set up andtear down resources, this had the potential of unnecessarily slowingdown test execution.
  • Loading branch information
@marcphilipp
marcphilipp committedJul 3, 2025
commitcdf9d9909ab1d7d23008fb6ae67295a6c7ea0fd0
Original file line numberDiff line numberDiff line change
Expand Up@@ -37,7 +37,6 @@
import org.apache.maven.surefire.api.suite.RunResult;
import org.apache.maven.surefire.api.testset.TestSetFailedException;
import org.apache.maven.surefire.api.util.ScanResult;
import org.apache.maven.surefire.api.util.SurefireReflectionException;
import org.apache.maven.surefire.api.util.TestsToRun;
import org.apache.maven.surefire.shared.utils.StringUtils;
import org.junit.platform.engine.DiscoverySelector;
Expand DownExpand Up@@ -80,48 +79,47 @@ public class JUnitPlatformProvider extends AbstractProvider {

private final ProviderParameters parameters;

private finalLauncher launcher;
private finalLauncherSessionFactory launcherSessionFactory;

private final Filter<?>[] filters;

private final Map<String, String> configurationParameters;

public JUnitPlatformProvider(ProviderParameters parameters) {
this(parameters,new LazyLauncher());
this(parameters,LauncherSessionFactory.DEFAULT);
}

JUnitPlatformProvider(ProviderParameters parameters,Launcher launcher) {
JUnitPlatformProvider(ProviderParameters parameters,LauncherSessionFactory launcherSessionFactory) {
this.parameters = parameters;
this.launcher =launcher;
this.launcherSessionFactory =launcherSessionFactory;
filters = newFilters();
configurationParameters = newConfigurationParameters();
}

@Override
public Iterable<Class<?>> getSuites() {
try {
return scanClasspath();
} finally {
closeLauncher();
try (LauncherSessionAdapter launcherSession = launcherSessionFactory.openSession()) {
return scanClasspath(launcherSession.getLauncher());
}
}

@Override
public RunResult invoke(Object forkTestSet) throws TestSetFailedException, ReporterException {
ReporterFactory reporterFactory = parameters.getReporterFactory();
final RunResult runResult;
try {
try(LauncherSessionAdapter launcherSession = launcherSessionFactory.openSession()){
RunListenerAdapter adapter = new RunListenerAdapter(reporterFactory.createTestReportListener());
adapter.setRunMode(NORMAL_RUN);

startCapture(adapter);
setupJunitLogger();
Launcher launcher = launcherSession.getLauncher();
if (forkTestSet instanceof TestsToRun) {
invokeAllTests((TestsToRun) forkTestSet, adapter);
invokeAllTests(launcher,(TestsToRun) forkTestSet, adapter);
} else if (forkTestSet instanceof Class) {
invokeAllTests(fromClass((Class<?>) forkTestSet), adapter);
invokeAllTests(launcher,fromClass((Class<?>) forkTestSet), adapter);
} else if (forkTestSet == null) {
invokeAllTests(scanClasspath(), adapter);
invokeAllTests(launcher,scanClasspath(launcher), adapter);
} else {
throw new IllegalArgumentException("Unexpected value of forkTestSet: " + forkTestSet);
}
Expand All@@ -138,42 +136,36 @@ private static void setupJunitLogger() {
}
}

private TestsToRun scanClasspath() {
private TestsToRun scanClasspath(Launcher launcher) {
TestPlanScannerFilter filter = new TestPlanScannerFilter(launcher, filters);
ScanResult scanResult = parameters.getScanResult();
TestsToRun scannedClasses = scanResult.applyFilter(filter, parameters.getTestClassLoader());
return parameters.getRunOrderCalculator().orderTestClasses(scannedClasses);
}

private void invokeAllTests(TestsToRun testsToRun, RunListenerAdapter adapter) {
try {
execute(testsToRun, adapter);
} finally {
closeLauncher();
}
private void invokeAllTests(Launcher launcher, TestsToRun testsToRun, RunListenerAdapter adapter) {

execute(launcher, testsToRun, adapter);

// Rerun failing tests if requested
int count = parameters.getTestRequest().getRerunFailingTestsCount();
if (count > 0 && adapter.hasFailingTests()) {
adapter.setRunMode(RERUN_TEST_AFTER_FAILURE);
for (int i = 0; i < count; i++) {
try {
// Replace the "discoveryRequest" so that it only specifies the failing tests
LauncherDiscoveryRequest discoveryRequest = buildLauncherDiscoveryRequestForRerunFailures(adapter);
// Reset adapter's recorded failures and invoke the failed tests again
adapter.reset();
launcher.execute(discoveryRequest, adapter);
// If no tests fail in the rerun, we're done
if (!adapter.hasFailingTests()) {
break;
}
} finally {
closeLauncher();
// Replace the "discoveryRequest" so that it only specifies the failing tests
LauncherDiscoveryRequest discoveryRequest = buildLauncherDiscoveryRequestForRerunFailures(adapter);
// Reset adapter's recorded failures and invoke the failed tests again
adapter.reset();
launcher.execute(discoveryRequest, adapter);
// If no tests fail in the rerun, we're done
if (!adapter.hasFailingTests()) {
break;
}
}
}
}

private void execute(TestsToRun testsToRun, RunListenerAdapter adapter) {
private void execute(Launcher launcher,TestsToRun testsToRun, RunListenerAdapter adapter) {
// parameters.getProviderProperties().get(CONFIGURATION_PARAMETERS)
// add this LegacyXmlReportGeneratingListener ?
// adapter,
Expand DownExpand Up@@ -203,16 +195,6 @@ private void execute(TestsToRun testsToRun, RunListenerAdapter adapter) {
}
}

private void closeLauncher() {
if (launcher instanceof AutoCloseable) {
try {
((AutoCloseable) launcher).close();
} catch (Exception e) {
throw new SurefireReflectionException(e);
}
}
}

private LauncherDiscoveryRequest buildLauncherDiscoveryRequestForRerunFailures(RunListenerAdapter adapter) {
LauncherDiscoveryRequestBuilder builder =
request().filters(filters).configurationParameters(configurationParameters);
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.surefire.junitplatform;

import org.junit.platform.launcher.Launcher;

/**
* Launcher proxy which delays the most possible the initialization of the real JUnit
* Launcher in order to avoid stream/stdout corruption due to early logging.
*/
interface LauncherSessionAdapter extends AutoCloseable {

Launcher getLauncher();

@Override
void close();
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.surefire.junitplatform;

import org.apache.maven.surefire.api.util.ReflectionUtils;
import org.apache.maven.surefire.api.util.SurefireReflectionException;
import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.core.LauncherFactory;

interface LauncherSessionFactory {

LauncherSessionFactory DEFAULT = () -> {
try {
Class<?> sessionClass = Class.forName("org.junit.platform.launcher.LauncherSession");
AutoCloseable launcherSession = ReflectionUtils.invokeGetter(LauncherFactory.class, null, "openSession");
return new SupportedLauncherSessionAdapter(sessionClass, launcherSession);
} catch (ClassNotFoundException e) {
return new LegacyLauncherSessionAdapter(LauncherFactory.create());
}
};

LauncherSessionAdapter openSession();

class SupportedLauncherSessionAdapter implements LauncherSessionAdapter {

private final Class<?> sessionClass;
private final AutoCloseable launcherSession;

SupportedLauncherSessionAdapter(Class<?> sessionClass, AutoCloseable launcherSession) {
this.sessionClass = sessionClass;
this.launcherSession = launcherSession;
}

@Override
public Launcher getLauncher() {
return ReflectionUtils.invokeGetter(sessionClass, launcherSession, "getLauncher");
}

@Override
public void close() {
try {
launcherSession.close();
} catch (Exception e) {
throw new SurefireReflectionException(e);
}
}
}

class LegacyLauncherSessionAdapter implements LauncherSessionAdapter {

private final Launcher launcher;

LegacyLauncherSessionAdapter(Launcher launcher) {
this.launcher = launcher;
}

@Override
public Launcher getLauncher() {
return launcher;
}

@Override
public void close() {
// do nothing
}
}
}

This file was deleted.

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.surefire.junitplatform;

import org.junit.platform.launcher.Launcher;
import org.junit.platform.launcher.LauncherSession;
import org.junit.platform.launcher.TestExecutionListener;
import org.junit.platform.launcher.core.LauncherConfig;
import org.junit.platform.launcher.core.LauncherFactory;

class ConcreteLauncherSessionAdapter implements LauncherSessionAdapter {

private final LauncherSession launcherSession;

public static LauncherSessionFactory createLauncherSessionWithListeners(TestExecutionListener... listeners) {
LauncherConfig launcherConfig =
LauncherConfig.builder().addTestExecutionListeners(listeners).build();
return () -> new ConcreteLauncherSessionAdapter(LauncherFactory.openSession(launcherConfig));
}

ConcreteLauncherSessionAdapter(LauncherSession launcherSession) {
this.launcherSession = launcherSession;
}

@Override
public Launcher getLauncher() {
return launcherSession.getLauncher();
}

@Override
public void close() {
launcherSession.close();
}
}
Loading
Loading

[8]ページ先頭

©2009-2025 Movatter.jp