Skip to content

Commit 57f452f

Browse files
committed
Disable ReactorResourceFactory use of global resources in tests
Add `ContextCustomizerFactory` to automatically disable the use of `ReactorResourceFactory` global resources in tests. Fixes gh-38199
1 parent b8927eb commit 57f452f

File tree

5 files changed

+190
-1
lines changed

5 files changed

+190
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2024 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+
* https://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.boot.test.web.reactor.netty;
18+
19+
import org.springframework.beans.BeansException;
20+
import org.springframework.beans.factory.config.BeanPostProcessor;
21+
import org.springframework.http.client.ReactorResourceFactory;
22+
23+
/**
24+
* {@link BeanPostProcessor} to disable the use of global resources in
25+
* {@link ReactorResourceFactory} preventing test cleanup issues.
26+
*
27+
* @author Phillip Webb
28+
*/
29+
class DisableReactorResourceFactoryGlobalResourcesBeanPostProcessor implements BeanPostProcessor {
30+
31+
@Override
32+
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
33+
if (bean instanceof ReactorResourceFactory reactorResourceFactory) {
34+
reactorResourceFactory.setUseGlobalResources(false);
35+
}
36+
return bean;
37+
}
38+
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2012-2024 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+
* https://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.boot.test.web.reactor.netty;
18+
19+
import java.util.List;
20+
21+
import org.springframework.context.ConfigurableApplicationContext;
22+
import org.springframework.http.client.ReactorResourceFactory;
23+
import org.springframework.test.context.ContextConfigurationAttributes;
24+
import org.springframework.test.context.ContextCustomizer;
25+
import org.springframework.test.context.ContextCustomizerFactory;
26+
import org.springframework.test.context.MergedContextConfiguration;
27+
import org.springframework.util.ClassUtils;
28+
29+
/**
30+
* {@link ContextCustomizerFactory} to disable the use of global resources in
31+
* {@link ReactorResourceFactory} preventing test cleanup issues.
32+
*
33+
* @author Phillip Webb
34+
*/
35+
class DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory implements ContextCustomizerFactory {
36+
37+
String REACTOR_RESOURCE_FACTORY_CLASS = "org.springframework.http.client.ReactorResourceFactory";
38+
39+
@Override
40+
public ContextCustomizer createContextCustomizer(Class<?> testClass,
41+
List<ContextConfigurationAttributes> configAttributes) {
42+
if (ClassUtils.isPresent(this.REACTOR_RESOURCE_FACTORY_CLASS, testClass.getClassLoader())) {
43+
return new DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer();
44+
}
45+
return null;
46+
47+
}
48+
49+
static final class DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer
50+
implements ContextCustomizer {
51+
52+
private DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer() {
53+
}
54+
55+
@Override
56+
public void customizeContext(ConfigurableApplicationContext context, MergedContextConfiguration mergedConfig) {
57+
context.getBeanFactory()
58+
.registerSingleton(DisableReactorResourceFactoryGlobalResourcesBeanPostProcessor.class.getName(),
59+
new DisableReactorResourceFactoryGlobalResourcesBeanPostProcessor());
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
return (obj instanceof DisableReactorResourceFactoryGlobalResourcesContextCustomizerCustomizer);
65+
}
66+
67+
@Override
68+
public int hashCode() {
69+
return getClass().hashCode();
70+
}
71+
72+
}
73+
74+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright 2012-2024 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+
* https://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+
/**
18+
* Spring Boot support for testing Reactor Netty.
19+
*/
20+
package org.springframework.boot.test.web.reactor.netty;

spring-boot-project/spring-boot-test/src/main/resources/META-INF/spring.factories

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ org.springframework.boot.test.graphql.tester.HttpGraphQlTesterContextCustomizerF
66
org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory,\
77
org.springframework.boot.test.mock.mockito.MockitoContextCustomizerFactory,\
88
org.springframework.boot.test.web.client.TestRestTemplateContextCustomizerFactory,\
9-
org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizerFactory
9+
org.springframework.boot.test.web.reactive.server.WebTestClientContextCustomizerFactory,\
10+
org.springframework.boot.test.web.reactor.netty.DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory
1011

1112
# Test Execution Listeners
1213
org.springframework.test.context.TestExecutionListener=\
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2024 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+
* https://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.boot.test.web.reactor.netty;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.beans.factory.annotation.Autowired;
22+
import org.springframework.context.annotation.Bean;
23+
import org.springframework.context.annotation.Configuration;
24+
import org.springframework.http.client.ReactorResourceFactory;
25+
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
26+
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
/**
30+
* Tests for {@link DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactory}.
31+
*
32+
* @author Phillip Webb
33+
*/
34+
@SpringJUnitConfig
35+
class DisableReactorResourceFactoryGlobalResourcesContextCustomizerFactoryTests {
36+
37+
@Autowired
38+
private ReactorResourceFactory reactorResourceFactory;
39+
40+
@Test
41+
void disablesUseGlobalResources() {
42+
assertThat(this.reactorResourceFactory.isUseGlobalResources()).isFalse();
43+
}
44+
45+
@Configuration
46+
static class Config {
47+
48+
@Bean
49+
ReactorResourceFactory reactorResourceFactory() {
50+
return new ReactorResourceFactory();
51+
}
52+
53+
}
54+
55+
}

0 commit comments

Comments
 (0)