Search in sources :

Example 41 with EntityStream

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

the class TestStreamRequest method testErrorWriter.

@Test
public void testErrorWriter() throws Exception {
    final long totalBytes = SMALL_BYTES_NUM;
    EntityStream entityStream = EntityStreams.newEntityStream(new ErrorWriter(totalBytes, BYTE));
    StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, FOOBAR_URI));
    StreamRequest request = builder.setMethod("POST").build(entityStream);
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicReference<Throwable> error = new AtomicReference<>();
    Callback<StreamResponse> callback = new Callback<StreamResponse>() {

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

        @Override
        public void onSuccess(StreamResponse result) {
            latch.countDown();
        }
    };
    _client.streamRequest(request, callback);
    latch.await();
    Assert.assertNotNull(error.get());
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) Callback(com.linkedin.common.callback.Callback) 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) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 42 with EntityStream

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

the class TestStreamingTimeout method testStreamTimeoutWithStreamingTimeoutInServerStream.

@Test
public void testStreamTimeoutWithStreamingTimeoutInServerStream() throws Exception {
    final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(SMALL_BYTES_NUM, BYTE));
    final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, RATE_LIMITED_URI));
    final StreamRequest request = builder.setMethod("POST").build(entityStream);
    final AtomicReference<Throwable> throwable = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final Callback<StreamResponse> callback = expectErrorCallback(latch, throwable);
    _client.streamRequest(request, callback);
    latch.await(HTTP_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(throwable.get());
    final Throwable rootCause = ExceptionUtils.getRootCause(throwable.get());
    Assert.assertTrue(rootCause instanceof TimeoutException);
    final TimeoutException timeoutException = (TimeoutException) rootCause;
    Assert.assertEquals(timeoutException.getMessage(), String.format(StreamingTimeout.STREAMING_TIMEOUT_MESSAGE, HTTP_STREAMING_TIMEOUT));
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) BytesWriter(test.r2.integ.helper.BytesWriter) 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) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 43 with EntityStream

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

the class TestStreamingTimeout method testStreamTimeoutWhenGreaterThanRequestTimeout.

@Test
public void testStreamTimeoutWhenGreaterThanRequestTimeout() throws Exception {
    final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(SMALL_BYTES_NUM, BYTE));
    final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, RATE_LIMITED_URI));
    final StreamRequest request = builder.setMethod("POST").build(entityStream);
    final AtomicReference<Throwable> throwable = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final Callback<StreamResponse> callback = expectErrorCallback(latch, throwable);
    Map<String, Object> clientProperties = getHttpClientProperties();
    clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(HTTP_STREAMING_TIMEOUT));
    clientProperties.put(HttpClientFactory.HTTP_STREAMING_TIMEOUT, String.valueOf(HTTP_STREAMING_TIMEOUT));
    Client client = _clientProvider.createClient(getClientFilterChain(), clientProperties);
    client.streamRequest(request, callback);
    latch.await(HTTP_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(throwable.get());
    final Throwable rootCause = ExceptionUtils.getRootCause(throwable.get());
    Assert.assertTrue(rootCause instanceof TimeoutException);
    final TimeoutException timeoutException = (TimeoutException) rootCause;
    Assert.assertEquals(timeoutException.getMessage(), String.format(REQUEST_TIMEOUT_MESSAGE, HTTP_STREAMING_TIMEOUT));
    tearDown(client);
}
Also used : BytesWriter(test.r2.integ.helper.BytesWriter) 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) EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) Client(com.linkedin.r2.transport.common.Client) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 44 with EntityStream

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

the class TestStreamingTimeout method testStreamTimeoutWithStreamTimeoutInClientStream.

@Test
public void testStreamTimeoutWithStreamTimeoutInClientStream() throws Exception {
    final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(LARGE_BYTES_NUM, BYTE) {

        int count = 2;

        @Override
        protected void afterWrite(WriteHandle wh, long written) {
            count = count * 2;
            long delay = Math.min(count, HTTP_STREAMING_TIMEOUT);
            try {
                Thread.sleep(delay);
            } catch (Exception ex) {
            // Do Nothing
            }
        }
    });
    final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, NON_RATE_LIMITED_URI));
    final StreamRequest request = builder.setMethod("POST").build(entityStream);
    final AtomicReference<Throwable> throwable = new AtomicReference<>();
    final CountDownLatch latch = new CountDownLatch(1);
    final Callback<StreamResponse> callback = expectErrorCallback(latch, throwable);
    _client.streamRequest(request, callback);
    latch.await(30000, TimeUnit.MILLISECONDS);
    Assert.assertNotNull(throwable.get());
    final Throwable rootCause = ExceptionUtils.getRootCause(throwable.get());
    Assert.assertTrue(rootCause instanceof TimeoutException);
    final TimeoutException timeoutException = (TimeoutException) rootCause;
    Assert.assertEquals(timeoutException.getMessage(), String.format(StreamingTimeout.STREAMING_TIMEOUT_MESSAGE, HTTP_STREAMING_TIMEOUT));
}
Also used : BytesWriter(test.r2.integ.helper.BytesWriter) WriteHandle(com.linkedin.r2.message.stream.entitystream.WriteHandle) 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) TimeoutException(java.util.concurrent.TimeoutException) RestException(com.linkedin.r2.message.rest.RestException) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) TimeoutException(java.util.concurrent.TimeoutException) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Example 45 with EntityStream

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

the class TestStreamingTimeout method testStreamSuccessWithoutStreamingTimeout.

@Test
public void testStreamSuccessWithoutStreamingTimeout() throws Exception {
    final long totalBytes = TINY_BYTES_NUM;
    final EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
    final StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, NON_RATE_LIMITED_URI));
    final StreamRequest request = builder.setMethod("POST").build(entityStream);
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    final Callback<StreamResponse> callback = expectSuccessCallback(latch, status);
    _client.streamRequest(request, callback);
    latch.await(HTTP_REQUEST_TIMEOUT, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), RestStatus.OK);
    final BytesReader reader = _requestHandler.getReader();
    Assert.assertNotNull(reader);
    Assert.assertEquals(totalBytes, reader.getTotalBytes());
    Assert.assertTrue(reader.allBytesCorrect());
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) BytesWriter(test.r2.integ.helper.BytesWriter) BytesReader(test.r2.integ.helper.BytesReader) TimedBytesReader(test.r2.integ.helper.TimedBytesReader) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) AbstractServiceTest(test.r2.integ.clientserver.providers.AbstractServiceTest)

Aggregations

EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)58 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)29 Test (org.testng.annotations.Test)29 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)27 CountDownLatch (java.util.concurrent.CountDownLatch)27 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)22 ByteString (com.linkedin.data.ByteString)11 StreamResponseBuilder (com.linkedin.r2.message.stream.StreamResponseBuilder)10 FutureCallback (com.linkedin.common.callback.FutureCallback)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 AbstractServiceTest (test.r2.integ.clientserver.providers.AbstractServiceTest)9 AtomicReference (java.util.concurrent.atomic.AtomicReference)8 Callback (com.linkedin.common.callback.Callback)7 Client (com.linkedin.r2.transport.common.Client)7 BytesWriter (test.r2.integ.helper.BytesWriter)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 TimeoutException (java.util.concurrent.TimeoutException)6 StreamingCompressor (com.linkedin.r2.filter.compression.streaming.StreamingCompressor)5 None (com.linkedin.common.util.None)4 StreamEncodingType (com.linkedin.r2.filter.compression.streaming.StreamEncodingType)4