Skip to content

Commit 2cfcd26

Browse files
committed
Polish "Allow to configure the Elasticsearch rest client timeouts"
Closes gh-15965
1 parent 5bacb32 commit 2cfcd26

File tree

2 files changed

+17
-15
lines changed

2 files changed

+17
-15
lines changed

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/rest/RestClientAutoConfigurationTests.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,39 @@ public void configureWhenBuilderCustomizerShouldApply() {
7878
}
7979

8080
@Test
81-
public void defaultTimeoutsShouldBeConfigured() {
81+
public void configureWithNoTimeoutsApplyDefaults() {
8282
this.contextRunner.run((context) -> {
8383
assertThat(context).hasSingleBean(RestClient.class);
8484
RestClient restClient = context.getBean(RestClient.class);
8585
assertTimeouts(restClient,
86-
Duration.ofMillis(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MILLIS), Duration.ofMillis(RestClientBuilder.DEFAULT_SOCKET_TIMEOUT_MILLIS)
87-
);
86+
Duration.ofMillis(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MILLIS),
87+
Duration.ofMillis(RestClientBuilder.DEFAULT_SOCKET_TIMEOUT_MILLIS));
8888
});
8989
}
9090

9191
@Test
92-
public void timeoutsCanBeConfigured() {
92+
public void configureWithCustomTimeouts() {
9393
this.contextRunner
9494
.withPropertyValues("spring.elasticsearch.rest.connection-timeout=15s",
9595
"spring.elasticsearch.rest.read-timeout=1m")
9696
.run((context) -> {
9797
assertThat(context).hasSingleBean(RestClient.class);
9898
RestClient restClient = context.getBean(RestClient.class);
99-
assertTimeouts(restClient, Duration.ofSeconds(15), Duration.ofMinutes(1)
100-
);
99+
assertTimeouts(restClient, Duration.ofSeconds(15),
100+
Duration.ofMinutes(1));
101101
});
102102
}
103103

104+
private static void assertTimeouts(RestClient restClient, Duration connectTimeout,
105+
Duration readTimeout) {
106+
Object client = ReflectionTestUtils.getField(restClient, "client");
107+
Object config = ReflectionTestUtils.getField(client, "defaultConfig");
108+
assertThat(config).hasFieldOrPropertyWithValue("socketTimeout",
109+
Math.toIntExact(readTimeout.toMillis()));
110+
assertThat(config).hasFieldOrPropertyWithValue("connectTimeout",
111+
Math.toIntExact(connectTimeout.toMillis()));
112+
}
113+
104114
@Test
105115
public void restClientCanQueryElasticsearchNode() {
106116
this.contextRunner
@@ -121,15 +131,6 @@ public void restClientCanQueryElasticsearchNode() {
121131
});
122132
}
123133

124-
private static void assertTimeouts(RestClient restClient, Duration connectTimeout, Duration readTimeout) {
125-
Object client = ReflectionTestUtils.getField(restClient, "client");
126-
Object config = ReflectionTestUtils.getField(client, "defaultConfig");
127-
assertThat(config).hasFieldOrPropertyWithValue("socketTimeout",
128-
Math.toIntExact(readTimeout.toMillis()));
129-
assertThat(config).hasFieldOrPropertyWithValue("connectTimeout",
130-
Math.toIntExact(connectTimeout.toMillis()));
131-
}
132-
133134
@Configuration(proxyBeanMethods = false)
134135
static class CustomRestClientConfiguration {
135136

spring-boot-project/spring-boot-docs/src/main/asciidoc/spring-boot-features.adoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4805,6 +4805,7 @@ You can further tune how `RestClient` is configured, as shown in the following e
48054805
[source,properties,indent=0]
48064806
----
48074807
spring.elasticsearch.rest.uris=https://search.example.com:9200
4808+
spring.elasticsearch.rest.read-timeout=10s
48084809
spring.elasticsearch.rest.username=user
48094810
spring.elasticsearch.rest.password=secret
48104811
----

0 commit comments

Comments
 (0)