Search in sources :

Example 6 with DrainReader

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

the class TestClientTimeout method testReadAfterTimeout.

@Test(dataProvider = "clients")
public void testReadAfterTimeout(Client client) throws Exception {
    StreamRequest request = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, NORMAL_URI)).build(EntityStreams.emptyStream());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<StreamResponse> response = new AtomicReference<StreamResponse>();
    client.streamRequest(request, new Callback<StreamResponse>() {

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

        @Override
        public void onSuccess(StreamResponse result) {
            response.set(result);
            latch.countDown();
        }
    });
    latch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(response.get());
    // let it timeout before we read
    Thread.sleep(5000);
    final AtomicReference<Throwable> throwable = new AtomicReference<Throwable>();
    final CountDownLatch errorLatch = new CountDownLatch(1);
    Reader reader = new DrainReader() {

        @Override
        public void onError(Throwable ex) {
            throwable.set(ex);
            errorLatch.countDown();
        }
    };
    response.get().getEntityStream().setReader(reader);
    errorLatch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(throwable.get());
    Throwable rootCause = ExceptionUtils.getRootCause(throwable.get());
    Assert.assertTrue(rootCause instanceof TimeoutException);
    Assert.assertEquals(rootCause.getMessage(), "Timeout while receiving the response entity.");
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Reader(com.linkedin.r2.message.stream.entitystream.Reader) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test)

Example 7 with DrainReader

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

the class TestJetty404 method testJetty404.

// make sure jetty's default behavior will read all the request bytes in case of 404
@Test
public void testJetty404() throws Exception {
    BytesWriter writer = new BytesWriter(200 * 1024, (byte) 100);
    final AtomicReference<Throwable> exRef = new AtomicReference<Throwable>();
    final CountDownLatch latch = new CountDownLatch(1);
    _client.streamRequest(new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, URI.create("/wrong-path"))).build(EntityStreams.newEntityStream(writer)), new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            exRef.set(e);
            latch.countDown();
        }

        @Override
        public void onSuccess(StreamResponse result) {
            latch.countDown();
        }
    });
    latch.await(5000, TimeUnit.MILLISECONDS);
    Assert.assertTrue(writer.isDone());
    Throwable ex = exRef.get();
    Assert.assertTrue(ex instanceof StreamException);
    StreamResponse response = ((StreamException) ex).getResponse();
    Assert.assertEquals(response.getStatus(), RestStatus.NOT_FOUND);
    response.getEntityStream().setReader(new DrainReader());
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamException(com.linkedin.r2.message.stream.StreamException) Test(org.testng.annotations.Test)

Example 8 with DrainReader

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

the class TestJetty404 method setup.

@BeforeClass
public void setup() throws IOException {
    _clientFactory = new HttpClientFactory();
    _client = new TransportClientAdapter(_clientFactory.getClient(Collections.<String, String>emptyMap()), true);
    _server = new HttpServerFactory().createH2cServer(PORT, "/correct-path", 50, new TransportDispatcher() {

        @Override
        public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
            callback.onResponse(TransportResponseImpl.success(new RestResponseBuilder().build()));
        }

        @Override
        public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
            req.getEntityStream().setReader(new DrainReader());
            callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().build(EntityStreams.emptyStream())));
        }
    }, true);
    _server.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) RequestContext(com.linkedin.r2.message.RequestContext) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) Map(java.util.Map) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) BeforeClass(org.testng.annotations.BeforeClass)

Aggregations

DrainReader (com.linkedin.r2.message.stream.entitystream.DrainReader)8 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)7 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)4 RequestContext (com.linkedin.r2.message.RequestContext)3 RestRequest (com.linkedin.r2.message.rest.RestRequest)3 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)3 StreamRequestHandler (com.linkedin.r2.transport.common.StreamRequestHandler)3 TransportClientAdapter (com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter)3 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)3 TransportDispatcher (com.linkedin.r2.transport.common.bridge.server.TransportDispatcher)3 HttpClientFactory (com.linkedin.r2.transport.http.client.HttpClientFactory)3 HttpServerFactory (com.linkedin.r2.transport.http.server.HttpServerFactory)3 URI (java.net.URI)3 Map (java.util.Map)3 BeforeClass (org.testng.annotations.BeforeClass)3 Test (org.testng.annotations.Test)3 ByteString (com.linkedin.data.ByteString)2 FilterChain (com.linkedin.r2.filter.FilterChain)2 RestResponse (com.linkedin.r2.message.rest.RestResponse)2 StreamException (com.linkedin.r2.message.stream.StreamException)2