'Elasticsearch search test timeout

I need some advise on how can I use unit test to test on the timeout setting for Elasticsearch. I am hoping to test with Thread.sleep() to be over the configured timeout setting when performing search. Is this possible?

I saw in Elasticsearch documentation that it is possible to set the timeout in Java. https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_timeouts.html

I would like to add some of my code into the thread, because it seems like the test method I wrote is not able to hit on the configured timeout.

This is the method where I declared the timeout and also the RestHighLevelClient

@Bean
public RestHighLevelClient client(){
    RestClientBuilder builder = RestClient.builder(new HttpHost("localhost", 9200))
    .setHttpClientConfigCallback(....)
    .setRequestConfigCallback(new RestClientBuilder.RequestConfigCallback(){
        @Override
        public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder builder){
            return builder  
                .setSocketTimeout(3000)
                .setConnectTimeout(3000);
        }
    });
    return new RestHighLevelClient(builder);        
}

My initial question is to write a test method to test that any search Request of the RestHighLevelClient, with the above configuration, and delay the search so to test the timeout configured in the RestHighLevelClient. Is it possible?

In my test java class, below is my code The client in the test1() is the same variable and also the configuration same as above method.

@Test
public void test1(){
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(
        QueryBuilders.wrapperQuery(query);
    ); 
    searchRequest.source(searchSourceBuilder);
    await().pollDelay(15, TimeUnit.SECONDS).untilAsserted(
        () ->
            client.search(searchRequest, RequestOptions.DEFAULT)
    );  
}

test1() keep failing (expected it to fail), but the error message is

"java.lang.IllegalArgumentException: Timeout (10 seconds) must be greater than the poll delay (15 seconds)"

I set the client timeout to be 3 seconds (for testing), but how come the error message says timeout 10 seconds. Probably I have did mistake on my code, can someone please give me some advise? Thank you very much.



Solution 1:[1]

You can use awaitility library and write something like this

@Autowired
protected ElasticsearchOperations elasticsearchOperations;

await().atMost(5, TimeUnit.SECONDS)
        .pollDelay(0, TimeUnit.MILLISECONDS)
        .pollInterval(250, TimeUnit.MILLISECONDS)
        .untilAsserted(() ->
                assertThat(elasticsearchOperations
                        .get(id, ReviewRequestDocument.class)).isNotNull());

P.S. As of the changes in your code I think you should change

await().pollDelay(15, TimeUnit.SECONDS).untilAsserted(
        () ->
            client.search(searchRequest, RequestOptions.DEFAULT)
    );

to

await().pollDelay(15, TimeUnit.SECONDS)
  .atMost(60, TimeUnit.SECONDS)
  .untilAsserted(
        () ->
            client.search(searchRequest, RequestOptions.DEFAULT)
    );

In other words you poll delay and poll timeout both mustn't be greater than assertion overall timeout, otherwise it doesn't make sense as assertion fails before even executing a single check.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1