From e6054a931e94d78684e118f415fc506d5514de88 Mon Sep 17 00:00:00 2001 From: javanna Date: Fri, 22 Jul 2016 19:01:56 +0200 Subject: [PATCH] add async request unit test --- .../client/RestClientIntegTests.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/client/rest/src/test/java/org/elasticsearch/client/RestClientIntegTests.java b/client/rest/src/test/java/org/elasticsearch/client/RestClientIntegTests.java index ceaccad846d..55f96bce508 100644 --- a/client/rest/src/test/java/org/elasticsearch/client/RestClientIntegTests.java +++ b/client/rest/src/test/java/org/elasticsearch/client/RestClientIntegTests.java @@ -48,11 +48,14 @@ import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; import static org.elasticsearch.client.RestClientTestUtil.getAllStatusCodes; import static org.elasticsearch.client.RestClientTestUtil.getHttpMethods; import static org.elasticsearch.client.RestClientTestUtil.randomStatusCode; import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.instanceOf; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertThat; @@ -215,4 +218,34 @@ public class RestClientIntegTests extends RestClientTestCase { assertEquals(statusCode, esResponse.getStatusLine().getStatusCode()); assertEquals(requestBody, EntityUtils.toString(esResponse.getEntity())); } + + public void testAsyncRequests() throws Exception { + int numRequests = randomIntBetween(2, 10); + final CountDownLatch latch = new CountDownLatch(numRequests); + for (int i = 0; i < numRequests; i++) { + final String method = RestClientTestUtil.randomHttpMethod(getRandom()); + final int statusCode = randomStatusCode(getRandom()); + restClient.performRequest(method, "/" + statusCode, new ResponseListener() { + @Override + public void onSuccess(Response response) { + latch.countDown(); + assertResponse(response); + } + + @Override + public void onFailure(Exception exception) { + latch.countDown(); + assertThat(exception, instanceOf(ResponseException.class)); + ResponseException responseException = (ResponseException) exception; + assertResponse(responseException.getResponse()); + } + + private void assertResponse(Response response) { + assertEquals(method, response.getRequestLine().getMethod()); + assertEquals(statusCode, response.getStatusLine().getStatusCode()); + } + }); + } + assertTrue(latch.await(5, TimeUnit.SECONDS)); + } }