Search in sources :

Example 71 with StreamRequest

use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.

the class TestNettyRequestAdapter method testStreamToHttp2HeadersBlacklist.

@Test
public void testStreamToHttp2HeadersBlacklist() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    HEADER_BLACKLIST.forEach(header -> streamRequestBuilder.addHeaderValue(header, ANY_HEADER));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    HEADER_BLACKLIST.forEach(header -> Assert.assertFalse(headers.contains(header), header));
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 72 with StreamRequest

use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.

the class TestNettyRequestAdapter method testStreamToHttp2HeadersPseudoHeaders.

@Test
public void testStreamToHttp2HeadersPseudoHeaders() throws Exception {
    StreamRequestBuilder streamRequestBuilder = new StreamRequestBuilder(new URI(ANY_URI));
    StreamRequest request = streamRequestBuilder.build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(ANY_ENTITY.getBytes()))));
    Http2Headers headers = NettyRequestAdapter.toHttp2Headers(request);
    Assert.assertNotNull(headers);
    Assert.assertEquals(headers.authority(), "localhost:8080");
    Assert.assertEquals(headers.method(), "GET");
    Assert.assertEquals(headers.path(), "/foo/bar?q=baz");
    Assert.assertEquals(headers.scheme(), "http");
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 73 with StreamRequest

use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.

the class TestHttpNettyClient method testUnsupportedStreamRequest.

@Test(expectedExceptions = UnsupportedOperationException.class)
public void testUnsupportedStreamRequest() throws UnsupportedOperationException {
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildRestClient();
    client.streamRequest(null, new RequestContext(), new HashMap<>(), null);
    Assert.fail("The Http Rest client should throw UnsupportedOperationException when streamRequest is called");
}
Also used : RequestContext(com.linkedin.r2.message.RequestContext) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) Test(org.testng.annotations.Test)

Example 74 with StreamRequest

use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testSlowReaderTimeout.

/**
 * Tests slow EntityStream {@link Reader} implementation should be subject to streaming timeout even
 * if the entire response entity can be buffered in memory.
 *
 * @throws Exception
 */
@Test(dataProvider = "slowReaderTimeoutClientProvider")
public void testSlowReaderTimeout(AbstractNettyStreamClient client) throws Exception {
    // Sets the response size to be greater than zero but smaller than the in-memory buffer for HTTP/1.1
    // and smaller than the receiving window size for HTTP/2 so the receiver will not block sender
    Server server = new HttpServerBuilder().responseSize(R2Constants.DEFAULT_DATA_CHUNK_SIZE).build();
    StreamRequest request = new StreamRequestBuilder(new URI(URL)).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.emptyStream());
    final CountDownLatch responseLatch = new CountDownLatch(1);
    final CountDownLatch streamLatch = new CountDownLatch(1);
    final AtomicReference<TransportResponse<StreamResponse>> atomicTransportResponse = new AtomicReference<>();
    final AtomicReference<Throwable> atomicThrowable = new AtomicReference<>();
    try {
        server.start();
        client.streamRequest(request, new RequestContext(), new HashMap<>(), response -> {
            atomicTransportResponse.set(response);
            responseLatch.countDown();
            // Sets a reader that does not consume any byte
            response.getResponse().getEntityStream().setReader(new Reader() {

                @Override
                public void onInit(ReadHandle rh) {
                }

                @Override
                public void onDataAvailable(ByteString data) {
                }

                @Override
                public void onDone() {
                }

                @Override
                public void onError(Throwable e) {
                    atomicThrowable.set(e);
                    streamLatch.countDown();
                }
            });
        });
    } finally {
        responseLatch.await(5, TimeUnit.SECONDS);
        streamLatch.await(5, TimeUnit.SECONDS);
        server.stop();
    }
    TransportResponse<StreamResponse> transportResponse = atomicTransportResponse.get();
    Assert.assertNotNull(transportResponse, "Expected to receive a response");
    Assert.assertFalse(transportResponse.hasError(), "Expected to receive a response without error");
    Assert.assertNotNull(transportResponse.getResponse());
    Assert.assertNotNull(transportResponse.getResponse().getEntityStream());
    Throwable throwable = atomicThrowable.get();
    Assert.assertNotNull(throwable, "Expected onError invoked with TimeoutException");
    Assert.assertTrue(throwable instanceof RemoteInvocationException);
    Assert.assertNotNull(throwable.getCause());
    Assert.assertTrue(throwable.getCause() instanceof TimeoutException);
}
Also used : Server(org.eclipse.jetty.server.Server) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) Reader(com.linkedin.r2.message.stream.entitystream.Reader) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 75 with StreamRequest

use of com.linkedin.r2.message.stream.StreamRequest in project rest.li by linkedin.

the class RAPStreamRequestEncoder method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
    if (msg instanceof StreamRequest) {
        StreamRequest request = (StreamRequest) msg;
        HttpRequest nettyRequest = NettyRequestAdapter.toNettyRequest(request);
        ctx.write(nettyRequest, promise);
        _currentReader = new OrderedEntityStreamReader(ctx, new BufferedReader(ctx, MAX_BUFFERED_CHUNKS, FLUSH_THRESHOLD));
        request.getEntityStream().setReader(_currentReader);
    } else {
        _currentReader = null;
        ctx.write(msg, promise);
    }
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) OrderedEntityStreamReader(com.linkedin.r2.transport.http.client.stream.OrderedEntityStreamReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Aggregations

StreamRequest (com.linkedin.r2.message.stream.StreamRequest)173 Test (org.testng.annotations.Test)132 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)121 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)116 URI (java.net.URI)84 RequestContext (com.linkedin.r2.message.RequestContext)82 CountDownLatch (java.util.concurrent.CountDownLatch)51 ByteString (com.linkedin.data.ByteString)46 RestRequest (com.linkedin.r2.message.rest.RestRequest)45 Callback (com.linkedin.common.callback.Callback)38 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)34 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)32 HashMap (java.util.HashMap)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)29 BeforeTest (org.testng.annotations.BeforeTest)26 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)25 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)24 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)23 RestResponse (com.linkedin.r2.message.rest.RestResponse)23 AfterTest (org.testng.annotations.AfterTest)23