|
32 | 32 | import org.apache.http.entity.StringEntity; |
33 | 33 | import org.apache.http.impl.client.BasicCredentialsProvider; |
34 | 34 | import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; |
| 35 | +import org.apache.http.nio.entity.NStringEntity; |
35 | 36 | import org.apache.http.util.EntityUtils; |
36 | 37 | import org.elasticsearch.mocksocket.MockHttpServer; |
37 | 38 | import org.junit.AfterClass; |
|
48 | 49 | import java.util.List; |
49 | 50 | import java.util.Map; |
50 | 51 | import java.util.Set; |
| 52 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 53 | +import java.util.concurrent.CountDownLatch; |
| 54 | +import java.util.concurrent.TimeUnit; |
51 | 55 |
|
52 | 56 | import static org.elasticsearch.client.RestClientTestUtil.getAllStatusCodes; |
53 | 57 | import static org.elasticsearch.client.RestClientTestUtil.getHttpMethods; |
@@ -159,6 +163,42 @@ public static void stopHttpServers() throws IOException { |
159 | 163 | httpServer = null; |
160 | 164 | } |
161 | 165 |
|
| 166 | + /** |
| 167 | + * Tests sending a bunch of async requests works well (e.g. no TimeoutException from the leased pool) |
| 168 | + * See https://github.com/elastic/elasticsearch/issues/24069 |
| 169 | + */ |
| 170 | + public void testManyAsyncRequests() throws Exception { |
| 171 | + int iters = randomIntBetween(500, 1000); |
| 172 | + final CountDownLatch latch = new CountDownLatch(iters); |
| 173 | + final List<Exception> exceptions = new CopyOnWriteArrayList<>(); |
| 174 | + for (int i = 0; i < iters; i++) { |
| 175 | + Request request = new Request("PUT", "/200"); |
| 176 | + request.setEntity(new NStringEntity("{}", ContentType.APPLICATION_JSON)); |
| 177 | + restClient.performRequestAsync(request, new ResponseListener() { |
| 178 | + @Override |
| 179 | + public void onSuccess(Response response) { |
| 180 | + latch.countDown(); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public void onFailure(Exception exception) { |
| 185 | + exceptions.add(exception); |
| 186 | + latch.countDown(); |
| 187 | + } |
| 188 | + }); |
| 189 | + } |
| 190 | + |
| 191 | + assertTrue("timeout waiting for requests to be sent", latch.await(10, TimeUnit.SECONDS)); |
| 192 | + if (exceptions.isEmpty() == false) { |
| 193 | + AssertionError error = new AssertionError("expected no failures but got some. see suppressed for first 10 of [" |
| 194 | + + exceptions.size() + "] failures"); |
| 195 | + for (Exception exception : exceptions.subList(0, Math.min(10, exceptions.size()))) { |
| 196 | + error.addSuppressed(exception); |
| 197 | + } |
| 198 | + throw error; |
| 199 | + } |
| 200 | + } |
| 201 | + |
162 | 202 | /** |
163 | 203 | * End to end test for headers. We test it explicitly against a real http client as there are different ways |
164 | 204 | * to set/add headers to the {@link org.apache.http.client.HttpClient}. |
|
0 commit comments