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));
}
Aggregations