Search in sources :

Example 6 with HttpStreamResetException

use of org.apache.hc.core5.http.HttpStreamResetException in project httpcomponents-core by apache.

the class ReactiveClientTest method testResponseCancellation.

@Test
public void testResponseCancellation() throws Exception {
    final InetSocketAddress address = startClientAndServer();
    final AtomicBoolean requestPublisherWasCancelled = new AtomicBoolean(false);
    final AtomicReference<Throwable> requestStreamError = new AtomicReference<>();
    final Publisher<ByteBuffer> stream = ReactiveTestUtils.produceStream(Long.MAX_VALUE, 1024, null).doOnCancel(() -> requestPublisherWasCancelled.set(true)).doOnError(requestStreamError::set);
    final ReactiveEntityProducer producer = new ReactiveEntityProducer(stream, -1, null, null);
    final BasicRequestProducer request = getRequestProducer(address, producer);
    final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer();
    final Future<Void> future = requester.execute(request, consumer, SOCKET_TIMEOUT, null);
    final Message<HttpResponse, Publisher<ByteBuffer>> response = consumer.getResponseFuture().get(RESULT_TIMEOUT.getDuration(), RESULT_TIMEOUT.getTimeUnit());
    final AtomicBoolean responsePublisherWasCancelled = new AtomicBoolean(false);
    final List<ByteBuffer> outputBuffers = Flowable.fromPublisher(response.getBody()).doOnCancel(() -> responsePublisherWasCancelled.set(true)).take(3).toList().blockingGet();
    Assertions.assertEquals(3, outputBuffers.size());
    Assertions.assertTrue(responsePublisherWasCancelled.get(), "The response subscription should have been cancelled");
    final Exception exception = Assertions.assertThrows(Exception.class, () -> future.get(RESULT_TIMEOUT.getDuration(), RESULT_TIMEOUT.getTimeUnit()));
    assertThat(exception, CoreMatchers.anyOf(CoreMatchers.instanceOf(CancellationException.class), CoreMatchers.instanceOf(ExecutionException.class)));
    Assertions.assertTrue(exception.getCause() instanceof HttpStreamResetException);
    Assertions.assertTrue(requestPublisherWasCancelled.get());
    Assertions.assertNull(requestStreamError.get());
}
Also used : InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) HttpResponse(org.apache.hc.core5.http.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) Publisher(org.reactivestreams.Publisher) ByteBuffer(java.nio.ByteBuffer) CancellationException(java.util.concurrent.CancellationException) HttpStreamResetException(org.apache.hc.core5.http.HttpStreamResetException) SocketTimeoutException(java.net.SocketTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ReactiveEntityProducer(org.apache.hc.core5.reactive.ReactiveEntityProducer) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ReactiveResponseConsumer(org.apache.hc.core5.reactive.ReactiveResponseConsumer) HttpStreamResetException(org.apache.hc.core5.http.HttpStreamResetException) Test(org.junit.Test)

Example 7 with HttpStreamResetException

use of org.apache.hc.core5.http.HttpStreamResetException in project httpcomponents-core by apache.

the class ReactiveClientTest method testRequestError.

@Test
public void testRequestError() throws Exception {
    final InetSocketAddress address = startClientAndServer();
    final RuntimeException exceptionThrown = new RuntimeException("Test");
    final Publisher<ByteBuffer> publisher = Flowable.error(exceptionThrown);
    final ReactiveEntityProducer producer = new ReactiveEntityProducer(publisher, 100, null, null);
    final BasicRequestProducer request = getRequestProducer(address, producer);
    final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer();
    final Future<Void> future = requester.execute(request, consumer, SOCKET_TIMEOUT, null);
    final ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () -> future.get(RESULT_TIMEOUT.getDuration(), RESULT_TIMEOUT.getTimeUnit()));
    Assertions.assertTrue(exception.getCause() instanceof HttpStreamResetException);
    Assertions.assertSame(exceptionThrown, exception.getCause().getCause());
}
Also used : ReactiveEntityProducer(org.apache.hc.core5.reactive.ReactiveEntityProducer) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) ReactiveResponseConsumer(org.apache.hc.core5.reactive.ReactiveResponseConsumer) ExecutionException(java.util.concurrent.ExecutionException) HttpStreamResetException(org.apache.hc.core5.http.HttpStreamResetException) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ByteBuffer (java.nio.ByteBuffer)7 HttpStreamResetException (org.apache.hc.core5.http.HttpStreamResetException)7 InetSocketAddress (java.net.InetSocketAddress)3 ExecutionException (java.util.concurrent.ExecutionException)3 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)3 ReactiveEntityProducer (org.apache.hc.core5.reactive.ReactiveEntityProducer)3 ReactiveResponseConsumer (org.apache.hc.core5.reactive.ReactiveResponseConsumer)3 Test (org.junit.Test)3 SocketTimeoutException (java.net.SocketTimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)2 Test (org.junit.jupiter.api.Test)2 Iterator (java.util.Iterator)1 Map (java.util.Map)1 CancellationException (java.util.concurrent.CancellationException)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 HttpException (org.apache.hc.core5.http.HttpException)1 HttpResponse (org.apache.hc.core5.http.HttpResponse)1