Skip to content

Commit c36283c

Browse files
committed
SOLR-17776: Fix test
Also improved testRequestTimeoutWithHttpClient to be structured like the idle timeout test.
1 parent 107348f commit c36283c

File tree

1 file changed

+22
-17
lines changed

1 file changed

+22
-17
lines changed

solr/solrj/src/test/org/apache/solr/client/solrj/impl/Http2SolrClientTest.java

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
package org.apache.solr.client.solrj.impl;
1919

2020
import static org.apache.solr.handler.admin.api.ReplicationAPIBase.FILE_STREAM;
21+
import static org.hamcrest.core.StringContains.containsStringIgnoringCase;
2122

2223
import java.io.IOException;
2324
import java.io.InputStream;
2425
import java.nio.charset.StandardCharsets;
2526
import java.util.Base64;
2627
import java.util.Collections;
27-
import java.util.List;
2828
import java.util.concurrent.TimeUnit;
2929
import org.apache.solr.client.api.util.SolrVersion;
3030
import org.apache.solr.client.solrj.ResponseParser;
@@ -74,7 +74,7 @@ public void testTimeout() throws Exception {
7474
client.query(q, SolrRequest.METHOD.GET);
7575
fail("No exception thrown.");
7676
} catch (SolrServerException e) {
77-
assertTrue(isTimeout(e));
77+
assertIsTimeout(e);
7878
}
7979
}
8080

@@ -105,7 +105,7 @@ public void testRequestTimeout() throws Exception {
105105
client.query(q, SolrRequest.METHOD.GET);
106106
fail("No exception thrown.");
107107
} catch (SolrServerException e) {
108-
assertTrue(isTimeout(e));
108+
assertIsTimeout(e);
109109
}
110110
}
111111

@@ -673,10 +673,9 @@ public void testIdleTimeoutWithHttpClient() throws Exception {
673673
// too little time to succeed
674674
QueryRequest req = new QueryRequest();
675675
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)));
678677

679-
int newIdleTimeoutMs = 5 * 1000; // enough time to succeed
678+
int newIdleTimeoutMs = 10 * 1000; // enough time to succeed
680679
try (Http2SolrClient idleTimeoutChangedClient =
681680
new Http2SolrClient.Builder(url)
682681
.withHttpClient(oldClient)
@@ -697,32 +696,38 @@ public void testRequestTimeoutWithHttpClient() throws Exception {
697696
String url = getBaseUrl() + SLOW_STREAM_SERVLET_PATH;
698697
try (Http2SolrClient oldClient =
699698
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)
702701
.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
703710
QueryRequest req = new QueryRequest();
704711
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
706715
try (Http2SolrClient requestTimeoutChangedClient =
707716
new Http2SolrClient.Builder(url)
708717
.withHttpClient(oldClient)
709718
.withRequestTimeout(newRequestTimeoutMs, TimeUnit.MILLISECONDS)
710719
.build()) {
711720
NamedList<Object> response = requestTimeoutChangedClient.request(req);
712721
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));
715723
}
716724
}
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-
}
721725
}
722726
}
723727

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"));
726730
}
731+
727732
/* Missed tests : - set cookies via interceptor - invariant params - compression */
728733
}

0 commit comments

Comments
 (0)