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

Commitbfa3cca

Browse files
authored
Create a single LauncherSession for invocations of JUnitPlatformProvider (#863)
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.
1 parent25eda40 commitbfa3cca

File tree

6 files changed

+270
-194
lines changed

6 files changed

+270
-194
lines changed

‎surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/JUnitPlatformProvider.java‎

Lines changed: 25 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
importorg.apache.maven.surefire.api.suite.RunResult;
3838
importorg.apache.maven.surefire.api.testset.TestSetFailedException;
3939
importorg.apache.maven.surefire.api.util.ScanResult;
40-
importorg.apache.maven.surefire.api.util.SurefireReflectionException;
4140
importorg.apache.maven.surefire.api.util.TestsToRun;
4241
importorg.apache.maven.surefire.shared.utils.StringUtils;
4342
importorg.junit.platform.engine.DiscoverySelector;
@@ -80,48 +79,47 @@ public class JUnitPlatformProvider extends AbstractProvider {
8079

8180
privatefinalProviderParametersparameters;
8281

83-
privatefinalLauncherlauncher;
82+
privatefinalLauncherSessionFactorylauncherSessionFactory;
8483

8584
privatefinalFilter<?>[]filters;
8685

8786
privatefinalMap<String,String>configurationParameters;
8887

8988
publicJUnitPlatformProvider(ProviderParametersparameters) {
90-
this(parameters,newLazyLauncher());
89+
this(parameters,LauncherSessionFactory.DEFAULT);
9190
}
9291

93-
JUnitPlatformProvider(ProviderParametersparameters,Launcherlauncher) {
92+
JUnitPlatformProvider(ProviderParametersparameters,LauncherSessionFactorylauncherSessionFactory) {
9493
this.parameters =parameters;
95-
this.launcher =launcher;
94+
this.launcherSessionFactory =launcherSessionFactory;
9695
filters =newFilters();
9796
configurationParameters =newConfigurationParameters();
9897
}
9998

10099
@Override
101100
publicIterable<Class<?>>getSuites() {
102-
try {
103-
returnscanClasspath();
104-
}finally {
105-
closeLauncher();
101+
try (LauncherSessionAdapterlauncherSession =launcherSessionFactory.openSession()) {
102+
returnscanClasspath(launcherSession.getLauncher());
106103
}
107104
}
108105

109106
@Override
110107
publicRunResultinvoke(ObjectforkTestSet)throwsTestSetFailedException,ReporterException {
111108
ReporterFactoryreporterFactory =parameters.getReporterFactory();
112109
finalRunResultrunResult;
113-
try {
110+
try(LauncherSessionAdapterlauncherSession =launcherSessionFactory.openSession()){
114111
RunListenerAdapteradapter =newRunListenerAdapter(reporterFactory.createTestReportListener());
115112
adapter.setRunMode(NORMAL_RUN);
116113

117114
startCapture(adapter);
118115
setupJunitLogger();
116+
Launcherlauncher =launcherSession.getLauncher();
119117
if (forkTestSetinstanceofTestsToRun) {
120-
invokeAllTests((TestsToRun)forkTestSet,adapter);
118+
invokeAllTests(launcher,(TestsToRun)forkTestSet,adapter);
121119
}elseif (forkTestSetinstanceofClass) {
122-
invokeAllTests(fromClass((Class<?>)forkTestSet),adapter);
120+
invokeAllTests(launcher,fromClass((Class<?>)forkTestSet),adapter);
123121
}elseif (forkTestSet ==null) {
124-
invokeAllTests(scanClasspath(),adapter);
122+
invokeAllTests(launcher,scanClasspath(launcher),adapter);
125123
}else {
126124
thrownewIllegalArgumentException("Unexpected value of forkTestSet: " +forkTestSet);
127125
}
@@ -138,42 +136,36 @@ private static void setupJunitLogger() {
138136
}
139137
}
140138

141-
privateTestsToRunscanClasspath() {
139+
privateTestsToRunscanClasspath(Launcherlauncher) {
142140
TestPlanScannerFilterfilter =newTestPlanScannerFilter(launcher,filters);
143141
ScanResultscanResult =parameters.getScanResult();
144142
TestsToRunscannedClasses =scanResult.applyFilter(filter,parameters.getTestClassLoader());
145143
returnparameters.getRunOrderCalculator().orderTestClasses(scannedClasses);
146144
}
147145

148-
privatevoidinvokeAllTests(TestsToRuntestsToRun,RunListenerAdapteradapter) {
149-
try {
150-
execute(testsToRun,adapter);
151-
}finally {
152-
closeLauncher();
153-
}
146+
privatevoidinvokeAllTests(Launcherlauncher,TestsToRuntestsToRun,RunListenerAdapteradapter) {
147+
148+
execute(launcher,testsToRun,adapter);
149+
154150
// Rerun failing tests if requested
155151
intcount =parameters.getTestRequest().getRerunFailingTestsCount();
156152
if (count >0 &&adapter.hasFailingTests()) {
157153
adapter.setRunMode(RERUN_TEST_AFTER_FAILURE);
158154
for (inti =0;i <count;i++) {
159-
try {
160-
// Replace the "discoveryRequest" so that it only specifies the failing tests
161-
LauncherDiscoveryRequestdiscoveryRequest =buildLauncherDiscoveryRequestForRerunFailures(adapter);
162-
// Reset adapter's recorded failures and invoke the failed tests again
163-
adapter.reset();
164-
launcher.execute(discoveryRequest,adapter);
165-
// If no tests fail in the rerun, we're done
166-
if (!adapter.hasFailingTests()) {
167-
break;
168-
}
169-
}finally {
170-
closeLauncher();
155+
// Replace the "discoveryRequest" so that it only specifies the failing tests
156+
LauncherDiscoveryRequestdiscoveryRequest =buildLauncherDiscoveryRequestForRerunFailures(adapter);
157+
// Reset adapter's recorded failures and invoke the failed tests again
158+
adapter.reset();
159+
launcher.execute(discoveryRequest,adapter);
160+
// If no tests fail in the rerun, we're done
161+
if (!adapter.hasFailingTests()) {
162+
break;
171163
}
172164
}
173165
}
174166
}
175167

176-
privatevoidexecute(TestsToRuntestsToRun,RunListenerAdapteradapter) {
168+
privatevoidexecute(Launcherlauncher,TestsToRuntestsToRun,RunListenerAdapteradapter) {
177169
// parameters.getProviderProperties().get(CONFIGURATION_PARAMETERS)
178170
// add this LegacyXmlReportGeneratingListener ?
179171
// adapter,
@@ -203,16 +195,6 @@ private void execute(TestsToRun testsToRun, RunListenerAdapter adapter) {
203195
}
204196
}
205197

206-
privatevoidcloseLauncher() {
207-
if (launcherinstanceofAutoCloseable) {
208-
try {
209-
((AutoCloseable)launcher).close();
210-
}catch (Exceptione) {
211-
thrownewSurefireReflectionException(e);
212-
}
213-
}
214-
}
215-
216198
privateLauncherDiscoveryRequestbuildLauncherDiscoveryRequestForRerunFailures(RunListenerAdapteradapter) {
217199
LauncherDiscoveryRequestBuilderbuilder =
218200
request().filters(filters).configurationParameters(configurationParameters);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
packageorg.apache.maven.surefire.junitplatform;
20+
21+
importorg.junit.platform.launcher.Launcher;
22+
23+
/**
24+
* Launcher proxy which delays the most possible the initialization of the real JUnit
25+
* Launcher in order to avoid stream/stdout corruption due to early logging.
26+
*/
27+
interfaceLauncherSessionAdapterextendsAutoCloseable {
28+
29+
LaunchergetLauncher();
30+
31+
@Override
32+
voidclose();
33+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
packageorg.apache.maven.surefire.junitplatform;
20+
21+
importorg.apache.maven.surefire.api.util.ReflectionUtils;
22+
importorg.apache.maven.surefire.api.util.SurefireReflectionException;
23+
importorg.junit.platform.launcher.Launcher;
24+
importorg.junit.platform.launcher.core.LauncherFactory;
25+
26+
interfaceLauncherSessionFactory {
27+
28+
LauncherSessionFactoryDEFAULT = () -> {
29+
try {
30+
Class<?>sessionClass =Class.forName("org.junit.platform.launcher.LauncherSession");
31+
AutoCloseablelauncherSession =ReflectionUtils.invokeGetter(LauncherFactory.class,null,"openSession");
32+
returnnewSupportedLauncherSessionAdapter(sessionClass,launcherSession);
33+
}catch (ClassNotFoundExceptione) {
34+
returnnewLegacyLauncherSessionAdapter(LauncherFactory.create());
35+
}
36+
};
37+
38+
LauncherSessionAdapteropenSession();
39+
40+
classSupportedLauncherSessionAdapterimplementsLauncherSessionAdapter {
41+
42+
privatefinalClass<?>sessionClass;
43+
privatefinalAutoCloseablelauncherSession;
44+
45+
SupportedLauncherSessionAdapter(Class<?>sessionClass,AutoCloseablelauncherSession) {
46+
this.sessionClass =sessionClass;
47+
this.launcherSession =launcherSession;
48+
}
49+
50+
@Override
51+
publicLaunchergetLauncher() {
52+
returnReflectionUtils.invokeGetter(sessionClass,launcherSession,"getLauncher");
53+
}
54+
55+
@Override
56+
publicvoidclose() {
57+
try {
58+
launcherSession.close();
59+
}catch (Exceptione) {
60+
thrownewSurefireReflectionException(e);
61+
}
62+
}
63+
}
64+
65+
classLegacyLauncherSessionAdapterimplementsLauncherSessionAdapter {
66+
67+
privatefinalLauncherlauncher;
68+
69+
LegacyLauncherSessionAdapter(Launcherlauncher) {
70+
this.launcher =launcher;
71+
}
72+
73+
@Override
74+
publicLaunchergetLauncher() {
75+
returnlauncher;
76+
}
77+
78+
@Override
79+
publicvoidclose() {
80+
// do nothing
81+
}
82+
}
83+
}

‎surefire-providers/surefire-junit-platform/src/main/java/org/apache/maven/surefire/junitplatform/LazyLauncher.java‎

Lines changed: 0 additions & 85 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
packageorg.apache.maven.surefire.junitplatform;
20+
21+
importorg.junit.platform.launcher.Launcher;
22+
importorg.junit.platform.launcher.LauncherSession;
23+
importorg.junit.platform.launcher.TestExecutionListener;
24+
importorg.junit.platform.launcher.core.LauncherConfig;
25+
importorg.junit.platform.launcher.core.LauncherFactory;
26+
27+
classConcreteLauncherSessionAdapterimplementsLauncherSessionAdapter {
28+
29+
privatefinalLauncherSessionlauncherSession;
30+
31+
publicstaticLauncherSessionFactorycreateLauncherSessionWithListeners(TestExecutionListener...listeners) {
32+
LauncherConfiglauncherConfig =
33+
LauncherConfig.builder().addTestExecutionListeners(listeners).build();
34+
return () ->newConcreteLauncherSessionAdapter(LauncherFactory.openSession(launcherConfig));
35+
}
36+
37+
ConcreteLauncherSessionAdapter(LauncherSessionlauncherSession) {
38+
this.launcherSession =launcherSession;
39+
}
40+
41+
@Override
42+
publicLaunchergetLauncher() {
43+
returnlauncherSession.getLauncher();
44+
}
45+
46+
@Override
47+
publicvoidclose() {
48+
launcherSession.close();
49+
}
50+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp