Search in sources :

Example 6 with ReactiveResponseConsumer

use of org.apache.hc.core5.reactive.ReactiveResponseConsumer 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 ReactiveResponseConsumer

use of org.apache.hc.core5.reactive.ReactiveResponseConsumer 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)

Example 8 with ReactiveResponseConsumer

use of org.apache.hc.core5.reactive.ReactiveResponseConsumer in project httpcomponents-core by apache.

the class ReactiveClientTest method testSimpleRequest.

@Test
public void testSimpleRequest() throws Exception {
    final InetSocketAddress address = startClientAndServer();
    final byte[] input = new byte[1024];
    RANDOM.nextBytes(input);
    final Publisher<ByteBuffer> publisher = Flowable.just(ByteBuffer.wrap(input));
    final ReactiveEntityProducer producer = new ReactiveEntityProducer(publisher, input.length, null, null);
    final BasicRequestProducer request = getRequestProducer(address, producer);
    final ReactiveResponseConsumer consumer = new ReactiveResponseConsumer();
    requester.execute(request, consumer, SOCKET_TIMEOUT, null);
    final Message<HttpResponse, Publisher<ByteBuffer>> response = consumer.getResponseFuture().get(RESULT_TIMEOUT.getDuration(), RESULT_TIMEOUT.getTimeUnit());
    final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    final WritableByteChannel writableByteChannel = Channels.newChannel(byteArrayOutputStream);
    for (final ByteBuffer byteBuffer : Observable.fromPublisher(response.getBody()).toList().blockingGet()) {
        writableByteChannel.write(byteBuffer);
    }
    writableByteChannel.close();
    final byte[] output = byteArrayOutputStream.toByteArray();
    Assertions.assertArrayEquals(input, output);
}
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) WritableByteChannel(java.nio.channels.WritableByteChannel) HttpResponse(org.apache.hc.core5.http.HttpResponse) Publisher(org.reactivestreams.Publisher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.Test)

Aggregations

ReactiveResponseConsumer (org.apache.hc.core5.reactive.ReactiveResponseConsumer)8 ByteBuffer (java.nio.ByteBuffer)7 ReactiveEntityProducer (org.apache.hc.core5.reactive.ReactiveEntityProducer)7 InetSocketAddress (java.net.InetSocketAddress)6 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)6 Test (org.junit.Test)6 HttpResponse (org.apache.hc.core5.http.HttpResponse)5 Publisher (org.reactivestreams.Publisher)5 ExecutionException (java.util.concurrent.ExecutionException)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)3 HttpStreamResetException (org.apache.hc.core5.http.HttpStreamResetException)3 SocketTimeoutException (java.net.SocketTimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AsyncRequestProducer (org.apache.hc.core5.http.nio.AsyncRequestProducer)2 StreamDescription (org.apache.hc.core5.testing.reactive.ReactiveTestUtils.StreamDescription)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 URI (java.net.URI)1 WritableByteChannel (java.nio.channels.WritableByteChannel)1 Random (java.util.Random)1 CancellationException (java.util.concurrent.CancellationException)1