Search in sources :

Example 96 with Timeout

use of com.linkedin.r2.util.Timeout 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<>();
        TransportCallback<StreamResponse> callback = new TransportCallbackAdapter<>(cb);
        client.streamRequest(Messages.toStreamRequest(r), new RequestContext(), new HashMap<>(), callback);
        FutureCallback<None> shutdownCallback = new FutureCallback<>();
        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(60, 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) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) 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 97 with Timeout

use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.

the class TestHttpNettyStreamClient method testCancelStreamRequests.

@Test(dataProvider = "requestResponseParameters", enabled = false)
public void testCancelStreamRequests(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() {

                        @Override
                        public void onDataAvailable(ByteString data) {
                        }

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

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

                        @Override
                        public void onInit(ReadHandle rh) {
                            rh.cancel();
                            succeeded.incrementAndGet();
                            latch.countDown();
                        }
                    });
                }

                @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) HttpServerBuilder(com.linkedin.r2.testutils.server.HttpServerBuilder) 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)

Example 98 with Timeout

use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.

the class TestChannelPoolStreamHandler method getChannel.

private static EmbeddedChannel getChannel() {
    EmbeddedChannel ch = new EmbeddedChannel(new RAPStreamResponseDecoder(1000), new RAPStreamResponseHandler(), new ChannelPoolStreamHandler());
    ch.attr(RAPStreamResponseDecoder.TIMEOUT_ATTR_KEY).set(new Timeout<>(Executors.newSingleThreadScheduledExecutor(), 1000, TimeUnit.MILLISECONDS, None.none()));
    ch.attr(RAPStreamResponseHandler.CALLBACK_ATTR_KEY).set(response -> {
        StreamResponse streamResponse = response.getResponse();
        streamResponse.getEntityStream().setReader(new DrainReader());
    });
    return ch;
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader)

Example 99 with Timeout

use of com.linkedin.r2.util.Timeout in project rest.li by linkedin.

the class TestHttp2ProtocolUpgradeHandler method testChannelCloseBeforeUpgrade.

@Test(timeOut = 10000)
@SuppressWarnings("unchecked")
public void testChannelCloseBeforeUpgrade() throws Exception {
    Http2UpgradeHandler handler = new Http2UpgradeHandler();
    EmbeddedChannel channel = new EmbeddedChannel(handler);
    // Reads the upgrade request from the outbound buffer to ensure nothing in the buffer
    Assert.assertEquals(channel.outboundMessages().size(), 1);
    Assert.assertNotNull(channel.readOutbound());
    Assert.assertTrue(channel.outboundMessages().isEmpty());
    RequestWithCallback request = Mockito.mock(RequestWithCallback.class);
    TimeoutAsyncPoolHandle handle = Mockito.mock(TimeoutAsyncPoolHandle.class);
    TimeoutTransportCallback callback = Mockito.mock(TimeoutTransportCallback.class);
    Mockito.when(request.handle()).thenReturn(handle);
    Mockito.when(request.callback()).thenReturn(callback);
    // Write should not succeed before upgrade completes
    Assert.assertFalse(channel.writeOutbound(request));
    Assert.assertFalse(channel.finish());
    // Synchronously waiting for channel to close
    channel.close().sync();
    Mockito.verify(request).handle();
    Mockito.verify(request).callback();
    Mockito.verify(handle).dispose();
    Mockito.verify(callback).onResponse(Mockito.any(TransportResponse.class));
}
Also used : RequestWithCallback(com.linkedin.r2.transport.common.bridge.common.RequestWithCallback) TimeoutTransportCallback(com.linkedin.r2.transport.http.client.TimeoutTransportCallback) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) TransportResponse(com.linkedin.r2.transport.common.bridge.common.TransportResponse) TimeoutAsyncPoolHandle(com.linkedin.r2.transport.http.client.TimeoutAsyncPoolHandle) Test(org.testng.annotations.Test)

Example 100 with Timeout

use of com.linkedin.r2.util.Timeout 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)

Aggregations

Test (org.testng.annotations.Test)78 RequestContext (com.linkedin.r2.message.RequestContext)46 CountDownLatch (java.util.concurrent.CountDownLatch)40 TimeoutException (java.util.concurrent.TimeoutException)40 None (com.linkedin.common.util.None)33 FutureCallback (com.linkedin.common.callback.FutureCallback)32 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)26 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)25 URI (java.net.URI)25 RestRequest (com.linkedin.r2.message.rest.RestRequest)21 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)21 ByteString (com.linkedin.data.ByteString)19 HashMap (java.util.HashMap)19 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)19 RestResponse (com.linkedin.r2.message.rest.RestResponse)18 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)17 ExecutionException (java.util.concurrent.ExecutionException)17 TransportClient (com.linkedin.r2.transport.common.bridge.client.TransportClient)15 IOException (java.io.IOException)15 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)13