Search in sources :

Example 86 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testResponseSize.

public void testResponseSize(AbstractNettyStreamClient client, int responseSize, int expectedResult) throws Exception {
    Server server = new HttpServerBuilder().responseSize(responseSize).build();
    try {
        server.start();
        RestRequest r = new RestRequestBuilder(new URI(URL)).build();
        FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
        TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
        client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
        StreamResponse response = cb.get(30, TimeUnit.SECONDS);
        final CountDownLatch latch = new CountDownLatch(1);
        final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
        response.getEntityStream().setReader(new Reader() {

            @Override
            public void onInit(ReadHandle rh) {
                rh.request(Integer.MAX_VALUE);
            }

            @Override
            public void onDataAvailable(ByteString data) {
            }

            @Override
            public void onDone() {
                latch.countDown();
            }

            @Override
            public void onError(Throwable e) {
                error.set(e);
                latch.countDown();
            }
        });
        if (!latch.await(30, TimeUnit.SECONDS)) {
            Assert.fail("Timeout waiting for response");
        }
        if (expectedResult == TOO_LARGE) {
            Assert.assertNotNull(error.get(), "Max response size exceeded, expected exception. ");
            verifyCauseChain(error.get(), TooLongFrameException.class);
        }
        if (expectedResult == RESPONSE_OK) {
            Assert.assertNull(error.get(), "Unexpected Exception: response size <= max size");
        }
    } catch (ExecutionException e) {
        if (expectedResult == RESPONSE_OK) {
            Assert.fail("Unexpected ExecutionException, response was <= max response size.", e);
        }
        verifyCauseChain(e, RemoteInvocationException.class, TooLongFrameException.class);
    } 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) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Reader(com.linkedin.r2.message.stream.entitystream.Reader) AtomicReference(java.util.concurrent.atomic.AtomicReference) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) 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)

Example 87 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testShutdownRequestOutstanding.

private void testShutdownRequestOutstanding(AbstractNettyStreamClient client, Class<?>... causeChain) throws Exception {
    CountDownLatch responseLatch = new CountDownLatch(1);
    Server server = new HttpServerBuilder().responseLatch(responseLatch).build();
    try {
        server.start();
        RestRequest r = new RestRequestBuilder(new URI(URL)).build();
        FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
        TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
        client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), callback);
        FutureCallback<None> shutdownCallback = new FutureCallback<None>();
        client.shutdown(shutdownCallback);
        shutdownCallback.get(30, TimeUnit.SECONDS);
        // 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("Get timed out, should have thrown ExecutionException", e);
    } catch (ExecutionException e) {
        verifyCauseChain(e, causeChain);
    } finally {
        responseLatch.countDown();
        server.stop();
    }
}
Also used : TransportCallbackAdapter(com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter) Server(org.eclipse.jetty.server.Server) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) ExecutionException(java.util.concurrent.ExecutionException) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException)

Example 88 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testBadAddress.

@Test(dataProvider = "badAddressClients")
public void testBadAddress(AbstractNettyStreamClient client) throws InterruptedException, IOException, TimeoutException {
    RestRequest r = new RestRequestBuilder(URI.create("http://this.host.does.not.exist.linkedin.com")).build();
    FutureCallback<StreamResponse> cb = new FutureCallback<StreamResponse>();
    TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<StreamResponse>(cb);
    client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), 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) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) 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 89 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testShutdownStuckInPool.

@Test
public void testShutdownStuckInPool() throws InterruptedException, ExecutionException, TimeoutException {
    // Test that shutdown works when the outstanding request is stuck in the pool waiting for a channel
    HttpNettyStreamClient client = new HttpNettyStreamClient(new NoCreations(_scheduler), _scheduler, 60000, 1, 1024 * 1024 * 2);
    RestRequest r = new RestRequestBuilder(URI.create("http://some.host/")).build();
    FutureCallback<StreamResponse> futureCallback = new FutureCallback<StreamResponse>();
    client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<String, String>(), new TransportCallbackAdapter<StreamResponse>(futureCallback));
    FutureCallback<None> shutdownCallback = new FutureCallback<None>();
    client.shutdown(shutdownCallback);
    shutdownCallback.get(30, TimeUnit.SECONDS);
    try {
        futureCallback.get(30, TimeUnit.SECONDS);
        Assert.fail("get should have thrown exception");
    } catch (ExecutionException e) {
        verifyCauseChain(e, RemoteInvocationException.class, TimeoutException.class);
    }
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AsciiString(io.netty.util.AsciiString) ByteString(com.linkedin.data.ByteString) 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) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 90 with FutureCallback

use of com.linkedin.common.callback.FutureCallback in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testStreamRequests.

/**
   * Tests implementations of {@link AbstractNettyStreamClient} with different request dimensions.
   *
   * @param client Client implementation of {@link AbstractNettyStreamClient}
   * @param method HTTP request method
   * @param requestSize Request content size
   * @param responseSize Response content size
   * @param isFullRequest Whether to buffer a full request before stream
   * @throws Exception
   */
@Test(dataProvider = "requestResponseParameters")
public void testStreamRequests(AbstractNettyStreamClient client, String method, int requestSize, int responseSize, boolean isFullRequest) throws Exception {
    AtomicInteger succeeded = new AtomicInteger(0);
    AtomicInteger failed = new AtomicInteger(0);
    Server server = new HttpServerBuilder().responseSize(responseSize).build();
    try {
        server.start();
        CountDownLatch latch = new CountDownLatch(REQUEST_COUNT);
        for (int i = 0; i < REQUEST_COUNT; i++) {
            StreamRequest request = new StreamRequestBuilder(new URI(URL)).setMethod(method).setHeader(HttpHeaderNames.HOST.toString(), HOST_NAME.toString()).build(EntityStreams.newEntityStream(new ByteStringWriter(ByteString.copy(new byte[requestSize]))));
            RequestContext context = new RequestContext();
            context.putLocalAttr(R2Constants.IS_FULL_REQUEST, isFullRequest);
            client.streamRequest(request, context, new HashMap<>(), new TransportCallbackAdapter<>(new Callback<StreamResponse>() {

                @Override
                public void onSuccess(StreamResponse response) {
                    response.getEntityStream().setReader(new Reader() {

                        ReadHandle _rh;

                        int _consumed = 0;

                        @Override
                        public void onDataAvailable(ByteString data) {
                            _consumed += data.length();
                            _rh.request(1);
                        }

                        @Override
                        public void onDone() {
                            succeeded.incrementAndGet();
                            latch.countDown();
                        }

                        @Override
                        public void onError(Throwable e) {
                            failed.incrementAndGet();
                            latch.countDown();
                        }

                        @Override
                        public void onInit(ReadHandle rh) {
                            _rh = rh;
                            _rh.request(1);
                        }
                    });
                }

                @Override
                public void onError(Throwable e) {
                    failed.incrementAndGet();
                    latch.countDown();
                }
            }));
        }
        if (!latch.await(30, TimeUnit.SECONDS)) {
            Assert.fail("Timeout waiting for responses. " + succeeded + " requests succeeded and " + failed + " requests failed out of total " + REQUEST_COUNT + " requests");
        }
        Assert.assertEquals(latch.getCount(), 0);
        Assert.assertEquals(failed.get(), 0);
        Assert.assertEquals(succeeded.get(), REQUEST_COUNT);
        FutureCallback<None> shutdownCallback = new FutureCallback<>();
        client.shutdown(shutdownCallback);
        shutdownCallback.get(30, TimeUnit.SECONDS);
    } finally {
        server.stop();
    }
}
Also used : Server(org.eclipse.jetty.server.Server) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Reader(com.linkedin.r2.message.stream.entitystream.Reader) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) FutureCallback(com.linkedin.common.callback.FutureCallback) Callback(com.linkedin.common.callback.Callback) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) RequestContext(com.linkedin.r2.message.RequestContext) ByteStringWriter(com.linkedin.r2.message.stream.entitystream.ByteStringWriter) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Aggregations

FutureCallback (com.linkedin.common.callback.FutureCallback)152 Test (org.testng.annotations.Test)116 None (com.linkedin.common.util.None)77 RestRequest (com.linkedin.r2.message.rest.RestRequest)50 ExecutionException (java.util.concurrent.ExecutionException)50 RequestContext (com.linkedin.r2.message.RequestContext)47 RestResponse (com.linkedin.r2.message.rest.RestResponse)43 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)36 HashMap (java.util.HashMap)26 CountDownLatch (java.util.concurrent.CountDownLatch)25 TransportCallbackAdapter (com.linkedin.r2.transport.common.bridge.client.TransportCallbackAdapter)22 ByteString (com.linkedin.data.ByteString)21 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)21 URI (java.net.URI)21 TimeoutException (java.util.concurrent.TimeoutException)21 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)20 UriProperties (com.linkedin.d2.balancer.properties.UriProperties)16 ArrayList (java.util.ArrayList)16 RestException (com.linkedin.r2.message.rest.RestException)15 AsyncSharedPoolImpl (com.linkedin.r2.transport.http.client.AsyncSharedPoolImpl)14