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