18
18
package org .apache .solr .client .solrj .impl ;
19
19
20
20
import static org .apache .solr .handler .admin .api .ReplicationAPIBase .FILE_STREAM ;
21
+ import static org .hamcrest .core .StringContains .containsStringIgnoringCase ;
21
22
22
23
import java .io .IOException ;
23
24
import java .io .InputStream ;
24
25
import java .nio .charset .StandardCharsets ;
25
26
import java .util .Base64 ;
26
27
import java .util .Collections ;
27
- import java .util .List ;
28
28
import java .util .concurrent .TimeUnit ;
29
29
import org .apache .solr .client .api .util .SolrVersion ;
30
30
import org .apache .solr .client .solrj .ResponseParser ;
@@ -74,7 +74,7 @@ public void testTimeout() throws Exception {
74
74
client .query (q , SolrRequest .METHOD .GET );
75
75
fail ("No exception thrown." );
76
76
} catch (SolrServerException e ) {
77
- assertTrue ( isTimeout ( e ) );
77
+ assertIsTimeout ( e );
78
78
}
79
79
}
80
80
@@ -105,7 +105,7 @@ public void testRequestTimeout() throws Exception {
105
105
client .query (q , SolrRequest .METHOD .GET );
106
106
fail ("No exception thrown." );
107
107
} catch (SolrServerException e ) {
108
- assertTrue ( isTimeout ( e ) );
108
+ assertIsTimeout ( e );
109
109
}
110
110
}
111
111
@@ -673,10 +673,9 @@ public void testIdleTimeoutWithHttpClient() throws Exception {
673
673
// too little time to succeed
674
674
QueryRequest req = new QueryRequest ();
675
675
req .setResponseParser (new InputStreamResponseParser (FILE_STREAM ));
676
- assertExceptionThrownWithMessageContaining (
677
- SolrServerException .class , List .of ("Timeout" ), () -> oldClient .request (req ));
676
+ assertIsTimeout (expectThrows (SolrServerException .class , () -> oldClient .request (req )));
678
677
679
- int newIdleTimeoutMs = 5 * 1000 ; // enough time to succeed
678
+ int newIdleTimeoutMs = 10 * 1000 ; // enough time to succeed
680
679
try (Http2SolrClient idleTimeoutChangedClient =
681
680
new Http2SolrClient .Builder (url )
682
681
.withHttpClient (oldClient )
@@ -697,32 +696,38 @@ public void testRequestTimeoutWithHttpClient() throws Exception {
697
696
String url = getBaseUrl () + SLOW_STREAM_SERVLET_PATH ;
698
697
try (Http2SolrClient oldClient =
699
698
new Http2SolrClient .Builder (url )
700
- .withIdleTimeout (1000 , TimeUnit .MILLISECONDS )
701
- .withRequestTimeout (10 , TimeUnit .SECONDS )
699
+ .withIdleTimeout (Long . MAX_VALUE , TimeUnit .MILLISECONDS )
700
+ .withRequestTimeout (100 , TimeUnit .MILLISECONDS )
702
701
.build ()) {
702
+
703
+ try (Http2SolrClient onlyBaseUrlChangedClient =
704
+ new Http2SolrClient .Builder (url ).withHttpClient (oldClient ).build ()) {
705
+ // Client created with the same HTTP client should have the same behavior
706
+ assertEquals (oldClient .getHttpClient (), onlyBaseUrlChangedClient .getHttpClient ());
707
+ }
708
+
709
+ // too little time to succeed
703
710
QueryRequest req = new QueryRequest ();
704
711
req .setResponseParser (new InputStreamResponseParser (FILE_STREAM ));
705
- int newRequestTimeoutMs = 2000 ;
712
+ assertIsTimeout (expectThrows (SolrServerException .class , () -> oldClient .request (req )));
713
+
714
+ int newRequestTimeoutMs = 10 * 1000 ; // enough time to succeed
706
715
try (Http2SolrClient requestTimeoutChangedClient =
707
716
new Http2SolrClient .Builder (url )
708
717
.withHttpClient (oldClient )
709
718
.withRequestTimeout (newRequestTimeoutMs , TimeUnit .MILLISECONDS )
710
719
.build ()) {
711
720
NamedList <Object > response = requestTimeoutChangedClient .request (req );
712
721
try (InputStream is = (InputStream ) response .get ("stream" )) {
713
- assertExceptionThrownWithMessageContaining (
714
- IOException .class , List .of ("Total timeout" ), is ::readAllBytes );
722
+ assertEquals ("0123456789" , new String (is .readAllBytes (), StandardCharsets .UTF_8 ));
715
723
}
716
724
}
717
- NamedList <Object > response = oldClient .request (req );
718
- try (InputStream is = (InputStream ) response .get ("stream" )) {
719
- assertEquals ("0123456789" , new String (is .readAllBytes (), StandardCharsets .UTF_8 ));
720
- }
721
725
}
722
726
}
723
727
724
- private static boolean isTimeout ( SolrServerException e ) {
725
- return e .getMessage (). contains ( "timeout" ) || e . getMessage (). contains ( "Timeout" );
728
+ private static void assertIsTimeout ( Throwable t ) {
729
+ assertThat ( t .getMessage (), containsStringIgnoringCase ( "Timeout" ) );
726
730
}
731
+
727
732
/* Missed tests : - set cookies via interceptor - invariant params - compression */
728
733
}
0 commit comments