Search in sources :

Example 1 with Cancellable

use of org.apache.hc.core5.concurrent.Cancellable in project httpcomponents-core by apache.

the class H2ServerAndMultiplexingRequesterTest method testMultiplexedRequestCancellation.

@Test
public void testMultiplexedRequestCancellation() throws Exception {
    server.start();
    final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), scheme);
    final ListenerEndpoint listener = future.get();
    final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
    requester.start();
    final int reqNo = 20;
    final CountDownLatch countDownLatch = new CountDownLatch(reqNo);
    final Random random = new Random();
    final HttpHost target = new HttpHost(scheme.id, "localhost", address.getPort());
    for (int i = 0; i < reqNo; i++) {
        final Cancellable cancellable = requester.execute(new BasicClientExchangeHandler<>(new BasicRequestProducer(Method.POST, target, "/stuff", new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), new FutureCallback<Message<HttpResponse, String>>() {

            @Override
            public void completed(final Message<HttpResponse, String> result) {
                countDownLatch.countDown();
            }

            @Override
            public void failed(final Exception ex) {
                countDownLatch.countDown();
            }

            @Override
            public void cancelled() {
                countDownLatch.countDown();
            }
        }), TIMEOUT, HttpCoreContext.create());
        Thread.sleep(random.nextInt(10));
        cancellable.cancel();
    }
    assertThat(countDownLatch.await(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()), CoreMatchers.equalTo(true));
}
Also used : StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) Message(org.apache.hc.core5.http.Message) InetSocketAddress(java.net.InetSocketAddress) Cancellable(org.apache.hc.core5.concurrent.Cancellable) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) StringAsyncEntityProducer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) Random(java.util.Random) ListenerEndpoint(org.apache.hc.core5.reactor.ListenerEndpoint) HttpHost(org.apache.hc.core5.http.HttpHost) BasicResponseConsumer(org.apache.hc.core5.http.nio.support.BasicResponseConsumer) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback) Test(org.junit.Test)

Aggregations

InetSocketAddress (java.net.InetSocketAddress)1 Random (java.util.Random)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Cancellable (org.apache.hc.core5.concurrent.Cancellable)1 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)1 HttpHost (org.apache.hc.core5.http.HttpHost)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1 Message (org.apache.hc.core5.http.Message)1 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)1 StringAsyncEntityProducer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityProducer)1 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)1 BasicResponseConsumer (org.apache.hc.core5.http.nio.support.BasicResponseConsumer)1 ListenerEndpoint (org.apache.hc.core5.reactor.ListenerEndpoint)1 Test (org.junit.Test)1