Search in sources :

Example 6 with Http2NettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testHeaderSize.

public void testHeaderSize(AbstractNettyStreamClient client, int headerSize, int expectedResult) throws Exception {
    Server server = new HttpServerBuilder().headerSize(headerSize).build();
    try {
        server.start();
        RestRequest r = new RestRequestBuilder(new URI(URL)).build();
        FutureCallback<StreamResponse> cb = new FutureCallback<>();
        TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
        client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
        cb.get(300, TimeUnit.SECONDS);
        if (expectedResult == TOO_LARGE) {
            Assert.fail("Max header size exceeded, expected exception. ");
        }
    } catch (ExecutionException e) {
        if (expectedResult == RESPONSE_OK) {
            Assert.fail("Unexpected ExecutionException, header was <= max header size.", e);
        }
        if (client instanceof HttpNettyStreamClient) {
            verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
        } else if (client instanceof Http2NettyStreamClient) {
            verifyCauseChain(e, RemoteInvocationException.class, Http2Exception.class);
        } else {
            Assert.fail("Unrecognized client");
        }
    } finally {
        server.stop();
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) TooLongFrameException(io.netty.handler.codec.TooLongFrameException) Server(org.eclipse.jetty.server.Server) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) URI(java.net.URI) HttpNettyStreamClient(com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) Http2NettyStreamClient(com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RemoteInvocationException(com.linkedin.r2.RemoteInvocationException) ExecutionException(java.util.concurrent.ExecutionException) FutureCallback(com.linkedin.common.callback.FutureCallback)

Example 7 with Http2NettyStreamClient

use of com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient in project rest.li by linkedin.

the class TestHttp2NettyStreamClient method testRequestTimeout.

/**
 * Tests the condition that when a client request times out before the request is processed
 * by the server, the servlet implementation throws when attempting to read the request entity.
 */
@Test(enabled = false)
public void testRequestTimeout() throws Exception {
    final AtomicInteger serverIOExceptions = new AtomicInteger(0);
    final CountDownLatch exceptionLatch = new CountDownLatch(1);
    final CountDownLatch responseLatch = new CountDownLatch(1);
    final CountDownLatch serverLatch = new CountDownLatch(1);
    final HttpServerBuilder serverBuilder = new HttpServerBuilder();
    final Server server = serverBuilder.exceptionListener(throwable -> {
        if (throwable instanceof IOException) {
            serverIOExceptions.incrementAndGet();
            exceptionLatch.countDown();
        }
    }).responseLatch(serverLatch).build();
    final HttpClientBuilder clientBuilder = new HttpClientBuilder(_eventLoop, _scheduler);
    final Http2NettyStreamClient client = clientBuilder.setRequestTimeout(500).buildHttp2StreamClient();
    try {
        server.start();
        // Sends the stream request
        final StreamRequestBuilder builder = new StreamRequestBuilder(new URI(URL));
        final StreamRequest request = builder.setMethod(METHOD).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[REQUEST_SIZE]))));
        client.streamRequest(request, new RequestContext(), new HashMap<>(), response -> responseLatch.countDown());
        // Waits for request to timeout
        Thread.sleep(1000);
        // Allows server to process request
        serverLatch.countDown();
    } finally {
        if (!responseLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)) {
            Assert.fail("Timeout waiting for response latch");
        }
        if (!exceptionLatch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS)) {
            Assert.fail("Timeout waiting for exception latch");
        }
        server.stop();
    }
    // Expects two IOExceptions thrown by the server. One for the initial OPTIONS upgrade request and one for
    // the actual GET request.
    Assert.assertEquals(serverIOExceptions.get(), 2);
}
Also used : Server(org.eclipse.jetty.server.Server) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) IOException(java.io.IOException) HttpClientBuilder(com.linkedin.r2.transport.http.client.HttpClientBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) Test(org.testng.annotations.Test)

Aggregations

RequestContext (com.linkedin.r2.message.RequestContext)6 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)5 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)5 URI (java.net.URI)5 Server (org.eclipse.jetty.server.Server)5 Test (org.testng.annotations.Test)5 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)4 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)4 HttpClientBuilder (com.linkedin.r2.transport.http.client.HttpClientBuilder)4 FutureTransportCallback (com.linkedin.r2.transport.common.bridge.common.FutureTransportCallback)3 HttpNettyStreamClient (com.linkedin.r2.transport.http.client.stream.http.HttpNettyStreamClient)3 Http2NettyStreamClient (com.linkedin.r2.transport.http.client.stream.http2.Http2NettyStreamClient)3 FutureCallback (com.linkedin.common.callback.FutureCallback)2 RestRequest (com.linkedin.r2.message.rest.RestRequest)2 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)2 DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)2 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)2 HttpProtocolVersion (com.linkedin.r2.transport.http.common.HttpProtocolVersion)2 ByteString (com.linkedin.data.ByteString)1