Polish "Allow to configure the Elasticsearch rest client timeouts"
Closes gh-15965
This commit is contained in:
parent
5bacb32557
commit
2cfcd2690e
|
@ -78,29 +78,39 @@ public class RestClientAutoConfigurationTests {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void defaultTimeoutsShouldBeConfigured() {
|
||||
public void configureWithNoTimeoutsApplyDefaults() {
|
||||
this.contextRunner.run((context) -> {
|
||||
assertThat(context).hasSingleBean(RestClient.class);
|
||||
RestClient restClient = context.getBean(RestClient.class);
|
||||
assertTimeouts(restClient,
|
||||
Duration.ofMillis(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MILLIS), Duration.ofMillis(RestClientBuilder.DEFAULT_SOCKET_TIMEOUT_MILLIS)
|
||||
);
|
||||
Duration.ofMillis(RestClientBuilder.DEFAULT_CONNECT_TIMEOUT_MILLIS),
|
||||
Duration.ofMillis(RestClientBuilder.DEFAULT_SOCKET_TIMEOUT_MILLIS));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeoutsCanBeConfigured() {
|
||||
public void configureWithCustomTimeouts() {
|
||||
this.contextRunner
|
||||
.withPropertyValues("spring.elasticsearch.rest.connection-timeout=15s",
|
||||
"spring.elasticsearch.rest.read-timeout=1m")
|
||||
.run((context) -> {
|
||||
assertThat(context).hasSingleBean(RestClient.class);
|
||||
RestClient restClient = context.getBean(RestClient.class);
|
||||
assertTimeouts(restClient, Duration.ofSeconds(15), Duration.ofMinutes(1)
|
||||
);
|
||||
assertTimeouts(restClient, Duration.ofSeconds(15),
|
||||
Duration.ofMinutes(1));
|
||||
});
|
||||
}
|
||||
|
||||
private static void assertTimeouts(RestClient restClient, Duration connectTimeout,
|
||||
Duration readTimeout) {
|
||||
Object client = ReflectionTestUtils.getField(restClient, "client");
|
||||
Object config = ReflectionTestUtils.getField(client, "defaultConfig");
|
||||
assertThat(config).hasFieldOrPropertyWithValue("socketTimeout",
|
||||
Math.toIntExact(readTimeout.toMillis()));
|
||||
assertThat(config).hasFieldOrPropertyWithValue("connectTimeout",
|
||||
Math.toIntExact(connectTimeout.toMillis()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void restClientCanQueryElasticsearchNode() {
|
||||
this.contextRunner
|
||||
|
@ -121,15 +131,6 @@ public class RestClientAutoConfigurationTests {
|
|||
});
|
||||
}
|
||||
|
||||
private static void assertTimeouts(RestClient restClient, Duration connectTimeout, Duration readTimeout) {
|
||||
Object client = ReflectionTestUtils.getField(restClient, "client");
|
||||
Object config = ReflectionTestUtils.getField(client, "defaultConfig");
|
||||
assertThat(config).hasFieldOrPropertyWithValue("socketTimeout",
|
||||
Math.toIntExact(readTimeout.toMillis()));
|
||||
assertThat(config).hasFieldOrPropertyWithValue("connectTimeout",
|
||||
Math.toIntExact(connectTimeout.toMillis()));
|
||||
}
|
||||
|
||||
@Configuration(proxyBeanMethods = false)
|
||||
static class CustomRestClientConfiguration {
|
||||
|
||||
|
|
|
@ -4805,6 +4805,7 @@ You can further tune how `RestClient` is configured, as shown in the following e
|
|||
[source,properties,indent=0]
|
||||
----
|
||||
spring.elasticsearch.rest.uris=https://search.example.com:9200
|
||||
spring.elasticsearch.rest.read-timeout=10s
|
||||
spring.elasticsearch.rest.username=user
|
||||
spring.elasticsearch.rest.password=secret
|
||||
----
|
||||
|
|
Loading…
Reference in New Issue