dsmiley commented on code in PR #2402:
URL: https://github.com/apache/solr/pull/2402#discussion_r1564233373
##
solr/solrj/src/test/org/apache/solr/client/solrj/impl/LBHttp2SolrClientTest.java:
##
@@ -51,4 +67,249 @@ public void
testLBHttp2SolrClientWithTheseParamNamesInTheUrl() {
http2SolrClient.getUrlParamNames().toArray());
}
}
+
+ @Test
+ public void testAsyncDeprecated() {
+testAsync(true);
+ }
+
+ @Test
+ public void testAsync() {
+testAsync(false);
+ }
+
+ @Test
+ public void testAsyncWithFailures() {
+
+// TODO: This demonstrates that the failing endpoint always gets retried,
but
+// I would expect it to be labelled as a "zombie" and be skipped with
additional iterations.
+
+LBSolrClient.Endpoint ep1 = new
LBSolrClient.Endpoint("http://endpoint.one;);
+LBSolrClient.Endpoint ep2 = new
LBSolrClient.Endpoint("http://endpoint.two;);
+List endpointList = List.of(ep1, ep2);
+
+Http2SolrClient.Builder b = new Http2SolrClient.Builder("http://base.url;);
+try (MockHttp2SolrClient client = new
MockHttp2SolrClient("http://base.url;, b);
+LBHttp2SolrClient testClient = new LBHttp2SolrClient.Builder(client,
ep1, ep2).build()) {
+
+ for (int j = 0; j < 2; j++) {
+// j: first time Endpoint One will retrun error code 500.
+// second time Endpoint One will be healthy
+
+String basePathToSucceed;
+if (j == 0) {
+ client.basePathToFail = ep1.getBaseUrl();
+ basePathToSucceed = ep2.getBaseUrl();
+} else {
+ client.basePathToFail = ep2.getBaseUrl();
+ basePathToSucceed = ep1.getBaseUrl();
+}
+
+for (int i = 0; i < 10; i++) {
+ // i: we'll try 10 times to see if it behaves the same every time.
+
+ QueryRequest queryRequest = new QueryRequest(new
MapSolrParams(Map.of("q", "" + i)));
+ LBSolrClient.Req req = new LBSolrClient.Req(queryRequest,
endpointList);
+ String iterMessage = "iter j/i " + j + "/" + i;
+ try {
+testClient.requestAsync(req).get(1, TimeUnit.MINUTES);
+ } catch (InterruptedException ie) {
+Thread.currentThread().interrupt();
+fail("interrupted");
+ } catch (TimeoutException | ExecutionException e) {
+fail(iterMessage + " Response ended in failure: " + e);
+ }
+ if (j == 0) {
+// The first endpoint gives an exception, so it retries.
+assertEquals(iterMessage, 2, client.lastBasePaths.size());
+
+String failedBasePath = client.lastBasePaths.remove(0);
+assertEquals(iterMessage, client.basePathToFail, failedBasePath);
+ } else {
+// The first endpoint does not give the exception, it doesn't
retry.
+assertEquals(iterMessage, 1, client.lastBasePaths.size());
+ }
+ String successBasePath = client.lastBasePaths.remove(0);
+ assertEquals(iterMessage, basePathToSucceed, successBasePath);
+}
+ }
+}
+ }
+
+ private void testAsync(boolean useDeprecatedApi) {
+LBSolrClient.Endpoint ep1 = new
LBSolrClient.Endpoint("http://endpoint.one;);
+LBSolrClient.Endpoint ep2 = new
LBSolrClient.Endpoint("http://endpoint.two;);
+List endpointList = List.of(ep1, ep2);
+
+Http2SolrClient.Builder b = new Http2SolrClient.Builder("http://base.url;);
+try (MockHttp2SolrClient client = new
MockHttp2SolrClient("http://base.url;, b);
+LBHttp2SolrClient testClient = new LBHttp2SolrClient.Builder(client,
ep1, ep2).build()) {
+
+ int limit = 10; // For simplicity use an even limit
+ int halfLimit = limit / 2; // see TODO below
+
+ CountDownLatch cdl = new CountDownLatch(limit); // deprecated API use
+ List listeners = new ArrayList<>(); // deprecated
API use
+ List> responses = new ArrayList<>();
+
+ for (int i = 0; i < limit; i++) {
+QueryRequest queryRequest = new QueryRequest(new
MapSolrParams(Map.of("q", "" + i)));
+LBSolrClient.Req req = new LBSolrClient.Req(queryRequest,
endpointList);
+if (useDeprecatedApi) {
+ LBTestAsyncListener listener = new LBTestAsyncListener(cdl);
+ listeners.add(listener);
+ testClient.asyncReq(req, listener);
+} else {
+ responses.add(testClient.requestAsync(req));
+}
+ }
+
+ if (useDeprecatedApi) {
+try {
+ // This is just a formality. This is a single-threaded test.
+ cdl.await(1, TimeUnit.MINUTES);
+} catch (InterruptedException ie) {
+ Thread.currentThread().interrupt();
+ fail("interrupted");
+}
+ }
+
+ QueryRequest[] queryRequests = new QueryRequest[limit];
+ int numEndpointOne = 0;
+ int numEndpointTwo = 0; // see TODO below
+ for (int i = 0; i < limit; i++) {
+SolrRequest lastSolrReq = client.lastSolrRequests.get(i);