Search in sources :

Example 36 with EntityStream

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

the class TestEntityStream method testWriterAndReaderInitError.

@Test
public void testWriterAndReaderInitError() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final TestWriter writer = new TestWriter() {

        @Override
        public void onInit(WriteHandle wh) {
            throw new RuntimeException();
        }
    };
    TestObserver observer = new TestObserver();
    final EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    final ControlReader reader = new ControlReader() {

        @Override
        public void onInit(ReadHandle rh) {
            throw new RuntimeException();
        }
    };
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        @Override
        public void run() {
            es.setReader(reader);
            latch.countDown();
        }
    });
    Assert.assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    Assert.assertEquals(reader.errorTimes(), 1);
    Assert.assertEquals(reader.getChunkCount(), 0);
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(reader.getChunkCount(), 0);
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertEquals(writer.getWritePossibleCount(), 0);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) WriteHandle(com.linkedin.r2.message.stream.entitystream.WriteHandle) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 37 with EntityStream

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

the class TestEntityStream method testWriterThrow.

public void testWriterThrow(ControlReader reader, TestWriter writerOnAbort, TestWriter writerOnWritePossible) {
    TestObserver observer = new TestObserver();
    EntityStream es = EntityStreams.newEntityStream(writerOnAbort);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(1);
    writerOnAbort.write();
    writerOnAbort.done();
    Assert.assertEquals(writerOnAbort.abortedTimes(), 1);
    Assert.assertEquals(observer.getChunkCount(), 1);
    Assert.assertEquals(observer.doneTimes(), 1);
    Assert.assertEquals(observer.errorTimes(), 0);
    Assert.assertEquals(reader.getChunkCount(), 1);
    Assert.assertEquals(reader.doneTimes(), 1);
    Assert.assertEquals(reader.errorTimes(), 0);
    reader = new ControlReader();
    observer = new TestObserver();
    es = EntityStreams.newEntityStream(writerOnWritePossible);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(1);
    writerOnWritePossible.write();
    writerOnWritePossible.done();
    Assert.assertEquals(writerOnWritePossible.abortedTimes(), 1);
    Assert.assertEquals(observer.getChunkCount(), 0);
    Assert.assertEquals(observer.doneTimes(), 0);
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(reader.getChunkCount(), 0);
    Assert.assertEquals(reader.doneTimes(), 0);
    Assert.assertEquals(reader.errorTimes(), 1);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream)

Example 38 with EntityStream

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

the class ResponseUtils method buildStreamException.

public static StreamException buildStreamException(RestLiResponseException restLiResponseException, ContentType contentType) {
    RestLiResponse restLiResponse = restLiResponseException.getRestLiResponse();
    StreamResponseBuilder responseBuilder = new StreamResponseBuilder().setHeaders(restLiResponse.getHeaders()).setHeader(RestConstants.HEADER_CONTENT_TYPE, contentType.getHeaderKey()).setCookies(CookieUtil.encodeSetCookies(restLiResponse.getCookies())).setStatus(restLiResponse.getStatus() == null ? HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode() : restLiResponse.getStatus().getCode());
    EntityStream<ByteString> entityStream = contentType.getStreamCodec().encodeMap(restLiResponse.getDataMap());
    StreamResponse response = responseBuilder.build(EntityStreamAdapters.fromGenericEntityStream(entityStream));
    return new StreamException(response, restLiResponseException.getCause());
}
Also used : StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) StreamException(com.linkedin.r2.message.stream.StreamException)

Example 39 with EntityStream

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

the class TestStreamRequest method testBackPressure.

@Test
public void testBackPressure() throws Exception {
    final long totalBytes = SMALL_BYTES_NUM;
    TimedBytesWriter writer = new TimedBytesWriter(totalBytes, BYTE);
    EntityStream entityStream = EntityStreams.newEntityStream(writer);
    StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, RATE_LIMITED_URI));
    StreamRequest request = builder.setMethod("POST").build(entityStream);
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    Callback<StreamResponse> callback = expectSuccessCallback(latch, status);
    _client.streamRequest(request, callback);
    latch.await(60000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), RestStatus.OK);
    TimedBytesReader reader = _rateLimitedRequestHandler.getReader();
    Assert.assertNotNull(reader);
    Assert.assertEquals(totalBytes, reader.getTotalBytes());
    Assert.assertTrue(reader.allBytesCorrect());
    long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
    long serverReceiveTimespan = reader.getStopTime() - reader.getStartTime();
    Assert.assertTrue(serverReceiveTimespan > 1000);
    double diff = Math.abs(serverReceiveTimespan - clientSendTimespan);
    double diffRatio = diff / clientSendTimespan;
    // make it generous to reduce the chance occasional test failures
    Assert.assertTrue(diffRatio < 0.2);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TimedBytesWriter(test.r2.integ.helper.TimedBytesWriter) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) TimedBytesReader(test.r2.integ.helper.TimedBytesReader) 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 40 with EntityStream

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

the class TestStreamRequest method test404.

// jetty 404 tests singled out
@Test(enabled = false)
public void test404() throws Exception {
    final long totalBytes = TINY_BYTES_NUM;
    EntityStream entityStream = EntityStreams.newEntityStream(new BytesWriter(totalBytes, BYTE));
    StreamRequestBuilder builder = new StreamRequestBuilder(_clientProvider.createHttpURI(_port, URI.create("/boo")));
    StreamRequest request = builder.setMethod("POST").build(entityStream);
    final AtomicInteger status = new AtomicInteger(-1);
    final CountDownLatch latch = new CountDownLatch(1);
    Callback<StreamResponse> callback = expectErrorCallback(latch, status);
    _client.streamRequest(request, callback);
    latch.await(60000, TimeUnit.MILLISECONDS);
    Assert.assertEquals(status.get(), 404);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) TimedBytesWriter(test.r2.integ.helper.TimedBytesWriter) BytesWriter(test.r2.integ.helper.BytesWriter) 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