Search in sources :

Example 1 with ReadHandle

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

the class TestServerTimeout method testServerTimeout.

@Test
public void testServerTimeout() throws Exception {
    final StreamRequest request = new StreamRequestBuilder(Bootstrap.createHttpURI(PORT, BUGGY_SERVER_URI)).build(EntityStreams.emptyStream());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger status = new AtomicInteger(-1);
    _client.streamRequest(request, new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            latch.countDown();
        }

        @Override
        public void onSuccess(StreamResponse result) {
            status.set(result.getStatus());
            result.getEntityStream().setReader(new Reader() {

                private ReadHandle _rh;

                @Override
                public void onInit(ReadHandle rh) {
                    _rh = rh;
                    _rh.request(Integer.MAX_VALUE);
                }

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

                @Override
                public void onDone() {
                    // server would close the connection if TimeoutException, and netty would end the chunked transferring
                    // with an empty chunk
                    latch.countDown();
                }

                @Override
                public void onError(Throwable e) {
                    latch.countDown();
                }
            });
        }
    });
    // server should timeout so await should return true
    Assert.assertTrue(latch.await(SERVER_IOHANDLER_TIMEOUT * 2, TimeUnit.MILLISECONDS));
    Assert.assertEquals(status.get(), RestStatus.OK);
}
Also used : ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) Reader(com.linkedin.r2.message.stream.entitystream.Reader) CountDownLatch(java.util.concurrent.CountDownLatch) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test)

Example 2 with ReadHandle

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

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

the class TestEntityStream method testNoStackOverflow.

@Test
public void testNoStackOverflow() throws Exception {
    Writer dumbWriter = new Writer() {

        WriteHandle _wh;

        long _count = 0;

        final int _total = 1024 * 1024 * 1024;

        @Override
        public void onInit(WriteHandle wh) {
            _wh = wh;
        }

        @Override
        public void onWritePossible() {
            while (_wh.remaining() > 0 && _count < _total) {
                byte[] bytes = new byte[(int) Math.min(4096, _total - _count)];
                _wh.write(ByteString.copy(bytes));
                _count += bytes.length;
            }
            if (_count >= _total) {
                _wh.done();
            }
        }

        @Override
        public void onAbort(Throwable ex) {
        // do nothing
        }
    };
    Reader dumbReader = new Reader() {

        ReadHandle _rh;

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

        @Override
        public void onDataAvailable(ByteString data) {
            _rh.request(1);
        }

        @Override
        public void onDone() {
        }

        @Override
        public void onError(Throwable e) {
        }
    };
    EntityStreams.newEntityStream(dumbWriter).setReader(dumbReader);
}
Also used : ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) WriteHandle(com.linkedin.r2.message.stream.entitystream.WriteHandle) ByteString(com.linkedin.data.ByteString) Reader(com.linkedin.r2.message.stream.entitystream.Reader) Writer(com.linkedin.r2.message.stream.entitystream.Writer) Test(org.testng.annotations.Test)

Example 4 with ReadHandle

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

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

the class TestServerTimeout method testServerTimeout.

@Test
public void testServerTimeout() throws Exception {
    final StreamRequest request = new StreamRequestBuilder(getHttpUri(BUGGY_SERVER_URI)).build(EntityStreams.emptyStream());
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger status = new AtomicInteger(-1);
    _client.streamRequest(request, new Callback<StreamResponse>() {

        @Override
        public void onError(Throwable e) {
            latch.countDown();
        }

        @Override
        public void onSuccess(StreamResponse result) {
            status.set(result.getStatus());
            result.getEntityStream().setReader(new Reader() {

                private ReadHandle _rh;

                @Override
                public void onInit(ReadHandle rh) {
                    _rh = rh;
                    _rh.request(Integer.MAX_VALUE);
                }

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

                @Override
                public void onDone() {
                    // server would close the connection if TimeoutException, and netty would end the chunked transferring
                    // with an empty chunk
                    latch.countDown();
                }

                @Override
                public void onError(Throwable e) {
                    latch.countDown();
                }
            });
        }
    });
    // server should timeout so await should return true
    Assert.assertTrue(latch.await(SERVER_IOHANDLER_TIMEOUT * 2, TimeUnit.MILLISECONDS));
    Assert.assertEquals(status.get(), RestStatus.OK);
}
Also used : ReadHandle(com.linkedin.r2.message.stream.entitystream.ReadHandle) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteString(com.linkedin.data.ByteString) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) Reader(com.linkedin.r2.message.stream.entitystream.Reader) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) 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

ReadHandle (com.linkedin.r2.message.stream.entitystream.ReadHandle)15 CountDownLatch (java.util.concurrent.CountDownLatch)13 Test (org.testng.annotations.Test)13 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)10 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)10 ByteString (com.linkedin.data.ByteString)9 StreamRequestBuilder (com.linkedin.r2.message.stream.StreamRequestBuilder)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 Reader (com.linkedin.r2.message.stream.entitystream.Reader)7 AtomicReference (java.util.concurrent.atomic.AtomicReference)7 None (com.linkedin.common.util.None)6 RequestContext (com.linkedin.r2.message.RequestContext)4 EntityStream (com.linkedin.r2.message.stream.entitystream.EntityStream)4 HttpServerBuilder (com.linkedin.r2.testutils.server.HttpServerBuilder)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 AbstractServiceTest (test.r2.integ.clientserver.providers.AbstractServiceTest)3