use of org.apache.hc.core5.http.nio.support.AbstractAsyncPushHandler in project httpcomponents-core by apache.
the class H2CompatibilityTest method executeH2.
void executeH2(final HttpHost target) throws Exception {
{
System.out.println("*** HTTP/2 simple request execution ***");
final Future<AsyncClientEndpoint> connectFuture = client.connect(target, TIMEOUT, HttpVersionPolicy.FORCE_HTTP_2, null);
try {
final AsyncClientEndpoint endpoint = connectFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
final CountDownLatch countDownLatch = new CountDownLatch(1);
final HttpRequest httpget = new BasicHttpRequest(Method.GET, target, "/status.html");
endpoint.execute(new BasicRequestProducer(httpget, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), new FutureCallback<Message<HttpResponse, String>>() {
@Override
public void completed(final Message<HttpResponse, String> responseMessage) {
final HttpResponse response = responseMessage.getHead();
final int code = response.getCode();
if (code == HttpStatus.SC_OK) {
logResult(TestResult.OK, target, httpget, response, Objects.toString(response.getFirstHeader("server")));
} else {
logResult(TestResult.NOK, target, httpget, response, "(status " + code + ")");
}
countDownLatch.countDown();
}
@Override
public void failed(final Exception ex) {
logResult(TestResult.NOK, target, httpget, null, "(" + ex.getMessage() + ")");
countDownLatch.countDown();
}
@Override
public void cancelled() {
logResult(TestResult.NOK, target, httpget, null, "(cancelled)");
countDownLatch.countDown();
}
});
if (!countDownLatch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit())) {
logResult(TestResult.NOK, target, null, null, "(single request execution failed to complete in time)");
}
} catch (final ExecutionException ex) {
final Throwable cause = ex.getCause();
logResult(TestResult.NOK, target, null, null, "(" + cause.getMessage() + ")");
} catch (final TimeoutException ex) {
logResult(TestResult.NOK, target, null, null, "(time out)");
}
}
{
System.out.println("*** HTTP/2 multiplexed request execution ***");
final Future<AsyncClientEndpoint> connectFuture = client.connect(target, TIMEOUT, HttpVersionPolicy.FORCE_HTTP_2, null);
try {
final AsyncClientEndpoint endpoint = connectFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
final int reqCount = 20;
final CountDownLatch countDownLatch = new CountDownLatch(reqCount);
for (int i = 0; i < reqCount; i++) {
final HttpRequest httpget = new BasicHttpRequest(Method.GET, target, "/status.html");
endpoint.execute(new BasicRequestProducer(httpget, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), new FutureCallback<Message<HttpResponse, String>>() {
@Override
public void completed(final Message<HttpResponse, String> responseMessage) {
final HttpResponse response = responseMessage.getHead();
final int code = response.getCode();
if (code == HttpStatus.SC_OK) {
logResult(TestResult.OK, target, httpget, response, "multiplexed / " + response.getFirstHeader("server"));
} else {
logResult(TestResult.NOK, target, httpget, response, "(status " + code + ")");
}
countDownLatch.countDown();
}
@Override
public void failed(final Exception ex) {
logResult(TestResult.NOK, target, httpget, null, "(" + ex.getMessage() + ")");
countDownLatch.countDown();
}
@Override
public void cancelled() {
logResult(TestResult.NOK, target, httpget, null, "(cancelled)");
countDownLatch.countDown();
}
});
}
if (!countDownLatch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit())) {
logResult(TestResult.NOK, target, null, null, "(multiplexed request execution failed to complete in time)");
}
} catch (final ExecutionException ex) {
final Throwable cause = ex.getCause();
logResult(TestResult.NOK, target, null, null, "(" + cause.getMessage() + ")");
} catch (final TimeoutException ex) {
logResult(TestResult.NOK, target, null, null, "(time out)");
}
}
{
System.out.println("*** HTTP/2 request execution with push ***");
final Future<AsyncClientEndpoint> connectFuture = client.connect(target, TIMEOUT, HttpVersionPolicy.FORCE_HTTP_2, null);
try {
final AsyncClientEndpoint endpoint = connectFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
final CountDownLatch countDownLatch = new CountDownLatch(5);
final HttpRequest httpget = new BasicHttpRequest(Method.GET, target, "/index.html");
final Future<Message<HttpResponse, String>> future = endpoint.execute(new BasicRequestProducer(httpget, null), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), (request, context) -> new AbstractAsyncPushHandler<Message<HttpResponse, Void>>(new BasicResponseConsumer<>(new DiscardingEntityConsumer<>())) {
@Override
protected void handleResponse(final HttpRequest promise, final Message<HttpResponse, Void> responseMessage) throws IOException, HttpException {
final HttpResponse response = responseMessage.getHead();
logResult(TestResult.OK, target, promise, response, "pushed / " + response.getFirstHeader("server"));
countDownLatch.countDown();
}
@Override
protected void handleError(final HttpRequest promise, final Exception cause) {
logResult(TestResult.NOK, target, promise, null, "(" + cause.getMessage() + ")");
countDownLatch.countDown();
}
}, null, null);
final Message<HttpResponse, String> message = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
final HttpResponse response = message.getHead();
final int code = response.getCode();
if (code == HttpStatus.SC_OK) {
logResult(TestResult.OK, target, httpget, response, Objects.toString(response.getFirstHeader("server")));
if (!countDownLatch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit())) {
logResult(TestResult.NOK, target, null, null, "Push messages not received");
}
} else {
logResult(TestResult.NOK, target, httpget, response, "(status " + code + ")");
}
} catch (final ExecutionException ex) {
final Throwable cause = ex.getCause();
logResult(TestResult.NOK, target, null, null, "(" + cause.getMessage() + ")");
} catch (final TimeoutException ex) {
logResult(TestResult.NOK, target, null, null, "(time out)");
}
}
}
use of org.apache.hc.core5.http.nio.support.AbstractAsyncPushHandler in project httpcomponents-core by apache.
the class H2IntegrationTest method testPush.
@Test
public void testPush() throws Exception {
final InetSocketAddress serverEndpoint = server.start();
server.register("/hello", () -> new MessageExchangeHandler<Void>(new DiscardingEntityConsumer<>()) {
@Override
protected void handle(final Message<HttpRequest, Void> request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws IOException, HttpException {
responseTrigger.pushPromise(new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/stuff")), context, new BasicPushProducer(new MultiLineEntityProducer("Pushing lots of stuff", 500)));
responseTrigger.submitResponse(AsyncResponseBuilder.create(HttpStatus.SC_OK).setEntity("Hi there", ContentType.TEXT_PLAIN).build(), context);
}
});
client.start(H2Config.custom().setPushEnabled(true).build());
final BlockingQueue<Message<HttpResponse, String>> pushMessageQueue = new LinkedBlockingDeque<>();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), (request, context) -> new AbstractAsyncPushHandler<Message<HttpResponse, String>>(new BasicResponseConsumer<>(new StringAsyncEntityConsumer())) {
@Override
protected void handleResponse(final HttpRequest promise, final Message<HttpResponse, String> responseMessage) throws IOException, HttpException {
try {
pushMessageQueue.put(responseMessage);
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new InterruptedIOException(ex.getMessage());
}
}
}, null, null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
final String entity1 = result1.getBody();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
Assertions.assertEquals("Hi there", entity1);
final Message<HttpResponse, String> result2 = pushMessageQueue.poll(5, TimeUnit.SECONDS);
Assertions.assertNotNull(result2);
final HttpResponse response2 = result2.getHead();
final String entity2 = result2.getBody();
Assertions.assertEquals(200, response2.getCode());
Assertions.assertNotNull(entity2);
final StringTokenizer t1 = new StringTokenizer(entity2, "\r\n");
while (t1.hasMoreTokens()) {
Assertions.assertEquals("Pushing lots of stuff", t1.nextToken());
}
}
Aggregations