Search in sources :

Example 16 with HttpClientBuilder

use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testUnsupportedRestRequest.

@Test(expectedExceptions = UnsupportedOperationException.class)
public void testUnsupportedRestRequest() throws UnsupportedOperationException {
    TransportClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildStreamClient();
    client.restRequest(null, new RequestContext(), new HashMap<>(), null);
    Assert.fail("The Http Stream clients should throw UnsupportedOperationException when streamRequest is called");
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 17 with HttpClientBuilder

use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testUnsupportedRestRequestHttp2.

@Test(expectedExceptions = UnsupportedOperationException.class)
public void testUnsupportedRestRequestHttp2() throws UnsupportedOperationException {
    TransportClient client = new HttpClientBuilder(_eventLoop, _scheduler).buildHttp2StreamClient();
    client.restRequest(null, new RequestContext(), new HashMap<>(), null);
    Assert.fail("The Http Stream clients should throw UnsupportedOperationException when streamRequest is called");
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 18 with HttpClientBuilder

use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.

the class TestHttpNettyClient method testNoResponseTimeout.

@Test
public void testNoResponseTimeout() throws InterruptedException, IOException {
    TestServer testServer = new TestServer();
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(500).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
    RestRequest r = new RestRequestBuilder(testServer.getNoResponseURI()).build();
    FutureCallback<RestResponse> cb = new FutureCallback<>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
    client.restRequest(r, new RequestContext(), new HashMap<>(), callback);
    try {
        // This timeout needs to be significantly larger than the getTimeout of the netty client;
        // we're testing that the client will generate its own timeout
        cb.get(30, TimeUnit.SECONDS);
        Assert.fail("Get was supposed to time out");
    } catch (TimeoutException e) {
        // TimeoutException means the timeout for Future.get() elapsed and nothing happened.
        // Instead, we are expecting our callback to be invoked before the future timeout
        // with a timeout generated by the HttpNettyClient.
        Assert.fail("Unexpected TimeoutException, should have been ExecutionException", e);
    } catch (ExecutionException e) {
        verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
    }
    testServer.shutdown();
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) 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) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 19 with HttpClientBuilder

use of com.linkedin.r2.transport.http.client.HttpClientBuilder in project rest.li by linkedin.

the class TestHttpNettyClient method testBadAddress.

@Test
public void testBadAddress() throws InterruptedException, IOException, TimeoutException {
    HttpNettyClient client = new HttpClientBuilder(_eventLoop, _scheduler).setRequestTimeout(30000).setIdleTimeout(10000).setShutdownTimeout(500).buildRestClient();
    RestRequest r = new RestRequestBuilder(URI.create("http://this.host.does.not.exist.linkedin.com")).build();
    FutureCallback<RestResponse> cb = new FutureCallback<>();
    TransportCallback<RestResponse> callback = new TransportCallbackAdapter<>(cb);
    client.restRequest(r, new RequestContext(), new HashMap<>(), callback);
    try {
        cb.get(30, TimeUnit.SECONDS);
        Assert.fail("Get was supposed to fail");
    } catch (ExecutionException e) {
        verifyCauseChain(e, RemoteInvocationException.class, UnknownHostException.class);
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) UnknownHostException(java.net.UnknownHostException) RestResponse(com.linkedin.r2.message.rest.RestResponse) HttpNettyClient(com.linkedin.r2.transport.http.client.rest.HttpNettyClient) RestRequest(com.linkedin.r2.message.rest.RestRequest) 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) Test(org.testng.annotations.Test)

Example 20 with HttpClientBuilder

use of com.linkedin.r2.transport.http.client.HttpClientBuilder 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)19 Test (org.testng.annotations.Test)17 FutureCallback (com.linkedin.common.callback.FutureCallback)13 RestRequest (com.linkedin.r2.message.rest.RestRequest)12 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)12 HttpNettyClient (com.linkedin.r2.transport.http.client.rest.HttpNettyClient)12 RestResponse (com.linkedin.r2.message.rest.RestResponse)11 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)11 ExecutionException (java.util.concurrent.ExecutionException)9 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)7 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)5 TimeoutException (java.util.concurrent.TimeoutException)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 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)4 HttpClientBuilder (com.linkedin.r2.transport.http.client.HttpClientBuilder)4 URI (java.net.URI)4 Server (org.eclipse.jetty.server.Server)4 None (com.linkedin.common.util.None)3