Skip to content

Commit aa7f69a

Browse files
committed
Ensure that parameter resolution in SpringExtension is thread-safe
Prior to this commit, parallel execution of @beforeeach and @AfterEach methods that accepted @Autowired arguments would fail intermittently due to a race condition in the internal implementation of the JDK's java.lang.reflect.Executable.getParameters() method. This commit addresses this issue by creating instances of SynthesizingMethodParameter via SynthesizingMethodParameter.forExecutable(Executable, int) instead of SynthesizingMethodParameter.forParameter(Parameter), since the latter looks up the parameter index by iterating over the array returned by Executable.getParameters() (which is not thread-safe). Issue: SPR-17533
1 parent 91de8d2 commit aa7f69a

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

spring-test/src/main/java/org/springframework/test/context/junit/jupiter/ParameterAutowireUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ static Object resolveDependency(
118118
Autowired autowired = AnnotatedElementUtils.findMergedAnnotation(annotatedParameter, Autowired.class);
119119
boolean required = (autowired == null || autowired.required());
120120

121-
MethodParameter methodParameter = SynthesizingMethodParameter.forParameter(parameter);
121+
MethodParameter methodParameter = SynthesizingMethodParameter.forExecutable(
122+
parameter.getDeclaringExecutable(), parameterIndex);
122123
DependencyDescriptor descriptor = new DependencyDescriptor(methodParameter, required);
123124
descriptor.setContainingClass(containingClass);
124125
return applicationContext.getAutowireCapableBeanFactory().resolveDependency(descriptor, null);

spring-test/src/test/java/org/springframework/test/context/junit/SpringJUnitJupiterTestSuite.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import org.junit.platform.runner.JUnitPlatform;
2020
import org.junit.platform.suite.api.ExcludeTags;
21+
import org.junit.platform.suite.api.IncludeClassNamePatterns;
2122
import org.junit.platform.suite.api.IncludeEngines;
2223
import org.junit.platform.suite.api.SelectPackages;
2324
import org.junit.platform.suite.api.UseTechnicalNames;
@@ -48,6 +49,7 @@
4849
@RunWith(JUnitPlatform.class)
4950
@IncludeEngines("junit-jupiter")
5051
@SelectPackages("org.springframework.test.context.junit.jupiter")
52+
@IncludeClassNamePatterns(".*Tests$")
5153
@ExcludeTags("failing-test-case")
5254
@UseTechnicalNames
5355
public class SpringJUnitJupiterTestSuite {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.junit.jupiter.parallel;
18+
19+
import java.lang.reflect.Parameter;
20+
21+
import org.junit.jupiter.api.AfterEach;
22+
import org.junit.jupiter.api.BeforeEach;
23+
import org.junit.jupiter.api.RepeatedTest;
24+
import org.junit.platform.launcher.Launcher;
25+
import org.junit.platform.launcher.LauncherDiscoveryRequest;
26+
import org.junit.platform.launcher.core.LauncherFactory;
27+
import org.junit.platform.launcher.listeners.SummaryGeneratingListener;
28+
29+
import org.springframework.beans.factory.annotation.Autowired;
30+
import org.springframework.context.ApplicationContext;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
33+
34+
import static org.junit.jupiter.api.Assertions.*;
35+
import static org.junit.jupiter.engine.Constants.*;
36+
import static org.junit.platform.engine.discovery.DiscoverySelectors.*;
37+
import static org.junit.platform.launcher.core.LauncherDiscoveryRequestBuilder.*;
38+
39+
/**
40+
* Integration tests which verify that {@code @BeforeEach} and {@code @AfterEach} methods
41+
* that accept {@code @Autowired} arguments can be executed in parallel without issues
42+
* regarding concurrent access to the {@linkplain Parameter parameters} of such methods.
43+
*
44+
* @author Sam Brannen
45+
* @since 5.1.3
46+
*/
47+
class ParallelExecutionSpringExtensionTests {
48+
49+
private static final int NUM_TESTS = 1000;
50+
51+
@RepeatedTest(10)
52+
void runTestsInParallel() {
53+
Launcher launcher = LauncherFactory.create();
54+
SummaryGeneratingListener listener = new SummaryGeneratingListener();
55+
launcher.registerTestExecutionListeners(listener);
56+
57+
LauncherDiscoveryRequest request = request()//
58+
.configurationParameter(PARALLEL_EXECUTION_ENABLED_PROPERTY_NAME, "true")//
59+
.configurationParameter(PARALLEL_CONFIG_DYNAMIC_FACTOR_PROPERTY_NAME, "10")//
60+
.selectors(selectClass(TestCase.class))//
61+
.build();
62+
63+
launcher.execute(request);
64+
65+
assertEquals(NUM_TESTS, listener.getSummary().getTestsSucceededCount(),
66+
"number of tests executed successfully");
67+
}
68+
69+
@SpringJUnitConfig
70+
static class TestCase {
71+
72+
@BeforeEach
73+
void beforeEach(@Autowired ApplicationContext context) {
74+
}
75+
76+
@RepeatedTest(NUM_TESTS)
77+
void repeatedTest(@Autowired ApplicationContext context) {
78+
}
79+
80+
@AfterEach
81+
void afterEach(@Autowired ApplicationContext context) {
82+
}
83+
84+
@Configuration
85+
static class Config {
86+
}
87+
}
88+
89+
}

0 commit comments

Comments
 (0)