use of org.asynchttpclient.AsyncCompletionHandler in project async-http-client by AsyncHttpClient.
the class ZeroCopyFileTest method zeroCopyPostTest.
@Test(groups = "standalone")
public void zeroCopyPostTest() throws IOException, ExecutionException, TimeoutException, InterruptedException, URISyntaxException {
try (AsyncHttpClient client = asyncHttpClient()) {
final AtomicBoolean headerSent = new AtomicBoolean(false);
final AtomicBoolean operationCompleted = new AtomicBoolean(false);
Response resp = client.preparePost("http://localhost:" + port1 + "/").setBody(SIMPLE_TEXT_FILE).execute(new AsyncCompletionHandler<Response>() {
public State onHeadersWritten() {
headerSent.set(true);
return State.CONTINUE;
}
public State onContentWritten() {
operationCompleted.set(true);
return State.CONTINUE;
}
@Override
public Response onCompleted(Response response) throws Exception {
return response;
}
}).get();
assertNotNull(resp);
assertEquals(resp.getStatusCode(), HttpServletResponse.SC_OK);
assertEquals(resp.getResponseBody(), SIMPLE_TEXT_FILE_STRING);
assertTrue(operationCompleted.get());
assertTrue(headerSent.get());
}
}
use of org.asynchttpclient.AsyncCompletionHandler in project async-http-client by AsyncHttpClient.
the class NettyRequestThrottleTimeoutTest method testRequestTimeout.
@Test(groups = "standalone")
public void testRequestTimeout() throws IOException {
final Semaphore requestThrottle = new Semaphore(1);
int samples = 10;
try (AsyncHttpClient client = asyncHttpClient(config().setMaxConnections(1))) {
final CountDownLatch latch = new CountDownLatch(samples);
final List<Exception> tooManyConnections = Collections.synchronizedList(new ArrayList<>(2));
for (int i = 0; i < samples; i++) {
new Thread(new Runnable() {
public void run() {
try {
requestThrottle.acquire();
Future<Response> responseFuture = null;
try {
responseFuture = client.prepareGet(getTargetUrl()).setRequestTimeout(SLEEPTIME_MS / 2).execute(new AsyncCompletionHandler<Response>() {
@Override
public Response onCompleted(Response response) throws Exception {
return response;
}
@Override
public void onThrowable(Throwable t) {
logger.error("onThrowable got an error", t);
try {
Thread.sleep(100);
} catch (InterruptedException e) {
}
requestThrottle.release();
}
});
} catch (Exception e) {
tooManyConnections.add(e);
}
if (responseFuture != null)
responseFuture.get();
} catch (Exception e) {
} finally {
latch.countDown();
}
}
}).start();
}
try {
latch.await(30, TimeUnit.SECONDS);
} catch (Exception e) {
fail("failed to wait for requests to complete");
}
for (Exception e : tooManyConnections) logger.error("Exception while calling execute", e);
assertTrue(tooManyConnections.isEmpty(), "Should not have any connection errors where too many connections have been attempted");
}
}
Aggregations