use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class ConnectionPoolTest method asyncHandlerOnThrowableTest.
@Test(groups = "standalone")
public void asyncHandlerOnThrowableTest() throws Exception {
try (AsyncHttpClient client = asyncHttpClient()) {
final AtomicInteger count = new AtomicInteger();
final String THIS_IS_NOT_FOR_YOU = "This is not for you";
final CountDownLatch latch = new CountDownLatch(16);
for (int i = 0; i < 16; i++) {
client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerBase() {
@Override
public Response onCompleted(Response response) throws Exception {
throw new Exception(THIS_IS_NOT_FOR_YOU);
}
});
client.prepareGet(getTargetUrl()).execute(new AsyncCompletionHandlerBase() {
@Override
public void onThrowable(Throwable t) {
if (t.getMessage() != null && t.getMessage().equalsIgnoreCase(THIS_IS_NOT_FOR_YOU)) {
count.incrementAndGet();
}
}
@Override
public Response onCompleted(Response response) throws Exception {
latch.countDown();
return response;
}
});
}
latch.await(TIMEOUT, TimeUnit.SECONDS);
assertEquals(count.get(), 0);
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class ConnectionPoolTest method multipleMaxConnectionOpenTest.
@Test(groups = "standalone", expectedExceptions = TooManyConnectionsException.class)
public void multipleMaxConnectionOpenTest() throws Throwable {
try (AsyncHttpClient c = asyncHttpClient(config().setKeepAlive(true).setConnectTimeout(5000).setMaxConnections(1))) {
String body = "hello there";
// once
Response response = c.preparePost(getTargetUrl()).setBody(body).execute().get(TIMEOUT, TimeUnit.SECONDS);
assertEquals(response.getResponseBody(), body);
// twice
Exception exception = null;
try {
c.preparePost(String.format("http://localhost:%d/foo/test", port2)).setBody(body).execute().get(TIMEOUT, TimeUnit.SECONDS);
fail("Should throw exception. Too many connections issued.");
} catch (Exception ex) {
ex.printStackTrace();
exception = ex;
}
assertNotNull(exception);
throw exception.getCause();
}
}
use of org.asynchttpclient.Response in project async-http-client by AsyncHttpClient.
the class ConnectionPoolTest method win7DisconnectTest.
/**
* This test just make sure the hack used to catch disconnected channel under win7 doesn't throw any exception. The onComplete method must be only called once.
*
* @throws Exception if something wrong happens.
*/
@Test(groups = "standalone")
public void win7DisconnectTest() throws Exception {
final AtomicInteger count = new AtomicInteger(0);
try (AsyncHttpClient client = asyncHttpClient()) {
AsyncCompletionHandler<Response> handler = new AsyncCompletionHandlerAdapter() {
@Override
public Response onCompleted(Response response) throws Exception {
count.incrementAndGet();
StackTraceElement e = new StackTraceElement("sun.nio.ch.SocketDispatcher", "read0", null, -1);
IOException t = new IOException();
t.setStackTrace(new StackTraceElement[] { e });
throw t;
}
};
try {
client.prepareGet(getTargetUrl()).execute(handler).get();
fail("Must have received an exception");
} catch (ExecutionException ex) {
assertNotNull(ex);
assertNotNull(ex.getCause());
assertEquals(ex.getCause().getClass(), IOException.class);
assertEquals(count.get(), 1);
}
}
}
use of org.asynchttpclient.Response 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.Response 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");
}
}
Aggregations