use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ConnectionPoolTest method testMaxTotalConnectionsException.
@Test(groups = "standalone", expectedExceptions = TooManyConnectionsException.class)
public void testMaxTotalConnectionsException() throws Throwable {
try (AsyncHttpClient client = asyncHttpClient(config().setKeepAlive(true).setMaxConnections(1))) {
String url = getTargetUrl();
List<ListenableFuture<Response>> futures = new ArrayList<>();
for (int i = 0; i < 5; i++) {
logger.info("{} requesting url [{}]...", i, url);
futures.add(client.prepareGet(url).execute());
}
Exception exception = null;
for (ListenableFuture<Response> future : futures) {
try {
future.get();
} catch (Exception ex) {
exception = ex;
break;
}
}
assertNotNull(exception);
throw exception.getCause();
}
}
use of org.asynchttpclient.AsyncHttpClient 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.AsyncHttpClient 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.AsyncHttpClient 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.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ConnectionPoolTest method nonPoolableConnectionReleaseSemaphoresTest.
@Test(groups = "standalone")
public void nonPoolableConnectionReleaseSemaphoresTest() throws Throwable {
RequestBuilder request = get(getTargetUrl()).setHeader("Connection", "close");
try (AsyncHttpClient client = asyncHttpClient(config().setMaxConnections(6).setMaxConnectionsPerHost(3))) {
client.executeRequest(request).get();
Thread.sleep(1000);
client.executeRequest(request).get();
Thread.sleep(1000);
client.executeRequest(request).get();
Thread.sleep(1000);
client.executeRequest(request).get();
}
}
Aggregations