Search in sources :

Example 11 with ReadHandle

use of com.linkedin.r2.message.stream.entitystream.ReadHandle 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 12 with ReadHandle

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

the class TestStreamEcho method testBackPressureEcho.

@Test
public void testBackPressureEcho() throws Exception {
    for (Client client : clients()) {
        TimedBytesWriter writer = new TimedBytesWriter(SMALL_BYTES_NUM, BYTE);
        StreamRequest request = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, ECHO_URI)).build(EntityStreams.newEntityStream(writer));
        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 TimedBytesReader reader = new TimedBytesReader(BYTE, readerCallback) {

            int count = 0;

            @Override
            protected void requestMore(final ReadHandle rh) {
                count++;
                if (count % 16 == 0) {
                    _scheduler.schedule(new Runnable() {

                        @Override
                        public void run() {
                            rh.request(1);
                        }
                    }, INTERVAL, TimeUnit.MILLISECONDS);
                } else {
                    rh.request(1);
                }
            }
        };
        Callback<StreamResponse> callback = getCallback(status, readerCallback, reader);
        client.streamRequest(request, callback);
        latch.await(60000, TimeUnit.MILLISECONDS);
        Assert.assertNull(error.get());
        Assert.assertEquals(status.get(), RestStatus.OK);
        Assert.assertEquals(reader.getTotalBytes(), SMALL_BYTES_NUM);
        Assert.assertTrue(reader.allBytesCorrect());
        long clientSendTimespan = writer.getStopTime() - writer.getStartTime();
        long clientReceiveTimespan = reader.getStopTime() - reader.getStartTime();
        double diff = Math.abs(clientReceiveTimespan - clientSendTimespan);
        double diffRatio = diff / clientSendTimespan;
        // make it generous to reduce the chance occasional test failures
        Assert.assertTrue(diffRatio < 0.5, "Send/receive time delta is " + diff + " but expected to be less than 0.5. Send time span is " + clientSendTimespan + " and receive time span is " + clientReceiveTimespan);
    }
}
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) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Client(com.linkedin.r2.transport.common.Client) None(com.linkedin.common.util.None) Test(org.testng.annotations.Test)

Aggregations

ReadHandle (com.linkedin.r2.message.stream.entitystream.ReadHandle)12 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.testng.annotations.Test)10 ByteString (com.linkedin.data.ByteString)8 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)7 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)7 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)6 Reader (com.linkedin.r2.message.stream.entitystream.Reader)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 None (com.linkedin.common.util.None)4 RequestContext (com.linkedin.r2.message.RequestContext)4 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)4 URI (java.net.URI)4 Server (org.eclipse.jetty.server.Server)4 FutureCallback (com.linkedin.common.callback.FutureCallback)3 ExecutorService (java.util.concurrent.ExecutorService)3 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)3 Callback (com.linkedin.common.callback.Callback)2 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)2