Skip to content

Commit 8c74b62

Browse files
committed
Polish "Prefer WebClient to RestTemplate for Zipkin's Sender"
See gh-32529
1 parent cd3b3d4 commit 8c74b62

File tree

1 file changed

+54
-12
lines changed

1 file changed

+54
-12
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/tracing/zipkin/ZipkinConfigurationsSenderConfigurationTests.java

+54-12
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.test.context.FilteredClassLoader;
2626
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2727
import org.springframework.boot.test.context.runner.ReactiveWebApplicationContextRunner;
28+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
2829
import org.springframework.boot.web.client.RestTemplateBuilder;
2930
import org.springframework.context.annotation.Bean;
3031
import org.springframework.context.annotation.Configuration;
@@ -46,6 +47,9 @@ class ZipkinConfigurationsSenderConfigurationTests {
4647
private final ReactiveWebApplicationContextRunner reactiveContextRunner = new ReactiveWebApplicationContextRunner()
4748
.withConfiguration(AutoConfigurations.of(SenderConfiguration.class));
4849

50+
private final WebApplicationContextRunner servletContextRunner = new WebApplicationContextRunner()
51+
.withConfiguration(AutoConfigurations.of(SenderConfiguration.class));
52+
4953
@Test
5054
void shouldSupplyBeans() {
5155
this.contextRunner.run((context) -> {
@@ -56,8 +60,8 @@ void shouldSupplyBeans() {
5660
}
5761

5862
@Test
59-
void shouldUseWebClientSenderIfWebApplicationIsReactive() {
60-
this.reactiveContextRunner.withUserConfiguration(WebClientConfiguration.class)
63+
void shouldPreferWebClientSenderIfWebApplicationIsReactiveAndUrlSenderIsNotAvailable() {
64+
this.reactiveContextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
6165
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
6266
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
6367
assertThat(context).hasSingleBean(Sender.class);
@@ -66,26 +70,64 @@ void shouldUseWebClientSenderIfWebApplicationIsReactive() {
6670
}
6771

6872
@Test
69-
void shouldNotUseWebClientSenderIfNoBuilderIsAvailable() {
70-
this.reactiveContextRunner.run((context) -> {
71-
assertThat(context).doesNotHaveBean(ZipkinWebClientSender.class);
72-
assertThat(context).hasSingleBean(Sender.class);
73-
assertThat(context).hasSingleBean(URLConnectionSender.class);
74-
});
73+
void shouldPreferWebClientSenderIfWebApplicationIsServletAndUrlSenderIsNotAvailable() {
74+
this.servletContextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
75+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
76+
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
77+
assertThat(context).hasSingleBean(Sender.class);
78+
assertThat(context).hasSingleBean(ZipkinWebClientSender.class);
79+
});
80+
}
81+
82+
@Test
83+
void shouldPreferWebClientInNonWebApplicationAndUrlConnectionSenderIsNotAvailable() {
84+
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class, WebClientConfiguration.class)
85+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
86+
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
87+
assertThat(context).hasSingleBean(Sender.class);
88+
assertThat(context).hasSingleBean(ZipkinWebClientSender.class);
89+
});
7590
}
7691

7792
@Test
78-
void shouldUseRestTemplateSenderIfUrlConnectionSenderIsNotAvailableAndWebAppIsNotReactive() {
93+
void willUseRestTemplateInNonWebApplicationIfUrlConnectionSenderIsNotAvailable() {
7994
this.contextRunner.withUserConfiguration(RestTemplateConfiguration.class)
80-
.withClassLoader(
81-
new FilteredClassLoader("zipkin2.reporter.urlconnection", "org.springframework.web.reactive"))
82-
.run((context) -> {
95+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
8396
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
8497
assertThat(context).hasSingleBean(Sender.class);
8598
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
8699
});
87100
}
88101

102+
@Test
103+
void willUseRestTemplateInServletWebApplicationIfUrlConnectionSenderIsNotAvailable() {
104+
this.servletContextRunner.withUserConfiguration(RestTemplateConfiguration.class)
105+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
106+
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
107+
assertThat(context).hasSingleBean(Sender.class);
108+
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
109+
});
110+
}
111+
112+
@Test
113+
void willUseRestTemplateInReactiveWebApplicationIfUrlConnectionSenderIsNotAvailable() {
114+
this.reactiveContextRunner.withUserConfiguration(RestTemplateConfiguration.class)
115+
.withClassLoader(new FilteredClassLoader("zipkin2.reporter.urlconnection")).run((context) -> {
116+
assertThat(context).doesNotHaveBean(URLConnectionSender.class);
117+
assertThat(context).hasSingleBean(Sender.class);
118+
assertThat(context).hasSingleBean(ZipkinRestTemplateSender.class);
119+
});
120+
}
121+
122+
@Test
123+
void shouldNotUseWebClientSenderIfNoBuilderIsAvailable() {
124+
this.reactiveContextRunner.run((context) -> {
125+
assertThat(context).doesNotHaveBean(ZipkinWebClientSender.class);
126+
assertThat(context).hasSingleBean(Sender.class);
127+
assertThat(context).hasSingleBean(URLConnectionSender.class);
128+
});
129+
}
130+
89131
@Test
90132
void shouldBackOffOnCustomBeans() {
91133
this.contextRunner.withUserConfiguration(CustomConfiguration.class).run((context) -> {

0 commit comments

Comments
 (0)