Search in sources :

Example 6 with Observer

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

the class TestEntityStream method testReaderThrow.

public void testReaderThrow(ControlReader readerOnData, ControlReader readerOnDone, ControlReader readerOnError, Throwable writerProblem) {
    TestWriter writer = new TestWriter();
    TestObserver observer = new TestObserver();
    EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(readerOnData);
    readerOnData.read(1);
    writer.write();
    writer.done();
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertEquals(observer.getChunkCount(), 1);
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(observer.doneTimes(), 0);
    Assert.assertEquals(readerOnData.getChunkCount(), 1);
    Assert.assertEquals(readerOnData.errorTimes(), 1);
    Assert.assertEquals(readerOnData.doneTimes(), 0);
    writer = new TestWriter();
    observer = new TestObserver();
    es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(readerOnDone);
    readerOnDone.read(1);
    writer.write();
    writer.done();
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertEquals(observer.getChunkCount(), 1);
    Assert.assertEquals(observer.doneTimes(), 1);
    Assert.assertEquals(observer.errorTimes(), 0);
    Assert.assertEquals(readerOnDone.getChunkCount(), 1);
    Assert.assertEquals(readerOnDone.doneTimes(), 1);
    Assert.assertEquals(readerOnDone.errorTimes(), 0);
    writer = new TestWriter();
    observer = new TestObserver();
    es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    es.setReader(readerOnError);
    readerOnError.read(1);
    writer.write();
    writer.error(writerProblem);
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertEquals(observer.getChunkCount(), 1);
    Assert.assertEquals(observer.doneTimes(), 0);
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(readerOnError.getChunkCount(), 1);
    Assert.assertEquals(readerOnError.doneTimes(), 0);
    Assert.assertEquals(readerOnError.errorTimes(), 1);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream)

Example 7 with Observer

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

the class TestEntityStream method testReaderInitErrorThrowInStreaming.

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

        @Override
        public void onWritePossible() {
            super.onWritePossible();
            write();
        }
    };
    TestObserver observer = new TestObserver();
    final EntityStream es = EntityStreams.newEntityStream(writer);
    es.addObserver(observer);
    final ControlReader reader = new ControlReader() {

        @Override
        public void onInit(ReadHandle rh) {
            rh.request(1);
        }

        @Override
        public void onDataAvailable(ByteString data) {
            super.onDataAvailable(data);
            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(), 1);
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(reader.getChunkCount(), 1);
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertEquals(writer.getWritePossibleCount(), 1);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) ByteString(com.linkedin.data.ByteString) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 8 with Observer

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

the class TestEntityStream method testRaceBetweenWriteAndCancel.

private void testRaceBetweenWriteAndCancel(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() > 0) {
                prepareLatch.countDown();
                startLatch.await();
                writer.write();
            }
            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));
    // in any case, reader shouldn't fail
    Assert.assertEquals(reader.errorTimes(), 0);
    Assert.assertEquals(writer.abortedTimes(), 1);
    Assert.assertEquals(reader.getChunkCount(), observer.getChunkCount());
    Assert.assertEquals(observer.errorTimes(), 1);
    Assert.assertEquals(observer.getLastEvent(), "onError");
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with Observer

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

the class TestEntityStream method testReaderInitError.

@Test
public void testReaderInitError() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    final TestWriter writer = new TestWriter();
    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(observer.errorTimes(), 1);
    Assert.assertEquals(writer.abortedTimes(), 1);
}
Also used : EntityStream(com.linkedin.r2.message.stream.entitystream.EntityStream) ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) ExecutorService(java.util.concurrent.ExecutorService) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.testng.annotations.Test)

Example 10 with Observer

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

the class TestClientRequestFinalizerFilter method testStreamRequestOrdering.

@Test
public void testStreamRequestOrdering() {
    final AtomicReference<Observer> observerReference = new AtomicReference<>();
    final AtomicInteger addObserverExecutionOrder = new AtomicInteger(0);
    when(_streamResponse.getEntityStream()).thenReturn(_entityStream);
    doAnswer(invocation -> {
        addObserverExecutionOrder.set(_index.incrementAndGet());
        observerReference.set((Observer) invocation.getArguments()[0]);
        return null;
    }).when(_entityStream).addObserver(anyObject());
    _filterChain.onStreamRequest(null, _requestContext, null);
    registerRequestFinalizer();
    _filterChain.onStreamResponse(_streamResponse, _requestContext, null);
    observerReference.get().onDone();
    Assert.assertEquals(_firstFilter._onRequestExecutionOrder, 1);
    Assert.assertEquals(_lastFilter._onRequestExecutionOrder, 2);
    Assert.assertEquals(_lastFilter._onResponseExecutionOrder, 3);
    Assert.assertEquals(addObserverExecutionOrder.get(), 4, "Expected observer with RequestFinalizer " + "to be added before calling the next filter.");
    Assert.assertEquals(_firstFilter._onResponseExecutionOrder, 5);
    Assert.assertEquals(_testRequestFinalizer._executionOrder, 6, "Expected request finalizer to be " + "executed last.");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Observer(com.linkedin.r2.message.stream.entitystream.Observer) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.testng.annotations.Test)

Aggregations

EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)15 Test (org.testng.annotations.Test)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ByteString (com.linkedin.data.ByteString)8 Observer (com.linkedin.r2.message.stream.entitystream.Observer)7 ScheduledExecutorService (java.util.concurrent.ScheduledExecutorService)6 ExecutorService (java.util.concurrent.ExecutorService)4 ReadHandle (com.linkedin.r2.message.stream.entitystream.ReadHandle)3 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)2 WriteHandle (com.linkedin.r2.message.stream.entitystream.WriteHandle)2 Callback (com.linkedin.common.callback.Callback)1 MultiPartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback)1 SinglePartMIMEFullReaderCallback (com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback)1 RequestContext (com.linkedin.r2.message.RequestContext)1 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)1 BaseConnector (com.linkedin.r2.message.stream.entitystream.BaseConnector)1 ByteStringWriter (com.linkedin.r2.message.stream.entitystream.ByteStringWriter)1 FullEntityObserver (com.linkedin.r2.message.stream.entitystream.FullEntityObserver)1 MessageType (com.linkedin.r2.transport.common.MessageType)1