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
28
import java .util .HashMap ;
28
- import java .util .List ;
29
29
import java .util .concurrent .TimeUnit ;
30
30
import org .apache .solr .client .api .util .SolrVersion ;
31
31
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
@@ -709,10 +709,9 @@ public void testIdleTimeoutWithHttpClient() throws Exception {
709
709
// too little time to succeed
710
710
QueryRequest req = new QueryRequest ();
711
711
req .setResponseParser (new InputStreamResponseParser (FILE_STREAM ));
712
- assertExceptionThrownWithMessageContaining (
713
- SolrServerException .class , List .of ("Timeout" ), () -> oldClient .request (req ));
712
+ assertIsTimeout (expectThrows (SolrServerException .class , () -> oldClient .request (req )));
714
713
715
- int newIdleTimeoutMs = 5 * 1000 ; // enough time to succeed
714
+ int newIdleTimeoutMs = 10 * 1000 ; // enough time to succeed
716
715
try (Http2SolrClient idleTimeoutChangedClient =
717
716
new Http2SolrClient .Builder (url )
718
717
.withHttpClient (oldClient )
@@ -733,32 +732,38 @@ public void testRequestTimeoutWithHttpClient() throws Exception {
733
732
String url = getBaseUrl () + SLOW_STREAM_SERVLET_PATH ;
734
733
try (Http2SolrClient oldClient =
735
734
new Http2SolrClient .Builder (url )
736
- .withIdleTimeout (1000 , TimeUnit .MILLISECONDS )
737
- .withRequestTimeout (10 , TimeUnit .SECONDS )
735
+ .withIdleTimeout (Long . MAX_VALUE , TimeUnit .MILLISECONDS )
736
+ .withRequestTimeout (100 , TimeUnit .MILLISECONDS )
738
737
.build ()) {
738
+
739
+ try (Http2SolrClient onlyBaseUrlChangedClient =
740
+ new Http2SolrClient .Builder (url ).withHttpClient (oldClient ).build ()) {
741
+ // Client created with the same HTTP client should have the same behavior
742
+ assertEquals (oldClient .getHttpClient (), onlyBaseUrlChangedClient .getHttpClient ());
743
+ }
744
+
745
+ // too little time to succeed
739
746
QueryRequest req = new QueryRequest ();
740
747
req .setResponseParser (new InputStreamResponseParser (FILE_STREAM ));
741
- int newRequestTimeoutMs = 2000 ;
748
+ assertIsTimeout (expectThrows (SolrServerException .class , () -> oldClient .request (req )));
749
+
750
+ int newRequestTimeoutMs = 10 * 1000 ; // enough time to succeed
742
751
try (Http2SolrClient requestTimeoutChangedClient =
743
752
new Http2SolrClient .Builder (url )
744
753
.withHttpClient (oldClient )
745
754
.withRequestTimeout (newRequestTimeoutMs , TimeUnit .MILLISECONDS )
746
755
.build ()) {
747
756
NamedList <Object > response = requestTimeoutChangedClient .request (req );
748
757
try (InputStream is = (InputStream ) response .get ("stream" )) {
749
- assertExceptionThrownWithMessageContaining (
750
- IOException .class , List .of ("Total timeout" ), is ::readAllBytes );
758
+ assertEquals ("0123456789" , new String (is .readAllBytes (), StandardCharsets .UTF_8 ));
751
759
}
752
760
}
753
- NamedList <Object > response = oldClient .request (req );
754
- try (InputStream is = (InputStream ) response .get ("stream" )) {
755
- assertEquals ("0123456789" , new String (is .readAllBytes (), StandardCharsets .UTF_8 ));
756
- }
757
761
}
758
762
}
759
763
760
- private static boolean isTimeout ( SolrServerException e ) {
761
- return e .getMessage (). contains ( "timeout" ) || e . getMessage (). contains ( "Timeout" );
764
+ private static void assertIsTimeout ( Throwable t ) {
765
+ assertThat ( t .getMessage (), containsStringIgnoringCase ( "Timeout" ) );
762
766
}
767
+
763
768
/* Missed tests : - set cookies via interceptor - invariant params - compression */
764
769
}
0 commit comments