1+ package org .opensearch .client .transport .httpclient5 ;
2+
3+ import com .fasterxml .jackson .databind .JsonNode ;
4+ import org .apache .hc .client5 .http .ConnectTimeoutException ;
5+ import org .apache .hc .client5 .http .config .ConnectionConfig ;
6+ import org .apache .hc .client5 .http .impl .nio .PoolingAsyncClientConnectionManager ;
7+ import org .apache .hc .client5 .http .impl .nio .PoolingAsyncClientConnectionManagerBuilder ;
8+ import org .apache .hc .core5 .http .HttpHost ;
9+ import org .apache .hc .core5 .util .Timeout ;
10+ import org .junit .Test ;
11+ import org .opensearch .client .opensearch .OpenSearchClient ;
12+ import org .opensearch .client .opensearch .core .SearchRequest ;
13+
14+ import static org .junit .Assert .assertThrows ;
15+ import static org .junit .Assert .assertTrue ;
16+
17+ public class ApacheHttpClient5TransportBuilderTest {
18+
19+ @ Test
20+ public void timeOutTest () {
21+
22+ int expectTime = 10 ;
23+ String expectMessage = expectTime + " MILLISECONDS" ;
24+
25+ ApacheHttpClient5Transport apacheHttpClient5Transport = ApacheHttpClient5TransportBuilder .builder (new HttpHost ("10.255.255.1" , 9200 ))
26+ .setHttpClientConfigCallback (httpClientBuilder -> {
27+
28+ ConnectionConfig connectionConfig = ConnectionConfig .custom ()
29+ .setConnectTimeout (Timeout .ofMilliseconds (expectTime ))
30+ .build ();
31+
32+ PoolingAsyncClientConnectionManager connectionManager = PoolingAsyncClientConnectionManagerBuilder
33+ .create ()
34+ .setDefaultConnectionConfig (connectionConfig )
35+ .build ();
36+
37+ return httpClientBuilder .setConnectionManager (connectionManager );
38+ }).build ();
39+
40+ OpenSearchClient osClient = new OpenSearchClient (apacheHttpClient5Transport );
41+
42+ ConnectTimeoutException exception = assertThrows (ConnectTimeoutException .class , () -> osClient .search (SearchRequest .of (sb -> sb .index ("index" )), JsonNode .class ));
43+
44+ assertTrue ("Expected timeout value not found in message" , exception .getMessage ().contains (expectMessage ));
45+ }
46+ }
0 commit comments