Skip to content

Commit e533162

Browse files
committed
SOLR-17776: Fix test
Also improved testRequestTimeoutWithHttpClient to be structured like the idle timeout test. (cherry picked from commit c36283c)
1 parent cf315a3 commit e533162

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,14 +18,14 @@
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;
2728
import java.util.HashMap;
28-
import java.util.List;
2929
import java.util.concurrent.TimeUnit;
3030
import org.apache.solr.client.api.util.SolrVersion;
3131
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

@@ -709,10 +709,9 @@ public void testIdleTimeoutWithHttpClient() throws Exception {
709709
// too little time to succeed
710710
QueryRequest req = new QueryRequest();
711711
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)));
714713

715-
int newIdleTimeoutMs = 5 * 1000; // enough time to succeed
714+
int newIdleTimeoutMs = 10 * 1000; // enough time to succeed
716715
try (Http2SolrClient idleTimeoutChangedClient =
717716
new Http2SolrClient.Builder(url)
718717
.withHttpClient(oldClient)
@@ -733,32 +732,38 @@ public void testRequestTimeoutWithHttpClient() throws Exception {
733732
String url = getBaseUrl() + SLOW_STREAM_SERVLET_PATH;
734733
try (Http2SolrClient oldClient =
735734
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)
738737
.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
739746
QueryRequest req = new QueryRequest();
740747
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
742751
try (Http2SolrClient requestTimeoutChangedClient =
743752
new Http2SolrClient.Builder(url)
744753
.withHttpClient(oldClient)
745754
.withRequestTimeout(newRequestTimeoutMs, TimeUnit.MILLISECONDS)
746755
.build()) {
747756
NamedList<Object> response = requestTimeoutChangedClient.request(req);
748757
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));
751759
}
752760
}
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-
}
757761
}
758762
}
759763

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"));
762766
}
767+
763768
/* Missed tests : - set cookies via interceptor - invariant params - compression */
764769
}

0 commit comments

Comments
 (0)