Search in sources :

Example 16 with Observer

use of com.linkedin.r2.message.stream.entitystream.Observer 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)

Example 17 with Observer

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

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

the class TestEntityStream method testObserversThrowUncheckedError.

@Test
public void testObserversThrowUncheckedError() {
    Observer observer = new TestObserver() {

        @Override
        public void onDone() {
            throw new Error("broken observer throws");
        }

        @Override
        public void onDataAvailable(ByteString data) {
            throw new Error("broken observer throws");
        }

        @Override
        public void onError(Throwable ex) {
            throw new Error("broken observer throws");
        }
    };
    Error ex = new Error("writer has problem");
    testObserverThrow(observer, ex);
}
Also used : ByteString(com.linkedin.data.ByteString) Observer(com.linkedin.r2.message.stream.entitystream.Observer) Test(org.testng.annotations.Test)

Example 19 with Observer

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

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

the class ClientRequestFinalizerFilter method onStreamResponse.

@Override
public void onStreamResponse(StreamResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<StreamRequest, StreamResponse> nextFilter) {
    res.getEntityStream().addObserver(new Observer() {

        @Override
        public void onDataAvailable(ByteString data) {
        // do nothing
        }

        @Override
        public void onDone() {
            doFinalizeRequest(requestContext, res, null);
        }

        @Override
        public void onError(Throwable e) {
            doFinalizeRequest(requestContext, res, e);
        }
    });
    nextFilter.onResponse(res, requestContext, wireAttrs);
}
Also used : ByteString(com.linkedin.data.ByteString) Observer(com.linkedin.r2.message.stream.entitystream.Observer)

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