Search in sources :

Example 31 with EntityStream

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

the class TestEntityStream method testEntityStream.

@Test
public void testEntityStream() throws Exception {
    TestWriter writer = new TestWriter();
    ControlReader reader = new ControlReader();
    TestObserver ob1 = new TestObserver();
    TestObserver ob2 = new TestObserver();
    EntityStream stream = EntityStreams.newEntityStream(writer);
    stream.addObserver(ob1);
    stream.addObserver(ob2);
    // write is not possible without a reader
    Assert.assertEquals(writer.getWritePossibleCount(), 0);
    stream.setReader(reader);
    // write is not possible before reader reads
    Assert.assertEquals(writer.getWritePossibleCount(), 0);
    reader.read(1);
    // write become possible
    Assert.assertEquals(writer.getWritePossibleCount(), 1);
    Assert.assertEquals(writer.remaining(), 1);
    writer.write();
    Assert.assertEquals(writer.remaining(), 0);
    reader.read(10);
    // write again become possible
    Assert.assertEquals(writer.getWritePossibleCount(), 2);
    Assert.assertEquals(writer.remaining(), 10);
    while (writer.remaining() > 1) {
        writer.write();
    }
    Assert.assertEquals(writer.remaining(), 1);
    reader.read(10);
    // write hasn't become impossible when reader reads again, so onWritePossible should not have been invoked again
    Assert.assertEquals(writer.getWritePossibleCount(), 2);
    while (writer.remaining() > 0) {
        writer.write();
    }
    Assert.assertEquals(ob1.getChunkCount(), 21);
    Assert.assertEquals(ob2.getChunkCount(), 21);
    Assert.assertEquals(reader.getChunkCount(), 21);
    try {
        writer.write();
        Assert.fail("should fail with IllegalStateException");
    } catch (IllegalStateException ex) {
    // expected
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) Test(org.testng.annotations.Test)

Example 32 with EntityStream

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

the class TestEntityStream method testCancelWhenNotWritePossible.

@Test
public void testCancelWhenNotWritePossible() throws Exception {
    TestWriter writer = new TestWriter();
    TestObserver observer = new TestObserver();
    final ControlReader reader = new ControlReader();
    EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(10);
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    final CountDownLatch latch = new CountDownLatch(1);
    scheduler.schedule(new Runnable() {

        @Override
        public void run() {
            reader.cancel();
            latch.countDown();
        }
    }, 50, TimeUnit.MILLISECONDS);
    while (writer.remaining() > 0) {
        writer.write();
    }
    Assert.assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertEquals(observer.getChunkCount(), 10);
    Assert.assertEquals(observer.doneTimes(), 0);
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(observer.getLastEvent(), "onError");
    Assert.assertEquals(reader.getChunkCount(), 10);
    Assert.assertEquals(reader.doneTimes(), 0);
    Assert.assertEquals(reader.errorTimes(), 0);
    scheduler.shutdown();
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 33 with EntityStream

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

the class TestEntityStream method testWriterInitError.

@Test
public void testWriterInitError() 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();
    ExecutorService executor = Executors.newFixedThreadPool(1);
    executor.submit(new Runnable() {

        @Override
        public void run() {
            es.setReader(reader);
            reader.read(5);
            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) 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 34 with EntityStream

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

the class TestEntityStream method testRaceBetweenErrorAndCancel.

private void testRaceBetweenErrorAndCancel(ExecutorService executor) throws Exception {
    final CountDownLatch startLatch = new CountDownLatch(1);
    final CountDownLatch finishLatch = new CountDownLatch(2);
    final CountDownLatch prepareLatch = new CountDownLatch(2);
    final TestWriter writer = new TestWriter();
    TestObserver observer = new TestObserver();
    final ControlReader reader = new ControlReader();
    EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(1000);
    executor.submit(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            while (writer.remaining() > 100) {
                writer.write();
            }
            prepareLatch.countDown();
            startLatch.await();
            writer.error(new RuntimeException("writer has problem"));
            finishLatch.countDown();
            return null;
        }
    });
    executor.submit(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            prepareLatch.countDown();
            startLatch.await();
            reader.cancel();
            finishLatch.countDown();
            return null;
        }
    });
    prepareLatch.await();
    startLatch.countDown();
    Assert.assertTrue(finishLatch.await(1000, TimeUnit.MILLISECONDS));
    // if error wins the race
    if (reader.errorTimes() > 0) {
        Assert.assertEquals(reader.doneTimes(), 0);
        Assert.assertEquals(observer.doneTimes(), 0);
        Assert.assertEquals(observer.errorTimes(), 1);
        Assert.assertEquals(writer.abortedTimes(), 0);
    } else // if cancel wins the race
    {
        Assert.assertEquals(observer.doneTimes(), 0);
        Assert.assertEquals(observer.errorTimes(), 1);
        Assert.assertEquals(writer.abortedTimes(), 1);
        Assert.assertEquals(reader.doneTimes(), 0);
        Assert.assertEquals(reader.errorTimes(), 0);
    }
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 35 with EntityStream

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

the class TestEntityStream method testCancelSimple.

@Test
public void testCancelSimple() throws Exception {
    TestWriter writer = new TestWriter();
    TestObserver observer = new TestObserver();
    final ControlReader reader = new ControlReader();
    EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(reader);
    reader.read(1000);
    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    scheduler.schedule(new Runnable() {

        @Override
        public void run() {
            reader.cancel();
        }
    }, 50, TimeUnit.MILLISECONDS);
    while (writer.remaining() > 0) {
        writer.write();
        Thread.sleep(1);
    }
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertTrue(observer.getChunkCount() < 100);
    Assert.assertEquals(observer.doneTimes(), 0);
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(observer.getLastEvent(), "onError");
    Assert.assertTrue(reader.getChunkCount() < 100);
    Assert.assertEquals(reader.doneTimes(), 0);
    Assert.assertEquals(reader.errorTimes(), 0);
    scheduler.shutdown();
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) Test(org.testng.annotations.Test)

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