{
final Integer REQUEST_TIMEOUT = 1000;
final TransportClient transportClient =
_clientFactory.getClient(Collections.singletonMap(HttpClientFactory.HTTP_REQUEST_TIMEOUT,
Integer.toString(REQUEST_TIMEOUT)));
final Client client = new TransportClientAdapter(transportClient);
RestRequestBuilder rb = new RestRequestBuilder(_testServer.getRequestURI());
rb.setMethod("GET");
RestRequest request = rb.build();
final RequestContext context = new RequestContext();
Future<RestResponse> f = client.restRequest(request, context);
Future<RestResponse> f2 = client.restRequest(request, context);
// This will block
RestResponse response = f.get(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
assertEquals(response.getStatus(), 200);
response = f2.get(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
assertEquals(response.getStatus(), 200);
final Integer iterations = 5;
final CountDownLatch latch = new CountDownLatch(iterations);
//Test that sending multiple requests with the same request context works correctly, without
//modifying the original request context.
for (int i=0; i<iterations; ++i)
{
client.restRequest(request, context, new Callback<RestResponse>()
{
@Override
public void onError(Throwable e)
{
Assert.fail("Expected success, received: " + e);
latch.countDown();
}
@Override
public void onSuccess(RestResponse result)
{
Assert.assertEquals(result.getStatus(), 200);
latch.countDown();
}
});
}
latch.await(REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
Assert.assertTrue(context.getLocalAttrs().isEmpty());
final FutureCallback<None> callback = new FutureCallback<None>();
client.shutdown(callback);
callback.get();
}