use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ConnectionPoolTest method testMaxTotalConnections.
@Test(groups = "standalone")
public void testMaxTotalConnections() throws Exception {
try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setMaxConnections(1))) {
String url = getTargetUrl();
int i;
Exception exception = null;
for (i = 0; i < 3; i++) {
try {
logger.info("{} requesting url [{}]...", i, url);
Response response = client.prepareGet(url).execute().get();
logger.info("{} response [{}].", i, response);
} catch (Exception ex) {
exception = ex;
}
}
assertNull(exception);
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class MaxConnectionsInThreads method testMaxConnectionsWithinThreads.
@Test(groups = "standalone")
public void testMaxConnectionsWithinThreads() throws Exception {
String[] urls = new String[] { getTargetUrl(), getTargetUrl() };
AsyncHttpClientConfig config = //
config().setConnectTimeout(//
1000).setRequestTimeout(//
5000).setKeepAlive(//
true).setMaxConnections(//
1).setMaxConnectionsPerHost(//
1).build();
final CountDownLatch inThreadsLatch = new CountDownLatch(2);
final AtomicInteger failedCount = new AtomicInteger();
try (AsyncHttpClient client = asyncHttpClient(config)) {
for (final String url : urls) {
Thread t = new Thread() {
public void run() {
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
@Override
public Response onCompleted(Response response) throws Exception {
Response r = super.onCompleted(response);
inThreadsLatch.countDown();
return r;
}
@Override
public void onThrowable(Throwable t) {
super.onThrowable(t);
failedCount.incrementAndGet();
inThreadsLatch.countDown();
}
});
}
};
t.start();
}
inThreadsLatch.await();
assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from concurrent threads");
final CountDownLatch notInThreadsLatch = new CountDownLatch(2);
failedCount.set(0);
for (final String url : urls) {
client.prepareGet(url).execute(new AsyncCompletionHandlerBase() {
@Override
public Response onCompleted(Response response) throws Exception {
Response r = super.onCompleted(response);
notInThreadsLatch.countDown();
return r;
}
@Override
public void onThrowable(Throwable t) {
super.onThrowable(t);
failedCount.incrementAndGet();
notInThreadsLatch.countDown();
}
});
}
notInThreadsLatch.await();
assertEquals(failedCount.get(), 1, "Max Connections should have been reached when launching from main thread");
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class FilterTest method replayHeaderResponseFilterTest.
@Test(groups = "standalone")
public void replayHeaderResponseFilterTest() throws Exception {
final AtomicBoolean replay = new AtomicBoolean(true);
ResponseFilter responseFilter = new ResponseFilter() {
public <T> FilterContext<T> filter(FilterContext<T> ctx) throws FilterException {
if (ctx.getResponseHeaders() != null && ctx.getResponseHeaders().getHeaders().get("Ping").equals("Pong") && replay.getAndSet(false)) {
Request request = new RequestBuilder(ctx.getRequest()).addHeader("Ping", "Pong").build();
return new FilterContext.FilterContextBuilder<T>().asyncHandler(ctx.getAsyncHandler()).request(request).replayRequest(true).build();
}
return ctx;
}
};
try (AsyncHttpClient c = asyncHttpClient(config().addResponseFilter(responseFilter))) {
Response response = c.preparePost(getTargetUrl()).addHeader("Ping", "Pong").execute().get();
assertNotNull(response);
assertEquals(response.getStatusCode(), 200);
assertEquals(response.getHeader("Ping"), "Pong");
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class FilterTest method maxConnectionsText.
@Test(groups = "standalone")
public void maxConnectionsText() throws Exception {
try (AsyncHttpClient c = asyncHttpClient(config().addRequestFilter(new ThrottleRequestFilter(0, 1000)))) {
c.preparePost(getTargetUrl()).execute().get();
fail("Should have timed out");
} catch (ExecutionException ex) {
assertTrue(ex.getCause() instanceof FilterException);
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class BodyDeferringAsyncHandlerTest method deferredInputStreamTrickWithFailure.
@Test(groups = "standalone", expectedExceptions = RemotelyClosedException.class)
public void deferredInputStreamTrickWithFailure() throws Throwable {
try (AsyncHttpClient client = asyncHttpClient(getAsyncHttpClientConfig())) {
BoundRequestBuilder r = client.prepareGet("http://localhost:" + port1 + "/deferredInputStreamTrickWithFailure").addHeader("X-FAIL-TRANSFER", Boolean.TRUE.toString());
PipedOutputStream pos = new PipedOutputStream();
PipedInputStream pis = new PipedInputStream(pos);
BodyDeferringAsyncHandler bdah = new BodyDeferringAsyncHandler(pos);
Future<Response> f = r.execute(bdah);
BodyDeferringInputStream is = new BodyDeferringInputStream(f, bdah, pis);
Response resp = is.getAsapResponse();
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getHeader(CONTENT_LENGTH), String.valueOf(CONTENT_LENGTH_VALUE));
// "consume" the body, but our code needs input stream
CountingOutputStream cos = new CountingOutputStream();
try {
try {
copy(is, cos);
} finally {
is.close();
cos.close();
}
} catch (IOException e) {
throw e.getCause();
}
}
}
Aggregations