Search in sources :

Example 61 with StreamRequestBuilder

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

the class TestStreamResponse method testErrorWhileStreaming.

@Test
public void testErrorWhileStreaming() throws Exception {
    HttpClientFactory clientFactory = new HttpClientFactory();
    Map<String, String> clientProperties = new HashMap<String, String>();
    clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, "1000");
    Client client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), true);
    StreamRequestBuilder builder = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, SERVER_ERROR_URI));
    StreamRequest request = builder.build(EntityStreams.emptyStream());
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<Throwable>();
    final Callback<None> readerCallback = getReaderCallback(latch, error);
    final BytesReader reader = new BytesReader(BYTE, readerCallback);
    Callback<StreamResponse> callback = getCallback(status, readerCallback, reader);
    client.streamRequest(request, callback);
    latch.await(2000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), RestStatus.OK);
    Throwable throwable = error.get();
    Assert.assertNotNull(throwable);
    final FutureCallback<None> clientShutdownCallback = new FutureCallback<None>();
    client.shutdown(clientShutdownCallback);
    clientShutdownCallback.get();
    final FutureCallback<None> factoryShutdownCallback = new FutureCallback<None>();
    clientFactory.shutdown(factoryShutdownCallback);
    factoryShutdownCallback.get();
}
Also used : HashMap(java.util.HashMap) 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) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) Client(com.linkedin.r2.transport.common.Client) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) Test(org.testng.annotations.Test)

Example 62 with StreamRequestBuilder

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

the class TrackerClientTest method testClientStreamRequest.

@Test(groups = { "small", "back-end" })
public void testClientStreamRequest() throws URISyntaxException {
    URI uri = URI.create("http://test.qa.com:1234/foo");
    double weight = 3d;
    TestClient wrappedClient = new TestClient(true);
    Clock clock = new SettableClock();
    Map<Integer, PartitionData> partitionDataMap = new HashMap<Integer, PartitionData>(2);
    partitionDataMap.put(DefaultPartitionAccessor.DEFAULT_PARTITION_ID, new PartitionData(3d));
    TrackerClient client = new TrackerClient(uri, partitionDataMap, wrappedClient, clock, null);
    assertEquals(client.getUri(), uri);
    Double clientWeight = client.getPartitionWeight(DefaultPartitionAccessor.DEFAULT_PARTITION_ID);
    assertEquals(clientWeight, weight);
    assertEquals(client.getWrappedClient(), wrappedClient);
    StreamRequest streamRequest = new StreamRequestBuilder(uri).build(EntityStreams.emptyStream());
    Map<String, String> restWireAttrs = new HashMap<String, String>();
    TestTransportCallback<StreamResponse> restCallback = new TestTransportCallback<StreamResponse>();
    client.streamRequest(streamRequest, new RequestContext(), restWireAttrs, restCallback);
    assertFalse(restCallback.response.hasError());
    assertSame(wrappedClient.streamRequest, streamRequest);
    assertEquals(wrappedClient.restWireAttrs, restWireAttrs);
}
Also used : HashMap(java.util.HashMap) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) Clock(com.linkedin.util.clock.Clock) SettableClock(com.linkedin.util.clock.SettableClock) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PartitionData(com.linkedin.d2.balancer.properties.PartitionData) SettableClock(com.linkedin.util.clock.SettableClock) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 63 with StreamRequestBuilder

use of com.linkedin.r2.message.stream.StreamRequestBuilder 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 64 with StreamRequestBuilder

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

the class TestCompressionEcho method testResponseCompression.

@Test(dataProvider = "compressionEchoData")
public void testResponseCompression(Client client, long bytes) throws InterruptedException, TimeoutException, ExecutionException {
    StreamRequestBuilder builder = new StreamRequestBuilder((Bootstrap.createHttpURI(PORT, ECHO_URI)));
    BytesWriter writer = new BytesWriter(bytes, BYTE);
    StreamRequest request = builder.build(EntityStreams.newEntityStream(writer));
    // add operation to enable sending accept encoding
    RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(R2Constants.OPERATION, "get");
    final FutureCallback<StreamResponse> callback = new FutureCallback<StreamResponse>();
    client.streamRequest(request, requestContext, callback);
    final StreamResponse response = callback.get(60, TimeUnit.SECONDS);
    Assert.assertEquals(response.getStatus(), RestStatus.OK);
    final FutureCallback<None> readerCallback = new FutureCallback<None>();
    final BytesReader reader = new BytesReader(BYTE, readerCallback);
    response.getEntityStream().setReader(reader);
    readerCallback.get(60, TimeUnit.SECONDS);
    Assert.assertEquals(reader.getTotalBytes(), bytes);
    Assert.assertTrue(reader.allBytesCorrect());
}
Also used : StreamResponse(com.linkedin.r2.message.stream.StreamResponse) RequestContext(com.linkedin.r2.message.RequestContext) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) None(com.linkedin.common.util.None) FutureCallback(com.linkedin.common.callback.FutureCallback) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 65 with StreamRequestBuilder

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

the class TestDisruptor method testStreamErrorDisrupt.

@Test
public void testStreamErrorDisrupt() throws Exception {
    final Map<String, String> properties = new HashMap<>();
    properties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(REQUEST_TIMEOUT));
    final TransportClientFactory factory = new HttpClientFactory.Builder().build();
    final TransportClient client = factory.getClient(properties);
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.error(REQUEST_LATENCY));
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicBoolean success = new AtomicBoolean(false);
    client.streamRequest(new StreamRequestBuilder(new URI(REQUEST_URI)).build(EntityStreams.emptyStream()), requestContext, new HashMap<>(), response -> {
        success.set(response.hasError() && response.getError() instanceof DisruptedException);
        latch.countDown();
    });
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Test execution timeout");
    Assert.assertTrue(success.get(), "Unexpected transport response");
}
Also used : TransportClient(com.linkedin.r2.transport.common.bridge.client.TransportClient) HashMap(java.util.HashMap) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DisruptedException(com.linkedin.r2.disruptor.DisruptedException) RequestContext(com.linkedin.r2.message.RequestContext) TransportClientFactory(com.linkedin.r2.transport.common.TransportClientFactory) HttpClientFactory(com.linkedin.r2.transport.http.client.HttpClientFactory) Test(org.testng.annotations.Test)

Aggregations

StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)121 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)106 Test (org.testng.annotations.Test)102 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)87 URI (java.net.URI)68 RequestContext (com.linkedin.r2.message.RequestContext)56 CountDownLatch (java.util.concurrent.CountDownLatch)51 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 Callback (com.linkedin.common.callback.Callback)25 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 ByteString (com.linkedin.data.ByteString)22 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)21 AbstractServiceTest (test.r2.integ.clientserver.providers.AbstractServiceTest)21 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)20 AfterTest (org.testng.annotations.AfterTest)19 BeforeTest (org.testng.annotations.BeforeTest)19 RestRequest (com.linkedin.r2.message.rest.RestRequest)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)18 FutureCallback (com.linkedin.common.callback.FutureCallback)17